Passed
Push — master ( e0b6af...ab23d3 )
by Ferry
11:54
created

verifyReferalUrl()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

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

26
            $md5hash = \Illuminate\Support\Facades\Cache::get("refurl_token_"./** @scrutinizer ignore-type */ request("ref"));
Loading history...
27
            $ref = \Illuminate\Support\Facades\Cache::get("refurl_".$md5hash);
28
            if($key) {
29
                return $ref[$key];
30
            } else {
31
                return $ref;
32
            }
33
        }
34
        return null;
35
    }
36
}
37
38
if(!function_exists("verifyReferalUrl")) {
39
    function verifyReferalUrl()
40
    {
41
        if(request("ref")) {
42
            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

42
            if($md5hash = \Illuminate\Support\Facades\Cache::get("refurl_token_"./** @scrutinizer ignore-type */ request("ref"))) {
Loading history...
43
                $ref = \Illuminate\Support\Facades\Cache::get("refurl_".$md5hash);
44
                if(filter_var($ref['url'], FILTER_VALIDATE_URL)) {
45
                    return true;
46
                }
47
            }
48
        }
49
        return false;
50
    }
51
}
52
53
if(!function_exists("findModuleByPermalink")) {
54
    /**
55
     * @param string $permalink
56
     * @return \crocodicstudio\crudbooster\controllers\CBController|null
57
     */
58
    function findControllerByPermalink($permalink) {
59
        try {
60
            $controllers = glob(app_path('Http/Controllers/Admin*Controller.php'));
61
            foreach($controllers as $controller) {
62
                $controllerName = basename($controller);
63
                $controllerName = rtrim($controllerName,".php");
64
                $className = '\App\Http\Controllers\\'.$controllerName;
65
                $controllerClass = new $className();
66
                if(method_exists($controllerClass, 'cbInit')) {
67
                    if($permalink == $controllerClass->getData('permalink')) {
68
                        return $controllerClass;
69
                    }
70
                }
71
            }
72
        }catch (\Exception $e) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
Consider adding a comment why this CATCH block is empty.
Loading history...
73
74
        }
75
        return null;
76
    }
77
}
78
79
if(!function_exists("putHtaccess")) {
80
    function putHtaccess($stringToPut)
81
    {
82
        file_put_contents(base_path(".htaccess"), "\n".$stringToPut, FILE_APPEND);
83
        file_put_contents(public_path(".htaccess"), "\n".$stringToPut, FILE_APPEND);
84
    }
85
}
86
87
if(!function_exists("checkHtaccess")) {
88
    function checkHtaccess($stringToCheck)
89
    {
90
        if(file_exists(base_path(".htaccess")) && file_exists(public_path(".htaccess"))) {
91
            $htaccess = file_get_contents(base_path(".htaccess"));
92
            $htaccess2= file_get_contents(public_path(".htaccess"));
93
            if(\Illuminate\Support\Str::contains($htaccess, $stringToCheck) && \Illuminate\Support\Str::contains($htaccess2, $stringToCheck)) {
94
                return true;
95
            }
96
        }
97
98
        return false;
99
    }
100
}
101
102
if(!function_exists("setEnvironmentValue")) {
103
    function setEnvironmentValue(array $values)
104
    {
105
        $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

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

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

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