/
functions2.lib.php
2825 lines (2576 loc) · 91 KB
/
functions2.lib.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/* Copyright (C) 2008-2011 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2008-2012 Regis Houssin <regis.houssin@inodbox.com>
* Copyright (C) 2008 Raphael Bertrand (Resultic) <raphael.bertrand@resultic.fr>
* Copyright (C) 2014-2016 Marcos García <marcosgdf@gmail.com>
* Copyright (C) 2015 Ferran Marcet <fmarcet@2byte.es>
* Copyright (C) 2015-2016 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
* Copyright (C) 2017 Juanjo Menent <jmenent@2byte.es>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
* or see https://www.gnu.org/
*/
/**
* \file htdocs/core/lib/functions2.lib.php
* \brief A set of functions for Dolibarr
* This file contains all rare functions.
*/
// Enable this line to trace path when function is called.
//print xdebug_print_function_stack('Functions2.lib was called');exit;
/**
* Same function than javascript unescape() function but in PHP.
*
* @param string $source String to decode
* @return string Unescaped string
*/
function jsUnEscape($source)
{
$decodedStr = "";
$pos = 0;
$len = strlen($source);
while ($pos < $len) {
$charAt = substr($source, $pos, 1);
if ($charAt == '%') {
$pos++;
$charAt = substr($source, $pos, 1);
if ($charAt == 'u') {
// we got a unicode character
$pos++;
$unicodeHexVal = substr($source, $pos, 4);
$unicode = hexdec($unicodeHexVal);
$entity = "&#".$unicode.';';
$decodedStr .= utf8_encode($entity);
$pos += 4;
} else {
// we have an escaped ascii character
$hexVal = substr($source, $pos, 2);
$decodedStr .= chr(hexdec($hexVal));
$pos += 2;
}
} else {
$decodedStr .= $charAt;
$pos++;
}
}
return dol_html_entity_decode($decodedStr, ENT_COMPAT | ENT_HTML5);
}
/**
* Return list of modules directories. We detect directories that contains a subdirectory /core/modules
* We discard directory modules that contains 'disabled' into their name.
*
* @param string $subdir Sub directory (Example: '/mailings')
* @return array Array of directories that can contains module descriptors
*/
function dolGetModulesDirs($subdir = '')
{
global $conf;
$modulesdir = array();
foreach ($conf->file->dol_document_root as $type => $dirroot) {
// Default core/modules dir
if ($type === 'main') {
$modulesdir[$dirroot.'/core/modules'.$subdir.'/'] = $dirroot.'/core/modules'.$subdir.'/';
}
// Scan dir from external modules
$handle = @opendir($dirroot);
if (is_resource($handle)) {
while (($file = readdir($handle)) !== false) {
if (preg_match('/disabled/', $file)) {
continue; // We discard module if it contains disabled into name.
}
if (is_dir($dirroot.'/'.$file) && substr($file, 0, 1) <> '.' && substr($file, 0, 3) <> 'CVS' && $file != 'includes') {
if (is_dir($dirroot.'/'.$file.'/core/modules'.$subdir.'/')) {
$modulesdir[$dirroot.'/'.$file.'/core/modules'.$subdir.'/'] = $dirroot.'/'.$file.'/core/modules'.$subdir.'/';
}
}
}
closedir($handle);
}
}
return $modulesdir;
}
/**
* Try to guess default paper format according to language into $langs
*
* @param Translate $outputlangs Output lang to use to autodetect output format if setup not done
* @return string Default paper format code
*/
function dol_getDefaultFormat(Translate $outputlangs = null)
{
global $langs;
$selected = 'EUA4';
if (!$outputlangs) {
$outputlangs = $langs;
}
if ($outputlangs->defaultlang == 'ca_CA') {
$selected = 'CAP4'; // Canada
}
if ($outputlangs->defaultlang == 'en_US') {
$selected = 'USLetter'; // US
}
return $selected;
}
/**
* Output content of a file $filename in version of current language (otherwise may use an alternate language)
*
* @param Translate $langs Object language to use for output
* @param string $filename Relative filename to output
* @param int $searchalt 1=Search also in alternative languages
* @return boolean true if OK, false if KO
*/
function dol_print_file($langs, $filename, $searchalt = 0)
{
global $conf;
// Test if file is in lang directory
foreach ($langs->dir as $searchdir) {
$formfile = ($searchdir."/langs/".$langs->defaultlang."/".$filename);
dol_syslog('functions2::dol_print_file search file '.$formfile, LOG_DEBUG);
if (is_readable($formfile)) {
$content = file_get_contents($formfile);
$isutf8 = utf8_check($content);
if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
print utf8_encode($content);
} elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
print utf8_decode($content);
} else {
print $content;
}
return true;
} else {
dol_syslog('functions2::dol_print_file not found', LOG_DEBUG);
}
if ($searchalt) {
// Test si fichier dans repertoire de la langue alternative
if ($langs->defaultlang != "en_US") {
$formfilealt = $searchdir."/langs/en_US/".$filename;
} else {
$formfilealt = $searchdir."/langs/fr_FR/".$filename;
}
dol_syslog('functions2::dol_print_file search alt file '.$formfilealt, LOG_DEBUG);
//print 'getcwd='.getcwd().' htmlfilealt='.$formfilealt.' X '.file_exists(getcwd().'/'.$formfilealt);
if (is_readable($formfilealt)) {
$content = file_get_contents($formfilealt);
$isutf8 = utf8_check($content);
if (!$isutf8 && $conf->file->character_set_client == 'UTF-8') {
print utf8_encode($content);
} elseif ($isutf8 && $conf->file->character_set_client == 'ISO-8859-1') {
print utf8_decode($content);
} else {
print $content;
}
return true;
} else {
dol_syslog('functions2::dol_print_file not found', LOG_DEBUG);
}
}
}
return false;
}
/**
* Show informations on an object
* TODO Move this into html.formother
*
* @param object $object Objet to show
* @param int $usetable Output into a table
* @return void
*/
function dol_print_object_info($object, $usetable = 0)
{
global $langs, $db;
// Load translation files required by the page
$langs->loadLangs(array('other', 'admin'));
include_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
$deltadateforserver = getServerTimeZoneInt('now');
$deltadateforclient = ((int) $_SESSION['dol_tz'] + (int) $_SESSION['dol_dst']);
//$deltadateforcompany=((int) $_SESSION['dol_tz'] + (int) $_SESSION['dol_dst']);
$deltadateforuser = round($deltadateforclient - $deltadateforserver);
//print "x".$deltadateforserver." - ".$deltadateforclient." - ".$deltadateforuser;
if ($usetable) {
print '<table class="border tableforfield centpercent">';
}
// Import key
if (!empty($object->import_key)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("ImportedWithSet");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print $object->import_key;
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// User creation (old method using already loaded object and not id is kept for backward compatibility)
if (!empty($object->user_creation) || !empty($object->user_creation_id)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("CreatedBy");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
if (is_object($object->user_creation)) {
if ($object->user_creation->id) {
print $object->user_creation->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
} else {
$userstatic = new User($db);
$userstatic->fetch($object->user_creation_id ? $object->user_creation_id : $object->user_creation);
if ($userstatic->id) {
print $userstatic->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// Date creation
if (!empty($object->date_creation)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("DateCreation");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print dol_print_date($object->date_creation, 'dayhour', 'tzserver');
if ($deltadateforuser) {
print ' '.$langs->trans("CurrentHour").' / '.dol_print_date($object->date_creation, "dayhour", "tzuserrel").' '.$langs->trans("ClientHour");
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// User change (old method using already loaded object and not id is kept for backward compatibility)
if (!empty($object->user_modification) || !empty($object->user_modification_id)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("ModifiedBy");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
if (is_object($object->user_modification)) {
if ($object->user_modification->id) {
print $object->user_modification->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
} else {
$userstatic = new User($db);
$userstatic->fetch($object->user_modification_id ? $object->user_modification_id : $object->user_modification);
if ($userstatic->id) {
print $userstatic->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// Date change
if (!empty($object->date_modification)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("DateLastModification");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print dol_print_date($object->date_modification, 'dayhour', 'tzserver');
if ($deltadateforuser) {
print ' '.$langs->trans("CurrentHour").' / '.dol_print_date($object->date_modification, "dayhour", "tzuserrel").' '.$langs->trans("ClientHour");
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// User validation (old method using already loaded object and not id is kept for backward compatibility)
if (!empty($object->user_validation) || !empty($object->user_validation_id)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("ValidatedBy");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
if (is_object($object->user_validation)) {
if ($object->user_validation->id) {
print $object->user_validation->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
} else {
$userstatic = new User($db);
$userstatic->fetch($object->user_validation_id ? $object->user_validation_id : $object->user_validation);
if ($userstatic->id) {
print $userstatic->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// Date validation
if (!empty($object->date_validation)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("DateValidation");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print dol_print_date($object->date_validation, 'dayhour', 'tzserver');
if ($deltadateforuser) {
print ' '.$langs->trans("CurrentHour").' / '.dol_print_date($object->date_validation, "dayhour", 'tzuserrel').' '.$langs->trans("ClientHour");
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// User approve (old method using already loaded object and not id is kept for backward compatibility)
if (!empty($object->user_approve) || !empty($object->user_approve_id)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("ApprovedBy");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
if (is_object($object->user_approve)) {
if ($object->user_approve->id) {
print $object->user_approve->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
} else {
$userstatic = new User($db);
$userstatic->fetch($object->user_approve_id ? $object->user_approve_id : $object->user_approve);
if ($userstatic->id) {
print $userstatic->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// Date approve
if (!empty($object->date_approve)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("DateApprove");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print dol_print_date($object->date_approve, 'dayhour', 'tzserver');
if ($deltadateforuser) {
print ' '.$langs->trans("CurrentHour").' / '.dol_print_date($object->date_approve, "dayhour", 'tzuserrel').' '.$langs->trans("ClientHour");
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// User approve
if (!empty($object->user_approve_id2)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("ApprovedBy");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
$userstatic = new User($db);
$userstatic->fetch($object->user_approve_id2);
if ($userstatic->id) {
print $userstatic->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// Date approve
if (!empty($object->date_approve2)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("DateApprove2");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print dol_print_date($object->date_approve2, 'dayhour', 'tzserver');
if ($deltadateforuser) {
print ' '.$langs->trans("CurrentHour").' / '.dol_print_date($object->date_approve2, "dayhour", 'tzuserrel').' '.$langs->trans("ClientHour");
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// User signature
if (!empty($object->user_signature)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans('SignedBy');
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
if (is_object($object->user_signature)) {
if ($object->user_signature->id) {
print $object->user_signature->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans('Unknown');
}
} else {
$userstatic = new User($db);
$userstatic->fetch($object->user_signature);
if ($userstatic->id) {
print $userstatic->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans('Unknown');
}
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// Date signature
if (!empty($object->date_signature)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans('DateSigning');
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print dol_print_date($object->date_signature, 'dayhour');
if ($deltadateforuser) {
print ' '.$langs->trans('CurrentHour').' / '.dol_print_date($object->date_signature, 'dayhour', 'tzuserrel').' '.$langs->trans('ClientHour');
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// User close
if (!empty($object->user_cloture) || !empty($object->user_closing)) {
if (isset($object->user_cloture) && !empty($object->user_cloture)) {
$object->user_closing = $object->user_cloture;
}
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("ClosedBy");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
if (is_object($object->user_closing)) {
if ($object->user_closing->id) {
print $object->user_closing->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
} else {
$userstatic = new User($db);
$userstatic->fetch($object->user_closing);
if ($userstatic->id) {
print $userstatic->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// Date close
if (!empty($object->date_cloture) || !empty($object->date_closing)) {
if (isset($object->date_cloture) && !empty($object->date_cloture)) {
$object->date_closing = $object->date_cloture;
}
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("DateClosing");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print dol_print_date($object->date_closing, 'dayhour', 'tzserver');
if ($deltadateforuser) {
print ' '.$langs->trans("CurrentHour").' / '.dol_print_date($object->date_closing, "dayhour", 'tzuserrel').' '.$langs->trans("ClientHour");
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// User conciliate
if (!empty($object->user_rappro)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("ConciliatedBy");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
if (is_object($object->user_rappro)) {
if ($object->user_rappro->id) {
print $object->user_rappro->getNomUrl(-1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
} else {
$userstatic = new User($db);
$userstatic->fetch($object->user_rappro);
if ($userstatic->id) {
print $userstatic->getNomUrl(1, '', 0, 0, 0);
} else {
print $langs->trans("Unknown");
}
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// Date conciliate
if (!empty($object->date_rappro)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("DateConciliating");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print dol_print_date($object->date_rappro, 'dayhour', 'tzserver');
if ($deltadateforuser) {
print ' '.$langs->trans("CurrentHour").' / '.dol_print_date($object->date_rappro, "dayhour", 'tzuserrel').' '.$langs->trans("ClientHour");
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
// Date send
if (!empty($object->date_envoi)) {
if ($usetable) {
print '<tr><td class="titlefield">';
}
print $langs->trans("DateLastSend");
if ($usetable) {
print '</td><td>';
} else {
print ': ';
}
print dol_print_date($object->date_envoi, 'dayhour', 'tzserver');
if ($deltadateforuser) {
print ' '.$langs->trans("CurrentHour").' / '.dol_print_date($object->date_envoi, "dayhour", 'tzuserrel').' '.$langs->trans("ClientHour");
}
if ($usetable) {
print '</td></tr>';
} else {
print '<br>';
}
}
if ($usetable) {
print '</table>';
}
}
/**
* Return an email formatted to include a tracking id
* For example myemail@example.com becom myemail+trackingid@example.com
*
* @param string $email Email address (Ex: "toto@example.com", "John Do <johndo@example.com>")
* @param string $trackingid Tracking id (Ex: thi123 for thirdparty with id 123)
* @return string Return email tracker string
*/
function dolAddEmailTrackId($email, $trackingid)
{
$tmp = explode('@', $email);
return $tmp[0].'+'.$trackingid.'@'.(isset($tmp[1]) ? $tmp[1] : '');
}
/**
* Return true if email has a domain name that can be resolved to MX type.
*
* @param string $mail Email address (Ex: "toto@example.com", "John Do <johndo@example.com>")
* @return int -1 if error (function not available), 0=Not valid, 1=Valid
*/
function isValidMailDomain($mail)
{
list($user, $domain) = explode("@", $mail, 2);
return ($domain ? isValidMXRecord($domain) : 0);
}
/**
* Url string validation
* <http[s]> :// [user[:pass]@] hostname [port] [/path] [?getquery] [anchor]
*
* @param string $url Url
* @param int $http 1: verify http is provided, 0: not verify http
* @param int $pass 1: verify user and pass is provided, 0: not verify user and pass
* @param int $port 1: verify port is provided, 0: not verify port
* @param int $path 1: verify a path is provided "/" or "/..." or "/.../", 0: not verify path
* @param int $query 1: verify query is provided, 0: not verify query
* @param int $anchor 1: verify anchor is provided, 0: not verify anchor
* @return int 1=Check is OK, 0=Check is KO
*/
function isValidUrl($url, $http = 0, $pass = 0, $port = 0, $path = 0, $query = 0, $anchor = 0)
{
$ValidUrl = 0;
$urlregex = '';
// SCHEME
if ($http) {
$urlregex .= "^(http:\/\/|https:\/\/)";
}
// USER AND PASS
if ($pass) {
$urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)";
}
// HOSTNAME OR IP
//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*"; // x allowed (ex. http://localhost, http://routerlogin)
//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+"; // x.x
$urlregex .= "([a-z0-9+\$_\\\:-])+(\.[a-z0-9+\$_-][a-z0-9+\$_-]+)*"; // x ou x.xx (2 x ou plus)
//use only one of the above
// PORT
if ($port) {
$urlregex .= "(\:[0-9]{2,5})";
}
// PATH
if ($path) {
$urlregex .= "(\/([a-z0-9+\$_-]\.?)+)*\/";
}
// GET Query
if ($query) {
$urlregex .= "(\?[a-z+&\$_.-][a-z0-9;:@\/&%=+\$_.-]*)";
}
// ANCHOR
if ($anchor) {
$urlregex .= "(#[a-z_.-][a-z0-9+\$_.-]*)$";
}
// check
if (preg_match('/'.$urlregex.'/i', $url)) {
$ValidUrl = 1;
}
//print $urlregex.' - '.$url.' - '.$ValidUrl;
return $ValidUrl;
}
/**
* Check if VAT numero is valid (check done on syntax only, no database or remote access)
*
* @param Societe $company VAT number
* @return int 1=Check is OK, 0=Check is KO
*/
function isValidVATID($company)
{
if ($company->isInEEC()) { // Syntax check rules for EEC countries
/* Disabled because some companies can have an address in Irland and a vat number in France.
$vatprefix = $company->country_code;
if ($vatprefix == 'GR') $vatprefix = '(EL|GR)';
elseif ($vatprefix == 'MC') $vatprefix = 'FR'; // Monaco is using french VAT numbers
else $vatprefix = preg_quote($vatprefix, '/');*/
$vatprefix = '[a-zA-Z][a-zA-Z]';
if (!preg_match('/^'.$vatprefix.'[a-zA-Z0-9\-\.]{5,14}$/i', str_replace(' ', '', $company->tva_intra))) {
return 0;
}
}
return 1;
}
/**
* Clean an url string
*
* @param string $url Url
* @param integer $http 1 = keep both http:// and https://, 0: remove http:// but not https://
* @return string Cleaned url
*/
function clean_url($url, $http = 1)
{
// Fixed by Matelli (see http://matelli.fr/showcases/patchs-dolibarr/fix-cleaning-url.html)
// To include the minus sign in a char class, we must not escape it but put it at the end of the class
// Also, there's no need of escape a dot sign in a class
$regs = array();
if (preg_match('/^(https?:[\\/]+)?([0-9A-Z.-]+\.[A-Z]{2,4})(:[0-9]+)?/i', $url, $regs)) {
$proto = $regs[1];
$domain = $regs[2];
$port = isset($regs[3]) ? $regs[3] : '';
//print $url." -> ".$proto." - ".$domain." - ".$port;
//$url = dol_string_nospecial(trim($url));
$url = trim($url);
// Si http: defini on supprime le http (Si https on ne supprime pas)
$newproto = $proto;
if ($http == 0) {
if (preg_match('/^http:[\\/]+/i', $url)) {
$url = preg_replace('/^http:[\\/]+/i', '', $url);
$newproto = '';
}
}
// On passe le nom de domaine en minuscule
$CleanUrl = preg_replace('/^'.preg_quote($proto.$domain, '/').'/i', $newproto.strtolower($domain), $url);
return $CleanUrl;
} else {
return $url;
}
}
/**
* Returns an email value with obfuscated parts.
*
* @param string $mail Email
* @param string $replace Replacement character (defaul: *)
* @param int $nbreplace Number of replacement character (default: 8)
* @param int $nbdisplaymail Number of character unchanged (default: 4)
* @param int $nbdisplaydomain Number of character unchanged of domain (default: 3)
* @param bool $displaytld Display tld (default: true)
* @return string Return email with hidden parts or '';
*/
function dolObfuscateEmail($mail, $replace = "*", $nbreplace = 8, $nbdisplaymail = 4, $nbdisplaydomain = 3, $displaytld = true)
{
if (!isValidEmail($mail)) {
return '';
}
$tab = explode('@', $mail);
$tab2 = explode('.', $tab[1]);
$string_replace = '';
$mail_name = $tab[0];
$mail_domaine = $tab2[0];
$mail_tld = '';
$nbofelem = count($tab2);
for ($i = 1; $i < $nbofelem && $displaytld; $i++) {
$mail_tld .= '.'.$tab2[$i];
}
for ($i = 0; $i < $nbreplace; $i++) {
$string_replace .= $replace;
}
if (strlen($mail_name) > $nbdisplaymail) {
$mail_name = substr($mail_name, 0, $nbdisplaymail);
}
if (strlen($mail_domaine) > $nbdisplaydomain) {
$mail_domaine = substr($mail_domaine, strlen($mail_domaine) - $nbdisplaydomain);
}
return $mail_name.$string_replace.$mail_domaine.$mail_tld;
}
/**
* Return lines of an html table from an array
* Used by array2table function only
*
* @param array $data Array of data
* @param string $troptions Options for tr
* @param string $tdoptions Options for td
* @return string
*/
function array2tr($data, $troptions = '', $tdoptions = '')
{
$text = '<tr '.$troptions.'>';
foreach ($data as $key => $item) {
$text .= '<td '.$tdoptions.'>'.$item.'</td>';
}
$text .= '</tr>';
return $text;
}
/**
* Return an html table from an array
*
* @param array $data Array of data
* @param int $tableMarkup Table markup
* @param string $tableoptions Options for table
* @param string $troptions Options for tr
* @param string $tdoptions Options for td
* @return string
*/
function array2table($data, $tableMarkup = 1, $tableoptions = '', $troptions = '', $tdoptions = '')
{
$text = '';
if ($tableMarkup) {
$text = '<table '.$tableoptions.'>';
}
foreach ($data as $key => $item) {
if (is_array($item)) {
$text .= array2tr($item, $troptions, $tdoptions);
} else {
$text .= '<tr '.$troptions.'>';
$text .= '<td '.$tdoptions.'>'.$key.'</td>';
$text .= '<td '.$tdoptions.'>'.$item.'</td>';
$text .= '</tr>';
}
}
if ($tableMarkup) {
$text .= '</table>';
}
return $text;
}
/**
* Return last or next value for a mask (according to area we should not reset)
*
* @param DoliDB $db Database handler
* @param string $mask Mask to use
* @param string $table Table containing field with counter
* @param string $field Field containing already used values of counter
* @param string $where To add a filter on selection (for exemple to filter on invoice types)
* @param Societe $objsoc The company that own the object we need a counter for
* @param string $date Date to use for the {y},{m},{d} tags.
* @param string $mode 'next' for next value or 'last' for last value
* @param bool $bentityon Activate the entity filter. Default is true (for modules not compatible with multicompany)
* @param User $objuser Object user we need data from.
* @param int $forceentity Entity id to force
* @return string New value (numeric) or error message
*/
function get_next_value($db, $mask, $table, $field, $where = '', $objsoc = '', $date = '', $mode = 'next', $bentityon = true, $objuser = null, $forceentity = null)
{
global $conf, $user;
if (!is_object($objsoc)) {
$valueforccc = $objsoc;
} elseif ($table == "commande_fournisseur" || $table == "facture_fourn") {
$valueforccc = dol_string_unaccent($objsoc->code_fournisseur);
} else {
$valueforccc = dol_string_unaccent($objsoc->code_client);
}
$sharetable = $table;
if ($table == 'facture' || $table == 'invoice') {
$sharetable = 'invoicenumber'; // for getEntity function
}
// Clean parameters
if ($date == '') {
$date = dol_now(); // We use local year and month of PHP server to search numbers
}
// but we should use local year and month of user
// For debugging
//dol_syslog("mask=".$mask, LOG_DEBUG);
//include_once(DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php');
//$mask='FA{yy}{mm}-{0000@99}';
//$date=dol_mktime(12, 0, 0, 1, 1, 1900);
//$date=dol_stringtotime('20130101');
$hasglobalcounter = false;