public function execute($raw = false) { $reply = $exception = false; if ($this->debug) { $raw = true; } try { if (strlen($this->url) == 0) { throw new Exception("webservice url was invalid"); } $curl = curl_init(); // This is just the first version of this code, it works, but it's hardly very elegant. // Also, I think I need to merge code from Amslib_Plugin_Service because we seem to be duplicating // it a lot here, perhaps we need to have a common shared object with methods to set and get from // the storage variable if ($this->sharedSession) { $key_remote = "/amslib/webservice/session/remote/"; $key_request = "/amslib/webservice/session/request/"; $id_session = Amslib_SESSION::get($key_remote); if ($id_session) { $cookie = "PHPSESSID={$id_session}; path=/"; //.Amslib_Router_URL::getFullURL(); curl_setopt($curl, CURLOPT_COOKIE, $cookie); Amslib_SESSION::set("REQUEST_COOKIE", $cookie); } else { $this->params[$key_request] = true; } } $params = http_build_query(Amslib_Array::valid($this->params)); if ($this->username && $this->password) { curl_setopt($curl, CURLOPT_USERPWD, "{$this->username}:{$this->password}"); } curl_setopt($curl, CURLOPT_URL, $this->url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTP_VERSION, 1.0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_POSTFIELDS, $params); $reply = curl_exec($curl); if (!$reply || !strlen($reply)) { Amslib_Debug::log("CURL ERROR", curl_error($curl), Amslib_Debug::dump($reply)); curl_close($curl); return false; } curl_close($curl); $response = new Amslib_Webservice_Response($reply); if ($this->sharedSession && !$id_session) { $data = $response->getData("amslib"); if ($value = $data->getKey($key_remote)) { Amslib_SESSION::set($key_remote, $value); } } return $response; } catch (Exception $e) { $exception = $e->getMessage(); } Amslib_Debug::log("EXCEPTION: ", $exception, "WEBSERVICE URL: ", $this->url, "PARAMS: ", $this->params, "DATA: ", $reply); return false; }
protected static function processMessage($list) { $data = array(); $function = false; foreach ($list as $k => $a) { if (is_string($a) && strpos($a, "stack_trace") === 0) { $command = explode(",", $a); $stack = Amslib_Debug::getStackTrace("type", "text"); $stack = explode("\n", $stack); $c = count($command); if ($c == 2) { $stack = array_slice($stack, $command[1]); } else { if ($c == 3 && $command[2] > 0) { $stack = array_slice($stack, $command[1], $command[2]); } } $trace = array("\n"); foreach ($stack as $row) { $trace[] = "[STACK TRACE] " . str_replace("\n", "", Amslib_Debug::dump($row)); } $data[] = implode("\n", $trace); } else { if (is_string($a) && strpos($a, "func_offset") === 0) { $command = explode(",", array_shift($list)); if (count($command) == 1) { $function = $command[0]; } } else { if (is_object($a)) { $a = array(get_class($a), Amslib_Debug::dump($a)); } if (is_array($a)) { $a = Amslib_Debug::dump($a); } if (is_bool($a)) { $a = $a ? "true" : "false"; } if (is_null($a)) { $a = "null"; } $a = trim(preg_replace("/\\s+/", " ", $a)); $data[] = "arg[{$k}]=> {$a}"; } } } $function = self::getLogOrigin(); return $data; }
/** * method: getError * * todo: write documentation */ public function getError() { return is_array($this->error) ? Amslib_Debug::dump($this->error) : $this->error; }
/** * method: createObject * * todo: write documentation */ protected function createObject(&$object, $singleton = false, $dieOnError = false) { // invalid requests get returned false if (!$object || empty($object)) { return false; } // existing objects just get returned from their cache if (isset($object["cache"])) { return $object["cache"]; } // otherwise, we go through the process of loading them, first of all, make sure the object file is in the system if (isset($object["file"])) { Amslib::requireFile($object["file"], array("require_once" => true)); } // Create the generic string for all the errors $error = "FATAL ERROR(" . __METHOD__ . ") Could not __ERROR__ for plugin '{$this->name}'<br/>"; // false by default to signal an error in creation $cache = false; if (isset($object["value"]) && class_exists($object["value"])) { if ($singleton) { if (method_exists($object["value"], "getInstance")) { $cache = call_user_func(array($object["value"], "getInstance")); } else { // your object was not a compatible singleton, we need to find the getInstance method $message = str_replace("__ERROR__", "find the getInstance method in the API object '{$object["value"]}'", $error); Amslib_Debug::log($message); die($message); } } else { // yay! we have a proper object $cache = new $object["value"](); } } else { if ($dieOnError) { // The class was not found, we cannot continue // NOTE: I am not sure why we cannot continue here, it might not be something // critical and yet we're stopping everything // NOTE: It's hard to die here because perhaps there is a better way of not dying everytime an object // which is requested that doesn't exist won't load, we immediately kill everything // perhaps a nicer way would be to notify the system of the error, but build better code // so that if an object doesnt exist, we could use a dummy object with a __call interface // and register all the methods as returning false, therefore hiding the missing object // but not killing the process, but handling all the failures which happen and properly written code // which respects the methods that return false, will keep functioning but failing to operating, // although they will not crash $error = str_replace("__ERROR__", "find class using configuration " . Amslib_Debug::dump($object) . "'", $error); Amslib_Debug::log("stack_trace", $error, $object); die($error); } } if ($cache) { $object["cache"] = $cache; } return $cache; }
/** * method: decodeResponse * * todo: write documentation */ protected function decodeResponse($response) { if ($response == false) { return false; } if (is_string($response)) { if ($this->debug) { print "DEBUGGING: " . Amslib_Debug::dump($response); } $parameters = explode("&", $response); $this->response = array(); foreach ($parameters as $p) { list($key, $value) = explode("=", $p); $this->response[urldecode($key)] = urldecode($value); } return $this->getResponse("ACK") == "Success" ? true : false; } }
public function execute() { try { if (strlen($this->url) == 0) { throw new Exception("webservice url was invalid"); } $curl = curl_init(); $params = http_build_query(Amslib_Array::valid($this->params)); if ($this->username && $this->password) { curl_setopt($curl, CURLOPT_USERPWD, "{$this->username}:{$this->password}"); } curl_setopt($curl, CURLOPT_URL, $this->url); curl_setopt($curl, CURLOPT_POST, true); curl_setopt($curl, CURLOPT_HTTP_VERSION, 1.0); curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); curl_setopt($curl, CURLOPT_HEADER, false); curl_setopt($curl, CURLOPT_POSTFIELDS, $params); // FIXME: perhaps we shouldn't disable these? it's maybe a security problem if (strpos($this->url, 'https://') !== false) { curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); } $this->reply = curl_exec($curl); if (!$this->reply || !strlen($this->reply)) { Amslib_Debug::log("CURL ERROR", curl_error($curl), Amslib_Debug::dump($this->reply)); curl_close($curl); return false; } curl_close($curl); return true; } catch (Exception $e) { $exception = $e->getMessage(); } Amslib_Debug::log("EXCEPTION: ", $exception, "WEBSERVICE URL: ", $this->url, "PARAMS: ", $this->params, "DATA: ", $reply); return false; }