Example #1
0
 public function get_carddav_entries()
 {
     $entries = array();
     foreach ($this->config['carddav'] as $conf) {
         print " " . $conf['url'] . PHP_EOL;
         $carddav = new CardDavPHP\CardDavBackend($conf['url']);
         $carddav->setAuth($conf['user'], $conf['pw']);
         // set the vcard extension in case the user
         // defined it in the config
         if (isset($conf['extension'])) {
             $carddav->setVcardExtension($conf['extension']);
         }
         // retrieve data from the CardDAV server now
         $xmldata = $carddav->get();
         // identify if we received UTF-8 encoded data from the
         // CardDAV server and if not reencode it since the FRITZ!Box
         // requires UTF-8 encoded data
         if (iconv('utf-8', 'utf-8//IGNORE', $xmldata) != $xmldata) {
             $xmldata = utf8_encode($xmldata);
         }
         // read raw_vcard data from xml response
         $raw_vcards = array();
         $xmlvcard = new SimpleXMLElement($xmldata);
         foreach ($xmlvcard->element as $vcard_element) {
             $id = $vcard_element->id->__toString();
             $value = (string) $vcard_element->vcard->__toString();
             $raw_vcards[$id] = $value;
         }
         print "  " . count($raw_vcards) . " VCards retrieved" . PHP_EOL;
         // parse raw_vcards
         $result = array();
         foreach ($raw_vcards as $v) {
             $vcard_obj = new vCard(false, $v);
             // name
             $name_arr = $vcard_obj->n[0];
             $name = $this->_concat($this->_concat($name_arr['lastname'], $name_arr['firstname']), $name_arr['additionalnames']);
             // if name is empty we take organization instead
             if (empty($name)) {
                 $name_arr = $vcard_obj->org[0];
                 $name = $name_arr['name'];
             }
             // format filename of contact photo; remove special letters
             if ($vcard_obj->photo) {
                 $photo = str_replace(array(',', '&', ' ', '/', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', 'á', 'à', 'ó', 'ò', 'ú', 'ù', 'í'), array('', '_', '_', '_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'a', 'o', 'o', 'u', 'u', 'i'), $name);
             } else {
                 $photo = '';
             }
             // phone
             $phone_no = array();
             if ($vcard_obj->categories) {
                 $categories = $vcard_obj->categories[0];
             } else {
                 $categories = array('');
             }
             // e-mail addresses
             $email_add = array();
             if (in_array($this->config['group_vip'], $categories)) {
                 $vip = 1;
             } else {
                 $vip = 0;
             }
             if (array_key_exists('group_filter', $this->config)) {
                 $add_entry = 0;
                 foreach ($this->config['group_filter'] as $group_filter) {
                     if (in_array($group_filter, $categories)) {
                         $add_entry = 1;
                         break;
                     }
                 }
             } else {
                 $add_entry = 1;
             }
             if ($add_entry == 1) {
                 foreach ($vcard_obj->tel as $t) {
                     $prio = 0;
                     if (!is_array($t) || empty($t['type'])) {
                         $type = "mobile";
                         $phone_number = $t;
                     } else {
                         $phone_number = $t['value'];
                         $typearr_lower = unserialize(strtolower(serialize($t['type'])));
                         // find out priority
                         if (in_array("pref", $typearr_lower)) {
                             $prio = 1;
                         }
                         // set the proper type
                         if (in_array("work", $typearr_lower)) {
                             $type = "work";
                         } elseif (in_array("cell", $typearr_lower)) {
                             $type = "mobile";
                         } elseif (in_array("home", $typearr_lower)) {
                             $type = "home";
                         } elseif (in_array("fax", $typearr_lower)) {
                             $type = "fax_work";
                         } elseif (in_array("other", $typearr_lower)) {
                             $type = "other";
                         } elseif (in_array("dom", $typearr_lower)) {
                             $type = "other";
                         } else {
                             continue;
                         }
                     }
                     $phone_no[] = array("type" => $type, "prio" => $prio, "value" => $this->_clear_phone_number($phone_number));
                 }
                 // request email address and type
                 if ($vcard_obj->email) {
                     foreach ($vcard_obj->email as $e) {
                         if (empty($e['type'])) {
                             $type_email = "work";
                             $email = $e;
                         } else {
                             $email = $e['value'];
                             $typearr_lower = unserialize(strtolower(serialize($e['type'])));
                             if (in_array("work", $typearr_lower)) {
                                 $type_email = "work";
                             } elseif (in_array("home", $typearr_lower)) {
                                 $type_email = "home";
                             } elseif (in_array("other", $typearr_lower)) {
                                 $type_email = "other";
                             } else {
                                 continue;
                             }
                         }
                         // DEBUG: print out the email address on the console
                         //print $type_email.": ".$email."\n";
                         $email_add[] = array("type" => $type_email, "value" => $email);
                     }
                 }
                 $entries[] = array("realName" => $name, "telephony" => $phone_no, "email" => $email_add, "vip" => $vip, "photo" => $photo, "photo_data" => $vcard_obj->photo);
             }
         }
     }
     $this->entries = $entries;
 }
