Completed
Push — master ( 98b2f1...a418ff )
by Dimas
08:55
created

read_file()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 11
c 0
b 0
f 0
nc 4
nop 2
dl 0
loc 18
rs 9.6111
1
<?php
2
3
/**
4
 * Include file if exists.
5
 *
6
 * @return include|false
0 ignored issues
show
Bug introduced by
The type include was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
 */
8
function inc(string $file)
9
{
10
  return file_exists($file) ? include $file : false;
11
}
12
13
/**
14
 * Sort array key ascending multidimensional supported.
15
 *
16
 * @return array
17
 */
18
function sort_iterable(array $arrayObj)
19
{
20
  $arrayObj = array_map(function ($object) {
21
    if (\ArrayHelper\helper::is_iterable($object)) {
22
      $object = sort_iterable($object);
23
    }
24
25
    return $object;
26
  }, $arrayObj);
27
  ksort($arrayObj);
28
29
  return $arrayObj;
30
}
31
32
/**
33
 * Exit var_dump with text/plain header.
34
 *
35
 * @return void
36
 */
37
function ev()
38
{
39
  $args = func_get_args();
40
  if (1 == count($args)) {
41
    $args = $args[0];
42
  }
43
  if (!headers_sent()) {
44
    header('Content-Type: text/plain; charset=utf-8');
45
  }
46
  exit(var_dump($args));
0 ignored issues
show
Bug introduced by
Are you sure the usage of var_dump($args) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
Security Debugging Code introduced by
var_dump($args) looks like debug code. Are you sure you do not want to remove it?
Loading history...
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
47
}
48
49
/**
50
 * Var Dump with removing all previous obstacles.
51
 *
52
 * @return void
53
 */
54
function vd()
55
{
56
  $args = func_get_args();
57
  if (1 == count($args)) {
58
    $args = $args[0];
59
  }
60
  if (ob_get_level()) {
61
    ob_end_clean();
62
  }
63
  var_dump($args);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($args) looks like debug code. Are you sure you do not want to remove it?
Loading history...
64
  exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
65
}
66
67
/**
68
 * Exit var_dump with JSON header.
69
 *
70
 * @param [type] ...$a
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
71
 *
72
 * @return void
73
 */
74
function evj(...$a)
75
{
76
  \JSON\json::json($a);
77
  exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
78
}
79
80
/**
81
 * Fastest Unique ID Generator.
82
 *
83
 * @param int $length default 5
84
 */
85
function uid(int $length = 5)
86
{
87
  return bin2hex(openssl_random_pseudo_bytes($length / 2));
88
}
89
90
/**
91
 * Clean string from multiple whitespaces.
92
 *
93
 * @param string $string
94
 *
95
 * @return string
96
 */
97
function clean_string($string)
98
{
99
  $string = str_replace(' ', '-', $string); // Replaces all spaces with hyphens.
100
101
  return preg_replace('/[^A-Za-z0-9\-]/', '', $string); // Removes special chars.
102
}
103
104
/**
105
 * Local path to url path conversion.
106
 *
107
 * @param string|array $locations
108
 */
109
function get_urlpath($locations)
110
{
111
  /**
112
   * @var string|null $get
113
   */
114
  $get = function (string $location) {
115
    if (file_exists($location)) {
116
      return \MVC\helper::get_url_path($location, true);
117
    }
118
119
    return null;
120
  };
121
  if (is_string($locations)) {
122
    return $get($locations);
123
  } elseif (\ArrayHelper\helper::is_iterable($locations)) {
124
    foreach ($locations as $location) {
125
      $result = $get($location);
126
      if ($result) {
127
        return $result;
128
        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
129
      }
130
    }
131
  }
132
133
  return null;
134
}
135
136
/**
137
 * Include asset with fallback and callback.
138
 * * if found automatically call include().
139
 *
140
 * @author Dimas Lanjaka <[email protected]>
141
 *
142
 * @param string        $fn       first file to check
143
 * @param string        $fn2      fallback file to check
144
 * @param function|null $callback if not exists both, shoul we calling the callback
0 ignored issues
show
Bug introduced by
The type function was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
145
 *
146
 * @return void always void
147
 */
