demo = $demo ? $demo : Configuration::get('ETIQUETAGE_RETURN_TEST'); $this->module_conf = unserialize(Configuration::get( 'ETIQUETAGE_RETURN_CONF', null, $this->context->shop->id_shop_group, $this->context->shop->id )); } public function l($string, $specific = false, $locale = null) { if (!$specific) { $specific = basename(__FILE__, '.php'); } if (class_exists('Translate')) { return Translate::getModuleTranslation(get_parent_class(), $string, $specific); } return parent::l($string, $specific); } private function _fillXML($params, &$sls_generate_label, &$document) { $requires_cdata = array( 'Description', 'instructions', 'commercialName', 'companyName', 'lastName', 'firstName', 'line0', 'line1', 'line2', 'line3', 'city' ); foreach ($params as $key => $value) { if (is_numeric($key)) { // only happens with articles $key = 'article'; } if (!is_array($value)) { if ($value) { if (in_array($key, $requires_cdata)) { $tmp_element = $document->createElement($key); $tmp_element->appendChild($document->createCDATASection($value)); } else { $tmp_element = $document->createElement($key, $value); } $sls_generate_label->appendChild($tmp_element); } } else { $new_child = $document->createElement($key); $sls_generate_label->appendChild($new_child); $this->_fillXML($value, $new_child, $document); } } } protected function _callWS($params) { $document = new DOMDocument('1.0', 'utf-8'); $document->formatOutput = true; $document->preserveWhiteSpace = true; $soap_envelope = $document->createElement('soap:Envelope'); $soap_envelope->setAttribute('xmlns:soap', 'http://schemas.xmlsoap.org/soap/envelope/'); $soap_envelope->setAttribute('xmlns:sls', 'http://sls.ws.coliposte.fr'); $soap_body = $document->createElement('soap:Body'); $sls_generate_label = $document->createElement('sls:generateLabel'); $sls_generate_label_request = $document->createElement('generateLabelRequest'); $soap_envelope->appendChild($soap_body); $soap_body->appendChild($sls_generate_label); $sls_generate_label->appendChild($sls_generate_label_request); $document->appendChild($soap_envelope); $this->_fillXML($params, $sls_generate_label_request, $document); $request = $document->saveXML(); // var_dump($this->xmlpp($request));die; $this->origin_request = $request; $this->request = $request; $headers = array( 'Accept-Encoding: gzip,deflate', 'Content-Type: text/xml; charset="utf-8"', 'SOAPAction: ""' ); $webservice_url = array_key_exists('dev_mode', $this->module_conf) && $this->module_conf['dev_mode'] ? self::SCE_WS_TELINTRANS : self::SCE_WS; $ch = curl_init(); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_URL, $webservice_url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); curl_setopt($ch, CURLOPT_POSTFIELDS, $request); curl_setopt($ch, CURLOPT_VERBOSE, true); // IF demo() ELSE webservice() ENDIF if ($this->demo) { $result = Tools::file_get_contents(dirname(__FILE__).'/../functions/raw_response.txt'); } else { $result = curl_exec($ch); } $this->raw_response = new SoColissimoCurlParser($result); $result = $this->xmldata($result); if ($result === false) { echo nl2br(print_r(curl_getinfo($ch), true)); echo 'Request'; print('
'.print_r(htmlentities($request, ENT_QUOTES, 'UTF-8'), true).'
'); echo 'cURL error number:'.curl_errno($ch).'\n
'; echo 'cURL error message:'.curl_error($ch).'\n
'; curl_close($ch); throw new Exception(curl_error($ch)); } curl_close($ch); return ($result); } public function xmldata($page) { if (empty($page)) { printf('%s(#%d): empty string passed to the function', basename(__FILE__), __LINE__); return (false); } // On some system the following regexp failed because the response is too long // Minimize the response by removing header at beginning and kepping enough text to catch the XML code. $page = Tools::substr($page, 100, 1250); preg_match('//', $page, $page); $page = reset($page); $page = $this->xmlpp($page); // remove namespaces $page = preg_replace('/xmlns[^=]*="[^"]*"/i', '', $page); $page = preg_replace('/[a-zA-Z0-9]+:([a-zA-Z0-9]+[ =>])/', '$1', $page); // filters $page = str_replace(array("\n", "\r", "\t"), '', $page); $page = trim(str_replace('"', "'", $page)); $xml = simplexml_load_string($page, null, LIBXML_NOCDATA); if (!$xml instanceof SimpleXMLElement) { printf('%s(#%d): invalid string passed to the function: "%s"', basename(__FILE__), __LINE__, $page); return (false); } return ($xml); } /** * Check the availability of ColiPoste web services * * @return boolean Supervision status */ public static function webServiceSupervision() { $supervision = Tools::file_get_contents(self::WS_SUPERVISION); if (preg_match('/\[OK\]/', $supervision)) { return (true); } return (false); } public function xmlpp($xml, $html_output = false) { $xml_obj = new SimpleXMLElement($xml); $level = 4; $indent = 0; $pretty = array(); $xml = explode("\n", preg_replace('/>\s*\n<", $xml_obj->asXML())); if (count($xml) && preg_match('/^<\?\s*xml/', $xml[0])) { $pretty[] = array_shift($xml); } foreach ($xml as $el) { if (preg_match('/^<([\w])+[^>\/]*>$/U', $el)) { $pretty[] = str_repeat(' ', $indent).$el; $indent += $level; } else { if (preg_match('/^<\/.+>$/', $el)) { $indent -= $level; } if ($indent < 0) { $indent += $level; } $pretty[] = str_repeat(' ', $indent).$el; } } $xml = implode("\n", $pretty); if ($html_output) { $xml = htmlentities($xml); } return $xml; } }