CodingPhpTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  1. <?php
  2. /* Copyright (C) 2013 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. * or see https://www.gnu.org/
  17. */
  18. /**
  19. * \file test/phpunit/CodingPhpTest.php
  20. * \ingroup test
  21. * \brief PHPUnit test
  22. * \remarks To run this script as CLI: phpunit filename.php
  23. */
  24. global $conf,$user,$langs,$db;
  25. //define('TEST_DB_FORCE_TYPE','mysql'); // This is to force using mysql driver
  26. //require_once 'PHPUnit/Autoload.php';
  27. require_once dirname(__FILE__).'/../../htdocs/master.inc.php';
  28. require_once dirname(__FILE__).'/../../htdocs/core/lib/security.lib.php';
  29. require_once dirname(__FILE__).'/../../htdocs/core/lib/security2.lib.php';
  30. if (! defined('NOREQUIREUSER')) {
  31. define('NOREQUIREUSER', '1');
  32. }
  33. if (! defined('NOREQUIREDB')) {
  34. define('NOREQUIREDB', '1');
  35. }
  36. if (! defined('NOREQUIRESOC')) {
  37. define('NOREQUIRESOC', '1');
  38. }
  39. if (! defined('NOREQUIRETRAN')) {
  40. define('NOREQUIRETRAN', '1');
  41. }
  42. if (! defined('NOCSRFCHECK')) {
  43. define('NOCSRFCHECK', '1');
  44. }
  45. if (! defined('NOTOKENRENEWAL')) {
  46. define('NOTOKENRENEWAL', '1');
  47. }
  48. if (! defined('NOREQUIREMENU')) {
  49. define('NOREQUIREMENU', '1'); // If there is no menu to show
  50. }
  51. if (! defined('NOREQUIREHTML')) {
  52. define('NOREQUIREHTML', '1'); // If we don't need to load the html.form.class.php
  53. }
  54. if (! defined('NOREQUIREAJAX')) {
  55. define('NOREQUIREAJAX', '1');
  56. }
  57. if (! defined("NOLOGIN")) {
  58. define("NOLOGIN", '1'); // If this page is public (can be called outside logged session)
  59. }
  60. if (empty($user->id)) {
  61. print "Load permissions for admin user nb 1\n";
  62. $user->fetch(1);
  63. $user->getrights();
  64. }
  65. $conf->global->MAIN_DISABLE_ALL_MAILS=1;
  66. /**
  67. * Class for PHPUnit tests
  68. *
  69. * @backupGlobals disabled
  70. * @backupStaticAttributes enabled
  71. * @remarks backupGlobals must be disabled to have db,conf,user and lang not erased.
  72. */
  73. class CodingPhpTest extends PHPUnit\Framework\TestCase
  74. {
  75. protected $savconf;
  76. protected $savuser;
  77. protected $savlangs;
  78. protected $savdb;
  79. /**
  80. * Constructor
  81. * We save global variables into local variables
  82. *
  83. * @return SecurityTest
  84. */
  85. public function __construct()
  86. {
  87. parent::__construct();
  88. //$this->sharedFixture
  89. global $conf,$user,$langs,$db;
  90. $this->savconf=$conf;
  91. $this->savuser=$user;
  92. $this->savlangs=$langs;
  93. $this->savdb=$db;
  94. print __METHOD__." db->type=".$db->type." user->id=".$user->id;
  95. //print " - db ".$db->db;
  96. print "\n";
  97. }
  98. /**
  99. * setUpBeforeClass
  100. *
  101. * @return void
  102. */
  103. public static function setUpBeforeClass()
  104. {
  105. global $conf,$user,$langs,$db;
  106. $db->begin(); // This is to have all actions inside a transaction even if test launched without suite.
  107. print __METHOD__."\n";
  108. }
  109. /**
  110. * tearDownAfterClass
  111. *
  112. * @return void
  113. */
  114. public static function tearDownAfterClass()
  115. {
  116. global $conf,$user,$langs,$db;
  117. $db->rollback();
  118. print __METHOD__."\n";
  119. }
  120. /**
  121. * Init phpunit tests
  122. *
  123. * @return void
  124. */
  125. protected function setUp()
  126. {
  127. global $conf,$user,$langs,$db;
  128. $conf=$this->savconf;
  129. $user=$this->savuser;
  130. $langs=$this->savlangs;
  131. $db=$this->savdb;
  132. print __METHOD__."\n";
  133. }
  134. /**
  135. * End phpunit tests
  136. *
  137. * @return void
  138. */
  139. protected function tearDown()
  140. {
  141. print __METHOD__."\n";
  142. }
  143. /**
  144. * testSql
  145. *
  146. * @return string
  147. */
  148. public function testPHP()
  149. {
  150. global $conf,$user,$langs,$db;
  151. $conf=$this->savconf;
  152. $user=$this->savuser;
  153. $langs=$this->savlangs;
  154. $db=$this->savdb;
  155. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  156. $filesarray = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, '\.php', null, 'fullname', SORT_ASC, 0, 0, '', 1);
  157. //$filesarray = dol_dir_list(DOL_DOCUMENT_ROOT, 'files', 1, '\.php', null, 'fullname');
  158. foreach ($filesarray as $key => $file) {
  159. if (preg_match('/\/htdocs\/includes\//', $file['fullname'])) {
  160. continue;
  161. }
  162. if (preg_match('/\/htdocs\/custom\//', $file['fullname'])) {
  163. continue;
  164. }
  165. if (preg_match('/\/htdocs\/dolimed/', $file['fullname'])) {
  166. continue;
  167. }
  168. if (preg_match('/\/htdocs\/nltechno/', $file['fullname'])) {
  169. continue;
  170. }
  171. if (preg_match('/\/htdocs\/teclib/', $file['fullname'])) {
  172. continue;
  173. }
  174. print 'Check php file '.$file['fullname']."\n";
  175. $filecontent=file_get_contents($file['fullname']);
  176. if (preg_match('/\.class\.php/', $file['relativename'])
  177. || preg_match('/boxes\/box_/', $file['relativename'])
  178. || preg_match('/modules\/.*\/doc\/(doc|pdf)_/', $file['relativename'])
  179. || preg_match('/modules\/(import|mailings|printing)\//', $file['relativename'])
  180. || in_array($file['name'], array('modules_boxes.php', 'rapport.pdf.php', 'TraceableDB.php'))) {
  181. // Check into Class files
  182. if (! in_array($file['name'], array(
  183. 'api.class.php',
  184. 'commonobject.class.php',
  185. 'conf.class.php',
  186. 'html.form.class.php',
  187. 'translate.class.php',
  188. 'utils.class.php',
  189. 'TraceableDB.php',
  190. 'multicurrency.class.php',
  191. 'infobox.class.php'
  192. ))) {
  193. // Must not find $db->
  194. $ok=true;
  195. $matches=array();
  196. // Check string $db-> inside a class.php file (it should be $this->db-> into such classes)
  197. preg_match_all('/'.preg_quote('$db->', '/').'/', $filecontent, $matches, PREG_SET_ORDER);
  198. foreach ($matches as $key => $val) {
  199. $ok=false;
  200. break;
  201. }
  202. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  203. $this->assertTrue($ok, 'Found string $db-> into a .class.php file in '.$file['relativename'].'. Inside a .class file, you should use $this->db-> instead.');
  204. //exit;
  205. }
  206. } else {
  207. // Check into Include files
  208. if (! in_array($file['name'], array(
  209. 'objectline_view.tpl.php',
  210. 'extrafieldsinexport.inc.php',
  211. 'extrafieldsinimport.inc.php',
  212. 'DolQueryCollector.php'
  213. ))) {
  214. // Must not found $this->db->
  215. $ok=true;
  216. $matches=array();
  217. // Check string $this->db-> into a non class.php file (it shoud be $db-> into such classes)
  218. preg_match_all('/'.preg_quote('$this->db->', '/').'/', $filecontent, $matches, PREG_SET_ORDER);
  219. foreach ($matches as $key => $val) {
  220. $ok=false;
  221. break;
  222. }
  223. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  224. $this->assertTrue($ok, 'Found string "$this->db->" in '.$file['relativename']);
  225. //exit;
  226. }
  227. }
  228. // Check if a var_dump has been forgotten
  229. if (!preg_match('/test\/phpunit/', $file['fullname'])) {
  230. $ok=true;
  231. $matches=array();
  232. preg_match_all('/(.)\s*var_dump/', $filecontent, $matches, PREG_SET_ORDER);
  233. //var_dump($matches);
  234. foreach ($matches as $key => $val) {
  235. if ($val[1] != '/' && $val[1] != '*') {
  236. $ok=false;
  237. break;
  238. }
  239. break;
  240. }
  241. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  242. $this->assertTrue($ok, 'Found string var_dump that is not just after /* or // in '.$file['relativename']);
  243. //exit;
  244. }
  245. // Check get_class followed by __METHOD__
  246. $ok=true;
  247. $matches=array();
  248. preg_match_all('/'.preg_quote('get_class($this)."::".__METHOD__', '/').'/', $filecontent, $matches, PREG_SET_ORDER);
  249. foreach ($matches as $key => $val) {
  250. $ok=false;
  251. break;
  252. }
  253. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  254. $this->assertTrue($ok, 'Found string get_class($this)."::".__METHOD__ that must be replaced with __METHOD__ only in '.$file['relativename']);
  255. //exit;
  256. // Check string $this->db->idate without quotes
  257. $ok=true;
  258. $matches=array();
  259. preg_match_all('/(..)\s*\.\s*\$this->db->idate\(/', $filecontent, $matches, PREG_SET_ORDER);
  260. foreach ($matches as $key => $val) {
  261. if ($val[1] != '\'"' && $val[1] != '\'\'') {
  262. $ok=false;
  263. break;
  264. }
  265. //if ($reg[0] != 'db') $ok=false;
  266. }
  267. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  268. $this->assertTrue($ok, 'Found a $this->db->idate to forge a sql request without quotes around this date field '.$file['relativename']);
  269. //exit;
  270. // Check sql string DELETE|OR|AND|WHERE|INSERT ... yyy = ".$xxx
  271. // with xxx that is not 'thi' (for $this->db->sanitize) and 'db-' (for $db->sanitize). It means we forget a ' if string, or an (int) if int, when forging sql request.
  272. $ok=true;
  273. $matches=array();
  274. preg_match_all('/(DELETE|OR|AND|WHERE|INSERT)\s.*([^\s][^\s][^\s])\s*=\s*(\'|")\s*\.\s*\$(...)/', $filecontent, $matches, PREG_SET_ORDER);
  275. foreach ($matches as $key => $val) {
  276. if ($val[2] == 'ity' && $val[4] == 'con') { // exclude entity = ".$conf->entity
  277. continue;
  278. }
  279. if ($val[2] == 'ame' && $val[4] == 'db-' && preg_match('/WHERE name/', $val[0])) { // exclude name = ".$db->encrypt(
  280. continue;
  281. }
  282. if ($val[2] == 'ame' && $val[4] == 'thi' && preg_match('/WHERE name/', $val[0])) { // exclude name = ".$this->db->encrypt(
  283. continue;
  284. }
  285. var_dump($matches);
  286. $ok=false;
  287. break;
  288. }
  289. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  290. $this->assertTrue($ok, 'Found non quoted or not casted var into sql request '.$file['relativename'].' - Bad.');
  291. //exit;
  292. // Check that forged sql string is using ' instead of " as string PHP quotes
  293. $ok=true;
  294. $matches=array();
  295. preg_match_all('/\$sql \.= \'\s*VALUES.*\$/', $filecontent, $matches, PREG_SET_ORDER);
  296. foreach ($matches as $key => $val) {
  297. //if ($val[1] != '\'"' && $val[1] != '\'\'') {
  298. var_dump($matches);
  299. $ok=false;
  300. break;
  301. //}
  302. //if ($reg[0] != 'db') $ok=false;
  303. }
  304. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  305. $this->assertTrue($ok, 'Found a forged SQL string that mix on same line the use of \' for PHP string and PHP variables into file '.$file['relativename'].' Use " to forge PHP string like this: $sql = "SELET ".$myvar...');
  306. //exit;
  307. // Check that forged sql string is using ' instead of " as string PHP quotes
  308. $ok=true;
  309. $matches=array();
  310. preg_match_all('/\$sql \.?= \'SELECT.*\$/', $filecontent, $matches, PREG_SET_ORDER);
  311. foreach ($matches as $key => $val) {
  312. var_dump($matches);
  313. $ok=false;
  314. break;
  315. }
  316. $this->assertTrue($ok, 'Found a forged SQL string that mix on same line the use of \' for PHP string and PHP variables into file '.$file['relativename'].' Use " to forge PHP string like this: $sql = "SELECT ".$myvar...');
  317. // Check sql string VALUES ... , ".$xxx
  318. // with xxx that is not 'db-' (for $db->escape). It means we forget a ' if string, or an (int) if int, when forging sql request.
  319. $ok=true;
  320. $matches=array();
  321. preg_match_all('/(VALUES).*,\s*"\s*\.\s*\$(...)/', $filecontent, $matches, PREG_SET_ORDER);
  322. foreach ($matches as $key => $val) {
  323. if ($val[1] == 'VALUES' && $val[2] == 'db-') { // exclude $db->escape(
  324. continue;
  325. }
  326. if ($val[1] == 'VALUES' && $val[2] == 'thi' && preg_match('/this->db->encrypt/', $val[0])) { // exclude ".$this->db->encrypt(
  327. continue;
  328. }
  329. var_dump($matches);
  330. $ok=false;
  331. break;
  332. }
  333. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  334. $this->assertTrue($ok, 'Found non quoted or not casted var into sql request '.$file['relativename'].' - Bad.');
  335. //exit;
  336. // Check '".$xxx non escaped
  337. // Check string ='".$this->xxx with xxx that is not 'escape'. It means we forget a db->escape when forging sql request.
  338. $ok=true;
  339. $matches=array();
  340. preg_match_all('/=\s*\'"\s*\.\s*\$this->(....)/', $filecontent, $matches, PREG_SET_ORDER);
  341. foreach ($matches as $key => $val) {
  342. if ($val[1] != 'db->' && $val[1] != 'esca') {
  343. $ok=false;
  344. break;
  345. }
  346. }
  347. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  348. $this->assertTrue($ok, 'Found non escaped string in building of a sql request (case 1) in '.$file['relativename'].' - Bad.');
  349. // Check string sql|set|WHERE|...'".$yyy->xxx with xxx that is not 'escape', 'idate', .... It means we forget a db->escape when forging sql request.
  350. $ok=true;
  351. $matches=array();
  352. preg_match_all('/(sql|SET|WHERE|INSERT|VALUES|LIKE).+\s*\'"\s*\.\s*\$(.......)/', $filecontent, $matches, PREG_SET_ORDER);
  353. foreach ($matches as $key => $val) {
  354. if (! in_array($val[2], array('this->d', 'this->e', 'db->esc', 'dbs->es', 'dbs->id', 'mydb->e', 'dbsessi', 'db->ida', 'escaped', 'exclude', 'include'))) {
  355. $ok=false; // This will generate error
  356. break;
  357. }
  358. //if ($reg[0] != 'db') $ok=false;
  359. }
  360. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  361. $this->assertTrue($ok, 'Found non escaped string in building of a sql request (case 2) in '.$file['relativename'].': '.$val[0].' - Bad.');
  362. //exit;
  363. // Check string sql|set...'.$yyy->xxx with xxx that is not 'escape', 'idate', .... It means we forget a db->escape when forging sql request.
  364. $ok=true;
  365. $matches=array();
  366. preg_match_all('/(\$sql|SET\s|WHERE\s|INSERT\s|VALUES\s|VALUES\().+\s*\'\s*\.\s*\$(.........)/', $filecontent, $matches, PREG_SET_ORDER);
  367. foreach ($matches as $key => $val) {
  368. if (! in_array($val[2], array('this->db-', 'db->prefi', 'db->sanit', 'conf->ent', 'key : \'\')', 'key])."\')', 'excludefi', 'regexstri', ''))) {
  369. $ok=false;
  370. var_dump($matches);
  371. break;
  372. }
  373. //if ($reg[0] != 'db') $ok=false;
  374. }
  375. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  376. $this->assertTrue($ok, 'Found non escaped string in building of a sql request (case 3) in '.$file['relativename'].': '.$val[0].' - Bad.');
  377. //exit;
  378. // Checks with IN
  379. // Check string ' IN (".xxx' or ' IN (\'.xxx' with xxx that is not '$this->db->sanitize' and not '$db->sanitize'. It means we forget a db->sanitize when forging sql request.
  380. $ok=true;
  381. $matches=array();
  382. preg_match_all('/\s+IN\s*\([\'"]\s*\.\s*(.........)/i', $filecontent, $matches, PREG_SET_ORDER);
  383. foreach ($matches as $key => $val) {
  384. //var_dump($val);
  385. if (!in_array($val[1], array('$db->sani', '$this->db', 'getEntity', 'WON\',\'L', 'self::STA', 'Commande:', 'CommandeF', 'Entrepot:', 'Facture::', 'FactureFo', 'ExpenseRe', 'Societe::', 'Ticket::S'))) {
  386. $ok=false;
  387. break;
  388. }
  389. //if ($reg[0] != 'db') $ok=false;
  390. }
  391. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  392. $this->assertTrue($ok, 'Found non sanitized string in building of a IN or NOT IN sql request '.$file['relativename'].' - Bad.');
  393. //exit;
  394. // Check string ' IN (\'".xxx' with xxx that is not '$this->db->sanitize' and not '$db->sanitize'. It means we forget a db->sanitize when forging sql request.
  395. $ok=true;
  396. $matches=array();
  397. preg_match_all('/\s+IN\s*\(\'"\s*\.\s*(.........)/i', $filecontent, $matches, PREG_SET_ORDER);
  398. foreach ($matches as $key => $val) {
  399. //var_dump($val);
  400. if (!in_array($val[1], array('$db->sani', '$this->db', 'getEntity', 'WON\',\'L', 'self::STA', 'Commande:', 'CommandeF', 'Entrepot:', 'Facture::', 'FactureFo', 'ExpenseRe', 'Societe::', 'Ticket::S'))) {
  401. $ok=false;
  402. break;
  403. }
  404. //if ($reg[0] != 'db') $ok=false;
  405. }
  406. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  407. $this->assertTrue($ok, 'Found non sanitized string in building of a IN or NOT IN sql request '.$file['relativename'].' - Bad.');
  408. //exit;
  409. // Test that output of $_SERVER\[\'QUERY_STRING\'\] is escaped.
  410. $ok=true;
  411. $matches=array();
  412. preg_match_all('/(..............)\$_SERVER\[\'QUERY_STRING\'\]/', $filecontent, $matches, PREG_SET_ORDER);
  413. foreach ($matches as $key => $val) {
  414. if ($val[1] != 'scape_htmltag(' && $val[1] != 'ing_nohtmltag(' && $val[1] != 'dol_escape_js(') {
  415. $ok=false;
  416. break;
  417. }
  418. }
  419. $this->assertTrue($ok, 'Found a $_SERVER[\'QUERY_STRING\'] without dol_escape_htmltag neither dol_string_nohtmltag around it, in file '.$file['relativename'].'. Bad.');
  420. // Check GETPOST(... 'none');
  421. $ok=true;
  422. $matches=array();
  423. preg_match_all('/GETPOST\s*\(([^\)]+),\s*["\']none["\']/i', $filecontent, $matches, PREG_SET_ORDER);
  424. foreach ($matches as $key => $val) {
  425. //var_dump($val);
  426. if (!in_array($val[1], array(
  427. "'replacestring'", "'htmlheader'", "'WEBSITE_HTML_HEADER'", "'WEBSITE_CSS_INLINE'", "'WEBSITE_JS_INLINE'", "'WEBSITE_MANIFEST_JSON'", "'PAGE_CONTENT'", "'WEBSITE_README'",
  428. "'search_status'", '"mysqldump"', '"postgresqldump"', "'db_pass_root'", "'db_pass'", '"pass"', '"pass1"', '"pass2"', '"password"', "'password'", '"MAIN_MAIL_SMTPS_PW"'))) {
  429. $ok=false;
  430. break;
  431. }
  432. //if ($reg[0] != 'db') $ok=false;
  433. }
  434. //print __METHOD__." Result for checking we don't have non escaped string in sql requests for file ".$file."\n";
  435. $this->assertTrue($ok, 'Found a GETPOST that use \'none\' as a parameter in file '.$file['relativename'].' and param is not an allowed parameter for using none - Bad.');
  436. //exit;
  437. // Test that first param of print_liste_field_titre is a translation key and not the translated value
  438. $ok=true;
  439. $matches=array();
  440. // Check string ='print_liste_field_titre\(\$langs'.
  441. preg_match_all('/print_liste_field_titre\(\$langs/', $filecontent, $matches, PREG_SET_ORDER);
  442. foreach ($matches as $key => $val) {
  443. $ok=false;
  444. break;
  445. }
  446. $this->assertTrue($ok, 'Found a use of print_liste_field_titre with first parameter that is a translated value instead of just the translation key in file '.$file['relativename'].'. Bad.');
  447. // Test we don't have <br />
  448. $ok=true;
  449. $matches=array();
  450. preg_match_all('/<br\s+\/>/', $filecontent, $matches, PREG_SET_ORDER);
  451. foreach ($matches as $key => $val) {
  452. if ($file['name'] != 'functions.lib.php') {
  453. $ok=false;
  454. break;
  455. }
  456. }
  457. $this->assertTrue($ok, 'Found a tag <br /> that is for xml in file '.$file['relativename'].'. You must use html syntax <br> instead.');
  458. // Test we don't have name="token" value="'.$_SESSION['newtoken'], we must use name="token" value="'.newToken() instead.
  459. $ok=true;
  460. $matches=array();
  461. preg_match_all('/name="token" value="\'\s*\.\s*\$_SESSION/', $filecontent, $matches, PREG_SET_ORDER);
  462. foreach ($matches as $key => $val) {
  463. if ($file['name'] != 'excludefile.php') {
  464. $ok=false;
  465. break;
  466. }
  467. }
  468. $this->assertTrue($ok, 'Found a forbidden string sequence into '.$file['relativename'].' : name="token" value="\'.$_SESSION[..., you must use a newToken() instead of $_SESSION[\'newtoken\'].');
  469. // Test we don't have @var array(
  470. $ok=true;
  471. $matches=array();
  472. preg_match_all('/preg_grep\(.*\$/', $filecontent, $matches, PREG_SET_ORDER);
  473. foreach ($matches as $key => $val) {
  474. if (strpos($val[0], 'preg_quote') === false) {
  475. $ok=false;
  476. break;
  477. }
  478. }
  479. $this->assertTrue($ok, 'Found a preg_grep with a param that is a $var but without preg_quote in file '.$file['relativename'].'.');
  480. // Test we don't have @var array(
  481. $ok=true;
  482. $matches=array();
  483. preg_match_all('/@var\s+array\(/', $filecontent, $matches, PREG_SET_ORDER);
  484. foreach ($matches as $key => $val) {
  485. $ok=false;
  486. break;
  487. }
  488. $this->assertTrue($ok, 'Found a declaration @var array() instead of @var array in file '.$file['relativename'].'.');
  489. // Test we don't have CURDATE()
  490. $ok=true;
  491. $matches=array();
  492. preg_match_all('/CURDATE\(\)/', $filecontent, $matches, PREG_SET_ORDER);
  493. foreach ($matches as $key => $val) {
  494. $ok=false;
  495. break;
  496. }
  497. $this->assertTrue($ok, 'Found a CURDATE\(\) into code. Do not use this SQL method in file '.$file['relativename'].'. You must use the PHP function dol_now() instead.');
  498. }
  499. return;
  500. }
  501. }