/var/www/hkosl.com/imusiccircle/webadmin/_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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
<?php
    
require_once("configure.php");

    
/*if (!function_exists('escape_string')) {
        function escape_string($data){

            //return ($data);

            $replacements = array(
                    "'" => '&#039;',
                    '"' => '&quot;',
                    //"\\" => '\\\\',
            );
            return strtr($data, $replacements);
        }
    }*/

    
if (!function_exists('bind_pdo')) {
        function 
bind_pdo($sql$parameters NULL$action NULL$escape true)
        {
            global 
$dbh;

            if (
$action == "insert" || $action == "update" || $action == "delete" || empty($action)) {
                if (!(
$sth $dbh->prepare($sql))) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                if (!
$sth->execute($parameters)) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }
            }

            if (
$action == "selectone") {
                if (!(
$sth $dbh->prepare($sql))) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                if (!
$sth->execute($parameters)) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                
$result $sth->fetch(PDO::FETCH_ASSOC);
                
/*if (!empty($result)) {
                    foreach ($result as $key => $row) {
                        $result[$key] = escape_string($result[$key]);
                    }
                }*/
                
return $result;

            }

            if (
$action == "selectall") {
                if (!(
$sth $dbh->prepare($sql))) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                if (!
$sth->execute($parameters)) {
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                
$result $sth->fetchAll(PDO::FETCH_ASSOC);
                
/*if (!empty($result)) {
                    foreach ($result as $key => $row) {
                        foreach ($row as $key2 => $row2) {
                            $result[$key][$key2] = escape_string($result[$key][$key2]);
                        }
                    }
                }*/
                
return $result;
            }

            if (
$action == "dump") {
                return 
dump_sql($sql$parameters);
            }

        }
    }


    if (!
function_exists('dump_sql')) {
        function 
dump_sql($sql$parameters)
        {
            
$keys = array();

            
# build a regular expression for each parameter
            
foreach ($parameters as $key => $value) {
                if (
is_string($key)) {
                    
$keys[] = '/:' $key '/';
                } else {
                    
$keys[] = '/[?]/';
                }
            }

            foreach (
$parameters as $key2 => $value) {
                
$parameters[$key2] = "'" $value "'";
            }

            
$sql preg_replace($keys$parameters$sql1$count);

            
#trigger_error('replaced '.$count.' keys');

            
return $sql;
        }
    }

    if (!
function_exists('paypal_log')) {
        function 
paypal_log($code$data)
        {
            
/*$date = new DateTime();
            $date->modify("-2 month");

            $sql = "delete from api_log where createdate <= ?";
            $parameters = array($date->format("Y-m-d"));
            bind_pdo($sql, $parameters);*/

            
unset($data[1]["nvpReqArray"]["PWD"]);
            unset(
$data[1]["nvpReqArray"]["USER"]);
            unset(
$data[1]["nvpReqArray"]["SIGNATURE"]);

            
$sql        "insert into paypal_log (code, log_data, createdate) value (?,?,?)";
            
$parameters = array($codejson_encode($data), date("Y-m-d H:i:s"));
            
bind_pdo($sql$parameters);
        }
    }

    if (!
function_exists('debug_log')) {
        function 
debug_log($code$data)
        {
            
$date = new DateTime();
            
$date->modify("-2 month");

            
$sql        "delete from debug_log where createdate <= ?";
            
$parameters = array($date->format("Y-m-d"));
            
bind_pdo($sql$parameters);

            
$sql        "insert into debug_log (code, log_data, createdate) value (?,?,?)";
            
$parameters = array($codejson_encode($data), date("Y-m-d H:i:s"));
            
bind_pdo($sql$parameters);
        }
    }

    
/***************************************************
     * _site info
     */

    
$sql        "SELECT * FROM site_info WHERE siteinfoid = ? ";
    
$parameters = array(1);
    
$row0       bind_pdo($sql$parameters"selectone");
    
$site_info  $row0;


    
/***************************************************
     * _language setting
     */

    
$sql           "SELECT * FROM sys_lang WHERE langstatus = 1 ORDER BY langsort ASC ";
    
$sys_lang_info bind_pdo($sqlNULL"selectall");
    foreach (
$sys_lang_info as $sys_lang) {
        
$arraylangcode[$sys_lang['langcode']] = $sys_lang['langname'];
    }

    if (!
function_exists('get_client_ip')) {
        function 
get_client_ip()
        {
            
$ipaddress '';
            if (isset(
$_SERVER['HTTP_CLIENT_IP']))
                
$ipaddress $_SERVER['HTTP_CLIENT_IP'];
            else if (isset(
$_SERVER['HTTP_X_FORWARDED_FOR']))
                
$ipaddress $_SERVER['HTTP_X_FORWARDED_FOR'];
            else if (isset(
$_SERVER['HTTP_X_FORWARDED']))
                
$ipaddress $_SERVER['HTTP_X_FORWARDED'];
            else if (isset(
$_SERVER['HTTP_FORWARDED_FOR']))
                
$ipaddress $_SERVER['HTTP_FORWARDED_FOR'];
            else if (isset(
$_SERVER['HTTP_FORWARDED']))
                
$ipaddress $_SERVER['HTTP_FORWARDED'];
            else if (isset(
$_SERVER['REMOTE_ADDR']))
                
$ipaddress $_SERVER['REMOTE_ADDR'];
            else
                
$ipaddress 'UNKNOWN';
            return 
$ipaddress;
        }
    }


    if (!
function_exists('get_langcode')) {
        function 
get_langcode()
        {
            if (
strpos($_SERVER["REQUEST_URI"], "webadmin") !== false) {
                if (!isset(
$_SESSION["wlangcode"])) {
                    
$_SESSION["wlangcode"] = "en";
                }

                
$this_langcode $_SESSION["wlangcode"];
            } else {
                if (!isset(
$_SESSION["langcode"])) {
                    
$_SESSION["langcode"] = "cn";
                }

                
$this_langcode $_SESSION["langcode"];
            }

            return 
$this_langcode;
        }
    }

    
//$langcode = get_langcode();

    /*if (!function_exists('lang')) {
        function lang($text, $lang=null)
        {
            if(empty($lang)){
                $this_langcode = get_langcode();
            }else{
                $this_langcode = $lang;
            }

            $sql        = "select * from `language` where BINARY name_en = ? and deleted = ? limit 1";
            $parameters = array($text, 0);
            $result     = bind_pdo($sql, $parameters, "selectone");

            if (!empty($result)) {
                return $result["name_" . $this_langcode];
            } else {
                //temp

                if(true){
                    $sql = "select * from `language_pending` where BINARY name_en = ? and deleted = ? limit 1";
                    $parameters = array($text, 0);
                    $is_pending = bind_pdo($sql, $parameters, "selectone");
                    if(empty($is_pending)){
                        $sql = "INSERT INTO `language_pending` (name_en, deleted) VALUES (?, ?)";
                        $parameters = array($text, 0);
                        bind_pdo($sql, $parameters, "insert");
                    }
                }
                return $text;
                //return $text . "(missing)";
                //return null;
            }
        }
    }*/

    
if (!function_exists('_h')) {
        function 
_h($str)
        {
            return 
htmlspecialchars($strENT_QUOTES);
        }
    }

    
// lang() with html escapse
    
