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

/ipserver/FLEA/FLEA.php

http://ipmanger.googlecode.com/
PHP | 2031 lines | 1090 code | 158 blank | 783 comment | 274 complexity | 46fe5430720fb8971ef86a457cce6065 MD5 | raw file
  1. <?php
  2. /////////////////////////////////////////////////////////////////////////////
  3. // FleaPHP Framework
  4. //
  5. // Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
  6. //
  7. // ??????????????? LICENSE.txt ???
  8. // ???? http://www.fleaphp.org/ ???????
  9. /////////////////////////////////////////////////////////////////////////////
  10. /**
  11. * ?? FLEA ??????????? FleaPHP ????
  12. *
  13. * ????? FleaPHP ???????????? FleaPHP ???
  14. * ??????????? require('FLEA.php') ??????
  15. * ???? FleaPHP ???????????
  16. *
  17. * @copyright Copyright (c) 2005 - 2008 QeeYuan China Inc. (http://www.qeeyuan.com)
  18. * @author ???? (www.qeeyuan.com)
  19. * @package Core
  20. * @version $Id: FLEA.php,v 1.1 2008/09/10 03:00:15 jiang Exp $
  21. */
  22. /**
  23. * ?????????
  24. */
  25. global $___fleaphp_loaded_time;
  26. $___fleaphp_loaded_time = microtime();
  27. /**
  28. * ?????????
  29. */
  30. // ?? FleaPHP ?????
  31. define('FLEA_VERSION', '1.0.70.1017');
  32. // ???? PHP4 ? PHP5 ???
  33. if (substr(PHP_VERSION, 0, 1) == '5') {
  34. define('PHP5', true);
  35. define('PHP4', false);
  36. } else {
  37. define('PHP5', false);
  38. define('PHP4', true);
  39. }
  40. // ??? DIRECTORY_SEPARATOR
  41. define('DS', DIRECTORY_SEPARATOR);
  42. // ?? URL ??
  43. define('URL_STANDARD', 'URL_STANDARD');
  44. // PATHINFO ??
  45. define('URL_PATHINFO', 'URL_PATHINFO');
  46. // URL ????
  47. define('URL_REWRITE', 'URL_REWRITE');
  48. /**#@+
  49. * ?? RBAC ??????
  50. */
  51. // RBAC_EVERYONE ?????????????????????
  52. define('RBAC_EVERYONE', 'RBAC_EVERYONE');
  53. // RBAC_HAS_ROLE ???????????
  54. define('RBAC_HAS_ROLE', 'RBAC_HAS_ROLE');
  55. // RBAC_NO_ROLE ????????????
  56. define('RBAC_NO_ROLE', 'RBAC_NO_ROLE');
  57. // RBAC_NULL ????????
  58. define('RBAC_NULL', 'RBAC_NULL');
  59. // ACTION_ALL ???????????
  60. define('ACTION_ALL', 'ACTION_ALL');
  61. /**#@-*/
  62. /**
  63. * ??? FleaPHP ??
  64. */
  65. define('G_FLEA_VAR', '__FLEA_CORE__');
  66. $GLOBALS[G_FLEA_VAR] = array(
  67. 'APP_INF' => array(),
  68. 'OBJECTS' => array(),
  69. 'DBO' => array(),
  70. 'CLASS_PATH' => array(),
  71. 'FLEA_EXCEPTION_STACK' => array(),
  72. 'FLEA_EXCEPTION_HANDLER'=> null,
  73. );
  74. // ?? FleaPHP ???????????? CLASS_PATH
  75. $GLOBALS[G_FLEA_VAR]['CLASS_PATH'][] = dirname(__FILE__);
  76. define('FLEA_DIR', $GLOBALS[G_FLEA_VAR]['CLASS_PATH'][0] . DS . 'FLEA');
  77. define('FLEA_3RD_DIR', $GLOBALS[G_FLEA_VAR]['CLASS_PATH'][0] . DS . '3rd');
  78. // ????? FleaPHP ????????
  79. if (!defined('NO_LEGACY_FLEAPHP') || NO_LEGACY_FLEAPHP == false) {
  80. require(FLEA_DIR . '/Compatibility.php');
  81. }
  82. /**
  83. * ????????
  84. *
  85. * ?????? DEPLOY_MODE ??? true??????????? FleaPHP
  86. */
  87. if (!defined('DEPLOY_MODE') || DEPLOY_MODE != true) {
  88. $GLOBALS[G_FLEA_VAR]['APP_INF'] = require(FLEA_DIR . '/Config/DEBUG_MODE_CONFIG.php');
  89. define('DEBUG_MODE', true);
  90. if (!defined('DEPLOY_MODE')) { define('DEPLOY_MODE', false); }
  91. } else {
  92. $GLOBALS[G_FLEA_VAR]['APP_INF'] = require(FLEA_DIR . '/Config/DEPLOY_MODE_CONFIG.php');
  93. define('DEBUG_MODE', false);
  94. if (!defined('DEPLOY_MODE')) { define('DEPLOY_MODE', true); }
  95. }
  96. // ??? PHP5 ???????????
  97. if (!defined('E_STRICT')) {
  98. define('E_STRICT', 2048);
  99. }
  100. if (DEBUG_MODE) {
  101. error_reporting(error_reporting(0) & ~E_STRICT);
  102. } else {
  103. error_reporting(0);
  104. }
  105. // ????????
  106. __SET_EXCEPTION_HANDLER('__FLEA_EXCEPTION_HANDLER');
  107. /**
  108. * FLEA ???? FleaPHP ???????
  109. *
  110. * ??????????????
  111. *
  112. * @package Core
  113. * @author ???? (www.qeeyuan.com)
  114. * @version 1.0
  115. */
  116. class FLEA
  117. {
  118. /**
  119. * ????????
  120. *
  121. * example:
  122. * <code>
  123. * FLEA::loadAppInf('./config/MyConfig.php');
  124. * </code>
  125. *
  126. * @param mixed $__config ??????????
  127. */
  128. function loadAppInf($__flea_internal_config = null)
  129. {
  130. if (!is_array($__flea_internal_config) && is_string($__flea_internal_config)) {
  131. if (!is_readable($__flea_internal_config)) {
  132. FLEA::loadClass('FLEA_Exception_ExpectedFile');
  133. return __THROW(new FLEA_Exception_ExpectedFile($__flea_internal_config));
  134. }
  135. $__flea_internal_config = require($__flea_internal_config);
  136. }
  137. if (is_array($__flea_internal_config)) {
  138. $GLOBALS[G_FLEA_VAR]['APP_INF'] = array_merge($GLOBALS[G_FLEA_VAR]['APP_INF'], $__flea_internal_config);
  139. }
  140. return null;
  141. }
  142. /**
  143. * ??????????
  144. *
  145. * example:
  146. * <code>
  147. * FLEA::setAppInf('siteTitle');
  148. * .....
  149. * $siteTitle = FLEA::getAppInf('siteTitle');
  150. * </code>
  151. *
  152. * @param string $option
  153. * @param mixed $default
  154. *
  155. * @return mixed
  156. */
  157. function getAppInf($option, $default = null)
  158. {
  159. return isset($GLOBALS[G_FLEA_VAR]['APP_INF'][$option]) ? $GLOBALS[G_FLEA_VAR]['APP_INF'][$option] : $default;
  160. }
  161. /**
  162. * ?????????????????????????
  163. *
  164. * example:
  165. * <code>
  166. * $arr = array('min' => 1, 'max' => 100, 'step' => 2);
  167. * FLEA::setAppInf('rule', $arr);
  168. * .....
  169. * $min = FLEA::getAppInfValue('rule', 'min');
  170. * </code>
  171. *
  172. * @param string $option
  173. * @param string $keyname
  174. * @param mixed $default
  175. *
  176. * @return mixed
  177. */
  178. function getAppInfValue($option, $keyname, $default = null)
  179. {
  180. if (!isset($GLOBALS[G_FLEA_VAR]['APP_INF'][$option])) {
  181. $GLOBALS[G_FLEA_VAR]['APP_INF'][$option] = array();
  182. }
  183. if (array_key_exists($keyname, $GLOBALS[G_FLEA_VAR]['APP_INF'][$option])) {
  184. return $GLOBALS[G_FLEA_VAR]['APP_INF'][$option][$keyname];
  185. } else {
  186. $GLOBALS[G_FLEA_VAR]['APP_INF'][$option][$keyname] = $default;
  187. return $default;
  188. }
  189. }
  190. /**
  191. * ??????????????????????????
  192. *
  193. * @param string $option
  194. * @param string $keyname
  195. * @param mixed $value
  196. */
  197. function setAppInfValue($option, $keyname, $value)
  198. {
  199. if (!isset($GLOBALS[G_FLEA_VAR]['APP_INF'][$option])) {
  200. $GLOBALS[G_FLEA_VAR]['APP_INF'][$option] = array();
  201. }
  202. $GLOBALS[G_FLEA_VAR]['APP_INF'][$option][$keyname] = $value;
  203. }
  204. /**
  205. * ?????
  206. *
  207. * @param string $option
  208. * @param mixed $data
  209. */
  210. function setAppInf($option, $data = null)
  211. {
  212. if (is_array($option)) {
  213. $GLOBALS[G_FLEA_VAR]['APP_INF'] = array_merge($GLOBALS[G_FLEA_VAR]['APP_INF'], $option);
  214. } else {
  215. $GLOBALS[G_FLEA_VAR]['APP_INF'][$option] = $data;
  216. }
  217. }
  218. /**
  219. * ????????
  220. *
  221. * FLEA::loadClass()?FLEA::getSingleton() ????????????????????
  222. * ?????? FLEA::import() ????????????????????
  223. * ?????????????????????????????????????????????
  224. *
  225. * ?????? Table_Posts???????? ./APP/Table/Posts.php?
  226. * ??????????? ./APP???? ./APP/Table ?
  227. *
  228. * example:
  229. * <code>
  230. * FLEA::import(APP_DIR . '/LIBS');
  231. * </code>
  232. *
  233. * @param string $dir
  234. */
  235. function import($dir)
  236. {
  237. if (array_search($dir, $GLOBALS[G_FLEA_VAR]['CLASS_PATH'], true)) { return; }
  238. if (DIRECTORY_SEPARATOR == '/') {
  239. $dir = str_replace('\\', DIRECTORY_SEPARATOR, $dir);
  240. } else {
  241. $dir = str_replace('/', DIRECTORY_SEPARATOR, $dir);
  242. }
  243. $GLOBALS[G_FLEA_VAR]['CLASS_PATH'][] = $dir;
  244. }
  245. /**
  246. * ???????
  247. *
  248. * FLEA::loadFile() ? $filename ???? “_” ?????????
  249. *
  250. * example:
  251. * <code>
  252. * FLEA::loadFile('Table_Posts.php');
  253. * // ??? include 'Table/Posts.php';
  254. * </code>
  255. *
  256. * @param string $className
  257. * @param boolean $loadOnce ??? true ??FLEA::loadFile() ??? require_once
  258. *
  259. * @return boolean
  260. */
  261. function loadFile($filename, $loadOnce = false)
  262. {
  263. static $is_loaded = array();
  264. $path = FLEA::getFilePath($filename);
  265. if ($path != '') {
  266. if (isset($is_loaded[$path]) && $loadOnce) { return true; }
  267. $is_loaded[$path] = true;
  268. if ($loadOnce) {
  269. return require_once($path);
  270. } else {
  271. return require($path);
  272. }
  273. }
  274. FLEA::loadClass('FLEA_Exception_ExpectedFile');
  275. __THROW(new FLEA_Exception_ExpectedFile($filename));
  276. return false;
  277. }
  278. /**
  279. * ??????????
  280. *
  281. * ????? “_” ??????????????????????????
  282. *
  283. * example:
  284. * <code>
  285. * FLEA::loadClass('Table_Posts');
  286. * // ?????? Table_Posts ?????? Table/Posts.php
  287. * // ?????????? Table/Posts.php ??
  288. * </code>
  289. *
  290. * @param string $filename
  291. * @param boolean $noException ??? true????????????????
  292. *
  293. * @return boolean
  294. */
  295. function loadClass($className, $noException = false)
  296. {
  297. if (PHP5) {
  298. if (class_exists($className, false) || interface_exists($className, false)) { return true; }
  299. } else {
  300. if (class_exists($className)) { return true; }
  301. }
  302. if (preg_match('/[^a-z0-9\-_.]/i', $className) === 0) {
  303. $filename = FLEA::getFilePath($className . '.php');
  304. if ($filename) {
  305. require($filename);
  306. if (PHP5) {
  307. if (class_exists($className, false) || interface_exists($className, false)) { return true; }
  308. } else {
  309. if (class_exists($className)) { return true; }
  310. }
  311. }
  312. }
  313. if ($noException) { return false; }
  314. $filename = FLEA::getFilePath($className . '.php', true);
  315. require_once(FLEA_DIR . '/Exception/ExpectedClass.php');
  316. __THROW(new FLEA_Exception_ExpectedClass($className, $filename, file_exists($filename)));
  317. return false;
  318. }
  319. /**
  320. * ?? FleaPHP ??????????
  321. *
  322. * FleaPHP ????????????“_”?????????
  323. *
  324. * @param string $filename
  325. * @param boolean $return ??????????????????????????
  326. *
  327. * @return string
  328. */
  329. function getFilePath($filename, $return = false)
  330. {
  331. $filename = str_replace('_', DIRECTORY_SEPARATOR, $filename);
  332. if (DIRECTORY_SEPARATOR == '/') {
  333. $filename = str_replace('\\', DIRECTORY_SEPARATOR, $filename);
  334. } else {
  335. $filename = str_replace('/', DIRECTORY_SEPARATOR, $filename);
  336. }
  337. if (strtolower(substr($filename, -4)) != '.php') {
  338. $filename .= '.php';
  339. }
  340. // ????????
  341. if (is_file($filename)) { return $filename; }
  342. foreach ($GLOBALS[G_FLEA_VAR]['CLASS_PATH'] as $classdir) {
  343. $path = $classdir . DIRECTORY_SEPARATOR . $filename;
  344. if (is_file($path)) { return $path; }
  345. }
  346. if ($return) { return $filename; }
  347. return false;
  348. }
  349. /**
  350. * ????????????
  351. *
  352. * example:
  353. * <code>
  354. * $obj =& FLEA::getSingleton('Table_Posts);
  355. * ......
  356. * $obj2 =& FLEA::getSingleton('Table_Posts);
  357. * // ?????????????????
  358. * echo $obj === $obj2 ? 'Equals' : 'Not equals';
  359. * </code>
  360. *
  361. * @param string $className
  362. *
  363. * @return object
  364. */
  365. function & getSingleton($className)
  366. {
  367. static $instances = array();
  368. if (FLEA::isRegistered($className)) {
  369. // ???????????
  370. return FLEA::registry($className);
  371. }
  372. if (PHP5) {
  373. $classExists = class_exists($className, false);
  374. } else {
  375. $classExists = class_exists($className);
  376. }
  377. if (!$classExists) {
  378. if (!FLEA::loadClass($className)) {
  379. $return = false;
  380. return $return;
  381. }
  382. }
  383. $instances[$className] =& new $className();
  384. FLEA::register($instances[$className], $className);
  385. return $instances[$className];
  386. }
  387. /**
  388. * ???????????????????????
  389. *
  390. * example:
  391. * <code>
  392. * $obj =& new MyClass();
  393. * // ????????
  394. * FLEA::register($obj, 'MyClass');
  395. * .....
  396. * // ??????????
  397. * $obj2 =& FLEA::registry('MyClass');
  398. * // ??????????
  399. * echo $obj === $obj2 ? 'Equals' : 'Not equals';
  400. * </code>
  401. *
  402. * @param object $obj
  403. * @param string $name
  404. *
  405. * @return object
  406. */
  407. function & register(& $obj, $name = null)
  408. {
  409. if (!is_object($obj)) {
  410. FLEA::loadClass('FLEA_Exception_TypeMismatch');
  411. return __THROW(new FLEA_Exception_TypeMismatch($obj, 'object', gettype($obj)));
  412. }
  413. if (is_null($name)) {
  414. $name = get_class($obj);
  415. }
  416. if (isset($GLOBALS[G_FLEA_VAR]['OBJECTS'][$name])) {
  417. FLEA::loadClass('FLEA_Exception_ExistsKeyName');
  418. return __THROW(new FLEA_Exception_ExistsKeyName($name));
  419. } else {
  420. $GLOBALS[G_FLEA_VAR]['OBJECTS'][$name] =& $obj;
  421. return $obj;
  422. }
  423. }
  424. /**
  425. * ????????????????????????????????????????
  426. *
  427. * example:?? FLEA::register()
  428. *
  429. * @param string $name
  430. *
  431. * @return object
  432. */
  433. function & registry($name = null)
  434. {
  435. if (is_null($name)) {
  436. return $GLOBALS[G_FLEA_VAR]['OBJECTS'];
  437. }
  438. if (isset($GLOBALS[G_FLEA_VAR]['OBJECTS'][$name]) && is_object($GLOBALS[G_FLEA_VAR]['OBJECTS'][$name])) {
  439. return $GLOBALS[G_FLEA_VAR]['OBJECTS'][$name];
  440. }
  441. FLEA::loadClass('FLEA_Exception_NotExistsKeyName');
  442. return __THROW(new FLEA_Exception_NotExistsKeyName($name));
  443. }
  444. /**
  445. * ???????????????
  446. *
  447. * example:
  448. * <code>
  449. * if (FLEA::isRegistered('MyClass')) {
  450. * $obj =& FLEA::registry('MyClass');
  451. * } else {
  452. * $obj =& new MyClass();
  453. * }
  454. * </code>
  455. *
  456. * @param string $name
  457. *
  458. * @return boolean
  459. */
  460. function isRegistered($name)
  461. {
  462. return isset($GLOBALS[G_FLEA_VAR]['OBJECTS'][$name]);
  463. }
  464. /**
  465. * ?????????????????????????? false
  466. *
  467. * example:
  468. * <code>
  469. * $cacheId = 'my_cache_id';
  470. * if (!($data = FLEA::getCache($cacheId))) {
  471. * $data = 'Data';
  472. * FLEA::writeCache($cacheId, $data);
  473. * }
  474. * </code>
  475. *
  476. * ?? $cacheIdIsFilename ??? true??????????? $cacheId ????????
  477. * ???????????? $cacheIdIsFilename ????? true?
  478. *
  479. * $time ??????????????????????????????????????????????????????
  480. *
  481. * ?? $timeIsLifetime ? false?? $time ????????????????????????
  482. * ?? $time ??????????????????????????????
  483. *
  484. * @param string $cacheId ??ID???????????????ID
  485. * @param int $time ?????????????
  486. * @param boolean $timeIsLifetime ?? $time ?????
  487. * @param boolean $cacheIdIsFilename ????? $cacheId ?????
  488. *
  489. * @return mixed ??????????????????? false
  490. */
  491. function getCache($cacheId, $time = 900, $timeIsLifetime = true, $cacheIdIsFilename = false)
  492. {
  493. $cacheDir = FLEA::getAppInf('internalCacheDir');
  494. if (is_null($cacheDir)) {
  495. FLEA::loadClass('FLEA_Exception_CacheDisabled');
  496. __THROW(new FLEA_Exception_CacheDisabled($cacheDir));
  497. return false;
  498. }
  499. if ($cacheIdIsFilename) {
  500. $cacheFile = $cacheDir . DS . preg_replace('/[^a-z0-9\-_]/i', '_', $cacheId);
  501. } else {
  502. $cacheFile = $cacheDir . DS . md5($cacheId);
  503. }
  504. if (!file_exists($cacheFile)) { return false; }
  505. if ($timeIsLifetime && $time == -1) {
  506. $data = safe_file_get_contents($cacheFile);
  507. return $data !== false ? unserialize($data) : false;
  508. }
  509. $filetime = filemtime($cacheFile);
  510. if ($timeIsLifetime) {
  511. if (time() >= $filetime + $time) { return false; }
  512. } else {
  513. if ($time >= $filetime) { return false; }
  514. }
  515. $data = safe_file_get_contents($cacheFile);
  516. return $data !== false ? unserialize($data) : false;
  517. }
  518. /**
  519. * ?????????
  520. *
  521. * example:
  522. * <code>
  523. * $data = .....; // ????????????????
  524. * // cache id ???????????????????????
  525. * $cacheId = 'data_cahce_1';
  526. * FLEA::writeCache($cacheId, $data);
  527. * </code>
  528. *
  529. * @param string $cacheId
  530. * @param mixed $data
  531. * @param boolean $cacheIdIsFilename
  532. *
  533. * @return boolean
  534. */
  535. function writeCache($cacheId, $data, $cacheIdIsFilename = false)
  536. {
  537. $cacheDir = FLEA::getAppInf('internalCacheDir');
  538. if (is_null($cacheDir)) {
  539. FLEA::loadClass('FLEA_Exception_CacheDisabled');
  540. __THROW(new FLEA_Exception_CacheDisabled($cacheDir));
  541. return false;
  542. }
  543. if ($cacheIdIsFilename) {
  544. $cacheFile = $cacheDir . DS . preg_replace('/[^a-z0-9\-_]/i', '_', $cacheId);
  545. } else {
  546. $cacheFile = $cacheDir . DS . md5($cacheId);
  547. }
  548. if (!safe_file_put_contents($cacheFile, serialize($data))) {
  549. FLEA::loadClass('FLEA_Exception_CacheDisabled');
  550. __THROW(new FLEA_Exception_CacheDisabled($cacheDir));
  551. return false;
  552. } else {
  553. return true;
  554. }
  555. }
  556. /**
  557. * ?????????
  558. *
  559. * @param string $cacheId
  560. * @param boolean $cacheIdIsFilename
  561. *
  562. * @return boolean
  563. */
  564. function purgeCache($cacheId, $cacheIdIsFilename = false)
  565. {
  566. $cacheDir = FLEA::getAppInf('internalCacheDir');
  567. if (is_null($cacheDir)) {
  568. FLEA::loadClass('FLEA_Exception_CacheDisabled');
  569. __THROW(new FLEA_Exception_CacheDisabled($cacheDir));
  570. return false;
  571. }
  572. if ($cacheIdIsFilename) {
  573. $cacheFile = $cacheDir . DS . preg_replace('/[^a-z0-9\-_]/i', '_', $cacheId);
  574. } else {
  575. $cacheFile = $cacheDir . DS . md5($cacheId);
  576. }
  577. if (file_exists($cacheFile)) {
  578. return unlink($cacheFile);
  579. }
  580. return true;
  581. }
  582. /**
  583. * ??? WebControls??? FLEA_WebControls ????
  584. *
  585. * ?????????? webControlsClassName?????? WebControls ??
  586. *
  587. * @return FLEA_WebControls
  588. */
  589. function & initWebControls()
  590. {
  591. return FLEA::getSingleton(FLEA::getAppInf('webControlsClassName'));
  592. }
  593. /**
  594. * ??? Ajax??? FLEA_Ajax ????
  595. *
  596. * ?????????? ajaxClassName?????? Ajax ??
  597. *
  598. * @return FLEA_Ajax
  599. */
  600. function & initAjax()
  601. {
  602. return FLEA::getSingleton(FLEA::getAppInf('ajaxClassName'));
  603. }
  604. /**
  605. * ??????
  606. *
  607. * ???????????????????? helper. ???
  608. * ?? helper.array ??? FLEA_Helper_Array?helper.image ??? FLEA_Helper_Image?
  609. *
  610. * @param string $helperName
  611. */
  612. function loadHelper($helperName)
  613. {
  614. $settingName = 'helper.' . strtolower($helperName);
  615. $setting = FLEA::getAppInf($settingName);
  616. if ($setting) {
  617. return FLEA::loadFile($setting, true);
  618. } else {
  619. FLEA::loadClass('FLEA_Exception_NotExistsKeyName');
  620. return __THROW(new FLEA_Exception_NotExistsKeyName('helper.' . $helperName));
  621. }
  622. }
  623. /**
  624. * ???????????
  625. *
  626. * ????? $dsn ????? $dsn ??? 0????????? dbDSN ? DSN ???
  627. *
  628. * DSN ? Database Source Name ???????????????
  629. * ? FleaPHP ??DSN ???????????????????????????????????
  630. *
  631. * DSN ??????
  632. *
  633. * example:
  634. * <code>
  635. * $dsn = array(
  636. * 'driver' => 'mysql',
  637. * 'host' => 'localhost',
  638. * 'login' => 'username',
  639. * 'password' => 'password',
  640. * 'database' => 'test_db',
  641. * 'charset' => 'utf8',
  642. * );
  643. *
  644. * $dbo =& FLEA::getDBO($dsn);
  645. * </code>
  646. *
  647. * @param array|string|int $dsn
  648. *
  649. * @return FLEA_Db_Driver_Abstract
  650. */
  651. function & getDBO($dsn = 0)
  652. {
  653. if ($dsn == 0) {
  654. $dsn = FLEA::getAppInf('dbDSN');
  655. }
  656. $dsn = FLEA::parseDSN($dsn);
  657. if (!is_array($dsn) || !isset($dsn['driver'])) {
  658. FLEA::loadClass('FLEA_Db_Exception_InvalidDSN');
  659. return __THROW(new FLEA_Db_Exception_InvalidDSN($dsn));
  660. }
  661. $dsnid = $dsn['id'];
  662. if (isset($GLOBALS[G_FLEA_VAR]['DBO'][$dsnid])) {
  663. return $GLOBALS[G_FLEA_VAR]['DBO'][$dsnid];
  664. }
  665. $driver = ucfirst(strtolower($dsn['driver']));
  666. $className = 'FLEA_Db_Driver_' . $driver;
  667. if ($driver == 'Mysql' || $driver == 'Mysqlt') {
  668. require_once(FLEA_DIR . '/Db/Driver/Mysql.php');
  669. } else {
  670. FLEA::loadClass($className);
  671. }
  672. $dbo =& new $className($dsn);
  673. /* @var $dbo FLEA_Db_Driver_Abstract */
  674. $dbo->connect();
  675. $GLOBALS[G_FLEA_VAR]['DBO'][$dsnid] =& $dbo;
  676. return $GLOBALS[G_FLEA_VAR]['DBO'][$dsnid];
  677. }
  678. /**
  679. * ?? DSN ??????????? DSN ???????????? false
  680. *
  681. * @param string|array $dsn
  682. *
  683. * @return array
  684. */
  685. function parseDSN($dsn)
  686. {
  687. if (is_array($dsn)) {
  688. $dsn['host'] = isset($dsn['host']) ? $dsn['host'] : '';
  689. $dsn['port'] = isset($dsn['port']) ? $dsn['port'] : '';
  690. $dsn['login'] = isset($dsn['login']) ? $dsn['login'] : '';
  691. $dsn['password'] = isset($dsn['password']) ? $dsn['password'] : '';
  692. $dsn['database'] = isset($dsn['database']) ? $dsn['database'] : '';
  693. $dsn['options'] = isset($dsn['options']) ? serialize($dsn['options']) : '';
  694. $dsn['prefix'] = isset($dsn['prefix']) ? $dsn['prefix'] : FLEA::getAppInf('dbTablePrefix');
  695. $dsn['schema'] = isset($dsn['schema']) ? $dsn['schema'] : '';
  696. } else {
  697. $dsn = str_replace('@/', '@localhost/', $dsn);
  698. $parse = parse_url($dsn);
  699. if (empty($parse['scheme'])) { return false; }
  700. $dsn = array();
  701. $dsn['host'] = isset($parse['host']) ? $parse['host'] : 'localhost';
  702. $dsn['port'] = isset($parse['port']) ? $parse['port'] : '';
  703. $dsn['login'] = isset($parse['user']) ? $parse['user'] : '';
  704. $dsn['password'] = isset($parse['pass']) ? $parse['pass'] : '';
  705. $dsn['driver'] = isset($parse['scheme']) ? strtolower($parse['scheme']) : '';
  706. $dsn['database'] = isset($parse['path']) ? substr($parse['path'], 1) : '';
  707. $dsn['options'] = isset($parse['query']) ? $parse['query'] : '';
  708. $dsn['prefix'] = FLEA::getAppInf('dbTablePrefix');
  709. $dsn['schema'] = '';
  710. }
  711. $dsnid = "{$dsn['driver']}://{$dsn['login']}:{$dsn['password']}@{$dsn['host']}_{$dsn['prefix']}/{$dsn['database']}/{$dsn['schema']}/{$dsn['options']}";
  712. $dsn['id'] = $dsnid;
  713. return $dsn;
  714. }
  715. /**
  716. * FleaPHP ???? MVC ????
  717. *
  718. * ?????????? FleaPHP ??? MVC ??????? FLEA.php ????????????????? FLEA::runMVC() ???????
  719. */
  720. function runMVC()
  721. {
  722. $MVCPackageFilename = FLEA::getAppInf('MVCPackageFilename');
  723. if ($MVCPackageFilename != '') {
  724. require_once($MVCPackageFilename);
  725. }
  726. FLEA::init();
  727. // ??????????????
  728. $dispatcherClass = FLEA::getAppInf('dispatcher');
  729. FLEA::loadClass($dispatcherClass);
  730. $dispatcher =& new $dispatcherClass($_GET);
  731. FLEA::register($dispatcher, $dispatcherClass);
  732. $dispatcher->dispatching();
  733. }
  734. /**
  735. * ??????
  736. *
  737. * @param boolean $loadMVC
  738. */
  739. function init($loadMVC = false)
  740. {
  741. static $firstTime = true;
  742. // ?????? FLEA::init()
  743. if (!$firstTime) { return; }
  744. $firstTime = false;
  745. /**
  746. * ???????????????
  747. */
  748. __SET_EXCEPTION_HANDLER(FLEA::getAppInf('exceptionHandler'));
  749. if (PHP5) {
  750. set_exception_handler(FLEA::getAppInf('exceptionHandler'));
  751. }
  752. /**
  753. * ??????????
  754. */
  755. if (FLEA::getAppInf('logEnabled') && FLEA::getAppInf('logProvider')) {
  756. FLEA::loadClass(FLEA::getAppInf('logProvider'));
  757. }
  758. if (!function_exists('log_message')) {
  759. // ?????????????????????? log_message() ??
  760. function log_message() {};
  761. }
  762. /**
  763. * ?????????????????????
  764. */
  765. $cachedir = FLEA::getAppInf('internalCacheDir');
  766. if (empty($cachedir)) {
  767. FLEA::setAppInf('internalCacheDir', dirname(__FILE__) . DS . '_Cache');
  768. }
  769. // ?? magic_quotes
  770. if (get_magic_quotes_gpc()) {
  771. $in = array(& $_GET, & $_POST, & $_COOKIE, & $_REQUEST);
  772. while (list($k,$v) = each($in)) {
  773. foreach ($v as $key => $val) {
  774. if (!is_array($val)) {
  775. $in[$k][$key] = stripslashes($val);
  776. continue;
  777. }
  778. $in[] =& $in[$k][$key];
  779. }
  780. }
  781. unset($in);
  782. }
  783. set_magic_quotes_runtime(0);
  784. // ?? URL ???????????? URL ?????
  785. if (FLEA::getAppInf('urlMode') != URL_STANDARD) {
  786. require(FLEA_DIR . '/Filter/Uri.php');
  787. }
  788. // ?? requestFilters
  789. foreach ((array)FLEA::getAppInf('requestFilters') as $file) {
  790. FLEA::loadFile($file);
  791. }
  792. // ?? $loadMVC
  793. if ($loadMVC) {
  794. $MVCPackageFilename = FLEA::getAppInf('MVCPackageFilename');
  795. if ($MVCPackageFilename != '') {
  796. require_once($MVCPackageFilename);
  797. }
  798. }
  799. // ?? autoLoad
  800. foreach ((array)FLEA::getAppInf('autoLoad') as $file) {
  801. FLEA::loadFile($file);
  802. }
  803. // ????? session ??????
  804. if (FLEA::getAppInf('sessionProvider')) {
  805. FLEA::getSingleton(FLEA::getAppInf('sessionProvider'));
  806. }
  807. // ???? session ??
  808. if (FLEA::getAppInf('autoSessionStart')) {
  809. session_start();
  810. }
  811. // ?? I18N ?????
  812. define('RESPONSE_CHARSET', FLEA::getAppInf('responseCharset'));
  813. define('DATABASE_CHARSET', FLEA::getAppInf('databaseCharset'));
  814. // ???????????
  815. if (FLEA::getAppInf('multiLanguageSupport')) {
  816. FLEA::loadClass(FLEA::getAppInf('languageSupportProvider'));
  817. }
  818. if (!function_exists('_T')) {
  819. function _T() {};
  820. }
  821. // ?????????
  822. if (FLEA::getAppInf('autoResponseHeader')) {
  823. header('Content-Type: text/html; charset=' . FLEA::getAppInf('responseCharset'));
  824. }
  825. }
  826. }
  827. /**
  828. * ?? FleaPHP ???????
  829. */
  830. /**
  831. * ?????????? URL
  832. *
  833. * @param string $url ????? url
  834. * @param int $delay ?????????
  835. * @param bool $js ??????????? JavaScript ??
  836. * @param bool $jsWrapped ???? JavaScript ??????? <script> ??????
  837. * @param bool $return ????????? JavaScript ??
  838. */
  839. function redirect($url, $delay = 0, $js = false, $jsWrapped = true, $return = false)
  840. {
  841. $delay = (int)$delay;
  842. if (!$js) {
  843. if (headers_sent() || $delay > 0) {
  844. echo <<<EOT
  845. <html>
  846. <head>
  847. <meta http-equiv="refresh" content="{$delay};URL={$url}" />
  848. </head>
  849. </html>
  850. EOT;
  851. exit;
  852. } else {
  853. header("Location: {$url}");
  854. exit;
  855. }
  856. }
  857. $out = '';
  858. if ($jsWrapped) {
  859. $out .= '<script language="JavaScript" type="text/javascript">';
  860. }
  861. if ($delay > 0) {
  862. $out .= "window.setTimeout(function () { document.location='{$url}'; }, {$delay});";
  863. } else {
  864. $out .= "document.location='{$url}';";
  865. }
  866. if ($jsWrapped) {
  867. $out .= '</script>';
  868. }
  869. if ($return) {
  870. return $out;
  871. }
  872. echo $out;
  873. exit;
  874. }
  875. /**
  876. * ?? url
  877. *
  878. * ?? url ??????????????????????????????????????
  879. * ? url() ????????????????????????????????
  880. *
  881. * url() ????????? urlMode ????? URL ???
  882. * - URL_STANDARD - ??????????? index.php?url=Login&action=Reject&id=1
  883. * - URL_PATHINFO - PATHINFO ????? index.php/Login/Reject/id/1
  884. * - URL_REWRITE - URL ??????? /Login/Reject/id/1
  885. *
  886. * ??? url ??????????????????
  887. * - controllerAccessor
  888. * - defaultController
  889. * - actionAccessor
  890. * - defaultAction
  891. * - urlMode
  892. * - urlLowerChar
  893. *
  894. * ???
  895. * <code>
  896. * $url = url('Login', 'checkUser');
  897. * // $url ??? ?controller=Login&action=checkUser
  898. *
  899. * $url = url('Login', 'checkUser', array('username' => 'dualface'));
  900. * // $url ??? ?controller=Login&action=checkUser&username=dualface
  901. *
  902. * $url = url('Article', 'View', array('id' => 1'), '#details');
  903. * // $url ??? ?controller=Article&action=View&id=1#details
  904. * </code>
  905. *
  906. * @param string $controllerName
  907. * @param string $actionName
  908. * @param array $params
  909. * @param string $anchor
  910. * @param array $options
  911. *
  912. * @return string
  913. */
  914. function url($controllerName = null, $actionName = null, $params = null, $anchor = null, $options = null)
  915. {
  916. static $baseurl = null, $currentBootstrap = null;
  917. // ????? URL ??????????
  918. if (is_null($baseurl)) {
  919. $baseurl = detect_uri_base();
  920. $p = strrpos($baseurl, '/');
  921. $currentBootstrap = substr($baseurl, $p + 1);
  922. $baseurl = substr($baseurl, 0, $p);
  923. }
  924. // ???? url ???? bootstrap
  925. $options = (array)$options;
  926. if (isset($options['bootstrap'])) {
  927. $bootstrap = $options['bootstrap'];
  928. } else if ($currentBootstrap == '') {
  929. $bootstrap = FLEA::getAppInf('urlBootstrap');
  930. } else {
  931. $bootstrap = $currentBootstrap;
  932. }
  933. // ???????????
  934. $defaultController = FLEA::getAppInf('defaultController');
  935. $defaultAction = FLEA::getAppInf('defaultAction');
  936. $lowerChar = isset($options['lowerChar']) ? $options['lowerChar'] : FLEA::getAppInf('urlLowerChar');
  937. if ($lowerChar) {
  938. $defaultController = strtolower($defaultController);
  939. $defaultAction = strtolower($defaultAction);
  940. }
  941. if ($bootstrap != $currentBootstrap && $currentBootstrap != '') {
  942. $controllerName = !empty($controllerName) ? $controllerName : null;
  943. $actionName = !empty($actionName) ? $actionName : null;
  944. } else {
  945. $controllerName = !empty($controllerName) ? $controllerName : $defaultController;
  946. $actionName = !empty($actionName) ? $actionName : $defaultAction;
  947. }
  948. if ($lowerChar) {
  949. $controllerName = strtolower($controllerName);
  950. $actionName = strtolower($actionName);
  951. }
  952. $url = '';
  953. $mode = isset($options['mode']) ? $options['mode'] : FLEA::getAppInf('urlMode');
  954. // PATHINFO ? REWRITE ??
  955. if ($mode == URL_PATHINFO || $mode == URL_REWRITE) {
  956. $url = $baseurl;
  957. if ($mode == URL_PATHINFO) {
  958. $url .= '/' . $bootstrap;
  959. }
  960. if ($controllerName != '' && $actionName != '') {
  961. $pps = isset($options['parameterPairStyle']) ? $options['parameterPairStyle'] : FLEA::getAppInf('urlParameterPairStyle');
  962. $url .= '/' . rawurlencode($controllerName);
  963. if (is_array($params) && !empty($params)) {
  964. $url .= '/' . rawurlencode($actionName);
  965. $url .= '/' . encode_url_args($params, $mode, $pps);
  966. } else {
  967. if (FLEA::getAppInf('urlAlwaysUseAccessor') || $actionName != $defaultAction) {
  968. $url .= '/' . rawurlencode($actionName);
  969. }
  970. }
  971. }
  972. if ($anchor) { $url .= '#' . $anchor; }
  973. return $url;
  974. }
  975. // ????
  976. $alwaysUseBootstrap = isset($options['alwaysUseBootstrap']) ? $options['alwaysUseBootstrap'] : FLEA::getAppInf('urlAlwaysUseBootstrap');
  977. $url = $baseurl . '/';
  978. if ($alwaysUseBootstrap || $bootstrap != FLEA::getAppInf('urlBootstrap')) {
  979. $url .= $bootstrap;
  980. }
  981. $parajoin = '?';
  982. if (FLEA::getAppInf('urlAlwaysUseAccessor')) {
  983. $defaultController = '';
  984. $defaultAction = '';
  985. }
  986. if ($controllerName != $defaultController && !is_null($controllerName)) {
  987. $url .= $parajoin . FLEA::getAppInf('controllerAccessor'). '=' . $controllerName;
  988. $parajoin = '&';
  989. }
  990. if ($actionName != $defaultAction && !is_null($actionName)) {
  991. $url .= $parajoin . FLEA::getAppInf('actionAccessor') . '=' . $actionName;
  992. $parajoin = '&';
  993. }
  994. if (is_array($params) && !empty($params)) {
  995. $url .= $parajoin . encode_url_args($params, $mode);
  996. }
  997. if ($anchor) { $url .= '#' . $anchor; }
  998. return $url;
  999. }
  1000. /**
  1001. * ??????? URL ??
  1002. *
  1003. * ?? tsingson ?????????? FleaPHP ?? url() ?????? CGI ??????
  1004. *
  1005. * @param boolean $queryMode ??? URL ????????????
  1006. *
  1007. * @return string
  1008. */
  1009. function detect_uri_base($queryMode = false)
  1010. {
  1011. $aURL = array();
  1012. // Try to get the request URL
  1013. if (!empty($_SERVER['SCRIPT_NAME'])) {
  1014. $arr = parse_url($_SERVER['SCRIPT_NAME']);
  1015. $aURL['path'] = $arr['path'];
  1016. } elseif (!empty($_SERVER['REQUEST_URI'])) {
  1017. $_SERVER['REQUEST_URI'] = str_replace(array('"',"'",'<','>'), array('%22','%27','%3C','%3E'), $_SERVER['REQUEST_URI']);
  1018. $p = strpos($_SERVER['REQUEST_URI'], ':');
  1019. if ($p > 0 && substr($_SERVER['REQUEST_URI'], $p + 1, 2) != '//') {
  1020. $aURL = array('path' => $_SERVER['REQUEST_URI']);
  1021. } else {
  1022. $aURL = parse_url($_SERVER['REQUEST_URI']);
  1023. }
  1024. if (isset($aURL['path']) && isset($_SERVER['PATH_INFO'])) {
  1025. $aURL['path'] = substr(urldecode($aURL['path']), 0, - strlen($_SERVER['PATH_INFO']));
  1026. }
  1027. }
  1028. // Fill in the empty values
  1029. if (empty($aURL['scheme'])) {
  1030. if (!empty($_SERVER['HTTP_SCHEME'])) {
  1031. $aURL['scheme'] = $_SERVER['HTTP_SCHEME'];
  1032. } else {
  1033. $aURL['scheme'] = (!empty($_SERVER['HTTPS']) && strtolower($_SERVER['HTTPS']) != 'off') ? 'https' : 'http';
  1034. }
  1035. }
  1036. if (empty($aURL['host'])) {
  1037. if (!empty($_SERVER['HTTP_X_FORWARDED_HOST'])) {
  1038. $p = strpos($_SERVER['HTTP_X_FORWARDED_HOST'], ':');
  1039. if ($p > 0) {
  1040. $aURL['host'] = substr($_SERVER['HTTP_X_FORWARDED_HOST'], 0, $p);
  1041. $aURL['port'] = substr($_SERVER['HTTP_X_FORWARDED_HOST'], $p + 1);
  1042. } else {
  1043. $aURL['host'] = $_SERVER['HTTP_X_FORWARDED_HOST'];
  1044. }
  1045. } else if (!empty($_SERVER['HTTP_HOST'])) {
  1046. $p = strpos($_SERVER['HTTP_HOST'], ':');
  1047. if ($p > 0) {
  1048. $aURL['host'] = substr($_SERVER['HTTP_HOST'], 0, $p);
  1049. $aURL['port'] = substr($_SERVER['HTTP_HOST'], $p + 1);
  1050. } else {
  1051. $aURL['host'] = $_SERVER['HTTP_HOST'];
  1052. }
  1053. } else if (!empty($_SERVER['SERVER_NAME'])) {
  1054. $aURL['host'] = $_SERVER['SERVER_NAME'];
  1055. }
  1056. }
  1057. if (empty($aURL['port']) && !empty($_SERVER['SERVER_PORT'])) {
  1058. $aURL['port'] = $_SERVER['SERVER_PORT'];
  1059. }
  1060. if (empty($aURL['path'])) {
  1061. if (!empty($_SERVER['PATH_INFO'])) {
  1062. $sPath = parse_url($_SERVER['PATH_INFO']);
  1063. } else {
  1064. $sPath = parse_url($_SERVER['PHP_SELF']);
  1065. }
  1066. $aURL['path'] = str_replace(array('"',"'",'<','>'), array('%22','%27','%3C','%3E'), $sPath['path']);
  1067. unset($sPath);
  1068. }
  1069. // Build the URL: Start with scheme, user and pass
  1070. $sURL = $aURL['scheme'].'://';
  1071. if (!empty($aURL['user'])) {
  1072. $sURL .= $aURL['user'];
  1073. if (!empty($aURL['pass'])) {
  1074. $sURL .= ':'.$aURL['pass'];
  1075. }
  1076. $sURL .= '@';
  1077. }
  1078. // Add the host
  1079. $sURL .= $aURL['host'];
  1080. // Add the port if needed
  1081. if (!empty($aURL['port']) && (($aURL['scheme'] == 'http' && $aURL['port'] != 80) || ($aURL['scheme'] == 'https' && $aURL['port'] != 443))) {
  1082. $sURL .= ':'.$aURL['port'];
  1083. }
  1084. $sURL .= $aURL['path'];
  1085. // Add the path and the query string
  1086. if ($queryMode && isset($aURL['query'])) {
  1087. $sURL .= $aURL['query'];
  1088. }
  1089. unset($aURL);
  1090. return $sURL;
  1091. }
  1092. /**
  1093. * ????????? url ????????
  1094. *
  1095. * ???
  1096. * <code>
  1097. * $string = encode_url_args(array('username' => 'dualface', 'mode' => 'md5'));
  1098. * // $string ??? username=dualface&mode=md5
  1099. * </code>
  1100. *
  1101. * @param array $args
  1102. * @param enum $urlMode
  1103. * @param string $parameterPairStyle
  1104. *
  1105. * @return string
  1106. */
  1107. function encode_url_args($args, $urlMode = URL_STANDARD, $parameterPairStyle = null)
  1108. {
  1109. $str = '';
  1110. switch ($urlMode) {
  1111. case URL_STANDARD:
  1112. if (is_null($parameterPairStyle)) {
  1113. $parameterPairStyle = '=';
  1114. }
  1115. $sc = '&';
  1116. break;
  1117. case URL_PATHINFO:
  1118. case URL_REWRITE:
  1119. if (is_null($parameterPairStyle)) {
  1120. $parameterPairStyle = FLEA::getAppInf('urlParameterPairStyle');
  1121. }
  1122. $sc = '/';
  1123. break;
  1124. }
  1125. foreach ($args as $key => $value) {
  1126. if (is_null($value) || $value === '') { continue; }
  1127. if (is_array($value)) {
  1128. $append = encode_url_args($value, $urlMode);
  1129. } else {
  1130. $append = rawurlencode($key) . $parameterPairStyle . rawurlencode($value);
  1131. }
  1132. if (substr($str, -1) != $sc) {
  1133. $str .= $sc;
  1134. }
  1135. $str .= $append;
  1136. }
  1137. return substr($str, 1);
  1138. }
  1139. /**
  1140. * ?? HTML ???????? htmlspecialchars()
  1141. *
  1142. * @param string $text
  1143. *
  1144. * @return string
  1145. */
  1146. function h($text)
  1147. {
  1148. return htmlspecialchars($text);
  1149. }
  1150. /**
  1151. * ?? HTML ????????????
  1152. *
  1153. * ????? &nbsp; ??????? <br />?
  1154. *
  1155. * @param string $text
  1156. *
  1157. * @return string
  1158. */
  1159. function t($text)
  1160. {
  1161. return nl2br(str_replace(' ', '&nbsp;', htmlspecialchars($text)));
  1162. }
  1163. /**
  1164. * ?? JavaScript ???????????????????????
  1165. *
  1166. * ???
  1167. * <code>
  1168. * js_alert('Dialog message', '', $url);
  1169. * // ??
  1170. * js_alert('Dialog message', 'window.close();');
  1171. * </code>
  1172. *
  1173. * @param string $message ??????
  1174. * @param string $after_action ???????????
  1175. * @param string $url ?????
  1176. */
  1177. function js_alert($message = '', $after_action = '', $url = '')
  1178. {
  1179. $out = "<script language=\"javascript\" type=\"text/javascript\">\n";
  1180. if (!empty($message)) {
  1181. $out .= "alert(\"";
  1182. $out .= str_replace("\\\\n", "\\n", t2js(addslashes($message)));
  1183. $out .= "\");\n";
  1184. }
  1185. if (!empty($after_action)) {
  1186. $out .= $after_action . "\n";
  1187. }
  1188. if (!empty($url)) {
  1189. $out .= "document.location.href=\"";
  1190. $out .= $url;
  1191. $out .= "\";\n";
  1192. }
  1193. $out .= "</script>";
  1194. echo $out;
  1195. exit;
  1196. }
  1197. /**
  1198. * ????????? JavaScript ??????????"?
  1199. *
  1200. * @param string $content
  1201. *
  1202. * @return string
  1203. */
  1204. function t2js($content)
  1205. {
  1206. return str_replace(array("\r", "\n"), array('', '\n'), addslashes($content));
  1207. }
  1208. /**
  1209. * safe_file_put_contents() ???????????????????????????????????????
  1210. *
  1211. * @param string $filename
  1212. * @param string $content
  1213. * @param int $flag
  1214. *
  1215. * @return boolean
  1216. */
  1217. function safe_file_put_contents($filename, & $content)
  1218. {
  1219. $fp = fopen($filename, 'wb');
  1220. if ($fp) {
  1221. flock($fp, LOCK_EX);
  1222. fwrite($fp, $content);
  1223. flock($fp, LOCK_UN);
  1224. fclose($fp);
  1225. return true;
  1226. } else {
  1227. return false;
  1228. }
  1229. }
  1230. /**
  1231. * safe_file_get_contents() ???????????????????????????????????
  1232. *
  1233. * @param string $filename
  1234. *
  1235. * @return mixed
  1236. */
  1237. function safe_file_get_contents($filename)
  1238. {
  1239. $fp = fopen($filename, 'rb');
  1240. if ($fp) {
  1241. flock($fp, LOCK_SH);
  1242. clearstatcache();
  1243. $data = fread($fp, filesize($filename));
  1244. flock($fp, LOCK_UN);
  1245. fclose($fp);
  1246. return $data;
  1247. } else {
  1248. return false;
  1249. }
  1250. }
  1251. if (!function_exists('file_put_contents'))
  1252. {
  1253. function file_put_contents($filename, & $content)
  1254. {
  1255. return safe_file_put_contents($filename, $content);
  1256. }
  1257. }
  1258. /**
  1259. * ??????????????
  1260. */
  1261. /**
  1262. * ??????
  1263. *
  1264. * FleaPHP ???? PHP4????????????????????????????????
  1265. * FleaPHP ?????????????
  1266. * - ? __TRY() ??? try ??????
  1267. * - ? __CATCH() ???????? catch?
  1268. * - ? __THROW() ?????
  1269. * - __TRY() ? __CATCH() ?????? PHP5 ?? throw ??????
  1270. * - ????? __THROW() ?????????? return false ????????????
  1271. * - __TRY() ? __CATCH() ????????? __CATCH() ?????????
  1272. * - ? __IS_EXCEPTION() ??? __CATCH() ????????????
  1273. * - ?? __TRY() ?????? __CATCH() ???????? __CANCEL_TRY() ?????
  1274. *
  1275. * ?? __THROW() ??????????????? FLEA_Exception ??????????
  1276. * ???? FleaPHP ??????????? FLEA_Exception ????????????
  1277. * FLEA_Exception ???????????????????????
  1278. *
  1279. * ?????????????????????
  1280. * <code>
  1281. * __TRY();
  1282. * $ret = doSomething(); // ????????????
  1283. * $ex = __CATCH();
  1284. * if (__IS_EXCEPTION($ex)) {
  1285. * // ????
  1286. * } else {
  1287. * echo $ret;
  1288. * }
  1289. *
  1290. * function doSomething() {
  1291. * if (rand(0, 9) % 2) {
  1292. * __THROW(new MyException());
  1293. * return false;
  1294. * }
  1295. * return true;
  1296. * }
  1297. * </code>
  1298. *
  1299. * <strong>?????????? __THROW() ???????? return false</strong>
  1300. *
  1301. * ?? doSomething() ?? __THROW() ???????????????? doSomething() ?
  1302. * ???????????????? doSomething() ?????????
  1303. *
  1304. * ???__TRY() ? __CATCH() ????????????
  1305. *
  1306. * <strong>?? __TRY() ? __CATCH() ??????</strong>
  1307. *
  1308. * FleaPHP ??? __TRY() ?????????????doSomething() ???????????
  1309. * ???????? doSomething() ?????? __TRY() ? __CATCH() ??????
  1310. *
  1311. * <code>
  1312. * function doSomething() {
  1313. * if (rand(0, 9) % 2) {
  1314. * __THROW(new MyException());
  1315. * return false;
  1316. * } else {
  1317. * __TRY();
  1318. * callAnotherFunc();
  1319. * $ex = __CATCH();
  1320. * if (__IS_EXCEPTION($ex)) {
  1321. * // ?? callAnotherFunc() ???????
  1322. * ...
  1323. * // ?????????? __THROW() ?????????
  1324. * // ??? doSomething() ?????????
  1325. * __THROW($ex);
  1326. * return false;
  1327. * }
  1328. * return true;
  1329. * }
  1330. * }
  1331. * </code>
  1332. *
  1333. * ???? __TRY() ??????? __CATCH() ????????? __CANCEL_TRY()
  1334. * ??? __TRY() ???????
  1335. *
  1336. * @package Core
  1337. *
  1338. * @param FLEA_Exception $exception
  1339. *
  1340. * @return boolean
  1341. */
  1342. function __THROW($exception)
  1343. {
  1344. // ????
  1345. if (function_exists('log_message')) {
  1346. log_message(get_class($exception) . ': ' . $exception->getMessage(), 'exception');
  1347. }
  1348. // ????????????
  1349. if (isset($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']) && is_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']))
  1350. {
  1351. $point = array_pop($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']);
  1352. if ($point != null) {
  1353. array_push($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'], $exception);
  1354. $ret = false;
  1355. return $ret;
  1356. }
  1357. }
  1358. if (isset($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'])) {
  1359. call_user_func_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'], array(& $exception));
  1360. } else {
  1361. __FLEA_EXCEPTION_HANDLER($exception);
  1362. }
  1363. exit;
  1364. }
  1365. /**
  1366. * ???????
  1367. *
  1368. * @package Core
  1369. */
  1370. function __TRY()
  1371. {
  1372. static $point = 0;
  1373. if (!isset($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']) ||
  1374. !is_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']))
  1375. {
  1376. $GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'] = array();
  1377. }
  1378. $point++;
  1379. array_push($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'], $point);
  1380. }
  1381. /**
  1382. * ??????????????????? false
  1383. *
  1384. * @package Core
  1385. *
  1386. * @return FLEA_Exception
  1387. */
  1388. function __CATCH()
  1389. {
  1390. if (!is_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'])) {
  1391. return false;
  1392. }
  1393. $exception = array_pop($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']);
  1394. if (!is_object($exception)) {
  1395. $exception = false;
  1396. }
  1397. return $exception;
  1398. }
  1399. /**
  1400. * ?????? __TRY() ??????
  1401. *
  1402. * @package Core
  1403. */
  1404. function __CANCEL_TRY()
  1405. {
  1406. if (is_array($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK'])) {
  1407. array_pop($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_STACK']);
  1408. }
  1409. }
  1410. /**
  1411. * ?????????
  1412. *
  1413. * $type ?????????????????
  1414. *
  1415. * @package Core
  1416. *
  1417. * @param FLEA_Exception $exception
  1418. * @param string $type
  1419. */
  1420. function __IS_EXCEPTION($exception, $type = null)
  1421. {
  1422. if (!is_object($exception) || !is_a($exception, 'FLEA_Exception')) {
  1423. return false;
  1424. }
  1425. if (is_null($type)) {
  1426. return true;
  1427. } else {
  1428. return strtoupper($type) == strtoupper(get_class($exception));
  1429. }
  1430. }
  1431. /**
  1432. * ????????????????????????
  1433. *
  1434. * ?????????? __TRY() ??????????????FleaPHP ???
  1435. * ????????????????????????????????????
  1436. *
  1437. * ???
  1438. * <code>
  1439. * // ?????????????
  1440. * global $prevExceptionHandler;
  1441. * $prevExceptionHandler = __SET_EXCEPTION_HANDLER('app_exception_handler');
  1442. *
  1443. * function app_exception_handler(& $ex) {
  1444. * global $prevExceptionHandler;
  1445. *
  1446. * if (is_a($ex, 'APP_Exception')) {
  1447. * // ?????
  1448. * ...
  1449. * } else {
  1450. * // ???????????
  1451. * if ($prevExceptionHandler) {
  1452. * call_user_func_array($prevExceptionHandler, array(& $exception));
  1453. * }
  1454. * }
  1455. * }
  1456. * </code>
  1457. *
  1458. * ???????????????????????????????????????
  1459. * ????????????????????????????????????????
  1460. *
  1461. * @package Core
  1462. *
  1463. * @param callback $callback
  1464. *
  1465. * @return mixed
  1466. */
  1467. function __SET_EXCEPTION_HANDLER($callback)
  1468. {
  1469. if (isset($GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'])) {
  1470. $current = $GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'];
  1471. } else {
  1472. $current = null;
  1473. }
  1474. $GLOBALS[G_FLEA_VAR]['FLEA_EXCEPTION_HANDLER'] = $callback;
  1475. return $current;
  1476. }
  1477. /**
  1478. * FleaPHP ?????????
  1479. *
  1480. * @package Core
  1481. *
  1482. * @param FLEA_Exception $ex
  1483. */
  1484. function __FLEA_EXCEPTION_HANDLER($ex)
  1485. {
  1486. if (!FLEA::getAppInf('displayErrors')) { exit; }
  1487. if (FLEA::getAppInf('friendlyErrorsMessage')) {
  1488. $language = FLEA::getAppInf('defaultLanguage');
  1489. $language = preg_replace('/[^a-z0-9\-_]+/i', '', $language);
  1490. $exclass = strtoupper(get_class($ex));
  1491. $template = FLEA_DIR . "/_Errors/{$language}/{$exclass}.php";
  1492. if (!file_exists($template)) {
  1493. $template = FLEA_DIR . "/_Errors/{$language}/FLEA_EXCEPTION.php";
  1494. if (!file_exists($template)) {
  1495. $template = FLEA_DIR . "/_Errors/default/FLEA_EXCEPTION.php";
  1496. }
  1497. }
  1498. include($template);
  1499. } else {
  1500. print_ex($ex);
  1501. }
  1502. exit;
  1503. }
  1504. /**
  1505. * ?????????
  1506. *
  1507. * @package Core
  1508. *
  1509. * @param FLEA_Exception $ex
  1510. * @param boolean $return ? true ???????????????
  1511. */
  1512. function print_ex($ex, $return = false)
  1513. {
  1514. $out = "exception '" . get_class($ex) . "'";
  1515. if ($ex->getMessage() != '') {
  1516. $out .= " with message '" . $ex->getMessage() . "'";
  1517. }
  1518. if (defined('DEPLOY_MODE') && DEPLOY_MODE != false) {
  1519. $out .= ' in ' . basename($ex->getFile()) . ':' . $ex->getLine() . "\n\n";
  1520. } else {
  1521. $out .= ' in ' . $ex->getFile() . ':' . $ex->getLine() . "\n\n";
  1522. $out .= $ex->getTraceAsString();
  1523. }
  1524. if ($return) { return $out; }
  1525. if (ini_get('html_errors')) {
  1526. echo nl2br(htmlspecialchars($out));
  1527. } else {
  1528. echo $out;
  1529. }
  1530. return '';
  1531. }
  1532. /**
  1533. * ??????????????
  1534. *
  1535. * @package Core
  1536. *
  1537. * @param mixed $vars ??????
  1538. * @param string $label
  1539. * @param boolean $return
  1540. */
  1541. function dump($vars, $label = '', $return = false)
  1542. {
  1543. if (ini_get('html_errors')) {
  1544. $content = "<pre>\n";
  1545. if ($label != '') {
  1546. $content .= "<strong>{$label} :</strong>\n";
  1547. }
  1548. $content .= htmlspecialchars(print_r($vars, true));
  1549. $content .= "\n</pre>\n";
  1550. } else {
  1551. $content = $label . " :\n" . print_r($vars, true);
  1552. }
  1553. if ($return) { return $content; }
  1554. echo $content;
  1555. return null;
  1556. }
  1557. /**
  1558. * ?????????????????
  1559. *
  1560. * @package Core
  1561. *
  1562. * @return string
  1563. */
  1564. function dump_trace()
  1565. {
  1566. $debug = debug_backtrace();
  1567. $lines = '';
  1568. $index = 0;
  1569. for ($i = 0; $i < count($debug); $i++) {
  1570. if ($i == 0) { continue; }
  1571. $file = $debug[$i];
  1572. if ($file['file'] == '') { continue; }
  1573. if (substr($file['file'], 0, strlen(FLEA_DIR)) != FLEA_DIR) {
  1574. $line = "#<strong>{$index} {$file['file']}({$file['line']}): </strong>";
  1575. } else {
  1576. $line = "#{$index} {$file['file']}({$file['line']}): ";
  1577. }
  1578. if (isset($file['class'])) {
  1579. $line .= "{$file['class']}{$file['type']}";
  1580. }
  1581. $line .= "{$file['function']}(";
  1582. if (isset($file['args']) && count($file['args'])) {
  1583. foreach ($file['args'] as $arg) {
  1584. $line .= gettype($arg) . ', ';
  1585. }
  1586. $line = substr($line, 0, -2);
  1587. }
  1588. $line .= ')';
  1589. $lines .= $line . "\n";
  1590. $index++;
  1591. } // for
  1592. $lines .= "#{$index} {main}\n";
  1593. if (ini_get('html_errors')) {
  1594. echo nl2br(str_replace(' ', '&nbsp;', $lines));
  1595. } else {
  1596. echo $lines;
  1597. }
  1598. }
  1599. /**
  1600. * ??????????????
  1601. *
  1602. * @param mixed $time
  1603. *
  1604. * @return float
  1605. */
  1606. function microtime_float($time = null)
  1607. {
  1608. list($usec, $sec) = explode(' ', $time ? $time : microtime());
  1609. return ((float)$usec + (float)$sec);
  1610. }
  1611. /**
  1612. * ???????????????
  1613. *
  1614. * ???????????? 'defaultLanguage' ??????????????
  1615. * ?????????????????????????
  1616. *
  1617. * ??????????????????????? default ??????
  1618. *
  1619. * ? $appError ? true ??_ET() ??????????
  1620. * 'languageFilesDir' ?????????????
  1621. *
  1622. * @package Core
  1623. *
  1624. * @param int $errorCode
  1625. * @param boolean $appError
  1626. *
  1627. * @return string
  1628. */
  1629. function _ET($errorCode, $appError = false)
  1630. {
  1631. static $message = array();
  1632. $language = FLEA::getAppInf('defaultLanguage');
  1633. $language = preg_replace('/[^a-z0-9\-_]+/i', '', $language);
  1634. if (!isset($message[$language])) {
  1635. if ($appError) {
  1636. $filename = FLEA::getAppInf('languageFilesDir') . DS .
  1637. $language . DS . 'ErrorMessage.php';
  1638. } else {
  1639. // ?? FleaPHP ?????????
  1640. $filename = FLEA_DIR . "/_Errors/{$language}/ErrorMessage.php";
  1641. }
  1642. if (!is_readable($filename)) {
  1643. $filename = FLEA_DIR . '/_Errors/default/ErrorMessage.php';
  1644. }
  1645. $message[$language] = include($filename);
  1646. }
  1647. return isset($message[$language][$errorCode]) ?
  1648. $message[$language][$errorCode] :
  1649. '';
  1650. }
  1651. /**
  1652. * PHP4 ? PHP5 ????????
  1653. */
  1654. if (PHP5) {
  1655. class FLEA_Exception extends Exception
  1656. {
  1657. function FLEA_Exception($message = '', $code = 0)
  1658. {
  1659. parent::__construct($message, $code);
  1660. }
  1661. }
  1662. } else {
  1663. /**
  1664. * FLEA_Exception ????????
  1665. *
  1666. * ? PHP5 ??FLEA_Exception ??? PHP ??? Exception ??
  1667. * ? PHP4 ???????????
  1668. *
  1669. * @package Exception
  1670. * @author ???? (www.qeeyuan.com)
  1671. * @version 1.0
  1672. */
  1673. class FLEA_Exception
  1674. {
  1675. /**
  1676. * ????
  1677. *
  1678. * @var string
  1679. */
  1680. var $message = 'Unknown exception';
  1681. /**
  1682. * ????
  1683. */
  1684. var $code = 0;
  1685. /**
  1686. * ???????
  1687. *
  1688. * @var string
  1689. */
  1690. var $file;
  1691. /**
  1692. * ?????????
  1693. *
  1694. * @var int
  1695. */
  1696. var $line;
  1697. /**
  1698. * ????
  1699. *
  1700. * @var array
  1701. */
  1702. var $trac;
  1703. /**
  1704. * ????
  1705. *
  1706. * @param string $message
  1707. * @param int $code
  1708. *
  1709. * @return FLEA_Exception
  1710. */
  1711. function FLEA_Exception($message = null, $code = 0)
  1712. {
  1713. $this->message = $message;
  1714. $this->code = $code;
  1715. $this->trac = debug_backtrace();
  1716. // ??????????????
  1717. $last = array_shift($this->trac);
  1718. $this->file = $last['file'];
  1719. $this->line = $last['line'];
  1720. }
  1721. /**
  1722. * ????????
  1723. *
  1724. * @return string
  1725. */
  1726. function getMessage()
  1727. {
  1728. return $this->message;
  1729. }
  1730. /**
  1731. * ????????
  1732. *
  1733. * @return int
  1734. */
  1735. function getCode()
  1736. {
  1737. return $this->code;
  1738. }
  1739. /**
  1740. * ??????????
  1741. *
  1742. * @return string
  1743. */
  1744. function getFile()
  1745. {
  1746. return $this->file;
  1747. }
  1748. /**
  1749. * ???????????
  1750. *
  1751. * @return int
  1752. */
  1753. function getLine()
  1754. {
  1755. return $this->line;
  1756. }
  1757. /**
  1758. * ??????
  1759. *
  1760. * @return array
  1761. */
  1762. function getTrace()
  1763. {
  1764. return $this->trac;
  1765. }
  1766. /**
  1767. * ????????????
  1768. */
  1769. function getTraceAsString()
  1770. {
  1771. $out = '';
  1772. $ix = 0;
  1773. foreach ($this->trac as $point) {
  1774. $out .= "#{$ix} {$point['file']}({$point['line']}): {$point['function']}(";
  1775. if (is_array($point['args']) && count($point['args']) > 0) {
  1776. foreach ($point['args'] as $arg) {
  1777. switch (gettype($arg)) {
  1778. case 'array':
  1779. case 'resource':
  1780. $out .= gettype($arg);
  1781. break;
  1782. case 'object':
  1783. $out .= get_class($arg);
  1784. break;
  1785. case 'string':
  1786. if (strlen($arg) > 30) {
  1787. $arg = substr($arg, 0, 27) . ' ...';
  1788. }
  1789. $out .= "'{$arg}'";
  1790. break;
  1791. default:
  1792. $out .= $arg;
  1793. }
  1794. $out .= ', ';
  1795. }
  1796. $out = substr($out, 0, -2);
  1797. }
  1798. $out .= ")\n";
  1799. $ix++;
  1800. }
  1801. $out .= "#{$ix} {main}\n";
  1802. return $out;
  1803. }
  1804. /**
  1805. * ??????????
  1806. *
  1807. * @return string
  1808. */
  1809. function __toString()
  1810. {
  1811. $out = "exception '" . get_class($this) . "'";
  1812. if ($this->message != '') {
  1813. $out .= " with message '{$this->message}'";
  1814. }
  1815. $out .= " in {$this->file}:{$this->line}\n\n";
  1816. $out .= $this->getTraceAsString();
  1817. return $out;
  1818. }
  1819. }
  1820. }
  1821. /**
  1822. * Ajax ?????
  1823. */
  1824. /**
  1825. * ????? PHP ??? JSON ?????? PEAR::Service_JSON ??? JSON ??????
  1826. *
  1827. * ?????? PHP ???? JSON ?????????????
  1828. */
  1829. if (!function_exists('json_encode')) {
  1830. /**
  1831. * ?????? JSON ???
  1832. *
  1833. * @param mixed $value
  1834. *
  1835. * @return string
  1836. */
  1837. function json_encode($value)
  1838. {
  1839. static $instance = array();
  1840. if (!isset($instance[0])) {
  1841. require_once(FLEA_DIR . '/Ajax/JSON.php');
  1842. $instance[0] =& new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  1843. }
  1844. return $instance[0]->encode($value);
  1845. }
  1846. }
  1847. if (!function_exists('json_decode')) {
  1848. /**
  1849. * ? JSON ????????
  1850. *
  1851. * @param string $jsonString
  1852. *
  1853. * @return mixed
  1854. */
  1855. function json_decode($jsonString)
  1856. {
  1857. static $instance = array();
  1858. if (!isset($instance[0])) {
  1859. require_once(FLEA_DIR . '/Ajax/JSON.php');
  1860. $instance[0] =& new Services_JSON(SERVICES_JSON_LOOSE_TYPE);
  1861. }
  1862. return $instance[0]->decode($jsonString);
  1863. }
  1864. }