diff --git a/htdocs/compta/ajaxpayment.php b/htdocs/compta/ajaxpayment.php index 66a1c997be3..27a167bc576 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'); @@ -115,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 a7006d1f4e1..8c99f2c1381 100644 --- a/htdocs/core/boxes/box_graph_invoices_permonth.php +++ b/htdocs/core/boxes/box_graph_invoices_permonth.php @@ -104,8 +104,7 @@ 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); + $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 f306b26eb28..a566abf3865 100644 --- a/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php +++ b/htdocs/core/boxes/box_graph_invoices_supplier_permonth.php @@ -103,8 +103,7 @@ 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); + $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 e701a4b9384..c0b338505c5 100644 --- a/htdocs/core/boxes/box_graph_orders_permonth.php +++ b/htdocs/core/boxes/box_graph_orders_permonth.php @@ -104,8 +104,7 @@ 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); + $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 56d822876a0..cc2e2eaa2a7 100644 --- a/htdocs/core/boxes/box_graph_orders_supplier_permonth.php +++ b/htdocs/core/boxes/box_graph_orders_supplier_permonth.php @@ -103,8 +103,7 @@ 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); + $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 bca47dd9460..74d106b7f69 100644 --- a/htdocs/core/boxes/box_graph_product_distribution.php +++ b/htdocs/core/boxes/box_graph_product_distribution.php @@ -96,8 +96,7 @@ 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); + $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 8efa4be54a8..93868620470 100644 --- a/htdocs/core/boxes/box_graph_propales_permonth.php +++ b/htdocs/core/boxes/box_graph_propales_permonth.php @@ -104,8 +104,7 @@ 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); + $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/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index f658d2550dc..faac941cb81 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 @@ -46,26 +43,16 @@ if (! function_exists('json_encode')) * @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.'); + return null; } @@ -1495,19 +1482,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; } /** @@ -3654,24 +3633,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); - } - 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; } } @@ -3767,12 +3741,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); } @@ -3803,7 +3776,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) @@ -3858,12 +3831,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(true); } /** 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; diff --git a/htdocs/main.inc.php b/htdocs/main.inc.php index ed0adef5dcc..314ee3bab9b 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=''; @@ -177,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 afe9e20e28b..b8fcee3e618 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'; @@ -65,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); /* @@ -106,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); @@ -118,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']); } diff --git a/test/phpunit/Functions2LibTest.php b/test/phpunit/Functions2LibTest.php index b5d98aea1b8..743bfce1801 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('gooçgle'); + $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); } /** diff --git a/test/phpunit/FunctionsLibTest.php b/test/phpunit/FunctionsLibTest.php index 2b81f46e978..75877895e52 100755 --- a/test/phpunit/FunctionsLibTest.php +++ b/test/phpunit/FunctionsLibTest.php @@ -787,4 +787,26 @@ 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"); + } + } 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');