148
function include_asset($fn, $fn2 = null, $callback = null)
149
{
150
  if (file_exists($fn)) {
151
    include $fn;
152
  } elseif ($fn2 && file_exists($fn2)) {
153
    include $fn2;
154
  } elseif (is_callable($callback)) {
155
    call_user_func($callback, $fn);
156
  }
157
}
158
159
/**
160
 * Create <pre/> element from array.
161
 *
162
 * @return void
163
 */
164
function pre()
165
{
166
  $obj = func_get_args();
167
  echo '<pre style="word-wrap: break-word;">';
168
  if (count($obj) > 1) {
169
    foreach ($obj as $objek) {
170
      \JSON\json::json($objek, false, true);
171
    }
172
  } else {
173
    \JSON\json::json($obj[0], false, true);
174
  }
175
  echo '</pre>';
176
}
177
/**
178
 * var_dump in <pre/>.
179
 *
180
 * @return void
181
 */
182
function predump()
183
{
184
  $obj = func_get_args();
185
  echo '<pre style="word-wrap: break-word;">';
186
  if (count($obj) > 1) {
187
    foreach ($obj as $objek) {
188
      var_dump($objek);
0 ignored issues
show
Security Debugging Code introduced by
var_dump($objek) looks like debug code. Are you sure you do not want to remove it?
Loading history...
189
    }
190
  } else {
191
    var_dump($obj[0]);
192
  }
193
  echo '</pre>';
194
}
195
196
/**
197
 * Exit JSON.
198
 *
199
 * @param mixed $result
200
 *
201
 * @return void
202
 */
203
function e()
204
{
205
  $results = func_get_args();
206
  if (count($results) > 1) {
207
    foreach ($results as $check) {
208
      if (is_string($check) && \JSON\json::is_json($check)) {
209
        $check = json_decode($check);
0 ignored issues
show
Unused Code introduced by
The assignment to $check is dead and can be removed.
Loading history...
210
      }
211
    }
212
  } else {
213
    $results = $results[0];
214
  }
215
  \JSON\json::json($results);
216
  exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
217
}
218
219
if (!function_exists('is_json')) {
220
  /**
221
   * Check is json string.
222
   *
223
   * @return bool
224
   */
225
  function is_json(string $string)
226
  {
227
    return \JSON\json::is_json($string);
228
  }
229
}
230
231
/**
232
 * Header redirect.
233
 */
234
function redirect(string $url, bool $exit = true)
235
{
236
  header("Location: $url");
237
  if ($exit) {
238
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
239
  }
240
}
241
242
/**
243
 * Header redirect advance.
244
 */
245
function safe_redirect(string $url, bool $exit = true)
246
{
247
  if (!headers_sent()) {
248
    return redirect($url, $exit);
0 ignored issues
show
Bug introduced by
Are you sure the usage of redirect($url, $exit) is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
249
  } else {
250
    echo '<script>location.href = `' . $url . '`;</script>';
251
  }
252
  if ($exit) {
253
    exit;
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
254
  }
255
}
256
257
/**
258
 * Get latest file from folder.
259
 *
260
 * @param array $path             Folder path array list
261
 * @param bool  $return_timestamp false return filename
262
 *                                ```php
263
 *                                latestFile([__DIR__ . '/src/MVC/', __DIR__ . '/libs/', __DIR__ . '/views/'])
264
 *                                ```
265
 */
266
function latestFile(array $path, bool $return_timestamp = true)
267
{
268
  $timestamp = 0;
269
  $file = '';
270
271
  foreach ($path as $str_path) {
272
    if (!is_dir($str_path)) {
273
      continue;
274
    }
275
    $cls_rii = new \RecursiveIteratorIterator(
276
      new \RecursiveDirectoryIterator($str_path),
277
      \RecursiveIteratorIterator::CHILD_FIRST
278
    );
279
280
    $ary_files = [];
281
282
    foreach ($cls_rii as $str_fullfilename => $cls_spl) {
283
      if ($cls_spl->isFile()) {
284
        $ary_files[] = $str_fullfilename;
285
      }
286
    }
287
288
    $ary_files = array_combine(
289
      $ary_files,
290
      array_map('filemtime', $ary_files)
291
    );
292
293
    arsort($ary_files);
0 ignored issues
show
Bug introduced by
It seems like $ary_files can also be of type false; however, parameter $array of arsort() 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

293
    arsort(/** @scrutinizer ignore-type */ $ary_files);
Loading history...
294
    $time = $ary_files[key($ary_files)];
295
    if ($time > $timestamp) {
296
      $file = key($ary_files);
297
      $timestamp = $time;
298
    }
299
  }
300
  if ($return_timestamp) {
301
    return $timestamp;
302
  } else {
303
    return $file;
304
  }
305
  //echo "file:" . $file . "\n";
306
  //echo "time:" . $time;
307
}
308
309
/**
310
 * Disable direct access static php.
311
 */
