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: if (!defined('XOOPS_ROOT_PATH')) {
33: exit();
34: }
35:
36: include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
37: include_once XOOPS_ROOT_PATH . '/include/notification_functions.php';
38:
39: 40: 41: 42: 43: 44: 45: 46: 47:
48:
49: 50: 51: 52: 53: 54: 55: 56: 57:
58: class XoopsNotification extends XoopsObject
59: {
60:
61: 62: 63:
64: function XoopsNotification()
65: {
66: $this->XoopsObject();
67: $this->initVar('not_id', XOBJ_DTYPE_INT, NULL, false);
68: $this->initVar('not_modid', XOBJ_DTYPE_INT, NULL, false);
69: $this->initVar('not_category', XOBJ_DTYPE_TXTBOX, null, false, 30);
70: $this->initVar('not_itemid', XOBJ_DTYPE_INT, 0, false);
71: $this->initVar('not_event', XOBJ_DTYPE_TXTBOX, null, false, 30);
72: $this->initVar('not_uid', XOBJ_DTYPE_INT, 0, true);
73: $this->initVar('not_mode', XOBJ_DTYPE_INT, 0, false);
74: }
75:
76:
77:
78:
79:
80:
81:
82:
83: 84: 85: 86: 87: 88: 89: 90: 91: 92:
93: function notifyUser($template_dir, $template, $subject, $tags)
94: {
95:
96:
97: $member_handler = xoops_gethandler('member');
98: $user =& $member_handler->getUser($this->getVar('not_uid'));
99: if (!is_object($user)) {
100: return true;
101: }
102: $method = $user->getVar('notify_method');
103:
104: $xoopsMailer =& getMailer();
105: include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
106: switch($method) {
107: case XOOPS_NOTIFICATION_METHOD_PM:
108: $xoopsMailer->usePM();
109: $config_handler = xoops_gethandler('config');
110: $xoopsMailerConfig =& $config_handler->getConfigsByCat(XOOPS_CONF_MAILER);
111: $xoopsMailer->setFromUser($member_handler->getUser($xoopsMailerConfig['fromuid']));
112: foreach ($tags as $k=>$v) {
113: $xoopsMailer->assign($k, $v);
114: }
115: break;
116: case XOOPS_NOTIFICATION_METHOD_EMAIL:
117: $xoopsMailer->useMail();
118: foreach ($tags as $k=>$v) {
119: $xoopsMailer->assign($k, preg_replace("/&/i", '&', $v));
120: }
121: break;
122: default:
123: return true;
124: break;
125: }
126:
127:
128: $xoopsMailer->setTemplateDir($template_dir);
129: $xoopsMailer->setTemplate($template);
130: $xoopsMailer->setToUsers($user);
131:
132:
133:
134: $xoopsMailer->setSubject($subject);
135: $success = $xoopsMailer->send();
136:
137:
138:
139:
140: include_once XOOPS_ROOT_PATH . '/include/notification_constants.php';
141: $notification_handler = xoops_gethandler('notification');
142:
143: if ($this->getVar('not_mode') == XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE) {
144: $notification_handler->delete($this);
145: return $success;
146: }
147:
148: if ($this->getVar('not_mode') == XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT) {
149: $this->setVar('not_mode', XOOPS_NOTIFICATION_MODE_WAITFORLOGIN);
150: $notification_handler->insert($this);
151: }
152: return $success;
153:
154: }
155:
156: }
157:
158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170:
171: class XoopsNotificationHandler extends XoopsObjectHandler
172: {
173: 174: 175:
176: var $mTrigger = null;
177:
178: 179: 180:
181: var $mTriggerPreAction = null;
182:
183: function XoopsNotificationHandler(&$db)
184: {
185: parent::XoopsObjectHandler($db);
186:
187: $this->mTrigger =new XCube_Delegate();
188: $this->mTrigger->register('XoopsNotificationHandler.Trigger');
189:
190: $this->mTriggerPreAction =new XCube_Delegate();
191: $this->mTriggerPreAction->register("XoopsNotificationHandler.TriggerPreAction");
192: }
193:
194: protected function _escapeValue($value, $type=XOBJ_DTYPE_STRING)
195: {
196: switch ($type) {
197: case XOBJ_DTYPE_BOOL:
198: $value = $value ? "1" : "0";
199: break;
200: case XOBJ_DTYPE_INT:
201: $value = intval($value);
202: break;
203: case XOBJ_DTYPE_FLOAT:
204: $value = floatval($value);
205: break;
206: case XOBJ_DTYPE_STRING:
207: case XOBJ_DTYPE_TEXT:
208: $value = mysql_real_escape_string($value, $this->db->conn);
209: break;
210: default:
211: $value = mysql_real_escape_string($value, $this->db->conn);
212: }
213: return $value;
214: }
215:
216: 217: 218: 219: 220: 221: 222:
223: function &create($isNew = true)
224: {
225: $notification =new XoopsNotification();
226: if ($isNew) {
227: $notification->setNew();
228: }
229: return $notification;
230: }
231:
232:
233: 234: 235: 236: 237: 238: 239:
240: function &get($id)
241: {
242: $id = (int)$id;
243: $ret = false;
244: if ($id > 0) {
245: $sql = 'SELECT * FROM '.$this->db->prefix('xoopsnotifications').' WHERE not_id='.$id;
246: if ($result = $this->db->query($sql)) {
247: $numrows = $this->db->getRowsNum($result);
248: if ($numrows == 1) {
249: $notification =new XoopsNotification();
250: $notification->assignVars($this->db->fetchArray($result));
251: $ret =& $notification;
252: }
253: }
254: }
255: return $ret;
256: }
257:
258: 259: 260: 261: 262: 263: 264:
265: function insert(&$notification)
266: {
267: if (strtolower(get_class($notification)) != 'xoopsnotification') {
268: return false;
269: }
270: if (!$notification->isDirty()) {
271: return true;
272: }
273: if (!$notification->cleanVars()) {
274: return false;
275: }
276: foreach ($notification->cleanVars as $k => $v) {
277: ${$k} = $v;
278: }
279: if ($notification->isNew()) {
280: $not_id = $this->db->genId('xoopsnotifications_not_id_seq');
281: $sql = sprintf("INSERT INTO %s (not_id, not_modid, not_itemid, not_category, not_uid, not_event, not_mode) VALUES (%u, %u, %u, %s, %u, %s, %u)", $this->db->prefix('xoopsnotifications'), $not_id, $not_modid, $not_itemid, $this->db->quoteString($not_category), $not_uid, $this->db->quoteString($not_event), $not_mode);
282: } else {
283: $sql = sprintf("UPDATE %s SET not_modid = %u, not_itemid = %u, not_category = %s, not_uid = %u, not_event = %s, not_mode = %u WHERE not_id = %u", $this->db->prefix('xoopsnotifications'), $not_modid, $not_itemid, $this->db->quoteString($not_category), $not_uid, $this->db->quoteString($not_event), $not_mode, $not_id);
284: }
285: if (!$result = $this->db->query($sql)) {
286: return false;
287: }
288: if (empty($not_id)) {
289: $not_id = $this->db->getInsertId();
290: }
291: $notification->assignVar('not_id', $not_id);
292: return true;
293: }
294:
295: 296: 297: 298: 299: 300: 301:
302: function delete(&$notification)
303: {
304: if (strtolower(get_class($notification)) != 'xoopsnotification') {
305: return false;
306: }
307: $sql = sprintf("DELETE FROM %s WHERE not_id = %u", $this->db->prefix('xoopsnotifications'), $notification->getVar('not_id'));
308: if (!$result = $this->db->query($sql)) {
309: return false;
310: }
311: return true;
312: }
313:
314: 315: 316: 317: 318: 319: 320: 321:
322: function &getObjects($criteria = null, $id_as_key = false)
323: {
324: $ret = array();
325: $limit = $start = 0;
326: $sql = 'SELECT * FROM '.$this->db->prefix('xoopsnotifications');
327: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
328: $sql .= ' '.$criteria->renderWhere();
329: $sort = ($criteria->getSort() != '') ? $criteria->getSort() : 'not_id';
330: $sql .= ' ORDER BY '.$sort.' '.$criteria->getOrder();
331: $limit = $criteria->getLimit();
332: $start = $criteria->getStart();
333: }
334: $result = $this->db->query($sql, $limit, $start);
335: if (!$result) {
336: return $ret;
337: }
338: while ($myrow = $this->db->fetchArray($result)) {
339: $notification =new XoopsNotification();
340: $notification->assignVars($myrow);
341: if (!$id_as_key) {
342: $ret[] =& $notification;
343: } else {
344: $ret[$myrow['not_id']] =& $notification;
345: }
346: unset($notification);
347: }
348: return $ret;
349: }
350:
351:
352: 353: 354: 355: 356: 357: 358:
359: function getCount($criteria = null)
360: {
361: $sql = 'SELECT COUNT(*) FROM '.$this->db->prefix('xoopsnotifications');
362: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
363: $sql .= ' '.$criteria->renderWhere();
364: }
365: if (!$result =& $this->db->query($sql)) {
366: return 0;
367: }
368: list($count) = $this->db->fetchRow($result);
369: return $count;
370: }
371:
372: 373: 374: 375: 376: 377: 378:
379: function deleteAll($criteria = null)
380: {
381: $sql = 'DELETE FROM '.$this->db->prefix('xoopsnotifications');
382: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
383: $sql .= ' '.$criteria->renderWhere();
384: }
385: if (!$result = $this->db->query($sql)) {
386: return false;
387: }
388: return true;
389: }
390:
391:
392: 393: 394: 395: 396: 397: 398: 399: 400:
401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414:
415:
416:
417:
418:
419: function &getNotification ($module_id, $category, $item_id, $event, $user_id)
420: {
421: $ret = false;
422: $criteria = new CriteriaCompo();
423: $criteria->add(new Criteria('not_modid', (int)$module_id));
424: $criteria->add(new Criteria('not_category', $this->_escapeValue($category)));
425: $criteria->add(new Criteria('not_itemid', (int)$item_id));
426: $criteria->add(new Criteria('not_event', $this->_escapeValue($event)));
427: $criteria->add(new Criteria('not_uid', (int)$user_id));
428: $objects = $this->getObjects($criteria);
429: if (count($objects) == 1) {
430: $ret =& $objects[0];
431: }
432: return $ret;
433: }
434:
435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445:
446:
447: function isSubscribed ($category, $item_id, $event, $module_id, $user_id)
448: {
449: $criteria = new CriteriaCompo();
450: $criteria->add(new Criteria('not_modid', (int)$module_id));
451: $criteria->add(new Criteria('not_category', $this->_escapeValue($category)));
452: $criteria->add(new Criteria('not_itemid', (int)$item_id));
453: $criteria->add(new Criteria('not_event', $this->_escapeValue($event)));
454: $criteria->add(new Criteria('not_uid', (int)$user_id));
455: return $this->getCount($criteria);
456:
457: }
458:
459:
460:
461:
462:
463:
464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474:
475: function subscribe ($category, $item_id, $events, $mode=null, $module_id=null, $user_id=null)
476: {
477: if (!isset($user_id)) {
478: global $xoopsUser;
479: if (empty($xoopsUser)) {
480: return false;
481: } else {
482: $user_id = $xoopsUser->getVar('uid');
483: }
484: }
485:
486: if (!isset($module_id)) {
487: global $xoopsModule;
488: $module_id = $xoopsModule->getVar('mid');
489: }
490:
491: if (!isset($mode)) {
492: $user = new XoopsUser($user_id);
493: $mode = $user->getVar('notify_mode');
494: }
495:
496: if (!is_array($events)) $events = array($events);
497: foreach ($events as $event) {
498: if ($notification =& $this->getNotification($module_id, $category, $item_id, $event, $user_id)) {
499: if ($notification->getVar('not_mode') != $mode) {
500: $this->updateByField($notification, 'not_mode', $mode);
501: }
502: } else {
503: $notification =& $this->create();
504: $notification->setVar('not_modid', $module_id);
505: $notification->setVar('not_category', $this->_escapeValue($category));
506: $notification->setVar('not_itemid', $item_id);
507: $notification->setVar('not_uid', $user_id);
508: $notification->setVar('not_event', $this->_escapeValue($event));
509: $notification->setVar('not_mode', $mode);
510: $this->insert($notification);
511: }
512: }
513: }
514:
515:
516:
517:
518:
519:
520: 521: 522: 523: 524: 525: 526:
527: function &getByUser ($user_id)
528: {
529: $criteria = new Criteria ('not_uid', $user_id);
530: return $this->getObjects($criteria, true);
531: }
532:
533:
534: 535: 536: 537:
538: function &getSubscribedEvents ($category, $item_id, $module_id, $user_id)
539: {
540: $criteria = new CriteriaCompo();
541: $criteria->add (new Criteria('not_modid', $module_id));
542: $criteria->add (new Criteria('not_category', $this->_escapeValue($category)));
543: if ($item_id) {
544: $criteria->add (new Criteria('not_itemid', $item_id));
545: }
546: $criteria->add (new Criteria('not_uid', $user_id));
547: $results = $this->getObjects($criteria, true);
548: $ret = array();
549: foreach (array_keys($results) as $i) {
550: $ret[] = $results[$i]->getVar('not_event');
551: }
552: return $ret;
553: }
554:
555:
556: 557: 558: 559: 560: 561: 562: 563: 564:
565: function &getByItemId($module_id, $item_id, $order = null, $status = null)
566: {
567: $criteria = new CriteriaCompo(new Criteria('com_modid', (int)$module_id));
568: $criteria->add(new Criteria('com_itemid', (int)$item_id));
569: if (isset($status)) {
570: $criteria->add(new Criteria('com_status', (int)$status));
571: }
572: if (isset($order)) {
573: $criteria->setOrder($order);
574: }
575: $ret =& $this->getObjects($criteria);
576: return $ret;
577: }
578:
579:
580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591:
592:
593:
594:
595:
596:
597:
598:
599: function triggerEvents ($category, $item_id, $events, $extra_tags=array(), $user_list=array(), $module_id=null, $omit_user_id=null)
600: {
601: if (!is_array($events)) {
602: $events = array($events);
603: }
604: foreach ($events as $event) {
605: $this->triggerEvent($category, $item_id, $event, $extra_tags, $user_list, $module_id, $omit_user_id);
606: }
607: }
608:
609: function triggerEvent ($category, $item_id, $event, $extra_tags=array(), $user_list=array(), $module_id=null, $omit_user_id=null)
610: {
611:
612: if (!isset($module_id)) {
613: global $xoopsModule;
614: $module =& $xoopsModule;
615: $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
616: } else {
617: $module_handler = xoops_gethandler('module');
618: $module =& $module_handler->get($module_id);
619: }
620:
621: $not_config = $module->getInfo('notification');
622:
623: $event_correct = false;
624: foreach ($not_config['event'] as $event_config) {
625: if (($event_config['name'] == $event)&&($event_config['category'] == $category)) {
626: $event_correct = true;
627: break;
628: }
629: }
630:
631:
632:
633:
634:
635:
636:
637:
638:
639:
640:
641:
642:
643: if ($event_correct) {
644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654:
655:
656: $force_return = false;
657:
658: $this->mTriggerPreAction->call(new XCube_Ref($category), new XCube_Ref($event), new XCube_Ref($item_id),
659: new XCube_Ref($extra_tags), new XCube_Ref($module), new XCube_Ref($user_list),
660: new XCube_Ref($omit_user_id), new XCube_Ref($not_config),
661: new XCube_Ref($force_return));
662:
663: $this->mTrigger->call($category, $event, $item_id, $extra_tags, new XCube_Ref($module), $user_list, $omit_user_id, $not_config, new XCube_Ref($force_return));
664: if ($force_return) {
665: return;
666: }
667: }
668:
669:
670: $config_handler = xoops_gethandler('config');
671: $mod_config =& $config_handler->getConfigsByCat(0,$module->getVar('mid'));
672: if (empty($mod_config['notification_enabled'])) {
673: return false;
674: }
675: $category_info =& notificationCategoryInfo ($category, $module_id);
676: $event_info =& notificationEventInfo ($category, $event, $module_id);
677: if (!in_array(notificationGenerateConfig($category_info,$event_info,'option_name'),$mod_config['notification_events']) && empty($event_info['invisible'])) {
678: return false;
679: }
680:
681: if (!isset($omit_user_id)) {
682: global $xoopsUser;
683: if (!empty($xoopsUser)) {
684: $omit_user_id = $xoopsUser->getVar('uid');
685: } else {
686: $omit_user_id = 0;
687: }
688: }
689: $criteria = new CriteriaCompo();
690: $criteria->add(new Criteria('not_modid', (int)$module_id));
691: $criteria->add(new Criteria('not_category', $this->_escapeValue($category)));
692: $criteria->add(new Criteria('not_itemid', (int)$item_id));
693: $criteria->add(new Criteria('not_event', $this->_escapeValue($event)));
694: $mode_criteria = new CriteriaCompo();
695: $mode_criteria->add (new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDALWAYS), 'OR');
696: $mode_criteria->add (new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENDELETE), 'OR');
697: $mode_criteria->add (new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT), 'OR');
698: $criteria->add($mode_criteria);
699: if (!empty($user_list)) {
700: $user_criteria = new CriteriaCompo();
701: foreach ($user_list as $user) {
702: $user_criteria->add (new Criteria('not_uid', $user), 'OR');
703: }
704: $criteria->add($user_criteria);
705: }
706: $notifications =& $this->getObjects($criteria);
707: if (empty($notifications)) {
708: return;
709: }
710:
711:
712:
713: $tags = array();
714: if (!empty($not_config)) {
715: if (!empty($not_config['tags_file'])) {
716: $tags_file = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/' . $not_config['tags_file'];
717: if (file_exists($tags_file)) {
718: include_once $tags_file;
719: if (!empty($not_config['tags_func'])) {
720: $tags_func = $not_config['tags_func'];
721: if (function_exists($tags_func)) {
722: $tags = $tags_func($category, (int)$item_id, $event);
723: }
724: }
725: }
726: }
727:
728: if (!empty($not_config['lookup_file'])) {
729: $lookup_file = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/' . $not_config['lookup_file'];
730: if (file_exists($lookup_file)) {
731: include_once $lookup_file;
732: if (!empty($not_config['lookup_func'])) {
733: $lookup_func = $not_config['lookup_func'];
734: if (function_exists($lookup_func)) {
735: $item_info = $lookup_func($category, (int)$item_id);
736: }
737: }
738: }
739: }
740: }
741: $tags['X_ITEM_NAME'] = !empty($item_info['name']) ? $item_info['name'] : '[' . _NOT_ITEMNAMENOTAVAILABLE . ']';
742: $tags['X_ITEM_URL'] = !empty($item_info['url']) ? $item_info['url'] : '[' . _NOT_ITEMURLNOTAVAILABLE . ']';
743: $tags['X_ITEM_TYPE'] = !empty($category_info['item_name']) ? $category_info['title'] : '[' . _NOT_ITEMTYPENOTAVAILABLE . ']';
744: $tags['X_MODULE'] = $module->getVar('name', 'n');
745: $tags['X_MODULE_URL'] = XOOPS_URL . '/modules/' . $module->getVar('dirname') . '/';
746: $tags['X_NOTIFY_CATEGORY'] = $category;
747: $tags['X_NOTIFY_EVENT'] = $event;
748:
749: $template_dir = $event_info['mail_template_dir'];
750: $template = $event_info['mail_template'] . '.tpl';
751: $subject = $event_info['mail_subject'];
752:
753: foreach ($notifications as $notification) {
754: if (empty($omit_user_id) || $notification->getVar('not_uid') != $omit_user_id) {
755:
756:
757:
758: $tags['X_UNSUBSCRIBE_URL'] = XOOPS_URL . '/notifications.php';
759: $tags = array_merge ($tags, $extra_tags);
760:
761: $notification->notifyUser($template_dir, $template, $subject, $tags);
762: }
763: }
764: }
765:
766:
767: 768: 769: 770: 771: 772:
773: function unsubscribeByUser ($user_id)
774: {
775: $criteria = new Criteria('not_uid', (int)$user_id);
776: return $this->deleteAll($criteria);
777: }
778:
779:
780:
781:
782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792:
793:
794: function unsubscribe ($category, $item_id, $events, $module_id=null, $user_id=null)
795: {
796: if (!isset($user_id)) {
797: global $xoopsUser;
798: if (empty($xoopsUser)) {
799: return false;
800: } else {
801: $user_id = $xoopsUser->getVar('uid');
802: }
803: }
804:
805: if (!isset($module_id)) {
806: global $xoopsModule;
807: $module_id = $xoopsModule->getVar('mid');
808: }
809:
810: $criteria = new CriteriaCompo();
811: $criteria->add (new Criteria('not_modid', (int)$module_id));
812: $criteria->add (new Criteria('not_category', $this->_escapeValue($category)));
813: $criteria->add (new Criteria('not_itemid', (int)$item_id));
814: $criteria->add (new Criteria('not_uid', (int)$user_id));
815: if (!is_array($events)) {
816: $events = array($events);
817: }
818: $event_criteria = new CriteriaCompo();
819: foreach ($events as $event) {
820: $event_criteria->add (new Criteria('not_event', $this->_escapeValue($event)), 'OR');
821: }
822: $criteria->add($event_criteria);
823: return $this->deleteAll($criteria);
824: }
825:
826:
827:
828:
829:
830:
831: 832: 833: 834: 835: 836:
837: function unsubscribeByModule ($module_id)
838: {
839: $criteria = new Criteria('not_modid', (int)$module_id);
840: return $this->deleteAll($criteria);
841: }
842:
843:
844: 845: 846: 847: 848: 849: 850: 851: 852:
853: function unsubscribeByItem ($module_id, $category, $item_id)
854: {
855: $criteria = new CriteriaCompo();
856: $criteria->add (new Criteria('not_modid', (int)$module_id));
857: $criteria->add (new Criteria('not_category', $this->_escapeValue($category)));
858: $criteria->add (new Criteria('not_itemid', (int)$item_id));
859: return $this->deleteAll($criteria);
860: }
861:
862:
863: 864: 865: 866: 867: 868: 869: 870:
871: function doLoginMaintenance ($user_id)
872: {
873: $criteria = new CriteriaCompo();
874: $criteria->add (new Criteria('not_uid', (int)$user_id));
875: $criteria->add (new Criteria('not_mode', XOOPS_NOTIFICATION_MODE_WAITFORLOGIN));
876:
877: $notifications = $this->getObjects($criteria, true);
878: foreach ($notifications as $n) {
879: $n->setVar('not_mode', XOOPS_NOTIFICATION_MODE_SENDONCETHENWAIT);
880: $this->insert($n);
881: }
882: }
883:
884:
885: 886: 887: 888: 889: 890: 891: 892: 893:
894: function updateByField(&$notification, $field_name, $field_value)
895: {
896: $notification->unsetNew();
897: $notification->setVar($field_name, $field_value);
898: return $this->insert($notification);
899: }
900:
901:
902: }
903: ?>
904: