pongo /
firebase-php
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 |
||
|
0 ignored issues
–
show
|
|||
| 2 | namespace Firebase; |
||
| 3 | |||
| 4 | require_once __DIR__ . '/firebaseInterface.php'; |
||
| 5 | |||
| 6 | use \Exception; |
||
| 7 | |||
| 8 | |||
| 9 | /** |
||
| 10 | * Firebase PHP Client Library |
||
| 11 | * |
||
| 12 | * @author Tamas Kalman <[email protected]> |
||
| 13 | * @url https://github.com/ktamas77/firebase-php/ |
||
| 14 | * @link https://www.firebase.com/docs/rest-api.html |
||
| 15 | */ |
||
| 16 | |||
| 17 | /** |
||
| 18 | * Firebase PHP Class |
||
| 19 | * |
||
| 20 | * @author Tamas Kalman <[email protected]> |
||
| 21 | * @link https://www.firebase.com/docs/rest-api.html |
||
| 22 | */ |
||
| 23 | class FirebaseLib implements FirebaseInterface |
||
| 24 | { |
||
| 25 | private $_baseURI; |
||
| 26 | private $_timeout; |
||
| 27 | private $_token; |
||
| 28 | private $_curlHandler; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Constructor |
||
| 32 | * |
||
| 33 | * @param string $baseURI |
||
| 34 | * @param string $token |
||
| 35 | */ |
||
| 36 | function __construct($baseURI = '', $token = '') |
||
|
0 ignored issues
–
show
|
|||
| 37 | { |
||
| 38 | if ($baseURI == '') { |
||
| 39 | trigger_error('You must provide a baseURI variable.', E_USER_ERROR); |
||
| 40 | } |
||
| 41 | |||
| 42 | if (!extension_loaded('curl')) { |
||
| 43 | trigger_error('Extension CURL is not loaded.', E_USER_ERROR); |
||
| 44 | } |
||
| 45 | |||
| 46 | $this->setBaseURI($baseURI); |
||
| 47 | $this->setTimeOut(10); |
||
| 48 | $this->setToken($token); |
||
| 49 | $this->initCurlHandler(); |
||
| 50 | } |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Initializing the CURL handler |
||
| 54 | * |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | public function initCurlHandler() |
||
| 58 | { |
||
| 59 | $this->_curlHandler = curl_init(); |
||
| 60 | } |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Closing the CURL handler |
||
| 64 | * |
||
| 65 | * @return void |
||
| 66 | */ |
||
| 67 | public function closeCurlHandler() |
||
| 68 | { |
||
| 69 | curl_close($this->_curlHandler); |
||
| 70 | } |
||
| 71 | |||
| 72 | /** |
||
| 73 | * Sets Token |
||
| 74 | * |
||
| 75 | * @param string $token Token |
||
| 76 | * |
||
| 77 | * @return void |
||
| 78 | */ |
||
| 79 | public function setToken($token) |
||
| 80 | { |
||
| 81 | $this->_token = $token; |
||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * Sets Base URI, ex: http://yourcompany.firebase.com/youruser |
||
| 86 | * |
||
| 87 | * @param string $baseURI Base URI |
||
| 88 | * |
||
| 89 | * @return void |
||
| 90 | */ |
||
| 91 | public function setBaseURI($baseURI) |
||
| 92 | { |
||
| 93 | $baseURI .= (substr($baseURI, -1) == '/' ? '' : '/'); |
||
| 94 | $this->_baseURI = $baseURI; |
||
| 95 | } |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Returns with the normalized JSON absolute path |
||
| 99 | * |
||
| 100 | * @param string $path Path |
||
| 101 | * @param array $options Options |
||
| 102 | * @return string |
||
| 103 | */ |
||
| 104 | private function _getJsonPath($path, $options = array()) |
||
| 105 | { |
||
| 106 | $url = $this->_baseURI; |
||
| 107 | if ($this->_token !== '') { |
||
| 108 | $options['auth'] = $this->_token; |
||
| 109 | } |
||
| 110 | $path = ltrim($path, '/'); |
||
| 111 | return $url . $path . '.json?' . http_build_query($options); |
||
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Sets REST call timeout in seconds |
||
| 116 | * |
||
| 117 | * @param integer $seconds Seconds to timeout |
||
| 118 | * |
||
| 119 | * @return void |
||
| 120 | */ |
||
| 121 | public function setTimeOut($seconds) |
||
| 122 | { |
||
| 123 | $this->_timeout = $seconds; |
||
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Writing data into Firebase with a PUT request |
||
| 128 | * HTTP 200: Ok |
||
| 129 | * |
||
| 130 | * @param string $path Path |
||
| 131 | * @param mixed $data Data |
||
| 132 | * @param array $options Options |
||
| 133 | * |
||
| 134 | * @return array Response |
||
| 135 | */ |
||
| 136 | public function set($path, $data, $options = array()) |
||
| 137 | { |
||
| 138 | return $this->_writeData($path, $data, 'PUT', $options); |
||
| 139 | } |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Pushing data into Firebase with a POST request |
||
| 143 | * HTTP 200: Ok |
||
| 144 | * |
||
| 145 | * @param string $path Path |
||
| 146 | * @param mixed $data Data |
||
| 147 | * @param array $options Options |
||
| 148 | * |
||
| 149 | * @return array Response |
||
| 150 | */ |
||
| 151 | public function push($path, $data, $options = array()) |
||
| 152 | { |
||
| 153 | return $this->_writeData($path, $data, 'POST', $options); |
||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * Updating data into Firebase with a PATH request |
||
| 158 | * HTTP 200: Ok |
||
| 159 | * |
||
| 160 | * @param string $path Path |
||
| 161 | * @param mixed $data Data |
||
| 162 | * @param array $options Options |
||
| 163 | * |
||
| 164 | * @return array Response |
||
| 165 | */ |
||
| 166 | public function update($path, $data, $options = array()) |
||
| 167 | { |
||
| 168 | return $this->_writeData($path, $data, 'PATCH', $options); |
||
| 169 | } |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Reading data from Firebase |
||
| 173 | * HTTP 200: Ok |
||
| 174 | * |
||
| 175 | * @param string $path Path |
||
| 176 | * @param array $options Options |
||
| 177 | * |
||
| 178 | * @return array Response |
||
| 179 | */ |
||
| 180 | View Code Duplication | public function get($path, $options = array()) |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 181 | { |
||
| 182 | try { |
||
| 183 | $ch = $this->_getCurlHandler($path, 'GET', $options); |
||
| 184 | $return = $this->_my_curl_exec($ch); |
||
| 185 | } catch (Exception $e) { |
||
| 186 | $return = null; |
||
| 187 | } |
||
| 188 | return $return; |
||
| 189 | } |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Deletes data from Firebase |
||
| 193 | * HTTP 204: Ok |
||
| 194 | * |
||
| 195 | * @param string $path Path |
||
| 196 | * @param array $options Options |
||
| 197 | * |
||
| 198 | * @return array Response |
||
| 199 | */ |
||
| 200 | View Code Duplication | public function delete($path, $options = array()) |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
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. Loading history...
|
|||
| 201 | { |
||
| 202 | try { |
||
| 203 | $ch = $this->_getCurlHandler($path, 'DELETE', $options); |
||
| 204 | $return = $this->_my_curl_exec($ch); |
||
| 205 | } catch (Exception $e) { |
||
| 206 | $return = null; |
||
| 207 | } |
||
| 208 | return $return; |
||
| 209 | } |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Returns with Initialized CURL Handler |
||
| 213 | * |
||
| 214 | * @param string $path Path |
||
| 215 | * @param string $mode Mode |
||
| 216 | * @param array $options Options |
||
| 217 | * |
||
| 218 | * @return resource Curl Handler |
||
| 219 | */ |
||
| 220 | private function _getCurlHandler($path, $mode, $options = array()) |
||
| 221 | { |
||
| 222 | $url = $this->_getJsonPath($path, $options); |
||
| 223 | $ch = $this->_curlHandler; |
||
| 224 | curl_setopt($ch, CURLOPT_URL, $url); |
||
| 225 | curl_setopt($ch, CURLOPT_TIMEOUT, $this->_timeout); |
||
| 226 | curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $this->_timeout); |
||
| 227 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||
| 228 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
| 229 | curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $mode); |
||
| 230 | return $ch; |
||
| 231 | } |
||
| 232 | |||
| 233 | /** |
||
| 234 | * @param string $path |
||
| 235 | */ |
||
| 236 | private function _writeData($path, $data, $method = 'PUT', $options = array()) |
||
| 237 | { |
||
| 238 | $jsonData = json_encode($data); |
||
| 239 | $header = array( |
||
| 240 | 'Content-Type: application/json', |
||
| 241 | 'Content-Length: ' . strlen($jsonData) |
||
| 242 | ); |
||
| 243 | try { |
||
| 244 | $ch = $this->_getCurlHandler($path, $method, $options); |
||
| 245 | curl_setopt($ch, CURLOPT_HTTPHEADER, $header); |
||
| 246 | curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonData); |
||
| 247 | $return = $this->_my_curl_exec($ch); |
||
| 248 | } catch (Exception $e) { |
||
| 249 | $return = null; |
||
| 250 | } |
||
| 251 | return $return; |
||
| 252 | } |
||
| 253 | |||
| 254 | // своя функция curl_exec, которая вручную обрабатывает 'location redirect'. |
||
| 255 | // это нужно, т.к. на сервере отключен CURLOPT_FOLLOWLOCATION |
||
| 256 | // решение частично отсюда http://stackoverflow.com/a/6918742/136559 |
||
| 257 | |||
| 258 | /** |
||
| 259 | * @param resource $curl |
||
| 260 | */ |
||
| 261 | private function _my_curl_exec($curl) |
||
| 262 | { |
||
| 263 | $html = curl_exec($curl); |
||
| 264 | $status = curl_getinfo($curl); |
||
| 265 | |||
| 266 | if ($status['http_code'] === 301 || $status['http_code'] === 302) { |
||
| 267 | list($header) = explode("\r\n\r\n", $html, 2); |
||
| 268 | $matches = array(); |
||
| 269 | preg_match("/(Location:|URI:)[^(\n)]*/", $header, $matches); |
||
| 270 | $url = trim(str_replace($matches[1], '', $matches[0])); |
||
| 271 | $url_parsed = parse_url($url); |
||
| 272 | if (isset($url_parsed)) { |
||
| 273 | curl_setopt($curl, CURLOPT_URL, $url); |
||
| 274 | return $this->_my_curl_exec($curl); |
||
| 275 | } |
||
| 276 | return ''; |
||
| 277 | } |
||
| 278 | return $html; |
||
| 279 | } |
||
| 280 | |||
| 281 | } |
||
| 282 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.