noalyss Version-9
NOALYSS : serveur de comptabilité et ERP (2002)
Loading...
Searching...
No Matches
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 <script src=\"export.php?loadjs=message\" type=\"text/javascript\" charset=\"utf-8\"></script>";
448
449 echo "<LINK id=\"pagestyle\" REL=\"stylesheet\" type=\"text/css\" href=\"css/font/fontello/css/fontello.css\" media=\"screen\"/>";
450
451 // Retrieve colors for this folder
452 if ( isset($_REQUEST['gDossier']) ) {
454 $noalyss_appearance->print_css();
455 }
456 echo '</HEAD>';
457 echo "<BODY $p_script>";
458 /* If we are on the user_login page */
459 if (basename($_SERVER['PHP_SELF']) == 'user_login.php')
460 {
461 return;
462 }
463}
464
465/**
466 * \brief end tag
467 *
468 */
469
471{
472 echo "</BODY>";
473 echo "</HTML>";
474}
475
476/**
477 * \brief Echo no access and stop
478 *
479 * \return nothing
480 */
481
482function NoAccess($js=1)
483{
484 if ($js == 1)
485 {
486 echo "<script>";
487 echo "alert ('" . _('Cette action ne vous est pas autorisée Contactez votre responsable') . "');";
488 echo "</script>";
489 }
490 else
491 {
492 echo '<div class="redcontent">';
493 echo '<h2 class="error">' . _(' Cette action ne vous est pas autorisée Contactez votre responsable') . '</h2>';
494 echo '</div>';
495 }
496 exit - 1;
497}
498/**
499 * @brief replaced by sql_string
500 * @deprecated
501 */
502function FormatString($p_string)
503{
504 return sql_string($p_string);
505}
506/**
507 * \brief Fix the problem with the quote char for the database
508 *
509 * \param $p_string
510 * \return a string which won't let strange char for the database
511 */
512
513function sql_string($p_string)
514{
515 $p_string = trim($p_string??"");
516 if (strlen($p_string) == 0)
517 return null;
518 $p_string = noalyss_str_replace("'", "''", $p_string);
519 $p_string = noalyss_str_replace('\\', '\\\\', $p_string);
520 return $p_string;
521}
522
523/**
524* \brief store the string which print
525 * the content of p_array in a table
526 * used to display the menu
527 * \param $p_array array like ( 0=>HREF reference, 1=>visible item (name),2=>Help(opt),
528 * 3=>selected (opt) 4=>javascript (normally a onclick event) (opt)
529 * \param $p_dir direction of the menu (H Horizontal V vertical)
530 * \param $class CSS for li tag
531 * \param $class_ref CSS for the A tag
532 * \param $default selected item
533 * \param $p_extra extra code for the table tag (CSS or javascript)
534 */
535 /* \return : string */
536
537function ShowItem($p_array, $p_dir='V', $class="nav-item", $class_ref="nav-link", $default="", $p_extra="nav nav-pills nav-fill")
538{
539 $ret = '';
540 // for comptability with old application mtitle for anchor is replace by nav-link
541
542
543 // direction Vertical
544 if ($p_dir == 'V')
545 {
546 $ret .= "<ul class=\"$p_extra noprint \" flex-row>";
547 } else {
548 $ret .= "<ul class=\"$p_extra noprint \" >";
549
550 }
551
552 foreach ($p_array as $all => $href)
553 {
554 $javascript = (isset($href[4])) ? $href[4] : "";
555 $title = "";
556 $set = "XX";
557 if (isset($href[2]))
558 {
559 $title=$href[2];
560 }
561 if (isset($href[3]))
562 {
563 $set=$href[3];
564 }
565
566 if ($set==$default)
567 {
568 $ret.='<li class="nav-item"><A class="'.$class_ref.' active'.'" HREF="'.$href[0].'" title="'.$title.'" '.$javascript.'>'.$href[1].'</A></li>';
569 }
570 else
571 {
572 $ret.='<li class="nav-item"><A class="'.$class_ref.'" HREF="'.$href[0].'" title="'.$title.'" '.$javascript.'>'.$href[1].'</A></li>';
573 }
574
575 }
576
577 $ret.="</ul>";
578 return $ret;
579}
580
581/**
582 * \brief warns
583 *
584 * \param p_string error message
585 * gen :
586 * - none
587 * \return:
588 * - none
589 */
590
591function echo_warning($p_string)
592{
593 echo '<span class="warning">' . $p_string . '</span>';
594}
595
596/**
597 * \brief Show the periode which found thanks its id
598 *
599 *
600 * \param $p_cn database connection
601 * \param p_id
602 * \param pos Start or end
603 *
604 * \return: string
605 */
606
607function getPeriodeName($p_cn, $p_id, $pos='p_start')
608{
609 if ($pos != 'p_start' && $pos != 'p_end')
610 {
611 echo_error('lib/ac_common.php' . "-" . __LINE__ . ' UNDEFINED PERIODE');
612 throw new Exception(_("paramètre invalide"));
613 }
614 if ( isNumber($p_id) == 0 )
615 {
616 throw new Exception("Paramètre invalide");
617 return;
618 }
619 $ret = $p_cn->get_value("select to_char($pos,'Mon YYYY') as t from parm_periode where p_id=$1",
620 array( $p_id));
621 return $ret;
622}
623
624/**
625 * \brief Return the period corresponding to the
626 * date
627 *
628 * \param p_cn database connection
629 * \param p_date the month + year 'MM.YYYY'
630 *
631 * \return:
632 * parm_periode.p_id
633 */
634
636{
637 $R = $p_cn->get_value("select p_id from parm_periode where
638 to_char(p_start,'DD.MM.YYYY') = $1", array('01.'.$p_date));
639 if ($R == "")
640 return -1;
641 return $R;
642}
643
644/**
645 * \brief Decode the html for the widegt richtext and remove newline
646 * \param $p_html string to decode
647 * \return the html code without new line
648 */
649
650function Decode($p_html)
651{
652 $p_html = noalyss_str_replace('%0D', '', $p_html);
653 $p_html = noalyss_str_replace('%0A', '', $p_html);
654 $p_html = urldecode($p_html);
655 return $p_html;
656}
657/**
658 * @brief transform the SQL for ANALYTIC table operation_analytique
659 * @see sql_filter_per
660 * @param string $p_sql
661 */
663{
664 $result=noalyss_str_replace("j_tech_per in (select p_id from parm_periode where","",$p_sql);
665 $result=noalyss_str_replace("jr_tech_per in (select p_id from parm_periode where","",$result);
666 $result=noalyss_str_replace("j_tech_per = (select p_id from parm_periode where p_start "," oa_date ",$result);
667 $result=noalyss_str_replace("p_start >= to_date","oa_date >= to_date",$result);
668 $result=noalyss_str_replace("p_end <= to_date","oa_date <= to_date",$result);
669
670 $result="( $result";
671 return $result;
672}
673/**
674 * \brief Create the condition to filter on the j_tech_per
675 * thanks a from and to date.
676 * \param $p_cn database conx
677 * \param $p_from start date (date)
678 * \param $p_to end date (date)
679 * \param $p_form if the p_from and p_to are date or p_id
680 * \param $p_field column name
681 * \return a string containg the query
682 */
683
684function sql_filter_per($p_cn, $p_from, $p_to, $p_form='p_id', $p_field='jr_tech_per')
685{
686
687 if ($p_form != 'p_id' && $p_form != 'date')
688 {
689 echo_error(__FILE__, __LINE__, 'Mauvais parametres ');
690 exit(-1);
691 }
692 $p_from= sql_string($p_from);
693 $p_to= sql_string($p_to);
694 $p_field= sql_string($p_field);
695 if ($p_form == 'p_id')
696 {
697 if ( isNUmber($p_from)==0 || isNUmber($p_to)==0){
698 throw new Exception("SFP1"._("Nombre invalide")."\$p_from=$p_from \$p_to=$p_to");
699 }
700 // retrieve the date
701 $pPeriode = new Periode($p_cn);
702 $a_start = $pPeriode->get_date_limit($p_from);
703 $a_end = $pPeriode->get_date_limit($p_to);
704 if ($a_start==null||$a_end==null)
705 {
706 throw new Exception(__FILE__.__LINE__.sprintf(_('Attention periode
707 non trouvee periode p_from= %s p_to_periode = %s'), $p_from, $p_to));
708 }
709
710
711 $p_from = $a_start['p_start'];
712 $p_to = $a_end['p_end'];
713 }else {
714 if ( isDate($p_from)==NULL || isDate($p_to)==NULL){
715 throw new Exception("SFP2"._("Date invalide")."\$p_from=$p_from \$p_to=$p_to");
716 }
717 }
718 if ($p_from==$p_to)
719 {
720 $periode=" $p_field = (select p_id from parm_periode ".
721 " where ".
722 " p_start = to_date('$p_from','DD.MM.YYYY')) ";
723 }
724 else
725 {
726 $periode="$p_field in (select p_id from parm_periode ".
727 " where p_start >= to_date('$p_from','DD.MM.YYYY') and p_end <= to_date('$p_to','DD.MM.YYYY')) ";
728 }
729 return $periode;
730}
731
732/**
733 * \brief alert in javascript
734 * \param $p_msg is the message
735 * \param $buffer if false, echo directly and execute the javascript, if $buffer is true, the alert javascript
736 * is in the return string
737 * \return string with alert javascript if $buffer is true
738 */
739
740function alert($p_msg, $buffer=false)
741{
742 $r = '<script>';
743 $r.= 'alert_box(\'' . j($p_msg) . '\')';
744 $r.= '</script>';
745
746 if ($buffer)
747 {
748 return $r;
749 }
750 echo $r;
751}
752
753/**
754 * @brief set the lang thanks the _SESSION['g_lang'] var.
755 */
756function set_language()
757{
758 // desactivate local check
759 if (defined("LOCALE")&&LOCALE==0)
760 {
761 return;
762 }
763 if (!isset($_SESSION[SESSION_KEY.'g_lang']))
764 {
765 return;
766 }
767
768 /*
769 * If translation is not supported by current
770 */
771 if (!function_exists("bindtextdomain"))
772 {
773 return;
774 }
775
776 $dir = "";
777 // set differently the language depending of the operating system
778 if (what_os() == 1)
779 {
780 $dir = setlocale(LC_MESSAGES, $_SESSION[SESSION_KEY.'g_lang']);
781 if ($dir == "")
782 {
783 $g_lang = 'fr_FR.utf8';
784 $dir = setlocale(LC_MESSAGES, $g_lang);
785 // echo '<span class="notice">' . $_SESSION[SESSION_KEY.'g_lang'] . ' domaine non supporté</h2>';
786 }
787 bindtextdomain('messages', NOALYSS_HOME.'/lang');
788 textdomain('messages');
789 bind_textdomain_codeset('messages', 'UTF8');
790
791 return;
792 }
793 // for windows
794 putenv('LANG=' . $_SESSION[SESSION_KEY.'g_lang']);
795 $dir = setlocale(LC_ALL, $_SESSION[SESSION_KEY.'g_lang']);
796 bindtextdomain('messages', '.\\lang');
797 textdomain('messages');
798 bind_textdomain_codeset('messages', 'UTF8');
799}
800
801/**
802 * @brief try to determine on what os you are running the pĥpcompte
803 * server
804 * @return
805 * 0 it is a windows
806 * 1 it is a Unix like
807 */
808function what_os()
809{
810 $inc_path = get_include_path();
811
812 if (strpos($inc_path, ";") != 0)
813 {
814 $os = 0; /* $os is 0 for windoz */
815 }
816 else
817 {
818 $os = 1; /* $os is 1 for unix */
819 }
820 return $os;
821}
822
823/**
824 * @brief shrink the date, make a date shorter for the printing
825 * @param $p_date format DD.MM.YYYY
826 * @return date in the format DDMMYY (size = 13 mm in arial 8)
827 */
828function shrink_date($p_date)
829{
830 $date = noalyss_str_replace('.', '', $p_date);
831 $str_date = substr($date, 0, 4) . substr($date, 6, 2);
832 return $str_date;
833}
834/**
835 * @brief shrink the date, make a date shorter for the printing
836 * @param $p_date format DD.MM.YYYY
837 * @return date in the format DD.MM.YY (size = 13 mm in arial 8)
838 */
839function smaller_date($p_date)
840{
841 if (empty ($p_date)) return "";
842 $str_date = substr($p_date, 0, 6) . substr($p_date, 8, 2);
843 return $str_date;
844}
845
846/**
847 * @brief format the date, when taken from the database the format
848 * is MM-DD-YYYY
849 * @param $p_date format
850 * @exception 1 if invalid format
851 * DOMEntity@param
852 * @return date in the format DD.MM.YYYY
853 */
854function format_date($p_date, $p_from_format = 'YYYY-MM-DD',$p_to_format='DD.MM.YYYY')
855{
856 if (empty($p_date)) {return $p_date;}
857 if ($p_from_format == 'YYYY-MM-DD')
858 {
859 $date = explode('-', $p_date);
860 if (count($date) != 3)
861 return $p_date;
862 }
863 if ($p_from_format == 'DD.MM.YYYY')
864 {
865 $temp_date = explode('.', $p_date);
866 if (count($temp_date) != 3)
867 return $p_date;
868 $date[0] = $temp_date[2]; // 0 is year
869 $date[1] = $temp_date[1]; // 1 for month
870 $date[2] = $temp_date[0]; // 2 for day
871 }
872
873 switch ($p_to_format)
874 {
875 case 'DD.MM.YYYY':
876 $str_date = $date[2] . '.' . $date[1] . '.' . $date[0];
877 break;
878 case 'DD-MM-YYYY':
879 $str_date = $date[2] . '-' . $date[1] . '-' . $date[0];
880 break;
881 case 'YYYY-MM-DD':
882 $str_date = $date[0] . '-' . $date[1] . '-' . $date[2];
883 break;
884 case 'YYYYMMDD':
885 $str_date = $date[0] . $date[1] . $date[2];
886 break;
887 case 'YYYY/MM/DD':
888 $str_date = $date[0] . '/' . $date[1] . '/' . $date[2];
889 break;
890 case "DD.MM.YY":
891 $str_date = $date[2] . '.' . $date[1] . '.' . substr($date[0],2,2);
892 break;
893 case "DD-MM-YY":
894 $str_date = $date[2] . '-' . $date[1] . '-' . substr($date[0],2,2);
895 break;
896 default:
897 throw new Exception(_("Format Invalide"),1);
898
899 }
900 return $str_date;
901}
902
903
904
905/**
906 *@brief Should a dialog box when you are disconnected from an ajax call
907 * propose to reload or to connect in another tab
908 */
909function ajax_disconnected($p_div)
910{
911
912 echo HtmlInput::title_box(_("Déconnecté"), $p_div);
913 echo h2(_('Données non disponibles'), 'class="error" ');
914 echo h2(_('Veuillez vous reconnecter soit dans une autre fenêtre soit '
915 . ' en cliquant sur le bouton'), 'class="error"');
916 // Reload button
917 $reload=new IButton("reload");
918 $reload->value=_("Se connecter");
919 $reload->class="button";
920 $reload->javascript='window.location.reload()';
921 // Link to log in another tab
922 echo '<p style="text-align:center">';
923 echo $reload->input();
924 echo HtmlInput::button_close($p_div,'button');
925 echo '</p>';
926
927
928}
929
930/**
931 * @brief Show the modules
932 * @param int $selected module selected profile_menu.pm_id
933 */
934function show_module($selected)
935{
936 global $g_user;
937 $cn = Dossier::connect();
938 $amodule = $cn->get_array("select
939 me_code,me_menu,me_url,me_javascript,p_order,me_type,me_description
940 from v_all_menu
941 where
942 p_id=$1
943 and p_type_display='M'
944 order by p_order", array($g_user->get_profile()));
945
946 if ($selected != -1)
947 {
948 $selected_module=$cn->get_value('select me_code from profile_menu where'
949 . ' pm_id = $1 ', array($selected));
950 require_once NOALYSS_TEMPLATE.'/module.php';
951 $file = $cn->get_array("select me_file,me_parameter,me_javascript,me_type,me_description from v_all_menu
952 where pm_id=$1 and p_id=$2", array($selected,$g_user->get_profile()));
953 if ( count($file ) == 0 )
954 {
955 echo '</div>';
956 echo '</div>';
957 echo '<div class="content">';
958 echo_warning(_("Module inexistant")."[ $selected ] ");
959 echo '</div>';
960 exit();
961 }
962 if ($file[0]['me_file'] != '')
963 {
964 if ($file[0]['me_parameter'] != "")
965 {
966 // if there are paramter put them in superglobal
967 $array=compute_variable($file[0]['me_parameter']);
968 put_global($array);
969 }
970
971 // if file is not a plugin, include the file, otherwise
972 // include the plugin launcher
973 if ($file[0]['me_type'] != 'PL')
974 {
975 require_once $file[0]['me_file'];
976 }
977 else
978 {
979 // nothing : direct call to plugin
980 }
981 }
982 if ( $file[0]['me_javascript'] != '')
983 {
984 create_script($file[0]['me_javascript']);
985 }
986 }
987}
988/**
989 * @brief Find the default module or the first one
990 * @var $g_user $g_user
991 * @return default module (string)
992 */
993function find_default_module()
994{
995 global $g_user;
996 $cn = Dossier::connect();
997
998 $default_module = $cn->get_array("select me_code
999 from profile_menu join profile_user using (p_id)
1000 where
1001 p_type_display='M' and
1002 user_name=$1 and pm_default=1", array($g_user->login));
1003
1004 /*
1005 * Try to find the smallest order for module
1006 */
1007 if (empty($default_module))
1008 {
1009 $default_module = $cn->get_array("select me_code
1010 from profile_menu join profile_user using (p_id)
1011 where
1012 p_type_display='M' and
1013 user_name=$1 order by p_order limit 1", array($g_user->login));
1014
1015 // if no default try to find the default menu
1016 if ( empty ($default_module))
1017 {
1018 $default_module = $cn->get_array("select me_code
1019 from profile_menu join profile_user using (p_id)
1020 where
1021 p_type_display='E' and
1022 user_name=$1 and pm_default=1 ", array($g_user->login));
1023 /*
1024 * Try to find a default menu by order
1025 */
1026 if (empty ($default_module))
1027 {
1028 $default_module = $cn->get_array("select me_code
1029 from profile_menu join profile_user using (p_id)
1030 where
1031 user_name=$1 and p_order=(select min(p_order) from profile_menu join profile_user using (p_id)
1032 where user_name=$2) limit 1", array($g_user->login, $g_user->login));
1033 }
1034
1035 /*
1036 * if nothing found, there is no profile for this user => exit
1037 */
1038 if (empty ($default_module))
1039 {
1040 /*
1041 * If administrateur, then we insert a default profile (1)
1042 * for him
1043 */
1044 if ( $g_user->admin == 1 )
1045 {
1046 $cn->exec_sql('insert into profile_user(user_name,p_id) values ($1,1) ',array($g_user->login));
1047 return find_default_module();
1048 }
1049 echo_warning(_("Utilisateur n'a pas de profil, votre administrateur doit en configurer un dans C0SEC"));
1050 exit();
1051 }
1052 }
1053 return $default_module[0]['me_code'];
1054 }
1055
1056 if (count($default_module) > 1)
1057 {
1058 // return the first module found
1059 return $default_module[0]['me_code'];
1060 }
1061 elseif (count($default_module) == 1)
1062 {
1063 return $default_module[0]['me_code'];
1064 }
1065}
1066
1067/**
1068 * @brief show the module
1069 * @var $g_user
1070 * @param $module the $_REQUEST['ac'] exploded into an array
1071 * @param $idx the index of the array : the AD code is splitted into an array thanks the slash
1072 */
1073function show_menu($module)
1074{
1075 if ($module == 0)return;
1076 static $level=0;
1077 global $g_user;
1078 $http=new HttpInput();
1079 $access_code=$http->request("ac");
1080 $cn = Dossier::connect();
1081 /**
1082 * Show the submenus
1083 */
1084 $amenu = $cn->get_array("
1085 select
1086 pm_id,
1087 me_code,
1088 pm_id_dep,
1089 me_file,
1090 me_javascript,
1091 me_url,
1092 me_menu,
1093 me_description,
1094 me_description_etendue
1095 from profile_menu
1096 join menu_ref using (me_code)
1097 where pm_id_dep=$1 and p_id=$2
1098 order by p_order", array($module, $g_user->get_profile()));
1099
1100 // There are submenuS, so show them
1101 if (!empty($amenu) && count($amenu) > 1)
1102 {
1103 $a_style_menu=array('topmenu','menu2','menu3');
1104 if ( $level > count($a_style_menu))
1105 $style_menu='menu3';
1106 else {
1107 $style_menu=$a_style_menu[$level];
1108 }
1109 require NOALYSS_TEMPLATE.'/menu.php';
1110 $level++;
1111 return;
1112 } elseif (count($amenu) == 1)
1113 {
1114 // there is only one submenu so we include the code or javascript
1115 // or we show the submenu
1116 if ( trim($amenu[0]['me_url']??"") != "" ||
1117 trim ($amenu[0]['me_file']??"") != "" ||
1118 trim ($amenu[0]['me_javascript']??"") != "" )
1119 {
1120 echo '<div class="topmenu">';
1121 echo h2info(_($amenu[0]['me_menu']));
1122 echo '</div>';
1123 $module = $amenu[0]['pm_id'];
1124 display_menu($module);
1125 $level++;
1126 return;
1127 } else {
1128 $url=$access_code.'/'.$amenu[0]['me_code'];
1129 echo '<a href="do.php?gDossier='.Dossier::id().'&ac='.$url.'">';
1130 echo _($amenu[0]['me_menu']);
1131 echo '</a>';
1132 $level++;
1133 return;
1134 }
1135 }
1136
1137 // !!! this point should never be reached
1138 // There is no submenu or only one
1139 if (empty($amenu) || count($amenu) == 1)
1140 {
1141 display_menu($module);
1142
1143 }
1144 $level++;
1145}
1146/**
1147 * @brief Display a menu
1148 * @global type $g_user
1149 * @param type $p_menuid
1150 * @return type
1151 */
1152function display_menu($p_menuid)
1153{
1154 if ($p_menuid == 0) return;
1155 global $g_user;
1156 $cn=Dossier::connect();
1157
1158 $file = $cn->get_array("
1159 select me_file,me_parameter,me_javascript,me_type
1160 from menu_ref
1161 join profile_menu using (me_code)
1162 join profile_user using (p_id)
1163 where
1164 pm_id=$1 and
1165 user_name=$2 and
1166 (me_file is not null or trim(me_file) <>'' or
1167 me_javascript is not null or trim (me_javascript) <> '')", array($p_menuid,$g_user->login));
1168
1169 if (count($file)==0)
1170 {
1171 return;
1172 }
1173
1174 if ($file[0]['me_file'] != "")
1175 {
1176 if ($file[0]['me_parameter'] !== "")
1177 {
1178 // if there are paramter put them in superglobal
1179 $array=compute_variable($file[0]['me_parameter']);
1180 put_global($array);
1181 }
1182 \Noalyss\Dbg::echo_var(1,$file[0]['me_file']." ".$file[0]['me_parameter']);
1183 /*
1184 * Log the file we input to put in the folder test-noalyss for replaying it
1185 */
1186 if (LOGINPUT) {
1187 $file_loginput=fopen($_ENV['TMP'].'/scenario-'.$_SERVER['REQUEST_TIME'].'.php','a+');
1188 fwrite($file_loginput, "include '".$file[0]['me_file']."';");
1189 fwrite($file_loginput,"\n");
1190 fclose($file_loginput);
1191 }
1192 // if file is not a plugin, include the file, otherwise
1193 // include the plugin launcher
1194 if ( $file[0]['me_type'] != 'PL') {
1195 if (file_exists ($file[0]['me_file']) )
1196 {
1197 require_once $file[0]['me_file'];
1198 } elseif ( file_exists(NOALYSS_INCLUDE.'/'.$file[0]['me_file'])) {
1199 require_once NOALYSS_INCLUDE.'/'.$file[0]['me_file'];
1200 }else {
1201 echo echo_warning(_("Fichier non trouvé"));
1202 }
1203 } else {
1204 require 'extension_get.inc.php';
1205 }
1206
1207 exit();
1208 } elseif ( $file[0]['me_javascript'] != '')
1209 {
1210 $js= noalyss_str_replace('<DOSSIER>', dossier::id(), $file[0]['me_javascript']);
1211 echo create_script($js);
1212 }
1213
1214}
1215/**
1216 * @brief Send an header CSV with a filename
1217 * @param string $p_filename , file name , caution , it must be sanitized BEFORE calling this function
1218 */
1219function header_csv($p_filename)
1220{
1221
1222 header('Pragma: public');
1223 header('Content-type: application/csv');
1224 header("Content-Disposition: attachment;filename=\"{$p_filename}\"",
1225 FALSE);
1226 header('Cache-Control: no-store, no-cache, must-revalidate');
1227 header('Expires: Sun, 1 Jan 2000 12:00:00 GMT');
1228 header('Last-Modified: '.gmdate('D, d M Y H:i:s').'GMT');
1229}
1230/**
1231 * @brief Put in superglobal (get,post,request) the value contained in
1232 * the parameter field (me_parameter)
1233 * @param $array [key] [value]
1234 */
1236{
1237 for ($i=0;$i<count($array);$i++)
1238 {
1239 $key=$array[$i]['key'];
1240 $value=$array[$i]['value'];
1241 $_GET[$key]=$value;
1242 $_POST[$key]=$value;
1243 $_REQUEST[$key]=$value;
1244 }
1245}
1246/**
1247 * @brief the string has the format a=b&c=d, it is parsed and an array[][key,value]
1248 * is returned
1249 * @param $p_string
1250 * @return $array usable in put_global
1251 */
1252function compute_variable($p_string)
1253{
1254 $array=array();
1255 if ($p_string == '') return $array;
1256
1257 $var=explode("&",$p_string);
1258 if (empty ($var)) return $array;
1259 for ($i=0;$i < count($var);$i++)
1260 {
1261 $var2=explode('=',$var[$i]);
1262 $array[$i]['key']=$var2[0];
1263 $array[$i]['value']=$var2[1];
1264 }
1265 return $array;
1266}
1267function ajax_xml_error($p_code,$p_string)
1268{
1269 $html = escape_xml($p_string);
1270 header('Content-type: text/xml; charset=UTF-8');
1271 echo <<<EOF
1272<?xml version="1.0" encoding="UTF-8"?>
1273<data>
1274<code>$p_code</code>
1275<value>$html</value>
1276</data>
1277EOF;
1278}
1279
1281{
1282 $array=array();
1283 for ($i=0;$i<count($p_array);$i++)
1284 {
1285 $r=$p_array[$i];
1286 if ( isset($r[$key])) {
1287 $array[]=$r[$key];
1288 }
1289 }
1290 return $array;
1291}
1292
1293/**
1294 * @brief This function create a ledger object and return the right one.
1295 * It uses the factory pattern
1296 * @param Database $p_cn
1297 * @param type $ledger_id
1298 * @return Acc_Ledger
1299 * @throws Exception
1300 */
1302{
1303 $ledger=new Acc_Ledger($p_cn, $ledger_id);
1304 $type=$ledger->get_type();
1305
1306 switch ($type)
1307 {
1308 case 'VEN':
1309 $obj=new Acc_Ledger_Sale($p_cn, $ledger_id);
1310 break;
1311 case 'ACH':
1313 break;
1314 case 'FIN':
1315 $obj= new Acc_Ledger_Fin($p_cn, $ledger_id);
1316 break;
1317 case 'ODS':
1318 $obj=$ledger;
1319 break;
1320
1321 default:
1322 throw new Exception('Ledger type not found');
1323 }
1324 return $obj;
1325}
1326/**
1327 * @brief Check if we use IE 8 or 9
1328 * @return int 1 for IE8-9;0 otherwise
1329 */
1330function is_msie()
1331{
1332 if ( strpos ($_SERVER['HTTP_USER_AGENT'],'MSIE 8.0') != 0 ||
1333 strpos ($_SERVER['HTTP_USER_AGENT'],'MSIE 9.0') != 0 )
1334 $is_msie=1;
1335 else
1336 $is_msie=0;
1337 return $is_msie;
1338}
1339/**
1340 *@brief Record an error message into the log file of the server or in the log folder of NOALYSS
1341 * Record also the GET and POST data
1342 * @param $p_message string message to display
1343 */
1344function record_log($p_message)
1345{
1346 $date= date ('Y-m-d');
1347 // variable: $handle_log resource on log file ,
1348 $handle_log=fopen(NOALYSS_BASE."/log/noalyss-{$date}.log","a+");
1349
1350 if ($handle_log == false )
1351 {
1352
1353 if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) {
1354 $exc=$p_message;
1355 do {
1356 error_log("noalyss exception File [".$exc->getFile().":".$exc->getLine()."]",0);
1357 error_log("noalyss exception Message [".$exc->getMessage()."]",0);
1358 error_log("noalyss exception Code [".$exc->getCode()."]",0);
1359 error_log("noalyss exception Trace ".$exc->getTraceAsString(),0);
1360 error_log("------ ",0);
1361 $exc=$exc->getPrevious();
1362 if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n");
1363 } while ($exc != null);
1364 } else {
1365 error_log("noalyss".var_export($p_message,true),0);
1366
1367 }
1368 error_log("noalyss GET [".json_encode($_GET,0,10)."]");
1369 error_log("_POST [".json_encode($_POST,0,10)."]",0);
1370 } else {
1371 if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) {
1372
1373 error_log("noalyss exception ".$p_message->getMessage(),0);
1374 error_log("noalyss exception".$p_message->getTraceAsString(),0);
1375 } else {
1376 error_log("noalyss".var_export($p_message,true),0);
1377
1378 }
1379
1380 $now=date ('Y-m-d H:i:s');
1381 fwrite ($handle_log,str_repeat("=", 80)."\n");
1382 fwrite ($handle_log,"ERROR: {$now}\n");
1383 fwrite($handle_log,"noalyss GET [".var_export($_GET,true)."]");
1384 fwrite ($handle_log,"\n");
1385 fwrite($handle_log,"_POST [".var_export($_POST,true)."]");
1386 fwrite ($handle_log,"\n");
1387 if ( gettype ($p_message) == "object" && method_exists($p_message,"getTraceAsString") == 1) {
1388 $exc=$p_message;
1389 do {
1390 fwrite($handle_log,"noalyss exception File [".$exc->getFile().":".$exc->getLine()."]");
1391 fwrite ($handle_log,"\n");
1392 fwrite($handle_log,"noalyss exception Message [".$exc->getMessage()."]");
1393 fwrite ($handle_log,"\n");
1394 fwrite($handle_log,"noalyss exception Code [".$exc->getCode()."]");
1395 fwrite ($handle_log,"\n");
1396 fwrite($handle_log,"noalyss exception Trace \n".$exc->getTraceAsString());
1397 fwrite ($handle_log,"\n");
1398 $exc=$exc->getPrevious();
1399 if ($exc != null )fwrite ($handle_log,"*********************** Previous *********************** \n");
1400 } while ($exc != null);
1401 } else {
1402 fwrite($handle_log,"noalyss".var_export($p_message,true));
1403 fwrite ($handle_log,"\n");
1404
1405 }
1406
1407 fwrite ($handle_log,str_repeat("=", 80)."\n");
1408 fclose($handle_log);
1409
1410 }
1411
1412}
1413if(!function_exists('tracedebug')) {
1414 function tracedebug($file,$var, $label = NULL) {
1415
1416 $tmp_file = sys_get_temp_dir().DIRECTORY_SEPARATOR.$file;
1417 $file_loginput=fopen( $tmp_file,'a+');
1418 if ( $file_loginput == false) { return;}
1419 $output = '';
1420 $output .= date('d-m-y H:i');
1421 if(!is_null($label)) {
1422 $output .= $label . ': ';
1423 }
1424 if ( gettype ($var) == 'object' && get_class($var)=='DOMDocument')
1425 {
1426 $var->formatOutput=true;
1427 $output.=$var->saveXML() .PHP_EOL;
1428 } else
1429 {
1430 $output .= print_r($var, 1) . PHP_EOL;
1431 }
1432
1433 file_put_contents($tmp_file, $output, FILE_APPEND);
1434 }
1435}
1436/**
1437 * @brief encode the string for RTF, return a string
1438 * @param $p_string string to convert
1439 * @return string
1440 */
1441function convert_to_rtf($p_string)
1442{
1443 $result="";
1444 $p_string2=mb_convert_encoding($p_string,'ISO-8859-1','UTF-8');
1445 $p_string2=iconv('UTF-8','ISO-8859-1//IGNORE',$p_string);
1446
1447
1448 $nb_result=strlen($p_string2);
1449 for ($i = 0 ; $i < $nb_result ; $i++ ){
1450 if (ord($p_string[$i]) < 127 ) {
1451 $result.=$p_string[$i];
1452 } else {
1453 $result.='\u'.ord($p_string[$i]).chr(92).chr(39).'3f';
1454 }
1455 }
1456 return $result;
1457}
1458/**
1459 * @brief When it is needed to eval a formula , this function prevent the divide by zero.
1460 * the formula is a math operation to evaluate like : 1.0+2.0/1 (...) , it is used in "report",
1461 * it removes the operation "divide by 0 "
1462 *
1463 * @param string $p_formula string containing a operation to evaluate
1464 *
1465 * @see Impress::parse_formula
1466 */
1467function remove_divide_zero($p_formula)
1468{
1469 $test=noalyss_str_replace(" ","",$p_formula).";";
1470 $p_formula=preg_replace("![0-9]+\.*[0-9]*/0\.{0,1}0*(\+|-|\*|/|;){1}!","0$1",$test);
1471 $p_formula=trim($p_formula,';');
1472 return $p_formula;
1473}
1474
1475/**
1476 * @brief Create randomly a string
1477 * @param int $p_length length of the generate string
1478 */
1479function generate_random_string($p_length,$special=1)
1480{
1481 $string="";
1482 if ($special == 1)
1483 $chaine="abcdefghijklmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY0123456789*/+-=";
1484 if ($special == 0)
1485 $chaine="abcdefghijklmnpqrstuvwxyABCDEFGHIJKLMNPQRSTUVWXY0123456789";
1486 $microtime=microtime(true)*microtime(true)*100;
1487 srand(0);
1488 srand((int)$microtime);
1489 for ($i=0; $i<$p_length; $i++)
1490 {
1491 $string .= $chaine[rand()%strlen($chaine)];
1492 }
1493 return $string;
1494}
1495
1496/**
1497 *@brief generate a string of p_car character and a input text with name p_ctl_name
1498 * work like a kind of captcha.The control code for checking is ctlcode.
1499 * You compare the content of the variable p_ctl_name with ctlcode
1500 * @param $p_ctl_name name of the HTML input text
1501 * @param $p_car length of the string
1502 */
1503function confirm_with_string($p_ctl_name,$p_car)
1504{
1506 $r = HtmlInput::hidden("ctlcode",$code);
1507 $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>';
1508 $ctl=new IText($p_ctl_name);
1509 $r.=$ctl->input();
1510 return $r;
1511}
1512/**
1513 * @brief Find the menu marked as default in the given profile
1514 * @param number $pn_menu (profile_menu.id)
1515 */
1516function find_default_menu($pn_menu)
1517{
1518 $cn=Dossier::connect();
1519 $sql = ' select pm_id from profile_menu where pm_default =1 and pm_id_dep = $1';
1520 $aresult=$cn->get_array($sql, [$pn_menu]);
1521 if (empty($aresult)) {
1522 return 0;
1523 }
1524 return $aresult[0]['pm_id'];
1525}
1526
1527/**
1528 * @brief Check if there is a default menu for this user and add it. The array is filling from 1 to 3
1529 * @verbatim
1530 *
1531 * COMPTA 0 - 0 - 173
1532 * COMPTA/MENUACH 0 - 173 - 3
1533 * COMPTA/MENUACH/ACH 173 - 3 - 85
1534 *
1535 * @endverbatim
1536 *
1537 *
1538 * @param array $pa_menu if the array of option ; index pm_id_v1 , pm_id_v2 and pm_id_v3
1539 *
1540 */
1541function complete_default_menu($pa_menu)
1542{
1543 $a_result=$pa_menu;
1544 // find the first one which is null
1545 if ($pa_menu[0]['pm_id_v2'] == 0) {
1546 $tmp=find_default_menu($pa_menu[0]['pm_id_v1']);
1547 if ( $tmp <> 0 ) {
1548 $a_result[0]['pm_id_v2']=$pa_menu[0]['pm_id_v1'];
1549 $a_result[0]['pm_id_v1']=$tmp;
1550 }
1551 }
1552 if ($pa_menu[0]['pm_id_v3'] == 0) {
1553 $tmp=find_default_menu($a_result[0]['pm_id_v1']);
1554 if ( $tmp <> 0 ) {
1555 $a_result[0]['pm_id_v3']=$a_result[0]['pm_id_v2'];
1556 $a_result[0]['pm_id_v2']=$a_result[0]['pm_id_v1'];
1557 $a_result[0]['pm_id_v1']=$tmp;
1558 }
1559 }
1560 return $a_result;
1561}
1562/**
1563 * @brief rebuild the access code
1564 * @see complete_default_menu
1565 * @param array of number $pan_code index row [0] = index pm_id_v1 , pm_id_v2 and pm_id_v3
1566 */
1567function rebuild_access_code($pan_code)
1568{
1569 if ( empty ($pan_code)) {return;}
1570 $s_result="";
1571 $cn=Dossier::connect();
1572 $an_code=['pm_id_v3','pm_id_v2','pm_id_v1'];
1573 $sep="";
1574 for ($i=0;$i<3;$i++)
1575 {
1576 $ix=$an_code[$i];
1577 $s_result.=$sep.$cn->get_value("select me_code from profile_menu where pm_id=$1",[ $pan_code[0][$ix] ]);
1578 $sep=($s_result != "" )?"/":"";
1579 }
1580 return $s_result;
1581}
1582
1583/***
1584 *@brief Transform a http link into a clickable link
1585 */
1587{
1588
1589 $ret=preg_replace("!http[s]{0,1}://[[:graph:]*]*!",'<a href="\\0" target="_blank">\0</a>',$text);
1590
1591 return $ret;
1592
1593}
1594//---------------------------------------------------------------------------------------------------------------------
1595//
1596// PHP 8.2 fails with these functions when a NULL is given as argument
1597//
1598//---------------------------------------------------------------------------------------------------------------------
1599
1600function noalyss_strlen($p_string) :int{
1601 if ($p_string ==null ) return 0;
1602 else return strlen($p_string);
1603}
1604function noalyss_trim($p_string) {
1605 if ($p_string===null) return "";
1606 else return trim($p_string);
1607}
1608function noalyss_strlentrim($p_string) :int {
1609 if ($p_string ==null ) return 0;
1610 return strlen(trim($p_string));
1611}
1612function noalyss_str_replace($search,$replace,$string) {
1613 if ($string===null) return "";
1614 else return str_replace($search,$replace??"",$string);
1615}
1616function noalyss_bcsub($p_first,$p_second,$p_decimal=4)
1617{
1618 $p_first=(empty($p_first))?0:$p_first;
1619 $p_second=(empty($p_second))?0:$p_second;
1620 return bcsub($p_first,$p_second,$p_decimal);
1621}
1622function noalyss_bcadd($p_first,$p_second,$p_decimal=4)
1623{
1624 $p_first=(empty($p_first))?0:$p_first;
1625 $p_second=(empty($p_second))?0:$p_second;
1626 return bcadd($p_first,$p_second,$p_decimal);
1627}
1628function noalyss_strip_tags($p_string)
1629{
1630 if ($p_string===null) return "";
1631 return strip_tags($p_string);
1632}
1633function noalyss_bcmul($p_first,$p_second)
1634{
1635 $p_first=(empty($p_first))?0:$p_first;
1636 $p_second=(empty($p_second))?0:$p_second;
1637 return bcmul($p_first??0,$p_second??0);
1638}
1639function noalyss_round($p_first,$p_second)
1640{
1641 $p_first=(empty($p_first))?0:$p_first;
1642 $p_second=(empty($p_second))?0:$p_second;
1643 return round($p_first??0,$p_second??0);
1644}
1645
1646/**
1647 * @brief to avoid deprecated in PHP8.1 : explode cannot use a null
1648 * @param $separator
1649 * @param $string
1650 * @return array | empty string
1651 */
1652function noalyss_explode($separator,$string) {
1653 if ($string===null) return '';
1654 return explode($separator,$string);
1655}
1656/**
1657 * @brief compose a HTML string with phone
1658 * @param string $p_tel
1659 * @return false|string returns false if $p_tel is empty
1660 */
1661function phoneTo($p_tel) {
1662 if (!empty($p_tel)) {
1663 $r=sprintf('<a href="tel:%s">%s</a>',h($p_tel),h($p_tel));
1664 return $r;
1665 }
1666 return false;
1667}
1668
1669/**
1670 * @brief compose a HTML string with email
1671 * @param string $p_email email or emails separated by a comma
1672 * @return false|string returns false if email not valide
1673 */
1674function mailTo($p_email) {
1675 if (empty($p_email )) return "";
1676 $nComma=preg_match("/,/",$p_email);
1677 if ( $nComma > 0) {
1678 $aEmail=explode(",",$p_email);
1679 } else {
1680 $aEmail[0]=$p_email;
1681 }
1682 $r="";
1683 foreach ($aEmail as $email) {
1684 if ( filter_var(trim($email),FILTER_VALIDATE_EMAIL) ) {
1685 $r.=sprintf('<a href="mailto:%s">%s</a> ',h($email),h($email));
1686 } else {
1687 $r.=sprintf("%s",h($email));
1689
1690 }
1691
1692 }
1693 return $r;
1694}
1695
1696/**
1697 * @brief compose a HTML string with fax
1698 * @param string $p_fax fax number
1699 * @return false|string returns false if $p_fax is empty
1700 */
1701function FaxTo($p_tel) {
1702 if (!empty($p_tel)) {
1703 $r=sprintf('<a href="fax:%s">%s</a>',h($p_tel),h($p_tel));
1704 return $r;
1705 }
1706 return false;
1707}
1709{
1710 if (empty($p_url) ) return "";
1711 if (filter_var(trim($p_url,FILTER_VALIDATE_URL)) ) {
1712 return sprintf('<a href="%s" target="_blank">%s</a>',$p_url,$p_url);
1713 } else {
1714 return $p_url;
1715 }
1716}
1717/**
1718 * @brief When you want to prevent users to connect, create a file in noalyss/ (NOALYSS_BASE) with the
1719 * message in Html
1720 * @param string $p_file file in NOALYSS_BASE
1721 */
1722function MaintenanceMode($p_file)
1723{
1724 if ( file_exists(NOALYSS_BASE."/".$p_file )) {
1725 include NOALYSS_BASE."/".$p_file;
1726 exit;
1727 }
1728}
1729
1730/**
1731 * @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
1732 * empty ,4 weak, ... the array contains the errors, [msg]=>array message [code] => array of code
1733 * Codes are
1734 * - 1 : too short
1735 * - 2 : missing digit
1736 * - 3 : missing lowercase letter
1737 * - 4 : missing uppercase letter
1738 * - 5 : too many time same letter or symbol..
1739 * - 6 : missing special char
1740 *
1741 * If the password is strong returns an empty array
1742 *
1743 * @param $password string
1744 * @code
1745
1746 $error = check_password_strength($password);
1747 if ( count($error['msg']) > 0 ) {
1748 echo "password to weak";
1749 foreach ($error['msg'] as $item_error) {
1750 echo "error $item_error";
1751 }
1752
1753 } else {
1754 echo "OK password strong";
1755 }
1756
1757 * @endcode
1758 */
1759function check_password_strength($password) {
1760 $errors=array();
1761 $error_code=array();
1762
1763 $len=strlen($password??"");
1764 if ( $len < 8) {
1765 $errors[] = _("mot de passe de 8 lettres minimum");
1766 $error_code[]=1;
1767 }
1768
1769 if (!preg_match("#[0-9]+#", $password)) {
1770 $errors[] = _("mot de passe doit inclure au moins un chiffre");
1771 $error_code[]=2;
1772 }
1773
1774 if (!preg_match("#[a-z]+#", $password)) {
1775 $errors[] = _("mot de passe doit inclure au moins une minuscule");
1776 $error_code[]=3;
1777 }
1778 if (!preg_match("#[A-Z]+#", $password)) {
1779 $errors[] = _("mot de passe doit inclure au moins une majuscule");
1780 $error_code[]=4;
1781 }
1782
1783 if ( $len > 0 ) {
1784 $cnt_diff=count(count_chars($password,1));
1785 $ratio_diff=$len/$cnt_diff;
1786
1787 if ($ratio_diff > 2) {
1788 $errors[] = _("Trop souvent le(s) même(s) symbole(s)");
1789 $error_code[]=5;
1790 }
1791 $special_char=preg_replace('/[[:alnum:]]/','',$password);
1792 if ( strlen($special_char??"")==0)
1793 {
1794 $errors[] = _("mot de passe doit inclure au moins un caractére spécial '+-/*[...'");
1795 $error_code[]=6;
1796
1797 }
1798 }
1799
1800 return array( 'msg'=>$errors, 'code'=>$error_code);
1801}
1802/**
1803 * @brief generate a strong random password
1804 * @param $car int length of the password, minimum 8
1805 *
1806 */
1807function generate_random_password($car):string
1808{
1809 $string="";
1810 $car=($car < 8 )?8:$car;
1811 $max_loop=20;$loop=0;
1812 do
1813 {
1814 $loop++;
1815 $string="";
1816 $chaine="abcdefghijklmnpqrstuvwxy";
1817 // srand( (int)microtime()*1020030);
1818 for ($i=0; $i<$car; $i++)
1819 {
1820 $string .= $chaine[rand()%strlen($chaine)];
1821 }
1822 $chaine="ABCDEFGHIJKLMNPQRSTUVWXY";
1823 for ($i=0;$i<2;$i++) {
1824 $string[rand()%$car]=$chaine[rand()%strlen($chaine)];;
1825 }
1826 $chaine="0123456789";
1827 for ($i=0;$i<2;$i++) {
1828 $string[rand()%$car]=$chaine[rand()%strlen($chaine)];;
1829 }
1830 $special_set="+-/*;,.=:&()[]";
1831 $special_car=$special_set[rand()%strlen($special_set)];
1832 $string[rand()%$car]=$special_car;
1833 // echo $string."\n";
1834 }while ( count(check_password_strength($string)['msg'])> 0 && $loop<$max_loop);
1835 return $string;
1836}
1837
1838/**
1839 * @brief removed invalid character when computing a filename, the suffix is kept
1840 * @param $filename String filename to sanitize
1841 * @return string without offending char
1842 */
1843function sanitize_filename($filename)
1844{
1845 // save the suffix
1846 $pos_prefix=strrpos($filename, ".");
1847 if ($pos_prefix==0)
1848 {
1849 $filename_suff=".pdf";
1850 $filename.=$filename_suff;
1851 $pos_prefix=strrpos($filename, ".");
1852 }
1853 else
1854 $filename_suff=substr($filename, $pos_prefix, strlen($filename));
1855
1856 $filename=str_replace(array('/', '*', '<', '>', ';', ',', '\\', '.', ':', '(', ')', ' ', '[', ']'), "-", $filename);
1857
1858 $filename_no=substr($filename, 0, $pos_prefix);
1859
1860 $new_filename=strtolower($filename_no)."-".date("Ymd-Hi").$filename_suff;
1861 return $new_filename;
1862}
noalyss_bcsub($p_first, $p_second, $p_decimal=4)
noalyss_round($p_first, $p_second)
noalyss_bcadd($p_first, $p_second, $p_decimal=4)
ajax_xml_error($p_code, $p_string)
transform_sql_filter_per($p_sql)
transform the SQL for ANALYTIC table operation_analytique
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
FormatString($p_string)
replaced by sql_string
isNumber($p_int)
complete_default_menu($pa_menu)
Check if there is a default menu for this user and add it.
compute_variable($p_string)
the string has the format a=b&c=d, it is parsed and an array[][key,value] is returned
noalyss_strlen($p_string)
phoneTo($p_tel)
compose a HTML string with phone
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 ...
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.
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...
noalyss_explode($separator, $string)
to avoid deprecated in PHP8.1 : explode cannot use a null
mailTo($p_email)
compose a HTML string with email
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.
getPeriodeFromMonth($p_cn, $p_date)
Return the period corresponding to the date.
put_global($array)
Put in superglobal (get,post,request) the value contained in the parameter field (me_parameter)
noalyss_strip_tags($p_string)
span($p_string, $p_extra='')
Definition ac_common.php:43
html_page_stop()
end tag
isDate($p_date)
get_array_column($p_array, $key)
getPeriodeName($p_cn, $p_id, $pos='p_start')
Show the periode which found thanks its id.
find_default_menu($pn_menu)
Find the menu marked as default in the given profile.
noalyss_strlentrim($p_string)
html_page_start($p_theme="", $p_script="", $p_script2="")
Default page header for each page.
echo_warning($p_string)
warns
Decode($p_html)
Decode the html for the widegt richtext and remove newline.
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
h1($p_string, $p_class="")
Definition ac_common.php:72
rebuild_access_code($pan_code)
rebuild the access code
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...
linkTo($p_url)
html_min_page_start($p_theme="", $p_script="", $p_script2="")
Minimal page header for each page, used for small popup window.
noalyss_str_replace($search, $replace, $string)
NoAccess($js=1)
Echo no access and stop.
echo_error($p_log, $p_line="", $p_message="")
log error into the /tmp/noalyss_error.log it doesn't work on windows
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.
generate_random_password($car)
generate a strong random password
p($p_string)
Definition ac_common.php:39
add_http_link($text)
record_log($p_message)
Record an error message into the log file of the server or in the log folder of NOALYSS Record also t...
h2info($p_string)
Definition ac_common.php:63
noalyss_bcmul($p_first, $p_second)
sql_string($p_string)
Fix the problem with the quote char for the database.
nb($p_number)
format the number for the CSV export
td($p_string='', $p_extra='')
surround the string with td
Definition ac_common.php:83
FaxTo($p_tel)
compose a HTML string with fax
nbm($p_number, $p_dec=2)
format the number with a sep.
generate_random_string($p_length, $special=1)
Create randomly a string.
cmpDate($p_date, $p_date_oth)
Compare 2 dates.
if(!function_exists( 'tracedebug')) convert_to_rtf($p_string)
encode the string for RTF, return a string
noalyss_trim($p_string)
j($p_string)
escape correctly php string to javascript
Definition ac_common.php:96
alert($p_msg, $buffer=false)
alert in javascript
is_msie()
Check if we use IE 8 or 9.
factory_Ledger(Database &$p_cn, $ledger_id)
This function create a ledger object and return the right one.
$href
Definition adm.inc.php:31
if(headers_sent() &&DEBUGNOALYSS > 0) $html
catch(Exception $exc) if(! $g_user->can_write_action($ag_id)) $r
h( $row[ 'oa_description'])
$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
if(isNumber($jr_id)==0) $ledger_id
$_REQUEST['ac']
if(isset( $_REQUEST[ 'show'])) if(isset($_REQUEST['del'])) $ac
$from_poste name
_("actif, passif,charge,...")
$input_from type
else $card content[$j]['j_montant']
$_GET['qcode']
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,.
Class for jrn, class acc_ledger for manipulating the ledger AND some acc.
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
Html Input.
static warnbulle($p_comment)
Display a warning in a bubble, text is in message_javascript.
Contains some parameters to change appearance of noalyss, the colors are saved into the table parm_ap...
if($request_jrn !="" && $g_user->check_jrn($request_jrn) !='W' $p_msg)
const COMPTA_MIN_YEAR
Definition constant.php:140
const COMPTA_MAX_YEAR
Definition constant.php:139
global $version_noalyss
Definition constant.php:26
$_POST['ac']
Definition do.php:312
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
$noalyss_appearance