public function __construct($uri = "") { // Uri passed? if (empty($uri)) { // HTTPS? $this->protocol = array_key_exists("HTTPS", $_SERVER) && $_SERVER['HTTPS'] == 'on' ? "https" : "http"; // Use server data to form url $this->uri = $this->protocol . "://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; // And get some other info at the same time $this->method = $_SERVER['REQUEST_METHOD']; $this->domain = $_SERVER['HTTP_HOST']; $this->fullPath = $_SERVER['REQUEST_URI']; $this->accept = Format::FromHTTPAcceptHeader($_SERVER['HTTP_ACCEPT']); $this->acceptLanguage = explode(",", substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], ";"))); } else { // Parse url $this->uri = $uri; throw new Exception("Parsing of url not implemented", 1); //preg_match_all() } // Check base path $this->path = $this->fullPath; if (StringUtil::startsWith($this->path, BASE_PATH)) { // Remove it from path $basePath = BASE_PATH; if (StringUtil::endsWith($basePath, "/")) { $basePath = substr($basePath, 0, -1); } $this->path = substr($this->path, strlen($basePath)); } // Check path for ?querystring $qPos = strpos($this->path, '?'); if ($qPos > 0) { $this->path = substr($this->path, 0, $qPos); } // Check path for extension preg_match_all("/(.+)\\.([a-z]+)\$/i", $this->path, $matches); if (count($matches[0]) == 1 && !is_null(Format::FromExtension($matches[2][0]))) { // Extension found $this->extension = $matches[2][0]; $this->path = $matches[1][0]; // Now look for format that matches this one (if extension overrides Accept headers) if (ChickenWire::get("extensionOverridesAcceptHeaders") !== false || is_null($this->accept)) { // Find format $format = Format::FromExtension($this->extension); if (!is_null($format)) { $this->accept = array($format); } } } // Store format $this->format = $this->accept[0]; }
public static function FromHTTPAcceptHeader() { // Get the header $accept = $_SERVER['HTTP_ACCEPT']; // Split on , $types = explode(",", $accept); // Loop through types $foundFormatNames = array(); $foundFormats = array(); foreach ($types as $type) { // Create format from it $format = Format::FromContentType($type); // Not yet found? if (!is_null($format) && !in_array($format->format, $foundFormatNames)) { array_push($foundFormats, $format); array_push($foundFormatNames, $format->format); } } // Sort on preference ArrayUtil::SortOn($foundFormats, "preference", false); // Done. return $foundFormats; }
<?php use ChickenWire\Core\ChickenWire; use ChickenWire\Request\Format; // Set application namespace ChickenWire::set("applicationNamespace", "WipkipAdmin"); // Set PHP extension ChickenWire::set("phpExtension", "php"); // Make it so that extensions in urls are more important than accept headers ChickenWire::set("extensionOverridesAcceptHeaders", true); // Set the default output format ChickenWire::set("defaultFormat", Format::HTML()); ChickenWire::set("defaultCharset", "utf-8"); // Set the default output formats for routes ChickenWire::set("defaultRouteFormats", array(Format::HTML(), Format::JSON())); // Set locations for static assets ChickenWire::set("pathCSS", "static/css"); ChickenWire::set("pathJavascript", "static/js"); ChickenWire::set("pathImages", "static/images"); // Set whether to use absolute URLs ChickenWire::set("useAbsoluteUrls", false); // Memory cache (http://www.php.net/manual/en/book.memcache.php) ChickenWire::set("memCache", "localhost:11211"); // Logging if (ChickenWire::get("environment") == "development") { ChickenWire::set("log", "FireBug"); }