Completed
Push — release-2.1 ( 15f485...0da2ef )
by Mathias
07:51
created
Sources/tasks/Buddy-Notify.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -19,7 +19,7 @@
 block discarded – undo
19 19
 class Buddy_Notify_Background extends SMF_BackgroundTask
20 20
 {
21 21
 	/**
22
-     * This executes the task - loads up the info, sets the alerts and loads up the email queue.
22
+	 * This executes the task - loads up the info, sets the alerts and loads up the email queue.
23 23
 	 * @return bool Always returns true
24 24
 	 */
25 25
 	public function execute()
Please login to merge, or discard this patch.
Sources/tasks/GroupReq-Notify.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -20,7 +20,7 @@
 block discarded – undo
20 20
 class GroupReq_Notify_Background extends SMF_BackgroundTask
21 21
 {
22 22
 	/**
23
-     * This executes the task - loads up the information, puts the email in the queue and inserts any alerts as needed.
23
+	 * This executes the task - loads up the information, puts the email in the queue and inserts any alerts as needed.
24 24
 	 * @return bool Always returns true.
25 25
 	 */
26 26
 	public function execute()
Please login to merge, or discard this patch.
Sources/tasks/GroupAct-Notify.php 1 patch
Indentation   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -19,10 +19,10 @@
 block discarded – undo
19 19
  */
20 20
 class GroupAct_Notify_Background extends SMF_BackgroundTask
21 21
 {
22
-    /**
23
-     * This executes the task - loads up the information, puts the email in the queue and inserts alerts as needed.
24
-     * @return bool Always returns true
25
-     */
22
+	/**
23
+	 * This executes the task - loads up the information, puts the email in the queue and inserts alerts as needed.
24
+	 * @return bool Always returns true
25
+	 */
26 26
 	public function execute()
27 27
 	{
28 28
 		global $sourcedir, $smcFunc, $language, $modSettings;
Please login to merge, or discard this patch.
Themes/default/GenericList.template.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -220,7 +220,7 @@
 block discarded – undo
220 220
 				),
221 221
 			),
222 222
 		);
223
-	*/
223
+	 */
224 224
 
225 225
 	// Are we using right-to-left orientation?
226 226
 	$first = $context['right_to_left'] ? 'last' : 'first';
Please login to merge, or discard this patch.
Themes/default/MoveTopic.template.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -63,7 +63,7 @@  discard block
 block discarded – undo
63 63
 	// Stick our "create a redirection topic" template in here...
64 64
 	template_redirect_options('move');
65 65
 
66
-    echo '
66
+	echo '
67 67
 					<input type="submit" value="', $txt['move_topic'], '" onclick="return submitThisOnce(this);" accesskey="s" class="button_submit">
68 68
 				</div>
69 69
 			</div>';
@@ -86,9 +86,9 @@  discard block
 block discarded – undo
86 86
  */
87 87
 function template_redirect_options($type)
88 88
 {
89
-    global $txt, $context, $modSettings;
89
+	global $txt, $context, $modSettings;
90 90
 
91
-    echo '
91
+	echo '
92 92
 					<label for="postRedirect"><input type="checkbox" name="postRedirect" id="postRedirect"', $context['is_approved'] ? ' checked' : '', ' onclick="', $context['is_approved'] ? '' : 'if (this.checked && !confirm(\'' . $txt[$type . '_topic_unapproved_js'] . '\')) return false; ', 'document.getElementById(\'reasonArea\').style.display = this.checked ? \'block\' : \'none\';" class="input_check"> ', $txt['post_redirection'], '.</label>
93 93
 					<fieldset id="reasonArea" style="margin-top: 1ex;', $context['is_approved'] ? '' : 'display: none;', '">
94 94
 						<dl class="settings">
Please login to merge, or discard this patch.
Sources/Who.php 1 patch
Indentation   +1 added lines, -1 removed lines patch added patch discarded remove patch
@@ -673,7 +673,7 @@
 block discarded – undo
673 673
 						'Nikola &quot;Dzonny&quot; Novakovi&cacute;',
674 674
 						// Localizers
675 675
 						'Dr. Deejay',
676
-                        			'd3vcho',
676
+									'd3vcho',
677 677
 						// Former Localizers
678 678
 						'Relyana',
679 679
 					),
Please login to merge, or discard this patch.
Sources/ManageBans.php 1 patch
Indentation   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -784,7 +784,7 @@  discard block
 block discarded – undo
784 784
 		)
785 785
 	);
786 786
 	while ($row = $smcFunc['db_fetch_assoc']($request))
787
-	    $error_ips[] = inet_dtop($row['ip']);
787
+		$error_ips[] = inet_dtop($row['ip']);
788 788
 	$smcFunc['db_free_result']($request);
789 789
 
790 790
 	return $error_ips;
@@ -2166,9 +2166,9 @@  discard block
 block discarded – undo
2166 2166
 
2167 2167
 	if ($low == '255.255.255.255') return 'unknown';
2168 2168
 	if ($low == $high)
2169
-	    return $low;
2169
+		return $low;
2170 2170
 	else
2171
-	    return $low . '-' . $high;
2171
+		return $low . '-' . $high;
2172 2172
 }
2173 2173
 
2174 2174
 /**
Please login to merge, or discard this patch.
Sources/minify/path-converter/src/Converter.php 1 patch
Indentation   +169 added lines, -169 removed lines patch added patch discarded remove patch
@@ -18,178 +18,178 @@
 block discarded – undo
18 18
  */
19 19
 class Converter
