1:   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:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 
<?php

/**
 * This file contains just the functions that turn on and off notifications
 * to topics or boards.
 *
 * Simple Machines Forum (SMF)
 *
 * @package SMF
 * @author Simple Machines http://www.simplemachines.org
 * @copyright 2019 Simple Machines and individual contributors
 * @license http://www.simplemachines.org/about/smf/license.php BSD
 *
 * @version 2.1 RC1
 */

if (!defined('SMF'))
    die('No direct access...');

/**
 * Turn off/on notification for a particular board.
 * Must be called with a board specified in the URL.
 * Only uses the template if no sub action is used. (on/off)
 * Redirects the user back to the board after it is done.
 * Accessed via ?action=notifyboard.
 *
 * uses Notify template, notify_board sub-template.
 */
function BoardNotify()
{
    global $board, $user_info, $context, $smcFunc, $sourcedir;

    // Permissions are an important part of anything ;).
    is_not_guest();

    // You have to specify a board to turn notifications on!
    if (empty($board))
        fatal_lang_error('no_board', false);

    // No subaction: find out what to do.
    if (isset($_GET['mode']))
    {
        checkSession('get');

        $mode = (int) $_GET['mode'];
        $alertPref = $mode <= 1 ? 0 : ($mode == 2 ? 1 : 3);

        require_once($sourcedir . '/Subs-Notify.php');
        setNotifyPrefs($user_info['id'], array('board_notify_' . $board => $alertPref));

        if ($mode > 1)
            // Turn notification on.  (note this just blows smoke if it's already on.)
            $smcFunc['db_insert']('ignore',
                '{db_prefix}log_notify',
                array('id_member' => 'int', 'id_board' => 'int'),
                array($user_info['id'], $board),
                array('id_member', 'id_board')
            );
        else
            $smcFunc['db_query']('', '
                DELETE FROM {db_prefix}log_notify
                WHERE id_member = {int:current_member}
                    AND id_board = {int:current_board}',
                array(
                    'current_board' => $board,
                    'current_member' => $user_info['id'],
                )
            );
    }

    // Back to the board!
    if (isset($_GET['xml']))
    {
        $context['xml_data']['errors'] = array(
            'identifier' => 'error',
            'children' => array(
                array(
                    'value' => 0,
                ),
            ),
        );
        $context['sub_template'] = 'generic_xml';
    }
    else
        redirectexit('board=' . $board . '.' . $_REQUEST['start']);
}

/**
 * Turn off/on unread replies subscription for a topic as well as sets individual topic's alert preferences
 * Must be called with a topic specified in the URL.
 * The mode can be from 0 to 3
 * 0 => unwatched, 1 => no alerts/emails, 2 => alerts, 3 => emails/alerts
 * Upon successful completion of action will direct user back to topic.
 * Accessed via ?action=unwatchtopic.
 */
function TopicNotify()
{
    global $smcFunc, $user_info, $topic, $sourcedir, $context;

    // Let's do something only if the function is enabled
    if (!$user_info['is_guest'])
    {
        checkSession('get');

        if (isset($_GET['mode']))
        {
            $mode = (int) $_GET['mode'];
            $alertPref = $mode <= 1 ? 0 : ($mode == 2 ? 1 : 3);

            $request = $smcFunc['db_query']('', '
                SELECT id_member, id_topic, id_msg, unwatched
                FROM {db_prefix}log_topics
                WHERE id_member = {int:current_user}
                    AND id_topic = {int:current_topic}',
                array(
                    'current_user' => $user_info['id'],
                    'current_topic' => $topic,
                )
            );
            $log = $smcFunc['db_fetch_assoc']($request);
            $smcFunc['db_free_result']($request);
            if (empty($log))
            {
                $insert = true;
                $log = array(
                    'id_member' => $user_info['id'],
                    'id_topic' => $topic,
                    'id_msg' => 0,
                    'unwatched' => empty($mode) ? 1 : 0,
                );
            }
            else
            {
                $insert = false;
                $log['unwatched'] = empty($mode) ? 1 : 0;
            }

            $smcFunc['db_insert']($insert ? 'insert' : 'replace',
                '{db_prefix}log_topics',
                array(
                    'id_member' => 'int', 'id_topic' => 'int', 'id_msg' => 'int', 'unwatched' => 'int',
                ),
                $log,
                array('id_member', 'id_topic')
            );

            require_once($sourcedir . '/Subs-Notify.php');
            setNotifyPrefs($user_info['id'], array('topic_notify_' . $log['id_topic'] => $alertPref));

            if ($mode > 1)
            {
                // Turn notification on.  (note this just blows smoke if it's already on.)
                $smcFunc['db_insert']('ignore',
                    '{db_prefix}log_notify',
                    array('id_member' => 'int', 'id_topic' => 'int'),
                    array($user_info['id'], $log['id_topic']),
                    array('id_member', 'id_board')
                );
            }
            else
                $smcFunc['db_query']('', '
                    DELETE FROM {db_prefix}log_notify
                    WHERE id_topic = {int:topic}
                        AND id_member = {int:member}',
                    array(
                        'topic' => $log['id_topic'],
                        'member' => $user_info['id'],
                    )
                );
        }
    }

    // Back to the topic.
    if (isset($_GET['xml']))
    {
        $context['xml_data']['errors'] = array(
            'identifier' => 'error',
            'children' => array(
                array(
                    'value' => 0,
                ),
            ),
        );
        $context['sub_template'] = 'generic_xml';
    }
    else
        redirectexit('topic=' . $topic . '.' . $_REQUEST['start']);
}

?>