Merge remote-tracking branch 'origin/master'
authorChristian Weiske <cweiske@cweiske.de>
Fri, 4 Jul 2014 05:37:06 +0000 (07:37 +0200)
committerChristian Weiske <cweiske@cweiske.de>
Fri, 4 Jul 2014 05:37:06 +0000 (07:37 +0200)
Conflicts:
src/phorkie/Renderer/Geshi.php
src/phorkie/SetupCheck.php

.gitignore
data/config.default.php
scripts/build-rewritemap.php [new file with mode: 0644]
src/phorkie/Renderer/Geshi.php
src/phorkie/SetupCheck.php
src/phorkie/Tools.php
src/phorkie/autoload.php
src/stub-phar.php
www/www-header.php

index 53196eceae061c8ddeed4110fff172f34a26862d..a195a9e1c1ff0140dfe45ba1a0dc0f4992d28494 100644 (file)
@@ -4,5 +4,6 @@
 /build.properties
 /dist
 /lib/
+/src/gen-rewritemap.php
 /www/*.phar
 /www/*.phar.bz2
index 2beb3549568693a9d4634cef92af31ea9c163074..2e588fadc39fb8a93a68bfade175f5285d899e6d 100644 (file)
@@ -1,10 +1,18 @@
 <?php
+$pharFile = \Phar::running();
+if ($pharFile == '') {
+    $phorkieDir = __DIR__ . '/../';
+} else {
+    //remove phar:// from the path
+    $phorkieDir = dirname(substr($pharFile, 7)) . '/';
+}
+
 $GLOBALS['phorkie']['cfg'] = array(
     'debug'         => false,
-    'gitdir'        => __DIR__ . '/../repos/git/',
-    'workdir'       => __DIR__ . '/../repos/work/',
+    'gitdir'        => $phorkieDir . 'repos/git/',
+    'workdir'       => $phorkieDir . 'repos/work/',
     'tpl'           => __DIR__ . '/templates/',
-    'baseurl'       => '/',
+    'baseurl'       => null,
     'avatars'       => true,
     'css'           => '',
     'iconpng'       => '',//phorkie browser icon (favicon)
diff --git a/scripts/build-rewritemap.php b/scripts/build-rewritemap.php
new file mode 100644 (file)
index 0000000..354af7f
--- /dev/null
@@ -0,0 +1,21 @@
+<?php
+/**
+ * Convert the .htaccess rewrite rules into an array of pattern-replacement
+ * pairs.
+ * Writes src/gen-rewritemap.php
+ */
+$lines    = file(__DIR__ . '/../www/.htaccess');
+$patterns = array();
+foreach ($lines as $line) {
+    if (substr($line, 0, 11) == 'RewriteRule') {
+        list($n, $pattern, $replace) = explode(' ', rtrim($line));
+        $patterns['#' . $pattern . '#'] = $replace;
+    }
+}
+file_put_contents(
+    __DIR__ . '/../src/gen-rewritemap.php',
+    "<?php\n/* automatically created from www/.htaccess */\nreturn "
+    . var_export($patterns, true)
+    . ";\n?>\n"
+);
+?>
index 9cb15a20d106da7ae27f3302ac1164ae3502fa39..0c545ed5dff3e9d674959eb9e2f07566a991a99c 100644 (file)
@@ -13,10 +13,14 @@ class Renderer_Geshi
      */
     public function toHtml(File $file, Tool_Result $res = null)
     {
-        if (!class_exists('\\GeSHi', true)) {
-            require_once 'geshi.php';
+        /**
+         * Yes, geshi needs to be in your include path
+         * We use the geshi pear package.
+         */
+        if (!class_exists('\\geshi', true)) {
+            require_once $GLOBALS['phorkie']['cfg']['geshi'];
         }
-        $geshi = new \GeSHi($file->getContent(), $this->getType($file));
+        $geshi = new \geshi($file->getContent(), $this->getType($file));
         $geshi->enable_line_numbers(GESHI_NORMAL_LINE_NUMBERS);
         $geshi->set_header_type(GESHI_HEADER_DIV);
 
index fbc99c7eae85ad0f262f31578e1435157971c34c..177a0f75d8c98ffd32b9b328de0807671a3ca134 100644 (file)
@@ -46,10 +46,12 @@ class SetupCheck
             }
         }
 
-        if (!class_exists('GeSHi', true)) {
-            @include_once 'geshi.php';
-            if (!class_exists('GeSHi', false)) {
-                $this->fail('PEAR package not installed: pear.geshi.org/geshi');
+        if (!class_exists('geshi', true)) {
+            $geshi = stream_resolve_include_path(
+                $GLOBALS['phorkie']['cfg']['geshi']
+            );
+            if ($geshi === false) {
+                $this->fail('GeSHi not available');
             }
         }
 
index fc815cc5679085c139028f35fab58ea1531f819b..7c9c46eb84a540709c52cc6932b5329095c68204 100644 (file)
@@ -70,6 +70,31 @@ class Tools
         return $file;
     }
 
-}
 
