Issues (994)

src/shim/cache.php (4 issues)

1
<?php
2
3
/**
4
 * Get page cache
5
 *
6
 * @return string
7
 */
8
function page_cache()
9
{
10
  $path = ROOT . '/tmp/html/';
11
  $path .= \MVC\helper::get_clean_uri(\MVC\helper::geturl());
12
  $path .= '/' . identifier();
13
  $path .= '.html';
14
  $path = normalize_path($path);
15
16
  return $path;
17
}
18
19
/**
20
 * Identify cached page by hour
21
 *
22
 * @param integer $hour
23
 * @return bool
24
 */
25
function cache_expired(int $hour = 24)
26
{
27
  $file_indicator = normalize_path(page_cache());
28
29
  if (file_exists($file_indicator)) {
30
    $is_24hours = time() - filemtime($file_indicator) > $hour * 3600;
31
    if ($is_24hours) {
32
      \Filemanager\file::delete($file_indicator);
33
34
      return true;
35
    } else {
36
      return false; // cache valid
37
    }
38
  } else {
39
    return true;
40
  }
41
}
42
43
/**
44
 * Render theme
45
 *
46
 * @param \MVC\themes $theme
47
 * @return void
48
 */
49
function render(\MVC\themes $theme)
50
{
51
  //global $theme;
52
  // render view in theme
53
  $theme->render();
54
55
  if (!CORS && $theme->meta['theme']) {
56
    process_page($theme->meta['obfuscate'], $theme);
57
    if ('development' == get_env() && $theme->meta['theme']) {
58
      echo '<!--' . get_includes() . '-->';
59
    }
60
  }
61
}
62
63
/**
64
 * Get page identifier
65
 *
66
 * @return string
67
 */
68
function identifier()
69
{
70
  return md5(UID . serialize(\Session\session::gets(['login', 'coupon'])));
71
}
72
73
74
/**
75
 * Process page to using obfuscate mode or not
76
 *
77
 * @param boolean $obfuscatejs
78
 * @param \MVC\themes $theme
79
 * @return void
80
 */
81
function process_page(bool $obfuscatejs, \MVC\themes $theme)
0 ignored issues
show
The parameter $obfuscatejs 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

81
function process_page(/** @scrutinizer ignore-unused */ bool $obfuscatejs, \MVC\themes $theme)

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...
The parameter $theme 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

81
function process_page(bool $obfuscatejs, /** @scrutinizer ignore-unused */ \MVC\themes $theme)

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...
82
{
83
  //global $theme;
84
  $dom = new SmartDOMDocument('1.0', 'ISO-8859-15');
85
  $dom->preserveWhiteSpace = true;
86
  $dom->formatOutput = true;
87
88
  $c = ob_get();
89
90
  if (!$dom->loadHTML($c)) {
91
    \HTML\js::var('HTML_ERROR', json_encode(['error' => true, 'message' => 'HTML Optimizer failed']));
92
    echo $c;
93
94
    return;
95
  }
96
97
  $xpath = new SmartDOMXpath($dom);
98
  $title = getTitle($dom);
99
100
  $scripts = $xpath->query('//script');
0 ignored issues
show
The call to SmartDOMXpath::query() has too few arguments starting with contextnode. ( Ignorable by Annotation )

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

100
  /** @scrutinizer ignore-call */ 
101
  $scripts = $xpath->query('//script');

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
101
  if (!empty($scripts)) {
102
    foreach ($scripts as $script) {
103
      if ($script->hasAttribute('type') && 'application/ld+json' == $script->getAttribute('type')) {
104
        $script->innerHTML = json_encode(json_decode(innerHTML($script)));
105
      }
106
    }
107
  }
108
109
  $imgs = $xpath->query('//img');
110
  if (!empty($imgs)) {
111
    foreach ($imgs as $img) {
112
      if (!$img->hasAttribute('title')) {
113
        $img->setAttribute('title', $title);
114
      }
115
      if (!$img->hasAttribute('alt')) {
116
        $img->setAttribute('alt', $title);
117
      }
118
      if (!$img->hasAttribute('rel')) {
119
        $img->setAttribute('rel', 'image');
120
      }
121
    }
122
  }
123
124
  $styles = $xpath->query('//style');
125
  if (!empty($styles)) {
126
    foreach ($styles as $style) {
127
      $style->innerHTML = trim(mincss(innerHTML($style)));
128
    }
129
  }
130
131
  $textareas = $xpath->query('//textarea');
132
  if (!empty($textareas)) {
133
    foreach ($textareas as $area) {
134
      $inner = trim(html_entity_decode($area->innerHTML));
135
      if (is_json(trim($inner))) {
136
        $area->setAttribute('innerhtml', base64_encode(\JSON\json::json($inner, false, false)));
137
      }
138
    }
139
  }
140
141
  $result = $dom->saveHTML();
142
  $result = prefilter(htmlmin($result));
143
  resolve_dir(dirname(page_cache()));
144
  \Filemanager\file::file(page_cache(), $result, true);
0 ignored issues
show
$result 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

144
  \Filemanager\file::file(page_cache(), /** @scrutinizer ignore-type */ $result, true);
Loading history...
145
  /**
146
   * @var string Load admin toolbox
147
   */
148
  $result = str_replace('</html>', '', $result);
149
  echo $result;
150
  //$theme->load_admin_tools();
151
152
  echo '</html>';
153
}
154