sidedevorg /
miniphpfw
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.
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace SideDevOrg\MiniPhpFw; |
||||
| 4 | |||||
| 5 | use Mustache_Engine; |
||||
| 6 | use Mustache_Loader_FilesystemLoader; |
||||
| 7 | |||||
| 8 | /** |
||||
| 9 | * Framework Controller. |
||||
| 10 | */ |
||||
| 11 | class Controller |
||||
| 12 | { |
||||
| 13 | /** |
||||
| 14 | * Data array container. |
||||
| 15 | * |
||||
| 16 | * @var array |
||||
| 17 | */ |
||||
| 18 | private $data = []; |
||||
| 19 | |||||
| 20 | /** |
||||
| 21 | * I18n array container. |
||||
| 22 | * |
||||
| 23 | * @var array |
||||
| 24 | */ |
||||
| 25 | private $i18n = []; |
||||
| 26 | |||||
| 27 | /** |
||||
| 28 | * Request. |
||||
| 29 | * |
||||
| 30 | * @var \Psr\Http\Message\RequestInterface |
||||
| 31 | */ |
||||
| 32 | private $request; |
||||
| 33 | |||||
| 34 | /** |
||||
| 35 | * Mustache instance. |
||||
| 36 | * |
||||
| 37 | * @var Mustache_Engine |
||||
| 38 | */ |
||||
| 39 | private $mustacheInstance; |
||||
| 40 | |||||
| 41 | /** |
||||
| 42 | * Set request. |
||||
| 43 | * |
||||
| 44 | * @param \Psr\Http\Message\RequestInterface $request |
||||
| 45 | * |
||||
| 46 | * @return \Psr\Http\Message\RequestInterface |
||||
| 47 | */ |
||||
| 48 | 10 | public function setRequest(\Psr\Http\Message\RequestInterface $request) : \Psr\Http\Message\RequestInterface |
|||
| 49 | { |
||||
| 50 | 10 | return $this->request = $request; |
|||
| 51 | } |
||||
| 52 | |||||
| 53 | /** |
||||
| 54 | * Get header. |
||||
| 55 | * |
||||
| 56 | * @param string $name |
||||
| 57 | * |
||||
| 58 | * @return mixed srtring or null |
||||
| 59 | */ |
||||
| 60 | 5 | protected function header(string $name) |
|||
| 61 | { |
||||
| 62 | 5 | return isset($this->request->getHeaders()[$name]) ? |
|||
| 63 | 5 | $this->request->getHeaders()[$name][0] : null; |
|||
| 64 | } |
||||
| 65 | |||||
| 66 | /** |
||||
| 67 | * Get input. |
||||
| 68 | * |
||||
| 69 | * @param string $name |
||||
| 70 | * @param mixed $defaultValue |
||||
| 71 | * @param bool $autoTrim |
||||
| 72 | * |
||||
| 73 | * @return mixed |
||||
| 74 | */ |
||||
| 75 | 1 | protected function input(string $name, $defaultValue = null, bool $autoTrim = true) |
|||
| 76 | { |
||||
| 77 | 1 | $value = false; |
|||
| 78 | |||||
| 79 | 1 | if ($this->request->getMethod() === 'GET') { |
|||
| 80 | 1 | if (isset($this->request->getQueryParams()[$name])) { |
|||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||
| 81 | 1 | $value = $this->request->getQueryParams()[$name]; |
|||
| 82 | } |
||||
| 83 | } |
||||
| 84 | |||||
| 85 | 1 | if ($this->request->getMethod() === 'POST') { |
|||
| 86 | 1 | if (isset($this->request->getParsedBody()[$name])) { |
|||
|
0 ignored issues
–
show
The method
getParsedBody() does not exist on Psr\Http\Message\RequestInterface. It seems like you code against a sub-type of Psr\Http\Message\RequestInterface such as Psr\Http\Message\ServerRequestInterface.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 87 | 1 | $value = $this->request->getParsedBody()[$name]; |
|||
| 88 | } |
||||
| 89 | } |
||||
| 90 | |||||
| 91 | 1 | if ($value) { |
|||
| 92 | 1 | return (!$autoTrim) ? $value : trim($value); |
|||
| 93 | } |
||||
| 94 | |||||
| 95 | 1 | return $defaultValue; |
|||
| 96 | } |
||||
| 97 | |||||
| 98 | /** |
||||
| 99 | * Set data. |
||||
| 100 | * |
||||
| 101 | * @param mixed $key string or array |
||||
| 102 | * @param mixed $value if $key is string |
||||
| 103 | * |
||||
| 104 | * @return mixed |
||||
| 105 | */ |
||||
| 106 | 1 | protected function data($key, $value = false) |
|||
| 107 | { |
||||
| 108 | 1 | if (is_array($key)) { |
|||
| 109 | 1 | return $this->data = array_merge($this->data, $key); |
|||
| 110 | } |
||||
| 111 | |||||
| 112 | 1 | return $this->data[$key] = $value; |
|||
| 113 | } |
||||
| 114 | |||||
| 115 | /** |
||||
| 116 | * Get data. |
||||
| 117 | * |
||||
| 118 | * @return array |
||||
| 119 | */ |
||||
| 120 | 4 | protected function getData() : array |
|||
| 121 | { |
||||
| 122 | 4 | return $this->data; |
|||
| 123 | } |
||||
| 124 | |||||
| 125 | /** |
||||
| 126 | * Get lang key in file.key format. |
||||
| 127 | * |
||||
| 128 | * @param string $fileKey |
||||
| 129 | * @param bool|string $changeLang false or lang code |
||||
| 130 | * |
||||
| 131 | * @return string |
||||
| 132 | */ |
||||
| 133 | 1 | protected function lang($fileKey, $changeLang = false) : string |
|||
| 134 | { |
||||
| 135 | 1 | $lang = (!$changeLang) ? $this->header('lang') : $changeLang; |
|||
| 136 | |||||
| 137 | 1 | $search = explode('.', $fileKey); |
|||
| 138 | 1 | $file = $search[0].'.php'; |
|||
| 139 | 1 | $key = isset($search[1]) ? $search[1] : ''; |
|||
| 140 | |||||
| 141 | 1 | $route = $this->config('paths.i18n').'/'.$lang.'/'.$file; |
|||
|
0 ignored issues
–
show
Are you sure
$lang of type null|string|true can be used in concatenation?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 142 | 1 | if (!file_exists($route)) { |
|||
| 143 | 1 | return $fileKey; |
|||
| 144 | } |
||||
| 145 | |||||
| 146 | 1 | $data = require_once $route; |
|||
| 147 | |||||
| 148 | 1 | if (is_array($data)) { |
|||
| 149 | 1 | $this->i18n[$lang][$file] = $data; |
|||
| 150 | } |
||||
| 151 | |||||
| 152 | 1 | return isset($this->i18n[$lang][$file][$key]) ? |
|||
| 153 | 1 | $this->i18n[$lang][$file][$key] : $fileKey |
|||
| 154 | ; |
||||
| 155 | } |
||||
| 156 | |||||
| 157 | /** |
||||
| 158 | * Get config item. |
||||
| 159 | * |
||||
| 160 | * @param string $key |
||||
| 161 | * |
||||
| 162 | * @return mixed |
||||
| 163 | */ |
||||
| 164 | 4 | protected function config(string $key) |
|||
| 165 | { |
||||
| 166 | 4 | $config = json_decode($this->header('config'), true); |
|||
| 167 | 4 | $definition = explode('.', $key); |
|||
| 168 | 4 | $item = isset($config[$definition[0]]) ? $config[$definition[0]] : null; |
|||
| 169 | |||||
| 170 | 4 | $numberOfDefinitions = count($definition); |
|||
| 171 | |||||
| 172 | 4 | for ($i = 1; $i < $numberOfDefinitions; ++$i) { |
|||
| 173 | 4 | $item = isset($item[$definition[$i]]) ? $item[$definition[$i]] : null; |
|||
| 174 | } |
||||
| 175 | |||||
| 176 | 4 | return $item; |
|||
| 177 | } |
||||
| 178 | |||||
| 179 | /** |
||||
| 180 | * Get view. |
||||
| 181 | * |
||||
| 182 | * @param string $template |
||||
| 183 | * |
||||
| 184 | * @return string |
||||
| 185 | */ |
||||
| 186 | 3 | protected function view(string $template, array $data = []) : string |
|||
| 187 | { |
||||
| 188 | 3 | if (!$this->mustacheInstance) { |
|||
| 189 | 3 | $viewsRoute = $this->config('paths.view'); |
|||
| 190 | 3 | $options = ['extension' => '.hbs']; |
|||
| 191 | |||||
| 192 | 3 | $this->mustacheInstance = new Mustache_Engine(array( |
|||
| 193 | 3 | 'loader' => new Mustache_Loader_FilesystemLoader($viewsRoute, $options), |
|||
| 194 | 3 | 'partials_loader' => new Mustache_Loader_FilesystemLoader($viewsRoute, $options), |
|||
| 195 | 3 | 'charset' => 'UTF-8', |
|||
| 196 | )); |
||||
| 197 | } |
||||
| 198 | |||||
| 199 | 3 | $tpl = $this->mustacheInstance->loadTemplate($template); |
|||
| 200 | 3 | $data = array_merge($this->getData(), $data); |
|||
| 201 | |||||
| 202 | 3 | return $tpl->render($this->mapData($data)); |
|||
| 203 | } |
||||
| 204 | |||||
| 205 | /** |
||||
| 206 | * Map data to output. |
||||
| 207 | * |
||||
| 208 | * @param array $data |
||||
| 209 | * |
||||
| 210 | * @return array |
||||
| 211 | */ |
||||
| 212 | 3 | private function mapData(array $data) : array |
|||
| 213 | { |
||||
| 214 | $assets = [ |
||||
| 215 | 3 | 'js' => false, |
|||
| 216 | 'css' => false, |
||||
| 217 | ]; |
||||
| 218 | |||||
| 219 | 3 | $assets_path = $this->config('paths.assets_manifest'); |
|||
| 220 | |||||
| 221 | 3 | if (file_exists($assets_path)) { |
|||
| 222 | 3 | $assets_data = json_decode(file_get_contents($assets_path), true); |
|||
| 223 | $assets = [ |
||||
| 224 | 3 | 'js' => $assets_data['/app.js'], |
|||
| 225 | 3 | 'css' => $assets_data['/app.css'], |
|||
| 226 | ]; |
||||
| 227 | } |
||||
| 228 | |||||
| 229 | $tplData = [ |
||||
| 230 | 3 | 'fw' => [ |
|||
| 231 | 3 | 'assets' => $assets, |
|||
| 232 | 'i18n' => [], |
||||
| 233 | 3 | 'data' => $data, |
|||
| 234 | 3 | 'lang' => $this->header('lang'), |
|||
| 235 | ], |
||||
| 236 | ]; |
||||
| 237 | |||||
| 238 | 3 | return $tplData; |
|||
| 239 | } |
||||
| 240 | |||||
| 241 | /** |
||||
| 242 | * Get "not found" view. |
||||
| 243 | * |
||||
| 244 | * @return string |
||||
| 245 | */ |
||||
| 246 | 2 | public function not_found() : string |
|||
| 247 | { |
||||
| 248 | 2 | return $this->view('404'); |
|||
| 249 | } |
||||
| 250 | } |
||||
| 251 |