1: <?php
2: /**
3: * XOOPS Kernel Class
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: */
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: /**
21: * A Comment
22: *
23: * @package kernel
24: *
25: * @author Kazumi Ono <onokazu@xoops.org>
26: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
27: */
28: class XoopsComment extends XoopsObject
29: {
30: //PHP 8.2 Dynamic properties deprecated
31: public $com_id;
32: public $com_pid;
33: public $com_modid;
34: public $com_icon;
35: public $com_title;
36: public $com_text;
37: public $com_created;
38: public $com_modified;
39: public $com_uid;
40: // Start Add by voltan
41: public $com_user;
42: public $com_email;
43: public $com_url;
44: // End Add by voltan
45: public $com_ip;
46: public $com_sig;
47: public $com_itemid;
48: public $com_rootid;
49: public $com_status;
50: public $com_exparams;
51: public $dohtml;
52: public $dosmiley;
53: public $doxcode;
54: public $doimage;
55: public $dobr;
56:
57: /**
58: * Constructor
59: **/
60: public function __construct()
61: {
62: parent::__construct();
63: $this->initVar('com_id', XOBJ_DTYPE_INT, null, false);
64: $this->initVar('com_pid', XOBJ_DTYPE_INT, 0, false);
65: $this->initVar('com_modid', XOBJ_DTYPE_INT, null, false);
66: $this->initVar('com_icon', XOBJ_DTYPE_OTHER, null, false);
67: $this->initVar('com_title', XOBJ_DTYPE_TXTBOX, null, true, 255, true);
68: $this->initVar('com_text', XOBJ_DTYPE_TXTAREA, null, true, null, true);
69: $this->initVar('com_created', XOBJ_DTYPE_INT, 0, false);
70: $this->initVar('com_modified', XOBJ_DTYPE_INT, 0, false);
71: $this->initVar('com_uid', XOBJ_DTYPE_INT, 0, true);
72: // Start added by voltan
73: $this->initVar('com_user', XOBJ_DTYPE_TXTBOX, null, false, 60);
74: $this->initVar('com_email', XOBJ_DTYPE_TXTBOX, null, false, 60);
75: $this->initVar('com_url', XOBJ_DTYPE_TXTBOX, null, false, 60);
76: // End added by voltan
77: $this->initVar('com_ip', XOBJ_DTYPE_OTHER, null, false);
78: $this->initVar('com_sig', XOBJ_DTYPE_INT, 0, false);
79: $this->initVar('com_itemid', XOBJ_DTYPE_INT, 0, false);
80: $this->initVar('com_rootid', XOBJ_DTYPE_INT, 0, false);
81: $this->initVar('com_status', XOBJ_DTYPE_INT, 0, false);
82: $this->initVar('com_exparams', XOBJ_DTYPE_OTHER, null, false, 255);
83: $this->initVar('dohtml', XOBJ_DTYPE_INT, 0, false);
84: $this->initVar('dosmiley', XOBJ_DTYPE_INT, 0, false);
85: $this->initVar('doxcode', XOBJ_DTYPE_INT, 0, false);
86: $this->initVar('doimage', XOBJ_DTYPE_INT, 0, false);
87: $this->initVar('dobr', XOBJ_DTYPE_INT, 0, false);
88: }
89:
90: /**
91: * Returns Class Base Variable com_id
92: * @param string $format
93: * @return mixed
94: */
95: public function id($format = 'N')
96: {
97: return $this->getVar('com_id', $format);
98: }
99:
100: /**
101: * Returns Class Base Variable com_id
102: * @param string $format
103: * @return mixed
104: */
105: public function com_id($format = '')
106: {
107: return $this->getVar('com_id', $format);
108: }
109:
110: /**
111: * Returns Class Base Variable com_pid
112: * @param string $format
113: * @return mixed
114: */
115: public function com_pid($format = '')
116: {
117: return $this->getVar('com_pid', $format);
118: }
119:
120: /**
121: * Returns Class Base Variable com_modid
122: * @param string $format
123: * @return mixed
124: */
125: public function com_modid($format = '')
126: {
127: return $this->getVar('com_modid', $format);
128: }
129:
130: /**
131: * Returns Class Base Variable com_icon
132: * @param string $format
133: * @return mixed
134: */
135: public function com_icon($format = '')
136: {
137: return $this->getVar('com_icon', $format);
138: }
139:
140: /**
141: * Returns Class Base Variable bid
142: * @param string $format
143: * @return mixed
144: */
145: public function com_title($format = '')
146: {
147: return $this->getVar('com_title', $format);
148: }
149:
150: /**
151: * Returns Class Base Variable com_text
152: * @param string $format
153: * @return mixed
154: */
155: public function com_text($format = '')
156: {
157: return $this->getVar('com_text', $format);
158: }
159:
160: /**
161: * Returns Class Base Variable com_created
162: * @param string $format
163: * @return mixed
164: */
165: public function com_created($format = '')
166: {
167: return $this->getVar('com_created', $format);
168: }
169:
170: /**
171: * Returns Class Base Variable com_modified
172: * @param string $format
173: * @return mixed
174: */
175: public function com_modified($format = '')
176: {
177: return $this->getVar('com_modified', $format);
178: }
179:
180: /**
181: * Returns Class Base Variable com_uid
182: * @param string $format
183: * @return mixed
184: */
185: public function com_uid($format = '')
186: {
187: return $this->getVar('com_uid', $format);
188: }
189:
190: // Start added by voltan
191: /**
192: * Returns Class Base Variable com_user
193: * @param string $format
194: * @return mixed
195: */
196: public function com_user($format = '')
197: {
198: return $this->getVar('com_user', $format);
199: }
200:
201: /**
202: * Returns Class Base Variable com_email
203: * @param string $format
204: * @return mixed
205: */
206: public function com_email($format = '')
207: {
208: return $this->getVar('com_email', $format);
209: }
210:
211: /**
212: * Returns Class Base Variable com_url
213: * @param string $format
214: * @return mixed
215: */
216: public function com_url($format = '')
217: {
218: return $this->getVar('com_url', $format);
219: }
220: // End added by voltan
221:
222: /**
223: * Returns Class Base Variable com_ip
224: * @param string $format
225: * @return mixed
226: */
227: public function com_ip($format = '')
228: {
229: return $this->getVar('com_ip', $format);
230: }
231:
232: /**
233: * Returns Class Base Variable com_sig
234: * @param string $format
235: * @return mixed
236: */
237: public function com_sig($format = '')
238: {
239: return $this->getVar('com_sig', $format);
240: }
241:
242: /**
243: * Returns Class Base Variable com_itemid
244: * @param string $format
245: * @return mixed
246: */
247: public function com_itemid($format = '')
248: {
249: return $this->getVar('com_itemid', $format);
250: }
251:
252: /**
253: * Returns Class Base Variable com_rootid
254: * @param string $format
255: * @return mixed
256: */
257: public function com_rootid($format = '')
258: {
259: return $this->getVar('com_rootid', $format);
260: }
261:
262: /**
263: * Returns Class Base Variable com_status
264: * @param string $format
265: * @return mixed
266: */
267: public function com_status($format = '')
268: {
269: return $this->getVar('com_status', $format);
270: }
271:
272: /**
273: * Returns Class Base Variable com_exparams
274: * @param string $format
275: * @return mixed
276: */
277: public function com_exparams($format = '')
278: {
279: return $this->getVar('com_exparams', $format);
280: }
281:
282: /**
283: * Returns Class Base Variable bid
284: * @param string $format
285: * @return mixed
286: */
287: public function dohtml($format = '')
288: {
289: return $this->getVar('dohtml', $format);
290: }
291:
292: /**
293: * Returns Class Base Variable dosmiley
294: * @param string $format
295: * @return mixed
296: */
297: public function dosmiley($format = '')
298: {
299: return $this->getVar('dosmiley', $format);
300: }
301:
302: /**
303: * Returns Class Base Variable doxcode
304: * @param string $format
305: * @return mixed
306: */
307: public function doxcode($format = '')
308: {
309: return $this->getVar('doxcode', $format);
310: }
311:
312: /**
313: * Returns Class Base Variable doimage
314: * @param string $format
315: * @return mixed
316: */
317: public function doimage($format = '')
318: {
319: return $this->getVar('doimage', $format);
320: }
321:
322: /**
323: * Returns Class Base Variable dobr
324: * @param string $format
325: * @return mixed
326: */
327: public function dobr($format = '')
328: {
329: return $this->getVar('dobr', $format);
330: }
331:
332: /**
333: * Is this comment on the root level?
334: *
335: * @return bool
336: */
337: public function isRoot()
338: {
339: return ($this->getVar('com_id') == $this->getVar('com_rootid'));
340: }
341: }
342:
343: /**
344: * XOOPS comment handler class.
345: *
346: * This class is responsible for providing data access mechanisms to the data source
347: * of XOOPS comment class objects.
348: *
349: *
350: * @package kernel
351: * @subpackage comment
352: *
353: * @author Kazumi Ono <onokazu@xoops.org>
354: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
355: *
356: * @todo Why is this not a XoopsPersistableObjectHandler?
357: */
358: class XoopsCommentHandler extends XoopsObjectHandler
359: {
360: /**
361: * Create a {@link XoopsComment}
362: *
363: * @param bool $isNew Flag the object as "new"?
364: *
365: * @return XoopsComment
366: */
367: public function create($isNew = true)
368: {
369: $comment = new XoopsComment();
370: if ($isNew) {
371: $comment->setNew();
372: }
373:
374: return $comment;
375: }
376:
377: /**
378: * Retrieve a {@link XoopsComment}
379: *
380: * @param int $id ID
381: *
382: * @return XoopsComment|false {@link XoopsComment}, false on fail
383: **/
384: public function get($id)
385: {
386: $comment = false;
387: $id = (int)$id;
388: if ($id > 0) {
389: $sql = 'SELECT * FROM ' . $this->db->prefix('xoopscomments') . ' WHERE com_id=' . $id;
390: $result = $this->db->query($sql);
391: if (!$this->db->isResultSet($result)) {
392: return $comment;
393: }
394: $numrows = $this->db->getRowsNum($result);
395: if ($numrows == 1) {
396: $comment = new XoopsComment();
397: $comment->assignVars($this->db->fetchArray($result));
398: }
399: }
400:
401: return $comment;
402: }
403:
404: /**
405: * Write a comment to database
406: *
407: * @param XoopsObject|XoopsComment $comment a XoopsComment object
408: *
409: * @return bool true on success, otherwise false
410: **/
411: public function insert(XoopsObject $comment)
412: {
413: $className = 'XoopsComment';
414: if (!($comment instanceof $className)) {
415: return false;
416: }
417: if (!$comment->isDirty()) {
418: return true;
419: }
420: if (!$comment->cleanVars()) {
421: return false;
422: }
423: foreach ($comment->cleanVars as $k => $v) {
424: ${$k} = $v;
425: }
426: // Start edit by voltan
427: if ($comment->isNew()) {
428: $com_id = $this->db->genId('xoopscomments_com_id_seq');
429: $sql = sprintf('INSERT INTO %s (com_id, com_pid, com_modid, com_icon, com_title, com_text, com_created, com_modified, com_uid, com_user, com_email, com_url, com_ip, com_sig, com_itemid, com_rootid, com_status, com_exparams, dohtml, dosmiley, doxcode, doimage, dobr) VALUES (%u, %u, %u, %s, %s, %s, %u, %u, %u, %s, %s, %s, %s, %u, %u, %u, %u, %s, %u, %u, %u, %u, %u)', $this->db->prefix('xoopscomments'), $com_id, $com_pid, $com_modid, $this->db->quoteString($com_icon), $this->db->quoteString($com_title), $this->db->quoteString($com_text), $com_created, $com_modified, $com_uid, $this->db->quoteString($com_user), $this->db->quoteString($com_email), $this->db->quoteString($com_url), $this->db->quoteString($com_ip), $com_sig, $com_itemid, $com_rootid, $com_status, $this->db->quoteString($com_exparams), $dohtml, $dosmiley, $doxcode, $doimage, $dobr);
430: } else {
431: $sql = sprintf('UPDATE %s SET com_pid = %u, com_icon = %s, com_title = %s, com_text = %s, com_created = %u, com_modified = %u, com_uid = %u, com_user = %s, com_email = %s, com_url = %s, com_ip = %s, com_sig = %u, com_itemid = %u, com_rootid = %u, com_status = %u, com_exparams = %s, dohtml = %u, dosmiley = %u, doxcode = %u, doimage = %u, dobr = %u WHERE com_id = %u', $this->db->prefix('xoopscomments'), $com_pid, $this->db->quoteString($com_icon), $this->db->quoteString($com_title), $this->db->quoteString($com_text), $com_created, $com_modified, $com_uid, $this->db->quoteString($com_user), $this->db->quoteString($com_email), $this->db->quoteString($com_url), $this->db->quoteString($com_ip), $com_sig, $com_itemid, $com_rootid, $com_status, $this->db->quoteString($com_exparams), $dohtml, $dosmiley, $doxcode, $doimage, $dobr, $com_id);
432: }
433: // End edit by voltan
434: if (!$result = $this->db->query($sql)) {
435: return false;
436: }
437: if (empty($com_id)) {
438: $com_id = $this->db->getInsertId();
439: }
440: $comment->assignVar('com_id', $com_id);
441:
442: return true;
443: }
444:
445: /**
446: * Delete a {@link XoopsComment} from the database
447: *
448: * @param XoopsObject|XoopsComment $comment a XoopsComment object
449: *
450: * @return bool true on success, otherwise false
451: **/
452: public function delete(XoopsObject $comment)
453: {
454: $className = 'XoopsComment';
455: if (!($comment instanceof $className)) {
456: return false;
457: }
458: $sql = sprintf('DELETE FROM %s WHERE com_id = %u', $this->db->prefix('xoopscomments'), $comment->getVar('com_id'));
459: if (!$result = $this->db->query($sql)) {
460: return false;
461: }
462:
463: return true;
464: }
465:
466: /**
467: * Get some {@link XoopsComment}s
468: *
469: * @param CriteriaElement|CriteriaCompo $criteria
470: * @param bool $id_as_key Use IDs as keys into the array?
471: *
472: * @return array Array of {@link XoopsComment} objects
473: **/
474: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
475: {
476: $ret = array();
477: $limit = $start = 0;
478: $sql = 'SELECT * FROM ' . $this->db->prefix('xoopscomments');
479: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
480: $sql .= ' ' . $criteria->renderWhere();
481: $sort = ($criteria->getSort() != '') ? $criteria->getSort() : 'com_id';
482: $sql .= ' ORDER BY ' . $sort . ' ' . $criteria->getOrder();
483: $limit = $criteria->getLimit();
484: $start = $criteria->getStart();
485: }
486: $result = $this->db->query($sql, $limit, $start);
487: if (!$this->db->isResultSet($result)) {
488: return $ret;
489: }
490: /** @var array $myrow */
491: while (false !== ($myrow = $this->db->fetchArray($result))) {
492: $comment = new XoopsComment();
493: $comment->assignVars($myrow);
494: if (!$id_as_key) {
495: $ret[] =& $comment;
496: } else {
497: $ret[$myrow['com_id']] = &$comment;
498: }
499: unset($comment);
500: }
501:
502: return $ret;
503: }
504:
505: /**
506: * Count Comments
507: *
508: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
509: *
510: * @return int Count
511: **/
512: public function getCount(CriteriaElement $criteria = null)
513: {
514: $sql = 'SELECT COUNT(*) FROM ' . $this->db->prefix('xoopscomments');
515: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
516: $sql .= ' ' . $criteria->renderWhere();
517: }
518: $result = $this->db->query($sql);
519: if (!$this->db->isResultSet($result)) {
520: return 0;
521: }
522: list($count) = $this->db->fetchRow($result);
523:
524: return (int)$count;
525: }
526:
527: /**
528: * Delete multiple comments
529: *
530: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
531: *
532: * @return bool
533: **/
534: public function deleteAll(CriteriaElement $criteria = null)
535: {
536: $sql = 'DELETE FROM ' . $this->db->prefix('xoopscomments');
537: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
538: $sql .= ' ' . $criteria->renderWhere();
539: }
540: if (!$result = $this->db->query($sql)) {
541: return false;
542: }
543:
544: return true;
545: }
546:
547: /**
548: * Get a list of comments
549: *
550: * @param CriteriaElement $criteria {@link CriteriaElement}
551: *
552: * @return array Array of raw database records
553: **/
554: public function getList(CriteriaElement $criteria = null)
555: {
556: $comments = $this->getObjects($criteria, true);
557: $ret = array();
558: foreach (array_keys($comments) as $i) {
559: $ret[$i] = $comments[$i]->getVar('com_title');
560: }
561:
562: return $ret;
563: }
564:
565: /**
566: * Retrieves comments for an item
567: *
568: * @param int $module_id Module ID
569: * @param int $item_id Item ID
570: * @param string $order Sort order
571: * @param int $status Status of the comment
572: * @param int $limit Max num of comments to retrieve
573: * @param int $start Start offset
574: *
575: * @return array Array of {@link XoopsComment} objects
576: **/
577: public function getByItemId($module_id, $item_id, $order = null, $status = null, $limit = null, $start = 0)
578: {
579: $criteria = new CriteriaCompo(new Criteria('com_modid', (int)$module_id));
580: $criteria->add(new Criteria('com_itemid', (int)$item_id));
581: if (isset($status)) {
582: $criteria->add(new Criteria('com_status', (int)$status));
583: }
584: if (isset($order)) {
585: $criteria->setOrder($order);
586: }
587: if (isset($limit)) {
588: $criteria->setLimit($limit);
589: $criteria->setStart($start);
590: }
591:
592: return $this->getObjects($criteria);
593: }
594:
595: /**
596: * Gets total number of comments for an item
597: *
598: * @param int $module_id Module ID
599: * @param int $item_id Item ID
600: * @param int $status Status of the comment
601: *
602: * @return array Array of {@link XoopsComment} objects
603: **/
604: public function getCountByItemId($module_id, $item_id, $status = null)
605: {
606: $criteria = new CriteriaCompo(new Criteria('com_modid', (int)$module_id));
607: $criteria->add(new Criteria('com_itemid', (int)$item_id));
608: if (isset($status)) {
609: $criteria->add(new Criteria('com_status', (int)$status));
610: }
611:
612: return $this->getCount($criteria);
613: }
614:
615: /**
616: * Get the top {@link XoopsComment}s
617: *
618: * @param int $module_id
619: * @param int $item_id
620: * @param string $order
621: * @param int $status
622: *
623: * @return array Array of {@link XoopsComment} objects
624: **/
625: public function getTopComments($module_id, $item_id, $order, $status = null)
626: {
627: $criteria = new CriteriaCompo(new Criteria('com_modid', (int)$module_id));
628: $criteria->add(new Criteria('com_itemid', (int)$item_id));
629: $criteria->add(new Criteria('com_pid', 0));
630: if (isset($status)) {
631: $criteria->add(new Criteria('com_status', (int)$status));
632: }
633: $criteria->setOrder($order);
634:
635: return $this->getObjects($criteria);
636: }
637:
638: /**
639: * Retrieve a whole thread
640: *
641: * @param int $comment_rootid
642: * @param int $comment_id
643: * @param int $status
644: *
645: * @return array Array of {@link XoopsComment} objects
646: **/
647: public function getThread($comment_rootid, $comment_id, $status = null)
648: {
649: $criteria = new CriteriaCompo(new Criteria('com_rootid', (int)$comment_rootid));
650: $criteria->add(new Criteria('com_id', (int)$comment_id, '>='));
651: if (isset($status)) {
652: $criteria->add(new Criteria('com_status', (int)$status));
653: }
654:
655: return $this->getObjects($criteria);
656: }
657:
658: /**
659: * Update
660: *
661: * @param XoopsComment $comment {@link XoopsComment} object
662: * @param string $field_name Name of the field
663: * @param mixed $field_value Value to write
664: *
665: * @return bool
666: **/
667: public function updateByField(XoopsComment $comment, $field_name, $field_value)
668: {
669: $comment->unsetNew();
670: $comment->setVar($field_name, $field_value);
671:
672: return $this->insert($comment);
673: }
674:
675: /**
676: * Delete all comments for one whole module
677: *
678: * @param int $module_id ID of the module
679: * @return bool
680: **/
681: public function deleteByModule($module_id)
682: {
683: return $this->deleteAll(new Criteria('com_modid', (int)$module_id));
684: }
685: }
686: