Code Duplication    Length = 15-50 lines in 4 locations

classes/autoptimizeBase.php 4 locations

@@ 159-173 (lines=15) @@
156
    }
157
    
158
    // unhide noptimize-tags
159
    protected function restore_noptimize($noptimize_in) {
160
        if ( strpos( $noptimize_in, '%%NOPTIMIZE%%' ) !== false ) { 
161
            $noptimize_out = preg_replace_callback(
162
                '#%%NOPTIMIZE'.AUTOPTIMIZE_HASH.'%%(.*?)%%NOPTIMIZE%%#is',
163
                create_function(
164
                    '$matches',
165
                    'return base64_decode($matches[1]);'
166
                ),
167
                $noptimize_in
168
            );
169
        } else {
170
            $noptimize_out = $noptimize_in;
171
        }
172
        return $noptimize_out;
173
    }
174
175
    protected function hide_iehacks($iehacks_in) {
176
        if ( strpos( $iehacks_in, '<!--[if' ) !== false ) { 
@@ 191-205 (lines=15) @@
188
        return $iehacks_out;
189
    }
190
191
    protected function restore_iehacks($iehacks_in) {
192
        if ( strpos( $iehacks_in, '%%IEHACK%%' ) !== false ) { 
193
            $iehacks_out = preg_replace_callback(
194
                '#%%IEHACK'.AUTOPTIMIZE_HASH.'%%(.*?)%%IEHACK%%#is',
195
                create_function(
196
                    '$matches',
197
                    'return base64_decode($matches[1]);'
198
                ),
199
                $iehacks_in
200
            );
201
        } else {
202
            $iehacks_out=$iehacks_in;
203
        }
204
        return $iehacks_out;
205
    }
206
207
    protected function hide_comments($comments_in) {
208
        if ( strpos( $comments_in, '<!--' ) !== false ) {
@@ 223-237 (lines=15) @@
220
        return $comments_out;
221
    }
222
223
    protected function restore_comments($comments_in) {
224
        if ( strpos( $comments_in, '%%COMMENTS%%' ) !== false ) {
225
            $comments_out = preg_replace_callback(
226
                '#%%COMMENTS'.AUTOPTIMIZE_HASH.'%%(.*?)%%COMMENTS%%#is',
227
                create_function(
228
                    '$matches',
229
                    'return base64_decode($matches[1]);'
230
                ),
231
                $comments_in
232
            );
233
        } else {
234
            $comments_out=$comments_in;
235
        }
236
        return $comments_out;
237
    }
238
239
    protected function url_replace_cdn( $url ) {
240
        // API filter to change base CDN URL
@@ 299-348 (lines=50) @@
296
    }
297
    
298
    // inject already minified code in optimized JS/CSS
299
    protected function inject_minified($in) {
300
        if ( strpos( $in, '%%INJECTLATER%%' ) !== false ) {
301
            $out = preg_replace_callback(
302
                '#\/\*\!%%INJECTLATER'.AUTOPTIMIZE_HASH.'%%(.*?)%%INJECTLATER%%\*\/#is',
303
                create_function(
304
                    '$matches',
305
                    '$filepath=base64_decode(strtok($matches[1],"|"));
306
                    $filehash=strtok("|");
307
                    $filecontent=file_get_contents($filepath);
308
                    
309
                    if ( md5($filecontent) === $filehash ) {
310
                        // remove BOM
311
                        $filecontent = preg_replace("#\x{EF}\x{BB}\x{BF}#","",$filecontent);
312
313
                        // remove comments and blank lines
314
                        if (substr($filepath,-3,3)===".js") {
315
                            $filecontent=preg_replace("#^\s*\/\/.*$#Um","",$filecontent);
316
                        }
317
318
                        $filecontent=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Um","",$filecontent);
319
                        $filecontent=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $filecontent);
320
321
                        // differentiate between JS, CSS and other files
322
                        if (substr($filepath,-3,3)===".js") {
323
                            if ((substr($filecontent,-1,1)!==";")&&(substr($filecontent,-1,1)!=="}")) {
324
                                $filecontent.=";";
325
                            }
326
327
                            if (get_option("autoptimize_js_trycatch")==="on") {
328
                                $filecontent="try{".$filecontent."}catch(e){}";
329
                            }
330
                        } else if ((substr($filepath,-4,4)===".css")) {
331
                            $filecontent=autoptimizeStyles::fixurls($filepath,$filecontent);
332
                        } else {
333
                            $filecontent="";
334
                        }
335
                    } else {
336
                        $filecontent="";
337
                    }
338
339
                    // return 
340
                    return "\n".$filecontent;'
341
                ),
342
                $in
343
            );
344
        } else {
345
            $out = $in;
346
        }
347
        return $out;
348
    }
349
    
350
    protected function minify_single($pathIn) {
351
		// determine JS or CSS and set var (also mimetype), return false if neither