/** * method: initialise * * todo: write documentation */ public static function initialise() { if (self::$is_initialised) { return; } $search = array_merge($_SERVER, $_GET); self::$pathList["__WEBSITE_ROOT__"] = $search["__WEBSITE_ROOT__"]; self::$path = NULL; self::$base = self::getPath("__WEBSITE_ROOT__"); if (self::$base) { // Obtain the path within the website, without the website base // we use this to calculate the path inside the website, not relative to the document root self::$path = Amslib_String::lchop($_SERVER["REQUEST_URI"], self::$base); self::$path = Amslib_String::rchop(self::$path, "?"); self::$path = Amslib_File::reduceSlashes("/" . self::$path . "/"); } // Now automatically load the amslib routers configuration as the framework system // This allows amslib to add routes the system can use to import/export router configurations // This isn't part of your application, this is part of the system and used to self-configure self::load(Amslib::locate() . "/router/router.xml", "xml", "framework"); self::$is_initialised = true; // remove this from the $_GET superglobal, it's kind of annoying to keep finding it in the application code Amslib_GET::delete("__WEBSITE_ROOT__"); }
/** * method: setCommands * * todo: write documentation */ public function setCommands($source) { if (is_array($source)) { $this->commands = $source; } elseif (is_string($source)) { $this->commands = explode("/", trim(Amslib_String::rchop($source, "?"), "/")); } else { return $this->setError(self::COMMAND_SEQUENCE_INVALID); } return true; }
/** * method: find * * todo: write documentation */ public static function find($filename, $includeFilename = false) { if (self::fileExists($filename)) { return $includeFilename ? $filename : Amslib_String::rchop($filename, "/"); } $includePath = array_reverse(explode(PATH_SEPARATOR, ini_get("include_path"))); foreach ($includePath as $path) { $test = strpos($filename, "/") !== 0 ? "{$path}/{$filename}" : "{$path}{$filename}"; if (self::fileExists($test)) { return realpath($includeFilename ? $test : $path); } } return false; }
/** * method: findResource * * todo: write documentation */ protected function findResource($resource, $absolute = false) { // If the resource has an attribute "absolute" don't process it, return it directly // An absolute resource will also have a protocol token inside the string if ($absolute || strpos($resource, "://") !== false) { return $resource; } $location = $this->getLocation(); // PREPARE THE STRING: expand any parameters inside the resource name $resource = self::expandPath($resource); // NOTE: we have to do this to get around a bug in lchop/rchop $query = ($str = Amslib_String::lchop($resource, "?")) == $resource ? false : $str; $resource = Amslib_String::rchop($resource, "?"); $output = false; // TEST 1: look in the package directory for the file $test1 = Amslib_File::reduceSlashes("{$location}/{$resource}"); if (!$output && file_exists($test1)) { $output = Amslib_File::relative($test1); } // TEST 2: Test whether the file "exists" without any assistance if (!$output && file_exists($resource)) { $output = Amslib_File::relative($resource); } // TEST 3: Does the file exists relative to the document root? $test3 = Amslib_File::absolute($resource); if (!$output && file_exists($test3)) { // Why do we see whether $test3 exists, then discard it and use $resource? $output = Amslib_File::relative($resource); } // TEST 4: search the include path for the file $test4 = Amslib_File::find($resource, true); if ($output && file_exists($test4)) { $output = Amslib_File::relative($test4); } // Return either $output value on it's own, or appended with the query if required // If output is false or valid string, it'll return $output at minimum, // only appending the query if it's valid too return $output && $query ? "{$output}?{$query}" : $output; }
/** * method: __construct * * todo: write documentation * * notes: * - The object is basically hard coded to ONLY use $_POST as an input source, but this might not be true * - In situations where $_GET is used, you'd need to post some parameters through $_GET, others $_POST * - So this should be made more flexible, so the webservice can define the input source and this accomodates */ public function __construct() { // Reset the service data and session structures $this->data = array(); $this->session = array(self::HD => array()); // FIXME: we are hardcoding a route "home" which might not exist, this could be a bad idea $default_url = Amslib_Router::getURL("home"); $url_return = Amslib_POST::get("url_return", Amslib_POST::get("return_url", $default_url)); $url_return = Amslib_String::rchop($url_return, "?"); $url_success = Amslib_POST::get("url_success", Amslib_POST::get("success_url", $url_return)); $url_success = Amslib_String::rchop($url_success, "?"); $url_failure = Amslib_POST::get("url_failure", Amslib_POST::get("failure_url", $url_return)); $url_failure = Amslib_String::rchop($url_failure, "?"); // TODO: I should remove all the url parameters from the source data so it doesn't pass through as data // NOTE: well then in this case perhaps I should have to namespace them too so they are in a separate // part of the source data and not obvious "url_return" might be a bit generic // NOTE: but if I namespace them, I don't want to pass that complexity onto the programmer // NOTE: perhaps this means I need to add functions to build these parameters for the programmer // instead of making them build them personally $this->setSuccessURL($url_success); $this->setFailureURL($url_failure); // blank the appropriate session key to stop previous sessions overlapping // NOTE: what if you are using the json output? then there is no key to erase, // so you're just creating session keys and not using them Amslib_SESSION::get(self::SR, false, true); // Obtain the old return_ajax parameter, either as json or false $return_ajax = Amslib_POST::get("return_ajax", false) ? "json" : false; // Obtain the new output_format parameter, or default to the return_ajax value if not found $format = Amslib_POST::get("output_format", $return_ajax); // Sanitise the format to be one of three possible options, or default to false // NOTE: we might want to use redirect, but we cannot store anything useful in the session // NOTE: however this is valid reasoning: to do the job, redirect, but don't bother writing session data cause it cannot be used if (!in_array($format, array(false, "json", "session"))) { $format = false; } // Now it should be safe to set the output format, false will of course reset $this->setOutputFormat($format); // Initialise all the terminator groups that can exist $this->terminatorList = array_fill_keys(array("common", "success", "failure"), array()); }