Mabilis::load_compiler()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 0
dl 0
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/** * ************************************************
4
 * Image CMS Template Engine (Mabilis TPL)
5
 *
6
 * Simple template engine for Image CMS based on regular expressions search and replace.
7
 *
8
 * @author <[email protected]>
9
 * @version 0.3 PHP5
10
 * ************************************************ */
11
class Mabilis
12
{
13
14
    /**
15
     * @var Mabilis_Compiler
16
     */
17
    private $compiler;
18
19
    /**
20
     * @var Mabilis_Config
21
     */
22
    private $config;
23
24
    /**
25
     * Mabilis constructor.
26
     * @param array $config
27
     */
28
    public function __construct(&$config = []) {
29
30
        $this->load_config($config);
31
    }
32
33
    /**
34
     * Display or fetch template file
35
     * @param string $file
36
     * @param array $data
37
     * @param bool $return
38
     * @return string
0 ignored issues
show
Documentation introduced by
Should the return type not be string|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
39
     */
40
    public function view($file, $data = [], $return = FALSE) {
41
42
        // Delete double .tpl.tpl
43
        //TODO remove this and find root of problem
44
        if (false !== strpos($file, '.tpl.tpl')) {
45
            $file = substr($file, 0, -4);
46
        }
47
48
        if (preg_match('/file:/', $file, $_Matches)) {
49
            $file_dir = preg_replace('/\/\//', '/', $file);
50
            $file_dir = preg_replace('/file\:/', '', $file_dir);
51
        } elseif (preg_match('/dir:/', $file, $_Matches)) {
52
            $file_dir = preg_replace('/\/\//', '/', $file);
53
            $file_dir = preg_replace('/dir\:/', '', $file_dir);
54
        } else {
55
            $file_dir = $this->config->tpl_path . $file;
56
        }
57
        $all_tpl_path = $this->config->tpl_path . 'shop/default/';
58
        //if (preg_match('/application\/modules/', $file_dir, $mm))
59
        if (strpos($file_dir, 'application\modules')) {
60
            $newFile = explode('application\modules', $file_dir);
61
            $new_file_dir = $all_tpl_path . 'modules' . $newFile[1];
62
63
            if (file_exists($new_file_dir)) {
64
                $file_dir = $new_file_dir;
65
            }
66
        }
67
68
        $compiled_file = $this->config->compile_path . md5($file_dir) . $this->config->compiled_ext;
69
70
        if (!file_exists($compiled_file) OR $this->config->force_compile == TRUE) {
71
            // Compile file
72
            $this->load_compiler();
73
            $this->compiler->compile($file_dir);
74
        }
75
76
        extract($data);
77
78
        ob_start();
79
80
        if (file_exists($compiled_file)) {
81
            include $compiled_file;
82
        } else {
83
            print '<p class="error">Error: ' . $compiled_file . ' does not exists!</p>';
84
        }
85
86
        // Time to live expried
87
        /* @var $mabilis_ttl integer */
88
        if ($mabilis_ttl <= time()) {
89
            @unlink($compiled_file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
90
        }
91
92
        if ($this->config->use_filemtime == TRUE AND $mabilis_last_modified != @filemtime($file_dir)) {
93
            @unlink($compiled_file);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
94
        }
95
96
        if ($return == TRUE) {
97
            $buffer = ob_get_contents();
98
            ob_end_clean();
99
            return $buffer;
100
        }
101
102
        ob_end_flush();
103
    }
104
105
    /**
106
     * @param array $config
107
     * @return bool
108
     */
109
    public function load_config($config = []) {
110
111
        if (extension_loaded('zlib') AND $config['compress_output'] == TRUE) {
112
            if (isset($_SERVER['HTTP_ACCEPT_ENCODING']) AND strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE) {
113
                ob_start('ob_gzhandler');
114
            }
115
        }
116
117
        if ($this->config == NULL) {
118
            include 'Config.class.php';
119
            $this->config = new Mabilis_Config($config);
120
        }
121
122
        return TRUE;
123
    }
124
125
    /**
126
     * @param string $param
127
     * @param string $value
128
     */
129
    public function set_config_value($param, $value) {
130
131
        $this->config->$param = $value;
132
    }
133
134
    /**
135
     * @param $param
136
     * @return mixed
137
     */
138
    public function get_config_value($param) {
139
140
        if (isset($this->config->$param)) {
141
            return $this->config->$param;
142
        }
143
    }
144
145
    /**
146
     * Load compiler class if not loaded yet
147
     */
148
    public function load_compiler() {
149
150
        if ($this->compiler == NULL) {
151
            include 'Mabilis.compiler.php';
152
            $this->compiler = new Mabilis_Compiler($this->config);
153
        }
154
155
        return TRUE;
156
    }
157
158
}
159
160
/* End of Mabilis.class.php */