vijinho /
kohana2-legacy
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 defined('SYSPATH') or die('No direct access allowed.'); |
||
| 2 | /** |
||
| 3 | * Loads and displays Kohana view files. Can also handle output of some binary |
||
| 4 | * files, such as image, Javascript, and CSS files. |
||
| 5 | * |
||
| 6 | * $Id: View.php 4072 2009-03-13 17:20:38Z jheathco $ |
||
| 7 | * |
||
| 8 | * @package Core |
||
| 9 | * @author Kohana Team |
||
| 10 | * @copyright (c) 2007-2008 Kohana Team |
||
| 11 | * @license http://kohanaphp.com/license.html |
||
| 12 | */ |
||
| 13 | class View_Core |
||
| 14 | { |
||
| 15 | |||
| 16 | // The view file name and type |
||
| 17 | protected $kohana_filename = false; |
||
| 18 | protected $kohana_filetype = false; |
||
| 19 | |||
| 20 | // View variable storage |
||
| 21 | protected $kohana_local_data = array(); |
||
| 22 | protected static $kohana_global_data = array(); |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Creates a new View using the given parameters. |
||
| 26 | * |
||
| 27 | * @param string view name |
||
| 28 | * @param array pre-load data |
||
| 29 | * @param string type of file: html, css, js, etc. |
||
| 30 | * @param string $name |
||
|
0 ignored issues
–
show
|
|||
| 31 | * @return View |
||
| 32 | */ |
||
| 33 | public static function factory($name = null, $data = null, $type = null) |
||
| 34 | { |
||
| 35 | return new View($name, $data, $type); |
||
| 36 | } |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Attempts to load a view and pre-load view data. |
||
| 40 | * |
||
| 41 | * @throws Kohana_Exception if the requested view cannot be found |
||
| 42 | * @param string view name |
||
| 43 | * @param array pre-load data |
||
| 44 | * @param string type of file: html, css, js, etc. |
||
| 45 | * @return void |
||
|
0 ignored issues
–
show
Comprehensibility
Best Practice
introduced
by
Adding a
@return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.
Adding a Please refer to the PHP core documentation on constructors. Loading history...
|
|||
| 46 | */ |
||
| 47 | public function __construct($name = null, $data = null, $type = null) |
||
| 48 | { |
||
| 49 | if (is_string($name) and $name !== '') { |
||
| 50 | // Set the filename |
||
| 51 | $this->set_filename($name, $type); |
||
| 52 | } |
||
| 53 | |||
| 54 | if (is_array($data) and ! empty($data)) { |
||
| 55 | // Preload data using array_merge, to allow user extensions |
||
| 56 | $this->kohana_local_data = array_merge($this->kohana_local_data, $data); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Magic method access to test for view property |
||
| 62 | * |
||
| 63 | * @param string View property to test for |
||
| 64 | * @return boolean |
||
|
0 ignored issues
–
show
Should the return type not be
array|boolean? Also, consider making the array more specific, something like array<String>, or String[].
This check compares the return type specified in the If the return type contains the type array, this check recommends the use of
a more specific type like Loading history...
|
|||
| 65 | */ |
||
| 66 | public function __isset($key = null) |
||
| 67 | { |
||
| 68 | return $this->is_set($key); |
||
| 69 | } |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Sets the view filename. |
||
| 73 | * |
||
| 74 | * @chainable |
||
| 75 | * @param string view filename |
||
| 76 | * @param string view file type |
||
| 77 | * @param string $name |
||
| 78 | * @return View_Core |
||
| 79 | */ |
||
| 80 | public function set_filename($name, $type = null) |
||
| 81 | { |
||
| 82 | if ($type == null) { |
||
| 83 | // Load the filename and set the content type |
||
| 84 | $this->kohana_filename = Kohana::find_file('views', $name, true); |
||
|
0 ignored issues
–
show
The property
$kohana_filename was declared of type boolean, but \Kohana::find_file('views', $name, true) is of type string. Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
| 85 | $this->kohana_filetype = EXT; |
||
|
0 ignored issues
–
show
The property
$kohana_filetype was declared of type boolean, but EXT is of type string. Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
| 86 | } else { |
||
| 87 | // Check if the filetype is allowed by the configuration |
||
| 88 | if (! in_array($type, Kohana::config('view.allowed_filetypes'))) { |
||
| 89 | throw new Kohana_Exception('core.invalid_filetype', $type); |
||
| 90 | } |
||
| 91 | |||
| 92 | // Load the filename and set the content type |
||
| 93 | $this->kohana_filename = Kohana::find_file('views', $name, true, $type); |
||
|
0 ignored issues
–
show
The property
$kohana_filename was declared of type boolean, but \Kohana::find_file('views', $name, true, $type) is of type string. Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
| 94 | $this->kohana_filetype = Kohana::config('mimes.'.$type); |
||
|
0 ignored issues
–
show
The property
$kohana_filetype was declared of type boolean, but \Kohana::config('mimes.' . $type) is of type string. Maybe add a type cast?
This check looks for assignments to scalar types that may be of the wrong type. To ensure the code behaves as expected, it may be a good idea to add an explicit type cast. $answer = 42;
$correct = false;
$correct = (bool) $answer;
Loading history...
|
|||
| 95 | |||
| 96 | if ($this->kohana_filetype == null) { |
||
| 97 | // Use the specified type |
||
| 98 | $this->kohana_filetype = $type; |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | return $this; |
||
| 103 | } |
||
| 104 | |||
| 105 | /** |
||
| 106 | * Sets a view variable. |
||
| 107 | * |
||
| 108 | * @param string|array name of variable or an array of variables |
||
| 109 | * @param mixed value when using a named variable |
||
| 110 | * @return View_Core |
||
| 111 | */ |
||
| 112 | public function set($name, $value = null) |
||
| 113 | { |
||
| 114 | if (is_array($name)) { |
||
| 115 | foreach ($name as $key => $value) { |
||
| 116 | $this->__set($key, $value); |
||
| 117 | } |
||
| 118 | } else { |
||
| 119 | $this->__set($name, $value); |
||
| 120 | } |
||
| 121 | |||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | /** |
||
| 126 | * Checks for a property existence in the view locally or globally. Unlike the built in __isset(), |
||
| 127 | * this method can take an array of properties to test simultaneously. |
||
| 128 | * |
||
| 129 | * @param string $key property name to test for |
||
|
0 ignored issues
–
show
Should the type for parameter
$key not be false|string?
This check looks for It makes a suggestion as to what type it considers more descriptive. Most often this is a case of a parameter that can be null in addition to its declared types. Loading history...
|
|||
| 130 | * @param array $key array of property names to test for |
||
| 131 | * @return boolean property test result |
||
| 132 | * @return array associative array of keys and boolean test result |
||
|
0 ignored issues
–
show
Should the return type not be
array|boolean? Also, consider making the array more specific, something like array<String>, or String[].
This check compares the return type specified in the If the return type contains the type array, this check recommends the use of
a more specific type like Loading history...
|
|||
| 133 | */ |
||
| 134 | public function is_set($key = false) |
||
| 135 | { |
||
| 136 | // Setup result; |
||
| 137 | $result = false; |
||
|
0 ignored issues
–
show
$result is not used, you could remove the assignment.
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently. $myVar = 'Value';
$higher = false;
if (rand(1, 6) > 3) {
$higher = true;
} else {
$higher = false;
}
Both the Loading history...
|
|||
| 138 | |||
| 139 | // If key is an array |
||
| 140 | if (is_array($key)) { |
||
| 141 | // Set the result to an array |
||
| 142 | $result = array(); |
||
| 143 | |||
| 144 | // Foreach key |
||
| 145 | foreach ($key as $property) { |
||
| 146 | // Set the result to an associative array |
||
| 147 | $result[$property] = (array_key_exists($property, $this->kohana_local_data) or array_key_exists($property, View::$kohana_global_data)) ? true : false; |
||
|
0 ignored issues
–
show
The property
$kohana_global_data is declared protected in View_Core. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 148 | } |
||
| 149 | } else { |
||
| 150 | // Otherwise just check one property |
||
| 151 | $result = (array_key_exists($key, $this->kohana_local_data) or array_key_exists($key, View::$kohana_global_data)) ? true : false; |
||
|
0 ignored issues
–
show
The property
$kohana_global_data is declared protected in View_Core. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 152 | } |
||
| 153 | |||
| 154 | // Return the result |
||
| 155 | return $result; |
||
| 156 | } |
||
| 157 | |||
| 158 | /** |
||
| 159 | * Sets a bound variable by reference. |
||
| 160 | * |
||
| 161 | * @param string name of variable |
||
| 162 | * @param mixed variable to assign by reference |
||
| 163 | * @param string $name |
||
| 164 | * @return View_Core |
||
| 165 | */ |
||
| 166 | public function bind($name, & $var) |
||
| 167 | { |
||
| 168 | $this->kohana_local_data[$name] =& $var; |
||
| 169 | |||
| 170 | return $this; |
||
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sets a view global variable. |
||
| 175 | * |
||
| 176 | * @param string|array name of variable or an array of variables |
||
| 177 | * @param mixed value when using a named variable |
||
| 178 | * @return void |
||
| 179 | */ |
||
| 180 | public static function set_global($name, $value = null) |
||
| 181 | { |
||
| 182 | if (is_array($name)) { |
||
| 183 | foreach ($name as $key => $value) { |
||
| 184 | View::$kohana_global_data[$key] = $value; |
||
|
0 ignored issues
–
show
The property
$kohana_global_data is declared protected in View_Core. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 185 | } |
||
| 186 | } else { |
||
| 187 | View::$kohana_global_data[$name] = $value; |
||
|
0 ignored issues
–
show
The property
$kohana_global_data is declared protected in View_Core. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Magically sets a view variable. |
||
| 193 | * |
||
| 194 | * @param string variable key |
||
| 195 | * @param string variable value |
||
| 196 | * @return void |
||
| 197 | */ |
||
| 198 | public function __set($key, $value) |
||
| 199 | { |
||
| 200 | $this->kohana_local_data[$key] = $value; |
||
| 201 | } |
||
| 202 | |||
| 203 | /** |
||
| 204 | * Magically gets a view variable. |
||
| 205 | * |
||
| 206 | * @param string variable key |
||
| 207 | * @return mixed variable value if the key is found |
||
| 208 | * @return void if the key is not found |
||
| 209 | */ |
||
| 210 | public function &__get($key) |
||
| 211 | { |
||
| 212 | if (isset($this->kohana_local_data[$key])) { |
||
| 213 | return $this->kohana_local_data[$key]; |
||
| 214 | } |
||
| 215 | |||
| 216 | if (isset(View::$kohana_global_data[$key])) { |
||
|
0 ignored issues
–
show
The property
$kohana_global_data is declared protected in View_Core. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 217 | return View::$kohana_global_data[$key]; |
||
|
0 ignored issues
–
show
The property
$kohana_global_data is declared protected in View_Core. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 218 | } |
||
| 219 | |||
| 220 | if (isset($this->$key)) { |
||
| 221 | return $this->$key; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Magically converts view object to string. |
||
| 227 | * |
||
| 228 | * @return string |
||
| 229 | */ |
||
| 230 | public function __toString() |
||
| 231 | { |
||
| 232 | try { |
||
| 233 | return $this->render(); |
||
| 234 | } catch (Exception $e) { |
||
| 235 | // Display the exception using its internal __toString method |
||
| 236 | return (string) $e; |
||
| 237 | } |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Renders a view. |
||
| 242 | * |
||
| 243 | * @param boolean set to TRUE to echo the output instead of returning it |
||
| 244 | * @param callback special renderer to pass the output through |
||
| 245 | * @return string if print is FALSE |
||
| 246 | * @return void if print is TRUE |
||
| 247 | */ |
||
| 248 | public function render($print = false, $renderer = false) |
||
| 249 | { |
||
| 250 | if (empty($this->kohana_filename)) { |
||
| 251 | throw new Kohana_Exception('core.view_set_filename'); |
||
| 252 | } |
||
| 253 | |||
| 254 | if (is_string($this->kohana_filetype)) { |
||
| 255 | // Merge global and local data, local overrides global with the same name |
||
| 256 | $data = array_merge(View::$kohana_global_data, $this->kohana_local_data); |
||
|
0 ignored issues
–
show
The property
$kohana_global_data is declared protected in View_Core. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.
Since your code implements the magic setter <?php
/**
* @property int $x
* @property int $y
* @property string $text
*/
class MyLabel
{
private $properties;
private $allowedProperties = array('x', 'y', 'text');
public function __get($name)
{
if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
return $properties[$name];
} else {
return null;
}
}
public function __set($name, $value)
{
if (in_array($name, $this->allowedProperties)) {
$properties[$name] = $value;
} else {
throw new \LogicException("Property $name is not defined.");
}
}
}
Since the property has write access only, you can use the @property-write annotation instead. Of course, you may also just have mistyped another name, in which case you should fix the error. See also the PhpDoc documentation for @property. Loading history...
|
|||
| 257 | |||
| 258 | // Load the view in the controller for access to $this |
||
| 259 | $output = Kohana::$instance->_kohana_load_view($this->kohana_filename, $data); |
||
| 260 | |||
| 261 | if ($renderer !== false and is_callable($renderer, true)) { |
||
| 262 | // Pass the output through the user defined renderer |
||
| 263 | $output = call_user_func($renderer, $output); |
||
| 264 | } |
||
| 265 | |||
| 266 | if ($print === true) { |
||
| 267 | // Display the output |
||
| 268 | echo $output; |
||
| 269 | return; |
||
| 270 | } |
||
| 271 | } else { |
||
| 272 | // Set the content type and size |
||
| 273 | header('Content-Type: '.$this->kohana_filetype[0]); |
||
| 274 | |||
| 275 | if ($print === true) { |
||
| 276 | if ($file = fopen($this->kohana_filename, 'rb')) { |
||
| 277 | // Display the output |
||
| 278 | fpassthru($file); |
||
| 279 | fclose($file); |
||
| 280 | } |
||
| 281 | return; |
||
| 282 | } |
||
| 283 | |||
| 284 | // Fetch the file contents |
||
| 285 | $output = file_get_contents($this->kohana_filename); |
||
| 286 | } |
||
| 287 | |||
| 288 | return $output; |
||
| 289 | } |
||
| 290 | } // End View |
||
| 291 |
This check looks for
@paramannotations where the type inferred by our type inference engine differs from the declared type.It makes a suggestion as to what type it considers more descriptive.
Most often this is a case of a parameter that can be null in addition to its declared types.