1<?php
2
3/**
4 * zenpage page class
5 *
6 * @author Malte Müller (acrylian)
7 * @package plugins
8 * @subpackage zenpage
9 */
10class ZenpagePage extends ZenpageItems {
11
12	public $manage_rights = MANAGE_ALL_PAGES_RIGHTS;
13	public $manage_some_rights = ZENPAGE_PAGES_RIGHTS;
14	public $view_rights = ALL_PAGES_RIGHTS;
15	public $parent = null;
16	public $parents = null;
17	protected $is_public = null;
18
19	function __construct($titlelink, $allowCreate = NULL) {
20		if (is_array($titlelink)) {
21			$titlelink = $titlelink['titlelink'];
22		}
23		$new = $this->instantiate('pages', array('titlelink' => $titlelink), 'titlelink', true, empty($titlelink), $allowCreate);
24		$this->exists = $this->loaded;
25	}
26
27	/**
28	 * Returns the sort order
29	 *
30	 * @return string
31	 */
32	function getSortOrder() {
33		return $this->get('sort_order');
34	}
35
36	/**
37	 * Stores the sort order
38	 *
39	 * @param string $sortorder image sort order
40	 */
41	function setSortOrder($sortorder) {
42		$this->set('sort_order', $sortorder);
43	}
44
45	/**
46	 * Sets a default sortorder for a page.
47	 *
48	 * Use this before save()
49	 *
50	 * a) If you created an new item after you set a parentid and no new specific sortorder
51	 * b) You updated the parentid without setting specific new sortorder
52	 *
53	 * The sortorder takes care of existing categories on the level and adds the item after existing ones.
54	 *
55	 * @since ZenphotoCMS 1.5.8
56	 */
57	function setDefaultSortorder() {
58		$default = $this->getDefaultSortorder();
59		$this->setSortorder($default);
60	}
61
62	/**
63	 * Gets a default sortorder for a page.
64	 *
65	 * Use this before save()
66	 *
67	 * a) If you created an new item after you set a parentid and no new specific sortorder
68	 * b) You updated the parentid without setting specific new sortorder
69	 *
70	 * The sortorder takes care of existing categories on the level and adds the item after existing ones.
71	 *
72	 * @since ZenphotoCMS 1.5.8
73	 *
74	 * @global obj $_zp_zenpage
75	 * @return string
76	 */
77	function getDefaultSortorder() {
78		global $_zp_zenpage;
79		return $_zp_zenpage->getItemDefaultSortorder('page', $this->getParentID());
80	}
81
82	/**
83	 * Returns the guest user
84	 *
85	 * @return string
86	 */
87	function getUser() {
88		return $this->get('user');
89	}
90
91	/**
92	 * Sets the guest user
93	 *
94	 * @param string $user
95	 */
96	function setUser($user) {
97		$this->set('user', $user);
98	}
99
100	/**
101	 * Returns the password
102	 *
103	 * @return string
104	 */
105	function getPassword() {
106		if (GALLERY_SECURITY != 'public') {
107			return NULL;
108		} else {
109			return $this->get('password');
110		}
111	}
112
113	/**
114	 * Sets the encrypted password
115	 *
116	 * @param string $pwd the cleartext password
117	 */
118	function setPassword($pwd) {
119		$this->set('password', $pwd);
120	}
121
122	/**
123	 * Returns the password hint
124	 *
125	 * @return string
126	 */
127	function getPasswordHint($locale = NULL) {
128		$text = ($this->get('password_hint'));
129		if ($locale !== 'all') {
130			$text = get_language_string($text, $locale);
131		}
132		$text = unTagURLs($text);
133		return $text;
134	}
135
136	/**
137	 * Sets the password hint
138	 *
139	 * @param string $hint the hint text
140	 */
141	function setPasswordHint($hint) {
142		$this->set('password_hint', tagURLs($hint));
143	}
144
145	/**
146	 * duplicates an article
147	 * @param string $newtitle the title for the new article
148	 */
149	function copy($newtitle) {
150		$newID = $newtitle;
151		$id = parent::copy(array('titlelink' => $newID));
152		if (!$id) {
153			$newID = $newtitle . ':' . seoFriendly(date('Y-m-d_H-i-s'));
154			$id = parent::copy(array('titlelink' => $newID));
155		}
156		if ($id) {
157			$newobj = new ZenpagePage($newID);
158			$newobj->setTitle($newtitle);
159			$newobj->setSortOrder(NULL);
160			$newobj->setTags($this->getTags());
161			$newobj->setDateTime(date('Y-m-d H:i:s'));
162			$newobj->setShow(0);
163			$newobj->save();
164			return $newobj;
165		}
166		return false;
167	}
168
169	/**
170	 * Deletes a page (and also if existing its subpages) from the database
171	 *
172	 */
173	function remove() {
174		if ($success = parent::remove()) {
175			$sortorder = $this->getSortOrder();
176			if ($this->id) {
177				$success = $success && query("DELETE FROM " . prefix('obj_to_tag') . "WHERE `type`='pages' AND `objectid`=" . $this->id);
178				$success = $success && query("DELETE FROM " . prefix('comments') . " WHERE ownerid = " . $this->getID() . ' AND type="pages"'); // delete any comments
179				//	remove subpages
180				$mychild = strlen($sortorder) + 4;
181				$result = query_full_array('SELECT * FROM ' . prefix('pages') . " WHERE `sort_order` like '" . $sortorder . "-%'");
182				if (is_array($result)) {
183					foreach ($result as $row) {
184						if (strlen($row['sort_order']) == $mychild) {
185							$subpage = new ZenpagePage($row['titlelink']);
186							$success = $success && $subpage->remove();
187						}
188					}
189				}
190			}
191		}
192		return $success;
193	}
194
195	/**
196	 * Gets the parent page object based on the parentid set
197	 *
198	 * @since Zenphoto 1.5.5
199	 *
200	 * @return obj|null
201	 */
202	function getParent() {
203		if (is_null($this->parent)) {
204			$parentid = $this->getParentID();
205			$obj = getItembyID('pages', $parentid);
206			if ($obj) {
207				return $obj;
208			}
209		} else {
210			return $this->parent;
211		}
212		return null;
213	}
214
215	/**
216	 * Gets the parent pages' titlelinks recursivly to the page
217	 *
218	 * @return array
219	 */
220	function getParents() {
221		if (func_num_args() != 0) {
222			debuglog(gettext('class ZenpagePage getParents(): The parameters $parentid and $initparents have been removed in Zenphoto 1.5.5.'));
223		}
224		if (is_null($this->parents)) {
225			$parents = array();
226			$page = $this;
227			while (!is_null($page = $page->getParent())) {
228				array_unshift($parents, $page->getTitlelink());
229			}
230			return $this->parents = $parents;
231		} else {
232			return $this->parents;
233		}
234	}
235
236	/**
237	 * Gets the sub pages of a page
238	 * @param bool $published TRUE for published or FALSE for all pages including un-published
239	 * @param bool $directchilds Default true to get only the direct sub level pages, set to false to get all levels
240	 * @param int $number number of pages to get (NULL by default for all)
241	 * @param string $sorttype NULL for the standard order as sorted on the backend, "title", "date", "popular", "mostrated", "toprated", "random"
242	 * @param string $sortdirection false for ascending, true for descending
243	 * @param string $author Optional author name to get the pages of
244	 * @return array
245	 */
246	function getPages($published = NULL, $directchilds = true, $number = NULL, $sorttype = NULL, $sortdirection = NULL, $author = null) {
247		global $_zp_zenpage;
248		return $_zp_zenpage->getPages($published, $directchilds, $number, $sorttype, $sortdirection, $author, $this);
249	}
250
251	/**
252	 * Checks if user is allowed to access the page
253	 * @param $hint
254	 * @param $show
255	 */
256	function checkforGuest(&$hint = NULL, &$show = NULL) {
257		if (!parent::checkForGuest()) {
258			return false;
259		}
260		$pageobj = $this;
261		$hash = $pageobj->getPassword();
262		while (empty($hash) && !is_null($pageobj)) {
263			$parentID = $pageobj->getParentID();
264			if (empty($parentID)) {
265				$pageobj = NULL;
266			} else {
267				$sql = 'SELECT `titlelink` FROM ' . prefix('pages') . ' WHERE `id`=' . $parentID;
268				$result = query_single_row($sql);
269				$pageobj = new ZenpagePage($result['titlelink']);
270				$hash = $pageobj->getPassword();
271			}
272		}
273		if (empty($hash)) { // no password required
274			return 'zp_public_access';
275		} else {
276			$authType = "zp_page_auth_" . $pageobj->getID();
277			$saved_auth = zp_getCookie($authType);
278			if ($saved_auth == $hash) {
279				return $authType;
280			} else {
281				$user = $pageobj->getUser();
282				$show = (!empty($user));
283				$hint = $pageobj->getPasswordHint();
284				return false;
285			}
286		}
287	}
288
289	/**
290	 * Checks if a page is protected and returns TRUE or FALSE
291	 * NOTE: This function does only check if a password is set not if it has been entered! Use $this->checkforGuest() for that.
292	 *
293	 * @return bool
294	 */
295	function isProtected() {
296		return $this->checkforGuest() != 'zp_public_access';
297	}
298
299	/**
300	 * Returns true if this page is published and also all of its parents.
301	 *
302	 * @since Zenphoto 1.5.5
303	 *
304	 * @return bool
305	 */
306	function isPublic() {
307		if (is_null($this->is_public)) {
308			if (!$this->isPublished()) {
309				return $this->is_public = false;
310			}
311			$parent = $this->getParent();
312			if($parent && !$parent->isPublic()) {
313				return $this->is_public = false;
314			}
315			return $this->is_public = true;
316		} else {
317			return $this->is_public;
318		}
319	}
320
321	/**
322	 * Checks if user is author of page
323	 * @param bit $action what the caller wants to do
324	 *
325	 * returns true of access is allowed
326	 */
327	function isMyItem($action) {
328		global $_zp_current_admin_obj;
329		if (parent::isMyItem($action)) {
330			return true;
331		}
332		if (zp_loggedin($action)) {
333			if (GALLERY_SECURITY != 'public' && $this->isPublic() && $action == LIST_RIGHTS) {
334				return LIST_RIGHTS;
335			}
336			if ($_zp_current_admin_obj->getUser() == $this->getAuthor()) {
337				return true;
338			}
339			$mypages = $_zp_current_admin_obj->getObjects('pages');
340			if (!empty($mypages)) {
341				if (array_search($this->getTitlelink(), $mypages) !== false) {
342					return true;
343				}
344			}
345		}
346		return false;
347	}
348
349	/**
350	 * Returns full path to a specific page
351	 *
352	 * @return string
353	 */
354	function getLink() {
355		return zp_apply_filter('getLink', rewrite_path(_PAGES_ . '/' . $this->getTitlelink() . '/', '/index.php?p=pages&title=' . $this->getTitlelink()), $this, NULL);
356	}
357
358}
359
360?>