basics.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464
  1. <?php
  2. /* SVN FILE: $Id$ */
  3. /**
  4. * Basic Cake functionality.
  5. *
  6. * Core functions for including other source files, loading models and so forth.
  7. *
  8. * PHP versions 4 and 5
  9. *
  10. * CakePHP(tm) : Rapid Development Framework <http://www.cakephp.org/>
  11. * Copyright 2005-2007, Cake Software Foundation, Inc.
  12. * 1785 E. Sahara Avenue, Suite 490-204
  13. * Las Vegas, Nevada 89104
  14. *
  15. * Licensed under The MIT License
  16. * Redistributions of files must retain the above copyright notice.
  17. *
  18. * @filesource
  19. * @copyright Copyright 2005-2007, Cake Software Foundation, Inc.
  20. * @link http://www.cakefoundation.org/projects/info/cakephp CakePHP(tm) Project
  21. * @package cake
  22. * @subpackage cake.cake
  23. * @since CakePHP(tm) v 0.2.9
  24. * @version $Revision$
  25. * @modifiedby $LastChangedBy$
  26. * @lastmodified $Date$
  27. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  28. */
  29. /**
  30. * Basic defines for timing functions.
  31. */
  32. define('SECOND', 1);
  33. define('MINUTE', 60 * SECOND);
  34. define('HOUR', 60 * MINUTE);
  35. define('DAY', 24 * HOUR);
  36. define('WEEK', 7 * DAY);
  37. define('MONTH', 30 * DAY);
  38. define('YEAR', 365 * DAY);
  39. /**
  40. * Patch for PHP < 5.0
  41. */
  42. if (version_compare(phpversion(), '5.0') < 0) {
  43. eval ('
  44. function clone($object)
  45. {
  46. return $object;
  47. }');
  48. }
  49. /**
  50. * Loads all models.
  51. */
  52. function loadModels() {
  53. if(!class_exists('Model')){
  54. require LIBS . 'model' . DS . 'model.php';
  55. }
  56. $path = Configure::getInstance();
  57. if (!class_exists('AppModel')) {
  58. if (file_exists(APP . 'app_model.php')) {
  59. require(APP . 'app_model.php');
  60. } else {
  61. require(CAKE . 'app_model.php');
  62. }
  63. Overloadable::overload('AppModel');
  64. }
  65. $loadedModels = array();
  66. foreach($path->modelPaths as $path) {
  67. foreach(listClasses($path) as $model_fn) {
  68. list($name) = explode('.', $model_fn);
  69. $className = Inflector::camelize($name);
  70. $loadedModels[$model_fn] = $model_fn;
  71. if (!class_exists($className)) {
  72. require($path . $model_fn);
  73. list($name) = explode('.', $model_fn);
  74. Overloadable::overload(Inflector::camelize($name));
  75. }
  76. }
  77. }
  78. return $loadedModels;
  79. }
  80. /**
  81. * Loads all plugin models.
  82. *
  83. * @param string $plugin Name of plugin
  84. * @return
  85. */
  86. function loadPluginModels($plugin) {
  87. if(!class_exists('AppModel')){
  88. loadModel();
  89. }
  90. $pluginAppModel = Inflector::camelize($plugin . '_app_model');
  91. $pluginAppModelFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_model.php';
  92. if (!class_exists($pluginAppModel)) {
  93. if (file_exists($pluginAppModelFile)) {
  94. require($pluginAppModelFile);
  95. Overloadable::overload($pluginAppModel);
  96. }
  97. }
  98. $pluginModelDir = APP . 'plugins' . DS . $plugin . DS . 'models' . DS;
  99. if(is_dir($pluginModelDir)) {
  100. foreach(listClasses($pluginModelDir)as $modelFileName) {
  101. list($name) = explode('.', $modelFileName);
  102. $className = Inflector::camelize($name);
  103. if (!class_exists($className)) {
  104. require($pluginModelDir . $modelFileName);
  105. Overloadable::overload($className);
  106. }
  107. }
  108. }
  109. }
  110. /**
  111. * Loads custom view class.
  112. *
  113. */
  114. function loadView($viewClass) {
  115. if (!class_exists($viewClass . 'View')) {
  116. $paths = Configure::getInstance();
  117. $file = Inflector::underscore($viewClass) . '.php';
  118. foreach($paths->viewPaths as $path) {
  119. if (file_exists($path . $file)) {
  120. return require($path . $file);
  121. }
  122. }
  123. if ($viewFile = fileExistsInPath(LIBS . 'view' . DS . $file)) {
  124. if (file_exists($viewFile)) {
  125. require($viewFile);
  126. return true;
  127. } else {
  128. return false;
  129. }
  130. }
  131. }
  132. }
  133. /**
  134. * Loads a model by CamelCase name.
  135. */
  136. function loadModel($name = null) {
  137. if(!class_exists('Model')){
  138. require LIBS . 'model' . DS . 'model.php';
  139. }
  140. if (!class_exists('AppModel')) {
  141. if (file_exists(APP . 'app_model.php')) {
  142. require(APP . 'app_model.php');
  143. } else {
  144. require(CAKE . 'app_model.php');
  145. }
  146. Overloadable::overload('AppModel');
  147. }
  148. if(strpos($name, '.') !== false){
  149. list($plugin, $name) = explode('.', $name);
  150. }
  151. if (!is_null($name) && !class_exists($name)) {
  152. $className = $name;
  153. $name = Inflector::underscore($name);
  154. $models = Configure::read('Models');
  155. if(is_array($models)) {
  156. if(array_key_exists($className, $models)) {
  157. require($models[$className]['path']);
  158. Overloadable::overload($className);
  159. return true;
  160. } elseif(isset($models['Core']) && array_key_exists($className, $models['Core'])) {
  161. require($models['Core'][$className]['path']);
  162. Overloadable::overload($className);
  163. return true;
  164. }
  165. }
  166. $paths = Configure::getInstance();
  167. foreach($paths->modelPaths as $path) {
  168. if (file_exists($path . $name . '.php')) {
  169. Configure::store('Models', 'class.paths', array($className => array('path' => $path . $name . '.php')));
  170. require($path . $name . '.php');
  171. Overloadable::overload($className);
  172. return true;
  173. }
  174. }
  175. return false;
  176. } else {
  177. return true;
  178. }
  179. }
  180. function paths(){
  181. $directories = Configure::getInstance();
  182. $paths = array();
  183. foreach($directories->modelPaths as $path) {
  184. $paths['Models'][] = $path;
  185. }
  186. foreach($directories->behaviorPaths as $path) {
  187. $paths['Behaviors'][] = $path;
  188. }
  189. foreach($directories->controllerPaths as $path) {
  190. $paths['Controllers'][] = $path;
  191. }
  192. foreach($directories->componentPaths as $path) {
  193. $paths['Components'][] = $path;
  194. }
  195. foreach($directories->helperPaths as $path) {
  196. $paths['Helpers'][] = $path;
  197. }
  198. if(!class_exists('Folder')){
  199. uses('Folder');
  200. }
  201. $folder =& new Folder(APP.'plugins'.DS);
  202. $plugins = $folder->ls();
  203. $classPaths = array('models', 'models'.DS.'behaviors', 'controllers', 'controllers'.DS.'components', 'views'.DS.'helpers');
  204. foreach($plugins[0] as $plugin){
  205. foreach($classPaths as $path){
  206. if(strpos($path, DS) !== false){
  207. $key = explode(DS, $path);
  208. $key = $key[1];
  209. } else {
  210. $key = $path;
  211. }
  212. $folder->path = APP.'plugins'.DS.$plugin.DS.$path;
  213. $paths[Inflector::camelize($plugin)][Inflector::camelize($key)][] = $folder->path;
  214. }
  215. }
  216. return $paths;
  217. }
  218. /**
  219. * Loads all controllers.
  220. */
  221. function loadControllers() {
  222. $paths = Configure::getInstance();
  223. if (!class_exists('AppController')) {
  224. if (file_exists(APP . 'app_controller.php')) {
  225. require(APP . 'app_controller.php');
  226. } else {
  227. require(CAKE . 'app_controller.php');
  228. }
  229. }
  230. $loadedControllers = array();
  231. foreach($paths->controllerPaths as $path) {
  232. foreach(listClasses($path) as $controller) {
  233. list($name) = explode('.', $controller);
  234. $className = Inflector::camelize($name);
  235. if (loadController($name)) {
  236. $loadedControllers[$controller] = $className;
  237. }
  238. }
  239. }
  240. return $loadedControllers;
  241. }
  242. /**
  243. * Loads a controller and its helper libraries.
  244. *
  245. * @param string $name Name of controller
  246. * @return boolean Success
  247. */
  248. function loadController($name) {
  249. if (!class_exists('AppController')) {
  250. if (file_exists(APP . 'app_controller.php')) {
  251. require(APP . 'app_controller.php');
  252. } else {
  253. require(CAKE . 'app_controller.php');
  254. }
  255. }
  256. if ($name === null) {
  257. return true;
  258. }
  259. if(strpos($name, '.') !== false){
  260. list($plugin, $name) = explode('.', $name);
  261. }
  262. $className = $name . 'Controller';
  263. if (!class_exists($className)) {
  264. $name = Inflector::underscore($className);
  265. $controllers = Configure::read('Controllers');
  266. if(is_array($controllers)) {
  267. if(array_key_exists($className, $controllers)) {
  268. require($controllers[$className]['path']);
  269. return true;
  270. } elseif(isset($controllers['Core']) && array_key_exists($className, $controllers['Core'])) {
  271. require($controllers['Core'][$className]['path']);
  272. return true;
  273. }
  274. }
  275. $paths = Configure::getInstance();
  276. foreach($paths->controllerPaths as $path) {
  277. if (file_exists($path . $name . '.php')) {
  278. Configure::store('Controllers', 'class.paths', array($className => array('path' => $path . $name . '.php')));
  279. require($path . $name . '.php');
  280. return true;
  281. }
  282. }
  283. if ($controllerFilename = fileExistsInPath(LIBS . 'controller' . DS . $name . '.php')) {
  284. if (file_exists($controllerFilename)) {
  285. Configure::store('Controllers\'][\'Core', 'class.paths', array($className => array('path' => $controllerFilename)));
  286. require($controllerFilename);
  287. return true;
  288. } else {
  289. return false;
  290. }
  291. }
  292. } else {
  293. return false;
  294. }
  295. }
  296. /**
  297. * Loads a plugin's controller.
  298. *
  299. * @param string $plugin Name of plugin
  300. * @param string $controller Name of controller to load
  301. * @return boolean Success
  302. */
  303. function loadPluginController($plugin, $controller) {
  304. $pluginAppController = Inflector::camelize($plugin . '_app_controller');
  305. $pluginAppControllerFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_controller.php';
  306. if (!class_exists($pluginAppController)) {
  307. if (file_exists($pluginAppControllerFile)) {
  308. require($pluginAppControllerFile);
  309. } else {
  310. return false;
  311. }
  312. }
  313. if (empty($controller)) {
  314. if (!class_exists($plugin . 'Controller')) {
  315. if (file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) {
  316. require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php');
  317. return true;
  318. }
  319. }
  320. }
  321. if (!class_exists($controller . 'Controller')) {
  322. $controller = Inflector::underscore($controller);
  323. $file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $controller . '_controller.php';
  324. if (file_exists($file)) {
  325. require($file);
  326. return true;
  327. } elseif (!class_exists(Inflector::camelize($plugin) . 'Controller')){
  328. if(file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) {
  329. require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php');
  330. return true;
  331. } else {
  332. return false;
  333. }
  334. }
  335. }
  336. return true;
  337. }
  338. /**
  339. * Loads a helper
  340. *
  341. * @param string $name Name of helper
  342. * @return boolean Success
  343. */
  344. function loadHelper($name) {
  345. if (!class_exists('AppHelper')) {
  346. if (file_exists(APP . 'app_helper.php')) {
  347. require(APP . 'app_helper.php');
  348. } else {
  349. require(CAKE . 'app_helper.php');
  350. }
  351. Overloadable::overload('AppHelper');
  352. }
  353. if ($name === null) {
  354. return true;
  355. }
  356. if(strpos($name, '.') !== false){
  357. list($plugin, $name) = explode('.', $name);
  358. }
  359. $className = $name . 'Helper';
  360. if (!class_exists($className)) {
  361. $name = Inflector::underscore($name);
  362. $helpers = Configure::read('Helpers');
  363. if(is_array($helpers)) {
  364. if(array_key_exists($className, $helpers)) {
  365. require($helpers[$className]['path']);
  366. return true;
  367. } elseif(isset($helpers['Core']) && array_key_exists($className, $helpers['Core'])) {
  368. require($helpers['Core'][$className]['path']);
  369. return true;
  370. }
  371. }
  372. $paths = Configure::getInstance();
  373. foreach($paths->helperPaths as $path) {
  374. if (file_exists($path . $name . '.php')) {
  375. Configure::store('Helpers', 'class.paths', array($className => array('path' => $path . $name . '.php')));
  376. require($path . $name . '.php');
  377. return true;
  378. }
  379. }
  380. if ($helperFilename = fileExistsInPath(LIBS . 'view' . DS . 'helpers' . DS . $name . '.php')) {
  381. if (file_exists($helperFilename)) {
  382. Configure::store('Helpers\'][\'Core', 'class.paths', array($className => array('path' => $helperFilename)));
  383. require($helperFilename);
  384. return true;
  385. } else {
  386. return false;
  387. }
  388. }
  389. } else {
  390. return true;
  391. }
  392. }
  393. /**
  394. * Loads a plugin's helper
  395. *
  396. * @param string $plugin Name of plugin
  397. * @param string $helper Name of helper to load
  398. * @return boolean Success
  399. */
  400. function loadPluginHelper($plugin, $helper) {
  401. loadHelper(null);
  402. if (!class_exists($helper . 'Helper')) {
  403. $helper = Inflector::underscore($helper);
  404. $file = APP . 'plugins' . DS . $plugin . DS . 'views' . DS . 'helpers' . DS . $helper . '.php';
  405. if (file_exists($file)) {
  406. require($file);
  407. return true;
  408. } else {
  409. return false;
  410. }
  411. } else {
  412. return true;
  413. }
  414. }
  415. /**
  416. * Loads a component
  417. *
  418. * @param string $name Name of component
  419. * @return boolean Success
  420. */
  421. function loadComponent($name) {
  422. if ($name === null) {
  423. return true;
  424. }
  425. if(strpos($name, '.') !== false){
  426. list($plugin, $name) = explode('.', $name);
  427. }
  428. $className = $name . 'Component';
  429. if (!class_exists($className)) {
  430. $name = Inflector::underscore($name);
  431. $components = Configure::read('Components');
  432. if(is_array($components)) {
  433. if(array_key_exists($className, $components)) {
  434. require($components[$className]['path']);
  435. return true;
  436. } elseif(isset($components['Core']) && array_key_exists($className, $components['Core'])) {
  437. require($components['Core'][$className]['path']);
  438. return true;
  439. }
  440. }
  441. $paths = Configure::getInstance();
  442. foreach($paths->componentPaths as $path) {
  443. if (file_exists($path . $name . '.php')) {
  444. Configure::store('Components', 'class.paths', array($className => array('path' => $path . $name . '.php')));
  445. require($path . $name . '.php');
  446. return true;
  447. }
  448. }
  449. if ($componentFilename = fileExistsInPath(LIBS . 'controller' . DS . 'components' . DS . $name . '.php')) {
  450. if (file_exists($componentFilename)) {
  451. Configure::store('Components\'][\'Core', 'class.paths', array($className => array('path' => $componentFilename)));
  452. require($componentFilename);
  453. return true;
  454. } else {
  455. return false;
  456. }
  457. }
  458. } else {
  459. return true;
  460. }
  461. }
  462. /**
  463. * Loads a plugin's component
  464. *
  465. * @param string $plugin Name of plugin
  466. * @param string $helper Name of component to load
  467. * @return boolean Success
  468. */
  469. function loadPluginComponent($plugin, $component) {
  470. if (!class_exists($component . 'Component')) {
  471. $component = Inflector::underscore($component);
  472. $file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . 'components' . DS . $component . '.php';
  473. if (file_exists($file)) {
  474. require($file);
  475. return true;
  476. } else {
  477. return false;
  478. }
  479. } else {
  480. return true;
  481. }
  482. }
  483. /**
  484. * Loads a behavior
  485. *
  486. * @param string $name Name of component
  487. * @return boolean Success
  488. */
  489. function loadBehavior($name) {
  490. $paths = Configure::getInstance();
  491. if ($name === null) {
  492. return true;
  493. }
  494. if (!class_exists($name . 'Behavior')) {
  495. $name = Inflector::underscore($name);
  496. foreach($paths->behaviorPaths as $path) {
  497. if (file_exists($path . $name . '.php')) {
  498. require($path . $name . '.php');
  499. return true;
  500. }
  501. }
  502. if ($behavior_fn = fileExistsInPath(LIBS . 'model' . DS . 'behaviors' . DS . $name . '.php')) {
  503. if (file_exists($behavior_fn)) {
  504. require($behavior_fn);
  505. return true;
  506. } else {
  507. return false;
  508. }
  509. }
  510. } else {
  511. return true;
  512. }
  513. }
  514. /**
  515. * Returns an array of filenames of PHP files in given directory.
  516. *
  517. * @param string $path Path to scan for files
  518. * @return array List of files in directory
  519. */
  520. function listClasses($path) {
  521. $dir = opendir($path);
  522. $classes=array();
  523. while(false !== ($file = readdir($dir))) {
  524. if ((substr($file, -3, 3) == 'php') && substr($file, 0, 1) != '.') {
  525. $classes[] = $file;
  526. }
  527. }
  528. closedir($dir);
  529. return $classes;
  530. }
  531. /**
  532. * Loads configuration files
  533. *
  534. * @return boolean Success
  535. */
  536. function config() {
  537. $args = func_get_args();
  538. foreach($args as $arg) {
  539. if (('database' == $arg) && file_exists(CONFIGS . $arg . '.php')) {
  540. include_once(CONFIGS . $arg . '.php');
  541. } elseif (file_exists(CONFIGS . $arg . '.php')) {
  542. include_once(CONFIGS . $arg . '.php');
  543. if (count($args) == 1) {
  544. return true;
  545. }
  546. } else {
  547. if (count($args) == 1) {
  548. return false;
  549. }
  550. }
  551. }
  552. return true;
  553. }
  554. /**
  555. * Loads component/components from LIBS.
  556. *
  557. * Example:
  558. * <code>
  559. * uses('flay', 'time');
  560. * </code>
  561. *
  562. * @uses LIBS
  563. */
  564. function uses() {
  565. $args = func_get_args();
  566. $c = func_num_args();
  567. for ($i = 0; $i < $c; $i++) {
  568. require_once(LIBS . strtolower($args[$i]) . '.php');
  569. }
  570. }
  571. /**
  572. * Require given files in the VENDORS directory. Takes optional number of parameters.
  573. *
  574. * @param string $name Filename without the .php part.
  575. *
  576. */
  577. function vendor($name) {
  578. $args = func_get_args();
  579. $c = func_num_args();
  580. for ($i = 0; $i < $c; $i++) {
  581. $arg = $args[$i];
  582. if (file_exists(APP . 'vendors' . DS . $arg . '.php')) {
  583. require_once(APP . 'vendors' . DS . $arg . '.php');
  584. } elseif (file_exists(VENDORS . $arg . '.php')) {
  585. require_once(VENDORS . $arg . '.php');
  586. } else {
  587. return false;
  588. }
  589. }
  590. return true;
  591. }
  592. /**
  593. * Prints out debug information about given variable.
  594. *
  595. * Only runs if DEBUG level is non-zero.
  596. *
  597. * @param boolean $var Variable to show debug information for.
  598. * @param boolean $show_html If set to true, the method prints the debug data in a screen-friendly way.
  599. */
  600. function debug($var = false, $showHtml = false) {
  601. if (Configure::read() > 0) {
  602. print "\n<pre class=\"cake_debug\">\n";
  603. ob_start();
  604. print_r($var);
  605. $var = ob_get_clean();
  606. if ($showHtml) {
  607. $var = str_replace('<', '&lt;', str_replace('>', '&gt;', $var));
  608. }
  609. print "{$var}\n</pre>\n";
  610. }
  611. }
  612. /**
  613. * Returns microtime for execution time checking
  614. *
  615. * @return integer
  616. */
  617. if (!function_exists('getMicrotime')) {
  618. function getMicrotime() {
  619. list($usec, $sec) = explode(" ", microtime());
  620. return ((float)$usec + (float)$sec);
  621. }
  622. }
  623. /**
  624. * Sorts given $array by key $sortby.
  625. *
  626. * @param array $array
  627. * @param string $sortby
  628. * @param string $order Sort order asc/desc (ascending or descending).
  629. * @param integer $type
  630. * @return mixed
  631. */
  632. if (!function_exists('sortByKey')) {
  633. function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
  634. if (!is_array($array)) {
  635. return null;
  636. }
  637. foreach($array as $key => $val) {
  638. $sa[$key] = $val[$sortby];
  639. }
  640. if ($order == 'asc') {
  641. asort($sa, $type);
  642. } else {
  643. arsort($sa, $type);
  644. }
  645. foreach($sa as $key => $val) {
  646. $out[] = $array[$key];
  647. }
  648. return $out;
  649. }
  650. }
  651. /**
  652. * Combines given identical arrays by using the first array's values as keys,
  653. * and the second one's values as values. (Implemented for back-compatibility with PHP4)
  654. *
  655. * @param array $a1
  656. * @param array $a2
  657. * @return mixed Outputs either combined array or false.
  658. */
  659. if (!function_exists('array_combine')) {
  660. function array_combine($a1, $a2) {
  661. $a1 = array_values($a1);
  662. $a2 = array_values($a2);
  663. $c1 = count($a1);
  664. $c2 = count($a2);
  665. if ($c1 != $c2) {
  666. return false;
  667. }
  668. if ($c1 <= 0) {
  669. return false;
  670. }
  671. $output=array();
  672. for($i = 0; $i < $c1; $i++) {
  673. $output[$a1[$i]] = $a2[$i];
  674. }
  675. return $output;
  676. }
  677. }
  678. /**
  679. * Convenience method for htmlspecialchars.
  680. *
  681. * @param string $text
  682. * @return string
  683. */
  684. function h($text) {
  685. if (is_array($text)) {
  686. return array_map('h', $text);
  687. }
  688. return htmlspecialchars($text);
  689. }
  690. /**
  691. * Returns an array of all the given parameters.
  692. *
  693. * Example:
  694. * <code>
  695. * a('a', 'b')
  696. * </code>
  697. *
  698. * Would return:
  699. * <code>
  700. * array('a', 'b')
  701. * </code>
  702. *
  703. * @return array
  704. */
  705. function a() {
  706. $args = func_get_args();
  707. return $args;
  708. }
  709. /**
  710. * Constructs associative array from pairs of arguments.
  711. *
  712. * Example:
  713. * <code>
  714. * aa('a','b')
  715. * </code>
  716. *
  717. * Would return:
  718. * <code>
  719. * array('a'=>'b')
  720. * </code>
  721. *
  722. * @return array
  723. */
  724. function aa() {
  725. $args = func_get_args();
  726. for($l = 0, $c = count($args); $l < $c; $l++) {
  727. if ($l + 1 < count($args)) {
  728. $a[$args[$l]] = $args[$l + 1];
  729. } else {
  730. $a[$args[$l]] = null;
  731. }
  732. $l++;
  733. }
  734. return $a;
  735. }
  736. /**
  737. * Convenience method for echo().
  738. *
  739. * @param string $text String to echo
  740. */
  741. function e($text) {
  742. echo $text;
  743. }
  744. /**
  745. * Convenience method for strtolower().
  746. *
  747. * @param string $str String to lowercase
  748. */
  749. function low($str) {
  750. return strtolower($str);
  751. }
  752. /**
  753. * Convenience method for strtoupper().
  754. *
  755. * @param string $str String to uppercase
  756. */
  757. function up($str) {
  758. return strtoupper($str);
  759. }
  760. /**
  761. * Convenience method for str_replace().
  762. *
  763. * @param string $search String to be replaced
  764. * @param string $replace String to insert
  765. * @param string $subject String to search
  766. */
  767. function r($search, $replace, $subject) {
  768. return str_replace($search, $replace, $subject);
  769. }
  770. /**
  771. * Print_r convenience function, which prints out <PRE> tags around
  772. * the output of given array. Similar to debug().
  773. *
  774. * @see debug()
  775. * @param array $var
  776. */
  777. function pr($var) {
  778. if (Configure::read() > 0) {
  779. echo "<pre>";
  780. print_r($var);
  781. echo "</pre>";
  782. }
  783. }
  784. /**
  785. * Display parameter
  786. *
  787. * @param mixed $p Parameter as string or array
  788. * @return string
  789. */
  790. function params($p) {
  791. if (!is_array($p) || count($p) == 0) {
  792. return null;
  793. } else {
  794. if (is_array($p[0]) && count($p) == 1) {
  795. return $p[0];
  796. } else {
  797. return $p;
  798. }
  799. }
  800. }
  801. /**
  802. * Merge a group of arrays
  803. *
  804. * @param array First array
  805. * @param array Second array
  806. * @param array Third array
  807. * @param array Etc...
  808. * @return array All array parameters merged into one
  809. */
  810. function am() {
  811. $r = array();
  812. foreach(func_get_args()as $a) {
  813. if (!is_array($a)) {
  814. $a = array($a);
  815. }
  816. $r = array_merge($r, $a);
  817. }
  818. return $r;
  819. }
  820. /**
  821. * Returns the REQUEST_URI from the server environment, or, failing that,
  822. * constructs a new one, using the PHP_SELF constant and other variables.
  823. *
  824. * @return string URI
  825. */
  826. function setUri() {
  827. if (env('HTTP_X_REWRITE_URL')) {
  828. $uri = env('HTTP_X_REWRITE_URL');
  829. } elseif(env('REQUEST_URI')) {
  830. $uri = env('REQUEST_URI');
  831. } else {
  832. if ($uri = env('argv')) {
  833. if (defined('SERVER_IIS') && SERVER_IIS) {
  834. if (key($_GET) && strpos(key($_GET), '?') !== false) {
  835. unset($_GET[key($_GET)]);
  836. }
  837. $uri = preg_split('/\?/', $uri[0], 2);
  838. if (isset($uri[1])) {
  839. foreach (preg_split('/&/', $uri[1]) as $var) {
  840. @list($key, $val) = explode('=', $var);
  841. $_GET[$key] = $val;
  842. }
  843. }
  844. $uri = BASE_URL . $uri[0];
  845. } else {
  846. $uri = env('PHP_SELF') . '/' . $uri[0];
  847. }
  848. } else {
  849. $uri = env('PHP_SELF') . '/' . env('QUERY_STRING');
  850. }
  851. }
  852. return preg_replace('/\?url=\//', '', $uri);
  853. }
  854. /**
  855. * Gets an environment variable from available sources, and provides emulation
  856. * for unsupported or inconsisten environment variables (i.e. DOCUMENT_ROOT on
  857. * IIS, or SCRIPT_NAME in CGI mode). Also exposes some additional custom
  858. * environment information.
  859. *
  860. * @param string $key Environment variable name.
  861. * @return string Environment variable setting.
  862. */
  863. function env($key) {
  864. if ($key == 'HTTPS') {
  865. if (isset($_SERVER) && !empty($_SERVER)) {
  866. return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
  867. } else {
  868. return (strpos(env('SCRIPT_URI'), 'https://') === 0);
  869. }
  870. }
  871. if ($key == 'SCRIPT_NAME') {
  872. if (env('CGI_MODE')) {
  873. $key = 'SCRIPT_URL';
  874. }
  875. }
  876. $val = null;
  877. if (isset($_SERVER[$key])) {
  878. $val = $_SERVER[$key];
  879. } elseif (isset($_ENV[$key])) {
  880. $val = $_ENV[$key];
  881. } elseif (getenv($key) !== false) {
  882. $val = getenv($key);
  883. }
  884. if ($key == 'REMOTE_ADDR' && $val == env('SERVER_ADDR')) {
  885. $addr = env('HTTP_PC_REMOTE_ADDR');
  886. if ($addr != null) {
  887. $val = $addr;
  888. }
  889. }
  890. if ($val !== null) {
  891. return $val;
  892. }
  893. switch ($key) {
  894. case 'DOCUMENT_ROOT':
  895. $offset = 0;
  896. if (!strpos(env('SCRIPT_NAME'), '.php')) {
  897. $offset = 4;
  898. }
  899. return substr(env('SCRIPT_FILENAME'), 0, strlen(env('SCRIPT_FILENAME')) - (strlen(env('SCRIPT_NAME')) + $offset));
  900. break;
  901. case 'PHP_SELF':
  902. return r(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
  903. break;
  904. case 'CGI_MODE':
  905. return (substr(php_sapi_name(), 0, 3) == 'cgi');
  906. break;
  907. case 'HTTP_BASE':
  908. return preg_replace ('/^([^.])*/i', null, env('HTTP_HOST'));
  909. break;
  910. }
  911. return null;
  912. }
  913. /**
  914. * Writes data into file.
  915. *
  916. * If file exists, it will be overwritten. If data is an array, it will be join()ed with an empty string.
  917. *
  918. * @param string $fileName File name.
  919. * @param mixed $data String or array.
  920. */
  921. if (!function_exists('file_put_contents')) {
  922. function file_put_contents($fileName, $data) {
  923. if (is_array($data)) {
  924. $data = join('', $data);
  925. }
  926. $res = @fopen($fileName, 'w+b');
  927. if ($res) {
  928. $write = @fwrite($res, $data);
  929. if($write === false) {
  930. return false;
  931. } else {
  932. return $write;
  933. }
  934. }
  935. return false;
  936. }
  937. }
  938. /**
  939. * Reads/writes temporary data to cache files or session.
  940. *
  941. * @param string $path File path within /tmp to save the file.
  942. * @param mixed $data The data to save to the temporary file.
  943. * @param mixed $expires A valid strtotime string when the data expires.
  944. * @param string $target The target of the cached data; either 'cache' or 'public'.
  945. * @return mixed The contents of the temporary file.
  946. */
  947. function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
  948. if (defined('DISABLE_CACHE')) {
  949. return null;
  950. }
  951. $now = time();
  952. if (!is_numeric($expires)) {
  953. $expires = strtotime($expires, $now);
  954. }
  955. switch(strtolower($target)) {
  956. case 'cache':
  957. $filename = CACHE . $path;
  958. break;
  959. case 'public':
  960. $filename = WWW_ROOT . $path;
  961. break;
  962. case 'tmp':
  963. $filename = TMP . $path;
  964. break;
  965. }
  966. $timediff = $expires - $now;
  967. $filetime = false;
  968. if(file_exists($filename)) {
  969. $filetime = @filemtime($filename);
  970. }
  971. if ($data === null) {
  972. // Read data from file
  973. if (file_exists($filename) && $filetime !== false) {
  974. if ($filetime + $timediff < $now) {
  975. // File has expired
  976. @unlink($filename);
  977. } else {
  978. $data = file_get_contents($filename);
  979. }
  980. }
  981. } else if(is_writable(dirname($filename))) {
  982. file_put_contents($filename, $data);
  983. }
  984. return $data;
  985. }
  986. /**
  987. * Used to delete files in the cache directories, or clear contents of cache directories
  988. *
  989. * @param mixed $params As String name to be searched for deletion, if name is a directory all files in directory will be deleted.
  990. * If array, names to be searched for deletion.
  991. * If clearCache() without params, all files in app/tmp/cache/views will be deleted
  992. *
  993. * @param string $type Directory in tmp/cache defaults to view directory
  994. * @param string $ext The file extension you are deleting
  995. * @return true if files found and deleted false otherwise
  996. */
  997. function clearCache($params = null, $type = 'views', $ext = '.php') {
  998. if (is_string($params) || $params === null) {
  999. $params = preg_replace('/\/\//', '/', $params);
  1000. $cache = CACHE . $type . DS . $params;
  1001. if (is_file($cache . $ext)) {
  1002. @unlink($cache . $ext);
  1003. return true;
  1004. } else if(is_dir($cache)) {
  1005. $files = glob("$cache*");
  1006. if ($files === false) {
  1007. return false;
  1008. }
  1009. foreach($files as $file) {
  1010. if (is_file($file)) {
  1011. @unlink($file);
  1012. }
  1013. }
  1014. return true;
  1015. } else {
  1016. $cache = CACHE . $type . DS . '*' . $params . '*' . $ext;
  1017. $files = glob($cache);
  1018. if ($files === false) {
  1019. return false;
  1020. }
  1021. foreach($files as $file) {
  1022. if (is_file($file)) {
  1023. @unlink($file);
  1024. }
  1025. }
  1026. return true;
  1027. }
  1028. } elseif (is_array($params)) {
  1029. foreach($params as $key => $file) {
  1030. $file = preg_replace('/\/\//', '/', $file);
  1031. $cache = CACHE . $type . DS . '*' . $file . '*' . $ext;
  1032. $files[] = glob($cache);
  1033. }
  1034. if (!empty($files)) {
  1035. foreach($files as $key => $delete) {
  1036. if (is_array($delete)) {
  1037. foreach($delete as $file) {
  1038. if (is_file($file)) {
  1039. @unlink($file);
  1040. }
  1041. }
  1042. }
  1043. }
  1044. return true;
  1045. } else {
  1046. return false;
  1047. }
  1048. } else {
  1049. return false;
  1050. }
  1051. }
  1052. /**
  1053. * Recursively strips slashes from all values in an array
  1054. *
  1055. * @param unknown_type $value
  1056. * @return unknown
  1057. */
  1058. function stripslashes_deep($value) {
  1059. if (is_array($value)) {
  1060. $return = array_map('stripslashes_deep', $value);
  1061. return $return;
  1062. } else {
  1063. $return = stripslashes($value);
  1064. return $return ;
  1065. }
  1066. }
  1067. /**
  1068. *
  1069. * Returns a translated string if one is found, or the submitted message if not found.
  1070. *
  1071. * @param string $singular
  1072. * @param boolean $return
  1073. * @return translated string if $return is false string will be echoed
  1074. */
  1075. function __($singular, $return = false) {
  1076. if(!class_exists('I18n')) {
  1077. uses('i18n');
  1078. }
  1079. $calledFrom = debug_backtrace();
  1080. $dir = dirname($calledFrom[0]['file']);
  1081. if($return === false) {
  1082. echo I18n::translate($singular, null, null, 5, null, $dir);
  1083. } else {
  1084. return I18n::translate($singular, null, null, 5, null, $dir);
  1085. }
  1086. }
  1087. /**
  1088. *
  1089. * Returns correct plural form of message identified by $singular and $plural for count $count.
  1090. * Some languages have more than one form for plural messages dependent on the count.
  1091. *
  1092. * @param string $singular
  1093. * @param string $plural
  1094. * @param integer $count
  1095. * @param boolean $return
  1096. * @return plural form of translated string if $return is false string will be echoed
  1097. */
  1098. function __n($singular, $plural, $count, $return = false) {
  1099. if(!class_exists('I18n')) {
  1100. uses('i18n');
  1101. }
  1102. $calledFrom = debug_backtrace();
  1103. $dir = dirname($calledFrom[0]['file']);
  1104. if($return === false) {
  1105. echo I18n::translate($singular, $plural, null, 5, $count, $dir);
  1106. } else {
  1107. return I18n::translate($singular, $plural, null, 5, $count, $dir);
  1108. }
  1109. }
  1110. /**
  1111. *
  1112. * Allows you to override the current domain for a single message lookup.
  1113. *
  1114. * @param string $domain
  1115. * @param string $msg
  1116. * @param string $return
  1117. * @return translated string if $return is false string will be echoed
  1118. */
  1119. function __d($domain, $msg, $return = false) {
  1120. if(!class_exists('I18n')) {
  1121. uses('i18n');
  1122. }
  1123. if($return === false) {
  1124. echo I18n::translate($msg, null, $domain);
  1125. } else {
  1126. return I18n::translate($msg, null, $domain);
  1127. }
  1128. }
  1129. /**
  1130. *
  1131. * Allows you to override the current domain for a single plural message lookup
  1132. * Returns correct plural form of message identified by $singular and $plural for count $count
  1133. * from domain $domain
  1134. *
  1135. * @param string $domain
  1136. * @param string $singular
  1137. * @param string $plural
  1138. * @param integer $count
  1139. * @param boolean $return
  1140. * @return plural form of translated string if $return is false string will be echoed
  1141. */
  1142. function __dn($domain, $singular, $plural, $count, $return = false) {
  1143. if(!class_exists('I18n')) {
  1144. uses('i18n');
  1145. }
  1146. if($return === false) {
  1147. echo I18n::translate($singular, $plural, $domain, 5, $count);
  1148. } else {
  1149. return I18n::translate($singular, $plural, $domain, 5, $count);
  1150. }
  1151. }
  1152. /**
  1153. *
  1154. * Allows you to override the current domain for a single message lookup.
  1155. * It also allows you to specify a category.
  1156. *
  1157. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  1158. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  1159. *
  1160. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  1161. * LC_CTYPE 0
  1162. * LC_NUMERIC 1
  1163. * LC_TIME 2
  1164. * LC_COLLATE 3
  1165. * LC_MONETARY 4
  1166. * LC_MESSAGES 5
  1167. * LC_ALL 6
  1168. *
  1169. * @param string $domain
  1170. * @param string $msg
  1171. * @param integer $category
  1172. * @param boolean $return
  1173. * @return translated string if $return is false string will be echoed
  1174. */
  1175. function __dc($domain, $msg, $category, $return = false) {
  1176. if(!class_exists('I18n')) {
  1177. uses('i18n');
  1178. }
  1179. if($return === false) {
  1180. echo I18n::translate($msg, null, $domain, $category);
  1181. } else {
  1182. return I18n::translate($msg, null, $domain, $category);
  1183. }
  1184. }
  1185. /**
  1186. *
  1187. * Allows you to override the current domain for a single plural message lookup.
  1188. * It also allows you to specify a category.
  1189. * Returns correct plural form of message identified by $singular and $plural for count $count
  1190. * from domain $domain
  1191. *
  1192. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  1193. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  1194. *
  1195. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  1196. * LC_CTYPE 0
  1197. * LC_NUMERIC 1
  1198. * LC_TIME 2
  1199. * LC_COLLATE 3
  1200. * LC_MONETARY 4
  1201. * LC_MESSAGES 5
  1202. * LC_ALL 6
  1203. *
  1204. * @param string $domain
  1205. * @param string $singular
  1206. * @param string $plural
  1207. * @param integer $count
  1208. * @param integer $category
  1209. * @param boolean $return
  1210. * @return plural form of translated string if $return is false string will be echoed
  1211. */
  1212. function __dcn($domain, $singular, $plural, $count, $category, $return = false) {
  1213. if(!class_exists('I18n')) {
  1214. uses('i18n');
  1215. }
  1216. if($return === false) {
  1217. echo I18n::translate($singular, $plural, $domain, $category, $count);
  1218. } else {
  1219. return I18n::translate($singular, $plural, $domain, $category, $count);
  1220. }
  1221. }
  1222. /**
  1223. *
  1224. * The category argument allows a specific category of the locale settings to be used for fetching a message.
  1225. * Valid categories are: LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES and LC_ALL.
  1226. *
  1227. * Note that the category must be specified with a numeric value, instead of the constant name. The values are:
  1228. * LC_CTYPE 0
  1229. * LC_NUMERIC 1
  1230. * LC_TIME 2
  1231. * LC_COLLATE 3
  1232. * LC_MONETARY 4
  1233. * LC_MESSAGES 5
  1234. * LC_ALL 6
  1235. *
  1236. * @param string $msg
  1237. * @param integer $category
  1238. * @param string $return
  1239. * @return translated string if $return is false string will be echoed
  1240. */
  1241. function __c($msg, $category, $return = false) {
  1242. if(!class_exists('I18n')) {
  1243. uses('i18n');
  1244. }
  1245. $calledFrom = debug_backtrace();
  1246. $dir = dirname($calledFrom[0]['file']);
  1247. if($return === false) {
  1248. echo I18n::translate($msg, null, null, $category, null, $dir);
  1249. } else {
  1250. return I18n::translate($msg, null, null, $category, null, $dir);
  1251. }
  1252. }
  1253. /**
  1254. * Computes the difference of arrays using keys for comparison
  1255. *
  1256. * @param array
  1257. * @param array
  1258. * @return array
  1259. */
  1260. if (!function_exists('array_diff_key')) {
  1261. function array_diff_key() {
  1262. $valuesDiff = array();
  1263. if (func_num_args() < 2) {
  1264. return false;
  1265. }
  1266. foreach (func_get_args() as $param) {
  1267. if (!is_array($param)) {
  1268. return false;
  1269. }
  1270. }
  1271. $args = func_get_args();
  1272. foreach ($args[0] as $valueKey => $valueData) {
  1273. for ($i = 1; $i < func_num_args(); $i++) {
  1274. if (isset($args[$i][$valueKey])) {
  1275. continue 2;
  1276. }
  1277. }
  1278. $valuesDiff[$valueKey] = $valueData;
  1279. }
  1280. return $valuesDiff;
  1281. }
  1282. }
  1283. /**
  1284. * Computes the intersection of arrays using keys for comparison
  1285. *
  1286. * @param array
  1287. * @param array
  1288. * @return array
  1289. */
  1290. if (!function_exists('array_intersect_key')) {
  1291. function array_intersect_key($arr1, $arr2) {
  1292. $res = array();
  1293. foreach($arr1 as $key=>$value) {
  1294. if(array_key_exists($key, $arr2)) {
  1295. $res[$key] = $arr1[$key];
  1296. }
  1297. }
  1298. return $res;
  1299. }
  1300. }
  1301. /**
  1302. * Shortcut to Log::write.
  1303. */
  1304. function LogError($message) {
  1305. if (!class_exists('CakeLog')) {
  1306. uses('cake_log');
  1307. }
  1308. $bad = array("\n", "\r", "\t");
  1309. $good = ' ';
  1310. CakeLog::write('error', str_replace($bad, $good, $message));
  1311. }
  1312. /**
  1313. * Searches include path for files
  1314. *
  1315. * @param string $file
  1316. * @return Full path to file if exists, otherwise false
  1317. */
  1318. function fileExistsInPath($file) {
  1319. $paths = explode(PATH_SEPARATOR, ini_get('include_path'));
  1320. foreach($paths as $path) {
  1321. $fullPath = $path . DIRECTORY_SEPARATOR . $file;
  1322. if (file_exists($fullPath)) {
  1323. return $fullPath;
  1324. } elseif (file_exists($file)) {
  1325. return $file;
  1326. }
  1327. }
  1328. return false;
  1329. }
  1330. /**
  1331. * Convert forward slashes to underscores and removes first and last underscores in a string
  1332. *
  1333. * @param string
  1334. * @return string with underscore remove from start and end of string
  1335. */
  1336. function convertSlash($string) {
  1337. $string = trim($string,"/");
  1338. $string = preg_replace('/\/\//', '/', $string);
  1339. $string = str_replace('/', '_', $string);
  1340. return $string;
  1341. }
  1342. /**
  1343. * chmod recursively on a directory
  1344. *
  1345. * @param string $path
  1346. * @param int $mode
  1347. * @return boolean
  1348. */
  1349. function chmodr($path, $mode = 0755) {
  1350. if (!is_dir($path)) {
  1351. return chmod($path, $mode);
  1352. }
  1353. $dir = opendir($path);
  1354. while($file = readdir($dir)) {
  1355. if ($file != '.' && $file != '..') {
  1356. $fullpath = $path . '/' . $file;
  1357. if (!is_dir($fullpath)) {
  1358. if (!chmod($fullpath, $mode)) {
  1359. return false;
  1360. }
  1361. } else {
  1362. if (!chmodr($fullpath, $mode)) {
  1363. return false;
  1364. }
  1365. }
  1366. }
  1367. }
  1368. closedir($dir);
  1369. if (chmod($path, $mode)) {
  1370. return true;
  1371. } else {
  1372. return false;
  1373. }
  1374. }
  1375. /**
  1376. * Wraps ternary operations. If $condition is a non-empty value, $val1 is returned, otherwise $val2.
  1377. *
  1378. * @param mixed $condition Conditional expression
  1379. * @param mixed $val1
  1380. * @param mixed $val2
  1381. * @return mixed $val1 or $val2, depending on whether $condition evaluates to a non-empty expression.
  1382. */
  1383. function ife($condition, $val1 = null, $val2 = null) {
  1384. if (!empty($condition)) {
  1385. return $val1;
  1386. }
  1387. return $val2;
  1388. }
  1389. ?>