mahi /atk4/lib/PageManager.php

Language PHP Lines 221
MD5 Hash 0afeb7c1d01c781be7ada6805168c085
Repository https://github.com/mahimarathore/mahi.git View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php // vim:ts=4:sw=4:et:fdm=marker
/*
 * Undocumented
 *
 * @link http://agiletoolkit.org/
*//*
==ATK4===================================================
   This file is part of Agile Toolkit 4
    http://agiletoolkit.org/

   (c) 2008-2013 Agile Toolkit Limited <info@agiletoolkit.org>
   Distributed under Affero General Public License v3 and
   commercial license.

   See LICENSE or LICENSE_COM for more information
 =====================================================ATK4=*/
class PageManager extends AbstractController {
    /*
     * This is a generic page manager. For web applications it calculates
     * base URI, sets up path manager with the URI locations, determines
     * which page was requested
     *
     * This class works with PathFinder, ApiWeb, and Location.
     */


    // you can access variabless below through $this->api->pm->base_url
    // concatinate them to get full URL

    public $base_url;           // http://yoursite.com:81
    /*
       Base URL defines the absolute destination of our server. Because some
       other resources may be located outside of our Base Path, we need to
       know a Base URL.

       For CLI scripts, you need to set this manually. Also if you are
       going to use URLs in emails, you should use this.

       See also: URL::useAbsoluteURL();
     */

    public $base_path;          // /admin/
    /*
       Base PATH points to the top location of our project. Basically it's
       where the project is installed in the webroot. This is determined
       by thelocation of catch-all file. It is determined by SCRIPT_NAME
       which should be supported by most web installations. It will also
       work when mod_rewrite is not used.

       You can use $base_path in your script to put it say on a logo link

       Also - some other parts of the library may have a different path,
       for example base_path could be = /admin/, and atk4_path could be /amodules/

       If project is installed in web-root, then $base_path will be "/"

       path always starts and ends with slash
     */

    public $page;               // user/add
    /*
       This is a third and a final part of the URLs. This points to a page
       which were reuqested. You can pass path to getDestinationURL() function,
       as a first argument. Also $path is used to determine which page class
       to load.

       Page must never start with slash. Also if path is empty, then
       the "index" is used automatically.


     */

    public $base_directory;     // /home/web/admin/ - physical path
    public $template_filename;

    function init(){
        parent::init();
        $this->page=&$this->api->page;      // link both variables

        $this->api->pm=$this;
        // Firstly, the original URL is retrieved. This function should
        // take care of all possible rewrite engines and bring up a real
        // URL which matches the one in the browser. Also e will need to
        // determine a relative path for the requested page
        $this->parseRequestedURL();

        // This function will continue initialization of the page itself.
        $this->calculatePageName();
    }

    function calculatePageName(){
        // Now. We need to decide what will be the main page to
        // to display the whole thing. This page will contain a
        // main template and will be responsible for rendering the
        // whole page. This function will initialize the object and
        // return it

        // Lastly we need a sub-class, which would worry only about
        // the requested page. This is what we call - a page.
    }

    function parseRequestedURL(){
        $this->base_path=$this->unix_dirname($_SERVER['SCRIPT_NAME']);
        // for windows
        if(substr($this->base_path,-1)=='\\')$this->base_path=substr($this->base_path,1,-1).'/';
        if(substr($this->base_path,-1)!='/')$this->base_path.='/';

        // We are assuming that all requests are being redirected though a single file
        $this->base_directory=$this->unix_dirname($_SERVER['SCRIPT_FILENAME']).'/';

        // This is the re-constructions of teh proper URL.
        // 1. Schema
        $url=$this->api->getConfig('atk/base_url',null);
        if(is_null($url)){
            // Detect it
            $url = 'http';
            $https = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || $_SERVER['SERVER_PORT']==443;
            if($https)$url.='s';

            // 2. Continue building. We are adding hostname next and port.
            $url .= "://".$_SERVER["SERVER_NAME"];
            //if($_SERVER["SERVER_PORT"]!="80")$url .= ":".$_SERVER['SERVER_PORT'];
            if(($_SERVER["SERVER_PORT"]=="80" && !$https ) || ($_SERVER["SERVER_PORT"]=="443" && $https)){
                ;

            }else{
                $url .= ":".$_SERVER['SERVER_PORT'];
            }
        }

        // We have now arrived at base_url as defined
        $this->base_url=$url;

        // 3. Next we need a base_part of our URL. There are many different
        // variables and approaches we tried it, REDIRECT_URL_ROOT, REDIRECT_URL,
        // etc, however most reliable is $this->unix_dirname(SCRIPT_NAME)
        $path=$this->unix_dirname($_SERVER['SCRIPT_NAME']);
        if(substr($path,-1)!='/')$path.='/';

        // We have now arrived at base_path as defined
        $this->base_path=$path;

        // 4. We now look at RequestURI and extract base_path from the beggining
        if(isset($_GET['page'])){
            $page=$_GET['page'];
        }else{
            $request_uri=$this->getRequestURI();
            if(strpos($request_uri,$path)!==0){
                throw $this->exception("URL matching problem")
                    ->addMoreInfo('RequestURI',$request_uri)
                    ->addMoreInfo('BasePath',$path);
            }
            $page=substr($request_uri,strlen($path));
            if(!$page)$page='index';

            // Remove postfix from page if any
            $page=preg_replace('/\..*$/','',$page);
            $page=preg_replace('/\/$/','',$page);
            $page=str_replace('/','_',$page);

            if(substr($page,-1)=='_')$page=substr($page,0,-1);
        }

        if(strpos($page,'.')!==false)throw $this->exception('Page may not contain periods (.)')
            ->addMoreInfo('page',$page);

        // We have now arrived at the page as per specification.
        $this->page=str_replace('/','_',$page);

        $this->template_filename=$this->page;
        if(substr($this->template_filename,-1)=='/')$this->template_filename.="index";

        $this->api->pathfinder->base_location->setBaseURL($this->base_path);

        $this->debug("base_path=".$this->base_path);
        $this->debug("base_directory=".$this->base_directory);
        $this->debug("page=".$this->page);
        $this->debug("api/page=".$this->api->page);
        $this->debug("template_filename=".$this->template_filename);
    }
    function getRequestURI(){
        // WARNING. This function URI excludes query string

        if (isset($_SERVER['HTTP_X_REWRITE_URL'])) { // IIS
            $request_uri = $_SERVER['HTTP_X_REWRITE_URL'];
        } elseif (isset($_SERVER['REQUEST_URI'])) { // Apache
            $request_uri = $_SERVER['REQUEST_URI'];
        } elseif (isset($_SERVER['ORIG_PATH_INFO'])) { // IIS 5.0, PHP as CGI
            $request_uri = $_SERVER['ORIG_PATH_INFO'];
            // This one comes without QUERRY string
        } else {
            throw new BaseException('Unable to determine RequestURI. This shouldn\'t be called at all in CLI');
        }
        $request_uri=explode('?',$request_uri,2);
        return $request_uri[0];
    }

    function unix_dirname($path){
        $chunks=explode('/',$path);
        array_pop($chunks);
        if(!$chunks)return '/';
        return implode('/',$chunks);
    }



    function getUrlRoot(){
        if($r=='')$r='/';
        return $r;
    }
    /* @obsolete since 4.2.2
    function getDestinationURL($page){
        if($page[0]=='/'){
            // Location absolute
            return $this->base_path.substr($page,1).'.html';
        }
        return $page.'.html';
    }
    */
}
Back to Top