1: <?php
2:
3: 4: 5: 6:
7:
8:
9:
10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32:
33:
34:
35: $_zp_object_cache = array();
36: define('OBJECT_CACHE_DEPTH', 150);
37:
38:
39: class PersistentObject {
40:
41: var $loaded = false;
42: var $exists = false;
43: var $table;
44: var $transient;
45: protected $id = 0;
46: private $unique_set = NULL;
47: private $cache_by;
48: private $use_cache = false;
49: private $tempdata = NULL;
50: private $data = NULL;
51: private $updates = NULL;
52:
53: 54: 55: 56:
57: function __construct($tablename, $unique_set, $cache_by = NULL, $use_cache = true, $is_transient = false, $allowCreate = true) {
58: deprecated_functions::PersistentObject();
59: return instantiate($tablename, $unique_set, $cache_by, $use_cache, $is_transient, $allowCreate);
60: }
61:
62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73:
74: function instantiate($tablename, $unique_set, $cache_by = NULL, $use_cache = true, $is_transient = false, $allowCreate = true) {
75: global $_zp_object_cache;
76:
77: $classname = get_class($this);
78: if (!isset($_zp_object_cache[$classname])) {
79: $_zp_object_cache[$classname] = array();
80: }
81:
82:
83: $this->data = $this->tempdata = $this->updates = array();
84: $this->loaded = false;
85: $this->table = $tablename;
86: $this->unique_set = $unique_set;
87: if (is_null($cache_by)) {
88: $this->cache_by = serialize($unique_set);
89: } else {
90: $this->cache_by = $this->unique_set[$cache_by];
91: }
92: $this->use_cache = $use_cache;
93: $this->transient = $is_transient;
94: return $this->load($allowCreate);
95: }
96:
97: 98: 99: 100: 101:
102: private function getFromCache() {
103: global $_zp_object_cache;
104: if (isset($_zp_object_cache[$c = get_class($this)]) && isset($_zp_object_cache[$c][$this->cache_by])) {
105: return $_zp_object_cache[$c][$this->cache_by];
106: }
107: return NULL;
108: }
109:
110: 111: 112: 113: 114:
115: private function addToCache($entry) {
116: global $_zp_object_cache;
117: if ($entry) {
118: if (count($_zp_object_cache[$classname = get_class($this)]) >= OBJECT_CACHE_DEPTH) {
119: array_shift($_zp_object_cache[$classname]);
120: }
121: $_zp_object_cache[$classname][$this->cache_by] = $entry;
122: }
123: }
124:
125: 126: 127: 128: 129: 130:
131: function set($var, $value) {
132: if (empty($var))
133: return false;
134: if ($this->loaded && !array_key_exists($var, $this->data)) {
135: $this->tempdata[$var] = $value;
136: } else {
137: $this->updates[$var] = $value;
138: }
139: return true;
140: }
141:
142: 143: 144: 145:
146: protected function setDefaults() {
147:
148: }
149:
150: 151: 152: 153: 154: 155:
156: function move($new_unique_set) {
157:
158: $result = query_single_row('SELECT * FROM ' . prefix($this->table) . getWhereClause($new_unique_set) . ' LIMIT 1;');
159: if (!$result || $result['id'] == $this->id) {
160: if (!zp_apply_filter('move_object', true, $this, $new_unique_set)) {
161: return false;
162: }
163: $sql = 'UPDATE ' . prefix($this->table) . getSetClause($new_unique_set) . ' ' . getWhereClause($this->unique_set);
164: $result = query($sql);
165: if ($result && db_affected_rows() == 1) {
166: $this->unique_set = $new_unique_set;
167: return true;
168: }
169: }
170: return false;
171: }
172:
173: 174: 175: 176: 177: 178:
179: function copy($new_unique_set) {
180:
181: $result = query('SELECT * FROM ' . prefix($this->table) . getWhereClause($new_unique_set) . ' LIMIT 1;');
182:
183: if ($result && db_num_rows($result) == 0) {
184: if (!zp_apply_filter('copy_object', true, $this, $new_unique_set)) {
185: return false;
186: }
187:
188: $insert_data = array_merge($this->data, $this->updates, $this->tempdata, $new_unique_set);
189: unset($insert_data['id']);
190: unset($insert_data['hitcounter']);
191: if (empty($insert_data)) {
192: return true;
193: }
194: $sql = 'INSERT INTO ' . prefix($this->table) . ' (';
195: $i = 0;
196: foreach (array_keys($insert_data) as $col) {
197: if ($i > 0)
198: $sql .= ", ";
199: $sql .= "`$col`";
200: $i++;
201: }
202: $sql .= ') VALUES (';
203: $i = 0;
204: foreach (array_values($insert_data) as $value) {
205: if ($i > 0)
206: $sql .= ', ';
207: if (is_null($value)) {
208: $sql .= 'NULL';
209: } else {
210: $sql .= db_quote($value);
211: }
212: $i++;
213: }
214: $sql .= ');';
215: $success = query($sql);
216: if ($success && db_affected_rows() == 1) {
217: return zp_apply_filter('copy_object', db_insert_id(), $this);
218: }
219: }
220: return false;
221: }
222:
223: 224: 225: 226: 227:
228: function remove() {
229: if (!zp_apply_filter('remove_object', true, $this)) {
230: return false;
231: }
232: $id = $this->id;
233: if (empty($id)) {
234: $id = ' is NULL';
235: } else {
236: $id = '=' . $id;
237: }
238: $sql = 'DELETE FROM ' . prefix($this->table) . ' WHERE `id`' . $id;
239: $this->loaded = false;
240: $this->transient = true;
241: return query($sql);
242: }
243:
244: 245: 246: 247: 248:
249: function getID() {
250: return $this->id;
251: }
252:
253: 254: 255: 256: 257:
258: function getData() {
259: $this->save();
260: return $this->data;
261: }
262:
263: 264: 265: 266:
267: function get($var, $current = true) {
268: if ($current && isset($this->updates[$var])) {
269: return $this->updates[$var];
270: } else if (isset($this->data[$var])) {
271: return $this->data[$var];
272: } else if (isset($this->tempdata[$var])) {
273: return $this->tempdata[$var];
274: } else {
275: return null;
276: }
277: }
278:
279: 280: 281: 282: 283: 284:
285: private function load($allowCreate) {
286: $new = $entry = null;
287:
288: $sql = 'SELECT * FROM ' . prefix($this->table) . getWhereClause($this->unique_set) . ' LIMIT 1;';
289:
290: if ($this->use_cache) {
291: $entry = $this->getFromCache();
292: }
293:
294: if (empty($entry)) {
295: $entry = query_single_row($sql, false);
296:
297: if ($entry)
298: $this->addToCache($entry);
299: }
300:
301:
302: if (empty($entry)) {
303: if ($this->transient) {
304: $entry = array_merge($this->unique_set, $this->updates, $this->tempdata);
305: $entry['id'] = 0;
306: } else if (!$allowCreate) {
307: return NULL;
308: } else {
309: $new = true;
310: $this->save();
311: $entry = query_single_row($sql);
312:
313: if (!$entry)
314: return null;
315:
316: $this->addToCache($entry);
317: }
318: }
319: $this->data = $entry;
320: $this->id = (int) $entry['id'];
321: $this->loaded = true;
322: return $new;
323: }
324:
325: 326: 327: 328:
329: function save() {
330: if ($this->transient)
331: return false;
332: if (!$this->unique_set) {
333: zp_error('empty $this->unique set is empty');
334: return false;
335: }
336: if (!$this->id) {
337: $this->setDefaults();
338:
339: $insert_data = array_merge($this->unique_set, $this->updates, $this->tempdata);
340: if (empty($insert_data)) {
341: return true;
342: }
343: $i = 0;
344: $cols = $vals = '';
345: foreach ($insert_data as $col => $value) {
346: if ($i > 0)
347: $cols .= ", ";
348: $cols .= "`$col`";
349: if ($i > 0)
350: $vals .= ", ";
351: if (is_null($value)) {
352: $vals .= "NULL";
353: } else {
354: $vals .= db_quote($value);
355: }
356: $i++;
357: }
358: $sql = 'INSERT INTO ' . prefix($this->table) . ' (' . $cols . ') VALUES (' . $vals . ')';
359: $success = query($sql);
360: if (!$success || db_affected_rows() != 1) {
361: return false;
362: }
363: foreach ($insert_data as $key => $value) {
364: $this->data[$key] = $value;
365: }
366: $this->data['id'] = $this->id = (int) db_insert_id();
367: $this->loaded = true;
368: $this->updates = array();
369: $this->tempdata = array();
370: } else {
371:
372: if (empty($this->updates)) {
373: return true;
374: } else {
375: $sql = 'UPDATE ' . prefix($this->table) . ' SET';
376: $i = 0;
377: foreach ($this->updates as $col => $value) {
378: if ($i > 0)
379: $sql .= ",";
380: if (is_null($value)) {
381: $sql .= " `$col` = NULL";
382: } else {
383: $sql .= " `$col` = " . db_quote($value);
384: }
385: $this->data[$col] = $value;
386: $i++;
387: }
388: $sql .= ' WHERE id=' . $this->id . ';';
389: $success = query($sql);
390: if (!$success || db_affected_rows() != 1) {
391: return false;
392: }
393: foreach ($this->updates as $key => $value) {
394: $this->data[$key] = $value;
395: }
396: $this->updates = array();
397: }
398: }
399: zp_apply_filter('save_object', true, $this);
400: $this->addToCache($this->data);
401: return true;
402: }
403:
404: 405: 406: 407: 408:
409: public function __toString() {
410: return $this->table . " (" . $this->id . ")";
411: }
412:
413: }
414:
415:
416: 417: 418: 419:
420: class ThemeObject extends PersistentObject {
421:
422: private $commentcount;
423: var $comments = NULL;
424: var $manage_rights = ADMIN_RIGHTS;
425: var $manage_some_rights = ADMIN_RIGHTS;
426: var $view_rights = VIEW_ALL_RIGHTS;
427:
428: 429: 430:
431: function __construct() {
432:
433: }
434:
435: 436: 437: 438: 439:
440: function getTitle($locale = NULL) {
441: $text = $this->get('title');
442: if ($locale !== 'all') {
443: $text = get_language_string($text, $locale);
444: }
445: $text = zpFunctions::unTagURLs($text);
446: return $text;
447: }
448:
449: 450: 451: 452: 453:
454: function setTitle($title) {
455: $this->set('title', zpFunctions::tagURLs($title));
456: }
457:
458: 459: 460: 461: 462:
463: function getParentID() {
464: return $this->get('parentid');
465: }
466:
467: 468: 469: 470:
471: function setParentID($v) {
472: $this->set('parentid', $v);
473: }
474:
475: 476: 477: 478: 479:
480: function getHitcounter() {
481: return $this->get('hitcounter');
482: }
483:
484: 485: 486:
487: function countHit() {
488: $this->set('hitcounter', $this->get('hitcounter') + 1);
489: $this->save();
490: }
491:
492: 493: 494: 495: 496:
497: function getShow() {
498: return $this->get('show');
499: }
500:
501: 502: 503: 504: 505:
506: function setShow($show) {
507: $old_show = $this->get('show');
508: $new_show = (int) ($show && true);
509: $this->set('show', $new_show);
510: if ($old_show != $new_show && $this->get('id')) {
511: zp_apply_filter('show_change', $this);
512: }
513: }
514:
515: 516: 517: 518: 519:
520: function getTags() {
521: return readTags($this->getID(), $this->table);
522: }
523:
524: 525: 526: 527: 528:
529: function setTags($tags) {
530: if (!$this->getID()) {
531: $this->save();
532: }
533: storeTags(array_unique($tags), $this->getID(), $this->table);
534: }
535:
536: 537: 538: 539: 540: 541: 542:
543: function hasTag($checktag) {
544: $tags = $this->getTags();
545: return in_array($checktag, $tags);
546: }
547:
548: 549: 550: 551: 552:
553: function getDateTime() {
554: return $this->get('date');
555: }
556:
557: 558: 559: 560: 561:
562: function setDateTime($datetime) {
563: if ($datetime) {
564: $newtime = dateTimeConvert($datetime);
565: if ($newtime !== false) {
566: $this->set('date', $newtime);
567: }
568: } else {
569: $this->set('date', NULL);
570: }
571: }
572:
573: 574: 575: 576: 577:
578: function getCodeblock() {
579: return zpFunctions::unTagURLs($this->get("codeblock"));
580: }
581:
582: 583: 584: 585:
586: function setCodeblock($cb) {
587: $this->set('codeblock', zpFunctions::tagURLs($cb));
588: }
589:
590: 591: 592: 593: 594:
595: function getCustomData($locale = NULL) {
596: $text = $this->get('custom_data');
597: if ($locale !== 'all') {
598: $text = get_language_string($text, $locale);
599: }
600: $text = zpFunctions::unTagURLs($text);
601: return $text;
602: }
603:
604: 605: 606: 607: 608:
609: function setCustomData($val) {
610: $this->set('custom_data', zpFunctions::tagURLs($val));
611: }
612:
613: 614: 615: 616: 617:
618: function getCommentsAllowed() {
619: return $this->get('commentson');
620: }
621:
622: 623: 624: 625: 626:
627: function setCommentsAllowed($commentson) {
628: $this->set('commentson', (int) ($commentson && true));
629: }
630:
631: 632: 633: 634: 635: 636: 637: 638:
639: function getComments($moderated = false, $private = false, $desc = false) {
640: $sql = "SELECT *, (date + 0) AS date FROM " . prefix("comments") .
641: " WHERE `type`='" . $this->table . "' AND `ownerid`='" . $this->getID() . "'";
642: if (!$moderated) {
643: $sql .= " AND `inmoderation`=0";
644: }
645: if (!$private) {
646: $sql .= " AND `private`=0";
647: }
648: $sql .= " ORDER BY id";
649: if ($desc) {
650: $sql .= ' DESC';
651: }
652: $comments = query_full_array($sql);
653: $this->comments = $comments;
654: return $this->comments;
655: }
656:
657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674:
675: function addComment($name, $email, $website, $comment, $code, $code_ok, $ip, $private, $anon, $customdata) {
676: $goodMessage = zp_apply_filter('object_addComment', $name, $email, $website, $comment, $code, $code_ok, $this, $ip, $private, $anon, $customdata);
677: return $goodMessage;
678: }
679:
680: 681: 682: 683: 684:
685: function getCommentCount() {
686: if (is_null($this->commentcount)) {
687: if ($this->comments == null) {
688: $count = db_count("comments", "WHERE `type`='" . $this->table . "' AND `inmoderation`=0 AND `private`=0 AND `ownerid`=" . $this->getID());
689: $this->commentcount = $count;
690: } else {
691: $this->commentcount = count($this->comments);
692: }
693: }
694: return $this->commentcount;
695: }
696:
697: 698: 699: 700:
701: function isMyItem($action) {
702: if (!$this->checkPublishDates()) {
703: $this->setShow(0);
704: }
705: if (zp_loggedin($this->manage_rights)) {
706: return true;
707: }
708: if (zp_loggedin($this->view_rights) && ($action == LIST_RIGHTS)) {
709: return true;
710: }
711: if (zp_apply_filter('check_credentials', false, $this, $action)) {
712: return true;
713: }
714: return NULL;
715: }
716:
717: 718: 719: 720: 721:
722: function checkForGuest(&$hint = NULL, &$show = NULL) {
723: return !(GALLERY_SECURITY != 'public');
724: }
725:
726: 727: 728: 729: 730: 731:
732: function checkAccess(&$hint = NULL, &$show = NULL) {
733: if ($this->isMyItem(LIST_RIGHTS)) {
734: return true;
735: }
736: return $this->checkforGuest($hint, $show);
737: }
738:
739: 740: 741: 742: 743:
744: function checkPublishDates() {
745: $row = array();
746: if (isAlbumClass($this) || isImageClass($this)) {
747: $row = array(
748: 'show' => $this->getShow(),
749: 'expiredate' => $this->getExpireDate(),
750: 'publishdate' => $this->getPublishDate()
751: );
752: } else if ($this->table == 'news' || $this->table == 'pages') {
753: $row = array(
754: 'show' => $this->getShow(),
755: 'expiredate' => $this->getExpireDate(),
756: 'publishdate' => $this->getDateTime()
757: );
758: }
759: $check = checkPublishDates($row);
760: if ($check == 1 || $check == 2) {
761: return false;
762: } else {
763: return true;
764: }
765: }
766:
767: }
768:
769:
770: 771: 772: 773:
774: class MediaObject extends ThemeObject {
775:
776: 777: 778:
779: function __construct() {
780:
781: }
782:
783: 784: 785: 786: 787:
788: function getDesc($locale = NULL) {
789: $text = $this->get('desc');
790: if ($locale == 'all') {
791: return zpFunctions::unTagURLs($text);
792: } else {
793: return applyMacros(zpFunctions::unTagURLs(get_language_string($text, $locale)));
794: }
795: }
796:
797: 798: 799: 800: 801:
802: function setDesc($desc) {
803: $desc = zpFunctions::tagURLs($desc);
804: $this->set('desc', $desc);
805: }
806:
807: 808: 809: 810: 811:
812: function getSortOrder() {
813: return $this->get('sort_order');
814: }
815:
816: 817: 818: 819: 820:
821: function setSortOrder($sortorder) {
822: $this->set('sort_order', $sortorder);
823: }
824:
825: 826: 827: 828: 829:
830: function getUser() {
831: return $this->get('user');
832: }
833:
834: 835: 836: 837: 838:
839: function setUser($user) {
840: $this->set('user', $user);
841: }
842:
843: 844: 845: 846: 847:
848: function getPassword() {
849: if (GALLERY_SECURITY != 'public') {
850: return NULL;
851: } else {
852: return $this->get('password');
853: }
854: }
855:
856: 857: 858: 859: 860:
861: function setPassword($pwd) {
862: $this->set('password', $pwd);
863: }
864:
865: 866: 867: 868: 869:
870: function getPasswordHint($locale = NULL) {
871: $text = $this->get('password_hint');
872: if ($locale !== 'all') {
873: $text = get_language_string($text, $locale);
874: }
875: $text = zpFunctions::unTagURLs($text);
876: return $text;
877: }
878:
879: 880: 881: 882: 883:
884: function setPasswordHint($hint) {
885: $this->set('password_hint', zpFunctions::tagURLs($hint));
886: }
887:
888: 889: 890: 891: 892:
893: function getExpireDate() {
894: $dt = $this->get("expiredate");
895: if ($dt == '0000-00-00 00:00:00') {
896: return NULL;
897: } else {
898: return $dt;
899: }
900: }
901:
902: 903: 904: 905:
906: function setExpireDate($ed) {
907: if ($ed) {
908: $newtime = dateTimeConvert($ed);
909: if ($newtime === false)
910: return;
911: $this->set('expiredate', $newtime);
912: } else {
913: $this->set('expiredate', NULL);
914: }
915: }
916:
917: 918: 919: 920: 921:
922: function getPublishDate() {
923: $dt = $this->get("publishdate");
924: if ($dt == '0000-00-00 00:00:00') {
925: return NULL;
926: } else {
927: return $dt;
928: }
929: }
930:
931: 932: 933: 934:
935: function setPublishDate($ed) {
936: if ($ed) {
937: $newtime = dateTimeConvert($ed);
938: if ($newtime === false)
939: return;
940: $this->set('publishdate', $newtime);
941: } else {
942: $this->set('publishdate', NULL);
943: }
944: }
945:
946: }
947:
948: ?>
949: