Vcard to mysql for number recognition

Hi i use khard for number recognition but its to slow. Takes 5 seconds to get trough all the vcards.

Does someone has a vcard to some database system for number and group? (Close friends and family can always call me others are redirected to voicemail when im busy)

Before i gona write my own perhaps someone has a working solution that is quick. Im a software tester i hate when i need to write code.

Are you looking for a one off conversion? If so, could you import into Thunderbird, then use something like GitHub - KevinGoodsell/mork-converter: Convert Mozilla Mork databases to more accessible formats to get it into CSV. (I’ve only spent a few minutes on this, so this is only a research suggestion; it may turn out to be badly flawed.)

#!/usr/bin/php
<?php
include("./vCard.php");
$contactDir = "/opt/vcf2asterisk/contacts/";
$nCountryCode = "31";

class asteriskContacts {
   public $aPhones = array();

   public function write() {
      $db = new SQLite3('./phones.db');
      $db->query("DROP TABLE `contacts`");
      $db->query("CREATE TABLE IF NOT EXISTS contacts (number text,name text,`group` text);");
      foreach($this->$aPhones as $number => $vPhone) {
       echo "INSERT INTO contacts (number,name,`group`) VALUES ('$number','".$vPhone["name"]."','".$vPhone["group"]."');";
       $db->query("INSERT INTO contacts (number,name,`group`) VALUES ('$number','".$vPhone["name"]."','".$vPhone["group"]."');");

     } 
   }
   
   public function add($sNumber,$sName,$sGroup = "ALL") {
     global $nCountryCode;
     if (preg_match('/(00|\+)'.$nCountryCode.'(\d+)/',$sNumber,$aMatch)) {
       $sNumber = "0".$aMatch[2];
     }    
     echo "$sNumber,$sName,$sGroup \n";
     if (@$vGroup = $this->$aPhones[$sNumber]["group"]) 
     { 
       if ($vGroup == "ALL" ) {
        $this->$aPhones[$sNumber]["name"] = $sName;
        $this->$aPhones[$sNumber]["group"] = $sGroup;
       }
     } else {
        $this->$aPhones[$sNumber]["name"] = $sName;
        $this->$aPhones[$sNumber]["group"] = $sGroup;
     }
   }
}

$asterisk = new asteriskContacts();

#Find Files
$aFiles = scandir($contactDir);
foreach($aFiles as $sFile) {
#...2022-12-28_19-45_close.vcfcontacts-2022-12-28.vcf
  print_r($sFile);
  if (preg_match('/\d+-\d+-\d+_\d+-\d+_(.*)\.vcf/',$sFile,$aMatch)) {
    $aGroup[$aMatch[1]] = $contactDir.$sFile;
  }
  if (preg_match('/contacts-\d+-\d+-\d+\.vcf/',$sFile,$aMatch)) {
    $sAllContacts = $contactDir.$sFile;
  }

}

#parse groups

foreach($aGroup as $sGroup => $sFile) {

   $vCard = new vCard($sFile);
   if (count($vCard) == 1)
   {
      print_r($vCard);
        $sName = $vCard->fn[0];
        foreach($vCard->tel as $vTel) 
        {
          if (is_array($vTel)) {
            #print_r($vTel['Value']);
            $asterisk->add($vTel['Value'],$sName,$sGroup);
          }
          else 
          {
            #echo "tel: ".$vTel;
            $asterisk->add($vTel['Value'],$sName,$sGroup);
          }
        }
   }
   else
   {
      foreach ($vCard as $vCardPart)
      {
        #print_r($vCardPart );
        $sName = $vCardPart->fn[0];
        foreach($vCardPart->tel as $vTel) 
        {
          if (is_array($vTel)) {
            #print_r($vTel['Value']);
            $asterisk->add($vTel['Value'],$sName,$sGroup);
          }
          else 
          {
            #echo "tel: ".$vTel;
            $asterisk->add($vTel,$sName,$sGroup);
          }
        }
      }
   }
}
#parse contacts

$vCard = new vCard($sAllContacts);
if (count($vCard) == 1)
{
   print_r($vCard);
     $sName = $vCard->fn[0];
     foreach($vCard->tel as $vTel) 
     {
       if (is_array($vTel)) {
         #print_r($vTel['Value']);
         $asterisk->add($vTel['Value'],$sName,$sGroup);
       }
       else 
       {
         #echo "tel: ".$vTel;
         $asterisk->add($vTel['Value'],$sName,$sGroup);
       }
     }
}
else
{
   foreach ($vCard as $vCardPart)
   {
     #print_r($vCardPart );
     $sName = $vCardPart->fn[0];
     foreach($vCardPart->tel as $vTel) 
     {
       if (is_array($vTel)) {
        #print_r($vTel['Value']);
        $asterisk->add($vTel['Value'],$sName);
       }
       else 
       {
        #echo "tel: ".$vTel;
        $asterisk->add($vTel,$sName);
      }
    }
  }
}

