Changeset 357 in t29-www


Ignore:
Timestamp:
Mar 8, 2013, 5:04:14 PM (6 years ago)
Author:
sven
Message:

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

Files:
1 added
10 edited

Legend:

Unmodified
Added
Removed
  • index.php

    r288 r357  
    55 $de = strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], 'de');
    66 $en = strpos($_SERVER['HTTP_ACCEPT_LANGUAGE'], 'en');
    7 
     7 
     8 // Support web prefixes, aka installation of the homepage in
     9 // subdirectorys and not in virtual hosts
     10 // Basically relative redirects would do the trick, too,
     11 // but officially they are not allowed (not standard compilant)
     12 include "lib/host.php";
     13 $host = t29Host::detect();
     14 // this is not implemented in the HTML below, since actually
     15 // there is no browser who see's that HTML.
     16 
    817 if( ($en !== false && $de !== false && $de < $en) ||
    918     ($en === false && de !== false)              )
    10         header("Location: /de/");
     19        header("Location: {$host->web_prefix}/de/");
    1120 else
    12         header("Location: /en/");
     21        header("Location: {$host->web_prefix}/en/");
    1322?>
    1423<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  • lib/cache.php

    r297 r357  
    178178         * otherwise it just calls ob_start() and that's it.
    179179         *
     180         * TODO FIXME Doku outdated for this method --
     181         *
    180182         * $register_shutdown_func can be:
    181183         *   - Just 'true': Then it will tell t29Cache to register it's
     
    196198         *     the stuff to the cache file.
    197199         **/
    198         function start_cache($register_shutdown_func=null, $shutdown_func_is_filter=false) {
     200        //function start_cache($register_shutdown_func=null, $shutdown_func_is_filter=false) {
     201        function start_cache(array $args) {
     202                $defaults = array(
     203                        'shutdown_func' => null,
     204                        'filter_func'   => null,
     205                        'write_cache'   => true,
     206                );
     207                $args = array_merge($defaults, $args);
     208               
    199209                if($this->debug)
    200                         print "Will start caching with shutdown: " . $register_shutdown_func . PHP_EOL;
     210                        print "Will start caching with shutdown: " . $args['shutdown_func'] . PHP_EOL;
    201211                       
    202212                // check if output file is writable; for logging and logging output
     
    209219                ob_start();
    210220
    211                 if(is_callable($register_shutdown_func)) {
    212                         // callback given: Register a shutdown function
     221                if($args['shutdown_func'] || $args['filter_func']) {
     222                        // callback/filter given: Register a shutdown function
    213223                        // which will call user's callback at first, then
    214                         // our own write function
     224                        // our own write function. and which handles filters
    215225                        $t = $this; // PHP stupidity
    216                         register_shutdown_function(function()
    217                           use($register_shutdown_func, $shutdown_func_is_filter, $t) {
    218                                 if($shutdown_func_is_filter) {
     226                        register_shutdown_function(function()  use($args, $t) {
     227                                if($args['filter_func']) {
     228                                        // also collect the shutdown func prints in the $content
     229                                        if($args['shutdown_func'])
     230                                                call_user_func($args['shutdown_func']);
     231
    219232                                        $content = ob_get_clean();
    220233                                        if($t->debug)
    221234                                                // can print output since OutputBuffering is finished
    222235                                                print 't29Cache: Applying user filter to output' . PHP_EOL;
    223                                         $content = call_user_func($register_shutdown_func, $content);
     236                                        $content = call_user_func($args['filter_func'], $content);
    224237                                        print $content;
    225                                         $t->write_cache($content);
    226                                 } else {
    227                                         call_user_func($register_shutdown_func);
     238                                               
     239                                        if($args['write_cache'])
     240                                                $t->write_cache($content);
     241                                        return;
     242                                } else if($args['shutdown_func'])
     243                                        call_user_func($args['shutdown_func']);
     244                                if($args['write_cache'])
    228245                                        $t->write_cache();
    229                                 }
    230246                        });
    231                 } elseif($register_shutdown_func) {
    232                         // only boolean value given: Just register our
    233                         // own write function
     247                } elseif($args['write_cache']) {
     248                        // Just register our own write function
    234249                        register_shutdown_function(array($this, 'write_cache'));
    235250                } else {
    236251                        // nothing given: Dont call our write function,
    237                         // it will be called by hand.
     252                        // it must therefore be called by hand.
    238253                }
    239254        }
     
    278293       
    279294        private function print_error($string, $even_if_nonverbose=false) {
     295                require_once dirname(__FILE__).'/logging.php';
     296                $log = t29Log::get();
     297               
    280298                if($this->verbose || $even_if_nonverbose)
    281                         echo "<div class='error t29cache'>t29Cache: $string</div>\n";
     299                        $log->WARN("t29Cache: ".$string, t29Log::IMMEDIATELY_PRINT);
    282300        }
    283301}
  • lib/host.php

    r347 r357  
    1818        const env_hidesuffix_name = "T29URLHIDESUFFIX";
    1919
    20 
    21         /// Some identifier like a FQDN. Must be overwritten!
     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!
    2223        public $hostname = "undefined";
    23 
     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       
    2452        /**
    2553         * Factory for creating a t29Host instance automatically
     
    6391         * Of course you can always write your own setup() class - it's just your __constructor.
    6492         * 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).
    6597         **/
    6698        function setup() {
    6799                $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;
    68161        }
    69162       
     
    85178        }
    86179       
    87         function rewrite_link($link_target) {
     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) {
    88199                // rewrite link if neccessary. This function will be called hundreds of times
    89200                // 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               
    90207                if($this->is_rewriting_host) {
    91208                        $new_target = preg_replace('/\.(?:php|shtml?)([#?].+)?$/i', '\\1', $link_target);
  • lib/loader.php

    r354 r357  
    6363require "$lib/ressourceloader.php";
    6464$loader = new $class($conf);
    65 if(!$debug) $js_cache->start_cache(array($loader, 'compression_filter'), true);
     65// TODO: there is a known bug with the filter function issue: Since t29Host variable web prefixes the
     66//       filter function for CSS also does path rewriting which doesn't work when it is in debug mode.
     67//       Anyway a page in debug mode also imports all untouched CSS files, anyway
     68if(!$debug)
     69        $js_cache->start_cache(array(
     70                'shutdown_func' => null, // nothing to do afterwards for the cache
     71                'filter_func'   => array($loader, 'compression_filter'), // compress if not debugging
     72                'write_cache'   => true, // write the cache if not debugging
     73        ));
    6674$loader->run();
  • lib/logging.php

    r347 r357  
    99 * Inspired by Klooger for PHP: https://github.com/katzgrau/KLogger/
    1010 **/
    11 
    1211
    1312class t29Log {
     
    2726     */
    2827    const NO_ARGUMENTS = 't29Log::NO_ARGUMENTS';
     28   
     29    /**
     30     * A magic argument which immediately forces the print_all()
     31     * function to be called. This is good when the log method is called while
     32     * php_shutdown is already in progress, otherwise your log lines will end
     33     * in nirvana.
     34     **/
     35    const IMMEDIATELY_PRINT = 't29Log::IMMEDIATELY_PRINT';
    2936        /*
    3037                log array format:
     
    5764       
    5865        function log($line, $severity, $args = self::NO_ARGUMENTS) {
     66                if($args === self::IMMEDIATELY_PRINT) {
     67                        // to be used when in shutdown already
     68                        $this->entries[] = array($severity, $line);
     69                        $this->print_all('immediately final');
     70                        return;
     71                }
    5972                if($args !== self::NO_ARGUMENTS)
    6073                        $line .= '; '. var_export($args, true);
     
    91104                // if $ul_classes is given, will print a list around
    92105                if($ul_classes)
    93                         print "<ul class='$ul_classes'>";
     106                        print "<ul class='messages footer $ul_classes'>";
    94107                foreach($this->entries as $entry) {
    95108                        printf('<li class="%s">%s</li>'.PHP_EOL, $entry[0], $entry[1]);
     
    104117        public function DEBUG($line, $args = self::NO_ARGUMENTS) { $this->log($line, self::DEBUG, $args); }
    105118        public function WARN($line, $args = self::NO_ARGUMENTS) { $this->log($line, self::WARN, $args); }
    106        
    107119} // class
  • lib/ressourceloader.php

    r301 r357  
    3232class t29RessourceLoader {
    3333        /**
    34          * expects: type, cache_file, module_dir, page_dir, glob_pattern, content_types, class, modules, debug
     34         * expects: type, cache_file, module_dir, page_dir, glob_pattern, content_types, class, modules, debug, host
    3535         **/
    3636        public $conf;
     
    7171        }
    7272       
     73        /**
     74         * Return a list of URLs appropriate for being included in a website. In general this
     75         * should be a list with one element, like array("/lib/loader.php?type=js"), which can
     76         * be directly expanded to a <script src="$1"></script> tag. Same applies for CSS.
     77         * In debug mode, the list will contain all base files.
     78         *
     79         * The URLs are relative to the t29 web document root, that is, no host specific web prefix
     80         * handling here.
     81         *
     82         * There is especially an issue with web prefixes and debug mode: Since clean untouched CSS/JS
     83         * files are passed there, there cannot be any rewriting in progress.
     84         *
     85         * @returns array
     86         **/
    7387        function get_urls($debug=null) {
    7488                global $webroot;
     
    228242        function compression_filter($code) {
    229243                global $lib;
     244                require "$lib/host.php";
     245                $host = t29Host::detect();
     246                if($host->has_web_prefix)
     247                        // rewrite CSS image includes
     248                        $code = preg_replace('#(url\(["\']?)/#i', '\\1'.$host->web_prefix.'/', $code);
     249               
     250               
    230251                require "$lib/CSSMin.php";
    231252                # compression: 40kb to 16kb
  • lib/technikum29.php

    r354 r357  
    2525$host = t29Host::detect();
    2626
    27 $file = substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])); # e.g.: "/de/page.php"
     27$file = $host->script_filename; # e.g.: "/de/page.php"
    2828# Bug when DOCUMENT_ROOT ends with trailing slash: make sure $file begins with /:
    2929if($file{0} != '/') $file = "/$file";
  • lib/template.php

    r350 r357  
    105105                foreach($javascript_transfer as $key)
    106106                        $this->javascript_config[$key] = $this->conf[$key];
     107                // also collect data from other classes, e.g. t29Host:
     108                $this->javascript_config['web_prefix'] = $this->conf['host']->web_prefix;
    107109               
    108110                // get all kind of relations. Pages can afterwards be overwritten with t29Template
     
    176178         **/
    177179        function create_cache($cache_object) {
    178                 $cache_object->start_cache(array($this, 'print_footer'));
     180                $cache_object->start_cache(array(
     181                        'shutdown_func' => array($this, 'print_footer'), // print the footer with it
     182                        'filter_func'   => $this->conf['host']->has_web_prefix ?
     183                                array($this, 'rewrite_page_prefix_links') : null, // Entrypoint for URL and content rewriting!
     184                ));
     185                // directly start header printing
    179186                $this->print_header();
    180187        }
     
    184191         **/
    185192        function create_separate_caches($header_cache, $footer_cache) {
    186                 $header_cache->start_cache();
     193                $header_cache->start_cache(array(
     194                         // start with no shutdown, filter, nor writing
     195                        'filter_func' => $this->conf['host']->has_web_prefix ? array($this, 'rewrite_page_prefix_links') : null,
     196                        'write_cache' => false,
     197                ));
    187198                $this->print_header();
    188199                $header_cache->write_cache(); // will also print out header immediately.
    189200               
    190                 $footer_cache->start_cache();
     201                $footer_cache->start_cache(array(
     202                         // start with no shutdown, filter, nor writing
     203                        'filter_func' => $this->conf['host']->has_web_prefix ? array($this, 'rewrite_page_prefix_links') : null,
     204                        'write_cache' => false,
     205                ));
    191206                $this->print_footer();
    192207                $footer_content = $footer_cache->write_cache(null, true); // don't print footer immediately.
     
    432447                $rl_pagespecific_links = $rl->get_page_specific_urls($this->conf['seiten_id']);
    433448
    434                 foreach(array($rl_links, $rl_pagespecific_links) as $rls)
    435                         foreach($rls as $link)
     449                foreach(array($rl_links, $rl_pagespecific_links) as $rls) {
     450                        foreach($rls as $link) {
     451                                // do the host link renaming conversion. This is more important if
     452                                // there is a web_prefix than for the suffix rewriting.
     453                                //$link = $this->conf['host']->rewrite_link($link, true);
    436454                                printf($template, $link);
     455                        }
     456                }
     457        }
     458       
     459        function rewrite_page_prefix_links($content) {
     460                // called by cache: rewrite the page contents
     461                return preg_replace('#(href|src|action)=("|\')/#i', '\\1=\\2'.$this->conf['host']->web_prefix.'/', $content);
    437462        }
    438463
  • shared/js-v6/modules/lightbox.js

    r277 r357  
    1010
    1111t29.lightbox = {};
     12
     13t29.lightbox.paths = {
     14        'css': "/shared/js/fancybox/jquery.fancybox-1.3.4.css",
     15        'js': "/shared/js/fancybox/jquery.fancybox-1.3.4.pack.js"
     16};
     17
    1218t29.lightbox.setup = function() {
    1319        var elements = $("#content .popup");
    1420        if(elements.length) {
    1521                // we have fancybox elements on this page. Load Javascript and CSS
    16                 $("<style type='text/css'/>").html('@import url("/shared/js/fancybox/jquery.fancybox-1.3.4.css")').appendTo("head");
    17                 $.getScript('/shared/js/fancybox/jquery.fancybox-1.3.4.pack.js', function(){
     22                t29.load.css(t29.lightbox.paths.css);
     23                t29.load.js(t29.lightbox.paths.js, function(){
    1824                        elements.fancybox({
    1925                                onComplete: function() {
  • shared/js-v6/modules/startup.js

    r347 r357  
    1414
    1515t29.startup.modules = [
     16        // infrastructure
     17        'load',
     18
    1619        // independent #content enrichment
    1720        'auto_bildbreite',
Note: See TracChangeset for help on using the changeset viewer.
© 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