Example #1
0
/**
 * Send an email to a group.
 * @see pwg_mail()
 *
 * @param int $group_id
 * @param array $args - as in pwg_mail()
 *       o language_selected: filters users of the group by language [default value empty]
 * @param array $tpl - as in pwg_mail()
 * @return boolean
 */
function pwg_mail_group($group_id, $args = array(), $tpl = array())
{
    if (empty($group_id) or empty($args['content']) and empty($tpl)) {
        return false;
    }
    global $conf;
    $return = true;
    // get distinct languages of targeted users
    $query = '
SELECT DISTINCT language
  FROM ' . USER_GROUP_TABLE . ' AS ug
    INNER JOIN ' . USERS_TABLE . ' AS u
    ON ' . $conf['user_fields']['id'] . ' = ug.user_id
    INNER JOIN ' . USER_INFOS_TABLE . ' AS ui
    ON ui.user_id = ug.user_id
  WHERE group_id = ' . $group_id . '
    AND ' . $conf['user_fields']['email'] . ' <> ""';
    if (!empty($args['language_selected'])) {
        $query .= '
    AND language = \'' . $args['language_selected'] . '\'';
    }
    $query .= '
;';
    $languages = array_from_query($query, 'language');
    if (empty($languages)) {
        return $return;
    }
    foreach ($languages as $language) {
        // get subset of users in this group for a specific language
        $query = '
SELECT
    ui.user_id,
    ui.status,
    u.' . $conf['user_fields']['username'] . ' AS name,
    u.' . $conf['user_fields']['email'] . ' AS email
  FROM ' . USER_GROUP_TABLE . ' AS ug
    INNER JOIN ' . USERS_TABLE . ' AS u
    ON ' . $conf['user_fields']['id'] . ' = ug.user_id
    INNER JOIN ' . USER_INFOS_TABLE . ' AS ui
    ON ui.user_id = ug.user_id
  WHERE group_id = ' . $group_id . '
    AND ' . $conf['user_fields']['email'] . ' <> ""
    AND language = \'' . $language . '\'
;';
        $users = array_from_query($query);
        if (empty($users)) {
            continue;
        }
        switch_lang_to($language);
        foreach ($users as $u) {
            $authkey = create_user_auth_key($u['user_id'], $u['status']);
            $user_tpl = $tpl;
            if ($authkey !== false) {
                $user_tpl['assign']['LINK'] = add_url_params($tpl['assign']['LINK'], array('auth' => $authkey['auth_key']));
                if (isset($user_tpl['assign']['IMG']['link'])) {
                    $user_tpl['assign']['IMG']['link'] = add_url_params($user_tpl['assign']['IMG']['link'], array('auth' => $authkey['auth_key']));
                }
            }
            $user_args = $args;
            if ($authkey !== false) {
                $user_args['auth_key'] = $authkey['auth_key'];
            }
            $return &= pwg_mail($u['email'], $user_args, $user_tpl);
        }
        switch_lang_back();
    }
    return $return;
}
Example #2
0
/**
 * Creates an authentication key.
 *
 * @since 2.8
 * @param int $user_id
 * @return array
 */
function create_user_auth_key($user_id, $user_status = null)
{
    global $conf;
    if (0 == $conf['auth_key_duration']) {
        return false;
    }
    if (!isset($user_status)) {
        // we have to find the user status
        $query = '
SELECT
    status
  FROM ' . USER_INFOS_TABLE . '
  WHERE user_id = ' . $user_id . '
;';
        $user_infos = query2array($query);
        if (count($user_infos) == 0) {
            return false;
        }
        $user_status = $user_infos[0]['status'];
    }
    if (!in_array($user_status, array('normal', 'generic'))) {
        return false;
    }
    $candidate = generate_key(30);
    $query = '
SELECT
    COUNT(*),
    NOW(),
    ADDDATE(NOW(), INTERVAL ' . $conf['auth_key_duration'] . ' SECOND)
  FROM ' . USER_AUTH_KEYS_TABLE . '
  WHERE auth_key = \'' . $candidate . '\'
;';
    list($counter, $now, $expiration) = pwg_db_fetch_row(pwg_query($query));
    if (0 == $counter) {
        $key = array('auth_key' => $candidate, 'user_id' => $user_id, 'created_on' => $now, 'duration' => $conf['auth_key_duration'], 'expired_on' => $expiration);
        single_insert(USER_AUTH_KEYS_TABLE, $key);
        $key['auth_key_id'] = pwg_db_insert_id();
        return $key;
    } else {
        return create_user_auth_key($user_id, $user_status);
    }
}
Example #3
0
        $query = '
