params = array( 'authDataV1' => array( 'login' => $settings->login, 'masterFid' => $settings->customer_fid, 'password' => $settings->password ) ); try { $this->client = new SoapClient($settings->ws_url, array('trace' => true)); return $this->client; } catch (Exception $e) { DpdPolandLog::addError('URL: '.$settings->ws_url.' - '.$e->getMessage()); self::$errors[] = $e->getMessage(); } return false; } /** * Main function used to make a request via WebServices * * @param string $function_name Function name * @param array $arguments Function arguments * @return array|bool Response */ public function __call($function_name, $arguments) { $result = null; $this->lastCalledFunctionName = $function_name; $this->lastCalledFunctionArgs = $arguments; if (isset($arguments[0]) && is_array($arguments[0])) { $this->params = array_merge($this->params, $arguments[0]); try { if (!$result = $this->client->$function_name($this->params)) self::$errors[] = $this->l('Could not connect to webservice server. Please check webservice URL'); } catch (Exception $e) { DpdPolandLog::addError('function_name: '.$function_name.' - message: '.$e->getMessage()); self::$errors[] = $e->getMessage(); } if (isset($result->return)) $result = $result->return; if (isset($result->faultstring)) self::$errors[] = $result->faultstring; if (_DPDPOLAND_DEBUG_MODE_) $this->debug($result); return $this->objectToArray($result); } return false; } /** * Response should be used as array * * @param object|array $response Response from WebServices * @return array Formatted response */ private function objectToArray($response) { if (!is_object($response) && !is_array($response)) return $response; return array_map(array($this, 'objectToArray'), (array)$response); } /** * Creates debug file if it is still not created * * @return string Debug file name */ private function createDebugFileIfNotExists() { if ((!$debug_filename = Configuration::get(self::DEBUG_FILENAME)) || !$this->isDebugFileName($debug_filename)) { $debug_filename = Tools::passwdGen(self::DEBUG_FILENAME_LENGTH).'.html'; Configuration::updateValue(self::DEBUG_FILENAME, $debug_filename); } if (!file_exists(_DPDPOLAND_MODULE_DIR_.$debug_filename)) { $file = fopen(_DPDPOLAND_MODULE_DIR_.$debug_filename, 'w'); fclose($file); } return $debug_filename; } /** * Checks if string can be used as debug file name * * @param string $debug_filename Debug file name * @return bool String can be used as debug file name */ private function isDebugFileName($debug_filename) { return Tools::strlen($debug_filename) == (int)self::DEBUG_FILENAME_LENGTH + 5 && preg_match('#^[a-zA-Z0-9]+\.html$#', $debug_filename); } /** * Adds data into debug file * * @param null $result Request / Response / Errors from / to WebsServices */ private function debug($result = null) { $debug_html = ''; if ($this->lastCalledFunctionName) { $debug_html .= '
'; $debug_html .= print_r($this->lastCalledFunctionArgs, true); $debug_html .= ''; } if ($this->lastCalledFunctionPayload = (string)$this->client->__getLastRequest()) $debug_html .= '
'.$this->displayPayload().''; if ($result) { if ($err = $this->getError()) $debug_html .= '
'.$err.''; else { $result = print_r($result, true); $debug_html .= '
'; $debug_html .= strip_tags($result); $debug_html .= ''; } } else $debug_html .= '
'.print_r(self::$errors, true).''; if ($debug_html) { $debug_filename = $this->createDebugFileIfNotExists(); $current_content = Tools::file_get_contents(_DPDPOLAND_MODULE_DIR_.$debug_filename); @file_put_contents(_DPDPOLAND_MODULE_DIR_.$debug_filename, $debug_html.$current_content, LOCK_EX); if (self::DEBUG_POPUP) { echo ' '; } } } /** * Only for debugging purposes * * @return string Last called function payload */ private function displayPayload() { $xml = preg_replace('/(>)(<)(\/*)/', "$1\n$2$3", $this->lastCalledFunctionPayload); $token = strtok($xml, "\n"); $result = ''; $pad = 0; $matches = array(); while ($token !== false) { if (preg_match('/.+<\/\w[^>]*>$/', $token, $matches)) $indent = 0; elseif (preg_match('/^<\/\w/', $token, $matches)) { $pad -= 4; $indent = 0; } elseif (preg_match('/^<\w[^>]*[^\/]>.*$/', $token, $matches)) $indent = 4; else $indent = 0; $line = str_pad($token, Tools::strlen($token) + $pad, ' ', STR_PAD_LEFT); $result .= $line."\n"; $token = strtok("\n"); $pad += $indent; } return htmlentities($result); } }