This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace Anax\Content; |
||
4 | |||
5 | /** |
||
6 | * Pages based on file content. |
||
7 | */ |
||
8 | class CPageContent |
||
9 | { |
||
10 | use \Anax\TConfigure, |
||
11 | \Anax\DI\TInjectionAware; |
||
12 | |||
13 | |||
14 | |||
15 | /** |
||
16 | * Properties. |
||
17 | */ |
||
18 | private $toc = null; |
||
19 | |||
20 | |||
21 | |||
22 | /** |
||
23 | * Map url to page if such mapping can be done. |
||
24 | * |
||
25 | * @throws NotFoundException when mapping can not be done. |
||
26 | */ |
||
27 | public function getContentForRoute() |
||
28 | { |
||
29 | $route = $this->di->request->getRoute(); |
||
30 | $parts = $this->di->request->getRouteParts(); |
||
31 | $toc = $this->getTableOfContent($parts[0]); |
||
32 | |||
33 | $route = $this->mapRoute2Toc($route, $toc); |
||
34 | $baseroute = dirname($route); |
||
35 | |||
36 | $filter = $this->config['textfilter']; |
||
37 | $title = $toc[$route]['title']; |
||
38 | $file = $toc[$route]['filename']; |
||
39 | |||
40 | //$content = $this->di->fileContent->get($baseroute . '/' . $file); |
||
41 | $basepath = $this->config['basepath']; |
||
42 | $target = "$basepath/$baseroute/$file"; |
||
43 | $content = file_get_contents($target); |
||
44 | $content = $this->di->textFilter->doFilter($content, $filter); |
||
45 | |||
46 | return [$title, $content, $toc]; |
||
47 | } |
||
48 | |||
49 | |||
50 | |||
51 | /** |
||
52 | * Map the route to the correct entry in the toc. |
||
53 | * |
||
54 | * @param string $route current route used to access page. |
||
55 | * @param array $toc the toc as array. |
||
56 | * |
||
57 | * @return string as the title for the content. |
||
58 | */ |
||
59 | public function mapRoute2Toc($route, $toc) |
||
60 | { |
||
61 | if (key_exists($route, $toc)) { |
||
62 | return $route; |
||
63 | } elseif (key_exists($route . "/index", $toc)) { |
||
64 | return $route . "/index"; |
||
65 | } |
||
66 | |||
67 | throw new \Anax\Exception\NotFoundException(t('The page does not exists.')); |
||
68 | } |
||
69 | |||
70 | |||
71 | |||
72 | /** |
||
73 | * Extract title from content. |
||
74 | * |
||
75 | * @param string $file filenam to load load content from. |
||
76 | * |
||
77 | * @return string as the title for the content. |
||
78 | */ |
||
79 | public function getTitleFromFirstLine($file) |
||
80 | { |
||
81 | $content = file_get_contents($file, false, null, -1, 512); |
||
82 | $title = strstr($content, "\n", true); |
||
83 | |||
84 | return $title; |
||
85 | } |
||
86 | |||
87 | |||
88 | |||
89 | /** |
||
90 | * Get table of content for all pages. |
||
91 | * |
||
92 | * @param string $id to use to generate key for toc. |
||
93 | * |
||
94 | * @return array as table of content. |
||
95 | */ |
||
96 | public function getTableOfContent($id) |
||
97 | { |
||
98 | if ($this->toc) { |
||
99 | return $this->toc; |
||
100 | } |
||
101 | |||
102 | $key = $this->di->cache->createKey(__CLASS__, 'toc-' . $id); |
||
103 | $this->toc = $this->di->cache->get($key); |
||
104 | |||
105 | if (!$this->toc) { |
||
106 | $this->toc = $this->createTableOfContent(); |
||
107 | $this->di->cache->put($key, $this->toc); |
||
108 | } |
||
109 | |||
110 | return $this->toc; |
||
111 | } |
||
112 | |||
113 | |||
114 | |||
115 | /** |
||
116 | * Generate ToC from directory structure, containing url, title and filename |
||
117 | * of each page. |
||
118 | * |
||
119 | * @return array as table of content. |
||
120 | */ |
||
121 | public function createTableOfContent() |
||
122 | { |
||
123 | $basepath = $this->config['basepath']; |
||
124 | $pattern = $this->config['pattern']; |
||
125 | $route = $this->di->request->getRoute(); |
||
126 | |||
127 | // if dir, add index if file exists. |
||
128 | // partly for adding doc/index to work |
||
129 | // partly to make doc/ generate proper toc. |
||
130 | $baseroute = dirname($route); |
||
131 | $path = $basepath . '/' . $baseroute . '/' . $pattern; |
||
132 | |||
133 | $toc = []; |
||
134 | foreach (glob($path) as $file) { |
||
135 | $parts = pathinfo($file); |
||
136 | $filename = $parts['filename']; |
||
137 | |||
138 | $title = $this->getTitleFromFirstLine($file); |
||
139 | $file2route = substr($filename, strpos($filename, '_') + 1); |
||
140 | |||
141 | $url = $baseroute . '/' . $file2route; |
||
142 | /* |
||
143 | if ($file2route == 'index' ) { |
||
144 | $url = $baseroute; |
||
145 | }*/ |
||
146 | |||
147 | // Create level depending on the file id |
||
148 | $id = substr($filename, 0, strpos($filename, '_')); |
||
149 | $level = 2; |
||
150 | View Code Duplication | if ($id % 100 === 0) { |
|
0 ignored issues
–
show
|
|||
151 | $level = 0; |
||
152 | } elseif ($id % 10 === 0) { |
||
153 | $level = 1; |
||
154 | } |
||
155 | |||
156 | $toc[$url] = [ |
||
157 | 'title' => $title, |
||
158 | 'filename' => $parts['basename'], |
||
159 | 'level' => $level, |
||
160 | ]; |
||
161 | } |
||
162 | |||
163 | return $toc; |
||
164 | } |
||
165 | } |
||
166 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.