SELECT
    ui.user_id,
    ui.status,
    ui.language,
    u.' . $conf['user_fields']['email'] . ' AS email,
    u.' . $conf['user_fields']['username'] . ' AS username
  FROM ' . USER_INFOS_TABLE . ' AS ui
    JOIN ' . USERS_TABLE . ' AS u ON u.' . $conf['user_fields']['id'] . ' = ui.user_id
  WHERE ui.user_id IN (' . implode(',', $_POST['users']) . ')
;';
        $users = query2array($query);
        $usernames = array();
        foreach ($users as $u) {
            $usernames[] = $u['username'];
            $authkey = create_user_auth_key($u['user_id'], $u['status']);
            $user_tpl = $tpl;
            if ($authkey !== false) {
                $user_tpl['assign']['LINK'] = add_url_params($tpl['assign']['LINK'], array('auth' => $authkey['auth_key']));
                if (isset($user_tpl['assign']['IMG']['link'])) {
                    $user_tpl['assign']['IMG']['link'] = add_url_params($user_tpl['assign']['IMG']['link'], array('auth' => $authkey['auth_key']));
                }
            }
            $user_args = $args;
            if (isset($authkey)) {
                $user_args['auth_key'] = $authkey['auth_key'];
            }
            switch_lang_to($u['language']);
            pwg_mail($u['email'], $user_args, $user_tpl);
            switch_lang_back();
        }
