예제 #1
0
파일: Taxes.php 프로젝트: msigley/shopp
 public function upload()
 {
     if (!isset($_FILES['ratefile'])) {
         return false;
     }
     $upload = $_FILES['ratefile'];
     $filename = $upload['tmp_name'];
     if (empty($filename) && empty($upload['name']) && !isset($_POST['upload'])) {
         return false;
     }
     $error = false;
     if ($upload['error'] != 0) {
         return $this->notice(ShoppLookup::errors('uploads', $upload['error']));
     }
     if (!is_readable($filename)) {
         return $this->notice(ShoppLookup::errors('uploadsecurity', 'is_readable'));
     }
     if (empty($upload['size'])) {
         return $this->notice(ShoppLookup::errors('uploadsecurity', 'is_empty'));
     }
     if ($upload['size'] != filesize($filename)) {
         return $this->notice(ShoppLookup::errors('uploadsecurity', 'filesize_mismatch'));
     }
     if (!is_uploaded_file($filename)) {
         return $this->notice(ShoppLookup::errors('uploadsecurity', 'is_uploaded_file'));
     }
     $data = file_get_contents($upload['tmp_name']);
     $cr = array("\r\n", "\r");
     $formats = array(0 => false, 3 => 'xml', 4 => 'tab', 5 => 'csv');
     preg_match('/((<[^>]+>.+?<\\/[^>]+>)|(.+?\\t.+?[\\n|\\r])|(.+?,.+?[\\n|\\r]))/', $data, $_);
     $format = $formats[count($_)];
     if (!$format) {
         return $this->notice(Shopp::__('The uploaded file is not properly formatted as an XML, CSV or tab-delimmited file.'));
     }
     $_ = array();
     switch ($format) {
         case 'xml':
             /*
             Example XML import file:
             	<localtaxrates>
             		<taxrate name="Kent">1</taxrate>
             		<taxrate name="New Castle">0.25</taxrate>
             		<taxrate name="Sussex">1.4</taxrate>
             	</localtaxrates>
             
             Taxrate record format:
             	<taxrate name="(Name of locality)">(Percentage of the supplemental tax)</taxrate>
             
             Tax rate percentages should be represented as percentage numbers, not decimal percentages:
             	1.25	= 1.25%	(0.0125)
             	10		= 10%	(0.1)
             */
             $XML = new xmlQuery($data);
             $taxrates = $XML->tag('taxrate');
             while ($rate = $taxrates->each()) {
                 $name = $rate->attr(false, 'name');
                 $value = $rate->content();
                 $_[$name] = $value;
             }
             break;
         case 'csv':
             ini_set('auto_detect_line_endings', true);
             if (($csv = fopen($upload['tmp_name'], 'r')) === false) {
                 return $this->notice(ShoppLookup::errors('uploadsecurity', 'is_readable'));
             }
             while (($data = fgetcsv($csv, 1000)) !== false) {
                 $_[$data[0]] = !empty($data[1]) ? $data[1] : 0;
             }
             fclose($csv);
             ini_set('auto_detect_line_endings', false);
             break;
         case 'tab':
         default:
             $data = str_replace($cr, "\n", $data);
             $lines = explode("\n", $data);
             foreach ($lines as $line) {
                 list($key, $value) = explode("\t", $line);
                 $_[$key] = $value;
             }
     }
     if (empty($_)) {
         return $this->notice(Shopp::__('No useable tax rates could be found. The uploaded file may not be properly formatted.'));
     }
     $id = $_POST['id'];
     $rates = shopp_setting('taxrates');
     $rates[$id]['locals'] = apply_filters('shopp_local_taxrates_upload', $_);
     shopp_set_setting('taxrates', $rates);
 }