Passed
Push — master ( 1d59b2...95e7d1 )
by Ferry
03:47
created

isConfigCached()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
if(!function_exists("isConfigCached")) {
3
    function isConfigCached()
4
    {
5
        if(env("APP_NAME")=="" || env("APP_NAME")==null) return true;
6
        else return false;
7
    }
8
}
9
10
if(!function_exists("miscellanousSingleton")) {
11
    /**
12
     * @return \crocodicstudio\crudbooster\helpers\MiscellanousSingleton
13
     */
14
    function miscellanousSingleton() {
15
        return app("MiscellanousSingleton");
0 ignored issues
show
Bug Best Practice introduced by
The expression return app('MiscellanousSingleton') also could return the type Illuminate\Contracts\Foundation\Application which is incompatible with the documented return type crocodicstudio\crudboost...s\MiscellanousSingleton.
Loading history...
16
    }
17
}
18
19
if(!function_exists("makeReferalUrl")) {
20
    function makeReferalUrl($name = null) {
21
        $ref = [];
22
        $ref['url'] = request()->fullUrl();
23
        $ref['name'] = $name;
24
        $md5Hash = md5(serialize($ref));
25
26
        if($exist = \Illuminate\Support\Facades\Cache::get("refurl_".$md5Hash)) {
27
            return $exist['id'];
28
        }
29
30
        $ref['id'] = $newID = \Illuminate\Support\Str::random(7);
31
32
        \Illuminate\Support\Facades\Cache::forever("refurl_token_".$newID, $md5Hash);
33
        \Illuminate\Support\Facades\Cache::forever("refurl_".$md5Hash, $ref);
34
        return $newID;
35
    }
36
}
37
38
if(!function_exists("getReferalUrl")) {
39
    function getReferalUrl($key = null) {
40
        if(verifyReferalUrl()) {
41
            $md5hash = \Illuminate\Support\Facades\Cache::get("refurl_token_".request("ref"));
0 ignored issues
show
Bug introduced by
Are you sure request('ref') of type Illuminate\Http\Request|array|string can be used in concatenation? ( Ignorable by Annotation )

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

41
            $md5hash = \Illuminate\Support\Facades\Cache::get("refurl_token_"./** @scrutinizer ignore-type */ request("ref"));
Loading history...
42
            $ref = \Illuminate\Support\Facades\Cache::get("refurl_".$md5hash);
43
            if($key) {
44
                return @$ref[$key];
45
            } else {
46
                return $ref;
47
            }
48
        }
49
        return null;
50
    }
51
}
52
53
if(!function_exists("verifyReferalUrl")) {
54
    function verifyReferalUrl()
55
    {
56
        if(request("ref")) {
57
            if($md5hash = \Illuminate\Support\Facades\Cache::get("refurl_token_".request("ref"))) {
0 ignored issues
show
Bug introduced by
Are you sure request('ref') of type Illuminate\Http\Request|array|string can be used in concatenation? ( Ignorable by Annotation )

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

57
            if($md5hash = \Illuminate\Support\Facades\Cache::get("refurl_token_"./** @scrutinizer ignore-type */ request("ref"))) {
Loading history...
58
                $ref = \Illuminate\Support\Facades\Cache::get("refurl_".$md5hash);
59
                if(filter_var($ref['url'], FILTER_VALIDATE_URL)) {
60
                    return true;
61
                }
62
            }
63
        }
64
        return false;
65
    }
66
}
67
68
if(!function_exists("putHtaccess")) {
69
    function putHtaccess($stringToPut)
70
    {
71
        file_put_contents(base_path(".htaccess"), "\n".$stringToPut, FILE_APPEND);
72
        file_put_contents(public_path(".htaccess"), "\n".$stringToPut, FILE_APPEND);
73
    }
74
}
75
76
if(!function_exists("checkHtaccess")) {
77
    function checkHtaccess($stringToCheck)
78
    {
79
        if(file_exists(base_path(".htaccess")) && file_exists(public_path(".htaccess"))) {
80
            $htaccess = file_get_contents(base_path(".htaccess"));
81
            $htaccess2= file_get_contents(public_path(".htaccess"));
82
            if(\Illuminate\Support\Str::contains($htaccess, $stringToCheck) && \Illuminate\Support\Str::contains($htaccess2, $stringToCheck)) {
83
                return true;
84
            }
85
        }
86
87
        return false;
88
    }
89
}
90
91
if(!function_exists("setEnvironmentValue")) {
92
    function setEnvironmentValue(array $values)
93
    {
94
        $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

94
        $envFile = app()->/** @scrutinizer ignore-call */ environmentFilePath();
Loading history...
95
        $str = file_get_contents($envFile);
96
97
        if (count($values) > 0) {
98
            foreach ($values as $envKey => $envValue) {
99
100
                $str .= "\n"; // In case the searched variable is in the last line without \n
101
                $keyPosition = strpos($str, "{$envKey}=");
102
                $endOfLinePosition = strpos($str, "\n", $keyPosition);
103
                $oldLine = substr($str, $keyPosition, $endOfLinePosition - $keyPosition);
104
105
                // If key does not exist, add it
106
                if (!$keyPosition || !$endOfLinePosition || !$oldLine) {
107
                    $str .= "{$envKey}={$envValue}\n";
108
                } else {
109
                    $str = str_replace($oldLine, "{$envKey}={$envValue}", $str);
110
                }
111
112
            }
113
        }
114
115
        $str = substr($str, 0, -1);
116
        if (!file_put_contents($envFile, $str)) return false;
117
        return true;
118
    }
119
}
120
121
122
if(!function_exists("cbLang")) {
123
    /**
124
     * @param string $key
125
     * @param array $replace
126
     * @return array|\Illuminate\Contracts\Translation\Translator|null|string
127
     */
128
    function cbLang($key, $replace = [])
129
    {
130
        return trans("cb::cb.".$key, $replace);
131
    }
132
}
133
134
if(!function_exists('rglob')) {
135
    function rglob($pattern, $flags = 0) {
136
        $files = glob($pattern, $flags);
137
        foreach (glob(dirname($pattern).'/*', GLOB_ONLYDIR|GLOB_NOSORT) as $dir) {
138
            $files = array_merge($files, rglob($dir.'/'.basename($pattern), $flags));
0 ignored issues
show
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

138
            $files = array_merge($files, /** @scrutinizer ignore-type */ rglob($dir.'/'.basename($pattern), $flags));
Loading history...
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

138
            $files = array_merge(/** @scrutinizer ignore-type */ $files, rglob($dir.'/'.basename($pattern), $flags));
Loading history...
139
        }
140
        return $files;
141
    }
142
}
143
144
if(!function_exists('convertPHPToMomentFormat')) {
145
    function convertPHPToMomentFormat($format)
146
    {
147
        $replacements = [
148
            'd' => 'DD',
149
            'D' => 'ddd',
150
            'j' => 'D',
151
            'l' => 'dddd',
152
            'N' => 'E',
153
            'S' => 'o',
154
            'w' => 'e',
155
            'z' => 'DDD',
156
            'W' => 'W',
157
            'F' => 'MMMM',
158
            'm' => 'MM',
159
            'M' => 'MMM',
160
            'n' => 'M',
161
            't' => '', // no equivalent
162
            'L' => '', // no equivalent
163
            'o' => 'YYYY',
164
            'Y' => 'YYYY',
165
            'y' => 'YY',
166
            'a' => 'a',
167
            'A' => 'A',
168
            'B' => '', // no equivalent
169
            'g' => 'h',
170
            'G' => 'H',
171
            'h' => 'hh',
172
            'H' => 'HH',
173
            'i' => 'mm',
174
            's' => 'ss',
175
            'u' => 'SSS',
176
            'e' => 'zz', // deprecated since version 1.6.0 of moment.js
177
            'I' => '', // no equivalent
178
            'O' => '', // no equivalent
179
            'P' => '', // no equivalent
180
            'T' => '', // no equivalent
181
            'Z' => '', // no equivalent
182
            'c' => '', // no equivalent
183
            'r' => '', // no equivalent
184
            'U' => 'X',
185
        ];
186
        $momentFormat = strtr($format, $replacements);
187
        return $momentFormat;
188
    }
189
}
190
191
if(!function_exists('slug')) {
192
    function slug($string, $separator = '-') {
193
        return \Illuminate\Support\Str::slug($string, $separator);
194
    }
195
}
196
197
if(!function_exists('columnSingleton')) {
198
    /**
199
     * @return \crocodicstudio\crudbooster\controllers\scaffolding\singletons\ColumnSingleton
200
     */
201
    function columnSingleton() {
202
        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...
203
    }
204
}
205
206
if(!function_exists('cbHook'))
207
{
208
    /**
209
     * @return crocodicstudio\crudbooster\hooks\CBHook
210
     */
211
    function cbHook()
212
    {
213
        $className = "\App\Http\CBHook";
214
        return new $className();
215
    }
216
}
217
218
if(!function_exists('getTypeHook'))
219
{
220
    /**
221
     * @param string $type
222
     * @return crocodicstudio\crudbooster\types\TypesHookInterface
223
     */
224
    function getTypeHook($type) {
225
        $className = '\crocodicstudio\crudbooster\types\\'.$type.'\Hook';
226
        $typeClass = new $className();
227
        return $typeClass;
228
    }
229
}
230
231
if(!function_exists('getPrimaryKey'))
232
{
233
    function getPrimaryKey($table_name)
234
    {
235
        return cb()->pk($table_name);
236
    }
237
}
238
239
if(!function_exists('cb'))
240
{
241
    function cb()
242
    {
243
        return new \crocodicstudio\crudbooster\helpers\CB();
244
    }
245
}
246
247
if(!function_exists('cbAsset')) {
248
    function cbAsset($path, $secure = null) {
249
        return asset("cb_asset/".$path, $secure);
250
    }
251
}
252
253
if(!function_exists("cbConfig")) {
254
    function cbConfig($name, $default = null) {
255
        return config("crudbooster.".$name, $default);
256
    }
257
}
258
259
if(!function_exists("strRandom")) {
260
    function strRandom($length = 5) {
261
        return \Illuminate\Support\Str::random($length);
262
    }
263
}
264
265
if(!function_exists('module')) {
266
    function module()
267
    {
268
        $module = new \crocodicstudio\crudbooster\helpers\Module();
269
        return $module;
270
    }
271
}
272
273
if(!function_exists('getAdminLoginURL')) {
274
    function getAdminLoginURL()
275
    {
276
        return cb()->getAdminUrl("login");
277
    }
278
}
279
280
if(!function_exists('dummyPhoto')) {
281
    function dummyPhoto()
282
    {
283
        return cbConfig("DUMMY_PHOTO");
284
    }
285
}
286
287
if(!function_exists('extract_unit')) {	
288
	/*
289
	Credits: Bit Repository
290
	URL: http://www.bitrepository.com/extract-content-between-two-delimiters-with-php.html
291
	*/
292
	function extract_unit($string, $start, $end)
293
	{
294
	$pos = stripos($string, $start);
295
	$str = substr($string, $pos);
296
	$str_two = substr($str, strlen($start));
297
	$second_pos = stripos($str_two, $end);
298
	$str_three = substr($str_two, 0, $second_pos);
299
	$unit = trim($str_three); // remove whitespaces
300
	return $unit;
301
	}
302
}
303
304
/* 
305
| --------------------------------------------------------------------------------------------------------------
306
| Get data from input post/get more simply
307
| --------------------------------------------------------------------------------------------------------------
308
| $name = name of input
309
|
310
*/
311
312
if(!function_exists('putSetting')) {
313
    function putSetting($key, $value)
314
    {
315
        if(file_exists(storage_path('.cbconfig'))) {
316
            $settings = file_get_contents(storage_path('.cbconfig'));
317
            $settings = decrypt($settings);
318
            $settings = unserialize($settings);
319
        }else{
320
            $settings = [];
321
        }
322
323
        $settings[$key] = $value;
324
325
        \Illuminate\Support\Facades\Cache::forget("setting_".$key);
326
327
        $settings = serialize($settings);
328
        $settings = encrypt($settings);
329
        file_put_contents(storage_path('.cbconfig'), $settings);
330
    }
331
}
332
333
if(!function_exists('getSetting')) {
334
    function getSetting($key, $default = null)
335
    {
336
        if($cache = \Illuminate\Support\Facades\Cache::get("setting_".$key)) {
337
            return $cache;
338
        }
339
340
        if(file_exists(storage_path('.cbconfig'))) {
341
            $settings = file_get_contents(storage_path('.cbconfig'));
342
            $settings = decrypt($settings);
343
            $settings = unserialize($settings);
344
        }else{
345
            $settings = [];
346
        }
347
348
        if(isset($settings[$key])) {
349
            \Illuminate\Support\Facades\Cache::forever("setting_".$key, $settings[$key]);
350
            return $settings[$key]?:$default;
351
        }else{
352
            return $default;
353
        }
354
    }
355
}
356
357
if(!function_exists('timeAgo')) {
358
    function timeAgo($datetime_to, $datetime_from = null, $full = false)
359
    {
360
        $datetime_from = ($datetime_from) ?: date('Y-m-d H:i:s');
361
        $now = new \DateTime;
362
        if ($datetime_from != '') {
363
            $now = new \DateTime($datetime_from);
364
        }
365
        $ago = new \DateTime($datetime_to);
366
        $diff = $now->diff($ago);
367
368
        $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...
369
        $diff->d -= $diff->w * 7;
370
371
        $string = [
372
            'y' => 'year',
373
            'm' => 'month',
374
            'w' => 'week',
375
            'd' => 'day',
376
            'h' => 'hour',
377
            'i' => 'minute',
378
            's' => 'second',
379
        ];
380
        foreach ($string as $k => &$v) {
381
            if ($diff->$k) {
382
                $v = $diff->$k.' '.$v.($diff->$k > 1 ? 's' : '');
383
            } else {
384
                unset($string[$k]);
385
            }
386
        }
387
388
        if (! $full) {
389
            $string = array_slice($string, 0, 1);
390
        }
391
392
        return $string ? implode(', ', $string).' ' : 'just now';
393
    }
394
}
395
396
if(!function_exists("array_map_r")) {
397
    function array_map_r( $func, $arr )
398
    {
399
        $newArr = array();
400
401
        foreach( $arr as $key => $value )
402
        {
403
            $key = $func($key);
404
            $newArr[ $key ] = ( is_array( $value ) ? array_map_r( $func, $value ) : ( is_array($func) ? call_user_func_array($func, $value) : $func( $value ) ) );
405
        }
406
407
        return $newArr;
408
    }
409
}
410
411
if(!function_exists("sanitizeXSS"))
412
{
413
    function sanitizeXSS($value)
414
    {
415
        $value = filter_var($value, FILTER_SANITIZE_STRING);
416
        $value = strip_tags($value);
417
        $value = htmlspecialchars($value);
418
        $value = preg_replace('~\r\n?~', "\n", $value);
419
        return $value;
420
    }
421
}
422
423
if(!function_exists("requestAll")) {
424
    function requestAll() {
425
        $all = array_map_r("sanitizeXSS", request()->all());
426
        return $all;
427
    }
428
}
429
430
431
432
if(!function_exists('getURLFormat')) {
433
    function getURLFormat($name) {
434
        $url = request($name);
435
        if(filter_var($url, FILTER_VALIDATE_URL)) {
436
            return $url;
437
        }else{
438
            return request()->url();
439
        }
440
    }
441
}
442
443
if(!function_exists('g')) {
444
    function g($name, $safe = true) {
445
        if($safe == true) {
446
            $response = request($name);
447
            if(is_string($response)) {
448
                $response = sanitizeXSS($response);
449
            }elseif (is_array($response)) {
450
                array_walk_recursive($response, function(&$response) {
451
                    $response = sanitizeXSS($response);
452
                });
453
            }
454
455
            return $response;
456
        }else{
457
            return Request::get($name);
458
        }
459
    }
460
}
461
462
if(!function_exists('min_var_export')) {
463
    function min_var_export($input) {
464
        if(is_array($input)) {
465
            $buffer = [];
466
            foreach($input as $key => $value)
467
                $buffer[] = var_export($key, true)."=>".min_var_export($value);
468
            return "[".implode(",",$buffer)."]";
469
        } else
470
            return var_export($input, true);
471
    }
472
}
473
474
if(!function_exists('rrmdir')) {
475
	/*
476
	* http://stackoverflow.com/questions/3338123/how-do-i-recursively-delete-a-directory-and-its-entire-contents-files-sub-dir
477
	*/
478
	function rrmdir($dir) { 
479
	   if (is_dir($dir)) { 
480
	     $objects = scandir($dir); 
481
	     foreach ($objects as $object) { 
482
	       if ($object != "." && $object != "..") { 
483
	         if (is_dir($dir."/".$object))
484
	           rrmdir($dir."/".$object);
485
	         else
486
	           unlink($dir."/".$object); 
487
	       } 
488
	     }
489
	     rmdir($dir); 
490
	   } 
491
	 }
492
}
493
494