Пример #1
0
 /**
  * 	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);
 }
Пример #2
0
 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"));
     }
 }
Пример #3
0
 /**
  * 	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
         }
     }
 }
Пример #4
0
 /**
  * 	method:	resetImageParam
  *
  * 	todo: write documentation
  */
 protected function resetImageParam()
 {
     $this->image_params = Amslib_Array::removeKeys($this->image_params, array("handle", "width", "height"));
 }
Пример #5
0
 /**
  * 	method:	selectValue
  *
  * 	todo: write documentation
  *
  * 	notes:
  * 		Anxo has explained that perhaps it's unnecessary that in the query to put the field if you are going to put
  * 		the field you want in the first parameter, so in the query you can just put a "$field $query" and it'll
  * 		select only what you want, without duplication.  He's clever sometimes.
  */
 public function selectValue($field, $query, $params = array(), $numResults = 0, $optimise = false)
 {
     $field = trim($field);
     $values = $this->select($query, $params, $numResults, $optimise);
     if ($numResults == 1 && $optimise) {
         return isset($values[$field]) ? $values[$field] : NULL;
     }
     //	TODO: This hasn't been tested yet, it might not return exactly what I want
     if ($numResults != 1 && !$optimise) {
         //	FIXME? Why am I optimising the array, when optimise is being tested for false?
         //	NOTE: I think the reason I never found this issue before was I always using 1,true for numResults/optimise
         return Amslib_Array::pluck($values, $field);
     }
     return $values;
 }
Пример #6
0
 /**
  * 	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;
     }
 }
Пример #7
0
 /**
  * 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:	getDatabaseMessage
  *
  * 	todo: write documentation
  */
 public function getDatabaseMessage($plugin, $default = false)
 {
     return Amslib_Array::filterKey($this->getDatabaseErrors($plugin, $default), array("db_error", "db_location"));
 }
Пример #9
0
 /**
  * 	method:	getDatabaseMessage
  *
  * 	todo: write documentation
  */
 public static function getDatabaseMessage($identifier, $default = false)
 {
     return Amslib_Array::filterKey(self::getDatabaseErrors($identifier, $default), array("db_error", "db_location"));
 }
Пример #10
0
 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;
 }
Пример #11
0
 /**
  * 	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));
 }
Пример #12
0
 /**
  * 	method:	getImports
  *
  * 	todo: write documentation
  */
 public function getImports()
 {
     return Amslib_Array::valid($this->import);
 }
Пример #13
0
 /**
  * 	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);
         }
     }
 }
Пример #14
0
 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;
 }
Пример #15
0
 /**
  * 	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);
     }
 }
Пример #16
0
 /**
  * 	method:	backtrace
  *
  *	a method to obtain a debug backtrace of the current PHP function stack with options to
  *	slice a part of the array
  *
  *	this function uses variable arguments, it will only slice the string if it first two
  *	parameters are integer numbers after that, all the arguments will be tested whether
  *	they are a string and if so, they will be used to filter each array stack element to
  *	return only the keys which match the strings, dropping all the unwanted keys from
  *	each array stack element
  *
  *	parameters:
  *		$start - The starting offset to slice from the array stack
  *		$finish - the ending offset to slice from the array stack
  *		vargs - any number of string variables which will be used to filter each array index to reutrn only the keys requested
  *
  *	notes:
  *		-	The function is really really memory hungry, use Amslib_Debug::getStackTrace if you can
  *		-	This is a really ancient function, it's hard to know what it's trying to do
  */
 public static function backtrace()
 {
     //	Find out who is using this method so I can upgrade it's code and delete this
     Amslib_Debug::log("stack_trace");
     $args = func_get_args();
     $bt = debug_backtrace();
     $slice = array($bt);
     if (count($args) && is_numeric($args[0])) {
         $slice[] = array_shift($args);
     }
     if (count($args) && is_numeric($args[0])) {
         $slice[] = array_shift($args);
     }
     if (count($slice) > 1) {
         $bt = call_user_func_array("array_slice", $slice);
     }
     return Amslib_Array::filterKey($bt, Amslib_Array::filterType($args, "is_string"));
 }
Пример #17
0
 /**
  * 	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;
 }