1
2 #ifdef HAVE_CONFIG_H
3 #include "../../ext_config.h"
4 #endif
5
6 #include <php.h>
7 #include "../../php_ext.h"
8 #include "../../ext.h"
9
10 #include <Zend/zend_operators.h>
11 #include <Zend/zend_exceptions.h>
12 #include <Zend/zend_interfaces.h>
13
14 #include "kernel/main.h"
15 #include "kernel/memory.h"
16 #include "kernel/string.h"
17 #include "kernel/operators.h"
18 #include "kernel/concat.h"
19 #include "kernel/fcall.h"
20 #include "kernel/array.h"
21 #include "ext/spl/spl_exceptions.h"
22 #include "kernel/exception.h"
23 #include "kernel/object.h"
24
25
26 /**
27 * This file is part of the Phalcon.
28 *
29 * (c) Phalcon Team <team@phalcon.com>
30 *
31 * For the full copyright and license information, please view the LICENSE
32 * file that was distributed with this source code.
33 */
34 /**
35 * This class offers file operation helper
36 */
ZEPHIR_INIT_CLASS(Phalcon_Helper_Fs)37 ZEPHIR_INIT_CLASS(Phalcon_Helper_Fs) {
38
39 ZEPHIR_REGISTER_CLASS(Phalcon\\Helper, Fs, phalcon, helper_fs, phalcon_helper_fs_method_entry, 0);
40
41 return SUCCESS;
42
43 }
44
45 /**
46 * Gets the filename from a given path, Same as PHP's basename() but has non-ASCII support.
47 * PHP's basename() does not properly support streams or filenames beginning with a non-US-ASCII character.
48 * see https://bugs.php.net/bug.php?id=37738
49 *
50 * @param string $uri
51 * @param string $suffix
52 *
53 * @return string
54 */
PHP_METHOD(Phalcon_Helper_Fs,basename)55 PHP_METHOD(Phalcon_Helper_Fs, basename) {
56
57 zephir_method_globals *ZEPHIR_METHOD_GLOBALS_PTR = NULL;
58 zend_long ZEPHIR_LAST_CALL_STATUS;
59 zval *uri_param = NULL, *suffix = NULL, suffix_sub, __$null, filename, matches, _0, _1, _2, _3, _4, _5, _6$$3, _7$$3, _8$$3, _9$$3;
60 zval uri;
61 zval *this_ptr = getThis();
62
63 ZVAL_UNDEF(&uri);
64 ZVAL_UNDEF(&suffix_sub);
65 ZVAL_NULL(&__$null);
66 ZVAL_UNDEF(&filename);
67 ZVAL_UNDEF(&matches);
68 ZVAL_UNDEF(&_0);
69 ZVAL_UNDEF(&_1);
70 ZVAL_UNDEF(&_2);
71 ZVAL_UNDEF(&_3);
72 ZVAL_UNDEF(&_4);
73 ZVAL_UNDEF(&_5);
74 ZVAL_UNDEF(&_6$$3);
75 ZVAL_UNDEF(&_7$$3);
76 ZVAL_UNDEF(&_8$$3);
77 ZVAL_UNDEF(&_9$$3);
78
79 ZEPHIR_MM_GROW();
80 zephir_fetch_params(1, 1, 1, &uri_param, &suffix);
81
82 if (UNEXPECTED(Z_TYPE_P(uri_param) != IS_STRING && Z_TYPE_P(uri_param) != IS_NULL)) {
83 zephir_throw_exception_string(spl_ce_InvalidArgumentException, SL("Parameter 'uri' must be of the type string"));
84 RETURN_MM_NULL();
85 }
86 if (EXPECTED(Z_TYPE_P(uri_param) == IS_STRING)) {
87 zephir_get_strval(&uri, uri_param);
88 } else {
89 ZEPHIR_INIT_VAR(&uri);
90 ZVAL_EMPTY_STRING(&uri);
91 }
92 if (!suffix) {
93 suffix = &suffix_sub;
94 suffix = &__$null;
95 }
96
97
98 ZEPHIR_INIT_VAR(&_0);
99 ZEPHIR_INIT_VAR(&_1);
100 ZVAL_STRING(&_1, "/");
101 zephir_fast_trim(&_0, &uri, &_1, ZEPHIR_TRIM_RIGHT);
102 zephir_get_strval(&uri, &_0);
103 ZEPHIR_INIT_VAR(&_2);
104 ZVAL_STRING(&_2, "/");
105 ZEPHIR_INIT_VAR(&_3);
106 ZVAL_STRING(&_3, "@");
107 ZEPHIR_CALL_FUNCTION(&_4, "preg_quote", NULL, 281, &_2, &_3);
108 zephir_check_call_status();
109 ZEPHIR_INIT_VAR(&_5);
110 ZEPHIR_CONCAT_SVS(&_5, "@[^", &_4, "]+$@");
111 ZEPHIR_INIT_NVAR(&_2);
112 zephir_preg_match(&_2, &_5, &uri, &matches, 0, 0 , 0 );
113 if (zephir_is_true(&_2)) {
114 ZEPHIR_OBS_VAR(&filename);
115 zephir_array_fetch_long(&filename, &matches, 0, PH_NOISY, "phalcon/Helper/Fs.zep", 37);
116 } else {
117 ZEPHIR_INIT_NVAR(&filename);
118 ZVAL_STRING(&filename, "");
119 }
120 if (zephir_is_true(suffix)) {
121 ZEPHIR_INIT_VAR(&_6$$3);
122 ZVAL_STRING(&_6$$3, "@");
123 ZEPHIR_CALL_FUNCTION(&_7$$3, "preg_quote", NULL, 281, suffix, &_6$$3);
124 zephir_check_call_status();
125 ZEPHIR_INIT_VAR(&_8$$3);
126 ZEPHIR_CONCAT_SVS(&_8$$3, "@", &_7$$3, "$@");
127 ZEPHIR_INIT_NVAR(&_6$$3);
128 ZVAL_STRING(&_6$$3, "");
129 ZEPHIR_CALL_FUNCTION(&_9$$3, "preg_replace", NULL, 50, &_8$$3, &_6$$3, &filename);
130 zephir_check_call_status();
131 ZEPHIR_CPY_WRT(&filename, &_9$$3);
132 }
133 RETURN_CCTOR(&filename);
134
135 }
136
137