1
|
<?php
|
2
|
/* SVN FILE: $Id: basics.php 4786 2007-04-05 17:57:00Z phpnut $ */
|
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: 4786 $
|
25
|
* @modifiedby $LastChangedBy: phpnut $
|
26
|
* @lastmodified $Date: 2007-04-05 12:57:00 -0500 (Thu, 05 Apr 2007) $
|
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 < 4.3
|
41
|
*/
|
42
|
if (!function_exists("ob_get_clean")) {
|
43
|
function ob_get_clean() {
|
44
|
$ob_contents = ob_get_contents();
|
45
|
ob_end_clean();
|
46
|
return $ob_contents;
|
47
|
}
|
48
|
}
|
49
|
/**
|
50
|
* Patch for PHP < 4.3
|
51
|
*/
|
52
|
if (version_compare(phpversion(), '5.0') < 0) {
|
53
|
eval ('
|
54
|
function clone($object) {
|
55
|
return $object;
|
56
|
}');
|
57
|
}
|
58
|
/**
|
59
|
* Computes the difference of arrays using keys for comparison
|
60
|
*
|
61
|
* @param array
|
62
|
* @param array
|
63
|
* @return array
|
64
|
*/
|
65
|
if (!function_exists('array_diff_key')) {
|
66
|
function array_diff_key() {
|
67
|
$valuesDiff = array();
|
68
|
|
69
|
if (func_num_args() < 2) {
|
70
|
return false;
|
71
|
}
|
72
|
|
73
|
foreach (func_get_args() as $param) {
|
74
|
if (!is_array($param)) {
|
75
|
return false;
|
76
|
}
|
77
|
}
|
78
|
|
79
|
$args = func_get_args();
|
80
|
foreach ($args[0] as $valueKey => $valueData) {
|
81
|
for ($i = 1; $i < func_num_args(); $i++) {
|
82
|
if (isset($args[$i][$valueKey])) {
|
83
|
continue 2;
|
84
|
}
|
85
|
}
|
86
|
$valuesDiff[$valueKey] = $valueData;
|
87
|
}
|
88
|
return $valuesDiff;
|
89
|
}
|
90
|
}
|
91
|
/**
|
92
|
* Computes the intersection of arrays using keys for comparison
|
93
|
*
|
94
|
* @param array
|
95
|
* @param array
|
96
|
* @return array
|
97
|
*/
|
98
|
if (!function_exists('array_intersect_key')) {
|
99
|
function array_intersect_key($arr1, $arr2) {
|
100
|
$res = array();
|
101
|
foreach($arr1 as $key=>$value) {
|
102
|
if(array_key_exists($key, $arr2)) {
|
103
|
$res[$key] = $arr1[$key];
|
104
|
}
|
105
|
}
|
106
|
return $res;
|
107
|
}
|
108
|
}
|
109
|
/**
|
110
|
* Loads all models.
|
111
|
*/
|
112
|
function loadModels() {
|
113
|
if(!class_exists('Model')){
|
114
|
require LIBS . 'model' . DS . 'model.php';
|
115
|
}
|
116
|
$path = Configure::getInstance();
|
117
|
if (!class_exists('AppModel')) {
|
118
|
if (file_exists(APP . 'app_model.php')) {
|
119
|
require(APP . 'app_model.php');
|
120
|
} else {
|
121
|
require(CAKE . 'app_model.php');
|
122
|
}
|
123
|
if (phpversion() < 5 && function_exists("overload")) {
|
124
|
overload('AppModel');
|
125
|
}
|
126
|
}
|
127
|
|
128
|
foreach($path->modelPaths as $path) {
|
129
|
foreach(listClasses($path)as $model_fn) {
|
130
|
list($name) = explode('.', $model_fn);
|
131
|
$className = Inflector::camelize($name);
|
132
|
|
133
|
if (!class_exists($className)) {
|
134
|
require($path . $model_fn);
|
135
|
|
136
|
if (phpversion() < 5 && function_exists("overload")) {
|
137
|
overload($className);
|
138
|
}
|
139
|
}
|
140
|
}
|
141
|
}
|
142
|
}
|
143
|
/**
|
144
|
* Loads all plugin models.
|
145
|
*
|
146
|
* @param string $plugin Name of plugin
|
147
|
* @return
|
148
|
*/
|
149
|
function loadPluginModels($plugin) {
|
150
|
if(!class_exists('AppModel')){
|
151
|
loadModel();
|
152
|
}
|
153
|
$pluginAppModel = Inflector::camelize($plugin . '_app_model');
|
154
|
$pluginAppModelFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_model.php';
|
155
|
if (!class_exists($pluginAppModel)) {
|
156
|
if (file_exists($pluginAppModelFile)) {
|
157
|
require($pluginAppModelFile);
|
158
|
} else {
|
159
|
die('Plugins must have a class named ' . $pluginAppModel);
|
160
|
}
|
161
|
if (phpversion() < 5 && function_exists("overload")) {
|
162
|
overload($pluginAppModel);
|
163
|
}
|
164
|
}
|
165
|
|
166
|
$pluginModelDir = APP . 'plugins' . DS . $plugin . DS . 'models' . DS;
|
167
|
|
168
|
foreach(listClasses($pluginModelDir)as $modelFileName) {
|
169
|
list($name) = explode('.', $modelFileName);
|
170
|
$className = Inflector::camelize($name);
|
171
|
|
172
|
if (!class_exists($className)) {
|
173
|
require($pluginModelDir . $modelFileName);
|
174
|
|
175
|
if (phpversion() < 5 && function_exists("overload")) {
|
176
|
overload($className);
|
177
|
}
|
178
|
}
|
179
|
}
|
180
|
}
|
181
|
/**
|
182
|
* Loads custom view class.
|
183
|
*
|
184
|
*/
|
185
|
function loadView($viewClass) {
|
186
|
if (!class_exists($viewClass . 'View')) {
|
187
|
$paths = Configure::getInstance();
|
188
|
$file = Inflector::underscore($viewClass) . '.php';
|
189
|
|
190
|
foreach($paths->viewPaths as $path) {
|
191
|
if (file_exists($path . $file)) {
|
192
|
return require($path . $file);
|
193
|
}
|
194
|
}
|
195
|
|
196
|
if ($viewFile = fileExistsInPath(LIBS . 'view' . DS . $file)) {
|
197
|
if (file_exists($viewFile)) {
|
198
|
require($viewFile);
|
199
|
return true;
|
200
|
} else {
|
201
|
return false;
|
202
|
}
|
203
|
}
|
204
|
}
|
205
|
}
|
206
|
/**
|
207
|
* Loads a model by CamelCase name.
|
208
|
*/
|
209
|
function loadModel($name = null) {
|
210
|
if(!class_exists('Model')){
|
211
|
require LIBS . 'model' . DS . 'model.php';
|
212
|
}
|
213
|
if (!class_exists('AppModel')) {
|
214
|
if (file_exists(APP . 'app_model.php')) {
|
215
|
require(APP . 'app_model.php');
|
216
|
} else {
|
217
|
require(CAKE . 'app_model.php');
|
218
|
}
|
219
|
if (phpversion() < 5 && function_exists("overload")) {
|
220
|
overload('AppModel');
|
221
|
}
|
222
|
}
|
223
|
|
224
|
if (!is_null($name) && !class_exists($name)) {
|
225
|
$className = $name;
|
226
|
$name = Inflector::underscore($name);
|
227
|
$paths = Configure::getInstance();
|
228
|
|
229
|
foreach($paths->modelPaths as $path) {
|
230
|
if (file_exists($path . $name . '.php')) {
|
231
|
require($path . $name . '.php');
|
232
|
if (phpversion() < 5 && function_exists("overload")) {
|
233
|
overload($className);
|
234
|
}
|
235
|
return true;
|
236
|
}
|
237
|
}
|
238
|
return false;
|
239
|
} else {
|
240
|
return true;
|
241
|
}
|
242
|
}
|
243
|
/**
|
244
|
* Loads all controllers.
|
245
|
*/
|
246
|
function loadControllers() {
|
247
|
$paths = Configure::getInstance();
|
248
|
if (!class_exists('AppController')) {
|
249
|
if (file_exists(APP . 'app_controller.php')) {
|
250
|
require(APP . 'app_controller.php');
|
251
|
} else {
|
252
|
require(CAKE . 'app_controller.php');
|
253
|
}
|
254
|
}
|
255
|
$loadedControllers = array();
|
256
|
|
257
|
foreach($paths->controllerPaths as $path) {
|
258
|
foreach(listClasses($path) as $controller) {
|
259
|
list($name) = explode('.', $controller);
|
260
|
$className = Inflector::camelize($name);
|
261
|
if (loadController($name)) {
|
262
|
$loadedControllers[$controller] = $className;
|
263
|
}
|
264
|
}
|
265
|
}
|
266
|
return $loadedControllers;
|
267
|
}
|
268
|
/**
|
269
|
* Loads a controller and its helper libraries.
|
270
|
*
|
271
|
* @param string $name Name of controller
|
272
|
* @return boolean Success
|
273
|
*/
|
274
|
function loadController($name) {
|
275
|
$paths = Configure::getInstance();
|
276
|
if (!class_exists('AppController')) {
|
277
|
if (file_exists(APP . 'app_controller.php')) {
|
278
|
require(APP . 'app_controller.php');
|
279
|
} else {
|
280
|
require(CAKE . 'app_controller.php');
|
281
|
}
|
282
|
}
|
283
|
|
284
|
if ($name === null) {
|
285
|
return true;
|
286
|
}
|
287
|
|
288
|
if (!class_exists($name . 'Controller')) {
|
289
|
$name = Inflector::underscore($name);
|
290
|
|
291
|
foreach($paths->controllerPaths as $path) {
|
292
|
if (file_exists($path . $name . '_controller.php')) {
|
293
|
require($path . $name . '_controller.php');
|
294
|
return true;
|
295
|
}
|
296
|
}
|
297
|
|
298
|
if ($controller_fn = fileExistsInPath(LIBS . 'controller' . DS . $name . '_controller.php')) {
|
299
|
if (file_exists($controller_fn)) {
|
300
|
require($controller_fn);
|
301
|
return true;
|
302
|
} else {
|
303
|
return false;
|
304
|
}
|
305
|
}
|
306
|
} else {
|
307
|
return false;
|
308
|
}
|
309
|
}
|
310
|
/**
|
311
|
* Loads a plugin's controller.
|
312
|
*
|
313
|
* @param string $plugin Name of plugin
|
314
|
* @param string $controller Name of controller to load
|
315
|
* @return boolean Success
|
316
|
*/
|
317
|
function loadPluginController($plugin, $controller) {
|
318
|
$pluginAppController = Inflector::camelize($plugin . '_app_controller');
|
319
|
$pluginAppControllerFile = APP . 'plugins' . DS . $plugin . DS . $plugin . '_app_controller.php';
|
320
|
if (!class_exists($pluginAppController)) {
|
321
|
if (file_exists($pluginAppControllerFile)) {
|
322
|
require($pluginAppControllerFile);
|
323
|
} else {
|
324
|
return false;
|
325
|
}
|
326
|
}
|
327
|
|
328
|
if (empty($controller)) {
|
329
|
if (!class_exists($plugin . 'Controller')) {
|
330
|
if (file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) {
|
331
|
require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php');
|
332
|
return true;
|
333
|
}
|
334
|
}
|
335
|
}
|
336
|
|
337
|
if (!class_exists($controller . 'Controller')) {
|
338
|
$controller = Inflector::underscore($controller);
|
339
|
$file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $controller . '_controller.php';
|
340
|
|
341
|
if (file_exists($file)) {
|
342
|
require($file);
|
343
|
return true;
|
344
|
} elseif (!class_exists(Inflector::camelize($plugin . '_controller'))) {
|
345
|
if(file_exists(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php')) {
|
346
|
require(APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . $plugin . '_controller.php');
|
347
|
return true;
|
348
|
} else {
|
349
|
return false;
|
350
|
}
|
351
|
}
|
352
|
}
|
353
|
return true;
|
354
|
}
|
355
|
/**
|
356
|
* Loads a helper
|
357
|
*
|
358
|
* @param string $name Name of helper
|
359
|
* @return boolean Success
|
360
|
*/
|
361
|
function loadHelper($name) {
|
362
|
$paths = Configure::getInstance();
|
363
|
|
364
|
if ($name === null) {
|
365
|
return true;
|
366
|
}
|
367
|
|
368
|
if (!class_exists($name . 'Helper')) {
|
369
|
$name=Inflector::underscore($name);
|
370
|
|
371
|
foreach($paths->helperPaths as $path) {
|
372
|
if (file_exists($path . $name . '.php')) {
|
373
|
require($path . $name . '.php');
|
374
|
return true;
|
375
|
}
|
376
|
}
|
377
|
|
378
|
if ($helper_fn = fileExistsInPath(LIBS . 'view' . DS . 'helpers' . DS . $name . '.php')) {
|
379
|
if (file_exists($helper_fn)) {
|
380
|
require($helper_fn);
|
381
|
return true;
|
382
|
} else {
|
383
|
return false;
|
384
|
}
|
385
|
}
|
386
|
} else {
|
387
|
return true;
|
388
|
}
|
389
|
}
|
390
|
/**
|
391
|
* Loads a plugin's helper
|
392
|
*
|
393
|
* @param string $plugin Name of plugin
|
394
|
* @param string $helper Name of helper to load
|
395
|
* @return boolean Success
|
396
|
*/
|
397
|
function loadPluginHelper($plugin, $helper) {
|
398
|
if (!class_exists($helper . 'Helper')) {
|
399
|
$helper = Inflector::underscore($helper);
|
400
|
$file = APP . 'plugins' . DS . $plugin . DS . 'views' . DS . 'helpers' . DS . $helper . '.php';
|
401
|
if (file_exists($file)) {
|
402
|
require($file);
|
403
|
return true;
|
404
|
} else {
|
405
|
return false;
|
406
|
}
|
407
|
} else {
|
408
|
return true;
|
409
|
}
|
410
|
}
|
411
|
/**
|
412
|
* Loads a component
|
413
|
*
|
414
|
* @param string $name Name of component
|
415
|
* @return boolean Success
|
416
|
*/
|
417
|
function loadComponent($name) {
|
418
|
$paths = Configure::getInstance();
|
419
|
|
420
|
if ($name === null) {
|
421
|
return true;
|
422
|
}
|
423
|
|
424
|
if (!class_exists($name . 'Component')) {
|
425
|
$name=Inflector::underscore($name);
|
426
|
|
427
|
foreach($paths->componentPaths as $path) {
|
428
|
if (file_exists($path . $name . '.php')) {
|
429
|
require($path . $name . '.php');
|
430
|
return true;
|
431
|
}
|
432
|
}
|
433
|
|
434
|
if ($component_fn = fileExistsInPath(LIBS . 'controller' . DS . 'components' . DS . $name . '.php')) {
|
435
|
if (file_exists($component_fn)) {
|
436
|
require($component_fn);
|
437
|
return true;
|
438
|
} else {
|
439
|
return false;
|
440
|
}
|
441
|
}
|
442
|
} else {
|
443
|
return true;
|
444
|
}
|
445
|
}
|
446
|
/**
|
447
|
* Loads a plugin's component
|
448
|
*
|
449
|
* @param string $plugin Name of plugin
|
450
|
* @param string $helper Name of component to load
|
451
|
* @return boolean Success
|
452
|
*/
|
453
|
function loadPluginComponent($plugin, $component) {
|
454
|
if (!class_exists($component . 'Component')) {
|
455
|
$component = Inflector::underscore($component);
|
456
|
$file = APP . 'plugins' . DS . $plugin . DS . 'controllers' . DS . 'components' . DS . $component . '.php';
|
457
|
if (file_exists($file)) {
|
458
|
require($file);
|
459
|
return true;
|
460
|
} else {
|
461
|
return false;
|
462
|
}
|
463
|
} else {
|
464
|
return true;
|
465
|
}
|
466
|
}
|
467
|
/**
|
468
|
* Returns an array of filenames of PHP files in given directory.
|
469
|
*
|
470
|
* @param string $path Path to scan for files
|
471
|
* @return array List of files in directory
|
472
|
*/
|
473
|
function listClasses($path) {
|
474
|
$dir = opendir($path);
|
475
|
$classes=array();
|
476
|
while(false !== ($file = readdir($dir))) {
|
477
|
if ((substr($file, -3, 3) == 'php') && substr($file, 0, 1) != '.') {
|
478
|
$classes[] = $file;
|
479
|
}
|
480
|
}
|
481
|
closedir($dir);
|
482
|
return $classes;
|
483
|
}
|
484
|
/**
|
485
|
* Loads configuration files
|
486
|
*
|
487
|
* @return boolean Success
|
488
|
*/
|
489
|
function config() {
|
490
|
$args = func_get_args();
|
491
|
foreach($args as $arg) {
|
492
|
if (('database' == $arg) && file_exists(CONFIGS . $arg . '.php')) {
|
493
|
include_once(CONFIGS . $arg . '.php');
|
494
|
} elseif (file_exists(CONFIGS . $arg . '.php')) {
|
495
|
include_once(CONFIGS . $arg . '.php');
|
496
|
|
497
|
if (count($args) == 1) {
|
498
|
return true;
|
499
|
}
|
500
|
} else {
|
501
|
if (count($args) == 1) {
|
502
|
return false;
|
503
|
}
|
504
|
}
|
505
|
}
|
506
|
return true;
|
507
|
}
|
508
|
/**
|
509
|
* Loads component/components from LIBS.
|
510
|
*
|
511
|
* Example:
|
512
|
* <code>
|
513
|
* uses('flay', 'time');
|
514
|
* </code>
|
515
|
*
|
516
|
* @uses LIBS
|
517
|
*/
|
518
|
function uses() {
|
519
|
$args = func_get_args();
|
520
|
foreach($args as $arg) {
|
521
|
require_once(LIBS . strtolower($arg) . '.php');
|
522
|
}
|
523
|
}
|
524
|
/**
|
525
|
* Require given files in the VENDORS directory. Takes optional number of parameters.
|
526
|
*
|
527
|
* @param string $name Filename without the .php part.
|
528
|
*
|
529
|
*/
|
530
|
function vendor($name) {
|
531
|
$args = func_get_args();
|
532
|
foreach($args as $arg) {
|
533
|
if (file_exists(APP . 'vendors' . DS . $arg . '.php')) {
|
534
|
require_once(APP . 'vendors' . DS . $arg . '.php');
|
535
|
} else {
|
536
|
require_once(VENDORS . $arg . '.php');
|
537
|
}
|
538
|
}
|
539
|
}
|
540
|
/**
|
541
|
* Prints out debug information about given variable.
|
542
|
*
|
543
|
* Only runs if DEBUG level is non-zero.
|
544
|
*
|
545
|
* @param boolean $var Variable to show debug information for.
|
546
|
* @param boolean $show_html If set to true, the method prints the debug data in a screen-friendly way.
|
547
|
*/
|
548
|
function debug($var = false, $showHtml = false) {
|
549
|
if (Configure::read() > 0) {
|
550
|
print "\n<pre class=\"cake_debug\">\n";
|
551
|
ob_start();
|
552
|
print_r($var);
|
553
|
$var = ob_get_clean();
|
554
|
|
555
|
if ($showHtml) {
|
556
|
$var = str_replace('<', '<', str_replace('>', '>', $var));
|
557
|
}
|
558
|
print "{$var}\n</pre>\n";
|
559
|
}
|
560
|
}
|
561
|
/**
|
562
|
* Returns microtime for execution time checking
|
563
|
*
|
564
|
* @return integer
|
565
|
*/
|
566
|
if (!function_exists('getMicrotime')) {
|
567
|
function getMicrotime() {
|
568
|
list($usec, $sec) = explode(" ", microtime());
|
569
|
return ((float)$usec + (float)$sec);
|
570
|
}
|
571
|
}
|
572
|
/**
|
573
|
* Sorts given $array by key $sortby.
|
574
|
*
|
575
|
* @param array $array
|
576
|
* @param string $sortby
|
577
|
* @param string $order Sort order asc/desc (ascending or descending).
|
578
|
* @param integer $type
|
579
|
* @return mixed
|
580
|
*/
|
581
|
if (!function_exists('sortByKey')) {
|
582
|
function sortByKey(&$array, $sortby, $order = 'asc', $type = SORT_NUMERIC) {
|
583
|
if (!is_array($array)) {
|
584
|
return null;
|
585
|
}
|
586
|
|
587
|
foreach($array as $key => $val) {
|
588
|
$sa[$key] = $val[$sortby];
|
589
|
}
|
590
|
|
591
|
if ($order == 'asc') {
|
592
|
asort($sa, $type);
|
593
|
} else {
|
594
|
arsort($sa, $type);
|
595
|
}
|
596
|
|
597
|
foreach($sa as $key => $val) {
|
598
|
$out[] = $array[$key];
|
599
|
}
|
600
|
return $out;
|
601
|
}
|
602
|
}
|
603
|
/**
|
604
|
* Combines given identical arrays by using the first array's values as keys,
|
605
|
* and the second one's values as values. (Implemented for back-compatibility with PHP4)
|
606
|
*
|
607
|
* @param array $a1
|
608
|
* @param array $a2
|
609
|
* @return mixed Outputs either combined array or false.
|
610
|
*/
|
611
|
if (!function_exists('array_combine')) {
|
612
|
function array_combine($a1, $a2) {
|
613
|
$a1 = array_values($a1);
|
614
|
$a2 = array_values($a2);
|
615
|
$c1 = count($a1);
|
616
|
$c2 = count($a2);
|
617
|
|
618
|
if ($c1 != $c2) {
|
619
|
return false;
|
620
|
}
|
621
|
if ($c1 <= 0) {
|
622
|
return false;
|
623
|
}
|
624
|
|
625
|
$output=array();
|
626
|
for($i = 0; $i < $c1; $i++) {
|
627
|
$output[$a1[$i]] = $a2[$i];
|
628
|
}
|
629
|
return $output;
|
630
|
}
|
631
|
}
|
632
|
/**
|
633
|
* Convenience method for htmlspecialchars.
|
634
|
*
|
635
|
* @param string $text
|
636
|
* @return string
|
637
|
*/
|
638
|
function h($text) {
|
639
|
if (is_array($text)) {
|
640
|
return array_map('h', $text);
|
641
|
}
|
642
|
return htmlspecialchars($text);
|
643
|
}
|
644
|
/**
|
645
|
* Returns an array of all the given parameters.
|
646
|
*
|
647
|
* Example:
|
648
|
* <code>
|
649
|
* a('a', 'b')
|
650
|
* </code>
|
651
|
*
|
652
|
* Would return:
|
653
|
* <code>
|
654
|
* array('a', 'b')
|
655
|
* </code>
|
656
|
*
|
657
|
* @return array
|
658
|
*/
|
659
|
function a() {
|
660
|
$args = func_get_args();
|
661
|
return $args;
|
662
|
}
|
663
|
/**
|
664
|
* Constructs associative array from pairs of arguments.
|
665
|
*
|
666
|
* Example:
|
667
|
* <code>
|
668
|
* aa('a','b')
|
669
|
* </code>
|
670
|
*
|
671
|
* Would return:
|
672
|
* <code>
|
673
|
* array('a'=>'b')
|
674
|
* </code>
|
675
|
*
|
676
|
* @return array
|
677
|
*/
|
678
|
function aa() {
|
679
|
$args = func_get_args();
|
680
|
for($l = 0, $c = count($args); $l < $c; $l++) {
|
681
|
if ($l + 1 < count($args)) {
|
682
|
$a[$args[$l]] = $args[$l + 1];
|
683
|
} else {
|
684
|
$a[$args[$l]] = null;
|
685
|
}
|
686
|
$l++;
|
687
|
}
|
688
|
return $a;
|
689
|
}
|
690
|
/**
|
691
|
* Convenience method for echo().
|
692
|
*
|
693
|
* @param string $text String to echo
|
694
|
*/
|
695
|
function e($text) {
|
696
|
echo $text;
|
697
|
}
|
698
|
/**
|
699
|
* Convenience method for strtolower().
|
700
|
*
|
701
|
* @param string $str String to lowercase
|
702
|
*/
|
703
|
function low($str) {
|
704
|
return strtolower($str);
|
705
|
}
|
706
|
/**
|
707
|
* Convenience method for strtoupper().
|
708
|
*
|
709
|
* @param string $str String to uppercase
|
710
|
*/
|
711
|
function up($str) {
|
712
|
return strtoupper($str);
|
713
|
}
|
714
|
/**
|
715
|
* Convenience method for str_replace().
|
716
|
*
|
717
|
* @param string $search String to be replaced
|
718
|
* @param string $replace String to insert
|
719
|
* @param string $subject String to search
|
720
|
*/
|
721
|
function r($search, $replace, $subject) {
|
722
|
return str_replace($search, $replace, $subject);
|
723
|
}
|
724
|
/**
|
725
|
* Print_r convenience function, which prints out <PRE> tags around
|
726
|
* the output of given array. Similar to debug().
|
727
|
*
|
728
|
* @see debug()
|
729
|
* @param array $var
|
730
|
*/
|
731
|
function pr($var) {
|
732
|
if (Configure::read() > 0) {
|
733
|
echo "<pre>";
|
734
|
print_r($var);
|
735
|
echo "</pre>";
|
736
|
}
|
737
|
}
|
738
|
/**
|
739
|
* Display parameter
|
740
|
*
|
741
|
* @param mixed $p Parameter as string or array
|
742
|
* @return string
|
743
|
*/
|
744
|
function params($p) {
|
745
|
if (!is_array($p) || count($p) == 0) {
|
746
|
return null;
|
747
|
} else {
|
748
|
if (is_array($p[0]) && count($p) == 1) {
|
749
|
return $p[0];
|
750
|
} else {
|
751
|
return $p;
|
752
|
}
|
753
|
}
|
754
|
}
|
755
|
/**
|
756
|
* Merge a group of arrays
|
757
|
*
|
758
|
* @param array First array
|
759
|
* @param array Second array
|
760
|
* @param array Third array
|
761
|
* @param array Etc...
|
762
|
* @return array All array parameters merged into one
|
763
|
*/
|
764
|
function am() {
|
765
|
$r = array();
|
766
|
foreach(func_get_args()as $a) {
|
767
|
if (!is_array($a)) {
|
768
|
$a = array($a);
|
769
|
}
|
770
|
$r = array_merge($r, $a);
|
771
|
}
|
772
|
return $r;
|
773
|
}
|
774
|
/**
|
775
|
* Returns the REQUEST_URI from the server environment, or, failing that,
|
776
|
* constructs a new one, using the PHP_SELF constant and other variables.
|
777
|
*
|
778
|
* @return string URI
|
779
|
*/
|
780
|
function setUri() {
|
781
|
if (env('HTTP_X_REWRITE_URL')) {
|
782
|
$uri = env('HTTP_X_REWRITE_URL');
|
783
|
} elseif(env('REQUEST_URI')) {
|
784
|
$uri = env('REQUEST_URI');
|
785
|
} else {
|
786
|
if (env('argv')) {
|
787
|
$uri = env('argv');
|
788
|
|
789
|
if (defined('SERVER_IIS')) {
|
790
|
$uri = BASE_URL . $uri[0];
|
791
|
} else {
|
792
|
$uri = env('PHP_SELF') . '/' . $uri[0];
|
793
|
}
|
794
|
} else {
|
795
|
$uri = env('PHP_SELF') . '/' . env('QUERY_STRING');
|
796
|
}
|
797
|
}
|
798
|
return $uri;
|
799
|
}
|
800
|
/**
|
801
|
* Gets an environment variable from available sources.
|
802
|
* Used as a backup if $_SERVER/$_ENV are disabled.
|
803
|
*
|
804
|
* @param string $key Environment variable name.
|
805
|
* @return string Environment variable setting.
|
806
|
*/
|
807
|
function env($key) {
|
808
|
|
809
|
if ($key == 'HTTPS') {
|
810
|
if (isset($_SERVER) && !empty($_SERVER)) {
|
811
|
return (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on');
|
812
|
} else {
|
813
|
return (strpos(env('SCRIPT_URI'), 'https://') === 0);
|
814
|
}
|
815
|
}
|
816
|
|
817
|
if (isset($_SERVER[$key])) {
|
818
|
return $_SERVER[$key];
|
819
|
} elseif (isset($_ENV[$key])) {
|
820
|
return $_ENV[$key];
|
821
|
} elseif (getenv($key) !== false) {
|
822
|
return getenv($key);
|
823
|
}
|
824
|
|
825
|
if ($key == 'DOCUMENT_ROOT') {
|
826
|
$offset = 0;
|
827
|
if (!strpos(env('SCRIPT_NAME'), '.php')) {
|
828
|
$offset = 4;
|
829
|
}
|
830
|
return substr(env('SCRIPT_FILENAME'), 0, strlen(env('SCRIPT_FILENAME')) - (strlen(env('SCRIPT_NAME')) + $offset));
|
831
|
}
|
832
|
if ($key == 'PHP_SELF') {
|
833
|
return r(env('DOCUMENT_ROOT'), '', env('SCRIPT_FILENAME'));
|
834
|
}
|
835
|
return null;
|
836
|
}
|
837
|
/**
|
838
|
* Returns contents of a file as a string.
|
839
|
*
|
840
|
* @param string $fileName Name of the file.
|
841
|
* @param boolean $useIncludePath Wheter the function should use the include path or not.
|
842
|
* @return mixed Boolean false or contents of required file.
|
843
|
*/
|
844
|
if (!function_exists('file_get_contents')) {
|
845
|
function file_get_contents($fileName, $useIncludePath = false) {
|
846
|
$res=fopen($fileName, 'rb', $useIncludePath);
|
847
|
|
848
|
if ($res === false) {
|
849
|
trigger_error('file_get_contents() failed to open stream: No such file or directory', E_USER_WARNING);
|
850
|
return false;
|
851
|
}
|
852
|
clearstatcache();
|
853
|
|
854
|
if ($fileSize = @filesize($fileName)) {
|
855
|
$data = fread($res, $fileSize);
|
856
|
} else {
|
857
|
$data = '';
|
858
|
|
859
|
while(!feof($res)) {
|
860
|
$data .= fread($res, 8192);
|
861
|
}
|
862
|
}
|
863
|
return "$data\n";
|
864
|
}
|
865
|
}
|
866
|
/**
|
867
|
* Writes data into file.
|
868
|
*
|
869
|
* If file exists, it will be overwritten. If data is an array, it will be join()ed with an empty string.
|
870
|
*
|
871
|
* @param string $fileName File name.
|
872
|
* @param mixed $data String or array.
|
873
|
*/
|
874
|
if (!function_exists('file_put_contents')) {
|
875
|
function file_put_contents($fileName, $data) {
|
876
|
if (is_array($data)) {
|
877
|
$data = join('', $data);
|
878
|
}
|
879
|
$res = @fopen($fileName, 'w+b');
|
880
|
if ($res) {
|
881
|
$write = @fwrite($res, $data);
|
882
|
if($write === false) {
|
883
|
return false;
|
884
|
} else {
|
885
|
return $write;
|
886
|
}
|
887
|
}
|
888
|
}
|
889
|
}
|
890
|
/**
|
891
|
* Reads/writes temporary data to cache files or session.
|
892
|
*
|
893
|
* @param string $path File path within /tmp to save the file.
|
894
|
* @param mixed $data The data to save to the temporary file.
|
895
|
* @param mixed $expires A valid strtotime string when the data expires.
|
896
|
* @param string $target The target of the cached data; either 'cache' or 'public'.
|
897
|
* @return mixed The contents of the temporary file.
|
898
|
*/
|
899
|
function cache($path, $data = null, $expires = '+1 day', $target = 'cache') {
|
900
|
$now = time();
|
901
|
if (!is_numeric($expires)) {
|
902
|
$expires = strtotime($expires, $now);
|
903
|
}
|
904
|
|
905
|
switch(strtolower($target)) {
|
906
|
case 'cache':
|
907
|
$filename = CACHE . $path;
|
908
|
break;
|
909
|
case 'public':
|
910
|
$filename = WWW_ROOT . $path;
|
911
|
break;
|
912
|
}
|
913
|
|
914
|
$timediff = $expires - $now;
|
915
|
$filetime = false;
|
916
|
if(file_exists($filename)) {
|
917
|
$filetime = @filemtime($filename);
|
918
|
}
|
919
|
|
920
|
if ($data === null) {
|
921
|
// Read data from file
|
922
|
if (file_exists($filename) && $filetime !== false) {
|
923
|
if ($filetime + $timediff < $now) {
|
924
|
// File has expired
|
925
|
@unlink($filename);
|
926
|
} else {
|
927
|
$data = file_get_contents($filename);
|
928
|
}
|
929
|
}
|
930
|
} else {
|
931
|
file_put_contents($filename, $data);
|
932
|
}
|
933
|
return $data;
|
934
|
}
|
935
|
/**
|
936
|
* Used to delete files in the cache directories, or clear contents of cache directories
|
937
|
*
|
938
|
* @param mixed $params As String name to be searched for deletion, if name is a directory all files in directory will be deleted.
|
939
|
* If array, names to be searched for deletion.
|
940
|
* If clearCache() without params, all files in app/tmp/cache/views will be deleted
|
941
|
*
|
942
|
* @param string $type Directory in tmp/cache defaults to view directory
|
943
|
* @param string $ext The file extension you are deleting
|
944
|
* @return true if files found and deleted false otherwise
|
945
|
*/
|
946
|
function clearCache($params = null, $type = 'views', $ext = '.php') {
|
947
|
if (is_string($params) || $params === null) {
|
948
|
$params = preg_replace('/\/\//', '/', $params);
|
949
|
$cache = CACHE . $type . DS . $params;
|
950
|
|
951
|
if (is_file($cache . $ext)) {
|
952
|
@unlink($cache . $ext);
|
953
|
return true;
|
954
|
} else if(is_dir($cache)) {
|
955
|
$files = glob("$cache*");
|
956
|
|
957
|
if ($files === false) {
|
958
|
return false;
|
959
|
}
|
960
|
|
961
|
foreach($files as $file) {
|
962
|
if (is_file($file)) {
|
963
|
@unlink($file);
|
964
|
}
|
965
|
}
|
966
|
return true;
|
967
|
} else {
|
968
|
$cache = CACHE . $type . DS . '*' . $params . '*' . $ext;
|
969
|
$files = glob($cache);
|
970
|
|
971
|
if ($files === false) {
|
972
|
return false;
|
973
|
}
|
974
|
foreach($files as $file) {
|
975
|
if (is_file($file)) {
|
976
|
@unlink($file);
|
977
|
}
|
978
|
}
|
979
|
return true;
|
980
|
}
|
981
|
} elseif (is_array($params)) {
|
982
|
foreach($params as $key => $file) {
|
983
|
$file = preg_replace('/\/\//', '/', $file);
|
984
|
$cache = CACHE . $type . DS . '*' . $file . '*' . $ext;
|
985
|
$files[] = glob($cache);
|
986
|
}
|
987
|
|
988
|
if (!empty($files)) {
|
989
|
foreach($files as $key => $delete) {
|
990
|
if (is_array($delete)) {
|
991
|
foreach($delete as $file) {
|
992
|
if (is_file($file)) {
|
993
|
@unlink($file);
|
994
|
}
|
995
|
}
|
996
|
}
|
997
|
}
|
998
|
return true;
|
999
|
} else {
|
1000
|
return false;
|
1001
|
}
|
1002
|
} else {
|
1003
|
return false;
|
1004
|
}
|
1005
|
}
|
1006
|
/**
|
1007
|
* Recursively strips slashes from all values in an array
|
1008
|
*
|
1009
|
* @param unknown_type $value
|
1010
|
* @return unknown
|
1011
|
*/
|
1012
|
function stripslashes_deep($value) {
|
1013
|
if (is_array($value)) {
|
1014
|
$return = array_map('stripslashes_deep', $value);
|
1015
|
return $return;
|
1016
|
} else {
|
1017
|
$return = stripslashes($value);
|
1018
|
return $return ;
|
1019
|
}
|
1020
|
}
|
1021
|
/**
|
1022
|
* Returns a translated string if one is found,
|
1023
|
* or the submitted message if not found.
|
1024
|
*
|
1025
|
* @param unknown_type $msg
|
1026
|
* @param unknown_type $return
|
1027
|
* @return unknown
|
1028
|
* @todo Not implemented fully till 2.0
|
1029
|
*/
|
1030
|
function __($msg, $return = null) {
|
1031
|
if (is_null($return)) {
|
1032
|
echo($msg);
|
1033
|
} else {
|
1034
|
return $msg;
|
1035
|
}
|
1036
|
}
|
1037
|
/**
|
1038
|
* Counts the dimensions of an array
|
1039
|
*
|
1040
|
* @param array $array
|
1041
|
* @return int The number of dimensions in $array
|
1042
|
*/
|
1043
|
function countdim($array) {
|
1044
|
if (is_array(reset($array))) {
|
1045
|
$return = countdim(reset($array)) + 1;
|
1046
|
} else {
|
1047
|
$return = 1;
|
1048
|
}
|
1049
|
return $return;
|
1050
|
}
|
1051
|
/**
|
1052
|
* Shortcut to Log::write.
|
1053
|
*/
|
1054
|
function LogError($message) {
|
1055
|
if (!class_exists('CakeLog')) {
|
1056
|
uses('cake_log');
|
1057
|
}
|
1058
|
$bad = array("\n", "\r", "\t");
|
1059
|
$good = ' ';
|
1060
|
CakeLog::write('error', str_replace($bad, $good, $message));
|
1061
|
}
|
1062
|
/**
|
1063
|
* Searches include path for files
|
1064
|
*
|
1065
|
* @param string $file
|
1066
|
* @return Full path to file if exists, otherwise false
|
1067
|
*/
|
1068
|
function fileExistsInPath($file) {
|
1069
|
$paths = explode(PATH_SEPARATOR, ini_get('include_path'));
|
1070
|
foreach($paths as $path) {
|
1071
|
$fullPath = $path . DIRECTORY_SEPARATOR . $file;
|
1072
|
|
1073
|
if (file_exists($fullPath)) {
|
1074
|
return $fullPath;
|
1075
|
} elseif (file_exists($file)) {
|
1076
|
return $file;
|
1077
|
}
|
1078
|
}
|
1079
|
return false;
|
1080
|
}
|
1081
|
/**
|
1082
|
* Convert forward slashes to underscores and removes first and last underscores in a string
|
1083
|
*
|
1084
|
* @param string
|
1085
|
* @return string with underscore remove from start and end of string
|
1086
|
*/
|
1087
|
function convertSlash($string) {
|
1088
|
$string = trim($string,"/");
|
1089
|
$string = preg_replace('/\/\//', '/', $string);
|
1090
|
$string = str_replace('/', '_', $string);
|
1091
|
return $string;
|
1092
|
}
|
1093
|
|
1094
|
/**
|
1095
|
* chmod recursively on a directory
|
1096
|
*
|
1097
|
* @param string $path
|
1098
|
* @param int $mode
|
1099
|
* @return boolean
|
1100
|
*/
|
1101
|
function chmodr($path, $mode = 0755) {
|
1102
|
if (!is_dir($path)) {
|
1103
|
return chmod($path, $mode);
|
1104
|
}
|
1105
|
$dir = opendir($path);
|
1106
|
|
1107
|
while($file = readdir($dir)) {
|
1108
|
if ($file != '.' && $file != '..') {
|
1109
|
$fullpath = $path . '/' . $file;
|
1110
|
|
1111
|
if (!is_dir($fullpath)) {
|
1112
|
if (!chmod($fullpath, $mode)) {
|
1113
|
return false;
|
1114
|
}
|
1115
|
} else {
|
1116
|
if (!chmodr($fullpath, $mode)) {
|
1117
|
return false;
|
1118
|
}
|
1119
|
}
|
1120
|
}
|
1121
|
}
|
1122
|
closedir($dir);
|
1123
|
|
1124
|
if (chmod($path, $mode)) {
|
1125
|
return true;
|
1126
|
} else {
|
1127
|
return false;
|
1128
|
}
|
1129
|
}
|
1130
|
/**
|
1131
|
* removed the plugin name from the base url
|
1132
|
*
|
1133
|
* @param string $base
|
1134
|
* @param string $plugin
|
1135
|
* @return base url with plugin name removed if present
|
1136
|
*/
|
1137
|
function strip_plugin($base, $plugin){
|
1138
|
if ($plugin != null) {
|
1139
|
$base = preg_replace('/' . $plugin . '/', '', $base);
|
1140
|
$base = str_replace('//', '', $base);
|
1141
|
$pos1 = strrpos($base, '/');
|
1142
|
$char = strlen($base) - 1;
|
1143
|
|
1144
|
if ($pos1 == $char) {
|
1145
|
$base = substr($base, 0, $char);
|
1146
|
}
|
1147
|
}
|
1148
|
return $base;
|
1149
|
}
|
1150
|
/**
|
1151
|
* Wraps ternary operations. If $condition is a non-empty value, $val1 is returned, otherwise $val2.
|
1152
|
*
|
1153
|
* @param mixed $condition Conditional expression
|
1154
|
* @param mixed $val1
|
1155
|
* @param mixed $val2
|
1156
|
* @return mixed $val1 or $val2, depending on whether $condition evaluates to a non-empty expression.
|
1157
|
*/
|
1158
|
function ife($condition, $val1 = null, $val2 = null) {
|
1159
|
if (!empty($condition)) {
|
1160
|
return $val1;
|
1161
|
}
|
1162
|
return $val2;
|
1163
|
}
|
1164
|
?>
|