if (!function_exists('_lang')) {
        function 
_lang($text$lang null)
        {
            
// TODO: escapse the translated string after frontend is changed
            
return _h(call_user_func_array('lang'func_get_args()));
            
//return (lang($text));
        
}
    }

    if (!
function_exists('get_master_type_code')) {
        function 
get_master_type_code($typeid$code NULL)
        {
            if (!empty(
$code)) {
                
$sql        "select * from master_type_code where typeid = ? and code = ? and deleted = ? order by sort ASC, name_en ASC";
                
$parameters = array($typeid$code0);
                
$result     bind_pdo($sql$parameters"selectone");
            } else {
                
$sql        "select * from master_type_code where typeid = ? and deleted = ? order by sort ASC, name_en ASC";
                
$parameters = array($typeid0);
                
$result     bind_pdo($sql$parameters"selectall");
            }

            return 
$result;
        }
    }

    if (!
function_exists('get_master_type_code_by_id')) {
        function 
get_master_type_code_by_id($id NULL)
        {
            if (!empty(
$id)) {
                
$sql        "select * from master_type_code where id = ? ";
                
$parameters = array($id);
                
$result     bind_pdo($sql$parameters"selectone");
            } else {
                
$sql        "select * from master_type_code where deleted = ? order by name_en ASC";
                
$parameters = array(0);
                
$result     bind_pdo($sql$parameters"selectall");
            }

            return 
$result;
        }
    }

    if (isset(
$_GET["msg"]) && (int)$_GET["msg"] == $_GET["msg"]) {
        
$msg_master_info get_master_type_code("WEBADMIN"$_GET["msg"]);
        
$msg             $msg_master_info["name_" $_SESSION["wlangcode"]];
        
$_GET["msg"]     = $msg_master_info["name_" $_SESSION["wlangcode"]];
    }

    if (!
function_exists('random_string')) {
        function 
random_string($length 8)
        {
            
$chars         "abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ23456789";
            
$random_string substr(str_shuffle($chars), 0$length);
            return 
$random_string;
        }
    }

    if (!
function_exists('last_url')) {
        function 
last_url()
        {
            
$protocol strpos(strtolower($_SERVER['SERVER_PROTOCOL']), 'https')
            === 
false 'http' 'https';
            
$host     $_SERVER['HTTP_HOST'];
            
/*$script   = $_SERVER['SCRIPT_NAME'];
            $params   = $_SERVER['QUERY_STRING'];*/

            //$lastUrl = $protocol . '://' . $host . $script . '?' . $params;

            
$path    $_SERVER['REQUEST_URI'];
            
$lastUrl $protocol '://' $host $path;

            return 
$lastUrl;
        }

    }

    
//useless
    
