/** * method: selectOptions * * todo: write documentation */ public static function selectOptions($array, $selected = NULL, $indexText = NULL, $indexValue = NULL, $createAttributes = false) { $options = array(); foreach (Amslib_Array::valid($array) as $arrayKey => $item) { if (is_array($item)) { $text = $indexText && isset($item[$indexText]) ? $item[$indexText] : ""; $value = $indexValue && isset($item[$indexValue]) ? $item[$indexValue] : ""; } else { if (is_string($item) && $indexText == "use_key") { $text = $item; $value = $arrayKey; } else { $text = $value = $item; } } $attributes = array(); if ($createAttributes && is_array($item) && isset($item[$indexText]) && isset($item[$indexValue])) { unset($item[$indexText], $item[$indexValue]); foreach ($item as $k => &$v) { $v = "{$k}='{$v}'"; } $attributes = $item; } $attributes = implode(" ", $attributes); if (strlen($text) == 0 || strlen($value) == 0) { continue; } $enabled = $value == $selected ? "selected='selected'" : ""; $options[] = "<option {$enabled} value='{$value}' {$attributes}>{$text}</option>"; } return implode("", $options); }
public function __construct($message, $data = array(), $use_callback = true) { parent::__construct($message); // This is so if an exception is generated using a true/false/1/0 type value // it'll still process into a predictable format if (is_scalar($data)) { $data = array("data" => $data); } foreach (Amslib_Array::valid($data) as $key => $value) { $this->setData($key, $value); } // NOTE: I have to test whether this records the correct location in all circumstances $this->setData("location", basename($this->getFile()) . "@" . $this->getLine()); // if this callback is usable, call it for the custom functionality if (self::$callback && is_callable(self::$callback) && $use_callback) { call_user_func(self::$callback, $this, Amslib_Debug::getStackTrace("type", "text")); } }
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; }
/** * method: importRouter * * todo: write documentation */ public static function importRouter($import) { $params = self::encodeURLPairs($import, "/", array("output", "url", "name")); // acquire the latest route for the export url and construct the url to call the external remote service $route = self::getRoute("service:framework:router:export:" . $import["output"]); $import["url"] = Amslib_File::resolvePath(Amslib_Website::expandPath($import["url"])); $import["url_full"] = rtrim($import["url"], "/") . rtrim($route["src"]["default"], "/") . "/{$params}/"; if ($import["output"] == "json") { // We are going to install a router using json as a data transfer medium // Acquire the json, decode it and obtain the domain $data = Amslib_File::getContents($import["url_full"], $error); if (strlen($error)) { Amslib_Debug::log("FAILED TO IMPORT ROUTER IN JSON FORMAT, OR OTHER PROBLEM DETECTED", $error, error_get_last()); } $data = json_decode($data, true); $domain = $data["domain"]; // For each route in the cache, create a new route in the local router, giving the name, group, domain and route data // You are not supposed to update the url cache, imported routes are not accessible through url // The reason for this is because it doesnt make sense a url from a remote system will be processed by the local system // All url requests for imported services should goto the remote server directly foreach (Amslib_Array::valid($data["cache"]) as $route) { // We cannot use the service inheritance when importing a remote router unset($route["extend"]); self::setRoute($route["name"], $route["group"], $domain, $route, false); } // Record that we imported something so we can possibly use this information self::setImportData($import["name"], $import); } else { if ($import["output"] == "xml") { $data = Amslib_File::getContents($import["url_full"], $error); if (strlen($error)) { Amslib_Debug::log("FAILED TO IMPORT ROUTER IN XML FORMAT, OR OTHER PROBLEM DETECTED", $error, error_get_last()); } // TODO: implement the logic to import from XML } } }
/** * method: connect * * Connect to the MYSQL database using various details * * todo: * - Need to move the database details to somewhere more * secure (like inside the database!! ROFL!! joke, don't do that!!!!) */ public function connect($details = false) { list($details, $password) = Amslib_Array::valid($this->setConnectionDetails($details)) + array(NULL, NULL); $this->disconnect(); $valid = Amslib_Array::hasKeys($details, array("server", "username", "database", "encoding")) && strlen($password); if ($valid) { try { $dsn = "mysql:dbname={$details["database"]};host={$details["server"]};charset={$details["encoding"]}"; $options = array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_STATEMENT_CLASS => array('Amslib_Database_Statement', array($this))); $this->connection = new PDO($dsn, $details["username"], $password, $options); $this->setEncoding($details["encoding"]); return $this->isConnected(true); } catch (PDOException $e) { $this->debug("DATABASE", array("exception" => $e->getMessage(), "data" => $details)); throw $e; } } else { $this->debug("DATABASE", "connection details were not valid", $details); } $this->disconnect(); return false; }
/** * method: setValue * * todo: write documentation */ public function setValue($key, $value) { switch ($key) { case "value": $this->data[$key] = Amslib_Array::valid($this->data[$key]); $this->data[$key] = array_merge($this->data[$key], $value); break; case "translator": $this->data[$key][$value["name"]] = $value; break; case "model": $this->data[$key] = $value; break; case "image": $this->data[$key][$value["id"]] = $value["value"]; break; case "view": $this->data[$key][$value["id"]] = $value; break; default: die(__METHOD__ . ": UNCONVERTED CODE: " . Amslib_Debug::pdump(true, $key, $value)); // THIS IS THE OLD UNCONVERTED CODE // This is important, because otherwise values imported/exported through transfer() will not execute in process() unset($value["import"], $value["export"]); if ($key == "value") { // Search and update any existing values $this->data[$key] = Amslib_Array::valid($this->data[$key]); foreach ($this->data[$key] as &$v) { if ($v["name"] == $value["name"] && !isset($v["export"]) && !isset($v["import"])) { $v["value"] = $value["value"]; return; } } // The value didnt already exist, so we must create it $this->data[$key][] = $value; } else { if (is_string($key)) { $this->data[$key] = $value; } else { $this->data[$key[0]][$key[1]] = $value; } } break; } }
/** * method: add * * add a validation to happen on a particular field * * parameters: * name - The name of the field to validate * type - The type of the field, methodology to use when validating * required - Boolean true or false, whether the field is mandatory or not * options - Other validation restrictions * * notes: * - An idea to expand on the flexibility here is to allow a field to be validated multiple times by different types * would allow you to layer validations instead of forcing the construction of customised validators which just * do two or three simple validation types together, this would require changing the structures a little bit because * right now, the field name is used as the key to the type of validator, we'd have to change that so each key can have * multiple types assigned to it * - I think to allow an item to be validated multiple times, we could just move to a system where * we don't use the $name parameter as the key to the rules array, we simply add a new rules[] element * and then loop through them identically, we could move the $name parameter into the array assigned * as the same "name" array index and it should work identically * - I think we need to stop assigning validData items to a blank string by default, because if an item * doesn't validate, it shouldn't appear in the array, but having a string here is beneficial because * it means you can just print the element and it'll print a blank string if it failed to validate. * this SOUNDS a good idea, until you start to think of situations where knowing it failed to validate * by merely looking for it and seeing it's not present is actually useful too. * The problem here seems to be a lack of direction, we haven't decided yet which is the best course of action */ public function add($name, $type = false, $required = false, $options = array()) { if (is_array($name) && ($rules = Amslib_Array::valid($name))) { foreach ($rules as $name => $r) { $type = count($r) ? array_shift($r) : NULL; $required = count($r) ? array_shift($r) : false; $options = count($r) ? array_shift($r) : array(); $this->add($name, $type, $required, $options); } return; } if (!strlen($name) || !strlen($type)) { return; } if ($this->errorLimit-- == 0) { Amslib_Debug::log("stack_trace"); die(__METHOD__ . ", error limit reached, terminating code because it appears an infinite loop"); } $required = isset($options["required"]) ? $options["required"] : $required; $this->rules[$name] = array("name" => $name, "type" => $type, "required" => $required, "options" => $options); $this->validData[$name] = ""; // Just to be sure, if the value is invalid and you have a default, you should set it now $this->setDefaultValue($name, $options); }
/** * method: add * * Add an array of tokens in one step to the storage * * params: * $list - The array of parameters to add * $overwrite - Whether you should overwrite the parameters if they exist or not */ public function add($list, $overwrite = true) { $list = Amslib_Array::valid($list); foreach ($list as $key => $value) { $this->set($key, $value, $overwrite); } }
/** * method: getValidationData * * todo: write documentation */ public static function getValidationData($identifier, $default = array()) { return Amslib_Array::valid(self::getHandlerData($identifier, $default, self::VD)); }
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; }
/** * method: getListExtended * * todo: write documentation */ public function getListExtended($i, $l = NULL) { if (!$l) { $l = $this->language; } $i = intval($i); $l = $this->getIdLang($l); $query = "name,id_object,value from {$this->table} where id_lang='{$l}' and id_object={$i}"; return Amslib_Array::valid($this->database->select($query)); }
/** * method: getImports * * todo: write documentation */ public function getImports() { return Amslib_Array::valid($this->import); }
/** * method: setLanguageKey * * Will retrieve the lang_key from the configuration and set it into the static data, ready to use when required * Then if the old key exists in the session, it will upgrade all the existing keys to the new language key, keeping * everything clean and tidy */ public function setLanguageKey() { $k = current(Amslib_Array::filter(Amslib_Array::valid($this->data["value"]), "name", "lang_key", true)); // key wasn't found, do nothing if (empty($k)) { return; } // key was found, attempt to upgrade old keys to new keys $old = self::$langKey; self::$langKey = $k["value"]; // loop through session, find matching keys against the old key, replace with the new keys foreach ($_SESSION as $key => $value) { if (strpos($key, $old) !== false) { unset($_SESSION[$key]); $key = str_replace($old, self::$langKey, $key); Amslib_SESSION::set(Amslib_File::reduceSlashes($key), $value); } } }
/** * method: getValidationData * * todo: write documentation */ public function getValidationData($plugin, $default = array()) { return Amslib_Array::valid($this->getHandlerData($plugin, $default, self::VD)); }
/** * method: getEmptyData * * todo: write documentation */ public function getEmptyData($group) { $rules = Amslib_Array::valid($this->getValidatorRules($group)); $values = array_fill_keys(array_keys($rules), ""); foreach ($rules as $k => $v) { if (isset($v[2]) && isset($v[2]["form_default"])) { $values[$k] = $v[2]["form_default"]; } } return $values; }