source: t29-www/lib/host.php @ 357

Last change on this file since 357 was 357, checked in by sven, 11 years ago

t29Host-Webroot-System entwickelt.

Damit können Installationen der technikum29-Website ab nun auch in Unterverzeichnissen
(bislang allerdings nur unterhalb des DocumentRoots) installiert werden, was einem
größere Flexibilität beim lokalen Aufsetzen der Seite liefert

File size: 8.8 KB
Line 
1<?php
2/**
3 * t29v6 new Hostinfo and Hosthook system.
4 *
5 * Local host.php files in the webroot can hook into t29* php
6 * and js classes without interfering the code. This can be usually
7 * be used for extra svn information.
8 * Hostinfos can also be appended in this file and therefore be
9 * managed centrally.
10 *
11 * webroot/host.php muss have a t29LocalHost extends t29Host class.
12 *
13 **/
14
15abstract class t29Host {
16        const webroot_host_file = '/host.php'; # relative to webroot
17        const webroot_local_host_classname = 't29LocalHost';
18        const env_hidesuffix_name = "T29URLHIDESUFFIX";
19
20        /// $hostname: An identifier like a FQDN. Is only used to identify the t29Host instance and not
21        ///            for constructing any URL.
22        /// This value must be overwritten in child classes!
23        public $hostname = "undefined";
24       
25        /// $document_root := realpath($_SERVER['DOCUMENT_ROOT']), performed by setup().
26        ///                   Can be used to identify the unix file path to the webserver docroot of the
27        ///                   webhost. Independent of the t29 system.
28        public $document_root;
29       
30        /// $webroot: The unix file path to the t29 web installation, actually the parent directory
31        ///           of the lib/ directory. Is widely used by many files and also computed by themselves.
32        public $webroot;
33        /// $lib  := realpath(__FILE__), just for convenience. $lib = "$webroot/lib" always holds.
34        public $lib;
35       
36        /// $web_prefix: The URL path to the webroot. Example:
37        ///              http://example.com/path/to/t29/de/page.php
38        ///                                ^^^^^^^^^^^^
39        ///                            This part is the web prefix, if /de/page.php is the script_filename.
40        /// This value is computed by setup().
41        public $web_prefix = "";
42       
43        /// $has_web_prefix := !empty($web_prefix). If this host is installed in root or not
44        public $has_web_prefix = false;
45       
46        /// $script_filename: The t29-internal identifying url path, like "/de/page.php" or "/en".
47        ///                   While $_SERVER['SCRIPT_FILENAME'] still contains the $web_prefix, this
48        ///                   string is sanitized.
49        /// This value is computed by setup().
50        public $script_filename;
51       
52        /**
53         * Factory for creating a t29Host instance automatically
54         * from the current host. This method will decide which
55         * subclass has to be taken.
56         **/
57        static function detect() {
58                $instance = null;
59
60                // check if local host file exists
61                $hostfile = dirname(__FILE__) . '/../' . self::webroot_host_file;
62                if(file_exists($hostfile)) {
63                        include $hostfile;
64                        if(class_exists(self::webroot_local_host_classname)) {
65                                $x = self::webroot_local_host_classname;
66                                $host = new $x;
67                                $host->setup();
68                                return $host;
69                        } else {
70                                print "Warning: Hostfile $hostfile does not contain class ".self::webroot_local_host_classname."\n";
71                        }
72                }
73               
74                // Quick and Dirty: Load hostname specific instances
75                switch($_SERVER['SERVER_NAME']) {
76                        case 'heribert':
77                        case 'localhost':
78                                $localhost = new t29HeribertHost;
79                                $localhost->setup();
80                                return $localhost;
81                }
82               
83                $publichost = new t29PublicHost;
84                $publichost->setup();
85                return $publichost;
86        }
87       
88        /**
89         * A constructing method which is always called by the t29Host::detect() factory.
90         * It does some general stuff.
91         * Of course you can always write your own setup() class - it's just your __constructor.
92         * The constructor will of course be called before the setup() method.
93         *
94         * This method detects two things:
95         *   1. if this host does Clean URLs (Suffix rewriting)
96         *   2. if this host is *not* installed in its own virtualhost (i.e. on docroot).
97         **/
98        function setup() {
99                $this->is_rewriting_host = isset($_SERVER[self::env_hidesuffix_name]);
100               
101                $this->lib = dirname(__FILE__);
102                $this->webroot = realpath($this->lib . '/../');  # file path to root of t29 web installation
103               
104                /*
105                   calculate the web_prefix. This is kind of a detection.
106                   
107                   Examples for an installation in Document root:
108                      $lib = /var/www/technikum29.de/lib
109                      $webroot = /var/www/technikum29.de
110                      $_SERVER["DOCUMENT_ROOT"] = /var/www/technikum29.de
111                      $this->document_root = /var/www/technikum29.de
112                      $_SERVER["SCRIPT_FILENAME"] = /var/www/technikum29-www/de/index.php
113                      $this->script_filename = /de/index.php
114                      $_SERVER["REQUEST_URI"] = /de
115                      $_SERVER["SCRIPT_NAME"] = /de/index.php
116                      $web_prefix = ""
117                     
118                   Example for an installation in an arbitrary directory below Document Root:
119                     $lib = /var/www/arbitrary/lib
120                     $webroot = /var/www/arbitrary
121                     $_SERVER['DOCUMENT_ROOT'] = /var/www
122                     $this->document_root = /var/www/arbitrary
123                     $_SERVER['SCRIPT_FILENAME'] = /var/www/arbitrary/de/index.php
124                     $this->script_filename = /arbitrary/de/index.php
125                     $_SERVER['REQUEST_URI'] = /arbitrary/de
126                     $_SERVER['SCRIPT_NAME'] = /arbitrary/de/index.php
127                     $web_prefix = "/arbitrary"
128                     
129                   Example for an installation in mod_userdirs homedir out of Docroot:
130                     $lib = /home/sven/public_html/foo/lib
131                     $webroot = /home/sven/public_html/foo
132                     $_SERVER['DOCUMENT_ROOT'] = /var/www   (mind that!)
133                     $this->document_root = /home/sven/public_html/foo
134                     $_SERVER['SCRIPT_FILENAME'] = /~sven/foo/en/index.php
135                     $this->script_filename = /~sven/foo/en/index.php
136                     $_SERVER['REQUEST_URI'] = /~sven/foo/en/
137                     $_SERVER['SCRIPT_NAME'] = /~sven/foo/en/index.php
138                     $web_prefix = "/~sven/foo"
139                */
140
141                // this algorithm is good for detecting paths below the document root.
142                // it is not suitable for paths out of the document root
143                $this->document_root = realpath($_SERVER['DOCUMENT_ROOT']);
144                if($this->webroot == $this->document_root) {
145                        // we are installed in document root
146                        $this->web_prefix = "";
147                } else {
148                        // we are installed in some arbitary directory
149                        $this->web_prefix = substr($this->webroot, strlen($this->document_root));
150                }
151               
152                // TODO: Somehow autodetect paths out of the document root
153               
154                $this->has_web_prefix = !empty($this->web_prefix);
155               
156                //print "Web prefix:<pre>";
157                //var_dump($this); exit;
158                   
159                $this->script_filename = substr(realpath($_SERVER['SCRIPT_FILENAME']), strlen($this->document_root)); # e.g.: "/de/page.php"
160                //phpinfo(); exit;
161        }
162       
163        function check_url_rewrite() {
164                if($this->is_rewriting_host) {
165                        $path = $_SERVER['REQUEST_URI'];
166                        $newpath = $this->rewrite_link($path);
167                        if($path != $newpath) {
168                                header('HTTP/1.1 301 Moved Permanently');
169                                header('Location: '.$newpath);
170                                return $newpath;
171                        }
172                }
173                return null;
174        }
175
176        public function __toString() {
177                return 't29v6/'.$this->hostname;
178        }
179       
180        /**
181         * Rewrite Links so they match for this host.
182         * This method acts like a pipeline:
183         *  $new_link = rewrite_link($old_link);
184         * It can perform two conversions:
185         *
186         *   1. Rewriting/Clean URL system: Will strip file suffixes, if appropriate.
187         *      This will be done whenever this is a rewriting host and this is the
188         *      main purpose for this function.
189         *
190         *   2. Prefixing the correct web prefix. This is *only* be done when
191         *      $also_rewrite_prefix = true. The reaseon is that prefix rewriting is
192         *      generally done by a global page rewrite after generation of the whole
193         *      page on a whole-page-level. This is less error prone.
194         *      Anyway you can use this function if you think you need. blubblubb
195         *
196         *
197         **/
198        function rewrite_link($link_target, $also_rewrite_prefix=false) {
199                // rewrite link if neccessary. This function will be called hundreds of times
200                // while rendering a page, rewriting all links found.
201               
202                // pending: prefix setzen.
203                if($this->has_web_prefix && $also_rewrite_prefix) {
204                        $link_target = $this->web_prefix . $link_target;
205                }
206               
207                if($this->is_rewriting_host) {
208                        $new_target = preg_replace('/\.(?:php|shtml?)([#?].+)?$/i', '\\1', $link_target);
209                        return $new_target;
210                } else {
211                        // just the identity function
212                        return $link_target;
213                }
214               
215        }
216       
217        function get_shorthand_link_returner() {
218                $t = $this;
219                return function($link_target)use($t) { return $t->rewrite_link($link_target); };
220        }
221
222        abstract function fillup_template_conf(&$template_conf);
223}
224
225class t29PublicHost extends t29Host {
226        /**
227         * This is actually the default public host which should be loaded
228         * at www.technikum29.de.
229         **/
230        public $hostname = "public";
231        function fillup_template_conf(&$template_conf) {}
232}
233
234/**
235 * Host auf heriberts Rechner; dort wird ein weiterer Metatag mit id eingefuehrt,
236 * mit dem seine Firefox Editthispage-Extension die Seite bearbeiten kann.
237 **/
238class t29HeribertHost extends t29Host {
239        public $hostname = "heribert";
240
241        function fillup_template_conf(&$template_conf) {
242                $template_conf['header_prepend'][] = 
243                        '<meta name="t29.localfile" content="'.$_SERVER['SCRIPT_FILENAME'].'" id="localFileSource">';
244        }
245}
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License