/lenasys/trunk

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/lenasys/trunk

« back to all changes in this revision

Viewing changes to codeigniter/system/core/Common.php

  • Committer: galaxyAbstractor
  • Date: 2013-04-10 15:49:32 UTC
  • mto: (19.1.5 lenasys)
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: galaxyabstractor@gmail.com-20130410154932-4vizlzk0ar5gykvi
* Added an simple admin panel to the codeviewer-cmssy stuff
* Redesigned a bit like the mockups - still stuff to come
* Implemented the codeviewer + admin panel again using the Framework CodeIgniter instead 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
 
2
/**
 
3
 * CodeIgniter
 
4
 *
 
5
 * An open source application development framework for PHP 5.1.6 or newer
 
6
 *
 
7
 * @package             CodeIgniter
 
8
 * @author              ExpressionEngine Dev Team
 
9
 * @copyright   Copyright (c) 2008 - 2011, EllisLab, Inc.
 
10
 * @license             http://codeigniter.com/user_guide/license.html
 
11
 * @link                http://codeigniter.com
 
12
 * @since               Version 1.0
 
13
 * @filesource
 
14
 */
 
15
 
 
16
// ------------------------------------------------------------------------
 
17
 
 
18
/**
 
19
 * Common Functions
 
20
 *
 
21
 * Loads the base classes and executes the request.
 
22
 *
 
23
 * @package             CodeIgniter
 
24
 * @subpackage  codeigniter
 
25
 * @category    Common Functions
 
26
 * @author              ExpressionEngine Dev Team
 
27
 * @link                http://codeigniter.com/user_guide/
 
28
 */
 
29
 
 
30
// ------------------------------------------------------------------------
 
31
 
 
32
/**
 
33
* Determines if the current version of PHP is greater then the supplied value
 
34
*
 
35
* Since there are a few places where we conditionally test for PHP > 5
 
36
* we'll set a static variable.
 
37
*
 
38
* @access       public
 
39
* @param        string
 
40
* @return       bool    TRUE if the current version is $version or higher
 
41
*/
 
42
if ( ! function_exists('is_php'))
 
43
{
 
44
        function is_php($version = '5.0.0')
 
45
        {
 
46
                static $_is_php;
 
47
                $version = (string)$version;
 
48
 
 
49
                if ( ! isset($_is_php[$version]))
 
50
                {
 
51
                        $_is_php[$version] = (version_compare(PHP_VERSION, $version) < 0) ? FALSE : TRUE;
 
52
                }
 
53
 
 
54
                return $_is_php[$version];
 
55
        }
 
56
}
 
57
 
 
58
// ------------------------------------------------------------------------
 
59
 
 
60
/**
 
61
 * Tests for file writability
 
62
 *
 
63
 * is_writable() returns TRUE on Windows servers when you really can't write to
 
64
 * the file, based on the read-only attribute.  is_writable() is also unreliable
 
65
 * on Unix servers if safe_mode is on.
 
66
 *
 
67
 * @access      private
 
68
 * @return      void
 
69
 */
 
70
if ( ! function_exists('is_really_writable'))
 