if (!function_exists('aes_crypt')) {
        function 
aes_crypt($data$encrypt_decrypt)
        {

            
/*set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib0.3.8');
            include('Net/SSH2.php');
            include('Crypt/RSA.php');*/

            
set_include_path(get_include_path() . PATH_SEPARATOR 'phpseclib1.0.1');

            if (!
class_exists('Crypt_AES')) {
                include_once(
'phpseclib1.0.1/Crypt/AES.php');
            }

            include_once(
'phpseclib1.0.1/Crypt/Random.php');

            
$aes_crypt = new Crypt_AES();

            
$aes_crypt->setKey('*9Wq*LGBE8GJch?wZUC+8n?WzZb*Y*Lq');
            
//$aes_crypt->setIV(crypt_random_string($aes_crypt->getBlockLength() >> 3));
            
$aes_crypt->setIV('d$w5aQCY-STP*MG_hfDxP4eC?uDP@szr');

            if (
$encrypt_decrypt == 1) {
                if (isset(
$data)) {
                    return 
base64_encode($aes_crypt->encrypt($data));
                } else {
                    return;
                }
            }

            if (
$encrypt_decrypt == 2) {
                if (isset(
$data)) {
                    return 
$aes_crypt->decrypt(base64_decode($data));
                } else {
                    return;
                }
            }
        }
    }

    if (!
function_exists('aes_crypt2')) {
        function 
aes_crypt2($data$encrypt_decrypt)
        {

            
/*set_include_path(get_include_path() . PATH_SEPARATOR . 'phpseclib0.3.8');
            include('Net/SSH2.php');
            include('Crypt/RSA.php');*/

            
set_include_path(get_include_path() . PATH_SEPARATOR 'phpseclib1.0.1');

            if (!
class_exists('Crypt_AES')) {
                include_once(
'phpseclib1.0.1/Crypt/AES.php');
            }

            include_once(
'phpseclib1.0.1/Crypt/Random.php');

            
$aes_crypt = new Crypt_AES();

            
$aes_crypt->setKey('*9Wq*LGBE8GJch?wZUC+8n?WzZb*Y*Lq');
            
//$aes_crypt->setIV(crypt_random_string($aes_crypt->getBlockLength() >> 3));
            
$aes_crypt->setIV('d$w5aQCY-STP*MG_hfDxP4eC?uDP@szr');

            if (
$encrypt_decrypt == 1) {
                if (isset(
$data)) {
                    return 
base64_encode($aes_crypt->encrypt($data));
                } else {
                    return;
                }
            }

            if (
$encrypt_decrypt == 2) {
                if (isset(
$data)) {
                    return 
$aes_crypt->decrypt(base64_decode($data));
                } else {
                    return;
                }
            }
        }
    }


    if (!
function_exists('matched_option')) {
        function 
matched_option($data1$data2$type)
        {
            if (
$data1 == $data2) {
                if (
$type == "checkbox" || $type == "radiobutton") {
                    return 
"checked";
                } else if (
$type == "select") {
                    return 
"selected";
                } else {
                }

            }
        }
    }

    if (!
function_exists('get_config_setting')) {
        function 
get_config_setting($config_id)
        {
            global 
$dbh;

            
$sql        "select * from config where old_record = ? and config_id = ? ";
            
$parameters = array("0"$config_id);
            if (!(
$sth $dbh->prepare($sql))) {
                throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
            }

            if (!
$sth->execute($parameters)) {
                throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
            }

            
$config_info $sth->fetch(PDO::FETCH_ASSOC);
            return 
$config_info;
        }
    }


    if (!
function_exists('select_options')) {
        function 
select_options($rows$selected_value$settings = array())
        {
            
$settings['value_field'] = $settings['value_field'] ?: 'code';
            
$settings['label_fn']    = $settings['label_fn'] ?: function ($row) {
                return 
"{$row['code']} - {$row['name_en']}";
            };

            
$str '';
            foreach (
$rows as $row) {
                
$str .= "<option value='" $row[$settings['value_field']] . "' " . ($row[$settings['value_field']] == $selected_value "selected='selected'" "") . ">" $settings['label_fn']($row) . "</option>";
            }
            return 
$str;
        }
    }


    if (!
function_exists('startsWith')) {
        function 
startsWith($haystack$needle)
        {
            
$length strlen($needle);
            return (
substr($haystack0$length) === $needle);
        }
    }

    if (!
function_exists('check_upload_path')) {
        function 
check_upload_path($img_file)
        {
            
$session_path_str  "/uploader/" $_SESSION['KCFINDER']['uploadURL'];
            
$session_path_long strlen($session_path_str);
            
$path_error        0;
            foreach (
$img_file as $key2 => $pathname) {
                if (
$key2 && $pathname) {
                    
$submit_path_str  substr($pathname0$session_path_long);
                    
$submit_path_long strlen($submit_path_str);
                    
$file             str_replace('..'''$pathname);
                    if (
$session_path_long <> $submit_path_long || $session_path_str <> $submit_path_str || !startsWith($file$session_path_str)) {
                        
$path_error 1;
                    }
                }
            }
        }
    }

    if (!
function_exists('get_order_status')) {
        function 
get_order_status($status null)
        {
            
$array = array('approved' => '處理中''refunded' => '處理中''process' => '處理中''paid' => '已付款''cancel' => '已取消''fail' => '交易失易''reject' => '已拒絕');
            return 
$array[$status];
        }
    }

    if (!
function_exists('get_course_detail')) {
        function 
get_course_detail($course_id null$status 2$deleted 0)
        {
            global 
$dbh;
            if (
$course_id) {
                
$sql        "select o.*,od.id as id2,od.complaint,od.datetime,od.tutor_status,od.student_status,od.solution_status, od.deduct_tutorial_mins, od.deduct_tutorial_mins_date from order_detail as od,`order` as o where o.id = od.order_id and od.id = ? and o.deleted = ? and o.status='paid'";
                
$parameters = array($course_id$deleted);
                return 
bind_pdo($sql$parameters"selectone");
            }
        }
    }

    if (!
function_exists('get_order')) {
        function 
get_order($order_id null$status$deleted 0)
        {
            global 
$dbh;
            if (
$order_id) {
                
$sql        "select * from `order` where id = ? and deleted = ? LIMIT 1";
                
$parameters = array($order_id$deleted);
                return 
bind_pdo($sql$parameters"selectone");
            } else {
                if (
$status) {
                    if (
$status == 'process') {
                        
$exsql 'and status in ("process","approved")';
                    } else {
                        
$exsql 'and status = "' $status '"';
                    }
                }
                
$sql        "select * from `order` where deleted = ? " $exsql " order by createdate desc";
                
$parameters = array($deleted);
                return 
bind_pdo($sql$parameters"selectall");
            }
        }
    }

    if (!
function_exists('send_order_notification')) {
        function 
send_order_notification($status$id)
        {
            
//
            
if ($status == 'approved' || $status == 'process' || $status == 'paid' || $status == 'reject' || $status == 'cancel') {
                
$sql                        "SELECT * FROM site_info WHERE siteinfoid = ? ";
                
$parameters                 = array(1);
                
$site_info                  bind_pdo($sql$parameters"selectone");
                
$lang                       'cn';
                
$sender_name                $site_info['companyname_' $lang];
                
$sender_email               $site_info['enquiryemail'];
                
$content_arr['member_name'] = $record_member['name'];
                if (
$lang == 'cn') {
                    
$subject $site_info['companyname_' $lang] . ' - 訂單處理狀況';
                }
                
$sql          "select * from `order` as tb 
                        where `id` = ? and deleted = ? LIMIT 1"
;
                
$parameters   = array($id0);
                
$record_order bind_pdo($sql$parameters"selectone");
                
$sender_name  $site_info['companyname_' $lang];
                if (
$status == 'paid') {
                    
$sql         "select datetime from order_detail where order_id = ?";
                    
$parameters  = array($record_order['id']);
                    
$rows_detail bind_pdo($sql$parameters"selectall");

                    
$sql        "select postjob_no,mas_instrument from student_postjob where id = ? and deleted = ?";
                    
$parameters = array($record_order['job_id'], 0);
                    
$row_job    bind_pdo($sql$parameters"selectone");

                    
$sql        "select tutor_no,nickname as name from tutor_main where id = ? and deleted = ?";
                    
$parameters = array($record_order['tutormain_id'], 0);
                    
$row_tutor  bind_pdo($sql$parameters"selectone");

                    
$sql         "select email,student_no,contactname as name from student_main where id = ? and deleted = ?";
                    
$parameters  = array($record_order['studentmain_id'], 0);
                    
$row_student bind_pdo($sql$parameters"selectone");

                    
$sql              "select name_cn from master_type_code where id = ? and typeid = ? and deleted = ?";
                    
$parameters       = array($record_order['mas_residencecode'], 'TEACH_POSITIONCODE'0);
                    
$row_positioncode bind_pdo($sql$parameters"selectone");

                    
$sql                      "select name_cn from master_type_code where id = ? and typeid = ? and deleted = ?";
                    
$parameters               = array($record_order['course_venus'], 'TEACH_VENUS'0);
                    
$row_venus                bind_pdo($sql$parameters"selectone");
                    
$sql                      "select name_en from master_type_code where id = ? and typeid = 'INSTRUMENT' LIMIT 1";
                    
$parameters               = array($row_job['mas_instrument']);
                    
$row_instrument           bind_pdo($sql$parameters"selectone");
                    
$content_arr['titlename'] = $site_info['companyname_' $lang] . '- ' $record_order['order_no'] . '確認付款';
                    
$content_arr['msg']       = '你好,<br><br>
                                           我們已經確認了你的付款
                                           <br><br>
                                           <table width="100%" border="0" cellpadding="0" cellspacing="1" style="font-size:12px;">
                                              <tr>
                                                <th width="120" align="left" valign="top" class="">Paypal 編號</th>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">' 
$record_order['paypal_id'] . '</div></td>
                                              </tr>
                                              <tr>
                                                <th width="120" align="left" valign="top" class="">Paypal 處理日期</th>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">
                                                    ' 
$record_order['paypal_date'] . '
                                                  </div></td>
                                              </tr>
                                              <tr>
                                                <th width="120" align="left" valign="top" height="40">' 
lang("Order No.") . '</th>
                                                <td valign="top" class="">&nbsp;</td>
                                                <td valign="top" class=""><div class="col-sm-8 form-group">' 
$record_order['order_no'] . '</div></td>
                                              </tr>
                                              <tr>
                                                <th width="120" align="left" valign="top">' 
lang("工作號碼") . '</th>
                                                <td valign="top" class="">&nbsp;</td>
                                                <td valign="top" class=""><div class="col-sm-8 form-group">
                                                    ' 
$row_job['postjob_no'] . '
                                                  </div></td>
                                              </tr>
                                              <tr>
                                                <th width="120" align="left" valign="top">學生</th>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">' 
$row_student['student_no'] . ' ' $row_student['name'] . '</div></td>
                                              </tr>
                                              <tr>
                                                <th width="120" align="left" valign="top">導師</th>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">' 
$row_tutor['tutor_no'] . ' ' $row_tutor['name'] . '</div></td>
                                              </tr>
                                              <tr>
                                                <td width="120" align="left" valign="top" class="">上課地區</td>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">
                                                    ' 
$row_positioncode["name_cn"] . '
                                                  </div></td>
                                              </tr>
                                              <tr>
                                                <td width="120" align="left" valign="top" class="">上課地區</td>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">' 
$row_venus['name_cn'] . '</div></td>
                                              </tr>
                                              <tr>
                                                <td width="120" align="left" valign="top" class="">學習級數</td>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">'
;
                    if (
$record_order['order_type'] == 'try') {
                        if (
$record_order['level'] == 1) {
                            
$content_arr['msg'] .= '1 - 2';
                        } elseif (
$record_order['level'] == 2) {
                            
$content_arr['msg'] .= '3 - 5';
                        } elseif (
$record_order['level'] == 3) {
                            
$content_arr['msg'] .= _lang("6 or above 6");
                        }
                    } else if (
$record_order['order_type'] == 'formal') {
                        if (
$record_order['level'] < 8) {
                            
$content_arr['msg'] .= $record_order['level'];
                        } else if (
$record_order['level'] == 9) {
                            
$content_arr['msg'] .= '8級以上';
                        }
                    }
                    
$content_arr['msg'] .= '
                                                  </div></td>
                                              </tr>
                                              <tr>
                                                <td width="120" align="left" valign="top" class="">學習時間長度</td>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">
                                                    ' 
$record_order['course_length'] . ' ' _lang("mins") . '
                                                  </div></td>
                                              </tr>
                                              <tr>
                                                <td width="120" align="left" valign="top" class="">學習堂數</td>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">' 
$record_order['course_count'] . '
                                                  </div></td>
                                              </tr>
                                              <tr>
                                                <td width="120" align="left" valign="top" class="">上課日期 / 時間</td>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group">'
;
                    foreach (
$rows_detail as $i => $row_detail) {
                        
$content_arr['msg'] .= '
                                                    <div>' 
. ($i 1) . '. ' $row_detail['datetime'] . '</div>
                                                    <div>'
;
                        if (
$row['status'] == 'paid') {
                            
$content_arr['msg'] .= '
                                                    <a href="course_form.php?id=' 
$row_detail['id'] . '" class="btn btn-default"><i class="fa fa-link" aria-hidden="true"></i> 課堂資料</a>';
                        }
                        
$content_arr['msg'] .= '
                                                    </div>'
;
                    }
                    
$content_arr['msg'] .= '
                                                  </div></td>
                                              </tr>
                                              <tr>
                                                <td width="120" align="left" valign="top" class="">每堂上課費用</td>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group"> $ ' 
intval($record_order['fee']) . '</div></td>
                                              </tr>
                                              <tr>
                                                <td width="120" align="left" valign="top" class="">每堂上門附加費</td>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group"> $ ' 
intval($record_order['extra_fee']) . '</div></td>
                                              </tr>
                                              <tr>
                                                <td width="120" align="left" valign="top" class="">總費用</td>
                                                <td width="5" valign="top" class="">:&nbsp;</td>
                                                <td class=""><div class="col-sm-8 form-group"> $ <span>' 
. (intval($record_order['total_fee']) - intval($record_order['coupon_fee'])) . '</span>
                                                  </div></td>
                                              </tr>'
;
                    if (
$row['coupon_code']) {
                        
$content_arr['msg'] .= '
                                                  <tr>
                                                    <td width="120" align="left" valign="top" class="">優惠劵折扣</td>
                                                    <td width="5" valign="top" class="">:&nbsp;</td>
                                                    <td class=""><div class="col-sm-8 form-group">- $ <span>' 
. (intval($row['coupon_fee'])) . '</span>
                                                      </div></td>
                                                  </tr>
                                                  '
;
                    }
                    
$content_arr['msg'] .= '
                                            </table>
                                       <br>
                                       請登入<a href="'
.$site_info["url"].'" target="_blank">'.$site_info["url"].'</a>繼續享用我們為你提供的服務。
                                       <br><br>
                                       如有任何問題,請電郵至cs@musiccircle.hk跟我們的客戶服務部聯絡。
                                       <br><br>
                                       MusicCircle 客戶服務
                                       '
;
                    
$content_arr['date'] = date('Y-m-d');
                    
$content_arr['time'] = date('H:i:s');
                    
$map_file            'email/page.html';
                    
$to                  aes_crypt($row_student['email'], 2);
                    
sendEmail($to$site_info['companyname_' $lang] . '- ' '確認付款'$content_arr$sender_name$sender_email$map_file);
                    
$to 'cs@musiccircle.hk';
                    
$to 'benny2507@yhaoo.com.hk';
                    
sendEmail($to$site_info['companyname_' $lang] . '- ' '確認付款'$content_arr$sender_name$sender_email$map_file);
                    
$row_member get_teacher($record_order['tutormain_id']);
                    
$name       '學生';
                } else if (
$record_order['from_type'] == 'student' && $status) {
                    if (
$status == 'process' || $status == 'cancel') {
                        
$row_member get_teacher($record_order['tutormain_id']);
                    } else {
                        
$row_member get_student($record_order['studentmain_id']);
                    }
                    
$name '導師';
                } else if (
$record_order['from_type'] == 'tutor' && $status) {
                    if (
$status == 'process' || $status == 'cancel') {
                        
$row_member get_student($record_order['studentmain_id']);
                    } else {
                        
$row_member get_teacher($record_order['tutormain_id']);
                    }
                    
$name '學生';
                }
                
$to         aes_crypt($row_member['email'], 2);
                
$cmsloginid $row_member['cmsloginid'];
                if (
$status == 'approved') {
                    
$order_status 'ORDER_ACCEPT';
                    
$title        '已確認了你的試堂';
                } else if (
$status == 'paid') {
                    
$order_status 'ORDER_PAID';
                    
$title        '已付款你的試堂';
                } else if (
$status == 'cancel') {
                    
$order_status 'ORDER_CANCEL';
                    
$title        '已取消你的試堂';
                } else if (
$status == 'reject') {
                    
$order_status 'ORDER_REJECT';
                    
$title        '已拒絕你的試堂';
                } else if (
$status == 'process') {
                    
$order_status 'ORDER_PROCESS';
                    
$title        '已邀請你的試堂';
                } elseif (
$status == 'refunded') {
                    
$order_status 'ORDER_REFUND';
                }
                
$content_arr['titlename'] = $site_info['companyname_' $lang] . '- ' $record_order['order_no'] . '訂單處理狀況';
                
$content_arr['msg']       = '你好,<br><br>
                                       ' 
$name $title '。
                                       <br><br>
                                       請登入<a href="'
.$site_info["url"].'" target="_blank">'.$site_info["url"].'</a>繼續享用我們為你提供的服務。
                                       <br><br>
                                       如有任何問題,請電郵至cs@musiccircle.hk跟我們的客戶服務部聯絡。
                                       <br><br>
                                       MusicCircle 客戶服務
                                       '
;
            }
            
$content_arr['date'] = date('Y-m-d');
            
$content_arr['time'] = date('H:i:s');
            if (
$status == 'paid') {
                
$map_file 'email/page.html';
            } else {
                
$map_file '../email/page.html';
            }
            
sendEmail($to$subject$content_arr$sender_name$sender_email$map_file);
            
//
            
insert_notification($order_status'order'$id$cmsloginid);
            if (
$status == 'paid') {
                
insert_notification($order_status'order'$id0);
            }
        }
    }

    if (!
function_exists('get_teacherpostjob')) {
        function 
get_teacherpostjob($tutor_id null$status 1$deleted 0)
        {
            global 
$dbh;
            if (
$tutor_id) {
                
$sql        "select * from tutor_postjob where id = ? and deleted = ? order by createdate DESC";
                
$parameters = array($tutor_id$deleted);
                return 
bind_pdo($sql$parameters"selectone");
            } else {
                
//$sql        = "select *,(select count(*) from `order` where job_id = tb.id and (status = 'process' or status = 'paid') and deleted = 0) as count
                
$sql        "select *,(select count(*) from `order` where job_id = tb.id and (status = 'paid') and deleted = 0) as count 
                               from tutor_postjob as tb where deleted = ? order by createdate DESC"
;
                
$parameters = array($deleted);
                return 
bind_pdo($sql$parameters"selectall");
            }
        }
    }

    if (!
function_exists('get_studentpostjob')) {
        function 
get_studentpostjob($student_id null$status 1$deleted 0)
        {
            global 
$dbh;
            if (
$student_id) {
                
$sql        "select * from student_postjob where id = ? and deleted = ? order by createdate DESC";
                
$parameters = array($student_id$deleted);
                return 
bind_pdo($sql$parameters"selectone");
            } else {
                
$sql        "select * from student_postjob where deleted = ? order by createdate DESC";
                
$parameters = array($deleted);
                return 
bind_pdo($sql$parameters"selectall");
            }
        }
    }

    if (!
function_exists('get_studentpostjob2')) {
        function 
get_studentpostjob2($studentpostjob_id null$status null$deleted null)
        {
            global 
$dbh;

            
$where_clause  "";
            
$parameters    = array();
            
$select_option "selectall";

            if (!empty(
$studentpostjob_id)) {
                
$where_clause .= " and id = ?";
                
$parameters[]  = $studentpostjob_id;
                
$select_option "selectone";
            }

            if (isset(
$status)) {
                
$where_clause .= " and status = ?";
                
$parameters[] = $status;
            }

            if (isset(
$deleted)) {
                
$where_clause .= " and deleted = ?";
                
$parameters[] = $deleted;
            }

            
$sql "select * from student_postjob where id > 0 {$where_clause} order by createdate DESC";

            return 
bind_pdo($sql$parameters$select_option);
        }
    }

    if (!
function_exists('get_student')) {
        function 
get_student($student_id null$status 1$deleted 0)
        {
            global 
$dbh;
            if (
$student_id) {
                
$sql        "select * from student_main where id = ? and deleted = ? order by student_no DESC";
                
$parameters = array($student_id$deleted);
                return 
bind_pdo($sql$parameters"selectone");
            } else {
                if (
$status == 3) {
                    
$sql "select * from student_main where deleted = ? and status <=2 and isemailverified = 0 order by student_no DESC";
                } elseif (
$status == 2) {
                    
$sql "select * from student_main where deleted = ? and status > 2 order by student_no DESC";
                } else if (
$status == 1) {
                    
$sql "select * from student_main where deleted = ? and status <=2 and isemailverified = 1 order by student_no DESC";
                }
                
$parameters = array($deleted);
                return 
bind_pdo($sql$parameters"selectall");
            }
        }
    }

    if (!
function_exists('getstudentNo')) {
        function 
getstudentNo($student_id null)
        {
            return 
'S' sprintf("%06d"$student_id);
        }
    }

    if (!
function_exists('get_teacher')) {
        function 
get_teacher($teacher_id null$status 2$deleted 0)
        {
            global 
$dbh;
            if (
$teacher_id) {
                
$sql        "select * from tutor_main where id = ? and deleted = ?";
                
$parameters = array($teacher_id$deleted);
                return 
bind_pdo($sql$parameters"selectone");
            } else {
                if (
$status == 3) {
                    
$sql "select * from tutor_main where deleted = ? and status <=2 and isemailverified = 0 order by tutor_no DESC";
                } elseif (
$status == 2) {
                    
$sql "select * from tutor_main where deleted = ? and status > 2 order by tutor_no DESC";
                } else if (
$status == 1) {
                    
$sql "select * from tutor_main where deleted = ? and status <=2 and isemailverified = 1 order by tutor_no DESC";
                }
                
$parameters = array($deleted);
                return 
bind_pdo($sql$parameters"selectall");
            }
        }
    }

    if (!
function_exists('gettutorNo')) {
        function 
gettutorNo($tutor_id null)
        {
            return 
'T' sprintf("%06d"$tutor_id);
        }
    }

    if (!
function_exists('get_tutor')) {
        function 
get_tutor($tutor_id null$status null$deleted 0)
        {
            
//status:  1: new registration, 2: email verified, 3: completed registration
            //$category_id -> mas_instrument
            
global $dbh;

            
$where_clause "";
            
$parameters   = array($deleted);

            if (!empty(
$tutor_id)) {
                
$where_clause .= " and tutor_main.id = ?";
                
$parameters[] = $tutor_id;
            }

            if (isset(
$status)) {
                
$where_clause .= " and tutor_main.status = ?";
                
$parameters[] = $status;
            }


            
$sql "select * from tutor_main where tutor_main.deleted = ? {$where_clause} order by tutor_no DESC";

            if (!empty(
$tutor_id)) {
                return 
bind_pdo($sql$parameters"selectone");
            } else {
                return 
bind_pdo($sql$parameters"selectall");
            }
        }
    }

    if (!
function_exists('get_tutor_expectcharge')) {
        function 
get_tutor_expectcharge($tutor_id null$category_id null$fee_type null$deleted 0)
        {
            global 
$dbh;

            
$where_clause "";
            
$parameters   = array(040);

            if (!empty(
$tutor_id)) {
                
$where_clause .= " and tutor_main.id = ?";
                
$parameters[] = $tutor_id;
            }

            if (!empty(
$category_id) && $category_id != "ANY") {
                
$where_clause .= " and tutor_charge.mas_instrument = ?";
                
$parameters[] = $category_id;
            }

            if (!empty(
$fee_type)) {
                
$where_clause .= " and tutor_expectcharge.type = ?";
                
$parameters[] = $fee_type;
            }

            
/*$sql = "select *, master_type_code.name_" . $_SESSION["langcode"] . " as instrument_name, mtc2.name_" . $_SESSION["langcode"] . " as grade_name from tutor_main
                    INNER JOIN tutor_charge ON tutor_main.id = tutor_charge.tutormain_id
                    INNER JOIN tutor_expectcharge ON tutor_charge.id = tutor_expectcharge.charge_id
                    INNER JOIN master_type_code ON master_type_code.id = tutor_charge.mas_instrument
                    INNER JOIN master_type_code mtc2 ON (mtc2.typeid = 'LEVEL_OF_GRADE' and mtc2.code = tutor_expectcharge.grade)
                    where tutor_charge.deleted = ? and tutor_main.status = ? and tutor_main.deleted = ? {$where_clause}";*/

            
$sql "select tutor_charge.mas_instrument, tutor_expectcharge.charge_30min, tutor_expectcharge.charge_45min, tutor_expectcharge.charge_60min, master_type_code.name_" $_SESSION["langcode"] . " as instrument_name, mtc2.name_" $_SESSION["langcode"] . " as grade_name from tutor_main
                    INNER JOIN tutor_charge ON tutor_main.id = tutor_charge.tutormain_id
                    INNER JOIN tutor_expectcharge ON tutor_charge.id = tutor_expectcharge.charge_id
                    INNER JOIN master_type_code ON master_type_code.id = tutor_charge.mas_instrument
                    INNER JOIN master_type_code mtc2 ON (mtc2.typeid = 'LEVEL_OF_GRADE' and mtc2.code = tutor_expectcharge.grade)
                    where tutor_charge.deleted = ? and tutor_main.status = ? and tutor_main.deleted = ? 
{$where_clause}";

            
//debug_log(1, dump_sql($sql, $parameters));
            
$tutor_expectcharge bind_pdo($sql$parameters"selectall");


            
$min_fee     9999999;
            
$min_minutes 9999999;

            
$max_fee     0;
            
$max_minutes 0;

            
$instrument_name  "";
            
$instrument_grade "";
            
$min_category_id  "";

            if (!empty(
$tutor_expectcharge)) {
                foreach (
$tutor_expectcharge as $key => $row) {
                    if (
$row["charge_30min"] < $min_fee && $row["charge_30min"] > 0) {
                        
$min_fee     $row["charge_30min"];
                        
$min_minutes 30;

                        
$instrument_name  $row["instrument_name"];
                        
$instrument_grade $row["grade_name"];
                        
$min_category_id  $row["mas_instrument"];
                    }

                    if (
$row["charge_45min"] < $min_fee && $row["charge_45min"] > 0) {
                        
$min_fee     $row["charge_45min"];
                        
$min_minutes 45;

                        
$instrument_name  $row["instrument_name"];
                        
$instrument_grade $row["grade_name"];
                        
$min_category_id  $row["mas_instrument"];
                    }

                    if (
$row["charge_60min"] < $min_fee && $row["charge_60min"] > 0) {
                        
$min_fee     $row["charge_60min"];
                        
$min_minutes 60;

                        
$instrument_name  $row["instrument_name"];
                        
$instrument_grade $row["grade_name"];
                        
$min_category_id  $row["mas_instrument"];
                    }

                    
/*if($row["charge_30min"] > $max_fee && $row["charge_30min"] > 0){
                        $max_fee = $row["charge_30min"];
                        $max_minutes = 30;
                    }

                    if($row["charge_45min"] > $max_fee && $row["charge_45min"] > 0){
                        $max_fee = $row["charge_45min"];
                        $max_minutes = 45;
                    }

                    if($row["charge_60min"] > $max_fee && $row["charge_60min"] > 0){
                        $max_fee = $row["charge_60min"];
                        $max_minutes = 60;
                    }*/

                    /*$tutor_expectcharge[$key]["min_fee"] = $min_fee;
                    $tutor_expectcharge[$key]["min_minutes"] = $min_minutes;
                    $tutor_expectcharge[$key]["max_fee"] = $max_fee;
                    $tutor_expectcharge[$key]["max_minutes"] = $max_minutes;*/
                
}

                
$tutor_expectcharge[0]["min_fee"]              = $min_fee;
                
$tutor_expectcharge[0]["min_minutes"]          = $min_minutes;
                
$tutor_expectcharge[0]["max_fee"]              = $max_fee;
                
$tutor_expectcharge[0]["max_minutes"]          = $max_minutes;
                
$tutor_expectcharge[0]["min_instrument_name"]  = $instrument_name;
                
$tutor_expectcharge[0]["min_instrument_grade"] = $instrument_grade;
                
$tutor_expectcharge[0]["min_category_id"]      = $min_category_id;

            }

            return 
$tutor_expectcharge;
        }
    }

    if (!
function_exists('get_tutor_expstudentage')) {
        function 
get_tutor_expstudentage($tutor_id$deleted 0$order_by " order by master_type_code.sort ASC")
        {
            global 
$dbh;

            
$sql        "select * from tutor_expstudentage INNER JOIN master_type_code ON tutor_expstudentage.mas_expstudentage = master_type_code.id where tutor_expstudentage.tutormain_id = ? and tutor_expstudentage.deleted = ?" $order_by;
            
$parameters = array($tutor_id$deleted);
            return 
bind_pdo($sql$parameters"selectall");
        }
    }

    if (!
function_exists('get_instrument_qualification')) {
        function 
get_instrument_qualification($tutor_id$deleted 0$order_by " order by year ASC")
        {
            global 
$dbh;

            
$sql        "select *, master_type_code.name_" $_SESSION["langcode"] . " as instrument_name, mtc2.name_" $_SESSION["langcode"] . " as grade_name from tutor_instrumentquali 
            INNER JOIN master_type_code ON tutor_instrumentquali.mas_instrument = master_type_code.id 
            INNER JOIN master_type_code mtc2 ON tutor_instrumentquali.grade = mtc2.id 
            where tutor_instrumentquali.tutormain_id = ? and tutor_instrumentquali.deleted = ?" 
$order_by;
            
$parameters = array($tutor_id$deleted);
            return 
bind_pdo($sql$parameters"selectall");
        }
    }

    if (!
function_exists('get_teaching_area')) {
        function 
get_teaching_area($tutor_id$deleted 0)
        {
            global 
$dbh;

            
$sql        "select * from tutor_teachareacode INNER JOIN master_type_code ON tutor_teachareacode.mas_teachareacode = master_type_code.id where tutor_teachareacode.tutormain_id = ? and tutor_teachareacode.deleted = ?";
            
$parameters = array($tutor_id$deleted);
            return 
bind_pdo($sql$parameters"selectall");
        }
    }

    if (!
function_exists('get_teaching_venue')) {
        function 
get_teaching_venue($tutor_id$deleted 0)
        {
            global 
$dbh;

            
$sql        "select * from tutor_teachvenus INNER JOIN master_type_code ON tutor_teachvenus.mas_teachvenus = master_type_code.id where tutor_teachvenus.tutormain_id = ? and tutor_teachvenus.deleted = ?";
            
$parameters = array($tutor_id$deleted);
            return 
bind_pdo($sql$parameters"selectall");
        }
    }

    if (!
function_exists('get_teaching_mode')) {
        function 
get_teaching_mode($tutor_id$deleted 0)
        {
            global 
$dbh;

            
$sql        "select * from tutor_teachmode INNER JOIN master_type_code ON tutor_teachmode.mas_teachmode = master_type_code.id where tutor_teachmode.tutormain_id = ? and tutor_teachmode.deleted = ?";
            
$parameters = array($tutor_id$deleted);
            return 
bind_pdo($sql$parameters"selectall");
        }
    }

    if (!
function_exists('get_teaching_instrument')) {
        function 
get_teaching_instrument($tutor_id$deleted 0)
        {
            global 
$dbh;

            
$sql        "select * from tutor_charge INNER JOIN master_type_code ON tutor_charge.mas_instrument = master_type_code.id where tutor_charge.tutormain_id = ? and tutor_charge.deleted = ?";
            
$parameters = array($tutor_id$deleted);
            return 
bind_pdo($sql$parameters"selectall");
        }
    }

    if (!
function_exists('low2high')) {
        function 
low2high($a$b)
        {
            if (
$a["min_fee"] == $b["min_fee"]) {
                return 
0;
            }
            return (
$a["min_fee"] < $b["min_fee"]) ? -1;
        }
    }

    if (!
function_exists('high2low')) {
        function 
high2low($a$b)
        {
            if (
$a["min_fee"] == $b["min_fee"]) {
                return 
0;
            }
            return (
$a["min_fee"] > $b["min_fee"]) ? -1;
        }
    }

    if (!
function_exists('accumulate_hour_low2high')) {
        function 
accumulate_hour_low2high($a$b)
        {
            if (
$a["stars"] == $b["stars"]) {
                return 
0;
            }
            return (
$a["stars"] < $b["stars"]) ? -1;
        }
    }

    if (!
function_exists('accumulate_hour_high2low')) {
        function 
accumulate_hour_high2low($a$b)
        {
            if (
$a["stars"] == $b["stars"]) {
                return 
0;
            }
            return (
$a["stars"] > $b["stars"]) ? -1;
        }
    }

    function 
