PageRenderTime 56ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/gas2/function_engine/fun_data_check.php

https://github.com/mauromorello/ReteDES
PHP | 676 lines | 431 code | 139 blank | 106 comment | 114 complexity | 4920d0e3ea4c88091cf0a91f001d3429 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. function puliscistringa($stringa){
  3. $stringa = str_replace("à", "a", $stringa);
  4. $stringa = str_replace("è", "e", $stringa);
  5. $stringa = str_replace("é", "e", $stringa);
  6. $stringa = str_replace("ì", "i", $stringa);
  7. $stringa = str_replace("ù", "u", $stringa);
  8. $stringa = ereg_replace("[^A-Za-z0-9.,:;() ]", "", $stringa );
  9. return $stringa;
  10. }
  11. function urlcorretta($url) {
  12. return preg_match('|^[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
  13. }
  14. function checkmail($email){
  15. /**
  16. Validate an email address.
  17. Provide email address (raw input)
  18. Returns true if the email address has the email
  19. address format and the domain exists.
  20. */
  21. $isValid = true;
  22. $atIndex = strrpos($email, "@");
  23. if (is_bool($atIndex) && !$atIndex)
  24. {
  25. $isValid = false;
  26. }
  27. else
  28. {
  29. $domain = substr($email, $atIndex+1);
  30. $local = substr($email, 0, $atIndex);
  31. $localLen = strlen($local);
  32. $domainLen = strlen($domain);
  33. if ($localLen < 1 || $localLen > 64)
  34. {
  35. // local part length exceeded
  36. $isValid = false;
  37. }
  38. else if ($domainLen < 1 || $domainLen > 255)
  39. {
  40. // domain part length exceeded
  41. $isValid = false;
  42. }
  43. else if ($local[0] == '.' || $local[$localLen-1] == '.')
  44. {
  45. // local part starts or ends with '.'
  46. $isValid = false;
  47. }
  48. else if (preg_match('/\\.\\./', $local))
  49. {
  50. // local part has two consecutive dots
  51. $isValid = false;
  52. }
  53. else if (!preg_match('/^[A-Za-z0-9\\-\\.]+$/', $domain))
  54. {
  55. // character not valid in domain part
  56. $isValid = false;
  57. }
  58. else if (preg_match('/\\.\\./', $domain))
  59. {
  60. // domain part has two consecutive dots
  61. $isValid = false;
  62. }
  63. else if
  64. (!preg_match('/^(\\\\.|[A-Za-z0-9!#%&`_=\\/$\'*+?^{}|~.-])+$/', str_replace("\\\\","",$local)))
  65. {
  66. // character not valid in local part unless
  67. // local part is quoted
  68. if (!preg_match('/^"(\\\\"|[^"])+"$/',
  69. str_replace("\\\\","",$local)))
  70. {
  71. $isValid = false;
  72. }
  73. }
  74. if ($isValid && !(checkdnsrr($domain,"MX") || checkdnsrr($domain,"A")))
  75. {
  76. // domain not found in DNS
  77. $isValid = false;
  78. }
  79. }
  80. return $isValid;
  81. }
  82. function is_empty($var, $allow_false = false, $allow_ws = false) {
  83. if (!isset($var) || is_null($var) || ($allow_ws == false && trim($var) == "" && !is_bool($var)) || ($allow_false === false && is_bool($var) && $var === false) || (is_array($var) && empty($var))) {
  84. return true;
  85. } else {
  86. return false;
  87. }
  88. }
  89. function crc16($string) {
  90. $crc = 0xFFFF;
  91. for ($x = 0; $x < strlen ($string); $x++) {
  92. $crc = $crc ^ ord($string[$x]);
  93. for ($y = 0; $y < 8; $y++) {
  94. if (($crc & 0x0001) == 0x0001) {
  95. $crc = (($crc >> 1) ^ 0xA001);
  96. } else { $crc = $crc >> 1; }
  97. }
  98. }
  99. return $crc;
  100. }
  101. function controllodataora($data){
  102. //echo "check : $data<br>";
  103. if(!ereg("^[0-9]{2}/[0-9]{2}/[0-9]{4} [0-9]{2}:[0-9]{2}$", $data)){
  104. //echo "NON Passato EREG : $data<br>";
  105. return false;
  106. }else{
  107. //echo "Passato EREG : $data<br>";
  108. //$arrayData = explode("/", $data);
  109. $Giorno = substr($data, 0, 2);
  110. $Mese = substr($data, 3, 2);
  111. $Anno = substr($data, 6, 4);
  112. $Ora = substr($data, 11, 2);
  113. $Minuti = substr($data, 14, 2);
  114. //echo "Conversione: : G $Giorno, M $Mese, A $Anno, O $Ora, M $Minuti<br>";
  115. if(!checkdate($Mese, $Giorno, $Anno)){
  116. //echo "NON PASSATO CHKdate : $Giorno, $Mese, $Anno, $Ora, $Minuti<br>";
  117. return false;
  118. }else{
  119. //echo "PASSATO CHKdate : $Giorno, $Mese, $Anno, $Ora, $Minuti<br>";
  120. if ($Ora > -1 && $Ora < 24 && $Minuti > -1 && $Minuti < 60) {
  121. //echo "PASSATO CHKTIME : $Giorno, $Mese, $Anno, $Ora, $Minuti<br>";
  122. return true;
  123. }else{return false;}
  124. }
  125. }
  126. }
  127. function controllodata($data){
  128. if(!ereg("^[0-9]{2}/[0-9]{2}/[0-9]{4}$", $data)){
  129. return false;
  130. }else{
  131. //$arrayData = explode("/", $data);
  132. $Giorno = substr($data, 0, 2);
  133. $Mese = substr($data, 3, 2);
  134. $Anno = substr($data, 6, 4);
  135. //echo $Giorno."<br>";
  136. //echo $Mese."<br>";
  137. //echo $Anno."<br>";
  138. if(!checkdate($Mese, $Giorno, $Anno)){
  139. return false;
  140. }else{
  141. return true;
  142. }
  143. }
  144. }
  145. function conv_date_from_db ($data){
  146. //list ($y, $m, $d) = explode ("-", $data);
  147. //return "$d/$m/$y"; YYYY-MM-DD
  148. $y=substr($data, 0, 4);
  149. $m=substr($data, 5, 2);
  150. $d=substr($data, 8, 2);
  151. $h=substr($data, 11, 2);
  152. $min=substr($data, 14, 2);
  153. $sec=substr($data, 17, 2);
  154. if(empty($h)){$h="00";}
  155. if(empty($min)){$min="00";}
  156. if(empty($sec)){$sec="00";}
  157. return "$d/$m/$y $h:$min";//":$sec";
  158. }
  159. function conv_only_date_from_db ($data){
  160. //list ($y, $m, $d) = explode ("-", $data);
  161. //return "$d/$m/$y"; YYYY-MM-DD
  162. $y=substr($data, 0, 4);
  163. $m=substr($data, 5, 2);
  164. $d=substr($data, 8, 2);
  165. $h=substr($data, 11, 2);
  166. $min=substr($data, 14, 2);
  167. $sec=substr($data, 17, 2);
  168. if(empty($h)){$h="00";}
  169. if(empty($min)){$min="00";}
  170. if(empty($sec)){$sec="00";}
  171. return "$d/$m/$y";//":$sec";
  172. }
  173. function conv_datetime_from_db ($data){
  174. //list ($y, $m, $d) = explode ("-", $data);
  175. //return "$d/$m/$y"; YYYY-MM-DD
  176. $y=substr($data, 0, 4);
  177. $m=substr($data, 5, 2);
  178. $d=substr($data, 8, 2);
  179. $h=substr($data, 11, 2);
  180. $min=substr($data, 14, 2);
  181. $sec=substr($data, 17, 2);
  182. if(empty($h)){$h="00";}
  183. if(empty($min)){$min="00";}
  184. if(empty($sec)){$sec="00";}
  185. return "$d/$m/$y $h:$min";//":$sec";
  186. }
  187. function conv_date_to_db ($data){
  188. //list ($d, $m, $y) = explode ("/", $data);
  189. $d=substr($data, 0, 2);
  190. $m=substr($data, 3, 2);
  191. $y=substr($data, 6, 4);
  192. $h=substr($data, 11, 2);
  193. $min=substr($data, 14, 2);
  194. $sec=substr($data, 17, 2);
  195. if(empty($h)){$h="00";}
  196. if(empty($min)){$min="00";}
  197. if(empty($sec)){$sec="00";}
  198. return "$y-$m-$d $h:$min:$sec";
  199. }
  200. function conv_datetime_to_javascript($mydatetime){
  201. $m = CAST_TO_INT(substr($mydatetime, 5, 2));
  202. $m = $m - 1;
  203. $mytimestamp = strtotime($mydatetime);
  204. $dj = date("Y, m, d, H, i, s", $mytimestamp);
  205. if($m>9){ $dj2 = substr_replace($dj, ($m.",") , 6,3);}else
  206. { $dj2 = substr_replace($dj, ("0".$m.",") , 6,3);}
  207. //echo $dj.", $m ---> $dj2<br>";
  208. return $dj2;
  209. }
  210. function conv_date_to_javascript($mydatetime){
  211. $m = CAST_TO_INT(substr($mydatetime, 5, 2));
  212. $m = $m - 1;
  213. $mytimestamp = strtotime($mydatetime);
  214. $dj = date("Y, m, d", $mytimestamp);
  215. if($m>9){ $dj2 = substr_replace($dj, ($m) , 6,2);}else
  216. { $dj2 = substr_replace($dj, ("0".$m) , 6,2);}
  217. //echo $dj.", $m ---> $dj2<br>";
  218. return $dj2;
  219. }
  220. function gas_mktime($data){
  221. // 01 / 01 / 2010 15: 00: 00
  222. // 01 2 34 5 67891 123 456 78
  223. // 20 0 0- 1 0-31
  224. $d=substr($data, 0, 2);
  225. $m=substr($data, 3, 2);
  226. $y=substr($data, 6, 4);
  227. $h=substr($data, 11, 2);
  228. $min=substr($data, 14, 2);
  229. $sec=substr($data, 17, 2);
  230. if(empty($h)){$h="00";}
  231. if(empty($min)){$min="00";}
  232. if(empty($sec)){$sec="00";}
  233. return mktime($h, $min, $sec, $m, $d, $y);
  234. }
  235. function valuta_valida($valu){
  236. $valu=trim($valu);
  237. if (!is_empty($valu)){
  238. if (is_numeric($valu)){
  239. if ($valu<0){
  240. return false;
  241. break;
  242. }else{
  243. return true;
  244. break;
  245. }
  246. }
  247. return false;
  248. break;
  249. }
  250. return false;
  251. }
  252. function percentuale_valida($valu){
  253. $valu=trim($valu);
  254. if (!is_empty($valu)){
  255. if (is_numeric($valu)){
  256. if (($valu<0) | ($valu>100)){
  257. return false;
  258. break;
  259. }else{
  260. return true;
  261. break;
  262. }
  263. }
  264. return false;
  265. break;
  266. }
  267. return false;
  268. }
  269. function is_multiplo($val,$bigval){
  270. if(!is_numeric($val)){
  271. return false;
  272. break;
  273. }
  274. if(!is_numeric($bigval)){
  275. return false;
  276. break;
  277. }
  278. if($val==0){
  279. return false;
  280. break;
  281. }
  282. if($bigval==0){
  283. return false;
  284. break;
  285. }
  286. //$val=round($val,2);
  287. //$bigval=round($bigval,2);
  288. //echo "VAL ->".$val."<br>";
  289. //echo "BIGVAL ->".$bigval."<br>";
  290. while ($bigval > 0) {
  291. $bigval=number_format($bigval-$val,8);
  292. }
  293. if($bigval==0){
  294. //echo "VAL = MULTIPLO <br>";
  295. return true;
  296. break;
  297. }else{
  298. //echo "VAL = NOT MULTIPLO <br>";
  299. return false;
  300. break;
  301. }
  302. }
  303. function convertiDataTime($dataTime) {
  304. $data = date("d/m/Y", $dataTime);
  305. $ora = date("H:i", $dataTime);
  306. $ieri = date("d/m/Y", mktime(0, 0, 0, date("m"), date("d") - 1, date("Y")));
  307. $oggi = date("d/m/Y", mktime(0, 0, 0, date("m"), date("d"), date("Y")));
  308. if ($data == $ieri) $dataOk = "Ieri alle"; elseif ($data == $oggi) $dataOk = "Oggi alle"; else $dataOk = $data; return("$dataOk $ora"); }
  309. function euro($num){
  310. if(empty($num)){$num=0;};
  311. $num = number_format($num,2,",","");
  312. //$num = str_pad($num,7," ",STR_PAD_LEFT);
  313. $num .=" Eu.";
  314. $num = str_replace(" ","&nbsp;",$num);
  315. //$num = "Eu. ".$num;
  316. return $num;
  317. }
  318. function sanitize($data){
  319. // remove whitespaces (not a must though)
  320. $data = trim($data);
  321. // apply stripslashes if magic_quotes_gpc is enabled
  322. if(get_magic_quotes_gpc())
  323. {
  324. $data = stripslashes($data);
  325. }
  326. // a mySQL connection is required before using this function
  327. $data = mysql_real_escape_string($data);
  328. return $data;
  329. }
  330. function calcola_avanzo($bigval,$val){
  331. if(!is_numeric($val)){
  332. return false;
  333. break;
  334. }
  335. if(!is_numeric($bigval)){
  336. return false;
  337. break;
  338. }
  339. $bigval=round($bigval,4);
  340. $val=round($val,4);
  341. while (($bigval-$val) >= 0) {
  342. $bigval=round($bigval-$val,4);
  343. }
  344. //echo "VAL = NOT MULTIPLO <br>";
  345. return round($bigval,4);
  346. }
  347. function utente_attivo_partecipa_ordine($id_ordine){
  348. //USER PRESO INTERNAMENTE
  349. global $db,$RG_addr;
  350. //ORDINE ESISTENTE
  351. if(ordine_inesistente($id_ordine)){
  352. return "Ordine insesistente";
  353. }
  354. //UTENTE NON PUO' PARTECIPARE AGLI ORDINI
  355. if(!(_USER_PERMISSIONS & perm::puo_partecipare_ordini)){
  356. return "Utente non abilitato a partecipare agli ordini";
  357. }
  358. //CONTROLLO SE ESISTE REFERENTE
  359. if(id_referente_ordine_globale($id_ordine)<>id_referente_ordine_proprio_gas($id_ordine,_USER_ID_GAS)){
  360. //CONTROLLO SE L'ORDINE E' PARTECIPABILE DAl mio gas
  361. if(!_GAS_PUO_PART_ORD_EST){
  362. return "Il tuo gas non può partecipare ad ordini esterni";
  363. }
  364. //CONTROLLO SE ESISTE UN REFERENTE
  365. if(id_referente_ordine_proprio_gas($id_ordine,_USER_ID_GAS)==0){
  366. return "Per questo ordine il tuo GAS non ha un referente";
  367. }
  368. }
  369. //CONTROLLO SE E' SOLO PER CASSATI
  370. if(_GAS_USA_CASSA){
  371. if(ordini_field_value($id_ordine,"solo_cassati")=="SI"){
  372. if(!_USER_USA_CASSA){
  373. return "Per questo ordine è necessaria la cassa attiva";
  374. die();
  375. }
  376. }
  377. }
  378. //ORDINE APERTO CONTROLLO DATA E ORA
  379. if(ordine_partecipabile($id_ordine)){
  380. return "OK";
  381. die();
  382. }else{
  383. //ORDINE NON APERTO, CONTROLLO STATO
  384. if(stato_from_id_ord($id_ordine)==3){
  385. //SE NON E' GIA' CONVALIDATO
  386. if(!is_printable_from_id_ord($id_ordine)){
  387. //UTENTE GESTORE ORDINI
  388. if(_USER_PERMISSIONS & perm::puo_vedere_tutti_ordini){
  389. //MA SOLO DEL PROPRIO GAS
  390. if(_USER_ID_GAS<>id_gas_user(id_referente_ordine_globale($id_ordine))){
  391. return "Hai i superpoteri per gestire tutti gli ordini, ma solo se sono partiti dal tuo GAS.";
  392. die();
  393. }else{
  394. return "OK";
  395. die();
  396. }
  397. }else{
  398. if(id_referente_ordine_globale($id_ordine)==_USER_ID){
  399. //REFERENTE ORDINE
  400. return "OK";
  401. die();
  402. }else{
  403. //UTENTE NORMALE
  404. return "Ordine chiuso. Impossibile partecipare";
  405. die();
  406. }
  407. }
  408. }
  409. }else{
  410. return "Ordine ancora da aprire. Impossibile partecipare";
  411. die();
  412. }
  413. }
  414. }
  415. function utente_attivo_controllo_cassa($valore_nuovo,$id_ordine){
  416. global $db, $RG_addr;
  417. $is_ok = "ERRORE GENERICO";
  418. if(_USER_USA_CASSA){
  419. //UTENTE CHE USA LA CASSA,
  420. $vo = CAST_TO_FLOAT($valore_nuovo,0) - valore_totale_mio_ordine($id_ordine,_USER_ID);
  421. // Aggiungo il 10% al valore del mio ordine
  422. $vo = round((($vo/100)* _GAS_COPERTURA_CASSA ) + $vo);
  423. $vc = cassa_utente_tutti_movimenti(_USER_ID);
  424. //se il gas effettua il controllo di minimo
  425. if(_GAS_CASSA_CHECK_MIN_LEVEL){
  426. //se il credito non basta
  427. if(($vc-$vo)< _GAS_CASSA_MIN_LEVEL){
  428. //se non ha una prenotazione attiva
  429. if(read_option_prenotazione_ordine($id_ordine,_USER_ID)<>"SI"){
  430. //UTENTE CON LA CASSA
  431. //GAS CONTROLLA MIN LEVEL
  432. //CREDITO RIMANENTE INFERIORE A MIN LEVEL
  433. //NON HA UNA PRENOTAZIONE ATTIVA
  434. log_me($id_ordine,_USER_ID,"ORD","XXX","Ordine rifiutato",0,"UTENTE CON CASSA<br>
  435. GAS CONTROLLA MIN LEVEL<br>
  436. CREDITO RIMANENTE ($vc - $vo) INF A MIN LEVEL "._GAS_CASSA_MIN_LEVEL);
  437. //go("ordine_partecipa",_USER_ID,"Credito insufficiente per questo acquisto;<br>
  438. // Ricorda che è contemplato un 10% di spese accessorie che vanno a sommarsi all'importo dell'ordine.<br>
  439. // Vi è inoltre una soglia minima di "._GAS_CASSA_MIN_LEVEL." Eu. (decisa dal tuo GAS) sotto la quale non si può ordinare.<br>
  440. // I totali effettivi saranno modificati o confermati ad ordine chiuso dal gestore o dal cassiere.","?id_ordine=$id_ordine");
  441. $is_ok = "Credito insufficiente per questo acquisto;<br>
  442. Ricorda che è contemplato un 10% di spese accessorie che vanno a sommarsi all'importo dell'ordine.<br>
  443. Vi è inoltre una soglia minima di "._GAS_CASSA_MIN_LEVEL." Eu. (decisa dal tuo GAS) sotto la quale non si può ordinare.<br>
  444. I totali effettivi saranno modificati o confermati ad ordine chiuso dal gestore o dal cassiere.";
  445. }else{
  446. log_me($id_ordine,_USER_ID,"ORD","XXX","Ordine con prenotazione",0,"UTENTE CON CASSA<br>
  447. GAS CONTROLLA MIN LEVEL<br>
  448. UTENTE CON PRENOTAZIONE ATTIVA");
  449. $is_ok = "SI";
  450. }
  451. }else{
  452. //IL CREDITO BASTA
  453. $is_ok = "SI";
  454. }
  455. }else{
  456. //UTENTE CON LA CASSA
  457. //GAS NON CONTROLLA MIN LEVEL
  458. $is_ok = "SI";
  459. }
  460. }else{
  461. //L'utente non ha la cassa, controllo se è un ordine solo per cassati.
  462. if(_GAS_USA_CASSA){
  463. if(_USER_ID_GAS ==id_gas_user(id_referente_ordine_globale($id_ordine))){
  464. //SE l'utente che partecipa è del gas dell'ordine
  465. if(ordini_field_value($id_ordine,"solo_cassati"=="SI")){
  466. //UTENTE SENZA CASSA
  467. //GAS USA CASSA
  468. //ORDINE SOLO PER CASSATI
  469. log_me($id_ordine,_USER_ID,"ORD","XXX","Ordine rifiutato",0,"UTENTE SENZA CASSA<br>
  470. GAS USA CASSA<br>
  471. ORDINE SOLO PER CASSATI");
  472. //go("ordine_partecipa",_USER_ID,"Questo ordine è SOLO per gli utenti che usano la cassa.","?id_ordine=$id_ordine");
  473. $is_ok = "Questo ordine è SOLO per gli utenti che usano la cassa.";
  474. }else{
  475. //UTENTE SENZA CASSA
  476. //GAS USA CASSA
  477. //ORDINE PER TUTTI
  478. log_me($id_ordine,_USER_ID,"ORD","XXX","Ordine con prenotazione",0,"UTENTE SENZA CASSA<br>
  479. GAS USA CASSA<br>
  480. ORDINE PER TUTTI");
  481. $is_ok = "SI";
  482. }
  483. }else{
  484. //UTENTE SENZA CASSA
  485. //GAS USA CASSA
  486. //MA UTENTE ESTERNO
  487. $is_ok = "SI";
  488. }
  489. }else{
  490. //UTENTE SENZA CASSA
  491. //GAS SENZA CASSA
  492. $is_ok = "SI";
  493. }
  494. }
  495. return $is_ok;
  496. }
  497. function isValid($str) {
  498. return !preg_match('/[^A-Za-z0-9.#\\-$]/', $str);
  499. }
  500. function posso_gestire_ordine_full($id_ordine,$id_utente){
  501. global $db;
  502. $posso=false;
  503. //SE E' IL REFERENTE ORDINE
  504. if(id_referente_ordine_globale($id_ordine)==$id_utente){
  505. return true;
  506. }
  507. //SE PUO' GESTIRE TUTTI GLI ORDINI (MA DEL PROPRIO GAS)
  508. if(leggi_permessi_utente($id_utente)& perm::puo_vedere_tutti_ordini){
  509. if(id_gas_user(id_referente_ordine_globale($id_ordine))==id_gas_user($id_utente)){
  510. return true;
  511. }
  512. }
  513. //SE E' UN AIUTO EXTRA
  514. if(check_option_referente_extra($id_ordine,$id_utente)>0){
  515. return true;
  516. }
  517. return false;
  518. }
  519. function posso_gestire_ordine_gas($id_ordine,$id_utente){
  520. global $db;
  521. $posso=false;
  522. //SE E' IL REFERENTE ORDINE GAS
  523. if(id_referente_ordine_proprio_gas($id_ordine)==$id_utente){
  524. return true;
  525. }
  526. //SE PUO' GESTIRE TUTTI GLI ORDINI (MA DEL PROPRIO GAS)
  527. //if(leggi_permessi_utente($id_utente)& perm::puo_vedere_tutti_ordini){
  528. // if(id_gas_user(id_referente_ordine_globale($id_ordine))==id_gas_user($id_utente)){
  529. // return true;
  530. // }
  531. //}
  532. //SE E' UN AIUTO EXTRA
  533. if(check_option_referente_extra($id_ordine,$id_utente)>0){
  534. return true;
  535. }
  536. return false;
  537. }