Completed
Push — develop ( 8b7740...30f26a )
by
unknown
08:10 queued 04:46
created

Page::getFolder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * The page model
4
 */
5
namespace Phile\Model;
6
7
use Phile\Core\Router;
8
use Phile\Core\Event;
9
use Phile\Core\ServiceLocator;
10
11
/**
12
 * the Model class for a page
13
 *
14
 * @author  Frank Nägler
15
 * @link    https://philecms.com
16
 * @license http://opensource.org/licenses/MIT
17
 * @package Phile\Model
18
 */
19
class Page
20
{
21
    /**
22
     * @var \Phile\Model\Meta the meta model
23
     */
24
    protected $meta;
25
26
    /**
27
     * @var string the content
28
     */
29
    protected $content;
30
31
    /**
32
     * @var string the path to the original file
33
     */
34
    protected $filePath;
35
36
    /**
37
     * @var string the raw file
38
     */
39
    protected $rawData;
40
41
    /**
42
     * @var \Phile\ServiceLocator\ParserInterface the parser
43
     */
44
    protected $parser;
45
46
    /**
47
     * @var string the pageId of the page
48
     */
49
    protected $pageId;
50
51
    /**
52
     * @var \Phile\Model\Page the previous page if one exist
53
     */
54
    protected $previousPage;
55
56
    /**
57
     * @var \Phile\Model\Page the next page if one exist
58
     */
59
    protected $nextPage;
60
61
    /**
62
     * @var string The content folder, as passed to the class constructor when initiating the object.
63
     */
64
    protected $contentFolder = CONTENT_DIR;
65
66
    /**
67
     * the constructor
68
     *
69
     * @param $filePath
70
     * @param string   $folder
71
     */
72 14
    public function __construct($filePath, $folder = CONTENT_DIR)
73
    {
74 14
        $this->contentFolder = $folder;
75 14
        $this->setFilePath($filePath);
76
77
        /**
78
         * @triggerEvent before_load_content this event is triggered before the content is loaded
79
         *
80
         * @param            string filePath the path to the file
81
         * @param \Phile\Model\Page page     the page model
82
         */
83 14
        Event::triggerEvent('before_load_content', array('filePath' => &$this->filePath, 'page' => &$this));
84 14
        if (file_exists($this->filePath)) {
85 14
            $this->rawData = file_get_contents($this->filePath);
0 ignored issues
show
Security File Exposure introduced by
$this->filePath can contain request data and is used in file inclusion context(s) leading to a potential security vulnerability.

General Strategies to prevent injection

In general, it is advisable to prevent any user-data to reach this point. This can be done by white-listing certain values:

if ( ! in_array($value, array('this-is-allowed', 'and-this-too'), true)) {
    throw new \InvalidArgumentException('This input is not allowed.');
}

For numeric data, we recommend to explicitly cast the data:

$sanitized = (integer) $tainted;
Loading history...
86 14
            $this->parseRawData();
87 14
        }
88
        /**
89
         * @triggerEvent after_load_content this event is triggered after the content is loaded
90
         *
91
         * @param            string filePath the path to the file
92
         * @param            string rawData  the raw data
93
         * @param \Phile\Model\Page page     the page model
94
         */
95 14
        Event::triggerEvent(
96 14
            'after_load_content',
97
            [
98 14
                'filePath' => &$this->filePath,
99 14
                'rawData' => $this->rawData,
100
                'page' => &$this
101 14
            ]
102 14
        );
103
104 14
        $this->parser = ServiceLocator::getService('Phile_Parser');
105 14
    }
106
107
    /**
108
     * method to get content of page, this method returned the parsed content
109
     *
110
     * @return mixed
111
     */
112 3
    public function getContent()
113
    {
114
        /**
115
         * @triggerEvent before_parse_content this event is triggered before the content is parsed
116
         *
117
         * @param            string content the raw data
118
         * @param \Phile\Model\Page page    the page model
119
         */
120 3
        Event::triggerEvent('before_parse_content', array('content' => $this->content, 'page' => &$this));
121 3
        $content = $this->parser->parse($this->content);
122
        /**
123
         * @triggerEvent after_parse_content this event is triggered after the content is parsed
124
         *
125
         * @param            string content the parsed content
126
         * @param \Phile\Model\Page page    the page model
127
         */
128 3
        Event::triggerEvent('after_parse_content', array('content' => &$content, 'page' => &$this));
129
130 3
        return $content;
131
    }
132
133
    /**
134
     * set content of page
135
     *
136
     * @param $content
137
     */
138 3
    public function setContent($content)
139
    {
140 3
        $this->content = $content;
141 3
    }
142
143
    /**
144
     * get raw (un-parsed) page content
145
     *
146
     * @return string
147
     */
148 2
    public function getRawContent()
149
    {
150 2
        return $this->content;
151
    }
152
153
    /**
154
     * get the meta model
155
     *
156
     * @return Meta
157
     */
158 8
    public function getMeta()
159
    {
160 8
        return $this->meta;
161
    }
162
163
    /**
164
     * parse the raw content
165
     */
166 14
    protected function parseRawData()
167
    {
168 14
        $this->meta = new Meta($this->rawData);
169
        // Remove only the optional, leading meta-block comment
170 14
        $rawData = trim($this->rawData);
171 14
        if (strncmp('<!--', $rawData, 4) === 0) {
172
            // leading meta-block comment uses the <!-- --> style
173 14
            $this->content = substr($rawData, max(4, strpos($rawData, '-->') + 3));
174 14
        } elseif (strncmp('/*', $rawData, 2) === 0) {
175
            // leading meta-block comment uses the /* */ style
176
            $this->content = substr($rawData, strpos($rawData, '*/') + 2);
177
        } else {
178
            // no leading meta-block comment
179
            $this->content = $rawData;
180
        }
181 14
    }
182
183
    /**
184
     * get the title of page from meta information
185
     *
186
     * @return string|null
187
     */
188 5
    public function getTitle()
189
    {
190 5
        return $this->getMeta()->get('title');
191
    }
192
193
    /**
194
     * get Phile $pageId
195
     *
196
     * @param  string $filePath
197
     * @return string
198
     */
199 14
    protected function buildPageId($filePath)
200
    {
201 14
        $pageId = str_replace($this->contentFolder, '', $filePath);
202 14
        $pageId = str_replace(CONTENT_EXT, '', $pageId);
203 14
        $pageId = str_replace(DIRECTORY_SEPARATOR, '/', $pageId);
204 14
        $pageId = ltrim($pageId, '/');
205 14
        $pageId = preg_replace('/(?<=^|\/)index$/', '', $pageId);
206 14
        return $pageId;
207
    }
208
209
    /**
210
     * get the url of page
211
     *
212
     * @return string
213
     */
214 2
    public function getUrl()
215
    {
216 2
        return (new Router)->urlForPage($this->pageId, false);
217
    }
218
219
    /**
220
     * set the filepath of the page
221
     *
222
     * @param string $filePath
223
     */
224 14
    public function setFilePath($filePath)
225
    {
226 14
        $this->filePath = $filePath;
227 14
        $this->pageId = $this->buildPageId($this->filePath);
228 14
    }
229
230
    /**
231
     * get the filepath of the page
232
     *
233
     * @return string
234
     */
235 3
    public function getFilePath()
236
    {
237 3
        return $this->filePath;
238
    }
239
240
    /**
241
     * get the folder name
242
     *
243
     * @return string
244
     */
245
    public function getFolder()
246
    {
247
        return basename(dirname($this->getFilePath()));
248
    }
249
250 2
    public function getPageId()
251
    {
252 2
        return $this->pageId;
253
    }
254
255
    /**
256
     * get the previous page if one exist
257
     *
258
     * @return null|\Phile\Model\Page
259
     */
260 1
    public function getPreviousPage()
261
    {
262 1
        $pageRepository = new \Phile\Repository\Page();
263 1
        return $pageRepository->getPageOffset($this, -1);
264
    }
265
266
    /**
267
     * get the next page if one exist
268
     *
269
     * @return null|\Phile\Model\Page
270
     */
271 1
    public function getNextPage()
272
    {
273 1
        $pageRepository = new \Phile\Repository\Page();
274 1
        return $pageRepository->getPageOffset($this, 1);
275
    }
276
}
277