@@ -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 ) |
@@ -63,39 +63,39 @@ discard block |
||
| 63 | 63 | * |
| 64 | 64 | * @access public |
| 65 | 65 | */ |
| 66 | - public function __construct($password = false,$user_name = false,$fritzbox_ip = 'fritz.box',$force_local_login = false) |
|
| 66 | + public function __construct($password = false, $user_name = false, $fritzbox_ip = 'fritz.box', $force_local_login = false) |
|
| 67 | 67 | { |
| 68 | 68 | // init the config object |
| 69 | 69 | $this->config = new fritzbox_api_config(); |
| 70 | 70 | |
| 71 | 71 | // try autoloading the config |
| 72 | - if (file_exists(__DIR__ . '/fritzbox_user.conf.php') && is_readable(__DIR__ . '/fritzbox_user.conf.php') ) { |
|
| 72 | + if(file_exists(__DIR__ . '/fritzbox_user.conf.php') && is_readable(__DIR__ . '/fritzbox_user.conf.php')) { |
|
| 73 | 73 | require_once(__DIR__ . '/fritzbox_user.conf.php'); |
| 74 | 74 | } |
| 75 | 75 | |
| 76 | 76 | // set FRITZ!Box-IP and URL |
| 77 | - $this->config->setItem('fritzbox_ip',$fritzbox_ip); |
|
| 77 | + $this->config->setItem('fritzbox_ip', $fritzbox_ip); |
|
| 78 | 78 | |
| 79 | 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')); |
|
| 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 | 85 | } else { |
| 86 | - $this->config->setItem('enable_remote_config',false); |
|
| 87 | - if($user_name != false){ |
|
| 88 | - $this->config->setItem('username',$user_name); |
|
| 86 | + $this->config->setItem('enable_remote_config', false); |
|
| 87 | + if($user_name != false) { |
|
| 88 | + $this->config->setItem('username', $user_name); |
|
| 89 | 89 | } |
| 90 | - if($password != false){ |
|
| 91 | - $this->config->setItem('password',$password); |
|
| 90 | + if($password != false) { |
|
| 91 | + $this->config->setItem('password', $password); |
|
| 92 | 92 | } |
| 93 | 93 | $this->config->setItem('fritzbox_url', 'http://' . $this->config->getItem('fritzbox_ip')); |
| 94 | 94 | } |
| 95 | 95 | |
| 96 | 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') ){ |
|
| 97 | + if($this->config->getItem('enable_remote_config') === true) { |
|
| 98 | + if(!$this->config->getItem('remote_config_user') || !$this->config->getItem('remote_config_password')) { |
|
| 99 | 99 | $this->error('ERROR: Remote config mode enabled, but no username or no password provided'); |
| 100 | 100 | } |
| 101 | 101 | } |
@@ -129,7 +129,7 @@ discard block |
||
| 129 | 129 | { |
| 130 | 130 | $ch = curl_init(); |
| 131 | 131 | |
| 132 | - if ( isset($formfields['getpage']) && strpos($formfields['getpage'], '.lua') > 0 ) |
|
| 132 | + if(isset($formfields['getpage']) && strpos($formfields['getpage'], '.lua') > 0) |
|
| 133 | 133 | { |
| 134 | 134 | curl_setopt($ch, CURLOPT_URL, $this->config->getItem('fritzbox_url') . $formfields['getpage'] . '?sid=' . $this->sid); |
| 135 | 135 | unset($formfields['getpage']); |
@@ -137,7 +137,7 @@ discard block |
||
| 137 | 137 | else |
| 138 | 138 | { |
| 139 | 139 | // add the sid, if it is already set |
| 140 | - if ($this->sid != '0000000000000000') |
|
| 140 | + if($this->sid != '0000000000000000') |
|
| 141 | 141 | { |
| 142 | 142 | $formfields['sid'] = $this->sid; |
| 143 | 143 | } |
@@ -145,10 +145,10 @@ discard block |
||
| 145 | 145 | } |
| 146 | 146 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 147 | 147 | curl_setopt($ch, CURLOPT_POST, 1); |
| 148 | - if ( $this->config->getItem('enable_remote_config') == true ) |
|
| 148 | + if($this->config->getItem('enable_remote_config') == true) |
|
| 149 | 149 | { |
| 150 | 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'),":")){ |
|
| 151 | + if(strpos($this->config->getItem('fritzbox_ip'), ":")) { |
|
| 152 | 152 | $ssl_cert_fritzbox = explode(":", $this->config->getItem('fritzbox_ip')); |
| 153 | 153 | $ssl_cert_fritzbox = $ssl_cert_fritzbox[0]; |
| 154 | 154 | } else { |
@@ -161,7 +161,7 @@ discard block |
||
| 161 | 161 | curl_setopt($ch, CURLOPT_CAPATH, '/etc/ssl/certs'); |
| 162 | 162 | |
| 163 | 163 | // support for pre FRITZ!OS 5.50 remote config |
| 164 | - if (!$this->config->getItem('use_lua_login_method')){ |
|
| 164 | + if(!$this->config->getItem('use_lua_login_method')) { |
|
| 165 | 165 | curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password')); |
| 166 | 166 | } |
| 167 | 167 | } |
@@ -176,7 +176,7 @@ discard block |
||
| 176 | 176 | $ch = curl_init(); |
| 177 | 177 | |
| 178 | 178 | // add the sid, if it is already set |
| 179 | - if ($this->sid != '0000000000000000') |
|
| 179 | + if($this->sid != '0000000000000000') |
|
| 180 | 180 | { |
| 181 | 181 | // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!) |
| 182 | 182 | // therfore we use array_merge to ensuere the foreach outputs 'sid' fist |
@@ -191,13 +191,13 @@ discard block |
||
| 191 | 191 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 192 | 192 | |
| 193 | 193 | // if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine) |
| 194 | - if(empty( $filefileds )) { |
|
| 194 | + if(empty($filefileds)) { |
|
| 195 | 195 | curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query |
| 196 | 196 | } |
| 197 | 197 | // post calculated raw data |
| 198 | 198 | else { |
| 199 | 199 | $header = $this->_create_custom_file_post_header($formfields, $filefileds); |
| 200 | - curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 200 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
|
| 201 | 201 | 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) ) |
| 202 | 202 | ); |
| 203 | 203 | |
@@ -208,12 +208,12 @@ discard block |
||
| 208 | 208 | |
| 209 | 209 | // curl error |
| 210 | 210 | if(curl_errno($ch)) { |
| 211 | - $this->error(curl_error($ch)." (".curl_errno($ch).")"); |
|
| 211 | + $this->error(curl_error($ch) . " (" . curl_errno($ch) . ")"); |
|
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | // finger out an error message, if given |
| 215 | 215 | preg_match('@<p class="ErrorMsg">(.*?)</p>@is', $output, $matches); |
| 216 | - if (isset($matches[1])) |
|
| 216 | + if(isset($matches[1])) |
|
| 217 | 217 | { |
| 218 | 218 | $this->error(str_replace(' ', ' ', $matches[1])); |
| 219 | 219 | } |
@@ -244,7 +244,7 @@ discard block |
||
| 244 | 244 | $data = ''; |
| 245 | 245 | |
| 246 | 246 | // populate normal fields first (simpler) |
| 247 | - foreach ($postFields as $name => $content) { |
|
| 247 | + foreach($postFields as $name => $content) { |
|
| 248 | 248 | $data .= "--" . $delimiter . "\r\n"; |
| 249 | 249 | $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"'; |
| 250 | 250 | $data .= "\r\n\r\n"; |
@@ -252,7 +252,7 @@ discard block |
||
| 252 | 252 | $data .= "\r\n"; |
| 253 | 253 | } |
| 254 | 254 | // populate file fields |
| 255 | - foreach ($fileFields as $name => $file) { |
|
| 255 | + foreach($fileFields as $name => $file) { |
|
| 256 | 256 | $data .= "--" . $delimiter . "\r\n"; |
| 257 | 257 | // "filename" attribute is not essential; server-side scripts may use it |
| 258 | 258 | $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' . |
@@ -282,13 +282,13 @@ discard block |
||
| 282 | 282 | public function doGetRequest($params = array()) |
| 283 | 283 | { |
| 284 | 284 | // add the sid, if it is already set |
| 285 | - if ($this->sid != '0000000000000000') |
|
| 285 | + if($this->sid != '0000000000000000') |
|
| 286 | 286 | { |
| 287 | 287 | $params['sid'] = $this->sid; |
| 288 | 288 | } |
| 289 | 289 | |
| 290 | 290 | $ch = curl_init(); |
| 291 | - if ( strpos($params['getpage'], '.lua') > 0 ) |
|
| 291 | + if(strpos($params['getpage'], '.lua') > 0) |
|
| 292 | 292 | { |
| 293 | 293 | $getpage = $params['getpage'] . '?'; |
| 294 | 294 | unset($params['getpage']); |
@@ -300,13 +300,13 @@ discard block |
||
| 300 | 300 | curl_setopt($ch, CURLOPT_URL, $this->config->getItem('fritzbox_url') . $getpage . http_build_query($params)); |
| 301 | 301 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 302 | 302 | curl_setopt($ch, CURLOPT_HTTPGET, 1); |
| 303 | - if ( $this->config->getItem('enable_remote_config') ) |
|
| 303 | + if($this->config->getItem('enable_remote_config')) |
|
| 304 | 304 | { |
| 305 | 305 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 306 | 306 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 307 | 307 | |
| 308 | 308 | // support for pre FRITZ!OS 5.50 remote config |
| 309 | - if ( !$this->config->getItem('use_lua_login_method') ) |
|
| 309 | + if(!$this->config->getItem('use_lua_login_method')) |
|
| 310 | 310 | { |
| 311 | 311 | curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password')); |
| 312 | 312 | } |
@@ -327,7 +327,7 @@ discard block |
||
| 327 | 327 | protected function initSID() |
| 328 | 328 | { |
| 329 | 329 | // determine, wich login type we have to use |
| 330 | - if ( $this->config->getItem('use_lua_login_method') == true ) |
|
| 330 | + if($this->config->getItem('use_lua_login_method') == true) |
|
| 331 | 331 | { |
| 332 | 332 | $loginpage = '/login_sid.lua'; |
| 333 | 333 | } |
@@ -339,13 +339,13 @@ discard block |
||
| 339 | 339 | // read the current status |
| 340 | 340 | $session_status_simplexml = simplexml_load_string($this->doGetRequest(array('getpage' => $loginpage))); |
| 341 | 341 | |
| 342 | - if ( !is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement' ) |
|
| 342 | + if(!is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement') |
|
| 343 | 343 | { |
| 344 | 344 | $this->error('Response of initialization call ' . $loginpage . ' in ' . __FUNCTION__ . ' was not xml-formatted.'); |
| 345 | 345 | } |
| 346 | 346 | |
| 347 | 347 | // perhaps we already have a SID (i.e. when no password is set) |
| 348 | - if ( $session_status_simplexml->SID != '0000000000000000' ) |
|
| 348 | + if($session_status_simplexml->SID != '0000000000000000') |
|
| 349 | 349 | { |
| 350 | 350 | return $session_status_simplexml->SID; |
| 351 | 351 | } |
@@ -359,16 +359,16 @@ discard block |
||
| 359 | 359 | $formfields = array( |
| 360 | 360 | 'getpage' => $loginpage, |
| 361 | 361 | ); |
| 362 | - if ( $this->config->getItem('use_lua_login_method') ) |
|
| 362 | + if($this->config->getItem('use_lua_login_method')) |
|
| 363 | 363 | { |
| 364 | - if ( $this->config->getItem('enable_remote_config') ) |
|
| 364 | + if($this->config->getItem('enable_remote_config')) |
|
| 365 | 365 | { |
| 366 | 366 | $formfields['username'] = $this->config->getItem('remote_config_user'); |
| 367 | 367 | $response = $challenge . '-' . md5(mb_convert_encoding($challenge . '-' . $this->config->getItem('remote_config_password'), "UCS-2LE", "UTF-8")); |
| 368 | 368 | } |
| 369 | 369 | else |
| 370 | 370 | { |
| 371 | - if ( $this->config->getItem('username') ) |
|
| 371 | + if($this->config->getItem('username')) |
|
| 372 | 372 | { |
| 373 | 373 | $formfields['username'] = $this->config->getItem('username'); |
| 374 | 374 | } |
@@ -385,12 +385,12 @@ discard block |
||
| 385 | 385 | |
| 386 | 386 | // finger out the SID from the response |
| 387 | 387 | $session_status_simplexml = simplexml_load_string($output); |
| 388 | - if ( !is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement' ) |
|
| 388 | + if(!is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement') |
|
| 389 | 389 | { |
| 390 | 390 | $this->error('Response of login call to ' . $loginpage . ' in ' . __FUNCTION__ . ' was not xml-formatted.'); |
| 391 | 391 | } |
| 392 | 392 | |
| 393 | - if ( $session_status_simplexml->SID != '0000000000000000' ) |
|
| 393 | + if($session_status_simplexml->SID != '0000000000000000') |
|
| 394 | 394 | { |
| 395 | 395 | return (string)$session_status_simplexml->SID; |
| 396 | 396 | } |
@@ -408,7 +408,7 @@ discard block |
||
| 408 | 408 | */ |
| 409 | 409 | protected function logout() |
| 410 | 410 | { |
| 411 | - if ( $this->config->getItem('use_lua_login_method') == true ) |
|
| 411 | + if($this->config->getItem('use_lua_login_method') == true) |
|
| 412 | 412 | { |
| 413 | 413 | $this->doGetRequest(array('getpage' => '/home/home.lua', 'logout' => '1')); |
| 414 | 414 | } |
@@ -451,22 +451,22 @@ discard block |
||
| 451 | 451 | */ |
| 452 | 452 | public function logMessage($message) |
| 453 | 453 | { |
| 454 | - if ( $this->config->getItem('newline') == false ) |
|
| 454 | + if($this->config->getItem('newline') == false) |
|
| 455 | 455 | { |
| 456 | 456 | $this->config->setItem('newline', (PHP_OS == 'WINNT') ? "\r\n" : "\n"); |
| 457 | 457 | } |
| 458 | 458 | |
| 459 | - if ( $this->config->getItem('logging') == 'console' ) |
|
| 459 | + if($this->config->getItem('logging') == 'console') |
|
| 460 | 460 | { |
| 461 | 461 | echo $message; |
| 462 | 462 | } |
| 463 | - else if ( $this->config->getItem('logging') == 'silent' || $this->config->getItem('logging') == false ) |
|
| 463 | + else if($this->config->getItem('logging') == 'silent' || $this->config->getItem('logging') == false) |
|
| 464 | 464 | { |
| 465 | 465 | // do nothing |
| 466 | 466 | } |
| 467 | 467 | else |
| 468 | 468 | { |
| 469 | - if ( is_writable($this->config->getItem('logging')) || is_writable(dirname($this->config->getItem('logging'))) ) |
|
| 469 | + if(is_writable($this->config->getItem('logging')) || is_writable(dirname($this->config->getItem('logging')))) |
|
| 470 | 470 | { |
| 471 | 471 | file_put_contents($this->config->getItem('logging'), $message . $this->config->getItem('newline'), FILE_APPEND); |
| 472 | 472 | } |
@@ -516,11 +516,11 @@ discard block |
||
| 516 | 516 | */ |
| 517 | 517 | public function getItem($item = 'all') |
| 518 | 518 | { |
| 519 | - if ( $item == 'all' ) |
|
| 519 | + if($item == 'all') |
|
| 520 | 520 | { |
| 521 | 521 | return $this->config; |
| 522 | 522 | } |
| 523 | - elseif ( isset($this->config[$item]) ) |
|
| 523 | + elseif(isset($this->config[$item])) |
|
| 524 | 524 | { |
| 525 | 525 | return $this->config[$item]; |
| 526 | 526 | } |
@@ -38,9 +38,12 @@ discard block |
||
| 38 | 38 | require_once('lib/fritzbox_api_php/fritzbox_api.class.php'); |
| 39 | 39 | |
| 40 | 40 | if($argc == 2) |
| 41 | +{ |
|
| 41 | 42 | $config_file_name = $argv[1]; |
| 42 | -else |
|
| 43 | +} |
|
| 44 | +else { |
|
| 43 | 45 | $config_file_name = __DIR__ . '/config.php'; |
| 46 | +} |
|
| 44 | 47 | |
| 45 | 48 | // default/fallback config options |
| 46 | 49 | $config['tmp_dir'] = sys_get_temp_dir(); |
@@ -60,7 +63,9 @@ discard block |
||
| 60 | 63 | $config['quickdial_keyword'] = 'Quickdial:'; |
| 61 | 64 | |
| 62 | 65 | if(is_file($config_file_name)) |
| 66 | +{ |
|
| 63 | 67 | require($config_file_name); |
| 68 | +} |
|
| 64 | 69 | else |
| 65 | 70 | { |
| 66 | 71 | print 'ERROR: No ' . $config_file_name . ' found, please take a look at config.example.php and create a ' . $config_file_name . ' file!' . PHP_EOL; |
@@ -91,9 +96,12 @@ discard block |
||
| 91 | 96 | // upload the XML-file to the FRITZ!Box (CAUTION: this will overwrite all current entries in the phone book!!) |
| 92 | 97 | print 'Upload data to FRITZ!Box @ ' . $config['fritzbox_ip'] . PHP_EOL; |
| 93 | 98 | if($client->upload_to_fb()) |
| 99 | +{ |
|
| 94 | 100 | print 'Done.' . PHP_EOL; |
| 95 | -else |
|
| 101 | +} |
|
| 102 | +else { |
|
| 96 | 103 | exit(1); |
| 104 | +} |
|
| 97 | 105 | |
| 98 | 106 | flush(); // in case this script runs by php-cgi |
| 99 | 107 | |
@@ -124,7 +132,9 @@ discard block |
||
| 124 | 132 | public function mktemp($dir, $prefix = '', $mode = 0700) |
| 125 | 133 | { |
| 126 | 134 | if(substr($dir, -1) != '/') |
| 127 | - $dir .= '/'; |
|
| 135 | + { |
|
| 136 | + $dir .= '/'; |
|
| 137 | + } |
|
| 128 | 138 | |
| 129 | 139 | do |
| 130 | 140 | { |
@@ -145,7 +155,12 @@ discard block |
||
| 145 | 155 | if($object != "." && $object != "..") |
| 146 | 156 | { |
| 147 | 157 | if(filetype($dir . "/" . $object) == "dir") |
| 148 | - rrmdir($dir . "/" . $object); else unlink($dir . "/" . $object); |
|
| 158 | + { |
|
| 159 | + rrmdir($dir . "/" . $object); |
|
| 160 | + } |
|
| 161 | + else { |
|
| 162 | + unlink($dir . "/" . $object); |
|
| 163 | + } |
|
| 149 | 164 | } |
| 150 | 165 | } |
| 151 | 166 | reset($objects); |
@@ -159,18 +174,25 @@ discard block |
||
| 159 | 174 | { |
| 160 | 175 | // Check if there are valid base64 characters |
| 161 | 176 | if(!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $str)) |
| 162 | - return false; |
|
| 177 | + { |
|
| 178 | + return false; |
|
| 179 | + } |
|
| 163 | 180 | |
| 164 | 181 | // Decode the string in strict mode and check the results |
| 165 | 182 | $decoded = base64_decode($str, true); |
| 166 | 183 | if($decoded === false) |
| 167 | - return false; |
|
| 184 | + { |
|
| 185 | + return false; |
|
| 186 | + } |
|
| 168 | 187 | |
| 169 | 188 | // Encode the string again |
| 170 | 189 | if(base64_encode($decoded) === $str) |
| 171 | - return true; |
|
| 172 | - else |
|
| 173 | - return false; |
|
| 190 | + { |
|
| 191 | + return true; |
|
| 192 | + } |
|
| 193 | + else { |
|
| 194 | + return false; |
|
| 195 | + } |
|
| 174 | 196 | } |
| 175 | 197 | catch(Exception $e) |
| 176 | 198 | { |
@@ -211,7 +233,9 @@ discard block |
||
| 211 | 233 | // set the vcard extension in case the user |
| 212 | 234 | // defined it in the config |
| 213 | 235 | if(isset($conf['extension'])) |
| 214 | - $carddav->setVcardExtension($conf['extension']); |
|
| 236 | + { |
|
| 237 | + $carddav->setVcardExtension($conf['extension']); |
|
| 238 | + } |
|
| 215 | 239 | |
| 216 | 240 | // retrieve data from the CardDAV server now |
| 217 | 241 | $xmldata = $carddav->get(); |
@@ -220,7 +244,9 @@ discard block |
||
| 220 | 244 | // CardDAV server and if not reencode it since the FRITZ!Box |
| 221 | 245 | // requires UTF-8 encoded data |
| 222 | 246 | if(iconv('utf-8', 'utf-8//IGNORE', $xmldata) != $xmldata) |
| 223 | - $xmldata = utf8_encode($xmldata); |
|
| 247 | + { |
|
| 248 | + $xmldata = utf8_encode($xmldata); |
|
| 249 | + } |
|
| 224 | 250 | |
| 225 | 251 | // read raw_vcard data from xml response |
| 226 | 252 | $raw_vcards = array(); |
@@ -242,10 +268,14 @@ discard block |
||
| 242 | 268 | $vcard_obj = new vCard(false, $v); |
| 243 | 269 | $name_arr = null; |
| 244 | 270 | if(isset($vcard_obj->n[0])) |
| 245 | - $name_arr = $vcard_obj->n[0]; |
|
| 271 | + { |
|
| 272 | + $name_arr = $vcard_obj->n[0]; |
|
| 273 | + } |
|
| 246 | 274 | $org_arr = null; |
| 247 | 275 | if(isset($vcard_obj->org[0])) |
| 248 | - $org_arr = $vcard_obj->org[0]; |
|
| 276 | + { |
|
| 277 | + $org_arr = $vcard_obj->org[0]; |
|
| 278 | + } |
|
| 249 | 279 | $addnames = ''; |
| 250 | 280 | $prefix = ''; |
| 251 | 281 | $suffix = ''; |
@@ -254,19 +284,29 @@ discard block |
||
| 254 | 284 | |
| 255 | 285 | // Build name Parts if existing ans switch to true in config |
| 256 | 286 | if(isset($name_arr['prefixes']) and $this->config['prefix']) |
| 257 | - $prefix = trim($name_arr['prefixes']); |
|
| 287 | + { |
|
| 288 | + $prefix = trim($name_arr['prefixes']); |
|
| 289 | + } |
|
| 258 | 290 | |
| 259 | 291 | if(isset($name_arr['suffixes']) and $this->config['suffix']) |
| 260 | - $suffix = trim($name_arr['suffixes']); |
|
| 292 | + { |
|
| 293 | + $suffix = trim($name_arr['suffixes']); |
|
| 294 | + } |
|
| 261 | 295 | |
| 262 | 296 | if(isset($name_arr['additionalnames']) and $this->config['addnames']) |
| 263 | - $addnames = trim($name_arr['additionalnames']); |
|
| 297 | + { |
|
| 298 | + $addnames = trim($name_arr['additionalnames']); |
|
| 299 | + } |
|
| 264 | 300 | |
| 265 | 301 | if(isset($org_arr['name']) and $this->config['orgname']) |
| 266 | - $orgname = trim($org_arr['name']); |
|
| 302 | + { |
|
| 303 | + $orgname = trim($org_arr['name']); |
|
| 304 | + } |
|
| 267 | 305 | |
| 268 | 306 | if (isset($vcard_obj->fn[0])) |
| 269 | - $formattedname = $vcard_obj->fn[0]; |
|
| 307 | + { |
|
| 308 | + $formattedname = $vcard_obj->fn[0]; |
|
| 309 | + } |
|
| 270 | 310 | |
| 271 | 311 | $firstname = trim($name_arr['firstname']); |
| 272 | 312 | $lastname = trim($name_arr['lastname']); |
@@ -286,78 +326,106 @@ discard block |
||
| 286 | 326 | |
| 287 | 327 | // Prefix |
| 288 | 328 | if(!empty($prefix)) |
| 289 | - $name .= $prefix; |
|
| 329 | + { |
|
| 330 | + $name .= $prefix; |
|
| 331 | + } |
|
| 290 | 332 | |
| 291 | 333 | if($format == 0) |
| 292 | 334 | { |
| 293 | 335 | // Lastname |
| 294 | 336 | if(!empty($name) and !empty($lastname)) |
| 295 | - $name .= ' ' . $lastname; |
|
| 296 | - else |
|
| 297 | - $name .= $lastname; |
|
| 337 | + { |
|
| 338 | + $name .= ' ' . $lastname; |
|
| 339 | + } |
|
| 340 | + else { |
|
| 341 | + $name .= $lastname; |
|
| 342 | + } |
|
| 298 | 343 | } |
| 299 | 344 | else |
| 300 | 345 | { |
| 301 | 346 | // Firstname |
| 302 | 347 | if(!empty($name) and !empty($firstname)) |
| 303 | - $name .= ' ' . $firstname; |
|
| 304 | - else |
|
| 305 | - $name .= $firstname; |
|
| 348 | + { |
|
| 349 | + $name .= ' ' . $firstname; |
|
| 350 | + } |
|
| 351 | + else { |
|
| 352 | + $name .= $firstname; |
|
| 353 | + } |
|
| 306 | 354 | } |
| 307 | 355 | |
| 308 | 356 | if($format == 2) |
| 309 | 357 | { |
| 310 | 358 | // AdditionalNames |
| 311 | 359 | if(!empty($name) and !empty($addnames)) |
| 312 | - $name .= ' ' . $addnames; |
|
| 313 | - else |
|
| 314 | - $name .= $addnames; |
|
| 360 | + { |
|
| 361 | + $name .= ' ' . $addnames; |
|
| 362 | + } |
|
| 363 | + else { |
|
| 364 | + $name .= $addnames; |
|
| 365 | + } |
|
| 315 | 366 | } |
| 316 | 367 | |
| 317 | 368 | if($format == 0) |
| 318 | 369 | { |
| 319 | 370 | // Firstname |
| 320 | 371 | if(!empty($name) and !empty($firstname)) |
| 321 | - $name .= ', ' . $firstname; |
|
| 322 | - else |
|
| 323 | - $name .= $firstname; |
|
| 372 | + { |
|
| 373 | + $name .= ', ' . $firstname; |
|
| 374 | + } |
|
| 375 | + else { |
|
| 376 | + $name .= $firstname; |
|
| 377 | + } |
|
| 324 | 378 | } |
| 325 | 379 | else |
| 326 | 380 | { |
| 327 | 381 | // Lastname |
| 328 | 382 | if(!empty($name) and !empty($lastname)) |
| 329 | - $name .= ' ' . $lastname; |
|
| 330 | - else |
|
| 331 | - $name .= $lastname; |
|
| 383 | + { |
|
| 384 | + $name .= ' ' . $lastname; |
|
| 385 | + } |
|
| 386 | + else { |
|
| 387 | + $name .= $lastname; |
|
| 388 | + } |
|
| 332 | 389 | } |
| 333 | 390 | |
| 334 | 391 | if($format != 2) |
| 335 | 392 | { |
| 336 | 393 | // AdditionalNames |
| 337 | 394 | if(!empty($name) and !empty($addnames)) |
| 338 | - $name .= ' ' . $addnames; |
|
| 339 | - else |
|
| 340 | - $name .= $addnames; |
|
| 395 | + { |
|
| 396 | + $name .= ' ' . $addnames; |
|
| 397 | + } |
|
| 398 | + else { |
|
| 399 | + $name .= $addnames; |
|
| 400 | + } |
|
| 341 | 401 | } |
| 342 | 402 | |
| 343 | 403 | // Suffix |
| 344 | 404 | if(!empty($name) and !empty($suffix)) |
| 345 | - $name .= ' ' . $suffix; |
|
| 346 | - else |
|
| 347 | - $name .= $suffix; |
|
| 405 | + { |
|
| 406 | + $name .= ' ' . $suffix; |
|
| 407 | + } |
|
| 408 | + else { |
|
| 409 | + $name .= $suffix; |
|
| 410 | + } |
|
| 348 | 411 | |
| 349 | 412 | // OrgName |
| 350 | 413 | if(!empty($name) and !empty($orgname)) |
| 351 | - $name .= ' (' . $orgname . ')'; |
|
| 352 | - else |
|
| 353 | - $name .= $orgname; |
|
| 414 | + { |
|
| 415 | + $name .= ' (' . $orgname . ')'; |
|
| 416 | + } |
|
| 417 | + else { |
|
| 418 | + $name .= $orgname; |
|
| 419 | + } |
|
| 354 | 420 | |
| 355 | 421 | // make sure to trim whitespaces and double spaces |
| 356 | 422 | $name = trim(str_replace(' ', ' ', $name)); |
| 357 | 423 | |
| 358 | 424 | // perform a fallback to formatted name, if we don't have any name and formatted name is available |
| 359 | 425 | if(empty($name) and !empty($formattedname)) |
| 360 | - $name = $formattedname; |
|
| 426 | + { |
|
| 427 | + $name = $formattedname; |
|
| 428 | + } |
|
| 361 | 429 | |
| 362 | 430 | if(empty($name)) |
| 363 | 431 | { |
@@ -371,15 +439,19 @@ discard block |
||
| 371 | 439 | $photo = str_replace(array(',', '&', ' ', '/', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', 'á', 'à', 'ó', 'ò', 'ú', 'ù', 'í', 'ø'), |
| 372 | 440 | array('', '_', '_', '_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'a', 'o', 'o', 'u', 'u', 'i', 'oe'), $name); |
| 373 | 441 | } |
| 374 | - else |
|
| 375 | - $photo = ''; |
|
| 442 | + else { |
|
| 443 | + $photo = ''; |
|
| 444 | + } |
|
| 376 | 445 | |
| 377 | 446 | // phone |
| 378 | 447 | $phone_no = array(); |
| 379 | 448 | if($vcard_obj->categories) |
| 380 | - $categories = $vcard_obj->categories[0]; |
|
| 381 | - else |
|
| 382 | - $categories = array(); |
|
| 449 | + { |
|
| 450 | + $categories = $vcard_obj->categories[0]; |
|
| 451 | + } |
|
| 452 | + else { |
|
| 453 | + $categories = array(); |
|
| 454 | + } |
|
| 383 | 455 | |
| 384 | 456 | // check for quickdial entry |
| 385 | 457 | if(isset($vcard_obj->note[0])) |
@@ -414,9 +486,10 @@ discard block |
||
| 414 | 486 | break; |
| 415 | 487 | } |
| 416 | 488 | } |
| 417 | - } |
|
| 418 | - else |
|
| 419 | - $add_entry = 1; |
|
| 489 | + } |
|
| 490 | + else { |
|
| 491 | + $add_entry = 1; |
|
| 492 | + } |
|
| 420 | 493 | |
| 421 | 494 | if($add_entry == 1) |
| 422 | 495 | { |
@@ -441,9 +514,13 @@ discard block |
||
| 441 | 514 | { |
| 442 | 515 | //Set quickdial |
| 443 | 516 | if($value == 1) |
| 444 | - print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n"; |
|
| 517 | + { |
|
| 518 | + print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n"; |
|
| 519 | + } |
|
| 445 | 520 | elseif($value >= 100) |
| 446 | - print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n"; |
|
| 521 | + { |
|
| 522 | + print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n"; |
|
| 523 | + } |
|
| 447 | 524 | |
| 448 | 525 | $quickdial = $value; |
| 449 | 526 | } |
@@ -453,23 +530,38 @@ discard block |
||
| 453 | 530 | |
| 454 | 531 | // find out priority |
| 455 | 532 | if(in_array("pref", $typearr_lower)) |
| 456 | - $prio = 1; |
|
| 533 | + { |
|
| 534 | + $prio = 1; |
|
| 535 | + } |
|
| 457 | 536 | |
| 458 | 537 | // set the proper type |
| 459 | 538 | if(in_array("cell", $typearr_lower)) |
| 460 | - $type = "mobile"; |
|
| 539 | + { |
|
| 540 | + $type = "mobile"; |
|
| 541 | + } |
|
| 461 | 542 | elseif(in_array("home", $typearr_lower)) |
| 462 | - $type = "home"; |
|
| 543 | + { |
|
| 544 | + $type = "home"; |
|
| 545 | + } |
|
| 463 | 546 | elseif(in_array("fax", $typearr_lower)) |
| 464 | - $type = "fax_work"; |
|
| 547 | + { |
|
| 548 | + $type = "fax_work"; |
|
| 549 | + } |
|
| 465 | 550 | elseif(in_array("work", $typearr_lower)) |
| 466 | - $type = "work"; |
|
| 551 | + { |
|
| 552 | + $type = "work"; |
|
| 553 | + } |
|
| 467 | 554 | elseif(in_array("other", $typearr_lower)) |
| 468 | - $type = "other"; |
|
| 555 | + { |
|
| 556 | + $type = "other"; |
|
| 557 | + } |
|
| 469 | 558 | elseif(in_array("dom", $typearr_lower)) |
| 470 | - $type = "other"; |
|
| 471 | - else |
|
| 472 | - continue; |
|
| 559 | + { |
|
| 560 | + $type = "other"; |
|
| 561 | + } |
|
| 562 | + else { |
|
| 563 | + continue; |
|
| 564 | + } |
|
| 473 | 565 | } |
| 474 | 566 | $phone_no[] = array("type"=>$type, "prio"=>$prio, "quickdial"=>$quickdial, "value" => $this->_clear_phone_number($phone_number)); |
| 475 | 567 | } |
@@ -489,13 +581,20 @@ discard block |
||
| 489 | 581 | $email = $e['value']; |
| 490 | 582 | $typearr_lower = unserialize(strtolower(serialize($e['type']))); |
| 491 | 583 | if(in_array("work", $typearr_lower)) |
| 492 | - $type_email = "work"; |
|
| 584 | + { |
|
| 585 | + $type_email = "work"; |
|
| 586 | + } |
|
| 493 | 587 | elseif(in_array("home", $typearr_lower)) |
| 494 | - $type_email = "home"; |
|
| 588 | + { |
|
| 589 | + $type_email = "home"; |
|
| 590 | + } |
|
| 495 | 591 | elseif(in_array("other", $typearr_lower)) |
| 496 | - $type_email = "other"; |
|
| 497 | - else |
|
| 498 | - continue; |
|
| 592 | + { |
|
| 593 | + $type_email = "other"; |
|
| 594 | + } |
|
| 595 | + else { |
|
| 596 | + continue; |
|
| 597 | + } |
|
| 499 | 598 | } |
| 500 | 599 | |
| 501 | 600 | // DEBUG: print out the email address on the console |
@@ -523,7 +622,9 @@ discard block |
||
| 523 | 622 | public function build_fb_xml() |
| 524 | 623 | { |
| 525 | 624 | if(empty($this->entries)) |
| 526 | - throw new Exception('No entries available! Call get_carddav_entries or set $this->entries manually!'); |
|
| 625 | + { |
|
| 626 | + throw new Exception('No entries available! Call get_carddav_entries or set $this->entries manually!'); |
|
| 627 | + } |
|
| 527 | 628 | |
| 528 | 629 | // create FB XML in utf-8 format |
| 529 | 630 | $root = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><phonebooks><phonebook></phonebook></phonebooks>'); |
@@ -562,7 +663,9 @@ discard block |
||
| 562 | 663 | |
| 563 | 664 | // output a warning if no telephone number was found |
| 564 | 665 | if($id == 0) |
| 565 | - print " WARNING: no phone entry found. VCard will be ignored." . PHP_EOL; |
|
| 666 | + { |
|
| 667 | + print " WARNING: no phone entry found. VCard will be ignored." . PHP_EOL; |
|
| 668 | + } |
|
| 566 | 669 | |
| 567 | 670 | // email: put the email addresses into the fritzbox xml file |
| 568 | 671 | $email = $contact->addChild("services"); |
@@ -612,8 +715,9 @@ discard block |
||
| 612 | 715 | |
| 613 | 716 | print " Added photo: " . basename($photo_file) . PHP_EOL; |
| 614 | 717 | } |
| 615 | - else |
|
| 616 | - print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL; |
|
| 718 | + else { |
|
| 719 | + print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL; |
|
| 720 | + } |
|
| 617 | 721 | } |
| 618 | 722 | elseif(substr($entry['photo_data'][0], 0, 4) == 'http') |
| 619 | 723 | { |
@@ -622,8 +726,9 @@ discard block |
||
| 622 | 726 | |
| 623 | 727 | print " Added photo: " . $entry['photo_data'][0] . PHP_EOL; |
| 624 | 728 | } |
| 625 | - else |
|
| 626 | - print " WARNING: Only VCard embedded photo data or a reference URL is currently supported." . PHP_EOL; |
|
| 729 | + else { |
|
| 730 | + print " WARNING: Only VCard embedded photo data or a reference URL is currently supported." . PHP_EOL; |
|
| 731 | + } |
|
| 627 | 732 | } |
| 628 | 733 | |
| 629 | 734 | $contact->addChild("services"); |
@@ -632,7 +737,9 @@ discard block |
||
| 632 | 737 | } |
| 633 | 738 | |
| 634 | 739 | if($root->asXML() !== false) |
| 635 | - $this->fbxml = $root->asXML(); |
|
| 740 | + { |
|
| 741 | + $this->fbxml = $root->asXML(); |
|
| 742 | + } |
|
| 636 | 743 | else |
| 637 | 744 | { |
| 638 | 745 | print " ERROR: created XML data isn't well-formed." . PHP_EOL; |
@@ -649,19 +756,27 @@ discard block |
||
| 649 | 756 | public function _concat($text1, $text2) |
| 650 | 757 | { |
| 651 | 758 | if($text1 == '') |
| 652 | - return $text2; |
|
| 759 | + { |
|
| 760 | + return $text2; |
|
| 761 | + } |
|
| 653 | 762 | elseif($text2 == '') |
| 654 | - return $text1; |
|
| 655 | - else |
|
| 656 | - return $text1 . ", " . $text2; |
|
| 763 | + { |
|
| 764 | + return $text1; |
|
| 765 | + } |
|
| 766 | + else { |
|
| 767 | + return $text1 . ", " . $text2; |
|
| 768 | + } |
|
| 657 | 769 | } |
| 658 | 770 | |
| 659 | 771 | public function _parse_fb_result($text) |
| 660 | 772 | { |
| 661 | 773 | if(preg_match("/\<h2\>([^\<]+)\<\/h2\>/", $text, $matches) == 1 && !empty($matches)) |
| 662 | - return $matches[1]; |
|
| 663 | - else |
|
| 664 | - return "Error while uploading xml to fritzbox"; |
|
| 774 | + { |
|
| 775 | + return $matches[1]; |
|
| 776 | + } |
|
| 777 | + else { |
|
| 778 | + return "Error while uploading xml to fritzbox"; |
|
| 779 | + } |
|
| 665 | 780 | } |
| 666 | 781 | |
| 667 | 782 | public function upload_to_fb() |
@@ -683,15 +798,17 @@ discard block |
||
| 683 | 798 | $newphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/","",file_get_contents($this->config['output_file'],'r'),-1,$debugnewtsreplace)); |
| 684 | 799 | print " INFO: Compare old and new phonebook file versions." . PHP_EOL . " INFO: old version: " . $oldphonebhash . PHP_EOL . " INFO: new version: " . $newphonebhash . PHP_EOL; |
| 685 | 800 | if($oldphonebhash === $newphonebhash) |
| 686 | - { |
|
| 801 | + { |
|
| 687 | 802 | print " INFO: Same versions ==> No changes in phonebook or images" . PHP_EOL . " EXIT: No need to upload phonebook to the FRITZ!Box.". PHP_EOL; |
| 688 | 803 | return 0; |
| 689 | 804 | } |
| 690 | - else |
|
| 691 | - print " INFO: Different versions ==> Changes in phonebook." . PHP_EOL . " INFO: Changes dedected! Continue with upload." . PHP_EOL; |
|
| 805 | + else { |
|
| 806 | + print " INFO: Different versions ==> Changes in phonebook." . PHP_EOL . " INFO: Changes dedected! Continue with upload." . PHP_EOL; |
|
| 807 | + } |
|
| 692 | 808 | } |
| 693 | - else |
|
| 694 | - return 0; |
|
| 809 | + else { |
|
| 810 | + return 0; |
|
| 811 | + } |
|
| 695 | 812 | } |
| 696 | 813 | // now we upload the photo jpgs first being stored in the |
| 697 | 814 | // temp directory. |
@@ -761,12 +878,16 @@ discard block |
||
| 761 | 878 | |
| 762 | 879 | ftp_pasv($conn_id, true); |
| 763 | 880 | if(!ftp_put($conn_id, $remote_path . "/" . $file, $fileinfo->getPathname(), FTP_BINARY)) |
| 764 | - print " ERROR: while uploading file " . $fileinfo->getFilename() . PHP_EOL; |
|
| 765 | - else |
|
| 766 | - print " ok." . PHP_EOL; |
|
| 881 | + { |
|
| 882 | + print " ERROR: while uploading file " . $fileinfo->getFilename() . PHP_EOL; |
|
| 883 | + } |
|
| 884 | + else { |
|
| 885 | + print " ok." . PHP_EOL; |
|
| 886 | + } |
|
| 887 | + } |
|
| 888 | + else { |
|
| 889 | + print " ok." . PHP_EOL; |
|
| 767 | 890 | } |
| 768 | - else |
|
| 769 | - print " ok." . PHP_EOL; |
|
| 770 | 891 | |
| 771 | 892 | // cleanup old files |
| 772 | 893 | foreach($all_existing_files as $existing_file) |
@@ -778,20 +899,23 @@ discard block |
||
| 778 | 899 | } |
| 779 | 900 | } |
| 780 | 901 | } |
| 781 | - else |
|
| 782 | - print " already exists." . PHP_EOL; |
|
| 902 | + else { |
|
| 903 | + print " already exists." . PHP_EOL; |
|
| 904 | + } |
|
| 783 | 905 | } |
| 784 | 906 | } |
| 785 | 907 | } |
| 786 | 908 | } |
| 787 | - else |
|
| 788 | - print " ERROR: couldn't login to FTP-server '" . $ftp_server . "' with provided username/password settings." . PHP_EOL; |
|
| 909 | + else { |
|
| 910 | + print " ERROR: couldn't login to FTP-server '" . $ftp_server . "' with provided username/password settings." . PHP_EOL; |
|
| 911 | + } |
|
| 789 | 912 | |
| 790 | 913 | // close ftp connection |
| 791 | 914 | ftp_close($conn_id); |
| 792 | 915 | } |
| 793 | - else |
|
| 794 | - print " ERROR: couldn't connect to FTP server '" . $ftp_server . "'." . PHP_EOL; |
|
| 916 | + else { |
|
| 917 | + print " ERROR: couldn't connect to FTP server '" . $ftp_server . "'." . PHP_EOL; |
|
| 918 | + } |
|
| 795 | 919 | |
| 796 | 920 | // lets post the phonebook xml to the FRITZ!Box |
| 797 | 921 | print " Uploading Phonebook XML to " . $this->config['fritzbox_ip'] . PHP_EOL; |