/**
  * Checks the VirtueMart Server for the latest available Version of VirtueMart
  *
  * @return string Example: 1.1.2
  */
 function getLatestVersion()
 {
     if (!class_exists('VmConnector')) {
         require VMPATH_ROOT . DS . 'administrator' . DS . 'components' . DS . 'com_virtuemart' . DS . 'helpers' . DS . 'connection.php';
     }
     $url = "http://virtuemart.net/index2.php?option=com_versions&catid=1&myVersion={" . VmConfig::getInstalledVersion() . "}&task=latestversionastext";
     $result = VmConnector::handleCommunication($url);
     return $result;
 }
 static function getSetExchangeRates($ecb_filename)
 {
     $archive = true;
     setlocale(LC_TIME, "en-GB");
     $now = time() + 3600;
     // Time in ECB (Germany) is GMT + 1 hour (3600 seconds)
     if (date("I")) {
         $now += 3600;
         // Adjust for daylight saving time
     }
     $weekday_now_local = gmdate('w', $now);
     // week day, important: week starts with sunday (= 0) !!
     $date_now_local = gmdate('Ymd', $now);
     $time_now_local = gmdate('Hi', $now);
     $time_ecb_update = '1415';
     if (is_writable(VMPATH_ROOT . DS . 'cache')) {
         $store_path = VMPATH_ROOT . DS . 'cache';
     } else {
         $store_path = VMPATH_ROOT . DS . 'media';
     }
     $archivefile_name = $store_path . '/daily.xml';
     $val = '';
     if (file_exists($archivefile_name) && filesize($archivefile_name) > 0) {
         // timestamp for the Filename
         $file_datestamp = date('Ymd', filemtime($archivefile_name));
         // check if today is a weekday - no updates on weekends
         if (date('w') > 0 && date('w') < 6 && $file_datestamp != $date_now_local && $time_now_local > $time_ecb_update) {
             $curr_filename = $ecb_filename;
         } else {
             $curr_filename = $archivefile_name;
             $last_updated = $file_datestamp;
             $archive = false;
         }
     } else {
         $curr_filename = $ecb_filename;
     }
     if (!is_writable($store_path)) {
         $archive = false;
         vmError("The file {$archivefile_name} can't be created. The directory {$store_path} is not writable");
     }
     //			JError::raiseNotice(1, "The file $archivefile_name should be in the directory $store_path " );
     if ($curr_filename == $ecb_filename) {
         // Fetch the file from the internet
         if (!class_exists('VmConnector')) {
             require VMPATH_ADMIN . DS . 'helpers' . DS . 'connection.php';
         }
         //				JError::raiseNotice(1, "Updating currency " );
         if (!($contents = VmConnector::handleCommunication($curr_filename))) {
             if (isset($file_datestamp)) {
                 $contents = @file_get_contents($curr_filename);
             }
         } else {
             $last_updated = date('Ymd');
         }
     } else {
         $contents = @file_get_contents($curr_filename);
     }
     if ($contents) {
         // if archivefile does not exist
         if ($archive) {
             // now write new file
             file_put_contents($archivefile_name, $contents);
         }
         $contents = str_replace("<Cube currency='USD'", " <Cube currency='EUR' rate='1'/> <Cube currency='USD'", $contents);
         /* XML Parsing */
         $xmlDoc = new DomDocument();
         if (!$xmlDoc->loadXML($contents)) {
             //todo
             vmError('Failed to parse the Currency Converter XML document.');
             vmError('The content: ' . $contents);
             //					$GLOBALS['product_currency'] = $vendor_currency;
             return false;
         }
         $currency_list = $xmlDoc->getElementsByTagName("Cube");
         // Loop through the Currency List
         $length = $currency_list->length;
         for ($i = 0; $i < $length; $i++) {
             $currNode = $currency_list->item($i);
             if (!empty($currNode) && !empty($currNode->attributes->getNamedItem("currency")->nodeValue)) {
                 $currency[$currNode->attributes->getNamedItem("currency")->nodeValue] = $currNode->attributes->getNamedItem("rate")->nodeValue;
                 unset($currNode);
             }
         }
         $globalCurrencyConverter = $currency;
     } else {
         $globalCurrencyConverter = false;
         vmError('Failed to retrieve the Currency Converter XML document.');
         // 				return false;
     }
     return $globalCurrencyConverter;
 }
 /**
  * Set headers and send the file to the client
  *
  * @author Andreas Gohr <*****@*****.**>
  * @param string The full path to the file
  * @param string The Mime Type of the file
  */
 function sendFile($file, $mime, $overrideFileName = '')
 {
     // send headers
     header("Content-Type: {$mime}");
     list($start, $len) = VmConnector::http_rangeRequest(filesize($file));
     header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
     header('Pragma: public');
     header('Accept-Ranges: bytes');
     //application mime type is downloadable
     if (strtolower(substr($mime, 0, 11)) == 'application') {
         if ($overrideFileName == '') {
             $filename = basename($file);
         } else {
             $filename = $overrideFileName;
         }
         header('Content-Disposition: attachment; filename="' . $filename . '";');
     }
     $chunksize = 1 * (1024 * 1024);
     // send file contents
     $fp = @fopen($file, "rb");
     if ($fp) {
         fseek($fp, $start);
         //seek to start of range
         $chunk = $len > $chunksize ? $chunksize : $len;
         while (!feof($fp) && $chunk > 0) {
             @set_time_limit();
             // large files can take a lot of time
             print fread($fp, $chunk);
             flush();
             $len -= $chunk;
             $chunk = $len > $chunksize ? $chunksize : $len;
         }
         fclose($fp);
     } else {
         header("HTTP/1.0 500 Internal Server Error");
         print "Could not read {$file} - bad permissions?";
         JFactory::getApplication()->close(true);
     }
 }