/action_pack/router/router_config.php

https://github.com/ameximes/akelos · PHP · 106 lines · 68 code · 19 blank · 19 comment · 25 complexity · 85e4bf4bdfa54ab3a8fa6b3a35ae70ae MD5 · raw file

  1. <?php
  2. # This file is part of the Akelos Framework
  3. # (Copyright) 2004-2010 Bermi Ferrer bermi a t bermilabs com
  4. # See LICENSE and CREDITS for details
  5. class AkRouterConfig
  6. {
  7. /**
  8. * This method tries to determine if url rewrite is enabled on this server.
  9. * It has only been tested on apache.
  10. * It is strongly recomended that you manually define the constant
  11. * AK_URL_REWRITE_ENABLED on your config file to the avoid overload
  12. * this function causes and to prevent from missfunctioning
  13. */
  14. static function loadUrlRewriteSettings() {
  15. static $result;
  16. if(isset($result)){
  17. return $result;
  18. }
  19. if(defined('AK_URL_REWRITE_ENABLED')){
  20. $result = AK_URL_REWRITE_ENABLED;
  21. return AK_URL_REWRITE_ENABLED;
  22. }
  23. if(defined('AK_ENABLE_URL_REWRITE') && AK_ENABLE_URL_REWRITE == false){
  24. if(!defined('AK_URL_REWRITE_ENABLED')){
  25. define('AK_URL_REWRITE_ENABLED',false);
  26. }
  27. $result = AK_URL_REWRITE_ENABLED;
  28. return false;
  29. }
  30. $url_rewrite_status = false;
  31. //echo '<pre>'.print_r(get_defined_functions(), true).'</pre>';
  32. if( isset($_SERVER['REDIRECT_STATUS'])
  33. && $_SERVER['REDIRECT_STATUS'] == 200
  34. && isset($_SERVER['REDIRECT_QUERY_STRING'])
  35. && strstr($_SERVER['REDIRECT_QUERY_STRING'],'ak=')){
  36. if(strstr($_SERVER['REDIRECT_QUERY_STRING'],'&')){
  37. $tmp_arr = explode('&',$_SERVER['REDIRECT_QUERY_STRING']);
  38. $ak_request = $tmp_arr[0];
  39. }else{
  40. $ak_request = $_SERVER['REDIRECT_QUERY_STRING'];
  41. }
  42. $ak_request = trim(str_replace('ak=','',$ak_request),'/');
  43. if(strstr($_SERVER['REDIRECT_URL'],$ak_request)){
  44. $url_rewrite_status = true;
  45. }else {
  46. $url_rewrite_status = false;
  47. }
  48. }
  49. // We check if available by investigating the .htaccess file if no query has been set yet
  50. elseif(function_exists('apache_get_modules')){
  51. $available_modules = apache_get_modules();
  52. if(in_array('mod_rewrite',(array)$available_modules)){
  53. // Local session name is changed intentionally from .htaccess
  54. // So we can see if the file has been loaded.
  55. // if so, we restore the session.name to its original
  56. // value
  57. if(ini_get('session.name') == 'AK_SESSID'){
  58. $session_name = defined('AK_SESSION_NAME') ? AK_SESSION_NAME : get_cfg_var('session.name');
  59. ini_set('session.name',$session_name);
  60. $url_rewrite_status = true;
  61. // In some cases where session.name cant be set up by htaccess file,
  62. // we can check for modrewrite status on this file
  63. }elseif (file_exists(AK_BASE_DIR.DS.'.htaccess')){
  64. $htaccess_file = AkFileSystem::file_get_contents(AK_BASE_DIR.DS.'.htaccess');
  65. if(stristr($htaccess_file,'RewriteEngine on')){
  66. $url_rewrite_status = true;
  67. }
  68. }
  69. }
  70. // If none of the above works we try to fetch a file that should be remaped
  71. }elseif (isset($_SERVER['REDIRECT_URL']) && $_SERVER['REDIRECT_URL'] == '/' && isset($_SERVER['REDIRECT_STATUS']) && $_SERVER['REDIRECT_STATUS'] == 200){
  72. $url_rewrite_test_url = AK_URL.'mod_rewrite_test';
  73. if(!empty($_SERVER['PHP_AUTH_USER']) && !empty($_SERVER['PHP_AUTH_PW'])){
  74. $url_rewrite_test_url = AK_PROTOCOL.$_SERVER['PHP_AUTH_USER'].':'.$_SERVER['PHP_AUTH_PW'].'@'.AK_HOST.'/mod_rewrite_test';
  75. }
  76. $url_rewrite_status = strstr(@file_get_contents($url_rewrite_test_url), 'AK_URL_REWRITE_ENABLED');
  77. $AK_URL_REWRITE_ENABLED = "define(\\'AK_URL_REWRITE_ENABLED\\', ".($url_rewrite_status ? 'true' : 'false').");\n";
  78. register_shutdown_function(create_function('',"AkFileSystem::file_put_contents(AkConfig::getDir('config').DS.'config.php',
  79. str_replace('<?php\n','<?php\n\n$AK_URL_REWRITE_ENABLED',AkFileSystem::file_get_contents(AkConfig::getDir('config').DS.'config.php')));"));
  80. }
  81. if(!defined('AK_URL_REWRITE_ENABLED')){
  82. define('AK_URL_REWRITE_ENABLED', $url_rewrite_status);
  83. }
  84. $result = AK_URL_REWRITE_ENABLED;
  85. return AK_URL_REWRITE_ENABLED;
  86. }
  87. }