312
function disable_direct_access_php(string $file)
313
{
314
  if (!file_exists(dirname($file) . '/.htaccess')) {
315
    \Filemanager\file::file(dirname($file) . '/.htaccess', 'RewriteEngine On
0 ignored issues
show
Bug introduced by
'RewriteEngine On Rewr...192.168.0.1 </Files>' of type string is incompatible with the type boolean expected by parameter $create of Filemanager\file::file(). ( Ignorable by Annotation )

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

315
    \Filemanager\file::file(dirname($file) . '/.htaccess', /** @scrutinizer ignore-type */ 'RewriteEngine On
Loading history...
316
		RewriteRule ^.*\.php$ - [F,L,NC]
317
		<Files (file|class)\.php>
318
		order allow,deny
319
		deny from all
320
		allow from 127.0.0.1
321
		allow from 192.168.0.1
322
		</Files>', true);
323
  }
324
}
325
326
/**
327
 * Create nested folder recursively.
328
 *
329
 * @return string $dir
330
 */
331
function resolve_dir(string $dir)
332
{
333
  $dir = normalize_path($dir);
334
  recursive_mkdir($dir);
335
336
  return $dir;
337
}
338
339
/**
340
 * Disable output buffering.
341
 *
342
 * @return void
343
 */
344
function disable_buffering()
345
{
346
  // Turn off output buffering
347
  ini_set('output_buffering', 'off');
348
  // Turn off PHP output compression
349
  ini_set('zlib.output_compression', false);
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $newvalue of ini_set(). ( Ignorable by Annotation )

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

349
  ini_set('zlib.output_compression', /** @scrutinizer ignore-type */ false);
Loading history...
350
351
  //Flush (send) the output buffer and turn off output buffering
352
  while (@ob_end_flush());
353
354
  // Implicitly flush the buffer(s)
355
  ini_set('implicit_flush', true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type string expected by parameter $newvalue of ini_set(). ( Ignorable by Annotation )

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

355
  ini_set('implicit_flush', /** @scrutinizer ignore-type */ true);
Loading history...
356
  ob_implicit_flush(true);
0 ignored issues
show
Bug introduced by
true of type true is incompatible with the type integer expected by parameter $flag of ob_implicit_flush(). ( Ignorable by Annotation )

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

356
  ob_implicit_flush(/** @scrutinizer ignore-type */ true);
Loading history...
357
}
358
359
/**
360
 * Disable user abort.
361
 *
362
 * @return ignore_user_abort|null
0 ignored issues
show
Bug introduced by
The type ignore_user_abort was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
363
 */
364
function disable_abort(bool $abort = false)
365
{
366
  if (function_exists('ignore_user_abort')) {
367
    return ignore_user_abort($abort);
0 ignored issues
show
Bug Best Practice introduced by
The expression return ignore_user_abort($abort) returns the type integer which is incompatible with the documented return type ignore_user_abort|null.
Loading history...
368
  }
369
}
370
371
function is_aborted()
372
{
373
  return CONNECTION_NORMAL != connection_status();
374
}
375
376
/**
377
 * set Limit execution.
378
 *
379
 * @return set_time_limit|null
0 ignored issues
show
Bug introduced by
The type set_time_limit was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
380
 */
381
function set_limit(int $secs = 0)
382
{
383
  if (function_exists('set_time_limit')) {
384
    return set_time_limit($secs);
0 ignored issues
show
Bug Best Practice introduced by
The expression return set_time_limit($secs) returns the type boolean which is incompatible with the documented return type null|set_time_limit.
Loading history...
385
  }
386
}
387
388
/**
389
 * Create dir recursively.
390
 *
391
 * @param int  $permissions
392
 * @param bool $recursive
393
 */
394
function recursive_mkdir(string $dest, $permissions = 0755, $recursive = true)
395
{
396
  if (!is_dir(dirname($dest))) {
397
    recursive_mkdir(dirname($dest), $permissions, $recursive);
398
  }
399
  if (!file_exists($dest)) {
400
    try {
401
      mkdir($dest, $permissions, $recursive);
402
403
      return true;
404
    } catch (\Throwable $th) {
405
      return false;
406
    }
407
  } else {
408
    return true;
409
  }
410
}
411
412
/**
413
 * Resolve file. (create if not exists)
414
 *
415
 * @param string $file
416
 * @param string $content
417
 * @return string
418
 */
419
function resolve_file(string $file, string $content = '')
420
{
421
  if (!is_dir(dirname($file))) {
422
    resolve_dir(dirname($file));
423
  }
424
425
  if (!file_exists($file)) {
426
    file_put_contents($file, $content);
427
  }
428
429
  return $file;
430
}
431
432
/**
433
 * Convert Windows path to UNIX path.
434
 *
435
 * @return string
436
 */
437
function normalize_path(string $path)
438
{
439
  $path = str_replace('\\', '/', $path);
440
  $path = preg_replace('|(?<=.)/+|', '/', $path);
441
  $path = preg_replace('/\/{2,99}/s', '/', $path);
442
  if (':' === substr($path, 1, 1)) {
443
    $path = ucfirst($path);
444
  }
445
446
  return $path;
447
}
448
449
/**
450
 * Remove root from path.
451
 */
452
function remove_root(string $path)
453
{
454
  $path = normalize_path($path);
455
  $path = str_replace(normalize_path(ROOT), '', $path);
456
457
  return $path;
458
}
459
460
/**
461
 * Shell runner.
462
 *
463
 * @return string|null
464
 */
465
function shell(string $command)
466
{
467
  $output = null;
468
  if (function_exists('shell_exec')) {
469
    $output = shell_exec($command);
470
  } elseif (function_exists('exec')) {
471
    exec($command, $output);
472
  }
473
474
  return \ArrayHelper\helper::is_iterable($output) ? \JSON\json::json($output, false, false) : $output;
475
}
476
477
/**
478
 * Check shell can be executed.
479
 *
480
 * @return string|null
481
 */
482
function shell_check()
483
{
484
  if (function_exists('shell_exec')) {
485
    return 'shell_exec';
486
  } elseif (function_exists('exec')) {
487
    return 'exec';
488
  } else {
489
    return null;
490
  }
491
}
492
493
function shell_required($callback)
0 ignored issues
show
Unused Code introduced by
The parameter $callback is not used and could be removed. ( Ignorable by Annotation )

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

493
function shell_required(/** @scrutinizer ignore-unused */ $callback)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
494
{
495
  $result = [];
496
  if ('string' != gettype(shell_check()) || !\MVC\helper::env('dev')) {
497
    $result = ['error' => true, 'message' => 'Cannot execute command here environtment(' . \MVC\helper::env('dev') . ') command(' . gettype(shell_check()) . ')', 'title' => 'Cannot Access This Page'];
498
  }
499
  if (!empty($result)) {
500
    if (headers_sent()) {
501
      exit('<div class="alert alert-danger">
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
502
      <span class="alert-title"> ' . $result['title'] . ' </span>
503
      </div>');
504
    }
505
  }
506
}
507
508
/**
509
 * ```php
510
 * // callback if function
511
 * call_user_func($callback, $path)
512
 * ```
513
 * Read file contents.
514
 *
515
 * @author Dimas Lanjaka <[email protected]>
516
 *
517
 * @param mixed $callback if null not exist return this callback
518
 *
519
 * @return string|null NULL if not exists
520
 */
521
function read_file(string $path, $callback = null)
522
{
523
  if (file_exists($path) && is_readable($path)) {
524
    if (function_exists('file_get_contents')) {
525
      return file_get_contents($path);
526
    } else {
527
      $handle = fopen($path, 'r');
528
      $contents = fread($handle, filesize($path));
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fread() does only seem to accept resource, 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

528
      $contents = fread(/** @scrutinizer ignore-type */ $handle, filesize($path));
Loading history...
529
      fclose($handle);
0 ignored issues
show
Bug introduced by
It seems like $handle can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

529
      fclose(/** @scrutinizer ignore-type */ $handle);
Loading history...
530
531
      return $contents;
532
    }
533
  }
534
  if (is_callable($callback)) {
535
    return call_user_func($callback, $path);
536
  }
537
538
  return $callback;
539
}
540
541
function write_file(string $path, $content, bool $force = false)
542
{
543
  resolve_dir(dirname($path));
544
  if (\ArrayHelper\helper::is_iterable($content)) {
545
    $content = json_encode($content, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
546
  }
547
  if ($force || !file_exists($path)) {
548
    if (!function_exists('file_put_contents')) {
549
      $fh = fopen($path, 'wa+');
550
      fwrite($path, $content);
0 ignored issues
show
Bug introduced by
$path of type string is incompatible with the type resource expected by parameter $handle of fwrite(). ( Ignorable by Annotation )

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

550
      fwrite(/** @scrutinizer ignore-type */ $path, $content);
Loading history...
551
      fclose($fh);
0 ignored issues
show
Bug introduced by
It seems like $fh can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, 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

551
      fclose(/** @scrutinizer ignore-type */ $fh);
Loading history...
552
    } else {
553
      file_put_contents($path, $content);
554
    }
555
  }
556
}
557
558
function htmlcomment()
559
{
560
  return '<comment style="display:none"> ' . json_encode(func_get_args(), JSON_PRETTY_PRINT) . ' </comment>';
561
}
562
563
function parse_newline(string $str)
564
{
565
  $str = str_replace("\r", '', $str);
566
  $parsed = explode("\n", $str);
567
  if ($parsed) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parsed of type string[] is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
568
    return $parsed;
569
  }
570
571
  return [];
572
}
573
574
/**
575
 * Get Output Buffer Content And Re-construct current output buffer.
576
 *
577
 * @return string|false
578
 */
579
function ob_get()
580
{
581
  $content = ob_get_clean();
582
  ob_end_clean();
583
  ob_start();
584
585
  return $content;
586
}
587
588
$imageCache = null;
589
function imgCDN(string $url)
590
{
591
  global $imageCache;
592
  if (!$imageCache) {
593
    $imageCache = new \img\cache();
594
  }
595
  $imageCache->url2cache($url);
596
}
597
598
/**
599
 * htaccess generator.
600
 *
601
 * @param bool $deny        default deny access. default (true)
602
 * @param bool $DirectPHP   allow direct php access. default (false)
603
 * @param bool $allowStatic allow static files access. default (true)
604
 */
605
function htaccess($deny = true, $DirectPHP = false, $allowStatic = true)
606
{
607
  $ht = '';
608
  if ($deny) {
609
    $ht .= 'deny from all';
610
  }
611
  if (!$DirectPHP) {
612
    $ht .= 'RewriteEngine On
613
    RewriteRule ^.*\.php$ - [F,L,NC]
614
    <Files (file|class)\.php>
615
      order allow,deny
616
      deny from all
617
      allow from 127.0.0.1
618
      allow from 192.168.0.1
619
    </Files>';
620
  }
621
  if ($allowStatic) {
622
    $ht .= '<Files ~ "\.(css|js|png|jpg|svg|jpeg|ico|gif)$">
623
  Allow from all
624
</Files>';
625
  }
626
627
  return $ht;
628
}
629
630
/**
631
 * Is valid email ?
632
 *
633
 * @param string $email
634
 *
635
 * @return bool
636
 */
637
function is_email($email)
638
{
639
  return filter_var($email, FILTER_VALIDATE_EMAIL);
640
}
641
642
/**
643
 * Is valid url ?
644
 *
645
 * @param string $url
646
 *
647
 * @return bool
648
 */
649
function is_url($url)
650
{
651
  return filter_var($url, FILTER_VALIDATE_URL);
652
}
653
654
include __DIR__ . '/../MVC/themes/assets/partial/fab.php';
655