Permalink
Browse files

Imported code for the Auto HTTP Path module.

  • Loading branch information...
0 parents commit b82afa4bcce359212bf680d7fd77d0616dcd69c8 @mmakaay mmakaay committed Jul 19, 2011
Showing with 252 additions and 0 deletions.
  1. +62 −0 Changelog
  2. +25 −0 README
  3. +152 −0 auto_http_path.php
  4. +13 −0 info.txt
@@ -0,0 +1,62 @@
+Changelog:
+----------
+
+2011-03-30 1.0.7
+
+ - Some more updates to make the path detection more robust.
+
+2011-02-20 1.0.6
+
+ - An attempt to fix some strange issue where the http_path is broken
+ (i.e. empty) for Phorum scripts that are run from cron.
+
+ - Updates to the automatic HTTP path generation code, to accomodate
+ for the use of path info in a URL (this makes this module work for
+ URLs like http://site.com/forum/index.php/a/b/c?xyz)
+
+2008-07-30 1.0.5
+
+ - Added compatibility for the $PHORUM['template_http_path'] variable.
+ That one needs some rewriting too, since it's based on the
+ $PHORUM['http_path']. We do not change this variable if some other
+ mod changed it already. In that case, the files might be coming
+ from some CDN.
+
+2008-01-22 1.0.4
+
+ - Fixed a problem with the stub CSS and JavaScript code that this
+ module registers. Winzip did not unpack the zero byte css.tpl.
+ I changed the code to let the module provide the stub code by
+ means of a function instead. That should work in all cases.
+
+ Thanks to Steve Healy for reporting this issue.
+
+2008-01-19 1.0.3
+
+ - Fixed undefined index warning for the css register hook.
+
+ - The http_path is not generated anymore for the admin interface.
+
+ - Added a formatting fixup hook that will rewrite URLs in subjects
+ and bodies that use the original http_path to the dynamic http_path.
+ This is for example needed for the smileys module, which caches the
+ URLs for the smiley images from the admin using the configured
+ http_path.
+
+ Thanks to Steve Healy for reporting these issues.
+
+2008-01-18 1.0.2
+
+ - The module now makes Phorum use the correct URLs in cached
+ CSS and JavaScript code.
+
+2007-12-08 1.0.1
+
+ - Use common_pre hook instead of common.
+
+ - Strip ending slash for the http_path. That is how Phorum expects it.
+
+2007-11-30 1.0.0
+
+ - Initial release
+
25 README
@@ -0,0 +1,25 @@
+Module : Automatic HTTP Path
+Version : 1.0.7
+Author : Maurice Makaay <maurice@phorum.org>
+
+This module automatically sets the HTTP Path configuration option, based
+on the active URL. This can be useful if for some reason you run one
+Phorum install under various host names and/or directories (e.g. a
+Phorum on a private space intranet server that is also accessible
+through a public URL or a server that has to be accessible on both an
+http:// and https:// URL).
+
+
+Installation:
+-------------
+
+- Unpack the archive;
+
+- Move the directory "auto_http_path" to the directory "mods"
+ within your Phorum installation;
+
+- Login as administrator in Phorum's administrative interface and
+ go to the "Modules" section;
+
+- Enable the module "Automatic HTTP Path".
+
@@ -0,0 +1,152 @@
+<?php
+
+function phorum_mod_auto_http_path_common_pre()
+{
+ global $PHORUM;
+
+ // Do not run this code from the admin interface, otherwise the
+ // HTTP Path setting would act on the generated http_path.
+ if (defined('PHORUM_ADMIN')) return;
+
+ // Keep track of the original http_path setting.
+ $PHORUM["http_path_orig"] = $PHORUM["http_path"];
+
+ // Determine absolute Phorum base URI.
+ if (!empty($_SERVER["SCRIPT_URI"]))
+ {
+ $uri = $_SERVER['SCRIPT_URI'];
+
+ if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !== '')
+ {
+ $p = preg_quote($_SERVER['PATH_INFO'], '!');
+ $uri = preg_replace(
+ '!' . preg_quote($p, '!') . '(?:\?.*)?$!',
+ '', $uri
+ );
+ }
+
+ $PHORUM["http_path"] = preg_match('!/$!', $uri)
+ ? $uri : dirname($uri);
+ }
+ elseif (!empty($_SERVER["HTTP_HOST"]))
+ {
+ // On some systems, the port is also in the HTTP_HOST, so we
+ // need to strip the port if it appears to be in there.
+ if (preg_match('/^(.+):(.+)$/', $_SERVER['HTTP_HOST'], $m)) {
+ $host = $m[1];
+ if (!isset($_SERVER['SERVER_PORT'])) {
+ $_SERVER['SERVER_PORT'] = $m[2];
+ }
+ } else {
+ $host = $_SERVER['HTTP_HOST'];
+ }
+
+ $protocol = (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]!="off")
+ ? "https" : "http";
+
+ $port = ($_SERVER["SERVER_PORT"]!=443 && $_SERVER["SERVER_PORT"]!=80)
+ ? ':'.$_SERVER["SERVER_PORT"] : "";
+
+ $uri = $_SERVER['REQUEST_URI'];
+ if (isset($_SERVER['PATH_INFO']) && $_SERVER['PATH_INFO'] !== '')
+ {
+ $p = preg_quote($_SERVER['PATH_INFO'], '!');
+ $uri = preg_replace(
+ '!' . preg_quote($p, '!') . '(\?.*)?$!',
+ '', $uri
+ );
+ }
+
+ $dir = dirname($uri);
+
+ $PHORUM["http_path"] = $protocol.'://'.$host.$port.$dir;
+ }
+
+ $PHORUM['http_path'] = preg_replace('!/$!', '', $PHORUM['http_path']);
+
+ // Change template_http_path that one depends on the original http_path.
+ if (isset($PHORUM['template_http_path'])) {
+ $origq = preg_quote(preg_replace(
+ '!/+$!', '', $PHORUM['http_path_orig']), '/'
+ );
+ $PHORUM['template_http_path'] = preg_replace(
+ "/^$origq/", $PHORUM['http_path'],
+ $PHORUM['template_http_path']
+ );
+ }
+}
+
+function mod_auto_http_path_empty_javascript()
+{
+ return "// Empty stub javascript for the auto_http_path module.";
+}
+
+function phorum_mod_auto_http_path_javascript_register($data)
+{
+ // JavaScript for this module. This module does not add actual
+ // code, but we need to add this to force Phorum into using
+ // different cached files per generated http_path.
+ $data[] = array(
+ 'module' => 'auto_http_path',
+ 'source' => 'function(mod_auto_http_path_empty_javascript)',
+ 'cache_key' => $GLOBALS['PHORUM']['http_path']
+ );
+
+ return $data;
+}
+
+function mod_auto_http_path_empty_css()
+{
+ return "/* Empty stub CSS for the auto_http_path module. */";
+}
+
+function phorum_mod_auto_http_path_css_register($data)
+{
+ // CSS for this module. This module does not add actual
+ // code, but we need to add this to force Phorum into using
+ // different cached files per generated http_path.
+ $data['register'][] = array(
+ 'module' => 'auto_http_path',
+ 'source' => 'function(mod_auto_http_path_empty_css)',
+ 'where' => 'after',
+ 'cache_key' => $GLOBALS['PHORUM']['http_path']
+ );
+
+ return $data;
+}
+
+// Fix http paths for data that doesn't follow the http_path setting
+// dynamically (e.g. the smileys module, which caches full image URLs
+// using the standard http_path setting).
+function phorum_mod_auto_http_path_format_fixup($messages)
+{
+ global $PHORUM;
+
+ // Do not run this code from the admin interface, otherwise the
+ // HTTP Path setting would act on the generated http_path.
+ if (defined('PHORUM_ADMIN')) return $messages;
+
+ $orig_http_path = preg_replace('!/+$!', '', $PHORUM['http_path_orig']);
+
+ foreach ($messages as $id => $message)
+ {
+ if (isset($message['subject'])) {
+ $messages[$id]['subject'] = str_replace(
+ $orig_http_path . '/',
+ $PHORUM['http_path'] . '/',
+ $message['subject']
+ );
+ }
+ if (isset($message['body'])) {
+ $messages[$id]['body'] = str_replace(
+ $orig_http_path . '/',
+ $PHORUM['http_path'] . '/',
+ $message['body']
+ );
+ }
+ }
+
+ return $messages;
+}
+
+?>
@@ -0,0 +1,13 @@
+title: Automatic HTTP Path
+desc: This module automatically sets the HTTP Path configuration option, based on the active URL. This can be useful if for some reason you run one Phorum install under various host names and/or directories (e.g. a Phorum on a private space intranet server that is also accessible through a public URL or a server that has to be accessible on both an http:// and https:// URL).
+version: 1.0.7
+url: http://www.phorum.org/phorum5/read.php?62,125914,125914#msg-125914
+category: admin
+
+hook: common_pre|phorum_mod_auto_http_path_common_pre
+hook: javascript_register|phorum_mod_auto_http_path_javascript_register
+hook: css_register|phorum_mod_auto_http_path_css_register
+hook: format_fixup|phorum_mod_auto_http_path_format_fixup
+
+priority: run hook common_pre before *
+priority: run hook format_fixup after *

0 comments on commit b82afa4

Please sign in to comment.