Example #2
0
 public function get_carddav_entries()
 {
     $entries = array();
     $snum = 0;
     if (is_array($this->config['carddav'])) {
         foreach ($this->config['carddav'] as $conf) {
             print " [" . $snum . "]: " . $conf['url'] . " ";
             $carddav = new CardDavPHP\CardDavBackend($conf['url']);
             $carddav->setAuth($conf['user'], $conf['pw']);
             // set the vcard extension in case the user
             // defined it in the config
             if (isset($conf['extension'])) {
                 $carddav->setVcardExtension($conf['extension']);
             }
             // retrieve data from the CardDAV server now
             $xmldata = $carddav->get();
             // identify if we received UTF-8 encoded data from the
             // CardDAV server and if not reencode it since the FRITZ!Box
             // requires UTF-8 encoded data
             if (iconv('utf-8', 'utf-8//IGNORE', $xmldata) != $xmldata) {
                 $xmldata = utf8_encode($xmldata);
             }
             // read raw_vcard data from xml response
             $raw_vcards = array();
             $xmlvcard = new SimpleXMLElement($xmldata);
             foreach ($xmlvcard->element as $vcard_element) {
                 $id = $vcard_element->id->__toString();
                 $value = (string) $vcard_element->vcard->__toString();
                 $raw_vcards[$id] = $value;
             }
             print " " . count($raw_vcards) . " VCards retrieved." . PHP_EOL;
             // parse raw_vcards
             $quick_dial_arr = array();
             foreach ($raw_vcards as $v) {
                 $vcard_obj = new vCard(false, $v);
                 $name_arr = null;
                 if (isset($vcard_obj->n[0])) {
                     $name_arr = $vcard_obj->n[0];
                 }
                 $org_arr = null;
                 if (isset($vcard_obj->org[0])) {
                     $org_arr = $vcard_obj->org[0];
                 }
                 $addnames = '';
                 $prefix = '';
                 $suffix = '';
                 $orgname = '';
                 // Build name Parts if existing ans switch to true in config
                 if (isset($name_arr['prefixes']) and $this->config['prefix']) {
                     $prefix = trim($name_arr['prefixes']);
                 }
                 if (isset($name_arr['suffixes']) and $this->config['suffix']) {
                     $suffix = trim($name_arr['suffixes']);
                 }
                 if (isset($name_arr['additionalnames']) and $this->config['addnames']) {
                     $addnames = trim($name_arr['additionalnames']);
                 }
                 if (isset($org_arr['name']) and $this->config['orgname']) {
                     $orgname = trim($org_arr['name']);
                 }
                 $firstname = trim($name_arr['firstname']);
                 $lastname = trim($name_arr['lastname']);
                 // the following section implemented different ways of constructing the
                 // final phonebook name entry depending on user preferred settings
                 // selectable in the config file. Possible options are:
                 //
                 // $this->config['fullname_format']:
                 //
                 // 0: "Prefix Lastname, Firstname AdditionalNames Suffix (orgname)"
                 // 1: "Prefix Firstname Lastname AdditionalNames Suffix (orgname)"
                 // 2: "Prefix Firstname AdditionalNames Lastname Suffix (orgname)"
                 //
                 $name = '';
                 $format = $this->config['fullname_format'];
                 // Prefix
                 if (!empty($prefix)) {
                     $name .= $prefix;
                 }
                 if ($format == 0) {
                     // Lastname
                     if (!empty($name) and !empty($lastname)) {
                         $name .= ' ' . $lastname;
                     } else {
                         $name .= $lastname;
                     }
                 } else {
                     // Firstname
                     if (!empty($name) and !empty($firstname)) {
                         $name .= ' ' . $firstname;
                     } else {
                         $name .= $firstname;
                     }
                 }
                 if ($format == 2) {
                     // AdditionalNames
                     if (!empty($name) and !empty($addnames)) {
                         $name .= ' ' . $addnames;
                     } else {
                         $name .= $addnames;
                     }
                 }
                 if ($format == 0) {
                     // Firstname
                     if (!empty($name) and !empty($firstname)) {
                         $name .= ', ' . $firstname;
                     } else {
                         $name .= $firstname;
                     }
                 } else {
                     // Lastname
                     if (!empty($name) and !empty($lastname)) {
                         $name .= ' ' . $lastname;
                     } else {
                         $name .= $lastname;
                     }
                 }
                 if ($format != 2) {
                     // AdditionalNames
                     if (!empty($name) and !empty($addnames)) {
                         $name .= ' ' . $addnames;
                     } else {
                         $name .= $addnames;
                     }
                 }
                 // Suffix
                 if (!empty($name) and !empty($suffix)) {
                     $name .= ' ' . $suffix;
                 } else {
                     $name .= $suffix;
                 }
                 // OrgName
                 if (!empty($name) and !empty($orgname)) {
                     $name .= ' (' . $orgname . ')';
                 } else {
                     $name .= $orgname;
                 }
                 // make sure to trim whitespaces and double spaces
                 $name = trim(str_replace('  ', ' ', $name));
                 if (empty($name)) {
                     print '  WARNING: No fullname, lastname or orgname found!';
                     $name = 'UNKNOWN';
                 }
                 // format filename of contact photo; remove special letters
                 if ($vcard_obj->photo) {
                     $photo = str_replace(array(',', '&', ' ', '/', 'ä', 'ö', 'ü', 'Ä', 'Ö', 'Ü', 'ß', 'á', 'à', 'ó', 'ò', 'ú', 'ù', 'í', 'ø'), array('', '_', '_', '_', 'ae', 'oe', 'ue', 'Ae', 'Oe', 'Ue', 'ss', 'a', 'a', 'o', 'o', 'u', 'u', 'i', 'oe'), $name);
                 } else {
                     $photo = '';
                 }
                 // phone
                 $phone_no = array();
                 if ($vcard_obj->categories) {
                     $categories = $vcard_obj->categories[0];
                 } else {
                     $categories = array();
                 }
                 // check for quickdial entry
                 if (isset($vcard_obj->note[0])) {
                     $note = $vcard_obj->note[0];
                     $notes = explode($this->config['quickdial_keyword'], $note);
                     foreach ($notes as $linenr => $linecontent) {
                         $found = strrpos($linecontent, ":**7");
                         if ($found > 0) {
                             $pos_qd_start = strrpos($linecontent, ":**7");
                             $quick_dial_for_nr = preg_replace("/[^0-9+]/", "", substr($linecontent, 0, $pos_qd_start));
                             $quick_dial_nr = intval(substr($linecontent, $pos_qd_start + 4, 3));
                             $quick_dial_arr[$quick_dial_for_nr] = $quick_dial_nr;
                         }
                     }
                 }
                 // e-mail addresses
                 $email_add = array();
                 $vip = isset($this->config['group_vip']) && in_array((string) $this->config['group_vip'], $categories);
                 if (array_key_exists('group_filter', $this->config) && is_array($this->config['group_filter'])) {
                     $add_entry = 0;
                     foreach ($this->config['group_filter'] as $group_filter) {
                         if (in_array($group_filter, $categories)) {
                             $add_entry = 1;
                             break;
                         }
                     }
                 } else {
                     $add_entry = 1;
                 }
                 if ($add_entry == 1) {
                     foreach ($vcard_obj->tel as $t) {
                         $prio = 0;
                         $quickdial = null;
                         if (!is_array($t) || empty($t['type'])) {
                             $type = "mobile";
                             $phone_number = $t;
                         } else {
                             $phone_number = $t['value'];
                             $phone_number_clean = preg_replace("/[^0-9+]/", "", $phone_number);
                             foreach ($quick_dial_arr as $qd_phone_nr => $value) {
                                 if ($qd_phone_nr == $phone_number_clean) {
                                     //Set quickdial
                                     if ($value == 1) {
                                         print "\nWARNING: Quickdial value 1 (**701) is not possible but used! \n";
                                     } elseif ($value >= 100) {
                                         print "\nWARNING: Quickdial value bigger than 99 (**799) is not possible but used! \n";
                                     }
                                     $quickdial = $value;
                                 }
                             }
                             $typearr_lower = unserialize(strtolower(serialize($t['type'])));
                             // find out priority
                             if (in_array("pref", $typearr_lower)) {
                                 $prio = 1;
                             }
                             // set the proper type
                             if (in_array("cell", $typearr_lower)) {
                                 $type = "mobile";
                             } elseif (in_array("home", $typearr_lower)) {
                                 $type = "home";
                             } elseif (in_array("fax", $typearr_lower)) {
                                 $type = "fax_work";
                             } elseif (in_array("work", $typearr_lower)) {
                                 $type = "work";
                             } elseif (in_array("other", $typearr_lower)) {
                                 $type = "other";
                             } elseif (in_array("dom", $typearr_lower)) {
                                 $type = "other";
                             } else {
                                 continue;
                             }
                         }
                         $phone_no[] = array("type" => $type, "prio" => $prio, "quickdial" => $quickdial, "value" => $this->_clear_phone_number($phone_number));
                     }
                     // request email address and type
                     if ($vcard_obj->email) {
                         foreach ($vcard_obj->email as $e) {
                             if (empty($e['type'])) {
                                 $type_email = "work";
                                 $email = $e;
                             } else {
                                 $email = $e['value'];
                                 $typearr_lower = unserialize(strtolower(serialize($e['type'])));
                                 if (in_array("work", $typearr_lower)) {
                                     $type_email = "work";
                                 } elseif (in_array("home", $typearr_lower)) {
                                     $type_email = "home";
                                 } elseif (in_array("other", $typearr_lower)) {
                                     $type_email = "other";
                                 } else {
                                     continue;
                                 }
                             }
                             // DEBUG: print out the email address on the console
                             //print $type_email.": ".$email."\n";
                             $email_add[] = array("type" => $type_email, "value" => $email);
                         }
                     }
                     $entries[] = array("realName" => $name, "telephony" => $phone_no, "email" => $email_add, "vip" => $vip, "photo" => $photo, "photo_data" => $vcard_obj->photo);
                 }
             }
             $snum++;
         }
     }
     $this->entries = $entries;
 }