1 <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
5 * An open source application development framework for PHP 4.3.2 or newer
8 * @author ExpressionEngine Dev Team
9 * @copyright Copyright (c) 2008, EllisLab, Inc.
10 * @license http://codeigniter.com/user_guide/license.html
11 * @link http://codeigniter.com
16 // ------------------------------------------------------------------------
19 * CodeIgniter Compatibility Helpers
21 * This helper contains some functions based on the PEAR PHP_Compat library
22 * http://pear.php.net/package/PHP_Compat
24 * The PEAR compat library is a little bloated and the code doesn't harmonize
25 * well with CodeIgniter, so those functions have been refactored.
26 * We cheat a little and use CI's _exception_handler() to output our own PHP errors
27 * so that the behavior fully mimicks the PHP 5 counterparts. -- Derek Jones
29 * @package CodeIgniter
32 * @author ExpressionEngine Dev Team
33 * @link http://codeigniter.com/user_guide/helpers/compatibility_helper.html
36 // ------------------------------------------------------------------------
38 if ( ! defined('PHP_EOL'))
40 define('PHP_EOL', (DIRECTORY_SEPARATOR == '/') ? "\n" : "\r\n");
43 // ------------------------------------------------------------------------
48 * Writes a string to a file
49 * http://us.php.net/manual/en/function.file_put_contents.php
50 * argument 4, $context, not supported
53 * @param string file name
54 * @param mixed data to be written
56 * @return int length of written string
58 if ( ! function_exists('file_put_contents'))
60 function file_put_contents($filename, $data, $flags = NULL)
64 settype($data, 'STRING');
67 if ( ! is_string($data) && ! is_array($data) && ! is_resource($data))
69 $backtrace = debug_backtrace();
70 _exception_handler(E_USER_WARNING, 'file_put_contents(): the 2nd parameter should be either a string or an array', $backtrace[0]['file'], $backtrace[0]['line']);
74 // read stream if given a stream resource
75 if (is_resource($data))
77 if (get_resource_type($data) !== 'stream')
79 $backtrace = debug_backtrace();
80 _exception_handler(E_USER_WARNING, 'file_put_contents(): supplied resource is not a valid stream resource', $backtrace[0]['file'], $backtrace[0]['line']);
86 while ( ! feof($data))
88 $text .= fread($data, 4096);
95 // strings only please!
98 $data = implode('', $data);
101 // Set the appropriate mode
102 if (($flags & 8) > 0) // 8 = FILE_APPEND flag
104 $mode = FOPEN_WRITE_CREATE;
108 $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE;
111 // Check if we're using the include path
112 if (($flags & 1) > 0) // 1 = FILE_USE_INCLUDE_PATH flag
114 $use_include_path = TRUE;
118 $use_include_path = FALSE;
121 $fp = @fopen($filename, $mode, $use_include_path);
125 $backtrace = debug_backtrace();
126 _exception_handler(E_USER_WARNING, 'file_put_contents('.htmlentities($filename).') failed to open stream', $backtrace[0]['file'], $backtrace[0]['line']);
130 if (($flags & LOCK_EX) > 0)
132 if ( ! flock($fp, LOCK_EX))
134 $backtrace = debug_backtrace();
135 _exception_handler(E_USER_WARNING, 'file_put_contents('.htmlentities($filename).') unable to acquire an exclusive lock on file', $backtrace[0]['file'], $backtrace[0]['line']);
141 if (($written = @fwrite($fp, $data)) === FALSE)
143 $backtrace = debug_backtrace();
144 _exception_handler(E_USER_WARNING, 'file_put_contents('.htmlentities($filename).') failed to write to '.htmlentities($filename), $backtrace[0]['file'], $backtrace[0]['line']);
155 // ------------------------------------------------------------------------
160 * Format line as CSV and write to file pointer
161 * http://us.php.net/manual/en/function.fputcsv.php
164 * @param resource file pointer
165 * @param array data to be written
166 * @param string delimiter
167 * @param string enclosure
168 * @return int length of written string
170 if ( ! function_exists('fputcsv'))
172 function fputcsv($handle, $fields, $delimiter = ',', $enclosure = '"')
174 // Checking for a handle resource
175 if ( ! is_resource($handle))
177 $backtrace = debug_backtrace();
178 _exception_handler(E_USER_WARNING, 'fputcsv() expects parameter 1 to be stream resource, '.gettype($handle).' given', $backtrace[0]['file'], $backtrace[0]['line']);
182 // OK, it is a resource, but is it a stream?
183 if (get_resource_type($handle) !== 'stream')
185 $backtrace = debug_backtrace();
186 _exception_handler(E_USER_WARNING, 'fputcsv() expects parameter 1 to be stream resource, '.get_resource_type($handle).' given', $backtrace[0]['file'], $backtrace[0]['line']);
190 // Checking for an array of fields
191 if ( ! is_array($fields))
193 $backtrace = debug_backtrace();
194 _exception_handler(E_USER_WARNING, 'fputcsv() expects parameter 2 to be array, '.gettype($fields).' given', $backtrace[0]['file'], $backtrace[0]['line']);
198 // validate delimiter
199 if (strlen($delimiter) > 1)
201 $delimiter = substr($delimiter, 0, 1);
202 $backtrace = debug_backtrace();
203 _exception_handler(E_NOTICE, 'fputcsv() delimiter must be one character long, "'.htmlentities($delimiter).'" used', $backtrace[0]['file'], $backtrace[0]['line']);
206 // validate enclosure
207 if (strlen($enclosure) > 1)
209 $enclosure = substr($enclosure, 0, 1);
210 $backtrace = debug_backtrace();
211 _exception_handler(E_NOTICE, 'fputcsv() enclosure must be one character long, "'.htmlentities($enclosure).'" used', $backtrace[0]['file'], $backtrace[0]['line']);
217 foreach ($fields as $cell)
219 $cell = str_replace($enclosure, $enclosure.$enclosure, $cell);
221 if (strpos($cell, $delimiter) !== FALSE OR strpos($cell, $enclosure) !== FALSE OR strpos($cell, "\n") !== FALSE)
223 $out .= $enclosure.$cell.$enclosure.$delimiter;
227 $out .= $cell.$delimiter;
231 $length = @fwrite($handle, substr($out, 0, -1)."\n");
237 // ------------------------------------------------------------------------
242 * Find position of first occurrence of a case-insensitive string
243 * http://us.php.net/manual/en/function.stripos.php
246 * @param string haystack
247 * @param string needle
249 * @return int numeric position of the first occurrence of needle in the haystack
251 if ( ! function_exists('stripos'))
253 function stripos($haystack, $needle, $offset = NULL)
255 // Cast non string scalar values
256 if (is_scalar($haystack))
258 settype($haystack, 'STRING');
261 if ( ! is_string($haystack))
263 $backtrace = debug_backtrace();
264 _exception_handler(E_USER_WARNING, 'stripos() expects parameter 1 to be string, '.gettype($haystack).' given', $backtrace[0]['file'], $backtrace[0]['line']);
268 if ( ! is_scalar($needle))
270 $backtrace = debug_backtrace();
271 _exception_handler(E_USER_WARNING, 'stripos() needle is not a string or an integer in '.$backtrace[0]['file'], $backtrace[0]['line']);
275 if (is_float($offset))
277 $offset = (int)$offset;
280 if ( ! is_int($offset) && ! is_bool($offset) && ! is_null($offset))
282 $backtrace = debug_backtrace();
283 _exception_handler(E_USER_WARNING, 'stripos() expects parameter 3 to be long, '.gettype($offset).' given', $backtrace[0]['file'], $backtrace[0]['line']);
287 return strpos(strtolower($haystack), strtolower($needle), $offset);
291 // ------------------------------------------------------------------------
296 * Find position of first occurrence of a case-insensitive string
297 * http://us.php.net/manual/en/function.str-ireplace.php
298 * (parameter 4, $count, is not supported as to do so in PHP 4 would make
299 * it a required parameter)
302 * @param mixed search
303 * @param mixed replace
304 * @param mixed subject
305 * @return int numeric position of the first occurrence of needle in the haystack
307 if ( ! function_exists('str_ireplace'))
309 function str_ireplace($search, $replace, $subject)
311 // Nothing to do here
312 if ($search === NULL OR $subject === NULL)
318 if (is_scalar($search) && is_array($replace))
320 $backtrace = debug_backtrace();
322 if (is_object($replace))
324 show_error('Object of class '.get_class($replace).' could not be converted to string in '.$backtrace[0]['file'].' on line '.$backtrace[0]['line']);
328 _exception_handler(E_USER_NOTICE, 'Array to string conversion in '.$backtrace[0]['file'], $backtrace[0]['line']);
332 // Searching for an array
333 if (is_array($search))
335 // Replacing with an array
336 if (is_array($replace))
338 $search = array_values($search);
339 $replace = array_values($replace);
341 if (count($search) >= count($replace))
343 $replace = array_pad($replace, count($search), '');
347 $replace = array_slice($replace, 0, count($search));
352 // Replacing with a string all positions
353 $replace = array_fill(0, count($search), $replace);
358 //Searching for a string and replacing with a string.
359 $search = array((string)$search);
360 $replace = array((string)$replace);
363 // Prepare the search array
364 foreach ($search as $search_key => $search_value)
366 $search[$search_key] = '/'.preg_quote($search_value, '/').'/i';
369 // Prepare the replace array (escape backreferences)
370 foreach ($replace as $k => $v)
372 $replace[$k] = str_replace(array(chr(92), '$'), array(chr(92).chr(92), '\$'), $v);
375 // do the replacement
376 $result = preg_replace($search, $replace, (array)$subject);
378 // Check if subject was initially a string and return it as a string
379 if ( ! is_array($subject))
381 return current($result);
384 // Otherwise, just return the array
389 // ------------------------------------------------------------------------
394 * Generate URL-encoded query string
395 * http://us.php.net/manual/en/function.http-build-query.php
398 * @param array form data
399 * @param string numeric prefix
400 * @param string argument separator
401 * @return string URL-encoded string
403 if ( ! function_exists('http_build_query'))
405 function http_build_query($formdata, $numeric_prefix = NULL, $separator = NULL)
408 if ( ! is_array($formdata) && ! is_object($formdata))
410 $backtrace = debug_backtrace();
411 _exception_handler(E_USER_WARNING, 'http_build_query() Parameter 1 expected to be Array or Object. Incorrect value given', $backtrace[0]['file'], $backtrace[0]['line']);
416 if (is_object($formdata))
418 $formdata = get_object_vars($formdata);
421 // If the array is empty, return NULL
422 if (empty($formdata))
427 // Argument separator
428 if ($separator === NULL)
430 $separator = ini_get('arg_separator.output');
432 if (strlen($separator) == 0)
438 // Start building the query
441 foreach ($formdata as $key => $val)
448 if (is_integer($key) && $numeric_prefix != NULL)
450 $key = $numeric_prefix.$key;
453 if (is_resource($val))
458 // hand it off to a recursive parser
459 $tmp[] = _http_build_query_helper($key, $val, $separator);
462 return implode($separator, $tmp);
466 // Helper helper. Remind anyone of college?
467 // Required to handle recursion in nested arrays.
469 // You could shave fractions of fractions of a second by moving where
470 // the urlencoding takes place, but it's much less intuitive, and if
471 // your application has 10,000 form fields, well, you have other problems ;)
472 function _http_build_query_helper($key, $val, $separator = '&')
476 return urlencode($key).'='.urlencode($val);
483 $val = get_object_vars($val);
486 foreach ($val as $k => $v)
488 $tmp[] = _http_build_query_helper($key.'['.$k.']', $v, $separator);
492 return implode($separator, $tmp);
497 /* End of file compatibility_helper.php */
498 /* Location: ./system/helpers/compatibility_helper.php */