/** * Find whether a particular kind of notification is available. * * @param integer The notification setting * @param ?MEMBER Member to check for (NULL: just check globally) * @return boolean Whether it is available */ function _notification_setting_available($setting, $member_id = NULL) { static $nsa_cache = array(); if (isset($nsa_cache[$setting][$member_id])) { return $nsa_cache[$setting][$member_id]; } $system_wide = false; $for_member = false; switch ($setting) { case A_INSTANT_EMAIL: $system_wide = true; if ($system_wide && !is_null($member_id)) { $for_member = $GLOBALS['FORUM_DRIVER']->get_member_email_address($member_id) != ''; } break; case A_DAILY_EMAIL_DIGEST: case A_WEEKLY_EMAIL_DIGEST: case A_MONTHLY_EMAIL_DIGEST: $system_wide = cron_installed(); if ($system_wide && !is_null($member_id)) { $for_member = $GLOBALS['FORUM_DRIVER']->get_member_email_address($member_id) != ''; } break; case A_INSTANT_SMS: $system_wide = addon_installed('sms') && get_option('sms_api_id') != ''; if ($system_wide && !is_null($member_id)) { require_code('permissions'); if (has_specific_permission($member_id, 'use_sms')) { require_code('sms'); $cpf_values = $GLOBALS['FORUM_DRIVER']->get_custom_fields($member_id); if (!is_null($cpf_values)) { if (array_key_exists('mobile_phone_number', $cpf_values)) { $for_member = cleanup_mobile_number($cpf_values['mobile_phone_number']) != ''; } } } } break; case A_INSTANT_PT: $system_wide = get_forum_type() == 'ocf' && addon_installed('ocf_forum'); if ($system_wide && !is_null($member_id)) { require_code('permissions'); $for_member = has_specific_permission($member_id, 'use_pt'); } break; } $ret = $system_wide && (is_null($member_id) || $for_member); $nsa_cache[$setting][$member_id] = $ret; return $ret; }
/** * Attempt to send an SMS. * * @param string The message * @param array The member IDs of those receiving messages * @return integer How many were sent */ function sms_wrap($message, $to_sms) { // 140 byte limit for single packet // 134*255 byte limit for multiple packets (but there's cost for each additional 134 byte segment) if (count($to_sms) == 0) { return 0; } require_lang('sms'); $is_super_admin = $GLOBALS['FORUM_DRIVER']->is_super_admin(get_member()); $api_id = xmlentities(get_option('sms_api_id')); $username = xmlentities(get_option('sms_username')); $password = xmlentities(get_option('sms_password')); $site_name = xmlentities(substr(get_site_name(), 0, 11)); if (strtolower(get_charset()) != 'utf-8' && strtolower(get_charset()) != 'utf8') { $site_name = utf8_encode($site_name); } //$callback=xmlentities(find_script('sms')); --- set on clickatell's site $callback = '0'; /* return nothing (for the moment); TODO: change to 3 (return all message statuses) */ $threshold = mktime(0, 0, 0, intval(date('m')), 0, intval(date('Y'))); // TODO: $confirmed_numbers=collapse_2d_complexity('m_phone_number','m_member_id',$GLOBALS['SITE_DB']->query_select('confirmed_mobiles',array('m_phone_number','m_member_id'),array('m_confirm_code'=>''))); // Check current user has not trigered too many $triggered_already = $GLOBALS['SITE_DB']->query_value_null_ok_full('SELECT COUNT(*) FROM ' . get_table_prefix() . 'sms_log WHERE s_time>' . strval(time() - 60 * 60 * 24 * 31) . ' AND ' . db_string_equal_to('s_trigger_ip', get_ip_address(2))); $trigger_limit = intval(get_option('sms_' . (has_specific_permission(get_member(), 'sms_higher_trigger_limit') ? 'high' : 'low') . '_trigger_limit')); if ($triggered_already + count($to_sms) > $trigger_limit) { return 0; } $num_sent = 0; foreach ($to_sms as $to_member) { if (!has_specific_permission($to_member, 'use_sms')) { continue; } // Check that not one over quota $sent_in_month = $GLOBALS['SITE_DB']->query_value_null_ok_full('SELECT COUNT(*) FROM ' . $GLOBALS['SITE_DB']->get_table_prefix() . 'sms_log WHERE s_member_id=' . strval($to_member) . ' AND s_time>' . strval($threshold)); $limit = intval(get_option('sms_' . (has_specific_permission($to_member, 'sms_higher_limit') ? 'high' : 'low') . '_limit')); if ($sent_in_month + 1 > $limit) { continue; } // If just gone over quota, tell them instead of sending the real notification $_message = $sent_in_month + 1 == $limit ? do_lang('OVER_SMS_LIMIT') : xmlentities($message); if (strtolower(get_charset()) != 'utf-8' && strtolower(get_charset()) != 'utf8') { $_message = utf8_encode($_message); } // Let the super-admin trigger or receive longer messages $is_this_super_admin = $GLOBALS['FORUM_DRIVER']->is_super_admin($to_member); $concat = $is_super_admin || $is_this_super_admin ? '3' : '1'; // Find the phone number configured $cpf_values = $GLOBALS['FORUM_DRIVER']->get_custom_fields($to_member); if (!array_key_exists('mobile_phone_number', $cpf_values)) { continue; } // :S -- should be there $to = cleanup_mobile_number($cpf_values['mobile_phone_number']); if ($to == '') { continue; } // TODO: if (!array_key_exists($to,$confirmed_numbers)) continue; $to = xmlentities($to); $xml = <<<END <clickAPI> \t<sendMsg> \t\t<api_id>{$api_id}</api_id> \t\t<user>{$username}</user> \t\t<password>{$password}</password> \t\t<to>{$to}</to> \t\t<text>{$_message}</text> \t\t<from>{$site_name}</from> \t\t<callback>{$callback}</callback> \t\t<max_credits>2.5</max_credits> \t\t<concat>{$concat}</concat> \t</sendMsg> </clickAPI> END; $result = http_download_file('http://api.clickatell.com/xml/xml', NULL, false, false, 'ocPortal', array('data' => $xml)); if (strpos($result, 'fault') !== false) { attach_message($result, 'warn'); continue; } $num_sent++; $GLOBALS['SITE_DB']->query_insert('sms_log', array('s_trigger_ip' => get_ip_address(2), 's_member_id' => $to_member, 's_time' => time())); } return $num_sent; }