Commit dd941cccc9e436413aae36fc51e4a31a1d681423
1 parent
9a61eaec
KTS-3688
"Refactor kt_url() and review the cache/serverName.txt implementation" Fixed. The serverName is now stored in the DB and only used when $_SERVER['HTTP_HOST'] is empty or set to localhost (the default in dmsDefaults). The dynamicConfigSettings are resolved every time and not cached to prevent localhost being cached as the serverName. Committed by: Megan Watson Reviewed by: Conrad Vermeulen git-svn-id: https://kt-dms.svn.sourceforge.net/svnroot/kt-dms/trunk@9330 c91229c3-7414-0410-bfa2-8a42b809f60b
Showing
3 changed files
with
47 additions
and
48 deletions
config/dmsDefaults.php
| @@ -273,8 +273,7 @@ class KTInit { | @@ -273,8 +273,7 @@ class KTInit { | ||
| 273 | $_SERVER['PATH_INFO'] = $kt_path_info; | 273 | $_SERVER['PATH_INFO'] = $kt_path_info; |
| 274 | } | 274 | } |
| 275 | 275 | ||
| 276 | - $oConfig =& KTConfig::getSingleton(); | ||
| 277 | - $sServerName = $oConfig->get('KnowledgeTree/serverName'); | 276 | + $sServerName = $oKTConfig->get('KnowledgeTree/serverName'); |
| 278 | $_SERVER['HTTP_HOST'] = $sServerName; | 277 | $_SERVER['HTTP_HOST'] = $sServerName; |
| 279 | } | 278 | } |
| 280 | // }}} | 279 | // }}} |
| @@ -423,10 +422,10 @@ class KTInit { | @@ -423,10 +422,10 @@ class KTInit { | ||
| 423 | 422 | ||
| 424 | $oKTConfig->setdefaultns('KnowledgeTree', 'fileSystemRoot', KT_DIR); | 423 | $oKTConfig->setdefaultns('KnowledgeTree', 'fileSystemRoot', KT_DIR); |
| 425 | $oKTConfig->setdefaultns('KnowledgeTree', 'serverName', KTUtil::arrayGet($_SERVER, 'HTTP_HOST', 'localhost')); | 424 | $oKTConfig->setdefaultns('KnowledgeTree', 'serverName', KTUtil::arrayGet($_SERVER, 'HTTP_HOST', 'localhost')); |
| 426 | - $oKTConfig->setdefaultns('KnowledgeTree', 'sslEnabled', 0); | 425 | + $oKTConfig->setdefaultns('KnowledgeTree', 'sslEnabled', 'false'); |
| 427 | if (array_key_exists('HTTPS', $_SERVER)) { | 426 | if (array_key_exists('HTTPS', $_SERVER)) { |
| 428 | if (strtolower($_SERVER['HTTPS']) === 'on') { | 427 | if (strtolower($_SERVER['HTTPS']) === 'on') { |
| 429 | - $oKTConfig->setdefaultns('KnowledgeTree', 'sslEnabled', 1); | 428 | + $oKTConfig->setdefaultns('KnowledgeTree', 'sslEnabled', 'true'); |
| 430 | } | 429 | } |
| 431 | } | 430 | } |
| 432 | $oKTConfig->setdefaultns('KnowledgeTree', 'rootUrl', $this->guessRootUrl()); | 431 | $oKTConfig->setdefaultns('KnowledgeTree', 'rootUrl', $this->guessRootUrl()); |
| @@ -466,25 +465,10 @@ class KTInit { | @@ -466,25 +465,10 @@ class KTInit { | ||
| 466 | // If the http_host server variable is not set then the serverName gets set to localhost | 465 | // If the http_host server variable is not set then the serverName gets set to localhost |
| 467 | // We don't want to store this setting so we set store_cache to false | 466 | // We don't want to store this setting so we set store_cache to false |
| 468 | $store_cache = false; | 467 | $store_cache = false; |
| 469 | - | ||
| 470 | - /* | ||
| 471 | - // Check if serverName.txt is set and use that | ||
| 472 | - $pathFile = KT_DIR . '/config/cache-path'; | ||
| 473 | - $cachePath = trim(file_get_contents($pathFile)); | ||
| 474 | - $serverNamePath = $cachePath . '/' . KTUtil::SERVER_NAME_FILE; | ||
| 475 | - | ||
| 476 | - if(file_exists($serverNamePath)){ | ||
| 477 | - $serverName = @file_get_contents($serverNamePath); | ||
| 478 | - $_SERVER['HTTP_HOST'] = $serverName; | ||
| 479 | - } | ||
| 480 | - */ | ||
| 481 | } | 468 | } |
| 482 | } | 469 | } |
| 483 | 470 | ||
| 484 | if(!$use_cache) { | 471 | if(!$use_cache) { |
| 485 | - // Get default server url settings | ||
| 486 | - $this->getDynamicConfigSettings(); | ||
| 487 | - | ||
| 488 | //Read in DB settings and config settings | 472 | //Read in DB settings and config settings |
| 489 | $oKTConfig->readDBConfig(); | 473 | $oKTConfig->readDBConfig(); |
| 490 | } | 474 | } |
| @@ -502,6 +486,8 @@ class KTInit { | @@ -502,6 +486,8 @@ class KTInit { | ||
| 502 | // Create the global $default array | 486 | // Create the global $default array |
| 503 | if(!$use_cache) $res = $oKTConfig->readConfig(); | 487 | if(!$use_cache) $res = $oKTConfig->readConfig(); |
| 504 | 488 | ||
| 489 | + // Get default server url settings | ||
| 490 | + $this->getDynamicConfigSettings(); | ||
| 505 | 491 | ||
| 506 | if($store_cache && isset($cachePath)){ | 492 | if($store_cache && isset($cachePath)){ |
| 507 | @touch($cachePath); | 493 | @touch($cachePath); |
lib/config/config.inc.php
| @@ -254,7 +254,10 @@ class KTConfig { | @@ -254,7 +254,10 @@ class KTConfig { | ||
| 254 | } | 254 | } |
| 255 | 255 | ||
| 256 | function setdefaultns($seck, $k, $v) { | 256 | function setdefaultns($seck, $k, $v) { |
| 257 | - return $this->setns($seck, $k, $v, true); | 257 | + $this->setns($seck, $k, $v, true); |
| 258 | + | ||
| 259 | + global $default; | ||
| 260 | + $default->$k = $this->expand($this->flatns["$seck/$k"]); | ||
| 258 | } | 261 | } |
| 259 | 262 | ||
| 260 | function expand($val) { | 263 | function expand($val) { |
lib/util/ktutil.inc
| @@ -50,7 +50,7 @@ class KTUtil { | @@ -50,7 +50,7 @@ class KTUtil { | ||
| 50 | const TB = 1099511627776; | 50 | const TB = 1099511627776; |
| 51 | const PB = 1125899906842624; | 51 | const PB = 1125899906842624; |
| 52 | 52 | ||
| 53 | - const SERVER_NAME_FILE = 'serverName.txt'; | 53 | + const SERVER_NAME_SETTING = 'server_name'; |
| 54 | 54 | ||
| 55 | /** | 55 | /** |
| 56 | * Used to resolve the server name | 56 | * Used to resolve the server name |
| @@ -59,19 +59,23 @@ class KTUtil { | @@ -59,19 +59,23 @@ class KTUtil { | ||
| 59 | static function save_base_kt_url() | 59 | static function save_base_kt_url() |
| 60 | { | 60 | { |
| 61 | global $default; | 61 | global $default; |
| 62 | - $cacheDir = $default->cacheDirectory; | ||
| 63 | 62 | ||
| 64 | - $serverFilename = $cacheDir . '/' . KTUtil::SERVER_NAME_FILE; | ||
| 65 | - if (file_exists($serverFilename)) | ||
| 66 | - { | ||
| 67 | - return; | ||
| 68 | - } | 63 | + $serverName = KTUtil::getSystemSetting(SERVER_NAME_SETTING, false); |
| 64 | + if($serverName !== false){ | ||
| 65 | + return $serverName; | ||
| 66 | + } | ||
| 69 | 67 | ||
| 70 | $rootUrl = $default->rootUrl; | 68 | $rootUrl = $default->rootUrl; |
| 71 | $protocol = $default->sslEnabled ? 'https' : 'http'; | 69 | $protocol = $default->sslEnabled ? 'https' : 'http'; |
| 72 | $port = $_SERVER['SERVER_PORT']+0; | 70 | $port = $_SERVER['SERVER_PORT']+0; |
| 73 | $serverName = $_SERVER['SERVER_NAME']; | 71 | $serverName = $_SERVER['SERVER_NAME']; |
| 74 | 72 | ||
| 73 | + // We don't want to set the servername to localhost, it should be set to the url that will be used normally | ||
| 74 | + if($serverName == 'localhost' || $serverName == '127.0.0.1') | ||
| 75 | + { | ||
| 76 | + return false; | ||
| 77 | + } | ||
| 78 | + | ||
| 75 | $base_url = $protocol . '://' . $serverName; | 79 | $base_url = $protocol . '://' . $serverName; |
| 76 | 80 | ||
| 77 | if (($protocol == 'http' && $port == 80) || ($protocol == 'https' && $port == 443)) | 81 | if (($protocol == 'http' && $port == 80) || ($protocol == 'https' && $port == 443)) |
| @@ -86,7 +90,8 @@ class KTUtil { | @@ -86,7 +90,8 @@ class KTUtil { | ||
| 86 | // Add the root url | 90 | // Add the root url |
| 87 | $base_url .= $rootUrl; | 91 | $base_url .= $rootUrl; |
| 88 | 92 | ||
| 89 | - @file_put_contents($serverFilename, $base_url); | 93 | + // Save as system setting |
| 94 | + KTUtil::setSystemSetting(SERVER_NAME_SETTING, $base_url); | ||
| 90 | } | 95 | } |
| 91 | 96 | ||
| 92 | 97 | ||
| @@ -100,28 +105,34 @@ class KTUtil { | @@ -100,28 +105,34 @@ class KTUtil { | ||
| 100 | return $base_url; | 105 | return $base_url; |
| 101 | } | 106 | } |
| 102 | 107 | ||
| 103 | - $cacheDir = $default->cacheDirectory; | ||
| 104 | - | ||
| 105 | - $base_url = @file_get_contents($cacheDir . '/' . KTUtil::SERVER_NAME_FILE); | 108 | + $config = KTConfig::getSingleton(); |
| 109 | + $serverName = $config->get('knowledgeTree/serverName', $_SERVER['HTTP_HOST']); | ||
| 106 | 110 | ||
| 107 | - /* We are checking if the object exists because we could have an error pre oor during initialization */ | ||
| 108 | - if(is_object($default->log)) | 111 | + // $serverName gets set in dmsDefaults, if $_SERVER['HTTP_HOST'] is empty, it gets set to localhost |
| 112 | + // we want to avoid this if possible | ||
| 113 | + if(empty($serverName) || $serverName == 'localhost') | ||
| 109 | { | 114 | { |
| 110 | - $default->log->debug("kt_url: base url - $base_url"); | ||
| 111 | - } | 115 | + // The host has not been set - check if the serverName setting exists |
| 116 | + $base_url = KTUtil::getSystemSetting(SERVER_NAME_SETTING, false); | ||
| 117 | + | ||
| 118 | + if (false !== $base_url) | ||
| 119 | + { | ||
| 120 | + /* We are checking if the object exists because we could have an error pre oor during initialization */ | ||
| 121 | + if(is_object($default->log)) | ||
| 122 | + { | ||
| 123 | + $default->log->debug("kt_url: base url - $base_url"); | ||
| 124 | + } | ||
| 125 | + | ||
| 126 | + $base_url = str_replace(array("\n","\r"), array('',''), $base_url); | ||
| 127 | + return $base_url; | ||
| 128 | + } | ||
| 112 | 129 | ||
| 113 | - if (false !== $base_url) | ||
| 114 | - { | ||
| 115 | - $base_url = str_replace(array("\n","\r"), array('',''), $base_url); | ||
| 116 | - return $base_url; | 130 | + // TODO: Check if the $_SERVER['SERVER_NAME'] variable is set. |
| 131 | + $serverName = 'localhost'; | ||
| 117 | } | 132 | } |
| 118 | 133 | ||
| 119 | - $config = KTConfig::getSingleton(); | ||
| 120 | - $serverName = $config->get('knowledgeTree/serverName', $_SERVER['HTTP_HOST']); | ||
| 121 | - $rootUrl = $default->rootUrl; | ||
| 122 | - | ||
| 123 | - $base_url = ($default->sslEnabled ? 'https' : 'http') .'://'.$serverName . $rootUrl; | ||
| 124 | - | 134 | + // build up the url |
| 135 | + $base_url = ($default->sslEnabled ? 'https' : 'http') .'://'.$serverName . $default->rootUrl; | ||
| 125 | if(is_object($default->log)) | 136 | if(is_object($default->log)) |
| 126 | { | 137 | { |
| 127 | $default->log->debug("kt_url: base url - $base_url"); | 138 | $default->log->debug("kt_url: base url - $base_url"); |
| @@ -132,11 +143,10 @@ class KTUtil { | @@ -132,11 +143,10 @@ class KTUtil { | ||
| 132 | static function call_page($path) | 143 | static function call_page($path) |
| 133 | { | 144 | { |
| 134 | global $default; | 145 | global $default; |
| 135 | - $cacheDir = $default->cacheDirectory; | ||
| 136 | 146 | ||
| 137 | - $base_url = @file_get_contents($cacheDir . '/' . KTUtil::SERVER_NAME_FILE); | 147 | + $base_url = KTUtil::getSystemSetting(SERVER_NAME_SETTING, false); |
| 138 | 148 | ||
| 139 | - if (false == $base_url) | 149 | + if (false === $base_url) |
| 140 | { | 150 | { |
| 141 | $default->log->info("call_page: $path - cannot call script until user logs in for the first time!"); | 151 | $default->log->info("call_page: $path - cannot call script until user logs in for the first time!"); |
| 142 | return; | 152 | return; |