Code Duplication    Length = 15-43 lines in 6 locations

classes/autoptimizeBase.php 6 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%%(.*?)%%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 ) { 
@@ 175-189 (lines=15) @@
172
        return $noptimize_out;
173
    }
174
175
    protected function hide_iehacks($iehacks_in) {
176
        if ( strpos( $iehacks_in, '<!--[if' ) !== false ) { 
177
            $iehacks_out = preg_replace_callback(
178
                '#<!--\[if.*?\[endif\]-->#is',
179
                create_function(
180
                    '$matches',
181
                    'return "%%IEHACK%%".base64_encode($matches[0])."%%IEHACK%%";'
182
                ),
183
                $iehacks_in
184
            );
185
        } else {
186
            $iehacks_out = $iehacks_in;
187
        }
188
        return $iehacks_out;
189
    }
190
191
    protected function restore_iehacks($iehacks_in) {
192
        if ( strpos( $iehacks_in, '%%IEHACK%%' ) !== 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%%(.*?)%%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 ) {
@@ 207-221 (lines=15) @@
204
        return $iehacks_out;
205
    }
206
207
    protected function hide_comments($comments_in) {
208
        if ( strpos( $comments_in, '<!--' ) !== false ) {
209
            $comments_out = preg_replace_callback(
210
                '#<!--.*?-->#is',
211
                create_function(
212
                    '$matches',
213
                    'return "%%COMMENTS%%".base64_encode($matches[0])."%%COMMENTS%%";'
214
                ),
215
                $comments_in
216
            );
217
        } else {
218
            $comments_out = $comments_in;
219
        }
220
        return $comments_out;
221
    }
222
223
    protected function restore_comments($comments_in) {
224
        if ( strpos( $comments_in, '%%COMMENTS%%' ) !== 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%%(.*?)%%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-341 (lines=43) @@
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%%(.*?)%%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
                        error_log("hash ".$filehash." matches calculated ".md5($filecontent)." for ".$filepath.", proceeding");
311
                        // remove BOM
312
                        $filecontent = preg_replace("#\x{EF}\x{BB}\x{BF}#","",$filecontent);
313
314
                        // remove comments and blank lines
315
                        if (substr($filepath,-3,3)===".js") {
316
                            $filecontent=preg_replace("#^\s*\/\/.*$#Um","",$filecontent);
317
                        }
318
319
                        $filecontent=preg_replace("#^\s*\/\*[^!].*\*\/\s?#Um","",$filecontent);
320
                        $filecontent=preg_replace("#(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+#", "\n", $filecontent);
321
322
                        // differentiate between JS, CSS and other files
323
                        if (substr($filepath,-3,3)===".js") {
324
                            if ((substr($filecontent,-1,1)!==";")&&(substr($filecontent,-1,1)!=="}")) {
325
                                $filecontent.=";";
326
                            }
327
328
                            if (get_option("autoptimize_js_trycatch")==="on") {
329
                                $filecontent="try{".$filecontent."}catch(e){}";
330
                            }
331
                        } else if ((substr($filepath,-4,4)===".css")) {
332
                            $filecontent=autoptimizeStyles::fixurls($filepath,$filecontent);
333
                        } else {
334
                            $filecontent="";
335
                        }
336
                    } else {
337
                        error_log("hash ".$filehash." does not match calculated ".md5($filecontent)." for ".$filepath.", proceeding");
338
                        $filecontent="";
339
                    }
340
341
                    // return 
342
                    return "\n".$filecontent;'
343
                ),
344
                $in