summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDerek Jones <derek.jones@ellislab.com>2008-05-06 17:59:14 +0200
committerDerek Jones <derek.jones@ellislab.com>2008-05-06 17:59:14 +0200
commit00618de8c4aca96edb09094a86242b2495ba124d (patch)
tree84114be8dabcea1660cf254c971e12d8eb5e9683
parentfd93d22814b090ef85f3079d31fffa66d23d95e8 (diff)
Added a Compatibility Helper
-rw-r--r--system/helpers/compatibility_helper.php496
-rw-r--r--user_guide/changelog.html2
-rw-r--r--user_guide/helpers/compatibility_helper.html118
-rw-r--r--user_guide/nav/nav.js1
-rw-r--r--user_guide/toc.html1
5 files changed, 617 insertions, 1 deletions
diff --git a/system/helpers/compatibility_helper.php b/system/helpers/compatibility_helper.php
new file mode 100644
index 000000000..9cc282598
--- /dev/null
+++ b/system/helpers/compatibility_helper.php
@@ -0,0 +1,496 @@
+<?php if (!defined('BASEPATH')) exit('No direct script access allowed');
+/**
+ * CodeIgniter
+ *
+ * An open source application development framework for PHP 4.3.2 or newer
+ *
+ * @package CodeIgniter
+ * @author ExpressionEngine Dev Team
+ * @copyright Copyright (c) 2006, EllisLab, Inc.
+ * @license http://codeigniter.com/user_guide/license.html
+ * @link http://codeigniter.com
+ * @since Version 1.0
+ * @filesource
+ */
+
+// ------------------------------------------------------------------------
+
+/**
+ * CodeIgniter Compatibility Helpers
+ *
+ * This helper contains some functions based on the PEAR PHP_Compat library
+ * http://pear.php.net/package/PHP_Compat
+ *
+ * The PEAR compat library is a little bloated and the code doesn't harmonize
+ * well with CodeIgniter, so those functions have been refactored.
+ * We cheat a little and use CI's _exception_handler() to output our own PHP errors
+ * so that the behavior fully mimicks the PHP 5 counterparts. -- Derek Jones
+ *
+ * @package CodeIgniter
+ * @subpackage Helpers
+ * @category Helpers
+ * @author ExpressionEngine Dev Team
+ * @link http://codeigniter.com/user_guide/helpers/compatibility_helper.html
+ */
+
+// ------------------------------------------------------------------------
+
+if (! defined('PHP_EOL'))
+{
+ define('PHP_EOL', (DIRECTORY_SEPARATOR == '/') ? "\n" : "\r\n");
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * file_put_contents()
+ *
+ * Writes a string to a file
+ * http://us.php.net/manual/en/function.file_put_contents.php
+ * argument 4, $context, not supported
+ *
+ * @access public
+ * @param string file name
+ * @param mixed data to be written
+ * @param int flags
+ * @return int length of written string
+ */
+if (! function_exists('file_put_contents'))
+{
+ function file_put_contents($filename, $data, $flags = NULL)
+ {
+ if (is_scalar($data))
+ {
+ settype($data, 'STRING');
+ }
+
+ if (! is_string($data) && ! is_array($data) && ! is_resource($data))
+ {
+ $backtrace = debug_backtrace();
+ _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']);
+ return FALSE;
+ }
+
+ // read stream if given a stream resource
+ if (is_resource($data))
+ {
+ if (get_resource_type($data) !== 'stream')
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'file_put_contents(): supplied resource is not a valid stream resource', $backtrace[0]['file'], $backtrace[0]['line']);
+ return FALSE;
+ }
+
+ $text = '';
+
+ while (! feof($data))
+ {
+ $text .= fread($data, 4096);
+ }
+
+ $data = $text;
+ unset($text);
+ }
+
+ // strings only please!
+ if (is_array($data))
+ {
+ $data = implode('', $data);
+ }
+
+ // Set the appropriate mode
+ if (($flags & 8) > 0) // 8 = FILE_APPEND flag
+ {
+ $mode = FOPEN_WRITE_CREATE;
+ }
+ else
+ {
+ $mode = FOPEN_WRITE_CREATE_DESTRUCTIVE;
+ }
+
+ // Check if we're using the include path
+ if (($flags & 1) > 0) // 1 = FILE_USE_INCLUDE_PATH flag
+ {
+ $use_include_path = TRUE;
+ }
+ else
+ {
+ $use_include_path = FALSE;
+ }
+
+ $fp = @fopen($filename, $mode, $use_include_path);
+
+ if ($fp === FALSE)
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'file_put_contents('.htmlentities($filename).') failed to open stream', $backtrace[0]['file'], $backtrace[0]['line']);
+ return FALSE;
+ }
+
+ if (($flags & LOCK_EX) > 0)
+ {
+ if (! flock($fh, LOCK_EX))
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'file_put_contents('.htmlentities($filename).') unable to acquire an exclusive lock on file', $backtrace[0]['file'], $backtrace[0]['line']);
+ return FALSE;
+ }
+ }
+
+ // write it
+ if (($written = @fwrite($fp, $data)) === FALSE)
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'file_put_contents('.htmlentities($filename).') failed to write to '.htmlentities($filename), $backtrace[0]['file'], $backtrace[0]['line']);
+ }
+
+ // Close the handle
+ @fclose($fh);
+
+ // Return length
+ return $written;
+ }
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * fputcsv()
+ *
+ * Format line as CSV and write to file pointer
+ * http://us.php.net/manual/en/function.fputcsv.php
+ *
+ * @access public
+ * @param resource file pointer
+ * @param array data to be written
+ * @param string delimiter
+ * @param string enclosure
+ * @return int length of written string
+ */
+if (! function_exists('fputcsv'))
+{
+ function fputcsv($handle, $fields, $delimiter = ',', $enclosure = '"')
+ {
+ // Checking for a handle resource
+ if (! is_resource($handle))
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'fputcsv() expects parameter 1 to be stream resource, '.gettype($handle).' given', $backtrace[0]['file'], $backtrace[0]['line']);
+ return FALSE;
+ }
+
+ // OK, it is a resource, but is it a stream?
+ if (get_resource_type($handle) !== 'stream')
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'fputcsv() expects parameter 1 to be stream resource, '.get_resource_type($handle).' given', $backtrace[0]['file'], $backtrace[0]['line']);
+ return FALSE;
+ }
+
+ // Checking for an array of fields
+ if (! is_array($fields))
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'fputcsv() expects parameter 2 to be array, '.gettype($fields).' given', $backtrace[0]['file'], $backtrace[0]['line']);
+ return FALSE;
+ }
+
+ // validate delimiter
+ if (strlen($delimiter) > 1)
+ {
+ $delimiter = substr($delimiter, 0, 1);
+ $backtrace = debug_backtrace();
+ _exception_handler(E_NOTICE, 'fputcsv() delimiter must be one character long, "'.htmlentities($delimiter).'" used', $backtrace[0]['file'], $backtrace[0]['line']);
+ }
+
+ // validate enclosure
+ if (strlen($enclosure) > 1)
+ {
+ $enclosure = substr($enclosure, 0, 1);
+ $backtrace = debug_backtrace();
+ _exception_handler(E_NOTICE, 'fputcsv() enclosure must be one character long, "'.htmlentities($enclosure).'" used', $backtrace[0]['file'], $backtrace[0]['line']);
+
+ }
+
+ $out = '';
+
+ foreach ($fields as $cell)
+ {
+ $cell = str_replace($enclosure, $enclosure.$enclosure, $cell);
+
+ if (strpos($cell, $delimiter) !== FALSE || strpos($cell, $enclosure) !== FALSE || strpos($cell, "\n") !== FALSE)
+ {
+ $out .= $enclosure.$cell.$enclosure.$delimiter;
+ }
+ else
+ {
+ $out .= $cell.$delimiter;
+ }
+ }
+
+ $length = @fwrite($handle, substr($out, 0, -1)."\n");
+
+ return $length;
+ }
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * stripos()
+ *
+ * Find position of first occurrence of a case-insensitive string
+ * http://us.php.net/manual/en/function.stripos.php
+ *
+ * @access public
+ * @param string haystack
+ * @param string needle
+ * @param int offset
+ * @return int numeric position of the first occurrence of needle in the haystack
+ */
+if (! function_exists('stripos'))
+{
+ function stripos($haystack, $needle, $offset = NULL)
+ {
+ // Cast non string scalar values
+ if (is_scalar($haystack))
+ {
+ settype($haystack, 'STRING');
+ }
+
+ if (! is_string($haystack))
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'stripos() expects parameter 1 to be string, '.gettype($haystack).' given', $backtrace[0]['file'], $backtrace[0]['line']);
+ return FALSE;
+ }
+
+ if (! is_scalar($needle))
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'stripos() needle is not a string or an integer in '.$backtrace[0]['file'], $backtrace[0]['line']);
+ return FALSE;
+ }
+
+ if (is_float($offset))
+ {
+ $offset = (int)$offset;
+ }
+
+ if (! is_int($offset) && ! is_bool($offset) && ! is_null($offset))
+ {
+ $backtrace = debug_backtrace();
+ _exception_handler(E_USER_WARNING, 'stripos() expects parameter 3 to be long, '.gettype($offset).' given', $backtrace[0]['file'], $backtrace[0]['line']);
+ return NULL;
+ }
+
+ return strpos(strtolower($haystack), strtolower($needle), $offset);
+ }
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * str_ireplace()
+ *
+ * Find position of first occurrence of a case-insensitive string
+ * http://us.php.net/manual/en/function.str-ireplace.php
+ * (parameter 4, $count, is not supported as to do so in PHP 4 would make
+ * it a required parameter)
+ *
+ * @access public
+ * @param mixed search
+ * @param mixed replace
+ * @param mixed subject
+ * @return int numeric position of the first occurrence of needle in the haystack
+ */
+if (! function_exists('str_ireplace'))
+{
+ function str_ireplace($search, $replace, $subject)
+ {
+ // Nothing to do here
+ if ($search === NULL || $subject === NULL)
+ {
+ return $subject;
+ }
+
+ // Crazy arguments
+ if (is_scalar($search) && is_array($replace))
+ {
+ $backtrace = debug_backtrace();
+
+ if (is_object($replace))
+ {
+ show_error('Object of class '.get_class($replace).' could not be converted to string in '.$backtrace[0]['file'].' on line '.$backtrace[0]['line']);
+ }
+ else
+ {
+ _exception_handler(E_USER_NOTICE, 'Array to string conversion in '.$backtrace[0]['file'], $backtrace[0]['line']);
+ }
+ }
+
+ // Searching for an array
+ if (is_array($search))
+ {
+ // Replacing with an array
+ if (is_array($replace))
+ {
+ $search = array_values($search);
+ $replace = array_values($replace);
+
+ if (count($search) >= count($replace))
+ {
+ $replace = array_pad($replace, count($search), '');
+ }
+ else
+ {
+ $replace = array_slice($replace, 0, count($search));
+ }
+ }
+ else
+ {
+ // Replacing with a string all positions
+ $replace = array_fill(0, count($search), $replace);
+ }
+ }
+ else
+ {
+ //Searching for a string and replacing with a string.
+ $search = array((string)$search);
+ $replace = array((string)$replace);
+ }
+
+ // Prepare the search array
+ foreach ($search as $search_key => $search_value)
+ {
+ $search[$search_key] = '/'.preg_quote($search_value, '/').'/i';
+ }
+
+ // Prepare the replace array (escape backreferences)
+ foreach ($replace as $k => $v)
+ {
+ $replace[$k] = str_replace(array(chr(92), '$'), array(chr(92).chr(92), '\$'), $v);
+ }
+
+ // do the replacement
+ $result = preg_replace($search, $replace, (array)$subject);
+
+ // Check if subject was initially a string and return it as a string
+ if (! is_array($subject))
+ {
+ return current($result);
+ }
+
+ // Otherwise, just return the array
+ return $result;
+ }
+}
+
+// ------------------------------------------------------------------------
+
+/**
+ * http_build_query()
+ *
+ * Generate URL-encoded query string
+ * http://us.php.net/manual/en/function.http-build-query.php
+ *
+ * @access public
+ * @param array form data
+ * @param string numeric prefix
+ * @param string argument separator
+ * @return string URL-encoded string
+ */
+if (! function_exists('http_build_query'))
+{
+ function http_build_query($formdata, $numeric_prefix = NULL, $separator = NULL)
+ {
+ // Check the data
+ if (! is_array($formdata) && ! is_object($formdata))
+ {
+ $backtrace = debug_backtrace();
+ _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']);
+ return FALSE;
+ }
+
+ // Cast it as array
+ if (is_object($formdata))
+ {
+ $formdata = get_object_vars($formdata);
+ }
+
+ // If the array is empty, return NULL
+ if (empty($formdata))
+ {
+ return NULL;
+ }
+
+ // Argument separator
+ if ($separator === NULL)
+ {
+ $separator = ini_get('arg_separator.output');
+
+ if (strlen($separator) == 0)
+ {
+ $separator = '&';
+ }
+ }
+
+ // Start building the query
+ $tmp = array();
+
+ foreach ($formdata as $key => $val)
+ {
+ if ($val === NULL)
+ {
+ continue;
+ }
+
+ if (is_integer($key) && $numeric_prefix != NULL)
+ {
+ $key = $numeric_prefix.$key;
+ }
+
+ if (is_resource($val))
+ {
+ return NULL;
+ }
+
+ // hand it off to a recursive parser
+ $tmp[] = _http_build_query_helper($key, $val, $separator);
+ }
+
+ return implode($separator, $tmp);
+ }
+
+
+ // Helper helper. Remind anyone of college?
+ // Required to handle recursion in nested arrays.
+ //
+ // You could shave fractions of fractions of a second by moving where
+ // the urlencoding takes place, but it's much less intuitive, and if
+ // your application has 10,000 form fields, well, you have other problems ;)
+ function _http_build_query_helper($key, $val, $separator = '&')
+ {
+ if (is_scalar($val))
+ {
+ return urlencode($key).'='.urlencode($val);
+ }
+ else
+ {
+ // arrays please
+ if (is_object($val))
+ {
+ $val = get_object_vars($val);
+ }
+
+ foreach ($val as $k => $v)
+ {
+ $tmp[] = _http_build_query_helper($key.'['.$k.']', $v, $separator);
+ }
+ }
+
+ return implode($separator, $tmp);
+ }
+}
+
+?> \ No newline at end of file
diff --git a/user_guide/changelog.html b/user_guide/changelog.html
index 0279e48b2..2166c5e54 100644
--- a/user_guide/changelog.html
+++ b/user_guide/changelog.html
@@ -89,7 +89,7 @@ SVN Commit: not currently released</p>
</li>
<li>Helpers
<ul>
- <li>Added a Compatibility Helper for using some common PHP 5 functions safely in applications that might run on PHP 4 servers</li>
+ <li>Added a <a href="helpers/compatibility_helper.html">Compatibility Helper</a> for using some common PHP 5 functions safely in applications that might run on PHP 4 servers</li>
<li>Added <kbd>form_button()</kbd> in the <a href="helpers/form_helper.html">Form helper</a>.</li>
<li>Changed the radio() and checkbox() functions to default to not checked by default.</li>
<li>Added the ability to include an optional HTTP Response Code in the <kbd>redirect()</kbd> function of the <a href="helpers/url_helper.html">URL Helper</a>.</li>
diff --git a/user_guide/helpers/compatibility_helper.html b/user_guide/helpers/compatibility_helper.html
new file mode 100644
index 000000000..15411c9dd
--- /dev/null
+++ b/user_guide/helpers/compatibility_helper.html
@@ -0,0 +1,118 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
+<head>
+
+<title>Compatibility Helper : CodeIgniter User Guide</title>
+
+<style type='text/css' media='all'>@import url('../userguide.css');</style>
+<link rel='stylesheet' type='text/css' media='all' href='../userguide.css' />
+
+<script type="text/javascript" src="../nav/nav.js"></script>
+<script type="text/javascript" src="../nav/prototype.lite.js"></script>
+<script type="text/javascript" src="../nav/moo.fx.js"></script>
+<script type="text/javascript" src="../nav/user_guide_menu.js"></script>
+
+<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+<meta http-equiv='expires' content='-1' />
+<meta http-equiv= 'pragma' content='no-cache' />
+<meta name='robots' content='all' />
+<meta name='author' content='ExpressionEngine Dev Team' />
+<meta name='description' content='CodeIgniter User Guide' />
+
+</head>
+<body>
+
+<!-- START NAVIGATION -->
+<div id="nav"><div id="nav_inner"><script type="text/javascript">create_menu('../');</script></div></div>
+<div id="nav2"><a name="top"></a><a href="javascript:void(0);" onclick="myHeight.toggle();"><img src="../images/nav_toggle.jpg" width="153" height="44" border="0" title="Toggle Table of Contents" alt="Toggle Table of Contents" /></a></div>
+<div id="masthead">
+<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
+<tr>
+<td><h1>CodeIgniter User Guide Version 1.6.1</h1></td>
+<td id="breadcrumb_right"><a href="../toc.html">Table of Contents Page</a></td>
+</tr>
+</table>
+</div>
+<!-- END NAVIGATION -->
+
+
+<!-- START BREADCRUMB -->
+
+<table cellpadding="0" cellspacing="0" border="0" style="width:100%">
+<tr>
+<td id="breadcrumb">
+<a href="http://codeigniter.com/">CodeIgniter Home</a> &nbsp;&#8250;&nbsp;
+<a href="../index.html">User Guide Home</a> &nbsp;&#8250;&nbsp;
+Compatibility Helper
+</td>
+<td id="searchbox"><form method="get" action="http://www.google.com/search"><input type="hidden" name="as_sitesearch" id="as_sitesearch" value="codeigniter.com/user_guide/" />Search User Guide&nbsp; <input type="text" class="input" style="width:200px;" name="q" id="q" size="31" maxlength="255" value="" />&nbsp;<input type="submit" class="submit" name="sa" value="Go" /></form></td>
+</tr>
+</table>
+<!-- END BREADCRUMB -->
+
+<br clear="all" />
+
+
+<!-- START CONTENT -->
+<div id="content">
+
+
+<h1>Compatibility Helper</h1>
+
+<p>The Compatibility Helper file contains PHP 4 implementations of some PHP 5 only native PHP functions and constants. This can be useful
+ if you'd like to take advantage of some of these native function but your application may end up running on a PHP 4 server.
+ In these cases, it may be advantageous to <a href="../general/autoloader.html">Auto-load</a> the Compatibility Helper so you
+ do not have to load it in each controller.</p>
+
+<p class="important"><strong>Note:</strong> There are a few compatibility functions that are in CodeIgniter's native Compat.php file.
+ You may use those functions without loading this helper. The functions are split between that file and this Helper so that only
+ functions required by the framework are included by default. This way, whether or not you load the additional functions in this Helper
+ remains your choice.</p>
+
+<h2>Loading this Helper</h2>
+
+<p>This helper is loaded using the following code:</p>
+<code>$this->load->helper('compatibility');</code>
+
+<h2>Available Constants</h2>
+<p>The following constants are available:</p>
+
+<h3>PHP_EOL</h3>
+<p>The newline character for the server's current OS, e.g. on Windows systems "\r\n", on *nix "\n".
+
+<h2>Available Functions</h2>
+<p>The following functions are available (see linked PHP documentation for documentation):</p>
+
+<h3><a href="http://us.php.net/manual/en/function.file-put-contents.php">file_put_contents()</a> - The fourth parameter, <var>$context</var>, is not supported.</h3>
+
+<h3><a href="http://us.php.net/manual/en/function.fputcsv.php">fputcsv()</a></h3>
+
+<h3><a href="http://us.php.net/manual/en/function.http-build-query.php">http_build_query()</a></h3>
+
+<h3><a href="http://us.php.net/manual/en/function.str-ireplace.php">str_ireplace()</a> - The fourth parameter, <var>$count</var>, is not supported, as PHP 4 would make it become required.</h3>
+
+<h3><a href="http://us.php.net/manual/en/function.stripos.php">stripos()</a></h3>
+
+
+
+
+
+
+
+</div>
+<!-- END CONTENT -->
+
+
+<div id="footer">
+<p>
+Previous Topic:&nbsp;&nbsp;<a href="array_helper.html">Array Helper</a>
+&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
+<a href="#top">Top of Page</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
+<a href="../index.html">User Guide Home</a>&nbsp;&nbsp;&nbsp;&middot;&nbsp;&nbsp;
+Next Topic:&nbsp;&nbsp;<a href="cookie_helper.html">Cookie Helper</a>
+</p>
+<p><a href="http://codeigniter.com">CodeIgniter</a> &nbsp;&middot;&nbsp; Copyright &#169; 2006-2008 &nbsp;&middot;&nbsp; <a href="http://ellislab.com/">Ellislab, Inc.</a></p>
+</div>
+
+</body>
+</html> \ No newline at end of file
diff --git a/user_guide/nav/nav.js b/user_guide/nav/nav.js
index c3c0d1122..88992804f 100644
--- a/user_guide/nav/nav.js
+++ b/user_guide/nav/nav.js
@@ -98,6 +98,7 @@ function create_menu(basepath)
'<h3>Helper Reference</h3>' +
'<ul>' +
'<li><a href="'+base+'helpers/array_helper.html">Array Helper</a></li>' +
+ '<li><a href="'+base+'helpers/compatibility_helper.html">Compatibility Helper</a></li>' +
'<li><a href="'+base+'helpers/cookie_helper.html">Cookie Helper</a></li>' +
'<li><a href="'+base+'helpers/date_helper.html">Date Helper</a></li>' +
'<li><a href="'+base+'helpers/directory_helper.html">Directory Helper</a></li>' +
diff --git a/user_guide/toc.html b/user_guide/toc.html
index 6a1813a32..e9dd44859 100644
--- a/user_guide/toc.html
+++ b/user_guide/toc.html
@@ -154,6 +154,7 @@ Table of Contents
<h3>Helper Reference</h3>
<ul>
<li><a href="./helpers/array_helper.html">Array Helper</a></li>
+<li><a href="./helpers/compatibility_helper.html">Compatibility Helper</a></li>
<li><a href="./helpers/cookie_helper.html">Cookie Helper</a></li>
<li><a href="./helpers/date_helper.html">Date Helper</a></li>
<li><a href="./helpers/directory_helper.html">Directory Helper</a></li>