Passed
Push — master ( ab23d3...456c2a )
by Ferry
05:06
created

miscellanousSingleton()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 2
rs 10
1
<?php
2
3
if(!function_exists("miscellanousSingleton")) {
4
    /**
5
     * @return \crocodicstudio\crudbooster\helpers\MiscellanousSingleton
6
     */
7
    function miscellanousSingleton() {
8
        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...
9
    }
10
}
11
12
if(!function_exists("makeReferalUrl")) {
13
    function makeReferalUrl($name = null) {
14
        $ref = [];
15
        $ref['url'] = request()->fullUrl();
16
        $ref['name'] = $name;
17
        $md5Hash = md5(serialize($ref));
18
19
        if($exist = \Illuminate\Support\Facades\Cache::get("refurl_".$md5Hash)) {
20
            return $exist['id'];
21
        }
22
23
        $ref['id'] = $newID = \Illuminate\Support\Str::random(7);
24
25
        \Illuminate\Support\Facades\Cache::forever("refurl_token_".$newID, $md5Hash);
26
        \Illuminate\Support\Facades\Cache::forever("refurl_".$md5Hash, $ref);
27
        return $newID;
28
    }
29
}
30
31
if(!function_exists("getReferalUrl")) {
32
    function getReferalUrl($key = null) {
33
        if(verifyReferalUrl()) {
34
            $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

34
            $md5hash = \Illuminate\Support\Facades\Cache::get("refurl_token_"./** @scrutinizer ignore-type */ request("ref"));
Loading history...
35
            $ref = \Illuminate\Support\Facades\Cache::get("refurl_".$md5hash);
36
            if($key) {
37
                return @$ref[$key];
38
            } else {
39
                return $ref;
40
            }
41
        }
42
        return null;
43
    }
44
}
45
46
if(!function_exists("verifyReferalUrl")) {
47
    function verifyReferalUrl()
48
    {
49
        if(request("ref")) {
50
            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

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

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

131
            $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

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