noalyss Version-9
ac_common.php
Go to the documentation of this file.
1<?php
2
3/*
4 * This file is part of NOALYSS.
5 *
6 * NOALYSS is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * NOALYSS is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with NOALYSS; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21// Copyright Author Dany De Bontridder danydb@aevalys.eu
22
23/**
24 * @file
25 * @brief common utilities for a lot of procedure, classe
26 */
27
28require_once NOALYSS_INCLUDE.'/lib/function_javascript.php';
29
30/**
31 * \brief to protect again bad characters which can lead to a cross scripting attack
32 the string to be diplayed must be protected. Side effects with htmlentities, especially for
33 * the date (transform dot in &periode;) and number
34 */
35function h($p_string)
36{
37 return ( $p_string === null)?"":htmlspecialchars($p_string,ENT_QUOTES|ENT_HTML5,'UTF-8',true);
38}
39function p($p_string)
40{
41 return '<p>'.$p_string."</p>";
42}
43function span($p_string, $p_extra='')
44{
45 return '<span ' . $p_extra . '>' . $p_string . '</span>';
46}
47
48function hi($p_string)
49{
50 return '<i>' . h($p_string) . '</i>';
51}
52
53function hb($p_string)
54{
55 return '<b>' . h($p_string) . '</b>';
56}
57
58function th($p_string, $p_extra='',$raw='')
59{
60 return '<th ' . $p_extra . '>' . h($p_string).$raw . '</th>';
61}
62
63function h2info($p_string)
64{
65 return '<h2 class="info">' . h($p_string) . '</h2>';
66}
67
68function h2($p_string, $p_class="",$raw="")
69{
70 return '<h2 ' . $p_class . '>' . $raw.h($p_string) . '</h2>';
71}
72function h1($p_string, $p_class="")
73{
74 return '<h1 ' . $p_class . '>' . h($p_string) . '</h1>';
75}
76/**
77 * \brief surround the string with td
78 * \param $p_string string to surround by TD
79 * \param $p_extra extra info (class, style, javascript...)
80 * \return string surrounded by td
81 */
82
83function td($p_string='', $p_extra='')
84{
85 return '<td ' . $p_extra . '>' . $p_string . '</td>';
86}
87
88function tr($p_string, $p_extra='')
89{
90 return '<tr ' . $p_extra . '>' . $p_string . '</tr>';
91}
92
93/**
94 * @brief escape correctly php string to javascript
95 */
96function j($p_string)
97{
98 $a = preg_replace("/\r?\n/", "\\n", addslashes($p_string));
99 $a = noalyss_str_replace("'", '\'', $a);
100 return $a;
101}
102
103/**
104 * format the number for the CSV export
105 * @param $p_number number
106 */
107function nb($p_number)
108{
109 $r=trim($p_number);
110 $r = sprintf('%.4f', $p_number);
111 $r = noalyss_str_replace('.', ',', $r);
112
113 return $r;
114}
115
116/**
117 * return D if the number is smaller than 0 , C if bigger and an empty string if
118 * equal to 0. Used for displaying saldo D / C (debit / credit )
119 * @param float $p_number
120 */
121function findSide($p_number)
122{
123 $return ='';
124 if ( $p_number > 0 ) {
125 $return ='D';
126 }else {
127 $return =($p_number== 0)?"":"C";
128 }
129 return $return;
130}
131
132/**
133 * format the number with a sep. for the thousand
134 * @param $p_number number
135 * @param $p_dec number of decimal to display
136 */
137function nbm($p_number,$p_dec = 2)
138{
139
140 if (noalyss_trim($p_number) == '')
141 return '';
142 if ($p_number == 0)
143 return "0,00";
144
145 $a = doubleval($p_number);
146 $r = number_format($a, $p_dec, ",", ".");
147 if (trim($r) == '')
148 {
149 var_dump($r);
150 var_dump($p_number);
151 var_dump($a);
152 exit();
153 }
154
155 return $r;
156}
157
158/**
159 * \brief log error into the /tmp/noalyss_error.log it doesn't work on windows
160 *
161 * \param p_log message
162 * \param p_line line number
163 * \param p_message is the message
164 *
165 * \return nothing
166 *
167 */
168
169function echo_error($p_log, $p_line="", $p_message="")
170{
171 $msg="ERREUR :" . $p_log . " " . $p_line . " " . $p_message;
172 echo $msg;
173 syslog(LOG_ERR,$msg);
174
175}
176
177/**
178 * \brief Compare 2 dates
179 * \param p_date
180 * \param p_date_oth
181 *
182 * \return
183 * - == 0 les dates sont identiques
184 * - > 0 date1 > date2
185 * - < 0 date1 < date2
186 */
187
188function cmpDate($p_date, $p_date_oth)
189{
190 date_default_timezone_set('Europe/Brussels');
191
192 $l_date = isDate($p_date);
193 $l2_date = isDate($p_date_oth);
194 if ($l_date == null || $l2_date == null)
195 {
196 throw new Exception("erreur date [$p_date] [$p_date_oth]");
197 }
198 $l_adate = explode(".", $l_date);
199 $l2_adate = explode(".", $l2_date);
200 $l_mkdate = mktime(0, 0, 0, $l_adate[1], $l_adate[0], $l_adate[2]);
201 $l2_mkdate = mktime(0, 0, 0, $l2_adate[1], $l2_adate[0], $l2_adate[2]);
202 // si $p_date > $p_date_oth return > 0
203 return $l_mkdate - $l2_mkdate;
204}
205
206/***!
207 * @brief check if the argument is a number
208 *
209 * @param $p_int number to test
210 *
211 * @return
212 * - 1 it's a number
213 * - 0 it is not
214 */
215function isNumber($p_int)
216{
217 if (strlen(noalyss_trim($p_int)) == 0)
218 return 0;
219 if (is_numeric($p_int) === true)
220 return 1;
221 else
222 return 0;
223}
224
225/***
226 * \brief Verifie qu'une date est bien formaté
227 * en d.m.y et est valable
228 * \param $p_date
229 *
230 * \return
231 * - null si la date est invalide ou malformaté
232 * - $p_date si tout est bon
233 *
234 */
235
237{
238 if (noalyss_strlentrim($p_date) == 0)
239 return null;
240 if (preg_match("/^[0-9]{1,2}\.[0-9]{1,2}\.[0-9]{4}$/", $p_date) == 0)
241 {
242
243 return null;
244 }
245 else
246 {
247 $l_date = explode(".", $p_date);
248
249 if (sizeof($l_date) != 3)
250 return null;
251
252 if ($l_date[2] > COMPTA_MAX_YEAR || $l_date[2] < COMPTA_MIN_YEAR)
253 {
254 return null;
255 }
256
257 if (checkdate($l_date[1], $l_date[0], $l_date[2]) == false)
258 {
259 return null;
260 }
261 }
262 return $p_date;
263}
264
265/**
266 * \brief Default page header for each page
267 *
268 * \param p_theme default theme
269 * \param $p_script
270 * \param $p_script2 another js script
271 * Must be called only once
272 * \return none
273 */
274
275function html_page_start($p_theme="", $p_script="", $p_script2="")
276{
277 // check not called twiced
278 static $already_call=0;
279 if ( $already_call==1)return;
280 $already_call=1;
281
282 $cn = new Database();
283 if ($p_theme != "")
284 {
285 $Res = $cn->exec_sql("select the_filestyle from theme
286 where the_name=$1" ,[$p_theme]);
287 if (Database::num_row($Res) == 0)
288 {
289 $style = "style-classic7.css";
290 }
291 else
292 {
294 $style = $s['the_filestyle'];
295 }
296 }
297 else
298 {
299 $style = "style-classic7.css";
300 } // end if
301 $title="NOALYSS";
302
303 if ( isset ($_REQUEST['ac'])) {
304 $ac=strip_tags($_REQUEST['ac']);
305 if (strpos($ac,'/') <> 0)
306 {
307 $m= explode('/',$ac);
308 $title=$m[count($m)-1]." ".$title;
309 }
310 else
311 $title=$ac." ".$title;
312 }
313 $is_msie=is_msie();
314
315 if ($is_msie == 0 )
316 {
317 echo '<!doctype html>';
318 printf("\n");
319
320 }
321 else {
322 echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 FINAL//EN" >';
323 printf("\n");
324 }
325 echo '<HTML>';
326
327 if ($p_script2!="")
328 {
329 $p_script2='<script src="'.$p_script2.'" type="text/javascript"></script>';
330 }
331 $style=trim($style);
332 echo "<HEAD>";
333 echo '<meta charset="utf-8">';
334 echo "<META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">";
335 if ($is_msie==1)
336 {
337 echo ' <meta http-equiv="x-ua-compatible" content="IE=edge"/>';
338 }
339 global $version_noalyss;
340 echo <<<EOF
341 <TITLE>$title</TITLE>
342 <link rel="icon" type="image/ico" href="favicon.ico" />
343 <meta name="viewport" content="width=device-width, initial-scale=1.0">
344 <LINK id="bootstrap" REL="stylesheet" type="text/css" href="css/bootstrap.min.css" media="screen"/>
345 <LINK id="fontello" REL="stylesheet" type="text/css" href="css/font/fontello/css/fontello.css" media="screen"/>
346 <LINK id="pagestyle" REL="stylesheet" type="text/css" href="css/$style?version=$version_noalyss" media="screen"/>
347 <link rel="stylesheet" type="text/css" href="css/style-print.css?version=$version_noalyss" media="print"/>
348 $p_script2
349EOF;
350 // preload font
351 echo '<link rel="preload" href="css/font/OpenSansRegular.woff" as="font" crossorigin="anonymous" />';
352 echo '<link rel="preload" href="css/font/SansationLight/SansationLight.woff" as="font" crossorigin="anonymous" />';
353 echo '<link rel="preload" href="css/font/fontello/fontello.woff" as="font" crossorigin="anonymous" />';
354
355 echo '<script language="javascript" src="js/calendar.js"></script>
356 <script type="text/javascript" src="js/lang/calendar-en.js"></script>';
357
358 if (isset($_SESSION[SESSION_KEY.'g_lang']) && $_SESSION[SESSION_KEY.'g_lang']=='fr_FR.utf8' )
359 {
360 echo '<script type="text/javascript" src="js/lang/calendar-fr.js"></script>';
361 }
362 if (isset($_SESSION[SESSION_KEY.'g_lang']) && $_SESSION[SESSION_KEY.'g_lang']=='nl_NL.utf8' )
363 {
364 echo '<script type="text/javascript" src="js/lang/calendar-nl.js"></script>';
365 }
366 echo '
367 <script language="javascript" src="js/calendar-setup.js"></script>
368 <LINK REL="stylesheet" type="text/css" href="css/calendar-blue.css" media="screen">
369 ';
370 // language
371 if (isset($_SESSION[SESSION_KEY.'g_lang']))
372 {
373 set_language();
374 }
375
376 echo load_all_script();
377
378 // Retrieve colors for this folder
379 if ( isset($_REQUEST['gDossier']) ) {
381 $noalyss_appearance->print_css();
382 }
383 echo ' </HEAD> ';
384
385 echo "<BODY $p_script>";
386 echo '<div id="info_div"></div>';
387 echo '<div id="error_div">'.
388 HtmlInput::title_box(_("Erreur"), 'error_div','hide').
389 '<div id="error_content_div">'.
390 '</div>'.
391 '<p style="text-align:center">'.
392 HtmlInput::button_action('Valider','$(\'error_div\').style.visibility=\'hidden\';$(\'error_content_div\').innerHTML=\'\';').
393 '</p>'.
394 '</div>';
395
396}
397
398/**
399 * \brief Minimal page header for each page, used for small popup window
400 *
401 * \param p_theme default theme
402 * \param $p_script
403 * \param $p_script2 another js script
404 *
405 * \return none
406 */
407
408function html_min_page_start($p_theme="", $p_script="", $p_script2="")
409{
410
411 $cn = new Database();
412 if ($p_theme != "")
413 {
414 $Res = $cn->exec_sql("select the_filestyle from theme
415 where the_name='" . $p_theme . "'");
416 if (Database::num_row($Res) == 0)
417 $style = "style-classic7.css";
418 else
419 {
421 $style = $s['the_filestyle'];
422 }
423 }
424 else
425 {
426 $style = "style-classic7.css";
427 } // end if
428 echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 FINAL//EN">';
429 echo "<HTML>";
430
431
432 if ($p_script2!="")
433 {
434 $p_script2='<script src="'.$p_script2.'" type="text/javascript"></script>';
435 }
436
437 echo "<HEAD>
438 <TITLE>NOALYSS</TITLE>
439 <META http-equiv=\"Content-Type\" content=\"text/html; charset=UTF8\">
440 <LINK REL=\"stylesheet\" type=\"text/css\" href=\"css/$style\" media=\"screen\">
441 <link rel=\"stylesheet\" type=\"text/css\" href=\"css/style-print.css\" media=\"print\">" .
442 $p_script2 . "
443 <script src=\"js/prototype.js\" type=\"text/javascript\"></script>
444 <script src=\"js/noalyss_script.js\" type=\"text/javascript\"></script>
445 <script src=\"js/acc_ledger.js\" type=\"text/javascript\"></script>
446 <script src=\"js/smoke.js\" type=\"text/javascript\"></script>";
447 echo "<LINK id=\"pagestyle\" REL=\"stylesheet\" type=\"text/css\" href=\"css/font/fontello/css/fontello.css\" media=\"screen\"/>";
448 include_once NOALYSS_INCLUDE.'/lib/message_javascript.php';
449 // Retrieve colors for this folder
450 if ( isset($_REQUEST['gDossier']) ) {
452 $noalyss_appearance->print_css();
453 }
454 echo '</HEAD>';
455 echo "<BODY $p_script>";
456 /* If we are on the user_login page */
457 if (basename($_SERVER['PHP_SELF']) == 'user_login.php')
458 {
459 return;
460 }
461}
462
463/**
464 * \brief end tag
465 *
466 */
467
469{
470 echo "</BODY>";
471 echo "</HTML>";
472}
473
474/**
475 * \brief Echo no access and stop
476 *
477 * \return nothing
478 */
479
480function NoAccess($js=1)
481{
482 if ($js == 1)
483 {
484 echo "<script>";
485 echo "alert ('" . _('Cette action ne vous est pas autorisée Contactez votre responsable') . "');";
486 echo "</script>";
487 }
488 else
489 {
490 echo '<div class="redcontent">';
491 echo '<h2 class="error">' . _(' Cette action ne vous est pas autorisée Contactez votre responsable') . '</h2>';
492 echo '</div>';
493 }
494 exit - 1;
495}
496/**
497 * @brief replaced by sql_string
498 * @deprecated
499 */
500function FormatString($p_string)
501{
502 return sql_string($p_string);
503}
504/**
505 * \brief Fix the problem with the quote char for the database
506 *
507 * \param $p_string
508 * \return a string which won't let strange char for the database
509 */
510
511function sql_string($p_string)
512{
513 $p_string = trim($p_string??"");
514 if (strlen($p_string) == 0)
515 return null;
516 $p_string = noalyss_str_replace("'", "''", $p_string);
517 $p_string = noalyss_str_replace('\\', '\\\\', $p_string);
518 return $p_string;
519}
520
521/**
522* \brief store the string which print
523 * the content of p_array in a table
524 * used to display the menu
525 * \param $p_array array like ( 0=>HREF reference, 1=>visible item (name),2=>Help(opt),
526 * 3=>selected (opt) 4=>javascript (normally a onclick event) (opt)
527 * \param $p_dir direction of the menu (H Horizontal V vertical)
528 * \param $class CSS for li tag
529 * \param $class_ref CSS for the A tag
530 * \param $default selected item
531 * \param $p_extra extra code for the table tag (CSS or javascript)
532 */
533 /* \return : string */
534
535function ShowItem($p_array, $p_dir='V', $class="nav-item", $class_ref="nav-link", $default="", $p_extra="nav nav-pills nav-fill")
536{
537 $ret = '';
538 // for comptability with old application mtitle for anchor is replace by nav-link
539
540
541 // direction Vertical
542 if ($p_dir == 'V')
543 {
544 $ret .= "<ul class=\"$p_extra \" flex-row>";
545 } else {
546 $ret .= "<ul class=\"$p_extra \" >";
547
548 }
549
550 foreach ($p_array as $all => $href)
551 {
552 $javascript = (isset($href[4])) ? $href[4] : "";
553 $title = "";
554 $set = "XX";
555 if (isset($href[2]))
556 {
557 $title=$href[2];
558 }
559 if (isset($href[3]))
560 {
561 $set=$href[3];
562 }
563
564 if ($set==$default)
565 {
566 $ret.='<li class="nav-item"><A class="'.$class_ref.' active'.'" HREF="'.$href[0].'" title="'.$title.'" '.$javascript.'>'.$href[1].'</A></li>';
567 }
568 else
569 {
570 $ret.='<li class="nav-item"><A class="'.$class_ref.'" HREF="'.$href[0].'" title="'.$title.'" '.$javascript.'>'.$href[1].'</A></li>';
571 }
572
573 }
574
575 $ret.="</ul>";
576 return $ret;
577}
578
579/**
580 * \brief warns
581 *
582 * \param p_string error message
583 * gen :
584 * - none
585 * \return:
586 * - none
587 */
588
589function echo_warning($p_string)
590{
591 echo '<span class="warning">' . $p_string . '</span>';
592}
593
594/**
595 * \brief Show the periode which found thanks its id
596 *
597 *
598 * \param $p_cn database connection
599 * \param p_id
600 * \param pos Start or end
601 *
602 * \return: string
603 */
604
605function getPeriodeName($p_cn, $p_id, $pos='p_start')
606{
607 if ($pos != 'p_start' && $pos != 'p_end')
608 {
609 echo_error('lib/ac_common.php' . "-" . __LINE__ . ' UNDEFINED PERIODE');
610 throw new Exception(_("paramètre invalide"));
611 }
612 if ( isNumber($p_id) == 0 )
613 {
614 throw new Exception("Paramètre invalide");
615 return;
616 }
617 $ret = $p_cn->get_value("select to_char($pos,'Mon YYYY') as t from parm_periode where p_id=$1",
618 array( $p_id));
619 return $ret;
620}
621
622/**
623 * \brief Return the period corresponding to the
624 * date
625 *
626 * \param p_cn database connection
627 * \param p_date the month + year 'MM.YYYY'
628 *
629 * \return:
630 * parm_periode.p_id
631 */
632
634{
635 $R = $p_cn->get_value("select p_id from parm_periode where
636 to_char(p_start,'DD.MM.YYYY') = $1", array('01.'.$p_date));
637 if ($R == "")
638 return -1;
639 return $R;
640}
641
642/**
643 * \brief Decode the html for the widegt richtext and remove newline
644 * \param $p_html string to decode
645 * \return the html code without new line
646 */
647
648function Decode($p_html)
649{
650 $p_html = noalyss_str_replace('%0D', '', $p_html);
651 $p_html = noalyss_str_replace('%0A', '', $p_html);
652 $p_html = urldecode($p_html);
653 return $p_html;
654}
655/**
656 * @brief transform the SQL for ANALYTIC table operation_analytique
657 * @see sql_filter_per
658 * @param string $p_sql
659 */
661{
662 $result=noalyss_str_replace("j_tech_per in (select p_id from parm_periode where","",$p_sql);
663 $result=noalyss_str_replace("jr_tech_per in (select p_id from parm_periode where","",$result);
664 $result=noalyss_str_replace("j_tech_per = (select p_id from parm_periode where p_start "," oa_date ",$result);
665 $result=noalyss_str_replace("p_start >= to_date","oa_date >= to_date",$result);
666 $result=noalyss_str_replace("p_end <= to_date","oa_date <= to_date",$result);
667
668 $result="( $result";
669 return $result;
670}
671/**
672 * \brief Create the condition to filter on the j_tech_per
673 * thanks a from and to date.
674 * \param $p_cn database conx
675 * \param $p_from start date (date)
676 * \param $p_to end date (date)
677 * \param $p_form if the p_from and p_to are date or p_id
678 * \param $p_field column name
679 * \return a string containg the query
680 */
681
682function sql_filter_per($p_cn, $p_from, $p_to, $p_form='p_id', $p_field='jr_tech_per')
683{
684
685 if ($p_form != 'p_id' && $p_form != 'date')
686 {
687 echo_error(__FILE__, __LINE__, 'Mauvais parametres ');
688 exit(-1);
689 }
690 $p_from= sql_string($p_from);
691 $p_to= sql_string($p_to);
692 $p_field= sql_string($p_field);
693 if ($p_form == 'p_id')
694 {
695 if ( isNUmber($p_from)==0 || isNUmber($p_to)==0){
696 throw new Exception("SFP1"._("Nombre invalide")."\$p_from=$p_from \$p_to=$p_to");
697 }
698 // retrieve the date
699 $pPeriode = new Periode($p_cn);
700 $a_start = $pPeriode->get_date_limit($p_from);
701 $a_end = $pPeriode->get_date_limit($p_to);
702 if ($a_start==null||$a_end==null)
703 {
704 throw new Exception(__FILE__.__LINE__.sprintf(_('Attention periode
705 non trouvee periode p_from= %s p_to_periode = %s'), $p_from, $p_to));
706 }
707
708
709 $p_from = $a_start['p_start'];
710 $p_to = $a_end['p_end'];
711 }else {
712 if ( isDate($p_from)==NULL || isDate($p_to)==NULL){
713 throw new Exception("SFP2"._("Date invalide")."\$p_from=$p_from \$p_to=$p_to");
714 }
715 }
716 if ($p_from==$p_to)
717 {
718 $periode=" $p_field = (select p_id from parm_periode ".
719 " where ".
720 " p_start = to_date('$p_from','DD.MM.YYYY')) ";
721 }
722 else
723 {
724 $periode="$p_field in (select p_id from parm_periode ".
725 " where p_start >= to_date('$p_from','DD.MM.YYYY') and p_end <= to_date('$p_to','DD.MM.YYYY')) ";
726 }
727 return $periode;
728}
729
730/**
731 * \brief alert in javascript
732 * \param $p_msg is the message
733 * \param $buffer if false, echo directly and execute the javascript, if $buffer is true, the alert javascript
734 * is in the return string
735 * \return string with alert javascript if $buffer is true
736 */
737
738function alert($p_msg, $buffer=false)
739{
740 $r = '<script>';
741 $r.= 'alert_box(\'' . j($p_msg) . '\')';
742 $r.= '</script>';
743
744 if ($buffer)
745 {
746 return $r;
747 }
748 echo $r;
749}
750
751/**
752 * @brief set the lang thanks the _SESSION['g_lang'] var.
753 */
754function set_language()
755{
756 // desactivate local check
757 if (defined("LOCALE")&&LOCALE==0)
758 {
759 return;
760 }
761 if (!isset($_SESSION[SESSION_KEY.'g_lang']))
762 {
763 return;
764 }
765
766 /*
767 * If translation is not supported by current
768 */
769 if (!function_exists("bindtextdomain"))
770 {
771 return;
772 }
773
774 $dir = "";
775 // set differently the language depending of the operating system
776 if (what_os() == 1)
777 {
778 $dir = setlocale(LC_MESSAGES, $_SESSION[SESSION_KEY.'g_lang']);
779 if ($dir == "")
780 {
781 $g_lang = 'fr_FR.utf8';
782 $dir = setlocale(LC_MESSAGES, $g_lang);
783 // echo '<span class="notice">' . $_SESSION[SESSION_KEY.'g_lang'] . ' domaine non supporté</h2>';
784 }
785 bindtextdomain('messages', NOALYSS_HOME.'/lang');
786 textdomain('messages');
787 bind_textdomain_codeset('messages', 'UTF8');
788
789 return;
790 }
791 // for windows
792 putenv('LANG=' . $_SESSION[SESSION_KEY.'g_lang']);
793 $dir = setlocale(LC_ALL, $_SESSION[SESSION_KEY.'g_lang']);
794 bindtextdomain('messages', '.\\lang');
795 textdomain('messages');
796 bind_textdomain_codeset('messages', 'UTF8');
797}
798
799/**
800 * @brief try to determine on what os you are running the pĥpcompte
801 * server
802 * @return
803 * 0 it is a windows
804 * 1 it is a Unix like
805 */
806function what_os()
807{
808 $inc_path = get_include_path();
809
810 if (strpos($inc_path, ";") != 0)
811 {
812 $os = 0; /* $os is 0 for windoz */
813 }
814 else
815 {
816 $os = 1; /* $os is 1 for unix */
817 }
818 return $os;
819}
820
821/**
822 * @brief shrink the date, make a date shorter for the printing
823 * @param $p_date format DD.MM.YYYY
824 * @return date in the format DDMMYY (size = 13 mm in arial 8)
825 */
826function shrink_date($p_date)
827{
828 $date = noalyss_str_replace('.', '', $p_date);
829 $str_date = substr($date, 0, 4) . substr($date, 6, 2);
830 return $str_date;
831}
832/**
833 * @brief shrink the date, make a date shorter for the printing
834 * @param $p_date format DD.MM.YYYY
835 * @return date in the format DD.MM.YY (size = 13 mm in arial 8)
836 */
837function smaller_date($p_date)
838{
839 if (empty ($p_date)) return "";
840 $str_date = substr($p_date, 0, 6) . substr($p_date, 8, 2);
841 return $str_date;
842}
843
844/**
845 * @brief format the date, when taken from the database the format
846 * is MM-DD-YYYY
847 * @param $p_date format
848 * @exception 1 if invalid format
849 * DOMEntity@param
850 * @return date in the format DD.MM.YYYY
851 */
852function format_date($p_date, $p_from_format = 'YYYY-MM-DD',$p_to_format='DD.MM.YYYY')
853{
854 if (empty($p_date)) {return $p_date;}
855 if ($p_from_format == 'YYYY-MM-DD')
856 {
857 $date = explode('-', $p_date);
858 if (count($date) != 3)
859 return $p_date;
860 }
861 if ($p_from_format == 'DD.MM.YYYY')
862 {
863 $temp_date = explode('.', $p_date);
864 if (count($temp_date) != 3)
865 return $p_date;
866 $date[0] = $temp_date[2]; // 0 is year
867 $date[1] = $temp_date[1]; // 1 for month
868 $date[2] = $temp_date[0]; // 2 for day
869 }
870
871 switch ($p_to_format)
872 {
873 case 'DD.MM.YYYY':
874 $str_date = $date[2] . '.' . $date[1] . '.' . $date[0];
875 break;
876 case 'DD-MM-YYYY':
877 $str_date = $date[2] . '-' . $date[1] . '-' . $date[0];
878 break;
879 case 'YYYY-MM-DD':
880 $str_date = $date[0] . '-' . $date[1] . '-' . $date[2];
881 break;
882 case 'YYYYMMDD':
883 $str_date = $date[0] . $date[1] . $date[2];
884 break;
885 case 'YYYY/MM/DD':
886 $str_date = $date[0] . '/' . $date[1] . '/' . $date[2];
887 break;
888 case "DD.MM.YY":
889 $str_date = $date[2] . '.' . $date[1] . '.' . substr($date[0],2,2);
890 break;
891 case "DD-MM-YY":
892 $str_date = $date[2] . '-' . $date[1] . '-' . substr($date[0],2,2);
893 break;
894 default:
895 throw new Exception(_("Format Invalide"),1);
896
897 }
898 return $str_date;
899}
900
901
902
903/**
904 *@brief Should a dialog box when you are disconnected from an ajax call
905 * propose to reload or to connect in another tab
906 */
907function ajax_disconnected($p_div)
908{
909
910 echo HtmlInput::title_box(_("Déconnecté"), $p_div);
911 echo h2(_('Données non disponibles'), 'class="error" ');
912 echo h2(_('Veuillez vous reconnecter soit dans une autre fenêtre soit '
913 . ' en cliquant sur le bouton'), 'class="error"');
914 // Reload button
915 $reload=new IButton("reload");
916 $reload->value=_("Se connecter");
917 $reload->class="button";
918 $reload->javascript='window.location.reload()';
919 // Link to log in another tab
920 echo '<p style="text-align:center">';
921 echo $reload->input();
922 echo HtmlInput::button_close($p_div,'button');
923 echo '</p>';
924
925
926}
927
928/**
929 * @brief Show the modules
930 * @param int $selected module selected profile_menu.pm_id
931 */
932function show_module($selected)
933{
934 global $g_user;
935 $cn = Dossier::connect();
936 $amodule = $cn->get_array("select
937 me_code,me_menu,me_url,me_javascript,p_order,me_type,me_description
938 from v_all_menu
939 where
940 p_id=$1
941 and p_type_display='M'
942 order by p_order", array($g_user->get_profile()));
943
944 if ($selected != -1)
945 {
946 $selected_module=$cn->get_value('select me_code from profile_menu where'
947 . ' pm_id = $1 ', array($selected));
948 require_once NOALYSS_TEMPLATE.'/module.php';
949 $file = $cn->get_array("select me_file,me_parameter,me_javascript,me_type,me_description from v_all_menu
950 where pm_id=$1 and p_id=$2", array($selected,$g_user->get_profile()));
951 if ( count($file ) == 0 )
952 {
953 echo '</div>';
954 echo '</div>';
955 echo '<div class="content">';
956 echo_warning(_("Module inexistant")."[ $selected ] ");
957 echo '</div>';
958 exit();
959 }
960 if ($file[0]['me_file'] != '')
961 {
962 if ($file[0]['me_parameter'] != "")
963 {
964 // if there are paramter put them in superglobal
965 $array=compute_variable($file[0]['me_parameter']);
966 put_global($array);
967 }
968
969 // if file is not a plugin, include the file, otherwise
970 // include the plugin launcher
971 if ($file[0]['me_type'] != 'PL')
972 {
973 require_once $file[0]['me_file'];
974 }
975 else
976 {
977 // nothing : direct call to plugin
978 }
979 }
980 if ( $file[0]['me_javascript'] != '')
981 {
982 create_script($file[0]['me_javascript']);
983 }
984 }
985}
986/**
987 * @brief Find the default module or the first one
988 * @var $g_user $g_user
989 * @return default module (string)
990 */
991function find_default_module()
992{
993 global $g_user;
994 $cn = Dossier::connect();
995
996 $default_module = $cn->get_array("select me_code
997 from profile_menu join profile_user using (p_id)
998 where
999 p_type_display='M' and
1000 user_name=$1 and pm_default=1", array($g_user->login));
1001
1002 /*
1003 * Try to find the smallest order for module
1004 */
1005 if (empty($default_module))
1006 {
1007 $default_module = $cn->get_array("select me_code
1008 from profile_menu join profile_user using (p_id)
1009 where
1010 p_type_display='M' and
1011 user_name=$1 order by p_order limit 1", array($g_user->login));
1012
1013 // if no default try to find the default menu
1014 if ( empty ($default_module))
1015 {
1016 $default_module = $cn->get_array("select me_code
1017 from profile_menu join profile_user using (p_id)
1018 where
1019 p_type_display='E' and
1020 user_name=$1 and pm_default=1 ", array($g_user->login));
1021 /*
1022 * Try to find a default menu by order
1023 */
1024 if (empty ($default_module))
1025 {
1026 $default_module = $cn->get_array("select me_code
1027 from profile_menu join profile_user using (p_id)
1028 where
1029 user_name=$1 and p_order=(select min(p_order) from profile_menu join profile_user using (p_id)
1030 where user_name=$2) limit 1", array($g_user->login, $g_user->login));
1031 }
1032
1033 /*
1034 * if nothing found, there is no profile for this user => exit
1035 */
1036 if (empty ($default_module))
1037 {
1038 /*
1039 * If administrateur, then we insert a default profile (1)
1040 * for him
1041 */
1042 if ( $g_user->admin == 1 )
1043 {
1044 $cn->exec_sql('insert into profile_user(user_name,p_id) values ($1,1) ',array($g_user->login));
1045 return find_default_module();
1046 }
1047 echo_warning(_("Utilisateur n'a pas de profil, votre administrateur doit en configurer un dans CFGSEC"));
1048 exit();
1049 }
1050 }
1051 return $default_module[0]['me_code'];
1052 }
1053
1054 if (count($default_module) > 1)
1055 {
1056 // return the first module found
1057 return $default_module[0]['me_code'];
1058 }
1059 elseif (count($default_module) == 1)
1060 {
1061 return $default_module[0]['me_code'];
1062 }
1063}
1064
1065/**
1066 * @brief show the module
1067 * @var $g_user
1068 * @param $module the $_REQUEST['ac'] exploded into an array
1069 * @param $idx the index of the array : the AD code is splitted into an array thanks the slash
1070 */
1071function show_menu($module)
1072{
1073 if ($module == 0)return;
1074 static $level=0;
1075 global $g_user;
1076 $http=new HttpInput();
1077 $access_code=$http->request("ac");
1078 $cn = Dossier::connect();
1079 /**
1080 * Show the submenus
1081 */
1082 $amenu = $cn->get_array("
1083 select
1084 pm_id,
1085 me_code,
1086 pm_id_dep,
1087 me_file,
1088 me_javascript,
1089 me_url,
1090 me_menu,
1091 me_description,
1092 me_description_etendue
1093 from profile_menu
1094 join menu_ref using (me_code)
1095 where pm_id_dep=$1 and p_id=$2
1096 order by p_order", array($module, $g_user->get_profile()));
1097
1098 // There are submenuS, so show them
1099 if (!empty($amenu) && count($amenu) > 1)
1100 {
1101 $a_style_menu=array('topmenu','menu2','menu3');
1102 if ( $level > count($a_style_menu))
1103 $style_menu='menu3';
1104 else {
1105 $style_menu=$a_style_menu[$level];
1106 }
1107 require NOALYSS_TEMPLATE.'/menu.php';
1108 $level++;
1109 return;
1110 } elseif (count($amenu) == 1)
1111 {
1112 // there is only one submenu so we include the code or javascript
1113 // or we show the submenu
1114 if ( trim($amenu[0]['me_url']??"") != "" ||
1115 trim ($amenu[0]['me_file']??"") != "" ||
1116 trim ($amenu[0]['me_javascript']??"") != "" )
1117 {
1118 echo '<div class="topmenu">';
1119 echo h2info(_($amenu[0]['me_menu']));
1120 echo '</div>';
1121 $module = $amenu[0]['pm_id'];
1122 display_menu($module);
1123 $level++;
1124 return;
1125 } else {
1126 $url=$access_code.'/'.$amenu[0]['me_code'];
1127 echo '<a href="do.php?gDossier='.Dossier::id().'&ac='.$url.'">';
1128 echo _($amenu[0]['me_menu']);
1129 echo '</a>';
1130 $level++;
1131 return;
1132 }
1133 }
1134
1135 // !!! this point should never be reached
1136 // There is no submenu or only one
1137 if (empty($amenu) || count($amenu) == 1)
1138 {
1139 display_menu($module);
1140
1141 }
1142 $level++;
1143}
1144/**
1145 * @brief Display a menu
1146 * @global type $g_user
1147 * @param type $p_menuid
1148 * @return type
1149 */
1150function display_menu($p_menuid)
1151{
1152 if ($p_menuid == 0) return;
1153 global $g_user;
1154 $cn=Dossier::connect();
1155
1156 $file = $cn->get_array("
1157 select me_file,me_parameter,me_javascript,me_type
1158 from menu_ref
1159 join profile_menu using (me_code)
1160 join profile_user using (p_id)
1161 where
1162 pm_id=$1 and
1163 user_name=$2 and
1164 (me_file is not null or trim(me_file) <>'' or
1165 me_javascript is not null or trim (me_javascript) <> '')", array($p_menuid,$g_user->login));
1166
1167 if (count($file)==0)
1168 {
1169 return;
1170 }
1171
1172 if ($file[0]['me_file'] != "")
1173 {
1174 if ($file[0]['me_parameter'] !== "")
1175 {
1176 // if there are paramter put them in superglobal
1177 $array=compute_variable($file[0]['me_parameter']);
1178 put_global($array);
1179 }
1180 \Noalyss\Dbg::echo_var(1,$file[0]['me_file']." ".$file[0]['me_parameter']);
1181 /*
1182 * Log the file we input to put in the folder test-noalyss for replaying it
1183 */
1184 if (LOGINPUT) {
1185 $file_loginput=fopen($_ENV['TMP'].'/scenario-'.$_SERVER['REQUEST_TIME'].'.php','a+');
1186 fwrite($file_loginput, "include '".$file[0]['me_file']."';");
1187 fwrite($file_loginput,"\n");
1188 fclose($file_loginput);
1189 }
1190 // if file is not a plugin, include the file, otherwise
1191 // include the plugin launcher
1192 if ( $file[0]['me_type'] != 'PL') {
1193 if (file_exists ($file[0]['me_file']) )
1194 {
1195 require_once $file[0]['me_file'];
1196 } elseif ( file_exists(NOALYSS_INCLUDE.'/'.$file[0]['me_file'])) {
1197 require_once NOALYSS_INCLUDE.'/'.$file[0]['me_file'];
1198 }else {
1199 echo echo_warning(_("Fichier non trouvé"));
1200 }
1201 } else {
1202 require 'extension_get.inc.php';
1203 }
1204
1205 exit();
1206 } elseif ( $file[0]['me_javascript'] != '')
1207 {
1208 $js= noalyss_str_replace('<DOSSIER>', dossier::id(), $file[0]['me_javascript']);
1209 echo create_script($js);
1210 }
1211
1212}
1213/**
1214 * @brief Send an header CSV with a filename
1215 * @param string $p_filename , file name , caution , it must be sanitized BEFORE calling this function
1216 */
1217function header_csv($p_filename)
1218{
1219
1220 header('Pragma: public');
1221 header('Content-type: application/csv');
1222 header("Content-Disposition: attachment;filename=\"{$p_filename}\"",
1223 FALSE);
1224 header('Cache-Control: no-store, no-cache, must-revalidate');
1225 header('Expires: Sun, 1 Jan 2000 12:00:00 GMT');
1226 header('Last-Modified: '.gmdate('D, d M Y H:i:s').'GMT');
1227}
1228/**
1229 * @brief Put in superglobal (get,post,request) the value contained in
1230 * the parameter field (me_parameter)
1231 * @param $array [key] [value]
1232 */
1234{
1235 for ($i=0;$i<count($array);$i++)
1236 {
1237 $key=$array[$i]['key'];
1238 $value=$array[$i]['value'];
1239 $_GET[$key]=$value;
1240 $_POST[$key]=$value;
1241 $_REQUEST[$key]=$value;
1242 }
1243}
1244/**
1245 * @brief the string has the format a=b&c=d, it is parsed and an array[][key,value]
1246 * is returned
1247 * @param $p_string
1248 * @return $array usable in put_global
1249 */
1250function compute_variable($p_string)
1251{
1252 $array=array();
1253 if ($p_string == '') return $array;
1254
1255 $var=explode("&",$p_string);
1256 if (empty ($var)) return $array;
1257 for ($i=0;$i < count($var);$i++)
1258 {
1259 $var2=explode('=',$var[$i]);
1260 $array[$i]['key']=$var2[0];
1261 $array[$i]['value']=$var2[1];
1262 }
1263 return $array;
1264}
1265function ajax_xml_error($p_code,$p_string)
1266{
1267 $html = escape_xml($p_string);
1268 header('Content-type: text/xml; charset=UTF-8');
1269 echo <<<EOF
1270<?xml version="1.0" encoding="UTF-8"?>
1271<data>
1272<code>$p_code</code>
1273<value>$html</value>
1274</data>
1275EOF;
1276}
1277
1279{
1280 $array=array();
1281 for ($i=0;$i<count($p_array);$i++)
1282 {
1283 $r=$p_array[$i];
1284 if ( isset($r[$key])) {
1285 $array[]=$r[$key];
1286 }
1287 }
1288 return $array;
1289}
1290
1291/**
1292 * @brief This function create a ledger object and return the right one.
1293 * It uses the factory pattern
1294 * @param Database $p_cn
1295 * @param type $ledger_id
1296 * @return Acc_Ledger
1297 * @throws Exception
1298 */
1300{
1301 $ledger=new Acc_Ledger($p_cn, $ledger_id);
1302 $type=$ledger->get_type();
1303
1304 switch ($type)
1305 {
1306 case 'VEN':
1307 $obj=new Acc_Ledger_Sale($p_cn, $ledger_id);
1308 break;
1309 case 'ACH':
1311 break;
1312 case 'FIN':
1313 $obj= new Acc_Ledger_Fin($p_cn, $ledger_id);
1314 break;
1315 case 'ODS':
1316 $obj=$ledger;
1317 break;
1318
1319 default:
1320 throw new Exception('Ledger type not found');
1321 }
1322 return $obj;
1323}
1324/**
1325 * @brief Check if we use IE 8 or 9
1326 * @return int 1 for IE8-9;0 otherwise
1327 */
1328function is_msie()
1329{
1330 if ( strpos ($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0') != 0 ||
1331 strpos ($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0') != 0 )
1332 $is_msie=1;
1333 else
1334 $is_msie=0;
1335 return $is_msie;
1336}
1337/**
1338 *@brief Record an error message into the log file of the server.
1339 * Record also the GET and POST data
1340 * @param string $p_message
1341 */
1342function record_log($p_message)
1343{
1344 if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) {
1345
1346 error_log("noalyss exception ".$p_message->getMessage(),0);
1347 error_log("noalyss exception".$p_message->getTraceAsString(),0);
1348 } else {
1349 error_log("noalyss".var_export($p_message,true),0);
1350
1351 }
1352 error_log("noalyss GET [".json_encode($_GET,0,10)."]");
1353 error_log("_POST [".json_encode($_POST,0,10)."]",0);
1354}
1355if(!function_exists('tracedebug')) {
1356 function tracedebug($file,$var, $label = NULL) {
1357
1358 $tmp_file = sys_get_temp_dir().DIRECTORY_SEPARATOR.$file;
1359 if ( ! is_writable($tmp_file)) return;
1360 $output = '';
1361 $output .= date('d-m-y H:i');
1362 if(!is_null($label)) {
1363 $output .= $label . ': ';
1364 }
1365 if ( gettype ($var) == 'object' && get_class($var)=='DOMDocument')
1366 {
1367 $var->formatOutput=true;
1368 $output.=$var->saveXML() .PHP_EOL;
1369 } else
1370 {
1371 $output .= print_r($var, 1) . PHP_EOL;
1372 }
1373
1374 file_put_contents($tmp_file, $output, FILE_APPEND);
1375 }
1376}
1377/**
1378 * @brief encode the string for RTF, return a string
1379 * @param $p_string string to convert
1380 * @return string
1381 */
1382function convert_to_rtf($p_string)
1383{
1384 $result="";
1385 $p_string2=mb_convert_encoding($p_string,'ISO-8859-1','UTF-8');
1386 $p_string2=iconv('UTF-8','ISO-8859-1//IGNORE',$p_string);
1387
1388
1389 $nb_result=strlen($p_string2);
1390 for ($i = 0 ; $i < $nb_result ; $i++ ){
1391 if (ord($p_string[$i]) < 127 ) {
1392 $result.=$p_string[$i];
1393 } else {
1394 $result.='\u'.ord($p_string[$i]).chr(92).chr(39).'3f';
1395 }
1396 }
1397 return $result;
1398}
1399/**
1400 * @brief When it is needed to eval a formula , this function prevent the divide by zero.
1401 * the formula is a math operation to evaluate like : 1.0+2.0/1 (...) , it is used in "report",
1402 * it removes the operation "divide by 0 "
1403 *
1404 * @param string $p_formula string containing a operation to evaluate
1405 *
1406 * @see Impress::parse_formula
1407 */
1408function remove_divide_zero($p_formula)
1409{
1410 $test=noalyss_str_replace(" ","",$p_formula).";";
1411 $p_formula=preg_replace("![0-9]+\.*[0-9]*/0\.{0,1}0*(\+|-|\*|/|;){1}!","0$1",$test);
1412 $p_formula=trim($p_formula,';');
1413 return $p_formula;
1414}
1415
1416/**
1417 * @brief Create randomly a string
1418 * @param int $p_length length of the generate string
1419 */
1420function generate_random_string($p_length,$special=1)
1421{
1422 $string="";
1423 if ($special == 1)
1424 $chaine="abcdefghijklmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY0123456789*/+-=";
1425 if ($special == 0)
1426 $chaine="abcdefghijklmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY0123456789";
1427 $microtime=microtime(true)*microtime(true)*100;
1428 srand(0);
1429 srand((int)$microtime);
1430 for ($i=0; $i<$p_length; $i++)
1431 {
1432 $string .= $chaine[rand()%strlen($chaine)];
1433 }
1434 return $string;
1435}
1436
1437/**
1438 *@brief generate a string of p_car character and a input text with name p_ctl_name
1439 * work like a kind of captcha.The control code for checking is ctlcode.
1440 * You compare the content of the variable p_ctl_name with ctlcode
1441 * @param $p_ctl_name name of the HTML input text
1442 * @param $p_car length of the string
1443 */
1444function confirm_with_string($p_ctl_name,$p_car)
1445{
1447 $r = HtmlInput::hidden("ctlcode",$code);
1448 $r.='<span style="margin-left:1.2em;margin-right:1.2em;font-size:112%;font-weight:bold;border:navy solid 1px ; padding:0.5rem">'. $code.'</span>';
1449 $ctl=new IText($p_ctl_name);
1450 $r.=$ctl->input();
1451 return $r;
1452}
1453/**
1454 * @brief Find the menu marked as default in the given profile
1455 * @param number $pn_menu (profile_menu.id)
1456 */
1457function find_default_menu($pn_menu)
1458{
1460 $sql = ' select pm_id from profile_menu where pm_default =1 and pm_id_dep = $1';
1461 $aresult=$cn->get_array($sql, [$pn_menu]);
1462 if (empty($aresult)) {
1463 return 0;
1464 }
1465 return $aresult[0]['pm_id'];
1466}
1467
1468/**
1469 * @brief Check if there is a default menu for this user and add it. The array is filling from 1 to 3
1470 * @verbatim
1471 *
1472 * COMPTA 0 - 0 - 173
1473 * COMPTA/MENUACH 0 - 173 - 3
1474 * COMPTA/MENUACH/ACH 173 - 3 - 85
1475 *
1476 * @endverbatim
1477 *
1478 *
1479 * @param array $pa_menu if the array of option ; index pm_id_v1 , pm_id_v2 and pm_id_v3
1480 *
1481 */
1482function complete_default_menu($pa_menu)
1483{
1484 $a_result=$pa_menu;
1485 // find the first one which is null
1486 if ($pa_menu[0]['pm_id_v2'] == 0) {
1487 $tmp=find_default_menu($pa_menu[0]['pm_id_v1']);
1488 if ( $tmp <> 0 ) {
1489 $a_result[0]['pm_id_v2']=$pa_menu[0]['pm_id_v1'];
1490 $a_result[0]['pm_id_v1']=$tmp;
1491 }
1492 }
1493 if ($pa_menu[0]['pm_id_v3'] == 0) {
1494 $tmp=find_default_menu($a_result[0]['pm_id_v1']);
1495 if ( $tmp <> 0 ) {
1496 $a_result[0]['pm_id_v3']=$a_result[0]['pm_id_v2'];
1497 $a_result[0]['pm_id_v2']=$a_result[0]['pm_id_v1'];
1498 $a_result[0]['pm_id_v1']=$tmp;
1499 }
1500 }
1501 return $a_result;
1502}
1503/**
1504 * @brief rebuild the access code
1505 * @see complete_default_menu
1506 * @param array of number $pan_code index row [0] = index pm_id_v1 , pm_id_v2 and pm_id_v3
1507 */
1508function rebuild_access_code($pan_code)
1509{
1510 if ( empty ($pan_code)) {return;}
1511 $s_result="";
1513 $an_code=['pm_id_v3','pm_id_v2','pm_id_v1'];
1514 $sep="";
1515 for ($i=0;$i<3;$i++)
1516 {
1517 $ix=$an_code[$i];
1518 $s_result.=$sep.$cn->get_value("select me_code from profile_menu where pm_id=$1",[ $pan_code[0][$ix] ]);
1519 $sep=($s_result != "" )?"/":"";
1520 }
1521 return $s_result;
1522}
1523
1524/***
1525 *@brief Transform a http link into a clickable link
1526 */
1528{
1529
1530 $ret=preg_replace("!http[s]{0,1}://[[:graph:]*]*!",'<a href="\\0" target="_blank">\0</a>',$text);
1531
1532 return $ret;
1533
1534}
1535//---------------------------------------------------------------------------------------------------------------------
1536//
1537// PHP 8.2 fails with these functions when a NULL is given as argument
1538//
1539//---------------------------------------------------------------------------------------------------------------------
1540
1541function noalyss_strlen($p_string) :int{
1542 if ($p_string ==null ) return 0;
1543 else return strlen($p_string);
1544}
1545function noalyss_trim($p_string) {
1546 if ($p_string===null) return "";
1547 else return trim($p_string);
1548}
1549function noalyss_strlentrim($p_string) :int {
1550 if ($p_string ==null ) return 0;
1551 return strlen(trim($p_string));
1552}
1553function noalyss_str_replace($search,$replace,$string) {
1554 if ($string===null) return "";
1555 else return str_replace($search,$replace??"",$string);
1556}
1557function noalyss_bcsub($p_first,$p_second,$p_decimal=4)
1558{
1559 $p_first=(empty($p_first))?0:$p_first;
1560 $p_second=(empty($p_second))?0:$p_second;
1561 return bcsub($p_first,$p_second,$p_decimal);
1562}
1563function noalyss_bcadd($p_first,$p_second,$p_decimal=4)
1564{
1565 $p_first=(empty($p_first))?0:$p_first;
1566 $p_second=(empty($p_second))?0:$p_second;
1567 return bcadd($p_first,$p_second,$p_decimal);
1568}
1569function noalyss_strip_tags($p_string)
1570{
1571 if ($p_string===null) return "";
1572 return strip_tags($p_string);
1573}
1574function noalyss_bcmul($p_first,$p_second)
1575{
1576 $p_first=(empty($p_first))?0:$p_first;
1577 $p_second=(empty($p_second))?0:$p_second;
1578 return bcmul($p_first??0,$p_second??0);
1579}
1580function noalyss_round($p_first,$p_second)
1581{
1582 $p_first=(empty($p_first))?0:$p_first;
1583 $p_second=(empty($p_second))?0:$p_second;
1584 return round($p_first??0,$p_second??0);
1585}
1586
1587/**
1588 * @brief to avoid deprecated in PHP8.1 : explode cannot use a null
1589 * @param $separator
1590 * @param $string
1591 * @return array | empty string
1592 */
1593function noalyss_explode($separator,$string) {
1594 if ($string===null) return '';
1595 return explode($separator,$string);
1596}
1597/**
1598 * @brief compose a HTML string with phone
1599 * @param string $p_tel
1600 * @return false|string returns false if $p_tel is empty
1601 */
1602function phoneTo($p_tel) {
1603 if (!empty($p_tel)) {
1604 $r=sprintf('<a href="tel:%s">%s</a>',h($p_tel),h($p_tel));
1605 return $r;
1606 }
1607 return false;
1608}
1609
1610/**
1611 * @brief compose a HTML string with email
1612 * @param string $p_email email or emails separated by a comma
1613 * @return false|string returns false if email not valide
1614 */
1615function mailTo($p_email) {
1616 if (empty($p_email )) return "";
1617 $nComma=preg_match("/,/",$p_email);
1618 if ( $nComma > 0) {
1619 $aEmail=explode(",",$p_email);
1620 } else {
1621 $aEmail[0]=$p_email;
1622 }
1623 $r="";
1624 foreach ($aEmail as $email) {
1625 if ( filter_var(trim($email),FILTER_VALIDATE_EMAIL) ) {
1626 $r.=sprintf('<a href="mailto:%s">%s</a> ',h($email),h($email));
1627 } else {
1628 $r.=sprintf("%s",h($email));
1630
1631 }
1632
1633 }
1634 return $r;
1635}
1636
1637/**
1638 * @brief compose a HTML string with fax
1639 * @param string $p_fax fax number
1640 * @return false|string returns false if $p_fax is empty
1641 */
1642function FaxTo($p_tel) {
1643 if (!empty($p_tel)) {
1644 $r=sprintf('<a href="fax:%s">%s</a>',h($p_tel),h($p_tel));
1645 return $r;
1646 }
1647 return false;
1648}
1650{
1651 if (empty($p_url) ) return "";
1652 if (filter_var(trim($p_url,FILTER_VALIDATE_URL)) ) {
1653 return sprintf('<a href="%s" target="_blank">%s</a>',$p_url,$p_url);
1654 } else {
1655 return $p_url;
1656 }
1657}
1658/**
1659 * @brief When you want to prevent users to connect, create a file in noalyss/ (NOALYSS_BASE) with the
1660 * message in Html
1661 * @param string $p_file file in NOALYSS_BASE
1662 */
1663function MaintenanceMode($p_file)
1664{
1665 if ( file_exists(NOALYSS_BASE."/".$p_file )) {
1666 include NOALYSS_BASE."/".$p_file;
1667 exit;
1668 }
1669}
1670
1671/**
1672 * @brief returns an double array with the error found and code , if the count is 0 then the password is very string, 5 means it is
1673 * empty ,4 weak, ... the array contains the errors, [msg]=>array message [code] => array of code
1674 * Codes are
1675 * - 1 : too short
1676 * - 2 : missing digit
1677 * - 3 : missing lowercase letter
1678 * - 4 : missing uppercase letter
1679 * - 5 : too many time same letter or symbol..
1680 * - 6 : missing special char
1681 *
1682 * If the password is strong returns an empty array
1683 *
1684 * @param $password string
1685 * @code
1686
1687 $error = check_password_strength($password);
1688 if ( count($error['msg']) > 0 ) {
1689 echo "password to weak";
1690 foreach ($error['msg'] as $item_error) {
1691 echo "error $item_error";
1692 }
1693
1694 } else {
1695 echo "OK password strong";
1696 }
1697
1698 * @endcode
1699 */
1700function check_password_strength($password) {
1701 $errors=array();
1702 $error_code=array();
1703
1704 $len=strlen($password??"");
1705 if ( $len < 8) {
1706 $errors[] = _("mot de passe de 8 lettres minimum");
1707 $error_code[]=1;
1708 }
1709
1710 if (!preg_match("#[0-9]+#", $password)) {
1711 $errors[] = _("mot de passe doit inclure au moins un chiffre");
1712 $error_code[]=2;
1713 }
1714
1715 if (!preg_match("#[a-z]+#", $password)) {
1716 $errors[] = _("mot de passe doit inclure au moins une minuscule");
1717 $error_code[]=3;
1718 }
1719 if (!preg_match("#[A-Z]+#", $password)) {
1720 $errors[] = _("mot de passe doit inclure au moins une majuscule");
1721 $error_code[]=4;
1722 }
1723
1724 if ( $len > 0 ) {
1725 $cnt_diff=count(count_chars($password,1));
1726 $ratio_diff=$len/$cnt_diff;
1727
1728 if ($ratio_diff > 2) {
1729 $errors[] = _("Trop souvent le(s) même(s) symbole(s)");
1730 $error_code[]=5;
1731 }
1732 $special_char=preg_replace('/[[:alnum:]]/','',$password);
1733 if ( strlen($special_char??"")==0)
1734 {
1735 $errors[] = _("mot de passe doit inclure au moins un caractére spécial '+-/*[...'");
1736 $error_code[]=6;
1737
1738 }
1739 }
1740
1741 return array( 'msg'=>$errors, 'code'=>$error_code);
1742}
1743/**
1744 * @brief generate a strong random password
1745 * @param $car int length of the password, minimum 8
1746 *
1747 */
1748function generate_random_password($car):string
1749{
1750 $string="";
1751 $car=($car < 8 )?8:$car;
1752 $max_loop=20;$loop=0;
1753 do
1754 {
1755 $loop++;
1756 $string="";
1757 $chaine="abcdefghijklmnpqrstuvwxy";
1758 // srand( (int)microtime()*1020030);
1759 for ($i=0; $i<$car; $i++)
1760 {
1761 $string .= $chaine[rand()%strlen($chaine)];
1762 }
1763 $chaine="ABCDEFGHIJKLMNPQRSTUVWXY";
1764 for ($i=0;$i<2;$i++) {
1765 $string[rand()%$car]=$chaine[rand()%strlen($chaine)];;
1766 }
1767 $chaine="0123456789";
1768 for ($i=0;$i<2;$i++) {
1769 $string[rand()%$car]=$chaine[rand()%strlen($chaine)];;
1770 }
1771 $special_set="+-/*;,.=:&()[]";
1772 $special_car=$special_set[rand()%strlen($special_set)];
1773 $string[rand()%$car]=$special_car;
1774 // echo $string."\n";
1775 }while ( count(check_password_strength($string)['msg'])> 0 && $loop<$max_loop);
1776 return $string;
1777}
1778
1779/**
1780 * @brief removed invalid character when computing a filename, the suffix is kept
1781 * @param $filename String filename to sanitize
1782 * @return string without offending char
1783 */
1784function sanitize_filename($filename)
1785{
1786 // save the suffix
1787 $pos_prefix=strrpos($filename, ".");
1788 if ($pos_prefix==0)
1789 {
1790 $filename_suff=".pdf";
1791 $filename.=$filename_suff;
1792 $pos_prefix=strrpos($filename, ".");
1793 }
1794 else
1795 $filename_suff=substr($filename, $pos_prefix, strlen($filename));
1796
1797 $filename=str_replace(array('/', '*', '<', '>', ';', ',', '\\', '.', ':', '(', ')', ' ', '[', ']'), "-", $filename);
1798
1799 $filename_no=substr($filename, 0, $pos_prefix);
1800
1801 $new_filename=strtolower($filename_no)."-".date("Ymd-Hi").$filename_suff;
1802 return $new_filename;
1803}
noalyss_bcsub($p_first, $p_second, $p_decimal=4)
Definition: ac_common.php:1557
noalyss_round($p_first, $p_second)
Definition: ac_common.php:1580
noalyss_bcadd($p_first, $p_second, $p_decimal=4)
Definition: ac_common.php:1563
ajax_xml_error($p_code, $p_string)
Definition: ac_common.php:1265
transform_sql_filter_per($p_sql)
transform the SQL for ANALYTIC table operation_analytique
Definition: ac_common.php:660
h2($p_string, $p_class="", $raw="")
Definition: ac_common.php:68
sanitize_filename($filename)
removed invalid character when computing a filename, the suffix is kept
Definition: ac_common.php:1784
FormatString($p_string)
replaced by sql_string
Definition: ac_common.php:500
isNumber($p_int)
Definition: ac_common.php:215
complete_default_menu($pa_menu)
Check if there is a default menu for this user and add it.
Definition: ac_common.php:1482
compute_variable($p_string)
the string has the format a=b&c=d, it is parsed and an array[][key,value] is returned
Definition: ac_common.php:1250
h($p_string)
to protect again bad characters which can lead to a cross scripting attack the string to be diplayed ...
Definition: ac_common.php:35
noalyss_strlen($p_string)
Definition: ac_common.php:1541
phoneTo($p_tel)
compose a HTML string with phone
Definition: ac_common.php:1602
th($p_string, $p_extra='', $raw='')
Definition: ac_common.php:58
hi($p_string)
Definition: ac_common.php:48
MaintenanceMode($p_file)
When you want to prevent users to connect, create a file in noalyss/ (NOALYSS_BASE) with the message ...
Definition: ac_common.php:1663
sql_filter_per($p_cn, $p_from, $p_to, $p_form='p_id', $p_field='jr_tech_per')
Create the condition to filter on the j_tech_per thanks a from and to date.
Definition: ac_common.php:682
confirm_with_string($p_ctl_name, $p_car)
generate a string of p_car character and a input text with name p_ctl_name work like a kind of captch...
Definition: ac_common.php:1444
noalyss_explode($separator, $string)
to avoid deprecated in PHP8.1 : explode cannot use a null
Definition: ac_common.php:1593
mailTo($p_email)
compose a HTML string with email
Definition: ac_common.php:1615
hb($p_string)
Definition: ac_common.php:53
findSide($p_number)
return D if the number is smaller than 0 , C if bigger and an empty string if equal to 0.
Definition: ac_common.php:121
getPeriodeFromMonth($p_cn, $p_date)
Return the period corresponding to the date.
Definition: ac_common.php:633
put_global($array)
Put in superglobal (get,post,request) the value contained in the parameter field (me_parameter)
Definition: ac_common.php:1233
noalyss_strip_tags($p_string)
Definition: ac_common.php:1569
span($p_string, $p_extra='')
Definition: ac_common.php:43
html_page_stop()
end tag
Definition: ac_common.php:468
isDate($p_date)
Definition: ac_common.php:236
get_array_column($p_array, $key)
Definition: ac_common.php:1278
getPeriodeName($p_cn, $p_id, $pos='p_start')
Show the periode which found thanks its id.
Definition: ac_common.php:605
find_default_menu($pn_menu)
Find the menu marked as default in the given profile.
Definition: ac_common.php:1457
noalyss_strlentrim($p_string)
Definition: ac_common.php:1549
html_page_start($p_theme="", $p_script="", $p_script2="")
Default page header for each page.
Definition: ac_common.php:275
echo_warning($p_string)
warns
Definition: ac_common.php:589
Decode($p_html)
Decode the html for the widegt richtext and remove newline.
Definition: ac_common.php:648
ShowItem($p_array, $p_dir='V', $class="nav-item", $class_ref="nav-link", $default="", $p_extra="nav nav-pills nav-fill")
store the string which print the content of p_array in a table used to display the menu
Definition: ac_common.php:535
h1($p_string, $p_class="")
Definition: ac_common.php:72
rebuild_access_code($pan_code)
rebuild the access code
Definition: ac_common.php:1508
check_password_strength($password)
returns an double array with the error found and code , if the count is 0 then the password is very s...
Definition: ac_common.php:1700
linkTo($p_url)
Definition: ac_common.php:1649
html_min_page_start($p_theme="", $p_script="", $p_script2="")
Minimal page header for each page, used for small popup window.
Definition: ac_common.php:408
noalyss_str_replace($search, $replace, $string)
Definition: ac_common.php:1553
NoAccess($js=1)
Echo no access and stop.
Definition: ac_common.php:480
echo_error($p_log, $p_line="", $p_message="")
log error into the /tmp/noalyss_error.log it doesn't work on windows
Definition: ac_common.php:169
tr($p_string, $p_extra='')
Definition: ac_common.php:88
remove_divide_zero($p_formula)
When it is needed to eval a formula , this function prevent the divide by zero.
Definition: ac_common.php:1408
generate_random_password($car)
generate a strong random password
Definition: ac_common.php:1748
p($p_string)
Definition: ac_common.php:39
add_http_link($text)
Definition: ac_common.php:1527
record_log($p_message)
Record an error message into the log file of the server.
Definition: ac_common.php:1342
h2info($p_string)
Definition: ac_common.php:63
noalyss_bcmul($p_first, $p_second)
Definition: ac_common.php:1574
sql_string($p_string)
Fix the problem with the quote char for the database.
Definition: ac_common.php:511
set_language()
set the lang thanks the _SESSION['g_lang'] var.
Definition: ac_common.php:754
nb($p_number)
format the number for the CSV export
Definition: ac_common.php:107
td($p_string='', $p_extra='')
surround the string with td
Definition: ac_common.php:83
FaxTo($p_tel)
compose a HTML string with fax
Definition: ac_common.php:1642
nbm($p_number, $p_dec=2)
format the number with a sep.
Definition: ac_common.php:137
generate_random_string($p_length, $special=1)
Create randomly a string.
Definition: ac_common.php:1420
cmpDate($p_date, $p_date_oth)
Compare 2 dates.
Definition: ac_common.php:188
if(!function_exists( 'tracedebug')) convert_to_rtf($p_string)
encode the string for RTF, return a string
Definition: ac_common.php:1382
noalyss_trim($p_string)
Definition: ac_common.php:1545
j($p_string)
escape correctly php string to javascript
Definition: ac_common.php:96
alert($p_msg, $buffer=false)
alert in javascript
Definition: ac_common.php:738
is_msie()
Check if we use IE 8 or 9.
Definition: ac_common.php:1328
factory_Ledger(Database &$p_cn, $ledger_id)
This function create a ledger object and return the right one.
Definition: ac_common.php:1299
$href
Definition: adm.inc.php:31
if(headers_sent() &&DEBUGNOALYSS > 0) $html
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
$code
$var
Definition: ajax_card.php:64
if(!headers_sent())
– pour utiliser unoconv démarrer un server libreoffice commande libreoffice –headless –accept="socket...
$profile p_id
$anc_grandlivre from
$opd_description style
$ledger_id
$_REQUEST['ac']
if(isset( $_REQUEST[ 'show'])) if(isset($_REQUEST['del'])) $ac
$from_poste name
$input_from type
Definition: balance.inc.php:65
else $card content[$j]['j_montant']
$_GET['qcode']
$class
the class Acc_Ledger_Fin inherits from Acc_Ledger, this object permit to manage the financial ledger
Handle the ledger of purchase,.
Handle the ledger of sold,.
static fetch_array($ret, $p_indice=0, $p_mode=PGSQL_ASSOC)
wrapper for the function pg_fetch_array
static num_row($ret)
wrapper for the function pg_num_rows
contains the class for connecting to Noalyss
static connect()
static button_action($action, $javascript, $id=NULL, $p_class="button", $p_symbole="")
button Html with javascript
static hidden($p_name, $p_value, $p_id="")
static title_box($p_name, $p_div, $p_mod="close", $p_js="", $p_draggable="n", $p_enlarge='n')
Title for boxes, you can customize the symbol thanks symbol with the mode "custom".
Html Input.
Definition: itext.class.php:30
static warnbulle($p_comment)
Display a warning in a bubble, text is in message_javascript.
For the periode tables parm_periode and jrn_periode.
$all
if($request_jrn !="" && $g_user->check_jrn($request_jrn) !='W') $p_msg
const COMPTA_MIN_YEAR
Definition: constant.php:141
const COMPTA_MAX_YEAR
Definition: constant.php:140
global $version_noalyss
Definition: constant.php:26
$ix
Definition: dashboard.php:68
$_POST['ac']
Definition: do.php:310
$Res
if(sizeof($array)==0) $pPeriode
for($e=0; $e< count($afiche); $e++) exit
load_all_script()
load all the javascript only one time
escape_xml($p_xml)
When data are transfered thanks ajax in a xml document, the xml can not contains some character,...
domaine
Definition: install.php:370
vous n
Definition: modele.inc.php:398
$noalyss_appearance
$p_url