1<?php
2/**
3 * The basic ThemeObject class. Extends PersistentObject, is extended by various Theme related objects.
4 * Provides some basic methods that all use.
5 *
6 * @package core
7 * @subpackage classes\objects
8 */
9class ThemeObject extends PersistentObject {
10
11	private $commentcount; //Contains the number of comments
12	public $comments = NULL; //Contains an array of the comments of the object
13	public $manage_rights = ADMIN_RIGHTS;
14	public $manage_some_rights = ADMIN_RIGHTS;
15	public $view_rights = VIEW_ALL_RIGHTS;
16
17	/**
18	 * Class instantiator
19	 */
20	function __construct() {
21		// no action required
22	}
23
24	/**
25	 * Returns the title
26	 *
27	 * @return string
28	 */
29	function getTitle($locale = NULL) {
30		$text = $this->get('title');
31		if ($locale !== 'all') {
32			$text = get_language_string($text, $locale);
33		}
34		$text = unTagURLs($text);
35		return $text;
36	}
37
38	/**
39	 * Stores the title
40	 *
41	 * @param string $title the title
42	 */
43	function setTitle($title) {
44		$this->set('title', tagURLs($title));
45	}
46
47	/**
48	 * Returns the partent id
49	 *
50	 * @return string
51	 */
52	function getParentID() {
53		return $this->get('parentid');
54	}
55
56	/**
57	 * Sets the ParentID field
58	 * @param $v id of the parent
59	 */
60	function setParentID($v) {
61		$this->set('parentid', $v);
62	}
63
64	/**
65	 * Returns the hitcount
66	 *
67	 * @return int
68	 */
69	function getHitcounter() {
70		return $this->get('hitcounter');
71	}
72
73	/**
74	 * counts visits to the object
75	 */
76	function countHit() {
77		$this->set('hitcounter', $this->get('hitcounter') + 1);
78		$this->save();
79	}
80
81	/**
82	 * Returns true if the item itself is published
83	 *
84	 * @since ZenphotoCMS 1.5.8
85	 *
86	 * For a check including inheritance and private status use isPublic()
87	 *
88	 * @param bool $use_dbvalue Set to true to use the actual db value stored
89	 * and not the possibly temporary modified value (e.g. if in scheduled publishing or expiration)
90	 * @return bool
91	 */
92	function isPublished($use_dbvalue = false) {
93		if ($use_dbvalue) {
94			return $this->get('show', false);
95		}
96		return $this->get('show');
97	}
98
99	/**
100	 * Stores the published value
101	 *
102	 * @since ZenphotoCMS 1.5.8
103	 *
104	 * @param bool $published True if the item is published
105	 */
106	function setPublished($published) {
107		$old = $this->get('show');
108		$new = (int) ($published && true);
109		$this->set('show', $new);
110		if ($old != $new && $this->get('id')) {
111			zp_apply_filter('show_change', $this); // TODO rename to "published_change"
112		}
113	}
114
115	/**
116	 * Returns true published
117	 *
118	 * @return bool
119	 */
120	function getShow() {
121		return $this->isPublished();
122	}
123
124	/**
125	 * Stores the published value
126	 *
127	 * @param bool $show True if the album is published
128	 */
129	function setShow($show) {
130		$this->setPublished($show);
131	}
132
133	/**
134	 * Returns the tag data
135	 *
136	 * @return string
137	 */
138	function getTags() {
139		return readTags($this->getID(), $this->table);
140	}
141
142	/**
143	 * Stores tag information
144	 *
145	 * @param string $tags the tag list
146	 */
147	function setTags($tags) {
148		if (!$this->getID()) { //	requires a valid id to link tags to the object
149			$this->save();
150		}
151		storeTags(array_unique($tags), $this->getID(), $this->table);
152	}
153
154	/**
155	 * Checks if an object has a tag assigned.
156	 *
157	 * @param string $checktag tag to check for
158	 *
159	 * @return bool
160	 */
161	function hasTag($checktag) {
162		$tags = $this->getTags();
163		return in_array($checktag, $tags);
164	}
165
166	/**
167	 * Returns the unformatted date
168	 *
169	 * @return int
170	 */
171	function getDateTime() {
172		return $this->get('date');
173	}
174
175	/**
176	 * Stores the date
177	 *
178	 * @param string $datetime formatted date
179	 */
180	function setDateTime($datetime) {
181		if ($datetime) {
182			$newtime = dateTimeConvert($datetime);
183			if ($newtime !== false) {
184				$this->set('date', $newtime);
185			}
186		} else {
187			$this->set('date', NULL);
188		}
189	}
190
191	/**
192	 * Returns the codeblocks as an serialized array
193	 *
194	 * @return array
195	 */
196	function getCodeblock() {
197		return unTagURLs($this->get("codeblock"));
198	}
199
200	/**
201	 * set the codeblocks as an serialized array
202	 *
203	 */
204	function setCodeblock($cb) {
205		$this->set('codeblock', tagURLs($cb));
206	}
207
208	/**
209	 * returns the custom data field
210	 *
211	 * @return string
212	 */
213	function getCustomData($locale = NULL) {
214		$text = $this->get('custom_data');
215		if ($locale !== 'all') {
216			$text = get_language_string($text, $locale);
217		}
218		$text = unTagURLs($text);
219		return $text;
220	}
221
222	/**
223	 * Sets the custom data field
224	 *
225	 * @param string $val the value to be put in custom_data
226	 */
227	function setCustomData($val) {
228		$this->set('custom_data', tagURLs($val));
229	}
230
231	/**
232	 * Retuns true if comments are allowed
233	 *
234	 * @return bool
235	 */
236	function getCommentsAllowed() {
237		return $this->get('commentson');
238	}
239
240	/**
241	 * Sets the comments allowed flag
242	 *
243	 * @param bool $commentson true if they are allowed
244	 */
245	function setCommentsAllowed($commentson) {
246		$this->set('commentson', (int) ($commentson && true));
247	}
248
249	/**
250	 * Returns an array of comments for this album
251	 *
252	 * @param bool $moderated if false, ignores comments marked for moderation
253	 * @param bool $private if false ignores private comments
254	 * @param bool $desc set to true for descending order
255	 * @return array
256	 */
257	function getComments($moderated = false, $private = false, $desc = false) {
258		$sql = "SELECT *, (date + 0) AS date FROM " . prefix("comments") .
259						" WHERE `type`='" . $this->table . "' AND `ownerid`='" . $this->getID() . "'";
260		if (!$moderated) {
261			$sql .= " AND `inmoderation`=0";
262		}
263		if (!$private) {
264			$sql .= " AND `private`=0";
265		}
266		$sql .= " ORDER BY id";
267		if ($desc) {
268			$sql .= ' DESC';
269		}
270		$comments = query_full_array($sql);
271		$this->comments = $comments;
272		return $this->comments;
273	}
274
275	/**
276	 * Adds comments to the album
277	 * assumes data is coming straight from GET or POST
278	 *
279	 * Returns a comment object
280	 *
281	 * @param string $name Comment author name
282	 * @param string $email Comment author email
283	 * @param string $website Comment author website
284	 * @param string $comment body of the comment
285	 * @param string $code CAPTCHA code entered
286	 * @param string $code_ok CAPTCHA hash expected
287	 * @param string $ip the IP address of the comment poster
288	 * @param bool $private set to true if the comment is for the admin only
289	 * @param bool $anon set to true if the poster wishes to remain anonymous
290	 * @param string $customdata
291	 * @param bool $dataconfirmation true or false if data privacy confirmation was required
292	 * @return object
293	 */
294	function addComment($name, $email, $website, $comment, $code, $code_ok, $ip, $private, $anon, $customdata, $dataconfirmation) {
295		$goodMessage = zp_apply_filter('object_addComment', $name, $email, $website, $comment, $code, $code_ok, $this, $ip, $private, $anon, $customdata, false, $dataconfirmation);
296		return $goodMessage;
297	}
298
299	/**
300	 * Returns the count of comments in the album. Ignores comments in moderation
301	 *
302	 * @return int
303	 */
304	function getCommentCount() {
305		if (is_null($this->commentcount)) {
306			if ($this->comments == null) {
307				$count = db_count("comments", "WHERE `type`='" . $this->table . "' AND `inmoderation`=0 AND `private`=0 AND `ownerid`=" . $this->getID());
308				$this->commentcount = $count;
309			} else {
310				$this->commentcount = count($this->comments);
311			}
312		}
313		return $this->commentcount;
314	}
315
316	/**
317	 * Checks basic access rights of an object
318	 * @param bit $action what the caller wants to do
319	 */
320	function isMyItem($action) {
321		if (!$this->checkPublishDates()) {
322			$this->setShow(0);
323		}
324		if (zp_loggedin($this->manage_rights)) {
325			return true;
326		}
327		if (zp_loggedin($this->view_rights) && ($action == LIST_RIGHTS)) { // sees all
328			return true;
329		}
330		if (zp_apply_filter('check_credentials', false, $this, $action)) {
331			return true;
332		}
333		return NULL;
334	}
335
336	/**
337	 * returns false (deny) if gallery is "private"
338	 * @param $hint
339	 * @param $show
340	 */
341	function checkForGuest(&$hint = NULL, &$show = NULL) {
342		return !(GALLERY_SECURITY != 'public');
343	}
344
345	/**
346	 *
347	 * Checks if viewing of object is allowed
348	 * @param string $hint
349	 * @param string $show
350	 */
351	function checkAccess(&$hint = NULL, &$show = NULL) {
352		if ($this->isMyItem(LIST_RIGHTS)) {
353			return true;
354		}
355		return $this->checkforGuest($hint, $show);
356	}
357
358	/**
359	 * Checks if the item is either expired or needs to be scheduled published
360	 * A class method wrapper of the functions.php function of the same name
361	 * @return boolean
362	 */
363	function checkPublishDates() {
364		$row = array();
365		if (isAlbumClass($this) || isImageClass($this)) {
366			$row = array(
367					'show' => $this->isPublished(),
368					'expiredate' => $this->getExpireDate(),
369					'publishdate' => $this->getPublishDate()
370			);
371		} else if ($this->table == 'news' || $this->table == 'pages') {
372			$row = array(
373					'show' => $this->isPublished(),
374					'expiredate' => $this->getExpireDate(),
375					'publishdate' => $this->getDateTime()
376			);
377		}
378		$check = self::checkScheduledPublishing($row);
379		if ($check == 1 || $check == 2) {
380			return false;
381		} else {
382			return true;
383		}
384	}
385
386	/**
387	 * Checks if the item has expired or is in scheduled publishing
388	 *
389	 * Returns 1 if expired, 2 if in scheduled future publishing
390	 *
391	 * @since ZenphotoCMS 1.5.7 - Code moved from the deprecated checKPublishDates() function
392	 * @param array $row database row of the object
393	 * @return int
394	 */
395	static function checkScheduledPublishing($row) {
396		if (@$row['show']) {
397			if (isset($row['expiredate']) && $row['expiredate'] && $row['expiredate'] != '0000-00-00 00:00:00') {
398				if ($row['expiredate'] < date('Y-m-d H:i:s')) {
399					return 1;
400				}
401			}
402			if (isset($row['publishdate']) && $row['publishdate'] && $row['publishdate'] != '0000-00-00 00:00:00') {
403				if ($row['publishdate'] > date('Y-m-d H:i:s')) {
404					return 2;
405				}
406			}
407			return null;
408		}
409	}
410
411	/**
412	 * Returns true if the item has a proper expire date set no matter if it has expired already or will expire in the future
413	 *
414	 * @since ZenphotoCMS 1.5.7
415	 * @return boolean
416	 */
417	function hasExpireDate() {
418		if ($this->getExpireDate() && $this->getExpireDate() != '0000-00-00 00:00:00') {
419			return true;
420		}
421	}
422
423	/**
424	 * Returns true if the item will be automatically unpublished by a not yet reached future expire date
425	 *
426	 * @since ZenphotoCMS 1.5.7
427	 * @return boolean
428	 */
429	function hasExpiration() {
430		if ($this->hasExpireDate() && $this->get('show', false) && $this->getExpireDate() > date('Y-m-d H:i:s')) {
431			return true;
432		}
433		return false;
434	}
435
436	/**
437	 * Returns true if a future expiredate is set but the item is unpublished
438	 *
439	 * @since ZenphotoCMS 1.5.7
440	 * @return boolean
441	 */
442	function hasInactiveExpiration() {
443		if($this->hasExpiredate() && !$this->get('show', false)) {
444			return true;
445		}
446		return false;
447	}
448
449	/**
450	 * Returns true if the items has been unpublished after reaching the set expire date.
451	 *
452	 * @since ZenphotoCMS 1.5.7
453	 * @return boolean
454	 */
455	function hasExpired() {
456		if ($this->hasExpireDate() && $this->getExpireDate() <= date('Y-m-d H:i:s')) {
457			return true;
458		}
459		return false;
460	}
461
462	/**
463	 * Returns the future date (publishdate for gallery, date for Zenpage items) if set to the future only
464	 *
465	 * @since ZenphotoCMS 1.5.7
466	 * @return string | null
467	 */
468	function hasFutureDate() {
469		$date = null;
470		if ($this->table == 'images' || $this->table == 'albums') {
471			$date = $this->getPublishDate();
472		} else if ($this->table == 'news' || $this->table == 'pages') {
473			$date = $this->getDateTime();
474		}
475		if($date && $date != '0000-00-00 00:00:00' && $date > date('Y-m-d H:i:s') ) {
476			return $date;
477		}
478		return null;
479	}
480
481	/**
482	 * Returns true if the item will be automatically published by a future date set
483	 *
484	 * @since ZenphotoCMS 1.5.7
485	 * @return boolean
486	 */
487	function hasPublishSchedule() {
488		if ($this->hasFutureDate() && $this->get('show', false)) {
489			return true;
490		}
491		return false;
492	}
493
494	/**
495	 * Returns true if the item has a future date but is not published
496	 *
497	 * @since ZenphotoCMS 1.5.7
498	 * @return boolean
499	 */
500	function hasInactivePublishSchedule() {
501		if($this->hasFutureDate() && !$this->get('show', false)) {
502			return true;
503		}
504		return false;
505	}
506
507}
508