pagenav()
    {
        global 
$limit$start$numpage$page$category_id;
        
$current_page = ($start $limit) + 1;
        
//$current_page = $start;
        
$parameters "&id=" $category_id;

        echo 
"<ul class='pagination pagination-md'>";

        if (
$current_page 3) {
            echo 
"<li class='start' data-value='0'><a>1...</a></li>";
        }

        
//previous set page
        
if ($numpage && $start > ($limit)) {
            
$newstart $start $limit;
            if (
$newstart <= 0) {
                
$newstart 0;
            }
        }

        
//previous page
        
$newstart "";
        if (
$start <= 0) {
            
$newstart 0;
        } else {
            
$newstart $start $limit;
        }
        
//echo "<li><a href='" . $page . "?start=" . $newstart . $parameters . "'>&lt;</a></li>";
        
echo "<li><a class='start' data-value='$newstart'>&lt;</a></li>";

        
//current page > 3
        
if ($start $limit >= 2) {
            for (
$i = (($start $limit) - 2); $i < (($start $limit) + 3); $i++) {
                if (((
$i) * $limit) == $start) {
                    echo 
"<li class='page-item active'><a>" . ($i 1) . "</a></li>";
                } else {
                    if (
$i >= && $i $numpage) {
                        
$newstart = ($i) * $limit;
                        
//echo "<li><a href='" . $page . "?start=" . $newstart . $parameters . "'>" . ($i + 1) . "</a></li>";
                        
echo "<li class='start' data-value='$newstart'><a>" . ($i 1) . "</a></li>";
                    }

                }
            }
        } else {
            for (
$i 0$i 5$i++) {
                if (
$i >= && $i $numpage) {
                    if (((
$i) * $limit) == $start) {
                        echo 
"<li class='page-item active'><a>" . ($i 1) . "</a></li>";
                    } else {

                        
$newstart = ($i) * $limit;
                        
//echo "<li><a href='" . $page . "?start=" . $newstart . $parameters . "'>" . ($i + 1) . "</a></li>";
                        
echo "<li class='start' data-value='$newstart'><a>" . ($i 1) . "</a></li>";

                    }
                }
            }
        }

        
//next page
        
if ($start $limit * ($numpage 1)) {
            
$newstart $start $limit;
            
//echo "<li><a href='" . $page . "?start=" . $newstart . $parameters . "' >&gt;</a></li>";
            
echo "<li class='start' data-value='$newstart'><a>&gt;</a></li>";
        }

        
//next set page
        
if ($start $limit >= 3) {
            if ((
$start $limit) + <= $numpage) {
                
$newstart $start $limit;
            }
        }

        if (
ceil($numpage) > && (ceil($numpage) - $current_page >= 3)) {
            echo 
"<li class='start' data-value='" . ((ceil($numpage) - 1) * $limit) . "'><a>..." ceil($numpage) . "</a></li>";
        }
        echo 
"</ul>";
    } 
