xref: /plugin/siteexport/inc/patchCSSmgr.php (revision 0571ece201b9e3bc14846f6c88d943a4f1512014)
1<?php
2
3/**
4 Honestly: this is evil code to come by a problem in the cssmgr.php class of mpdf that breaks generating PDFs with large CSS files
5 It comes from: http://stackoverflow.com/questions/137006/redefine-class-methods-or-class
6*/
7class CSSMgrPatch {
8
9    private $_code;
10
11    public function __construct($include_file = null) {
12        if ($include_file) {
13            $this->includeCode($include_file);
14        }
15    }
16
17    public function setCode($code) {
18        $this->_code = $code;
19    }
20
21    public function includeCode($path) {
22
23        $fp = fopen($path, 'r');
24        $contents = fread($fp, filesize($path));
25        $contents = str_replace('<?php', '', $contents);
26        $contents = str_replace('?>', '', $contents);
27        fclose($fp);
28
29        $this->setCode($contents);
30    }
31
32    function redefineFunction($new_function) {
33
34        preg_match('/function (.+)\(/', $new_function, $aryMatches);
35        $func_name = trim($aryMatches[1]);
36
37        if (preg_match('/(function ' . $func_name . '[\w\W\n]+?)(function)/s', $this->_code, $aryMatches)) {
38
39            $search_code = $aryMatches[1];
40
41            $new_code = str_replace($search_code, $new_function . "\n\n", $this->_code);
42
43            $this->setCode($new_code);
44
45            return true;
46
47        } else {
48
49            return false;
50
51        }
52    }
53
54    function getCode() {
55        return $this->_code;
56    }
57}
58?>