Passed
Push — master ( c14ede...c50061 )
by Ferry
03:59
created

setEnvironmentValue()   B

Complexity

Conditions 7
Paths 4

Size

Total Lines 26
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 15
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 26
rs 8.8333
1
<?php
2
3
if(!function_exists("putHtaccess")) {
4
    function putHtaccess($stringToPut)
5
    {
6
        file_put_contents(base_path(".htaccess"), "\n".$stringToPut, FILE_APPEND);
7
        file_put_contents(public_path(".htaccess"), "\n".$stringToPut, FILE_APPEND);
8
    }
9
}
10
11
if(!function_exists("checkHtaccess")) {
12
    function checkHtaccess($stringToCheck)
13
    {
14
        if(file_exists(base_path(".htaccess")) && file_exists(public_path(".htaccess"))) {
15
            $htaccess = file_get_contents(base_path(".htaccess"));
16
            $htaccess2= file_get_contents(public_path(".htaccess"));
17
            if(\Illuminate\Support\Str::contains($htaccess, $stringToCheck) && \Illuminate\Support\Str::contains($htaccess2, $stringToCheck)) {
18
                return true;
19
            }
20
        }
21
22
        return false;
23
    }
24
}
25
26
if(!function_exists("setEnvironmentValue")) {
27
    function setEnvironmentValue(array $values)
28
    {
29
        $envFile = app()->environmentFilePath();
0 ignored issues
show
introduced by
The method environmentFilePath() does not exist on Illuminate\Container\Container. Are you sure you never get this type here, but always one of the subclasses? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

29
        $envFile = app()->/** @scrutinizer ignore-call */ environmentFilePath();
Loading history...
30
        $str = file_get_contents($envFile);
31
32
        if (count($values) > 0) {
33
            foreach ($values as $envKey => $envValue) {
34
35
                $str .= "\n"; // In case the searched variable is in the last line without \n
36
                $keyPosition = strpos($str, "{$envKey}=");
37
                $endOfLinePosition = strpos($str, "\n", $keyPosition);
38
                $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
39
40
                // If key does not exist, add it
41
                if (!$keyPosition || !$endOfLinePosition || !$oldLine) {
42
                    $str .= "{$envKey}={$envValue}\n";
43
                } else {
44
                    $str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
45
                }
46
47
            }
48
        }
49
50
        $str = substr($str, 0, -1);
51
        if (!file_put_contents($envFile, $str)) return false;
52
        return true;
53
    }
54
}
55
56
57
if(!function_exists("cbLang")) {
58
    /**
59
     * @param string $key
60
     * @param array $replace
61
     * @return array|\Illuminate\Contracts\Translation\Translator|null|string
62
     */
63
    function cbLang($key, $replace = [])
64
    {
65
        return trans("cb::cb.".$key, $replace);
66
    }
67
}
68
69
if(!function_exists('rglob')) {
70
    function rglob($pattern, $flags = 0) {
71
        $files = glob($pattern, $flags);
72
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
73
            $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
0 ignored issues
show
Bug introduced by
It seems like $files can also be of type false; however, parameter $array1 of array_merge() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $files = array_merge(/** @scrutinizer ignore-type */ $files, rglob($dir.'/'.basename($pattern), $flags));
Loading history...
Bug introduced by
It seems like rglob($dir . '/' . basename($pattern), $flags) can also be of type false; however, parameter $array2 of array_merge() does only seem to accept array|null, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

73
            $files = array_merge($files, /** @scrutinizer ignore-type */ rglob($dir.'/'.basename($pattern), $flags));
Loading history...
74
        }
75
        return $files;
76
    }
77
}
78
79
if(!function_exists('convertPHPToMomentFormat')) {
80
    function convertPHPToMomentFormat($format)
81
    {
82
        $replacements = [
83
            'd' => 'DD',
84
            'D' => 'ddd',
85
            'j' => 'D',
86
            'l' => 'dddd',
87
            'N' => 'E',
88
            'S' => 'o',
89
            'w' => 'e',
90
            'z' => 'DDD',
91
            'W' => 'W',
92
            'F' => 'MMMM',
93
            'm' => 'MM',
94
            'M' => 'MMM',
95
            'n' => 'M',
96
            't' => '', // no equivalent
97
            'L' => '', // no equivalent
98
            'o' => 'YYYY',
99
            'Y' => 'YYYY',
100
            'y' => 'YY',
101
            'a' => 'a',
102
            'A' => 'A',
103
            'B' => '', // no equivalent
104
            'g' => 'h',
105
            'G' => 'H',
106
            'h' => 'hh',
107
            'H' => 'HH',
108
            'i' => 'mm',
109
            's' => 'ss',
110
            'u' => 'SSS',
111
            'e' => 'zz', // deprecated since version 1.6.0 of moment.js
112
            'I' => '', // no equivalent
113
            'O' => '', // no equivalent
114
            'P' => '', // no equivalent
115
            'T' => '', // no equivalent
116
            'Z' => '', // no equivalent
117
            'c' => '', // no equivalent
118
            'r' => '', // no equivalent
119
            'U' => 'X',
120
        ];
121
        $momentFormat = strtr($format, $replacements);
122
        return $momentFormat;
123
    }
124
}
125
126
if(!function_exists('slug')) {
127
    function slug($string, $separator = '-') {
128
        return \Illuminate\Support\Str::slug($string, $separator);
129
    }
130
}
131
132
if(!function_exists('columnSingleton')) {
133
    /**
134
     * @return \crocodicstudio\crudbooster\controllers\scaffolding\singletons\ColumnSingleton
135
     */
136
    function columnSingleton() {
137
        return app('ColumnSingleton');
0 ignored issues
show
Bug Best Practice introduced by
The expression return app('ColumnSingleton') also could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the documented return type crocodicstudio\crudboost...gletons\ColumnSingleton.
Loading history...
138
    }
139
}
140
141
if(!function_exists('cbHook'))
142
{
143
    /**
144
     * @return crocodicstudio\crudbooster\hooks\CBHook
145
     */
146
    function cbHook()
147
    {
148
        $className = "\App\Http\CBHook";
149
        return new $className();
150
    }
151
}
152
153
if(!function_exists('getTypeHook'))
154
{
155
    /**
156
     * @param string $type
157
     * @return crocodicstudio\crudbooster\types\TypesHookInterface
158
     */
159
    function getTypeHook($type) {
160
        $className = '\crocodicstudio\crudbooster\types\\'.$type.'\Hook';
161
        $typeClass = new $className();
162
        return $typeClass;
163
    }
164
}
165
166
if(!function_exists('getPrimaryKey'))
167
{
168
    function getPrimaryKey($table_name)
169
    {
170
        return cb()->pk($table_name);
171
    }
172
}
173
174
if(!function_exists('cb'))
175
{
176
    function cb()
177
    {
178
        return new \crocodicstudio\crudbooster\helpers\CB();
179
    }
180
}
181
182
if(!function_exists('cbAsset')) {
183
    function cbAsset($path, $secure = null) {
184
        return asset("cb_asset/".$path, $secure);
185
    }
186
}
187
188
if(!function_exists("cbConfig")) {
189
    function cbConfig($name, $default = null) {
190
        return config("crudbooster.".$name, $default);
191
    }
192
}
193
194
if(!function_exists("strRandom")) {
195
    function strRandom($length = 5) {
196
        return \Illuminate\Support\Str::random($length);
197
    }
198
}
199
200
if(!function_exists('module')) {
201
    function module()
202
    {
203
        $module = new \crocodicstudio\crudbooster\helpers\Module();
204
        return $module;
205
    }
206
}
207
208
if(!function_exists('getAdminLoginURL')) {
209
    function getAdminLoginURL()
210
    {
211
        return cb()->getAdminUrl("login");
212
    }
213
}
214
215
if(!function_exists('dummyPhoto')) {
216
    function dummyPhoto()
217
    {
218
        return cbConfig("DUMMY_PHOTO");
219
    }
220
}
221
222
if(!function_exists('extract_unit')) {	
223
	/*
224
	Credits: Bit Repository
225
	URL: http://www.bitrepository.com/extract-content-between-two-delimiters-with-php.html
226
	*/
227
	function extract_unit($string, $start, $end)
228
	{
229
	$pos = stripos($string, $start);
230
	$str = substr($string, $pos);
231
	$str_two = substr($str, strlen($start));
232
	$second_pos = stripos($str_two, $end);
233
	$str_three = substr($str_two, 0, $second_pos);
234
	$unit = trim($str_three); // remove whitespaces
235
	return $unit;
236
	}
237
}
238
239
/* 
240
| --------------------------------------------------------------------------------------------------------------
241
| Get data from input post/get more simply
242
| --------------------------------------------------------------------------------------------------------------
243
| $name = name of input
244
|
245
*/
246
247
if(!function_exists('putSetting')) {
248
    function putSetting($key, $value)
249
    {
250
        if(file_exists(storage_path('.cbconfig'))) {
251
            $settings = file_get_contents(storage_path('.cbconfig'));
252
            $settings = decrypt($settings);
253
            $settings = unserialize($settings);
254
        }else{
255
            $settings = [];
256
        }
257
258
        $settings[$key] = $value;
259
260
        \Illuminate\Support\Facades\Cache::forget("setting_".$key);
261
262
        $settings = serialize($settings);
263
        $settings = encrypt($settings);
264
        file_put_contents(storage_path('.cbconfig'), $settings);
265
    }
266
}
267
268
if(!function_exists('getSetting')) {
269
    function getSetting($key, $default = null)
270
    {
271
        if($cache = \Illuminate\Support\Facades\Cache::get("setting_".$key)) {
272
            return $cache;
273
        }
274
275
        if(file_exists(storage_path('.cbconfig'))) {
276
            $settings = file_get_contents(storage_path('.cbconfig'));
277
            $settings = decrypt($settings);
278
            $settings = unserialize($settings);
279
        }else{
280
            $settings = [];
281
        }
282
283
        if(isset($settings[$key])) {
284
            \Illuminate\Support\Facades\Cache::forever("setting_".$key, $settings[$key]);
285
            return $settings[$key]?:$default;
286
        }else{
287
            return $default;
288
        }
289
    }
290
}
291
292
if(!function_exists('timeAgo')) {
293
    function timeAgo($datetime_to, $datetime_from = null, $full = false)
294
    {
295
        $datetime_from = ($datetime_from) ?: date('Y-m-d H:i:s');
296
        $now = new \DateTime;
297
        if ($datetime_from != '') {
298
            $now = new \DateTime($datetime_from);
299
        }
300
        $ago = new \DateTime($datetime_to);
301
        $diff = $now->diff($ago);
302
303
        $diff->w = floor($diff->d / 7);
0 ignored issues
show
Bug introduced by
The property w does not seem to exist on DateInterval.
Loading history...
304
        $diff->d -= $diff->w * 7;
305
306
        $string = [
307
            'y' => 'year',
308
            'm' => 'month',
309
            'w' => 'week',
310
            'd' => 'day',
311
            'h' => 'hour',
312
            'i' => 'minute',
313
            's' => 'second',
314
        ];
315
        foreach ($string as $k => &$v) {
316
            if ($diff->$k) {
317
                $v = $diff->$k.' '.$v.($diff->$k > 1 ? 's' : '');
318
            } else {
319
                unset($string[$k]);
320
            }
321
        }
322
323
        if (! $full) {
324
            $string = array_slice($string, 0, 1);
325
        }
326
327
        return $string ? implode(', ', $string).' ' : 'just now';
328
    }
329
}
330
331
if(!function_exists("array_map_r")) {
332
    function array_map_r( $func, $arr )
333
    {
334
        $newArr = array();
335
336
        foreach( $arr as $key => $value )
337
        {
338
            $key = $func($key);
339
            $newArr[ $key ] = ( is_array( $value ) ? array_map_r( $func, $value ) : ( is_array($func) ? call_user_func_array($func, $value) : $func( $value ) ) );
340
        }
341
342
        return $newArr;
343
    }
344
}
345
346
if(!function_exists("sanitizeXSS"))
347
{
348
    function sanitizeXSS($value)
349
    {
350
        $value = filter_var($value, FILTER_SANITIZE_STRING);
351
        $value = strip_tags($value);
352
        $value = htmlspecialchars($value);
353
        $value = preg_replace('~\r\n?~', "\n", $value);
354
        return $value;
355
    }
356
}
357
358
if(!function_exists("requestAll")) {
359
    function requestAll() {
360
        $all = array_map_r("sanitizeXSS", request()->all());
361
        return $all;
362
    }
363
}
364
365
366
367
if(!function_exists('getURLFormat')) {
368
    function getURLFormat($name) {
369
        $url = request($name);
370
        if(filter_var($url, FILTER_VALIDATE_URL)) {
371
            return $url;
372
        }else{
373
            return request()->url();
374
        }
375
    }
376
}
377
378
if(!function_exists('g')) {
379
    function g($name, $safe = true) {
380
        if($safe == true) {
381
            $response = request($name);
382
            if(is_string($response)) {
383
                $response = sanitizeXSS($response);
384
            }elseif (is_array($response)) {
385
                array_walk_recursive($response, function(&$response) {
386
                    $response = sanitizeXSS($response);
387
                });
388
            }
389
390
            return $response;
391
        }else{
392
            return Request::get($name);
393
        }
394
    }
395
}
396
397
if(!function_exists('min_var_export')) {
398
    function min_var_export($input) {
399
        if(is_array($input)) {
400
            $buffer = [];
401
            foreach($input as $key => $value)
402
                $buffer[] = var_export($key, true)."=>".min_var_export($value);
403
            return "[".implode(",",$buffer)."]";
404
        } else
405
            return var_export($input, true);
406
    }
407
}
408
409
if(!function_exists('rrmdir')) {
410
	/*
411
	* http://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir
412
	*/
413
	function rrmdir($dir) { 
414
	   if (is_dir($dir)) { 
415
	     $objects = scandir($dir); 
416
	     foreach ($objects as $object) { 
417
	       if ($object != "." && $object != "..") { 
418
	         if (is_dir($dir."/".$object))
419
	           rrmdir($dir."/".$object);
420
	         else
421
	           unlink($dir."/".$object); 
422
	       } 
423
	     }
424
	     rmdir($dir); 
425
	   } 
426
	 }
427
}
428
429