+    public static function detectBaseUrl()
+    {
+        if (!isset($_SERVER['REQUEST_URI'])
+            || !isset($_SERVER['SCRIPT_NAME'])
+        ) {
+            return '/';
+        }
+
+        $scriptName = $_SERVER['SCRIPT_NAME'];
+        $requestUri = $_SERVER['REQUEST_URI'];
+        if (substr($scriptName, -4) != '.php') {
+            //a phar
+            return $scriptName . '/';
+        }
+
+        if (substr($requestUri, -4) != '.php') {
+            $requestUri .= '.php';
+        }
+        $snl = strlen($scriptName);
+        if (substr($requestUri, -$snl) == $scriptName) {
+            return substr($requestUri, 0, -$snl) . '/';
+        }
+
+        return '/';
+    }
+}
 ?>
index 189cde48a7fc263232e8f64464221db94ddb6b2e..6441a1a71790fc8b7ab13494f701c7e5cb3589fd 100644 (file)
@@ -4,7 +4,14 @@
  *
  * @author Christian Weiske <cweiske@cweiske.de>
  */
-if (file_exists(__DIR__ . '/../../lib/autoload.php')) {
+if (file_exists(__DIR__ . '/../../lib/PEAR.php')) {
+    //phing-installed dependencies available ("phing collectdeps")
+    set_include_path(
+        __DIR__ . '/../'
+        . PATH_SEPARATOR . __DIR__ . '/../../lib/'
+        . PATH_SEPARATOR . '.'
+    );
+} else if (file_exists(__DIR__ . '/../../lib/autoload.php')) {
     //composer-installed dependencies available
     set_include_path(
         __DIR__ . '/../'
index 72e367d17e2a0ceb897e6ab2e1dd8a65563b38a3..ac82a382c1d8baa33220913ae357c86b6f240b22 100644 (file)
@@ -45,10 +45,38 @@ function rewritePath($path)
         return 'www/index.php';
     }
 
-    if (substr($path, -4) == '.css') {
+    $path = rewriteWithHtaccess($path);
+
+    if (substr($path, -4) == '.css' || substr($path, -3) == '.js') {
         header('Expires: ' . date('r', time() + 86400 * 7));
     }
-    return $path;
+    return 'www' . $path;
+}
+
+function rewriteWithHtaccess($path)
+{
+    //remove the trailing slash /
+    $cpath = substr($path, 1);
+    $bFoundMatch = false;
+    $map = include(__DIR__ . '/../src/gen-rewritemap.php');
+    foreach ($map as $pattern => $replace) {
+        if (preg_match($pattern, $cpath, $matches)) {
+            $bFoundMatch = true;
+            break;
+        }
+    }
+    if (!$bFoundMatch) {
+        return $path;
+    }
+    $newcpath = preg_replace($pattern, $replace, $cpath);
+    if (strpos($newcpath, '?') === false) {
+        return '/' . $newcpath;
+    }
+    list($cfile, $getParams) = explode('?', $newcpath, 2);
+    if ($getParams != '') {
+        parse_str($getParams, $_GET);
+    }
+    return '/' . $cfile;
 }
 
 //Phar::interceptFileFuncs();
index 56c7b8ed94461fb3d8169d537be35ac2bb54d4cf..3fa4e0e7ba6e05b38d10cf9ffb4d41ff5ffdb525 100644 (file)
@@ -32,8 +32,21 @@ set_exception_handler(
 );
 
 require_once __DIR__ . '/../data/config.default.php';
-if (file_exists(__DIR__ . '/../data/config.php')) {
-    require_once __DIR__ . '/../data/config.php';
+$pharFile = \Phar::running();
+if ($pharFile == '') {
+    $cfgFilePath = __DIR__ . '/../data/config.php';
+} else {
+    //remove phar:// from the path
+    $cfgFilePath = substr($pharFile, 7) . '.config.php';
+}
+$GLOBALS['phorkie']['cfgfiles'][$cfgFilePath] = false;
+if (file_exists($cfgFilePath)) {
+    $GLOBALS['phorkie']['cfgfiles'][$cfgFilePath] = true;
+    require_once $cfgFilePath;
+}
+
+if ($GLOBALS['phorkie']['cfg']['baseurl'] === null) {
+    $GLOBALS['phorkie']['cfg']['baseurl'] = Tools::detectBaseUrl();
 }
 
 // Set/Get git commit session variables