20 20
 {
21
-    /**
22
-     * @var string
23
-     */
24
-    protected $from;
25
-
26
-    /**
27
-     * @var string
28
-     */
29
-    protected $to;
30
-
31
-    /**
32
-     * @param string $from The original base path (directory, not file!)
33
-     * @param string $to   The new base path (directory, not file!)
34
-     */
35
-    public function __construct($from, $to)
36
-    {
37
-        $shared = $this->shared($from, $to);
38
-        if ($shared === '') {
39
-            // when both paths have nothing in common, one of them is probably
40
-            // absolute while the other is relative
41
-            $cwd = getcwd();
42
-            $from = strpos($from, $cwd) === 0 ? $from : $cwd.'/'.$from;
43
-            $to = strpos($to, $cwd) === 0 ? $to : $cwd.'/'.$to;
44
-
45
-            // or traveling the tree via `..`
46
-            // attempt to resolve path, or assume it's fine if it doesn't exist
47
-            $from = realpath($from) ?: $from;
48
-            $to = realpath($to) ?: $to;
49
-        }
50
-
51
-        $from = $this->dirname($from);
52
-        $to = $this->dirname($to);
53
-
54
-        $from = $this->normalize($from);
55
-        $to = $this->normalize($to);
56
-
57
-        $this->from = $from;
58
-        $this->to = $to;
59
-    }
60
-
61
-    /**
62
-     * Normalize path.
63
-     *
64
-     * @param string $path
65
-     *
66
-     * @return string
67
-     */
68
-    protected function normalize($path)
69
-    {
70
-        // deal with different operating systems' directory structure
71
-        $path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '/');
72
-
73
-        /*
21
+	/**
22
+	 * @var string
23
+	 */
24
+	protected $from;
25
+
26
+	/**
27
+	 * @var string
28
+	 */
29
+	protected $to;
30
+
31
+	/**
32
+	 * @param string $from The original base path (directory, not file!)
33
+	 * @param string $to   The new base path (directory, not file!)
34
+	 */
35
+	public function __construct($from, $to)
36
+	{
37
+		$shared = $this->shared($from, $to);
38
+		if ($shared === '') {
39
+			// when both paths have nothing in common, one of them is probably
40
+			// absolute while the other is relative
41
+			$cwd = getcwd();
42
+			$from = strpos($from, $cwd) === 0 ? $from : $cwd.'/'.$from;
43
+			$to = strpos($to, $cwd) === 0 ? $to : $cwd.'/'.$to;
44
+
45
+			// or traveling the tree via `..`
46
+			// attempt to resolve path, or assume it's fine if it doesn't exist
47
+			$from = realpath($from) ?: $from;
48
+			$to = realpath($to) ?: $to;
49
+		}
50
+
51
+		$from = $this->dirname($from);
52
+		$to = $this->dirname($to);
53
+
54
+		$from = $this->normalize($from);
55
+		$to = $this->normalize($to);
56
+
57
+		$this->from = $from;
58
+		$this->to = $to;
59
+	}
60
+
61
+	/**
62
+	 * Normalize path.
63
+	 *
64
+	 * @param string $path
65
+	 *
66
+	 * @return string
67
+	 */
68
+	protected function normalize($path)
69
+	{
70
+		// deal with different operating systems' directory structure
71
+		$path = rtrim(str_replace(DIRECTORY_SEPARATOR, '/', $path), '/');
72
+
73
+		/*
74 74
          * Example:
75 75
          *     /home/forkcms/frontend/cache/compiled_templates/../../core/layout/css/../images/img.gif
76 76
          * to
77 77
          *     /home/forkcms/frontend/core/layout/images/img.gif
78 78
          */
79
-        do {
80
-            $path = preg_replace('/[^\/]+(?<!\.\.)\/\.\.\//', '', $path, -1, $count);
81
-        } while ($count);
82
-
83
-        return $path;
84
-    }
85
-
86
-    /**
87
-     * Figure out the shared path of 2 locations.
88
-     *
89
-     * Example:
90
-     *     /home/forkcms/frontend/core/layout/images/img.gif
91
-     * and
92
-     *     /home/forkcms/frontend/cache/minified_css
93
-     * share
94
-     *     /home/forkcms/frontend
95
-     *
96
-     * @param string $path1
97
-     * @param string $path2
98
-     *
99
-     * @return string
100
-     */
101
-    protected function shared($path1, $path2)
102
-    {
103
-        // $path could theoretically be empty (e.g. no path is given), in which
104
-        // case it shouldn't expand to array(''), which would compare to one's
105
-        // root /
106
-        $path1 = $path1 ? explode('/', $path1) : array();
107
-        $path2 = $path2 ? explode('/', $path2) : array();
108
-
109
-        $shared = array();
110
-
111
-        // compare paths & strip identical ancestors
112
-        foreach ($path1 as $i => $chunk) {
113
-            if (isset($path2[$i]) && $path1[$i] == $path2[$i]) {
114
-                $shared[] = $chunk;
115
-            } else {
116
-                break;
117
-            }
118
-        }
119
-
120
-        return implode('/', $shared);
121
-    }
122
-
123
-    /**
124
-     * Convert paths relative from 1 file to another.
125
-     *
126
-     * E.g.
127
-     *     ../images/img.gif relative to /home/forkcms/frontend/core/layout/css
128
-     * should become:
129
-     *     ../../core/layout/images/img.gif relative to
130
-     *     /home/forkcms/frontend/cache/minified_css
131
-     *
132
-     * @param string $path The relative path that needs to be converted.
133
-     *
134
-     * @return string The new relative path.
135
-     */
136
-    public function convert($path)
137
-    {
138
-        // quit early if conversion makes no sense
139
-        if ($this->from === $this->to) {
140
-            return $path;
141
-        }
142
-
143
-        $path = $this->normalize($path);
144
-        // if we're not dealing with a relative path, just return absolute
145
-        if (strpos($path, '/') === 0) {
146
-            return $path;
147
-        }
148
-
149
-        // normalize paths
150
-        $path = $this->normalize($this->from.'/'.$path);
151
-
152
-        // strip shared ancestor paths
153
-        $shared = $this->shared($path, $this->to);
154
-        $path = mb_substr($path, mb_strlen($shared));
155
-        $to = mb_substr($this->to, mb_strlen($shared));
156
-
157
-        // add .. for every directory that needs to be traversed to new path
158
-        $to = str_repeat('../', mb_substr_count($to, '/'));
159
-
160
-        return $to.ltrim($path, '/');
161
-    }
162
-
163
-    /**
164
-     * Attempt to get the directory name from a path.
165
-     *
166
-     * @param string $path
167
-     *
168
-     * @return string
169
-     */
170
-    public function dirname($path)
171
-    {
172
-        if (is_file($path)) {
173
-            return dirname($path);
174
-        }
175
-
176
-        if (is_dir($path)) {
177
-            return rtrim($path, '/');
178
-        }
179
-
180
-        // no known file/dir, start making assumptions
181
-
182
-        // ends in / = dir
183
-        if (mb_substr($path, -1) === '/') {
184
-            return rtrim($path, '/');
185
-        }
186
-
187
-        // has a dot in the name, likely a file
188
-        if (preg_match('/.*\..*$/', basename($path)) !== 0) {
189
-            return dirname($path);
190
-        }
191
-
192
-        // you're on your own here!
193
-        return $path;
194
-    }
79
+		do {
80
+			$path = preg_replace('/[^\/]+(?<!\.\.)\/\.\.\//', '', $path, -1, $count);
81
+		} while ($count);
82
+
83
+		return $path;
84
+	}
85
+
86
+	/**
87
+	 * Figure out the shared path of 2 locations.
88
+	 *
89
+	 * Example:
90
+	 *     /home/forkcms/frontend/core/layout/images/img.gif
91
+	 * and
92
+	 *     /home/forkcms/frontend/cache/minified_css
93
+	 * share
94
+	 *     /home/forkcms/frontend
95
+	 *
96
+	 * @param string $path1
97
+	 * @param string $path2
98
+	 *
99
+	 * @return string
100
+	 */
101
+	protected function shared($path1, $path2)
102
+	{
103
+		// $path could theoretically be empty (e.g. no path is given), in which
104
+		// case it shouldn't expand to array(''), which would compare to one's
105
+		// root /
106
+		$path1 = $path1 ? explode('/', $path1) : array();
107
+		$path2 = $path2 ? explode('/', $path2) : array();
108
+
109
+		$shared = array();
110
+
111
+		// compare paths & strip identical ancestors
112
+		foreach ($path1 as $i => $chunk) {
113
+			if (isset($path2[$i]) && $path1[$i] == $path2[$i]) {
114
+				$shared[] = $chunk;
115
+			} else {
116
+				break;
117
+			}
118
+		}
119
+
120
+		return implode('/', $shared);
121
+	}
122
+
123
+	/**
124
+	 * Convert paths relative from 1 file to another.
125
+	 *
126
+	 * E.g.
127
+	 *     ../images/img.gif relative to /home/forkcms/frontend/core/layout/css
128
+	 * should become:
129
+	 *     ../../core/layout/images/img.gif relative to
130
+	 *     /home/forkcms/frontend/cache/minified_css
131
+	 *
132
+	 * @param string $path The relative path that needs to be converted.
133
+	 *
134
+	 * @return string The new relative path.
135
+	 */
136
+	public function convert($path)
137
+	{
138
+		// quit early if conversion makes no sense
139
+		if ($this->from === $this->to) {
140
+			return $path;
141
+		}
142
+
143
+		$path = $this->normalize($path);
144
+		// if we're not dealing with a relative path, just return absolute
145
+		if (strpos($path, '/') === 0) {
146
+			return $path;
147
+		}
148
+
149
+		// normalize paths
150
+		$path = $this->normalize($this->from.'/'.$path);
151
+
152
+		// strip shared ancestor paths
153
+		$shared = $this->shared($path, $this->to);
154
+		$path = mb_substr($path, mb_strlen($shared));
155
+		$to = mb_substr($this->to, mb_strlen($shared));
156
+
157
+		// add .. for every directory that needs to be traversed to new path
158
+		$to = str_repeat('../', mb_substr_count($to, '/'));
159
+
160
+		return $to.ltrim($path, '/');
161
+	}
162
+
163
+	/**
164
+	 * Attempt to get the directory name from a path.
165
+	 *
166
+	 * @param string $path
167
+	 *
168
+	 * @return string
169
+	 */
170
+	public function dirname($path)
171
+	{
172
+		if (is_file($path)) {
173
+			return dirname($path);
174
+		}
175
+
176
+		if (is_dir($path)) {
177
+			return rtrim($path, '/');
178
+		}
179
+
180
+		// no known file/dir, start making assumptions
181
+
182
+		// ends in / = dir
183
+		if (mb_substr($path, -1) === '/') {
184
+			return rtrim($path, '/');
185
+		}
186
+
187
+		// has a dot in the name, likely a file
188
+		if (preg_match('/.*\..*$/', basename($path)) !== 0) {
189
+			return dirname($path);
190
+		}
191
+
192
+		// you're on your own here!
193
+		return $path;
194
+	}
195 195
 }
