PageRenderTime 39ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/fuel/application/helpers/compatibility_helper.php

https://github.com/miso003/FUEL-CMS
PHP | 202 lines | 161 code | 6 blank | 35 comment | 8 complexity | eb646777ca4e2fc6f2130f78c6a04d46 MD5 | raw file
  1. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  2. /**
  3. * FUEL CMS
  4. * http://www.getfuelcms.com
  5. *
  6. * An open source Content Management System based on the
  7. * Codeigniter framework (http://codeigniter.com)
  8. *
  9. * @package FUEL CMS
  10. * @author David McReynolds @ Daylight Studio
  11. * @copyright Copyright (c) 2011, Run for Daylight LLC.
  12. * @license http://www.getfuelcms.com/user_guide/general/license
  13. * @link http://www.getfuelcms.com
  14. * @filesource
  15. */
  16. // ------------------------------------------------------------------------
  17. /**
  18. * Extends CI's array helper functions
  19. *
  20. * @package FUEL CMS
  21. * @subpackage Helpers
  22. * @category Helpers
  23. * @author David McReynolds @ Daylight Studio
  24. * @link http://www.getfuelcms.com/user_guide/helpers/compatibility_helper
  25. */
  26. // --------------------------------------------------------------------
  27. /**
  28. * Used for older versions of PHP that don't support json_encode.
  29. * another option http://derekallard.com/blog/post/using-json-on-servers-without-native-support/
  30. * Original function found here: http://php.net/manual/en/function.json-encode.php
  31. *
  32. * @access public
  33. * @param mixed php value to encode into JSON format
  34. * @return string
  35. */
  36. if (!function_exists('json_encode'))
  37. {
  38. function json_encode($a=FALSE)
  39. {
  40. if (is_null($a)) return 'null';
  41. if ($a === FALSE) return 'false';
  42. if ($a === TRUE) return 'true';
  43. if (is_scalar($a))
  44. {
  45. if (is_float($a))
  46. {
  47. // Always use "." for floats.
  48. return floatval(str_replace(",", ".", strval($a)));
  49. }
  50. if (is_string($a))
  51. {
  52. static $jsonReplaces = array(array("\\", "/", "\n", "\t", "\r", "\b", "\f", '"'), array('\\\\', '\\/', '\\n', '\\t', '\\r', '\\b', '\\f', '\"'));
  53. return '"' . str_replace($jsonReplaces[0], $jsonReplaces[1], $a) . '"';
  54. }
  55. else
  56. return $a;
  57. }
  58. $isList = TRUE;
  59. for ($i = 0, reset($a); $i < count($a); $i++, next($a))
  60. {
  61. if (key($a) !== $i)
  62. {
  63. $isList = FALSE;
  64. break;
  65. }
  66. }
  67. $result = array();
  68. if ($isList)
  69. {
  70. foreach ($a as $v) $result[] = json_encode($v);
  71. return '[' . join(',', $result) . ']';
  72. }
  73. else
  74. {
  75. foreach ($a as $k => $v) $result[] = json_encode($k).':'.json_encode($v);
  76. return '{' . join(',', $result) . '}';
  77. }
  78. }
  79. }
  80. // --------------------------------------------------------------------
  81. /**
  82. * Used for older versions of PHP that don't support json_decode.
  83. * another option http://derekallard.com/blog/post/using-json-on-servers-without-native-support/
  84. * Original function found here: http://php.net/manual/en/function.json-decode.php
  85. *
  86. * @access public
  87. * @param string json formatted string
  88. * @return mixed
  89. */
  90. if ( !function_exists('json_decode')){
  91. function json_decode($json)
  92. {
  93. // Author: walidator.info 2009
  94. $comment = FALSE;
  95. $out = '$x=';
  96. for ($i=0; $i<strlen($json); $i++)
  97. {
  98. if (!$comment)
  99. {
  100. if ($json[$i] == '{')
  101. {
  102. $out .= ' array(';
  103. }
  104. else if ($json[$i] == '}')
  105. {
  106. $out .= ')';
  107. }
  108. else if ($json[$i] == ':')
  109. {
  110. $out .= '=>';
  111. }
  112. else
  113. {
  114. $out .= $json[$i];
  115. }
  116. }
  117. else
  118. {
  119. $out .= $json[$i];
  120. }
  121. if ($json[$i] == '"')
  122. {
  123. $comment = !$comment;
  124. }
  125. }
  126. eval($out . ';');
  127. return $x;
  128. }
  129. }
  130. // --------------------------------------------------------------------
  131. /**
  132. * Used for older versions of PHP that don't support str_getcsv.
  133. * Original function found here: http://php.net/manual/en/function.str-getcsv.php
  134. *
  135. * @access public
  136. * @param string
  137. * @param string
  138. * @param string
  139. * @param string
  140. * @return mixed
  141. */
  142. if (!function_exists('str_getcsv'))
  143. {
  144. function str_getcsv($input, $delimiter = ",", $enclosure = '"', $escape = "\\")
  145. {
  146. $fiveMBs = 5 * 1024 * 1024;
  147. $fp = fopen("php://temp/maxmemory:$fiveMBs", 'r+');
  148. fputs($fp, $input);
  149. rewind($fp);
  150. $data = fgetcsv($fp, 1000, $delimiter, $enclosure); // $escape only got added in 5.3.0
  151. fclose($fp);
  152. return $data;
  153. }
  154. }
  155. // --------------------------------------------------------------------
  156. /**
  157. * Not really a compatibility function since it doesn't exist natively in PHP.
  158. * However it probably should so we provide it here.
  159. * A version of the original function found here: http://glossword.googlecode.com/svn-history/r600/trunk/core/gw_includes/functions.php
  160. *
  161. * @access public
  162. * @param string
  163. * @param string
  164. * @param string
  165. * @return mixed
  166. */
  167. if(!function_exists('str_putcsv'))
  168. {
  169. function str_putcsv($input, $delimiter = ',', $enclosure = '"')
  170. {
  171. // Open a memory "file" for read/write...
  172. $fp = fopen('php://temp', 'r+');
  173. // ... write the $input array to the "file" using fputcsv()...
  174. fputcsv($fp, $input, $delimiter, $enclosure);
  175. // ... rewind the "file" so we can read what we just wrote...
  176. rewind($fp);
  177. // ... read the entire line into a variable...
  178. $data = fgets($fp);
  179. // ... close the "file"...
  180. fclose($fp);
  181. // ... and return the $data to the caller, with the trailing newline from fgets() removed.
  182. return rtrim( $data, "\n" );
  183. }
  184. }
  185. /* End of file compatibility_helper.php */
  186. /* Location: ./application/helpers/compatibility_helper.php */