Beispiel #1
0
 public function __construct()
 {
     // include some initial assertions on current context providing minimum
     // set of expected information
     assert('$_SERVER["HTTP_HOST"]');
     assert('$_SERVER["DOCUMENT_ROOT"]');
     assert('$_SERVER["SCRIPT_FILENAME"]');
     /*
      * PHASE 1: Detect basic pathnames and URL components
      */
     $this->frameworkPathname = dirname(dirname(__FILE__));
     $this->installationPathname = dirname($this->frameworkPathname);
     $this->isHTTPS = $_SERVER['HTTPS'] != false || $_SERVER['HTTP_X_HTTPS'] != false;
     // analyse special case of working behind reverse proxy
     if (array_key_exists('HTTP_X_ORIGINAL_URL', $_SERVER)) {
         $url = parse_url($_SERVER['HTTP_X_ORIGINAL_URL']);
         $this->hostname = $url['host'];
         $proxyPrefix = $url['path'];
     } else {
         $proxyPrefix = false;
     }
     if (trim($this->hostname) === '') {
         $this->hostname = $_SERVER['HTTP_HOST'];
     }
     /*
      * PHASE 2: Validate and detect current application
      */
     // validate location of processing script ...
     // ... must be inside document root
     if (path::isInWebfolder($_SERVER['SCRIPT_FILENAME']) === false) {
         throw new \InvalidArgumentException('script is not part of webspace');
     }
     // ... must be inside installation folder of TXF
     $this->scriptPathname = path::relativeToAnother($this->installationPathname, realpath($_SERVER['SCRIPT_FILENAME']));
     if ($this->scriptPathname === false) {
         throw new \InvalidArgumentException('script is not part of TXF installation');
     }
     // derive URL's path prefix to select folder containing TXF installation
     $this->prefixPathname = path::relativeToAnother(realpath(static::getDocumentRoot()), $this->installationPathname);
     if ($this->prefixPathname === false) {
         // installation's folder might be linked into document root using symlink
         // --> comparing pathname of current script with document root will fail then
         //     --> try alternative method to find prefix pathname
         $this->prefixPathname = path::relativeToAnother(static::getDocumentRoot(), dirname($_SERVER['SCRIPT_FILENAME']));
     }
     // running behind reverse proxy?
     if ($proxyPrefix) {
         // detect prefix used to control that reverse proxy
         $request = implode('/', $this->getRequestedScriptUri($dummy));
         $split = strpos($proxyPrefix, $request);
         if ($split >= 0) {
             // extract prefix required by reverse proxy
             $proxyPrefix = substr($proxyPrefix, 0, $split);
             // prepend extracted prefix of reverse proxy to previously
             // detected prefix used to address installation of TXF locally
             $this->prefixPathname = path::glue($proxyPrefix, $this->prefixPathname);
         }
     }
     // cache some derivable names
     list($this->applicationPathname, $this->applicationScriptPathname) = path::stripCommonPrefix($this->scriptPathname, $this->prefixPathname, null);
     // compile base URL of current installation
     $this->url = path::glue(($this->isHTTPS ? 'https://' : 'http://') . $this->hostname, $this->prefixPathname);
     // detect current application
     $this->application = application::current($this);
 }