fwk /
Xml
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 | * Fwk |
||
| 4 | * |
||
| 5 | * Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>. |
||
| 6 | * All rights reserved. |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | * |
||
| 11 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||
| 12 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||
| 13 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
||
| 14 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
||
| 15 | * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
||
| 16 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
||
| 17 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
||
| 18 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
||
| 19 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
| 20 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
||
| 21 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
||
| 22 | * POSSIBILITY OF SUCH DAMAGE. |
||
| 23 | * |
||
| 24 | * PHP Version 5.3 |
||
| 25 | * |
||
| 26 | * @category XML |
||
| 27 | * @package Fwk\Xml |
||
| 28 | * @author Julien Ballestracci <[email protected]> |
||
| 29 | * @copyright 2011-2014 Julien Ballestracci <[email protected]> |
||
| 30 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
||
| 31 | * @link http://www.nitronet.org/fwk |
||
| 32 | */ |
||
| 33 | namespace Fwk\Xml; |
||
| 34 | |||
| 35 | use SimpleXMLElement; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * XmlFile |
||
| 39 | * |
||
| 40 | * Represents an existing XML file on filesystem. Uses SimpleXML. |
||
| 41 | * |
||
| 42 | * @category Library |
||
| 43 | * @package Fwk\Xml |
||
| 44 | * @author Julien Ballestracci <[email protected]> |
||
| 45 | * @license http://www.opensource.org/licenses/bsd-license.php BSD License |
||
| 46 | * @link http://www.nitronet.org/fwk |
||
| 47 | */ |
||
| 48 | class XmlFile |
||
| 49 | { |
||
| 50 | /** |
||
| 51 | * Path to XML file |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $path; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * Root SimpleXML node |
||
| 58 | * @var SimpleXMLElement |
||
| 59 | */ |
||
| 60 | protected $xml; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Constructor |
||
| 64 | * |
||
| 65 | * @param string $filePath Path to XML file |
||
| 66 | * |
||
| 67 | * @throws \InvalidArgumentException if path links to a directory |
||
| 68 | * @return void |
||
|
0 ignored issues
–
show
|
|||
| 69 | */ |
||
| 70 | 20 | public function __construct($filePath) |
|
| 71 | { |
||
| 72 | 20 | if (is_dir($filePath)) { |
|
| 73 | 1 | throw new \InvalidArgumentException( |
|
| 74 | 1 | sprintf("File %s is a directory", $filePath) |
|
| 75 | ); |
||
| 76 | } |
||
| 77 | |||
| 78 | 20 | if (\function_exists('libxml_use_internal_errors')) { |
|
| 79 | 20 | \libxml_use_internal_errors(true); |
|
| 80 | } |
||
| 81 | |||
| 82 | 20 | $this->path = $filePath; |
|
| 83 | 20 | } |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Tells if the file exists |
||
| 87 | * |
||
| 88 | * @return boolean |
||
| 89 | */ |
||
| 90 | 19 | public function exists() |
|
| 91 | { |
||
| 92 | 19 | return is_file($this->getRealPath()); |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Try to return the real path of the file. If not possible, |
||
| 97 | * returns user-submitted path (@see __construct) |
||
| 98 | * |
||
| 99 | * @return string |
||
| 100 | */ |
||
| 101 | 19 | public function getRealPath() |
|
| 102 | { |
||
| 103 | 19 | $rpt = \realpath($this->path); |
|
| 104 | |||
| 105 | 19 | return ($rpt === false ? $this->path : $rpt); |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Tells if the file is readable |
||
| 110 | * |
||
| 111 | * @return boolean |
||
| 112 | */ |
||
| 113 | 16 | public function isReadable() |
|
| 114 | { |
||
| 115 | 16 | return ($this->exists() ? is_readable($this->path) : false); |
|
| 116 | } |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Opens the XML file (if not already done) and return the SimpleXML root |
||
| 120 | * node. |
||
| 121 | * |
||
| 122 | * @throws Exceptions\FileNotFoundException If file not found/readable |
||
| 123 | * @throws Exceptions\XmlErrorException If XML errors were found (libxml) |
||
| 124 | * @return SimpleXMLElement |
||
| 125 | */ |
||
| 126 | 16 | public function open() |
|
| 127 | { |
||
| 128 | 16 | if (!isset($this->xml)) { |
|
| 129 | 16 | if (!$this->isReadable()) { |
|
| 130 | 1 | throw new Exceptions\FileNotFoundException( |
|
| 131 | 1 | "XML file not found/readable: ". $this->path |
|
| 132 | ); |
||
| 133 | } |
||
| 134 | |||
| 135 | 15 | if (!$this->xml = simplexml_load_file($this->getRealPath())) { |
|
| 136 | 1 | $error = sprintf( |
|
| 137 | 1 | "%s [%s]", |
|
| 138 | 1 | libxml_get_last_error()->message, |
|
| 139 | 1 | \libxml_get_last_error()->code |
|
| 140 | ); |
||
| 141 | |||
| 142 | 1 | throw new Exceptions\XmlErrorException($error); |
|
| 143 | } |
||
| 144 | } |
||
| 145 | |||
| 146 | 14 | return $this->xml; |
|
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Returns the contents of the XML file |
||
| 151 | * |
||
| 152 | * @return string |
||
|
0 ignored issues
–
show
|
|||
| 153 | */ |
||
| 154 | 1 | public function __toString() |
|
| 155 | { |
||
| 156 | 1 | return $this->open()->asXML(); |
|
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | * SimpleXML wrapper to access XML nodes the simple way: $file->node->name |
||
| 161 | * |
||
| 162 | * @param string $var XML node name |
||
| 163 | * |
||
| 164 | * @return mixed |
||
| 165 | */ |
||
| 166 | 1 | public function __get($var) |
|
| 167 | { |
||
| 168 | 1 | return $this->open()->{$var}; |
|
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * SimpleXML wrapper to do a Xpath query on the file. |
||
| 173 | * |
||
| 174 | * @param string $path Path to XML node |
||
| 175 | * |
||
| 176 | * @return mixed |
||
| 177 | */ |
||
| 178 | 12 | public function xpath($path) |
|
| 179 | { |
||
| 180 | 12 | return $this->open()->xpath($path); |
|
| 181 | } |
||
| 182 | } |
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.