PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/KurogoSite.php

http://github.com/modolabs/Kurogo-Mobile-Web
PHP | 319 lines | 241 code | 65 blank | 13 comment | 31 complexity | 5875b2664d97007a01b04d05b541779f MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1
  1. <?php
  2. /*
  3. * Copyright © 2010 - 2013 Modo Labs Inc. All rights reserved.
  4. *
  5. * The license governing the contents of this file is located in the LICENSE
  6. * file located at the root directory of this distribution. If the LICENSE file
  7. * is missing, please contact sales@modolabs.com.
  8. *
  9. */
  10. class KurogoSite
  11. {
  12. protected $name;
  13. protected $title;
  14. protected $siteDir;
  15. protected $initArgs;
  16. protected $urlBase='/';
  17. protected $urlBaseAuto=true;
  18. protected $host;
  19. protected $hosts=array();
  20. protected $default = false;
  21. protected $enabled = true;
  22. protected $configStore;
  23. protected $configMode='';
  24. protected $cacher;
  25. protected $requiresSecure = false;
  26. const EXACT_MATCH=3;
  27. const DEFAULT_MATCH=2;
  28. const INEXACT_MATCH=1;
  29. const NO_MATCH=0;
  30. public function __construct($name, $config) {
  31. $config = is_array($config) ? $config : array();
  32. if (!self::isValidSiteName($name)) {
  33. throw new KurogoException("Invalid site name $name");
  34. }
  35. $this->name = $name;
  36. if (isset($config['SITE_DIR'])) {
  37. $this->siteDir = $config['SITE_DIR'];
  38. } else {
  39. $this->siteDir = SITES_DIR . DIRECTORY_SEPARATOR . $name;
  40. }
  41. if (isset($config['urlBase'])) {
  42. //trim all slashes
  43. $urlBase = trim(strtolower($config['urlBase']),'/');
  44. if (strlen($urlBase)>0 && !preg_match("#^[a-z0-9_/.-]+$#i", $urlBase)) {
  45. throw new KurogoConfigurationException("Invalid urlBase " . $config['urlBase']);
  46. }
  47. $this->urlBase = '/' . $urlBase;
  48. $this->urlBaseAuto = false;
  49. }
  50. if (isset($config['host'])) {
  51. if (is_array($config['host'])) {
  52. foreach ($config['host'] as $host) {
  53. if (self::isValidHostName($host)) {
  54. $this->hosts[] = $host;
  55. }
  56. }
  57. } elseif (self::isValidHostName($config['host'])) {
  58. $this->host = $config['host'];
  59. $this->hosts[] = $config['host'];
  60. } else {
  61. throw new KurogoConfigurationException('Invalid host "' . $config['host'] . '"');
  62. }
  63. }
  64. if (isset($config['title'])) {
  65. $this->title = $config['title'];
  66. } else {
  67. $this->title = $this->name;
  68. }
  69. if (isset($config['CONFIG_MODE'])) {
  70. $this->configMode = $config['CONFIG_MODE'];
  71. }
  72. $this->enabled = (bool) Kurogo::arrayVal($config, 'enabled', true);
  73. if (isset($config['default'])) {
  74. $this->default = (bool) $config['default'];
  75. }
  76. if (isset($config['secure'])) {
  77. $this->requiresSecure = (bool) $config['secure'];
  78. }
  79. $this->initArgs = $config;
  80. }
  81. public function getRequiresSecure() {
  82. return $this->requiresSecure || $this->getOptionalSiteVar('SECURE_REQUIRED');
  83. }
  84. public function getTitle() {
  85. return $this->title;
  86. }
  87. public function isDefault() {
  88. return $this->default;
  89. }
  90. public function isEnabled() {
  91. return $this->enabled;
  92. }
  93. public function getConfigMode() {
  94. return $this->configMode;
  95. }
  96. public static function isValidHostName($host) {
  97. return preg_match("!^([a-z0-9.-]+)(:\d+)?$!i", $host);
  98. }
  99. public static function isValidSiteName($name) {
  100. return preg_match("/^[a-z][a-z0-9_-]*$/i", $name);
  101. }
  102. public function getName() {
  103. return $this->name;
  104. }
  105. public function getURLBaseAuto() {
  106. return $this->urlBaseAuto;
  107. }
  108. public function getHost() {
  109. if(!empty($this->host)) {
  110. return $this->host;
  111. } elseif (count($this->hosts)>0) {
  112. return current($this->hosts);
  113. } else {
  114. return SERVER_HOST;
  115. }
  116. }
  117. public function getHostValue() {
  118. return $this->host;
  119. }
  120. protected function replaceDir($dir) {
  121. static $hostname;
  122. if (strlen($hostname)==0) {
  123. $hostname = gethostname();
  124. }
  125. return str_replace(array('%SITE_DIR%','%SITES_DIR%','%HOSTNAME%'), array($this->siteDir, SITES_DIR, $hostname), $dir);
  126. }
  127. public function getSiteDir() {
  128. return $this->siteDir;
  129. }
  130. public function getSharedDir() {
  131. return $this->replaceDir(Kurogo::arrayVal($this->initArgs, 'SHARED_DIR', SITES_DIR . DIRECTORY_SEPARATOR . 'shared'));
  132. }
  133. public function getSharedConfigDir() {
  134. return $this->getSharedDir() . DIRECTORY_SEPARATOR . 'config';
  135. }
  136. public function getSharedAppDir() {
  137. return $this->getSharedDir() . DIRECTORY_SEPARATOR . 'app';
  138. }
  139. public function getSharedModulesDir() {
  140. return $this->getSharedAppDir() . DIRECTORY_SEPARATOR . 'modules';
  141. }
  142. public function getSharedDataDir() {
  143. return $this->getSharedDir() . DIRECTORY_SEPARATOR . 'data';
  144. }
  145. public function getSharedLibDir() {
  146. return $this->getSharedDir() . DIRECTORY_SEPARATOR . 'lib';
  147. }
  148. public function getSharedScriptsDir() {
  149. return $this->getSharedDir() . DIRECTORY_SEPARATOR . 'scripts';
  150. }
  151. public function getBaseCacheDir() {
  152. return $this->replaceDir(Kurogo::arrayVal($this->initArgs, 'BASE_CACHE_DIR', $this->siteDir . DIRECTORY_SEPARATOR . 'cache'));
  153. }
  154. public function getCacheDir() {
  155. return $this->replaceDir(Kurogo::arrayVal($this->initArgs, 'CACHE_DIR', $this->siteDir . DIRECTORY_SEPARATOR . 'cache'));
  156. }
  157. public function getBaseLogDir() {
  158. return $this->replaceDir(Kurogo::arrayVal($this->initArgs, 'BASE_LOG_DIR', $this->siteDir . DIRECTORY_SEPARATOR . 'logs'));
  159. }
  160. public function getLogDir() {
  161. return $this->replaceDir(Kurogo::arrayVal($this->initArgs, 'LOG_DIR', $this->siteDir . DIRECTORY_SEPARATOR . 'logs'));
  162. }
  163. public function getSiteLibDir() {
  164. return $this->siteDir . DIRECTORY_SEPARATOR . 'lib';
  165. }
  166. public function getSiteAppDir() {
  167. return $this->siteDir . DIRECTORY_SEPARATOR . 'app';
  168. }
  169. public function getSiteModulesDir() {
  170. return $this->getSiteAppDir() . DIRECTORY_SEPARATOR . 'modules';
  171. }
  172. public function getDataDir() {
  173. return $this->siteDir . DIRECTORY_SEPARATOR . 'data';
  174. }
  175. public function getWebBridgeDir() {
  176. return $this->siteDir . DIRECTORY_SEPARATOR . KurogoWebBridge::getAssetsDir();
  177. }
  178. public function getConfigDir() {
  179. return $this->siteDir . DIRECTORY_SEPARATOR . 'config';
  180. }
  181. public function getConfigDisabledDir() {
  182. return $this->siteDir . DIRECTORY_SEPARATOR . 'config_disabled';
  183. }
  184. public function getScriptsDir() {
  185. return $this->siteDir . DIRECTORY_SEPARATOR . 'scripts';
  186. }
  187. public function getConfigStore() {
  188. if (!$this->configStore) {
  189. $args = array('site'=>$this);
  190. $configStoreClass = Kurogo::arrayVal($this->initArgs, 'CONFIG_CLASS', 'ConfigFileStore');
  191. $this->configStore = ConfigStore::factory($configStoreClass, $args);
  192. }
  193. return $this->configStore;
  194. }
  195. public function cacher() {
  196. if (!isset($this->cacher)) {
  197. if ($cacheClass = Kurogo::arrayVal($this->initArgs, 'CACHE_CLASS')) {
  198. $this->cacher = KurogoMemoryCache::factory($cacheClass, $this->initArgs);
  199. } else {
  200. $this->cacher = false;
  201. }
  202. }
  203. return $this->cacher;
  204. }
  205. // returns a score based on the path/host
  206. public function getSiteScore($path, $host) {
  207. // if the urlBase is '/' then treat it as empty
  208. $urlBase = $this->urlBase == '/' ? '' : $this->urlBase;
  209. // get the first path component
  210. if (strlen($path)>1 && ($offset = strpos($path, '/', 1)) !== false) {
  211. $path = substr($path, 0, $offset);
  212. }
  213. $score = 0;
  214. //if the url base matches
  215. if ($urlBase == $path) {
  216. $score+=2;
  217. }
  218. if (in_array($host, $this->hosts)) {
  219. if (empty($this->host)) {
  220. $this->host = $host;
  221. }
  222. $score+=3;
  223. }
  224. if ($this->default) {
  225. $score+=1;
  226. }
  227. return $score;
  228. }
  229. public function getURL() {
  230. $url = sprintf("http%s://%s%s", $this->getRequiresSecure() ? 's' : '', $this->getHost(), $this->getUrlBase());
  231. return $url;
  232. }
  233. public function getURLBase() {
  234. return $this->urlBase;
  235. }
  236. public function getSiteSections($area, $applyContexts=Config::IGNORE_CONTEXTS) {
  237. return $this->getConfigStore()->getSections($area, 'site', $applyContexts);
  238. }
  239. public function getOptionalSiteSections($area, $applyContexts=Config::IGNORE_CONTEXTS) {
  240. return $this->getConfigStore()->getOptionalSections($area, 'site', $applyContexts);
  241. }
  242. public function getSiteSection($section, $area='site') {
  243. return $this->getConfigStore()->getSection($section, $area, 'site');
  244. }
  245. public function getOptionalSiteSection($section, $area='site') {
  246. return $this->getConfigStore()->getOptionalSection($section, $area, 'site');
  247. }
  248. public function getSiteVar($var, $section=null, $area='site') {
  249. return $this->getConfigStore()->getVar($var, $section, $area, 'site');
  250. }
  251. public function getOptionalSiteVar($var, $default='', $section=null, $area='site') {
  252. return $this->getConfigStore()->getOptionalVar($var, $default, $section, $area, 'site');
  253. }
  254. }