array( 'remote_file' => 'relais.gz', 'local_file' => 'relais.txt', 'table_name' => 'icirelais' ), 'backup' => array( 'remote_file' => 'suggestion.gz', 'local_file' => 'suggestion.txt', 'table_name' => 'icibackup') ); protected $_tmpFile; protected $_lines; protected $_download_dir; protected $_configuration_key = array(); //========================================================================================================================= /** * - get configuration keys (ftp access) */ public function __construct($input){ $this->_download_dir = $input['download_dir']; //$this->_download_dir = DIR_FS_CATALOG . 'download/'; // Get ICI relais configuration key from database $query = "SELECT configuration_key, configuration_value FROM configuration WHERE configuration_key LIKE 'MODULE_SHIPPING_ICIRELAIS_FTP%'"; $get_configuration_value = tep_db_query($query); while($row = tep_db_fetch_array($get_configuration_value)){ $this->_configuration_key[$row['configuration_key']] = $row['configuration_value']; } include_once('includes/languages/'.$input['language'].'/icirelais_file_synchronize.php'); } //========================================================================================================================= /** * - get the new files by FTP * - uncompress the new files * - truncate tables icirelais (relais point and backup) * - insert new icirelais datas */ public function file_synchronize_action() { // Guarantee, there will not be a maximum execution time error set_time_limit(0); // FTP param control if((!isset($this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_HOST']) || $this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_HOST'] == '') && (!isset($this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_USER']) || $this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_USER'] == '' ) && (!isset($this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_PASSWORD']) || $this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_PASSWORD'] == '')){ return array('result' => false, 'msg' => FTP_PARAM_EMPTY); } foreach($this->_files as $current_file_info){ // step 1 : download the file $res_download = $this->download($current_file_info); if($res_download['result'] !== true) { return $res_download['msg']; } // step 2 : uncompress the file $res_uncompress = $this->uncompress($current_file_info); if($res_uncompress['result'] !== true) { return $res_uncompress['msg']; } // step 3 : ckeck $res_init = $this->get_content($current_file_info); if($res_init['result'] !== true) { return $res_init['msg']; } // step 4 : save datas $res_save_to_database = $this->save_to_database($current_file_info); if($res_save_to_database['result'] !== true) { return $res_save_to_database['msg']; } } return array('result' => true, 'msg' => SYNC_OK); } //========================================================================================================================= public function download($input) { // FTP Connection $conn = ftp_connect($this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_HOST']); if(!$conn) { return array('result' => false, 'msg' => FTP_CONNECT_FAILED); } // FTP login if(!ftp_login($conn, (string)$this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_USER'], (string)$this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_PASSWORD'])) { return array('result' => false, 'msg' => FTP_AUTHENTIFICATION_FAILED); } // FTP Passive mode active (in case of firewall) ftp_pasv($conn, true); // FTP change directory if(!ftp_chdir($conn, (string)$this->_configuration_key['MODULE_SHIPPING_ICIRELAIS_FTP_DIRECTORY'])){ return array('result' => false, 'msg' => FTP_CHDIR_FAILED); } // Create a temporary file for the compressed file $this->_tmpFile = tempnam('/tmp', 'ICIRELAIS'); $handle = fopen($this->_tmpFile, 'w+'); // Get the compressed file in the temporary file if(!ftp_fget($conn, $handle, $input['remote_file'], FTP_BINARY, 0)) { return array('result' => false, FTP_GET_FAILED.$input['remote_file']); } fclose($handle); ftp_close($conn); return array('result' => true, 'msg' => DOWNLOAD_OK.$input['remote_file']); } //========================================================================================================================= public function uncompress($input) { // If the download directory doesn't exist, create it if(!is_dir($this->_download_dir)) { @mkdir($this->_download_dir, 0777, true); } // Open the local file $file = $this->_download_dir.DIRECTORY_SEPARATOR.$input['local_file']; if(!($handle = @fopen($file, 'w+'))) { return array('result' => false, 'msg' => FOPEN_FAILED.$input['local_file']); } if($this->_tmpFile === null) { return array('result' => false, 'msg' => FILE_DOESNOT_EXIST.$this->_tmpFile); } // Open the compresses file if(!$zp = gzopen($this->_tmpFile, 'r')){ return array('result' => false, 'msg' => GZOPEN_FAILED.$this->_tmpFile); } // read the compress temporary file and write it in an uncompressed one if(!fwrite($handle, gzread($zp, 9999999))) { return array('result' => false, 'msg' => FWRITE_FAILED.$input['local_file']); } fclose($handle); gzclose($zp); return array('result' => true, 'msg' => UNCOMPRESS_OK.$input['remote_file']); } //========================================================================================================================= public function get_content ($input) { $local_file = $this->_download_dir.DIRECTORY_SEPARATOR.$input['local_file']; // check if it is a file if(!is_file($local_file)) { return array('result' => false, 'msg' => NOT_A_FILE.$local_file); } // check if the file is empty if(filesize($local_file) == 0) { return array('result' => false, 'msg' => FILE_EMPTY.$local_file); } // get file content $this->_lines = file($local_file); // check the file integrity with the header (D+date) and the footer (F+date) if(!$this->_hasDate()) { return array('result' => false, 'msg' => $local_file.' n\'est pas intègre'); } // Delete the header and the footer for save in database array_shift($this->_lines); array_pop($this->_lines); return array('result' => true, 'msg' => GET_CONTENT_NEARLY_OK.$local_file); } //========================================================================================================================= /** * Check is there a creation date in header or footer * * @return boolean */ protected function _hasDate() { return strpos($this->_lines[0], 'D') === 0 && strpos($this->_lines[sizeof($this->_lines)-1], 'F') === 0; } //========================================================================================================================= public function save_to_database ($input) { // truncate table $query = "TRUNCATE ".$input['table_name']; $truncate_table = tep_db_query($query); // get table fields $function = "_".$input['table_name']."_fields"; $fields = $this->$function(); // Query construction 1/2 (field part) $insert = "INSERT INTO ".$input['table_name']." ("; foreach($fields as $filed_name) { $insert .= $filed_name.", "; } $insert = substr($insert,0,strlen($insert)-2).") VALUES "; // Query construction 2/2 (values part) $values = ""; $i = 0; foreach($this->_lines as $line) { // Save to database frequently if($i == 1000){ $values = substr($values,0,strlen($values)-1); $insert_to_database = tep_db_query($insert.$values); $values = ""; $i = 0; } $data = explode(';', $line); $values .= "("; foreach($data as $value) { $values .= "'".$this->_valueFilter($value)."',"; } $values = substr($values,0,strlen($values)-1)."),"; $i++; } // insert $values = substr($values,0,strlen($values)-1); $insert_to_database = tep_db_query($insert.$values); return array('result' => true, 'msg' => 'L\'intégration en base de données s\'est bien déroulée'); } //========================================================================================================================= /** * @param string $value * @return string */ protected function _valueFilter($value) { if($value == '-') { $value = null; } return $value; } //========================================================================================================================= protected function _icirelais_fields () { return array( 'file_id', 'relay_id', 'insee', 'owner_name', 'adresse_1', 'adresse_2', 'adresse_3', 'postal_code', 'city', 'shop_name', 'coord_lat', 'coord_long', 'pda_equipment', 'start_validity_date', 'end_validity_date', 'last_shippment_date', 'new_shippment_date', 'text', 'monday_opening_hours', 'tuesday_opening_hours', 'wednesday_opening_hours', 'thursday_opening_hours', 'friday_opening_hours', 'saturday_opening_hours', 'sunday_opening_hours', 'closing_period_start_date_1', 'closing_period_end_date_1', 'closing_period_start_date_2', 'closing_period_end_date_2', 'closing_period_start_date_3', 'closing_period_end_date_3', 'required_time'); } //========================================================================================================================= protected function _icibackup_fields () { return array( 'postal_code', 'relay_id', 'sort_order', 'distance'); } } ?>