Commit 3df35815ad7b15d2c54a1dc25a5d8c7469e378cc

Authored by kevin_fourie
1 parent 84224042

Merged in from STABLE trunk...

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/STABLE/branches/3.5.3a-Release-Branch@9332 c91229c3-7414-0410-bfa2-8a42b809f60b
config/dmsDefaults.php
... ... @@ -273,8 +273,7 @@ class KTInit {
273 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 277 $_SERVER['HTTP_HOST'] = $sServerName;
279 278 }
280 279 // }}}
... ... @@ -423,10 +422,10 @@ class KTInit {
423 422  
424 423 $oKTConfig->setdefaultns('KnowledgeTree', 'fileSystemRoot', KT_DIR);
425 424 $oKTConfig->setdefaultns('KnowledgeTree', 'serverName', KTUtil::arrayGet($_SERVER, 'HTTP_HOST', 'localhost'));
426   - $oKTConfig->setdefaultns('KnowledgeTree', 'sslEnabled', 0);
  425 + $oKTConfig->setdefaultns('KnowledgeTree', 'sslEnabled', 'false');
427 426 if (array_key_exists('HTTPS', $_SERVER)) {
428 427 if (strtolower($_SERVER['HTTPS']) === 'on') {
429   - $oKTConfig->setdefaultns('KnowledgeTree', 'sslEnabled', 1);
  428 + $oKTConfig->setdefaultns('KnowledgeTree', 'sslEnabled', 'true');
430 429 }
431 430 }
432 431 $oKTConfig->setdefaultns('KnowledgeTree', 'rootUrl', $this->guessRootUrl());
... ... @@ -466,25 +465,10 @@ class KTInit {
466 465 // If the http_host server variable is not set then the serverName gets set to localhost
467 466 // We don't want to store this setting so we set store_cache to false
468 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 471 if(!$use_cache) {
485   - // Get default server url settings
486   - $this->getDynamicConfigSettings();
487   -
488 472 //Read in DB settings and config settings
489 473 $oKTConfig->readDBConfig();
490 474 }
... ... @@ -502,6 +486,8 @@ class KTInit {
502 486 // Create the global $default array
503 487 if(!$use_cache) $res = $oKTConfig->readConfig();
504 488  
  489 + // Get default server url settings
  490 + $this->getDynamicConfigSettings();
505 491  
506 492 if($store_cache && isset($cachePath)){
507 493 @touch($cachePath);
... ...
lib/config/config.inc.php
... ... @@ -254,7 +254,10 @@ class KTConfig {
254 254 }
255 255  
256 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 263 function expand($val) {
... ...
lib/util/ktutil.inc
... ... @@ -50,7 +50,7 @@ class KTUtil {
50 50 const TB = 1099511627776;
51 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 56 * Used to resolve the server name
... ... @@ -59,19 +59,23 @@ class KTUtil {
59 59 static function save_base_kt_url()
60 60 {
61 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 68 $rootUrl = $default->rootUrl;
71 69 $protocol = $default->sslEnabled ? 'https' : 'http';
72 70 $port = $_SERVER['SERVER_PORT']+0;
73 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 79 $base_url = $protocol . '://' . $serverName;
76 80  
77 81 if (($protocol == 'http' && $port == 80) || ($protocol == 'https' && $port == 443))
... ... @@ -86,7 +90,8 @@ class KTUtil {
86 90 // Add the root url
87 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 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 136 if(is_object($default->log))
126 137 {
127 138 $default->log->debug("kt_url: base url - $base_url");
... ... @@ -132,11 +143,10 @@ class KTUtil {
132 143 static function call_page($path)
133 144 {
134 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 151 $default->log->info("call_page: $path - cannot call script until user logs in for the first time!");
142 152 return;
... ...