/** * method: load * * returns: * Boolean true or false depending on whether it succeeded, there are some codepaths which call setError() * this is because of serious errors which can't be handled at the moment * * NOTE: * - if language is false, you need to call setLanguage before you * call load otherwise the source can't load the correct file */ public function load() { if (!$this->language) { return false; } $dir = $this->getConfig("directory"); $source = Amslib_File::reduceSlashes("{$dir}/{$this->language}.xml"); $filename = false; try { // NOTE: This is ugly and I believe it's a failure of Amslib_File::find() to not do this automatically if (file_exists($source)) { $filename = $source; } else { if (file_exists($f = Amslib_File::find(Amslib_Website::relative($source), true))) { $filename = $f; } else { if (file_exists($f = Amslib_File::find(Amslib_Website::absolute($source), true))) { $filename = $f; } } } if (!$filename) { Amslib_Debug::log("stack_trace", "directory", $dir, "filename", $filename, "language", $this->language); } else { $this->qp = Amslib_QueryPath::qp($filename); return true; } } catch (Exception $e) { Amslib_Debug::log("Exception: ", $e->getMessage(), "file=", $filename, "source=", $source); Amslib_Debug::log("stack_trace"); } return false; }
/** * method: getURL * * todo: write documentation */ public static function getURL($name = NULL, $group = NULL, $lang = "default", $domain = NULL) { if ($domain == NULL) { $domain = self::$domain; } // if the $name parameter is an array, this code will split the $name parameter to // use the first as the route name and all the rest as optional parameters if (is_array($name)) { $params = array_slice($name, 1); $name = array_shift($name); } else { $params = array(); } $route = self::getRoute($name, $group, $domain); // NOTE: If you request a webservice and cannot find it, make this a serious error // You cannot return a "closest" url in this case, cause it causes a LOT of problems // NOTE: If you call a webservice which results in the home page opening, which is what called // the webservice in the first place, it'll cause an infinite loop of repeatedly trying to // open the home page, which generates a new webservice request and this cycle never ends // NOTE: This solves the problem, for now, but ultimately, shows a critical problem that needs addressing if (strpos($name, "service:") === 0 && $route["name"] === false) { return false; } // NOTE: I think it's safe to assume sending NULL means you want the default language // NOTE: otherwise it would never ever match a language and the system would fail worse // NOTE: although this is silly, cause it means that passing "default" and NULL are the same, why not just use one? if ($lang == NULL) { $lang = "default"; } $url = isset($route["src"][$lang]) ? $route["src"][$lang] : ""; // If the url contains a http, don't attempt to make it relative, it's an absolute url // NOTE: perhaps a better way to solve this is to mark routes as absolute, then I don't have to "best guess" $url = strpos($url, "http") !== false ? $url : Amslib_Website::relative($url); // NOTE: perhaps I should move this highly specific code into it's own method so I can keep things small and tidy // Now we can replace all the wildcard targets in the url with parameters passed into the function // NOTE: I like codeigniters version of this better, they use like (:any) or (:num) and replace specifically $target = array('(\\w+)', '(.*?)', '(.*)'); $append = array(); foreach (Amslib_Array::valid($params) as $k => $p) { // I copied this replacement code from: http://stackoverflow.com/revisions/1252710/3 $pos = false; foreach ($target as $t) { $pos = strpos($url, $t); if ($pos !== false) { $pos = array($pos, strlen($t)); break; } } if (is_array($pos)) { $url = substr_replace($url, $p, $pos[0], $pos[1]); } else { if (!is_array($p)) { $p = array($p); } $append = array_merge($append, $p); } } // NOTE: this is pretty dumb, I should try to work out a generic way to deal with all of these // http-like prefixes so I don't have to keep writing this shitty code everywhere..... // NOTE: I think I have solved this in the new Amslib_Website::reduceSlashes method $prefix = ""; if (strpos($url, "http://") !== false) { $prefix = "http://"; } if (strpos($url, "https://") !== false) { $prefix = "https://"; } return $prefix . Amslib_File::reduceSlashes(str_replace($prefix, "", $url) . implode("/", array_filter($append)) . "/"); }
/** * method: getFullURL * * todo: write documentation */ public static function getFullURL() { return Amslib_Website::relative(Amslib_Router::getPath()); }
/** * method: glob * * todo: write documentation */ public static function glob($location, $relative = false) { $items = glob(Amslib_Website::absolute($location)); if ($relative) { foreach ($items as &$i) { $i = Amslib_Website::relative($i); } } return $items; }
/** * method: __construct * * todo: write documentation */ public function __construct($source) { $this->route = false; $this->import = false; $this->valid = false; $filename = false; try { // NOTE: This is ugly and I believe it's a failure of Amslib_File::find() to not do this automatically if (file_exists($source)) { $filename = $source; } else { if (file_exists($f = Amslib_File::find(Amslib_Website::relative($source), true))) { $filename = $f; } else { if (file_exists($f = Amslib_File::find(Amslib_Website::absolute($source), true))) { $filename = $f; } } } if (!$filename) { Amslib_Debug::log("The filename was not valid, we could not getRoutes from this XML Source"); } else { Amslib_QueryPath::qp($filename); Amslib_QueryPath::execCallback("router > path[name]", array($this, "configPath"), $this); Amslib_QueryPath::execCallback("router > service[name]", array($this, "configService"), $this); Amslib_QueryPath::execCallback("router > callback", array($this, "configCallback"), $this); Amslib_QueryPath::execCallback("router > import", array($this, "configImport"), $this); Amslib_QueryPath::execCallback("router > export", array($this, "configExport"), $this); $this->valid = true; } } catch (Exception $e) { Amslib_Debug::log("Exception: ", $e->getMessage(), "file=", $filename, "source=", $source); Amslib_Debug::log("stack_trace"); } }
/** * method: getImage * * todo: write documentation * * NOTE: this function is being abused to be a generic "make relative url for a file" method for pretty much everything */ public function getImage($id, $relative = true) { if (!is_string($id)) { return false; } // Step 1: Expand any recognised tokens in the path $path = Amslib_Website::expandPath($id); // Step 2: Is the image absolute, beginning with http? if (strpos($id, "http") === 0) { return $id; } // Step 3: find the image inside the plugin if (isset($this->image[$id])) { return $this->image[$id]; } // Step 4: find the image relative to the website base (perhaps it's a path) $path = Amslib_Website::absolute($this->location . "/" . $id); if (file_exists($path)) { return $relative ? Amslib_Website::relative($path) : $path; } Amslib_Debug::log("stack_trace", "failed to find image", $id, $relative, $path, $this->location); // Step 4: return false, image was not found return false; }