00001 <?php
00007 namespace Habari;
00014 class Site
00015 {
00022 const CONFIG_LOCAL = 0;
00023 const CONFIG_SUBDIR = 1;
00024 const CONFIG_SUBDOMAIN = 2;
00025
00033 static $config_path;
00034 static $config_dir;
00035 static $config_type = Site::CONFIG_LOCAL;
00036 static $scriptname;
00037 static $habari_url;
00038 static $config_urldir;
00039
00044 private function __construct()
00045 {
00046 }
00047
00053 public static function script_name()
00054 {
00055 switch ( true ) {
00056 case isset( self::$scriptname ):
00057 break;
00058 case isset( $_SERVER['SCRIPT_NAME'] ):
00059 self::$scriptname = $_SERVER['SCRIPT_NAME'];
00060 break;
00061 case isset( $_SERVER['PHP_SELF'] ):
00062 self::$scriptname = $_SERVER['PHP_SELF'];
00063 break;
00064 default:
00065 Error::raise( _t( 'Could not determine script name.' ) );
00066 die();
00067 }
00068 return self::$scriptname;
00069 }
00070
00082 public static function is( $what )
00083 {
00084 switch ( strtolower( $what ) ) {
00085 case 'main':
00086 case 'primary':
00087 if ( Site::$config_type == Site::CONFIG_LOCAL ) {
00088 return true;
00089 }
00090 else {
00091 return false;
00092 }
00093 break;
00094 case 'multi':
00095 if ( Site::$config_type != Site::CONFIG_LOCAL ) {
00096 return true;
00097 }
00098 else {
00099 return false;
00100 }
00101 break;
00102 }
00103 }
00104
00132 public static function get_url( $name, $trail = false )
00133 {
00134 $url = '';
00135
00136 switch ( strtolower( $name ) ) {
00137 case 'host':
00138 $protocol = 'http';
00139
00140
00141
00142 $port = 80;
00143 $port = Config::get( 'custom_http_port', isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : $port );
00144 $portpart = '';
00145 $host = Site::get_url( 'hostname' );
00146
00147 if ( ( $port != 80 ) && ( $port != 443 ) && ( MultiByte::substr( $host, MultiByte::strlen( $host ) - strlen( $port ) ) != $port ) ) {
00148 $portpart = ':' . $port;
00149 }
00150 if ( isset( $_SERVER['HTTPS'] ) && !empty( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] != 'off' ) {
00151 $protocol = 'https';
00152 }
00153 $url = $protocol . '://' . $host . $portpart;
00154 break;
00155 case 'habari':
00156 if(self::$habari_url == null) {
00157 self::$habari_url = Site::get_url( 'host' );
00158
00159 $path = trim( dirname( Site::script_name() ), '/\\' );
00160 if ( '' != $path ) {
00161 self::$habari_url .= '/' . $path;
00162 }
00163 }
00164 $url = self::$habari_url;
00165 break;
00166 case 'site':
00167 $url = Site::get_url( 'habari' );
00168
00169 if( self::$config_type == Site::CONFIG_SUBDIR ) {
00170 $url = Utils::end_in_slash($url) . self::$config_urldir;
00171 }
00172 break;
00173 case 'user':
00174 $url = Site::get_url( 'habari', true ) . Site::get_path( 'user' );
00175 break;
00176 case 'theme':
00177 $theme = Themes::get_theme_dir();
00178 if ( file_exists( Site::get_dir( 'config' ) . '/themes/' . $theme ) ) {
00179 $url = Site::get_url( 'user' ) . '/themes/' . $theme;
00180 }
00181 elseif ( file_exists( HABARI_PATH . '/user/themes/' . $theme ) ) {
00182 $url = Site::get_url( 'habari' ) . '/user/themes/' . $theme;
00183 }
00184 elseif ( file_exists( HABARI_PATH . '/3rdparty/themes/' . $theme ) ) {
00185 $url = Site::get_url( 'habari' ) . '/3rdparty/themes/' . $theme;
00186 }
00187 else {
00188 $url = Site::get_url( 'habari' ) . '/system/themes/' . $theme;
00189 }
00190 break;
00191 case 'admin':
00192 $url = Site::get_url( 'site' ) . '/admin';
00193 break;
00194 case 'admin_theme':
00195 $url = Site::get_url( 'habari' ) . '/system/admin';
00196 break;
00197 case 'login':
00198 $url = Site::get_url( 'site' ) . '/auth/login';
00199 break;
00200 case 'logout':
00201 $url = Site::get_url( 'site' ) . '/auth/logout';
00202 break;
00203 case 'system':
00204 $url = Site::get_url( 'habari' ) . '/system';
00205 break;
00206 case 'vendor':
00207 case 'scripts':
00208 $url = Site::get_url( 'system' ) . '/vendor';
00209 break;
00210 case '3rdparty':
00211
00212
00213 if ( file_exists( HABARI_PATH . '/3rdparty' ) ) {
00214 $url = Site::get_url( 'habari' ) . '/3rdparty';
00215 }
00216 else {
00217 $url = Site::get_url( 'vendor' );
00218 }
00219 break;
00220 case 'hostname':
00221
00222 $url = ( $_SERVER['SERVER_PROTOCOL'] == 'HTTP/1.0' || !isset( $_SERVER['HTTP_HOST'] ) ) ? $_SERVER['SERVER_NAME'] : $_SERVER['HTTP_HOST'];
00223 break;
00224 }
00225 $url .= Utils::trail( $trail );
00226 $url = Plugins::filter( 'site_url_' . $name, $url );
00227 return $url;
00228 }
00229
00244 public static function get_path( $name, $trail = false )
00245 {
00246 $path = '';
00247 switch ( strtolower( $name ) ) {
00248 case 'base':
00249 case 'habari':
00250 $path = rtrim( dirname( Site::script_name() ), '/\\' );
00251 if(self::$config_urldir != '') {
00252 $path .= '/' . self::$config_urldir;
00253 }
00254 break;
00255 case 'user':
00256 if ( Site::is( 'main' ) ) {
00257 $path = 'user';
00258 }
00259 else {
00260 $path = ltrim( str_replace( HABARI_PATH, '', Site::get_dir( 'config' ) ), '/' );
00261 }
00262 break;
00263 case 'theme':
00264 $theme = Themes::get_theme_dir();
00265 if ( file_exists( Site::get_dir( 'config' ) . '/themes/' . $theme ) ) {
00266 $path = Site::get_path( 'user' ) . '/themes/' . $theme;
00267 }
00268 elseif ( file_exists( HABARI_PATH . '/3rdparty/themes/' . $theme ) ) {
00269 $path = Site::get_path( 'habari' ) . '/3rdparty/themes/' . $theme;
00270 }
00271 else {
00272 $path = Site::get_path( 'base' ) . '/user/themes/' . $theme;
00273 }
00274 break;
00275 }
00276 $path .= Utils::trail( $trail );
00277
00278
00279 $path = str_replace( '//', '/', $path );
00280 $path = Plugins::filter( 'site_path_' . $name, $path );
00281 return $path;
00282 }
00283
00296 public static function get_dir( $name, $trail = false )
00297 {
00298 $path = '';
00299
00300 switch ( strtolower( $name ) ) {
00301 case 'config_file':
00302 $path = Site::get_dir( 'config' ) . '/config.php';
00303 break;
00304 case 'config':
00305 if ( self::$config_path ) {
00306 return self::$config_path;
00307 }
00308
00309 self::$config_path = HABARI_PATH;
00310
00311 $config_dirs = preg_replace( '/^' . preg_quote( HABARI_PATH, '/' ) . '\/user\/sites\/(.*)/', '$1', Utils::glob( HABARI_PATH . '/user/sites/*', GLOB_ONLYDIR ) );
00312
00313 if ( empty( $config_dirs ) ) {
00314 return self::$config_path;
00315 }
00316
00317
00318 $server = InputFilter::parse_url( Site::get_url( 'habari' ) );
00319 $request = array();
00320 if ( isset( $server['port'] ) && $server['port'] != '' && $server['port'] != '80' ) {
00321 $request[] = $server['port'];
00322 }
00323 $request = array_merge($request, explode('.', $server['host']));
00324
00325
00326 $basesegments = count($request);
00327 if(!empty($server['path'])) {
00328 $coresubdir = explode( '/', trim( $server['path'], '/' ) );
00329 $basesegments += count($coresubdir);
00330 }
00331 $request = array_merge($request, explode( '/', trim( $_SERVER['REQUEST_URI'], '/' ) ) );
00332
00333 $x = 0;
00334 do {
00335 $match = implode('.', $request);
00336 if ( in_array( $match, $config_dirs ) ) {
00337 self::$config_dir = $match;
00338 self::$config_path = HABARI_PATH . '/user/sites/' . self::$config_dir;
00339 self::$config_type = ( $basesegments < count($request) ) ? Site::CONFIG_SUBDIR : Site::CONFIG_SUBDOMAIN;
00340 self::$config_urldir = implode('/', array_slice($request, $basesegments));
00341 break;
00342 }
00343
00344 array_pop($request);
00345 $x--;
00346 if ( $x < -10 ) {
00347 echo $x;
00348 var_dump($request);
00349 die('too many ');
00350 }
00351 } while ( count($request) > 0 );
00352 $path = self::$config_path;
00353 break;
00354 case 'user':
00355 if ( Site::get_dir( 'config' ) == HABARI_PATH ) {
00356 $path = HABARI_PATH . '/user';
00357 }
00358 else {
00359 $path = Site::get_dir( 'config' );
00360 }
00361 break;
00362 case 'theme':
00363 $theme = Themes::get_theme_dir();
00364 if ( file_exists( Site::get_dir( 'config' ) . '/themes/' . $theme ) ) {
00365 $path = Site::get_dir( 'user' ) . '/themes/' . $theme;
00366 }
00367 elseif ( file_exists( HABARI_PATH . '/user/themes/' . $theme ) ) {
00368 $path = HABARI_PATH . '/user/themes/' . $theme;
00369 }
00370 elseif ( file_exists( HABARI_PATH . '/3rdparty/themes/' . $theme ) ) {
00371 $path = Site::get_dir( 'habari' ) . '/3rdparty/themes/' . $theme;
00372 }
00373 else {
00374 $path = HABARI_PATH . '/system/themes/' . $theme;
00375 }
00376 break;
00377 case 'admin_theme':
00378 $path = HABARI_PATH . '/system/admin';
00379 break;
00380 case 'vendor':
00381 $path = HABARI_PATH . '/system/vendor';
00382 break;
00383 }
00384 $path .= Utils::trail( $trail );
00385 $path = Plugins::filter( 'site_dir_' . $name, $path );
00386 return $path;
00387 }
00388
00394 public static function out_url( $url, $trail = false )
00395 {
00396 echo Site::get_url( $url, $trail );
00397 }
00398
00404 public static function out_path( $path, $trail = false )
00405 {
00406 echo Site::get_path( $path, $trail );
00407 }
00408
00414 public static function out_dir( $dir, $trail = false )
00415 {
00416 echo Site::get_dir( $dir, $trail );
00417 }
00418 }
00419
00420 ?>