XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
XCube_Utils.class.php
1<?php
12
18 public function __construct() {
19 }
20
34 public static function redirectHeader( $url, $time, $messages = null ) {
35 $root =& XCube_Root::getSingleton();
36 $root->mController->executeRedirect( $url, $time, $messages );
37 }
38
70 public static function formatString() {
71 $arr = func_get_args();
72
73 if (count($arr) === 0) {
74 return null;
75 }
76
77 $message = $arr[0];
78
79 $variables = [];
80 if ( is_array( $arr[1] ) ) {
81 $variables = $arr[1];
82 } else {
83 $variables = $arr;
84 array_shift( $variables );
85 }
86 foreach ( $variables as $i => $iValue ) {
87 // Temporary....
88 // @gigamaster merged calls
89 $message = str_replace(
90 [
91 '{' . ( $i ) . '}',
92 '{' . ( $i ) . ':ucFirst}',
93 '{' . ( $i ) . ':toLower}',
94 '{' . ( $i ) . ':toUpper}'
95 ],
96 [
97 $variables[ $i ],
98 ucfirst( $iValue ),
99 strtolower( $iValue ),
100 strtoupper( $iValue )
101 ],
102 $message );
103 }
104
105 return $message;
106 }
107
117 public static function encrypt(string $plain_text, string $key = null ) {
118 if ($plain_text === '') {
119 return $plain_text;
120 }
121
122 // @todo @gigamaster
123 // TODO check if ondition is unnecessary because it is checked by '! is_string( $key )'
124 // if (! is_string( $key )) {
125 if ( null === $key || ! is_string( $key ) ) {
126 if ( ! defined( 'XOOPS_SALT' ) ) {
127 return $plain_text;
128 }
129 $key = XOOPS_SALT;
130 }
131 $key = substr( md5( $key ), 0, 8 );
132
133 // Legacy backwards compatibility
134/* if ( ! function_exists( 'openssl_encrypt' ) ) {
135 if ( ! extension_loaded( 'mcrypt' ) ) {
136 return $plain_text;
137 }
138 $td = mcrypt_module_open( 'des', '', 'ecb', '' );
139
140 if ( mcrypt_generic_init( $td, $key, 'iv_dummy' ) < 0 ) {
141 return $plain_text;
142 }
143
144 $crypt_text = base64_encode( mcrypt_generic( $td, $plain_text ) );
145
146 mcrypt_generic_deinit( $td );
147 mcrypt_module_close( $td );
148 } else {*/
149 $crypt_text = openssl_encrypt( $plain_text, 'DES-ECB', $key );
150// }
151
152 return false === $crypt_text ? $plain_text : $crypt_text;
153 }
154
164 public static function decrypt(string $crypt_text, string $key = null ) {
165 if ( '' === $crypt_text ) {
166 return $crypt_text;
167 }
168
169 // @todo @gigamaster
170 // Condition is unnecessary because it is checked by '! is_string( $key )'
171 // if (! is_string( $key )) {
172 if ( null === $key || ! is_string( $key ) ) {
173 if ( ! defined( 'XOOPS_SALT' ) ) {
174 return $crypt_text;
175 }
176 $key = XOOPS_SALT;
177 }
178 $key = substr( md5( $key ), 0, 8 );
179
180 // Legacy backwards compatibility
181 // PHP < 5.4.0 can not use `OPENSSL_ZERO_PADDING`
182// if ( ! function_exists( 'openssl_decrypt' ) || PHP_VERSION_ID < 50400 ) {
183// if ( ! extension_loaded( 'mcrypt' ) ) {
184// return $crypt_text;
185// }
186// $td = mcrypt_module_open( 'des', '', 'ecb', '' );
187//
188// if ( mcrypt_generic_init( $td, $key, 'iv_dummy' ) < 0 ) {
189// return $crypt_text;
190// }
191//
192// $plain_text = mdecrypt_generic( $td, base64_decode( $crypt_text ) );
193//
194// mcrypt_generic_deinit( $td );
195// mcrypt_module_close( $td );
196// } else {
197 $plain_text = openssl_decrypt( $crypt_text, 'DES-ECB', $key, OPENSSL_ZERO_PADDING );
198// }
199 // remove \0 padding for mcrypt encrypted text
200 $plain_text = rtrim( $plain_text, "\0" );
201 // remove pkcs#7 padding for openssl encrypted text if padding string found
202 $pad_ch = substr( $plain_text, - 1 );
203 $pad_len = ord( $pad_ch );
204 if ( 0 == substr_compare( $plain_text, str_repeat( $pad_ch, $pad_len ), - $pad_len ) ) {
205 $plain_text = substr( $plain_text, 0, strlen( $plain_text ) - $pad_len );
206 }
207
208 return false === $plain_text ? $crypt_text : $plain_text;
209 }
210
215 public function formatMessage() {
216 $arr = func_get_args();
217
218 if ( 0 == count( $arr ) ) {
219 return null;
220 }
221
222 if ( 1 == count( $arr ) ) {
223 //@gigamaster replaced by self - this return XCube_Utils::formatString($arr[0]);
224 return self::formatString( $arr[0] );
225 }
226
227 if ( count( $arr ) > 1 ) {
228 $vals = $arr;
229 array_shift( $vals );
230
231 //@gigamaster replaced by self - return XCube_Utils::formatString($arr[0], $vals);
232 return self::formatString( $arr[0], $vals );
233 }
234 }
235
243 public function formatMessageByMap( $subject, $arr ) {
244 $searches = [];
245 $replaces = [];
246 foreach ( $arr as $key => $value ) {
247 $searches[] = '{' . $key . '}';
248 $replaces[] = $value;
249 }
250
251 return str_replace( $searches, $replaces, $subject );
252 }
253
259 public function recursiveRemove( string $delete_path ) {
260 if (is_dir( $delete_path )) {
261 foreach (scandir( $delete_path ) as $entry ) {
262 if (!in_array( $entry, ['.', '..'], true )) {
263 $this->recursiveRemove($delete_path . DIRECTORY_SEPARATOR . $entry);
264 }
265 }
266 rmdir( $delete_path );
267 } else {
268 unlink( $delete_path );
269 }
270 }
271
279 public function preloadActive(string $pre_disable, string $pre_active ) {
280
281 if( !copy( $pre_disable, $pre_active ) ) {
282 echo "File can't be copied! \n";
283 } else {
284 echo "File has been copied! \n";
285 }
286
287 }
288}
static formatString()
[Static] Formats string with special care for international.
__construct()
Private Constructor. In other words, it's impossible to generate an instance of this class.
static encrypt(string $plain_text, string $key=null)
[Static] To encrypt strings by "DES-ECB".
preloadActive(string $pre_disable, string $pre_active)
static decrypt(string $crypt_text, string $key=null)
[Static] To decrypt strings by "DES-ECB".
recursiveRemove(string $delete_path)
static redirectHeader( $url, $time, $messages=null)
[Static] The alias for the current controller::redirectHeader(). This function will be deprecated.
formatMessageByMap( $subject, $arr)