/
utils.php
4201 lines (3811 loc) · 153 KB
/
utils.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
/*+**********************************************************************************
* The contents of this file are subject to the vtiger CRM Public License Version 1.0
* ("License"); You may not use this file except in compliance with the License
* The Original Code is: vtiger CRM Open Source
* The Initial Developer of the Original Code is vtiger.
* Portions created by vtiger are Copyright (C) vtiger.
* All Rights Reserved.
************************************************************************************/
include_once 'modules/Settings/configod.php';
require_once('include/utils/Session.php');
require_once('include/utils/Request.php');
require_once('include/database/PearDatabase.php');
require_once('include/utils/cbSettings.php');
require_once('include/cbmqtm/cbmqtm_loader.php');
require_once('include/events/include.inc');
require_once('modules/com_vtiger_workflow/VTWorkflowManager.inc');
require_once 'modules/GlobalVariable/GlobalVariable.php';
require_once 'modules/cbMap/cbMap.php';
require_once('include/ComboUtil.php'); //new
require_once('include/utils/ListViewUtils.php');
require_once('include/utils/EditViewUtils.php');
require_once('include/utils/DetailViewUtils.php');
require_once('include/utils/CommonUtils.php');
require_once('include/utils/InventoryUtils.php');
require_once('include/utils/SearchUtils.php');
require_once('include/DatabaseUtil.php');
require_once('include/events/SqlResultIterator.inc');
require_once('include/events/cbEventHandler.php');
require_once('include/fields/DateTimeField.php');
require_once('include/fields/CurrencyField.php');
require_once('data/CRMEntity.php');
require_once 'vtlib/Vtiger/Language.php';
// Constants to be defined here
// For Customview status.
define("CV_STATUS_DEFAULT", 0);
define("CV_STATUS_PRIVATE", 1);
define("CV_STATUS_PENDING", 2);
define("CV_STATUS_PUBLIC", 3);
// For Restoration.
define("RB_RECORD_DELETED", 'delete');
define("RB_RECORD_INSERTED", 'insert');
define("RB_RECORD_UPDATED", 'update');
/** Function to load global browser variables for javascript
* @param smarty object to load the variables, if empty it will only return the variables in an array
* @returns array with the variables
*/
function getBrowserVariables(&$smarty) {
global $currentModule,$current_user,$default_charset,$theme,$adb,$current_language;
$vars = array();
$vars['gVTModule'] = $currentModule;
$vars['gVTTheme'] = $theme;
$vars['gVTUserID'] = $current_user->id;
$vars['default_charset'] = $default_charset;
$vars['userDateFormat'] = $current_user->date_format;
$vars['userHourFormat'] = ($current_user->hour_format=='24' ? '24' : 'am/pm');
$sql = 'SELECT dayoftheweek FROM its4you_calendar4you_settings WHERE userid=?';
$result = $adb->pquery($sql, array($current_user->id));
if ($result and $adb->num_rows($result)>0) {
$fDOW = $adb->query_result($result, 0,0);
$vars['userFirstDOW'] = ($fDOW=='Monday' ? 1 : 0);
} else {
$vars['userFirstDOW'] = 0;
}
if(isset($current_user->currency_grouping_separator) && $current_user->currency_grouping_separator == '') {
$vars['userCurrencySeparator'] = ' ';
} else {
$vars['userCurrencySeparator'] = html_entity_decode($current_user->currency_grouping_separator, ENT_QUOTES, $default_charset);
}
if(isset($current_user->currency_decimal_separator) && $current_user->currency_decimal_separator == '') {
$vars['userDecimalSeparator'] = ' ';
} else {
$vars['userDecimalSeparator'] = html_entity_decode($current_user->currency_decimal_separator, ENT_QUOTES, $default_charset);
}
if(isset($current_user->no_of_currency_decimals) && $current_user->no_of_currency_decimals == '') {
$vars['userNumberOfDecimals'] = '2';
} else {
$vars['userNumberOfDecimals'] = html_entity_decode($current_user->no_of_currency_decimals, ENT_QUOTES, $default_charset);
}
$swmd5file = file_get_contents('include/sw-precache/service-worker.md5');
$swmd5 = substr($swmd5file,0,strpos($swmd5file,' '));
$corebos_browsertabID = (empty($_COOKIE['corebos_browsertabID']) ? '' : $_COOKIE['corebos_browsertabID']);
if ($smarty) {
$smarty->assign('GVTMODULE',$vars['gVTModule']);
$smarty->assign('THEME', $vars['gVTTheme']);
$smarty->assign('DEFAULT_CHARSET', $vars['default_charset']);
$smarty->assign('CURRENT_USER_ID', $vars['gVTUserID']);
$smarty->assign('USER_DATE_FORMAT',$vars['userDateFormat']);
$smarty->assign('USER_HOUR_FORMAT',$vars['userHourFormat']);
$smarty->assign('USER_FIRST_DOW',$vars['userFirstDOW']);
$smarty->assign('USER_CURRENCY_SEPARATOR', $vars['userCurrencySeparator']);
$smarty->assign('USER_DECIMAL_FORMAT', $vars['userDecimalSeparator']);
$smarty->assign('USER_NUMBER_DECIMALS', $vars['userNumberOfDecimals']);
$smarty->assign('USER_LANGUAGE', $current_language);
$smarty->assign('SW_MD5', $swmd5);
$smarty->assign('corebos_browsertabID', $corebos_browsertabID);
}
}
/** Function to return a full name
* @param $row -- row:: Type integer
* @param $first_column -- first column:: Type string
* @param $last_column -- last column:: Type string
* @returns $fullname -- fullname:: Type string
*/
function return_name(&$row, $first_column, $last_column)
{
global $log;
$log->debug("Entering return_name(".$row.",".$first_column.",".$last_column.") method ...");
$first_name = "";
$last_name = "";
$full_name = "";
if(isset($row[$first_column]))
{
$first_name = stripslashes($row[$first_column]);
}
if(isset($row[$last_column]))
{
$last_name = stripslashes($row[$last_column]);
}
$full_name = $first_name;
// If we have a first name and we have a last name
if($full_name != "" && $last_name != "")
{
// append a space, then the last name
$full_name .= " ".$last_name;
}
// If we have no first name, but we have a last name
else if($last_name != "")
{
// append the last name without the space.
$full_name .= $last_name;
}
$log->debug("Exiting return_name method ...");
return $full_name;
}
/** Function to return language
* @returns $languages -- languages:: Type string
*/
function get_languages()
{
global $log;
$log->debug("Entering get_languages() method ...");
global $languages;
$log->debug("Exiting get_languages method ...");
return $languages;
}
/** Function to return language
* @param $key -- key:: Type string
* @returns $languages -- languages:: Type string
*/
function get_language_display($key)
{
global $log;
$log->debug("Entering get_language_display(".$key.") method ...");
global $languages;
$log->debug("Exiting get_language_display method ...");
return $languages[$key];
}
/** Function returns the user array
* @param $assigned_user_id -- assigned_user_id:: Type string
* @returns $user_list -- user list:: Type array
*/
function get_assigned_user_name($assigned_user_id)
{
global $log;
$log->debug("Entering get_assigned_user_name(".$assigned_user_id.") method ...");
$user_list = get_user_array(false,"");
if(isset($user_list[$assigned_user_id]))
{
$log->debug("Exiting get_assigned_user_name method ...");
return $user_list[$assigned_user_id];
}
$log->debug("Exiting get_assigned_user_name method ...");
return "";
}
/** Function returns the user key in user array
* @param $add_blank -- boolean:: Type boolean
* @param $status -- user status:: Type string
* @param $assigned_user -- user id:: Type string
* @param $private -- sharing type:: Type string
* @returns $user_array -- user array:: Type array
*/
function get_user_array($add_blank=true, $status="Active", $assigned_user="",$private="")
{
global $log, $current_user;
$log->debug("Entering get_user_array(".$add_blank.",". $status.",".$assigned_user.",".$private.") method ...");
if (isset($current_user) && $current_user->id != '') {
require('user_privileges/sharing_privileges_'.$current_user->id.'.php');
require('user_privileges/user_privileges_'.$current_user->id.'.php');
}
static $user_array = null;
$module = isset($_REQUEST['module']) ? $_REQUEST['module'] : '';
if ($user_array == null) {
require_once('include/database/PearDatabase.php');
$db = PearDatabase::getInstance();
$temp_result = Array();
// Including deleted users for now.
if (empty($status)) {
$query = "SELECT id, user_name from vtiger_users";
$params = array();
} else {
$assignUP = GlobalVariable::getVariable('Application_Permit_Assign_Up', 0, $module, $current_user->id);
if ($private == 'private' and empty($assignUP)) {
$assignBrothers = GlobalVariable::getVariable('Application_Permit_Assign_SameRole', 0, $module, $current_user->id);
$query = "select id as id,user_name as user_name,first_name,last_name from vtiger_users where id=? and status='Active' union select vtiger_user2role.userid as id,vtiger_users.user_name as user_name ,
vtiger_users.first_name as first_name ,vtiger_users.last_name as last_name
from vtiger_user2role inner join vtiger_users on vtiger_users.id=vtiger_user2role.userid inner join vtiger_role on vtiger_role.roleid=vtiger_user2role.roleid where vtiger_role.parentrole like ? and status='Active' union
select shareduserid as id,vtiger_users.user_name as user_name ,
vtiger_users.first_name as first_name ,vtiger_users.last_name as last_name from vtiger_tmp_write_user_sharing_per inner join vtiger_users on vtiger_users.id=vtiger_tmp_write_user_sharing_per.shareduserid where status='Active' and vtiger_tmp_write_user_sharing_per.userid=? and vtiger_tmp_write_user_sharing_per.tabid=?";
$params = array($current_user->id, (isset($current_user_parent_role_seq) ? $current_user_parent_role_seq : '').(empty($assignBrothers) ? '::%' : '%'), $current_user->id, getTabid($module));
} else {
$query = "SELECT id, user_name,first_name,last_name from vtiger_users WHERE status=?";
$params = array($status);
}
}
if (!empty($assigned_user)) {
$query .= " OR id=?";
$params[] = $assigned_user;
}
$query .= " order by user_name ASC";
$result = $db->pquery($query, $params, true, "Error filling in user array: ");
if ($add_blank==true) {
// Add in a blank row
$temp_result[''] = '';
}
// Get the id and the name.
while ($row = $db->fetchByAssoc($result)) {
$temp_result[$row['id']] = getFullNameFromArray('Users', $row);
}
$user_array = $temp_result;
}
$log->debug("Exiting get_user_array method ...");
return $user_array;
}
function get_group_array($add_blank=true, $status="Active", $assigned_user="",$private="")
{
global $log;
$log->debug("Entering get_group_array(".$add_blank.",". $status.",".$assigned_user.",".$private.") method ...");
global $current_user;
if(isset($current_user) && $current_user->id != '')
{
require('user_privileges/sharing_privileges_'.$current_user->id.'.php');
require('user_privileges/user_privileges_'.$current_user->id.'.php');
}
static $group_array = null;
$module=$_REQUEST['module'];
if($group_array == null)
{
require_once('include/database/PearDatabase.php');
$db = PearDatabase::getInstance();
$temp_result = Array();
// Including deleted vtiger_users for now.
$log->debug("Sharing is Public. All vtiger_users should be listed");
$query = "SELECT groupid, groupname from vtiger_groups";
$params = array();
if($private == 'private'){
$query .= " WHERE groupid=?";
$params = array( $current_user->id);
if(count($current_user_groups) != 0) {
$query .= " OR vtiger_groups.groupid in (".generateQuestionMarks($current_user_groups).")";
$params[] = $current_user_groups;
}
$log->debug("Sharing is Private. Only the current user should be listed");
$query .= " union select vtiger_group2role.groupid as groupid,vtiger_groups.groupname as groupname from vtiger_group2role inner join vtiger_groups on vtiger_groups.groupid=vtiger_group2role.groupid inner join vtiger_role on vtiger_role.roleid=vtiger_group2role.roleid where vtiger_role.parentrole like ?";
$params[] = $current_user_parent_role_seq.'::%';
if(count($current_user_groups) != 0) {
$query .= " union select vtiger_groups.groupid as groupid,vtiger_groups.groupname as groupname from vtiger_groups inner join vtiger_group2rs on vtiger_groups.groupid=vtiger_group2rs.groupid where vtiger_group2rs.roleandsubid in (".generateQuestionMarks($parent_roles).")";
$params[] = $parent_roles;
}
$query .= " union select sharedgroupid as groupid,vtiger_groups.groupname as groupname from vtiger_tmp_write_group_sharing_per inner join vtiger_groups on vtiger_groups.groupid=vtiger_tmp_write_group_sharing_per.sharedgroupid where vtiger_tmp_write_group_sharing_per.userid=?";
$params[] = $current_user->id;
$query .= " and vtiger_tmp_write_group_sharing_per.tabid=?";
$params[] = getTabid($module);
}
$query .= " order by groupname ASC";
$result = $db->pquery($query, $params, true, "Error filling in user array: ");
if ($add_blank==true){
// Add in a blank row
$temp_result[''] = '';
}
// Get the id and the name.
while($row = $db->fetchByAssoc($result))
{
$temp_result[$row['groupid']] = $row['groupname'];
}
$group_array = $temp_result;
}
$log->debug("Exiting get_group_array method ...");
return $group_array;
}
/** Function skips executing arbitary commands given in a string
* @param $string -- string:: Type string
* @param $maxlength -- maximun length:: Type integer
* @returns $string -- escaped string:: Type string
*/
function clean($string, $maxLength)
{
global $log;
$log->debug("Entering clean(".$string.",". $maxLength.") method ...");
$string = substr($string, 0, $maxLength);
$log->debug("Exiting clean method ...");
return escapeshellcmd($string);
}
/**
* Copy the specified request variable to the member variable of the specified object.
* Do no copy if the member variable is already set.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
*/
function safe_map($request_var, & $focus, $always_copy = false)
{
global $log;
$log->debug("Entering safe_map(".$request_var.",".get_class($focus).",".$always_copy.") method ...");
safe_map_named($request_var, $focus, $request_var, $always_copy);
$log->debug("Exiting safe_map method ...");
}
/**
* Copy the specified request variable to the member variable of the specified object.
* Do no copy if the member variable is already set.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
*/
function safe_map_named($request_var, & $focus, $member_var, $always_copy)
{
global $log;
$log->debug("Entering safe_map_named(".$request_var.",".get_class($focus).",".$member_var.",".$always_copy.") method ...");
if (isset($_REQUEST[$request_var]) && ($always_copy || is_null($focus->$member_var))) {
$log->debug("safe map named called assigning '{$_REQUEST[$request_var]}' to $member_var");
$focus->$member_var = $_REQUEST[$request_var];
}
$log->debug("Exiting safe_map_named method ...");
}
/**
* @deprecated: use getTranslatedString
*/
function return_app_list_strings_language($language) {
// function left with an empty value for backward compatibility
return null;
}
/**
* Retrieve the app_currency_strings for the required language.
*/
function return_app_currency_strings_language($language) {
global $log;
$log->debug("Entering return_app_currency_strings_language(".$language.") method ...");
global $app_currency_strings, $default_language, $log;
// Backup the value first
$temp_app_currency_strings = $app_currency_strings;
@include("include/language/$language.lang.php");
if(!isset($app_currency_strings))
{
$log->warn("Unable to find the application language file for language: ".$language);
require("include/language/$default_language.lang.php");
}
if(!isset($app_currency_strings))
{
$log->fatal("Unable to load the application language file for the selected language($language) or the default language($default_language)");
$log->debug("Exiting return_app_currency_strings_language method ...");
return null;
}
$return_value = $app_currency_strings;
// Restore the value back
$app_currency_strings = $temp_app_currency_strings;
$log->debug("Exiting return_app_currency_strings_language method ...");
return $return_value;
}
/** This function retrieves an application language file and returns the array of strings included.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
* If you are using the current language, do not call this function unless you are loading it for the first time */
function return_application_language($language)
{
global $log;
$log->debug("Entering return_application_language(".$language.") method ...");
global $app_strings, $default_language, $log;
$temp_app_strings = $app_strings;
checkFileAccessForInclusion("include/language/$language.lang.php");
@include("include/language/$language.lang.php");
if(!isset($app_strings))
{
$log->warn("Unable to find the application language file for language: ".$language);
require("include/language/$default_language.lang.php");
}
if(!isset($app_strings))
{
$log->fatal("Unable to load the application language file for the selected language($language) or the default language($default_language)");
$log->debug("Exiting return_application_language method ...");
return null;
}
$return_value = $app_strings;
$app_strings = $temp_app_strings;
$log->debug("Exiting return_application_language method ...");
return $return_value;
}
/** This function retrieves a module's language file and returns the array of strings included.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
* If you are in the current module, do not call this function unless you are loading it for the first time */
function return_module_language($language, $module) {
global $mod_strings, $default_language, $log, $currentModule;
$log->debug("Entering return_module_language(".$language.",". $module.") method ...");
if ($module == 'Events') $module = 'Calendar';
static $cachedModuleStrings = array();
if(!empty($cachedModuleStrings[$module])) {
$log->debug("Exiting return_module_language method ...");
return $cachedModuleStrings[$module];
}
$temp_mod_strings = $mod_strings;
@include("modules/$module/language/$language.lang.php");
if(!isset($mod_strings))
{
$log->warn("Unable to find the module language file for language: ".$language." and module: ".$module);
if($default_language == 'en_us') {
require("modules/$module/language/$default_language.lang.php");
} else {
@include("modules/$module/language/$default_language.lang.php");
if(!isset($mod_strings)) {
require("modules/$module/language/en_us.lang.php");
} else {
}
}
}
if(!isset($mod_strings))
{
$log->fatal("Unable to load the module($module) language file for the selected language($language) or the default language($default_language)");
$log->debug("Exiting return_module_language method ...");
return null;
}
$return_value = $mod_strings;
$mod_strings = $temp_mod_strings;
$log->debug("Exiting return_module_language method ...");
$cachedModuleStrings[$module] = $return_value;
return $return_value;
}
/*This function returns the mod_strings for the current language and the specified module */
function return_specified_module_language($language, $module)
{
global $log, $default_language;
@include("modules/$module/language/$language.lang.php");
if(!isset($mod_strings))
{
$log->warn("Unable to find the module language file for language: ".$language." and module: ".$module);
require("modules/$module/language/$default_language.lang.php");
}
if(!isset($mod_strings))
{
$log->fatal("Unable to load the module($module) language file for the selected language($language) or the default language($default_language)");
$log->debug("Exiting return_module_language method ...");
return null;
}
$return_value = $mod_strings;
$log->debug("Exiting return_module_language method ...");
return $return_value;
}
/** This function retrieves a theme's language file and returns the array of strings included.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
*/
function return_theme_language($language, $theme)
{
global $log;
$log->debug("Entering return_theme_language(".$language.",". $theme.") method ...");
global $mod_strings, $default_language, $log, $currentModule;
@include("themes/$theme/language/$current_language.lang.php");
if(!isset($theme_strings))
{
$log->warn("Unable to find the theme file for language: ".$language." and theme: ".$theme);
require("themes/$theme/language/$default_language.lang.php");
}
if(!isset($theme_strings))
{
$log->fatal("Unable to load the theme($theme) language file for the selected language($language) or the default language($default_language)");
$log->debug("Exiting return_theme_language method ...");
return null;
}
$log->debug("Exiting return_theme_language method ...");
return $theme_strings;
}
/** If the session variable is defined and is not equal to "" then return it. Otherwise, return the default value.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
*/
function return_session_value_or_default($varname, $default)
{
global $log;
$log->debug("Entering return_session_value_or_default(".$varname.",". $default.") method ...");
if(isset($_SESSION[$varname]) && $_SESSION[$varname] != "")
{
$log->debug("Exiting return_session_value_or_default method ...");
return $_SESSION[$varname];
}
$log->debug("Exiting return_session_value_or_default method ...");
return $default;
}
/**
* Creates an array of where restrictions. These are used to construct a where SQL statement on the query
* It looks for the variable in the $_REQUEST array. If it is set and is not "" it will create a where clause out of it.
* @param &$where_clauses - The array to append the clause to
* @param $variable_name - The name of the variable to look for an add to the where clause if found
* @param $SQL_name - [Optional] If specified, this is the SQL column name that is used. If not specified, the $variable_name is used as the SQL_name.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
*/
function append_where_clause(&$where_clauses, $variable_name, $SQL_name = null)
{
global $log;
$log->debug("Entering append_where_clause(".$where_clauses.",".$variable_name.",".$SQL_name.") method ...");
if($SQL_name == null)
{
$SQL_name = $variable_name;
}
if(isset($_REQUEST[$variable_name]) && $_REQUEST[$variable_name] != "")
{
$where_clauses[] = "$SQL_name like '$_REQUEST[$variable_name]%'";
}
$log->debug("Exiting append_where_clause method ...");
}
/**
* Generate the appropriate SQL based on the where clauses.
* @param $where_clauses - An Array of individual where clauses stored as strings
* @returns string where_clause - The final SQL where clause to be executed.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
*/
function generate_where_statement($where_clauses)
{
global $log;
$log->debug("Entering generate_where_statement(".$where_clauses.") method ...");
$where = "";
foreach($where_clauses as $clause)
{
if($where != "")
$where .= " and ";
$where .= $clause;
}
$log->info("Here is the where clause for the list view: $where");
$log->debug("Exiting generate_where_statement method ...");
return $where;
}
/**
* A temporary method of generating GUIDs of the correct format for our DB.
* @return String contianing a GUID in the format: aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee
*
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
*/
function create_guid()
{
global $log;
$log->debug("Entering create_guid() method ...");
$microTime = microtime();
list($a_dec, $a_sec) = explode(" ", $microTime);
$dec_hex = sprintf("%x", $a_dec* 1000000);
$sec_hex = sprintf("%x", $a_sec);
ensure_length($dec_hex, 5);
ensure_length($sec_hex, 6);
$guid = "";
$guid .= $dec_hex;
$guid .= create_guid_section(3);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= create_guid_section(4);
$guid .= '-';
$guid .= $sec_hex;
$guid .= create_guid_section(6);
$log->debug("Exiting create_guid method ...");
return $guid;
}
/** Function to create guid section for a given character
* @param $characters -- characters:: Type string
* @returns $return -- integer:: Type integer``
*/
function create_guid_section($characters)
{
global $log;
$log->debug("Entering create_guid_section(".$characters.") method ...");
$return = "";
for($i=0; $i<$characters; $i++)
{
$return .= sprintf("%x", rand(0,15));
}
$log->debug("Exiting create_guid_section method ...");
return $return;
}
/** Function to ensure length
* @param $string -- string:: Type string
* @param $length -- length:: Type string
*/
function ensure_length(&$string, $length)
{
global $log;
$log->debug("Entering ensure_length(".$string.",". $length.") method ...");
$strlen = strlen($string);
if($strlen < $length)
{
$string = str_pad($string,$length,"0");
}
else if($strlen > $length)
{
$string = substr($string, 0, $length);
}
$log->debug("Exiting ensure_length method ...");
}
/*
function microtime_diff($a, $b) {
global $log;
$log->debug("Entering microtime_diff(".$a.",". $b.") method ...");
list($a_dec, $a_sec) = explode(" ", $a);
list($b_dec, $b_sec) = explode(" ", $b);
$log->debug("Exiting microtime_diff method ...");
return $b_sec - $a_sec + $b_dec - $a_dec;
}
*/
/**
* Return an array of directory names.
* Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
* All Rights Reserved.
*/
function get_themes() {
global $log;
$log->debug('Entering get_themes() method ...');
$filelist = array();
if ($dir = @opendir('./themes')) {
while (($file = readdir($dir))) {
if ($file != '..' && $file != '.' && is_dir('./themes/'.$file) && $file[0] != '.' && is_file("./themes/$file/style.css")) {
$filelist[$file] = $file;
}
}
closedir($dir);
}
ksort($filelist);
$log->debug('Exiting get_themes method ...');
return $filelist;
}
//this is an optimisation of the to_html function, here we make the decision
//decide once if we are going to convert things to html
// Alan Bell Libertus Solutions shared on vtiger CRM developer's list
function decide_to_html(){
global $doconvert;
$request = $_REQUEST;
$chkvalue = array('action','search','module','file','submode');
foreach ($chkvalue as $value) {
$request[$value] = empty($request[$value]) ? '' : $request[$value];
}
$action = vtlib_purify($request['action']);
$search = vtlib_purify($request['search']);
$ajax_action = '';
if($request['module'] != 'Settings' && $request['file'] != 'ListView' && $request['module'] != 'Portal' && $request['module'] != "Reports")// && $request['module'] != 'Emails')
$ajax_action = $request['module'].'Ajax';
if($action != 'CustomView' && $action != 'Export' && $action != $ajax_action && $action != 'LeadConvertToEntities' && $action != 'CreatePDF' && $action != 'ConvertAsFAQ' && $request['module'] != 'Dashboard' && $action != 'CreateSOPDF' && $action != 'SendPDFMail' && (!isset($_REQUEST['submode'])) ) {
$doconvert = true;
} else if($search == true) {
// Fix for tickets #4647, #4648. Conversion required in case of search results also.
$doconvert = true;
}
}
decide_to_html();//call the function once when loading
/** Function to convert the given string to html
* @param $string -- string:: Type string
* @returns $string -- string:: Type string
*/
function to_html($string) {
global $doconvert,$default_charset;
if ($doconvert == true) {
list($cachedresult,$found) = VTCacheUtils::lookupCachedInformation('to_html::'.$string);
if ($found) {
return $cachedresult;
}
$key = $string;
if($default_charset == 'UTF-8')
$string = htmlentities($string, ENT_QUOTES, $default_charset);
else
$string = preg_replace(array('/</', '/>/', '/"/'), array('<', '>', '"'), $string);
VTCacheUtils::updateCachedInformation('to_html::'.$key, $string);
}
return $string;
}
/** Function to get the tablabel for a given id
* @param $tabid -- tab id:: Type integer
* @returns $string -- string:: Type string
*/
function getTabname($tabid) {
global $log, $adb;
$log->debug("Entering getTabname(".$tabid.") method ...");
$sql = "select tablabel from vtiger_tab where tabid=?";
$result = $adb->pquery($sql, array($tabid));
$tabname = $adb->query_result($result,0,"tablabel");
$log->debug("Exiting getTabname method ...");
return $tabname;
}
/** Function to get the tab module name for a given id
* @param $tabid -- tab id:: Type integer
* @returns $string -- string:: Type string
*/
function getTabModuleName($tabid) {
global $log, $adb;
$log->debug("Entering getTabModuleName($tabid) ...");
// Lookup information in cache first
$tabname = VTCacheUtils::lookupModulename($tabid);
if ($tabname === false) {
if (file_exists('tabdata.php') && (filesize('tabdata.php') != 0)) {
include('tabdata.php');
$tabname = array_search($tabid,$tab_info_array);
}
if ($tabname === false) {
$sql = "select name from vtiger_tab where tabid=?";
$result = $adb->pquery($sql, array($tabid));
$tabname = $adb->query_result($result,0,"name");
}
// Update information to cache for re-use
VTCacheUtils::updateTabidInfo($tabid, $tabname);
}
$log->debug("Exiting getTabModuleName ($tabname) ...");
return $tabname;
}
/** Function to get column fields for a given module
* @param $module -- module:: Type string
* @returns $column_fld -- column field :: Type array
*/
function getColumnFields($module) {
global $log, $adb;
$log->debug("Entering getColumnFields(".$module.") method ...");
// Lookup in cache for information
$cachedModuleFields = VTCacheUtils::lookupFieldInfo_Module($module);
if($cachedModuleFields === false) {
$tabid = getTabid($module);
if ($module == 'Calendar') {
$tabid = array('9','16');
}
// Let us pick up all the fields first so that we can cache information
$sql = "SELECT tabid, fieldname, fieldid, fieldlabel, columnname, tablename, uitype, typeofdata, presence
FROM vtiger_field WHERE tabid in (" . generateQuestionMarks($tabid) . ")";
$result = $adb->pquery($sql, array($tabid));
$noofrows = $adb->num_rows($result);
if($noofrows) {
while($resultrow = $adb->fetch_array($result)) {
// Update information to cache for re-use
VTCacheUtils::updateFieldInfo(
$resultrow['tabid'], $resultrow['fieldname'], $resultrow['fieldid'],
decode_html($resultrow['fieldlabel']), $resultrow['columnname'], $resultrow['tablename'],
$resultrow['uitype'], $resultrow['typeofdata'], $resultrow['presence']
);
}
}
// For consistency get information from cache
$cachedModuleFields = VTCacheUtils::lookupFieldInfo_Module($module);
}
if($module == 'Calendar') {
$cachedEventsFields = VTCacheUtils::lookupFieldInfo_Module('Events');
if($cachedModuleFields == false) $cachedModuleFields = $cachedEventsFields;
else $cachedModuleFields = array_merge($cachedModuleFields, $cachedEventsFields);
}
$column_fld = array();
if($cachedModuleFields) {
foreach($cachedModuleFields as $fieldinfo) {
$column_fld[$fieldinfo['fieldname']] = '';
}
}
$log->debug("Exiting getColumnFields method ...");
return $column_fld;
}
/** Function to get a users's mail id
* @param $userid -- userid :: Type integer
* @returns $email -- email :: Type string
*/
function getUserEmail($userid) {
global $log, $adb;
$log->debug('Entering getUserEmail('.print_r($userid,true).') method ...');
$email = '';
if (!empty($userid) and is_numeric($userid)) {
$sql = 'select email1 from vtiger_users where id=?';
$userid = (array)$userid;
$result = $adb->pquery($sql, $userid);
if ($result and $adb->num_rows($result)>0) {
$email = $adb->query_result($result,0,'email1');
}
}
$log->debug('Exiting getUserEmail method ...');
return $email;
}
/** Function to get a userid for outlook // outlook security
* @param $username -- username :: Type string
* @returns $user_id -- user id :: Type integer
*/
function getUserId_Ol($username) {
global $log, $adb;
$log->debug("Entering getUserId_Ol(".$username.") method ...");
$sql = "select id from vtiger_users where user_name=?";
$result = $adb->pquery($sql, array($username));
$num_rows = $adb->num_rows($result);
if($num_rows > 0) {
$user_id = $adb->query_result($result,0,"id");
} else {
$user_id = 0;
}
$log->debug("Exiting getUserId_Ol method ...");
return $user_id;
}
/** Function to get a action id for a given action name //outlook security
* @param $action -- action name :: Type string
* @returns $actionid -- action id :: Type integer
*/
function getActionid($action) {
global $log, $adb;
$log->debug("Entering getActionid(".$action.") method ...");
$actionid = '';
if (file_exists('tabdata.php') && (filesize('tabdata.php') != 0)) {
include('tabdata.php');
$actionid = (isset($action_id_array[$action]) ? $action_id_array[$action] : '');
}
if ($actionid == '') {
$query="select actionid from vtiger_actionmapping where actionname=?";
$result =$adb->pquery($query, array($action));
$actionid=$adb->query_result($result,0,'actionid');
}
$log->debug('Exiting getActionid method: id selected is '.$actionid);
return $actionid;
}
/** Function to get a action for a given action id
* @param $action id -- action id :: Type integer
* @returns $actionname-- action name :: Type string
*/
function getActionname($actionid) {
global $log, $adb;
$log->debug("Entering getActionname(".$actionid.") method ...");
$actionname='';
if (file_exists('tabdata.php') && (filesize('tabdata.php') != 0)) {
include('tabdata.php');
$actionname = (isset($action_name_array[$actionid]) ? $action_name_array[$actionid] : '');
}
if ($actionname == '') {
$query="select actionname from vtiger_actionmapping where actionid=? and securitycheck=0";
$result =$adb->pquery($query, array($actionid));
$actionname=$adb->query_result($result,0,"actionname");
}
$log->debug("Exiting getActionname method ...");
return $actionname;
}
/** Function to get a assigned user id for a given entity
* @param $record -- entity id :: Type integer
* @returns $user_id -- user id :: Type integer
*/
function getUserId($record) {
global $log, $adb;
$log->debug("Entering getUserId(".$record.") method ...");
$userrs = $adb->pquery('select smownerid from vtiger_crmentity where crmid = ?', array($record));
$user_id = $adb->query_result($userrs,0,'smownerid');
$log->debug("Exiting getUserId method ...");
return $user_id;
}
/** Function to get a user id or group id for a given entity
* @param $record -- entity id :: Type integer
* @returns $ownerArr -- owner id :: Type array
*/
function getRecordOwnerId($record) {
global $log, $adb;
$log->debug("Entering getRecordOwnerId(".$record.") method ...");
$ownerArr=Array();
$query="select smownerid from vtiger_crmentity where crmid = ?";
$result=$adb->pquery($query, array($record));
if($adb->num_rows($result) > 0)
{
$ownerId=$adb->query_result($result,0,'smownerid');
$sql_result = $adb->pquery("select count(*) as count from vtiger_users where id = ?",array($ownerId));
if($adb->query_result($sql_result,0,'count') > 0)
$ownerArr['Users'] = $ownerId;
else
$ownerArr['Groups'] = $ownerId;
}
$log->debug("Exiting getRecordOwnerId method ...");
return $ownerArr;
}
/** Function to insert value to profile2field table
* @param $profileid -- profileid :: Type integer
*/
function insertProfile2field($profileid) {
global $log, $adb;
$log->debug("Entering insertProfile2field(".$profileid.") method ...");
$adb->database->SetFetchMode(ADODB_FETCH_ASSOC);
$fld_result = $adb->pquery("select tabid, fieldid from vtiger_field where generatedtype=1 and displaytype in (1,2,3) and vtiger_field.presence in (0,2) and tabid != 29", array());
$num_rows = $adb->num_rows($fld_result);
for($i=0; $i<$num_rows; $i++) {
$tab_id = $adb->query_result($fld_result,$i,'tabid');
$field_id = $adb->query_result($fld_result,$i,'fieldid');
$params = array($profileid, $tab_id, $field_id, 0, 0);
$adb->pquery("insert into vtiger_profile2field values (?,?,?,?,?)", $params);
}
$log->debug("Exiting insertProfile2field method ...");
}
/** Function to insert into default org field */
function insert_def_org_field() {
global $log, $adb;
$log->debug("Entering insert_def_org_field() method ...");