1: <?php
2: /**
3: * XOOPS Notifications
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: * @subpackage Xoop Notifications Functions
16: * @since 2.0.0
17: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
18: */
19:
20: use Xmf\Request;
21:
22: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
23:
24: // RMV-NOTIFY
25:
26: // FIXME: Do some caching, so we don't retrieve the same category / event
27: // info many times.
28:
29: /**
30: * Determine if notification is enabled for the selected module.
31: *
32: * @param string $style Subscription style: 'block' or 'inline'
33: * @param int $module_id ID of the module (default current module)
34: * @return bool
35: */
36: function notificationEnabled($style, $module_id = null)
37: {
38: if (isset($GLOBALS['xoopsModuleConfig']['notification_enabled'])) {
39: $status = $GLOBALS['xoopsModuleConfig']['notification_enabled'];
40: } else {
41: if (!isset($module_id)) {
42: return false;
43: }
44: /** @var XoopsModuleHandler $module_handler */
45: $module_handler = xoops_getHandler('module');
46: $module = $module_handler->get($module_id);
47: if (!empty($module) && $module->getVar('hasnotification') == 1) {
48: /** @var XoopsConfigHandler $config_handler */
49: $config_handler = xoops_getHandler('config');
50: $config = $config_handler->getConfigsByCat(0, $module_id);
51: $status = $config['notification_enabled'];
52: } else {
53: return false;
54: }
55: }
56: include_once $GLOBALS['xoops']->path('include/notification_constants.php');
57: if (($style === 'block') && ($status === XOOPS_NOTIFICATION_ENABLEBLOCK || $status === XOOPS_NOTIFICATION_ENABLEBOTH)) {
58: return true;
59: }
60:
61: return ($style === 'inline') && ($status === XOOPS_NOTIFICATION_ENABLEINLINE || $status === XOOPS_NOTIFICATION_ENABLEBOTH);
62: }
63:
64: /**
65: * Get an associative array of info for a particular notification
66: * category in the selected module. If no category is selected,
67: * return an array of info for all categories.
68: *
69: * @param string $category_name
70: * @param int $module_id ID of the module (default current module)
71: *
72: * @internal param string $name Category name (default all categories)
73: * @return mixed
74: */
75: function &notificationCategoryInfo($category_name = '', $module_id = null)
76: {
77: if (!isset($module_id)) {
78: global $xoopsModule;
79: $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
80: $module =& $xoopsModule;
81: } else {
82: /** @var XoopsModuleHandler $module_handler */
83: $module_handler = xoops_getHandler('module');
84: $module = $module_handler->get($module_id);
85: }
86: $not_config = &$module->getInfo('notification');
87: if (empty($category_name)) {
88: return $not_config['category'];
89: }
90: foreach ($not_config['category'] as $category) {
91: if ($category['name'] == $category_name) {
92: return $category;
93: }
94: }
95: $ret = false;
96:
97: return $ret;
98: }
99:
100: /**
101: * Get associative array of info for the category to which comment events
102: * belong.
103: *
104: * @todo This could be more efficient... maybe specify in
105: * $modversion['comments'] the notification category.
106: * This would also serve as a way to enable notification
107: * of comments, and also remove the restriction that
108: * all notification categories must have unique item_name. (TODO)
109: *
110: * @param int $module_id ID of the module (default current module)
111: * @return mixed Associative array of category info
112: */
113: function &notificationCommentCategoryInfo($module_id = null)
114: {
115: $ret = false;
116: $all_categories =& notificationCategoryInfo('', $module_id);
117: if (empty($all_categories)) {
118: return $ret;
119: }
120: foreach ($all_categories as $category) {
121: $all_events =& notificationEvents($category['name'], false, $module_id);
122: if (empty($all_events)) {
123: continue;
124: }
125: foreach ($all_events as $event) {
126: if ($event['name'] === 'comment') {
127: return $category;
128: }
129: }
130: }
131:
132: return $ret;
133: }
134:
135: // TODO: some way to include or exclude admin-only events...
136:
137: /**
138: * Get an array of info for all events (each event has associative array)
139: * in the selected category of the selected module.
140: *
141: * @param string $category_name Category name
142: * @param bool $enabled_only If true, return only enabled events
143: * @param int $module_id ID of the module (default current module)
144: * @return mixed
145: */
146: function &notificationEvents($category_name, $enabled_only, $module_id = null)
147: {
148: if (!isset($module_id)) {
149: global $xoopsModule;
150: $module_id = !empty($xoopsModule) ? $xoopsModule->getVar('mid') : 0;
151: $module =& $xoopsModule;
152: } else {
153: /** @var XoopsModuleHandler $module_handler */
154: $module_handler = xoops_getHandler('module');
155: $module = $module_handler->get($module_id);
156: }
157: $not_config = $module->getInfo('notification');
158: /** @var XoopsConfigHandler $config_handler */
159: $config_handler = xoops_getHandler('config');
160: $mod_config = $config_handler->getConfigsByCat(0, $module_id);
161:
162: $category =& notificationCategoryInfo($category_name, $module_id);
163:
164: global $xoopsConfig;
165: $event_array = array();
166:
167: $override_comment = false;
168: $override_commentsubmit = false;
169: $override_bookmark = false;
170:
171: foreach ($not_config['event'] as $event) {
172: if ($event['category'] == $category_name) {
173: if (!is_dir($dir = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/' . $xoopsConfig['language'] . '/mail_template/')) {
174: $dir = XOOPS_ROOT_PATH . '/modules/' . $module->getVar('dirname') . '/language/english/mail_template/';
175: }
176: $event['mail_template_dir'] = $dir;
177: if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
178: $event_array[] = $event;
179: }
180: if ($event['name'] === 'comment') {
181: $override_comment = true;
182: }
183: if ($event['name'] === 'comment_submit') {
184: $override_commentsubmit = true;
185: }
186: if ($event['name'] === 'bookmark') {
187: $override_bookmark = true;
188: }
189: }
190: }
191:
192: xoops_loadLanguage('notification');
193: // Insert comment info if applicable
194:
195: if ($module->getVar('hascomments')) {
196: $com_config = $module->getInfo('comments');
197: if (!empty($category['item_name']) && $category['item_name'] == $com_config['itemName']) {
198: if (!is_dir($dir = XOOPS_ROOT_PATH . '/language/' . $xoopsConfig['language'] . '/mail_template/')) {
199: $dir = XOOPS_ROOT_PATH . '/language/english/mail_template/';
200: }
201: $mail_template_dir = $dir;
202:
203: include_once $GLOBALS['xoops']->path('include/comment_constants.php');
204: /** @var \XoopsConfigHandler $config_handler */
205: $config_handler = xoops_getHandler('config');
206: $com_config = $config_handler->getConfigsByCat(0, $module_id);
207: if (!$enabled_only) {
208: $insert_comment = true;
209: $insert_submit = true;
210: } else {
211: $insert_comment = false;
212: $insert_submit = false;
213: switch ($com_config['com_rule']) {
214: case XOOPS_COMMENT_APPROVENONE:
215: // comments disabled, no comment events
216: break;
217: case XOOPS_COMMENT_APPROVEALL:
218: // all comments are automatically approved, no 'submit'
219: if (!$override_comment) {
220: $insert_comment = true;
221: }
222: break;
223: case XOOPS_COMMENT_APPROVEUSER:
224: case XOOPS_COMMENT_APPROVEADMIN:
225: // comments first submitted, require later approval
226: if (!$override_comment) {
227: $insert_comment = true;
228: }
229: if (!$override_commentsubmit) {
230: $insert_submit = true;
231: }
232: break;
233: }
234: }
235: if ($insert_comment) {
236: $event = array(
237: 'name' => 'comment',
238: 'category' => $category['name'],
239: 'title' => _NOT_COMMENT_NOTIFY,
240: 'caption' => _NOT_COMMENT_NOTIFYCAP,
241: 'description' => _NOT_COMMENT_NOTIFYDSC,
242: 'mail_template_dir' => $mail_template_dir,
243: 'mail_template' => 'comment_notify',
244: 'mail_subject' => _NOT_COMMENT_NOTIFYSBJ);
245: if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
246: $event_array[] = $event;
247: }
248: }
249: if ($insert_submit) {
250: $event = array(
251: 'name' => 'comment_submit',
252: 'category' => $category['name'],
253: 'title' => _NOT_COMMENTSUBMIT_NOTIFY,
254: 'caption' => _NOT_COMMENTSUBMIT_NOTIFYCAP,
255: 'description' => _NOT_COMMENTSUBMIT_NOTIFYDSC,
256: 'mail_template_dir' => $mail_template_dir,
257: 'mail_template' => 'commentsubmit_notify',
258: 'mail_subject' => _NOT_COMMENTSUBMIT_NOTIFYSBJ,
259: 'admin_only' => 1);
260: if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
261: $event_array[] = $event;
262: }
263: }
264: }
265: }
266:
267: // Insert bookmark info if appropriate
268:
269: if (!empty($category['allow_bookmark'])) {
270: if (!$override_bookmark) {
271: $event = array(
272: 'name' => 'bookmark',
273: 'category' => $category['name'],
274: 'title' => _NOT_BOOKMARK_NOTIFY,
275: 'caption' => _NOT_BOOKMARK_NOTIFYCAP,
276: 'description' => _NOT_BOOKMARK_NOTIFYDSC);
277: if (!$enabled_only || notificationEventEnabled($category, $event, $module)) {
278: $event_array[] = $event;
279: }
280: }
281: }
282:
283: return $event_array;
284: }
285:
286: /**
287: * Determine whether a particular notification event is enabled.
288: * Depends on module config options.
289: *
290: * @todo Check that this works correctly for comment and other
291: * events which depend on additional config options...
292: *
293: * @param array $category Category info array
294: * @param array $event Event info array
295: * @param object $module Module
296: * @return bool
297: **/
298: function notificationEventEnabled(&$category, &$event, &$module)
299: {
300: /** @var XoopsConfigHandler $config_handler */
301: $config_handler = xoops_getHandler('config');
302: $mod_config = $config_handler->getConfigsByCat(0, $module->getVar('mid'));
303:
304: if (is_array($mod_config['notification_events']) && $mod_config['notification_events'] != array()) {
305: $option_name = notificationGenerateConfig($category, $event, 'option_name');
306: if (in_array($option_name, $mod_config['notification_events'])) {
307: return true;
308: }
309: $notification_handler = xoops_getHandler('notification');
310: }
311:
312: return false;
313: }
314:
315: /**
316: * Get associative array of info for the selected event in the selected
317: * category (for the selected module).
318: *
319: * @param string $category_name Notification category
320: * @param string $event_name Notification event
321: * @param int $module_id ID of the module (default current module)
322: * @return mixed
323: */
324: function &notificationEventInfo($category_name, $event_name, $module_id = null)
325: {
326: $all_events =& notificationEvents($category_name, false, $module_id);
327: foreach ($all_events as $event) {
328: if ($event['name'] == $event_name) {
329: return $event;
330: }
331: }
332: $ret = false;
333:
334: return $ret;
335: }
336:
337: /**
338: * Get an array of associative info arrays for subscribable categories
339: * for the selected module.
340: *
341: * @param int $module_id ID of the module
342: * @return mixed
343: */
344:
345: function &notificationSubscribableCategoryInfo($module_id = null)
346: {
347: $all_categories =& notificationCategoryInfo('', $module_id);
348:
349: // FIXME: better or more standardized way to do this?
350: $script_url = explode('/', $_SERVER['PHP_SELF']);
351: $script_name = $script_url[count($script_url) - 1];
352:
353: $sub_categories = array();
354: if (null != $all_categories) {
355: foreach ($all_categories as $category) {
356: // Check the script name
357: $subscribe_from = $category['subscribe_from'];
358: if (!is_array($subscribe_from)) {
359: if ($subscribe_from === '*') {
360: $subscribe_from = array(
361: $script_name);
362: // FIXME: this is just a hack: force a match
363: } else {
364: $subscribe_from = array(
365: $subscribe_from);
366: }
367: }
368: if (!in_array($script_name, $subscribe_from)) {
369: continue;
370: }
371: // If 'item_name' is missing, automatic match. Otherwise,
372: // check if that argument exists...
373: if (empty($category['item_name'])) {
374: $category['item_name'] = '';
375: $category['item_id'] = 0;
376: $sub_categories[] = $category;
377: } else {
378: $item_name = $category['item_name'];
379: $id = ($item_name != '' && isset($_GET[$item_name])) ? Request::getInt($item_name, 0, 'GET'): 0;
380: if ($id > 0) {
381: $category['item_id'] = $id;
382: $sub_categories[] = $category;
383: }
384: }
385: }
386: }
387: return $sub_categories;
388: }
389:
390: /**
391: * Generate module config info for a particular category, event pair.
392: * The selectable config options are given names depending on the
393: * category and event names, and the text depends on the category
394: * and event titles. These are pieced together in this function in
395: * case we wish to alter the syntax.
396: *
397: * @param array $category Array of category info
398: * @param array $event Array of event info
399: * @param string $type The particular name to generate
400: * return string
401: * *
402: *
403: * @return bool|string
404: */
405: function notificationGenerateConfig(&$category, &$event, $type)
406: {
407: switch ($type) {
408: case 'option_value':
409: case 'name':
410: return 'notify:' . $category['name'] . '-' . $event['name'];
411: break;
412: case 'option_name':
413: return $category['name'] . '-' . $event['name'];
414: break;
415: default:
416: return false;
417: break;
418: }
419: }
420: