@@ -48,70 +48,70 @@ discard block |
||
| 48 | 48 | */ |
| 49 | 49 | class fritzbox_api { |
| 50 | 50 | /** |
| 51 | - * @var object config object |
|
| 52 | - */ |
|
| 51 | + * @var object config object |
|
| 52 | + */ |
|
| 53 | 53 | public $config = array(); |
| 54 | 54 | |
| 55 | 55 | /** |
| 56 | - * @var string the session ID, set by method initSID() after login |
|
| 57 | - */ |
|
| 56 | + * @var string the session ID, set by method initSID() after login |
|
| 57 | + */ |
|
| 58 | 58 | protected $sid = '0000000000000000'; |
| 59 | 59 | |
| 60 | 60 | |
| 61 | 61 | /** |
| 62 | - * the constructor, initializes the object and calls the login method |
|
| 63 | - * |
|
| 64 | - * @access public |
|
| 65 | - */ |
|
| 62 | + * the constructor, initializes the object and calls the login method |
|
| 63 | + * |
|
| 64 | + * @access public |
|
| 65 | + */ |
|
| 66 | 66 | public function __construct($password = false,$user_name = false,$fritzbox_ip = 'fritz.box',$force_local_login = false) |
| 67 | 67 | { |
| 68 | - // init the config object |
|
| 69 | - $this->config = new fritzbox_api_config(); |
|
| 68 | + // init the config object |
|
| 69 | + $this->config = new fritzbox_api_config(); |
|
| 70 | 70 | |
| 71 | - // try autoloading the config |
|
| 72 | - if (file_exists(__DIR__ . '/fritzbox_user.conf.php') && is_readable(__DIR__ . '/fritzbox_user.conf.php') ) { |
|
| 73 | - require_once(__DIR__ . '/fritzbox_user.conf.php'); |
|
| 74 | - } |
|
| 71 | + // try autoloading the config |
|
| 72 | + if (file_exists(__DIR__ . '/fritzbox_user.conf.php') && is_readable(__DIR__ . '/fritzbox_user.conf.php') ) { |
|
| 73 | + require_once(__DIR__ . '/fritzbox_user.conf.php'); |
|
| 74 | + } |
|
| 75 | 75 | |
| 76 | - // set FRITZ!Box-IP and URL |
|
| 77 | - $this->config->setItem('fritzbox_ip',$fritzbox_ip); |
|
| 76 | + // set FRITZ!Box-IP and URL |
|
| 77 | + $this->config->setItem('fritzbox_ip',$fritzbox_ip); |
|
| 78 | 78 | |
| 79 | - // check if login on local network (fritz.box) or via a dynamic DNS-host |
|
| 80 | - if ($fritzbox_ip != 'fritz.box' && !$force_local_login) { |
|
| 81 | - $this->config->setItem('enable_remote_config',true); |
|
| 82 | - $this->config->setItem('remote_config_user',$user_name); |
|
| 83 | - $this->config->setItem('remote_config_password',$password); |
|
| 84 | - $this->config->setItem('fritzbox_url', 'https://'.$this->config->getItem('fritzbox_ip')); |
|
| 85 | - } else { |
|
| 86 | - $this->config->setItem('enable_remote_config',false); |
|
| 87 | - if($user_name != false){ |
|
| 88 | - $this->config->setItem('username',$user_name); |
|
| 89 | - } |
|
| 90 | - if($password != false){ |
|
| 91 | - $this->config->setItem('password',$password); |
|
| 92 | - } |
|
| 93 | - $this->config->setItem('fritzbox_url', 'http://' . $this->config->getItem('fritzbox_ip')); |
|
| 94 | - } |
|
| 79 | + // check if login on local network (fritz.box) or via a dynamic DNS-host |
|
| 80 | + if ($fritzbox_ip != 'fritz.box' && !$force_local_login) { |
|
| 81 | + $this->config->setItem('enable_remote_config',true); |
|
| 82 | + $this->config->setItem('remote_config_user',$user_name); |
|
| 83 | + $this->config->setItem('remote_config_password',$password); |
|
| 84 | + $this->config->setItem('fritzbox_url', 'https://'.$this->config->getItem('fritzbox_ip')); |
|
| 85 | + } else { |
|
| 86 | + $this->config->setItem('enable_remote_config',false); |
|
| 87 | + if($user_name != false){ |
|
| 88 | + $this->config->setItem('username',$user_name); |
|
| 89 | + } |
|
| 90 | + if($password != false){ |
|
| 91 | + $this->config->setItem('password',$password); |
|
| 92 | + } |
|
| 93 | + $this->config->setItem('fritzbox_url', 'http://' . $this->config->getItem('fritzbox_ip')); |
|
| 94 | + } |
|
| 95 | 95 | |
| 96 | - // make some config consistency checks |
|
| 97 | - if ( $this->config->getItem('enable_remote_config') === true ){ |
|
| 98 | - if ( !$this->config->getItem('remote_config_user') || !$this->config->getItem('remote_config_password') ){ |
|
| 99 | - $this->error('ERROR: Remote config mode enabled, but no username or no password provided'); |
|
| 100 | - } |
|
| 101 | - } |
|
| 102 | - else { |
|
| 103 | - $this->config->setItem('old_remote_config_user', null); |
|
| 104 | - $this->config->setItem('old_remote_config_password', null); |
|
| 105 | - } |
|
| 106 | - $this->sid = $this->initSID(); |
|
| 96 | + // make some config consistency checks |
|
| 97 | + if ( $this->config->getItem('enable_remote_config') === true ){ |
|
| 98 | + if ( !$this->config->getItem('remote_config_user') || !$this->config->getItem('remote_config_password') ){ |
|
| 99 | + $this->error('ERROR: Remote config mode enabled, but no username or no password provided'); |
|
| 100 | + } |
|
| 101 | + } |
|
| 102 | + else { |
|
| 103 | + $this->config->setItem('old_remote_config_user', null); |
|
| 104 | + $this->config->setItem('old_remote_config_password', null); |
|
| 105 | + } |
|
| 106 | + $this->sid = $this->initSID(); |
|
| 107 | 107 | } |
| 108 | 108 | |
| 109 | 109 | |
| 110 | 110 | /** |
| 111 | - * the destructor just calls the logout method |
|
| 112 | - * |
|
| 113 | - * @access public |
|
| 114 | - */ |
|
| 111 | + * the destructor just calls the logout method |
|
| 112 | + * |
|
| 113 | + * @access public |
|
| 114 | + */ |
|
| 115 | 115 | public function __destruct() |
| 116 | 116 | { |
| 117 | 117 | $this->logout(); |
@@ -119,12 +119,12 @@ discard block |
||
| 119 | 119 | |
| 120 | 120 | |
| 121 | 121 | /** |
| 122 | - * do a POST request on the box |
|
| 123 | - * the main cURL wrapper handles the command |
|
| 124 | - * |
|
| 125 | - * @param array $formfields an associative array with the POST fields to pass |
|
| 126 | - * @return string the raw HTML code returned by the Fritz!Box |
|
| 127 | - */ |
|
| 122 | + * do a POST request on the box |
|
| 123 | + * the main cURL wrapper handles the command |
|
| 124 | + * |
|
| 125 | + * @param array $formfields an associative array with the POST fields to pass |
|
| 126 | + * @return string the raw HTML code returned by the Fritz!Box |
|
| 127 | + */ |
|
| 128 | 128 | public function doPostForm($formfields = array()) |
| 129 | 129 | { |
| 130 | 130 | $ch = curl_init(); |
@@ -147,23 +147,23 @@ discard block |
||
| 147 | 147 | curl_setopt($ch, CURLOPT_POST, 1); |
| 148 | 148 | if ( $this->config->getItem('enable_remote_config') == true ) |
| 149 | 149 | { |
| 150 | - // set name of SSL-certificate (must be same as remote-hostname (dynDNS) of FRITZ!Box) and remove port in address if alternate port is given |
|
| 151 | - if (strpos($this->config->getItem('fritzbox_ip'),":")){ |
|
| 152 | - $ssl_cert_fritzbox = explode(":", $this->config->getItem('fritzbox_ip')); |
|
| 153 | - $ssl_cert_fritzbox = $ssl_cert_fritzbox[0]; |
|
| 154 | - } else { |
|
| 155 | - $ssl_cert_fritzbox = $this->config->getItem('fritzbox_ip'); |
|
| 156 | - } |
|
| 150 | + // set name of SSL-certificate (must be same as remote-hostname (dynDNS) of FRITZ!Box) and remove port in address if alternate port is given |
|
| 151 | + if (strpos($this->config->getItem('fritzbox_ip'),":")){ |
|
| 152 | + $ssl_cert_fritzbox = explode(":", $this->config->getItem('fritzbox_ip')); |
|
| 153 | + $ssl_cert_fritzbox = $ssl_cert_fritzbox[0]; |
|
| 154 | + } else { |
|
| 155 | + $ssl_cert_fritzbox = $this->config->getItem('fritzbox_ip'); |
|
| 156 | + } |
|
| 157 | 157 | |
| 158 | - // set SSL-options and path to certificate |
|
| 159 | - curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2); |
|
| 160 | - curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
|
| 161 | - curl_setopt($ch, CURLOPT_CAPATH, '/etc/ssl/certs'); |
|
| 158 | + // set SSL-options and path to certificate |
|
| 159 | + curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2); |
|
| 160 | + curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
|
| 161 | + curl_setopt($ch, CURLOPT_CAPATH, '/etc/ssl/certs'); |
|
| 162 | 162 | |
| 163 | - // support for pre FRITZ!OS 5.50 remote config |
|
| 164 | - if (!$this->config->getItem('use_lua_login_method')){ |
|
| 165 | - curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password')); |
|
| 166 | - } |
|
| 163 | + // support for pre FRITZ!OS 5.50 remote config |
|
| 164 | + if (!$this->config->getItem('use_lua_login_method')){ |
|
| 165 | + curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password')); |
|
| 166 | + } |
|
| 167 | 167 | } |
| 168 | 168 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formfields)); |
| 169 | 169 | $output = curl_exec($ch); |
@@ -178,10 +178,10 @@ discard block |
||
| 178 | 178 | // add the sid, if it is already set |
| 179 | 179 | if ($this->sid != '0000000000000000') |
| 180 | 180 | { |
| 181 | - // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!) |
|
| 182 | - // therfore we use array_merge to ensuere the foreach outputs 'sid' fist |
|
| 183 | - $formfields = array_merge(array('sid' => $this->sid), $formfields); |
|
| 184 | - //$formfields['sid'] = $this->sid; |
|
| 181 | + // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!) |
|
| 182 | + // therfore we use array_merge to ensuere the foreach outputs 'sid' fist |
|
| 183 | + $formfields = array_merge(array('sid' => $this->sid), $formfields); |
|
| 184 | + //$formfields['sid'] = $this->sid; |
|
| 185 | 185 | } |
| 186 | 186 | curl_setopt($ch, CURLOPT_URL, $this->config->getItem('fritzbox_url') . '/cgi-bin/firmwarecfg'); |
| 187 | 187 | curl_setopt($ch, CURLOPT_POST, 1); |
@@ -192,30 +192,30 @@ discard block |
||
| 192 | 192 | |
| 193 | 193 | // if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine) |
| 194 | 194 | if(empty( $filefileds )) { |
| 195 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query |
|
| 196 | - } |
|
| 197 | - // post calculated raw data |
|
| 198 | - else { |
|
| 199 | - $header = $this->_create_custom_file_post_header($formfields, $filefileds); |
|
| 200 | - curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 201 | - 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) ) |
|
| 202 | - ); |
|
| 195 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query |
|
| 196 | + } |
|
| 197 | + // post calculated raw data |
|
| 198 | + else { |
|
| 199 | + $header = $this->_create_custom_file_post_header($formfields, $filefileds); |
|
| 200 | + curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 201 | + 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) ) |
|
| 202 | + ); |
|
| 203 | 203 | |
| 204 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $header['data']); |
|
| 205 | - } |
|
| 204 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $header['data']); |
|
| 205 | + } |
|
| 206 | 206 | |
| 207 | 207 | $output = curl_exec($ch); |
| 208 | 208 | |
| 209 | - // curl error |
|
| 210 | - if(curl_errno($ch)) { |
|
| 211 | - $this->error(curl_error($ch)." (".curl_errno($ch).")"); |
|
| 212 | - } |
|
| 209 | + // curl error |
|
| 210 | + if(curl_errno($ch)) { |
|
| 211 | + $this->error(curl_error($ch)." (".curl_errno($ch).")"); |
|
| 212 | + } |
|
| 213 | 213 | |
| 214 | 214 | // finger out an error message, if given |
| 215 | 215 | preg_match('@<p class="ErrorMsg">(.*?)</p>@is', $output, $matches); |
| 216 | 216 | if (isset($matches[1])) |
| 217 | 217 | { |
| 218 | - $this->error(str_replace(' ', ' ', $matches[1])); |
|
| 218 | + $this->error(str_replace(' ', ' ', $matches[1])); |
|
| 219 | 219 | } |
| 220 | 220 | |
| 221 | 221 | curl_close($ch); |
@@ -223,10 +223,10 @@ discard block |
||
| 223 | 223 | } |
| 224 | 224 | |
| 225 | 225 | private function _create_custom_file_post_header($postFields, $fileFields) { |
| 226 | - // form field separator |
|
| 227 | - $delimiter = '-------------' . uniqid(); |
|
| 226 | + // form field separator |
|
| 227 | + $delimiter = '-------------' . uniqid(); |
|
| 228 | 228 | |
| 229 | - /* |
|
| 229 | + /* |
|
| 230 | 230 | // file upload fields: name => array(type=>'mime/type',content=>'raw data') |
| 231 | 231 | $fileFields = array( |
| 232 | 232 | 'file1' => array( |
@@ -241,44 +241,44 @@ discard block |
||
| 241 | 241 | ); |
| 242 | 242 | */ |
| 243 | 243 | |
| 244 | - $data = ''; |
|
| 244 | + $data = ''; |
|
| 245 | 245 | |
| 246 | - // populate normal fields first (simpler) |
|
| 247 | - foreach ($postFields as $name => $content) { |
|
| 248 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 249 | - $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"'; |
|
| 250 | - $data .= "\r\n\r\n"; |
|
| 251 | - $data .= $content; |
|
| 252 | - $data .= "\r\n"; |
|
| 253 | - } |
|
| 254 | - // populate file fields |
|
| 255 | - foreach ($fileFields as $name => $file) { |
|
| 256 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 257 | - // "filename" attribute is not essential; server-side scripts may use it |
|
| 258 | - $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' . |
|
| 259 | - ' filename="' . $file['filename'] . '"' . "\r\n"; |
|
| 246 | + // populate normal fields first (simpler) |
|
| 247 | + foreach ($postFields as $name => $content) { |
|
| 248 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 249 | + $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"'; |
|
| 250 | + $data .= "\r\n\r\n"; |
|
| 251 | + $data .= $content; |
|
| 252 | + $data .= "\r\n"; |
|
| 253 | + } |
|
| 254 | + // populate file fields |
|
| 255 | + foreach ($fileFields as $name => $file) { |
|
| 256 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 257 | + // "filename" attribute is not essential; server-side scripts may use it |
|
| 258 | + $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' . |
|
| 259 | + ' filename="' . $file['filename'] . '"' . "\r\n"; |
|
| 260 | 260 | |
| 261 | - //$data .= 'Content-Transfer-Encoding: binary'."\r\n"; |
|
| 262 | - // this is, again, informative only; good practice to include though |
|
| 263 | - $data .= 'Content-Type: ' . $file['type'] . "\r\n"; |
|
| 264 | - // this endline must be here to indicate end of headers |
|
| 265 | - $data .= "\r\n"; |
|
| 266 | - // the file itself (note: there's no encoding of any kind) |
|
| 267 | - $data .= $file['content'] . "\r\n"; |
|
| 268 | - } |
|
| 269 | - // last delimiter |
|
| 270 | - $data .= "--" . $delimiter . "--\r\n"; |
|
| 261 | + //$data .= 'Content-Transfer-Encoding: binary'."\r\n"; |
|
| 262 | + // this is, again, informative only; good practice to include though |
|
| 263 | + $data .= 'Content-Type: ' . $file['type'] . "\r\n"; |
|
| 264 | + // this endline must be here to indicate end of headers |
|
| 265 | + $data .= "\r\n"; |
|
| 266 | + // the file itself (note: there's no encoding of any kind) |
|
| 267 | + $data .= $file['content'] . "\r\n"; |
|
| 268 | + } |
|
| 269 | + // last delimiter |
|
| 270 | + $data .= "--" . $delimiter . "--\r\n"; |
|
| 271 | 271 | |
| 272 | - return array('delimiter' => $delimiter, 'data' => $data); |
|
| 273 | - } |
|
| 272 | + return array('delimiter' => $delimiter, 'data' => $data); |
|
| 273 | + } |
|
| 274 | 274 | |
| 275 | 275 | /** |
| 276 | - * do a GET request on the box |
|
| 277 | - * the main cURL wrapper handles the command |
|
| 278 | - * |
|
| 279 | - * @param array $params an associative array with the GET params to pass |
|
| 280 | - * @return string the raw HTML code returned by the Fritz!Box |
|
| 281 | - */ |
|
| 276 | + * do a GET request on the box |
|
| 277 | + * the main cURL wrapper handles the command |
|
| 278 | + * |
|
| 279 | + * @param array $params an associative array with the GET params to pass |
|
| 280 | + * @return string the raw HTML code returned by the Fritz!Box |
|
| 281 | + */ |
|
| 282 | 282 | public function doGetRequest($params = array()) |
| 283 | 283 | { |
| 284 | 284 | // add the sid, if it is already set |
@@ -318,12 +318,12 @@ discard block |
||
| 318 | 318 | |
| 319 | 319 | |
| 320 | 320 | /** |
| 321 | - * the login method, handles the secured login-process |
|
| 322 | - * newer firmwares (xx.04.74 and newer) need a challenge-response mechanism to prevent Cross-Site Request Forgery attacks |
|
| 323 | - * see http://www.avm.de/de/Extern/Technical_Note_Session_ID.pdf for details |
|
| 324 | - * |
|
| 325 | - * @return string a valid SID, if the login was successful, otherwise throws an Exception with an error message |
|
| 326 | - */ |
|
| 321 | + * the login method, handles the secured login-process |
|
| 322 | + * newer firmwares (xx.04.74 and newer) need a challenge-response mechanism to prevent Cross-Site Request Forgery attacks |
|
| 323 | + * see http://www.avm.de/de/Extern/Technical_Note_Session_ID.pdf for details |
|
| 324 | + * |
|
| 325 | + * @return string a valid SID, if the login was successful, otherwise throws an Exception with an error message |
|
| 326 | + */ |
|
| 327 | 327 | protected function initSID() |
| 328 | 328 | { |
| 329 | 329 | // determine, wich login type we have to use |
@@ -403,9 +403,9 @@ discard block |
||
| 403 | 403 | |
| 404 | 404 | |
| 405 | 405 | /** |
| 406 | - * the logout method just sends a logout command to the Fritz!Box |
|
| 407 | - * |
|
| 408 | - */ |
|
| 406 | + * the logout method just sends a logout command to the Fritz!Box |
|
| 407 | + * |
|
| 408 | + */ |
|
| 409 | 409 | protected function logout() |
| 410 | 410 | { |
| 411 | 411 | if ( $this->config->getItem('use_lua_login_method') == true ) |
@@ -424,10 +424,10 @@ discard block |
||
| 424 | 424 | |
| 425 | 425 | |
| 426 | 426 | /** |
| 427 | - * the error method just throws an Exception |
|
| 428 | - * |
|
| 429 | - * @param string $message an error message for the Exception |
|
| 430 | - */ |
|
| 427 | + * the error method just throws an Exception |
|
| 428 | + * |
|
| 429 | + * @param string $message an error message for the Exception |
|
| 430 | + */ |
|
| 431 | 431 | public function error($message = null) |
| 432 | 432 | { |
| 433 | 433 | throw new Exception($message); |
@@ -435,20 +435,20 @@ discard block |
||
| 435 | 435 | |
| 436 | 436 | |
| 437 | 437 | /** |
| 438 | - * a getter for the session ID |
|
| 439 | - * |
|
| 440 | - * @return string $this->sid |
|
| 441 | - */ |
|
| 438 | + * a getter for the session ID |
|
| 439 | + * |
|
| 440 | + * @return string $this->sid |
|
| 441 | + */ |
|
| 442 | 442 | public function getSID() |
| 443 | 443 | { |
| 444 | 444 | return $this->sid; |
| 445 | 445 | } |
| 446 | 446 | |
| 447 | 447 | /** |
| 448 | - * log a message |
|
| 449 | - * |
|
| 450 | - * @param $message string the message to log |
|
| 451 | - */ |
|
| 448 | + * log a message |
|
| 449 | + * |
|
| 450 | + * @param $message string the message to log |
|
| 451 | + */ |
|
| 452 | 452 | public function logMessage($message) |
| 453 | 453 | { |
| 454 | 454 | if ( $this->config->getItem('newline') == false ) |