#print_r($asterisk->$aPhones["0614331929"]);

$asterisk->write();

?>

#!/usr/bin/php
<?php
$db = new SQLite3('/opt/vcf2asterisk/phones.db');
$results = $db->query("SELECT * FROM contacts where number = '$argv[1]'");
$row = $results->fetchArray();
#print_r($row);

if ($row) {
  echo $row['name']."#!#".$row['group'];
  file_put_contents("/opt/vcf2asterisk/name",$row['name']);
  file_put_contents("/opt/vcf2asterisk/name_number",$row['name'].' '.$argv[1]);
  file_put_contents("/opt/vcf2asterisk/group",$row['group']);
} else {
  file_put_contents("/opt/vcf2asterisk/name",$argv[1]);
  file_put_contents("/opt/vcf2asterisk/name_number",$argv[1]);
  file_put_contents("/opt/vcf2asterisk/group",$row['group'])
}
?>

exten => 1234,1,GotoIf(${BLACKLIST()}?call,blacklist,1)
exten => 1234,2,Log(NOTICE, Call from ${CALLERID(all)} )
exten => 1234,3,System(/opt/vcf2asterisk/lookup.php ${CALLERID(NUM)})
exten => 1234,4,GotoIf(${SHELL(/opt/HA/read.php ${SHELL(cat /opt/vcf2asterisk/group)}) } = “0” )?,14)
exten => 1234,5,Log(NOTICE, Pickup Karin )
exten => 1234,6,Set(SAVE=${CALLERID(NUM)})
exten => 1234,7,Set(CALLERID(NUM)=5555)
exten => 1234,8,Dial(PJSIP/6021&PJSIP/6023,10,t)
exten => 1234,9,Set(CALLERID(NUM)=${SAVE})
exten => 1234,10,Set(CALLERID(NAME)=${SHELL(cat /opt/vcf2asterisk/name)})
exten => 1234,11,Log(NOTICE, Transfer to voicemail} )
exten => 1234,12,VoiceMail(6001@default,b)
exten => 1234,13,HangUp()
exten => 1234,14,System(/opt/HA/inkomendeBeller.php)
exten => 1234,15,Set(CALLERID(NAME)=${SHELL(cat /opt/vcf2asterisk/name)})
exten => 1234,16,Goto(call,callall,1)
exten => 1234,17,Hangup()


Just a note that DROPping a nonexistent table is an error. But by default you don’t notice SQL errors in PHP, do you?

It does not give an error. it gives a warning. warnings are no problem.

SQLite3 itself is reporting an error, which PHP is ignoring or downplaying:

ldo@theon:~> sqlite3 test.db
SQLite version 3.40.0 2022-11-16 12:10:08
Enter ".help" for usage hints.
sqlite> drop table nosuch;
Parse error: no such table: nosuch
sqlite>

Yeah what do you expect. the php code does not break so its a warning.
Do you expect your browser closes with an exit code when you hit a 404 error page?

Thankfully, I have no need to run PHP in a browser.

I expect programmers to have some understanding of the APIs they use. Thus:

$db = new SQLite3("test.db");
$db->query("drop table nosuch");
echo "SQLite reports: " . $db->lastErrorMsg() . "\n";
$db->query("drop table if exists nosuch");
echo "SQLite reports: " . $db->lastErrorMsg() . "\n";

produces output:

PHP Warning:  SQLite3::query(): no such table: nosuch in /home/ldo/hack/php_try/sqlite3_error_try.php on line 12
SQLite reports: no such table: nosuch
SQLite reports: not an error

while

$db = new SQLite3("test.db");
$db->enableExceptions(true);
$db->query("drop table nosuch");
echo "SQLite reports: " . $db->lastErrorMsg() . "\n";
$db->query("drop table if exists nosuch");
echo "SQLite reports: " . $db->lastErrorMsg() . "\n";

produces:

PHP Fatal error:  Uncaught Exception: no such table: nosuch in /home/ldo/hack/php_try/sqlite3_error_try.php:13
Stack trace:
#0 /home/ldo/hack/php_try/sqlite3_error_try.php(13): SQLite3->query()
#1 {main}
  thrown in /home/ldo/hack/php_try/sqlite3_error_try.php on line 13

which makes things much easier to debug, don’t you think?

As a software test I think bugs are issues that encounter problems or unwanted behavior for the end user.
but anyway i added the if exists to the script and fixed some other warnings.

gonna make a github page with all my home assistant asterisk integrations one of these days.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.