PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/core/lhcore/lhsys.php

http://hppg.googlecode.com/
PHP | 385 lines | 293 code | 69 blank | 23 comment | 60 complexity | 3dccef11d26fe550caa7c217050941c9 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. class CSCacheAPC {
  3. static private $m_objMem = NULL;
  4. public $cacheEngine = null;
  5. public $cacheGlobalKey = null;
  6. public $cacheKeys = array(
  7. 'last_hits_version', // Last visited pages
  8. 'most_popular_version', // Most popular images, watched times
  9. 'top_rated', // Top rated images
  10. 'last_uploads', // Last uploaded images
  11. 'last_commented', // Last commented images
  12. 'last_rated', // Last rated images
  13. 'ratedrecent_version', // Recently rated images
  14. 'site_version', // Global site version
  15. 'color_images', // Global color pages version
  16. 'album_version', // Albums version key
  17. 'sphinx_cache_version', // Sphinx search cache version
  18. );
  19. public function increaseImageManipulationCache()
  20. {
  21. $this->increaseCacheVersion('last_hits_version',time(),600);
  22. $this->increaseCacheVersion('most_popular_version',time(),1500);
  23. $this->increaseCacheVersion('last_uploads');
  24. $this->increaseCacheVersion('top_rated');
  25. $this->increaseCacheVersion('last_commented');
  26. $this->increaseCacheVersion('last_rated');
  27. $this->increaseCacheVersion('ratedrecent_version');
  28. $this->increaseCacheVersion('sphinx_cache_version');
  29. $this->increaseCacheVersion('site_version');
  30. $this->increaseCacheVersion('color_images');
  31. $this->increaseCacheVersion('album_version');
  32. $this->increaseCacheVersion('popularrecent_version',time(),600);
  33. }
  34. function __construct() {
  35. $cacheEngineClassName = erConfigClassLhConfig::getInstance()->getSetting( 'cacheEngine', 'className' );
  36. $this->cacheGlobalKey = erConfigClassLhConfig::getInstance()->getSetting( 'cacheEngine', 'cache_global_key' );
  37. if ($cacheEngineClassName !== false)
  38. {
  39. $this->cacheEngine = new $cacheEngineClassName();
  40. }
  41. }
  42. function __destruct() {
  43. }
  44. static function getMem() {
  45. if (self::$m_objMem == NULL) {
  46. self::$m_objMem = new CSCacheAPC();
  47. }
  48. return self::$m_objMem;
  49. }
  50. function delete($key) {
  51. if (isset($GLOBALS[$key])) unset($GLOBALS[$key]);
  52. if ( $this->cacheEngine != null )
  53. {
  54. $this->cacheEngine->set($this->cacheGlobalKey.$key,false,0);
  55. }
  56. }
  57. function restore($key) {
  58. if (isset($GLOBALS[$key]) && $GLOBALS[$key] !== false) return $GLOBALS[$key];
  59. if ( $this->cacheEngine != null )
  60. {
  61. $GLOBALS[$key] = $this->cacheEngine->get($this->cacheGlobalKey.$key);
  62. } else {
  63. $GLOBALS[$key] = false;
  64. }
  65. return $GLOBALS[$key];
  66. }
  67. function getCacheVersion($cacheVariable, $valuedefault = 1, $ttl = 0)
  68. {
  69. if (isset($GLOBALS['CacheKeyVersion_'.$cacheVariable])) return $GLOBALS['CacheKeyVersion_'.$cacheVariable];
  70. if ( $this->cacheEngine != null )
  71. {
  72. if (($version = $this->cacheEngine->get($this->cacheGlobalKey.$cacheVariable)) == false){
  73. $version = $valuedefault;
  74. $this->cacheEngine->set($this->cacheGlobalKey.$cacheVariable,$version,0,$ttl);
  75. $GLOBALS['CacheKeyVersion_'.$cacheVariable] = $valuedefault;
  76. } else $GLOBALS['CacheKeyVersion_'.$cacheVariable] = $version;
  77. } else {
  78. $version = $valuedefault;
  79. $GLOBALS['CacheKeyVersion_'.$cacheVariable] = $valuedefault;
  80. }
  81. return $version;
  82. }
  83. function increaseCacheVersion($cacheVariable, $valuedefault = 1, $ttl = 0)
  84. {
  85. if ( $this->cacheEngine != null )
  86. {
  87. if (($version = $this->cacheEngine->get($this->cacheGlobalKey.$cacheVariable)) == false) {
  88. $this->cacheEngine->set($this->cacheGlobalKey.$cacheVariable,$valuedefault,0,$ttl);
  89. $GLOBALS['CacheKeyVersion_'.$cacheVariable] = $valuedefault;
  90. } else {$this->cacheEngine->increment($this->cacheGlobalKey.$cacheVariable,$version+1);$GLOBALS['CacheKeyVersion_'.$cacheVariable] = $version+1;}
  91. } else {
  92. $GLOBALS['CacheKeyVersion_'.$cacheVariable] = $valuedefault;
  93. }
  94. }
  95. function store($key, $value, $ttl = 720000) {
  96. if ( $this->cacheEngine != null )
  97. {
  98. $GLOBALS[$key] = $value;
  99. $this->cacheEngine->set($this->cacheGlobalKey.$key,$value,0,$ttl);
  100. } else {
  101. $GLOBALS[$key] = $value;
  102. }
  103. }
  104. }
  105. class erLhcoreClassSystem{
  106. public static function instance()
  107. {
  108. if ( is_null( self::$instance ) )
  109. {
  110. self::$instance = new erLhcoreClassSystem();
  111. }
  112. return self::$instance;
  113. }
  114. static function init()
  115. {
  116. $index = "index.php";
  117. $def_index = '';
  118. $instance = erLhcoreClassSystem::instance();
  119. $isCGI = (stristr(php_sapi_name(),'cgi') !== false);
  120. $force_VirtualHost = false;
  121. $phpSelf = $_SERVER['PHP_SELF'];
  122. // Find out, where our files are.
  123. if ( preg_match( "!(.*/)$index$!", $_SERVER['SCRIPT_FILENAME'], $regs ) )
  124. {
  125. $siteDir = $regs[1];
  126. $index = "/$index";
  127. }
  128. elseif ( preg_match( "!(.*/)$index/?!", $phpSelf, $regs ) )
  129. {
  130. // Some people using CGI have their $_SERVER['SCRIPT_FILENAME'] not right... so we are trying this.
  131. $siteDir = $_SERVER['DOCUMENT_ROOT'] . $regs[1];
  132. $index = "/$index";
  133. }
  134. else
  135. {
  136. // Fallback... doesn't work with virtual-hosts, but better than nothing
  137. $siteDir = "./";
  138. $index = "/$index";
  139. }
  140. if ( $isCGI and !$force_VirtualHost )
  141. {
  142. $index .= '?';
  143. }
  144. // Setting the right include_path
  145. $includePath = ini_get( "include_path" );
  146. if ( trim( $includePath ) != "" )
  147. {
  148. $includePath = $includePath . /*$instance->envSeparator()*/'/'. $siteDir;
  149. }
  150. else
  151. {
  152. $includePath = $siteDir;
  153. }
  154. ini_set( "include_path", $includePath );
  155. $scriptName = $_SERVER['SCRIPT_NAME'];
  156. // Get the webdir.
  157. $wwwDir = "";
  158. if ( $force_VirtualHost )
  159. {
  160. $wwwDir = "";
  161. }
  162. else
  163. {
  164. if ( preg_match( "!(.*)$index$!", $scriptName, $regs ) )
  165. $wwwDir = $regs[1];
  166. if ( preg_match( "!(.*)$index$!", $phpSelf, $regs ) )
  167. $wwwDir = $regs[1];
  168. }
  169. if ( ! $isCGI || $force_VirtualHost )
  170. {
  171. $requestURI = $_SERVER['REQUEST_URI'];
  172. }
  173. else
  174. {
  175. $requestURI = $_SERVER['QUERY_STRING'];
  176. /* take out PHPSESSID, if url-encoded */
  177. if ( preg_match( "/(.*)&PHPSESSID=[^&]+(.*)/", $requestURI, $matches ) )
  178. {
  179. $requestURI = $matches[1].$matches[2];
  180. }
  181. }
  182. // Fallback... Finding the paths above failed, so $_SERVER['PHP_SELF'] is not set right.
  183. if ( $siteDir == "./" )
  184. $phpSelf = $requestURI;
  185. if ( ! $isCGI )
  186. {
  187. $index_reg = str_replace( ".", "\\.", $index );
  188. // Trick: Rewrite setup doesn't have index.php in $_SERVER['PHP_SELF'], so we don't want an $index
  189. if ( !preg_match( "!.*$index_reg.*!", $phpSelf ) || $force_VirtualHost )
  190. {
  191. $index = "";
  192. }
  193. else
  194. {
  195. // Get the right $_SERVER['REQUEST_URI'], when using nVH setup.
  196. if ( preg_match( "!^$wwwDir$index(.*)!", $phpSelf, $req ) )
  197. {
  198. if ( !$req[1] )
  199. {
  200. if ( $phpSelf != "$wwwDir$index" and preg_match( "!^$wwwDir(.*)!", $requestURI, $req ) )
  201. {
  202. $requestURI = $req[1];
  203. $index = '';
  204. }
  205. elseif ( $phpSelf == "$wwwDir$index" and
  206. ( preg_match( "!^$wwwDir$index(.*)!", $requestURI, $req ) or preg_match( "!^$wwwDir(.*)!", $requestURI, $req ) ) )
  207. {
  208. $requestURI = $req[1];
  209. }
  210. }
  211. else
  212. {
  213. $requestURI = $req[1];
  214. }
  215. }
  216. }
  217. }
  218. if ( $isCGI and $force_VirtualHost )
  219. $index = '';
  220. // Remove url parameters
  221. if ( $isCGI and !$force_VirtualHost )
  222. {
  223. $pattern = "!(\/[^&]+)!";
  224. }
  225. else
  226. {
  227. $pattern = "!([^?]+)!";
  228. }
  229. if ( preg_match( $pattern, $requestURI, $regs ) )
  230. {
  231. $requestURI = $regs[1];
  232. }
  233. // Remove internal links
  234. if ( preg_match( "!([^#]+)!", $requestURI, $regs ) )
  235. {
  236. $requestURI = $regs[1];
  237. }
  238. if ( !$isCGI )
  239. {
  240. $currentPath = substr( $_SERVER['SCRIPT_FILENAME'] , 0, -strlen( 'index.php' ) );
  241. if ( strpos( $currentPath, $_SERVER['DOCUMENT_ROOT'] ) === 0 )
  242. {
  243. $prependRequest = substr( $currentPath, strlen( $_SERVER['DOCUMENT_ROOT'] ) );
  244. if ( strpos( $requestURI, $prependRequest ) === 0 )
  245. {
  246. $requestURI = substr( $requestURI, strlen( $prependRequest ) - 1 );
  247. $wwwDir = substr( $prependRequest, 0, -1 );
  248. }
  249. }
  250. }
  251. if ( erConfigClassLhConfig::getInstance()->getSetting( 'site', 'classcompile' ) == true ) {
  252. $instance->compileClass();
  253. }
  254. $instance->SiteDir = $siteDir;
  255. $instance->WWWDir = $wwwDir;
  256. $instance->WWWDirLang = '';
  257. $instance->IndexFile = erConfigClassLhConfig::getInstance()->getSetting( 'site', 'force_virtual_host' )== false ? $index : '';
  258. $instance->RequestURI = $requestURI;
  259. }
  260. function compileClass()
  261. {
  262. $groups = include('lib/autoloads/lhcompile_group.php');
  263. $cache = CSCacheAPC::getMem();
  264. $versionSite = $cache->getCacheVersion('site_version');
  265. foreach ($groups as $group => $classes) {
  266. if ( ($cacheClasses = $cache->restore('ClassCompileCache_'.$group.'_version_'.$versionSite) ) !== false)
  267. {
  268. include($cacheClasses);
  269. return ;
  270. }
  271. $contentFiles = '';
  272. $ret = array();
  273. foreach ($classes as $class => $path) {
  274. $refl = new ReflectionClass($class);
  275. $file = $refl->getFileName();
  276. if ($file != '')
  277. {
  278. $lines = file($file);
  279. $start = $refl->getStartLine() - 1;
  280. $end = $refl->getEndLine();
  281. $ret = array_merge($ret, array_slice($lines, $start, ($end - $start)));
  282. }
  283. }
  284. file_put_contents('cache/compiledclasses/'.$group.'_class.php','<?php '.implode('',$ret));
  285. $cache->store('ClassCompileCache_'.$group.'_version_'.$versionSite, 'cache/compiledclasses/'.$group.'_class.php');
  286. }
  287. // print_r($groups);
  288. // echo "Compiling class";
  289. }
  290. function wwwDir()
  291. {
  292. return $this->WWWDir;
  293. }
  294. /// The path to where all the code resides
  295. public $SiteDir;
  296. /// The access path of the current site view
  297. /// The relative directory path of the vhless setup
  298. public $WWWDir;
  299. // The www dir used in links formating
  300. public $WWWDirLang;
  301. /// The filepath for the index
  302. public $IndexFile;
  303. /// The uri which is used for parsing module/view information from, may differ from $_SERVER['REQUEST_URI']
  304. public $RequestURI;
  305. /// The type of filesystem, is either win32 or unix. This often used to determine os specific paths.
  306. /// Current language
  307. public $Language;
  308. // Content language
  309. public $ContentLanguage;
  310. /// Theme site
  311. public $ThemeSite;
  312. public $SiteAccess;
  313. public $MobileDevice = false;
  314. private static $instance = null;
  315. }
  316. ?>