/
functions.php
7468 lines (6577 loc) · 280 KB
/
functions.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
<?
if(!function_exists("money_format"))
{
function money_format($var)
{
$var=str_replace("$","",$var);
$var=str_replace(",","",$var);
$var=(float)$var;
return "$".number_format($var, 2, '.', ',');
}
}
function d($use_string, $die_here = true)
{
echo "<br><br><br><br><br><br>";
echo "<pre>";
print_r($use_string);
echo "</pre>";
if($die_here) die;
}
function get_internal_address()
{
// function to return the site internal address
// this is needed for items like the PDF creator that references the internal address
global $defaultsarray;
$use_address = $_SERVER['HTTP_HOST'];
if($defaultsarray['internal_address'] != '') $use_address = $defaultsarray['internal_address'];
if(substr($use_address,-1) != '/') return $use_address.'/';
return $use_address;
}
function duplicate_row($table, $id)
{
// function to duplicate a row from a table
$sql = "
show columns
from $table
";
$data_fields = simple_query($sql);
$row_fields = mysqli_fetch_array($data_fields);
$field_array = array();
while($row_fields = mysqli_fetch_array($data_fields))
{
//echo $row_fields['Field']."<br>";
if($row_fields['Field'] != 'id') $field_array[] = $row_fields['Field'];
}
$field_list = implode(",", $field_array);
$sql = "
insert into $table ($field_list)
select $field_list from $table i where i.id = '".sql_friendly($id)."'
";
simple_query($sql);
return mysqli_insert_id();
}
function get_option_text_by_id($option_id,$mode=0)
{
$sql = "
select *
from option_values
where id = '".sql_friendly($option_id)."'
";
$data = simple_query($sql);
$row = mysqli_fetch_array($data);
$res=$row['fname'];
if($mode==1) $res=$row['fvalue'];
return $res;
}
function build_option_box($option_cat_name, $selected_value = "", $field_name, $show_name = false, $show_blank_text = true)
{
$data = get_options($option_cat_name);
echo "<select name='$field_name' id='$field_name'>";
$row = mysqli_fetch_array($data);
if($row['blank_text'] != '')
{
echo "<option value='0'>".($show_blank_text ? $row['blank_text'] : "")."</option>";
}
mysqli_data_seek($data, 0);
while($row = mysqli_fetch_array($data))
{
$use_selected = "";
if($row['id'] == $selected_value) $use_selected = "selected";
$disp_name = "";
if($show_name) $disp_name = "($row[fname]) ";
echo "<option $use_selected value='$row[id]'>$disp_name $row[fvalue]</option>";
}
echo "</select>";
}
function mrr_build_option_box($option_cat_name, $selected_value = "", $field_name, $show_name = false, $show_blank_text = true, $class="")
{
$data = get_options($option_cat_name);
$selbx="";
$selbx.="<select name='$field_name' id='$field_name'".$class.">";
$row = mysqli_fetch_array($data);
if($row['blank_text'] != '')
{
$selbx.="<option value='0'>".($show_blank_text ? $row['blank_text'] : "")."</option>";
}
mysqli_data_seek($data, 0);
while($row = mysqli_fetch_array($data))
{
$use_selected = "";
if($row['id'] == $selected_value) $use_selected = "selected";
$disp_name = "$row[fvalue]";
if(trim($disp_name)=="" || trim($disp_name)=="New Entry") $disp_name = "$row[fname]";
//if($show_name) $disp_name = "($row[fname]) ";
$selbx.="<option $use_selected value='$row[id]'>$disp_name</option>";
}
$selbx.="</select>";
return $selbx;
}
function get_options($option_cat_name,$sort="")
{
$sql = "
select option_values.*,
option_cat.blank_text
from option_values, option_cat
where option_values.cat_id = '".sql_friendly(get_option_cat_id($option_cat_name))."'
and option_values.deleted = 0
and option_values.cat_id = option_cat.id
order by ".$sort."option_values.zorder, option_values.fvalue
";
$data = simple_query($sql);
return $data;
}
function get_options_special($option_cat_name,$sort="",$builder_id=0,$cust_id=0)
{
$sql = "
select option_values.*,
option_cat.blank_text
from option_values
left join option_cat on option_cat.id = option_values.cat_id
where option_values.cat_id = '".sql_friendly(get_option_cat_id($option_cat_name))."'
and option_values.deleted = 0
order by ".$sort."option_values.zorder, option_values.fvalue
";
$data = simple_query($sql);
return $data;
}
function get_option_cat_id($cat_name)
{
$sql = "
select id
from option_cat
where cat_name = '".sql_friendly($cat_name)."'
and deleted = 0
";
$data = simple_query($sql);
$row = mysqli_fetch_array($data);
return $row['id'];
}
function checkbox_value($field_name)
{
// takes the name of an input checkbox and returns 1 if it exists, or 0 if it doesn't
if(isset($_POST[$field_name])) return "1";
return "0";
}
function log_alert($user_id, $msg)
{
$sql = "
insert into user_alerts
(user_id,
linedate_added,
alert_msg)
values ('".sql_friendly($user_id)."',
now(),
'".sql_friendly($msg)."')
";
simple_query($sql);
}
//database
function simple_query($sql)
{
global $datasource;
global $debug_mode;
if($debug_mode)
{
$data = mysqli_query($datasource,$sql) or die("database query failed! <br>". mysqli_error($datasource) . "<pre>". $sql ."</pre>");
}
else
{
$data = mysqli_query($datasource,$sql) or die("General Error Occurred...");
}
return $data;
}
function sql_friendly($istring)
{
global $datasource;
if(get_magic_quotes_gpc())
{
$hold = stripslashes($istring);
}
else
{
$hold = $istring;
}
return mysqli_real_escape_string($datasource,$hold);
}
function mysql_user_function_exists($function_name)
{
// check to see if a user defined function in mysql already exists
global $db_name;
$sql = "SELECT * FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE='function' AND routine_name = '".sql_friendly($function_name)."' AND ROUTINE_SCHEMA='".sql_friendly($db_name)."'";
$data = simple_query($sql);
if(mysqli_num_rows($data)) {
return true;
} else {
return false;
}
}
function get_mysql_insert_id()
{
global $datasource;
return mysqli_insert_id($datasource);
}
function query_string_remove($query_string,$removestring)
{
$uQueryString = explode("&",$query_string);
$query_string = '';
foreach($uQueryString as $uVar)
{
if(preg_match("[^". $removestring ."]",$uVar) == 0) $query_string .= "&".$uVar;
}
return substr($query_string,1);
}
function sort_fields($default_sort_field = "id", $force_update = false)
{
//this function will create the session variable for the page that holds how we are sorting, and which direction (i.e. ascending, or descending)
global $SCRIPT_NAME;
global $page_name;
$page_name = substr($SCRIPT_NAME,strpos($SCRIPT_NAME,"/") + 1);
$page_name = substr($page_name,0,strpos($page_name,"."));
if(!isset($_SESSION['sort_field_'.$page_name]) || $force_update)
{
$_SESSION['sort_field_'.$page_name] = $default_sort_field;
$_SESSION['sort_field_direction_'.$page_name] = "desc";
}
if(isset($_GET['sort_field']))
{
if($_SESSION['sort_field_'.$page_name] == $_GET['sort_field'])
{
// switch sort direction
if($_SESSION['sort_field_direction_'.$page_name] == "asc")
{
$_SESSION['sort_field_direction_'.$page_name] = "desc";
}
else
{
$_SESSION['sort_field_direction_'.$page_name] = "asc";
}
}
else
{
$_SESSION['sort_field_direction_'.$page_name] = "asc";
$_SESSION['sort_field_'.$page_name] = $_GET['sort_field'];
}
}
}
function data_nav_bar($data_list, $record_start = 0,$records_per_page = 20)
{
//function to display the [previous] [next], page range, and search box on our data view pages
global $query_string;
if(!isset($_SESSION['results_per_page']))
{
$_SESSION['results_per_page'] = $records_per_page;
}
$use_query_string = query_string_remove($query_string, "eid");
$use_query_string = query_string_remove($use_query_string, "id");
$use_query_string = query_string_remove($use_query_string, "search");
$use_query_string = query_string_remove($use_query_string, "results_per_page");
if(isset($_POST['sbox']) && $_POST['sbox'] != "")
{
$extra_search = "&search=$_POST[sbox]";
}
else
{
$extra_search = "";
}
/* calculate our top record for this set */
$top_record = $record_start + $_SESSION['results_per_page'];
if($top_record > mysqli_num_rows($data_list)) $top_record = mysqli_num_rows($data_list);
?>
<table class='admin_menu' width='100%'>
<tr>
<form action='?<?=$use_query_string?>' method='post'>
<td width='1%'>
</td>
<td>
<b> <?=mysqli_num_rows($data_list)?> record(s) | viewing records <?=$record_start + 1?> - <?=$top_record?></b><br>
<?
if($record_start > 0)
{
$prev_no = $record_start - $_SESSION['results_per_page'];
if($prev_no < 0) $prev_no = 0;
echo "<a href='?$use_query_string&results_per_page=$_SESSION[results_per_page]&start_row=".$prev_no."$extra_search'>[Previous]</a>";
}
else
{
echo "[Previous]";
}
echo " ";
if(($record_start + $_SESSION['results_per_page']) < mysqli_num_rows($data_list))
{
echo "<a href='?$use_query_string&results_per_page=$_SESSION[results_per_page]&start_row=".($record_start + $_SESSION['results_per_page'])."$extra_search'>[Next]</a>";
}
else
{
echo "[Next]";
}
?>
</td>
</tr>
<tr>
<td> </td>
<td>
<b>Page:</b>
<?
// only show a max of 15 page links, first 5, mid 5, last 5
$max_links = 15;
$links_per_section = 5;
$page_count = mysqli_num_rows($data_list) / $_SESSION['results_per_page'];
$page_middle = $page_count / 2;
$max_dots = 5;
$page_start = $record_start / $_SESSION['results_per_page'];
for($i=0;$i < $page_count;$i++)
{
if($i < $links_per_section
|| ($i >= $page_middle && $i < ($page_middle + $links_per_section))
|| $i > ($page_count - $links_per_section)
|| ($i >= ($page_start - $links_per_section) && $i < ($page_start + $links_per_section))
)
{
echo "<a href='?$use_query_string&results_per_page=$_SESSION[results_per_page]&start_row=".($i*$_SESSION['results_per_page'])."$extra_search'>[".($i + 1)."]</a> ";
$dot_counter = 0;
}
else
{
if($dot_counter < $max_dots)
{
$dot_counter ++;
echo " . ";
}
}
}
?>
<br>
<b>Search:</b>
<input name="sbox" class='standard12'>
<input type='submit' value='Go'>
<?
if(isset($_POST['sbox']) && $_POST['sbox'] != "")
{
echo "<br><b>Current Search Term: '$_POST[sbox]'";
}
?>
</td>
</form>
</tr>
<tr>
<td> </td>
<td colspan='5'>
<b>Records per page: </b>
<a href='?results_per_page=20&<?=$use_query_string?><?=$extra_search?>'>[20]</a>
<a href='?results_per_page=50&<?=$use_query_string?><?=$extra_search?>'>[50]</a>
<a href='?results_per_page=100&<?=$use_query_string?><?=$extra_search?>'>[100]</a>
<a href='?results_per_page=200&<?=$use_query_string?><?=$extra_search?>'>[200]</a>
<a href='?results_per_page=99999&<?=$use_query_string?><?=$extra_search?>'>[all]</a>
</td>
</tr>
</table>
<? }
//users
function get_welcome_by_id($id,$cd=0)
{
if($id==0) return "";
$sql = "
select *
from users
where id = '".sql_friendly($id)."'
";
$data = simple_query($sql);
$row = mysqli_fetch_array($data);
if(trim($row['first_name']." ".$row['last_name'])!="" && $cd==0)
{
$disp_name = "<h2>Welcome, <span>".$row['first_name']." ".$row['last_name']."</span>";
if(trim($row['title'])!="") $disp_name.=", ".trim($row['title'])."";
$disp_name.="!</h2>";
}
else
{
if($cd==1) return $row['username'];
$disp_name = "<h2>Welcome, <span>".$row['username']."</span>";
}
$tab=$disp_name;
return $tab;
}
function get_tagline_trail($id=0)
{
$new_tab="";
//get tagline text.
$tagline="";
$sql = "
select *
from cce_messages
where deleted=0
and section='tagline'
order by linedate_added desc
limit 1
";
$data=simple_query($sql);
if($row=mysqli_fetch_array($data))
{
//see if user can edit this...
$res_tab=mrr_display_cce_message_pad($row['id'],1);
$editor="";
if(trim($res_tab)!="")
{
$editor="<i class='fa fa-pencil' style='color:#e19918;' title='Click to edit the tagline' onClick='allow_cce_message_edit2(".$row['id'].");'></i> ";
//$editor="<input type='button' class='buttonize btn btn-default add_new_btn' onClick='allow_cce_message_edit2(".$row['id'].");' value='Edit Tagline'>";
}
$tagline="<span id='cce_sub_".$row['id']."'>".$row['subject']."</span> ".$editor."".$res_tab."";
}
//...
$new_tab.="<div class='mrr_tagline'>".$tagline."</div><span id='bread_crumb_trail'></span>"; //
return $new_tab;
}
function mrr_show_important_dates($dater="")
{
$min_cal_list="";
if($dater=="") $dater=date("Y-m-d", time());
$mrr_adder="";
//find merchant dates first...acts as a default.
if($_SESSION['merchant_id'] == 0 && $_SESSION['selected_merchant_id'] > 0)
{
$mrr_adder.=" and important_dates.merchant_id='".sql_friendly($_SESSION['selected_merchant_id'])."'";
}
elseif($_SESSION['merchant_id'] > 0)
{
$mrr_adder.=" and important_dates.merchant_id='".sql_friendly($_SESSION['merchant_id'])."'";
}
//find store dates next...override merchant if set.
if($_SESSION['store_id'] == 0 && $_SESSION['selected_store_id'] > 0)
{
$mrr_adder.=" and (important_dates.store_id='".sql_friendly($_SESSION['selected_store_id'])."' or important_dates.store_id=0)";
}
elseif($_SESSION['store_id'] > 0)
{
$mrr_adder.=" and (important_dates.store_id='".sql_friendly($_SESSION['store_id'])."' or important_dates.store_id=0)";
}
$sql = "
select *
from important_dates
where linedate >= '".date("Y-m-d", strtotime($dater))." 00:00:00'
and deleted=0
and archived=0
".$mrr_adder."
order by linedate asc
limit 5
";
$data = simple_query($sql);
while($row = mysqli_fetch_array($data))
{
$allow_editor1=" ";
$allow_editor2=" ";
$allow_editor3=" "; //<img src='/images/spacer.png' alt='' border='0' width='17' height='15'>";
$edit_mode="";
$valid_user=check_user_edit_access('important_dates',$row['id'],$_SESSION['user_id']);
if($valid_user > 1)
{
$allow_editor1="<i class='fa fa-pencil' title='Click to edit this date' onClick='edit_important_date(".$row['id'].",1);'></i>";
$allow_editor2="<i class='fa fa-trash' title='Click to remove this date' onClick='edit_important_date(".$row['id'].",2);'></i>";
//$allow_editor3="<i class='fa fa-chevron-circle-down' title='Click to check it off (archive).' onClick='edit_important_date(".$row['id'].",3);'></i>";
}
$min_cal_list.="
<li class='important_date_id_".$row['id']."'>
<ul class='edit_icons'>
<li><a href='javascript: void(0);'>".$allow_editor1."</a></li>
<li><a href='javascript: void(0);'>".$allow_editor2."</a></li>
<li><a href='javascript: void(0);'>".$allow_editor3."</a></li>
</ul>
<span><small>".date("M Y", strtotime($row['linedate']))."</small><strong>".date("d", strtotime($row['linedate']))."</strong></span>
<p><span>".trim($row['date_description'])."</span>".trim($row['date_message'])."</p>
</li>
";
}
//Important Dates <input type='button' value='Add Important Date' class='buttonize'>
$tab="
<div class='mrr_button_left_margin'>
<button class='btn btn-default add_new_btn' name='create-new-date' id='create-new-date' type='button' onClick='edit_important_date(0,1);'>ADD NEW DATE</button>
</div>
<ul class='imp_date_list'>
".$min_cal_list."
</ul>
<div id='dialog_delete_date' title='Remove this Important date?' style='display:none;'>
<p><span class='ui-icon ui-icon-alert' style='float:left; margin:0 7px 20px 0;'></span>This Date will be permanently removed and cannot be recovered. Are you sure you want to delete it?</p>
</div>
";
return $tab;
}
function get_user_info_section_by_id($id)
{
if($id==0) return "";
$sql = "
select *
from users
where id = '".sql_friendly($id)."'
";
$data = simple_query($sql);
$row = mysqli_fetch_array($data);
$store_display="All";
if($row['store_id'] > 0)
{
$sqlx = "
select *
from store_locations
where id = '".sql_friendly($row['store_id'])."'
";
$datax = simple_query($sqlx);
$rowx = mysqli_fetch_array($datax);
$store_display="".$rowx['store_number'].": ".$rowx['store_name']."";
}
$edit_link="";
$edit_mode="";
$valid_user=check_user_edit_access('users',$row['id'],$_SESSION['user_id']);
if($valid_user==1) $edit_mode="readonly";
if($valid_user==2)
{
$edit_link="
<div style='float:right; margin-right:25px;'>
<input type='button' class='buttonize' value='Edit' onClick='select_user_id(".$row['id'].",\"".$edit_mode."\");'>
</div>
";
}
//Account Information
$tab="
<div id='user_info_section'>
<div class='user_info_title' style='height:35px;'>
".$edit_link."
</div>
<div class='user_info_body'>
<div class='user_info_row'>
<div class='user_info_first'>
<div class='user_info_label'>First Name</div>
<div class='user_info_data'>".$row['first_name']."</div>
</div>
<div class='user_info_last'>
<div class='user_info_label'>Last Name</div>
<div class='user_info_data'>".$row['last_name']."</div>
</div>
<div class='user_info_stores'>
<div class='user_info_label'>Store Location</div>
<div class='user_info_data'>".$store_display."</div>
</div>
</div>
<div class='user_info_row'>
<div class='user_info_email'>
<div class='user_info_label'>Username</div>
<div class='user_info_data'>".$row['username']."</div>
</div>
<div class='user_info_pass'>
<div class='user_info_label'>Password</div>
<div class='user_info_data'>********</div>
</div>
</div>
</div>
</div>
";
$tab="
<table class='table table-striped'>
<tbody>
<tr>
<td>FIRST NAME<br><span>".$row['first_name']."</span></td>
<td>LAST NAME<br><span>".$row['last_name']."</span></td>
<td>STORE LOCATION<br><span>".$store_display."</span></td>
</tr>
<tr>
<td>USERNAME<br><span>".$row['username']."</span></td>
<td>PASSWORD<br><span>************</span></td>
<td>E-MAIL<br><span>".$row['email']."</span></td>
</tr>
</tbody>
</table>
";
return $tab;
}
function mrr_add_login_attempt($use_userid,$user,$invalid_password)
{
$sql = "
insert into log_login
(user_id,
username,
ip_address,
linedate_added,
invalid_password)
values ('".sql_friendly($use_userid)."',
'".sql_friendly($user)."',
'".sql_friendly($_SERVER['REMOTE_ADDR'])."',
now(),
'".sql_friendly($invalid_password)."')
";
simple_query($sql);
}
function mrr_reset_acct_pass($try_id,$try_user)
{
$error="";
if($try_id > 0 && $try_user!="")
{
$sql = "select users.*
from users
where id = '".sql_friendly($try_id)."'
and username='".sql_friendly($try_user)."'
";
$data = simple_query($sql);
if($row = mysqli_fetch_array($data))
{
$use_pswd=mrr_encryptor("reset_password",$try_user);
$sql="
update users set
password='".sql_friendly($use_pswd)."',
reset_password=1,
linedate_failed='0000-00-00 00:00:00',
failed_logins=0
where id='".sql_friendly($try_id)."'
and username='".sql_friendly($try_user)."'
";
simple_query($sql);
$error = 'Password reset, please check your E-mail log in again.';
//header("Location: login.php");
//exit;
}
else
{
$error = 'Account not found.';
}
}
return $error;
}
function mrr_reset_acct_pass_email($e_addr)
{
$error_email="";
global $datasource;
if($e_addr!="")
{
$sql = "select users.*
from users
where email = '" . sql_friendly($e_addr) . "'
and deleted=0
and archived=0
";
$data = simple_query($sql);
$row = mysqli_fetch_array($data);
if(is_array($row))
{
$uuid = createuuid()."-".createuuid()."-".uniqid(true);
$sql = "
insert into users_reset_pass
(user_id,
linedate_added,
deleted,
uuid,
email_to)
values ('".sql_friendly($row['id'])."',
now(),
0,
'".sql_friendly($uuid)."',
'".sql_friendly($e_addr)."')
";
$data = simple_query($sql);
$reset_id = mysqli_insert_id($datasource);
$id=$row['id'];
$us=trim($row['username']);
$ps="*********************";
$error_email = "";
mrr_send_login_email($e_addr,$us,$ps,$id, $reset_id);
if(trim($error_email)=="") $email_success = 1;
}
}
else
{
$error_email = $lang['login_emailer'];
}
return $error_email;
}
function mrr_find_user_roles($cust=0,$store=0,$id=0)
{
$roles=0;
if($cust > 0)
{ //if no roles delete all in customer
$sql2 = "
select id
from users
where merchant_id = '" . sql_friendly($cust) . "'
and access_level<='61'
and deleted=0
";
$data2 = simple_query($sql2);
while($row2 = mysqli_fetch_array($data2))
{
$pos=0;
$uid=$row2['id'];
//customer or group roles
$sql = "
select count(*) as mrr_cntr
from merchants
where (co_user_id = '" . sql_friendly($uid) . "' or group_user_id = '" . sql_friendly($uid) . "')
and id!='" . sql_friendly($cust) . "'
and deleted=0
";
$data = simple_query($sql);
if($row = mysqli_fetch_array($data))
{
$pos+=$row['mrr_cntr'];
}
//store roles
$sql = "
select count(*) as mrr_cntr
from store_locations
where cm_user_id = '" . sql_friendly($uid) . "'
and merchant_id!='" . sql_friendly($cust) . "'
and deleted=0
";
$data = simple_query($sql);
if($row = mysqli_fetch_array($data))
{
$pos+=$row['mrr_cntr'];
}
if($pos==0)
{ //no positions, so flag for removal
$sql3 = "update users set deleted='1' where id = '" . sql_friendly($uid) . "'";
simple_query($sql3);
}
}
}
elseif($store > 0)
{ //if no roles, delete all in users in this store
$sql2 = "
select id
from users
where store_id = '" . sql_friendly($store) . "'
and access_level<='40'
and deleted=0
";
$data2 = simple_query($sql2);
while($row2 = mysqli_fetch_array($data2))
{
$pos=0;
$uid=$row2['id'];
//customer or group roles
$sql = "
select count(*) as mrr_cntr
from merchants
where (co_user_id = '" . sql_friendly($uid) . "' or group_user_id = '" . sql_friendly($uid) . "')
and deleted=0
";
$data = simple_query($sql);
if($row = mysqli_fetch_array($data))
{
$pos+=$row['mrr_cntr'];
}
//store roles
$sql = "
select count(*) as mrr_cntr
from store_locations
where cm_user_id = '" . sql_friendly($uid) . "'
and store_id!='" . sql_friendly($store) . "'
and deleted=0
";
$data = simple_query($sql);
if($row = mysqli_fetch_array($data))
{
$pos+=$row['mrr_cntr'];
}
if($pos==0)
{ //no positions, so flag for removal
$sql3 = "update users set deleted='1' where id = '" . sql_friendly($uid) . "'";
simple_query($sql3);
}
}
}
else
{
//customer or group roles
$sql = "
select count(*) as mrr_cntr
from merchants
where (co_user_id = '" . sql_friendly($id) . "' or group_user_id = '" . sql_friendly($id) . "')
and deleted=0
";
$data = simple_query($sql);
if($row = mysqli_fetch_array($data))
{
$roles+=$row['mrr_cntr'];
}
//store roles
$sql = "
select count(*) as mrr_cntr
from store_locations
where cm_user_id = '" . sql_friendly($id) . "'
and deleted=0
";
$data = simple_query($sql);
if($row = mysqli_fetch_array($data))
{
$roles+=$row['mrr_cntr'];
}
}
return $roles;
}
function mrr_remove_account($id)
{
$sql="update users set deleted='1' where id='".sql_friendly($id)."'";
simple_query($sql);
//remove from merchant linkage
$sql="update merchants set co_user_id='0' where co_user_id='".sql_friendly($id)."'";
simple_query($sql);
$sql="update merchants set group_user_id='0' where group_user_id='".sql_friendly($id)."'";
simple_query($sql);
//remove from store linkage
$sql="update store_locations set cm_user_id='0' where cm_user_id='".sql_friendly($id)."'";
simple_query($sql);
}
function mrr_update_account($id,$level,$arch,$email,$first,$last,$merch,$store,$title,$logs,$phone1,$phone2)
{
if($merch==0) $store=0;
$sql="
update users set
access_level='".sql_friendly($level)."',
archived='".sql_friendly($arch)."',
first_name='".sql_friendly($first)."',
last_name='".sql_friendly($last)."',
merchant_id='".sql_friendly($merch)."',
store_id='".sql_friendly($store)."',
title='".sql_friendly($title)."',
monitor_logs='".sql_friendly($logs)."',
contact_phone1='".sql_friendly($phone1)."',
contact_phone2='".sql_friendly($phone2)."',
contact_phone3='',
contact_phone4='',
email='".sql_friendly($email)."'
where id = '".sql_friendly($id)."'
";
simple_query($sql);
if($id == $_SESSION['user_id']) {
// we're editing ourselves, make sure the other related session information is updated (if it changed)
$_SESSION['merchant_id'] = $merch;
$_SESSION['store_id'] = $store;
}
if($level ==60 && $merch>0)
{ //if selected update the merchant to use this user as the CO.
$sql = "
update merchants set
co_user_id='".sql_friendly($id)."'
where id='".sql_friendly($merch)."'
";
simple_query($sql);
}
elseif($level ==70 && $merch>0)
{ //if selected update the merchant to use this user as the Group Manager.
$sql = "
update merchants set
group_user_id='".sql_friendly($id)."'
where id='".sql_friendly($merch)."'
";
simple_query($sql);
}
elseif($level ==40 && $merch>0 && $store > 0)
{ //if selected update the merchant to use this user as the Group Manager.
$sql = "
update store_locations set
cm_user_id='".sql_friendly($id)."'
where id='".sql_friendly($store)."'
";
simple_query($sql);
}
}
function mrr_update_account_pass($id,$user,$pass,$confirm)
{
if(trim($pass)!="" && trim($confirm)!="" && trim($user)!="" && trim($pass)==trim($confirm) )
{
$newpass= mrr_encryptor(trim($pass),trim($user) );
$sql="
update users set
password='".sql_friendly( $newpass )."',
reset_password=0,
linedate_failed='0000-00-00 00:00:00',
failed_logins=0
where id = '".sql_friendly($id)."'
";
simple_query($sql);
//return 1;
}
return 0;
}
function mrr_create_account($user,$pass,$e_addr,$utype=0)
{
global $lang;
$error_new="";
$newid=0;
$auto_edit=0;
if(isset($_SESSION['user_id']) && $_SESSION['user_id'] > 0 && trim($_SESSION['username'])!="") $auto_edit=1; //current user is already logged in, so this new user can be edited by him/her.
//set merchant if possible.
$use_merchant=0;
if($_SESSION['merchant_id'] == 0 && $_SESSION['selected_merchant_id'] > 0)
{
$use_merchant=$_SESSION['selected_merchant_id'];