예제 #1
0
 /**
  * Standard modular run function for CRON hooks. Searches for tasks to perform.
  */
 function run()
 {
     if (get_option('mail_queue_debug') == '0') {
         // Implement basic locking
         if (get_long_value_newer_than('mailer_currently_dripping', time() - 60 * 5) === '1') {
             return;
         }
         set_long_value('mailer_currently_dripping', '1');
         $mails = $GLOBALS['SITE_DB']->query_select('logged_mail_messages', array('id', 'm_subject', 'm_message', 'm_to_email', 'm_to_name', 'm_from_email', 'm_from_name', 'm_priority', 'm_attachments', 'm_no_cc', 'm_as', 'm_as_admin', 'm_in_html', 'm_date_and_time', 'm_member_id', 'm_url', 'm_template'), array('m_queued' => 1), '', 100);
         if (count($mails) != 0) {
             require_code('mail');
             foreach ($mails as $row) {
                 $subject = $row['m_subject'];
                 $message = $row['m_message'];
                 $to_email = unserialize($row['m_to_email']);
                 $to_name = unserialize($row['m_to_name']);
                 $from_email = $row['m_from_email'];
                 $from_name = $row['m_from_name'];
                 mail_wrap($subject, $message, $to_email, $to_name, $from_email, $from_name, $row['m_priority'], unserialize($row['m_attachments']), $row['m_no_cc'] == 1, $row['m_as'], $row['m_as_admin'] == 1, $row['m_in_html'] == 1, true, $row['m_template']);
                 $GLOBALS['SITE_DB']->query_update('logged_mail_messages', array('m_queued' => 0), array('id' => $row['id']), '', 1);
             }
         }
     }
     set_long_value('mailer_currently_dripping', '0');
 }
예제 #2
0
/**
 * Perform a currency conversion.
 *
 * @param  mixed		The starting amount (integer or float).
 * @param  ID_TEXT	The start currency code.
 * @param  ?ID_TEXT  The end currency code (NULL: unknown, guess it).
 * @param  boolean	Whether to get as a string.
 * @return ?mixed		The new amount as float, or if $string then as a string (NULL: failed to do it).
 */
function currency_convert($amount, $from_currency, $to_currency = NULL, $string = false)
{
    // Check data
    $from_currency = strtoupper($from_currency);
    $map = get_currency_map();
    if (!array_key_exists($from_currency, $map)) {
        return NULL;
    }
    if (is_null($to_currency)) {
        // Perform a preferential guessing sequence
        // ========================================
        // keep_currency
        $to_currency = get_param('keep_currency', NULL);
        if (is_null($to_currency)) {
            // a specially named custom profile field for the currency.
            $to_currency = get_ocp_cpf('currency');
            if ($to_currency === '') {
                $to_currency = NULL;
            }
            if (is_null($to_currency)) {
                // keep_country
                $country = get_param('keep_country', NULL);
                if (!is_null($country)) {
                    $to_currency = country_to_currency($country);
                }
                if (is_null($to_currency)) {
                    // a specially named custom profile field for the country.
                    $country = get_ocp_cpf('country');
                    if ($country != '') {
                        $to_currency = country_to_currency($country);
                    }
                    if (is_null($to_currency)) {
                        // geolocation
                        $to_currency = ip_to_currency();
                        if (is_null($to_currency)) {
                            //							// Euros to the rescue (it's a reasonable guess based on the largest population using a single currency)
                            //							$to_currency='EUR';
                            $to_currency = get_option('currency');
                        }
                    }
                }
            }
        }
    }
    // (We now know $to_currency)
    // We'll use Google as a simple web service
    if ($from_currency == $to_currency) {
        $new_amount = is_integer($amount) ? floatval($amount) : $amount;
    } else {
        $cache_key = 'currency_' . $from_currency . '_' . $to_currency . (is_float($amount) ? float_to_raw_string($amount) : strval($amount));
        $_new_amount = get_long_value_newer_than($cache_key, time() - 60 * 60 * 24 * 2);
        $new_amount = is_null($_new_amount) ? NULL : floatval($_new_amount);
        if (is_null($new_amount)) {
            $GLOBALS['SITE_DB']->query('DELETE FROM ' . get_table_prefix() . 'long_values WHERE the_name LIKE \'' . db_encode_like('currency_%') . '\' AND date_and_time<' . strval(time() - 60 * 60 * 24 * 2));
            // Cleanup
            $google_url = 'http://www.google.com/finance/converter?a=' . (is_float($amount) ? float_to_raw_string($amount) : strval($amount)) . '&from=' . $from_currency . '&to=' . strtoupper($to_currency);
            $result = http_download_file($google_url, NULL, false);
            if (is_string($result)) {
                $matches = array();
                for ($i = 0; $i < strlen($result); $i++) {
                    if (ord($result[$i]) > 127) {
                        $result[$i] = ' ';
                    }
                }
                if (preg_match('#<span class=bld>([\\d\\., ]+) [A-Z]+</span>#U', $result, $matches) != 0) {
                    $new_amount = floatval(str_replace(',', '', str_replace(' ', '', $matches[1])));
                    set_long_value($cache_key, float_to_raw_string($new_amount));
                } else {
                    return NULL;
                }
            } else {
                $new_amount = is_integer($amount) ? floatval($amount) : $amount;
                $to_currency = $from_currency;
            }
        }
    }
    if ($string) {
        $ret = '';
        if (in_array($to_currency, array('USD', 'AUD', 'CAD', 'SRD', 'SBD', 'SGD', 'NZD', 'NAD', 'MXN', 'LRD', 'GYD', 'FJD', 'SVC', 'XCD', 'COP', 'CLP', 'KYD', 'BND', 'BMD', 'BBD', 'BSD', 'ARS'))) {
            $ret .= '$';
        } elseif (in_array($to_currency, array('GBP', 'SHP', 'LBP', 'JEP', 'GGP', 'GIP', 'FKP', 'EGP'))) {
            $ret .= '&pound;';
        } elseif (in_array($to_currency, array('JPY'))) {
            $ret .= '&yen;';
        } elseif (in_array($to_currency, array('EUR'))) {
            $ret .= '&euro;';
        }
        $ret .= escape_html(float_format($new_amount)) . '&nbsp;' . escape_html($to_currency);
        return $ret;
    }
    return $new_amount;
}