/** * method: documentRoot * * todo: write documentation */ public static function documentRoot($docroot = NULL) { if ($docroot && is_string($docroot) && strlen($docroot)) { $docroot = Amslib_String::reduceSlashes($docroot); } else { if (isset($_SERVER["CONTEXT_DOCUMENT_ROOT"])) { $docroot = $_SERVER["CONTEXT_DOCUMENT_ROOT"]; if (isset($_SERVER["CONTEXT_PREFIX"])) { $docroot = Amslib_String::rchop($docroot, $_SERVER["CONTEXT_PREFIX"]); } } else { if (isset($_SERVER["DOCUMENT_ROOT"])) { $docroot = $_SERVER["DOCUMENT_ROOT"]; } else { Amslib_Debug::log("Not sure how to obtain the docroot on this platform", $docroot, $_SERVER); // NOTE: ADD DEBUGGING CODE HERE AND UPDATE WHEN YOU FIND A SCENARIO WHICH WORKS //die(__METHOD__.Amslib_Debug::vdump(array("docroot"=>$docroot,"server"=>$_SERVER))); return false; } } } self::$docroot = $docroot; self::$docroot = realpath(self::$docroot); self::$docroot = self::win2unix(self::$docroot); return self::$docroot; }
/** * 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: 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 } } }
function TestOfabsolute() { $this->caseTitle('absolute'); $test = 'http://www.crm-test.local/vendor/amstudios/amslib/file/index.php'; $this->log($test); $result = Amslib_File::absolute($test); $this->log($result); $this->assertTrue(strpos($result, 'http://www.crm-test.local') === 0); $test = 'C:\\Sites\\crm-test\\vendor\\amstudios\\amslib\\file\\index.php'; $this->log($test); $result = Amslib_File::absolute($test); $this->log($result); $this->assertTrue(strpos($result, '/Sites/crm-test') === 0); $test = 'vendor\\amstudios\\amslib\\file\\index.php'; $this->log($test); $result = Amslib_File::absolute($test); $this->log($result); $this->assertEqual($result, '/Sites/crm-test/vendor/amstudios/amslib/file/index.php'); }
public function getValue($key) { switch ($key) { // We can put specific keys which do something other than get the value of selectors // But of course this stops us from using those keys as XML Nodes in the code // This isn't really a problem, since I can't see them overlapping right now case "filename": return Amslib_File::absolute($this->location . "/" . $this->packageName); break; } return false; }
/** * method: setCacheDirectory * * todo: write documentation */ public function setCacheDirectory($directory) { $directory = Amslib_File::reduceSlashes($directory); if (!is_dir($directory) && !file_exists($directory)) { if (!Amslib_File::mkdir($directory)) { return false; } } $this->cache_dir = $directory; }
/** * method: setLocation * * todo: write documentation */ public function setLocation($location) { $this->location = Amslib_File::absolute($location); // NOTE: (08/03/2015) I don't think the below setPaths will work the way expected // as each plugin will keep resetting them with whatever location they are // given, hence there is no "plugin" location, it could diff and current plugin // will obviously change depending on the plugin being loaded, so this is // way off for a start. // NOTE: (05/05/2014) why the duplicated name....or more or less // duplicated, obviously I have old code to fix Amslib_Website::setPath("plugin", $this->location); Amslib_Website::setPath("current_plugin", $this->location); }
/** * method: getJavascript * * todo: write documentation */ public static function getJavascript() { $output = ""; $content = ""; foreach (self::$js as $file) { $content .= file_get_contents(Amslib_File::absolute($file)); } $output .= "<script type='text/javascript'>{$content}</script>"; foreach (self::$jsc as $condition => $list) { $content = ""; foreach ($list as $file) { $content .= file_get_contents(Amslib_File::absolute($file)); } $output .= "<!--[{$conditional}]><script type='text/javascript'>{$content}</script><![endif]-->"; } foreach (self::$jsi as $file) { $output .= "<script type='text/javascript' src='{$file}'></script>"; } return $output; }
/** * method: file_exists * * todo: write documentation */ protected function validate_file_exists($name, $value, $required, $options) { if ($options["absolute"] == true) { $value = Amslib_File::absolute($value); } if (is_file($value)) { $this->setValid("{$options["key"]}{$name}", $value); return true; } if ($required == false) { return true; } return "FILE_EXISTS_FAILED"; }
/** * method: sanitiseURL * * todo: write documentation */ protected function sanitiseURL($url) { // Capture the http:// part so you can replace it afterwards // NOTE: what happens if you are running from https:// ? $http = strpos($url, "http://") !== false ? "http://" : ""; // strip away the http:// part first, because it won't survive the reduceSlashes otherwise $url = $http . Amslib_File::reduceSlashes(str_replace("http://", "", $url)); return $url == "/" ? "" : $url; }
/** * method: reduceSlashes * * A method to reduceSlashes but take care of urls like http:// so they don't break * * params: * $string - The string to reduce the slashes in * $token - The token to split the string on, this should * only exist once in the string and the right * side will be reduced and the left side will not * defaults: "://" * * returns: * A string with any // or ///[n+] reduced to / */ public static function reduceSlashes($string, $token = "://") { list($prefix, $postfix) = explode($token, $string) + array(NULL, NULL); if ($postfix) { $string = $postfix; } return ($postfix ? $prefix . $token : "") . Amslib_File::reduceSlashes($string); }
/** * method: openFile * * todo: write documentation */ public function openFile($filename) { // NOTE: Added a call to Amslib_Website::abs to fix finding the file, because in some cases, // the file cannot be found. But I am not sure of the side-effects (if any) of doing this $this->filename = Amslib_File::find(Amslib_Website::absolute($filename), true); $document = new DOMDocument('1.0', 'UTF-8'); if (is_file($this->filename) && $document->load($this->filename)) { $this->xpath = new DOMXPath($document); return true; } Amslib_Keystore::add("error", "Amslib_XML::openFile[{$filename}], path[{$this->filename}] failed"); return false; }
/** * 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: getLanguage * * Obtain the language set in the session for the specific type of language "domain" (normally website or content) * * parameters: * $name - The name of the language to retrieve * * returns: * Boolean false if obtaining a language has failed, or the 4 character language code, e.g: en_GB, en_ES * * notes: * - See the notes from the method Amslib_Plugin_Application::setLanguage for what the * language code, domains terms mean. * - Remember also, if a language is not found, the first language in the approved list will be * selected, stored and returned as a sensible default */ public static function getLanguage($name) { $lang = false; if (is_string($name) && strlen($name)) { $lang = Amslib_SESSION::get(Amslib_File::reduceSlashes(self::$langKey . "/{$name}")); } if (!$lang) { $lang = self::setLanguage($name, current(self::getLanguageList($name))); } return $lang; }
/** * method: extract * * todo: write documentation */ public static function extract($path) { // If the path is just /, then obviously it has nothing, just return it if ($path == "/") { return $path; } $parts = explode("/", trim($path, "/ ")); if (count($parts)) { $first = array_shift($parts); $lang = self::sanitise($first); if ($lang == $first) { self::set($lang); // Make sure there are only single slashes and make sure the path starts and ends with a slash return Amslib_File::reduceSlashes("/" . implode("/", $parts) . "/"); } } return $path; }
/** * method: addLocation * * todo: write documentation */ public static function addLocation($location) { self::$location[] = Amslib_File::absolute($location); }