PageRenderTime 204ms CodeModel.GetById 121ms app.highlight 7ms RepoModel.GetById 73ms app.codeStats 0ms

/atk4/lib/PageManager.php

https://github.com/mahimarathore/mahi
PHP | 220 lines | 134 code | 12 blank | 74 comment | 1 complexity | 0afeb7c1d01c781be7ada6805168c085 MD5 | raw file
  1<?php // vim:ts=4:sw=4:et:fdm=marker
  2/*
  3 * Undocumented
  4 *
  5 * @link http://agiletoolkit.org/
  6*//*
  7==ATK4===================================================
  8   This file is part of Agile Toolkit 4
  9    http://agiletoolkit.org/
 10
 11   (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
 12   Distributed under Affero General Public License v3 and
 13   commercial license.
 14
 15   See LICENSE or LICENSE_COM for more information
 16 =====================================================ATK4=*/
 17class PageManager extends AbstractController {
 18    /*
 19     * This is a generic page manager. For web applications it calculates
 20     * base URI, sets up path manager with the URI locations, determines
 21     * which page was requested
 22     *
 23     * This class works with PathFinder, ApiWeb, and Location.
 24     */
 25
 26
 27    // you can access variabless below through $this->api->pm->base_url
 28    // concatinate them to get full URL
 29
 30    public $base_url;           // http://yoursite.com:81
 31    /*
 32       Base URL defines the absolute destination of our server. Because some
 33       other resources may be located outside of our Base Path, we need to
 34       know a Base URL.
 35
 36       For CLI scripts, you need to set this manually. Also if you are
 37       going to use URLs in emails, you should use this.
 38
 39       See also: URL::useAbsoluteURL();
 40     */
 41
 42    public $base_path;          // /admin/
 43    /*
 44       Base PATH points to the top location of our project. Basically it's
 45       where the project is installed in the webroot. This is determined
 46       by thelocation of catch-all file. It is determined by SCRIPT_NAME
 47       which should be supported by most web installations. It will also
 48       work when mod_rewrite is not used.
 49
 50       You can use $base_path in your script to put it say on a logo link
 51
 52       Also - some other parts of the library may have a different path,
 53       for example base_path could be = /admin/, and atk4_path could be /amodules/
 54
 55       If project is installed in web-root, then $base_path will be "/"
 56
 57       path always starts and ends with slash
 58     */
 59
 60    public $page;               // user/add
 61    /*
 62       This is a third and a final part of the URLs. This points to a page
 63       which were reuqested. You can pass path to getDestinationURL() function,
 64       as a first argument. Also $path is used to determine which page class
 65       to load.
 66
 67       Page must never start with slash. Also if path is empty, then
 68       the "index" is used automatically.
 69
 70
 71     */
 72
 73    public $base_directory;     // /home/web/admin/ - physical path
 74    public $template_filename;
 75
 76    function init(){
 77        parent::init();
 78        $this->page=&$this->api->page;      // link both variables
 79
 80        $this->api->pm=$this;
 81        // Firstly, the original URL is retrieved. This function should
 82        // take care of all possible rewrite engines and bring up a real
 83        // URL which matches the one in the browser. Also e will need to
 84        // determine a relative path for the requested page
 85        $this->parseRequestedURL();
 86
 87        // This function will continue initialization of the page itself.
 88        $this->calculatePageName();
 89    }
 90
 91    function calculatePageName(){
 92        // Now. We need to decide what will be the main page to
 93        // to display the whole thing. This page will contain a
 94        // main template and will be responsible for rendering the
 95        // whole page. This function will initialize the object and
 96        // return it
 97
 98        // Lastly we need a sub-class, which would worry only about
 99        // the requested page. This is what we call - a page.
100    }
101
102    function parseRequestedURL(){
103        $this->base_path=$this->unix_dirname($_SERVER['SCRIPT_NAME']);
104        // for windows
105        if(substr($this->base_path,-1)=='\\')$this->base_path=substr($this->base_path,1,-1).'/';
106        if(substr($this->base_path,-1)!='/')$this->base_path.='/';
107
108        // We are assuming that all requests are being redirected though a single file
109        $this->base_directory=$this->unix_dirname($_SERVER['SCRIPT_FILENAME']).'/';
110
111        // This is the re-constructions of teh proper URL.
112        // 1. Schema
113        $url=$this->api->getConfig('atk/base_url',null);
114        if(is_null($url)){
115            // Detect it
116            $url = 'http';
117            $https = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || $_SERVER['SERVER_PORT']==443;
118            if($https)$url.='s';
119
120            // 2. Continue building. We are adding hostname next and port.
121            $url .= "://".$_SERVER["SERVER_NAME"];
122            //if($_SERVER["SERVER_PORT"]!="80")$url .= ":".$_SERVER['SERVER_PORT'];
123            if(($_SERVER["SERVER_PORT"]=="80" && !$https ) || ($_SERVER["SERVER_PORT"]=="443" && $https)){
124                ;
125
126            }else{
127                $url .= ":".$_SERVER['SERVER_PORT'];
128            }
129        }
130
131        // We have now arrived at base_url as defined
132        $this->base_url=$url;
133
134        // 3. Next we need a base_part of our URL. There are many different
135        // variables and approaches we tried it, REDIRECT_URL_ROOT, REDIRECT_URL,
136        // etc, however most reliable is $this->unix_dirname(SCRIPT_NAME)
137        $path=$this->unix_dirname($_SERVER['SCRIPT_NAME']);
138        if(substr($path,-1)!='/')$path.='/';
139
140        // We have now arrived at base_path as defined
141        $this->base_path=$path;
142
143        // 4. We now look at RequestURI and extract base_path from the beggining
144        if(isset($_GET['page'])){
145            $page=$_GET['page'];
146        }else{
147            $request_uri=$this->getRequestURI();
148            if(strpos($request_uri,$path)!==0){
149                throw $this->exception("URL matching problem")
150                    ->addMoreInfo('RequestURI',$request_uri)
151                    ->addMoreInfo('BasePath',$path);
152            }
153            $page=substr($request_uri,strlen($path));
154            if(!$page)$page='index';
155
156            // Remove postfix from page if any
157            $page=preg_replace('/\..*$/','',$page);
158            $page=preg_replace('/\/$/','',$page);
159            $page=str_replace('/','_',$page);
160
161            if(substr($page,-1)=='_')$page=substr($page,0,-1);
162        }
163
164        if(strpos($page,'.')!==false)throw $this->exception('Page may not contain periods (.)')
165            ->addMoreInfo('page',$page);
166
167        // We have now arrived at the page as per specification.
168        $this->page=str_replace('/','_',$page);
169
170        $this->template_filename=$this->page;
171        if(substr($this->template_filename,-1)=='/')$this->template_filename.="index";
172
173        $this->api->pathfinder->base_location->setBaseURL($this->base_path);
174
175        $this->debug("base_path=".$this->base_path);
176        $this->debug("base_directory=".$this->base_directory);
177        $this->debug("page=".$this->page);
178        $this->debug("api/page=".$this->api->page);
179        $this->debug("template_filename=".$this->template_filename);
180    }
181    function getRequestURI(){
182        // WARNING. This function URI excludes query string
183
184        if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // IIS
185            $request_uri = $_SERVER['HTTP_X_REWRITE_URL'];
186        } elseif (isset($_SERVER['REQUEST_URI'])) { // Apache
187            $request_uri = $_SERVER['REQUEST_URI'];
188        } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
189            $request_uri = $_SERVER['ORIG_PATH_INFO'];
190            // This one comes without QUERRY string
191        } else {
192            throw new BaseException('Unable to determine RequestURI. This shouldn\'t be called at all in CLI');
193        }
194        $request_uri=explode('?',$request_uri,2);
195        return $request_uri[0];
196    }
197
198    function unix_dirname($path){
199        $chunks=explode('/',$path);
200        array_pop($chunks);
201        if(!$chunks)return '/';
202        return implode('/',$chunks);
203    }
204
205
206
207    function getUrlRoot(){
208        if($r=='')$r='/';
209        return $r;
210    }
211    /* @obsolete since 4.2.2
212    function getDestinationURL($page){
213        if($page[0]=='/'){
214            // Location absolute
215            return $this->base_path.substr($page,1).'.html';
216        }
217        return $page.'.html';
218    }
219    */
220}