PageRenderTime 41ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/system/classes/site.php

https://github.com/HabariMag/habarimag-old
PHP | 417 lines | 288 code | 20 blank | 109 comment | 41 complexity | 08bf462b3cb125bfd41db78a6d7f0bcc MD5 | raw file
Possible License(s): Apache-2.0
  1. <?php
  2. /**
  3. * @package Habari
  4. *
  5. */
  6. /**
  7. * Habari Site class
  8. *
  9. * Contains functions for getting details about the site directories and URLs.
  10. *
  11. */
  12. class Site
  13. {
  14. /**
  15. * Constants
  16. * CONFIG_LOCAL Local installation
  17. * CONFIG_SUBDIR Subdirectory or multisite installation
  18. * CONFIG_SUBDOMAIN Subdomain installation
  19. */
  20. const CONFIG_LOCAL = 0;
  21. const CONFIG_SUBDIR = 1;
  22. const CONFIG_SUBDOMAIN = 2;
  23. /**
  24. * @staticvar $config_path Filesystem path to config.php
  25. * @staticvar $config_dir Multisite directory to config.php
  26. * @staticvar $config_type Installation type (local, subdir, subdomain)
  27. * @staticvar $scriptname the name of the currently executing script (index.php)
  28. * @staticvar $habari_url fully-qualified URL to the habari directory
  29. */
  30. static $config_path;
  31. static $config_dir;
  32. static $config_type = Site::CONFIG_LOCAL;
  33. static $scriptname;
  34. static $habari_url;
  35. static $config_urldir;
  36. /**
  37. * Constructor
  38. * This class should not be instantiated
  39. */
  40. private function __construct()
  41. {
  42. }
  43. /**
  44. * script_name is a helper function to determine the name of the script
  45. * not all PHP installations return the same values for $_SERVER['SCRIPT_URL']
  46. * and $_SERVER['SCRIPT_NAME']
  47. */
  48. public static function script_name()
  49. {
  50. switch ( true ) {
  51. case isset( self::$scriptname ):
  52. break;
  53. case isset( $_SERVER['SCRIPT_NAME'] ):
  54. self::$scriptname = $_SERVER['SCRIPT_NAME'];
  55. break;
  56. case isset( $_SERVER['PHP_SELF'] ):
  57. self::$scriptname = $_SERVER['PHP_SELF'];
  58. break;
  59. default:
  60. Error::raise( _t( 'Could not determine script name.' ) );
  61. die();
  62. }
  63. return self::$scriptname;
  64. }
  65. /**
  66. * is() returns a boolean value for whether the current site is the
  67. * primary site, or a multi-site, as determined by the location of
  68. * the config.php that is in use for this request.
  69. * valid values are "main" and "primary" (synonymous) and "multi"
  70. * @examples:
  71. * if ( Site::is('main') )
  72. * if ( Site::is('multi') )
  73. * @param string The name of the boolean to test
  74. * @return bool the result of the check
  75. */
  76. public static function is( $what )
  77. {
  78. switch ( strtolower( $what ) ) {
  79. case 'main':
  80. case 'primary':
  81. if ( Site::$config_type == Site::CONFIG_LOCAL ) {
  82. return true;
  83. }
  84. else {
  85. return false;
  86. }
  87. break;
  88. case 'multi':
  89. if ( Site::$config_type != Site::CONFIG_LOCAL ) {
  90. return true;
  91. }
  92. else {
  93. return false;
  94. }
  95. break;
  96. }
  97. }
  98. /**
  99. * get_url returns a fully-qualified URL
  100. * 'host' returns http://www.habariproject.org
  101. * 'habari' returns http://www.habariproject.org/habari, if you
  102. * have Habari installed into a /habari/ sub-directory
  103. * 'site' returns http://www.habariproject.org/site if
  104. * you are installing with a subdirectory path
  105. * 'user' returns one of the following:
  106. * http://www.habariproject.org/user
  107. * http://www.habariproject.org/user/sites/x.y.z
  108. * 'theme' returns one of the following:
  109. * http://www.habariproject.org/user/themes/theme_name
  110. * http://www.habariproject.org/user/sites/x.y.z/themes/theme_name
  111. * 'admin' returns http://www.habariproject.org/admin
  112. * 'admin_theme' returns http://www.habariproject.org/system/admin
  113. * 'system' returns http://www.habariproject.org/system
  114. * 'scripts' returns http://www.habariproject.org/scripts
  115. * '3rdparty' returns http://www.habariproject.org/3rdparty
  116. * 'hostname' returns www.habariproject.org
  117. * @param string the name of the URL to return
  118. * @param bool whether to include a trailing slash. Default: No
  119. * @return string URL
  120. */
  121. public static function get_url( $name, $trail = false )
  122. {
  123. $url = '';
  124. switch ( strtolower( $name ) ) {
  125. case 'host':
  126. $protocol = 'http';
  127. // If we're running on a port other than 80, i
  128. // add the port number to the value returned
  129. // from host_url
  130. $port = 80; // Default in case not set.
  131. if ( isset( $_SERVER['SERVER_PORT'] ) ) {
  132. $port = $_SERVER['SERVER_PORT'];
  133. }
  134. $portpart = '';
  135. $host = Site::get_url( 'hostname' );
  136. // if the port isn't a standard port, and isn't part of $host already, add it
  137. if ( ( $port != 80 ) && ( $port != 443 ) && ( MultiByte::substr( $host, MultiByte::strlen( $host ) - strlen( $port ) ) != $port ) ) {
  138. $portpart = ':' . $port;
  139. }
  140. if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ) {
  141. $protocol = 'https';
  142. }
  143. $url = $protocol . '://' . $host . $portpart;
  144. break;
  145. case 'habari':
  146. if ( null !== self::$habari_url ) {
  147. $url = self::$habari_url;
  148. }
  149. else {
  150. $url = Site::get_url( 'host' );
  151. $path = trim( dirname( Site::script_name() ), '/\\' );
  152. if ( '' != $path ) {
  153. $url .= '/' . $path;
  154. }
  155. self::$habari_url = $url;
  156. }
  157. break;
  158. case 'site':
  159. $url = Site::get_url( 'host' );
  160. if( self::$config_type == Site::CONFIG_SUBDIR ) {
  161. $url .= '/' . self::$config_urldir;
  162. }
  163. break;
  164. case 'user':
  165. $url = Site::get_url( 'host' ) . Site::get_path( 'base', true ) . Site::get_path( 'user' );
  166. break;
  167. case 'theme':
  168. $theme = Themes::get_theme_dir();
  169. if ( file_exists( Site::get_dir( 'config' ) . '/themes/' . $theme ) ) {
  170. $url = Site::get_url( 'user' ) . '/themes/' . $theme;
  171. }
  172. elseif ( file_exists( HABARI_PATH . '/user/themes/' . $theme ) ) {
  173. $url = Site::get_url( 'habari' ) . '/user/themes/' . $theme;
  174. }
  175. elseif ( file_exists( HABARI_PATH . '/3rdparty/themes/' . $theme ) ) {
  176. $url = Site::get_url( 'habari' ) . '/3rdparty/themes/' . $theme;
  177. }
  178. else {
  179. $url = Site::get_url( 'habari' ) . '/system/themes/' . $theme;
  180. }
  181. break;
  182. case 'admin':
  183. $url = Site::get_url( 'habari' ) . '/admin';
  184. break;
  185. case 'admin_theme':
  186. $url = Site::get_url( 'habari' ) . '/system/admin';
  187. break;
  188. case 'login':
  189. $url = Site::get_url( 'habari' ) . '/auth/login';
  190. break;
  191. case 'logout':
  192. $url = Site::get_url( 'habari' ) . '/auth/logout';
  193. break;
  194. case 'system':
  195. $url = Site::get_url( 'habari' ) . '/system';
  196. break;
  197. case 'vendor':
  198. case 'scripts':
  199. $url = Site::get_url( 'system' ) . '/vendor';
  200. break;
  201. case '3rdparty':
  202. // this should be removed at a later date as it will cause problems
  203. // once 'vendor' is adopted, dump the condition!
  204. if ( file_exists( HABARI_PATH . '/3rdparty' ) ) {
  205. $url = Site::get_url( 'habari' ) . '/3rdparty';
  206. }
  207. else {
  208. $url = Site::get_url( 'vendor' );
  209. }
  210. break;
  211. case 'hostname':
  212. // HTTP_HOST is not set for HTTP/1.0 requests
  213. $url = ( $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' || !isset( $_SERVER['HTTP_HOST'] ) ) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
  214. break;
  215. }
  216. $url .= Utils::trail( $trail );
  217. $url = Plugins::filter( 'site_url_' . $name, $url );
  218. return $url;
  219. }
  220. /**
  221. * get_path returns a relative URL path, without leading protocol or host
  222. * 'base' returns the URL sub-directory in which Habari is installed, if any.
  223. * 'user' returns one of the following:
  224. * user
  225. * user/sites/x.y.z
  226. * 'theme' returns one of the following:
  227. * /user/themes/theme_name
  228. * /user/sites/x.y.z/themes/theme_dir
  229. * @param string the name of the path to return
  230. * @param bool whether to include a trailing slash. Default: No
  231. */
  232. public static function get_path( $name, $trail = false )
  233. {
  234. $path = '';
  235. switch ( strtolower( $name ) ) {
  236. case 'base':
  237. case 'habari':
  238. $path = rtrim( dirname( Site::script_name() ), '/\\' );
  239. break;
  240. case 'user':
  241. if ( Site::is( 'main' ) ) {
  242. $path = 'user';
  243. }
  244. else {
  245. $path = ltrim( str_replace( HABARI_PATH, '', Site::get_dir( 'config' ) ), '/' );
  246. }
  247. break;
  248. case 'theme':
  249. $theme = Themes::get_theme_dir();
  250. if ( file_exists( Site::get_dir( 'config' ) . '/themes/' . $theme ) ) {
  251. $path = Site::get_path( 'user' ) . '/themes/' . $theme;
  252. }
  253. elseif ( file_exists( HABARI_PATH . '/3rdparty/themes/' . $theme ) ) {
  254. $url = Site::get_url( 'habari' ) . '/3rdparty/themes/' . $theme;
  255. }
  256. else {
  257. $path = Site::get_path( 'base' ) . '/user/themes/' . $theme;
  258. }
  259. break;
  260. }
  261. $path .= Utils::trail( $trail );
  262. // if running Habari in docroot, get_url('base') will return
  263. // a double slash. Let's fix that.
  264. $path = str_replace( '//', '/', $path );
  265. $path = Plugins::filter( 'site_path_' . $name, $path );
  266. return $path;
  267. }
  268. /**
  269. * get_dir returns a complete filesystem path to the requested item
  270. * 'config_file' returns the complete path to the config.php file, including the filename
  271. * 'config' returns the path of the directory containing config.php
  272. * 'user' returns the path of the user directory
  273. * 'theme' returns the path of the site's active theme
  274. * @param string the name of the path item to return
  275. * @param bool whether to include a trailing slash. Default: No
  276. * @return string Path
  277. */
  278. public static function get_dir( $name, $trail = false )
  279. {
  280. $path = '';
  281. switch ( strtolower( $name ) ) {
  282. case 'config_file':
  283. $path = Site::get_dir( 'config' ) . '/config.php';
  284. break;
  285. case 'config':
  286. if ( self::$config_path ) {
  287. return self::$config_path;
  288. }
  289. self::$config_path = HABARI_PATH;
  290. $config_dirs = preg_replace( '/^' . preg_quote( HABARI_PATH, '/' ) . '\/user\/sites\/(.*)\/config.php/', '$1', Utils::glob( HABARI_PATH . '/user/sites/*/config.php' ) );
  291. if ( empty( $config_dirs ) ) {
  292. return self::$config_path;
  293. }
  294. $server = InputFilter::parse_url( Site::get_url( 'habari' ) );
  295. $request = array();
  296. if ( isset( $server['port'] ) && $server['port'] != '' && $server['port'] != '80' ) {
  297. $request[] = $server['port'];
  298. }
  299. $request = array_merge($request, explode('.', $server['host']));
  300. $basesegments = count($request);
  301. $request = array_merge($request, explode( '/', trim( $_SERVER['REQUEST_URI'], '/' ) ) );
  302. $x = 0;
  303. do {
  304. $match = implode('.', $request);
  305. if ( in_array( $match, $config_dirs ) ) {
  306. self::$config_dir = $match;
  307. self::$config_path = HABARI_PATH . '/user/sites/' . self::$config_dir;
  308. self::$config_type = ( $basesegments > count($request) ) ? Site::CONFIG_SUBDOMAIN : Site::CONFIG_SUBDIR;
  309. self::$config_urldir = implode('/', array_slice($request, $basesegments));
  310. break;
  311. }
  312. array_pop($request);
  313. $x--;
  314. if ( $x < -10 ) {
  315. echo $x;
  316. var_dump($request);
  317. die('too many ');
  318. }
  319. } while ( count($request) > 0 );
  320. $path = self::$config_path;
  321. break;
  322. case 'user':
  323. if ( Site::get_dir( 'config' ) == HABARI_PATH ) {
  324. $path = HABARI_PATH . '/user';
  325. }
  326. else {
  327. $path = Site::get_dir( 'config' );
  328. }
  329. break;
  330. case 'theme':
  331. $theme = Themes::get_theme_dir();
  332. if ( file_exists( Site::get_dir( 'config' ) . '/themes/' . $theme ) ) {
  333. $path = Site::get_dir( 'user' ) . '/themes/' . $theme;
  334. }
  335. elseif ( file_exists( HABARI_PATH . '/user/themes/' . $theme ) ) {
  336. $path = HABARI_PATH . '/user/themes/' . $theme;
  337. }
  338. elseif ( file_exists( HABARI_PATH . '/3rdparty/themes/' . $theme ) ) {
  339. $url = Site::get_url( 'habari' ) . '/3rdparty/themes/' . $theme;
  340. }
  341. else {
  342. $path = HABARI_PATH . '/system/themes/' . $theme;
  343. }
  344. break;
  345. case 'admin_theme':
  346. $path = HABARI_PATH . '/system/admin';
  347. break;
  348. case 'vendor':
  349. $path = HABARI_PATH . '/system/vendor';
  350. break;
  351. }
  352. $path .= Utils::trail( $trail );
  353. $path = Plugins::filter( 'site_dir_' . $name, $path );
  354. return $path;
  355. }
  356. /**
  357. * out_url echos out a URL
  358. * @param string the URL to display
  359. * @param bool whether or not to include a trailing slash. Default: No
  360. */
  361. public static function out_url( $url, $trail = false )
  362. {
  363. echo Site::get_url( $url, $trail );
  364. }
  365. /**
  366. * out_path echos a URL path
  367. * @param string the URL path to display
  368. * @param bool whether or not to include a trailing slash. Default: No
  369. */
  370. public static function out_path( $path, $trail = false )
  371. {
  372. echo Site::get_path( $path, $trail );
  373. }
  374. /**
  375. * our_dir echos our a filesystem directory
  376. * @param string the filesystem directory to display
  377. * @param bool whether or not to include a trailing slash. Default: No
  378. */
  379. public static function out_dir( $dir, $trail = false )
  380. {
  381. echo Site::get_dir( $dir, $trail );
  382. }
  383. /*
  384. I'm unclear whether we need these. If so, they likely belong in a new method, since they're neither URLs, paths, nor directories.
  385. case 'config_type':
  386. self::get_config_path();
  387. $path= self::$config_type;
  388. break;
  389. case 'config_name':
  390. self::get_dir( 'config' );
  391. $path= self::$config_dir;
  392. break;
  393. */
  394. }
  395. ?>