From 8a17feb69ba02ecf22c817391bdcf7908b438603 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Tue, 15 Jul 2014 17:35:12 +0200 Subject: [PATCH 01/20] Deprecated some functions and improved method documentation --- htdocs/core/lib/functions.lib.php | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index b1f11590bdc..81671ab82d3 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3631,20 +3631,19 @@ function dol_string_nohtmltag($StringHtml,$removelinefeed=1,$pagecodeto='UTF-8') /** - * Replace CRLF in string with a HTML BR tag + * Replace CRLF in string with a HTML BR tag * - * @param string $stringtoencode String to encode - * @param string $nl2brmode 0=Adding br before \n, 1=Replacing \n by br - * @param string $forxml false=Use
, true=Use
- * @return string String encoded + * @param string $stringtoencode String to encode + * @param int $nl2brmode 0=Adding br before \n, 1=Replacing \n by br + * @param bool $forxml false=Use
, true=Use
+ * @return string String encoded */ function dol_nl2br($stringtoencode,$nl2brmode=0,$forxml=false) { if (! $nl2brmode) { - // We use @ to avoid warning on PHP4 that does not support entity encoding from UTF8; - if (version_compare(PHP_VERSION, '5.3.0') < 0) return @nl2br($stringtoencode); - else return @nl2br($stringtoencode,$forxml); + if (version_compare(PHP_VERSION, '5.3.0') < 0) return nl2br($stringtoencode); + else return nl2br($stringtoencode,$forxml); } else { @@ -3744,12 +3743,11 @@ function dol_html_entity_decode($a,$b,$c='UTF-8') * @param string $encoding Encoding * @param bool $double_encode When double_encode is turned off PHP will not encode existing html entities * @return string $ret Encoded string + * @deprecated Since PHP4 support is no longer available, this function does not make sense */ function dol_htmlentities($string, $flags=null, $encoding='UTF-8', $double_encode=false) { - // We use @ to avoid warning on PHP4 that does not support entity decoding to UTF8; - $ret=@htmlentities($string, $flags, $encoding, $double_encode); - return $ret; + return htmlentities($string, $flags, $encoding, $double_encode); } @@ -3780,7 +3778,7 @@ function dol_string_is_good_iso($s) * Return nb of lines of a clear text * * @param string $s String to check - * @param string $maxchar Not yet used + * @param int $maxchar Not yet used * @return int Number of lines */ function dol_nboflines($s,$maxchar=0) @@ -3835,12 +3833,12 @@ function dol_nboflines_bis($text,$maxlinesize=0,$charset='UTF-8') /** * Same function than microtime in PHP 5 but compatible with PHP4 * - * @return float Time (millisecondes) with microsecondes in decimal part + * @return float Time (millisecondes) with microsecondes in decimal part + * @deprecated Dolibarr does not support PHP4, you should use native function */ function dol_microtime_float() { - list($usec, $sec) = explode(" ", microtime()); - return ((float) $usec + (float) $sec); + return microtime(); } /** From 2142ab997d4d1f94ce9e665a93e4030c5a7f8e7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Fri, 18 Jul 2014 11:17:32 +0200 Subject: [PATCH 02/20] Removed Dolibarr implementation of json_encode and json_decode, and deprecated dol_json_encode --- htdocs/core/lib/json.lib.php | 76 +----------------------------------- 1 file changed, 2 insertions(+), 74 deletions(-) diff --git a/htdocs/core/lib/json.lib.php b/htdocs/core/lib/json.lib.php index 69dbb4eba1a..006010d61b7 100644 --- a/htdocs/core/lib/json.lib.php +++ b/htdocs/core/lib/json.lib.php @@ -23,73 +23,16 @@ * \ingroup core */ -if (! function_exists('json_encode')) -{ - /** - * Implement json_encode for PHP that does not support it - * - * @param mixed $elements PHP Object to json encode - * @return string Json encoded string - */ - function json_encode($elements) - { - return dol_json_encode($elements); - } -} - /** * Implement json_encode for PHP that does not support it * * @param mixed $elements PHP Object to json encode * @return string Json encoded string + * @deprecated Use json_encode. */ function dol_json_encode($elements) { - $num=count($elements); - if (is_object($elements)) // Count number of properties for an object - { - $num=0; - foreach($elements as $key => $value) $num++; - } - //var_dump($num); - - // determine type - if (is_numeric(key($elements)) && key($elements) == 0) - { - // indexed (list) - $keysofelements=array_keys($elements); // Elements array mus have key that does not start with 0 and end with num-1, so we will use this later. - $output = '['; - for ($i = 0, $last = ($num - 1); $i < $num; $i++) - { - if (! isset($elements[$keysofelements[$i]])) continue; - if (is_array($elements[$keysofelements[$i]]) || is_object($elements[$keysofelements[$i]])) $output.= json_encode($elements[$keysofelements[$i]]); - else $output .= _val($elements[$keysofelements[$i]]); - if ($i !== $last) $output.= ','; - } - $output.= ']'; - } - else - { - // associative (object) - $output = '{'; - $last = $num - 1; - $i = 0; - $tmpelements=array(); - if (is_array($elements)) $tmpelements=$elements; - if (is_object($elements)) $tmpelements=get_object_vars($elements); - foreach($tmpelements as $key => $value) - { - $output .= '"'.$key.'":'; - if (is_array($value)) $output.= json_encode($value); - else $output .= _val($value); - if ($i !== $last) $output.= ','; - ++$i; - } - $output.= '}'; - } - - // return - return $output; + return json_encode($elements); } /** @@ -198,21 +141,6 @@ function _val($val) else return 'null'; } -if (! function_exists('json_decode')) -{ - /** - * Implement json_decode for PHP that does not support it - * - * @param string $json Json encoded to PHP Object or Array - * @param bool $assoc False return an object, true return an array - * @return mixed Object or Array - */ - function json_decode($json, $assoc=false) - { - return dol_json_decode($json, $assoc); - } -} - /** * Implement json_decode for PHP that does not support it * From 214fe2d552e608ce8c5dec97daf4e03490e7f4e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Fri, 18 Jul 2014 11:18:25 +0200 Subject: [PATCH 03/20] Optimized isValidEmail function and load json.lib.php always as it contains common Dolibarr functions --- htdocs/core/lib/functions.lib.php | 21 +++++---------------- 1 file changed, 5 insertions(+), 16 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 81671ab82d3..3bea3697f52 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -10,6 +10,7 @@ * Copyright (C) 2010-2014 Juanjo Menent * Copyright (C) 2013 Cédric Salvador * Copyright (C) 2013 Alexandre Spangaro + * Copyright (C) 2014 Marcos García * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -32,11 +33,7 @@ * This file contains all frequently used functions. */ - -if (! function_exists('json_encode')) -{ - include_once DOL_DOCUMENT_ROOT .'/core/lib/json.lib.php'; -} +include_once DOL_DOCUMENT_ROOT .'/core/lib/json.lib.php'; /** * Function to return value of a static property when class @@ -1478,19 +1475,11 @@ function dol_print_address($address, $htmlid, $mode, $id) */ function isValidEmail($address) { - if (preg_match("/.*<(.+)>/i", $address, $regs)) { - $address = $regs[1]; - } - // 2 letters domains extensions are for countries - // 3 letters domains extensions: biz|com|edu|gov|int|mil|net|org|pro|... - if (preg_match("/^[^@\s\t]+@([a-zA-Z0-9\-]+\.)+([a-zA-Z0-9\-]{2,3}|asso|aero|coop|info|name)\$/i",$address)) - { + if (filter_var($address, FILTER_VALIDATE_EMAIL)) { return true; } - else - { - return false; - } + + return false; } /** From 997a7ff8432960976c644f359da4b6aa3dfd0c60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Fri, 18 Jul 2014 11:57:21 +0200 Subject: [PATCH 04/20] Deprecated function dol_json_decode and replaced with native behaviour --- htdocs/core/lib/json.lib.php | 170 +---------------------------------- 1 file changed, 3 insertions(+), 167 deletions(-) diff --git a/htdocs/core/lib/json.lib.php b/htdocs/core/lib/json.lib.php index 006010d61b7..dc87a04224a 100644 --- a/htdocs/core/lib/json.lib.php +++ b/htdocs/core/lib/json.lib.php @@ -28,188 +28,24 @@ * * @param mixed $elements PHP Object to json encode * @return string Json encoded string - * @deprecated Use json_encode. + * @deprecated Use json_encode native function */ function dol_json_encode($elements) { return json_encode($elements); } -/** - * Return text according to type - * - * @param mixed $val Value to show - * @return string Formated value - */ -function _val($val) -{ - if (is_string($val)) - { - // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT - $ascii = ''; - $strlen_var = strlen($val); - - /* - * Iterate over every character in the string, - * escaping with a slash or encoding to UTF-8 where necessary - */ - for ($c = 0; $c < $strlen_var; ++$c) { - - $ord_var_c = ord($val{$c}); - - switch (true) { - case $ord_var_c == 0x08: - $ascii .= '\b'; - break; - case $ord_var_c == 0x09: - $ascii .= '\t'; - break; - case $ord_var_c == 0x0A: - $ascii .= '\n'; - break; - case $ord_var_c == 0x0C: - $ascii .= '\f'; - break; - case $ord_var_c == 0x0D: - $ascii .= '\r'; - break; - - case $ord_var_c == 0x22: - case $ord_var_c == 0x2F: - case $ord_var_c == 0x5C: - // double quote, slash, slosh - $ascii .= '\\'.$val{$c}; - break; - - case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): - // characters U-00000000 - U-0000007F (same as ASCII) - $ascii .= $val{$c}; - break; - - case (($ord_var_c & 0xE0) == 0xC0): - // characters U-00000080 - U-000007FF, mask 110XXXXX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, ord($val{$c + 1})); - $c += 1; - $utf16 = utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - - case (($ord_var_c & 0xF0) == 0xE0): - // characters U-00000800 - U-0000FFFF, mask 1110XXXX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2})); - $c += 2; - $utf16 = utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - - case (($ord_var_c & 0xF8) == 0xF0): - // characters U-00010000 - U-001FFFFF, mask 11110XXX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}), ord($val{$c + 3})); - $c += 3; - $utf16 = utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - - case (($ord_var_c & 0xFC) == 0xF8): - // characters U-00200000 - U-03FFFFFF, mask 111110XX - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}), ord($val{$c + 3}), ord($val{$c + 4})); - $c += 4; - $utf16 = utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - - case (($ord_var_c & 0xFE) == 0xFC): - // characters U-04000000 - U-7FFFFFFF, mask 1111110X - // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 - $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}), ord($val{$c + 3}), ord($val{$c + 4}), ord($val{$c + 5})); - $c += 5; - $utf16 = utf82utf16($char); - $ascii .= sprintf('\u%04s', bin2hex($utf16)); - break; - } - } - - return '"'.$ascii.'"'; - } - elseif (is_int($val)) return sprintf('%d', $val); - elseif (is_float($val)) return sprintf('%F', $val); - elseif (is_bool($val)) return ($val ? 'true' : 'false'); - else return 'null'; -} - /** * Implement json_decode for PHP that does not support it * * @param string $json Json encoded to PHP Object or Array * @param bool $assoc False return an object, true return an array. Try to always use it with true ! * @return mixed Object or Array + * @deprecated Use json_decode native function */ function dol_json_decode($json, $assoc=false) { - $comment = false; - - $out=''; - $strLength = strlen($json); // Must stay strlen and not dol_strlen because we want technical length, not visible length - for ($i=0; $i<$strLength; $i++) - { - if (! $comment) - { - if (($json[$i] == '{') || ($json[$i] == '[')) $out.= 'array('; - else if (($json[$i] == '}') || ($json[$i] == ']')) $out.= ')'; - else if ($json[$i] == ':') $out.= ' => '; - else $out.=$json[$i]; - } - else $out.= $json[$i]; - if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment; - } - - $out=_unval($out); - - // Return an array - if ($out != '') eval('$array = '.$out.';'); - else $array=array(); - - // Return an object - if (! $assoc) - { - if (! empty($array)) - { - $object = false; - - foreach ($array as $key => $value) - { - $object->{$key} = $value; - } - - return $object; - } - - return false; - } - - return $array; -} - -/** - * Return text according to type - * - * @param mixed $val Value to decode - * @return string Formated value - */ -function _unval($val) -{ - while (preg_match('/\\\u([0-9A-F]{2})([0-9A-F]{2})/i', $val, $reg)) - { - // single, escaped unicode character - $utf16 = chr(hexdec($reg[1])) . chr(hexdec($reg[2])); - $utf8 = utf162utf8($utf16); - $val=preg_replace('/\\\u'.$reg[1].$reg[2].'/i',$utf8,$val); - } - return $val; + return json_decode($json, $assoc); } /** From 2c7b2cf227cde548c7df47270110cd9ffbe6a3d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Fri, 18 Jul 2014 11:57:49 +0200 Subject: [PATCH 05/20] Copyright --- htdocs/core/lib/functions2.lib.php | 1 + 1 file changed, 1 insertion(+) diff --git a/htdocs/core/lib/functions2.lib.php b/htdocs/core/lib/functions2.lib.php index 4e9598d5e20..d159a6bbb88 100644 --- a/htdocs/core/lib/functions2.lib.php +++ b/htdocs/core/lib/functions2.lib.php @@ -2,6 +2,7 @@ /* Copyright (C) 2008-2011 Laurent Destailleur * Copyright (C) 2008-2012 Regis Houssin * Copyright (C) 2008 Raphael Bertrand (Resultic) + * Copyright (C) 2014 Marcos García * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by From 55bfc98d481c9f8570f0fa60b6052baf0e25327f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Fri, 18 Jul 2014 12:00:01 +0200 Subject: [PATCH 06/20] Corrected dol_microtime_float refactor --- htdocs/core/lib/functions.lib.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 3bea3697f52..c23c14aecee 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3827,7 +3827,7 @@ function dol_nboflines_bis($text,$maxlinesize=0,$charset='UTF-8') */ function dol_microtime_float() { - return microtime(); + return microtime(true); } /** From a3ceb2a9705d31bc3d442826becd1daaaaff9e6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Fri, 18 Jul 2014 12:01:19 +0200 Subject: [PATCH 07/20] Added a test for isValidUrl function --- test/phpunit/Functions2LibTest.php | 56 ++++++++++++++++++++++++++---- 1 file changed, 49 insertions(+), 7 deletions(-) diff --git a/test/phpunit/Functions2LibTest.php b/test/phpunit/Functions2LibTest.php index 8e4bfe7550f..8ebd2dd3dd6 100755 --- a/test/phpunit/Functions2LibTest.php +++ b/test/phpunit/Functions2LibTest.php @@ -146,12 +146,54 @@ class Functions2LibTest extends PHPUnit_Framework_TestCase */ public function testIsValidUrl() { - $result=isValidUrl('http://www.google.com',1); - print __METHOD__." result=".$result."\n"; - $this->assertEquals(1,$result); - $result=isValidUrl('www.google.com',2); - print __METHOD__." result=".$result."\n"; - $this->assertEquals(0,$result); + //Simple check + $result = isValidUrl('http://google.com'); + $this->assertEquals($result, 1); + + $result = isValidUrl('google'); + $this->assertEquals($result, 0); + + //With scheme check + $result = isValidUrl('http://www.google.com', 1); + $this->assertEquals($result, 1); + + $result = isValidUrl('ftp://www.google.com', 1); + $this->assertEquals($result, 0); + + //With password check + $result = isValidUrl('http://user:password@http://www.google.com', 1, 1); + $this->assertEquals($result, 1); + + $result = isValidUrl('http://www.google.com', 1, 1); + $this->assertEquals($result, 0); + + //With port check + $result = isValidUrl('http://google.com:8080', 0, 0, 1); + $this->assertEquals($result, 1); + + $result = isValidUrl('http://google.com', 0, 0, 1); + $this->assertEquals($result, 0); + + //With path check + $result = isValidUrl('http://google.com/search', 0, 0, 0, 1); + $this->assertEquals($result, 1); + + $result = isValidUrl('http://google.com', 0, 0, 0, 1); + $this->assertEquals($result, 0); + + //With query check + $result = isValidUrl('http://google.com/search?test=test', 0, 0, 0, 0, 1); + $this->assertEquals($result, 1); + + $result = isValidUrl('http://google.com', 0, 0, 0, 0, 1); + $this->assertEquals($result, 0); + + //With anchor check + $result = isValidUrl('http://google.com/search#done', 0, 0, 0, 0, 0, 1); + $this->assertEquals($result, 1); + + $result = isValidUrl('http://google.com/search', 0, 0, 0, 0, 0, 1); + $this->assertEquals($result, 0); } -} +} \ No newline at end of file From 3e160fb20907c129c5a971653044d638439d2033 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Fri, 18 Jul 2014 12:05:03 +0200 Subject: [PATCH 08/20] Testing dol_json_encode is not interesting now as it uses a core function --- test/phpunit/JsonLibTest.php | 53 ------------------------------------ 1 file changed, 53 deletions(-) diff --git a/test/phpunit/JsonLibTest.php b/test/phpunit/JsonLibTest.php index caceb6d0dc3..67485c118e2 100755 --- a/test/phpunit/JsonLibTest.php +++ b/test/phpunit/JsonLibTest.php @@ -116,57 +116,4 @@ class JsonLibTest extends PHPUnit_Framework_TestCase print __METHOD__."\n"; } - /** - * testJsonEncode - * - * @return void - */ - public function testJsonEncode() - { - //$this->sharedFixture - global $conf,$user,$langs,$db; - $this->savconf=$conf; - $this->savuser=$user; - $this->savlangs=$langs; - $this->savdb=$db; - - // Do a test with an array starting with 0 - $arraytotest=array(0=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "')); - $arrayencodedexpected='[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]'; - - $encoded=json_encode($arraytotest); - $this->assertEquals($arrayencodedexpected,$encoded); - $decoded=json_decode($encoded,true); - $this->assertEquals($arraytotest,$decoded,'test for json_xxx'); - - $encoded=dol_json_encode($arraytotest); - $this->assertEquals($arrayencodedexpected,$encoded); - $decoded=dol_json_decode($encoded,true); - $this->assertEquals($arraytotest,$decoded,'test for dol_json_xxx'); - - // Same test but array start with 2 instead of 0 - $arraytotest=array(2=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "')); - $arrayencodedexpected='{"2":{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}}'; - - $encoded=json_encode($arraytotest); - $this->assertEquals($arrayencodedexpected,$encoded); - $decoded=json_decode($encoded,true); - $this->assertEquals($arraytotest,$decoded,'test for json_xxx'); - - $encoded=dol_json_encode($arraytotest); - $this->assertEquals($arrayencodedexpected,$encoded); - $decoded=dol_json_decode($encoded,true); - $this->assertEquals($arraytotest,$decoded,'test for dol_json_xxx'); - - // Test with object - $now=gmmktime(12,0,0,1,1,1970); - $objecttotest=new stdClass(); - $objecttotest->property1='abc'; - $objecttotest->property2=1234; - $objecttotest->property3=$now; - $encoded=dol_json_encode($objecttotest); - $this->assertEquals('{"property1":"abc","property2":1234,"property3":43200}',$encoded); - - } - } From cf1a2c44a359291288d7cd8f49b86ca4bb36d26a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Sun, 20 Jul 2014 08:33:56 +0200 Subject: [PATCH 09/20] Removed empty test --- test/phpunit/AllTests.php | 2 - test/phpunit/JsonLibTest.php | 119 ----------------------------------- 2 files changed, 121 deletions(-) delete mode 100755 test/phpunit/JsonLibTest.php diff --git a/test/phpunit/AllTests.php b/test/phpunit/AllTests.php index d8dda85f2fc..27e1b760f6e 100644 --- a/test/phpunit/AllTests.php +++ b/test/phpunit/AllTests.php @@ -75,8 +75,6 @@ class AllTests $suite->addTestSuite('MarginsLibTest'); require_once dirname(__FILE__).'/FilesLibTest.php'; $suite->addTestSuite('FilesLibTest'); - require_once dirname(__FILE__).'/JsonLibTest.php'; - $suite->addTestSuite('JsonLibTest'); require_once dirname(__FILE__).'/ImagesLibTest.php'; $suite->addTestSuite('ImagesLibTest'); require_once dirname(__FILE__).'/FunctionsLibTest.php'; diff --git a/test/phpunit/JsonLibTest.php b/test/phpunit/JsonLibTest.php deleted file mode 100755 index 67485c118e2..00000000000 --- a/test/phpunit/JsonLibTest.php +++ /dev/null @@ -1,119 +0,0 @@ - - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - * or see http://www.gnu.org/ - */ - -/** - * \file test/phpunit/JsonLibTest.php - * \ingroup test - * \brief PHPUnit test - * \remarks To run this script as CLI: phpunit filename.php - */ - -global $conf,$user,$langs,$db; -//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver -//require_once 'PHPUnit/Autoload.php'; -require_once dirname(__FILE__).'/../../htdocs/master.inc.php'; -require_once dirname(__FILE__).'/../../htdocs/core/lib/json.lib.php'; - -if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); -if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); -if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); -if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); -if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); -if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); -if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no menu to show -if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php -if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); -if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session) - - -/** - * Class for PHPUnit tests - * - * @backupGlobals disabled - * @backupStaticAttributes enabled - * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased. - */ -class JsonLibTest extends PHPUnit_Framework_TestCase -{ - protected $savconf; - protected $savuser; - protected $savlangs; - protected $savdb; - - /** - * Constructor - * We save global variables into local variables - * - * @return CoreTest - */ - function __construct() - { - //$this->sharedFixture - global $conf,$user,$langs,$db; - $this->savconf=$conf; - $this->savuser=$user; - $this->savlangs=$langs; - $this->savdb=$db; - - print __METHOD__." db->type=".$db->type." user->id=".$user->id; - //print " - db ".$db->db; - print "\n"; - } - - // Static methods - public static function setUpBeforeClass() - { - global $conf,$user,$langs,$db; - //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite. - - print __METHOD__."\n"; - } - public static function tearDownAfterClass() - { - global $conf,$user,$langs,$db; - //$db->rollback(); - - print __METHOD__."\n"; - } - - /** - * Init phpunit tests - * - * @return void - */ - protected function setUp() - { - global $conf,$user,$langs,$db; - $conf=$this->savconf; - $user=$this->savuser; - $langs=$this->savlangs; - $db=$this->savdb; - - print __METHOD__."\n"; - } - /** - * End phpunit tests - * - * @return void - */ - protected function tearDown() - { - print __METHOD__."\n"; - } - -} From a6275181ecebd5a144eebb280102974786900d91 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Sun, 20 Jul 2014 10:20:05 +0200 Subject: [PATCH 10/20] Corrected testIsValidUrl --- test/phpunit/Functions2LibTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/phpunit/Functions2LibTest.php b/test/phpunit/Functions2LibTest.php index 8ebd2dd3dd6..543c8a148ee 100755 --- a/test/phpunit/Functions2LibTest.php +++ b/test/phpunit/Functions2LibTest.php @@ -150,7 +150,7 @@ class Functions2LibTest extends PHPUnit_Framework_TestCase $result = isValidUrl('http://google.com'); $this->assertEquals($result, 1); - $result = isValidUrl('google'); + $result = isValidUrl('gooçgle'); $this->assertEquals($result, 0); //With scheme check From 9b3e65d80f375e4a881679470203d88fe66f376c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Tue, 22 Jul 2014 04:07:33 +0200 Subject: [PATCH 11/20] Removed PHP 5.3 comparison in dol_nl2br function --- htdocs/core/lib/functions.lib.php | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index c23c14aecee..6395bd77d34 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -3629,14 +3629,10 @@ function dol_string_nohtmltag($StringHtml,$removelinefeed=1,$pagecodeto='UTF-8') */ function dol_nl2br($stringtoencode,$nl2brmode=0,$forxml=false) { - if (! $nl2brmode) - { - if (version_compare(PHP_VERSION, '5.3.0') < 0) return nl2br($stringtoencode); - else return nl2br($stringtoencode,$forxml); - } - else - { - $ret=preg_replace('/(\r\n|\r|\n)/i',($forxml?'
':'
'),$stringtoencode); + if (!$nl2brmode) { + return nl2br($stringtoencode, $forxml); + } else { + $ret=preg_replace('/(\r\n|\r|\n)/i', ($forxml?'
':'
'), $stringtoencode); return $ret; } } From 76725c61ed33c6189c4eed1725727f2a091eae96 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Tue, 22 Jul 2014 04:10:42 +0200 Subject: [PATCH 12/20] Created test for dol_nl2br --- test/phpunit/FunctionsLibTest.php | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index 2178ad6fb5a..b72e470edec 100755 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -783,5 +783,27 @@ class FunctionsLibTest extends PHPUnit_Framework_TestCase $this->assertEquals('{"AA":"B\/B","CC":"","EE":"FF","HH":"GG;"}',json_encode($tmp)); } + /** + * dol_nl2br + * + * @return void + */ + public function testDolNl2Br() { + + //String to encode + $string = "a\na"; + + $this->assertEquals(dol_nl2br($string), "a
\na"); + + //With $forxml parameter + $this->assertEquals(dol_nl2br($string, 0, 1), "a
\na"); + + //Replacing \n by br + $this->assertEquals(dol_nl2br($string, 1), "a
a"); + + //With $forxml parameter + $this->assertEquals(dol_nl2br($string, 1, 1), "a
a"); + } + } ?> From 31adeb7477969913b59ba92eb9ee58e0290791be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Tue, 22 Jul 2014 04:12:45 +0200 Subject: [PATCH 13/20] Deprecated getStaticMember --- htdocs/core/lib/functions.lib.php | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 6395bd77d34..87ee457fa7d 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -43,26 +43,15 @@ include_once DOL_DOCUMENT_ROOT .'/core/lib/json.lib.php'; * @param string $class Class name * @param string $member Name of property * @return mixed Return value of static property + * @deprecated PHP 5.3 is now the minimum requirement, this is no longer necessary */ function getStaticMember($class, $member) { - if (is_object($class)) $class = get_class($class); - $classObj = new ReflectionClass($class); - $result = null; - - $found=0; - foreach($classObj->getStaticProperties() as $prop => $value) - { - if ($prop == $member) - { - $result = $value; - $found++; - break; - } + if (isset($class::$member)) { + return $class::$member; } - if (! $found) dol_print_error('','Try to get a static member "'.$member.'" in class "'.$class.'" that does not exists or is not static.'); - return $result; + dol_print_error('','Try to get a static member "'.$member.'" in class "'.$class.'" that does not exists or is not static.'); } From 097e8d33a2a23b07a30fa75e16003a19002e87bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Tue, 22 Jul 2014 04:16:32 +0200 Subject: [PATCH 14/20] Removed PHP 5.3 comparison in ImportCsv --- htdocs/core/modules/import/import_csv.modules.php | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/htdocs/core/modules/import/import_csv.modules.php b/htdocs/core/modules/import/import_csv.modules.php index cd3d82576ff..c8d77040a9d 100644 --- a/htdocs/core/modules/import/import_csv.modules.php +++ b/htdocs/core/modules/import/import_csv.modules.php @@ -256,15 +256,7 @@ class ImportCsv extends ModeleImports { global $conf; - $arrayres=array(); - if (version_compare(phpversion(), '5.3') < 0) - { - $arrayres=fgetcsv($this->handle,100000,$this->separator,$this->enclosure); - } - else - { - $arrayres=fgetcsv($this->handle,100000,$this->separator,$this->enclosure,$this->escape); - } + $arrayres=fgetcsv($this->handle,100000,$this->separator,$this->enclosure,$this->escape); // End of file if ($arrayres === false) return false; From 37c7d05a9e0eccdd701fc97dd634091c730328d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Tue, 22 Jul 2014 04:46:17 +0200 Subject: [PATCH 15/20] Added missing return null for getStaticMember --- htdocs/core/lib/functions.lib.php | 1 + 1 file changed, 1 insertion(+) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index 87ee457fa7d..a378cd2c01e 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -52,6 +52,7 @@ function getStaticMember($class, $member) } dol_print_error('','Try to get a static member "'.$member.'" in class "'.$class.'" that does not exists or is not static.'); + return null; } From 5baf48fc15c132b882e8be8d99b1fcc2a9c76aec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Sat, 26 Jul 2014 19:12:11 +0200 Subject: [PATCH 16/20] Revert "Removed empty test" This reverts commit cf1a2c44a359291288d7cd8f49b86ca4bb36d26a. --- test/phpunit/AllTests.php | 2 + test/phpunit/JsonLibTest.php | 119 +++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100755 test/phpunit/JsonLibTest.php diff --git a/test/phpunit/AllTests.php b/test/phpunit/AllTests.php index 27e1b760f6e..d8dda85f2fc 100644 --- a/test/phpunit/AllTests.php +++ b/test/phpunit/AllTests.php @@ -75,6 +75,8 @@ class AllTests $suite->addTestSuite('MarginsLibTest'); require_once dirname(__FILE__).'/FilesLibTest.php'; $suite->addTestSuite('FilesLibTest'); + require_once dirname(__FILE__).'/JsonLibTest.php'; + $suite->addTestSuite('JsonLibTest'); require_once dirname(__FILE__).'/ImagesLibTest.php'; $suite->addTestSuite('ImagesLibTest'); require_once dirname(__FILE__).'/FunctionsLibTest.php'; diff --git a/test/phpunit/JsonLibTest.php b/test/phpunit/JsonLibTest.php new file mode 100755 index 00000000000..67485c118e2 --- /dev/null +++ b/test/phpunit/JsonLibTest.php @@ -0,0 +1,119 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * or see http://www.gnu.org/ + */ + +/** + * \file test/phpunit/JsonLibTest.php + * \ingroup test + * \brief PHPUnit test + * \remarks To run this script as CLI: phpunit filename.php + */ + +global $conf,$user,$langs,$db; +//define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver +//require_once 'PHPUnit/Autoload.php'; +require_once dirname(__FILE__).'/../../htdocs/master.inc.php'; +require_once dirname(__FILE__).'/../../htdocs/core/lib/json.lib.php'; + +if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); +if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); +if (! defined('NOREQUIRESOC')) define('NOREQUIRESOC','1'); +if (! defined('NOREQUIRETRAN')) define('NOREQUIRETRAN','1'); +if (! defined('NOCSRFCHECK')) define('NOCSRFCHECK','1'); +if (! defined('NOTOKENRENEWAL')) define('NOTOKENRENEWAL','1'); +if (! defined('NOREQUIREMENU')) define('NOREQUIREMENU','1'); // If there is no menu to show +if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't need to load the html.form.class.php +if (! defined('NOREQUIREAJAX')) define('NOREQUIREAJAX','1'); +if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session) + + +/** + * Class for PHPUnit tests + * + * @backupGlobals disabled + * @backupStaticAttributes enabled + * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased. + */ +class JsonLibTest extends PHPUnit_Framework_TestCase +{ + protected $savconf; + protected $savuser; + protected $savlangs; + protected $savdb; + + /** + * Constructor + * We save global variables into local variables + * + * @return CoreTest + */ + function __construct() + { + //$this->sharedFixture + global $conf,$user,$langs,$db; + $this->savconf=$conf; + $this->savuser=$user; + $this->savlangs=$langs; + $this->savdb=$db; + + print __METHOD__." db->type=".$db->type." user->id=".$user->id; + //print " - db ".$db->db; + print "\n"; + } + + // Static methods + public static function setUpBeforeClass() + { + global $conf,$user,$langs,$db; + //$db->begin(); // This is to have all actions inside a transaction even if test launched without suite. + + print __METHOD__."\n"; + } + public static function tearDownAfterClass() + { + global $conf,$user,$langs,$db; + //$db->rollback(); + + print __METHOD__."\n"; + } + + /** + * Init phpunit tests + * + * @return void + */ + protected function setUp() + { + global $conf,$user,$langs,$db; + $conf=$this->savconf; + $user=$this->savuser; + $langs=$this->savlangs; + $db=$this->savdb; + + print __METHOD__."\n"; + } + /** + * End phpunit tests + * + * @return void + */ + protected function tearDown() + { + print __METHOD__."\n"; + } + +} From a779eea202616dcd9d0de07d865c259c96d59e1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Wed, 30 Jul 2014 15:17:57 +0200 Subject: [PATCH 17/20] Restored JSON reimplementation --- htdocs/core/lib/json.lib.php | 244 ++++++++++++++++++++++++++++++++++- 1 file changed, 240 insertions(+), 4 deletions(-) diff --git a/htdocs/core/lib/json.lib.php b/htdocs/core/lib/json.lib.php index dc87a04224a..69dbb4eba1a 100644 --- a/htdocs/core/lib/json.lib.php +++ b/htdocs/core/lib/json.lib.php @@ -23,16 +23,194 @@ * \ingroup core */ +if (! function_exists('json_encode')) +{ + /** + * Implement json_encode for PHP that does not support it + * + * @param mixed $elements PHP Object to json encode + * @return string Json encoded string + */ + function json_encode($elements) + { + return dol_json_encode($elements); + } +} + /** * Implement json_encode for PHP that does not support it * * @param mixed $elements PHP Object to json encode * @return string Json encoded string - * @deprecated Use json_encode native function */ function dol_json_encode($elements) { - return json_encode($elements); + $num=count($elements); + if (is_object($elements)) // Count number of properties for an object + { + $num=0; + foreach($elements as $key => $value) $num++; + } + //var_dump($num); + + // determine type + if (is_numeric(key($elements)) && key($elements) == 0) + { + // indexed (list) + $keysofelements=array_keys($elements); // Elements array mus have key that does not start with 0 and end with num-1, so we will use this later. + $output = '['; + for ($i = 0, $last = ($num - 1); $i < $num; $i++) + { + if (! isset($elements[$keysofelements[$i]])) continue; + if (is_array($elements[$keysofelements[$i]]) || is_object($elements[$keysofelements[$i]])) $output.= json_encode($elements[$keysofelements[$i]]); + else $output .= _val($elements[$keysofelements[$i]]); + if ($i !== $last) $output.= ','; + } + $output.= ']'; + } + else + { + // associative (object) + $output = '{'; + $last = $num - 1; + $i = 0; + $tmpelements=array(); + if (is_array($elements)) $tmpelements=$elements; + if (is_object($elements)) $tmpelements=get_object_vars($elements); + foreach($tmpelements as $key => $value) + { + $output .= '"'.$key.'":'; + if (is_array($value)) $output.= json_encode($value); + else $output .= _val($value); + if ($i !== $last) $output.= ','; + ++$i; + } + $output.= '}'; + } + + // return + return $output; +} + +/** + * Return text according to type + * + * @param mixed $val Value to show + * @return string Formated value + */ +function _val($val) +{ + if (is_string($val)) + { + // STRINGS ARE EXPECTED TO BE IN ASCII OR UTF-8 FORMAT + $ascii = ''; + $strlen_var = strlen($val); + + /* + * Iterate over every character in the string, + * escaping with a slash or encoding to UTF-8 where necessary + */ + for ($c = 0; $c < $strlen_var; ++$c) { + + $ord_var_c = ord($val{$c}); + + switch (true) { + case $ord_var_c == 0x08: + $ascii .= '\b'; + break; + case $ord_var_c == 0x09: + $ascii .= '\t'; + break; + case $ord_var_c == 0x0A: + $ascii .= '\n'; + break; + case $ord_var_c == 0x0C: + $ascii .= '\f'; + break; + case $ord_var_c == 0x0D: + $ascii .= '\r'; + break; + + case $ord_var_c == 0x22: + case $ord_var_c == 0x2F: + case $ord_var_c == 0x5C: + // double quote, slash, slosh + $ascii .= '\\'.$val{$c}; + break; + + case (($ord_var_c >= 0x20) && ($ord_var_c <= 0x7F)): + // characters U-00000000 - U-0000007F (same as ASCII) + $ascii .= $val{$c}; + break; + + case (($ord_var_c & 0xE0) == 0xC0): + // characters U-00000080 - U-000007FF, mask 110XXXXX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, ord($val{$c + 1})); + $c += 1; + $utf16 = utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + + case (($ord_var_c & 0xF0) == 0xE0): + // characters U-00000800 - U-0000FFFF, mask 1110XXXX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2})); + $c += 2; + $utf16 = utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + + case (($ord_var_c & 0xF8) == 0xF0): + // characters U-00010000 - U-001FFFFF, mask 11110XXX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}), ord($val{$c + 3})); + $c += 3; + $utf16 = utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + + case (($ord_var_c & 0xFC) == 0xF8): + // characters U-00200000 - U-03FFFFFF, mask 111110XX + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}), ord($val{$c + 3}), ord($val{$c + 4})); + $c += 4; + $utf16 = utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + + case (($ord_var_c & 0xFE) == 0xFC): + // characters U-04000000 - U-7FFFFFFF, mask 1111110X + // see http://www.cl.cam.ac.uk/~mgk25/unicode.html#utf-8 + $char = pack('C*', $ord_var_c, ord($val{$c + 1}), ord($val{$c + 2}), ord($val{$c + 3}), ord($val{$c + 4}), ord($val{$c + 5})); + $c += 5; + $utf16 = utf82utf16($char); + $ascii .= sprintf('\u%04s', bin2hex($utf16)); + break; + } + } + + return '"'.$ascii.'"'; + } + elseif (is_int($val)) return sprintf('%d', $val); + elseif (is_float($val)) return sprintf('%F', $val); + elseif (is_bool($val)) return ($val ? 'true' : 'false'); + else return 'null'; +} + +if (! function_exists('json_decode')) +{ + /** + * Implement json_decode for PHP that does not support it + * + * @param string $json Json encoded to PHP Object or Array + * @param bool $assoc False return an object, true return an array + * @return mixed Object or Array + */ + function json_decode($json, $assoc=false) + { + return dol_json_decode($json, $assoc); + } } /** @@ -41,11 +219,69 @@ function dol_json_encode($elements) * @param string $json Json encoded to PHP Object or Array * @param bool $assoc False return an object, true return an array. Try to always use it with true ! * @return mixed Object or Array - * @deprecated Use json_decode native function */ function dol_json_decode($json, $assoc=false) { - return json_decode($json, $assoc); + $comment = false; + + $out=''; + $strLength = strlen($json); // Must stay strlen and not dol_strlen because we want technical length, not visible length + for ($i=0; $i<$strLength; $i++) + { + if (! $comment) + { + if (($json[$i] == '{') || ($json[$i] == '[')) $out.= 'array('; + else if (($json[$i] == '}') || ($json[$i] == ']')) $out.= ')'; + else if ($json[$i] == ':') $out.= ' => '; + else $out.=$json[$i]; + } + else $out.= $json[$i]; + if ($json[$i] == '"' && $json[($i-1)]!="\\") $comment = !$comment; + } + + $out=_unval($out); + + // Return an array + if ($out != '') eval('$array = '.$out.';'); + else $array=array(); + + // Return an object + if (! $assoc) + { + if (! empty($array)) + { + $object = false; + + foreach ($array as $key => $value) + { + $object->{$key} = $value; + } + + return $object; + } + + return false; + } + + return $array; +} + +/** + * Return text according to type + * + * @param mixed $val Value to decode + * @return string Formated value + */ +function _unval($val) +{ + while (preg_match('/\\\u([0-9A-F]{2})([0-9A-F]{2})/i', $val, $reg)) + { + // single, escaped unicode character + $utf16 = chr(hexdec($reg[1])) . chr(hexdec($reg[2])); + $utf8 = utf162utf8($utf16); + $val=preg_replace('/\\\u'.$reg[1].$reg[2].'/i',$utf8,$val); + } + return $val; } /** From 42c7fc1c8d9b3c58b00b4b11f10b9a5e2d7a4fca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Wed, 30 Jul 2014 15:19:31 +0200 Subject: [PATCH 18/20] Revert "Testing dol_json_encode is not interesting now as it uses a core function" --- test/phpunit/JsonLibTest.php | 53 ++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/test/phpunit/JsonLibTest.php b/test/phpunit/JsonLibTest.php index 67485c118e2..caceb6d0dc3 100755 --- a/test/phpunit/JsonLibTest.php +++ b/test/phpunit/JsonLibTest.php @@ -116,4 +116,57 @@ class JsonLibTest extends PHPUnit_Framework_TestCase print __METHOD__."\n"; } + /** + * testJsonEncode + * + * @return void + */ + public function testJsonEncode() + { + //$this->sharedFixture + global $conf,$user,$langs,$db; + $this->savconf=$conf; + $this->savuser=$user; + $this->savlangs=$langs; + $this->savdb=$db; + + // Do a test with an array starting with 0 + $arraytotest=array(0=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "')); + $arrayencodedexpected='[{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}]'; + + $encoded=json_encode($arraytotest); + $this->assertEquals($arrayencodedexpected,$encoded); + $decoded=json_decode($encoded,true); + $this->assertEquals($arraytotest,$decoded,'test for json_xxx'); + + $encoded=dol_json_encode($arraytotest); + $this->assertEquals($arrayencodedexpected,$encoded); + $decoded=dol_json_decode($encoded,true); + $this->assertEquals($arraytotest,$decoded,'test for dol_json_xxx'); + + // Same test but array start with 2 instead of 0 + $arraytotest=array(2=>array('key'=>1,'value'=>'PRODREF','label'=>'Product ref with é and special chars \\ \' "')); + $arrayencodedexpected='{"2":{"key":1,"value":"PRODREF","label":"Product ref with \u00e9 and special chars \\\\ \' \""}}'; + + $encoded=json_encode($arraytotest); + $this->assertEquals($arrayencodedexpected,$encoded); + $decoded=json_decode($encoded,true); + $this->assertEquals($arraytotest,$decoded,'test for json_xxx'); + + $encoded=dol_json_encode($arraytotest); + $this->assertEquals($arrayencodedexpected,$encoded); + $decoded=dol_json_decode($encoded,true); + $this->assertEquals($arraytotest,$decoded,'test for dol_json_xxx'); + + // Test with object + $now=gmmktime(12,0,0,1,1,1970); + $objecttotest=new stdClass(); + $objecttotest->property1='abc'; + $objecttotest->property2=1234; + $objecttotest->property3=$now; + $encoded=dol_json_encode($objecttotest); + $this->assertEquals('{"property1":"abc","property2":1234,"property3":43200}',$encoded); + + } + } From 972d3c69b67ab24e80d06dc32602485556adbc3a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Wed, 30 Jul 2014 15:42:58 +0200 Subject: [PATCH 19/20] Removed duplicated requires. Json.lib.php is included with functions.lib.php --- htdocs/compta/ajaxpayment.php | 1 - htdocs/core/boxes/box_graph_invoices_permonth.php | 1 - htdocs/core/boxes/box_graph_invoices_supplier_permonth.php | 1 - htdocs/core/boxes/box_graph_orders_permonth.php | 1 - htdocs/core/boxes/box_graph_orders_supplier_permonth.php | 1 - htdocs/core/boxes/box_graph_product_distribution.php | 1 - htdocs/core/boxes/box_graph_propales_permonth.php | 1 - htdocs/main.inc.php | 1 - htdocs/product/stock/massstockmove.php | 1 - test/phpunit/JsonLibTest.php | 1 - 10 files changed, 10 deletions(-) diff --git a/htdocs/compta/ajaxpayment.php b/htdocs/compta/ajaxpayment.php index 66a1c997be3..4753687c9d9 100644 --- a/htdocs/compta/ajaxpayment.php +++ b/htdocs/compta/ajaxpayment.php @@ -32,7 +32,6 @@ if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't nee //if (! defined("NOLOGIN")) define("NOLOGIN",'1'); // If this page is public (can be called outside logged session) require '../main.inc.php'; -require_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php'; $langs->load('compta'); diff --git a/htdocs/core/boxes/box_graph_invoices_permonth.php b/htdocs/core/boxes/box_graph_invoices_permonth.php index bece2e55c6f..da94427aec3 100644 --- a/htdocs/core/boxes/box_graph_invoices_permonth.php +++ b/htdocs/core/boxes/box_graph_invoices_permonth.php @@ -104,7 +104,6 @@ class box_graph_invoices_permonth extends ModeleBoxes } else { - include_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php'; $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; diff --git a/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php b/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php index 4214a9a81b8..a14dba58674 100644 --- a/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php +++ b/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php @@ -103,7 +103,6 @@ class box_graph_invoices_supplier_permonth extends ModeleBoxes } else { - include_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php'; $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; diff --git a/htdocs/core/boxes/box_graph_orders_permonth.php b/htdocs/core/boxes/box_graph_orders_permonth.php index 7389c2c6b8b..d158923a1de 100644 --- a/htdocs/core/boxes/box_graph_orders_permonth.php +++ b/htdocs/core/boxes/box_graph_orders_permonth.php @@ -104,7 +104,6 @@ class box_graph_orders_permonth extends ModeleBoxes } else { - include_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php'; $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; diff --git a/htdocs/core/boxes/box_graph_orders_supplier_permonth.php b/htdocs/core/boxes/box_graph_orders_supplier_permonth.php index 788528b501f..6fbca8d3e9e 100644 --- a/htdocs/core/boxes/box_graph_orders_supplier_permonth.php +++ b/htdocs/core/boxes/box_graph_orders_supplier_permonth.php @@ -103,7 +103,6 @@ class box_graph_orders_supplier_permonth extends ModeleBoxes } else { - include_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php'; $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; diff --git a/htdocs/core/boxes/box_graph_product_distribution.php b/htdocs/core/boxes/box_graph_product_distribution.php index a05c4baf5ad..78f25ccd724 100644 --- a/htdocs/core/boxes/box_graph_product_distribution.php +++ b/htdocs/core/boxes/box_graph_product_distribution.php @@ -96,7 +96,6 @@ class box_graph_product_distribution extends ModeleBoxes } else { - include_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php'; $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $year=$tmparray['year']; $showinvoicenb=$tmparray['showinvoicenb']; diff --git a/htdocs/core/boxes/box_graph_propales_permonth.php b/htdocs/core/boxes/box_graph_propales_permonth.php index d80d40c64f2..fd4fb385bcc 100644 --- a/htdocs/core/boxes/box_graph_propales_permonth.php +++ b/htdocs/core/boxes/box_graph_propales_permonth.php @@ -104,7 +104,6 @@ class box_graph_propales_permonth extends ModeleBoxes } else { - include_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php'; $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; diff --git a/htdocs/main.inc.php b/htdocs/main.inc.php index f35d493a485..08f1fd47d47 100644 --- a/htdocs/main.inc.php +++ b/htdocs/main.inc.php @@ -166,7 +166,6 @@ require_once 'filefunc.inc.php'; // If there is a POST parameter to tell to save automatically some POST parameters into a cookies, we do it if (! empty($_POST["DOL_AUTOSET_COOKIE"])) { - require_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php'; $tmpautoset=explode(':',$_POST["DOL_AUTOSET_COOKIE"],2); $tmplist=explode(',',$tmpautoset[1]); $cookiearrayvalue=''; diff --git a/htdocs/product/stock/massstockmove.php b/htdocs/product/stock/massstockmove.php index afe9e20e28b..0458ddd6aff 100644 --- a/htdocs/product/stock/massstockmove.php +++ b/htdocs/product/stock/massstockmove.php @@ -25,7 +25,6 @@ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php'; require_once DOL_DOCUMENT_ROOT.'/product/stock/class/entrepot.class.php'; -require_once DOL_DOCUMENT_ROOT.'/core/lib/json.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.form.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formother.class.php'; require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.commande.class.php'; diff --git a/test/phpunit/JsonLibTest.php b/test/phpunit/JsonLibTest.php index caceb6d0dc3..1a7127c69b1 100755 --- a/test/phpunit/JsonLibTest.php +++ b/test/phpunit/JsonLibTest.php @@ -27,7 +27,6 @@ global $conf,$user,$langs,$db; //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver //require_once 'PHPUnit/Autoload.php'; require_once dirname(__FILE__).'/../../htdocs/master.inc.php'; -require_once dirname(__FILE__).'/../../htdocs/core/lib/json.lib.php'; if (! defined('NOREQUIREUSER')) define('NOREQUIREUSER','1'); if (! defined('NOREQUIREDB')) define('NOREQUIREDB','1'); From 1c97b3f7796d62641c38871664a4f231f3bfe863 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcos=20Garci=CC=81a=20de=20La=20Fuente?= Date: Wed, 30 Jul 2014 15:47:19 +0200 Subject: [PATCH 20/20] Replaced usage of dol_json_encode and dol_json_decode with json_encode and json_decode. If json module is not installed, it will refer to dol_json_encode/decode --- htdocs/compta/ajaxpayment.php | 2 +- htdocs/core/boxes/box_graph_invoices_permonth.php | 2 +- .../boxes/box_graph_invoices_supplier_permonth.php | 2 +- htdocs/core/boxes/box_graph_orders_permonth.php | 2 +- .../boxes/box_graph_orders_supplier_permonth.php | 2 +- htdocs/core/boxes/box_graph_product_distribution.php | 2 +- htdocs/core/boxes/box_graph_propales_permonth.php | 2 +- htdocs/core/class/stats.class.php | 12 ++++++------ htdocs/main.inc.php | 2 +- htdocs/product/stock/massstockmove.php | 6 +++--- 10 files changed, 17 insertions(+), 17 deletions(-) diff --git a/htdocs/compta/ajaxpayment.php b/htdocs/compta/ajaxpayment.php index 4753687c9d9..27a167bc576 100644 --- a/htdocs/compta/ajaxpayment.php +++ b/htdocs/compta/ajaxpayment.php @@ -114,4 +114,4 @@ $toJsonArray['result'] = price($result); // Return value to user format $toJsonArray['resultnum'] = price2num($result); // Return value to numeric format // Encode to JSON to return -echo dol_json_encode($toJsonArray); // Printing the call's result +echo json_encode($toJsonArray); // Printing the call's result diff --git a/htdocs/core/boxes/box_graph_invoices_permonth.php b/htdocs/core/boxes/box_graph_invoices_permonth.php index da94427aec3..0d24ed22dcc 100644 --- a/htdocs/core/boxes/box_graph_invoices_permonth.php +++ b/htdocs/core/boxes/box_graph_invoices_permonth.php @@ -104,7 +104,7 @@ class box_graph_invoices_permonth extends ModeleBoxes } else { - $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); + $tmparray=json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; $showtot=$tmparray['showtot']; diff --git a/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php b/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php index a14dba58674..a9da4d7bb16 100644 --- a/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php +++ b/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php @@ -103,7 +103,7 @@ class box_graph_invoices_supplier_permonth extends ModeleBoxes } else { - $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); + $tmparray=json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; $showtot=$tmparray['showtot']; diff --git a/htdocs/core/boxes/box_graph_orders_permonth.php b/htdocs/core/boxes/box_graph_orders_permonth.php index d158923a1de..86ddd77c108 100644 --- a/htdocs/core/boxes/box_graph_orders_permonth.php +++ b/htdocs/core/boxes/box_graph_orders_permonth.php @@ -104,7 +104,7 @@ class box_graph_orders_permonth extends ModeleBoxes } else { - $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); + $tmparray=json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; $showtot=$tmparray['showtot']; diff --git a/htdocs/core/boxes/box_graph_orders_supplier_permonth.php b/htdocs/core/boxes/box_graph_orders_supplier_permonth.php index 6fbca8d3e9e..2fb4f17cebb 100644 --- a/htdocs/core/boxes/box_graph_orders_supplier_permonth.php +++ b/htdocs/core/boxes/box_graph_orders_supplier_permonth.php @@ -103,7 +103,7 @@ class box_graph_orders_supplier_permonth extends ModeleBoxes } else { - $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); + $tmparray=json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; $showtot=$tmparray['showtot']; diff --git a/htdocs/core/boxes/box_graph_product_distribution.php b/htdocs/core/boxes/box_graph_product_distribution.php index 78f25ccd724..b186200daca 100644 --- a/htdocs/core/boxes/box_graph_product_distribution.php +++ b/htdocs/core/boxes/box_graph_product_distribution.php @@ -96,7 +96,7 @@ class box_graph_product_distribution extends ModeleBoxes } else { - $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); + $tmparray=json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $year=$tmparray['year']; $showinvoicenb=$tmparray['showinvoicenb']; $showpropalnb=$tmparray['showpropalnb']; diff --git a/htdocs/core/boxes/box_graph_propales_permonth.php b/htdocs/core/boxes/box_graph_propales_permonth.php index fd4fb385bcc..7d9eae4e878 100644 --- a/htdocs/core/boxes/box_graph_propales_permonth.php +++ b/htdocs/core/boxes/box_graph_propales_permonth.php @@ -104,7 +104,7 @@ class box_graph_propales_permonth extends ModeleBoxes } else { - $tmparray=dol_json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); + $tmparray=json_decode($_COOKIE['DOLUSERCOOKIE_box_'.$this->boxcode],true); $endyear=$tmparray['year']; $shownb=$tmparray['shownb']; $showtot=$tmparray['showtot']; diff --git a/htdocs/core/class/stats.class.php b/htdocs/core/class/stats.class.php index b2e85b3113a..8a4ffd29d68 100644 --- a/htdocs/core/class/stats.class.php +++ b/htdocs/core/class/stats.class.php @@ -81,7 +81,7 @@ abstract class Stats if ($foundintocache) // Cache file found and is not too old { dol_syslog(get_class($this).'::'.__FUNCTION__." read data from cache file ".$newpathofdestfile." ".$filedate."."); - $data = dol_json_decode(file_get_contents($newpathofdestfile), true); + $data = json_decode(file_get_contents($newpathofdestfile), true); } else { @@ -112,7 +112,7 @@ abstract class Stats dol_syslog(get_class($this).'::'.__FUNCTION__." save cache file ".$newpathofdestfile." onto disk."); if (! dol_is_dir($conf->user->dir_temp)) dol_mkdir($conf->user->dir_temp); $fp = fopen($newpathofdestfile, 'w'); - fwrite($fp, dol_json_encode($data)); + fwrite($fp, json_encode($data)); fclose($fp); if (! empty($conf->global->MAIN_UMASK)) $newmask=$conf->global->MAIN_UMASK; @chmod($newpathofdestfile, octdec($newmask)); @@ -172,7 +172,7 @@ abstract class Stats if ($foundintocache) // Cache file found and is not too old { dol_syslog(get_class($this).'::'.__FUNCTION__." read data from cache file ".$newpathofdestfile." ".$filedate."."); - $data = dol_json_decode(file_get_contents($newpathofdestfile), true); + $data = json_decode(file_get_contents($newpathofdestfile), true); } else { @@ -203,7 +203,7 @@ abstract class Stats dol_syslog(get_class($this).'::'.__FUNCTION__." save cache file ".$newpathofdestfile." onto disk."); if (! dol_is_dir($conf->user->dir_temp)) dol_mkdir($conf->user->dir_temp); $fp = fopen($newpathofdestfile, 'w'); - fwrite($fp, dol_json_encode($data)); + fwrite($fp, json_encode($data)); fclose($fp); if (! empty($conf->global->MAIN_UMASK)) $newmask=$conf->global->MAIN_UMASK; @chmod($newpathofdestfile, octdec($newmask)); @@ -295,7 +295,7 @@ abstract class Stats if ($foundintocache) // Cache file found and is not too old { dol_syslog(get_class($this).'::'.__FUNCTION__." read data from cache file ".$newpathofdestfile." ".$filedate."."); - $data = dol_json_decode(file_get_contents($newpathofdestfile), true); + $data = json_decode(file_get_contents($newpathofdestfile), true); } else { @@ -309,7 +309,7 @@ abstract class Stats dol_syslog(get_class($this).'::'.__FUNCTION__." save cache file ".$newpathofdestfile." onto disk."); if (! dol_is_dir($conf->user->dir_temp)) dol_mkdir($conf->user->dir_temp); $fp = fopen($newpathofdestfile, 'w'); - fwrite($fp, dol_json_encode($data)); + fwrite($fp, json_encode($data)); fclose($fp); if (! empty($conf->global->MAIN_UMASK)) $newmask=$conf->global->MAIN_UMASK; @chmod($newpathofdestfile, octdec($newmask)); diff --git a/htdocs/main.inc.php b/htdocs/main.inc.php index 08f1fd47d47..15ee098945f 100644 --- a/htdocs/main.inc.php +++ b/htdocs/main.inc.php @@ -176,7 +176,7 @@ if (! empty($_POST["DOL_AUTOSET_COOKIE"])) if (! empty($_POST[$postkey])) $cookiearrayvalue[$tmpkey]=$_POST[$postkey]; } $cookiename=$tmpautoset[0]; - $cookievalue=dol_json_encode($cookiearrayvalue); + $cookievalue=json_encode($cookiearrayvalue); //var_dump('setcookie cookiename='.$cookiename.' cookievalue='.$cookievalue); setcookie($cookiename, empty($cookievalue)?'':$cookievalue, empty($cookievalue)?0:(time()+(86400*354)), '/'); // keep cookie 1 year if (empty($cookievalue)) unset($_COOKIE[$cookiename]); diff --git a/htdocs/product/stock/massstockmove.php b/htdocs/product/stock/massstockmove.php index 0458ddd6aff..b8fcee3e618 100644 --- a/htdocs/product/stock/massstockmove.php +++ b/htdocs/product/stock/massstockmove.php @@ -64,7 +64,7 @@ $limit = $conf->liste_limit; $offset = $limit * $page ; $listofdata=array(); -if (! empty($_SESSION['massstockmove'])) $listofdata=dol_json_decode($_SESSION['massstockmove'],true); +if (! empty($_SESSION['massstockmove'])) $listofdata=json_decode($_SESSION['massstockmove'],true); /* @@ -105,7 +105,7 @@ if ($action == 'addline') if (count(array_keys($listofdata)) > 0) $id=max(array_keys($listofdata)) + 1; else $id=1; $listofdata[$id]=array('id'=>$id, 'id_product'=>$id_product, 'qty'=>$qty, 'id_sw'=>$id_sw, 'id_tw'=>$id_tw); - $_SESSION['massstockmove']=dol_json_encode($listofdata); + $_SESSION['massstockmove']=json_encode($listofdata); unset($id_product); //unset($id_sw); @@ -117,7 +117,7 @@ if ($action == 'addline') if ($action == 'delline' && $idline != '') { if (! empty($listofdata[$idline])) unset($listofdata[$idline]); - if (count($listofdata) > 0) $_SESSION['massstockmove']=dol_json_encode($listofdata); + if (count($listofdata) > 0) $_SESSION['massstockmove']=json_encode($listofdata); else unset($_SESSION['massstockmove']); }