Jump to content



Photo
- - - - -

Auto Update Currencies


  • Please log in to reply
99 replies to this topic

#81   Jack_mcs

Jack_mcs
  • Members
  • 27,138 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 13 November 2014 - 14:58

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.



#82   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 13 November 2014 - 16:33

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 ?



#83   Jack_mcs

Jack_mcs
  • Members
  • 27,138 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 13 November 2014 - 17:29

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.



#84   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 15 November 2014 - 12:10

What versions are compatible ?

is there something i can do ?



#85   Jack_mcs

Jack_mcs
  • Members
  • 27,138 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 15 November 2014 - 18:00

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.



#86   Bob Terveuren

Bob Terveuren
  • Members
  • 456 posts
  • Real Name:Bob Terveuren
  • Gender:Male

Posted 16 November 2014 - 13:37

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, 16 November 2014 - 13:38.


#87   Jack_mcs

Jack_mcs
  • Members
  • 27,138 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 16 November 2014 - 14:41

Thanks for posting that. I'm sure it will help others.



#88   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 17 November 2014 - 08:16

@Bob Terveuren thx, i really appreciate your help. also i needed the 2% markup. :thumbsup:



#89   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 19 January 2015 - 21:30

@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?



#90   Jack_mcs

Jack_mcs
  • Members
  • 27,138 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 20 January 2015 - 00:58

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



#91   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 20 January 2015 - 13:01

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.



#92   Jack_mcs

Jack_mcs
  • Members
  • 27,138 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 20 January 2015 - 13:18

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. 



#93   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 20 January 2015 - 14:39

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



#94   Jack_mcs

Jack_mcs
  • Members
  • 27,138 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 20 January 2015 - 17:16

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.



#95   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 15 May 2015 - 12:50

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



#96   Jack_mcs

Jack_mcs
  • Members
  • 27,138 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 15 May 2015 - 14:13

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.



#97   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 16 May 2015 - 09:11

done and working.

 

php -q public_html/backend/auto_update_currencies.php



#98   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 07 June 2015 - 19:38

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/uc...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/uc...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>



#99   Psytanium

Psytanium
  • Members
  • 263 posts
  • Real Name:Psytanium
  • Gender:Male

Posted 08 June 2015 - 09:58

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, 08 June 2015 - 09:58.


#100   Jack_mcs

Jack_mcs
  • Members
  • 27,138 posts
  • Real Name:Jack York
  • Gender:Male
  • Location:Michigan

Posted 08 June 2015 - 12:15

I haven't seen that problem yet but thanks for posting the url.