/var/www/hkosl.com/littleark/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
<?php
    
require_once("configure.php");

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

    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))) {
                    
debug_print_backtrace();
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

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

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

                if (!
$sth->execute($parameters)) {
                    
debug_print_backtrace();
                    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))) {
                    
debug_print_backtrace();
                    throw new 
Exception('[' $sth->errorCode() . ']: ' print_r($sth->errorInfo()));
                }

                if (!
$sth->execute($parameters)) {
                    
debug_print_backtrace();
                    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('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);
        }
    }

    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_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;
        }

    }

    if (!
function_exists('aes_crypt')) {
        function 
aes_crypt($data$encrypt_decrypt)
        {
            
// updated to phpesclib 2.0
            
$aes_crypt = new phpseclib\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 (!empty(
$data)) {
                    return 
base64_encode($aes_crypt->encrypt($data));
                } else {
                    return;
                }
            }

            if (
$encrypt_decrypt == 2) {
                if (!empty(
$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('yesno')) {
        function 
yesno($type$inputname NULL$required NULL$data NULL)
        {
            if (
$type == "addform") {
                echo 
"<input type='radio' name='$inputname' value='1' $required /> Yes";
                echo 
"<input type='radio' name='$inputname' value='0' $required /> No";
            }

            if (
$type == "modifyform") {
                echo 
"<input type='radio' name='$inputname' value='1' " matched_option($data1"radiobutton") . $required /> Yes";
                echo 
"<input type='radio' name='$inputname' value='0' " matched_option($data0"radiobutton") . $required /> No";
            }

            if (
$type == "readonly") {
                if (
$data == 1)
                    echo 
"Yes";
                else
                    echo 
"No";
            }
        }
    }

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

            
$str '';
            foreach(
$rows as $row){
                
$str .= "<option value='" $row[$settings['value_field']] . "' {$settings['attr']} " . ($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_supplier')) {
        function 
get_supplier($supplier_id null$status null$deleted 0)
        {
            if (!empty(
$supplier_id)) {
                if (isset(
$status)) {
                    
$sql        "select * from supplier where id = ? and status = ? and deleted = ? order by code ASC";
                    
$parameters = array($supplier_id$status$deleted);
                } else {
                    
$sql        "select * from supplier where id = ? and deleted = ? order by code ASC";
                    
$parameters = array($supplier_id$deleted);
                }

                
$result bind_pdo($sql$parameters"selectone");
            } else {
                if (isset(
$status)) {
                    
$sql        "select * from supplier where status = ? and deleted = ? order by code ASC";
                    
$parameters = array($status$deleted);
                } else {
                    
$sql        "select * from supplier where deleted = ? order by code ASC";
                    
$parameters = array($deleted);
                }

                
$result bind_pdo($sql$parameters"selectall");
            }
            return 
$result;
        }
    }

    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('check_image_resolution')) {
        function 
check_image_resolution($image)
        {
            return 
true;
            
/*list($width, $height, $type, $attr) = getimagesize($image);

            if ($width <= 2500 && $height <= 2500) {
                return true;
            } else {
                return false;
            }*/
        
}
    }