@@ -532,7 +532,7 @@ discard block |
||
| 532 | 532 | * |
| 533 | 533 | * @param string $vcard vCard |
| 534 | 534 | * @param string $vcard_id vCard id on the CardDAV server |
| 535 | - * @return boolean |
|
| 535 | + * @return string |
|
| 536 | 536 | */ |
| 537 | 537 | public function update($vcard, $vcard_id) |
| 538 | 538 | { |
@@ -672,7 +672,7 @@ discard block |
||
| 672 | 672 | * @param string $method HTTP method like (OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, COPY, MOVE) |
| 673 | 673 | * @param string $content Content for CardDAV queries |
| 674 | 674 | * @param string $content_type Set content type |
| 675 | - * @return array Raw CardDAV Response and http status code |
|
| 675 | + * @return boolean Raw CardDAV Response and http status code |
|
| 676 | 676 | */ |
| 677 | 677 | private function query($url, $method, $content = null, $content_type = null) |
| 678 | 678 | { |
@@ -143,17 +143,17 @@ discard block |
||
| 143 | 143 | private $auth = null; |
| 144 | 144 | |
| 145 | 145 | /** |
| 146 | - * Authentication: username |
|
| 147 | - * |
|
| 148 | - * @var string |
|
| 149 | - */ |
|
| 146 | + * Authentication: username |
|
| 147 | + * |
|
| 148 | + * @var string |
|
| 149 | + */ |
|
| 150 | 150 | private $username = null; |
| 151 | 151 | |
| 152 | 152 | /** |
| 153 | - * Authentication: password |
|
| 154 | - * |
|
| 155 | - * @var string |
|
| 156 | - */ |
|
| 153 | + * Authentication: password |
|
| 154 | + * |
|
| 155 | + * @var string |
|
| 156 | + */ |
|
| 157 | 157 | private $password = null; |
| 158 | 158 | |
| 159 | 159 | /** |
@@ -236,11 +236,11 @@ discard block |
||
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
| 239 | - * Sets the CardDAV server url |
|
| 240 | - * |
|
| 241 | - * @param string $url CardDAV server url |
|
| 242 | - * @return void |
|
| 243 | - */ |
|
| 239 | + * Sets the CardDAV server url |
|
| 240 | + * |
|
| 241 | + * @param string $url CardDAV server url |
|
| 242 | + * @return void |
|
| 243 | + */ |
|
| 244 | 244 | public function setUrl($url) |
| 245 | 245 | { |
| 246 | 246 | $this->url = $url; |
@@ -354,11 +354,11 @@ discard block |
||
| 354 | 354 | } |
| 355 | 355 | |
| 356 | 356 | /** |
| 357 | - * Gets a clean vCard from the CardDAV server |
|
| 358 | - * |
|
| 359 | - * @param string $vcard_id vCard id on the CardDAV server |
|
| 360 | - * @return string vCard (text/vcard) |
|
| 361 | - */ |
|
| 357 | + * Gets a clean vCard from the CardDAV server |
|
| 358 | + * |
|
| 359 | + * @param string $vcard_id vCard id on the CardDAV server |
|
| 360 | + * @return string vCard (text/vcard) |
|
| 361 | + */ |
|
| 362 | 362 | public function getVcard($vcard_id) |
| 363 | 363 | { |
| 364 | 364 | $vcard_id = str_replace($this->url_vcard_extension, null, $vcard_id); |
@@ -367,29 +367,29 @@ discard block |
||
| 367 | 367 | // DEBUG: print the response of the carddav-server |
| 368 | 368 | // print_r($result); |
| 369 | 369 | |
| 370 | - switch ($result['http_code']) |
|
| 370 | + switch ($result['http_code']) |
|
| 371 | 371 | { |
| 372 | - case 404: |
|
| 373 | - $altResult = $this->query($this->url . $vcard_id , 'GET'); |
|
| 374 | - // DEBUG: print the response of the carddav-server |
|
| 375 | - // print_r($altResult); |
|
| 376 | - switch ($altResult['http_code']) |
|
| 377 | - { |
|
| 378 | - case 200: |
|
| 379 | - case 207: |
|
| 380 | - print "Ignoring given Vcard Extension (".$this->url_vcard_extension.")" . PHP_EOL. "."; |
|
| 381 | - $this->setVcardExtension(""); |
|
| 382 | - return $altResult['response']; |
|
| 383 | - } |
|
| 384 | - throw new \Exception( |
|
| 385 | - "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}:{$result['response']}:{$vcard_id}.", |
|
| 386 | - self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD |
|
| 387 | - ); |
|
| 388 | - |
|
| 389 | - case 200: |
|
| 390 | - case 207: |
|
| 391 | - print "."; |
|
| 392 | - return $result['response']; |
|
| 372 | + case 404: |
|
| 373 | + $altResult = $this->query($this->url . $vcard_id , 'GET'); |
|
| 374 | + // DEBUG: print the response of the carddav-server |
|
| 375 | + // print_r($altResult); |
|
| 376 | + switch ($altResult['http_code']) |
|
| 377 | + { |
|
| 378 | + case 200: |
|
| 379 | + case 207: |
|
| 380 | + print "Ignoring given Vcard Extension (".$this->url_vcard_extension.")" . PHP_EOL. "."; |
|
| 381 | + $this->setVcardExtension(""); |
|
| 382 | + return $altResult['response']; |
|
| 383 | + } |
|
| 384 | + throw new \Exception( |
|
| 385 | + "Woops, something's gone wrong! The CardDAV server returned the http status code {$result['http_code']}:{$result['response']}:{$vcard_id}.", |
|
| 386 | + self::EXCEPTION_WRONG_HTTP_STATUS_CODE_GET_VCARD |
|
| 387 | + ); |
|
| 388 | + |
|
| 389 | + case 200: |
|
| 390 | + case 207: |
|
| 391 | + print "."; |
|
| 392 | + return $result['response']; |
|
| 393 | 393 | } |
| 394 | 394 | |
| 395 | 395 | throw new \Exception( |
@@ -450,10 +450,10 @@ discard block |
||
| 450 | 450 | } |
| 451 | 451 | |
| 452 | 452 | /** |
| 453 | - * Checks if the CardDAV server is reachable |
|
| 454 | - * |
|
| 455 | - * @return boolean |
|
| 456 | - */ |
|
| 453 | + * Checks if the CardDAV server is reachable |
|
| 454 | + * |
|
| 455 | + * @return boolean |
|
| 456 | + */ |
|
| 457 | 457 | public function checkConnection() |
| 458 | 458 | { |
| 459 | 459 | $result = $this->query($this->url, 'OPTIONS'); |
@@ -208,7 +208,7 @@ discard block |
||
| 208 | 208 | const EXCEPTION_WRONG_HTTP_STATUS_CODE_ADD = 1004; |
| 209 | 209 | const EXCEPTION_WRONG_HTTP_STATUS_CODE_UPDATE = 1005; |
| 210 | 210 | const EXCEPTION_MALFORMED_XML_RESPONSE = 1006; |
| 211 | - const EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID = 1007; |
|
| 211 | + const EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID = 1007; |
|
| 212 | 212 | |
| 213 | 213 | |
| 214 | 214 | /** |
@@ -219,7 +219,7 @@ discard block |
||
| 219 | 219 | */ |
| 220 | 220 | public function __construct($url = null) |
| 221 | 221 | { |
| 222 | - if ($url !== null) { |
|
| 222 | + if($url !== null) { |
|
| 223 | 223 | $this->setUrl($url); |
| 224 | 224 | } |
| 225 | 225 | } |
@@ -245,14 +245,14 @@ discard block |
||
| 245 | 245 | { |
| 246 | 246 | $this->url = $url; |
| 247 | 247 | |
| 248 | - if (substr($this->url, -1, 1) !== '/') { |
|
| 248 | + if(substr($this->url, -1, 1) !== '/') { |
|
| 249 | 249 | $this->url = $this->url . '/'; |
| 250 | 250 | } |
| 251 | 251 | |
| 252 | 252 | $this->url_parts = parse_url($this->url); |
| 253 | 253 | |
| 254 | 254 | // workaround for providers that don't use the default .vcf extension |
| 255 | - if (strpos($this->url, "google.com")) |
|
| 255 | + if(strpos($this->url, "google.com")) |
|
| 256 | 256 | { |
| 257 | 257 | $this->setVcardExtension(""); |
| 258 | 258 | } |
@@ -335,11 +335,11 @@ discard block |
||
| 335 | 335 | // DEBUG: print the response of the carddav-server |
| 336 | 336 | //print_r($result); |
| 337 | 337 | |
| 338 | - switch ($result['http_code']) |
|
| 338 | + switch($result['http_code']) |
|
| 339 | 339 | { |
| 340 | 340 | case 200: |
| 341 | 341 | case 207: |
| 342 | - if ($raw === true) { |
|
| 342 | + if($raw === true) { |
|
| 343 | 343 | return $result['response']; |
| 344 | 344 | } else { |
| 345 | 345 | return $this->simplify($result['response'], $include_vcards); |
@@ -367,17 +367,17 @@ discard block |
||
| 367 | 367 | // DEBUG: print the response of the carddav-server |
| 368 | 368 | // print_r($result); |
| 369 | 369 | |
| 370 | - switch ($result['http_code']) |
|
| 370 | + switch($result['http_code']) |
|
| 371 | 371 | { |
| 372 | 372 | case 404: |
| 373 | - $altResult = $this->query($this->url . $vcard_id , 'GET'); |
|
| 373 | + $altResult = $this->query($this->url . $vcard_id, 'GET'); |
|
| 374 | 374 | // DEBUG: print the response of the carddav-server |
| 375 | 375 | // print_r($altResult); |
| 376 | - switch ($altResult['http_code']) |
|
| 376 | + switch($altResult['http_code']) |
|
| 377 | 377 | { |
| 378 | 378 | case 200: |
| 379 | 379 | case 207: |
| 380 | - print "Ignoring given Vcard Extension (".$this->url_vcard_extension.")" . PHP_EOL. "."; |
|
| 380 | + print "Ignoring given Vcard Extension (" . $this->url_vcard_extension . ")" . PHP_EOL . "."; |
|
| 381 | 381 | $this->setVcardExtension(""); |
| 382 | 382 | return $altResult['response']; |
| 383 | 383 | } |
@@ -425,7 +425,7 @@ discard block |
||
| 425 | 425 | |
| 426 | 426 | $result = $this->query($this->url, 'REPORT', $xml->outputMemory(), 'text/xml'); |
| 427 | 427 | |
| 428 | - switch ($result['http_code']) |
|
| 428 | + switch($result['http_code']) |
|
| 429 | 429 | { |
| 430 | 430 | case 200: |
| 431 | 431 | case 207: |
@@ -458,7 +458,7 @@ discard block |
||
| 458 | 458 | { |
| 459 | 459 | $result = $this->query($this->url, 'OPTIONS'); |
| 460 | 460 | |
| 461 | - if ($result['http_code'] === 200) { |
|
| 461 | + if($result['http_code'] === 200) { |
|
| 462 | 462 | return true; |
| 463 | 463 | } else { |
| 464 | 464 | return false; |
@@ -488,7 +488,7 @@ discard block |
||
| 488 | 488 | { |
| 489 | 489 | $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'DELETE'); |
| 490 | 490 | |
| 491 | - switch ($result['http_code']) |
|
| 491 | + switch($result['http_code']) |
|
| 492 | 492 | { |
| 493 | 493 | case 204: |
| 494 | 494 | return true; |
@@ -509,8 +509,8 @@ discard block |
||
| 509 | 509 | */ |
| 510 | 510 | public function add($vcard, $vcard_id = null) |
| 511 | 511 | { |
| 512 | - if ($vcard_id === null) { |
|
| 513 | - $vcard_id = $this->generateVcardId(); |
|
| 512 | + if($vcard_id === null) { |
|
| 513 | + $vcard_id = $this->generateVcardId(); |
|
| 514 | 514 | } |
| 515 | 515 | $vcard = $this->cleanVcard($vcard); |
| 516 | 516 | $result = $this->query($this->url . $vcard_id . $this->url_vcard_extension, 'PUT', $vcard, 'text/vcard'); |
@@ -538,7 +538,7 @@ discard block |
||
| 538 | 538 | { |
| 539 | 539 | try { |
| 540 | 540 | return $this->add($vcard, $vcard_id); |
| 541 | - } catch (Exception $e) { |
|
| 541 | + } catch(Exception $e) { |
|
| 542 | 542 | throw new \Exception($e->getMessage(), self::EXCEPTION_WRONG_HTTP_STATUS_CODE_UPDATE); |
| 543 | 543 | } |
| 544 | 544 | } |
@@ -556,7 +556,7 @@ discard block |
||
| 556 | 556 | |
| 557 | 557 | try { |
| 558 | 558 | $xml = new \SimpleXMLElement($response); |
| 559 | - } catch (Exception $e) { |
|
| 559 | + } catch(Exception $e) { |
|
| 560 | 560 | throw new \Exception( |
| 561 | 561 | "The XML response seems to be malformed and can't be simplified!", |
| 562 | 562 | self::EXCEPTION_MALFORMED_XML_RESPONSE, |
@@ -571,33 +571,33 @@ discard block |
||
| 571 | 571 | $simplified_xml->startDocument('1.0', 'utf-8'); |
| 572 | 572 | $simplified_xml->startElement('response'); |
| 573 | 573 | |
| 574 | - if (!empty($xml->response)) { |
|
| 575 | - foreach ($xml->response as $response) { |
|
| 576 | - if ((preg_match('/vcard/', $response->propstat->prop->getcontenttype) || preg_match('/vcf/', $response->href)) && |
|
| 574 | + if(!empty($xml->response)) { |
|
| 575 | + foreach($xml->response as $response) { |
|
| 576 | + if((preg_match('/vcard/', $response->propstat->prop->getcontenttype) || preg_match('/vcf/', $response->href)) && |
|
| 577 | 577 | !$response->propstat->prop->resourcetype->collection) { |
| 578 | 578 | $id = basename($response->href); |
| 579 | 579 | $id = str_replace($this->url_vcard_extension, null, $id); |
| 580 | 580 | |
| 581 | 581 | try { |
| 582 | 582 | $vcardData = $this->getVcard($id); |
| 583 | - if (!empty($id)) { |
|
| 583 | + if(!empty($id)) { |
|
| 584 | 584 | $simplified_xml->startElement('element'); |
| 585 | 585 | $simplified_xml->writeElement('id', $id); |
| 586 | 586 | $simplified_xml->writeElement('etag', str_replace('"', null, $response->propstat->prop->getetag)); |
| 587 | 587 | $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified); |
| 588 | 588 | |
| 589 | - if ($include_vcards === true) { |
|
| 589 | + if($include_vcards === true) { |
|
| 590 | 590 | $simplified_xml->writeElement('vcard', $vcardData); |
| 591 | 591 | } |
| 592 | 592 | $simplified_xml->endElement(); |
| 593 | 593 | } |
| 594 | - } catch (\Exception $e) { |
|
| 594 | + } catch(\Exception $e) { |
|
| 595 | 595 | print("Error fetching vCard: {$id}: {$e->getMessage()}\n"); |
| 596 | 596 | } |
| 597 | - } elseif (preg_match('/unix-directory/', $response->propstat->prop->getcontenttype)) { |
|
| 598 | - if (isset($response->propstat->prop->href)) { |
|
| 597 | + } elseif(preg_match('/unix-directory/', $response->propstat->prop->getcontenttype)) { |
|
| 598 | + if(isset($response->propstat->prop->href)) { |
|
| 599 | 599 | $href = $response->propstat->prop->href; |
| 600 | - } elseif (isset($response->href)) { |
|
| 600 | + } elseif(isset($response->href)) { |
|
| 601 | 601 | $href = $response->href; |
| 602 | 602 | } else { |
| 603 | 603 | $href = null; |
@@ -643,22 +643,22 @@ discard block |
||
| 643 | 643 | */ |
| 644 | 644 | public function curlInit() |
| 645 | 645 | { |
| 646 | - if (empty($this->curl)) { |
|
| 646 | + if(empty($this->curl)) { |
|
| 647 | 647 | $this->curl = curl_init(); |
| 648 | 648 | curl_setopt($this->curl, CURLOPT_HEADER, true); |
| 649 | 649 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false); |
| 650 | 650 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, false); |
| 651 | 651 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
| 652 | - curl_setopt($this->curl, CURLOPT_USERAGENT, self::USERAGENT.self::VERSION); |
|
| 652 | + curl_setopt($this->curl, CURLOPT_USERAGENT, self::USERAGENT . self::VERSION); |
|
| 653 | 653 | |
| 654 | - if ($this->auth !== null) { |
|
| 654 | + if($this->auth !== null) { |
|
| 655 | 655 | curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
| 656 | 656 | curl_setopt($this->curl, CURLOPT_USERPWD, $this->auth); |
| 657 | 657 | } |
| 658 | 658 | |
| 659 | 659 | /* allow to follow redirects if activated */ |
| 660 | 660 | curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, $this->follow_redirects); |
| 661 | - if ($this->follow_redirects) |
|
| 661 | + if($this->follow_redirects) |
|
| 662 | 662 | { |
| 663 | 663 | curl_setopt($this->curl, CURLOPT_MAXREDIRS, $this->follow_redirects_count); |
| 664 | 664 | } |
@@ -681,7 +681,7 @@ discard block |
||
| 681 | 681 | curl_setopt($this->curl, CURLOPT_URL, $url); |
| 682 | 682 | curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); |
| 683 | 683 | |
| 684 | - if ($content !== null) { |
|
| 684 | + if($content !== null) { |
|
| 685 | 685 | curl_setopt($this->curl, CURLOPT_POST, true); |
| 686 | 686 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content); |
| 687 | 687 | } else { |
@@ -689,8 +689,8 @@ discard block |
||
| 689 | 689 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, null); |
| 690 | 690 | } |
| 691 | 691 | |
| 692 | - if ($content_type !== null) { |
|
| 693 | - curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: '.$content_type, 'Depth: 1')); |
|
| 692 | + if($content_type !== null) { |
|
| 693 | + curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: ' . $content_type, 'Depth: 1')); |
|
| 694 | 694 | } else { |
| 695 | 695 | curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Depth: 1')); |
| 696 | 696 | } |
@@ -698,7 +698,7 @@ discard block |
||
| 698 | 698 | $complete_response = curl_exec($this->curl); |
| 699 | 699 | $header_size = curl_getinfo($this->curl, CURLINFO_HEADER_SIZE); |
| 700 | 700 | $http_code = curl_getinfo($this->curl, CURLINFO_HTTP_CODE); |
| 701 | - $header = trim(substr($complete_response, 0, $header_size)); |
|
| 701 | + $header = trim(substr($complete_response, 0, $header_size)); |
|
| 702 | 702 | $response = substr($complete_response, $header_size); |
| 703 | 703 | |
| 704 | 704 | $return = array( |
@@ -706,7 +706,7 @@ discard block |
||
| 706 | 706 | 'http_code' => $http_code |
| 707 | 707 | ); |
| 708 | 708 | |
| 709 | - if ($this->debug === true) { |
|
| 709 | + if($this->debug === true) { |
|
| 710 | 710 | $debug = $return; |
| 711 | 711 | $debug['url'] = $url; |
| 712 | 712 | $debug['method'] = $method; |
@@ -728,8 +728,8 @@ discard block |
||
| 728 | 728 | { |
| 729 | 729 | $vcard_id = null; |
| 730 | 730 | |
| 731 | - for ($number = 0; $number <= 25; $number ++) { |
|
| 732 | - if ($number == 8 || $number == 17) { |
|
| 731 | + for($number = 0; $number <= 25; $number++) { |
|
| 732 | + if($number == 8 || $number == 17) { |
|
| 733 | 733 | $vcard_id .= '-'; |
| 734 | 734 | } else { |
| 735 | 735 | $vcard_id .= $this->vcard_id_chars[mt_rand(0, (count($this->vcard_id_chars) - 1))]; |
@@ -742,12 +742,12 @@ discard block |
||
| 742 | 742 | |
| 743 | 743 | $result = $carddav->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET'); |
| 744 | 744 | |
| 745 | - if ($result['http_code'] !== 404) { |
|
| 745 | + if($result['http_code'] !== 404) { |
|
| 746 | 746 | $vcard_id = $this->generateVcardId(); |
| 747 | 747 | } |
| 748 | 748 | |
| 749 | 749 | return $vcard_id; |
| 750 | - } catch (Exception $e) { |
|
| 750 | + } catch(Exception $e) { |
|
| 751 | 751 | throw new \Exception($e->getMessage(), self::EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID); |
| 752 | 752 | } |
| 753 | 753 | } |
@@ -760,7 +760,7 @@ discard block |
||
| 760 | 760 | */ |
| 761 | 761 | public function __destruct() |
| 762 | 762 | { |
| 763 | - if (!empty($this->curl)) { |
|
| 763 | + if(!empty($this->curl)) { |
|
| 764 | 764 | curl_close($this->curl); |
| 765 | 765 | } |
| 766 | 766 | } |
@@ -219,7 +219,8 @@ discard block |
||
| 219 | 219 | */ |
| 220 | 220 | public function __construct($url = null) |
| 221 | 221 | { |
| 222 | - if ($url !== null) { |
|
| 222 | + if ($url !== null) |
|
| 223 | + { |
|
| 223 | 224 | $this->setUrl($url); |
| 224 | 225 | } |
| 225 | 226 | } |
@@ -245,7 +246,8 @@ discard block |
||
| 245 | 246 | { |
| 246 | 247 | $this->url = $url; |
| 247 | 248 | |
| 248 | - if (substr($this->url, -1, 1) !== '/') { |
|
| 249 | + if (substr($this->url, -1, 1) !== '/') |
|
| 250 | + { |
|
| 249 | 251 | $this->url = $this->url . '/'; |
| 250 | 252 | } |
| 251 | 253 | |
@@ -339,9 +341,12 @@ discard block |
||
| 339 | 341 | { |
| 340 | 342 | case 200: |
| 341 | 343 | case 207: |
| 342 | - if ($raw === true) { |
|
| 344 | + if ($raw === true) |
|
| 345 | + { |
|
| 343 | 346 | return $result['response']; |
| 344 | - } else { |
|
| 347 | + } |
|
| 348 | + else |
|
| 349 | + { |
|
| 345 | 350 | return $this->simplify($result['response'], $include_vcards); |
| 346 | 351 | } |
| 347 | 352 | } |
@@ -368,13 +373,13 @@ discard block |
||
| 368 | 373 | // print_r($result); |
| 369 | 374 | |
| 370 | 375 | switch ($result['http_code']) |
| 371 | - { |
|
| 376 | + { |
|
| 372 | 377 | case 404: |
| 373 | 378 | $altResult = $this->query($this->url . $vcard_id , 'GET'); |
| 374 | 379 | // DEBUG: print the response of the carddav-server |
| 375 | 380 | // print_r($altResult); |
| 376 | 381 | switch ($altResult['http_code']) |
| 377 | - { |
|
| 382 | + { |
|
| 378 | 383 | case 200: |
| 379 | 384 | case 207: |
| 380 | 385 | print "Ignoring given Vcard Extension (".$this->url_vcard_extension.")" . PHP_EOL. "."; |
@@ -458,9 +463,12 @@ discard block |
||
| 458 | 463 | { |
| 459 | 464 | $result = $this->query($this->url, 'OPTIONS'); |
| 460 | 465 | |
| 461 | - if ($result['http_code'] === 200) { |
|
| 466 | + if ($result['http_code'] === 200) |
|
| 467 | + { |
|
| 462 | 468 | return true; |
| 463 | - } else { |
|
| 469 | + } |
|
| 470 | + else |
|
| 471 | + { |
|
| 464 | 472 | return false; |
| 465 | 473 | } |
| 466 | 474 | } |
@@ -509,7 +517,8 @@ discard block |
||
| 509 | 517 | */ |
| 510 | 518 | public function add($vcard, $vcard_id = null) |
| 511 | 519 | { |
| 512 | - if ($vcard_id === null) { |
|
| 520 | + if ($vcard_id === null) |
|
| 521 | + { |
|
| 513 | 522 | $vcard_id = $this->generateVcardId(); |
| 514 | 523 | } |
| 515 | 524 | $vcard = $this->cleanVcard($vcard); |
@@ -536,9 +545,12 @@ discard block |
||
| 536 | 545 | */ |
| 537 | 546 | public function update($vcard, $vcard_id) |
| 538 | 547 | { |
| 539 | - try { |
|
| 548 | + try |
|
| 549 | + { |
|
| 540 | 550 | return $this->add($vcard, $vcard_id); |
| 541 | - } catch (Exception $e) { |
|
| 551 | + } |
|
| 552 | + catch (Exception $e) |
|
| 553 | + { |
|
| 542 | 554 | throw new \Exception($e->getMessage(), self::EXCEPTION_WRONG_HTTP_STATUS_CODE_UPDATE); |
| 543 | 555 | } |
| 544 | 556 | } |
@@ -554,9 +566,12 @@ discard block |
||
| 554 | 566 | { |
| 555 | 567 | $response = $this->cleanResponse($response); |
| 556 | 568 | |
| 557 | - try { |
|
| 569 | + try |
|
| 570 | + { |
|
| 558 | 571 | $xml = new \SimpleXMLElement($response); |
| 559 | - } catch (Exception $e) { |
|
| 572 | + } |
|
| 573 | + catch (Exception $e) |
|
| 574 | + { |
|
| 560 | 575 | throw new \Exception( |
| 561 | 576 | "The XML response seems to be malformed and can't be simplified!", |
| 562 | 577 | self::EXCEPTION_MALFORMED_XML_RESPONSE, |
@@ -571,35 +586,50 @@ discard block |
||
| 571 | 586 | $simplified_xml->startDocument('1.0', 'utf-8'); |
| 572 | 587 | $simplified_xml->startElement('response'); |
| 573 | 588 | |
| 574 | - if (!empty($xml->response)) { |
|
| 575 | - foreach ($xml->response as $response) { |
|
| 589 | + if (!empty($xml->response)) |
|
| 590 | + { |
|
| 591 | + foreach ($xml->response as $response) |
|
| 592 | + { |
|
| 576 | 593 | if ((preg_match('/vcard/', $response->propstat->prop->getcontenttype) || preg_match('/vcf/', $response->href)) && |
| 577 | - !$response->propstat->prop->resourcetype->collection) { |
|
| 594 | + !$response->propstat->prop->resourcetype->collection) |
|
| 595 | + { |
|
| 578 | 596 | $id = basename($response->href); |
| 579 | 597 | $id = str_replace($this->url_vcard_extension, null, $id); |
| 580 | 598 | |
| 581 | - try { |
|
| 599 | + try |
|
| 600 | + { |
|
| 582 | 601 | $vcardData = $this->getVcard($id); |
| 583 | - if (!empty($id)) { |
|
| 602 | + if (!empty($id)) |
|
| 603 | + { |
|
| 584 | 604 | $simplified_xml->startElement('element'); |
| 585 | 605 | $simplified_xml->writeElement('id', $id); |
| 586 | 606 | $simplified_xml->writeElement('etag', str_replace('"', null, $response->propstat->prop->getetag)); |
| 587 | 607 | $simplified_xml->writeElement('last_modified', $response->propstat->prop->getlastmodified); |
| 588 | 608 | |
| 589 | - if ($include_vcards === true) { |
|
| 609 | + if ($include_vcards === true) |
|
| 610 | + { |
|
| 590 | 611 | $simplified_xml->writeElement('vcard', $vcardData); |
| 591 | 612 | } |
| 592 | 613 | $simplified_xml->endElement(); |
| 593 | 614 | } |
| 594 | - } catch (\Exception $e) { |
|
| 615 | + } |
|
| 616 | + catch (\Exception $e) |
|
| 617 | + { |
|
| 595 | 618 | print("Error fetching vCard: {$id}: {$e->getMessage()}\n"); |
| 596 | 619 | } |
| 597 | - } elseif (preg_match('/unix-directory/', $response->propstat->prop->getcontenttype)) { |
|
| 598 | - if (isset($response->propstat->prop->href)) { |
|
| 620 | + } |
|
| 621 | + elseif (preg_match('/unix-directory/', $response->propstat->prop->getcontenttype)) |
|
| 622 | + { |
|
| 623 | + if (isset($response->propstat->prop->href)) |
|
| 624 | + { |
|
| 599 | 625 | $href = $response->propstat->prop->href; |
| 600 | - } elseif (isset($response->href)) { |
|
| 626 | + } |
|
| 627 | + elseif (isset($response->href)) |
|
| 628 | + { |
|
| 601 | 629 | $href = $response->href; |
| 602 | - } else { |
|
| 630 | + } |
|
| 631 | + else |
|
| 632 | + { |
|
| 603 | 633 | $href = null; |
| 604 | 634 | } |
| 605 | 635 | |
@@ -643,7 +673,8 @@ discard block |
||
| 643 | 673 | */ |
| 644 | 674 | public function curlInit() |
| 645 | 675 | { |
| 646 | - if (empty($this->curl)) { |
|
| 676 | + if (empty($this->curl)) |
|
| 677 | + { |
|
| 647 | 678 | $this->curl = curl_init(); |
| 648 | 679 | curl_setopt($this->curl, CURLOPT_HEADER, true); |
| 649 | 680 | curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, false); |
@@ -651,7 +682,8 @@ discard block |
||
| 651 | 682 | curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true); |
| 652 | 683 | curl_setopt($this->curl, CURLOPT_USERAGENT, self::USERAGENT.self::VERSION); |
| 653 | 684 | |
| 654 | - if ($this->auth !== null) { |
|
| 685 | + if ($this->auth !== null) |
|
| 686 | + { |
|
| 655 | 687 | curl_setopt($this->curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY); |
| 656 | 688 | curl_setopt($this->curl, CURLOPT_USERPWD, $this->auth); |
| 657 | 689 | } |
@@ -681,17 +713,23 @@ discard block |
||
| 681 | 713 | curl_setopt($this->curl, CURLOPT_URL, $url); |
| 682 | 714 | curl_setopt($this->curl, CURLOPT_CUSTOMREQUEST, $method); |
| 683 | 715 | |
| 684 | - if ($content !== null) { |
|
| 716 | + if ($content !== null) |
|
| 717 | + { |
|
| 685 | 718 | curl_setopt($this->curl, CURLOPT_POST, true); |
| 686 | 719 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, $content); |
| 687 | - } else { |
|
| 720 | + } |
|
| 721 | + else |
|
| 722 | + { |
|
| 688 | 723 | curl_setopt($this->curl, CURLOPT_POST, false); |
| 689 | 724 | curl_setopt($this->curl, CURLOPT_POSTFIELDS, null); |
| 690 | 725 | } |
| 691 | 726 | |
| 692 | - if ($content_type !== null) { |
|
| 727 | + if ($content_type !== null) |
|
| 728 | + { |
|
| 693 | 729 | curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Content-type: '.$content_type, 'Depth: 1')); |
| 694 | - } else { |
|
| 730 | + } |
|
| 731 | + else |
|
| 732 | + { |
|
| 695 | 733 | curl_setopt($this->curl, CURLOPT_HTTPHEADER, array('Depth: 1')); |
| 696 | 734 | } |
| 697 | 735 | |
@@ -706,7 +744,8 @@ discard block |
||
| 706 | 744 | 'http_code' => $http_code |
| 707 | 745 | ); |
| 708 | 746 | |
| 709 | - if ($this->debug === true) { |
|
| 747 | + if ($this->debug === true) |
|
| 748 | + { |
|
| 710 | 749 | $debug = $return; |
| 711 | 750 | $debug['url'] = $url; |
| 712 | 751 | $debug['method'] = $method; |
@@ -728,26 +767,34 @@ discard block |
||
| 728 | 767 | { |
| 729 | 768 | $vcard_id = null; |
| 730 | 769 | |
| 731 | - for ($number = 0; $number <= 25; $number ++) { |
|
| 732 | - if ($number == 8 || $number == 17) { |
|
| 770 | + for ($number = 0; $number <= 25; $number ++) |
|
| 771 | + { |
|
| 772 | + if ($number == 8 || $number == 17) |
|
| 773 | + { |
|
| 733 | 774 | $vcard_id .= '-'; |
| 734 | - } else { |
|
| 775 | + } |
|
| 776 | + else |
|
| 777 | + { |
|
| 735 | 778 | $vcard_id .= $this->vcard_id_chars[mt_rand(0, (count($this->vcard_id_chars) - 1))]; |
| 736 | 779 | } |
| 737 | 780 | } |
| 738 | 781 | |
| 739 | - try { |
|
| 782 | + try |
|
| 783 | + { |
|
| 740 | 784 | $carddav = new CardDavBackend($this->url); |
| 741 | 785 | $carddav->setAuth($this->username, $this->password); |
| 742 | 786 | |
| 743 | 787 | $result = $carddav->query($this->url . $vcard_id . $this->url_vcard_extension, 'GET'); |
| 744 | 788 | |
| 745 | - if ($result['http_code'] !== 404) { |
|
| 789 | + if ($result['http_code'] !== 404) |
|
| 790 | + { |
|
| 746 | 791 | $vcard_id = $this->generateVcardId(); |
| 747 | 792 | } |
| 748 | 793 | |
| 749 | 794 | return $vcard_id; |
| 750 | - } catch (Exception $e) { |
|
| 795 | + } |
|
| 796 | + catch (Exception $e) |
|
| 797 | + { |
|
| 751 | 798 | throw new \Exception($e->getMessage(), self::EXCEPTION_COULD_NOT_GENERATE_NEW_VCARD_ID); |
| 752 | 799 | } |
| 753 | 800 | } |
@@ -760,7 +807,8 @@ discard block |
||
| 760 | 807 | */ |
| 761 | 808 | public function __destruct() |
| 762 | 809 | { |
| 763 | - if (!empty($this->curl)) { |
|
| 810 | + if (!empty($this->curl)) |
|
| 811 | + { |
|
| 764 | 812 | curl_close($this->curl); |
| 765 | 813 | } |
| 766 | 814 | } |
@@ -533,6 +533,10 @@ |
||
| 533 | 533 | * @param $item string the item to set |
| 534 | 534 | * @param $value mixed the value to store into the item |
| 535 | 535 | */ |
| 536 | + |
|
| 537 | + /** |
|
| 538 | + * @param string $item |
|
| 539 | + */ |
|
| 536 | 540 | public function setItem($item, $value) |
| 537 | 541 | { |
| 538 | 542 | $this->config[$item] = $value; |
@@ -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,24 +147,24 @@ 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_CAINFO, '/etc/ssl/certs/'.$ssl_cert_fritzbox.'.pem'); |
|
| 162 | - 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_CAINFO, '/etc/ssl/certs/'.$ssl_cert_fritzbox.'.pem'); |
|
| 162 | + curl_setopt($ch, CURLOPT_CAPATH, '/etc/ssl/certs'); |
|
| 163 | 163 | |
| 164 | - // support for pre FRITZ!OS 5.50 remote config |
|
| 165 | - if (!$this->config->getItem('use_lua_login_method')){ |
|
| 166 | - curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password')); |
|
| 167 | - } |
|
| 164 | + // support for pre FRITZ!OS 5.50 remote config |
|
| 165 | + if (!$this->config->getItem('use_lua_login_method')){ |
|
| 166 | + curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password')); |
|
| 167 | + } |
|
| 168 | 168 | } |
| 169 | 169 | curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($formfields)); |
| 170 | 170 | $output = curl_exec($ch); |
@@ -179,10 +179,10 @@ discard block |
||
| 179 | 179 | // add the sid, if it is already set |
| 180 | 180 | if ($this->sid != '0000000000000000') |
| 181 | 181 | { |
| 182 | - // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!) |
|
| 183 | - // therfore we use array_merge to ensuere the foreach outputs 'sid' fist |
|
| 184 | - $formfields = array_merge(array('sid' => $this->sid), $formfields); |
|
| 185 | - //$formfields['sid'] = $this->sid; |
|
| 182 | + // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!) |
|
| 183 | + // therfore we use array_merge to ensuere the foreach outputs 'sid' fist |
|
| 184 | + $formfields = array_merge(array('sid' => $this->sid), $formfields); |
|
| 185 | + //$formfields['sid'] = $this->sid; |
|
| 186 | 186 | } |
| 187 | 187 | curl_setopt($ch, CURLOPT_URL, $this->config->getItem('fritzbox_url') . '/cgi-bin/firmwarecfg'); |
| 188 | 188 | curl_setopt($ch, CURLOPT_POST, 1); |
@@ -193,30 +193,30 @@ discard block |
||
| 193 | 193 | |
| 194 | 194 | // if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine) |
| 195 | 195 | if(empty( $filefileds )) { |
| 196 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query |
|
| 197 | - } |
|
| 198 | - // post calculated raw data |
|
| 199 | - else { |
|
| 200 | - $header = $this->_create_custom_file_post_header($formfields, $filefileds); |
|
| 201 | - curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 202 | - 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) ) |
|
| 203 | - ); |
|
| 196 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query |
|
| 197 | + } |
|
| 198 | + // post calculated raw data |
|
| 199 | + else { |
|
| 200 | + $header = $this->_create_custom_file_post_header($formfields, $filefileds); |
|
| 201 | + curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 202 | + 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) ) |
|
| 203 | + ); |
|
| 204 | 204 | |
| 205 | - curl_setopt($ch, CURLOPT_POSTFIELDS, $header['data']); |
|
| 206 | - } |
|
| 205 | + curl_setopt($ch, CURLOPT_POSTFIELDS, $header['data']); |
|
| 206 | + } |
|
| 207 | 207 | |
| 208 | 208 | $output = curl_exec($ch); |
| 209 | 209 | |
| 210 | - // curl error |
|
| 211 | - if(curl_errno($ch)) { |
|
| 212 | - $this->error(curl_error($ch)." (".curl_errno($ch).")"); |
|
| 213 | - } |
|
| 210 | + // curl error |
|
| 211 | + if(curl_errno($ch)) { |
|
| 212 | + $this->error(curl_error($ch)." (".curl_errno($ch).")"); |
|
| 213 | + } |
|
| 214 | 214 | |
| 215 | 215 | // finger out an error message, if given |
| 216 | 216 | preg_match('@<p class="ErrorMsg">(.*?)</p>@is', $output, $matches); |
| 217 | 217 | if (isset($matches[1])) |
| 218 | 218 | { |
| 219 | - $this->error(str_replace(' ', ' ', $matches[1])); |
|
| 219 | + $this->error(str_replace(' ', ' ', $matches[1])); |
|
| 220 | 220 | } |
| 221 | 221 | |
| 222 | 222 | curl_close($ch); |
@@ -224,10 +224,10 @@ discard block |
||
| 224 | 224 | } |
| 225 | 225 | |
| 226 | 226 | private function _create_custom_file_post_header($postFields, $fileFields) { |
| 227 | - // form field separator |
|
| 228 | - $delimiter = '-------------' . uniqid(); |
|
| 227 | + // form field separator |
|
| 228 | + $delimiter = '-------------' . uniqid(); |
|
| 229 | 229 | |
| 230 | - /* |
|
| 230 | + /* |
|
| 231 | 231 | // file upload fields: name => array(type=>'mime/type',content=>'raw data') |
| 232 | 232 | $fileFields = array( |
| 233 | 233 | 'file1' => array( |
@@ -242,44 +242,44 @@ discard block |
||
| 242 | 242 | ); |
| 243 | 243 | */ |
| 244 | 244 | |
| 245 | - $data = ''; |
|
| 245 | + $data = ''; |
|
| 246 | 246 | |
| 247 | - // populate normal fields first (simpler) |
|
| 248 | - foreach ($postFields as $name => $content) { |
|
| 249 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 250 | - $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"'; |
|
| 251 | - $data .= "\r\n\r\n"; |
|
| 252 | - $data .= $content; |
|
| 253 | - $data .= "\r\n"; |
|
| 254 | - } |
|
| 255 | - // populate file fields |
|
| 256 | - foreach ($fileFields as $name => $file) { |
|
| 257 | - $data .= "--" . $delimiter . "\r\n"; |
|
| 258 | - // "filename" attribute is not essential; server-side scripts may use it |
|
| 259 | - $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' . |
|
| 260 | - ' filename="' . $file['filename'] . '"' . "\r\n"; |
|
| 247 | + // populate normal fields first (simpler) |
|
| 248 | + foreach ($postFields as $name => $content) { |
|
| 249 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 250 | + $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"'; |
|
| 251 | + $data .= "\r\n\r\n"; |
|
| 252 | + $data .= $content; |
|
| 253 | + $data .= "\r\n"; |
|
| 254 | + } |
|
| 255 | + // populate file fields |
|
| 256 | + foreach ($fileFields as $name => $file) { |
|
| 257 | + $data .= "--" . $delimiter . "\r\n"; |
|
| 258 | + // "filename" attribute is not essential; server-side scripts may use it |
|
| 259 | + $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' . |
|
| 260 | + ' filename="' . $file['filename'] . '"' . "\r\n"; |
|
| 261 | 261 | |
| 262 | - //$data .= 'Content-Transfer-Encoding: binary'."\r\n"; |
|
| 263 | - // this is, again, informative only; good practice to include though |
|
| 264 | - $data .= 'Content-Type: ' . $file['type'] . "\r\n"; |
|
| 265 | - // this endline must be here to indicate end of headers |
|
| 266 | - $data .= "\r\n"; |
|
| 267 | - // the file itself (note: there's no encoding of any kind) |
|
| 268 | - $data .= $file['content'] . "\r\n"; |
|
| 269 | - } |
|
| 270 | - // last delimiter |
|
| 271 | - $data .= "--" . $delimiter . "--\r\n"; |
|
| 262 | + //$data .= 'Content-Transfer-Encoding: binary'."\r\n"; |
|
| 263 | + // this is, again, informative only; good practice to include though |
|
| 264 | + $data .= 'Content-Type: ' . $file['type'] . "\r\n"; |
|
| 265 | + // this endline must be here to indicate end of headers |
|
| 266 | + $data .= "\r\n"; |
|
| 267 | + // the file itself (note: there's no encoding of any kind) |
|
| 268 | + $data .= $file['content'] . "\r\n"; |
|
| 269 | + } |
|
| 270 | + // last delimiter |
|
| 271 | + $data .= "--" . $delimiter . "--\r\n"; |
|
| 272 | 272 | |
| 273 | - return array('delimiter' => $delimiter, 'data' => $data); |
|
| 274 | - } |
|
| 273 | + return array('delimiter' => $delimiter, 'data' => $data); |
|
| 274 | + } |
|
| 275 | 275 | |
| 276 | 276 | /** |
| 277 | - * do a GET request on the box |
|
| 278 | - * the main cURL wrapper handles the command |
|
| 279 | - * |
|
| 280 | - * @param array $params an associative array with the GET params to pass |
|
| 281 | - * @return string the raw HTML code returned by the Fritz!Box |
|
| 282 | - */ |
|
| 277 | + * do a GET request on the box |
|
| 278 | + * the main cURL wrapper handles the command |
|
| 279 | + * |
|
| 280 | + * @param array $params an associative array with the GET params to pass |
|
| 281 | + * @return string the raw HTML code returned by the Fritz!Box |
|
| 282 | + */ |
|
| 283 | 283 | public function doGetRequest($params = array()) |
| 284 | 284 | { |
| 285 | 285 | // add the sid, if it is already set |
@@ -319,12 +319,12 @@ discard block |
||
| 319 | 319 | |
| 320 | 320 | |
| 321 | 321 | /** |
| 322 | - * the login method, handles the secured login-process |
|
| 323 | - * newer firmwares (xx.04.74 and newer) need a challenge-response mechanism to prevent Cross-Site Request Forgery attacks |
|
| 324 | - * see http://www.avm.de/de/Extern/Technical_Note_Session_ID.pdf for details |
|
| 325 | - * |
|
| 326 | - * @return string a valid SID, if the login was successful, otherwise throws an Exception with an error message |
|
| 327 | - */ |
|
| 322 | + * the login method, handles the secured login-process |
|
| 323 | + * newer firmwares (xx.04.74 and newer) need a challenge-response mechanism to prevent Cross-Site Request Forgery attacks |
|
| 324 | + * see http://www.avm.de/de/Extern/Technical_Note_Session_ID.pdf for details |
|
| 325 | + * |
|
| 326 | + * @return string a valid SID, if the login was successful, otherwise throws an Exception with an error message |
|
| 327 | + */ |
|
| 328 | 328 | protected function initSID() |
| 329 | 329 | { |
| 330 | 330 | // determine, wich login type we have to use |
@@ -404,9 +404,9 @@ discard block |
||
| 404 | 404 | |
| 405 | 405 | |
| 406 | 406 | /** |
| 407 | - * the logout method just sends a logout command to the Fritz!Box |
|
| 408 | - * |
|
| 409 | - */ |
|
| 407 | + * the logout method just sends a logout command to the Fritz!Box |
|
| 408 | + * |
|
| 409 | + */ |
|
| 410 | 410 | protected function logout() |
| 411 | 411 | { |
| 412 | 412 | if ( $this->config->getItem('use_lua_login_method') == true ) |
@@ -425,10 +425,10 @@ discard block |
||
| 425 | 425 | |
| 426 | 426 | |
| 427 | 427 | /** |
| 428 | - * the error method just throws an Exception |
|
| 429 | - * |
|
| 430 | - * @param string $message an error message for the Exception |
|
| 431 | - */ |
|
| 428 | + * the error method just throws an Exception |
|
| 429 | + * |
|
| 430 | + * @param string $message an error message for the Exception |
|
| 431 | + */ |
|
| 432 | 432 | public function error($message = null) |
| 433 | 433 | { |
| 434 | 434 | throw new Exception($message); |
@@ -436,20 +436,20 @@ discard block |
||
| 436 | 436 | |
| 437 | 437 | |
| 438 | 438 | /** |
| 439 | - * a getter for the session ID |
|
| 440 | - * |
|
| 441 | - * @return string $this->sid |
|
| 442 | - */ |
|
| 439 | + * a getter for the session ID |
|
| 440 | + * |
|
| 441 | + * @return string $this->sid |
|
| 442 | + */ |
|
| 443 | 443 | public function getSID() |
| 444 | 444 | { |
| 445 | 445 | return $this->sid; |
| 446 | 446 | } |
| 447 | 447 | |
| 448 | 448 | /** |
| 449 | - * log a message |
|
| 450 | - * |
|
| 451 | - * @param $message string the message to log |
|
| 452 | - */ |
|
| 449 | + * log a message |
|
| 450 | + * |
|
| 451 | + * @param $message string the message to log |
|
| 452 | + */ |
|
| 453 | 453 | public function logMessage($message) |
| 454 | 454 | { |
| 455 | 455 | 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 { |
@@ -158,11 +158,11 @@ discard block |
||
| 158 | 158 | // set SSL-options and path to certificate |
| 159 | 159 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 2); |
| 160 | 160 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2); |
| 161 | - curl_setopt($ch, CURLOPT_CAINFO, '/etc/ssl/certs/'.$ssl_cert_fritzbox.'.pem'); |
|
| 161 | + curl_setopt($ch, CURLOPT_CAINFO, '/etc/ssl/certs/' . $ssl_cert_fritzbox . '.pem'); |
|
| 162 | 162 | curl_setopt($ch, CURLOPT_CAPATH, '/etc/ssl/certs'); |
| 163 | 163 | |
| 164 | 164 | // support for pre FRITZ!OS 5.50 remote config |
| 165 | - if (!$this->config->getItem('use_lua_login_method')){ |
|
| 165 | + if(!$this->config->getItem('use_lua_login_method')) { |
|
| 166 | 166 | curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password')); |
| 167 | 167 | } |
| 168 | 168 | } |
@@ -177,7 +177,7 @@ discard block |
||
| 177 | 177 | $ch = curl_init(); |
| 178 | 178 | |
| 179 | 179 | // add the sid, if it is already set |
| 180 | - if ($this->sid != '0000000000000000') |
|
| 180 | + if($this->sid != '0000000000000000') |
|
| 181 | 181 | { |
| 182 | 182 | // 'sid' MUST be the first POST variable!!! (otherwise it will not work!!) |
| 183 | 183 | // therfore we use array_merge to ensuere the foreach outputs 'sid' fist |
@@ -192,13 +192,13 @@ discard block |
||
| 192 | 192 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 193 | 193 | |
| 194 | 194 | // if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine) |
| 195 | - if(empty( $filefileds )) { |
|
| 195 | + if(empty($filefileds)) { |
|
| 196 | 196 | curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query |
| 197 | 197 | } |
| 198 | 198 | // post calculated raw data |
| 199 | 199 | else { |
| 200 | 200 | $header = $this->_create_custom_file_post_header($formfields, $filefileds); |
| 201 | - curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
|
| 201 | + curl_setopt($ch, CURLOPT_HTTPHEADER, array( |
|
| 202 | 202 | 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) ) |
| 203 | 203 | ); |
| 204 | 204 | |
@@ -209,12 +209,12 @@ discard block |
||
| 209 | 209 | |
| 210 | 210 | // curl error |
| 211 | 211 | if(curl_errno($ch)) { |
| 212 | - $this->error(curl_error($ch)." (".curl_errno($ch).")"); |
|
| 212 | + $this->error(curl_error($ch) . " (" . curl_errno($ch) . ")"); |
|
| 213 | 213 | } |
| 214 | 214 | |
| 215 | 215 | // finger out an error message, if given |
| 216 | 216 | preg_match('@<p class="ErrorMsg">(.*?)</p>@is', $output, $matches); |
| 217 | - if (isset($matches[1])) |
|
| 217 | + if(isset($matches[1])) |
|
| 218 | 218 | { |
| 219 | 219 | $this->error(str_replace(' ', ' ', $matches[1])); |
| 220 | 220 | } |
@@ -245,7 +245,7 @@ discard block |
||
| 245 | 245 | $data = ''; |
| 246 | 246 | |
| 247 | 247 | // populate normal fields first (simpler) |
| 248 | - foreach ($postFields as $name => $content) { |
|
| 248 | + foreach($postFields as $name => $content) { |
|
| 249 | 249 | $data .= "--" . $delimiter . "\r\n"; |
| 250 | 250 | $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"'; |
| 251 | 251 | $data .= "\r\n\r\n"; |
@@ -253,7 +253,7 @@ discard block |
||
| 253 | 253 | $data .= "\r\n"; |
| 254 | 254 | } |
| 255 | 255 | // populate file fields |
| 256 | - foreach ($fileFields as $name => $file) { |
|
| 256 | + foreach($fileFields as $name => $file) { |
|
| 257 | 257 | $data .= "--" . $delimiter . "\r\n"; |
| 258 | 258 | // "filename" attribute is not essential; server-side scripts may use it |
| 259 | 259 | $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' . |
@@ -283,13 +283,13 @@ discard block |
||
| 283 | 283 | public function doGetRequest($params = array()) |
| 284 | 284 | { |
| 285 | 285 | // add the sid, if it is already set |
| 286 | - if ($this->sid != '0000000000000000') |
|
| 286 | + if($this->sid != '0000000000000000') |
|
| 287 | 287 | { |
| 288 | 288 | $params['sid'] = $this->sid; |
| 289 | 289 | } |
| 290 | 290 | |
| 291 | 291 | $ch = curl_init(); |
| 292 | - if ( strpos($params['getpage'], '.lua') > 0 ) |
|
| 292 | + if(strpos($params['getpage'], '.lua') > 0) |
|
| 293 | 293 | { |
| 294 | 294 | $getpage = $params['getpage'] . '?'; |
| 295 | 295 | unset($params['getpage']); |
@@ -301,13 +301,13 @@ discard block |
||
| 301 | 301 | curl_setopt($ch, CURLOPT_URL, $this->config->getItem('fritzbox_url') . $getpage . http_build_query($params)); |
| 302 | 302 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 303 | 303 | curl_setopt($ch, CURLOPT_HTTPGET, 1); |
| 304 | - if ( $this->config->getItem('enable_remote_config') ) |
|
| 304 | + if($this->config->getItem('enable_remote_config')) |
|
| 305 | 305 | { |
| 306 | 306 | curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
| 307 | 307 | curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); |
| 308 | 308 | |
| 309 | 309 | // support for pre FRITZ!OS 5.50 remote config |
| 310 | - if ( !$this->config->getItem('use_lua_login_method') ) |
|
| 310 | + if(!$this->config->getItem('use_lua_login_method')) |
|
| 311 | 311 | { |
| 312 | 312 | curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password')); |
| 313 | 313 | } |
@@ -328,7 +328,7 @@ discard block |
||
| 328 | 328 | protected function initSID() |
| 329 | 329 | { |
| 330 | 330 | // determine, wich login type we have to use |
| 331 | - if ( $this->config->getItem('use_lua_login_method') == true ) |
|
| 331 | + if($this->config->getItem('use_lua_login_method') == true) |
|
| 332 | 332 | { |
| 333 | 333 | $loginpage = '/login_sid.lua'; |
| 334 | 334 | } |
@@ -340,13 +340,13 @@ discard block |
||
| 340 | 340 | // read the current status |
| 341 | 341 | $session_status_simplexml = simplexml_load_string($this->doGetRequest(array('getpage' => $loginpage))); |
| 342 | 342 | |
| 343 | - if ( !is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement' ) |
|
| 343 | + if(!is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement') |
|
| 344 | 344 | { |
| 345 | 345 | $this->error('Response of initialization call ' . $loginpage . ' in ' . __FUNCTION__ . ' was not xml-formatted.'); |
| 346 | 346 | } |
| 347 | 347 | |
| 348 | 348 | // perhaps we already have a SID (i.e. when no password is set) |
| 349 | - if ( $session_status_simplexml->SID != '0000000000000000' ) |
|
| 349 | + if($session_status_simplexml->SID != '0000000000000000') |
|
| 350 | 350 | { |
| 351 | 351 | return $session_status_simplexml->SID; |
| 352 | 352 | } |
@@ -360,16 +360,16 @@ discard block |
||
| 360 | 360 | $formfields = array( |
| 361 | 361 | 'getpage' => $loginpage, |
| 362 | 362 | ); |
| 363 | - if ( $this->config->getItem('use_lua_login_method') ) |
|
| 363 | + if($this->config->getItem('use_lua_login_method')) |
|
| 364 | 364 | { |
| 365 | - if ( $this->config->getItem('enable_remote_config') ) |
|
| 365 | + if($this->config->getItem('enable_remote_config')) |
|
| 366 | 366 | { |
| 367 | 367 | $formfields['username'] = $this->config->getItem('remote_config_user'); |
| 368 | 368 | $response = $challenge . '-' . md5(mb_convert_encoding($challenge . '-' . $this->config->getItem('remote_config_password'), "UCS-2LE", "UTF-8")); |
| 369 | 369 | } |
| 370 | 370 | else |
| 371 | 371 | { |
| 372 | - if ( $this->config->getItem('username') ) |
|
| 372 | + if($this->config->getItem('username')) |
|
| 373 | 373 | { |
| 374 | 374 | $formfields['username'] = $this->config->getItem('username'); |
| 375 | 375 | } |
@@ -386,12 +386,12 @@ discard block |
||
| 386 | 386 | |
| 387 | 387 | // finger out the SID from the response |
| 388 | 388 | $session_status_simplexml = simplexml_load_string($output); |
| 389 | - if ( !is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement' ) |
|
| 389 | + if(!is_object($session_status_simplexml) || get_class($session_status_simplexml) != 'SimpleXMLElement') |
|
| 390 | 390 | { |
| 391 | 391 | $this->error('Response of login call to ' . $loginpage . ' in ' . __FUNCTION__ . ' was not xml-formatted.'); |
| 392 | 392 | } |
| 393 | 393 | |
| 394 | - if ( $session_status_simplexml->SID != '0000000000000000' ) |
|
| 394 | + if($session_status_simplexml->SID != '0000000000000000') |
|
| 395 | 395 | { |
| 396 | 396 | return (string)$session_status_simplexml->SID; |
| 397 | 397 | } |
@@ -409,7 +409,7 @@ discard block |
||
| 409 | 409 | */ |
| 410 | 410 | protected function logout() |
| 411 | 411 | { |
| 412 | - if ( $this->config->getItem('use_lua_login_method') == true ) |
|
| 412 | + if($this->config->getItem('use_lua_login_method') == true) |
|
| 413 | 413 | { |
| 414 | 414 | $this->doGetRequest(array('getpage' => '/home/home.lua', 'logout' => '1')); |
| 415 | 415 | } |
@@ -452,22 +452,22 @@ discard block |
||
| 452 | 452 | */ |
| 453 | 453 | public function logMessage($message) |
| 454 | 454 | { |
| 455 | - if ( $this->config->getItem('newline') == false ) |
|
| 455 | + if($this->config->getItem('newline') == false) |
|
| 456 | 456 | { |
| 457 | 457 | $this->config->setItem('newline', (PHP_OS == 'WINNT') ? "\r\n" : "\n"); |
| 458 | 458 | } |
| 459 | 459 | |
| 460 | - if ( $this->config->getItem('logging') == 'console' ) |
|
| 460 | + if($this->config->getItem('logging') == 'console') |
|
| 461 | 461 | { |
| 462 | 462 | echo $message; |
| 463 | 463 | } |
| 464 | - else if ( $this->config->getItem('logging') == 'silent' || $this->config->getItem('logging') == false ) |
|
| 464 | + else if($this->config->getItem('logging') == 'silent' || $this->config->getItem('logging') == false) |
|
| 465 | 465 | { |
| 466 | 466 | // do nothing |
| 467 | 467 | } |
| 468 | 468 | else |
| 469 | 469 | { |
| 470 | - if ( is_writable($this->config->getItem('logging')) || is_writable(dirname($this->config->getItem('logging'))) ) |
|
| 470 | + if(is_writable($this->config->getItem('logging')) || is_writable(dirname($this->config->getItem('logging')))) |
|
| 471 | 471 | { |
| 472 | 472 | file_put_contents($this->config->getItem('logging'), $message . $this->config->getItem('newline'), FILE_APPEND); |
| 473 | 473 | } |
@@ -517,11 +517,11 @@ discard block |
||
| 517 | 517 | */ |
| 518 | 518 | public function getItem($item = 'all') |
| 519 | 519 | { |
| 520 | - if ( $item == 'all' ) |
|
| 520 | + if($item == 'all') |
|
| 521 | 521 | { |
| 522 | 522 | return $this->config; |
| 523 | 523 | } |
| 524 | - elseif ( isset($this->config[$item]) ) |
|
| 524 | + elseif(isset($this->config[$item])) |
|
| 525 | 525 | { |
| 526 | 526 | return $this->config[$item]; |
| 527 | 527 | } |
@@ -46,7 +46,8 @@ discard block |
||
| 46 | 46 | * the main Fritz!Box API class |
| 47 | 47 | * |
| 48 | 48 | */ |
| 49 | -class fritzbox_api { |
|
| 49 | +class fritzbox_api |
|
| 50 | +{ |
|
| 50 | 51 | /** |
| 51 | 52 | * @var object config object |
| 52 | 53 | */ |
@@ -69,7 +70,8 @@ discard block |
||
| 69 | 70 | $this->config = new fritzbox_api_config(); |
| 70 | 71 | |
| 71 | 72 | // try autoloading the config |
| 72 | - if (file_exists(__DIR__ . '/fritzbox_user.conf.php') && is_readable(__DIR__ . '/fritzbox_user.conf.php') ) { |
|
| 73 | + if (file_exists(__DIR__ . '/fritzbox_user.conf.php') && is_readable(__DIR__ . '/fritzbox_user.conf.php') ) |
|
| 74 | + { |
|
| 73 | 75 | require_once(__DIR__ . '/fritzbox_user.conf.php'); |
| 74 | 76 | } |
| 75 | 77 | |
@@ -77,29 +79,37 @@ discard block |
||
| 77 | 79 | $this->config->setItem('fritzbox_ip',$fritzbox_ip); |
| 78 | 80 | |
| 79 | 81 | // check if login on local network (fritz.box) or via a dynamic DNS-host |
| 80 | - if ($fritzbox_ip != 'fritz.box' && !$force_local_login) { |
|
| 82 | + if ($fritzbox_ip != 'fritz.box' && !$force_local_login) |
|
| 83 | + { |
|
| 81 | 84 | $this->config->setItem('enable_remote_config',true); |
| 82 | 85 | $this->config->setItem('remote_config_user',$user_name); |
| 83 | 86 | $this->config->setItem('remote_config_password',$password); |
| 84 | 87 | $this->config->setItem('fritzbox_url', 'https://'.$this->config->getItem('fritzbox_ip')); |
| 85 | - } else { |
|
| 88 | + } |
|
| 89 | + else |
|
| 90 | + { |
|
| 86 | 91 | $this->config->setItem('enable_remote_config',false); |
| 87 | - if($user_name != false){ |
|
| 92 | + if($user_name != false) |
|
| 93 | + { |
|
| 88 | 94 | $this->config->setItem('username',$user_name); |
| 89 | 95 | } |
| 90 | - if($password != false){ |
|
| 96 | + if($password != false) |
|
| 97 | + { |
|
| 91 | 98 | $this->config->setItem('password',$password); |
| 92 | 99 | } |
| 93 | 100 | $this->config->setItem('fritzbox_url', 'http://' . $this->config->getItem('fritzbox_ip')); |
| 94 | 101 | } |
| 95 | 102 | |
| 96 | 103 | // 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') ){ |
|
| 104 | + if ( $this->config->getItem('enable_remote_config') === true ) |
|
| 105 | + { |
|
| 106 | + if ( !$this->config->getItem('remote_config_user') || !$this->config->getItem('remote_config_password') ) |
|
| 107 | + { |
|
| 99 | 108 | $this->error('ERROR: Remote config mode enabled, but no username or no password provided'); |
| 100 | 109 | } |
| 101 | 110 | } |
| 102 | - else { |
|
| 111 | + else |
|
| 112 | + { |
|
| 103 | 113 | $this->config->setItem('old_remote_config_user', null); |
| 104 | 114 | $this->config->setItem('old_remote_config_password', null); |
| 105 | 115 | } |
@@ -148,10 +158,13 @@ discard block |
||
| 148 | 158 | if ( $this->config->getItem('enable_remote_config') == true ) |
| 149 | 159 | { |
| 150 | 160 | // 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'),":")){ |
|
| 161 | + if (strpos($this->config->getItem('fritzbox_ip'),":")) |
|
| 162 | + { |
|
| 152 | 163 | $ssl_cert_fritzbox = explode(":", $this->config->getItem('fritzbox_ip')); |
| 153 | 164 | $ssl_cert_fritzbox = $ssl_cert_fritzbox[0]; |
| 154 | - } else { |
|
| 165 | + } |
|
| 166 | + else |
|
| 167 | + { |
|
| 155 | 168 | $ssl_cert_fritzbox = $this->config->getItem('fritzbox_ip'); |
| 156 | 169 | } |
| 157 | 170 | |
@@ -162,7 +175,8 @@ discard block |
||
| 162 | 175 | curl_setopt($ch, CURLOPT_CAPATH, '/etc/ssl/certs'); |
| 163 | 176 | |
| 164 | 177 | // support for pre FRITZ!OS 5.50 remote config |
| 165 | - if (!$this->config->getItem('use_lua_login_method')){ |
|
| 178 | + if (!$this->config->getItem('use_lua_login_method')) |
|
| 179 | + { |
|
| 166 | 180 | curl_setopt($ch, CURLOPT_USERPWD, $this->config->getItem('remote_config_user') . ':' . $this->config->getItem('remote_config_password')); |
| 167 | 181 | } |
| 168 | 182 | } |
@@ -173,7 +187,7 @@ discard block |
||
| 173 | 187 | } |
| 174 | 188 | |
| 175 | 189 | public function doPostFile($formfields = array(), $filefileds = array()) |
| 176 | - { |
|
| 190 | + { |
|
| 177 | 191 | $ch = curl_init(); |
| 178 | 192 | |
| 179 | 193 | // add the sid, if it is already set |
@@ -192,11 +206,13 @@ discard block |
||
| 192 | 206 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); |
| 193 | 207 | |
| 194 | 208 | // if filefileds not specified ('@/path/to/file.xml;type=text/xml' works fine) |
| 195 | - if(empty( $filefileds )) { |
|
| 209 | + if(empty( $filefileds )) |
|
| 210 | + { |
|
| 196 | 211 | curl_setopt($ch, CURLOPT_POSTFIELDS, $formfields); // http_build_query |
| 197 | 212 | } |
| 198 | 213 | // post calculated raw data |
| 199 | - else { |
|
| 214 | + else |
|
| 215 | + { |
|
| 200 | 216 | $header = $this->_create_custom_file_post_header($formfields, $filefileds); |
| 201 | 217 | curl_setopt($ch, CURLOPT_HTTPHEADER , array( |
| 202 | 218 | 'Content-Type: multipart/form-data; boundary=' . $header['delimiter'], 'Content-Length: ' . strlen($header['data']) ) |
@@ -208,7 +224,8 @@ discard block |
||
| 208 | 224 | $output = curl_exec($ch); |
| 209 | 225 | |
| 210 | 226 | // curl error |
| 211 | - if(curl_errno($ch)) { |
|
| 227 | + if(curl_errno($ch)) |
|
| 228 | + { |
|
| 212 | 229 | $this->error(curl_error($ch)." (".curl_errno($ch).")"); |
| 213 | 230 | } |
| 214 | 231 | |
@@ -223,7 +240,8 @@ discard block |
||
| 223 | 240 | return $output; |
| 224 | 241 | } |
| 225 | 242 | |
| 226 | - private function _create_custom_file_post_header($postFields, $fileFields) { |
|
| 243 | + private function _create_custom_file_post_header($postFields, $fileFields) |
|
| 244 | + { |
|
| 227 | 245 | // form field separator |
| 228 | 246 | $delimiter = '-------------' . uniqid(); |
| 229 | 247 | |
@@ -245,7 +263,8 @@ discard block |
||
| 245 | 263 | $data = ''; |
| 246 | 264 | |
| 247 | 265 | // populate normal fields first (simpler) |
| 248 | - foreach ($postFields as $name => $content) { |
|
| 266 | + foreach ($postFields as $name => $content) |
|
| 267 | + { |
|
| 249 | 268 | $data .= "--" . $delimiter . "\r\n"; |
| 250 | 269 | $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '"'; |
| 251 | 270 | $data .= "\r\n\r\n"; |
@@ -253,7 +272,8 @@ discard block |
||
| 253 | 272 | $data .= "\r\n"; |
| 254 | 273 | } |
| 255 | 274 | // populate file fields |
| 256 | - foreach ($fileFields as $name => $file) { |
|
| 275 | + foreach ($fileFields as $name => $file) |
|
| 276 | + { |
|
| 257 | 277 | $data .= "--" . $delimiter . "\r\n"; |
| 258 | 278 | // "filename" attribute is not essential; server-side scripts may use it |
| 259 | 279 | $data .= 'Content-Disposition: form-data; name="' . urlencode($name) . '";' . |
@@ -479,7 +499,8 @@ discard block |
||
| 479 | 499 | } |
| 480 | 500 | } |
| 481 | 501 | |
| 482 | -class fritzbox_api_config { |
|
| 502 | +class fritzbox_api_config |
|
| 503 | +{ |
|
| 483 | 504 | protected $config = array(); |
| 484 | 505 | |
| 485 | 506 | public function __construct() |
@@ -94,7 +94,7 @@ |
||
| 94 | 94 | preg_match('@name="guest_enabled"[^>]+(checked)[^>]*@', $output, $matches); |
| 95 | 95 | if ( isset($matches[1]) && $mode == true ) |
| 96 | 96 | { |
| 97 | - $message .= 'LAN4 guest access is now active.'; |
|
| 97 | + $message .= 'LAN4 guest access is now active.'; |
|
| 98 | 98 | } |
| 99 | 99 | else if ( !isset($matches[1]) && $mode == false ) |
| 100 | 100 | { |
@@ -23,13 +23,13 @@ discard block |
||
| 23 | 23 | $message = date('Y-m-d H:i') . ' '; |
| 24 | 24 | |
| 25 | 25 | // handle the CLI arguments or give a help message |
| 26 | - if (isset($argv[1]) && ($argv[1] == 0 || $argv[1] == 1) ) |
|
| 26 | + if(isset($argv[1]) && ($argv[1] == 0 || $argv[1] == 1)) |
|
| 27 | 27 | { |
| 28 | 28 | $mode = (bool)$argv[1]; |
| 29 | 29 | } |
| 30 | 30 | else |
| 31 | 31 | { |
| 32 | - if ( $fritz->config->getItem('logging') == 'console' ) |
|
| 32 | + if($fritz->config->getItem('logging') == 'console') |
|
| 33 | 33 | { |
| 34 | 34 | echo ' |
| 35 | 35 | Enables or disables the LAN4 guest access of a Fritz!Box |
@@ -61,7 +61,7 @@ discard block |
||
| 61 | 61 | |
| 62 | 62 | // read time_server_activ setting |
| 63 | 63 | preg_match('@name="time_server_activ"[^>]+(checked)[^>]*@', $output, $matches); |
| 64 | - if ( isset($matches[1]) ) |
|
| 64 | + if(isset($matches[1])) |
|
| 65 | 65 | { |
| 66 | 66 | $formfields['time_server_activ'] = 'on'; |
| 67 | 67 | } |
@@ -70,19 +70,19 @@ discard block |
||
| 70 | 70 | $formfields['time_server'] = isset($matches[1]) ? $matches[1] : '0.europe.pool.ntp.org'; |
| 71 | 71 | // read other_prefix_allowed setting |
| 72 | 72 | preg_match('@name="other_prefix_allowed"[^>]+(checked)[^>]*@', $output, $matches); |
| 73 | - if ( isset($matches[1]) ) |
|
| 73 | + if(isset($matches[1])) |
|
| 74 | 74 | { |
| 75 | 75 | $formfields['other_prefix_allowed'] = 'on'; |
| 76 | 76 | } |
| 77 | 77 | // read dnsv6_server_activ setting |
| 78 | 78 | preg_match('@name="dnsv6_server_activ"[^>]+(checked)[^>]*@', $output, $matches); |
| 79 | - if ( isset($matches[1]) ) |
|
| 79 | + if(isset($matches[1])) |
|
| 80 | 80 | { |
| 81 | 81 | $formfields['dnsv6_server_activ'] = 'on'; |
| 82 | 82 | } |
| 83 | 83 | |
| 84 | 84 | // set new given setting |
| 85 | - if ( $mode == true ) |
|
| 85 | + if($mode == true) |
|
| 86 | 86 | { |
| 87 | 87 | $formfields['guest_enabled'] = 'on'; |
| 88 | 88 | } |
@@ -92,30 +92,30 @@ discard block |
||
| 92 | 92 | $output = $fritz->doPostForm($formfields); |
| 93 | 93 | |
| 94 | 94 | preg_match('@name="guest_enabled"[^>]+(checked)[^>]*@', $output, $matches); |
| 95 | - if ( isset($matches[1]) && $mode == true ) |
|
| 95 | + if(isset($matches[1]) && $mode == true) |
|
| 96 | 96 | { |
| 97 | 97 | $message .= 'LAN4 guest access is now active.'; |
| 98 | 98 | } |
| 99 | - else if ( !isset($matches[1]) && $mode == false ) |
|
| 99 | + else if(!isset($matches[1]) && $mode == false) |
|
| 100 | 100 | { |
| 101 | 101 | $message .= 'LAN4 guest access is now inactive.'; |
| 102 | 102 | } |
| 103 | - else if ( isset($matches[1]) && $mode == false ) |
|
| 103 | + else if(isset($matches[1]) && $mode == false) |
|
| 104 | 104 | { |
| 105 | 105 | $message .= 'ERROR: LAN4 guest access status change failed, should be inactive, but is still active.'; |
| 106 | 106 | } |
| 107 | - else if ( !isset($matches[1]) && $mode == true ) |
|
| 107 | + else if(!isset($matches[1]) && $mode == true) |
|
| 108 | 108 | { |
| 109 | 109 | $message .= 'ERROR: LAN4 guest access status change failed, should be active, but is still inactive.'; |
| 110 | 110 | } |
| 111 | 111 | } |
| 112 | -catch (Exception $e) |
|
| 112 | +catch(Exception $e) |
|
| 113 | 113 | { |
| 114 | 114 | $message .= $e->getMessage(); |
| 115 | 115 | } |
| 116 | 116 | |
| 117 | 117 | // log the result |
| 118 | -if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' ) |
|
| 118 | +if(isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api') |
|
| 119 | 119 | { |
| 120 | 120 | $fritz->logMessage($message); |
| 121 | 121 | } |
@@ -21,11 +21,11 @@ |
||
| 21 | 21 | |
| 22 | 22 | // delete calllist form fields |
| 23 | 23 | $formfields = array( |
| 24 | - 'getpage' => '/fon_num/foncalls_list.lua', |
|
| 25 | - 'usejournal' => '1', |
|
| 26 | - 'callstab' => 'all', |
|
| 27 | - 'submit' => 'clear', |
|
| 28 | - 'clear' => '1', |
|
| 24 | + 'getpage' => '/fon_num/foncalls_list.lua', |
|
| 25 | + 'usejournal' => '1', |
|
| 26 | + 'callstab' => 'all', |
|
| 27 | + 'submit' => 'clear', |
|
| 28 | + 'clear' => '1', |
|
| 29 | 29 | ); |
| 30 | 30 | |
| 31 | 31 | $fritz->doPostForm($formfields); |
@@ -38,13 +38,13 @@ |
||
| 38 | 38 | // this is based on 1 => 1, 2 => 10, 3 => 11, 4 => 100, 5 => 101, 6 => 110, 7 => 111 |
| 39 | 39 | $fritz->doPostForm($formfields); |
| 40 | 40 | } |
| 41 | -catch (Exception $e) |
|
| 41 | +catch(Exception $e) |
|
| 42 | 42 | { |
| 43 | 43 | $message .= $e->getMessage(); |
| 44 | 44 | } |
| 45 | 45 | |
| 46 | 46 | // log the result |
| 47 | -if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' ) |
|
| 47 | +if(isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api') |
|
| 48 | 48 | { |
| 49 | 49 | $fritz->logMessage($message); |
| 50 | 50 | } |
@@ -106,11 +106,11 @@ |
||
| 106 | 106 | preg_match('@name="activate_guest_access"[^>]+(checked)[^>]*@', $output, $matches); |
| 107 | 107 | if ( isset($matches[1]) && $mode == true ) |
| 108 | 108 | { |
| 109 | - preg_match('@name="wpa_key"[^>]+value="([^"]*)"[^>]*@', $output, $matches); |
|
| 110 | - if ( isset($matches[1]) ) |
|
| 111 | - { |
|
| 112 | - $message .= 'WLAN guest access is now active. WPA-Key is "' . $matches[1] . '"'; |
|
| 113 | - } |
|
| 109 | + preg_match('@name="wpa_key"[^>]+value="([^"]*)"[^>]*@', $output, $matches); |
|
| 110 | + if ( isset($matches[1]) ) |
|
| 111 | + { |
|
| 112 | + $message .= 'WLAN guest access is now active. WPA-Key is "' . $matches[1] . '"'; |
|
| 113 | + } |
|
| 114 | 114 | } |
| 115 | 115 | else if ( !isset($matches[1]) && $mode == false ) |
| 116 | 116 | { |
@@ -22,13 +22,13 @@ discard block |
||
| 22 | 22 | $message = date('Y-m-d H:i') . ' '; |
| 23 | 23 | |
| 24 | 24 | // handle the CLI arguments or give a help message |
| 25 | - if (isset($argv[1]) && ($argv[1] == 0 || $argv[1] == 1) ) |
|
| 25 | + if(isset($argv[1]) && ($argv[1] == 0 || $argv[1] == 1)) |
|
| 26 | 26 | { |
| 27 | 27 | $mode = (bool)$argv[1]; |
| 28 | 28 | } |
| 29 | 29 | else |
| 30 | 30 | { |
| 31 | - if ( $fritz->config->getItem('logging') == 'console' ) |
|
| 31 | + if($fritz->config->getItem('logging') == 'console') |
|
| 32 | 32 | { |
| 33 | 33 | echo ' |
| 34 | 34 | Enables or disables the WLAN guest access of a Fritz!Box |
@@ -63,7 +63,7 @@ discard block |
||
| 63 | 63 | |
| 64 | 64 | // read down_time_activ setting |
| 65 | 65 | preg_match('@name="down_time_activ"[^>]+(checked)[^>]*@', $output, $matches); |
| 66 | - if ( isset($matches[1]) ) |
|
| 66 | + if(isset($matches[1])) |
|
| 67 | 67 | { |
| 68 | 68 | $formfields['down_time_activ'] = 'on'; |
| 69 | 69 | } |
@@ -72,7 +72,7 @@ discard block |
||
| 72 | 72 | $formfields['down_time_value'] = isset($matches[1]) ? $matches[1] : '15'; |
| 73 | 73 | // read disconnect_guest_access setting |
| 74 | 74 | preg_match('@name="disconnect_guest_access"[^>]+(checked)[^>]*@', $output, $matches); |
| 75 | - if ( isset($matches[1]) ) |
|
| 75 | + if(isset($matches[1])) |
|
| 76 | 76 | { |
| 77 | 77 | $formfields['disconnect_guest_access'] = 'on'; |
| 78 | 78 | } |
@@ -90,10 +90,10 @@ discard block |
||
| 90 | 90 | $formfields['wpa_modus'] = isset($matches[1]) ? $matches[1] : 'x'; |
| 91 | 91 | |
| 92 | 92 | // set new given setting |
| 93 | - if ( $mode == true ) |
|
| 93 | + if($mode == true) |
|
| 94 | 94 | { |
| 95 | 95 | $formfields['activate_guest_access'] = 'on'; |
| 96 | - if ( $wpa_key !== false ) |
|
| 96 | + if($wpa_key !== false) |
|
| 97 | 97 | { |
| 98 | 98 | $formfields['wpa_key'] = $wpa_key; |
| 99 | 99 | } |
@@ -104,34 +104,34 @@ discard block |
||
| 104 | 104 | $output = $fritz->doPostForm($formfields); |
| 105 | 105 | |
| 106 | 106 | preg_match('@name="activate_guest_access"[^>]+(checked)[^>]*@', $output, $matches); |
| 107 | - if ( isset($matches[1]) && $mode == true ) |
|
| 107 | + if(isset($matches[1]) && $mode == true) |
|
| 108 | 108 | { |
| 109 | 109 | preg_match('@name="wpa_key"[^>]+value="([^"]*)"[^>]*@', $output, $matches); |
| 110 | - if ( isset($matches[1]) ) |
|
| 110 | + if(isset($matches[1])) |
|
| 111 | 111 | { |
| 112 | 112 | $message .= 'WLAN guest access is now active. WPA-Key is "' . $matches[1] . '"'; |
| 113 | 113 | } |
| 114 | 114 | } |
| 115 | - else if ( !isset($matches[1]) && $mode == false ) |
|
| 115 | + else if(!isset($matches[1]) && $mode == false) |
|
| 116 | 116 | { |
| 117 | 117 | $message .= 'WLAN guest access is now inactive.'; |
| 118 | 118 | } |
| 119 | - else if ( isset($matches[1]) && $mode == false ) |
|
| 119 | + else if(isset($matches[1]) && $mode == false) |
|
| 120 | 120 | { |
| 121 | 121 | $message .= 'ERROR: WLAN guest access status change failed, should be inactive, but is still active.'; |
| 122 | 122 | } |
| 123 | - else if ( !isset($matches[1]) && $mode == true ) |
|
| 123 | + else if(!isset($matches[1]) && $mode == true) |
|
| 124 | 124 | { |
| 125 | 125 | $message .= 'ERROR: WLAN guest access status change failed, should be active, but is still inactive.'; |
| 126 | 126 | } |
| 127 | 127 | } |
| 128 | -catch (Exception $e) |
|
| 128 | +catch(Exception $e) |
|
| 129 | 129 | { |
| 130 | 130 | $message .= $e->getMessage(); |
| 131 | 131 | } |
| 132 | 132 | |
| 133 | 133 | // log the result |
| 134 | -if ( isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api' ) |
|
| 134 | +if(isset($fritz) && is_object($fritz) && get_class($fritz) == 'fritzbox_api') |
|
| 135 | 135 | { |
| 136 | 136 | $fritz->logMessage($message); |
| 137 | 137 | } |
@@ -7,686 +7,686 @@ |
||
| 7 | 7 | * @see RFC 2426, RFC 2425 |
| 8 | 8 | * @version 0.4.8 |
| 9 | 9 | */ |
| 10 | - class vCard implements Countable, Iterator |
|
| 11 | - { |
|
| 12 | - const MODE_ERROR = 'error'; |
|
| 13 | - const MODE_SINGLE = 'single'; |
|
| 14 | - const MODE_MULTIPLE = 'multiple'; |
|
| 15 | - |
|
| 16 | - const endl = "\n"; |
|
| 17 | - |
|
| 18 | - /** |
|
| 19 | - * @var string Current object mode - error, single or multiple (for a single vCard within a file and multiple combined vCards) |
|
| 20 | - */ |
|
| 21 | - private $Mode; //single, multiple, error |
|
| 22 | - |
|
| 23 | - private $Path = ''; |
|
| 24 | - private $RawData = ''; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var array Internal options container. Options: |
|
| 28 | - * bool Collapse: If true, elements that can have multiple values but have only a single value are returned as that value instead of an array |
|
| 29 | - * If false, an array is returned even if it has only one value. |
|
| 30 | - */ |
|
| 31 | - private $Options = array( |
|
| 32 | - 'Collapse' => false |
|
| 33 | - ); |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var array Internal data container. Contains vCard objects for multiple vCards and just the data for single vCards. |
|
| 37 | - */ |
|
| 38 | - private $Data = array(); |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @static Parts of structured elements according to the spec. |
|
| 42 | - */ |
|
| 43 | - private static $Spec_StructuredElements = array( |
|
| 44 | - 'n' => array('lastname', 'firstname', 'additionalnames', 'prefixes', 'suffixes'), |
|
| 45 | - 'adr' => array('pobox', 'extendedaddress', 'streetaddress', 'locality', 'region', 'postalcode', 'country'), |
|
| 46 | - 'geo' => array('latitude', 'longitude'), |
|
| 47 | - 'org' => array('name', 'unit1', 'unit2') |
|
| 48 | - ); |
|
| 49 | - private static $Spec_MultipleValueElements = array('nickname', 'categories'); |
|
| 50 | - |
|
| 51 | - private static $Spec_ElementTypes = array( |
|
| 52 | - 'email' => array('internet', 'x400', 'pref', 'home', 'work'), |
|
| 53 | - 'adr' => array('dom', 'intl', 'postal', 'parcel', 'home', 'work', 'pref'), |
|
| 54 | - 'label' => array('dom', 'intl', 'postal', 'parcel', 'home', 'work', 'pref'), |
|
| 55 | - 'tel' => array('home', 'msg', 'work', 'pref', 'voice', 'fax', 'cell', 'video', 'pager', 'bbs', 'modem', 'car', 'isdn', 'pcs'), |
|
| 56 | - 'impp' => array('personal', 'business', 'home', 'work', 'mobile', 'pref') |
|
| 57 | - ); |
|
| 58 | - |
|
| 59 | - private static $Spec_FileElements = array('photo', 'logo', 'sound'); |
|
| 60 | - |
|
| 61 | - /** |
|
| 62 | - * vCard constructor |
|
| 63 | - * |
|
| 64 | - * @param string Path to file, optional. |
|
| 65 | - * @param string Raw data, optional. |
|
| 66 | - * @param array Additional options, optional. Currently supported options: |
|
| 67 | - * bool Collapse: If true, elements that can have multiple values but have only a single value are returned as that value instead of an array |
|
| 68 | - * If false, an array is returned even if it has only one value. |
|
| 69 | - * |
|
| 70 | - * One of these parameters must be provided, otherwise an exception is thrown. |
|
| 71 | - */ |
|
| 72 | - public function __construct($Path = false, $RawData = false, array $Options = null) |
|
| 73 | - { |
|
| 74 | - // Checking preconditions for the parser. |
|
| 75 | - // If path is given, the file should be accessible. |
|
| 76 | - // If raw data is given, it is taken as it is. |
|
| 77 | - // In both cases the real content is put in $this -> RawData |
|
| 78 | - if ($Path) |
|
| 79 | - { |
|
| 80 | - if (!is_readable($Path)) |
|
| 81 | - { |
|
| 82 | - throw new Exception('vCard: Path not accessible ('.$Path.')'); |
|
| 83 | - } |
|
| 84 | - |
|
| 85 | - $this -> Path = $Path; |
|
| 86 | - $this -> RawData = file_get_contents($this -> Path); |
|
| 87 | - } |
|
| 88 | - elseif ($RawData) |
|
| 89 | - { |
|
| 90 | - $this -> RawData = $RawData; |
|
| 91 | - } |
|
| 92 | - else |
|
| 93 | - { |
|
| 94 | - //throw new Exception('vCard: No content provided'); |
|
| 95 | - // Not necessary anymore as possibility to create vCards is added |
|
| 96 | - } |
|
| 97 | - |
|
| 98 | - if (!$this -> Path && !$this -> RawData) |
|
| 99 | - { |
|
| 100 | - return true; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - if ($Options) |
|
| 104 | - { |
|
| 105 | - $this -> Options = array_merge($this -> Options, $Options); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - // Counting the begin/end separators. If there aren't any or the count doesn't match, there is a problem with the file. |
|
| 109 | - // If there is only one, this is a single vCard, if more, multiple vCards are combined. |
|
| 110 | - $Matches = array(); |
|
| 111 | - $vCardBeginCount = preg_match_all('{^BEGIN\:VCARD}miS', $this -> RawData, $Matches); |
|
| 112 | - $vCardEndCount = preg_match_all('{^END\:VCARD}miS', $this -> RawData, $Matches); |
|
| 113 | - |
|
| 114 | - if (($vCardBeginCount != $vCardEndCount) || !$vCardBeginCount) |
|
| 115 | - { |
|
| 116 | - $this -> Mode = vCard::MODE_ERROR; |
|
| 117 | - throw new Exception('vCard: invalid vCard'); |
|
| 118 | - } |
|
| 119 | - |
|
| 120 | - $this -> Mode = $vCardBeginCount == 1 ? vCard::MODE_SINGLE : vCard::MODE_MULTIPLE; |
|
| 121 | - |
|
| 122 | - // Removing/changing inappropriate newlines, i.e., all CRs or multiple newlines are changed to a single newline |
|
| 123 | - $this -> RawData = str_replace("\r", "\n", $this -> RawData); |
|
| 124 | - $this -> RawData = preg_replace('{(\n+)}', "\n", $this -> RawData); |
|
| 125 | - |
|
| 126 | - // In multiple card mode the raw text is split at card beginning markers and each |
|
| 127 | - // fragment is parsed in a separate vCard object. |
|
| 128 | - if ($this -> Mode == self::MODE_MULTIPLE) |
|
| 129 | - { |
|
| 130 | - $this -> RawData = explode('BEGIN:VCARD', $this -> RawData); |
|
| 131 | - $this -> RawData = array_filter($this -> RawData); |
|
| 132 | - |
|
| 133 | - foreach ($this -> RawData as $SinglevCardRawData) |
|
| 134 | - { |
|
| 135 | - // Prepending "BEGIN:VCARD" to the raw string because we exploded on that one. |
|
| 136 | - // If there won't be the BEGIN marker in the new object, it will fail. |
|
| 137 | - $SinglevCardRawData = 'BEGIN:VCARD'."\n".$SinglevCardRawData; |
|
| 138 | - |
|
| 139 | - $ClassName = get_class($this); |
|
| 140 | - $this -> Data[] = new $ClassName(false, $SinglevCardRawData); |
|
| 141 | - } |
|
| 142 | - } |
|
| 143 | - else |
|
| 144 | - { |
|
| 145 | - // Protect the BASE64 final = sign (detected by the line beginning with whitespace), otherwise the next replace will get rid of it |
|
| 146 | - $this -> RawData = preg_replace('{(\n\s.+)=(\n)}', '$1-base64=-$2', $this -> RawData); |
|
| 147 | - |
|
| 148 | - // Joining multiple lines that are split with a hard wrap and indicated by an equals sign at the end of line |
|
| 149 | - // (quoted-printable-encoded values in v2.1 vCards) |
|
| 150 | - $this -> RawData = str_replace("=\n", '', $this -> RawData); |
|
| 151 | - |
|
| 152 | - // Joining multiple lines that are split with a soft wrap (space or tab on the beginning of the next line |
|
| 153 | - $this -> RawData = str_replace(array("\n ", "\n\t"), '-wrap-', $this -> RawData); |
|
| 154 | - |
|
| 155 | - // Restoring the BASE64 final equals sign (see a few lines above) |
|
| 156 | - $this -> RawData = str_replace("-base64=-\n", "=\n", $this -> RawData); |
|
| 157 | - |
|
| 158 | - $Lines = explode("\n", $this -> RawData); |
|
| 159 | - |
|
| 160 | - foreach ($Lines as $Line) |
|
| 161 | - { |
|
| 162 | - // Lines without colons are skipped because, most likely, they contain no data. |
|
| 163 | - if (strpos($Line, ':') === false) |
|
| 164 | - { |
|
| 165 | - continue; |
|
| 166 | - } |
|
| 167 | - |
|
| 168 | - // Each line is split into two parts. The key contains the element name and additional parameters, if present, |
|
| 169 | - // value is just the value |
|
| 170 | - list($Key, $Value) = explode(':', $Line, 2); |
|
| 171 | - |
|
| 172 | - // Key is transformed to lowercase because, even though the element and parameter names are written in uppercase, |
|
| 173 | - // it is quite possible that they will be in lower- or mixed case. |
|
| 174 | - // The key is trimmed to allow for non-significant WSP characters as allowed by v2.1 |
|
| 175 | - $Key = strtolower(trim(self::Unescape($Key))); |
|
| 176 | - |
|
| 177 | - // These two lines can be skipped as they aren't necessary at all. |
|
| 178 | - if ($Key == 'begin' || $Key == 'end') |
|
| 179 | - { |
|
| 180 | - continue; |
|
| 181 | - } |
|
| 182 | - |
|
| 183 | - if ((strpos($Key, 'agent') === 0) && (stripos($Value, 'begin:vcard') !== false)) |
|
| 184 | - { |
|
| 185 | - $ClassName = get_class($this); |
|
| 186 | - $Value = new $ClassName(false, str_replace('-wrap-', "\n", $Value)); |
|
| 187 | - if (!isset($this -> Data[$Key])) |
|
| 188 | - { |
|
| 189 | - $this -> Data[$Key] = array(); |
|
| 190 | - } |
|
| 191 | - $this -> Data[$Key][] = $Value; |
|
| 192 | - continue; |
|
| 193 | - } |
|
| 194 | - else |
|
| 195 | - { |
|
| 196 | - $Value = str_replace('-wrap-', '', $Value); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - $Value = trim(self::Unescape($Value)); |
|
| 200 | - $Type = array(); |
|
| 201 | - |
|
| 202 | - // Here additional parameters are parsed |
|
| 203 | - $KeyParts = explode(';', $Key); |
|
| 204 | - $Key = $KeyParts[0]; |
|
| 205 | - $Encoding = false; |
|
| 206 | - |
|
| 207 | - if (strpos($Key, 'item') === 0) |
|
| 208 | - { |
|
| 209 | - $TmpKey = explode('.', $Key, 2); |
|
| 210 | - $Key = $TmpKey[1]; |
|
| 211 | - $ItemIndex = (int)str_ireplace('item', '', $TmpKey[0]); |
|
| 212 | - } |
|
| 213 | - |
|
| 214 | - if (count($KeyParts) > 1) |
|
| 215 | - { |
|
| 216 | - $Parameters = self::ParseParameters($Key, array_slice($KeyParts, 1)); |
|
| 217 | - |
|
| 218 | - foreach ($Parameters as $ParamKey => $ParamValue) |
|
| 219 | - { |
|
| 220 | - switch ($ParamKey) |
|
| 221 | - { |
|
| 222 | - case 'encoding': |
|
| 223 | - $Encoding = $ParamValue; |
|
| 224 | - if (in_array($ParamValue, array('b', 'base64'))) |
|
| 225 | - { |
|
| 226 | - //$Value = base64_decode($Value); |
|
| 227 | - } |
|
| 228 | - elseif ($ParamValue == 'quoted-printable') // v2.1 |
|
| 229 | - { |
|
| 230 | - $Value = quoted_printable_decode($Value); |
|
| 231 | - } |
|
| 232 | - break; |
|
| 233 | - case 'charset': // v2.1 |
|
| 234 | - if ($ParamValue != 'utf-8' && $ParamValue != 'utf8') |
|
| 235 | - { |
|
| 236 | - $Value = mb_convert_encoding($Value, 'UTF-8', $ParamValue); |
|
| 237 | - } |
|
| 238 | - break; |
|
| 239 | - case 'type': |
|
| 240 | - $Type = $ParamValue; |
|
| 241 | - break; |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - } |
|
| 245 | - |
|
| 246 | - // Checking files for colon-separated additional parameters (Apple's Address Book does this), for example, "X-ABCROP-RECTANGLE" for photos |
|
| 247 | - if (in_array($Key, self::$Spec_FileElements) && isset($Parameters['encoding']) && in_array($Parameters['encoding'], array('b', 'base64'))) |
|
| 248 | - { |
|
| 249 | - // If colon is present in the value, it must contain Address Book parameters |
|
| 250 | - // (colon is an invalid character for base64 so it shouldn't appear in valid files) |
|
| 251 | - if (strpos($Value, ':') !== false) |
|
| 252 | - { |
|
| 253 | - $Value = explode(':', $Value); |
|
| 254 | - $Value = array_pop($Value); |
|
| 255 | - } |
|
| 256 | - } |
|
| 257 | - |
|
| 258 | - // Values are parsed according to their type |
|
| 259 | - if (isset(self::$Spec_StructuredElements[$Key])) |
|
| 260 | - { |
|
| 261 | - $Value = self::ParseStructuredValue($Value, $Key); |
|
| 262 | - if ($Type) |
|
| 263 | - { |
|
| 264 | - $Value['type'] = $Type; |
|
| 265 | - } |
|
| 266 | - } |
|
| 267 | - else |
|
| 268 | - { |
|
| 269 | - if (in_array($Key, self::$Spec_MultipleValueElements)) |
|
| 270 | - { |
|
| 271 | - $Value = self::ParseMultipleTextValue($Value, $Key); |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - if ($Type) |
|
| 275 | - { |
|
| 276 | - $Value = array( |
|
| 277 | - 'value' => $Value, |
|
| 278 | - 'type' => $Type |
|
| 279 | - ); |
|
| 280 | - } |
|
| 281 | - } |
|
| 282 | - |
|
| 283 | - if (is_array($Value) && $Encoding) |
|
| 284 | - { |
|
| 285 | - $Value['encoding'] = $Encoding; |
|
| 286 | - } |
|
| 287 | - |
|
| 288 | - if (!isset($this -> Data[$Key])) |
|
| 289 | - { |
|
| 290 | - $this -> Data[$Key] = array(); |
|
| 291 | - } |
|
| 292 | - |
|
| 293 | - $this -> Data[$Key][] = $Value; |
|
| 294 | - } |
|
| 295 | - } |
|
| 296 | - } |
|
| 297 | - |
|
| 298 | - /** |
|
| 299 | - * Magic method to get the various vCard values as object members, e.g. |
|
| 300 | - * a call to $vCard -> N gets the "N" value |
|
| 301 | - * |
|
| 302 | - * @param string Key |
|
| 303 | - * |
|
| 304 | - * @return mixed Value |
|
| 305 | - */ |
|
| 306 | - public function __get($Key) |
|
| 307 | - { |
|
| 308 | - $Key = strtolower($Key); |
|
| 309 | - if (isset($this -> Data[$Key])) |
|
| 310 | - { |
|
| 311 | - if ($Key == 'agent') |
|
| 312 | - { |
|
| 313 | - return $this -> Data[$Key]; |
|
| 314 | - } |
|
| 315 | - elseif (in_array($Key, self::$Spec_FileElements)) |
|
| 316 | - { |
|
| 317 | - $Value = $this -> Data[$Key]; |
|
| 318 | - foreach ($Value as $K => $V) |
|
| 319 | - { |
|
| 320 | - if (stripos($V['value'], 'uri:') === 0) |
|
| 321 | - { |
|
| 322 | - $Value[$K]['value'] = substr($V, 4); |
|
| 323 | - $Value[$K]['encoding'] = 'uri'; |
|
| 324 | - } |
|
| 325 | - } |
|
| 326 | - return $Value; |
|
| 327 | - } |
|
| 328 | - |
|
| 329 | - if ($this -> Options['Collapse'] && is_array($this -> Data[$Key]) && (count($this -> Data[$Key]) == 1)) |
|
| 330 | - { |
|
| 331 | - return $this -> Data[$Key][0]; |
|
| 332 | - } |
|
| 333 | - return $this -> Data[$Key]; |
|
| 334 | - } |
|
| 335 | - elseif ($Key == 'Mode') |
|
| 336 | - { |
|
| 337 | - return $this -> Mode; |
|
| 338 | - } |
|
| 339 | - return array(); |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Saves an embedded file |
|
| 344 | - * |
|
| 345 | - * @param string Key |
|
| 346 | - * @param int Index of the file, defaults to 0 |
|
| 347 | - * @param string Target path where the file should be saved, including the filename |
|
| 348 | - * |
|
| 349 | - * @return bool Operation status |
|
| 350 | - */ |
|
| 351 | - public function SaveFile($Key, $Index = 0, $TargetPath = '') |
|
| 352 | - { |
|
| 353 | - if (!isset($this -> Data[$Key])) |
|
| 354 | - { |
|
| 355 | - return false; |
|
| 356 | - } |
|
| 357 | - if (!isset($this -> Data[$Key][$Index])) |
|
| 358 | - { |
|
| 359 | - return false; |
|
| 360 | - } |
|
| 361 | - |
|
| 362 | - // Returing false if it is an image URL |
|
| 363 | - if (stripos($this -> Data[$Key][$Index]['value'], 'uri:') === 0) |
|
| 364 | - { |
|
| 365 | - return false; |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - if (is_writable($TargetPath) || (!file_exists($TargetPath) && is_writable(dirname($TargetPath)))) |
|
| 369 | - { |
|
| 370 | - $RawContent = $this -> Data[$Key][$Index]['value']; |
|
| 371 | - if (isset($this -> Data[$Key][$Index]['encoding']) && $this -> Data[$Key][$Index]['encoding'] == 'b') |
|
| 372 | - { |
|
| 373 | - $RawContent = base64_decode($RawContent); |
|
| 374 | - } |
|
| 375 | - $Status = file_put_contents($TargetPath, $RawContent); |
|
| 376 | - return (bool)$Status; |
|
| 377 | - } |
|
| 378 | - else |
|
| 379 | - { |
|
| 380 | - throw new Exception('vCard: Cannot save file ('.$Key.'), target path not writable ('.$TargetPath.')'); |
|
| 381 | - } |
|
| 382 | - return false; |
|
| 383 | - } |
|
| 384 | - |
|
| 385 | - /** |
|
| 386 | - * Magic method for adding data to the vCard |
|
| 387 | - * |
|
| 388 | - * @param string Key |
|
| 389 | - * @param string Method call arguments. First element is value. |
|
| 390 | - * |
|
| 391 | - * @return vCard Current object for method chaining |
|
| 392 | - */ |
|
| 393 | - public function __call($Key, $Arguments) |
|
| 394 | - { |
|
| 395 | - $Key = strtolower($Key); |
|
| 396 | - |
|
| 397 | - if (!isset($this -> Data[$Key])) |
|
| 398 | - { |
|
| 399 | - $this -> Data[$Key] = array(); |
|
| 400 | - } |
|
| 401 | - |
|
| 402 | - $Value = isset($Arguments[0]) ? $Arguments[0] : false; |
|
| 403 | - |
|
| 404 | - if (count($Arguments) > 1) |
|
| 405 | - { |
|
| 406 | - $Types = array_map('strtolower', array_values(array_slice($Arguments, 1))); |
|
| 407 | - |
|
| 408 | - if (isset(self::$Spec_StructuredElements[$Key]) && |
|
| 409 | - in_array(strtolower($Arguments[1]), self::$Spec_StructuredElements[$Key]) |
|
| 410 | - ) |
|
| 411 | - { |
|
| 412 | - $LastElementIndex = 0; |
|
| 413 | - |
|
| 414 | - if (count($this -> Data[$Key])) |
|
| 415 | - { |
|
| 416 | - $LastElementIndex = count($this -> Data[$Key]) - 1; |
|
| 417 | - } |
|
| 418 | - |
|
| 419 | - if (isset($this -> Data[$Key][$LastElementIndex])) |
|
| 420 | - { |
|
| 421 | - if (empty($this -> Data[$Key][$LastElementIndex][$Types[0]])) |
|
| 422 | - { |
|
| 423 | - $this -> Data[$Key][$LastElementIndex][$Types[0]] = $Value; |
|
| 424 | - } |
|
| 425 | - else |
|
| 426 | - { |
|
| 427 | - $LastElementIndex++; |
|
| 428 | - } |
|
| 429 | - } |
|
| 430 | - |
|
| 431 | - if (!isset($this -> Data[$Key][$LastElementIndex])) |
|
| 432 | - { |
|
| 433 | - $this -> Data[$Key][$LastElementIndex] = array( |
|
| 434 | - $Types[0] => $Value |
|
| 435 | - ); |
|
| 436 | - } |
|
| 437 | - } |
|
| 438 | - elseif (isset(self::$Spec_ElementTypes[$Key])) |
|
| 439 | - { |
|
| 440 | - $this -> Data[$Key][] = array( |
|
| 441 | - 'value' => $Value, |
|
| 442 | - 'type' => $Types |
|
| 443 | - ); |
|
| 444 | - } |
|
| 445 | - } |
|
| 446 | - elseif ($Value) |
|
| 447 | - { |
|
| 448 | - $this -> Data[$Key][] = $Value; |
|
| 449 | - } |
|
| 450 | - |
|
| 451 | - return $this; |
|
| 452 | - } |
|
| 453 | - |
|
| 454 | - /** |
|
| 455 | - * Magic method for getting vCard content out |
|
| 456 | - * |
|
| 457 | - * @return string Raw vCard content |
|
| 458 | - */ |
|
| 459 | - public function __toString() |
|
| 460 | - { |
|
| 461 | - $Text = 'BEGIN:VCARD'.self::endl; |
|
| 462 | - $Text .= 'VERSION:3.0'.self::endl; |
|
| 463 | - |
|
| 464 | - foreach ($this -> Data as $Key => $Values) |
|
| 465 | - { |
|
| 466 | - $KeyUC = strtoupper($Key); |
|
| 467 | - $Key = strtolower($Key); |
|
| 468 | - |
|
| 469 | - if (in_array($KeyUC, array('PHOTO', 'VERSION'))) |
|
| 470 | - { |
|
| 471 | - continue; |
|
| 472 | - } |
|
| 473 | - |
|
| 474 | - foreach ($Values as $Index => $Value) |
|
| 475 | - { |
|
| 476 | - $Text .= $KeyUC; |
|
| 477 | - if (is_array($Value) && isset($Value['type'])) |
|
| 478 | - { |
|
| 479 | - $Text .= ';TYPE='.self::PrepareTypeStrForOutput($Value['type']); |
|
| 480 | - } |
|
| 481 | - $Text .= ':'; |
|
| 482 | - |
|
| 483 | - if (isset(self::$Spec_StructuredElements[$Key])) |
|
| 484 | - { |
|
| 485 | - $PartArray = array(); |
|
| 486 | - foreach (self::$Spec_StructuredElements[$Key] as $Part) |
|
| 487 | - { |
|
| 488 | - $PartArray[] = isset($Value[$Part]) ? $Value[$Part] : ''; |
|
| 489 | - } |
|
| 490 | - $Text .= implode(';', $PartArray); |
|
| 491 | - } |
|
| 492 | - elseif (is_array($Value) && isset(self::$Spec_ElementTypes[$Key])) |
|
| 493 | - { |
|
| 494 | - $Text .= $Value['value']; |
|
| 495 | - } |
|
| 496 | - else |
|
| 497 | - { |
|
| 498 | - $Text .= $Value; |
|
| 499 | - } |
|
| 500 | - |
|
| 501 | - $Text .= self::endl; |
|
| 502 | - } |
|
| 503 | - } |
|
| 504 | - |
|
| 505 | - $Text .= 'END:VCARD'.self::endl; |
|
| 506 | - return $Text; |
|
| 507 | - } |
|
| 508 | - |
|
| 509 | - // !Helper methods |
|
| 510 | - |
|
| 511 | - private static function PrepareTypeStrForOutput($Type) |
|
| 512 | - { |
|
| 513 | - return implode(',', array_map('strtoupper', $Type)); |
|
| 514 | - } |
|
| 515 | - |
|
| 516 | - /** |
|
| 517 | - * Removes the escaping slashes from the text. |
|
| 518 | - * |
|
| 519 | - * @access private |
|
| 520 | - * |
|
| 521 | - * @param string Text to prepare. |
|
| 522 | - * |
|
| 523 | - * @return string Resulting text. |
|
| 524 | - */ |
|
| 525 | - private static function Unescape($Text) |
|
| 526 | - { |
|
| 527 | - return str_replace(array('\:', '\;', '\,', "\n"), array(':', ';', ',', ''), $Text); |
|
| 528 | - } |
|
| 529 | - |
|
| 530 | - /** |
|
| 531 | - * Separates the various parts of a structured value according to the spec. |
|
| 532 | - * |
|
| 533 | - * @access private |
|
| 534 | - * |
|
| 535 | - * @param string Raw text string |
|
| 536 | - * @param string Key (e.g., N, ADR, ORG, etc.) |
|
| 537 | - * |
|
| 538 | - * @return array Parts in an associative array. |
|
| 539 | - */ |
|
| 540 | - private static function ParseStructuredValue($Text, $Key) |
|
| 541 | - { |
|
| 542 | - $Text = array_map('trim', explode(';', $Text)); |
|
| 543 | - |
|
| 544 | - $Result = array(); |
|
| 545 | - $Ctr = 0; |
|
| 546 | - |
|
| 547 | - foreach (self::$Spec_StructuredElements[$Key] as $Index => $StructurePart) |
|
| 548 | - { |
|
| 549 | - $Result[$StructurePart] = isset($Text[$Index]) ? $Text[$Index] : null; |
|
| 550 | - } |
|
| 551 | - return $Result; |
|
| 552 | - } |
|
| 553 | - |
|
| 554 | - /** |
|
| 555 | - * @access private |
|
| 556 | - */ |
|
| 557 | - private static function ParseMultipleTextValue($Text) |
|
| 558 | - { |
|
| 559 | - return explode(',', $Text); |
|
| 560 | - } |
|
| 561 | - |
|
| 562 | - /** |
|
| 563 | - * @access private |
|
| 564 | - */ |
|
| 565 | - private static function ParseParameters($Key, array $RawParams = null) |
|
| 566 | - { |
|
| 567 | - if (!$RawParams) |
|
| 568 | - { |
|
| 569 | - return array(); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - // Parameters are split into (key, value) pairs |
|
| 573 | - $Parameters = array(); |
|
| 574 | - foreach ($RawParams as $Item) |
|
| 575 | - { |
|
| 576 | - $Parameters[] = explode('=', strtolower($Item)); |
|
| 577 | - } |
|
| 578 | - |
|
| 579 | - $Type = array(); |
|
| 580 | - $Result = array(); |
|
| 581 | - |
|
| 582 | - // And each parameter is checked whether anything can/should be done because of it |
|
| 583 | - foreach ($Parameters as $Index => $Parameter) |
|
| 584 | - { |
|
| 585 | - // Skipping empty elements |
|
| 586 | - if (!$Parameter) |
|
| 587 | - { |
|
| 588 | - continue; |
|
| 589 | - } |
|
| 590 | - |
|
| 591 | - // Handling type parameters without the explicit TYPE parameter name (2.1 valid) |
|
| 592 | - if (count($Parameter) == 1) |
|
| 593 | - { |
|
| 594 | - // Checks if the type value is allowed for the specific element |
|
| 595 | - // The second part of the "if" statement means that email elements can have non-standard types (see the spec) |
|
| 596 | - if ( |
|
| 597 | - (isset(self::$Spec_ElementTypes[$Key]) && in_array($Parameter[0], self::$Spec_ElementTypes[$Key])) || |
|
| 598 | - ($Key == 'email' && is_scalar($Parameter[0])) |
|
| 599 | - ) |
|
| 600 | - { |
|
| 601 | - $Type[] = $Parameter[0]; |
|
| 602 | - } |
|
| 603 | - } |
|
| 604 | - elseif (count($Parameter) > 2) |
|
| 605 | - { |
|
| 606 | - if(count(explode(',', $RawParams[$Index], -1)) > 0) |
|
| 607 | - { |
|
| 608 | - $TempTypeParams = self::ParseParameters($Key, explode(',', $RawParams[$Index])); |
|
| 609 | - if ($TempTypeParams['type']) |
|
| 610 | - { |
|
| 611 | - $Type = array_merge($Type, $TempTypeParams['type']); |
|
| 612 | - } |
|
| 613 | - } |
|
| 614 | - } |
|
| 615 | - else |
|
| 616 | - { |
|
| 617 | - switch ($Parameter[0]) |
|
| 618 | - { |
|
| 619 | - case 'encoding': |
|
| 620 | - if (in_array($Parameter[1], array('quoted-printable', 'b', 'base64'))) |
|
| 621 | - { |
|
| 622 | - $Result['encoding'] = $Parameter[1] == 'base64' ? 'b' : $Parameter[1]; |
|
| 623 | - } |
|
| 624 | - break; |
|
| 625 | - case 'charset': |
|
| 626 | - $Result['charset'] = $Parameter[1]; |
|
| 627 | - break; |
|
| 628 | - case 'type': |
|
| 629 | - $Type = array_merge($Type, explode(',', $Parameter[1])); |
|
| 630 | - break; |
|
| 631 | - case 'value': |
|
| 632 | - if (strtolower($Parameter[1]) == 'url') |
|
| 633 | - { |
|
| 634 | - $Result['encoding'] = 'uri'; |
|
| 635 | - } |
|
| 636 | - break; |
|
| 637 | - } |
|
| 638 | - } |
|
| 639 | - } |
|
| 640 | - |
|
| 641 | - $Result['type'] = $Type; |
|
| 642 | - |
|
| 643 | - return $Result; |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - // !Interface methods |
|
| 647 | - |
|
| 648 | - // Countable interface |
|
| 649 | - public function count() |
|
| 650 | - { |
|
| 651 | - switch ($this -> Mode) |
|
| 652 | - { |
|
| 653 | - case self::MODE_ERROR: |
|
| 654 | - return 0; |
|
| 655 | - break; |
|
| 656 | - case self::MODE_SINGLE: |
|
| 657 | - return 1; |
|
| 658 | - break; |
|
| 659 | - case self::MODE_MULTIPLE: |
|
| 660 | - return count($this -> Data); |
|
| 661 | - break; |
|
| 662 | - } |
|
| 663 | - return 0; |
|
| 664 | - } |
|
| 665 | - |
|
| 666 | - // Iterator interface |
|
| 667 | - public function rewind() |
|
| 668 | - { |
|
| 669 | - reset($this -> Data); |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - public function current() |
|
| 673 | - { |
|
| 674 | - return current($this -> Data); |
|
| 675 | - } |
|
| 676 | - |
|
| 677 | - public function next() |
|
| 678 | - { |
|
| 679 | - return next($this -> Data); |
|
| 680 | - } |
|
| 681 | - |
|
| 682 | - public function valid() |
|
| 683 | - { |
|
| 684 | - return ($this -> current() !== false); |
|
| 685 | - } |
|
| 686 | - |
|
| 687 | - public function key() |
|
| 688 | - { |
|
| 689 | - return key($this -> Data); |
|
| 690 | - } |
|
| 691 | - } |
|
| 10 | + class vCard implements Countable, Iterator |
|
| 11 | + { |
|
| 12 | + const MODE_ERROR = 'error'; |
|
| 13 | + const MODE_SINGLE = 'single'; |
|
| 14 | + const MODE_MULTIPLE = 'multiple'; |
|
| 15 | + |
|
| 16 | + const endl = "\n"; |
|
| 17 | + |
|
| 18 | + /** |
|
| 19 | + * @var string Current object mode - error, single or multiple (for a single vCard within a file and multiple combined vCards) |
|
| 20 | + */ |
|
| 21 | + private $Mode; //single, multiple, error |
|
| 22 | + |
|
| 23 | + private $Path = ''; |
|
| 24 | + private $RawData = ''; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var array Internal options container. Options: |
|
| 28 | + * bool Collapse: If true, elements that can have multiple values but have only a single value are returned as that value instead of an array |
|
| 29 | + * If false, an array is returned even if it has only one value. |
|
| 30 | + */ |
|
| 31 | + private $Options = array( |
|
| 32 | + 'Collapse' => false |
|
| 33 | + ); |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var array Internal data container. Contains vCard objects for multiple vCards and just the data for single vCards. |
|
| 37 | + */ |
|
| 38 | + private $Data = array(); |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @static Parts of structured elements according to the spec. |
|
| 42 | + */ |
|
| 43 | + private static $Spec_StructuredElements = array( |
|
| 44 | + 'n' => array('lastname', 'firstname', 'additionalnames', 'prefixes', 'suffixes'), |
|
| 45 | + 'adr' => array('pobox', 'extendedaddress', 'streetaddress', 'locality', 'region', 'postalcode', 'country'), |
|
| 46 | + 'geo' => array('latitude', 'longitude'), |
|
| 47 | + 'org' => array('name', 'unit1', 'unit2') |
|
| 48 | + ); |
|
| 49 | + private static $Spec_MultipleValueElements = array('nickname', 'categories'); |
|
| 50 | + |
|
| 51 | + private static $Spec_ElementTypes = array( |
|
| 52 | + 'email' => array('internet', 'x400', 'pref', 'home', 'work'), |
|
| 53 | + 'adr' => array('dom', 'intl', 'postal', 'parcel', 'home', 'work', 'pref'), |
|
| 54 | + 'label' => array('dom', 'intl', 'postal', 'parcel', 'home', 'work', 'pref'), |
|
| 55 | + 'tel' => array('home', 'msg', 'work', 'pref', 'voice', 'fax', 'cell', 'video', 'pager', 'bbs', 'modem', 'car', 'isdn', 'pcs'), |
|
| 56 | + 'impp' => array('personal', 'business', 'home', 'work', 'mobile', 'pref') |
|
| 57 | + ); |
|
| 58 | + |
|
| 59 | + private static $Spec_FileElements = array('photo', 'logo', 'sound'); |
|
| 60 | + |
|
| 61 | + /** |
|
| 62 | + * vCard constructor |
|
| 63 | + * |
|
| 64 | + * @param string Path to file, optional. |
|
| 65 | + * @param string Raw data, optional. |
|
| 66 | + * @param array Additional options, optional. Currently supported options: |
|
| 67 | + * bool Collapse: If true, elements that can have multiple values but have only a single value are returned as that value instead of an array |
|
| 68 | + * If false, an array is returned even if it has only one value. |
|
| 69 | + * |
|
| 70 | + * One of these parameters must be provided, otherwise an exception is thrown. |
|
| 71 | + */ |
|
| 72 | + public function __construct($Path = false, $RawData = false, array $Options = null) |
|
| 73 | + { |
|
| 74 | + // Checking preconditions for the parser. |
|
| 75 | + // If path is given, the file should be accessible. |
|
| 76 | + // If raw data is given, it is taken as it is. |
|
| 77 | + // In both cases the real content is put in $this -> RawData |
|
| 78 | + if ($Path) |
|
| 79 | + { |
|
| 80 | + if (!is_readable($Path)) |
|
| 81 | + { |
|
| 82 | + throw new Exception('vCard: Path not accessible ('.$Path.')'); |
|
| 83 | + } |
|
| 84 | + |
|
| 85 | + $this -> Path = $Path; |
|
| 86 | + $this -> RawData = file_get_contents($this -> Path); |
|
| 87 | + } |
|
| 88 | + elseif ($RawData) |
|
| 89 | + { |
|
| 90 | + $this -> RawData = $RawData; |
|
| 91 | + } |
|
| 92 | + else |
|
| 93 | + { |
|
| 94 | + //throw new Exception('vCard: No content provided'); |
|
| 95 | + // Not necessary anymore as possibility to create vCards is added |
|
| 96 | + } |
|
| 97 | + |
|
| 98 | + if (!$this -> Path && !$this -> RawData) |
|
| 99 | + { |
|
| 100 | + return true; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + if ($Options) |
|
| 104 | + { |
|
| 105 | + $this -> Options = array_merge($this -> Options, $Options); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + // Counting the begin/end separators. If there aren't any or the count doesn't match, there is a problem with the file. |
|
| 109 | + // If there is only one, this is a single vCard, if more, multiple vCards are combined. |
|
| 110 | + $Matches = array(); |
|
| 111 | + $vCardBeginCount = preg_match_all('{^BEGIN\:VCARD}miS', $this -> RawData, $Matches); |
|
| 112 | + $vCardEndCount = preg_match_all('{^END\:VCARD}miS', $this -> RawData, $Matches); |
|
| 113 | + |
|
| 114 | + if (($vCardBeginCount != $vCardEndCount) || !$vCardBeginCount) |
|
| 115 | + { |
|
| 116 | + $this -> Mode = vCard::MODE_ERROR; |
|
| 117 | + throw new Exception('vCard: invalid vCard'); |
|
| 118 | + } |
|
| 119 | + |
|
| 120 | + $this -> Mode = $vCardBeginCount == 1 ? vCard::MODE_SINGLE : vCard::MODE_MULTIPLE; |
|
| 121 | + |
|
| 122 | + // Removing/changing inappropriate newlines, i.e., all CRs or multiple newlines are changed to a single newline |
|
| 123 | + $this -> RawData = str_replace("\r", "\n", $this -> RawData); |
|
| 124 | + $this -> RawData = preg_replace('{(\n+)}', "\n", $this -> RawData); |
|
| 125 | + |
|
| 126 | + // In multiple card mode the raw text is split at card beginning markers and each |
|
| 127 | + // fragment is parsed in a separate vCard object. |
|
| 128 | + if ($this -> Mode == self::MODE_MULTIPLE) |
|
| 129 | + { |
|
| 130 | + $this -> RawData = explode('BEGIN:VCARD', $this -> RawData); |
|
| 131 | + $this -> RawData = array_filter($this -> RawData); |
|
| 132 | + |
|
| 133 | + foreach ($this -> RawData as $SinglevCardRawData) |
|
| 134 | + { |
|
| 135 | + // Prepending "BEGIN:VCARD" to the raw string because we exploded on that one. |
|
| 136 | + // If there won't be the BEGIN marker in the new object, it will fail. |
|
| 137 | + $SinglevCardRawData = 'BEGIN:VCARD'."\n".$SinglevCardRawData; |
|
| 138 | + |
|
| 139 | + $ClassName = get_class($this); |
|
| 140 | + $this -> Data[] = new $ClassName(false, $SinglevCardRawData); |
|
| 141 | + } |
|
| 142 | + } |
|
| 143 | + else |
|
| 144 | + { |
|
| 145 | + // Protect the BASE64 final = sign (detected by the line beginning with whitespace), otherwise the next replace will get rid of it |
|
| 146 | + $this -> RawData = preg_replace('{(\n\s.+)=(\n)}', '$1-base64=-$2', $this -> RawData); |
|
| 147 | + |
|
| 148 | + // Joining multiple lines that are split with a hard wrap and indicated by an equals sign at the end of line |
|
| 149 | + // (quoted-printable-encoded values in v2.1 vCards) |
|
| 150 | + $this -> RawData = str_replace("=\n", '', $this -> RawData); |
|
| 151 | + |
|
| 152 | + // Joining multiple lines that are split with a soft wrap (space or tab on the beginning of the next line |
|
| 153 | + $this -> RawData = str_replace(array("\n ", "\n\t"), '-wrap-', $this -> RawData); |
|
| 154 | + |
|
| 155 | + // Restoring the BASE64 final equals sign (see a few lines above) |
|
| 156 | + $this -> RawData = str_replace("-base64=-\n", "=\n", $this -> RawData); |
|
| 157 | + |
|
| 158 | + $Lines = explode("\n", $this -> RawData); |
|
| 159 | + |
|
| 160 | + foreach ($Lines as $Line) |
|
| 161 | + { |
|
| 162 | + // Lines without colons are skipped because, most likely, they contain no data. |
|
| 163 | + if (strpos($Line, ':') === false) |
|
| 164 | + { |
|
| 165 | + continue; |
|
| 166 | + } |
|
| 167 | + |
|
| 168 | + // Each line is split into two parts. The key contains the element name and additional parameters, if present, |
|
| 169 | + // value is just the value |
|
| 170 | + list($Key, $Value) = explode(':', $Line, 2); |
|
| 171 | + |
|
| 172 | + // Key is transformed to lowercase because, even though the element and parameter names are written in uppercase, |
|
| 173 | + // it is quite possible that they will be in lower- or mixed case. |
|
| 174 | + // The key is trimmed to allow for non-significant WSP characters as allowed by v2.1 |
|
| 175 | + $Key = strtolower(trim(self::Unescape($Key))); |
|
| 176 | + |
|
| 177 | + // These two lines can be skipped as they aren't necessary at all. |
|
| 178 | + if ($Key == 'begin' || $Key == 'end') |
|
| 179 | + { |
|
| 180 | + continue; |
|
| 181 | + } |
|
| 182 | + |
|
| 183 | + if ((strpos($Key, 'agent') === 0) && (stripos($Value, 'begin:vcard') !== false)) |
|
| 184 | + { |
|
| 185 | + $ClassName = get_class($this); |
|
| 186 | + $Value = new $ClassName(false, str_replace('-wrap-', "\n", $Value)); |
|
| 187 | + if (!isset($this -> Data[$Key])) |
|
| 188 | + { |
|
| 189 | + $this -> Data[$Key] = array(); |
|
| 190 | + } |
|
| 191 | + $this -> Data[$Key][] = $Value; |
|
| 192 | + continue; |
|
| 193 | + } |
|
| 194 | + else |
|
| 195 | + { |
|
| 196 | + $Value = str_replace('-wrap-', '', $Value); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + $Value = trim(self::Unescape($Value)); |
|
| 200 | + $Type = array(); |
|
| 201 | + |
|
| 202 | + // Here additional parameters are parsed |
|
| 203 | + $KeyParts = explode(';', $Key); |
|
| 204 | + $Key = $KeyParts[0]; |
|
| 205 | + $Encoding = false; |
|
| 206 | + |
|
| 207 | + if (strpos($Key, 'item') === 0) |
|
| 208 | + { |
|
| 209 | + $TmpKey = explode('.', $Key, 2); |
|
| 210 | + $Key = $TmpKey[1]; |
|
| 211 | + $ItemIndex = (int)str_ireplace('item', '', $TmpKey[0]); |
|
| 212 | + } |
|
| 213 | + |
|
| 214 | + if (count($KeyParts) > 1) |
|
| 215 | + { |
|
| 216 | + $Parameters = self::ParseParameters($Key, array_slice($KeyParts, 1)); |
|
| 217 | + |
|
| 218 | + foreach ($Parameters as $ParamKey => $ParamValue) |
|
| 219 | + { |
|
| 220 | + switch ($ParamKey) |
|
| 221 | + { |
|
| 222 | + case 'encoding': |
|
| 223 | + $Encoding = $ParamValue; |
|
| 224 | + if (in_array($ParamValue, array('b', 'base64'))) |
|
| 225 | + { |
|
| 226 | + //$Value = base64_decode($Value); |
|
| 227 | + } |
|
| 228 | + elseif ($ParamValue == 'quoted-printable') // v2.1 |
|
| 229 | + { |
|
| 230 | + $Value = quoted_printable_decode($Value); |
|
| 231 | + } |
|
| 232 | + break; |
|
| 233 | + case 'charset': // v2.1 |
|
| 234 | + if ($ParamValue != 'utf-8' && $ParamValue != 'utf8') |
|
| 235 | + { |
|
| 236 | + $Value = mb_convert_encoding($Value, 'UTF-8', $ParamValue); |
|
| 237 | + } |
|
| 238 | + break; |
|
| 239 | + case 'type': |
|
| 240 | + $Type = $ParamValue; |
|
| 241 | + break; |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + } |
|
| 245 | + |
|
| 246 | + // Checking files for colon-separated additional parameters (Apple's Address Book does this), for example, "X-ABCROP-RECTANGLE" for photos |
|
| 247 | + if (in_array($Key, self::$Spec_FileElements) && isset($Parameters['encoding']) && in_array($Parameters['encoding'], array('b', 'base64'))) |
|
| 248 | + { |
|
| 249 | + // If colon is present in the value, it must contain Address Book parameters |
|
| 250 | + // (colon is an invalid character for base64 so it shouldn't appear in valid files) |
|
| 251 | + if (strpos($Value, ':') !== false) |
|
| 252 | + { |
|
| 253 | + $Value = explode(':', $Value); |
|
| 254 | + $Value = array_pop($Value); |
|
| 255 | + } |
|
| 256 | + } |
|
| 257 | + |
|
| 258 | + // Values are parsed according to their type |
|
| 259 | + if (isset(self::$Spec_StructuredElements[$Key])) |
|
| 260 | + { |
|
| 261 | + $Value = self::ParseStructuredValue($Value, $Key); |
|
| 262 | + if ($Type) |
|
| 263 | + { |
|
| 264 | + $Value['type'] = $Type; |
|
| 265 | + } |
|
| 266 | + } |
|
| 267 | + else |
|
| 268 | + { |
|
| 269 | + if (in_array($Key, self::$Spec_MultipleValueElements)) |
|
| 270 | + { |
|
| 271 | + $Value = self::ParseMultipleTextValue($Value, $Key); |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + if ($Type) |
|
| 275 | + { |
|
| 276 | + $Value = array( |
|
| 277 | + 'value' => $Value, |
|
| 278 | + 'type' => $Type |
|
| 279 | + ); |
|
| 280 | + } |
|
| 281 | + } |
|
| 282 | + |
|
| 283 | + if (is_array($Value) && $Encoding) |
|
| 284 | + { |
|
| 285 | + $Value['encoding'] = $Encoding; |
|
| 286 | + } |
|
| 287 | + |
|
| 288 | + if (!isset($this -> Data[$Key])) |
|
| 289 | + { |
|
| 290 | + $this -> Data[$Key] = array(); |
|
| 291 | + } |
|
| 292 | + |
|
| 293 | + $this -> Data[$Key][] = $Value; |
|
| 294 | + } |
|
| 295 | + } |
|
| 296 | + } |
|
| 297 | + |
|
| 298 | + /** |
|
| 299 | + * Magic method to get the various vCard values as object members, e.g. |
|
| 300 | + * a call to $vCard -> N gets the "N" value |
|
| 301 | + * |
|
| 302 | + * @param string Key |
|
| 303 | + * |
|
| 304 | + * @return mixed Value |
|
| 305 | + */ |
|
| 306 | + public function __get($Key) |
|
| 307 | + { |
|
| 308 | + $Key = strtolower($Key); |
|
| 309 | + if (isset($this -> Data[$Key])) |
|
| 310 | + { |
|
| 311 | + if ($Key == 'agent') |
|
| 312 | + { |
|
| 313 | + return $this -> Data[$Key]; |
|
| 314 | + } |
|
| 315 | + elseif (in_array($Key, self::$Spec_FileElements)) |
|
| 316 | + { |
|
| 317 | + $Value = $this -> Data[$Key]; |
|
| 318 | + foreach ($Value as $K => $V) |
|
| 319 | + { |
|
| 320 | + if (stripos($V['value'], 'uri:') === 0) |
|
| 321 | + { |
|
| 322 | + $Value[$K]['value'] = substr($V, 4); |
|
| 323 | + $Value[$K]['encoding'] = 'uri'; |
|
| 324 | + } |
|
| 325 | + } |
|
| 326 | + return $Value; |
|
| 327 | + } |
|
| 328 | + |
|
| 329 | + if ($this -> Options['Collapse'] && is_array($this -> Data[$Key]) && (count($this -> Data[$Key]) == 1)) |
|
| 330 | + { |
|
| 331 | + return $this -> Data[$Key][0]; |
|
| 332 | + } |
|
| 333 | + return $this -> Data[$Key]; |
|
| 334 | + } |
|
| 335 | + elseif ($Key == 'Mode') |
|
| 336 | + { |
|
| 337 | + return $this -> Mode; |
|
| 338 | + } |
|
| 339 | + return array(); |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Saves an embedded file |
|
| 344 | + * |
|
| 345 | + * @param string Key |
|
| 346 | + * @param int Index of the file, defaults to 0 |
|
| 347 | + * @param string Target path where the file should be saved, including the filename |
|
| 348 | + * |
|
| 349 | + * @return bool Operation status |
|
| 350 | + */ |
|
| 351 | + public function SaveFile($Key, $Index = 0, $TargetPath = '') |
|
| 352 | + { |
|
| 353 | + if (!isset($this -> Data[$Key])) |
|
| 354 | + { |
|
| 355 | + return false; |
|
| 356 | + } |
|
| 357 | + if (!isset($this -> Data[$Key][$Index])) |
|
| 358 | + { |
|
| 359 | + return false; |
|
| 360 | + } |
|
| 361 | + |
|
| 362 | + // Returing false if it is an image URL |
|
| 363 | + if (stripos($this -> Data[$Key][$Index]['value'], 'uri:') === 0) |
|
| 364 | + { |
|
| 365 | + return false; |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + if (is_writable($TargetPath) || (!file_exists($TargetPath) && is_writable(dirname($TargetPath)))) |
|
| 369 | + { |
|
| 370 | + $RawContent = $this -> Data[$Key][$Index]['value']; |
|
| 371 | + if (isset($this -> Data[$Key][$Index]['encoding']) && $this -> Data[$Key][$Index]['encoding'] == 'b') |
|
| 372 | + { |
|
| 373 | + $RawContent = base64_decode($RawContent); |
|
| 374 | + } |
|
| 375 | + $Status = file_put_contents($TargetPath, $RawContent); |
|
| 376 | + return (bool)$Status; |
|
| 377 | + } |
|
| 378 | + else |
|
| 379 | + { |
|
| 380 | + throw new Exception('vCard: Cannot save file ('.$Key.'), target path not writable ('.$TargetPath.')'); |
|
| 381 | + } |
|
| 382 | + return false; |
|
| 383 | + } |
|
| 384 | + |
|
| 385 | + /** |
|
| 386 | + * Magic method for adding data to the vCard |
|
| 387 | + * |
|
| 388 | + * @param string Key |
|
| 389 | + * @param string Method call arguments. First element is value. |
|
| 390 | + * |
|
| 391 | + * @return vCard Current object for method chaining |
|
| 392 | + */ |
|
| 393 | + public function __call($Key, $Arguments) |
|
| 394 | + { |
|
| 395 | + $Key = strtolower($Key); |
|
| 396 | + |
|
| 397 | + if (!isset($this -> Data[$Key])) |
|
| 398 | + { |
|
| 399 | + $this -> Data[$Key] = array(); |
|
| 400 | + } |
|
| 401 | + |
|
| 402 | + $Value = isset($Arguments[0]) ? $Arguments[0] : false; |
|
| 403 | + |
|
| 404 | + if (count($Arguments) > 1) |
|
| 405 | + { |
|
| 406 | + $Types = array_map('strtolower', array_values(array_slice($Arguments, 1))); |
|
| 407 | + |
|
| 408 | + if (isset(self::$Spec_StructuredElements[$Key]) && |
|
| 409 | + in_array(strtolower($Arguments[1]), self::$Spec_StructuredElements[$Key]) |
|
| 410 | + ) |
|
| 411 | + { |
|
| 412 | + $LastElementIndex = 0; |
|
| 413 | + |
|
| 414 | + if (count($this -> Data[$Key])) |
|
| 415 | + { |
|
| 416 | + $LastElementIndex = count($this -> Data[$Key]) - 1; |
|
| 417 | + } |
|
| 418 | + |
|
| 419 | + if (isset($this -> Data[$Key][$LastElementIndex])) |
|
| 420 | + { |
|
| 421 | + if (empty($this -> Data[$Key][$LastElementIndex][$Types[0]])) |
|
| 422 | + { |
|
| 423 | + $this -> Data[$Key][$LastElementIndex][$Types[0]] = $Value; |
|
| 424 | + } |
|
| 425 | + else |
|
| 426 | + { |
|
| 427 | + $LastElementIndex++; |
|
| 428 | + } |
|
| 429 | + } |
|
| 430 | + |
|
| 431 | + if (!isset($this -> Data[$Key][$LastElementIndex])) |
|
| 432 | + { |
|
| 433 | + $this -> Data[$Key][$LastElementIndex] = array( |
|
| 434 | + $Types[0] => $Value |
|
| 435 | + ); |
|
| 436 | + } |
|
| 437 | + } |
|
| 438 | + elseif (isset(self::$Spec_ElementTypes[$Key])) |
|
| 439 | + { |
|
| 440 | + $this -> Data[$Key][] = array( |
|
| 441 | + 'value' => $Value, |
|
| 442 | + 'type' => $Types |
|
| 443 | + ); |
|
| 444 | + } |
|
| 445 | + } |
|
| 446 | + elseif ($Value) |
|
| 447 | + { |
|
| 448 | + $this -> Data[$Key][] = $Value; |
|
| 449 | + } |
|
| 450 | + |
|
| 451 | + return $this; |
|
| 452 | + } |
|
| 453 | + |
|
| 454 | + /** |
|
| 455 | + * Magic method for getting vCard content out |
|
| 456 | + * |
|
| 457 | + * @return string Raw vCard content |
|
| 458 | + */ |
|
| 459 | + public function __toString() |
|
| 460 | + { |
|
| 461 | + $Text = 'BEGIN:VCARD'.self::endl; |
|
| 462 | + $Text .= 'VERSION:3.0'.self::endl; |
|
| 463 | + |
|
| 464 | + foreach ($this -> Data as $Key => $Values) |
|
| 465 | + { |
|
| 466 | + $KeyUC = strtoupper($Key); |
|
| 467 | + $Key = strtolower($Key); |
|
| 468 | + |
|
| 469 | + if (in_array($KeyUC, array('PHOTO', 'VERSION'))) |
|
| 470 | + { |
|
| 471 | + continue; |
|
| 472 | + } |
|
| 473 | + |
|
| 474 | + foreach ($Values as $Index => $Value) |
|
| 475 | + { |
|
| 476 | + $Text .= $KeyUC; |
|
| 477 | + if (is_array($Value) && isset($Value['type'])) |
|
| 478 | + { |
|
| 479 | + $Text .= ';TYPE='.self::PrepareTypeStrForOutput($Value['type']); |
|
| 480 | + } |
|
| 481 | + $Text .= ':'; |
|
| 482 | + |
|
| 483 | + if (isset(self::$Spec_StructuredElements[$Key])) |
|
| 484 | + { |
|
| 485 | + $PartArray = array(); |
|
| 486 | + foreach (self::$Spec_StructuredElements[$Key] as $Part) |
|
| 487 | + { |
|
| 488 | + $PartArray[] = isset($Value[$Part]) ? $Value[$Part] : ''; |
|
| 489 | + } |
|
| 490 | + $Text .= implode(';', $PartArray); |
|
| 491 | + } |
|
| 492 | + elseif (is_array($Value) && isset(self::$Spec_ElementTypes[$Key])) |
|
| 493 | + { |
|
| 494 | + $Text .= $Value['value']; |
|
| 495 | + } |
|
| 496 | + else |
|
| 497 | + { |
|
| 498 | + $Text .= $Value; |
|
| 499 | + } |
|
| 500 | + |
|
| 501 | + $Text .= self::endl; |
|
| 502 | + } |
|
| 503 | + } |
|
| 504 | + |
|
| 505 | + $Text .= 'END:VCARD'.self::endl; |
|
| 506 | + return $Text; |
|
| 507 | + } |
|
| 508 | + |
|
| 509 | + // !Helper methods |
|
| 510 | + |
|
| 511 | + private static function PrepareTypeStrForOutput($Type) |
|
| 512 | + { |
|
| 513 | + return implode(',', array_map('strtoupper', $Type)); |
|
| 514 | + } |
|
| 515 | + |
|
| 516 | + /** |
|
| 517 | + * Removes the escaping slashes from the text. |
|
| 518 | + * |
|
| 519 | + * @access private |
|
| 520 | + * |
|
| 521 | + * @param string Text to prepare. |
|
| 522 | + * |
|
| 523 | + * @return string Resulting text. |
|
| 524 | + */ |
|
| 525 | + private static function Unescape($Text) |
|
| 526 | + { |
|
| 527 | + return str_replace(array('\:', '\;', '\,', "\n"), array(':', ';', ',', ''), $Text); |
|
| 528 | + } |
|
| 529 | + |
|
| 530 | + /** |
|
| 531 | + * Separates the various parts of a structured value according to the spec. |
|
| 532 | + * |
|
| 533 | + * @access private |
|
| 534 | + * |
|
| 535 | + * @param string Raw text string |
|
| 536 | + * @param string Key (e.g., N, ADR, ORG, etc.) |
|
| 537 | + * |
|
| 538 | + * @return array Parts in an associative array. |
|
| 539 | + */ |
|
| 540 | + private static function ParseStructuredValue($Text, $Key) |
|
| 541 | + { |
|
| 542 | + $Text = array_map('trim', explode(';', $Text)); |
|
| 543 | + |
|
| 544 | + $Result = array(); |
|
| 545 | + $Ctr = 0; |
|
| 546 | + |
|
| 547 | + foreach (self::$Spec_StructuredElements[$Key] as $Index => $StructurePart) |
|
| 548 | + { |
|
| 549 | + $Result[$StructurePart] = isset($Text[$Index]) ? $Text[$Index] : null; |
|
| 550 | + } |
|
| 551 | + return $Result; |
|
| 552 | + } |
|
| 553 | + |
|
| 554 | + /** |
|
| 555 | + * @access private |
|
| 556 | + */ |
|
| 557 | + private static function ParseMultipleTextValue($Text) |
|
| 558 | + { |
|
| 559 | + return explode(',', $Text); |
|
| 560 | + } |
|
| 561 | + |
|
| 562 | + /** |
|
| 563 | + * @access private |
|
| 564 | + */ |
|
| 565 | + private static function ParseParameters($Key, array $RawParams = null) |
|
| 566 | + { |
|
| 567 | + if (!$RawParams) |
|
| 568 | + { |
|
| 569 | + return array(); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + // Parameters are split into (key, value) pairs |
|
| 573 | + $Parameters = array(); |
|
| 574 | + foreach ($RawParams as $Item) |
|
| 575 | + { |
|
| 576 | + $Parameters[] = explode('=', strtolower($Item)); |
|
| 577 | + } |
|
| 578 | + |
|
| 579 | + $Type = array(); |
|
| 580 | + $Result = array(); |
|
| 581 | + |
|
| 582 | + // And each parameter is checked whether anything can/should be done because of it |
|
| 583 | + foreach ($Parameters as $Index => $Parameter) |
|
| 584 | + { |
|
| 585 | + // Skipping empty elements |
|
| 586 | + if (!$Parameter) |
|
| 587 | + { |
|
| 588 | + continue; |
|
| 589 | + } |
|
| 590 | + |
|
| 591 | + // Handling type parameters without the explicit TYPE parameter name (2.1 valid) |
|
| 592 | + if (count($Parameter) == 1) |
|
| 593 | + { |
|
| 594 | + // Checks if the type value is allowed for the specific element |
|
| 595 | + // The second part of the "if" statement means that email elements can have non-standard types (see the spec) |
|
| 596 | + if ( |
|
| 597 | + (isset(self::$Spec_ElementTypes[$Key]) && in_array($Parameter[0], self::$Spec_ElementTypes[$Key])) || |
|
| 598 | + ($Key == 'email' && is_scalar($Parameter[0])) |
|
| 599 | + ) |
|
| 600 | + { |
|
| 601 | + $Type[] = $Parameter[0]; |
|
| 602 | + } |
|
| 603 | + } |
|
| 604 | + elseif (count($Parameter) > 2) |
|
| 605 | + { |
|
| 606 | + if(count(explode(',', $RawParams[$Index], -1)) > 0) |
|
| 607 | + { |
|
| 608 | + $TempTypeParams = self::ParseParameters($Key, explode(',', $RawParams[$Index])); |
|
| 609 | + if ($TempTypeParams['type']) |
|
| 610 | + { |
|
| 611 | + $Type = array_merge($Type, $TempTypeParams['type']); |
|
| 612 | + } |
|
| 613 | + } |
|
| 614 | + } |
|
| 615 | + else |
|
| 616 | + { |
|
| 617 | + switch ($Parameter[0]) |
|
| 618 | + { |
|
| 619 | + case 'encoding': |
|
| 620 | + if (in_array($Parameter[1], array('quoted-printable', 'b', 'base64'))) |
|
| 621 | + { |
|
| 622 | + $Result['encoding'] = $Parameter[1] == 'base64' ? 'b' : $Parameter[1]; |
|
| 623 | + } |
|
| 624 | + break; |
|
| 625 | + case 'charset': |
|
| 626 | + $Result['charset'] = $Parameter[1]; |
|
| 627 | + break; |
|
| 628 | + case 'type': |
|
| 629 | + $Type = array_merge($Type, explode(',', $Parameter[1])); |
|
| 630 | + break; |
|
| 631 | + case 'value': |
|
| 632 | + if (strtolower($Parameter[1]) == 'url') |
|
| 633 | + { |
|
| 634 | + $Result['encoding'] = 'uri'; |
|
| 635 | + } |
|
| 636 | + break; |
|
| 637 | + } |
|
| 638 | + } |
|
| 639 | + } |
|
| 640 | + |
|
| 641 | + $Result['type'] = $Type; |
|
| 642 | + |
|
| 643 | + return $Result; |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + // !Interface methods |
|
| 647 | + |
|
| 648 | + // Countable interface |
|
| 649 | + public function count() |
|
| 650 | + { |
|
| 651 | + switch ($this -> Mode) |
|
| 652 | + { |
|
| 653 | + case self::MODE_ERROR: |
|
| 654 | + return 0; |
|
| 655 | + break; |
|
| 656 | + case self::MODE_SINGLE: |
|
| 657 | + return 1; |
|
| 658 | + break; |
|
| 659 | + case self::MODE_MULTIPLE: |
|
| 660 | + return count($this -> Data); |
|
| 661 | + break; |
|
| 662 | + } |
|
| 663 | + return 0; |
|
| 664 | + } |
|
| 665 | + |
|
| 666 | + // Iterator interface |
|
| 667 | + public function rewind() |
|
| 668 | + { |
|
| 669 | + reset($this -> Data); |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + public function current() |
|
| 673 | + { |
|
| 674 | + return current($this -> Data); |
|
| 675 | + } |
|
| 676 | + |
|
| 677 | + public function next() |
|
| 678 | + { |
|
| 679 | + return next($this -> Data); |
|
| 680 | + } |
|
| 681 | + |
|
| 682 | + public function valid() |
|
| 683 | + { |
|
| 684 | + return ($this -> current() !== false); |
|
| 685 | + } |
|
| 686 | + |
|
| 687 | + public function key() |
|
| 688 | + { |
|
| 689 | + return key($this -> Data); |
|
| 690 | + } |
|
| 691 | + } |
|
| 692 | 692 | ?> |
@@ -18,7 +18,7 @@ discard block |
||
| 18 | 18 | /** |
| 19 | 19 | * @var string Current object mode - error, single or multiple (for a single vCard within a file and multiple combined vCards) |
| 20 | 20 | */ |
| 21 | - private $Mode; //single, multiple, error |
|
| 21 | + private $Mode; //single, multiple, error |
|
| 22 | 22 | |
| 23 | 23 | private $Path = ''; |
| 24 | 24 | private $RawData = ''; |
@@ -75,17 +75,17 @@ discard block |
||
| 75 | 75 | // If path is given, the file should be accessible. |
| 76 | 76 | // If raw data is given, it is taken as it is. |
| 77 | 77 | // In both cases the real content is put in $this -> RawData |
| 78 | - if ($Path) |
|
| 78 | + if($Path) |
|
| 79 | 79 | { |
| 80 | - if (!is_readable($Path)) |
|
| 80 | + if(!is_readable($Path)) |
|
| 81 | 81 | { |
| 82 | - throw new Exception('vCard: Path not accessible ('.$Path.')'); |
|
| 82 | + throw new Exception('vCard: Path not accessible (' . $Path . ')'); |
|
| 83 | 83 | } |
| 84 | 84 | |
| 85 | 85 | $this -> Path = $Path; |
| 86 | 86 | $this -> RawData = file_get_contents($this -> Path); |
| 87 | 87 | } |
| 88 | - elseif ($RawData) |
|
| 88 | + elseif($RawData) |
|
| 89 | 89 | { |
| 90 | 90 | $this -> RawData = $RawData; |
| 91 | 91 | } |
@@ -95,12 +95,12 @@ discard block |
||
| 95 | 95 | // Not necessary anymore as possibility to create vCards is added |
| 96 | 96 | } |
| 97 | 97 | |
| 98 | - if (!$this -> Path && !$this -> RawData) |
|
| 98 | + if(!$this -> Path && !$this -> RawData) |
|
| 99 | 99 | { |
| 100 | 100 | return true; |
| 101 | 101 | } |
| 102 | 102 | |
| 103 | - if ($Options) |
|
| 103 | + if($Options) |
|
| 104 | 104 | { |
| 105 | 105 | $this -> Options = array_merge($this -> Options, $Options); |
| 106 | 106 | } |
@@ -111,7 +111,7 @@ discard block |
||
| 111 | 111 | $vCardBeginCount = preg_match_all('{^BEGIN\:VCARD}miS', $this -> RawData, $Matches); |
| 112 | 112 | $vCardEndCount = preg_match_all('{^END\:VCARD}miS', $this -> RawData, $Matches); |
| 113 | 113 | |
| 114 | - if (($vCardBeginCount != $vCardEndCount) || !$vCardBeginCount) |
|
| 114 | + if(($vCardBeginCount != $vCardEndCount) || !$vCardBeginCount) |
|
| 115 | 115 | { |
| 116 | 116 | $this -> Mode = vCard::MODE_ERROR; |
| 117 | 117 | throw new Exception('vCard: invalid vCard'); |
@@ -125,16 +125,16 @@ discard block |
||
| 125 | 125 | |
| 126 | 126 | // In multiple card mode the raw text is split at card beginning markers and each |
| 127 | 127 | // fragment is parsed in a separate vCard object. |
| 128 | - if ($this -> Mode == self::MODE_MULTIPLE) |
|
| 128 | + if($this -> Mode == self::MODE_MULTIPLE) |
|
| 129 | 129 | { |
| 130 | 130 | $this -> RawData = explode('BEGIN:VCARD', $this -> RawData); |
| 131 | 131 | $this -> RawData = array_filter($this -> RawData); |
| 132 | 132 | |
| 133 | - foreach ($this -> RawData as $SinglevCardRawData) |
|
| 133 | + foreach($this -> RawData as $SinglevCardRawData) |
|
| 134 | 134 | { |
| 135 | 135 | // Prepending "BEGIN:VCARD" to the raw string because we exploded on that one. |
| 136 | 136 | // If there won't be the BEGIN marker in the new object, it will fail. |
| 137 | - $SinglevCardRawData = 'BEGIN:VCARD'."\n".$SinglevCardRawData; |
|
| 137 | + $SinglevCardRawData = 'BEGIN:VCARD' . "\n" . $SinglevCardRawData; |
|
| 138 | 138 | |
| 139 | 139 | $ClassName = get_class($this); |
| 140 | 140 | $this -> Data[] = new $ClassName(false, $SinglevCardRawData); |
@@ -157,10 +157,10 @@ discard block |
||
| 157 | 157 | |
| 158 | 158 | $Lines = explode("\n", $this -> RawData); |
| 159 | 159 | |
| 160 | - foreach ($Lines as $Line) |
|
| 160 | + foreach($Lines as $Line) |
|
| 161 | 161 | { |
| 162 | 162 | // Lines without colons are skipped because, most likely, they contain no data. |
| 163 | - if (strpos($Line, ':') === false) |
|
| 163 | + if(strpos($Line, ':') === false) |
|
| 164 | 164 | { |
| 165 | 165 | continue; |
| 166 | 166 | } |
@@ -175,16 +175,16 @@ discard block |
||
| 175 | 175 | $Key = strtolower(trim(self::Unescape($Key))); |
| 176 | 176 | |
| 177 | 177 | // These two lines can be skipped as they aren't necessary at all. |
| 178 | - if ($Key == 'begin' || $Key == 'end') |
|
| 178 | + if($Key == 'begin' || $Key == 'end') |
|
| 179 | 179 | { |
| 180 | 180 | continue; |
| 181 | 181 | } |
| 182 | 182 | |
| 183 | - if ((strpos($Key, 'agent') === 0) && (stripos($Value, 'begin:vcard') !== false)) |
|
| 183 | + if((strpos($Key, 'agent') === 0) && (stripos($Value, 'begin:vcard') !== false)) |
|
| 184 | 184 | { |
| 185 | 185 | $ClassName = get_class($this); |
| 186 | 186 | $Value = new $ClassName(false, str_replace('-wrap-', "\n", $Value)); |
| 187 | - if (!isset($this -> Data[$Key])) |
|
| 187 | + if(!isset($this -> Data[$Key])) |
|
| 188 | 188 | { |
| 189 | 189 | $this -> Data[$Key] = array(); |
| 190 | 190 | } |
@@ -204,34 +204,34 @@ discard block |
||
| 204 | 204 | $Key = $KeyParts[0]; |
| 205 | 205 | $Encoding = false; |
| 206 | 206 | |
| 207 | - if (strpos($Key, 'item') === 0) |
|
| 207 | + if(strpos($Key, 'item') === 0) |
|
| 208 | 208 | { |
| 209 | 209 | $TmpKey = explode('.', $Key, 2); |
| 210 | 210 | $Key = $TmpKey[1]; |
| 211 | 211 | $ItemIndex = (int)str_ireplace('item', '', $TmpKey[0]); |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | - if (count($KeyParts) > 1) |
|
| 214 | + if(count($KeyParts) > 1) |
|
| 215 | 215 | { |
| 216 | 216 | $Parameters = self::ParseParameters($Key, array_slice($KeyParts, 1)); |
| 217 | 217 | |
| 218 | - foreach ($Parameters as $ParamKey => $ParamValue) |
|
| 218 | + foreach($Parameters as $ParamKey => $ParamValue) |
|
| 219 | 219 | { |
| 220 | - switch ($ParamKey) |
|
| 220 | + switch($ParamKey) |
|
| 221 | 221 | { |
| 222 | 222 | case 'encoding': |
| 223 | 223 | $Encoding = $ParamValue; |
| 224 | - if (in_array($ParamValue, array('b', 'base64'))) |
|
| 224 | + if(in_array($ParamValue, array('b', 'base64'))) |
|
| 225 | 225 | { |
| 226 | 226 | //$Value = base64_decode($Value); |
| 227 | 227 | } |
| 228 | - elseif ($ParamValue == 'quoted-printable') // v2.1 |
|
| 228 | + elseif($ParamValue == 'quoted-printable') // v2.1 |
|
| 229 | 229 | { |
| 230 | 230 | $Value = quoted_printable_decode($Value); |
| 231 | 231 | } |
| 232 | 232 | break; |
| 233 | 233 | case 'charset': // v2.1 |
| 234 | - if ($ParamValue != 'utf-8' && $ParamValue != 'utf8') |
|
| 234 | + if($ParamValue != 'utf-8' && $ParamValue != 'utf8') |
|
| 235 | 235 | { |
| 236 | 236 | $Value = mb_convert_encoding($Value, 'UTF-8', $ParamValue); |
| 237 | 237 | } |
@@ -244,11 +244,11 @@ discard block |
||
| 244 | 244 | } |
| 245 | 245 | |
| 246 | 246 | // Checking files for colon-separated additional parameters (Apple's Address Book does this), for example, "X-ABCROP-RECTANGLE" for photos |
| 247 | - if (in_array($Key, self::$Spec_FileElements) && isset($Parameters['encoding']) && in_array($Parameters['encoding'], array('b', 'base64'))) |
|
| 247 | + if(in_array($Key, self::$Spec_FileElements) && isset($Parameters['encoding']) && in_array($Parameters['encoding'], array('b', 'base64'))) |
|
| 248 | 248 | { |
| 249 | 249 | // If colon is present in the value, it must contain Address Book parameters |
| 250 | 250 | // (colon is an invalid character for base64 so it shouldn't appear in valid files) |
| 251 | - if (strpos($Value, ':') !== false) |
|
| 251 | + if(strpos($Value, ':') !== false) |
|
| 252 | 252 | { |
| 253 | 253 | $Value = explode(':', $Value); |
| 254 | 254 | $Value = array_pop($Value); |
@@ -256,22 +256,22 @@ discard block |
||
| 256 | 256 | } |
| 257 | 257 | |
| 258 | 258 | // Values are parsed according to their type |
| 259 | - if (isset(self::$Spec_StructuredElements[$Key])) |
|
| 259 | + if(isset(self::$Spec_StructuredElements[$Key])) |
|
| 260 | 260 | { |
| 261 | 261 | $Value = self::ParseStructuredValue($Value, $Key); |
| 262 | - if ($Type) |
|
| 262 | + if($Type) |
|
| 263 | 263 | { |
| 264 | 264 | $Value['type'] = $Type; |
| 265 | 265 | } |
| 266 | 266 | } |
| 267 | 267 | else |
| 268 | 268 | { |
| 269 | - if (in_array($Key, self::$Spec_MultipleValueElements)) |
|
| 269 | + if(in_array($Key, self::$Spec_MultipleValueElements)) |
|
| 270 | 270 | { |
| 271 | 271 | $Value = self::ParseMultipleTextValue($Value, $Key); |
| 272 | 272 | } |
| 273 | 273 | |
| 274 | - if ($Type) |
|
| 274 | + if($Type) |
|
| 275 | 275 | { |
| 276 | 276 | $Value = array( |
| 277 | 277 | 'value' => $Value, |
@@ -280,12 +280,12 @@ discard block |
||
| 280 | 280 | } |
| 281 | 281 | } |
| 282 | 282 | |
| 283 | - if (is_array($Value) && $Encoding) |
|
| 283 | + if(is_array($Value) && $Encoding) |
|
| 284 | 284 | { |
| 285 | 285 | $Value['encoding'] = $Encoding; |
| 286 | 286 | } |
| 287 | 287 | |
| 288 | - if (!isset($this -> Data[$Key])) |
|
| 288 | + if(!isset($this -> Data[$Key])) |
|
| 289 | 289 | { |
| 290 | 290 | $this -> Data[$Key] = array(); |
| 291 | 291 | } |
@@ -306,18 +306,18 @@ discard block |
||
| 306 | 306 | public function __get($Key) |
| 307 | 307 | { |
| 308 | 308 | $Key = strtolower($Key); |
| 309 | - if (isset($this -> Data[$Key])) |
|
| 309 | + if(isset($this -> Data[$Key])) |
|
| 310 | 310 | { |
| 311 | - if ($Key == 'agent') |
|
| 311 | + if($Key == 'agent') |
|
| 312 | 312 | { |
| 313 | 313 | return $this -> Data[$Key]; |
| 314 | 314 | } |
| 315 | - elseif (in_array($Key, self::$Spec_FileElements)) |
|
| 315 | + elseif(in_array($Key, self::$Spec_FileElements)) |
|
| 316 | 316 | { |
| 317 | 317 | $Value = $this -> Data[$Key]; |
| 318 | - foreach ($Value as $K => $V) |
|
| 318 | + foreach($Value as $K => $V) |
|
| 319 | 319 | { |
| 320 | - if (stripos($V['value'], 'uri:') === 0) |
|
| 320 | + if(stripos($V['value'], 'uri:') === 0) |
|
| 321 | 321 | { |
| 322 | 322 | $Value[$K]['value'] = substr($V, 4); |
| 323 | 323 | $Value[$K]['encoding'] = 'uri'; |
@@ -326,13 +326,13 @@ discard block |
||
| 326 | 326 | return $Value; |
| 327 | 327 | } |
| 328 | 328 | |
| 329 | - if ($this -> Options['Collapse'] && is_array($this -> Data[$Key]) && (count($this -> Data[$Key]) == 1)) |
|
| 329 | + if($this -> Options['Collapse'] && is_array($this -> Data[$Key]) && (count($this -> Data[$Key]) == 1)) |
|
| 330 | 330 | { |
| 331 | 331 | return $this -> Data[$Key][0]; |
| 332 | 332 | } |
| 333 | 333 | return $this -> Data[$Key]; |
| 334 | 334 | } |
| 335 | - elseif ($Key == 'Mode') |
|
| 335 | + elseif($Key == 'Mode') |
|
| 336 | 336 | { |
| 337 | 337 | return $this -> Mode; |
| 338 | 338 | } |
@@ -350,25 +350,25 @@ discard block |
||
| 350 | 350 | */ |
| 351 | 351 | public function SaveFile($Key, $Index = 0, $TargetPath = '') |
| 352 | 352 | { |
| 353 | - if (!isset($this -> Data[$Key])) |
|
| 353 | + if(!isset($this -> Data[$Key])) |
|
| 354 | 354 | { |
| 355 | 355 | return false; |
| 356 | 356 | } |
| 357 | - if (!isset($this -> Data[$Key][$Index])) |
|
| 357 | + if(!isset($this -> Data[$Key][$Index])) |
|
| 358 | 358 | { |
| 359 | 359 | return false; |
| 360 | 360 | } |
| 361 | 361 | |
| 362 | 362 | // Returing false if it is an image URL |
| 363 | - if (stripos($this -> Data[$Key][$Index]['value'], 'uri:') === 0) |
|
| 363 | + if(stripos($this -> Data[$Key][$Index]['value'], 'uri:') === 0) |
|
| 364 | 364 | { |
| 365 | 365 | return false; |
| 366 | 366 | } |
| 367 | 367 | |
| 368 | - if (is_writable($TargetPath) || (!file_exists($TargetPath) && is_writable(dirname($TargetPath)))) |
|
| 368 | + if(is_writable($TargetPath) || (!file_exists($TargetPath) && is_writable(dirname($TargetPath)))) |
|
| 369 | 369 | { |
| 370 | 370 | $RawContent = $this -> Data[$Key][$Index]['value']; |
| 371 | - if (isset($this -> Data[$Key][$Index]['encoding']) && $this -> Data[$Key][$Index]['encoding'] == 'b') |
|
| 371 | + if(isset($this -> Data[$Key][$Index]['encoding']) && $this -> Data[$Key][$Index]['encoding'] == 'b') |
|
| 372 | 372 | { |
| 373 | 373 | $RawContent = base64_decode($RawContent); |
| 374 | 374 | } |
@@ -377,7 +377,7 @@ discard block |
||
| 377 | 377 | } |
| 378 | 378 | else |
| 379 | 379 | { |
| 380 | - throw new Exception('vCard: Cannot save file ('.$Key.'), target path not writable ('.$TargetPath.')'); |
|
| 380 | + throw new Exception('vCard: Cannot save file (' . $Key . '), target path not writable (' . $TargetPath . ')'); |
|
| 381 | 381 | } |
| 382 | 382 | return false; |
| 383 | 383 | } |
@@ -394,31 +394,31 @@ discard block |
||
| 394 | 394 | { |
| 395 | 395 | $Key = strtolower($Key); |
| 396 | 396 | |
| 397 | - if (!isset($this -> Data[$Key])) |
|
| 397 | + if(!isset($this -> Data[$Key])) |
|
| 398 | 398 | { |
| 399 | 399 | $this -> Data[$Key] = array(); |
| 400 | 400 | } |
| 401 | 401 | |
| 402 | 402 | $Value = isset($Arguments[0]) ? $Arguments[0] : false; |
| 403 | 403 | |
| 404 | - if (count($Arguments) > 1) |
|
| 404 | + if(count($Arguments) > 1) |
|
| 405 | 405 | { |
| 406 | 406 | $Types = array_map('strtolower', array_values(array_slice($Arguments, 1))); |
| 407 | 407 | |
| 408 | - if (isset(self::$Spec_StructuredElements[$Key]) && |
|
| 408 | + if(isset(self::$Spec_StructuredElements[$Key]) && |
|
| 409 | 409 | in_array(strtolower($Arguments[1]), self::$Spec_StructuredElements[$Key]) |
| 410 | 410 | ) |
| 411 | 411 | { |
| 412 | 412 | $LastElementIndex = 0; |
| 413 | 413 | |
| 414 | - if (count($this -> Data[$Key])) |
|
| 414 | + if(count($this -> Data[$Key])) |
|
| 415 | 415 | { |
| 416 | 416 | $LastElementIndex = count($this -> Data[$Key]) - 1; |
| 417 | 417 | } |
| 418 | 418 | |
| 419 | - if (isset($this -> Data[$Key][$LastElementIndex])) |
|
| 419 | + if(isset($this -> Data[$Key][$LastElementIndex])) |
|
| 420 | 420 | { |
| 421 | - if (empty($this -> Data[$Key][$LastElementIndex][$Types[0]])) |
|
| 421 | + if(empty($this -> Data[$Key][$LastElementIndex][$Types[0]])) |
|
| 422 | 422 | { |
| 423 | 423 | $this -> Data[$Key][$LastElementIndex][$Types[0]] = $Value; |
| 424 | 424 | } |
@@ -428,14 +428,14 @@ discard block |
||
| 428 | 428 | } |
| 429 | 429 | } |
| 430 | 430 | |
| 431 | - if (!isset($this -> Data[$Key][$LastElementIndex])) |
|
| 431 | + if(!isset($this -> Data[$Key][$LastElementIndex])) |
|
| 432 | 432 | { |
| 433 | 433 | $this -> Data[$Key][$LastElementIndex] = array( |
| 434 | 434 | $Types[0] => $Value |
| 435 | 435 | ); |
| 436 | 436 | } |
| 437 | 437 | } |
| 438 | - elseif (isset(self::$Spec_ElementTypes[$Key])) |
|
| 438 | + elseif(isset(self::$Spec_ElementTypes[$Key])) |
|
| 439 | 439 | { |
| 440 | 440 | $this -> Data[$Key][] = array( |
| 441 | 441 | 'value' => $Value, |
@@ -443,7 +443,7 @@ discard block |
||
| 443 | 443 | ); |
| 444 | 444 | } |
| 445 | 445 | } |
| 446 | - elseif ($Value) |
|
| 446 | + elseif($Value) |
|
| 447 | 447 | { |
| 448 | 448 | $this -> Data[$Key][] = $Value; |
| 449 | 449 | } |
@@ -458,38 +458,38 @@ discard block |
||
| 458 | 458 | */ |
| 459 | 459 | public function __toString() |
| 460 | 460 | { |
| 461 | - $Text = 'BEGIN:VCARD'.self::endl; |
|
| 462 | - $Text .= 'VERSION:3.0'.self::endl; |
|
| 461 | + $Text = 'BEGIN:VCARD' . self::endl; |
|
| 462 | + $Text .= 'VERSION:3.0' . self::endl; |
|
| 463 | 463 | |
| 464 | - foreach ($this -> Data as $Key => $Values) |
|
| 464 | + foreach($this -> Data as $Key => $Values) |
|
| 465 | 465 | { |
| 466 | 466 | $KeyUC = strtoupper($Key); |
| 467 | 467 | $Key = strtolower($Key); |
| 468 | 468 | |
| 469 | - if (in_array($KeyUC, array('PHOTO', 'VERSION'))) |
|
| 469 | + if(in_array($KeyUC, array('PHOTO', 'VERSION'))) |
|
| 470 | 470 | { |
| 471 | 471 | continue; |
| 472 | 472 | } |
| 473 | 473 | |
| 474 | - foreach ($Values as $Index => $Value) |
|
| 474 | + foreach($Values as $Index => $Value) |
|
| 475 | 475 | { |
| 476 | 476 | $Text .= $KeyUC; |
| 477 | - if (is_array($Value) && isset($Value['type'])) |
|
| 477 | + if(is_array($Value) && isset($Value['type'])) |
|
| 478 | 478 | { |
| 479 | - $Text .= ';TYPE='.self::PrepareTypeStrForOutput($Value['type']); |
|
| 479 | + $Text .= ';TYPE=' . self::PrepareTypeStrForOutput($Value['type']); |
|
| 480 | 480 | } |
| 481 | 481 | $Text .= ':'; |
| 482 | 482 | |
| 483 | - if (isset(self::$Spec_StructuredElements[$Key])) |
|
| 483 | + if(isset(self::$Spec_StructuredElements[$Key])) |
|
| 484 | 484 | { |
| 485 | 485 | $PartArray = array(); |
| 486 | - foreach (self::$Spec_StructuredElements[$Key] as $Part) |
|
| 486 | + foreach(self::$Spec_StructuredElements[$Key] as $Part) |
|
| 487 | 487 | { |
| 488 | 488 | $PartArray[] = isset($Value[$Part]) ? $Value[$Part] : ''; |
| 489 | 489 | } |
| 490 | 490 | $Text .= implode(';', $PartArray); |
| 491 | 491 | } |
| 492 | - elseif (is_array($Value) && isset(self::$Spec_ElementTypes[$Key])) |
|
| 492 | + elseif(is_array($Value) && isset(self::$Spec_ElementTypes[$Key])) |
|
| 493 | 493 | { |
| 494 | 494 | $Text .= $Value['value']; |
| 495 | 495 | } |
@@ -502,7 +502,7 @@ discard block |
||
| 502 | 502 | } |
| 503 | 503 | } |
| 504 | 504 | |
| 505 | - $Text .= 'END:VCARD'.self::endl; |
|
| 505 | + $Text .= 'END:VCARD' . self::endl; |
|
| 506 | 506 | return $Text; |
| 507 | 507 | } |
| 508 | 508 | |
@@ -544,7 +544,7 @@ discard block |
||
| 544 | 544 | $Result = array(); |
| 545 | 545 | $Ctr = 0; |
| 546 | 546 | |
| 547 | - foreach (self::$Spec_StructuredElements[$Key] as $Index => $StructurePart) |
|
| 547 | + foreach(self::$Spec_StructuredElements[$Key] as $Index => $StructurePart) |
|
| 548 | 548 | { |
| 549 | 549 | $Result[$StructurePart] = isset($Text[$Index]) ? $Text[$Index] : null; |
| 550 | 550 | } |
@@ -564,14 +564,14 @@ discard block |
||
| 564 | 564 | */ |
| 565 | 565 | private static function ParseParameters($Key, array $RawParams = null) |
| 566 | 566 | { |
| 567 | - if (!$RawParams) |
|
| 567 | + if(!$RawParams) |
|
| 568 | 568 | { |
| 569 | 569 | return array(); |
| 570 | 570 | } |
| 571 | 571 | |
| 572 | 572 | // Parameters are split into (key, value) pairs |
| 573 | 573 | $Parameters = array(); |
| 574 | - foreach ($RawParams as $Item) |
|
| 574 | + foreach($RawParams as $Item) |
|
| 575 | 575 | { |
| 576 | 576 | $Parameters[] = explode('=', strtolower($Item)); |
| 577 | 577 | } |
@@ -580,20 +580,20 @@ discard block |
||
| 580 | 580 | $Result = array(); |
| 581 | 581 | |
| 582 | 582 | // And each parameter is checked whether anything can/should be done because of it |
| 583 | - foreach ($Parameters as $Index => $Parameter) |
|
| 583 | + foreach($Parameters as $Index => $Parameter) |
|
| 584 | 584 | { |
| 585 | 585 | // Skipping empty elements |
| 586 | - if (!$Parameter) |
|
| 586 | + if(!$Parameter) |
|
| 587 | 587 | { |
| 588 | 588 | continue; |
| 589 | 589 | } |
| 590 | 590 | |
| 591 | 591 | // Handling type parameters without the explicit TYPE parameter name (2.1 valid) |
| 592 | - if (count($Parameter) == 1) |
|
| 592 | + if(count($Parameter) == 1) |
|
| 593 | 593 | { |
| 594 | 594 | // Checks if the type value is allowed for the specific element |
| 595 | 595 | // The second part of the "if" statement means that email elements can have non-standard types (see the spec) |
| 596 | - if ( |
|
| 596 | + if( |
|
| 597 | 597 | (isset(self::$Spec_ElementTypes[$Key]) && in_array($Parameter[0], self::$Spec_ElementTypes[$Key])) || |
| 598 | 598 | ($Key == 'email' && is_scalar($Parameter[0])) |
| 599 | 599 | ) |
@@ -601,12 +601,12 @@ discard block |
||
| 601 | 601 | $Type[] = $Parameter[0]; |
| 602 | 602 | } |
| 603 | 603 | } |
| 604 | - elseif (count($Parameter) > 2) |
|
| 604 | + elseif(count($Parameter) > 2) |
|
| 605 | 605 | { |
| 606 | 606 | if(count(explode(',', $RawParams[$Index], -1)) > 0) |
| 607 | 607 | { |
| 608 | 608 | $TempTypeParams = self::ParseParameters($Key, explode(',', $RawParams[$Index])); |
| 609 | - if ($TempTypeParams['type']) |
|
| 609 | + if($TempTypeParams['type']) |
|
| 610 | 610 | { |
| 611 | 611 | $Type = array_merge($Type, $TempTypeParams['type']); |
| 612 | 612 | } |
@@ -614,10 +614,10 @@ discard block |
||
| 614 | 614 | } |
| 615 | 615 | else |
| 616 | 616 | { |
| 617 | - switch ($Parameter[0]) |
|
| 617 | + switch($Parameter[0]) |
|
| 618 | 618 | { |
| 619 | 619 | case 'encoding': |
| 620 | - if (in_array($Parameter[1], array('quoted-printable', 'b', 'base64'))) |
|
| 620 | + if(in_array($Parameter[1], array('quoted-printable', 'b', 'base64'))) |
|
| 621 | 621 | { |
| 622 | 622 | $Result['encoding'] = $Parameter[1] == 'base64' ? 'b' : $Parameter[1]; |
| 623 | 623 | } |
@@ -629,7 +629,7 @@ discard block |
||
| 629 | 629 | $Type = array_merge($Type, explode(',', $Parameter[1])); |
| 630 | 630 | break; |
| 631 | 631 | case 'value': |
| 632 | - if (strtolower($Parameter[1]) == 'url') |
|
| 632 | + if(strtolower($Parameter[1]) == 'url') |
|
| 633 | 633 | { |
| 634 | 634 | $Result['encoding'] = 'uri'; |
| 635 | 635 | } |
@@ -648,7 +648,7 @@ discard block |
||
| 648 | 648 | // Countable interface |
| 649 | 649 | public function count() |
| 650 | 650 | { |
| 651 | - switch ($this -> Mode) |
|
| 651 | + switch($this -> Mode) |
|
| 652 | 652 | { |
| 653 | 653 | case self::MODE_ERROR: |
| 654 | 654 | return 0; |
@@ -225,10 +225,13 @@ discard block |
||
| 225 | 225 | { |
| 226 | 226 | //$Value = base64_decode($Value); |
| 227 | 227 | } |
| 228 | - elseif ($ParamValue == 'quoted-printable') // v2.1 |
|
| 228 | + elseif ($ParamValue == 'quoted-printable') |
|
| 229 | + { |
|
| 230 | + // v2.1 |
|
| 229 | 231 | { |
| 230 | 232 | $Value = quoted_printable_decode($Value); |
| 231 | 233 | } |
| 234 | + } |
|
| 232 | 235 | break; |
| 233 | 236 | case 'charset': // v2.1 |
| 234 | 237 | if ($ParamValue != 'utf-8' && $ParamValue != 'utf8') |
@@ -407,8 +410,7 @@ discard block |
||
| 407 | 410 | |
| 408 | 411 | if (isset(self::$Spec_StructuredElements[$Key]) && |
| 409 | 412 | in_array(strtolower($Arguments[1]), self::$Spec_StructuredElements[$Key]) |
| 410 | - ) |
|
| 411 | - { |
|
| 413 | + ) { |
|
| 412 | 414 | $LastElementIndex = 0; |
| 413 | 415 | |
| 414 | 416 | if (count($this -> Data[$Key])) |
@@ -596,8 +598,7 @@ discard block |
||
| 596 | 598 | if ( |
| 597 | 599 | (isset(self::$Spec_ElementTypes[$Key]) && in_array($Parameter[0], self::$Spec_ElementTypes[$Key])) || |
| 598 | 600 | ($Key == 'email' && is_scalar($Parameter[0])) |
| 599 | - ) |
|
| 600 | - { |
|
| 601 | + ) { |
|
| 601 | 602 | $Type[] = $Parameter[0]; |
| 602 | 603 | } |
| 603 | 604 | } |
@@ -1,20 +1,20 @@ |
||
| 1 | 1 | <?php |
| 2 | - require_once('vCard.php'); |
|
| 2 | + require_once('vCard.php'); |
|
| 3 | 3 | |
| 4 | - $vCard = new vCard; |
|
| 5 | - $vCard -> n('John', 'FirstName'); |
|
| 6 | - $vCard -> n('Doe', 'LastName'); |
|
| 7 | - $vCard -> tel('555-1111'); |
|
| 8 | - $vCard -> tel('555-1234', 'Work'); |
|
| 9 | - $vCard -> adr('', 'POBox'); |
|
| 10 | - $vCard -> adr('', 'ExtendedAddress'); |
|
| 11 | - $vCard -> adr('42 Plantation St.', 'StreetAddress'); |
|
| 12 | - $vCard -> adr('Baytown', 'Locality'); |
|
| 13 | - $vCard -> adr('LA', 'Region'); |
|
| 14 | - $vCard -> adr('30314', 'PostalCode'); |
|
| 15 | - $vCard -> adr('USA', 'Country'); |
|
| 4 | + $vCard = new vCard; |
|
| 5 | + $vCard -> n('John', 'FirstName'); |
|
| 6 | + $vCard -> n('Doe', 'LastName'); |
|
| 7 | + $vCard -> tel('555-1111'); |
|
| 8 | + $vCard -> tel('555-1234', 'Work'); |
|
| 9 | + $vCard -> adr('', 'POBox'); |
|
| 10 | + $vCard -> adr('', 'ExtendedAddress'); |
|
| 11 | + $vCard -> adr('42 Plantation St.', 'StreetAddress'); |
|
| 12 | + $vCard -> adr('Baytown', 'Locality'); |
|
| 13 | + $vCard -> adr('LA', 'Region'); |
|
| 14 | + $vCard -> adr('30314', 'PostalCode'); |
|
| 15 | + $vCard -> adr('USA', 'Country'); |
|
| 16 | 16 | |
| 17 | - //$vCard = new vCard('Example3.0.vcf'); |
|
| 17 | + //$vCard = new vCard('Example3.0.vcf'); |
|
| 18 | 18 | |
| 19 | - echo '<pre>'.$vCard.'</pre>'; |
|
| 19 | + echo '<pre>'.$vCard.'</pre>'; |
|
| 20 | 20 | ?> |
| 21 | 21 | \ No newline at end of file |
@@ -16,5 +16,5 @@ |
||
| 16 | 16 | |
| 17 | 17 | //$vCard = new vCard('Example3.0.vcf'); |
| 18 | 18 | |
| 19 | - echo '<pre>'.$vCard.'</pre>'; |
|
| 19 | + echo '<pre>' . $vCard . '</pre>'; |
|
| 20 | 20 | ?> |
| 21 | 21 | \ No newline at end of file |
@@ -26,30 +26,30 @@ discard block |
||
| 26 | 26 | <body> |
| 27 | 27 | |
| 28 | 28 | <?php |
| 29 | - require_once('vCard.php'); |
|
| 29 | + require_once('vCard.php'); |
|
| 30 | 30 | |
| 31 | - /** |
|
| 32 | - * Test function for vCard content output |
|
| 33 | - * @param vCard vCard object |
|
| 34 | - */ |
|
| 35 | - function OutputvCard(vCard $vCard) |
|
| 36 | - { |
|
| 37 | - echo '<h2>'.$vCard -> FN[0].'</h2>'; |
|
| 31 | + /** |
|
| 32 | + * Test function for vCard content output |
|
| 33 | + * @param vCard vCard object |
|
| 34 | + */ |
|
| 35 | + function OutputvCard(vCard $vCard) |
|
| 36 | + { |
|
| 37 | + echo '<h2>'.$vCard -> FN[0].'</h2>'; |
|
| 38 | 38 | |
| 39 | - if ($vCard -> PHOTO) |
|
| 40 | - { |
|
| 41 | - foreach ($vCard -> PHOTO as $Photo) |
|
| 42 | - { |
|
| 43 | - if ($Photo['encoding'] == 'b') |
|
| 44 | - { |
|
| 45 | - echo '<img src="data:image/'.$Photo['type'][0].';base64,'.$Photo['value'].'" /><br />'; |
|
| 46 | - } |
|
| 47 | - else |
|
| 48 | - { |
|
| 49 | - echo '<img src="'.$Photo['value'].'" /><br />'; |
|
| 50 | - } |
|
| 39 | + if ($vCard -> PHOTO) |
|
| 40 | + { |
|
| 41 | + foreach ($vCard -> PHOTO as $Photo) |
|
| 42 | + { |
|
| 43 | + if ($Photo['encoding'] == 'b') |
|
| 44 | + { |
|
| 45 | + echo '<img src="data:image/'.$Photo['type'][0].';base64,'.$Photo['value'].'" /><br />'; |
|
| 46 | + } |
|
| 47 | + else |
|
| 48 | + { |
|
| 49 | + echo '<img src="'.$Photo['value'].'" /><br />'; |
|
| 50 | + } |
|
| 51 | 51 | |
| 52 | - /* |
|
| 52 | + /* |
|
| 53 | 53 | // It can also be saved to a file |
| 54 | 54 | try |
| 55 | 55 | { |
@@ -64,153 +64,153 @@ discard block |
||
| 64 | 64 | // Target path not writable |
| 65 | 65 | } |
| 66 | 66 | */ |
| 67 | - } |
|
| 68 | - } |
|
| 67 | + } |
|
| 68 | + } |
|
| 69 | 69 | |
| 70 | - foreach ($vCard -> N as $Name) |
|
| 71 | - { |
|
| 72 | - echo '<h3>Name: '.$Name['firstname'].' '.$Name['lastname'].'</h3>'; |
|
| 73 | - } |
|
| 70 | + foreach ($vCard -> N as $Name) |
|
| 71 | + { |
|
| 72 | + echo '<h3>Name: '.$Name['firstname'].' '.$Name['lastname'].'</h3>'; |
|
| 73 | + } |
|
| 74 | 74 | |
| 75 | - foreach ($vCard -> ORG as $Organization) |
|
| 76 | - { |
|
| 77 | - echo '<h3>Organization: '.$Organization['name']. |
|
| 78 | - ($Organization['unit1'] || $Organization['unit2'] ? |
|
| 79 | - ' ('.implode(', ', array($Organization['unit1'], $Organization['unit2'])).')' : |
|
| 80 | - '' |
|
| 81 | - ).'</h3>'; |
|
| 82 | - } |
|
| 75 | + foreach ($vCard -> ORG as $Organization) |
|
| 76 | + { |
|
| 77 | + echo '<h3>Organization: '.$Organization['name']. |
|
| 78 | + ($Organization['unit1'] || $Organization['unit2'] ? |
|
| 79 | + ' ('.implode(', ', array($Organization['unit1'], $Organization['unit2'])).')' : |
|
| 80 | + '' |
|
| 81 | + ).'</h3>'; |
|
| 82 | + } |
|
| 83 | 83 | |
| 84 | - if ($vCard -> TEL) |
|
| 85 | - { |
|
| 86 | - echo '<p><h4>Phone</h4>'; |
|
| 87 | - foreach ($vCard -> TEL as $Tel) |
|
| 88 | - { |
|
| 89 | - if (is_scalar($Tel)) |
|
| 90 | - { |
|
| 91 | - echo $Tel.'<br />'; |
|
| 92 | - } |
|
| 93 | - else |
|
| 94 | - { |
|
| 95 | - echo $Tel['value'].' ('.implode(', ', $Tel['type']).')<br />'; |
|
| 96 | - } |
|
| 97 | - } |
|
| 98 | - echo '</p>'; |
|
| 99 | - } |
|
| 84 | + if ($vCard -> TEL) |
|
| 85 | + { |
|
| 86 | + echo '<p><h4>Phone</h4>'; |
|
| 87 | + foreach ($vCard -> TEL as $Tel) |
|
| 88 | + { |
|
| 89 | + if (is_scalar($Tel)) |
|
| 90 | + { |
|
| 91 | + echo $Tel.'<br />'; |
|
| 92 | + } |
|
| 93 | + else |
|
| 94 | + { |
|
| 95 | + echo $Tel['value'].' ('.implode(', ', $Tel['type']).')<br />'; |
|
| 96 | + } |
|
| 97 | + } |
|
| 98 | + echo '</p>'; |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | - if ($vCard -> EMAIL) |
|
| 102 | - { |
|
| 103 | - echo '<p><h4>Email</h4>'; |
|
| 104 | - foreach ($vCard -> EMAIL as $Email) |
|
| 105 | - { |
|
| 106 | - if (is_scalar($Email)) |
|
| 107 | - { |
|
| 108 | - echo $Email; |
|
| 109 | - } |
|
| 110 | - else |
|
| 111 | - { |
|
| 112 | - echo $Email['value'].' ('.implode(', ', $Email['type']).')<br />'; |
|
| 113 | - } |
|
| 114 | - } |
|
| 115 | - echo '</p>'; |
|
| 116 | - } |
|
| 101 | + if ($vCard -> EMAIL) |
|
| 102 | + { |
|
| 103 | + echo '<p><h4>Email</h4>'; |
|
| 104 | + foreach ($vCard -> EMAIL as $Email) |
|
| 105 | + { |
|
| 106 | + if (is_scalar($Email)) |
|
| 107 | + { |
|
| 108 | + echo $Email; |
|
| 109 | + } |
|
| 110 | + else |
|
| 111 | + { |
|
| 112 | + echo $Email['value'].' ('.implode(', ', $Email['type']).')<br />'; |
|
| 113 | + } |
|
| 114 | + } |
|
| 115 | + echo '</p>'; |
|
| 116 | + } |
|
| 117 | 117 | |
| 118 | - if ($vCard -> URL) |
|
| 119 | - { |
|
| 120 | - echo '<p><h4>URL</h4>'; |
|
| 121 | - foreach ($vCard -> URL as $URL) |
|
| 122 | - { |
|
| 123 | - if (is_scalar($URL)) |
|
| 124 | - { |
|
| 125 | - echo $URL.'<br />'; |
|
| 126 | - } |
|
| 127 | - else |
|
| 128 | - { |
|
| 129 | - echo $URL['value'].'<br />'; |
|
| 130 | - } |
|
| 131 | - } |
|
| 132 | - echo '</p>'; |
|
| 133 | - } |
|
| 118 | + if ($vCard -> URL) |
|
| 119 | + { |
|
| 120 | + echo '<p><h4>URL</h4>'; |
|
| 121 | + foreach ($vCard -> URL as $URL) |
|
| 122 | + { |
|
| 123 | + if (is_scalar($URL)) |
|
| 124 | + { |
|
| 125 | + echo $URL.'<br />'; |
|
| 126 | + } |
|
| 127 | + else |
|
| 128 | + { |
|
| 129 | + echo $URL['value'].'<br />'; |
|
| 130 | + } |
|
| 131 | + } |
|
| 132 | + echo '</p>'; |
|
| 133 | + } |
|
| 134 | 134 | |
| 135 | - if ($vCard -> IMPP) |
|
| 136 | - { |
|
| 137 | - echo '<p><h4>Instant messaging</h4>'; |
|
| 138 | - foreach ($vCard -> IMPP as $IMPP) |
|
| 139 | - { |
|
| 140 | - if (is_scalar($IMPP)) |
|
| 141 | - { |
|
| 142 | - echo $IMPP.'<br />'; |
|
| 143 | - } |
|
| 144 | - else |
|
| 145 | - { |
|
| 146 | - echo $IMPP['value'].'<br/ >'; |
|
| 147 | - } |
|
| 148 | - } |
|
| 149 | - echo '</p>'; |
|
| 150 | - } |
|
| 135 | + if ($vCard -> IMPP) |
|
| 136 | + { |
|
| 137 | + echo '<p><h4>Instant messaging</h4>'; |
|
| 138 | + foreach ($vCard -> IMPP as $IMPP) |
|
| 139 | + { |
|
| 140 | + if (is_scalar($IMPP)) |
|
| 141 | + { |
|
| 142 | + echo $IMPP.'<br />'; |
|
| 143 | + } |
|
| 144 | + else |
|
| 145 | + { |
|
| 146 | + echo $IMPP['value'].'<br/ >'; |
|
| 147 | + } |
|
| 148 | + } |
|
| 149 | + echo '</p>'; |
|
| 150 | + } |
|
| 151 | 151 | |
| 152 | - if ($vCard -> ADR) |
|
| 153 | - { |
|
| 154 | - foreach ($vCard -> ADR as $Address) |
|
| 155 | - { |
|
| 156 | - echo '<p><h4>Address ('.implode(', ', $Address['type']).')</h4>'; |
|
| 157 | - echo 'Street address: <strong>'.($Address['streetaddress'] ? $Address['streetaddress'] : '-').'</strong><br />'. |
|
| 158 | - 'PO Box: <strong>'.($Address['pobox'] ? $Address['pobox'] : '-').'</strong><br />'. |
|
| 159 | - 'Extended address: <strong>'.($Address['extendedaddress'] ? $Address['extendedaddress'] : '-').'</strong><br />'. |
|
| 160 | - 'Locality: <strong>'.($Address['locality'] ? $Address['locality'] : '-').'</strong><br />'. |
|
| 161 | - 'Region: <strong>'.($Address['region'] ? $Address['region'] : '-').'</strong><br />'. |
|
| 162 | - 'ZIP/Post code: <strong>'.($Address['postalcode'] ? $Address['postalcode'] : '-').'</strong><br />'. |
|
| 163 | - 'Country: <strong>'.($Address['country'] ? $Address['country'] : '-').'</strong>'; |
|
| 164 | - } |
|
| 165 | - echo '</p>'; |
|
| 166 | - } |
|
| 152 | + if ($vCard -> ADR) |
|
| 153 | + { |
|
| 154 | + foreach ($vCard -> ADR as $Address) |
|
| 155 | + { |
|
| 156 | + echo '<p><h4>Address ('.implode(', ', $Address['type']).')</h4>'; |
|
| 157 | + echo 'Street address: <strong>'.($Address['streetaddress'] ? $Address['streetaddress'] : '-').'</strong><br />'. |
|
| 158 | + 'PO Box: <strong>'.($Address['pobox'] ? $Address['pobox'] : '-').'</strong><br />'. |
|
| 159 | + 'Extended address: <strong>'.($Address['extendedaddress'] ? $Address['extendedaddress'] : '-').'</strong><br />'. |
|
| 160 | + 'Locality: <strong>'.($Address['locality'] ? $Address['locality'] : '-').'</strong><br />'. |
|
| 161 | + 'Region: <strong>'.($Address['region'] ? $Address['region'] : '-').'</strong><br />'. |
|
| 162 | + 'ZIP/Post code: <strong>'.($Address['postalcode'] ? $Address['postalcode'] : '-').'</strong><br />'. |
|
| 163 | + 'Country: <strong>'.($Address['country'] ? $Address['country'] : '-').'</strong>'; |
|
| 164 | + } |
|
| 165 | + echo '</p>'; |
|
| 166 | + } |
|
| 167 | 167 | |
| 168 | - if ($vCard -> AGENT) |
|
| 169 | - { |
|
| 170 | - echo '<h4>Agents</h4>'; |
|
| 171 | - foreach ($vCard -> AGENT as $Agent) |
|
| 172 | - { |
|
| 173 | - if (is_scalar($Agent)) |
|
| 174 | - { |
|
| 175 | - echo '<div class="Agent">'.$Agent.'</div>'; |
|
| 176 | - } |
|
| 177 | - elseif (is_a($Agent, 'vCard')) |
|
| 178 | - { |
|
| 179 | - echo '<div class="Agent">'; |
|
| 180 | - OutputvCard($Agent); |
|
| 181 | - echo '</div>'; |
|
| 182 | - } |
|
| 183 | - } |
|
| 184 | - } |
|
| 185 | - } |
|
| 168 | + if ($vCard -> AGENT) |
|
| 169 | + { |
|
| 170 | + echo '<h4>Agents</h4>'; |
|
| 171 | + foreach ($vCard -> AGENT as $Agent) |
|
| 172 | + { |
|
| 173 | + if (is_scalar($Agent)) |
|
| 174 | + { |
|
| 175 | + echo '<div class="Agent">'.$Agent.'</div>'; |
|
| 176 | + } |
|
| 177 | + elseif (is_a($Agent, 'vCard')) |
|
| 178 | + { |
|
| 179 | + echo '<div class="Agent">'; |
|
| 180 | + OutputvCard($Agent); |
|
| 181 | + echo '</div>'; |
|
| 182 | + } |
|
| 183 | + } |
|
| 184 | + } |
|
| 185 | + } |
|
| 186 | 186 | |
| 187 | - $vCard = new vCard( |
|
| 188 | - 'Example3.0.vcf', // Path to vCard file |
|
| 189 | - false, // Raw vCard text, can be used instead of a file |
|
| 190 | - array( // Option array |
|
| 191 | - // This lets you get single values for elements that could contain multiple values but have only one value. |
|
| 192 | - // This defaults to false so every value that could have multiple values is returned as array. |
|
| 193 | - 'Collapse' => false |
|
| 194 | - ) |
|
| 195 | - ); |
|
| 187 | + $vCard = new vCard( |
|
| 188 | + 'Example3.0.vcf', // Path to vCard file |
|
| 189 | + false, // Raw vCard text, can be used instead of a file |
|
| 190 | + array( // Option array |
|
| 191 | + // This lets you get single values for elements that could contain multiple values but have only one value. |
|
| 192 | + // This defaults to false so every value that could have multiple values is returned as array. |
|
| 193 | + 'Collapse' => false |
|
| 194 | + ) |
|
| 195 | + ); |
|
| 196 | 196 | |
| 197 | - if (count($vCard) == 0) |
|
| 198 | - { |
|
| 199 | - throw new Exception('vCard test: empty vCard!'); |
|
| 200 | - } |
|
| 201 | - // if the file contains a single vCard, it is accessible directly. |
|
| 202 | - elseif (count($vCard) == 1) |
|
| 203 | - { |
|
| 204 | - OutputvCard($vCard); |
|
| 205 | - } |
|
| 206 | - // if the file contains multiple vCards, they are accessible as elements of an array |
|
| 207 | - else |
|
| 208 | - { |
|
| 209 | - foreach ($vCard as $Index => $vCardPart) |
|
| 210 | - { |
|
| 211 | - OutputvCard($vCardPart); |
|
| 212 | - } |
|
| 213 | - } |
|
| 197 | + if (count($vCard) == 0) |
|
| 198 | + { |
|
| 199 | + throw new Exception('vCard test: empty vCard!'); |
|
| 200 | + } |
|
| 201 | + // if the file contains a single vCard, it is accessible directly. |
|
| 202 | + elseif (count($vCard) == 1) |
|
| 203 | + { |
|
| 204 | + OutputvCard($vCard); |
|
| 205 | + } |
|
| 206 | + // if the file contains multiple vCards, they are accessible as elements of an array |
|
| 207 | + else |
|
| 208 | + { |
|
| 209 | + foreach ($vCard as $Index => $vCardPart) |
|
| 210 | + { |
|
| 211 | + OutputvCard($vCardPart); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | 214 | ?> |
| 215 | 215 | </body> |
| 216 | 216 | </html> |
@@ -34,19 +34,19 @@ discard block |
||
| 34 | 34 | */ |
| 35 | 35 | function OutputvCard(vCard $vCard) |
| 36 | 36 | { |
| 37 | - echo '<h2>'.$vCard -> FN[0].'</h2>'; |
|
| 37 | + echo '<h2>' . $vCard -> FN[0] . '</h2>'; |
|
| 38 | 38 | |
| 39 | - if ($vCard -> PHOTO) |
|
| 39 | + if($vCard -> PHOTO) |
|
| 40 | 40 | { |
| 41 | - foreach ($vCard -> PHOTO as $Photo) |
|
| 41 | + foreach($vCard -> PHOTO as $Photo) |
|
| 42 | 42 | { |
| 43 | - if ($Photo['encoding'] == 'b') |
|
| 43 | + if($Photo['encoding'] == 'b') |
|
| 44 | 44 | { |
| 45 | - echo '<img src="data:image/'.$Photo['type'][0].';base64,'.$Photo['value'].'" /><br />'; |
|
| 45 | + echo '<img src="data:image/' . $Photo['type'][0] . ';base64,' . $Photo['value'] . '" /><br />'; |
|
| 46 | 46 | } |
| 47 | 47 | else |
| 48 | 48 | { |
| 49 | - echo '<img src="'.$Photo['value'].'" /><br />'; |
|
| 49 | + echo '<img src="' . $Photo['value'] . '" /><br />'; |
|
| 50 | 50 | } |
| 51 | 51 | |
| 52 | 52 | /* |
@@ -67,114 +67,113 @@ discard block |
||
| 67 | 67 | } |
| 68 | 68 | } |
| 69 | 69 | |
| 70 | - foreach ($vCard -> N as $Name) |
|
| 70 | + foreach($vCard -> N as $Name) |
|
| 71 | 71 | { |
| 72 | - echo '<h3>Name: '.$Name['firstname'].' '.$Name['lastname'].'</h3>'; |
|
| 72 | + echo '<h3>Name: ' . $Name['firstname'] . ' ' . $Name['lastname'] . '</h3>'; |
|
| 73 | 73 | } |
| 74 | 74 | |
| 75 | - foreach ($vCard -> ORG as $Organization) |
|
| 75 | + foreach($vCard -> ORG as $Organization) |
|
| 76 | 76 | { |
| 77 | - echo '<h3>Organization: '.$Organization['name']. |
|
| 77 | + echo '<h3>Organization: ' . $Organization['name'] . |
|
| 78 | 78 | ($Organization['unit1'] || $Organization['unit2'] ? |
| 79 | - ' ('.implode(', ', array($Organization['unit1'], $Organization['unit2'])).')' : |
|
| 80 | - '' |
|
| 81 | - ).'</h3>'; |
|
| 79 | + ' (' . implode(', ', array($Organization['unit1'], $Organization['unit2'])) . ')' : '' |
|
| 80 | + ) . '</h3>'; |
|
| 82 | 81 | } |
| 83 | 82 | |
| 84 | - if ($vCard -> TEL) |
|
| 83 | + if($vCard -> TEL) |
|
| 85 | 84 | { |
| 86 | 85 | echo '<p><h4>Phone</h4>'; |
| 87 | - foreach ($vCard -> TEL as $Tel) |
|
| 86 | + foreach($vCard -> TEL as $Tel) |
|
| 88 | 87 | { |
| 89 | - if (is_scalar($Tel)) |
|
| 88 | + if(is_scalar($Tel)) |
|
| 90 | 89 | { |
| 91 | - echo $Tel.'<br />'; |
|
| 90 | + echo $Tel . '<br />'; |
|
| 92 | 91 | } |
| 93 | 92 | else |
| 94 | 93 | { |
| 95 | - echo $Tel['value'].' ('.implode(', ', $Tel['type']).')<br />'; |
|
| 94 | + echo $Tel['value'] . ' (' . implode(', ', $Tel['type']) . ')<br />'; |
|
| 96 | 95 | } |
| 97 | 96 | } |
| 98 | 97 | echo '</p>'; |
| 99 | 98 | } |
| 100 | 99 | |
| 101 | - if ($vCard -> EMAIL) |
|
| 100 | + if($vCard -> EMAIL) |
|
| 102 | 101 | { |
| 103 | 102 | echo '<p><h4>Email</h4>'; |
| 104 | - foreach ($vCard -> EMAIL as $Email) |
|
| 103 | + foreach($vCard -> EMAIL as $Email) |
|
| 105 | 104 | { |
| 106 | - if (is_scalar($Email)) |
|
| 105 | + if(is_scalar($Email)) |
|
| 107 | 106 | { |
| 108 | 107 | echo $Email; |
| 109 | 108 | } |
| 110 | 109 | else |
| 111 | 110 | { |
| 112 | - echo $Email['value'].' ('.implode(', ', $Email['type']).')<br />'; |
|
| 111 | + echo $Email['value'] . ' (' . implode(', ', $Email['type']) . ')<br />'; |
|
| 113 | 112 | } |
| 114 | 113 | } |
| 115 | 114 | echo '</p>'; |
| 116 | 115 | } |
| 117 | 116 | |
| 118 | - if ($vCard -> URL) |
|
| 117 | + if($vCard -> URL) |
|
| 119 | 118 | { |
| 120 | 119 | echo '<p><h4>URL</h4>'; |
| 121 | - foreach ($vCard -> URL as $URL) |
|
| 120 | + foreach($vCard -> URL as $URL) |
|
| 122 | 121 | { |
| 123 | - if (is_scalar($URL)) |
|
| 122 | + if(is_scalar($URL)) |
|
| 124 | 123 | { |
| 125 | - echo $URL.'<br />'; |
|
| 124 | + echo $URL . '<br />'; |
|
| 126 | 125 | } |
| 127 | 126 | else |
| 128 | 127 | { |
| 129 | - echo $URL['value'].'<br />'; |
|
| 128 | + echo $URL['value'] . '<br />'; |
|
| 130 | 129 | } |
| 131 | 130 | } |
| 132 | 131 | echo '</p>'; |
| 133 | 132 | } |
| 134 | 133 | |
| 135 | - if ($vCard -> IMPP) |
|
| 134 | + if($vCard -> IMPP) |
|
| 136 | 135 | { |
| 137 | 136 | echo '<p><h4>Instant messaging</h4>'; |
| 138 | - foreach ($vCard -> IMPP as $IMPP) |
|
| 137 | + foreach($vCard -> IMPP as $IMPP) |
|
| 139 | 138 | { |
| 140 | - if (is_scalar($IMPP)) |
|
| 139 | + if(is_scalar($IMPP)) |
|
| 141 | 140 | { |
| 142 | - echo $IMPP.'<br />'; |
|
| 141 | + echo $IMPP . '<br />'; |
|
| 143 | 142 | } |
| 144 | 143 | else |
| 145 | 144 | { |
| 146 | - echo $IMPP['value'].'<br/ >'; |
|
| 145 | + echo $IMPP['value'] . '<br/ >'; |
|
| 147 | 146 | } |
| 148 | 147 | } |
| 149 | 148 | echo '</p>'; |
| 150 | 149 | } |
| 151 | 150 | |
| 152 | - if ($vCard -> ADR) |
|
| 151 | + if($vCard -> ADR) |
|
| 153 | 152 | { |
| 154 | - foreach ($vCard -> ADR as $Address) |
|
| 153 | + foreach($vCard -> ADR as $Address) |
|
| 155 | 154 | { |
| 156 | - echo '<p><h4>Address ('.implode(', ', $Address['type']).')</h4>'; |
|
| 157 | - echo 'Street address: <strong>'.($Address['streetaddress'] ? $Address['streetaddress'] : '-').'</strong><br />'. |
|
| 158 | - 'PO Box: <strong>'.($Address['pobox'] ? $Address['pobox'] : '-').'</strong><br />'. |
|
| 159 | - 'Extended address: <strong>'.($Address['extendedaddress'] ? $Address['extendedaddress'] : '-').'</strong><br />'. |
|
| 160 | - 'Locality: <strong>'.($Address['locality'] ? $Address['locality'] : '-').'</strong><br />'. |
|
| 161 | - 'Region: <strong>'.($Address['region'] ? $Address['region'] : '-').'</strong><br />'. |
|
| 162 | - 'ZIP/Post code: <strong>'.($Address['postalcode'] ? $Address['postalcode'] : '-').'</strong><br />'. |
|
| 163 | - 'Country: <strong>'.($Address['country'] ? $Address['country'] : '-').'</strong>'; |
|
| 155 | + echo '<p><h4>Address (' . implode(', ', $Address['type']) . ')</h4>'; |
|
| 156 | + echo 'Street address: <strong>' . ($Address['streetaddress'] ? $Address['streetaddress'] : '-') . '</strong><br />' . |
|
| 157 | + 'PO Box: <strong>' . ($Address['pobox'] ? $Address['pobox'] : '-') . '</strong><br />' . |
|
| 158 | + 'Extended address: <strong>' . ($Address['extendedaddress'] ? $Address['extendedaddress'] : '-') . '</strong><br />' . |
|
| 159 | + 'Locality: <strong>' . ($Address['locality'] ? $Address['locality'] : '-') . '</strong><br />' . |
|
| 160 | + 'Region: <strong>' . ($Address['region'] ? $Address['region'] : '-') . '</strong><br />' . |
|
| 161 | + 'ZIP/Post code: <strong>' . ($Address['postalcode'] ? $Address['postalcode'] : '-') . '</strong><br />' . |
|
| 162 | + 'Country: <strong>' . ($Address['country'] ? $Address['country'] : '-') . '</strong>'; |
|
| 164 | 163 | } |
| 165 | 164 | echo '</p>'; |
| 166 | 165 | } |
| 167 | 166 | |
| 168 | - if ($vCard -> AGENT) |
|
| 167 | + if($vCard -> AGENT) |
|
| 169 | 168 | { |
| 170 | 169 | echo '<h4>Agents</h4>'; |
| 171 | - foreach ($vCard -> AGENT as $Agent) |
|
| 170 | + foreach($vCard -> AGENT as $Agent) |
|
| 172 | 171 | { |
| 173 | - if (is_scalar($Agent)) |
|
| 172 | + if(is_scalar($Agent)) |
|
| 174 | 173 | { |
| 175 | - echo '<div class="Agent">'.$Agent.'</div>'; |
|
| 174 | + echo '<div class="Agent">' . $Agent . '</div>'; |
|
| 176 | 175 | } |
| 177 | - elseif (is_a($Agent, 'vCard')) |
|
| 176 | + elseif(is_a($Agent, 'vCard')) |
|
| 178 | 177 | { |
| 179 | 178 | echo '<div class="Agent">'; |
| 180 | 179 | OutputvCard($Agent); |
@@ -194,19 +193,19 @@ discard block |
||
| 194 | 193 | ) |
| 195 | 194 | ); |
| 196 | 195 | |
| 197 | - if (count($vCard) == 0) |
|
| 196 | + if(count($vCard) == 0) |
|
| 198 | 197 | { |
| 199 | 198 | throw new Exception('vCard test: empty vCard!'); |
| 200 | 199 | } |
| 201 | 200 | // if the file contains a single vCard, it is accessible directly. |
| 202 | - elseif (count($vCard) == 1) |
|
| 201 | + elseif(count($vCard) == 1) |
|
| 203 | 202 | { |
| 204 | 203 | OutputvCard($vCard); |
| 205 | 204 | } |
| 206 | 205 | // if the file contains multiple vCards, they are accessible as elements of an array |
| 207 | 206 | else |
| 208 | 207 | { |
| 209 | - foreach ($vCard as $Index => $vCardPart) |
|
| 208 | + foreach($vCard as $Index => $vCardPart) |
|
| 210 | 209 | { |
| 211 | 210 | OutputvCard($vCardPart); |
| 212 | 211 | } |
@@ -133,6 +133,9 @@ discard block |
||
| 133 | 133 | return $path; |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | + /** |
|
| 137 | + * @param string $dir |
|
| 138 | + */ |
|
| 136 | 139 | public function rmtemp($dir) |
| 137 | 140 | { |
| 138 | 141 | if(is_dir($dir)) |
@@ -177,6 +180,10 @@ discard block |
||
| 177 | 180 | } |
| 178 | 181 | } |
| 179 | 182 | |
| 183 | + /** |
|
| 184 | + * @param string $inputfile |
|
| 185 | + * @param string $outputfile |
|
| 186 | + */ |
|
| 180 | 187 | public function base64_to_jpeg($inputfile, $outputfile) |
| 181 | 188 | { |
| 182 | 189 | // read data (binary) |
@@ -603,7 +603,7 @@ discard block |
||
| 603 | 603 | print " Added photo: " . basename($photo_file) . PHP_EOL; |
| 604 | 604 | } |
| 605 | 605 | else |
| 606 | - print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL; |
|
| 606 | + print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL; |
|
| 607 | 607 | } |
| 608 | 608 | elseif(substr($entry['photo_data'][0], 0, 4) == 'http') |
| 609 | 609 | { |
@@ -668,19 +668,19 @@ discard block |
||
| 668 | 668 | fclose($output); |
| 669 | 669 | print " Saved to file " . $this->config['output_file'] . PHP_EOL; |
| 670 | 670 | } |
| 671 | - if (array_key_exists('output_and_upload', $this->config) and $this->config['output_and_upload']) |
|
| 672 | - { |
|
| 673 | - $newphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/","",file_get_contents($this->config['output_file'],'r'),-1,$debugnewtsreplace)); |
|
| 674 | - print " INFO: Compare old and new phonebook file versions." . PHP_EOL . " INFO: old version: " . $oldphonebhash . PHP_EOL . " INFO: new version: " . $newphonebhash . PHP_EOL; |
|
| 675 | - if($oldphonebhash === $newphonebhash) |
|
| 676 | - { |
|
| 677 | - print " INFO: Same versions ==> No changes in phonebook or images" . PHP_EOL . " EXIT: No need to upload phonebook to the FRITZ!Box.". PHP_EOL; |
|
| 678 | - return 0; |
|
| 679 | - } |
|
| 680 | - else |
|
| 681 | - print " INFO: Different versions ==> Changes in phonebook." . PHP_EOL . " INFO: Changes dedected! Continue with upload." . PHP_EOL; |
|
| 671 | + if (array_key_exists('output_and_upload', $this->config) and $this->config['output_and_upload']) |
|
| 672 | + { |
|
| 673 | + $newphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/","",file_get_contents($this->config['output_file'],'r'),-1,$debugnewtsreplace)); |
|
| 674 | + print " INFO: Compare old and new phonebook file versions." . PHP_EOL . " INFO: old version: " . $oldphonebhash . PHP_EOL . " INFO: new version: " . $newphonebhash . PHP_EOL; |
|
| 675 | + if($oldphonebhash === $newphonebhash) |
|
| 676 | + { |
|
| 677 | + print " INFO: Same versions ==> No changes in phonebook or images" . PHP_EOL . " EXIT: No need to upload phonebook to the FRITZ!Box.". PHP_EOL; |
|
| 678 | + return 0; |
|
| 679 | + } |
|
| 680 | + else |
|
| 681 | + print " INFO: Different versions ==> Changes in phonebook." . PHP_EOL . " INFO: Changes dedected! Continue with upload." . PHP_EOL; |
|
| 682 | 682 | } |
| 683 | - else |
|
| 683 | + else |
|
| 684 | 684 | return 0; |
| 685 | 685 | } |
| 686 | 686 | // now we upload the photo jpgs first being stored in the |
@@ -811,10 +811,10 @@ discard block |
||
| 811 | 811 | ); |
| 812 | 812 | |
| 813 | 813 | $filefileds = array('PhonebookImportFile' => array( |
| 814 | - 'type' => 'text/xml', |
|
| 815 | - 'filename' => 'updatepb.xml', |
|
| 816 | - 'content' => $this->fbxml, |
|
| 817 | - ) |
|
| 814 | + 'type' => 'text/xml', |
|
| 815 | + 'filename' => 'updatepb.xml', |
|
| 816 | + 'content' => $this->fbxml, |
|
| 817 | + ) |
|
| 818 | 818 | ); |
| 819 | 819 | |
| 820 | 820 | $raw_result = $fritz->doPostFile($formfields, $filefileds); // send the command |
@@ -660,7 +660,7 @@ discard block |
||
| 660 | 660 | if(array_key_exists('output_file', $this->config)) |
| 661 | 661 | { |
| 662 | 662 | // build md5 hash of previous stored xml without <mod_time> Elements |
| 663 | - $oldphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/","",file_get_contents($this->config['output_file'],'r'),-1,$debugoldtsreplace)); |
|
| 663 | + $oldphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/", "", file_get_contents($this->config['output_file'], 'r'), -1, $debugoldtsreplace)); |
|
| 664 | 664 | $output = fopen($this->config['output_file'], 'w'); |
| 665 | 665 | if($output) |
| 666 | 666 | { |
@@ -668,13 +668,13 @@ discard block |
||
| 668 | 668 | fclose($output); |
| 669 | 669 | print " Saved to file " . $this->config['output_file'] . PHP_EOL; |
| 670 | 670 | } |
| 671 | - if (array_key_exists('output_and_upload', $this->config) and $this->config['output_and_upload']) |
|
| 671 | + if(array_key_exists('output_and_upload', $this->config) and $this->config['output_and_upload']) |
|
| 672 | 672 | { |
| 673 | - $newphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/","",file_get_contents($this->config['output_file'],'r'),-1,$debugnewtsreplace)); |
|
| 673 | + $newphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/", "", file_get_contents($this->config['output_file'], 'r'), -1, $debugnewtsreplace)); |
|
| 674 | 674 | print " INFO: Compare old and new phonebook file versions." . PHP_EOL . " INFO: old version: " . $oldphonebhash . PHP_EOL . " INFO: new version: " . $newphonebhash . PHP_EOL; |
| 675 | 675 | if($oldphonebhash === $newphonebhash) |
| 676 | 676 | { |
| 677 | - print " INFO: Same versions ==> No changes in phonebook or images" . PHP_EOL . " EXIT: No need to upload phonebook to the FRITZ!Box.". PHP_EOL; |
|
| 677 | + print " INFO: Same versions ==> No changes in phonebook or images" . PHP_EOL . " EXIT: No need to upload phonebook to the FRITZ!Box." . PHP_EOL; |
|
| 678 | 678 | return 0; |
| 679 | 679 | } |
| 680 | 680 | else |
@@ -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; |
@@ -122,7 +127,9 @@ discard block |
||
| 122 | 127 | public function mktemp($dir, $prefix = '', $mode = 0700) |
| 123 | 128 | { |
| 124 | 129 | if(substr($dir, -1) != '/') |
| 125 | - $dir .= '/'; |
|
| 130 | + { |
|
| 131 | + $dir .= '/'; |
|
| 132 | + } |
|
| 126 | 133 | |
| 127 | 134 | do |
| 128 | 135 | { |
@@ -143,7 +150,12 @@ discard block |
||
| 143 | 150 | if($object != "." && $object != "..") |
| 144 | 151 | { |
| 145 | 152 | if(filetype($dir . "/" . $object) == "dir") |
| 146 | - rrmdir($dir . "/" . $object); else unlink($dir . "/" . $object); |
|
| 153 | + { |
|
| 154 | + rrmdir($dir . "/" . $object); |
|
| 155 | + } |
|
| 156 | + else { |
|
| 157 | + unlink($dir . "/" . $object); |
|
| 158 | + } |
|
| 147 | 159 | } |
| 148 | 160 | } |
| 149 | 161 | reset($objects); |
@@ -157,18 +169,25 @@ discard block |
||
| 157 | 169 | { |
| 158 | 170 | // Check if there are valid base64 characters |
| 159 | 171 | if(!preg_match('/^[a-zA-Z0-9\/\r\n+]*={0,2}$/', $str)) |
| 160 | - return false; |
|
| 172 | + { |
|
| 173 | + return false; |
|
| 174 | + } |
|
| 161 | 175 | |
| 162 | 176 | // Decode the string in strict mode and check the results |
| 163 | 177 | $decoded = base64_decode($str, true); |
| 164 | 178 | if($decoded === false) |
| 165 | - return false; |
|
| 179 | + { |
|
| 180 | + return false; |
|
| 181 | + } |
|
| 166 | 182 | |
| 167 | 183 | // Encode the string again |
| 168 | 184 | if(base64_encode($decoded) === $str) |
| 169 | - return true; |
|
| 170 | - else |
|
| 171 | - return false; |
|
| 185 | + { |
|
| 186 | + return true; |
|
| 187 | + } |
|
| 188 | + else { |
|
| 189 | + return false; |
|
| 190 | + } |
|
| 172 | 191 | } |
| 173 | 192 | catch(Exception $e) |
| 174 | 193 | { |
@@ -209,7 +228,9 @@ discard block |
||
| 209 | 228 | // set the vcard extension in case the user |
| 210 | 229 | // defined it in the config |
| 211 | 230 | if(isset($conf['extension'])) |
| 212 | - $carddav->setVcardExtension($conf['extension']); |
|
| 231 | + { |
|
| 232 | + $carddav->setVcardExtension($conf['extension']); |
|
| 233 | + } |
|
| 213 | 234 | |
| 214 | 235 | // retrieve data from the CardDAV server now |
| 215 | 236 | $xmldata = $carddav->get(); |
@@ -218,7 +239,9 @@ discard block |
||
| 218 | 239 | // CardDAV server and if not reencode it since the FRITZ!Box |
| 219 | 240 | // requires UTF-8 encoded data |
| 220 | 241 | if(iconv('utf-8', 'utf-8//IGNORE', $xmldata) != $xmldata) |
| 221 | - $xmldata = utf8_encode($xmldata); |
|
| 242 | + { |
|
| 243 | + $xmldata = utf8_encode($xmldata); |
|
| 244 | + } |
|
| 222 | 245 | |
| 223 | 246 | // read raw_vcard data from xml response |
| 224 | 247 | $raw_vcards = array(); |
@@ -240,10 +263,14 @@ discard block |
||
| 240 | 263 | $vcard_obj = new vCard(false, $v); |
| 241 | 264 | $name_arr = null; |
| 242 | 265 | if(isset($vcard_obj->n[0])) |
| 243 | - $name_arr = $vcard_obj->n[0]; |
|
| 266 | + { |
|
| 267 | + $name_arr = $vcard_obj->n[0]; |
|
| 268 | + } |
|
| 244 | 269 | $org_arr = null; |
| 245 | 270 | if(isset($vcard_obj->org[0])) |
| 246 | - $org_arr = $vcard_obj->org[0]; |
|
| 271 | + { |
|
| 272 | + $org_arr = $vcard_obj->org[0]; |
|
| 273 | + } |
|
| 247 | 274 | $addnames = ''; |
| 248 | 275 | $prefix = ''; |
| 249 | 276 | $suffix = ''; |
@@ -251,16 +278,24 @@ discard block |
||
| 251 | 278 | |
| 252 | 279 | // Build name Parts if existing ans switch to true in config |
| 253 | 280 | if(isset($name_arr['prefixes']) and $this->config['prefix']) |
| 254 | - $prefix = trim($name_arr['prefixes']); |
|
| 281 | + { |
|
| 282 | + $prefix = trim($name_arr['prefixes']); |
|
| 283 | + } |
|
| 255 | 284 | |
| 256 | 285 | if(isset($name_arr['suffixes']) and $this->config['suffix']) |
| 257 | - $suffix = trim($name_arr['suffixes']); |
|
| 286 | + { |
|
| 287 | + $suffix = trim($name_arr['suffixes']); |
|
| 288 | + } |
|
| 258 | 289 | |
| 259 | 290 | if(isset($name_arr['additionalnames']) and $this->config['addnames']) |
| 260 | - $addnames = trim($name_arr['additionalnames']); |
|
| 291 | + { |
|
| 292 | + $addnames = trim($name_arr['additionalnames']); |
|
| 293 | + } |
|
| 261 | 294 | |
| 262 | 295 | if(isset($org_arr['name']) and $this->config['orgname']) |
| 263 | - $orgname = trim($org_arr['name']); |
|
| 296 | + { |
|
| 297 | + $orgname = trim($org_arr['name']); |
|
| 298 | + } |
|
| 264 | 299 | |
| 265 | 300 | $firstname = trim($name_arr['firstname']); |
| 266 | 301 | $lastname = trim($name_arr['lastname']); |
@@ -280,71 +315,97 @@ discard block |
||
| 280 | 315 | |
| 281 | 316 | // Prefix |
| 282 | 317 | if(!empty($prefix)) |
| 283 | - $name .= $prefix; |
|
| 318 | + { |
|
| 319 | + $name .= $prefix; |
|
| 320 | + } |
|
| 284 | 321 | |
| 285 | 322 | if($format == 0) |
| 286 | 323 | { |
| 287 | 324 | // Lastname |
| 288 | 325 | if(!empty($name) and !empty($lastname)) |
| 289 | - $name .= ' ' . $lastname; |
|
| 290 | - else |
|
| 291 | - $name .= $lastname; |
|
| 326 | + { |
|
| 327 | + $name .= ' ' . $lastname; |
|
| 328 | + } |
|
| 329 | + else { |
|
| 330 | + $name .= $lastname; |
|
| 331 | + } |
|
| 292 | 332 | } |
| 293 | 333 | else |
| 294 | 334 | { |
| 295 | 335 | // Firstname |
| 296 | 336 | if(!empty($name) and !empty($firstname)) |
| 297 | - $name .= ' ' . $firstname; |
|
| 298 | - else |
|
| 299 | - $name .= $firstname; |
|
| 337 | + { |
|
| 338 | + $name .= ' ' . $firstname; |
|
| 339 | + } |
|
| 340 | + else { |
|
| 341 | + $name .= $firstname; |
|
| 342 | + } |
|
| 300 | 343 | } |
| 301 | 344 | |
| 302 | 345 | if($format == 2) |
| 303 | 346 | { |
| 304 | 347 | // AdditionalNames |
| 305 | 348 | if(!empty($name) and !empty($addnames)) |
| 306 | - $name .= ' ' . $addnames; |
|
| 307 | - else |
|
| 308 | - $name .= $addnames; |
|
| 349 | + { |
|
| 350 | + $name .= ' ' . $addnames; |
|
| 351 | + } |
|
| 352 | + else { |
|
| 353 | + $name .= $addnames; |
|
| 354 | + } |
|
| 309 | 355 | } |
| 310 | 356 | |
| 311 | 357 | if($format == 0) |
| 312 | 358 | { |
| 313 | 359 | // Firstname |
| 314 | 360 | if(!empty($name) and !empty($firstname)) |
| 315 | - $name .= ', ' . $firstname; |
|
| 316 | - else |
|
| 317 | - $name .= $firstname; |
|
| 361 | + { |
|
| 362 | + $name .= ', ' . $firstname; |
|
| 363 | + } |
|
| 364 | + else { |
|
| 365 | + $name .= $firstname; |
|
| 366 | + } |
|
| 318 | 367 | } |
| 319 | 368 | else |
| 320 | 369 | { |
| 321 | 370 | // Lastname |
| 322 | 371 | if(!empty($name) and !empty($lastname)) |
| 323 | - $name .= ' ' . $lastname; |
|
| 324 | - else |
|
| 325 | - $name .= $lastname; |
|
| 372 | + { |
|
| 373 | + $name .= ' ' . $lastname; |
|
| 374 | + } |
|
| 375 | + else { |
|
| 376 | + $name .= $lastname; |
|
| 377 | + } |
|
| 326 | 378 | } |
| 327 | 379 | |
| 328 | 380 | if($format != 2) |
| 329 | 381 | { |
| 330 | 382 | // AdditionalNames |
| 331 | 383 | if(!empty($name) and !empty($addnames)) |
| 332 | - $name .= ' ' . $addnames; |
|
| 333 | - else |
|
| 334 | - $name .= $addnames; |
|
| 384 | + { |
|
| 385 | + $name .= ' ' . $addnames; |
|
| 386 | + } |
|
| 387 | + else { |
|
| 388 | + $name .= $addnames; |
|
| 389 | + } |
|
| 335 | 390 | } |
| 336 | 391 | |
| 337 | 392 | // Suffix |
| 338 | 393 | if(!empty($name) and !empty($suffix)) |
| 339 | - $name .= ' ' . $suffix; |
|
| 340 | - else |
|
| 341 | - $name .= $suffix; |
|
| 394 | + { |
|
| 395 | + $name .= ' ' . $suffix; |
|
| 396 | + } |
|
| 397 | + else { |
|
| 398 | + $name .= $suffix; |
|
| 399 | + } |
|
| 342 | 400 | |
| 343 | 401 | // OrgName |
| 344 | 402 | if(!empty($name) and !empty($orgname)) |
| 345 | - $name .= ' (' . $orgname . ')'; |
|
| 346 | - else |
|
| 347 | - $name .= $orgname; |
|
| 403 | + { |
|
| 404 | + $name .= ' (' . $orgname . ')'; |
|
| 405 | + } |
|
| 406 | + else { |
|
| 407 | + $name .= $orgname; |
|
| 408 | + } |
|
| 348 | 409 | |
| 349 | 410 | // make sure to trim whitespaces and double spaces |
| 350 | 411 | $name = trim(str_replace(' ', ' ', $name)); |
@@ -361,15 +422,19 @@ discard block |
||
| 361 | 422 | $photo = str_replace(array(',', '&', ' ', '/', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', 'á', 'à', 'ó', 'ò', 'ú', 'ù', 'í', 'ø'), |
| 362 | 423 | array('', '_', '_', '_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'a', 'o', 'o', 'u', 'u', 'i', 'oe'), $name); |
| 363 | 424 | } |
| 364 | - else |
|
| 365 | - $photo = ''; |
|
| 425 | + else { |
|
| 426 | + $photo = ''; |
|
| 427 | + } |
|
| 366 | 428 | |
| 367 | 429 | // phone |
| 368 | 430 | $phone_no = array(); |
| 369 | 431 | if($vcard_obj->categories) |
| 370 | - $categories = $vcard_obj->categories[0]; |
|
| 371 | - else |
|
| 372 | - $categories = array(); |
|
| 432 | + { |
|
| 433 | + $categories = $vcard_obj->categories[0]; |
|
| 434 | + } |
|
| 435 | + else { |
|
| 436 | + $categories = array(); |
|
| 437 | + } |
|
| 373 | 438 | |
| 374 | 439 | // check for quickdial entry |
| 375 | 440 | if(isset($vcard_obj->note[0])) |
@@ -404,9 +469,10 @@ discard block |
||
| 404 | 469 | break; |
| 405 | 470 | } |
| 406 | 471 | } |
| 407 | - } |
|
| 408 | - else |
|
| 409 | - $add_entry = 1; |
|
| 472 | + } |
|
| 473 | + else { |
|
| 474 | + $add_entry = 1; |
|
| 475 | + } |
|
| 410 | 476 | |
| 411 | 477 | if($add_entry == 1) |
| 412 | 478 | { |
@@ -431,9 +497,13 @@ discard block |
||
| 431 | 497 | { |
| 432 | 498 | //Set quickdial |
| 433 | 499 | if($value == 1) |
| 434 | - print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n"; |
|
| 500 | + { |
|
| 501 | + print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n"; |
|
| 502 | + } |
|
| 435 | 503 | elseif($value >= 100) |
| 436 | - print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n"; |
|
| 504 | + { |
|
| 505 | + print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n"; |
|
| 506 | + } |
|
| 437 | 507 | |
| 438 | 508 | $quickdial = $value; |
| 439 | 509 | } |
@@ -443,23 +513,38 @@ discard block |
||
| 443 | 513 | |
| 444 | 514 | // find out priority |
| 445 | 515 | if(in_array("pref", $typearr_lower)) |
| 446 | - $prio = 1; |
|
| 516 | + { |
|
| 517 | + $prio = 1; |
|
| 518 | + } |
|
| 447 | 519 | |
| 448 | 520 | // set the proper type |
| 449 | 521 | if(in_array("cell", $typearr_lower)) |
| 450 | - $type = "mobile"; |
|
| 522 | + { |
|
| 523 | + $type = "mobile"; |
|
| 524 | + } |
|
| 451 | 525 | elseif(in_array("home", $typearr_lower)) |
| 452 | - $type = "home"; |
|
| 526 | + { |
|
| 527 | + $type = "home"; |
|
| 528 | + } |
|
| 453 | 529 | elseif(in_array("fax", $typearr_lower)) |
| 454 | - $type = "fax_work"; |
|
| 530 | + { |
|
| 531 | + $type = "fax_work"; |
|
| 532 | + } |
|
| 455 | 533 | elseif(in_array("work", $typearr_lower)) |
| 456 | - $type = "work"; |
|
| 534 | + { |
|
| 535 | + $type = "work"; |
|
| 536 | + } |
|
| 457 | 537 | elseif(in_array("other", $typearr_lower)) |
| 458 | - $type = "other"; |
|
| 538 | + { |
|
| 539 | + $type = "other"; |
|
| 540 | + } |
|
| 459 | 541 | elseif(in_array("dom", $typearr_lower)) |
| 460 | - $type = "other"; |
|
| 461 | - else |
|
| 462 | - continue; |
|
| 542 | + { |
|
| 543 | + $type = "other"; |
|
| 544 | + } |
|
| 545 | + else { |
|
| 546 | + continue; |
|
| 547 | + } |
|
| 463 | 548 | } |
| 464 | 549 | $phone_no[] = array("type"=>$type, "prio"=>$prio, "quickdial"=>$quickdial, "value" => $this->_clear_phone_number($phone_number)); |
| 465 | 550 | } |
@@ -479,13 +564,20 @@ discard block |
||
| 479 | 564 | $email = $e['value']; |
| 480 | 565 | $typearr_lower = unserialize(strtolower(serialize($e['type']))); |
| 481 | 566 | if(in_array("work", $typearr_lower)) |
| 482 | - $type_email = "work"; |
|
| 567 | + { |
|
| 568 | + $type_email = "work"; |
|
| 569 | + } |
|
| 483 | 570 | elseif(in_array("home", $typearr_lower)) |
| 484 | - $type_email = "home"; |
|
| 571 | + { |
|
| 572 | + $type_email = "home"; |
|
| 573 | + } |
|
| 485 | 574 | elseif(in_array("other", $typearr_lower)) |
| 486 | - $type_email = "other"; |
|
| 487 | - else |
|
| 488 | - continue; |
|
| 575 | + { |
|
| 576 | + $type_email = "other"; |
|
| 577 | + } |
|
| 578 | + else { |
|
| 579 | + continue; |
|
| 580 | + } |
|
| 489 | 581 | } |
| 490 | 582 | |
| 491 | 583 | // DEBUG: print out the email address on the console |
@@ -513,7 +605,9 @@ discard block |
||
| 513 | 605 | public function build_fb_xml() |
| 514 | 606 | { |
| 515 | 607 | if(empty($this->entries)) |
| 516 | - throw new Exception('No entries available! Call get_carddav_entries or set $this->entries manually!'); |
|
| 608 | + { |
|
| 609 | + throw new Exception('No entries available! Call get_carddav_entries or set $this->entries manually!'); |
|
| 610 | + } |
|
| 517 | 611 | |
| 518 | 612 | // create FB XML in utf-8 format |
| 519 | 613 | $root = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8"?><phonebooks><phonebook></phonebook></phonebooks>'); |
@@ -552,7 +646,9 @@ discard block |
||
| 552 | 646 | |
| 553 | 647 | // output a warning if no telephone number was found |
| 554 | 648 | if($id == 0) |
| 555 | - print " WARNING: no phone entry found. VCard will be ignored." . PHP_EOL; |
|
| 649 | + { |
|
| 650 | + print " WARNING: no phone entry found. VCard will be ignored." . PHP_EOL; |
|
| 651 | + } |
|
| 556 | 652 | |
| 557 | 653 | // email: put the email addresses into the fritzbox xml file |
| 558 | 654 | $email = $contact->addChild("services"); |
@@ -602,8 +698,9 @@ discard block |
||
| 602 | 698 | |
| 603 | 699 | print " Added photo: " . basename($photo_file) . PHP_EOL; |
| 604 | 700 | } |
| 605 | - else |
|
| 606 | - print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL; |
|
| 701 | + else { |
|
| 702 | + print " WARNING: Only jpg contact photos are currently supported." . PHP_EOL; |
|
| 703 | + } |
|
| 607 | 704 | } |
| 608 | 705 | elseif(substr($entry['photo_data'][0], 0, 4) == 'http') |
| 609 | 706 | { |
@@ -612,8 +709,9 @@ discard block |
||
| 612 | 709 | |
| 613 | 710 | print " Added photo: " . $entry['photo_data'][0] . PHP_EOL; |
| 614 | 711 | } |
| 615 | - else |
|
| 616 | - print " WARNING: Only VCard embedded photo data or a reference URL is currently supported." . PHP_EOL; |
|
| 712 | + else { |
|
| 713 | + print " WARNING: Only VCard embedded photo data or a reference URL is currently supported." . PHP_EOL; |
|
| 714 | + } |
|
| 617 | 715 | } |
| 618 | 716 | |
| 619 | 717 | $contact->addChild("services"); |
@@ -622,7 +720,9 @@ discard block |
||
| 622 | 720 | } |
| 623 | 721 | |
| 624 | 722 | if($root->asXML() !== false) |
| 625 | - $this->fbxml = $root->asXML(); |
|
| 723 | + { |
|
| 724 | + $this->fbxml = $root->asXML(); |
|
| 725 | + } |
|
| 626 | 726 | else |
| 627 | 727 | { |
| 628 | 728 | print " ERROR: created XML data isn't well-formed." . PHP_EOL; |
@@ -639,19 +739,27 @@ discard block |
||
| 639 | 739 | public function _concat($text1, $text2) |
| 640 | 740 | { |
| 641 | 741 | if($text1 == '') |
| 642 | - return $text2; |
|
| 742 | + { |
|
| 743 | + return $text2; |
|
| 744 | + } |
|
| 643 | 745 | elseif($text2 == '') |
| 644 | - return $text1; |
|
| 645 | - else |
|
| 646 | - return $text1 . ", " . $text2; |
|
| 746 | + { |
|
| 747 | + return $text1; |
|
| 748 | + } |
|
| 749 | + else { |
|
| 750 | + return $text1 . ", " . $text2; |
|
| 751 | + } |
|
| 647 | 752 | } |
| 648 | 753 | |
| 649 | 754 | public function _parse_fb_result($text) |
| 650 | 755 | { |
| 651 | 756 | if(preg_match("/\<h2\>([^\<]+)\<\/h2\>/", $text, $matches) == 1 && !empty($matches)) |
| 652 | - return $matches[1]; |
|
| 653 | - else |
|
| 654 | - return "Error while uploading xml to fritzbox"; |
|
| 757 | + { |
|
| 758 | + return $matches[1]; |
|
| 759 | + } |
|
| 760 | + else { |
|
| 761 | + return "Error while uploading xml to fritzbox"; |
|
| 762 | + } |
|
| 655 | 763 | } |
| 656 | 764 | |
| 657 | 765 | public function upload_to_fb() |
@@ -673,15 +781,17 @@ discard block |
||
| 673 | 781 | $newphonebhash = md5(preg_replace("/<mod_time>(\\d{10})/","",file_get_contents($this->config['output_file'],'r'),-1,$debugnewtsreplace)); |
| 674 | 782 | print " INFO: Compare old and new phonebook file versions." . PHP_EOL . " INFO: old version: " . $oldphonebhash . PHP_EOL . " INFO: new version: " . $newphonebhash . PHP_EOL; |
| 675 | 783 | if($oldphonebhash === $newphonebhash) |
| 676 | - { |
|
| 784 | + { |
|
| 677 | 785 | print " INFO: Same versions ==> No changes in phonebook or images" . PHP_EOL . " EXIT: No need to upload phonebook to the FRITZ!Box.". PHP_EOL; |
| 678 | 786 | return 0; |
| 679 | 787 | } |
| 680 | - else |
|
| 681 | - print " INFO: Different versions ==> Changes in phonebook." . PHP_EOL . " INFO: Changes dedected! Continue with upload." . PHP_EOL; |
|
| 788 | + else { |
|
| 789 | + print " INFO: Different versions ==> Changes in phonebook." . PHP_EOL . " INFO: Changes dedected! Continue with upload." . PHP_EOL; |
|
| 790 | + } |
|
| 682 | 791 | } |
| 683 | - else |
|
| 684 | - return 0; |
|
| 792 | + else { |
|
| 793 | + return 0; |
|
| 794 | + } |
|
| 685 | 795 | } |
| 686 | 796 | // now we upload the photo jpgs first being stored in the |
| 687 | 797 | // temp directory. |
@@ -751,12 +861,16 @@ discard block |
||
| 751 | 861 | |
| 752 | 862 | ftp_pasv($conn_id, true); |
| 753 | 863 | if(!ftp_put($conn_id, $remote_path . "/" . $file, $fileinfo->getPathname(), FTP_BINARY)) |
| 754 | - print " ERROR: while uploading file " . $fileinfo->getFilename() . PHP_EOL; |
|
| 755 | - else |
|
| 756 | - print " ok." . PHP_EOL; |
|
| 864 | + { |
|
| 865 | + print " ERROR: while uploading file " . $fileinfo->getFilename() . PHP_EOL; |
|
| 866 | + } |
|
| 867 | + else { |
|
| 868 | + print " ok." . PHP_EOL; |
|
| 869 | + } |
|
| 870 | + } |
|
| 871 | + else { |
|
| 872 | + print " ok." . PHP_EOL; |
|
| 757 | 873 | } |
| 758 | - else |
|
| 759 | - print " ok." . PHP_EOL; |
|
| 760 | 874 | |
| 761 | 875 | // cleanup old files |
| 762 | 876 | foreach($all_existing_files as $existing_file) |
@@ -768,20 +882,23 @@ discard block |
||
| 768 | 882 | } |
| 769 | 883 | } |
| 770 | 884 | } |
| 771 | - else |
|
| 772 | - print " already exists." . PHP_EOL; |
|
| 885 | + else { |
|
| 886 | + print " already exists." . PHP_EOL; |
|
| 887 | + } |
|
| 773 | 888 | } |
| 774 | 889 | } |
| 775 | 890 | } |
| 776 | 891 | } |
| 777 | - else |
|
| 778 | - print " ERROR: couldn't login to FTP-server '" . $ftp_server . "' with provided username/password settings." . PHP_EOL; |
|
| 892 | + else { |
|
| 893 | + print " ERROR: couldn't login to FTP-server '" . $ftp_server . "' with provided username/password settings." . PHP_EOL; |
|
| 894 | + } |
|
| 779 | 895 | |
| 780 | 896 | // close ftp connection |
| 781 | 897 | ftp_close($conn_id); |
| 782 | 898 | } |
| 783 | - else |
|
| 784 | - print " ERROR: couldn't connect to FTP server '" . $ftp_server . "'." . PHP_EOL; |
|
| 899 | + else { |
|
| 900 | + print " ERROR: couldn't connect to FTP server '" . $ftp_server . "'." . PHP_EOL; |
|
| 901 | + } |
|
| 785 | 902 | |
| 786 | 903 | // in case numeric IP is given, try to resolve to hostname. Otherwise Fritzbox may decline login, because it is determine to be (prohibited) remote access |
| 787 | 904 | $hostname = $this->config['fritzbox_ip']; |
@@ -789,7 +906,9 @@ discard block |
||
| 789 | 906 | { |
| 790 | 907 | $hostname = gethostbyaddr($hostname); |
| 791 | 908 | if($hostname == $this->config['fritzbox_ip']) |
| 792 | - print " WARNING: Unable to get hostname for IP address (" . $this->config['fritzbox_ip'] . ") <" . $hostname . "<" . PHP_EOL; |
|
| 909 | + { |
|
| 910 | + print " WARNING: Unable to get hostname for IP address (" . $this->config['fritzbox_ip'] . ") <" . $hostname . "<" . PHP_EOL; |
|
| 911 | + } |
|
| 793 | 912 | else |
| 794 | 913 | { |
| 795 | 914 | print " INFO: Given IP address (" . $this->config['fritzbox_ip'] . ") has hostname " . $hostname . "." . PHP_EOL; |