Example #4
0
function do_action_send_mail_notification($action = 'list_to_send', $check_key_list = array(), $customize_mail_content = '')
{
    global $conf, $page, $user, $lang_info, $lang, $env_nbm;
    $return_list = array();
    if (in_array($action, array('list_to_send', 'send'))) {
        list($dbnow) = pwg_db_fetch_row(pwg_query('SELECT NOW();'));
        $is_action_send = $action == 'send';
        // disabled and null mail_address are not selected in the list
        $data_users = get_user_notifications('send', $check_key_list);
        // List all if it's define on options or on timeout
        $is_list_all_without_test = ($env_nbm['is_sendmail_timeout'] or $conf['nbm_list_all_enabled_users_to_send']);
        // Check if exist news to list user or send mails
        if (!$is_list_all_without_test or $is_action_send) {
            if (count($data_users) > 0) {
                $datas = array();
                if (!isset($customize_mail_content)) {
                    $customize_mail_content = $conf['nbm_complementary_mail_content'];
                }
                $customize_mail_content = trigger_change('nbm_render_global_customize_mail_content', $customize_mail_content);
                // Prepare message after change language
                if ($is_action_send) {
                    $msg_break_timeout = l10n('Time to send mail is limited. Others mails are skipped.');
                } else {
                    $msg_break_timeout = l10n('Prepared time for list of users to send mail is limited. Others users are not listed.');
                }
                // Begin nbm users environment
                begin_users_env_nbm($is_action_send);
                foreach ($data_users as $nbm_user) {
                    if (!$is_action_send and check_sendmail_timeout()) {
                        // Stop fill list on 'list_to_send', if the quota is override
                        $page['infos'][] = $msg_break_timeout;
                        break;
                    }
                    if ($is_action_send and check_sendmail_timeout()) {
                        // Stop fill list on 'send', if the quota is override
                        $page['errors'][] = $msg_break_timeout;
                        break;
                    }
                    // set env nbm user
                    set_user_on_env_nbm($nbm_user, $is_action_send);
                    if ($is_action_send) {
                        $auth = null;
                        $add_url_params = array();
                        $auth_key = create_user_auth_key($nbm_user['user_id'], $nbm_user['status']);
                        if ($auth_key !== false) {
                            $auth = $auth_key['auth_key'];
                            $add_url_params['auth'] = $auth;
                        }
                        set_make_full_url();
                        // Fill return list of "treated" check_key for 'send'
                        $return_list[] = $nbm_user['check_key'];
                        if ($conf['nbm_send_detailed_content']) {
                            $news = news($nbm_user['last_send'], $dbnow, false, $conf['nbm_send_html_mail'], $auth);
                            $exist_data = count($news) > 0;
                        } else {
                            $exist_data = news_exists($nbm_user['last_send'], $dbnow);
                        }
                        if ($exist_data) {
                            $subject = '[' . $conf['gallery_title'] . '] ' . l10n('New photos added');
                            // Assign current var for nbm mail
                            assign_vars_nbm_mail_content($nbm_user);
                            if (!is_null($nbm_user['last_send'])) {
                                $env_nbm['mail_template']->assign('content_new_elements_between', array('DATE_BETWEEN_1' => $nbm_user['last_send'], 'DATE_BETWEEN_2' => $dbnow));
                            } else {
                                $env_nbm['mail_template']->assign('content_new_elements_single', array('DATE_SINGLE' => $dbnow));
                            }
                            if ($conf['nbm_send_detailed_content']) {
                                $env_nbm['mail_template']->assign('global_new_lines', $news);
                            }
                            $nbm_user_customize_mail_content = trigger_change('nbm_render_user_customize_mail_content', $customize_mail_content, $nbm_user);
                            if (!empty($nbm_user_customize_mail_content)) {
                                $env_nbm['mail_template']->assign('custom_mail_content', $nbm_user_customize_mail_content);
                            }
                            if ($conf['nbm_send_html_mail'] and $conf['nbm_send_recent_post_dates']) {
                                $recent_post_dates = get_recent_post_dates_array($conf['recent_post_dates']['NBM']);
                                foreach ($recent_post_dates as $date_detail) {
                                    $env_nbm['mail_template']->append('recent_posts', array('TITLE' => get_title_recent_post_date($date_detail), 'HTML_DATA' => get_html_description_recent_post_date($date_detail, $auth)));
                                }
                            }
                            $env_nbm['mail_template']->assign(array('GOTO_GALLERY_TITLE' => $conf['gallery_title'], 'GOTO_GALLERY_URL' => add_url_params(get_gallery_home_url(), $add_url_params), 'SEND_AS_NAME' => $env_nbm['send_as_name']));
                            $ret = pwg_mail(array('name' => stripslashes($nbm_user['username']), 'email' => $nbm_user['mail_address']), array('from' => $env_nbm['send_as_mail_formated'], 'subject' => $subject, 'email_format' => $env_nbm['email_format'], 'content' => $env_nbm['mail_template']->parse('notification_by_mail', true), 'content_format' => $env_nbm['email_format'], 'auth_key' => $auth));
                            if ($ret) {
                                inc_mail_sent_success($nbm_user);
                                $datas[] = array('user_id' => $nbm_user['user_id'], 'last_send' => $dbnow);
                            } else {
                                inc_mail_sent_failed($nbm_user);
                            }
                            unset_make_full_url();
                        }
                    } else {
                        if (news_exists($nbm_user['last_send'], $dbnow)) {
                            // Fill return list of "selected" users for 'list_to_send'
                            $return_list[] = $nbm_user;
                        }
                    }
                    // unset env nbm user
                    unset_user_on_env_nbm();
                }
                // Restore nbm environment
                end_users_env_nbm();
                if ($is_action_send) {
                    mass_updates(USER_MAIL_NOTIFICATION_TABLE, array('primary' => array('user_id'), 'update' => array('last_send')), $datas);
                    display_counter_info();
                }
            } else {
                if ($is_action_send) {
                    $page['errors'][] = l10n('No user to send notifications by mail.');
                }
            }
        } else {
            // Quick List, don't check news
            // Fill return list of "selected" users for 'list_to_send'
            $return_list = $data_users;
        }
    }
    // Return list of "selected" users for 'list_to_send'
    // Return list of "treated" check_key for 'send'
    return $return_list;
}