PageRenderTime 58ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/framework/yiilite.php

http://yii-doctrine2.googlecode.com/
PHP | 2205 lines | 2162 code | 2 blank | 41 comment | 312 complexity | b0022f742c6ca50e94f70d9833b71a00 MD5 | raw file
Possible License(s): BSD-2-Clause, LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Yii bootstrap file.
  4. *
  5. * This file is automatically generated using 'build lite' command.
  6. * It is the result of merging commonly used Yii class files with
  7. * comments and trace statements removed away.
  8. *
  9. * By using this file instead of yii.php, an Yii application may
  10. * improve performance due to the reduction of PHP parsing time.
  11. * The performance improvement is especially obvious when PHP APC extension
  12. * is enabled.
  13. *
  14. * DO NOT modify this file manually.
  15. *
  16. * @author Qiang Xue <qiang.xue@gmail.com>
  17. * @link http://www.yiiframework.com/
  18. * @copyright Copyright &copy; 2008-2011 Yii Software LLC
  19. * @license http://www.yiiframework.com/license/
  20. * @version $Id: yiilite.php 3134 2011-03-27 01:39:25Z qiang.xue $
  21. * @since 1.0
  22. */
  23. defined('YII_BEGIN_TIME') or define('YII_BEGIN_TIME',microtime(true));
  24. defined('YII_DEBUG') or define('YII_DEBUG',false);
  25. defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',0);
  26. defined('YII_ENABLE_EXCEPTION_HANDLER') or define('YII_ENABLE_EXCEPTION_HANDLER',true);
  27. defined('YII_ENABLE_ERROR_HANDLER') or define('YII_ENABLE_ERROR_HANDLER',true);
  28. defined('YII_PATH') or define('YII_PATH',dirname(__FILE__));
  29. defined('YII_ZII_PATH') or define('YII_ZII_PATH',YII_PATH.DIRECTORY_SEPARATOR.'zii');
  30. class YiiBase
  31. {
  32. public static $classMap=array();
  33. private static $_aliases=array('system'=>YII_PATH,'zii'=>YII_ZII_PATH); // alias => path
  34. private static $_imports=array(); // alias => class name or directory
  35. private static $_includePaths; // list of include paths
  36. private static $_app;
  37. private static $_logger;
  38. public static function getVersion()
  39. {
  40. return '1.1.7';
  41. }
  42. public static function createWebApplication($config=null)
  43. {
  44. return self::createApplication('CWebApplication',$config);
  45. }
  46. public static function createConsoleApplication($config=null)
  47. {
  48. return self::createApplication('CConsoleApplication',$config);
  49. }
  50. public static function createApplication($class,$config=null)
  51. {
  52. return new $class($config);
  53. }
  54. public static function app()
  55. {
  56. return self::$_app;
  57. }
  58. public static function setApplication($app)
  59. {
  60. if(self::$_app===null || $app===null)
  61. self::$_app=$app;
  62. else
  63. throw new CException(Yii::t('yii','Yii application can only be created once.'));
  64. }
  65. public static function getFrameworkPath()
  66. {
  67. return YII_PATH;
  68. }
  69. public static function createComponent($config)
  70. {
  71. if(is_string($config))
  72. {
  73. $type=$config;
  74. $config=array();
  75. }
  76. else if(isset($config['class']))
  77. {
  78. $type=$config['class'];
  79. unset($config['class']);
  80. }
  81. else
  82. throw new CException(Yii::t('yii','Object configuration must be an array containing a "class" element.'));
  83. if(!class_exists($type,false))
  84. $type=Yii::import($type,true);
  85. if(($n=func_num_args())>1)
  86. {
  87. $args=func_get_args();
  88. if($n===2)
  89. $object=new $type($args[1]);
  90. else if($n===3)
  91. $object=new $type($args[1],$args[2]);
  92. else if($n===4)
  93. $object=new $type($args[1],$args[2],$args[3]);
  94. else
  95. {
  96. unset($args[0]);
  97. $class=new ReflectionClass($type);
  98. // Note: ReflectionClass::newInstanceArgs() is available for PHP 5.1.3+
  99. // $object=$class->newInstanceArgs($args);
  100. $object=call_user_func_array(array($class,'newInstance'),$args);
  101. }
  102. }
  103. else
  104. $object=new $type;
  105. foreach($config as $key=>$value)
  106. $object->$key=$value;
  107. return $object;
  108. }
  109. public static function import($alias,$forceInclude=false)
  110. {
  111. if(isset(self::$_imports[$alias])) // previously imported
  112. return self::$_imports[$alias];
  113. if(class_exists($alias,false) || interface_exists($alias,false))
  114. return self::$_imports[$alias]=$alias;
  115. if(($pos=strrpos($alias,'\\'))!==false) // a class name in PHP 5.3 namespace format
  116. {
  117. $namespace=str_replace('\\','.',ltrim(substr($alias,0,$pos),'\\'));
  118. if(($path=self::getPathOfAlias($namespace))!==false)
  119. {
  120. $classFile=$path.DIRECTORY_SEPARATOR.substr($alias,$pos+1).'.php';
  121. if($forceInclude)
  122. {
  123. if(is_file($classFile))
  124. require($classFile);
  125. else
  126. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing PHP file.',array('{alias}'=>$alias)));
  127. self::$_imports[$alias]=$alias;
  128. }
  129. else
  130. self::$classMap[$alias]=$classFile;
  131. return $alias;
  132. }
  133. else
  134. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory.',
  135. array('{alias}'=>$namespace)));
  136. }
  137. if(($pos=strrpos($alias,'.'))===false) // a simple class name
  138. {
  139. if($forceInclude && self::autoload($alias))
  140. self::$_imports[$alias]=$alias;
  141. return $alias;
  142. }
  143. $className=(string)substr($alias,$pos+1);
  144. $isClass=$className!=='*';
  145. if($isClass && (class_exists($className,false) || interface_exists($className,false)))
  146. return self::$_imports[$alias]=$className;
  147. if(($path=self::getPathOfAlias($alias))!==false)
  148. {
  149. if($isClass)
  150. {
  151. if($forceInclude)
  152. {
  153. if(is_file($path.'.php'))
  154. require($path.'.php');
  155. else
  156. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing PHP file.',array('{alias}'=>$alias)));
  157. self::$_imports[$alias]=$className;
  158. }
  159. else
  160. self::$classMap[$className]=$path.'.php';
  161. return $className;
  162. }
  163. else // a directory
  164. {
  165. if(self::$_includePaths===null)
  166. {
  167. self::$_includePaths=array_unique(explode(PATH_SEPARATOR,get_include_path()));
  168. if(($pos=array_search('.',self::$_includePaths,true))!==false)
  169. unset(self::$_includePaths[$pos]);
  170. }
  171. array_unshift(self::$_includePaths,$path);
  172. if(set_include_path('.'.PATH_SEPARATOR.implode(PATH_SEPARATOR,self::$_includePaths))===false)
  173. throw new CException(Yii::t('yii','Unable to import "{alias}". Please check your server configuration to make sure you are allowed to change PHP include_path.',array('{alias}'=>$alias)));
  174. return self::$_imports[$alias]=$path;
  175. }
  176. }
  177. else
  178. throw new CException(Yii::t('yii','Alias "{alias}" is invalid. Make sure it points to an existing directory or file.',
  179. array('{alias}'=>$alias)));
  180. }
  181. public static function getPathOfAlias($alias)
  182. {
  183. if(isset(self::$_aliases[$alias]))
  184. return self::$_aliases[$alias];
  185. else if(($pos=strpos($alias,'.'))!==false)
  186. {
  187. $rootAlias=substr($alias,0,$pos);
  188. if(isset(self::$_aliases[$rootAlias]))
  189. return self::$_aliases[$alias]=rtrim(self::$_aliases[$rootAlias].DIRECTORY_SEPARATOR.str_replace('.',DIRECTORY_SEPARATOR,substr($alias,$pos+1)),'*'.DIRECTORY_SEPARATOR);
  190. else if(self::$_app instanceof CWebApplication)
  191. {
  192. if(self::$_app->findModule($rootAlias)!==null)
  193. return self::getPathOfAlias($alias);
  194. }
  195. }
  196. return false;
  197. }
  198. public static function setPathOfAlias($alias,$path)
  199. {
  200. if(empty($path))
  201. unset(self::$_aliases[$alias]);
  202. else
  203. self::$_aliases[$alias]=rtrim($path,'\\/');
  204. }
  205. public static function autoload($className)
  206. {
  207. // use include so that the error PHP file may appear
  208. if(isset(self::$_coreClasses[$className]))
  209. include(YII_PATH.self::$_coreClasses[$className]);
  210. else if(isset(self::$classMap[$className]))
  211. include(self::$classMap[$className]);
  212. else
  213. {
  214. if(strpos($className,'\\')===false)
  215. include($className.'.php');
  216. else // class name with namespace in PHP 5.3
  217. {
  218. $namespace=str_replace('\\','.',ltrim($className,'\\'));
  219. if(($path=self::getPathOfAlias($namespace))!==false)
  220. include($path.'.php');
  221. else
  222. return false;
  223. }
  224. return class_exists($className,false) || interface_exists($className,false);
  225. }
  226. return true;
  227. }
  228. public static function trace($msg,$category='application')
  229. {
  230. if(YII_DEBUG)
  231. self::log($msg,CLogger::LEVEL_TRACE,$category);
  232. }
  233. public static function log($msg,$level=CLogger::LEVEL_INFO,$category='application')
  234. {
  235. if(self::$_logger===null)
  236. self::$_logger=new CLogger;
  237. if(YII_DEBUG && YII_TRACE_LEVEL>0 && $level!==CLogger::LEVEL_PROFILE)
  238. {
  239. $traces=debug_backtrace();
  240. $count=0;
  241. foreach($traces as $trace)
  242. {
  243. if(isset($trace['file'],$trace['line']) && strpos($trace['file'],YII_PATH)!==0)
  244. {
  245. $msg.="\nin ".$trace['file'].' ('.$trace['line'].')';
  246. if(++$count>=YII_TRACE_LEVEL)
  247. break;
  248. }
  249. }
  250. }
  251. self::$_logger->log($msg,$level,$category);
  252. }
  253. public static function beginProfile($token,$category='application')
  254. {
  255. self::log('begin:'.$token,CLogger::LEVEL_PROFILE,$category);
  256. }
  257. public static function endProfile($token,$category='application')
  258. {
  259. self::log('end:'.$token,CLogger::LEVEL_PROFILE,$category);
  260. }
  261. public static function getLogger()
  262. {
  263. if(self::$_logger!==null)
  264. return self::$_logger;
  265. else
  266. return self::$_logger=new CLogger;
  267. }
  268. public static function powered()
  269. {
  270. return 'Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a>.';
  271. }
  272. public static function t($category,$message,$params=array(),$source=null,$language=null)
  273. {
  274. if(self::$_app!==null)
  275. {
  276. if($source===null)
  277. $source=($category==='yii'||$category==='zii')?'coreMessages':'messages';
  278. if(($source=self::$_app->getComponent($source))!==null)
  279. $message=$source->translate($category,$message,$language);
  280. }
  281. if($params===array())
  282. return $message;
  283. if(!is_array($params))
  284. $params=array($params);
  285. if(isset($params[0])) // number choice
  286. {
  287. if(strpos($message,'|')!==false)
  288. {
  289. if(strpos($message,'#')===false)
  290. {
  291. $chunks=explode('|',$message);
  292. $expressions=self::$_app->getLocale($language)->getPluralRules();
  293. if($n=min(count($chunks),count($expressions)))
  294. {
  295. for($i=0;$i<$n;$i++)
  296. $chunks[$i]=$expressions[$i].'#'.$chunks[$i];
  297. $message=implode('|',$chunks);
  298. }
  299. }
  300. $message=CChoiceFormat::format($message,$params[0]);
  301. }
  302. if(!isset($params['{n}']))
  303. $params['{n}']=$params[0];
  304. unset($params[0]);
  305. }
  306. return $params!==array() ? strtr($message,$params) : $message;
  307. }
  308. public static function registerAutoloader($callback)
  309. {
  310. spl_autoload_unregister(array('YiiBase','autoload'));
  311. spl_autoload_register($callback);
  312. spl_autoload_register(array('YiiBase','autoload'));
  313. }
  314. private static $_coreClasses=array(
  315. 'CApplication' => '/base/CApplication.php',
  316. 'CApplicationComponent' => '/base/CApplicationComponent.php',
  317. 'CBehavior' => '/base/CBehavior.php',
  318. 'CComponent' => '/base/CComponent.php',
  319. 'CErrorEvent' => '/base/CErrorEvent.php',
  320. 'CErrorHandler' => '/base/CErrorHandler.php',
  321. 'CException' => '/base/CException.php',
  322. 'CExceptionEvent' => '/base/CExceptionEvent.php',
  323. 'CHttpException' => '/base/CHttpException.php',
  324. 'CModel' => '/base/CModel.php',
  325. 'CModelBehavior' => '/base/CModelBehavior.php',
  326. 'CModelEvent' => '/base/CModelEvent.php',
  327. 'CModule' => '/base/CModule.php',
  328. 'CSecurityManager' => '/base/CSecurityManager.php',
  329. 'CStatePersister' => '/base/CStatePersister.php',
  330. 'CApcCache' => '/caching/CApcCache.php',
  331. 'CCache' => '/caching/CCache.php',
  332. 'CDbCache' => '/caching/CDbCache.php',
  333. 'CDummyCache' => '/caching/CDummyCache.php',
  334. 'CEAcceleratorCache' => '/caching/CEAcceleratorCache.php',
  335. 'CFileCache' => '/caching/CFileCache.php',
  336. 'CMemCache' => '/caching/CMemCache.php',
  337. 'CWinCache' => '/caching/CWinCache.php',
  338. 'CXCache' => '/caching/CXCache.php',
  339. 'CZendDataCache' => '/caching/CZendDataCache.php',
  340. 'CCacheDependency' => '/caching/dependencies/CCacheDependency.php',
  341. 'CChainedCacheDependency' => '/caching/dependencies/CChainedCacheDependency.php',
  342. 'CDbCacheDependency' => '/caching/dependencies/CDbCacheDependency.php',
  343. 'CDirectoryCacheDependency' => '/caching/dependencies/CDirectoryCacheDependency.php',
  344. 'CExpressionDependency' => '/caching/dependencies/CExpressionDependency.php',
  345. 'CFileCacheDependency' => '/caching/dependencies/CFileCacheDependency.php',
  346. 'CGlobalStateCacheDependency' => '/caching/dependencies/CGlobalStateCacheDependency.php',
  347. 'CAttributeCollection' => '/collections/CAttributeCollection.php',
  348. 'CConfiguration' => '/collections/CConfiguration.php',
  349. 'CList' => '/collections/CList.php',
  350. 'CListIterator' => '/collections/CListIterator.php',
  351. 'CMap' => '/collections/CMap.php',
  352. 'CMapIterator' => '/collections/CMapIterator.php',
  353. 'CQueue' => '/collections/CQueue.php',
  354. 'CQueueIterator' => '/collections/CQueueIterator.php',
  355. 'CStack' => '/collections/CStack.php',
  356. 'CStackIterator' => '/collections/CStackIterator.php',
  357. 'CTypedList' => '/collections/CTypedList.php',
  358. 'CTypedMap' => '/collections/CTypedMap.php',
  359. 'CConsoleApplication' => '/console/CConsoleApplication.php',
  360. 'CConsoleCommand' => '/console/CConsoleCommand.php',
  361. 'CConsoleCommandRunner' => '/console/CConsoleCommandRunner.php',
  362. 'CHelpCommand' => '/console/CHelpCommand.php',
  363. 'CDbCommand' => '/db/CDbCommand.php',
  364. 'CDbConnection' => '/db/CDbConnection.php',
  365. 'CDbDataReader' => '/db/CDbDataReader.php',
  366. 'CDbException' => '/db/CDbException.php',
  367. 'CDbMigration' => '/db/CDbMigration.php',
  368. 'CDbTransaction' => '/db/CDbTransaction.php',
  369. 'CActiveFinder' => '/db/ar/CActiveFinder.php',
  370. 'CActiveRecord' => '/db/ar/CActiveRecord.php',
  371. 'CActiveRecordBehavior' => '/db/ar/CActiveRecordBehavior.php',
  372. 'CDbColumnSchema' => '/db/schema/CDbColumnSchema.php',
  373. 'CDbCommandBuilder' => '/db/schema/CDbCommandBuilder.php',
  374. 'CDbCriteria' => '/db/schema/CDbCriteria.php',
  375. 'CDbExpression' => '/db/schema/CDbExpression.php',
  376. 'CDbSchema' => '/db/schema/CDbSchema.php',
  377. 'CDbTableSchema' => '/db/schema/CDbTableSchema.php',
  378. 'CMssqlColumnSchema' => '/db/schema/mssql/CMssqlColumnSchema.php',
  379. 'CMssqlCommandBuilder' => '/db/schema/mssql/CMssqlCommandBuilder.php',
  380. 'CMssqlPdoAdapter' => '/db/schema/mssql/CMssqlPdoAdapter.php',
  381. 'CMssqlSchema' => '/db/schema/mssql/CMssqlSchema.php',
  382. 'CMssqlTableSchema' => '/db/schema/mssql/CMssqlTableSchema.php',
  383. 'CMysqlColumnSchema' => '/db/schema/mysql/CMysqlColumnSchema.php',
  384. 'CMysqlSchema' => '/db/schema/mysql/CMysqlSchema.php',
  385. 'CMysqlTableSchema' => '/db/schema/mysql/CMysqlTableSchema.php',
  386. 'COciColumnSchema' => '/db/schema/oci/COciColumnSchema.php',
  387. 'COciCommandBuilder' => '/db/schema/oci/COciCommandBuilder.php',
  388. 'COciSchema' => '/db/schema/oci/COciSchema.php',
  389. 'COciTableSchema' => '/db/schema/oci/COciTableSchema.php',
  390. 'CPgsqlColumnSchema' => '/db/schema/pgsql/CPgsqlColumnSchema.php',
  391. 'CPgsqlSchema' => '/db/schema/pgsql/CPgsqlSchema.php',
  392. 'CPgsqlTableSchema' => '/db/schema/pgsql/CPgsqlTableSchema.php',
  393. 'CSqliteColumnSchema' => '/db/schema/sqlite/CSqliteColumnSchema.php',
  394. 'CSqliteCommandBuilder' => '/db/schema/sqlite/CSqliteCommandBuilder.php',
  395. 'CSqliteSchema' => '/db/schema/sqlite/CSqliteSchema.php',
  396. 'CChoiceFormat' => '/i18n/CChoiceFormat.php',
  397. 'CDateFormatter' => '/i18n/CDateFormatter.php',
  398. 'CDbMessageSource' => '/i18n/CDbMessageSource.php',
  399. 'CGettextMessageSource' => '/i18n/CGettextMessageSource.php',
  400. 'CLocale' => '/i18n/CLocale.php',
  401. 'CMessageSource' => '/i18n/CMessageSource.php',
  402. 'CNumberFormatter' => '/i18n/CNumberFormatter.php',
  403. 'CPhpMessageSource' => '/i18n/CPhpMessageSource.php',
  404. 'CGettextFile' => '/i18n/gettext/CGettextFile.php',
  405. 'CGettextMoFile' => '/i18n/gettext/CGettextMoFile.php',
  406. 'CGettextPoFile' => '/i18n/gettext/CGettextPoFile.php',
  407. 'CDbLogRoute' => '/logging/CDbLogRoute.php',
  408. 'CEmailLogRoute' => '/logging/CEmailLogRoute.php',
  409. 'CFileLogRoute' => '/logging/CFileLogRoute.php',
  410. 'CLogFilter' => '/logging/CLogFilter.php',
  411. 'CLogRoute' => '/logging/CLogRoute.php',
  412. 'CLogRouter' => '/logging/CLogRouter.php',
  413. 'CLogger' => '/logging/CLogger.php',
  414. 'CProfileLogRoute' => '/logging/CProfileLogRoute.php',
  415. 'CWebLogRoute' => '/logging/CWebLogRoute.php',
  416. 'CDateTimeParser' => '/utils/CDateTimeParser.php',
  417. 'CFileHelper' => '/utils/CFileHelper.php',
  418. 'CFormatter' => '/utils/CFormatter.php',
  419. 'CMarkdownParser' => '/utils/CMarkdownParser.php',
  420. 'CPropertyValue' => '/utils/CPropertyValue.php',
  421. 'CTimestamp' => '/utils/CTimestamp.php',
  422. 'CVarDumper' => '/utils/CVarDumper.php',
  423. 'CBooleanValidator' => '/validators/CBooleanValidator.php',
  424. 'CCaptchaValidator' => '/validators/CCaptchaValidator.php',
  425. 'CCompareValidator' => '/validators/CCompareValidator.php',
  426. 'CDateValidator' => '/validators/CDateValidator.php',
  427. 'CDefaultValueValidator' => '/validators/CDefaultValueValidator.php',
  428. 'CEmailValidator' => '/validators/CEmailValidator.php',
  429. 'CExistValidator' => '/validators/CExistValidator.php',
  430. 'CFileValidator' => '/validators/CFileValidator.php',
  431. 'CFilterValidator' => '/validators/CFilterValidator.php',
  432. 'CInlineValidator' => '/validators/CInlineValidator.php',
  433. 'CNumberValidator' => '/validators/CNumberValidator.php',
  434. 'CRangeValidator' => '/validators/CRangeValidator.php',
  435. 'CRegularExpressionValidator' => '/validators/CRegularExpressionValidator.php',
  436. 'CRequiredValidator' => '/validators/CRequiredValidator.php',
  437. 'CSafeValidator' => '/validators/CSafeValidator.php',
  438. 'CStringValidator' => '/validators/CStringValidator.php',
  439. 'CTypeValidator' => '/validators/CTypeValidator.php',
  440. 'CUniqueValidator' => '/validators/CUniqueValidator.php',
  441. 'CUnsafeValidator' => '/validators/CUnsafeValidator.php',
  442. 'CUrlValidator' => '/validators/CUrlValidator.php',
  443. 'CValidator' => '/validators/CValidator.php',
  444. 'CActiveDataProvider' => '/web/CActiveDataProvider.php',
  445. 'CArrayDataProvider' => '/web/CArrayDataProvider.php',
  446. 'CAssetManager' => '/web/CAssetManager.php',
  447. 'CBaseController' => '/web/CBaseController.php',
  448. 'CCacheHttpSession' => '/web/CCacheHttpSession.php',
  449. 'CClientScript' => '/web/CClientScript.php',
  450. 'CController' => '/web/CController.php',
  451. 'CDataProvider' => '/web/CDataProvider.php',
  452. 'CDbHttpSession' => '/web/CDbHttpSession.php',
  453. 'CExtController' => '/web/CExtController.php',
  454. 'CFormModel' => '/web/CFormModel.php',
  455. 'CHttpCookie' => '/web/CHttpCookie.php',
  456. 'CHttpRequest' => '/web/CHttpRequest.php',
  457. 'CHttpSession' => '/web/CHttpSession.php',
  458. 'CHttpSessionIterator' => '/web/CHttpSessionIterator.php',
  459. 'COutputEvent' => '/web/COutputEvent.php',
  460. 'CPagination' => '/web/CPagination.php',
  461. 'CSort' => '/web/CSort.php',
  462. 'CSqlDataProvider' => '/web/CSqlDataProvider.php',
  463. 'CTheme' => '/web/CTheme.php',
  464. 'CThemeManager' => '/web/CThemeManager.php',
  465. 'CUploadedFile' => '/web/CUploadedFile.php',
  466. 'CUrlManager' => '/web/CUrlManager.php',
  467. 'CWebApplication' => '/web/CWebApplication.php',
  468. 'CWebModule' => '/web/CWebModule.php',
  469. 'CWidgetFactory' => '/web/CWidgetFactory.php',
  470. 'CAction' => '/web/actions/CAction.php',
  471. 'CInlineAction' => '/web/actions/CInlineAction.php',
  472. 'CViewAction' => '/web/actions/CViewAction.php',
  473. 'CAccessControlFilter' => '/web/auth/CAccessControlFilter.php',
  474. 'CAuthAssignment' => '/web/auth/CAuthAssignment.php',
  475. 'CAuthItem' => '/web/auth/CAuthItem.php',
  476. 'CAuthManager' => '/web/auth/CAuthManager.php',
  477. 'CBaseUserIdentity' => '/web/auth/CBaseUserIdentity.php',
  478. 'CDbAuthManager' => '/web/auth/CDbAuthManager.php',
  479. 'CPhpAuthManager' => '/web/auth/CPhpAuthManager.php',
  480. 'CUserIdentity' => '/web/auth/CUserIdentity.php',
  481. 'CWebUser' => '/web/auth/CWebUser.php',
  482. 'CFilter' => '/web/filters/CFilter.php',
  483. 'CFilterChain' => '/web/filters/CFilterChain.php',
  484. 'CInlineFilter' => '/web/filters/CInlineFilter.php',
  485. 'CForm' => '/web/form/CForm.php',
  486. 'CFormButtonElement' => '/web/form/CFormButtonElement.php',
  487. 'CFormElement' => '/web/form/CFormElement.php',
  488. 'CFormElementCollection' => '/web/form/CFormElementCollection.php',
  489. 'CFormInputElement' => '/web/form/CFormInputElement.php',
  490. 'CFormStringElement' => '/web/form/CFormStringElement.php',
  491. 'CGoogleApi' => '/web/helpers/CGoogleApi.php',
  492. 'CHtml' => '/web/helpers/CHtml.php',
  493. 'CJSON' => '/web/helpers/CJSON.php',
  494. 'CJavaScript' => '/web/helpers/CJavaScript.php',
  495. 'CPradoViewRenderer' => '/web/renderers/CPradoViewRenderer.php',
  496. 'CViewRenderer' => '/web/renderers/CViewRenderer.php',
  497. 'CWebService' => '/web/services/CWebService.php',
  498. 'CWebServiceAction' => '/web/services/CWebServiceAction.php',
  499. 'CWsdlGenerator' => '/web/services/CWsdlGenerator.php',
  500. 'CActiveForm' => '/web/widgets/CActiveForm.php',
  501. 'CAutoComplete' => '/web/widgets/CAutoComplete.php',
  502. 'CClipWidget' => '/web/widgets/CClipWidget.php',
  503. 'CContentDecorator' => '/web/widgets/CContentDecorator.php',
  504. 'CFilterWidget' => '/web/widgets/CFilterWidget.php',
  505. 'CFlexWidget' => '/web/widgets/CFlexWidget.php',
  506. 'CHtmlPurifier' => '/web/widgets/CHtmlPurifier.php',
  507. 'CInputWidget' => '/web/widgets/CInputWidget.php',
  508. 'CMarkdown' => '/web/widgets/CMarkdown.php',
  509. 'CMaskedTextField' => '/web/widgets/CMaskedTextField.php',
  510. 'CMultiFileUpload' => '/web/widgets/CMultiFileUpload.php',
  511. 'COutputCache' => '/web/widgets/COutputCache.php',
  512. 'COutputProcessor' => '/web/widgets/COutputProcessor.php',
  513. 'CStarRating' => '/web/widgets/CStarRating.php',
  514. 'CTabView' => '/web/widgets/CTabView.php',
  515. 'CTextHighlighter' => '/web/widgets/CTextHighlighter.php',
  516. 'CTreeView' => '/web/widgets/CTreeView.php',
  517. 'CWidget' => '/web/widgets/CWidget.php',
  518. 'CCaptcha' => '/web/widgets/captcha/CCaptcha.php',
  519. 'CCaptchaAction' => '/web/widgets/captcha/CCaptchaAction.php',
  520. 'CBasePager' => '/web/widgets/pagers/CBasePager.php',
  521. 'CLinkPager' => '/web/widgets/pagers/CLinkPager.php',
  522. 'CListPager' => '/web/widgets/pagers/CListPager.php',
  523. );
  524. }
  525. spl_autoload_register(array('YiiBase','autoload'));
  526. class Yii extends YiiBase
  527. {
  528. }
  529. class CComponent
  530. {
  531. private $_e;
  532. private $_m;
  533. public function __get($name)
  534. {
  535. $getter='get'.$name;
  536. if(method_exists($this,$getter))
  537. return $this->$getter();
  538. else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  539. {
  540. // duplicating getEventHandlers() here for performance
  541. $name=strtolower($name);
  542. if(!isset($this->_e[$name]))
  543. $this->_e[$name]=new CList;
  544. return $this->_e[$name];
  545. }
  546. else if(isset($this->_m[$name]))
  547. return $this->_m[$name];
  548. else if(is_array($this->_m))
  549. {
  550. foreach($this->_m as $object)
  551. {
  552. if($object->getEnabled() && (property_exists($object,$name) || $object->canGetProperty($name)))
  553. return $object->$name;
  554. }
  555. }
  556. throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
  557. array('{class}'=>get_class($this), '{property}'=>$name)));
  558. }
  559. public function __set($name,$value)
  560. {
  561. $setter='set'.$name;
  562. if(method_exists($this,$setter))
  563. return $this->$setter($value);
  564. else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  565. {
  566. // duplicating getEventHandlers() here for performance
  567. $name=strtolower($name);
  568. if(!isset($this->_e[$name]))
  569. $this->_e[$name]=new CList;
  570. return $this->_e[$name]->add($value);
  571. }
  572. else if(is_array($this->_m))
  573. {
  574. foreach($this->_m as $object)
  575. {
  576. if($object->getEnabled() && (property_exists($object,$name) || $object->canSetProperty($name)))
  577. return $object->$name=$value;
  578. }
  579. }
  580. if(method_exists($this,'get'.$name))
  581. throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
  582. array('{class}'=>get_class($this), '{property}'=>$name)));
  583. else
  584. throw new CException(Yii::t('yii','Property "{class}.{property}" is not defined.',
  585. array('{class}'=>get_class($this), '{property}'=>$name)));
  586. }
  587. public function __isset($name)
  588. {
  589. $getter='get'.$name;
  590. if(method_exists($this,$getter))
  591. return $this->$getter()!==null;
  592. else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  593. {
  594. $name=strtolower($name);
  595. return isset($this->_e[$name]) && $this->_e[$name]->getCount();
  596. }
  597. else if(is_array($this->_m))
  598. {
  599. if(isset($this->_m[$name]))
  600. return true;
  601. foreach($this->_m as $object)
  602. {
  603. if($object->getEnabled() && (property_exists($object,$name) || $object->canGetProperty($name)))
  604. return true;
  605. }
  606. }
  607. return false;
  608. }
  609. public function __unset($name)
  610. {
  611. $setter='set'.$name;
  612. if(method_exists($this,$setter))
  613. $this->$setter(null);
  614. else if(strncasecmp($name,'on',2)===0 && method_exists($this,$name))
  615. unset($this->_e[strtolower($name)]);
  616. else if(is_array($this->_m))
  617. {
  618. if(isset($this->_m[$name]))
  619. $this->detachBehavior($name);
  620. else
  621. {
  622. foreach($this->_m as $object)
  623. {
  624. if($object->getEnabled())
  625. {
  626. if(property_exists($object,$name))
  627. return $object->$name=null;
  628. else if($object->canSetProperty($name))
  629. return $object->$setter(null);
  630. }
  631. }
  632. }
  633. }
  634. else if(method_exists($this,'get'.$name))
  635. throw new CException(Yii::t('yii','Property "{class}.{property}" is read only.',
  636. array('{class}'=>get_class($this), '{property}'=>$name)));
  637. }
  638. public function __call($name,$parameters)
  639. {
  640. if($this->_m!==null)
  641. {
  642. foreach($this->_m as $object)
  643. {
  644. if($object->getEnabled() && method_exists($object,$name))
  645. return call_user_func_array(array($object,$name),$parameters);
  646. }
  647. }
  648. if(class_exists('Closure', false) && $this->canGetProperty($name) && $this->$name instanceof Closure)
  649. return call_user_func_array($this->$name, $parameters);
  650. throw new CException(Yii::t('yii','{class} does not have a method named "{name}".',
  651. array('{class}'=>get_class($this), '{name}'=>$name)));
  652. }
  653. public function asa($behavior)
  654. {
  655. return isset($this->_m[$behavior]) ? $this->_m[$behavior] : null;
  656. }
  657. public function attachBehaviors($behaviors)
  658. {
  659. foreach($behaviors as $name=>$behavior)
  660. $this->attachBehavior($name,$behavior);
  661. }
  662. public function detachBehaviors()
  663. {
  664. if($this->_m!==null)
  665. {
  666. foreach($this->_m as $name=>$behavior)
  667. $this->detachBehavior($name);
  668. $this->_m=null;
  669. }
  670. }
  671. public function attachBehavior($name,$behavior)
  672. {
  673. if(!($behavior instanceof IBehavior))
  674. $behavior=Yii::createComponent($behavior);
  675. $behavior->setEnabled(true);
  676. $behavior->attach($this);
  677. return $this->_m[$name]=$behavior;
  678. }
  679. public function detachBehavior($name)
  680. {
  681. if(isset($this->_m[$name]))
  682. {
  683. $this->_m[$name]->detach($this);
  684. $behavior=$this->_m[$name];
  685. unset($this->_m[$name]);
  686. return $behavior;
  687. }
  688. }
  689. public function enableBehaviors()
  690. {
  691. if($this->_m!==null)
  692. {
  693. foreach($this->_m as $behavior)
  694. $behavior->setEnabled(true);
  695. }
  696. }
  697. public function disableBehaviors()
  698. {
  699. if($this->_m!==null)
  700. {
  701. foreach($this->_m as $behavior)
  702. $behavior->setEnabled(false);
  703. }
  704. }
  705. public function enableBehavior($name)
  706. {
  707. if(isset($this->_m[$name]))
  708. $this->_m[$name]->setEnabled(true);
  709. }
  710. public function disableBehavior($name)
  711. {
  712. if(isset($this->_m[$name]))
  713. $this->_m[$name]->setEnabled(false);
  714. }
  715. public function hasProperty($name)
  716. {
  717. return method_exists($this,'get'.$name) || method_exists($this,'set'.$name);
  718. }
  719. public function canGetProperty($name)
  720. {
  721. return method_exists($this,'get'.$name);
  722. }
  723. public function canSetProperty($name)
  724. {
  725. return method_exists($this,'set'.$name);
  726. }
  727. public function hasEvent($name)
  728. {
  729. return !strncasecmp($name,'on',2) && method_exists($this,$name);
  730. }
  731. public function hasEventHandler($name)
  732. {
  733. $name=strtolower($name);
  734. return isset($this->_e[$name]) && $this->_e[$name]->getCount()>0;
  735. }
  736. public function getEventHandlers($name)
  737. {
  738. if($this->hasEvent($name))
  739. {
  740. $name=strtolower($name);
  741. if(!isset($this->_e[$name]))
  742. $this->_e[$name]=new CList;
  743. return $this->_e[$name];
  744. }
  745. else
  746. throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',
  747. array('{class}'=>get_class($this), '{event}'=>$name)));
  748. }
  749. public function attachEventHandler($name,$handler)
  750. {
  751. $this->getEventHandlers($name)->add($handler);
  752. }
  753. public function detachEventHandler($name,$handler)
  754. {
  755. if($this->hasEventHandler($name))
  756. return $this->getEventHandlers($name)->remove($handler)!==false;
  757. else
  758. return false;
  759. }
  760. public function raiseEvent($name,$event)
  761. {
  762. $name=strtolower($name);
  763. if(isset($this->_e[$name]))
  764. {
  765. foreach($this->_e[$name] as $handler)
  766. {
  767. if(is_string($handler))
  768. call_user_func($handler,$event);
  769. else if(is_callable($handler,true))
  770. {
  771. if(is_array($handler))
  772. {
  773. // an array: 0 - object, 1 - method name
  774. list($object,$method)=$handler;
  775. if(is_string($object)) // static method call
  776. call_user_func($handler,$event);
  777. else if(method_exists($object,$method))
  778. $object->$method($event);
  779. else
  780. throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
  781. array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>$handler[1])));
  782. }
  783. else // PHP 5.3: anonymous function
  784. call_user_func($handler,$event);
  785. }
  786. else
  787. throw new CException(Yii::t('yii','Event "{class}.{event}" is attached with an invalid handler "{handler}".',
  788. array('{class}'=>get_class($this), '{event}'=>$name, '{handler}'=>gettype($handler))));
  789. // stop further handling if param.handled is set true
  790. if(($event instanceof CEvent) && $event->handled)
  791. return;
  792. }
  793. }
  794. else if(YII_DEBUG && !$this->hasEvent($name))
  795. throw new CException(Yii::t('yii','Event "{class}.{event}" is not defined.',
  796. array('{class}'=>get_class($this), '{event}'=>$name)));
  797. }
  798. public function evaluateExpression($_expression_,$_data_=array())
  799. {
  800. if(is_string($_expression_))
  801. {
  802. extract($_data_);
  803. return eval('return '.$_expression_.';');
  804. }
  805. else
  806. {
  807. $_data_[]=$this;
  808. return call_user_func_array($_expression_, $_data_);
  809. }
  810. }
  811. }
  812. class CEvent extends CComponent
  813. {
  814. public $sender;
  815. public $handled=false;
  816. public $params;
  817. public function __construct($sender=null,$params=null)
  818. {
  819. $this->sender=$sender;
  820. $this->params=$params;
  821. }
  822. }
  823. class CEnumerable
  824. {
  825. }
  826. abstract class CModule extends CComponent
  827. {
  828. public $preload=array();
  829. public $behaviors=array();
  830. private $_id;
  831. private $_parentModule;
  832. private $_basePath;
  833. private $_modulePath;
  834. private $_params;
  835. private $_modules=array();
  836. private $_moduleConfig=array();
  837. private $_components=array();
  838. private $_componentConfig=array();
  839. public function __construct($id,$parent,$config=null)
  840. {
  841. $this->_id=$id;
  842. $this->_parentModule=$parent;
  843. // set basePath at early as possible to avoid trouble
  844. if(is_string($config))
  845. $config=require($config);
  846. if(isset($config['basePath']))
  847. {
  848. $this->setBasePath($config['basePath']);
  849. unset($config['basePath']);
  850. }
  851. Yii::setPathOfAlias($id,$this->getBasePath());
  852. $this->preinit();
  853. $this->configure($config);
  854. $this->attachBehaviors($this->behaviors);
  855. $this->preloadComponents();
  856. $this->init();
  857. }
  858. public function __get($name)
  859. {
  860. if($this->hasComponent($name))
  861. return $this->getComponent($name);
  862. else
  863. return parent::__get($name);
  864. }
  865. public function __isset($name)
  866. {
  867. if($this->hasComponent($name))
  868. return $this->getComponent($name)!==null;
  869. else
  870. return parent::__isset($name);
  871. }
  872. public function getId()
  873. {
  874. return $this->_id;
  875. }
  876. public function setId($id)
  877. {
  878. $this->_id=$id;
  879. }
  880. public function getBasePath()
  881. {
  882. if($this->_basePath===null)
  883. {
  884. $class=new ReflectionClass(get_class($this));
  885. $this->_basePath=dirname($class->getFileName());
  886. }
  887. return $this->_basePath;
  888. }
  889. public function setBasePath($path)
  890. {
  891. if(($this->_basePath=realpath($path))===false || !is_dir($this->_basePath))
  892. throw new CException(Yii::t('yii','Base path "{path}" is not a valid directory.',
  893. array('{path}'=>$path)));
  894. }
  895. public function getParams()
  896. {
  897. if($this->_params!==null)
  898. return $this->_params;
  899. else
  900. {
  901. $this->_params=new CAttributeCollection;
  902. $this->_params->caseSensitive=true;
  903. return $this->_params;
  904. }
  905. }
  906. public function setParams($value)
  907. {
  908. $params=$this->getParams();
  909. foreach($value as $k=>$v)
  910. $params->add($k,$v);
  911. }
  912. public function getModulePath()
  913. {
  914. if($this->_modulePath!==null)
  915. return $this->_modulePath;
  916. else
  917. return $this->_modulePath=$this->getBasePath().DIRECTORY_SEPARATOR.'modules';
  918. }
  919. public function setModulePath($value)
  920. {
  921. if(($this->_modulePath=realpath($value))===false || !is_dir($this->_modulePath))
  922. throw new CException(Yii::t('yii','The module path "{path}" is not a valid directory.',
  923. array('{path}'=>$value)));
  924. }
  925. public function setImport($aliases)
  926. {
  927. foreach($aliases as $alias)
  928. Yii::import($alias);
  929. }
  930. public function setAliases($mappings)
  931. {
  932. foreach($mappings as $name=>$alias)
  933. {
  934. if(($path=Yii::getPathOfAlias($alias))!==false)
  935. Yii::setPathOfAlias($name,$path);
  936. else
  937. Yii::setPathOfAlias($name,$alias);
  938. }
  939. }
  940. public function getParentModule()
  941. {
  942. return $this->_parentModule;
  943. }
  944. public function getModule($id)
  945. {
  946. if(isset($this->_modules[$id]) || array_key_exists($id,$this->_modules))
  947. return $this->_modules[$id];
  948. else if(isset($this->_moduleConfig[$id]))
  949. {
  950. $config=$this->_moduleConfig[$id];
  951. if(!isset($config['enabled']) || $config['enabled'])
  952. {
  953. $class=$config['class'];
  954. unset($config['class'], $config['enabled']);
  955. if($this===Yii::app())
  956. $module=Yii::createComponent($class,$id,null,$config);
  957. else
  958. $module=Yii::createComponent($class,$this->getId().'/'.$id,$this,$config);
  959. return $this->_modules[$id]=$module;
  960. }
  961. }
  962. }
  963. public function hasModule($id)
  964. {
  965. return isset($this->_moduleConfig[$id]) || isset($this->_modules[$id]);
  966. }
  967. public function getModules()
  968. {
  969. return $this->_moduleConfig;
  970. }
  971. public function setModules($modules)
  972. {
  973. foreach($modules as $id=>$module)
  974. {
  975. if(is_int($id))
  976. {
  977. $id=$module;
  978. $module=array();
  979. }
  980. if(!isset($module['class']))
  981. {
  982. Yii::setPathOfAlias($id,$this->getModulePath().DIRECTORY_SEPARATOR.$id);
  983. $module['class']=$id.'.'.ucfirst($id).'Module';
  984. }
  985. if(isset($this->_moduleConfig[$id]))
  986. $this->_moduleConfig[$id]=CMap::mergeArray($this->_moduleConfig[$id],$module);
  987. else
  988. $this->_moduleConfig[$id]=$module;
  989. }
  990. }
  991. public function hasComponent($id)
  992. {
  993. return isset($this->_components[$id]) || isset($this->_componentConfig[$id]);
  994. }
  995. public function getComponent($id,$createIfNull=true)
  996. {
  997. if(isset($this->_components[$id]))
  998. return $this->_components[$id];
  999. else if(isset($this->_componentConfig[$id]) && $createIfNull)
  1000. {
  1001. $config=$this->_componentConfig[$id];
  1002. if(!isset($config['enabled']) || $config['enabled'])
  1003. {
  1004. unset($config['enabled']);
  1005. $component=Yii::createComponent($config);
  1006. $component->init();
  1007. return $this->_components[$id]=$component;
  1008. }
  1009. }
  1010. }
  1011. public function setComponent($id,$component)
  1012. {
  1013. if($component===null)
  1014. unset($this->_components[$id]);
  1015. else
  1016. {
  1017. $this->_components[$id]=$component;
  1018. if(!$component->getIsInitialized())
  1019. $component->init();
  1020. }
  1021. }
  1022. public function getComponents($loadedOnly=true)
  1023. {
  1024. if($loadedOnly)
  1025. return $this->_components;
  1026. else
  1027. return array_merge($this->_componentConfig, $this->_components);
  1028. }
  1029. public function setComponents($components,$merge=true)
  1030. {
  1031. foreach($components as $id=>$component)
  1032. {
  1033. if($component instanceof IApplicationComponent)
  1034. $this->setComponent($id,$component);
  1035. else if(isset($this->_componentConfig[$id]) && $merge)
  1036. $this->_componentConfig[$id]=CMap::mergeArray($this->_componentConfig[$id],$component);
  1037. else
  1038. $this->_componentConfig[$id]=$component;
  1039. }
  1040. }
  1041. public function configure($config)
  1042. {
  1043. if(is_array($config))
  1044. {
  1045. foreach($config as $key=>$value)
  1046. $this->$key=$value;
  1047. }
  1048. }
  1049. protected function preloadComponents()
  1050. {
  1051. foreach($this->preload as $id)
  1052. $this->getComponent($id);
  1053. }
  1054. protected function preinit()
  1055. {
  1056. }
  1057. protected function init()
  1058. {
  1059. }
  1060. }
  1061. abstract class CApplication extends CModule
  1062. {
  1063. public $name='My Application';
  1064. public $charset='UTF-8';
  1065. public $sourceLanguage='en_us';
  1066. private $_id;
  1067. private $_basePath;
  1068. private $_runtimePath;
  1069. private $_extensionPath;
  1070. private $_globalState;
  1071. private $_stateChanged;
  1072. private $_ended=false;
  1073. private $_language;
  1074. abstract public function processRequest();
  1075. public function __construct($config=null)
  1076. {
  1077. Yii::setApplication($this);
  1078. // set basePath at early as possible to avoid trouble
  1079. if(is_string($config))
  1080. $config=require($config);
  1081. if(isset($config['basePath']))
  1082. {
  1083. $this->setBasePath($config['basePath']);
  1084. unset($config['basePath']);
  1085. }
  1086. else
  1087. $this->setBasePath('protected');
  1088. Yii::setPathOfAlias('application',$this->getBasePath());
  1089. Yii::setPathOfAlias('webroot',dirname($_SERVER['SCRIPT_FILENAME']));
  1090. Yii::setPathOfAlias('ext',$this->getBasePath().DIRECTORY_SEPARATOR.'extensions');
  1091. $this->preinit();
  1092. $this->initSystemHandlers();
  1093. $this->registerCoreComponents();
  1094. $this->configure($config);
  1095. $this->attachBehaviors($this->behaviors);
  1096. $this->preloadComponents();
  1097. $this->init();
  1098. }
  1099. public function run()
  1100. {
  1101. if($this->hasEventHandler('onBeginRequest'))
  1102. $this->onBeginRequest(new CEvent($this));
  1103. $this->processRequest();
  1104. if($this->hasEventHandler('onEndRequest'))
  1105. $this->onEndRequest(new CEvent($this));
  1106. }
  1107. public function end($status=0, $exit=true)
  1108. {
  1109. if($this->hasEventHandler('onEndRequest'))
  1110. $this->onEndRequest(new CEvent($this));
  1111. if($exit)
  1112. exit($status);
  1113. }
  1114. public function onBeginRequest($event)
  1115. {
  1116. $this->raiseEvent('onBeginRequest',$event);
  1117. }
  1118. public function onEndRequest($event)
  1119. {
  1120. if(!$this->_ended)
  1121. {
  1122. $this->_ended=true;
  1123. $this->raiseEvent('onEndRequest',$event);
  1124. }
  1125. }
  1126. public function getId()
  1127. {
  1128. if($this->_id!==null)
  1129. return $this->_id;
  1130. else
  1131. return $this->_id=sprintf('%x',crc32($this->getBasePath().$this->name));
  1132. }
  1133. public function setId($id)
  1134. {
  1135. $this->_id=$id;
  1136. }
  1137. public function getBasePath()
  1138. {
  1139. return $this->_basePath;
  1140. }
  1141. public function setBasePath($path)
  1142. {
  1143. if(($this->_basePath=realpath($path))===false || !is_dir($this->_basePath))
  1144. throw new CException(Yii::t('yii','Application base path "{path}" is not a valid directory.',
  1145. array('{path}'=>$path)));
  1146. }
  1147. public function getRuntimePath()
  1148. {
  1149. if($this->_runtimePath!==null)
  1150. return $this->_runtimePath;
  1151. else
  1152. {
  1153. $this->setRuntimePath($this->getBasePath().DIRECTORY_SEPARATOR.'runtime');
  1154. return $this->_runtimePath;
  1155. }
  1156. }
  1157. public function setRuntimePath($path)
  1158. {
  1159. if(($runtimePath=realpath($path))===false || !is_dir($runtimePath) || !is_writable($runtimePath))
  1160. throw new CException(Yii::t('yii','Application runtime path "{path}" is not valid. Please make sure it is a directory writable by the Web server process.',
  1161. array('{path}'=>$path)));
  1162. $this->_runtimePath=$runtimePath;
  1163. }
  1164. public function getExtensionPath()
  1165. {
  1166. return Yii::getPathOfAlias('ext');
  1167. }
  1168. public function setExtensionPath($path)
  1169. {
  1170. if(($extensionPath=realpath($path))===false || !is_dir($extensionPath))
  1171. throw new CException(Yii::t('yii','Extension path "{path}" does not exist.',
  1172. array('{path}'=>$path)));
  1173. Yii::setPathOfAlias('ext',$extensionPath);
  1174. }
  1175. public function getLanguage()
  1176. {
  1177. return $this->_language===null ? $this->sourceLanguage : $this->_language;
  1178. }
  1179. public function setLanguage($language)
  1180. {
  1181. $this->_language=$language;
  1182. }
  1183. public function getTimeZone()
  1184. {
  1185. return date_default_timezone_get();
  1186. }
  1187. public function setTimeZone($value)
  1188. {
  1189. date_default_timezone_set($value);
  1190. }
  1191. public function findLocalizedFile($srcFile,$srcLanguage=null,$language=null)
  1192. {
  1193. if($srcLanguage===null)
  1194. $srcLanguage=$this->sourceLanguage;
  1195. if($language===null)
  1196. $language=$this->getLanguage();
  1197. if($language===$srcLanguage)
  1198. return $srcFile;
  1199. $desiredFile=dirname($srcFile).DIRECTORY_SEPARATOR.$language.DIRECTORY_SEPARATOR.basename($srcFile);
  1200. return is_file($desiredFile) ? $desiredFile : $srcFile;
  1201. }
  1202. public function getLocale($localeID=null)
  1203. {
  1204. return CLocale::getInstance($localeID===null?$this->getLanguage():$localeID);
  1205. }
  1206. public function getLocaleDataPath()
  1207. {
  1208. return CLocale::$dataPath===null ? Yii::getPathOfAlias('system.i18n.data') : CLocale::$dataPath;
  1209. }
  1210. public function setLocaleDataPath($value)
  1211. {
  1212. CLocale::$dataPath=$value;
  1213. }
  1214. public function getNumberFormatter()
  1215. {
  1216. return $this->getLocale()->getNumberFormatter();
  1217. }
  1218. public function getDateFormatter()
  1219. {
  1220. return $this->getLocale()->getDateFormatter();
  1221. }
  1222. public function getDb()
  1223. {
  1224. return $this->getComponent('db');
  1225. }
  1226. public function getErrorHandler()
  1227. {
  1228. return $this->getComponent('errorHandler');
  1229. }
  1230. public function getSecurityManager()
  1231. {
  1232. return $this->getComponent('securityManager');
  1233. }
  1234. public function getStatePersister()
  1235. {
  1236. return $this->getComponent('statePersister');
  1237. }
  1238. public function getCache()
  1239. {
  1240. return $this->getComponent('cache');
  1241. }
  1242. public function getCoreMessages()
  1243. {
  1244. return $this->getComponent('coreMessages');
  1245. }
  1246. public function getMessages()
  1247. {
  1248. return $this->getComponent('messages');
  1249. }
  1250. public function getRequest()
  1251. {
  1252. return $this->getComponent('request');
  1253. }
  1254. public function getUrlManager()
  1255. {
  1256. return $this->getComponent('urlManager');
  1257. }
  1258. public function getGlobalState($key,$defaultValue=null)
  1259. {
  1260. if($this->_globalState===null)
  1261. $this->loadGlobalState();
  1262. if(isset($this->_globalState[$key]))
  1263. return $this->_globalState[$key];
  1264. else
  1265. return $defaultValue;
  1266. }
  1267. public function setGlobalState($key,$value,$defaultValue=null)
  1268. {
  1269. if($this->_globalState===null)
  1270. $this->loadGlobalState();
  1271. $changed=$this->_stateChanged;
  1272. if($value===$defaultValue)
  1273. {
  1274. if(isset($this->_globalState[$key]))
  1275. {
  1276. unset($this->_globalState[$key]);
  1277. $this->_stateChanged=true;
  1278. }
  1279. }
  1280. else if(!isset($this->_globalState[$key]) || $this->_globalState[$key]!==$value)
  1281. {
  1282. $this->_globalState[$key]=$value;
  1283. $this->_stateChanged=true;
  1284. }
  1285. if($this->_stateChanged!==$changed)
  1286. $this->attachEventHandler('onEndRequest',array($this,'saveGlobalState'));
  1287. }
  1288. public function clearGlobalState($key)
  1289. {
  1290. $this->setGlobalState($key,true,true);
  1291. }
  1292. public function loadGlobalState()
  1293. {
  1294. $persister=$this->getStatePersister();
  1295. if(($this->_globalState=$persister->load())===null)
  1296. $this->_globalState=array();
  1297. $this->_stateChanged=false;
  1298. $this->detachEventHandler('onEndRequest',array($this,'saveGlobalState'));
  1299. }
  1300. public function saveGlobalState()
  1301. {
  1302. if($this->_stateChanged)
  1303. {
  1304. $this->_stateChanged=false;
  1305. $this->detachEventHandler('onEndRequest',array($this,'saveGlobalState'));
  1306. $this->getStatePersister()->save($this->_globalState);
  1307. }
  1308. }
  1309. public function handleException($exception)
  1310. {
  1311. // disable error capturing to avoid recursive errors
  1312. restore_error_handler();
  1313. restore_exception_handler();
  1314. $category='exception.'.get_class($exception);
  1315. if($exception instanceof CHttpException)
  1316. $category.='.'.$exception->statusCode;
  1317. // php <5.2 doesn't support string conversion auto-magically
  1318. $message=$exception->__toString();
  1319. if(isset($_SERVER['REQUEST_URI']))
  1320. $message.=' REQUEST_URI='.$_SERVER['REQUEST_URI'];
  1321. Yii::log($message,CLogger::LEVEL_ERROR,$category);
  1322. try
  1323. {
  1324. $event=new CExceptionEvent($this,$exception);
  1325. $this->onException($event);
  1326. if(!$event->handled)
  1327. {
  1328. // try an error handler
  1329. if(($handler=$this->getErrorHandler())!==null)
  1330. $handler->handle($event);
  1331. else
  1332. $this->displayException($exception);
  1333. }
  1334. }
  1335. catch(Exception $e)
  1336. {
  1337. $this->displayException($e);
  1338. }
  1339. try
  1340. {
  1341. $this->end(1);
  1342. }
  1343. catch(Exception $e)
  1344. {
  1345. // use the most primitive way to log error
  1346. $msg = get_class($e).': '.$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
  1347. $msg .= $e->getTraceAsString()."\n";
  1348. $msg .= "Previous exception:\n";
  1349. $msg .= get_class($exception).': '.$exception->getMessage().' ('.$exception->getFile().':'.$exception->getLine().")\n";
  1350. $msg .= $exception->getTraceAsString()."\n";
  1351. $msg .= '$_SERVER='.var_export($_SERVER,true);
  1352. error_log($msg);
  1353. exit(1);
  1354. }
  1355. }
  1356. public function handleError($code,$message,$file,$line)
  1357. {
  1358. if($code & error_reporting())
  1359. {
  1360. // disable error capturing to avoid recursive errors
  1361. restore_error_handler();
  1362. restore_exception_handler();
  1363. $log="$message ($file:$line)\nStack trace:\n";
  1364. $trace=debug_backtrace();
  1365. // skip the first 3 stacks as they do not tell the error position
  1366. if(count($trace)>3)
  1367. $trace=array_slice($trace,3);
  1368. foreach($trace as $i=>$t)
  1369. {
  1370. if(!isset($t['file']))
  1371. $t['file']='unknown';
  1372. if(!isset($t['line']))
  1373. $t['line']=0;
  1374. if(!isset($t['function']))
  1375. $t['function']='unknown';
  1376. $log.="#$i {$t['file']}({$t['line']}): ";
  1377. if(isset($t['object']) && is_object($t['object']))
  1378. $log.=get_class($t['object']).'->';
  1379. $log.="{$t['function']}()\n";
  1380. }
  1381. if(isset($_SERVER['REQUEST_URI']))
  1382. $log.='REQUEST_URI='.$_SERVER['REQUEST_URI'];
  1383. Yii::log($log,CLogger::LEVEL_ERROR,'php');
  1384. try
  1385. {
  1386. Yii::import('CErrorEvent',true);
  1387. $event=new CErrorEvent($this,$code,$message,$file,$line);
  1388. $this->onError($event);
  1389. if(!$event->handled)
  1390. {
  1391. // try an error handler
  1392. if(($handler=$this->getErrorHandler())!==null)
  1393. $handler->handle($event);
  1394. else
  1395. $this->displayError($code,$message,$file,$line);
  1396. }
  1397. }
  1398. catch(Exception $e)
  1399. {
  1400. $this->displayException($e);
  1401. }
  1402. try
  1403. {
  1404. $this->end(1);
  1405. }
  1406. catch(Exception $e)
  1407. {
  1408. // use the most primitive way to log error
  1409. $msg = get_class($e).': '.$e->getMessage().' ('.$e->getFile().':'.$e->getLine().")\n";
  1410. $msg .= $e->getTraceAsString()."\n";
  1411. $msg .= "Previous error:\n";
  1412. $msg .= $log."\n";
  1413. $msg .= '$_SERVER='.var_export($_SERVER,true);
  1414. error_log($msg);
  1415. exit(1);
  1416. }
  1417. }
  1418. }
  1419. public function onException($event)
  1420. {
  1421. $this->raiseEvent('onException',$event);
  1422. }
  1423. public function onError($event)
  1424. {
  1425. $this->raiseEvent('onError',$event);
  1426. }
  1427. public function displayError($code,$message,$file,$line)
  1428. {
  1429. if(YII_DEBUG)
  1430. {
  1431. echo "<h1>PHP Error [$code]</h1>\n";
  1432. echo "<p>$message ($file:$line)</p>\n";
  1433. echo '<pre>';
  1434. $trace=debug_backtrace();
  1435. // skip the first 3 stacks as they do not tell the error position
  1436. if(count($trace)>3)
  1437. $trace=array_slice($trace,3);
  1438. foreach($trace as $i=>$t)
  1439. {
  1440. if(!isset($t['file']))
  1441. $t['file']='unknown';
  1442. if(!isset($t['line']))
  1443. $t['line']=0;
  1444. if(!isset($t['function']))
  1445. $t['function']='unknown';
  1446. echo "#$i {$t['file']}({$t['line']}): ";
  1447. if(isset($t['object']) && is_object($t['object']))
  1448. echo get_class($t['object']).'->';
  1449. echo "{$t['function']}()\n";
  1450. }
  1451. echo '</pre>';
  1452. }
  1453. else
  1454. {
  1455. echo "<h1>PHP Error [$code]</h1>\n";
  1456. echo "<p>$message</p>\n";
  1457. }
  1458. }
  1459. public function displayException($exception)
  1460. {
  1461. if(YII_DEBUG)
  1462. {
  1463. echo '<h1>'.get_class($exception)."</h1>\n";
  1464. echo '<p>'.$exception->getMessage().' ('.$exception->getFile().':'.$exception->getLine().')</p>';
  1465. echo '<pre>'.$exception->getTraceAsString().'</pre>';
  1466. }
  1467. else
  1468. {
  1469. echo '<h1>'.get_class($exception)."</h1>\n";
  1470. echo '<p>'.$exception->getMessage().'</p>';
  1471. }
  1472. }
  1473. protected function initSystemHandlers()
  1474. {
  1475. if(YII_ENABLE_EXCEPTION_HANDLER)
  1476. set_exception_handler(array($this,'handleException'));
  1477. if(YII_ENABLE_ERROR_HANDLER)
  1478. set_error_handler(array($this,'handleError'),error_reporting());
  1479. }
  1480. protected function registerCoreComponents()
  1481. {
  1482. $components=array(
  1483. 'coreMessages'=>array(
  1484. 'class'=>'CPhpMessageSource',
  1485. 'language'=>'en_us',
  1486. 'basePath'=>YII_PATH.DIRECTORY_SEPARATOR.'messages',
  1487. ),
  1488. 'db'=>array(
  1489. 'class'=>'CDbConnection',
  1490. ),
  1491. 'messages'=>array(
  1492. 'class'=>'CPhpMessageSource',
  1493. ),
  1494. 'errorHandler'=>array(
  1495. 'class'=>'CErrorHandler',
  1496. ),
  1497. 'securityManager'=>array(
  1498. 'class'=>'CSecurityManager',
  1499. ),
  1500. 'statePersister'=>array(
  1501. 'class'=>'CStatePersister',
  1502. ),
  1503. 'urlManager'=>array(
  1504. 'class'=>'CUrlManager',
  1505. ),
  1506. 'request'=>array(
  1507. 'class'=>'CHttpRequest',
  1508. ),
  1509. 'format'=>array(
  1510. 'class'=>'CFormatter',
  1511. ),
  1512. );
  1513. $this->setComponents($components);
  1514. }
  1515. }
  1516. class CWebApplication extends CApplication
  1517. {
  1518. public $defaultController='site';
  1519. public $layout='main';
  1520. public $controllerMap=array();
  1521. public $catchAllRequest;
  1522. private $_controllerPath;
  1523. private $_viewPath;
  1524. private $_systemViewPath;
  1525. private $_layoutPath;
  1526. private $_controller;
  1527. private $_homeUrl;
  1528. private $_theme;
  1529. public function processRequest()
  1530. {
  1531. if(is_array($this->catchAllRequest) && isset($this->catchAllRequest[0]))
  1532. {
  1533. $route=$this->catchAllRequest[0];
  1534. foreach(array_splice($this->catchAllRequest,1) as $name=>$value)
  1535. $_GET[$name]=$value;
  1536. }
  1537. else
  1538. $route=$this->getUrlManager()->parseUrl($this->getRequest());
  1539. $this->runController($route);
  1540. }
  1541. protected function registerCoreComponents()
  1542. {
  1543. parent::registerCoreComponents();
  1544. $components=array(
  1545. 'session'=>array(
  1546. 'class'=>'CHttpSession',
  1547. ),
  1548. 'assetManager'=>array(
  1549. 'class'=>'CAssetManager',
  1550. ),
  1551. 'user'=>array(
  1552. 'class'=>'CWebUser',
  1553. ),
  1554. 'themeManager'=>array(
  1555. 'class'=>'CThemeManager',
  1556. ),
  1557. 'authManager'=>array(
  1558. 'class'=>'CPhpAuthManager',
  1559. ),
  1560. 'clientScript'=>array(
  1561. 'class'=>'CClientScript',
  1562. ),
  1563. 'widgetFactory'=>array(
  1564. 'class'=>'CWidgetFactory',
  1565. ),
  1566. );
  1567. $this->setComponents($components);
  1568. }
  1569. public function getAuthManager()
  1570. {
  1571. return $this->getComponent('authManager');
  1572. }
  1573. public function getAssetManager()
  1574. {
  1575. return $this->getComponent('assetManager');
  1576. }
  1577. public function getSession()
  1578. {
  1579. return $this->getComponent('session');
  1580. }
  1581. public function getUser()
  1582. {
  1583. return $this->getComponent('user');
  1584. }
  1585. public function getViewRenderer()
  1586. {
  1587. return $this->getComponent('viewRenderer');
  1588. }
  1589. public function getClientScript()
  1590. {
  1591. return $this->getComponent('clientScript');
  1592. }
  1593. public function getWidgetFactory()
  1594. {
  1595. return $this->getComponent('widgetFactory');
  1596. }
  1597. public function getThemeManager()
  1598. {
  1599. return $this->getComponent('themeManager');
  1600. }
  1601. public function getTheme()
  1602. {
  1603. if(is_string($this->_theme))
  1604. $this->_theme=$this->getThemeManager()->getTheme($this->_theme);
  1605. return $this->_theme;
  1606. }
  1607. public function setTheme($value)
  1608. {
  1609. $this->_theme=$value;
  1610. }
  1611. public function createUrl($route,$params=array(),$ampersand='&')
  1612. {
  1613. return $this->getUrlManager()->createUrl($route,$params,$ampersand);
  1614. }
  1615. public function createAbsoluteUrl($route,$params=array(),$schema='',$ampersand='&')
  1616. {
  1617. $url=$this->createUrl($route,$params,$ampersand);
  1618. if(strpos($url,'http')===0)
  1619. return $url;
  1620. else
  1621. return $this->getRequest()->getHostInfo($schema).$url;
  1622. }
  1623. public function getBaseUrl($absolute=false)
  1624. {
  1625. return $this->getRequest()->getBaseUrl($absolute);
  1626. }
  1627. public function getHomeUrl()
  1628. {
  1629. if($this->_homeUrl===null)
  1630. {
  1631. if($this->getUrlManager()->showScriptName)
  1632. return $this->getRequest()->getScriptUrl();
  1633. else
  1634. return $this->getRequest()->getBaseUrl().'/';
  1635. }
  1636. else
  1637. return $this->_homeUrl;
  1638. }
  1639. public function setHomeUrl($value)
  1640. {
  1641. $this->_homeUrl=$value;
  1642. }
  1643. public function runController($route)
  1644. {
  1645. if(($ca=$this->createController($route))!==null)
  1646. {
  1647. list($controller,$actionID)=$ca;
  1648. $oldController=$this->_controller;
  1649. $this->_controller=$controller;
  1650. $controller->init();
  1651. $controller->run($actionID);
  1652. $this->_controller=$oldController;
  1653. }
  1654. else
  1655. throw new CHttpException(404,Yii::t('yii','Unable to resolve the request "{route}".',
  1656. array('{route}'=>$route===''?$this->defaultController:$route)));
  1657. }
  1658. public function createController($route,$owner=null)
  1659. {
  1660. if($owner===null)
  1661. $owner=$this;
  1662. if(($route=trim($route,'/'))==='')
  1663. $route=$owner->defaultController;
  1664. $caseSensitive=$this->getUrlManager()->caseSensitive;
  1665. $route.='/';
  1666. while(($pos=strpos($route,'/'))!==false)
  1667. {
  1668. $id=substr($route,0,$pos);
  1669. if(!preg_match('/^\w+$/',$id))
  1670. return null;
  1671. if(!$caseSensitive)
  1672. $id=strtolower($id);
  1673. $route=(string)substr($route,$pos+1);
  1674. if(!isset($basePath)) // first segment
  1675. {
  1676. if(isset($owner->controllerMap[$id]))
  1677. {
  1678. return array(
  1679. Yii::createComponent($owner->controllerMap[$id],$id,$owner===$this?null:$owner),
  1680. $this->parseActionParams($route),
  1681. );
  1682. }
  1683. if(($module=$owner->getModule($id))!==null)
  1684. return $this->createController($route,$module);
  1685. $basePath=$owner->getControllerPath();
  1686. $controllerID='';
  1687. }
  1688. else
  1689. $controllerID.='/';
  1690. $className=ucfirst($id).'Controller';
  1691. $classFile=$basePath.DIRECTORY_SEPARATOR.$className.'.php';
  1692. if(is_file($classFile))
  1693. {
  1694. if(!class_exists($className,false))
  1695. require($classFile);
  1696. if(class_exists($className,false) && is_subclass_of($className,'CController'))
  1697. {
  1698. $id[0]=strtolower($id[0]);
  1699. return array(
  1700. new $className($controllerID.$id,$owner===$this?null:$owner),
  1701. $this->parseActionParams($route),
  1702. );
  1703. }
  1704. return null;
  1705. }
  1706. $controllerID.=$id;
  1707. $basePath.=DIRECTORY_SEPARATOR.$id;
  1708. }
  1709. }
  1710. protected function parseActionParams($pathInfo)
  1711. {
  1712. if(($pos=strpos($pathInfo,'/'))!==false)
  1713. {
  1714. $manager=$this->getUrlManager();
  1715. $manager->parsePathInfo((string)substr($pathInfo,$pos+1));
  1716. $actionID=substr($pathInfo,0,$pos);
  1717. return $manager->caseSensitive ? $actionID : strtolower($actionID);
  1718. }
  1719. else
  1720. return $pathInfo;
  1721. }
  1722. public function getController()
  1723. {
  1724. return $this->_controller;
  1725. }
  1726. public function setController($value)
  1727. {
  1728. $this->_controller=$value;
  1729. }
  1730. public function getControllerPath()
  1731. {
  1732. if($this->_controllerPath!==null)
  1733. return $this->_controllerPath;
  1734. else
  1735. return $this->_controllerPath=$this->getBasePath().DIRECTORY_SEPARATOR.'controllers';
  1736. }
  1737. public function setControllerPath($value)
  1738. {
  1739. if(($this->_controllerPath=realpath($value))===false || !is_dir($this->_controllerPath))
  1740. throw new CException(Yii::t('yii','The controller path "{path}" is not a valid directory.',
  1741. array('{path}'=>$value)));
  1742. }
  1743. public function getViewPath()
  1744. {
  1745. if($this->_viewPath!==null)
  1746. return $this->_viewPath;
  1747. else
  1748. return $this->_viewPath=$this->getBasePath().DIRECTORY_SEPARATOR.'views';
  1749. }
  1750. public function setViewPath($path)
  1751. {
  1752. if(($this->_viewPath=realpath($path))===false || !is_dir($this->_viewPath))
  1753. throw new CException(Yii::t('yii','The view path "{path}" is not a valid directory.',
  1754. array('{path}'=>$path)));
  1755. }
  1756. public function getSystemViewPath()
  1757. {
  1758. if($this->_systemViewPath!==null)
  1759. return $this->_systemViewPath;
  1760. else
  1761. return $this->_systemViewPath=$this->getViewPath().DIRECTORY_SEPARATOR.'system';
  1762. }
  1763. public function setSystemViewPath($path)
  1764. {
  1765. if(($this->_systemViewPath=realpath($path))===false || !is_dir($this->_systemViewPath))
  1766. throw new CException(Yii::t('yii','The system view path "{path}" is not a valid directory.',
  1767. array('{path}'=>$path)));
  1768. }
  1769. public function getLayoutPath()
  1770. {
  1771. if($this->_layoutPath!==null)
  1772. return $this->_layoutPath;
  1773. else
  1774. return $this->_layoutPath=$this->getViewPath().DIRECTORY_SEPARATOR.'layouts';
  1775. }
  1776. public function setLayoutPath($path)
  1777. {
  1778. if(($this->_layoutPath=realpath($path))===false || !is_dir($this->_layoutPath))
  1779. throw new CException(Yii::t('yii','The layout path "{path}" is not a valid directory.',
  1780. array('{path}'=>$path)));
  1781. }
  1782. public function beforeControllerAction($controller,$action)
  1783. {
  1784. return true;
  1785. }
  1786. public function afterControllerAction($controller,$action)
  1787. {
  1788. }
  1789. public function findModule($id)
  1790. {
  1791. if(($controller=$this->getController())!==null && ($module=$controller->getModule())!==null)
  1792. {
  1793. do
  1794. {
  1795. if(($m=$module->getModule($id))!==null)
  1796. return $m;
  1797. } while(($module=$module->getParentModule())!==null);
  1798. }
  1799. if(($m=$this->getModule($id))!==null)
  1800. return $m;
  1801. }
  1802. protected function init()
  1803. {
  1804. parent::init();
  1805. // preload 'request' so that it has chance to respond to onBeginRequest event.
  1806. $this->getRequest();
  1807. }
  1808. }
  1809. class CMap extends CComponent implements IteratorAggregate,ArrayAccess,Countable
  1810. {
  1811. private $_d=array();
  1812. private $_r=false;
  1813. public function __construct($data=null,$readOnly=false)
  1814. {
  1815. if($data!==null)
  1816. $this->copyFrom($data);
  1817. $this->setReadOnly($readOnly);
  1818. }
  1819. public function getReadOnly()
  1820. {
  1821. return $this->_r;
  1822. }
  1823. protected function setReadOnly($value)
  1824. {
  1825. $this->_r=$value;
  1826. }
  1827. public function getIterator()
  1828. {
  1829. return new CMapIterator($this->_d);
  1830. }
  1831. public function count()
  1832. {
  1833. return $this->getCount();
  1834. }
  1835. public function getCount()
  1836. {
  1837. return count($this->_d);
  1838. }
  1839. public function getKeys()
  1840. {
  1841. return array_keys($this->_d);
  1842. }
  1843. public function itemAt($key)
  1844. {
  1845. if(isset($this->_d[$key]))
  1846. return $this->_d[$key];
  1847. else
  1848. return null;
  1849. }
  1850. public function add($key,$value)
  1851. {
  1852. if(!$this->_r)
  1853. {
  1854. if($key===null)
  1855. $this->_d[]=$value;
  1856. else
  1857. $this->_d[$key]=$value;
  1858. }
  1859. else
  1860. throw new CException(Yii::t('yii','The map is read only.'));
  1861. }
  1862. public function remove($key)
  1863. {
  1864. if(!$this->_r)
  1865. {
  1866. if(isset($this->_d[$key]))
  1867. {
  1868. $value=$this->_d[$key];
  1869. unset($this->_d[$key]);
  1870. return $value;
  1871. }
  1872. else
  1873. {
  1874. // it is possible the value is null, which is not detected by isset
  1875. unset($this->_d[$key]);
  1876. return null;
  1877. }
  1878. }
  1879. else
  1880. throw new CException(Yii::t('yii','The map is read only.'));
  1881. }
  1882. public function clear()
  1883. {
  1884. foreach(array_keys($this->_d) as $key)
  1885. $this->remove($key);
  1886. }
  1887. public function contains($key)
  1888. {
  1889. return isset($this->_d[$key]) || array_key_exists($key,$this->_d);
  1890. }
  1891. public function toArray()
  1892. {
  1893. return $this->_d;
  1894. }
  1895. public function copyFrom($data)
  1896. {
  1897. if(is_array($data) || $data instanceof Traversable)
  1898. {
  1899. if($this->getCount()>0)
  1900. $this->clear();
  1901. if($data instanceof CMap)
  1902. $data=$data->_d;
  1903. foreach($data as $key=>$value)
  1904. $this->add($key,$value);
  1905. }
  1906. else if($data!==null)
  1907. throw new CException(Yii::t('yii','Map data must be an array or an object implementing Traversable.'));
  1908. }
  1909. public function mergeWith($data,$recursive=true)
  1910. {
  1911. if(is_array($data) || $data instanceof Traversable)
  1912. {
  1913. if($data instanceof CMap)
  1914. $data=$data->_d;
  1915. if($recursive)
  1916. {
  1917. if($data instanceof Traversable)
  1918. {
  1919. $d=array();
  1920. foreach($data as $key=>$value)
  1921. $d[$key]=$value;
  1922. $this->_d=self::mergeArray($this->_d,$d);
  1923. }
  1924. else
  1925. $this->_d=self::mergeArray($this->_d,$data);
  1926. }
  1927. else
  1928. {
  1929. foreach($data as $key=>$value)
  1930. $this->add($key,$value);
  1931. }
  1932. }
  1933. else if($data!==null)
  1934. throw new CException(Yii::t('yii','Map data must be an array or an object implementing Traversable.'));
  1935. }
  1936. public static function mergeArray($a,$b)
  1937. {
  1938. foreach($b as $k=>$v)
  1939. {
  1940. if(is_integer($k))
  1941. $a[]=$v;
  1942. else if(is_array($v) && isset($a[$k]) && is_array($a[$k]))
  1943. $a[$k]=self::mergeArray($a[$k],$v);
  1944. else
  1945. $a[$k]=$v;
  1946. }
  1947. return $a;
  1948. }
  1949. public function offsetExists($offset)
  1950. {
  1951. return $this->contains($offset);
  1952. }
  1953. public function offsetGet($offset)
  1954. {
  1955. return $this->itemAt($offset);
  1956. }
  1957. public function offsetSet($offset,$item)
  1958. {
  1959. $this->add($offset,$item);
  1960. }
  1961. public function offsetUnset($offset)
  1962. {
  1963. $this->remove($offset);
  1964. }
  1965. }
  1966. class CLogger extends CComponent
  1967. {
  1968. const LEVEL_TRACE='trace';
  1969. const LEVEL_WARNING='warning';
  1970. const LEVEL_ERROR='error';
  1971. const LEVEL_INFO='info';
  1972. const LEVEL_PROFILE='profile';
  1973. public $autoFlush=10000;
  1974. private $_logs=array();
  1975. private $_logCount=0;
  1976. private $_levels;
  1977. private $_categories;
  1978. private $_timings;
  1979. public function log($message,$level='info',$category='application')
  1980. {
  1981. $this->_logs[]=array($message,$level,$category,microtime(true));
  1982. $this->_logCount++;
  1983. if($this->autoFlush>0 && $this->_logCount>=$this->autoFlush)
  1984. $this->flush();
  1985. }
  1986. public function getLogs($levels='',$categories='')
  1987. {
  1988. $this->_levels=preg_split('/[\s,]+/',strtolower($levels),-1,PREG_SPLIT_NO_EMPTY);
  1989. $this->_categories=preg_split('/[\s,]+/',strtolower($categories),-1,PREG_SPLIT_NO_EMPTY);
  1990. if(empty($levels) && empty($categories))
  1991. return $this->_logs;
  1992. else if(empty($levels))
  1993. return array_values(array_filter(array_filter($this->_logs,array($this,'filterByCategory'))));
  1994. else if(empty($categories))
  1995. return array_values(array_filter(array_filter($this->_logs,array($this,'filterByLevel'))));
  1996. else
  1997. {
  1998. $ret=array_values(array_filter(array_filter($this->_logs,array($this,'filterByLevel'))));
  1999. return array_values(array_filter(array_filter($ret,array($this,'filterByCategory'))));
  2000. }
  2001. }
  2002. private function filterByCategory($value)
  2003. {
  2004. foreach($this->_categories as $category)
  2005. {
  2006. $cat=strtolower($value[2]);
  2007. if($cat===$category || (($c=rtrim($category,'.*'))!==$category && strpos($cat,$c)===0))
  2008. return $value;
  2009. }
  2010. return false;
  2011. }
  2012. private function filterByLevel($value)
  2013. {
  2014. return in_array(strtolower($value[1]),$this->_levels)?$value:false;
  2015. }
  2016. public function getExecutionTime()
  2017. {
  2018. return microtime(true)-YII_BEGIN_TIME;
  2019. }
  2020. public function getMemoryUsage()
  2021. {
  2022. if(function_exists('memory_get_usage'))
  2023. return memory_get_usage();
  2024. else
  2025. {
  2026. $output=array();
  2027. if(strncmp(PHP_OS,'WIN',3)===0)
  2028. {
  2029. exec('tasklist /FI "PID eq ' . getmypid() . '" /FO LIST',$output);
  2030. return isset($output[5])?preg_replace('/[\D]/','',$output[5])*1024 : 0;
  2031. }
  2032. else
  2033. {
  2034. $pid=getmypid();
  2035. exec("ps -eo%mem,rss,pid | grep $pid", $output);
  2036. $output=explode(" ",$output[0]);
  2037. return isset($output[1]) ? $output[1]*1024 : 0;
  2038. }
  2039. }
  2040. }
  2041. public function getProfilingResults($token=null,$category=null,$refresh=false)
  2042. {
  2043. if($this->_timings===null || $refresh)
  2044. $this->calculateTimings();
  2045. if($token===null && $category===null)
  2046. return $this->_timings;
  2047. $results=array();
  2048. foreach($this->_timings as $timing)
  2049. {
  2050. if(($category===null || $timing[1]===$category) && ($token===null || $timing[0]===$token))
  2051. $results[]=$timing[2];
  2052. }
  2053. return $results;
  2054. }
  2055. private function calculateTimings()
  2056. {
  2057. $this->_timings=array();
  2058. $stack=array();
  2059. foreach($this->_logs as $log)
  2060. {
  2061. if($log[1]!==CLogger::LEVEL_PROFILE)
  2062. continue;
  2063. list($message,$level,$category,$timestamp)=$log;
  2064. if(!strncasecmp($message,'begin:',6))
  2065. {
  2066. $log[0]=substr($message,6);
  2067. $stack[]=$log;
  2068. }
  2069. else if(!strncasecmp($message,'end:',4))
  2070. {
  2071. $token=substr($message,4);
  2072. if(($last=array_pop($stack))!==null && $last[0]===$token)
  2073. {
  2074. $delta=$log[3]-$last[3];
  2075. $this->_timings[]=array($message,$category,$delta);
  2076. }
  2077. else
  2078. throw new CException(Yii::t('yii','CProfileLogRoute found a mismatching code block "{token}". Make sure the calls to Yii::beginProfile() and Yii::endProfile() be properly nested.',
  2079. array('{token}'=>$token)));
  2080. }
  2081. }
  2082. $now=microtime(true);
  2083. while(($last=array_pop($stack))!==null)
  2084. {
  2085. $delta=$now-$last[3];
  2086. $this->_timings[]=array($last[0],$last[2],$delta);
  2087. }
  2088. }
  2089. public function flush($dumpLogs=false)
  2090. {
  2091. $this->onFlush(new CEvent($this, array('dumpLogs'=>$dumpLogs)));
  2092. $this->_logs=array();
  2093. $this->_logCount=0;
  2094. }
  2095. public function onFlush($event)
  2096. {
  2097. $this->raiseEvent('onFlush', $event);
  2098. }
  2099. }
  2100. abstract class CApplicationComponent extends CComponent implements IApplicationComponent
  2101. {
  2102. public $behaviors=array();
  2103. private $_initialized=false;
  2104. public function init()
  2105. {
  2106. $this->attachBehaviors($this->behaviors);
  2107. $this->_initialized=true;
  2108. }
  2109. public function getIsInitialized()
  2110. {
  2111. return $this->_initialized;
  2112. }
  2113. }
  2114. class CHttpRequest extends CApplicationComponent
  2115. {
  2116. public $enableCookieValidation=false;
  2117. public $enableCsrfValidation=false;
  2118. public $csrfTokenName='YII_CSRF_TOKEN';
  2119. public $csrfCookie;
  2120. private $_requestUri;
  2121. private $_pathInfo;
  2122. private $_scriptFile;
  2123. private $_scriptUrl;
  2124. private $_hostInfo;
  2125. private $_baseUrl;
  2126. private $_cookies;
  2127. private $_preferredLanguage;
  2128. private $_csrfToken;
  2129. private $_deleteParams;
  2130. private $_putParams;
  2131. public function init()
  2132. {
  2133. parent::init();
  2134. $this->normalizeRequest();
  2135. }
  2136. protected function normalizeRequest()
  2137. {
  2138. // normalize request
  2139. if(function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc())
  2140. {
  2141. if(isset($_GET))
  2142. $_GET=$this->stripSlashes($_GET);
  2143. if(isset($_POST))
  2144. $_POST=$this->stripSlashes($_POST);
  2145. if(isset($_REQUEST))
  2146. $_REQUEST=$this->stripSlashes($_REQUEST);
  2147. if(isset($_COOKIE))
  2148. $_COOKIE=$this->stripSlashes($_COOKIE);
  2149. }
  2150. if($this->enableCsrfValidation)
  2151. Yii::app()->attachEventHandler('onBeginRequest',array($this,'validateCsrfToken'));
  2152. }
  2153. public function stripSlashes(&$data)
  2154. {
  2155. return is_array($data)?array_map(array($this,'stripSlashes'),$data):stripslashes($data);
  2156. }
  2157. public function getParam($name,$defaultValue=null)
  2158. {
  2159. return isset($_GET[$name]) ? $_GET[$name] : (isset($_POST[$name]) ? $_POST[$name] : $defaultValue);
  2160. }
  2161. public function getQuery($name,$defaultValue=null)
  2162. {
  2163. return isset($_GET[$name]) ? $_GET[$name] : $defaultValue;
  2164. }
  2165. public function getPost($name,$defaultValue=null)
  2166. {
  2167. return isset($_POST[$name]) ? $_POST[$name] : $defaultValue;
  2168. }
  2169. public function getDelete($name,$defaultValue=null)
  2170. {
  2171. if($this->_deleteParams===null)
  2172. $this->_deleteParams=$this->getIsDeleteRequest() ? $this->getRestParams() : array();
  2173. return isset($this->_deleteParams[$name]) ? $this->_deleteParams[$name] : $defaultValue;
  2174. }
  2175. public function getPut($name,$defaultValue=null)
  2176. {
  2177. if($this->_putParams===null)
  2178. $this->_putParams=$this->getIsPutRequest() ? $this->getRestParams() : array();
  2179. return isset($this->_putParams[$name]) ? $this->_putParams[$name] : $defaultValue;
  2180. }
  2181. protected function getRestParams()
  2182. {
  2183. $result=array();
  2184. if(function_exists('mb_parse_str'))
  2185. mb_parse_str(file_get_contents('php://input'), $result);
  2186. else
  2187. parse_str(file_get_contents('php://input'), $result);
  2188. return $result;
  2189. }
  2190. public function getUrl()
  2191. {
  2192. return $this->getRequestUri();
  2193. }
  2194. public function getHostInfo($schema='')
  2195. {
  2196. if($this->_hostInfo===null)
  2197. {
  2198. if($secure=$this->getIsSecureConnection())
  2199. $http='https';
  2200. else
  2201. $http='http';
  2202. if(isset($_SERVER['HTTP_HOST']))
  2203. $this->_hostInf