PageRenderTime 56ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/include/phorum_get_url.php

https://bitbucket.org/webop/webop-forum
PHP | 306 lines | 191 code | 54 blank | 61 comment | 49 complexity | 0842998411babb2539a87051b79399fe MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * This function is used for generating all the Phorum related URL's.
  4. *
  5. * Important note for the developers:
  6. * ----------------------------------
  7. * If anything changes in this file, then beware that the Phorum
  8. * PHP Extension code needs to be updated as well. Add a TODO marker
  9. * to the updated pieces of code if this update is not done immediately.
  10. * ----------------------------------
  11. */
  12. /**
  13. * Descriptions of standard Phorum page URL types and their options.
  14. * The keys in this array describe the type of Phorum URL.
  15. * The values are arrays, containing the following three elements:
  16. * - The name of the Phorum page to link to;
  17. * - A constan, telling whether the forum_id has to be added to the URL;
  18. * - A boolean, telling whether the GET vars have to be added to the URL.
  19. */
  20. $PHORUM["url_patterns"] = array(
  21. PHORUM_BASE_URL => array("", PHORUM_URL_NO_FORUM_ID, true),
  22. PHORUM_CHANGES_URL => array("changes", PHORUM_URL_ADD_FORUM_ID, true),
  23. PHORUM_CONTROLCENTER_ACTION_URL => array("control", PHORUM_URL_NO_FORUM_ID, false),
  24. PHORUM_CONTROLCENTER_URL => array("control", PHORUM_URL_ADD_FORUM_ID, true),
  25. PHORUM_CSS_URL => array("css", PHORUM_URL_ADD_FORUM_ID, true),
  26. PHORUM_JAVASCRIPT_URL => array("javascript", PHORUM_URL_ADD_FORUM_ID, true),
  27. PHORUM_FEED_URL => array("feed", PHORUM_URL_NO_FORUM_ID, true),
  28. PHORUM_FOLLOW_ACTION_URL => array("follow", PHORUM_URL_NO_FORUM_ID, false),
  29. PHORUM_FOLLOW_URL => array("follow", PHORUM_URL_ADD_FORUM_ID, true),
  30. PHORUM_INDEX_URL => array("index", PHORUM_URL_NO_FORUM_ID, true),
  31. PHORUM_LIST_URL => array("list", PHORUM_URL_COND_FORUM_ID, true),
  32. PHORUM_LOGIN_ACTION_URL => array("login", PHORUM_URL_NO_FORUM_ID, false),
  33. PHORUM_LOGIN_URL => array("login", PHORUM_URL_ADD_FORUM_ID, true),
  34. PHORUM_MODERATION_ACTION_URL => array("moderation", PHORUM_URL_NO_FORUM_ID, false),
  35. PHORUM_MODERATION_URL => array("moderation", PHORUM_URL_ADD_FORUM_ID, true),
  36. PHORUM_PM_ACTION_URL => array("pm", PHORUM_URL_NO_FORUM_ID, false),
  37. PHORUM_PM_URL => array("pm", PHORUM_URL_ADD_FORUM_ID, true),
  38. PHORUM_POSTING_URL => array("posting", PHORUM_URL_ADD_FORUM_ID, true),
  39. PHORUM_POSTING_ACTION_URL => array("posting", PHORUM_URL_NO_FORUM_ID, false),
  40. PHORUM_PROFILE_URL => array("profile", PHORUM_URL_ADD_FORUM_ID, true),
  41. PHORUM_REDIRECT_URL => array("redirect", PHORUM_URL_NO_FORUM_ID, true),
  42. PHORUM_REGISTER_ACTION_URL => array("register", PHORUM_URL_NO_FORUM_ID, false),
  43. PHORUM_REGISTER_URL => array("register", PHORUM_URL_ADD_FORUM_ID, true),
  44. PHORUM_REPORT_URL => array("report", PHORUM_URL_ADD_FORUM_ID, true),
  45. PHORUM_SEARCH_ACTION_URL => array("search", PHORUM_URL_NO_FORUM_ID, false),
  46. PHORUM_SEARCH_URL => array("search", PHORUM_URL_ADD_FORUM_ID, true),
  47. PHORUM_SUBSCRIBE_URL => array("subscribe", PHORUM_URL_ADD_FORUM_ID, true),
  48. PHORUM_ADDON_URL => array("addon", PHORUM_URL_ADD_FORUM_ID, true),
  49. PHORUM_AJAX_URL => array("ajax", PHORUM_URL_NO_FORUM_ID, false),
  50. );
  51. function phorum_get_url()
  52. {
  53. $PHORUM = $GLOBALS["PHORUM"];
  54. $argv = func_get_args();
  55. $url = "";
  56. $suffix = "";
  57. $pathinfo = NULL;
  58. $add_forum_id = false;
  59. $add_get_vars = true;
  60. $type = array_shift( $argv );
  61. if(!isset($PHORUM["url_patterns"][$type])){
  62. // these URL types need extra care
  63. // please do not add anything to this unless it is a last resort
  64. switch($type){
  65. case PHORUM_READ_URL:
  66. $name = "read";
  67. $add_forum_id = true;
  68. $add_get_vars = true;
  69. if ( !empty( $argv[1] ) &&
  70. (is_numeric( $argv[1] ) || $argv[1] == '%message_id%') )
  71. $suffix = "#msg-$argv[1]";
  72. break;
  73. case PHORUM_REPLY_URL:
  74. if(isset($PHORUM["reply_on_read_page"]) && $PHORUM["reply_on_read_page"]){
  75. $name = "read";
  76. $suffix = "#REPLY";
  77. } else {
  78. $name = "posting";
  79. // For reply on a separate page, we call posting.php on
  80. // its own. In that case argv[0] is the editor mode we
  81. // want to use (reply in this case). Currently, the thread
  82. // id is in argv[0], but we don't need that one for
  83. // posting.php. So we simply replace argv[0] with the
  84. // correct argument.
  85. $argv[0] = "reply";
  86. }
  87. $add_get_vars = true;
  88. $add_forum_id = true;
  89. break;
  90. case PHORUM_FOREIGN_READ_URL:
  91. $name = "read";
  92. $add_forum_id = false;
  93. $add_get_vars = true;
  94. if ( !empty( $argv[2] ) && is_numeric( $argv[2] ) ) $suffix = "#msg-$argv[2]";
  95. break;
  96. case PHORUM_FILE_URL:
  97. $name = "file";
  98. $add_forum_id = true;
  99. // If a filename=... parameter is set, then change that
  100. // parameter to a URL path, unless this feature is not
  101. // enabled in the admin setup.
  102. $unset = array();
  103. if (!empty($PHORUM['file_url_uses_pathinfo']))
  104. {
  105. $file_id = NULL;
  106. $filename = NULL;
  107. $download = '';
  108. foreach ($argv as $id => $arg) {
  109. if (substr($arg, 0, 5) == 'file=') {
  110. $file_id = substr($arg, 5);
  111. // %file_id% is sometimes used for creating URL
  112. // templates, so we should not mangle that one.
  113. if ($file_id != '%file_id%') {
  114. settype($file_id, 'int');
  115. }
  116. $unset[] = $id;
  117. } elseif (substr($arg, 0, 9) == 'filename=') {
  118. $filename = urldecode(substr($arg, 9));
  119. // %file_name% is sometimes used for creating URL
  120. // templates, so we should not mangle that one.
  121. if ($filename != '%file_name%') {
  122. $filename = preg_replace('/[^\w\_\-\.]/', '_', $filename);
  123. $filename = preg_replace('/_+/', '_', $filename);
  124. }
  125. $unset[] = $id;
  126. } elseif (substr($arg, 0, 9) == 'download=') {
  127. $download = 'download/';
  128. $unset[] = $id;
  129. }
  130. }
  131. if ($file_id !== NULL && $filename !== NULL) {
  132. foreach ($unset as $id) unset($argv[$id]);
  133. $add_forum_id = false;
  134. $pathinfo = "/$download{$PHORUM['forum_id']}/$file_id/$filename";
  135. }
  136. }
  137. break;
  138. // this is for adding own generic urls
  139. case PHORUM_CUSTOM_URL:
  140. // first arg is our page
  141. $name = array_shift($argv);
  142. // second arg determines if we should add the forum_id
  143. $add_forum_id = (bool) array_shift($argv);
  144. break;
  145. }
  146. } else {
  147. list($name, $add_forum_id, $add_get_vars) = $PHORUM["url_patterns"][$type];
  148. // add forum id if setting is conditional and there are no params
  149. if($add_forum_id==PHORUM_URL_COND_FORUM_ID && count($argv)==0){
  150. $add_forum_id=PHORUM_URL_ADD_FORUM_ID;
  151. }
  152. }
  153. if(isset($name)){
  154. $query_string = "";
  155. $url = $PHORUM["http_path"]."/";
  156. if($name){
  157. $url.= $name.".".PHORUM_FILE_EXTENSION;
  158. }
  159. if($add_forum_id==PHORUM_URL_ADD_FORUM_ID){
  160. $query_string = $PHORUM["forum_id"].",";
  161. }
  162. if ( count( $argv ) > 0 ) {
  163. $query_string.= implode(",", $argv ).",";
  164. }
  165. if($add_get_vars) {
  166. if ( !empty( $PHORUM["DATA"]["GET_VARS"] ) && $add_get_vars ) {
  167. $query_string.= implode(",", $PHORUM["DATA"]["GET_VARS"] ).",";
  168. }
  169. }
  170. if($query_string){
  171. $query_string = substr($query_string, 0, -1 ); // trim off ending ,
  172. }
  173. if ( function_exists( "phorum_custom_get_url" ) ) {
  174. $query_items = $query_string == ''
  175. ? array() : explode(',', $query_string);
  176. $url = phorum_custom_get_url( $name, $query_items, $suffix, $pathinfo );
  177. } else {
  178. if ($pathinfo !== null) $url .= $pathinfo;
  179. if ($query_string){
  180. $url.= "?" . $query_string;
  181. }
  182. if ( !empty( $suffix ) ) $url .= $suffix;
  183. }
  184. } else {
  185. trigger_error( "Unhandled page type ".$type.".", E_USER_WARNING );
  186. }
  187. return $url;
  188. }
  189. /**
  190. * Generate a Phorum URL, without any URI authentication information in it.
  191. */
  192. function phorum_get_url_no_uri_auth()
  193. {
  194. global $PHORUM;
  195. $uri_auth = NULL;
  196. if (isset($PHORUM['DATA']['GET_VARS'][PHORUM_SESSION_LONG_TERM])) {
  197. $uri_auth = $PHORUM['DATA']['GET_VARS'][PHORUM_SESSION_LONG_TERM];
  198. unset($PHORUM['DATA']['GET_VARS'][PHORUM_SESSION_LONG_TERM]);
  199. }
  200. $argv = func_get_args();
  201. $url = call_user_func_array('phorum_get_url', $argv);
  202. if ($uri_auth !== NULL) {
  203. $PHORUM['DATA']['GET_VARS'][PHORUM_SESSION_LONG_TERM] = $uri_auth;
  204. }
  205. return $url;
  206. }
  207. /**
  208. * Determines the current pages URL
  209. *
  210. * Several places in code we need to produce the current URL for use in
  211. * redirects and use in forms. This function does that to the best of
  212. * our ability
  213. *
  214. * @param boolean $include_query_string
  215. * If true, the query string is appended to the URL.
  216. * If false the query string is left off
  217. *
  218. * @return string
  219. * The current URL
  220. *
  221. */
  222. function phorum_get_current_url($include_query_string=true) {
  223. $url = "";
  224. // On some systems, the SCRIPT_URI is set, but using a different host
  225. // name than the one in HTTP_HOST (probably due to some mass virtual
  226. // hosting request rewriting). If that happens, we do not trust
  227. // the SCRIPT_URI. Otherwise, we use the SCRIPT_URI as the current URL.
  228. if (isset($_SERVER["SCRIPT_URI"]) &&
  229. (!isset($_SERVER['HTTP_HOST']) ||
  230. strpos($_SERVER['SCRIPT_URI'], $_SERVER['HTTP_HOST']) !== FALSE)) {
  231. $url = $_SERVER["SCRIPT_URI"];
  232. } else {
  233. // On some systems, the port is also in the HTTP_HOST, so we
  234. // need to strip the port if it appears to be in there.
  235. if (preg_match('/^(.+):(.+)$/', $_SERVER['HTTP_HOST'], $m)) {
  236. $host = $m[1];
  237. if (!isset($_SERVER['SERVER_PORT'])) {
  238. $_SERVER['SERVER_PORT'] = $m[2];
  239. }
  240. } else {
  241. $host = $_SERVER['HTTP_HOST'];
  242. }
  243. $protocol = (!empty($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]!="off") ? "https" : "http";
  244. $port = ($_SERVER["SERVER_PORT"]!=443 && $_SERVER["SERVER_PORT"]!=80) ? ':'.$_SERVER["SERVER_PORT"] : "";
  245. $url = $protocol.'://'.$host.$port.$_SERVER['PHP_SELF'];
  246. }
  247. if($include_query_string && !empty($_SERVER["QUERY_STRING"])){
  248. $url .= "?".$_SERVER["QUERY_STRING"];
  249. }
  250. return $url;
  251. }
  252. ?>