Please login to merge, or discard this patch.
Sources/minify/src/Minify.php 1 patch
Indentation   +392 added lines, -392 removed lines patch added patch discarded remove patch
@@ -16,335 +16,335 @@  discard block
 block discarded – undo
16 16
  */
17 17
 abstract class Minify
18 18
 {
19
-    /**
20
-     * The data to be minified.
21
-     *
22
-     * @var string[]
23
-     */
24
-    protected $data = array();
25
-
26
-    /**
27
-     * Array of patterns to match.
28
-     *
29
-     * @var string[]
30
-     */
31
-    protected $patterns = array();
32
-
33
-    /**
34
-     * This array will hold content of strings and regular expressions that have
35
-     * been extracted from the JS source code, so we can reliably match "code",
36
-     * without having to worry about potential "code-like" characters inside.
37
-     *
38
-     * @var string[]
39
-     */
40
-    public $extracted = array();
41
-
42
-    /**
43
-     * Init the minify class - optionally, code may be passed along already.
44
-     */
45
-    public function __construct(/* $data = null, ... */)
46
-    {
47
-        // it's possible to add the source through the constructor as well ;)
48
-        if (func_num_args()) {
49
-            call_user_func_array(array($this, 'add'), func_get_args());
50
-        }
51
-    }
52
-
53
-    /**
54
-     * Add a file or straight-up code to be minified.
55
-     *
56
-     * @param string|string[] $data
57
-     */
58
-    public function add($data /* $data = null, ... */)
59
-    {
60
-        // bogus "usage" of parameter $data: scrutinizer warns this variable is
61
-        // not used (we're using func_get_args instead to support overloading),
62
-        // but it still needs to be defined because it makes no sense to have
63
-        // this function without argument :)
64
-        $args = array($data) + func_get_args();
65
-
66
-        // this method can be overloaded
67
-        foreach ($args as $data) {
68
-            if (is_array($data)) {
69
-                call_user_func_array(array($this, 'add'), $data);
70
-                continue;
71
-            }
72
-
73
-            // redefine var
74
-            $data = (string) $data;
75
-
76
-            // load data
77
-            $value = $this->load($data);
78
-            $key = ($data != $value) ? $data : count($this->data);
79
-
80
-            // replace CR linefeeds etc.
81
-            // @see https://github.com/matthiasmullie/minify/pull/139
82
-            $value = str_replace(array("\r\n", "\r"), "\n", $value);
83
-
84
-            // store data
85
-            $this->data[$key] = $value;
86
-        }
87
-    }
88
-
89
-    /**
90
-     * Minify the data & (optionally) saves it to a file.
91
-     *
92
-     * @param string[optional] $path Path to write the data to
93
-     *
94
-     * @return string The minified data
95
-     */
96
-    public function minify($path = null)
97
-    {
98
-        $content = $this->execute($path);
99
-
100
-        // save to path
101
-        if ($path !== null) {
102
-            $this->save($content, $path);
103
-        }
104
-
105
-        return $content;
106
-    }
107
-
108
-    /**
109
-     * Minify & gzip the data & (optionally) saves it to a file.
110
-     *
111
-     * @param string[optional] $path  Path to write the data to
112
-     * @param int[optional]    $level Compression level, from 0 to 9
113
-     *
114
-     * @return string The minified & gzipped data
115
-     */
116
-    public function gzip($path = null, $level = 9)
117
-    {
118
-        $content = $this->execute($path);
119
-        $content = gzencode($content, $level, FORCE_GZIP);
120
-
121
-        // save to path
122
-        if ($path !== null) {
123
-            $this->save($content, $path);
124
-        }
125
-
126
-        return $content;
127
-    }
128
-
129
-    /**
130
-     * Minify the data & write it to a CacheItemInterface object.
131
-     *
132
-     * @param CacheItemInterface $item Cache item to write the data to
133
-     *
134
-     * @return CacheItemInterface Cache item with the minifier data
135
-     */
136
-    public function cache(CacheItemInterface $item)
137
-    {
138
-        $content = $this->execute();
139
-        $item->set($content);
140
-
141
-        return $item;
142
-    }
143
-
144
-    /**
145
-     * Minify the data.
146
-     *
147
-     * @param string[optional] $path Path to write the data to
148
-     *
149
-     * @return string The minified data
150
-     */
151
-    abstract public function execute($path = null);
152
-
153
-    /**
154
-     * Load data.
155
-     *
156
-     * @param string $data Either a path to a file or the content itself
157
-     *
158
-     * @return string
159
-     */
160
-    protected function load($data)
161
-    {
162
-        // check if the data is a file
163
-        if ($this->canImportFile($data)) {
164
-            $data = file_get_contents($data);
165
-
166
-            // strip BOM, if any
167
-            if (substr($data, 0, 3) == "\xef\xbb\xbf") {
168
-                $data = substr($data, 3);
169
-            }
170
-        }
171
-
172
-        return $data;
173
-    }
174
-
175
-    /**
176
-     * Save to file.
177
-     *
178
-     * @param string $content The minified data
179
-     * @param string $path    The path to save the minified data to
180
-     *
181
-     * @throws IOException
182
-     */
183
-    protected function save($content, $path)
184
-    {
185
-        $handler = $this->openFileForWriting($path);
186
-
187
-        $this->writeToFile($handler, $content);
188
-
189
-        @fclose($handler);
190
-    }
191
-
192
-    /**
193
-     * Register a pattern to execute against the source content.
194
-     *
195
-     * @param string          $pattern     PCRE pattern
196
-     * @param string|callable $replacement Replacement value for matched pattern
197
-     */
198
-    protected function registerPattern($pattern, $replacement = '')
199
-    {
200
-        // study the pattern, we'll execute it more than once
201
-        $pattern .= 'S';
202
-
203
-        $this->patterns[] = array($pattern, $replacement);
204
-    }
205
-
206
-    /**
207
-     * We can't "just" run some regular expressions against JavaScript: it's a
208
-     * complex language. E.g. having an occurrence of // xyz would be a comment,
209
-     * unless it's used within a string. Of you could have something that looks
210
-     * like a 'string', but inside a comment.
211
-     * The only way to accurately replace these pieces is to traverse the JS one
212
-     * character at a time and try to find whatever starts first.
213
-     *
214
-     * @param string $content The content to replace patterns in
215
-     *
216
-     * @return string The (manipulated) content
217
-     */
218
-    protected function replace($content)
219
-    {
220
-        $processed = '';
221
-        $positions = array_fill(0, count($this->patterns), -1);
222
-        $matches = array();
223
-
224
-        while ($content) {
225
-            // find first match for all patterns
226
-            foreach ($this->patterns as $i => $pattern) {
227
-                list($pattern, $replacement) = $pattern;
228
-
229
-                // no need to re-run matches that are still in the part of the
230
-                // content that hasn't been processed
231
-                if ($positions[$i] >= 0) {
232
-                    continue;
233
-                }
234
-
235
-                $match = null;
236
-                if (preg_match($pattern, $content, $match)) {
237
-                    $matches[$i] = $match;
238
-
239
-                    // we'll store the match position as well; that way, we
240
-                    // don't have to redo all preg_matches after changing only
241
-                    // the first (we'll still know where those others are)
242
-                    $positions[$i] = strpos($content, $match[0]);
243
-                } else {
244
-                    // if the pattern couldn't be matched, there's no point in
245
-                    // executing it again in later runs on this same content;
246
-                    // ignore this one until we reach end of content
247
-                    unset($matches[$i]);
248
-                    $positions[$i] = strlen($content);
249
-                }
250
-            }
251
-
252
-            // no more matches to find: everything's been processed, break out
253
-            if (!$matches) {
254
-                $processed .= $content;
255
-                break;
256
-            }
257
-
258
-            // see which of the patterns actually found the first thing (we'll
259
-            // only want to execute that one, since we're unsure if what the
260
-            // other found was not inside what the first found)
261
-            $discardLength = min($positions);
262
-            $firstPattern = array_search($discardLength, $positions);
263
-            $match = $matches[$firstPattern][0];
264
-
265
-            // execute the pattern that matches earliest in the content string
266
-            list($pattern, $replacement) = $this->patterns[$firstPattern];
267
-            $replacement = $this->replacePattern($pattern, $replacement, $content);
268
-
269
-            // figure out which part of the string was unmatched; that's the
270
-            // part we'll execute the patterns on again next
271
-            $content = substr($content, $discardLength);
272
-            $unmatched = (string) substr($content, strpos($content, $match) + strlen($match));
273
-
274
-            // move the replaced part to $processed and prepare $content to
275
-            // again match batch of patterns against
276
-            $processed .= substr($replacement, 0, strlen($replacement) - strlen($unmatched));
277
-            $content = $unmatched;
278
-
279
-            // first match has been replaced & that content is to be left alone,
280
-            // the next matches will start after this replacement, so we should
281
-            // fix their offsets
282
-            foreach ($positions as $i => $position) {
283
-                $positions[$i] -= $discardLength + strlen($match);
284
-            }
285
-        }
286
-
287
-        return $processed;
288
-    }
289
-
290
-    /**
291
-     * This is where a pattern is matched against $content and the matches
292
-     * are replaced by their respective value.
293
-     * This function will be called plenty of times, where $content will always
294
-     * move up 1 character.
295
-     *
296
-     * @param string          $pattern     Pattern to match
297
-     * @param string|callable $replacement Replacement value
298
-     * @param string          $content     Content to match pattern against
299
-     *
300
-     * @return string
301
-     */
302
-    protected function replacePattern($pattern, $replacement, $content)
303
-    {
304
-        if (is_callable($replacement)) {
305
-            return preg_replace_callback($pattern, $replacement, $content, 1, $count);
306
-        } else {
307
-            return preg_replace($pattern, $replacement, $content, 1, $count);
308
-        }
309
-    }
310
-
311
-    /**
312
-     * Strings are a pattern we need to match, in order to ignore potential
313
-     * code-like content inside them, but we just want all of the string
314
-     * content to remain untouched.
315
-     *
316
-     * This method will replace all string content with simple STRING#
317
-     * placeholder text, so we've rid all strings from characters that may be
318
-     * misinterpreted. Original string content will be saved in $this->extracted
319
-     * and after doing all other minifying, we can restore the original content
320
-     * via restoreStrings().
321
-     *
322
-     * @param string[optional] $chars
323
-     */
324
-    protected function extractStrings($chars = '\'"')
325
-    {
326
-        // PHP only supports $this inside anonymous functions since 5.4
327
-        $minifier = $this;
328
-        $callback = function ($match) use ($minifier) {
329
-            // check the second index here, because the first always contains a quote
330
-            if ($match[2] === '') {
331
-                /*
19
+	/**
20
+	 * The data to be minified.
21
+	 *
22
+	 * @var string[]
23
+	 */
24
+	protected $data = array();
25
+
26
+	/**
27
+	 * Array of patterns to match.
28
+	 *
29
+	 * @var string[]
30
+	 */
31
+	protected $patterns = array();
32
+
33
+	/**
34
+	 * This array will hold content of strings and regular expressions that have
35
+	 * been extracted from the JS source code, so we can reliably match "code",
36
+	 * without having to worry about potential "code-like" characters inside.
37
+	 *
38
+	 * @var string[]
39
+	 */
40
+	public $extracted = array();
41
+
42
+	/**
43
+	 * Init the minify class - optionally, code may be passed along already.
44
+	 */
45
+	public function __construct(/* $data = null, ... */)
46
+	{
47
+		// it's possible to add the source through the constructor as well ;)
48
+		if (func_num_args()) {
49
+			call_user_func_array(array($this, 'add'), func_get_args());
50
+		}
51
+	}
52
+
53
+	/**
54
+	 * Add a file or straight-up code to be minified.
55
+	 *
56
+	 * @param string|string[] $data
57
+	 */
58
+	public function add($data /* $data = null, ... */)
59
+	{
60
+		// bogus "usage" of parameter $data: scrutinizer warns this variable is
61
+		// not used (we're using func_get_args instead to support overloading),
62
+		// but it still needs to be defined because it makes no sense to have
63
+		// this function without argument :)
64
+		$args = array($data) + func_get_args();
65
+
66
+		// this method can be overloaded
67
+		foreach ($args as $data) {
68
+			if (is_array($data)) {
69
+				call_user_func_array(array($this, 'add'), $data);
70
+				continue;
71
+			}
72
+
73
+			// redefine var
74
+			$data = (string) $data;
75
+
76
+			// load data
77
+			$value = $this->load($data);
78
+			$key = ($data != $value) ? $data : count($this->data);
79
+
80
+			// replace CR linefeeds etc.
81
+			// @see https://github.com/matthiasmullie/minify/pull/139
82
+			$value = str_replace(array("\r\n", "\r"), "\n", $value);
83
+
84
+			// store data
85
+			$this->data[$key] = $value;
86
+		}
87
+	}
88
+
89
+	/**
90
+	 * Minify the data & (optionally) saves it to a file.
91
+	 *
92
+	 * @param string[optional] $path Path to write the data to
93
+	 *
94
+	 * @return string The minified data
95
+	 */
96
+	public function minify($path = null)
97
+	{
98
+		$content = $this->execute($path);
99
+
100
+		// save to path
101
+		if ($path !== null) {
102
+			$this->save($content, $path);
103
+		}
104
+
105
+		return $content;
106
+	}
107
+
108
+	/**
109
+	 * Minify & gzip the data & (optionally) saves it to a file.
110
+	 *
111
+	 * @param string[optional] $path  Path to write the data to
112
+	 * @param int[optional]    $level Compression level, from 0 to 9
113
+	 *
114
+	 * @return string The minified & gzipped data
115
+	 */
116
+	public function gzip($path = null, $level = 9)
117
+	{
118
+		$content = $this->execute($path);
119
+		$content = gzencode($content, $level, FORCE_GZIP);
120
+
121
+		// save to path
122
+		if ($path !== null) {
123
+			$this->save($content, $path);
124
+		}
125
+
126
+		return $content;
127
+	}
128
+
129
+	/**
130
+	 * Minify the data & write it to a CacheItemInterface object.
131
+	 *
132
+	 * @param CacheItemInterface $item Cache item to write the data to
133
+	 *
134
+	 * @return CacheItemInterface Cache item with the minifier data
135
+	 */
136
+	public function cache(CacheItemInterface $item)
137
+	{
138
+		$content = $this->execute();
139
+		$item->set($content);
140
+
141
+		return $item;
142
+	}
143
+
144
+	/**
145
+	 * Minify the data.
146
+	 *
147
+	 * @param string[optional] $path Path to write the data to
148
+	 *
149
+	 * @return string The minified data
150
+	 */
151
+	abstract public function execute($path = null);
152
+
153
+	/**
154
+	 * Load data.
155
+	 *
156
+	 * @param string $data Either a path to a file or the content itself
157
+	 *
158
+	 * @return string
159
+	 */
160
+	protected function load($data)
161
+	{
162
+		// check if the data is a file
163
+		if ($this->canImportFile($data)) {
164
+			$data = file_get_contents($data);
165
+
166
+			// strip BOM, if any
167
+			if (substr($data, 0, 3) == "\xef\xbb\xbf") {
168
+				$data = substr($data, 3);
169
+			}
170
+		}
171
+
172
+		return $data;
173
+	}
174
+
175
+	/**
176
+	 * Save to file.
177
+	 *
178
+	 * @param string $content The minified data
179
+	 * @param string $path    The path to save the minified data to
180
+	 *
181
+	 * @throws IOException
182
+	 */
183
+	protected function save($content, $path)
184
+	{
185
+		$handler = $this->openFileForWriting($path);
186
+
187
+		$this->writeToFile($handler, $content);
188
+
189
+		@fclose($handler);
190
+	}
191
+
192
+	/**
193
+	 * Register a pattern to execute against the source content.
194
+	 *
195
+	 * @param string          $pattern     PCRE pattern
196
+	 * @param string|callable $replacement Replacement value for matched pattern
197
+	 */
198
+	protected function registerPattern($pattern, $replacement = '')
199
+	{
200
+		// study the pattern, we'll execute it more than once
201
+		$pattern .= 'S';
202
+
203
+		$this->patterns[] = array($pattern, $replacement);
204
+	}
205
+
206
+	/**
207
+	 * We can't "just" run some regular expressions against JavaScript: it's a
208
+	 * complex language. E.g. having an occurrence of // xyz would be a comment,
209
+	 * unless it's used within a string. Of you could have something that looks
210
+	 * like a 'string', but inside a comment.
211
+	 * The only way to accurately replace these pieces is to traverse the JS one
212
+	 * character at a time and try to find whatever starts first.
213
+	 *
214
+	 * @param string $content The content to replace patterns in
215
+	 *
216
+	 * @return string The (manipulated) content
217
+	 */
218
+	protected function replace($content)
219
+	{
220
+		$processed = '';
221
+		$positions = array_fill(0, count($this->patterns), -1);
222
+		$matches = array();
223
+
224
+		while ($content) {
225
+			// find first match for all patterns
226
+			foreach ($this->patterns as $i => $pattern) {
227
+				list($pattern, $replacement) = $pattern;
228
+
229
+				// no need to re-run matches that are still in the part of the
230
+				// content that hasn't been processed
231
+				if ($positions[$i] >= 0) {
232
+					continue;
233
+				}
234
+
235
+				$match = null;
236
+				if (preg_match($pattern, $content, $match)) {
237
+					$matches[$i] = $match;
238
+
239
+					// we'll store the match position as well; that way, we
240
+					// don't have to redo all preg_matches after changing only
241
+					// the first (we'll still know where those others are)
242
+					$positions[$i] = strpos($content, $match[0]);
243
+				} else {
244
+					// if the pattern couldn't be matched, there's no point in
245
+					// executing it again in later runs on this same content;
246
+					// ignore this one until we reach end of content
247
+					unset($matches[$i]);
248
+					$positions[$i] = strlen($content);
249
+				}
250
+			}
251
+
252
+			// no more matches to find: everything's been processed, break out
253
+			if (!$matches) {
254
+				$processed .= $content;
255
+				break;
256
+			}
257
+
258
+			// see which of the patterns actually found the first thing (we'll
259
+			// only want to execute that one, since we're unsure if what the
260
+			// other found was not inside what the first found)
261
+			$discardLength = min($positions);
262
+			$firstPattern = array_search($discardLength, $positions);
263
+			$match = $matches[$firstPattern][0];
264
+
265
+			// execute the pattern that matches earliest in the content string
266
+			list($pattern, $replacement) = $this->patterns[$firstPattern];
267
+			$replacement = $this->replacePattern($pattern, $replacement, $content);
268
+
269
+			// figure out which part of the string was unmatched; that's the
270
+			// part we'll execute the patterns on again next
271
+			$content = substr($content, $discardLength);
272
+			$unmatched = (string) substr($content, strpos($content, $match) + strlen($match));
273
+
274
+			// move the replaced part to $processed and prepare $content to
275
+			// again match batch of patterns against
276
+			$processed .= substr($replacement, 0, strlen($replacement) - strlen($unmatched));
277
+			$content = $unmatched;
278
+
279
+			// first match has been replaced & that content is to be left alone,
280
+			// the next matches will start after this replacement, so we should
281
+			// fix their offsets
282
+			foreach ($positions as $i => $position) {
283
+				$positions[$i] -= $discardLength + strlen($match);
284
+			}
285
+		}
286
+
287
+		return $processed;
288
+	}
289
+
290
+	/**
291
+	 * This is where a pattern is matched against $content and the matches
292
+	 * are replaced by their respective value.
293
+	 * This function will be called plenty of times, where $content will always
294
+	 * move up 1 character.
295
+	 *
296
+	 * @param string          $pattern     Pattern to match
297
+	 * @param string|callable $replacement Replacement value
298
+	 * @param string          $content     Content to match pattern against
299
+	 *
300
+	 * @return string
301
+	 */
302
+	protected function replacePattern($pattern, $replacement, $content)
303
+	{
304
+		if (is_callable($replacement)) {
305
+			return preg_replace_callback($pattern, $replacement, $content, 1, $count);
306
+		} else {
307
+			return preg_replace($pattern, $replacement, $content, 1, $count);
308
+		}
309
+	}
310
+
311
+	/**
312
+	 * Strings are a pattern we need to match, in order to ignore potential
313
+	 * code-like content inside them, but we just want all of the string
314
+	 * content to remain untouched.
315
+	 *
316
+	 * This method will replace all string content with simple STRING#
317
+	 * placeholder text, so we've rid all strings from characters that may be
318
+	 * misinterpreted. Original string content will be saved in $this->extracted
319
+	 * and after doing all other minifying, we can restore the original content
320
+	 * via restoreStrings().
321
+	 *
322
+	 * @param string[optional] $chars
323
+	 */
324
+	protected function extractStrings($chars = '\'"')
325
+	{
326
+		// PHP only supports $this inside anonymous functions since 5.4
327
+		$minifier = $this;
328
+		$callback = function ($match) use ($minifier) {
329
+			// check the second index here, because the first always contains a quote
330
+			if ($match[2] === '') {
331
+				/*
332 332
                  * Empty strings need no placeholder; they can't be confused for
333 333
                  * anything else anyway.
334 334
                  * But we still needed to match them, for the extraction routine
335 335
                  * to skip over this particular string.
336 336
                  */
337
-                return $match[0];
338
-            }
337
+				return $match[0];
338
+			}
339 339
 
340
-            $count = count($minifier->extracted);
341
-            $placeholder = $match[1].$count.$match[1];
342
-            $minifier->extracted[$placeholder] = $match[1].$match[2].$match[1];
340
+			$count = count($minifier->extracted);
341
+			$placeholder = $match[1].$count.$match[1];
342
+			$minifier->extracted[$placeholder] = $match[1].$match[2].$match[1];
343 343
 
344
-            return $placeholder;
345
-        };
344
+			return $placeholder;
345
+		};
346 346
 
347
-        /*
347
+		/*
348 348
          * The \\ messiness explained:
349 349
          * * Don't count ' or " as end-of-string if it's escaped (has backslash
350 350
          * in front of it)
@@ -356,75 +356,75 @@  discard block
 block discarded – undo
356 356
          * considered as escape-char (times 2) and to get it in the regex,
357 357
          * escaped (times 2)
358 358
          */
359
-        $this->registerPattern('/(['.$chars.'])(.*?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback);
360
-    }
361
-
362
-    /**
363
-     * This method will restore all extracted data (strings, regexes) that were
364
-     * replaced with placeholder text in extract*(). The original content was
365
-     * saved in $this->extracted.
366
-     *
367
-     * @param string $content
368
-     *
369
-     * @return string
370
-     */
371
-    protected function restoreExtractedData($content)
372
-    {
373
-        if (!$this->extracted) {
374
-            // nothing was extracted, nothing to restore
375
-            return $content;
376
-        }
377
-
378
-        $content = strtr($content, $this->extracted);
379
-
380
-        $this->extracted = array();
381
-
382
-        return $content;
383
-    }
384
-
385
-    /**
386
-     * Check if the path is a regular file and can be read.
387
-     *
388
-     * @param string $path
389
-     *
390
-     * @return bool
391
-     */
392
-    protected function canImportFile($path)
393
-    {
394
-        return strlen($path) < PHP_MAXPATHLEN && @is_file($path) && is_readable($path);
395
-    }
396
-
397
-    /**
398
-     * Attempts to open file specified by $path for writing.
399
-     *
400
-     * @param string $path The path to the file
401
-     *
402
-     * @return resource Specifier for the target file
403
-     *
404
-     * @throws IOException
405
-     */
406
-    protected function openFileForWriting($path)
407
-    {
408
-        if (($handler = @fopen($path, 'w')) === false) {
409
-            throw new IOException('The file "'.$path.'" could not be opened for writing. Check if PHP has enough permissions.');
410
-        }
411
-
412
-        return $handler;
413
-    }
414
-
415
-    /**
416
-     * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.
417
-     *
418
-     * @param resource $handler The resource to write to
419
-     * @param string   $content The content to write
420
-     * @param string   $path    The path to the file (for exception printing only)
421
-     *
422
-     * @throws IOException
423
-     */
424
-    protected function writeToFile($handler, $content, $path = '')
425
-    {
426
-        if (($result = @fwrite($handler, $content)) === false || ($result < strlen($content))) {
427
-            throw new IOException('The file "'.$path.'" could not be written to. Check your disk space and file permissions.');
428
-        }
429
-    }
359
+		$this->registerPattern('/(['.$chars.'])(.*?(?<!\\\\)(\\\\\\\\)*+)\\1/s', $callback);
360
+	}
361
+
362
+	/**
363
+	 * This method will restore all extracted data (strings, regexes) that were
364
+	 * replaced with placeholder text in extract*(). The original content was
365
+	 * saved in $this->extracted.
366
+	 *
367
+	 * @param string $content
368
+	 *
369
+	 * @return string
370
+	 */
371
+	protected function restoreExtractedData($content)
372
+	{
373
+		if (!$this->extracted) {
374
+			// nothing was extracted, nothing to restore
375
+			return $content;
376
+		}
377
+
378
+		$content = strtr($content, $this->extracted);
379
+
380
+		$this->extracted = array();
381
+
382
+		return $content;
383
+	}
384
+
385
+	/**
386
+	 * Check if the path is a regular file and can be read.
387
+	 *
388
+	 * @param string $path
389
+	 *
390
+	 * @return bool
391
+	 */
392
+	protected function canImportFile($path)
393
+	{
394
+		return strlen($path) < PHP_MAXPATHLEN && @is_file($path) && is_readable($path);
395
+	}
396
+
397
+	/**
398
+	 * Attempts to open file specified by $path for writing.
399
+	 *
400
+	 * @param string $path The path to the file
401
+	 *
402
+	 * @return resource Specifier for the target file
403
+	 *
404
+	 * @throws IOException
405
+	 */
406
+	protected function openFileForWriting($path)
407
+	{
408
+		if (($handler = @fopen($path, 'w')) === false) {
409
+			throw new IOException('The file "'.$path.'" could not be opened for writing. Check if PHP has enough permissions.');
410
+		}
411
+
412
+		return $handler;
413
+	}
414
+
415
+	/**
416
+	 * Attempts to write $content to the file specified by $handler. $path is used for printing exceptions.
417
+	 *
418
+	 * @param resource $handler The resource to write to
419
+	 * @param string   $content The content to write
420
+	 * @param string   $path    The path to the file (for exception printing only)
421
+	 *
422
+	 * @throws IOException
423
+	 */
424
+	protected function writeToFile($handler, $content, $path = '')
425
+	{
426
+		if (($result = @fwrite($handler, $content)) === false || ($result < strlen($content))) {
427
+			throw new IOException('The file "'.$path.'" could not be written to. Check your disk space and file permissions.');
428
+		}
429
+	}
430 430
 }
Please login to merge, or discard this patch.