// END FUNCTION

    
if (!function_exists('get_last_login_date')) {
        function 
get_last_login_date($cmsloginid)
        {
            
$sql        "select last_login_date from sys_cms_login where cmsloginid = ?";
            
$parameters = array($cmsloginid);
            
$result     bind_pdo($sql$parameters"selectone");

            
$date = new DateTime($result["last_login_date"]);

            return 
$date->format("Y-m-d");
        }
    }

    if (!
function_exists('get_tutor_year_range')) {
        function 
get_tutor_year_range($dob_year$dob_mon)
        {
            global 
$langcode;

            if (!empty(
$dob_year) && !empty($dob_mon)) {
                
$start_date  = new DateTime($dob_year "-" $dob_mon);
                
$since_start $start_date->diff(new DateTime());
                
$tutor_age   $since_start->y;

                
$year_range          get_master_type_code("YEAR_RANGE");
                
$tutor_year_range    "";
                
$tutor_year_range_id "";

                foreach (
$year_range as $range) {
                    if (empty(
$tutor_year_range) && $tutor_age <= $range["extra1"]) {
                        
$tutor_year_range    $range["name_" $langcode];
                        
$tutor_year_range_id $range["id"];
                    }
                }

                return array(
"tutor_year_range" => $tutor_year_range"master_type_code_id" => $tutor_year_range_id);
            } else {
                return array(
"tutor_year_range" => """master_type_code_id" => "");
            }

        }
    }

    if (!
function_exists('insert_approval_list')) {
        function 
insert_approval_list($type$table_name$table_id)
        {
            
//type = TUTOR / STUDENT / JOB / CHAT
            
if (!empty($type) && !empty($table_name) && !empty($table_id)) {
                
$sql        "insert into approval_list (`type`, `table_name`, `table_id`, createdate, lastupdate) values (?,?,?,?,?)";
                
$parameters = array($type$table_name$table_iddate("Y-m-d H:i:s"), date("Y-m-d H:i:s"));
                
bind_pdo($sql$parameters);
            }
        }
    }

    if (!
function_exists('update_approval_list')) {
        function 
update_approval_list($table_name$table_id)
        {
            
//$table = approval_list / chat / student_postjob / tutor_main
            
if (!empty($table_name) && !empty($table_id)) {
                if (
$table_name == "approval_list") {
                    
$sql        "select * from approval_list where id = ? and deleted = ?";
                    
$parameters = array($table_id0);
                    
$result     bind_pdo($sql$parameters"selectone");

                    if (!empty(
$result)) {
                        
//approved target

                        
$sql        "update " $result["table_name"] . " set approved = ?, approved_date = ?, approved_by = ? where id = ?";
                        
$parameters = array(1date("Y-m-d H:i:s"), $_SESSION["cmsloginid"], $result["table_id"]);
                        
bind_pdo($sql$parameters);

                        
//approved list
                        
$sql        "update approval_list set approved = ?, lastupdate = ?, lastupby = ? where id = ? and approved = ?";
                        
$parameters = array(1date("Y-m-d H:i:s"), $_SESSION["cmsloginid"], $table_id0);
                        
bind_pdo($sql$parameters);
                    }
                } else {
                    
$sql        "update " $table_name " set approved = ?, approved_date = ?, approved_by = ? where id = ?";
                    
$parameters = array(1date("Y-m-d H:i:s"), $_SESSION["cmsloginid"], $table_id);
                    
bind_pdo($sql$parameters);

                    
//approved list
                    
$sql        "update approval_list set approved = ?, lastupdate = ?, lastupby = ? where table_id = ? and table_name = ? and approved = ?";
                    
$parameters = array(1date("Y-m-d H:i:s"), $_SESSION["cmsloginid"], $table_id$table_name0);
                    
bind_pdo($sql$parameters);
                }
            }

        }
    }

    if (!
function_exists('image_fix_orientation')) {
        function 
image_fix_orientation($filename)
        {
            
$exif exif_read_data($filename);
            if (!empty(
$exif['Orientation'])) {
                
$image imagecreatefromjpeg($filename);
                switch (
$exif['Orientation']) {
                    case 
3:
                        
$image imagerotate($image1800);
                        break;

                    case 
6:
                        
$image imagerotate($image, -900);
                        break;

                    case 
8:
                        
$image imagerotate($image900);
                        break;
                }

                
imagejpeg($image$filename90);
            }
        }
    }

    if (!
function_exists('compare_job_and_tutor')) {
        function 
compare_job_and_tutor($job_id$tutor_id)
        {
            
//check if musical instrument match
            
$sql "select student_postjob.id from tutor_main
            INNER JOIN tutor_charge ON tutor_charge.tutormain_id = tutor_main.id
            INNER JOIN student_postjob ON student_postjob.mas_instrument = tutor_charge.mas_instrument
            INNER JOIN student_main ON student_main.id = student_postjob.studentmain_id
            where 
            tutor_main.status = ? and tutor_main.approved = ? and tutor_main.deleted = ? and 
            tutor_charge.deleted = ? and 
            student_postjob.status = ? and student_postjob.approved = ? and student_postjob.deleted = ? and
            student_main.status = ? and student_main.approved = ? and student_main.deleted = ? and 
            student_postjob.id = ? and tutor_main.id = ?"
;

            
$parameters = array(4100110410$job_id$tutor_id);
            
$result     bind_pdo($sql$parameters"selectone");

            if (!empty(
$result)) {
                return 
true;
            } else {
                return 
false;
            }
        }
    }

    if (!
function_exists('calculate_tutor_stars')) {
        function 
calculate_tutor_stars()
        {
            global 
$site_info;

            
$calculate_stars_date = New DateTime($site_info["calculate_stars_date"]);
            if (
$calculate_stars_date->format("Y-m-d") != date("Y-m-d")) {

                
$sql        "select * from tutor_main where status = ? and deleted = ? order by tutorial_mins ASC, recommend ASC, createdate DESC";
                
$parameters = array(40);
                
$all_tutors bind_pdo($sql$parameters"selectall");

                
$tutor_num count($all_tutors);
                
$stars[1]  = floor($tutor_num 10 100);
                
$stars[2]  = floor($tutor_num 10 100);
                
$stars[3]  = floor($tutor_num 30 100);
                
$stars[4]  = floor($tutor_num 30 100);
                
$stars[5]  = $tutor_num $stars[1] - $stars[2] - $stars[3] - $stars[4];

                
$stars_array = array();
                
$j           0;

                foreach (
$stars as $key => $row) {
                    for (
$i 0$i $row$i++) {
                        if (
$j <= ($tutor_num 1)) {
                            
//$stars_array[$j]["id"] = $all_tutors[$j]["id"];
                            //$stars_array[$j]["stars"] = $key;

                            
$sql        "update tutor_main set stars = ? where id = ?";
                            
$parameters = array($key$all_tutors[$j]["id"]);
                            
bind_pdo($sql$parameters);

                            
$j++;
                        } else {
                            break 
2;
                        }
                    }
                }

                
$sql        "update site_info set calculate_stars_date = ? where siteinfoid = ?";
                
$parameters = array(date("Y-m-d"), 1);
                
bind_pdo($sql$parameters);
            }
        }
    }

    
//calculate_tutor_stars();

    
if (!function_exists('insert_notification')) {
        function 
insert_notification($type$table_name$table_id$cmsloginid)
        {
            
$sql        "insert into notification (type, table_name, table_id, cmsloginid, createdate) values (?,?,?,?,?)";
            
$parameters = array($type$table_name$table_id$cmsloginiddate("Y-m-d H:i:s"));
            
bind_pdo($sql$parameters);
        }
    }

    if (!
function_exists('read_notification')) {
        function 
read_notification($ids null)
        {
            if (empty(
$ids)) {
                
//update all
                
$sql        "update notification set `read` = ?, read_date = ? where cmsloginid = ?";
                
$parameters = array(1date("Y-m-d H:i:s"), $_SESSION["cmsloginid"]);
                
bind_pdo($sql$parameters);
            } else {
                
$notification_ids explode(","$ids);
                if (
is_array($notification_ids)) {
                    foreach (
$notification_ids as $id) {
                        
$sql        "update notification set `read` = ?, read_date = ? where cmsloginid = ? and id = ?";
                        
$parameters = array(1date("Y-m-d H:i:s"), $_SESSION["cmsloginid"], $id);
                        
bind_pdo($sql$parameters);
                    }
                } else {
                    
$sql        "update notification set `read` = ?, read_date = ? where cmsloginid = ? and id = ?";
                    
$parameters = array(1date("Y-m-d H:i:s"), $_SESSION["cmsloginid"], $notification_ids);
                    
bind_pdo($sql$parameters);
                }
            }

        }
    }

    if (!
function_exists('show_notification')) {
        function 
show_notification($read null$num 5)
        {
            
$sql "select * from notification where deleted = ? and cmsloginid = ?";
            if (
$_SESSION["cmsrole"] == "super_admin" || $_SESSION["cmsrole"] == "admin") {
                
$parameters = array(00);
            } else {
                
$parameters = array(0$_SESSION["cmsloginid"]);
            }

            if (isset(
$read)) {
                
$sql .= " and `read` = ?";
                
$parameters[] = $read;
            }

            
$sql .= " order by `read` ASC, createdate DESC";

            if (
$num != "ALL") {
                
$sql .= " limit " $num;
            }

            
$notifications bind_pdo($sql$parameters"selectall");

            
$show_notifications                     = array();
            
$show_notifications["notification_num"] = 0;

            if (!empty(
$notifications)) {
                
$show_notifications["notification_num"] = count($notifications);

                foreach (
$notifications as $key => $notification) {
                    if (
$notification["type"] == "TUTOR") {
                        
//admin has approved a tutor
                        
$show_notifications["detail"][$key]["title"] = _lang("Admin has approved your information.");
                        
$show_notifications["detail"][$key]["link"]  = "";
                    } else if (
$notification["type"] == "JOB") {
                        
//
                        
$show_notifications["detail"][$key]["title"] = _lang("Admin has approved your job.");
                        
$show_notifications["detail"][$key]["link"]  = "student_postjob_form.php?id=" $notification["table_id"];
                    } else if (
$notification["type"] == "ORDER_PROCESS") {
                        
//
                        
$show_notifications["detail"][$key]["title"] = _lang("You have receive a tutorial invitation.");
                        
$show_notifications["detail"][$key]["link"]  = "order_form.php?id=" $notification["table_id"];
                    } else if (
$notification["type"] == "ORDER_CANCEL") {
                        
//
                        
$show_notifications["detail"][$key]["title"] = _lang("Your tutorial invitation has been canceled.");
                        
$show_notifications["detail"][$key]["link"]  = "order_form.php?id=" $notification["table_id"];
                    } else if (
$notification["type"] == "ORDER_ACCEPT") {
                        
//
                        
$show_notifications["detail"][$key]["title"] = _lang("Your tutorial invitation has been accepted.");
                        
$show_notifications["detail"][$key]["link"]  = "order_form.php?id=" $notification["table_id"];
                    } else if (
$notification["type"] == "ORDER_REJECT") {
                        
//
                        
$show_notifications["detail"][$key]["title"] = _lang("Your tutorial invitation has been rejected.");
                        
$show_notifications["detail"][$key]["link"]  = "order_form.php?id=" $notification["table_id"];
                    } else if (
$notification["type"] == "ORDER_PAID") {
                        
//
                        
$show_notifications["detail"][$key]["title"] = _lang("Your tutorial invitation has been paid.");
                        
$show_notifications["detail"][$key]["link"]  = "order_form.php?id=" $notification["table_id"];
                    } else if (
$notification["type"] == "ORDER_REFUND") {
                        
//
                        
$show_notifications["detail"][$key]["title"] = _lang("Your tutorial invitation has been refunded.");
                        
$show_notifications["detail"][$key]["link"]  = "order_form.php?id=" $notification["table_id"];
                    } else if (
$notification["type"] == "ORDER_COMPLAIN") {
                        
//for admin
                        
$show_notifications["detail"][$key]["title"] = _lang("Student has complain a lesson.");
                        
$show_notifications["detail"][$key]["link"]  = "course_form.php?id=" $notification["table_id"];
                    } else if (
$notification["type"] == "CHAT_DELETE") {
                        
//for student/tutor
                        
$show_notifications["detail"][$key]["title"] = _lang("A message has been deleted by administrator.");
                        
$show_notifications["detail"][$key]["link"]  = "chat_index.php";
                    }

                    if (!empty(
$show_notifications["detail"][$key]["title"])) {
                        
$show_notifications["detail"][$key]["id"]         = $notification["id"];
                        
$show_notifications["detail"][$key]["createdate"] = $notification["createdate"];
                        
$show_notifications["detail"][$key]["read"]       = $notification["read"];
                    }
                }
            }

            
$show_notifications["message_num"] = 0;
            
$where_clause                      "";
            
$parameters                        = array();


            if (
$_SESSION["is_tutor"] == 1) {
                
$where_clause " and ((chat.from_id = ? and chat.from_role = ?) or (chat.to_id = ? and chat.to_role = ?)) ";
                
$parameters[] = $_SESSION['member_login'];
                
$parameters[] = "TUTOR";
                
$parameters[] = $_SESSION['member_login'];
                
$parameters[] = "TUTOR";

            } else if (
$_SESSION["is_student"] == 1) {
                
$where_clause " and ((chat.from_id = ? and chat.from_role = ?) or (chat.to_id = ? and chat.to_role = ?)) ";
                
$parameters[] = $_SESSION['student_login'];
                
$parameters[] = "STUDENT";
                
$parameters[] = $_SESSION['student_login'];
                
$parameters[] = "STUDENT";

            }

            
$sql "select *
                    from chat 
                    where chat.id > 0 
{$where_clause} 
                    order by chat.sent_date ASC"
;

            
$chats_detail bind_pdo($sql$parameters"selectall");

            
$not_read 0;
            foreach (
$chats_detail as $row2) {
                if (((
$_SESSION["is_student"] == && empty($row2["read_date"]) && $row2["to_id"] == $_SESSION["student_login"]) || ($_SESSION["is_tutor"] == && empty($row2["read_date"]) && $row2["to_id"] == $_SESSION["member_login"])) && $row2["approved"] == 1) {
                    
$not_read++;
                }
            }

            
$show_notifications["message_num"] = $not_read;

            return 
$show_notifications;
        }
    }

    if (!
function_exists('val_in_array')) {
        function 
val_in_array($a$field)
        {
            if (
is_array($a)) {
                return 
$a[$field];
            }
        }
    }

    if (!
function_exists('validateDate')) {
        function 
validateDate($date$format 'Y-m-d H:i:s')
        {
            
$d DateTime::createFromFormat($format$date);
            return 
$d && $d->format($format) == $date;
        }
    }

    if (!
function_exists('get_public_holiday_json')) {
        function 
get_public_holiday_json()
        {
            
$this_year date("Y");

            
$sql  "select `date` from public_holiday where deleted = ? and `date` between ? and ? order by `date` DESC";
            
$rows bind_pdo($sql, array(0, ($this_year 1) . "-01-01", ($this_year 1) . "-12-31",), "selectall");

            return 
json_encode($rows);
        }
    }

    if (!
function_exists('call_curl')) {
        function 
call_curl($url$postData$post$http_header = array())
        {
            global 
$site_info;

            if (!empty(
$post)) {
                
$ch curl_init();
                
curl_setopt($chCURLOPT_URL$url);
                
curl_setopt($chCURLOPT_RETURNTRANSFER1);
                
curl_setopt($chCURLOPT_POST1);
                
curl_setopt($chCURLOPT_IPRESOLVECURL_IPRESOLVE_V4);
                
curl_setopt($chCURLOPT_HEADER0);
                
curl_setopt($chCURLOPT_HTTPHEADER$http_header);
                
curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
                
curl_setopt($chCURLOPT_POSTFIELDS$postData);

                
$result curl_exec($ch);

                
/*var_dump($http_header);
                var_dump($result);
                var_dump(curl_getinfo($ch));*/

                
curl_close($ch);
            } else {
                
$ch curl_init();
                
curl_setopt($chCURLOPT_URL$url);
                
curl_setopt($chCURLOPT_RETURNTRANSFER1);
                
curl_setopt($chCURLOPT_IPRESOLVECURL_IPRESOLVE_V4);
                
curl_setopt($chCURLOPT_HEADER0);
                
curl_setopt($chCURLOPT_HTTPHEADER$http_header);
                
curl_setopt($chCURLOPT_SSL_VERIFYPEER0);
                
$result curl_exec($ch);
                
curl_close($ch);
            }

            return 
$result;
        }
    }

    if (!
function_exists('send_app_notification')) {
        function 
send_app_notification($app_notification_id$cmsloginid$name null$notification_title$notification_body$url=null)
        {
            global 
$site_info;

            
$sql        "select * from sys_cms_login where deleted = ? and cmsloginid = ?";
            
$parameters = array(0$cmsloginid);
            
$result     bind_pdo($sql$parameters"selectone");

            if(!empty(
$result["app_device_id"]) && !empty($result["app_notification_token"])){
                if (empty(
$name)) {
                    if (
$result["is_tutor"] == 1) {
                        
$sql        "select nickname, tutor_no from tutor_main where cmsloginid = ?";
                        
$parameters = array($cmsloginid);
                        
$user       bind_pdo($sql$parameters"selectone");
                        
$name       $user["nickname"] . " (" $user["tutor_no"] . ")";
                    } else if (
$result["is_student"] == 1) {
                        
$sql        "select contactname, student_no from student_main where cmsloginid = ?";
                        
$parameters = array($cmsloginid);
                        
$user       bind_pdo($sql$parameters"selectone");
                        
$name       $user["contactname"] . " (" $user["student_no"] . ")";
                    }
                }

                if(
$result["app_device"] == "IOS"){
                    
$postData = array(
                        
"to"           => ($result["app_notification_token"]),
                        
"notification" => array("title" => $notification_title"body" => $notification_body"body_loc_key" => $url),
                        
"priority"           => 'high',
                    );

                    
$key aes_crypt2($site_info["app_notification_key_ios"], 2);
                }else{
                    
$postData = array(
                        
"to"           => ($result["app_notification_token"]),
                        
"data" => array("title" => $notification_title"message" => $notification_body"url" => $url),
                        
"priority"           => 'high',
                    );

                    
$key aes_crypt2($site_info["app_notification_key"], 2);
                }

                
$url $site_info["app_notification_url"];
                
$response call_curl($urljson_encode($postData), 1, array('Content-Type:application/json''Authorization: key='.$key));

                
//only admin notification?
                
$response_array json_decode($responsetrue);
                
$success $response_array["success"];
                
$sql        "insert into app_notification_user (app_notification_id, cmsloginid, name, response, success) value (?, ?, ?, ?, ?)";
                
$parameters = array($app_notification_id$cmsloginid$name$response$success);
                
bind_pdo($sql$parameters);
            }

            return 
true;
        }
    }

    if (!
function_exists('create_app_notification')) {
        function 
create_app_notification($title$content$status$type)
        {
            global 
$dbh;

            
$sql        "insert into app_notification (title, content, status, sent_date, type) value (?, ?, ?, ?, ?)";
            
$parameters = array($title$content$statusdate("Y-m-d H:i:s"), $type);
            
bind_pdo($sql$parameters);

            return 
$dbh->lastInsertId();
        }
    }

    if (!
function_exists('hide_sensitive_word')) {
        function 
hide_sensitive_word($data)
        {
            
//replace: 11111111, 1111-1111, email to ********
            
$data preg_replace("/(\d{8})|(\d{4})-(\d{4})|[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*(\.[a-zA-Z0-9]{2,})$/""****"$data);

            return 
$data;
        }
    }

    if (isset(
$_GET["msg"]) && (int)$_GET["msg"] > 0) {
        
$msg_master_info get_master_type_code("WEBADMIN"$_GET["msg"]);
        
$msg             $msg_master_info["name_en"];
        
$_GET["msg"]     = $msg_master_info["name_en"];
    }