oct_minify.php
5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<?php
/**
* @copyright OCTemplates
* @support https://octemplates.net/
* @license LICENSE.txt
*/
class ModelOCTemplatesWidgetsOctMinify extends Model {
public function octMinifyCss($data) {
$oct_deals_data = isset($this->config->get('theme_oct_deals_data')['minify']) ? 1 : 0;
$oct_styles = $this->document->getOctStyles();
if ($oct_deals_data) {
$dir_minify = dirname(DIR_APPLICATION) . '/min/';
require_once $dir_minify . 'lib/Minify/CSS/UriRewriter.php';
$link_server = $this->request->server['HTTPS'] ? $this->config->get('config_ssl') : $this->config->get('config_url');
if (!is_dir($dir_minify . 'cache/')) {
@mkdir($dir_minify . 'cache/', 0755);
}
$oct_fn = [];
$oct_name = md5(json_encode($oct_styles)) . '.css';
if (!file_exists($dir_minify . 'cache/' . $oct_name)) {
$oct_file = fopen($dir_minify . 'cache/' . $oct_name, "a+");
foreach ($oct_styles as $href => $style) {
$link = parse_url($href);
if (!isset($link['host'])) {
if (file_exists(dirname(DIR_APPLICATION) . '/' . ltrim($href))) {
$oct_content_to = Minify_CSS_UriRewriter::rewrite(@file_get_contents(dirname(DIR_APPLICATION) . '/' . ltrim($href)), dirname($href));
$oct_content_to = str_replace('m+', 'm + ', $oct_content_to);
$oct_content_to = str_replace('%+', '% + ', $oct_content_to);
$oct_content_to = str_replace('x+', 'x + ', $oct_content_to);
fwrite($oct_file, $oct_content_to);
fwrite($oct_file, PHP_EOL);
}
} else {
$oct_fn[$href] = [
'href' => $href,
'rel' => $style['rel'],
'media' => $style['media']
];
}
}
fclose($oct_file);
if (file_exists($dir_minify . 'cache/' . $oct_name) && filesize($dir_minify . 'cache/' . $oct_name)) {
$oct_content = @file_get_contents($link_server . 'min/?f=min/cache/' . $oct_name);
if ($oct_content) {
$oct_content = str_replace('m+', 'm + ', preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $oct_content));
$oct_content = str_replace('%+', '% + ', preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $oct_content));
$oct_content = str_replace('x+', 'x + ', preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $oct_content));
@file_put_contents($dir_minify . 'cache/' . $oct_name, '');
$oct_file = fopen($dir_minify . 'cache/' . $oct_name, "a");
fwrite($oct_file, $oct_content);
fclose($oct_file);
}
}
} else {
foreach ($oct_styles as $href => $style) {
$link = parse_url($href);
if (isset($link['host'])) {
$oct_fn[$href] = [
'href' => $href,
'rel' => $style['rel'],
'media' => $style['media']
];
}
}
}
if (file_exists($dir_minify . 'cache/' . $oct_name)) {
$oct_styles = [];
$oct_styles[0] = [
'href' => $link_server . 'min/cache/' . $oct_name . "?" . date('YmdHis', filemtime($_SERVER['DOCUMENT_ROOT'] . '/min/cache/' . $oct_name)),
'rel' => 'stylesheet',
'media' => 'screen'
];
$oct_styles = array_merge($oct_styles, $oct_fn);
}
}
return $oct_styles;
}
public function octMinifyJs() {
$oct_deals_data = isset($this->config->get('theme_oct_deals_data')['minify']) ? 1 : 0;
$oct_scripts = $this->document->getOctScripts();
if ($oct_deals_data) {
$dir_minify = dirname(DIR_APPLICATION) . '/min/';
require_once $dir_minify . 'lib/Minify/CSS/UriRewriter.php';
$link_server = $this->request->server['HTTPS'] ? $this->config->get('config_ssl') : $this->config->get('config_url');
if (!is_dir($dir_minify . 'cache/')) {
@mkdir($dir_minify . 'cache/', 0755);
}
$oct_fn = [];
$oct_name = md5(json_encode($oct_scripts)) . '.js';
if (!file_exists($dir_minify . 'cache/' . $oct_name)) {
$oct_file = fopen($dir_minify . 'cache/' . $oct_name, "a+");
foreach ($oct_scripts as $href => $script) {
$link = parse_url($href);
if (!isset($link['host'])) {
if (file_exists(dirname(DIR_APPLICATION) . '/' . ltrim($href))) {
$oct_content_to = Minify_CSS_UriRewriter::rewrite(@file_get_contents(dirname(DIR_APPLICATION) . '/' . ltrim($href)), dirname($href));
$oct_content_to = str_replace('"background": "url("/catalog/view/javascript/ + self.params.additionalParams + self.resources.loading + ") no-repeat center center",','"background": "url(" + self.params.additionalParams + self.resources.loading + ") no-repeat center center",', $oct_content_to);
$oct_content_to = str_replace('/catalog/view/javascript/mf/','', $oct_content_to);
fwrite($oct_file, $oct_content_to);
fwrite($oct_file, PHP_EOL);
}
} else {
$oct_fn[$href] = $href;
}
}
fclose($oct_file);
if (file_exists($dir_minify . 'cache/' . $oct_name) && filesize($dir_minify . 'cache/' . $oct_name)) {
$oct_content = @file_get_contents($link_server . 'min/?f=min/cache/' . $oct_name);
if ($oct_content) {
@file_put_contents($dir_minify . 'cache/' . $oct_name, '');
$oct_file = fopen($dir_minify . 'cache/' . $oct_name, "a");
fwrite($oct_file, $oct_content);
fclose($oct_file);
}
}
} else {
foreach ($oct_scripts as $href => $script) {
$link = parse_url($href);
if (isset($link['host'])) {
$oct_fn[$href] = $href;
}
}
}
if (file_exists($dir_minify . 'cache/' . $oct_name)) {
$oct_scripts = [];
$oct_scripts[0] = $link_server . 'min/cache/' . $oct_name . "?" . date('YmdHis', filemtime($_SERVER['DOCUMENT_ROOT'] . '/min/cache/' . $oct_name));
$oct_scripts = array_merge($oct_scripts, $oct_fn);
}
}
return $oct_scripts;
}
}