Exemplo n.º 1
0
    /**
     * Gallery Notification
     *
     * borrowed from phpBB3
     * @author: phpBB Group
     * @function: user_notification
     */
    public static function notification($mode, $handle_id, $image_name)
    {
        global $user, $db, $album_id, $image_id, $image_data, $album_data;
        $help_mode = $mode . '_id';
        $mode_id = ${$help_mode};
        $mode_notification = $mode == 'album' ? 'image' : 'comment';
        // Get banned User ID's
        $sql = 'SELECT ban_userid
			FROM ' . BANLIST_TABLE . '
			WHERE ban_userid <> 0
				AND ban_exclude <> 1';
        $result = $db->sql_query($sql);
        $sql_ignore_users = ANONYMOUS . ', ' . $user->data['user_id'];
        while ($row = $db->sql_fetchrow($result)) {
            $sql_ignore_users .= ', ' . (int) $row['ban_userid'];
        }
        $db->sql_freeresult($result);
        $notify_rows = array();
        // -- get album_userids	|| image_userids
        $sql = 'SELECT u.user_id, u.username, u.user_email, u.user_lang, u.user_notify_type, u.user_jabber
			FROM ' . GALLERY_WATCH_TABLE . ' w, ' . USERS_TABLE . ' u
			WHERE w.' . $help_mode . ' = ' . $handle_id . "\n\t\t\t\tAND w.user_id NOT IN ({$sql_ignore_users})\n\t\t\t\tAND u.user_type IN (" . USER_NORMAL . ', ' . USER_FOUNDER . ')
				AND u.user_id = w.user_id';
        $result = $db->sql_query($sql);
        while ($row = $db->sql_fetchrow($result)) {
            $notify_rows[$row['user_id']] = array('user_id' => $row['user_id'], 'username' => $row['username'], 'user_email' => $row['user_email'], 'user_jabber' => $row['user_jabber'], 'user_lang' => $row['user_lang'], 'notify_type' => $mode != 'album' ? 'image' : 'album', 'template' => "new{$mode_notification}_notify", 'method' => $row['user_notify_type'], 'allowed' => false);
        }
        $db->sql_freeresult($result);
        if (!sizeof($notify_rows)) {
            return;
        }
        // Get album_user_id to check for personal albums.
        $sql = 'SELECT album_id, album_user_id
			FROM ' . GALLERY_ALBUMS_TABLE . '
			WHERE album_id = ' . $album_id;
        $result = $db->sql_query($sql);
        $album = $db->sql_fetchrow($result);
        $db->sql_freeresult($result);
        if (empty($album)) {
            trigger_error('ALBUM_NOT_EXIST');
        }
        // Make sure users are allowed to view the album
        $i_view_ary = $groups_ary = $groups_row = array();
        $sql_array = array('SELECT' => 'pr.i_view, p.perm_system, p.perm_group_id, p.perm_user_id', 'FROM' => array(GALLERY_PERMISSIONS_TABLE => 'p'), 'LEFT_JOIN' => array(array('FROM' => array(GALLERY_ROLES_TABLE => 'pr'), 'ON' => 'p.perm_role_id = pr.role_id')), 'WHERE' => $album['album_user_id'] == phpbb_gallery_album::PUBLIC_ALBUM ? 'p.perm_album_id = ' . $album_id : 'p.perm_system <> ' . phpbb_gallery_album::PUBLIC_ALBUM, 'ORDER_BY' => 'pr.i_view ASC');
        $sql = $db->sql_build_query('SELECT', $sql_array);
        $result = $db->sql_query($sql);
        while ($row = $db->sql_fetchrow($result)) {
            if ($row['perm_group_id']) {
                $groups_ary[] = $row['perm_group_id'];
                $groups_row[$row['perm_group_id']] = $row;
            } else {
                if (!isset($i_view_ary[$row['perm_user_id']]) || isset($i_view_ary[$row['perm_user_id']]) && $i_view_ary[$row['perm_user_id']] < $row['i_view']) {
                    if (!$row['perm_system']) {
                        $i_view_ary[$row['perm_user_id']] = $row['i_view'];
                    } elseif ($row['perm_system'] == phpbb_gallery_auth::OWN_ALBUM && $album['album_user_id'] == $row['perm_user_id']) {
                        $i_view_ary[$row['perm_user_id']] = $row['i_view'];
                    } elseif ($row['perm_system'] == phpbb_gallery_auth::PERSONAL_ALBUM && $album['album_user_id'] != $row['perm_user_id']) {
                        $i_view_ary[$row['perm_user_id']] = $row['i_view'];
                    }
                }
            }
        }
        $db->sql_freeresult($result);
        if (sizeof($groups_ary)) {
            // Get all users by their group permissions
            $sql = 'SELECT user_id, group_id
				FROM ' . USER_GROUP_TABLE . '
				WHERE ' . $db->sql_in_set('group_id', $groups_ary) . '
					AND user_pending = 0';
            $result = $db->sql_query($sql);
            while ($row = $db->sql_fetchrow($result)) {
                if (!isset($i_view_ary[$row['user_id']]) || isset($i_view_ary[$row['user_id']]) && $i_view_ary[$row['user_id']] < $groups_row[$row['group_id']]['i_view']) {
                    if (!$groups_row[$row['group_id']]['perm_system']) {
                        $i_view_ary[$row['user_id']] = $groups_row[$row['group_id']]['i_view'];
                    } else {
                        if ($groups_row[$row['group_id']]['perm_system'] == phpbb_gallery_auth::OWN_ALBUM && $album['album_user_id'] == $row['user_id']) {
                            $i_view_ary[$row['user_id']] = $groups_row[$row['group_id']]['i_view'];
                        } else {
                            if ($groups_row[$row['group_id']]['perm_system'] == phpbb_gallery_auth::PERSONAL_ALBUM && $album['album_user_id'] != $row['user_id']) {
                                $i_view_ary[$row['user_id']] = $groups_row[$row['group_id']]['i_view'];
                            }
                        }
                    }
                }
            }
            $db->sql_freeresult($result);
        }
        // Now, we have to do a little step before really sending, we need to distinguish our users a little bit. ;)
        $msg_users = $delete_ids = $update_notification = array();
        foreach ($notify_rows as $user_id => $row) {
            if ($i_view_ary[$row['user_id']] != phpbb_gallery_auth::ACL_YES || !trim($row['user_email'])) {
                $delete_ids[$row['notify_type']][] = $row['user_id'];
            } else {
                $msg_users[] = $row;
                $update_notification[$row['notify_type']][] = $row['user_id'];
            }
        }
        unset($notify_rows);
        // Now, we are able to really send out notifications
        if (sizeof($msg_users)) {
            if (!class_exists('messenger')) {
                phpbb_gallery_url::_include('functions_messenger', 'phpbb');
            }
            $messenger = new messenger();
            $msg_list_ary = array();
            foreach ($msg_users as $row) {
                $pos = !isset($msg_list_ary[$row['template']]) ? 0 : sizeof($msg_list_ary[$row['template']]);
                $msg_list_ary[$row['template']][$pos]['method'] = $row['method'];
                $msg_list_ary[$row['template']][$pos]['email'] = $row['user_email'];
                $msg_list_ary[$row['template']][$pos]['jabber'] = $row['user_jabber'];
                $msg_list_ary[$row['template']][$pos]['name'] = $row['username'];
                $msg_list_ary[$row['template']][$pos]['lang'] = $row['user_lang'];
            }
            unset($msg_users);
            foreach ($msg_list_ary as $email_template => $email_list) {
                foreach ($email_list as $addr) {
                    $messenger->template($email_template, $addr['lang']);
                    $messenger->to($addr['email'], $addr['name']);
                    $messenger->im($addr['jabber'], $addr['name']);
                    $messenger->assign_vars(array('USERNAME' => htmlspecialchars_decode($addr['name']), 'IMAGE_NAME' => htmlspecialchars_decode($image_name), 'ALBUM_NAME' => htmlspecialchars_decode($album_data['album_name']), 'U_ALBUM' => phpbb_gallery_url::create_link('full', 'album', "album_id={$album_id}"), 'U_IMAGE' => phpbb_gallery_url::create_link('full', 'image_page', "album_id={$album_id}&amp;image_id={$image_id}"), 'U_NEWEST_POST' => phpbb_gallery_url::create_link('full', 'viewtopic', "album_id={$album_id}&amp;image_id={$image_id}"), 'U_STOP_WATCHING_IMAGE' => phpbb_gallery_url::create_link('full', 'posting', "mode=image&amp;submode=unwatch&amp;album_id={$album_id}&amp;image_id={$image_id}"), 'U_STOP_WATCHING_ALBUM' => phpbb_gallery_url::create_link('full', 'posting', "mode=album&amp;submode=unwatch&amp;album_id={$album_id}")));
                    $messenger->send($addr['method']);
                }
            }
            unset($msg_list_ary);
            $messenger->save_queue();
        }
        // Now delete the user_ids not authorised to receive notifications on this image/album
        if (!empty($delete_ids['image'])) {
            $sql = 'DELETE FROM ' . GALLERY_WATCH_TABLE . "\n\t\t\t\tWHERE image_id = {$image_id}\n\t\t\t\t\tAND " . $db->sql_in_set('user_id', $delete_ids['image']);
            $db->sql_query($sql);
        }
        if (!empty($delete_ids['album'])) {
            $sql = 'DELETE FROM ' . GALLERY_WATCH_TABLE . "\n\t\t\t\tWHERE album_id = {$album_id}\n\t\t\t\t\tAND " . $db->sql_in_set('user_id', $delete_ids['album']);
            $db->sql_query($sql);
        }
    }