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

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

Bug gefixt: Unter Windows wurde Sprache nicht mehr richtig erkannt (Grund: t29Host-Webroot-Abstrahierungsschicht, die nicht unter Windows getestet wurde).

Damit wird u.a. auch das englischsprachige Menü wieder richtig eingebunden.

File size: 9.1 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               
161                // Windows realpath() converts Unix Paths ($_SERVER) to Windows Paths (like \en\index.php).
162                // We want to use unix paths ($_SERVER like) internally, so do this dummy conversion back, if neccessary
163                $this->script_filename = str_replace('\\', '/', $this->script_filename);
164               
165                //phpinfo(); exit;
166        }
167       
168        function check_url_rewrite() {
169                if($this->is_rewriting_host) {
170                        $path = $_SERVER['REQUEST_URI'];
171                        $newpath = $this->rewrite_link($path);
172                        if($path != $newpath) {
173                                header('HTTP/1.1 301 Moved Permanently');
174                                header('Location: '.$newpath);
175                                return $newpath;
176                        }
177                }
178                return null;
179        }
180
181        public function __toString() {
182                return 't29v6/'.$this->hostname;
183        }
184       
185        /**
186         * Rewrite Links so they match for this host.
187         * This method acts like a pipeline:
188         *  $new_link = rewrite_link($old_link);
189         * It can perform two conversions:
190         *
191         *   1. Rewriting/Clean URL system: Will strip file suffixes, if appropriate.
192         *      This will be done whenever this is a rewriting host and this is the
193         *      main purpose for this function.
194         *
195         *   2. Prefixing the correct web prefix. This is *only* be done when
196         *      $also_rewrite_prefix = true. The reaseon is that prefix rewriting is
197         *      generally done by a global page rewrite after generation of the whole
198         *      page on a whole-page-level. This is less error prone.
199         *      Anyway you can use this function if you think you need. blubblubb
200         *
201         *
202         **/
203        function rewrite_link($link_target, $also_rewrite_prefix=false) {
204                // rewrite link if neccessary. This function will be called hundreds of times
205                // while rendering a page, rewriting all links found.
206               
207                // pending: prefix setzen.
208                if($this->has_web_prefix && $also_rewrite_prefix) {
209                        $link_target = $this->web_prefix . $link_target;
210                }
211               
212                if($this->is_rewriting_host) {
213                        $new_target = preg_replace('/\.(?:php|shtml?)([#?].+)?$/i', '\\1', $link_target);
214                        return $new_target;
215                } else {
216                        // just the identity function
217                        return $link_target;
218                }
219               
220        }
221       
222        function get_shorthand_link_returner() {
223                $t = $this;
224                return function($link_target)use($t) { return $t->rewrite_link($link_target); };
225        }
226
227        abstract function fillup_template_conf(&$template_conf);
228}
229
230class t29PublicHost extends t29Host {
231        /**
232         * This is actually the default public host which should be loaded
233         * at www.technikum29.de.
234         **/
235        public $hostname = "public";
236        function fillup_template_conf(&$template_conf) {}
237}
238
239/**
240 * Host auf heriberts Rechner; dort wird ein weiterer Metatag mit id eingefuehrt,
241 * mit dem seine Firefox Editthispage-Extension die Seite bearbeiten kann.
242 **/
243class t29HeribertHost extends t29Host {
244        public $hostname = "heribert";
245
246        function fillup_template_conf(&$template_conf) {
247                $template_conf['header_prepend'][] = 
248                        '<meta name="t29.localfile" content="'.$_SERVER['SCRIPT_FILENAME'].'" id="localFileSource">';
249        }
250}
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