Jump to content
Jack_mcs

Auto Update Currencies

Recommended Posts

You can change that in the auto_update_currencies.php file, near the top. But you may want to just simulate a return instead of actually going to the site each time because if you visit it too much they may block your IP, at least for a while.

Edited by Jack_mcs

Share this post


Link to post
Share on other sites

Sick of me yet???? :blush:

 

I think I have my code sorted so it works for what I asked earlier (posts #72 & #73) once I'm sure I'll post it up here just incase there is another person who needs that info....

 

Quick question though... Do you know how often you can access the exchange rates without getting blocked out??? is every hour too frequent?

Share this post


Link to post
Share on other sites

No, I don't. Those sites probably have a TOS page though.

Edited by Jack_mcs

Share this post


Link to post
Share on other sites

Thanks, I understand now. There's nothing written for that that I am aware of. You could try editing the admin/includes/functions/localization.php file and change

function quote_xe_currency($to, $from = DEFAULT_CURRENCY) {
to
function quote_xe_currency($from = DEFAULT_CURRENCY, $to) {
I haven't tried it but it would be my first guess. If it works, you would also have to change the function for oanda. If that doesn't work, then the code in the update section of currencies.php could be altered to figure out the exchange rate, once retrieved, and then alter the calculation so it uses the inverse. Good luck. :)

 

in case anyone wants to get the inverse value, this is how you do it...

function quote_xe_currency($from, $to = DEFAULT_CURRENCY) {

and,

function quote_oanda_currency($base, $code = DEFAULT_CURRENCY) {

Share this post


Link to post
Share on other sites

Hello @@Jack_mcs I did the edit of the file and upload it to the admin directory, setup a cron job, but I don't know why its not working, I waited 48h and the exchange rate didn't changed.

Also I tried to run the script directly by browsing domain.com/admin/auto_update_currencies.php the page returned a blank page and the exchange rate didn't changed.

 

can you please help me ?

Edited by Psytanium

Share this post


Link to post
Share on other sites

The page shouldn't be blank when you run it. That indicates a problem of some kind. If your site generates an error_log file in the admin directory, look at it to see if it shows any errors for this problem. You might also want to go over the installation to be sure you didn't miss something. And try going into admin->Localization->Currencies and clicking on the update currencies button. If that fails to work, then something on your server is blocking the connection.

Share this post


Link to post
Share on other sites

the update currency button is working fine, currencies updated.

 

thats the code

<?
/*
  Auto Update Currencies v 1.0 by Jack_mcs - oscommerce-solution.com
  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
  ///////CONFIGURABLE VARIABLES///////////
  define('DEFAULT_CURRENCY', 'EUR');
  define('HOURS_BETWEEN_UPDATES' , '24'); //if more than one day, multiply days by hours so the setting for two days would be 2 * 24 = 48
  define('REPORT_NO_UPDATE', true); //if the currencies are not updated since they have already been updated, don't report it. Set to false to always receive this message
  
  define('NO_REPORT',    0);
  define('SCREEN',       1);
  define('EMAIL',        2);
  define('SCREEN_EMAIL', 3);
  
  $output_report = EMAIL;
  ///////////////END//////////////////////

  define('CURRENCY_SERVER_PRIMARY', 'xe');
  define('CURRENCY_SERVER_BACKUP', 'oanda');

  require_once('includes/configure.php');
  require_once(DIR_WS_FUNCTIONS . 'localization.php');

  function date_diff_new($str_start, $str_end)
  {
    $str_start = strtotime($str_start); // The start date becomes a timestamp
    $str_end = strtotime($str_end); // The end date becomes a timestamp

    $nseconds = $str_end - $str_start; // Number of seconds between the two dates
    $ndays = round($nseconds / 86400); // One day has 86400 seconds
    $nseconds = $nseconds % 86400; // The remainder from the operation
    $nhours = round($nseconds / 3600); // One hour has 3600 seconds
    $nseconds = $nseconds % 3600;
    $nminutes = round($nseconds / 60); // One minute has 60 seconds, duh!
    $nseconds = $nseconds % 60;

    return $nhours;
  }


  $link = mysql_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD) or die("Unable to connect to database server!");
  mysql_select_db(DB_DATABASE);

  $report = '';
  $updated = array();
  $rate = '';
  $today = date("Y-m-d H:i:s"); // The exact time
  $currency_query = mysql_query("select currencies_id, code, title, last_updated from currencies" ) or die(mysql_error());

  while ($currency = mysql_fetch_array($currency_query, MYSQL_ASSOC))
  {
    if (($ndays = date_diff_new($currency['last_updated'], $today)) > HOURS_BETWEEN_UPDATES)
    {
      $quote_function = 'quote_' . CURRENCY_SERVER_PRIMARY . '_currency';
      $rate = $quote_function($currency['code']);

      if (empty($rate)) {
        $quote_function = 'quote_' . CURRENCY_SERVER_BACKUP . '_currency';
        $rate = $quote_function($currency['code']);
      }

      if (!empty($rate)) {
        $updated[] = 'The exchange rate for ' . $currency['title'] . ' <b>WAS</b> successfully updated.' . "\n";
        mysql_query("update currencies set value = '" . $rate . "', last_updated = now() where currencies_id = '" . (int)$currency['currencies_id'] . "'");
      }
      else
        $updated[] = 'The exchange rate for ' . $currency['title'] . ' <b>WAS NOT</b> successfully updated. It was last updated on ' . $currency['last_updated'] . "\n";
    }
    else if (REPORT_NO_UPDATE)
      $updated[] = $currency['title'] . ' was not updated since it has been updated in the last ' . HOURS_BETWEEN_UPDATES . ((HOURS_BETWEEN_UPDATES > 1) ? ' hours.' : ' hour.') . "\n";
  }

  if ($output_report > NO_REPORT) {
    $report = 'Currencies Update Report' . "\n\n";

    $configuration_query = mysql_query("select configuration_value as store_name from configuration where configuration_key = 'STORE_NAME' limit 1") or die(mysql_error());
    $configuration = mysql_fetch_array($configuration_query, MYSQL_ASSOC);
    $report .= 'Currencies for ' . $configuration['store_name'] . ' updated on ' . date("D M j G:i:s Y") . "\n\n";
 
    foreach ($updated as $changed)
      $report .= $changed;

    if ($output_report != EMAIL) {
      echo str_replace("\n", '<br>', $report);
    }

    if ($output_report != SCREEN) {
      $config_query = mysql_query("select configuration_value as email_address from configuration where configuration_key = 'STORE_OWNER_EMAIL_ADDRESS' limit 1") or die(mysql_error());
      $config = mysql_fetch_array($config_query, MYSQL_ASSOC);
      mail($config['email_address'], 'Currency Rates updated for ' . $configuration['store_name'], stripslashes($report), $configuration['store_name']);
    }
  }
  mysql_close($link);
?>

anything wrong ?

Share this post


Link to post
Share on other sites

If you are using around 2.3.3 (maybe before), that won't work. The database calls are different for those versions. I'll update this as soon as I can.

Share this post


Link to post
Share on other sites

It will work with any of the versions that don't use mysqli, which start around 2.3.3, I think. You can try replacing all instance of mysql in that file with mysqli. I haven't looked close enough at it to say if that will work or not but those calls are the problem. The correct fix, and what should have been done initially, is to include application_top and use the tep functions. That's the change I will make once I get to it.

Share this post


Link to post
Share on other sites

Hi there Jack

 

@@Psytanium

 

Here's the code I use on a 2.3.4 site - has the mysqli stuff in there

<?php
/*
  Auto Update Currencies v 1.0 by Jack_mcs - oscommerce-solution.com
  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2003 osCommerce

  Released under the GNU General Public License
*/
  ///////CONFIGURABLE VARIABLES///////////
  define('DEFAULT_CURRENCY', 'GBP');
  define('HOURS_BETWEEN_UPDATES' , '24'); //if more than one day, multiply days by hours so the setting for two days would be 2 * 24 = 48
  define('REPORT_NO_UPDATE', true); //if the currencies are not updated since they have already been updated, don't report it. Set to false to always receive this message
  
  define('NO_REPORT',    0);
  define('SCREEN',       1);
  define('EMAIL',        2);
  define('SCREEN_EMAIL', 3);
  
  $output_report = EMAIL;
  ///////////////END//////////////////////

  define('CURRENCY_SERVER_PRIMARY', 'xe');
  define('CURRENCY_SERVER_BACKUP', 'oanda');

  require_once('includes/configure.php');
  require_once(DIR_WS_FUNCTIONS . 'localization.php');




  $link = mysqli_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD,DB_DATABASE) or die("Unable to connect to database server!");
  //mysql_select_db(DB_DATABASE);

  $report = '';
  $updated = array();
  $rate = '';
  $today = date("Y-m-d H:i:s"); // The exact time
  $currency_query = mysqli_query($link,"select currencies_id, code, title, last_updated from currencies" ) or die(mysql_error());

  while ($currency = mysqli_fetch_array($currency_query, MYSQL_ASSOC))
  {
    
    {
      $quote_function = 'quote_' . CURRENCY_SERVER_PRIMARY . '_currency';
      $rate = $quote_function($currency['code']);

      if (empty($rate)) {
        $quote_function = 'quote_' . CURRENCY_SERVER_BACKUP . '_currency';
        $rate = $quote_function($currency['code']);
      }

      if (!empty($rate)) {
        $updated[] = 'The exchange rate for ' . $currency['title'] . ' <b>WAS</b> successfully updated.' . "\n";
		//if ($rate !=1){$rate=$rate*1.02;}
        mysqli_query($link,"update currencies set value = '" . $rate . "', last_updated = now() where currencies_id = '" . (int)$currency['currencies_id'] . "'");
      }
      else
        $updated[] = 'The exchange rate for ' . $currency['title'] . ' <b>WAS NOT</b> successfully updated. It was last updated on ' . $currency['last_updated'] . "\n";
    }

  }

  if ($output_report > NO_REPORT) {
    $report = 'Currencies Update Report' . "\n\n";

    $configuration_query = mysqli_query($link,"select configuration_value as store_name from configuration where configuration_key = 'STORE_NAME' limit 1") or die(mysqli_error());
    $configuration = mysqli_fetch_array($configuration_query, MYSQL_ASSOC);
    $report .= 'Currencies for ' . $configuration['store_name'] . ' updated on ' . date("D M j G:i:s Y") . "\n\n";
 
    foreach ($updated as $changed)
      $report .= $changed;

     {
      echo str_replace("\n", '<br>', $report);
    }

    
  }
  mysqli_close($link);
?>

n.b I have an extra quick and dirty line in there (if ($rate !=1){$rate=$rate*1.02;}) that adds 2% markup on the exchange rate as my payment processor adds that in as their premium over and above the bank rates.

 

edit: commented out that line but left it in there FYI

Edited by Bob Terveuren

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

@@Jack_mcs, i have no idea why the cron jobs is not working. just a quick question

 

if a browse directly www.domain.com/admin/auto_update_currencies.php i should be asked to login?

Share this post


Link to post
Share on other sites

If your admin is setup correctly, yes. Otherwise there is a big security hole in your admin.

Share this post


Link to post
Share on other sites

i think you are right, there is a security problem, some phishing files.

how can i fix this issue ? just upgrading to the latest version ? or there is something else ?

 

thank you for your advice.

Share this post


Link to post
Share on other sites

If you can get into your admin without logging in, it doesn't have anything to do with this addon. I suggest you first the security checker on my site since there may be more problems besides admin. Then create a new thread in the general forum asking for help in fixing the problems, if you need it. 

Share this post


Link to post
Share on other sites

sry i get you wrong at first. yes if i run the url from a browser the page ask me to login. does this mean the cron jobs is running fine but have permission problem with the addon ?

that's what i have in the file :

require_once('includes/configure.php');
    require_once(DIR_WS_FUNCTIONS . 'localization.php');
     
    $link = mysqli_connect(DB_SERVER, DB_SERVER_USERNAME, DB_SERVER_PASSWORD,DB_DATABASE) or die("Unable to connect to database server!");

should i replace some variables ?

 

Cron jobs: /home/wood/public_html/admin/auto_update_currencies.php

Share this post


Link to post
Share on other sites

That sounds like the code is not loading properly when ran from cron. I'll work on this as soon as I can, though that may be weeks. You could use the alternate method as mentioned in the install instructions.

Share this post


Link to post
Share on other sites

this is the cronjob output i'm getting:

 

/usr/local/cpanel/bin/jailshell: /home/domain/public_html/backend/auto_update_currencies.php: Permission denied

 

any idea what could be the reason ?

 

I'm running osc 2.3.4

Share this post


Link to post
Share on other sites

That is a server issue. The correct syntax for a cron job varies with the server. It might be that what you are using is wrong or that ownership of the admin is not set to user, or something like that. You will have to ask your host to take a look at it.

Share this post


Link to post
Share on other sites

everything was working, i was receiving emails from cron on successful currencies update. but recently i'm receiving this email:
 

Warning: file(http://www.xe.net/ucc/convert.cgi?Amount=1&From=EUR&To=USD): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request  in /home/domain/public_html/backend/includes/functions/localization.php on line 24

 

Warning: implode(): Invalid arguments passed in /home/domain/public_html/backend/includes/functions/localization.php on line 26

 

Warning: file(http://www.xe.net/ucc/convert.cgi?Amount=1&From=EUR&To=EUR): failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request  in /home/domain/public_html/backend/includes/functions/localization.php on line 24

 

Warning: implode(): Invalid arguments passed in /home/domain/public_html/backend/includes/functions/localization.php on line 26 Currencies Update Report<br><br>Currencies for Website Title updated on Sat Jun 6 18:00:13 2015<br><br>The exchange rate for US dollar <b>WAS</b> successfully updated.<br>The exchange rate for Euro <b>WAS</b> successfully updated.<br>

Share this post


Link to post
Share on other sites

Probably xe is using a new api link, i change in admin/includes/functions/localization.php

 

this:

$page = file('http://www.xe.net/ucc/convert.cgi?Amount=1&From=' . $from . '&To=' . $to);

to this:

$page = file('http://www.xe.com/currencyconverter/convert/?Amount=1&From='. $from . '&To=' . $to);

Now its working again.

Edited by Psytanium

Share this post


Link to post
Share on other sites

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×