-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrfile.c
542 lines (436 loc) · 12.8 KB
/
krfile.c
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
/*
+----------------------------------------------------------------------+
| Copyright 2022. JoungKyun.Kim All rights reserved. |
+----------------------------------------------------------------------+
| This source file is subject to version 3.01 of the PHP license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.php.net/license/3_01.txt |
| If you did not receive a copy of the PHP license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@php.net so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Author: JoungKyun Kim <hostmaster@oops.org> |
+----------------------------------------------------------------------+
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#ifdef PHP_WIN32
#include "win32/readdir.h"
#else
#include <dirent.h>
#endif
#include "php.h"
#include "php_ini.h"
#include "zend_API.h"
#include "php_krfile.h"
#include "php_krparse.h"
#include "php_krmath.h"
#include "php_kr.h"
#include "standard/file.h"
#include <regex.h>
struct stat filestat;
#if ! defined(E_DEPRECATED)
#define E_DEPRECATED E_WARNING
#endif
/* {{{ proto string human_fsize_lib(int filesize, int sub, int unit, int cunit)
* filesize => init value (byte or bit)
* sub => whether print origianl value (ex: 7.5 MB (7,500,000 bytes))
* unit => bit or byte (0: byte, 1: bit)
* cunit => byte or bit unit (default: 1024)
* print move action to url */
PHP_FUNCTION(human_fsize_lib)
{
double fsize = 0.0;
int sub = 0;
int unit = 0;
int cunit = 0;
char * ret = NULL;
if ( kr_parameters("d|bbb", &fsize, &sub, &unit, &cunit) == FAILURE )
return;
cunit = cunit ? 1000 : 1024;
ret = human_file_size (fsize, sub, unit, cunit);
RETVAL_STRING (ret);
kr_safe_efree (ret);
}
/* }}} */
/* {{{ proto array filelist_lib (string path, [ string mode, [ string regex ] ])
* return file list in path */
PHP_FUNCTION(filelist_lib)
{
struct dirent *d;
zend_string * Zinput = NULL,
* Zmode = NULL,
* Zregex = NULL;
char * mode;
DIR * dp;
char dirpath[MAXPATHLENGTH] = { 0, };
regex_t preg;
if ( kr_parameters ("S|SS", &Zinput, &Zmode, &Zregex) == FAILURE )
return;
if ( ZSTR_LEN (Zinput) == 0 )
RETURN_FALSE;
mode = Zmode ? ZSTR_VAL (Zmode) : "";
#if PHP_VERSION_ID < 70300
if ( array_init (return_value) == FAILURE )
RETURN_FALSE;
#else
array_init (return_value);
#endif
if ( VCWD_REALPATH (ZSTR_VAL (Zinput), dirpath) == NULL )
strcpy (dirpath, ZSTR_VAL (Zinput));
PHP_KR_CHECK_OPEN_BASEDIR (dirpath);
if ( (dp = opendir (dirpath)) == NULL ) {
php_error (E_ERROR, "Can't open %s directory in read mode", ZSTR_VAL (Zinput));
RETURN_FALSE;
}
if ( Zregex && ZSTR_LEN (Zregex) ) {
if ( regcomp (&preg, ZSTR_VAL (Zregex), REG_EXTENDED) != 0 ) {
php_error (E_WARNING, "Problem REGEX compile in PHP");
RETURN_FALSE;
}
}
while ( (d = readdir (dp)) ) {
if ( d->d_ino != 0) {
if ( ! strcmp (d->d_name, ".") || ! strcmp (d->d_name, "..") )
continue;
if ( ! strcmp (mode, "f") ) {
if ( check_filedev (dirpath, d->d_name) != RETURN_FILE_TYPE )
continue;
} else if (! strcmp (mode, "d") ) {
if ( check_filedev(dirpath, d->d_name) != RETURN_DIR_TYPE )
continue;
} else if ( ! strcmp (mode, "l") ) {
if ( check_filedev(dirpath, d->d_name) != RETURN_LINK_TYPE )
continue;
} else if ( ! strcmp (mode, "fd") ) {
if ( check_filedev(dirpath, d->d_name) != RETURN_FILE_TYPE &&
check_filedev(dirpath, d->d_name) != RETURN_DIR_TYPE )
continue;
} else if ( ! strcmp (mode, "fl") ) {
if ( check_filedev(dirpath, d->d_name) != RETURN_FILE_TYPE &&
check_filedev(dirpath, d->d_name) != RETURN_LINK_TYPE )
continue;
} else if ( ! strcmp (mode, "dl") ) {
if ( check_filedev(dirpath, d->d_name) != RETURN_LINK_TYPE &&
check_filedev(dirpath, d->d_name) != RETURN_DIR_TYPE )
continue;
}
if ( Zregex && ZSTR_LEN (Zregex) && regexec(&preg,d->d_name, 0, NULL, 0) != 0)
continue;
add_next_index_string (return_value, d->d_name);
}
}
if ( Zregex && ZSTR_LEN (Zregex) )
regfree (&preg);
closedir(dp);
}
/* }}} */
/* {{{ proto void putfile_lib(string filename, stirng str, [ int mode ])
* write file */
PHP_FUNCTION(putfile_lib)
{
char filepath[MAXPATHLENGTH] = { 0, };
zend_string * fname = NULL,
* input = NULL;
int mode = 0;
php_error (E_DEPRECATED, "Use file_put_contents function instead of putfile_lib");
if ( kr_parameters ("SS|b", &fname, &input, &mode) == FAILURE )
return;
if ( ZSTR_LEN (fname) == 0 )
RETURN_FALSE;
if ( VCWD_REALPATH (ZSTR_VAL (fname), filepath) == NULL )
strcpy (filepath, ZSTR_VAL (fname));
PHP_KR_CHECK_OPEN_BASEDIR (filepath);
RETURN_LONG(writefile(filepath, ZSTR_VAL (input), mode));
}
/* }}} */
/* {{{ proto string getfile_lib(string filename, [ int readsize ])
* return file context */
PHP_FUNCTION(getfile_lib)
{
char * str, getfilename[MAXPATHLENGTH] = { 0, };
size_t orgsize = 0, chksize = 0;
struct stat buf;
zend_string * input = NULL;
size_t size = 0;
php_error (E_DEPRECATED, "Use file_get_contents function instead of getfile_lib");
if ( kr_parameters ("S|l", &input, &size) == FAILURE )
return;
if ( ZSTR_LEN (input) == 0 )
RETURN_FALSE;
if ( VCWD_REALPATH (ZSTR_VAL (input), getfilename) == NULL )
strcpy (getfilename, ZSTR_VAL (input));
PHP_KR_CHECK_OPEN_BASEDIR (getfilename);
// get file info
stat (getfilename, &buf);
// original file size
orgsize = buf.st_size;
if ( size > orgsize )
chksize = orgsize;
else
chksize = ! size ? orgsize : size;
if ( chksize < 0 )
RETURN_FALSE;
PHP_KR_CHECK_OPEN_BASEDIR (getfilename);
str = readfile (getfilename);
RETVAL_STRINGL (str, chksize);
kr_safe_efree (str);
}
/* }}} */
/* {{{ proto string getfiletype_lib(string filename)
* return file extensions */
PHP_FUNCTION(getfiletype_lib)
{
zend_string * input = NULL;
char * ext = NULL;
if ( kr_parameters ("S", &input) == FAILURE )
return;
if ( ZSTR_LEN (input) == 0 )
RETURN_FALSE;
if ( (ext = strrchr (ZSTR_VAL (input), '.')) == NULL )
RETURN_EMPTY_STRING ();
RETURN_STRING (ext + 1);
}
/* }}} */
/* {{{ proto string pcregrep_lib(string text) */
PHP_FUNCTION(pcregrep_lib)
{
const char delimiters[] = "\n";
zend_string * regex = NULL,
* input = NULL;
int opt = 0;
char * str = NULL,
* bufstr = NULL,
buf[4096] = { 0, };
char ** sep,
** sep_t;
int retval = 0,
len = 0,
buflen = 0,
newline;
if ( kr_parameters ("SS|b", ®ex, &input, &opt) == FAILURE )
return;
if ( ZSTR_LEN (regex) == 0 || ZSTR_LEN (input) == 0 )
RETURN_EMPTY_STRING ();
newline = numberOfchar (ZSTR_VAL (input), '\n');
sep = emalloc ( sizeof (char *) * (newline + 3) );
sep_t = sep;
str = emalloc ( sizeof (char) );
bufstr = strdup (ZSTR_VAL (input));
while ( (*sep = strsep (&bufstr, delimiters)) != NULL ) {
if ( **sep != 0 ) {
memset (buf, 0, 4096);
memmove (buf, * sep, strlen (*sep));
buflen = strlen (buf);
retval = pcre_match (ZSTR_VAL (regex), buf);
if (retval < 0) {
kr_safe_efree (str);
free (bufstr);
RETURN_FALSE;
}
// print matched
if ( opt)
retval = ! retval ? 1 : 0;
if (retval) {
str = erealloc ( str, sizeof (char) * (len + buflen + 3) );
memcpy ( str + len, buf, buflen);
len += buflen;
memset ( str + len, '\n', 1);
len++;
memset ( str + len, 0, 1);
}
sep++;
}
}
kr_safe_efree (sep_t);
free (bufstr);
if (len < 1) {
kr_safe_efree (str);
RETURN_EMPTY_STRING();
}
RETVAL_STRINGL(str, len - 1);
kr_safe_efree (str);
}
/* }}} */
/* {{{ PHPAPI int writefile (char * filename, char * str, int mode) */
int writefile (char * filename, char * str, int mode)
{
struct stat s;
FILE * fp;
char * act;
char * string;
int ret;
if ( mode == 1 ) {
ret = stat (filename, &s);
act = (ret < 0) ? "wb" : "ab";
} else
act = "wb";
string = estrdup (str);
if ( (fp = fopen (filename, act)) == NULL ) {
php_error (E_WARNING, "Can't open %s in write mode", filename);
kr_safe_efree (string);
return -1;
}
if ( fwrite (string, sizeof (char), strlen (string), fp) != strlen (string) ) {
fclose (fp);
php_error (E_WARNING, "Error writing to file %s", filename);
kr_safe_efree (string);
return -1;
}
kr_safe_efree (string);
fclose (fp);
return 0;
}
/* }}} */
/* {{{ PHPAPI char * readfile (char * filename) */
char * readfile (char * filename)
{
struct stat filebuf;
FILE * fp;
size_t filesize = 0,
len = 0,
strlength = 0;
char * text = NULL,
tmptext[FILEBUFS];
/* get file info */
stat (filename, &filebuf);
/* original file size */
filesize = filebuf.st_size;
if ( (fp = fopen (filename, "rb")) == NULL ) {
php_error (E_ERROR, "Can't open %s in read mode", filename);
return NULL;
}
text = emalloc (sizeof (char) * (filesize + 32));
memset (tmptext, '\0', sizeof (tmptext));
while ( (len = fread (tmptext, sizeof(char), FILEBUFS, fp)) > 0 )
{
tmptext[len] = '\0';
memmove (text + strlength, tmptext, len);
strlength += len;
}
if ( strlength < filesize )
filesize = strlength;
text[filesize] = '\0';
fclose(fp);
return text;
}
/* }}} */
/* {{{ char *human_file_size (double size_o, int sub_o, int unit, int cunit) */
char * human_file_size (double size_o, int sub_o, int unit, int cunit)
{
float res;
char buf[32] = { 0, },
sunit[6] = "Bytes",
ssunit = 'B',
danwe[3] = { 0, };
char * BYTES_C = (char *) kr_math_number_format (size_o, 0, '.', ',');
char * ret;
if ( unit == 1 ) {
strcpy (sunit, "Bits");
ssunit = 'b';
}
if ( size_o < cunit ) {
sprintf (buf, "%s %s", BYTES_C, sunit);
} else {
if ( size_o < (cunit * cunit) ) {
res = (float) size_o / cunit;
memset (danwe, 'K', 1);
} else if ( size_o < (cunit * cunit * cunit) ) {
res = (float) size_o / (cunit * cunit);
memset (danwe, 'M', 1);
} else if ( size_o < (cunit * cunit * cunit * cunit) ) {
res = (float) size_o / (cunit * cunit * cunit);
memset (danwe, 'G', 1);
} else {
res = (float) size_o / (cunit * cunit * cunit * cunit);
memset (danwe, 'T', 1);
}
memset (danwe + 1, ssunit, 1);
if(sub_o)
sprintf(buf, "%.2f %s (%s %s)", res, danwe, BYTES_C, sunit);
else
sprintf(buf, "%.2f %s", res, danwe);
}
kr_safe_efree(BYTES_C);
ret = estrdup (buf);
return ret;
}
/* }}} */
/* {{{ int check_filedev (char *path_f, char *filename) */
int check_filedev (char * path_f, char * filename)
{
struct stat s;
char * fullpath;
fullpath = emalloc (sizeof (char) * (strlen (path_f) + strlen (filename) + 2));
sprintf (fullpath, "%s/%s", path_f, filename);
if ( lstat (fullpath, &s) != 0 ) {
kr_safe_efree (fullpath);
return 0;
}
kr_safe_efree (fullpath);
if ( S_ISDIR(s.st_mode) )
return RETURN_DIR_TYPE;
else if ( S_ISREG(s.st_mode) ) {
return RETURN_FILE_TYPE;
} else if ( S_ISLNK(s.st_mode) )
return RETURN_LINK_TYPE;
return 0;
}
/* }}} */
/* {{{ char * includePath (char * filepath) */
char * includePath (char * filepath) {
const char delimiters[] = ":";
char * filename = NULL;
char * token,
chkfile[512];
char * includetmp,
* includepath;
int exists = 1;
//static void ***tsrm_ls;
includetmp = PG(include_path);
includepath = (includetmp == NULL) ? "" : estrdup (includetmp);
if ( strchr (includepath, ':') != NULL ) {
token = strtok (includepath, delimiters);
while (token != NULL) {
if ( strcmp (token, ".") && strcmp (token, "./") ) {
sprintf (chkfile, "%s/%s", token, filepath);
if( (exists = stat (chkfile, &filestat)) == 0 ) {
filename = emalloc (sizeof (char) * (strlen (chkfile) + 1));
memmove (filename, chkfile, strlen (chkfile));
break;
}
}
token = strtok (NULL, delimiters);
}
} else {
char tmpfilename[512] = { 0, };
if ( strlen (includepath) > 0 )
sprintf (tmpfilename, "%s/%s", includepath, filepath);
else
sprintf (tmpfilename, "%s", filepath);
filename = emalloc (sizeof (char) * (strlen (tmpfilename) + 1));
memmove (filename, tmpfilename, strlen (tmpfilename));
}
if ( strlen (filename) == 0 || filename == NULL ) {
filename = erealloc (filename, sizeof (char) * (strlen (filepath) + 1));
memmove (filename, filepath, strlen (filepath));
}
if ( strlen (includepath) > 0 )
kr_safe_efree (includepath);
return filename;
}
/* }}} */
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: noet sw=4 ts=4 fdm=marker
* vim<600: noet sw=4 ts=4
*/