71
{
 
72
        function is_really_writable($file)
 
73
        {
 
74
                // If we're on a Unix server with safe_mode off we call is_writable
 
75
                if (DIRECTORY_SEPARATOR == '/' AND @ini_get("safe_mode") == FALSE)
 
76
                {
 
77
                        return is_writable($file);
 
78
                }
 
79
 
 
80
                // For windows servers and safe_mode "on" installations we'll actually
 
81
                // write a file then read it.  Bah...
 
82
                if (is_dir($file))
 
83
                {
 
84
                        $file = rtrim($file, '/').'/'.md5(mt_rand(1,100).mt_rand(1,100));
 
85
 
 
86
                        if (($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
 
87
                        {
 
88
                                return FALSE;
 
89
                        }
 
90
 
 
91
                        fclose($fp);
 
92
                        @chmod($file, DIR_WRITE_MODE);
 
93
                        @unlink($file);
 
94
                        return TRUE;
 
95
                }
 
96
                elseif ( ! is_file($file) OR ($fp = @fopen($file, FOPEN_WRITE_CREATE)) === FALSE)
 
97
                {
 
98
                        return FALSE;
 
99
                }
 
100
 
 
101
                fclose($fp);
 
102
                return TRUE;
 
103
        }
 
104
}
 
105
 
 
106
// ------------------------------------------------------------------------
 
107
 
 
108
/**
 
109
* Class registry
 
110
*
 
111
* This function acts as a singleton.  If the requested class does not
 
112
* exist it is instantiated and set to a static variable.  If it has
 
113
* previously been instantiated the variable is returned.
 
114
*
 
115
* @access       public
 
116
* @param        string  the class name being requested
 
117
* @param        string  the directory where the class should be found
 
118
* @param        string  the class name prefix
 
119
* @return       object
 
120
*/
 
121
if ( ! function_exists('load_class'))
 
122
{
 
123
        function &load_class($class, $directory = 'libraries', $prefix = 'CI_')
 
124
        {
 
125
                static $_classes = array();
 
126
 
 
127
                // Does the class exist?  If so, we're done...
 
128
                if (isset($_classes[$class]))
 
129
                {
 
130
                        return $_classes[$class];
 
131
                }
 
132
 
 
133
                $name = FALSE;
 
134
 
 
135
                // Look for the class first in the local application/libraries folder
 
136
                // then in the native system/libraries folder
 
137
                foreach (array(APPPATH, BASEPATH) as $path)
 
138
                {
 
139
                        if (file_exists($path.$directory.'/'.$class.'.php'))
 
140
                        {
 
141
                                $name = $prefix.$class;
 
142
 
 
143
                                if (class_exists($name) === FALSE)
 
144
                                {
 
145
                                        require($path.$directory.'/'.$class.'.php');
 
146
                                }
 
147
 
 
148
                                break;
 
149
                        }
 
150
                }
 
151
 
 
152
                // Is the request a class extension?  If so we load it too
 
153
                if (file_exists(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php'))
 
154
                {
 
155
                        $name = config_item('subclass_prefix').$class;
 
156
 
 
157
                        if (class_exists($name) === FALSE)
 
158
                        {
 
159
                                require(APPPATH.$directory.'/'.config_item('subclass_prefix').$class.'.php');
 
160
                        }
 
161
                }
 
162
 
 
163
                // Did we find the class?
 
164
                if ($name === FALSE)
 
165
                {
 
166
                        // Note: We use exit() rather then show_error() in order to avoid a
 
167
                        // self-referencing loop with the Excptions class
 
168
                        exit('Unable to locate the specified class: '.$class.'.php');
 
169
                }
 
170
 
 
171
                // Keep track of what we just loaded
 
172
                is_loaded($class);
 
173
 
 
174
                $_classes[$class] = new $name();
 
175
                return $_classes[$class];
 
176
        }
 
177
}
 
178
 
 
179
// --------------------------------------------------------------------
 
180
 
 
181
/**
 
182
* Keeps track of which libraries have been loaded.  This function is
 
183
* called by the load_class() function above
 
184
*
 
185
* @access       public
 
186
* @return       array
 
187
*/
 
188
if ( ! function_exists('is_loaded'))
 
189
{
 
190
        function &is_loaded($class = '')
 
191
        {
 
192
                static $_is_loaded = array();
 
193
 
 
194
                if ($class != '')
 
195
                {
 
196
                        $_is_loaded[strtolower($class)] = $class;
 
197
                }
 
198
 
 
199
                return $_is_loaded;
 
200
        }
 
201
}
 
202
 
 
203
// ------------------------------------------------------------------------
 
204
 
 
205
/**
 
206
* Loads the main config.php file
 
207
*
 
208
* This function lets us grab the config file even if the Config class
 
209
* hasn't been instantiated yet
 
210
*
 
211
* @access       private
 
212
* @return       array
 
213
*/
 
214
if ( ! function_exists('get_config'))
 
215
{
 
216
        function &get_config($replace = array())
 
217
        {
 
218
                static $_config;
 
219
 
 
220
                if (isset($_config))
 
221
                {
 
222
                        return $_config[0];
 
223
                }
 
224
 
 
225
                // Is the config file in the environment folder?
 
226
                if ( ! defined('ENVIRONMENT') OR ! file_exists($file_path = APPPATH.'config/'.ENVIRONMENT.'/config.php'))
 
227
                {
 
228
                        $file_path = APPPATH.'config/config.php';
 
229
                }
 
230
 
 
231
                // Fetch the config file
 
232
                if ( ! file_exists($file_path))
 
233
                {
 
234
                        exit('The configuration file does not exist.');
 
235
                }
 
236
 
 
237
                require($file_path);
 
238
 
 
239
                // Does the $config array exist in the file?
 
240
                if ( ! isset($config) OR ! is_array($config))
 
241
                {
 
242
                        exit('Your config file does not appear to be formatted correctly.');
 
243
                }
 
244
 
 
245
                // Are any values being dynamically replaced?
 
246
                if (count($replace) > 0)
 
247
                {
 
248
                        foreach ($replace as $key => $val)
 
249
                        {
 
250
                                if (isset($config[$key]))
 
251
                                {
 
252
                                        $config[$key] = $val;
 
253
                                }
 
254
                        }
 
255
                }
 
256
 
 
257
                return $_config[0] =& $config;
 
258
        }
 
259
}
 
260
 
 
261
// ------------------------------------------------------------------------
 
262
 
 
263
/**
 
264
* Returns the specified config item
 
265
*
 
266
* @access       public
 
267
* @return       mixed
 
268
*/
 
269
if ( ! function_exists('config_item'))
 
270
{
 
271
        function config_item($item)
 
272
        {
 
273
                static $_config_item = array();
 
274
 
 
275
                if ( ! isset($_config_item[$item]))
 
276
                {
 
277
                        $config =& get_config();
 
278
 
 
279
                        if ( ! isset($config[$item]))
 
280
                        {
 
281
                                return FALSE;
 
282
                        }
 
283
                        $_config_item[$item] = $config[$item];
 
284
                }
 
285
 
 
286
                return $_config_item[$item];
 
287
        }
 
288
}
 
289
 
 
290
// ------------------------------------------------------------------------
 
291
 
 
292
/**
 
293
* Error Handler
 
294
*
 
295
* This function lets us invoke the exception class and
 
296
* display errors using the standard error template located
 
297
* in application/errors/errors.php
 
298
* This function will send the error page directly to the
 
299
* browser and exit.
 
300
*
 
301
* @access       public
 
302
* @return       void
 
303
*/
 
304
if ( ! function_exists('show_error'))
 
305
{
 
306
        function show_error($message, $status_code = 500, $heading = 'An Error Was Encountered')
 
307
        {
 
308
                $_error =& load_class('Exceptions', 'core');
 
309
                echo $_error->show_error($heading, $message, 'error_general', $status_code);
 
310
                exit;
 
311
        }
 
312
}
 
313
 
 
314
// ------------------------------------------------------------------------
 
315
 
 
316
/**
 
317
* 404 Page Handler
 
318
*
 
319
* This function is similar to the show_error() function above
 
320
* However, instead of the standard error template it displays
 
321
* 404 errors.
 
322
*
 
323
* @access       public
 
324
* @return       void
 
325
*/
 
326
if ( ! function_exists('show_404'))
 
327
{
 
328
        function show_404($page = '', $log_error = TRUE)
 
329
        {
 
330
                $_error =& load_class('Exceptions', 'core');
 
331
                $_error->show_404($page, $log_error);
 
332
                exit;
 
333
        }
 
334
}
 
335
 
 
336
// ------------------------------------------------------------------------
 
337
 
 
338
/**
 
339
* Error Logging Interface
 
340
*
 
341
* We use this as a simple mechanism to access the logging
 
342
* class and send messages to be logged.
 
343
*
 
344
* @access       public
 
345
* @return       void
 
346
*/
 
347
if ( ! function_exists('log_message'))
 
348
{
 
349
        function log_message($level = 'error', $message, $php_error = FALSE)
 
350
        {
 
351
                static $_log;
 
352
 
 
353
                if (config_item('log_threshold') == 0)
 
354
                {
 
355
                        return;
 
356
                }
 
357
 
 
358
                $_log =& load_class('Log');
 
359
                $_log->write_log($level, $message, $php_error);
 
360
        }
 
361
}
 
362
 
 
363
// ------------------------------------------------------------------------
 
364
 
 
365
/**
 
366
 * Set HTTP Status Header
 
367
 *
 
368
 * @access      public
 
369
 * @param       int             the status code
 
370
 * @param       string
 
371
 * @return      void
 
372
 */
 
373
if ( ! function_exists('set_status_header'))
 
374
{
 
375
        function set_status_header($code = 200, $text = '')
 
376
        {
 
377
                $stati = array(
 
378
                                                        200     => 'OK',
 
379
                                                        201     => 'Created',
 
380
                                                        202     => 'Accepted',
 
381
                                                        203     => 'Non-Authoritative Information',
 
382
                                                        204     => 'No Content',
 
383
                                                        205     => 'Reset Content',
 
384
                                                        206     => 'Partial Content',
 
385
 
 
386
                                                        300     => 'Multiple Choices',
 
387
                                                        301     => 'Moved Permanently',
 
388
                                                        302     => 'Found',
 
389
                                                        304     => 'Not Modified',
 
390
                                                        305     => 'Use Proxy',
 
391
                                                        307     => 'Temporary Redirect',
 
392
 
 
393
                                                        400     => 'Bad Request',
 
394
                                                        401     => 'Unauthorized',
 
395
                                                        403     => 'Forbidden',
 
396
                                                        404     => 'Not Found',
 
397
                                                        405     => 'Method Not Allowed',
 
398
                                                        406     => 'Not Acceptable',
 
399
                                                        407     => 'Proxy Authentication Required',
 
400
                                                        408     => 'Request Timeout',
 
401
                                                        409     => 'Conflict',
 
402
                                                        410     => 'Gone',
 
403
                                                        411     => 'Length Required',
 
404
                                                        412     => 'Precondition Failed',
 
405
                                                        413     => 'Request Entity Too Large',
 
406
                                                        414     => 'Request-URI Too Long',
 
407
                                                        415     => 'Unsupported Media Type',
 
408
                                                        416     => 'Requested Range Not Satisfiable',
 
409
                                                        417     => 'Expectation Failed',
 
410
 
 
411
                                                        500     => 'Internal Server Error',
 
412
                                                        501     => 'Not Implemented',
 
413
                                                        502     => 'Bad Gateway',
 
414
                                                        503     => 'Service Unavailable',
 
415
                                                        504     => 'Gateway Timeout',
 
416
                                                        505     => 'HTTP Version Not Supported'
 
417
                                                );
 
418
 
 
419
                if ($code == '' OR ! is_numeric($code))
 
420
                {
 
421
                        show_error('Status codes must be numeric', 500);
 
422
                }
 
423
 
 
424
                if (isset($stati[$code]) AND $text == '')
 
425
                {
 
426
                        $text = $stati[$code];
 
427
                }
 
428
 
 
429
                if ($text == '')
 
430
                {
 
431
                        show_error('No status text available.  Please check your status code number or supply your own message text.', 500);
 
432
                }
 
433
 
 
434
                $server_protocol = (isset($_SERVER['SERVER_PROTOCOL'])) ? $_SERVER['SERVER_PROTOCOL'] : FALSE;
 
435
 
 
436
                if (substr(php_sapi_name(), 0, 3) == 'cgi')
 
437
                {
 
438
                        header("Status: {$code} {$text}", TRUE);
 
439
                }
 
440
                elseif ($server_protocol == 'HTTP/1.1' OR $server_protocol == 'HTTP/1.0')
 
441
                {
 
442
                        header($server_protocol." {$code} {$text}", TRUE, $code);
 
443
                }
 
444
                else
 
445
                {
 
446
                        header("HTTP/1.1 {$code} {$text}", TRUE, $code);
 
447
                }
 
448
        }
 
449
}
 
450
 
 
451
// --------------------------------------------------------------------
 
452
 
 
453
/**
 
454
* Exception Handler
 
455
*
 
456
* This is the custom exception handler that is declaired at the top
 
457
* of Codeigniter.php.  The main reason we use this is to permit
 
458
* PHP errors to be logged in our own log files since the user may
 
459
* not have access to server logs. Since this function
 
460
* effectively intercepts PHP errors, however, we also need
 
461
* to display errors based on the current error_reporting level.
 
462
* We do that with the use of a PHP error template.
 
463
*
 
464
* @access       private
 
465
* @return       void
 
466
*/
 
467
if ( ! function_exists('_exception_handler'))
 
468
{
 
469
        function _exception_handler($severity, $message, $filepath, $line)
 
470
        {
 
471
                 // We don't bother with "strict" notices since they tend to fill up
 
472
                 // the log file with excess information that isn't normally very helpful.
 
473
                 // For example, if you are running PHP 5 and you use version 4 style
 
474
                 // class functions (without prefixes like "public", "private", etc.)
 
475
                 // you'll get notices telling you that these have been deprecated.
 
476
                if ($severity == E_STRICT)
 
477
                {
 
478
                        return;
 
479
                }
 
480
 
 
481
                $_error =& load_class('Exceptions', 'core');
 
482
 
 
483
                // Should we display the error? We'll get the current error_reporting
 
484
                // level and add its bits with the severity bits to find out.
 
485
                if (($severity & error_reporting()) == $severity)
 
486
                {
 
487
                        $_error->show_php_error($severity, $message, $filepath, $line);
 
488
                }
 
489
 
 
490
                // Should we log the error?  No?  We're done...
 
491
                if (config_item('log_threshold') == 0)
 
492
                {
 
493
                        return;
 
494
                }
 
495
 
 
496
                $_error->log_exception($severity, $message, $filepath, $line);
 
497
        }
 
498
}
 
499
 
 
500
// --------------------------------------------------------------------
 
501
 
 
502
/**
 
503
 * Remove Invisible Characters
 
504
 *
 
505
 * This prevents sandwiching null characters
 
506
 * between ascii characters, like Java\0script.
 
507
 *
 
508
 * @access      public
 
509
 * @param       string
 
510
 * @return      string
 
511
 */
 
512
if ( ! function_exists('remove_invisible_characters'))
 
513
{
 
514
        function remove_invisible_characters($str, $url_encoded = TRUE)
 
515
        {
 
516
                $non_displayables = array();
 
517
                
 
518
                // every control character except newline (dec 10)
 
519
                // carriage return (dec 13), and horizontal tab (dec 09)
 
520
                
 
521
                if ($url_encoded)
 
522
                {
 
523
                        $non_displayables[] = '/%0[0-8bcef]/';  // url encoded 00-08, 11, 12, 14, 15
 
524
                        $non_displayables[] = '/%1[0-9a-f]/';   // url encoded 16-31
 
525
                }
 
526
                
 
527
                $non_displayables[] = '/[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]+/S';   // 00-08, 11, 12, 14-31, 127
 
528
 
 
529
                do
 
530
                {
 
531
                        $str = preg_replace($non_displayables, '', $str, -1, $count);
 
532
                }
 
533
                while ($count);
 
534
 
 
535
                return $str;
 
536
        }
 
537
}
 
538
 
 
539
// ------------------------------------------------------------------------
 
540
 
 
541
/**
 
542
* Returns HTML escaped variable
 
543
*
 
544
* @access       public
 
545
* @param        mixed
 
546
* @return       mixed
 
547
*/
 
548
if ( ! function_exists('html_escape'))
 
549
{
 
550
        function html_escape($var)
 
551
        {
 
552
                if (is_array($var))
 
553
                {
 
554
                        return array_map('html_escape', $var);
 
555
                }
 
556
                else
 
557
                {
 
558
                        return htmlspecialchars($var, ENT_QUOTES, config_item('charset'));
 
559
                }
 
560
        }
 
561
}
 
562
 
 
563
/* End of file Common.php */
 
564
/* Location: ./system/core/Common.php */
 
 
b'\\ No newline at end of file'