USE asterisk; CREATE TABLE asterisk.phonebook ( id int(11) NOT NULL AUTO_INCREMENT, create_date timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, number varchar(20) NOT NULL, name varchar(80) NOT NULL, temporary_contact tinyint(1) NOT NULL DEFAULT 1, PRIMARY KEY (id) ) ENGINE = INNODB AUTO_INCREMENT = 9 AVG_ROW_LENGTH = 8192 CHARACTER SET utf8 COLLATE utf8_general_ci;
#!/usr/bin/php -q <?php $error_level = error_reporting(0); set_time_limit(30); //require('phpagi.php'); //$agi = new AGI(); if (isset($argv[1])) {$num=$argv[1];} else {$num=NULL;} $cookie_file='/tmp/asterisk_truecaller_vk.cookie'; $vk = array("login"=> "_", "password"=> "_"); $mysql = array("hostname" => "localhost", "login"=> "root", "password"=> "_mysql", "database"=> "asterisk"); if (!is_null($num)) { $callerid_name=get_num($num,$vk,$mysql,$cookie_file,true); return $callerid_name; } else { echo " \n"; //$agi->set_variable("CID_NAME", ""); return false; } // truecaller function get_num($num,$vk,$mysql,$cookie_file,$isauth) { // mysql_connect($mysql['hostname'],$mysql['login'],$mysql['password']); mysql_select_db($mysql['database']) or die(mysql_error()); mysql_query("SET NAMES 'utf8'"); mysql_query("SET CHARACTER SET 'utf8'"); mysql_query("SET SESSION collation_connection = 'utf8_general_ci'"); $query = "SELECT * FROM phonebook WHERE `number`=$num"; $res = mysql_query($query); $count = mysql_num_rows($res); if ($count>0) while ($row=mysql_fetch_array($res)) { $name=$row['name']; echo " MySQL '".$name."'\n"; //$agi->set_variable("CID_NAME", "$name"); return $name; } mysql_close(); // truecaller if ($isauth) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'http://www.truecaller.com/ru/'.$num ); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $data = curl_exec($ch); curl_close($ch); if (preg_match("/You need to sign in to view the result/i", $data)) { echo " TC\n"; $isauth = oauth_vk($vk, $cookie_file); get_num($num,$vk,$mysql,$cookie_file,$isauth); } else { preg_match("/<div class=\"detailView__nameText\">\n\s*(.+)\s\n\s*<\/div>/i", $data, $matches); if (count($matches)>0) { $name=$matches[1]; echo " TC '".$name."'\n"; mysql_connect($mysql['hostname'],$mysql['login'],$mysql['password']); mysql_select_db($mysql['database']) or die(mysql_error()); mysql_query("SET NAMES 'utf8'"); mysql_query("SET CHARACTER SET 'utf8'"); mysql_query("SET SESSION collation_connection = 'utf8_general_ci'"); $query = "INSERT INTO phonebook (`name`,`number`) VALUE ('".$name."','".$num."')"; $res = mysql_query($query); mysql_close(); //$agi->set_variable("CID_NAME", "$name"); return $name; } else { echo " TC \n"; //$agi->set_variable("CID_NAME", ""); return false; } } } else { echo " TC , \n"; //$agi->set_variable("CID_NAME", ""); return false; } } // " " function oauth_vk($vk, $cookie_file) { unlink($cookie_file); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, 'https://oauth.vk.com/authorize?client_id=4951501&scope=friends%2Coffline&redirect_uri=http%3A%2F%2Fwww.truecaller.com%2Fsign-in%2Fvk&response_type=code&state=KKoLuT0vbWEOXfqIW9C0yAvoX7uoEDszIrVOxYSr'); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); // curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $data = curl_exec($ch); curl_close($ch); preg_match("/<form method=\"post\" action=\"(.+)\">/i", $data, $matches); if (count($matches)>0) $action=$matches[1]; preg_match("/<input type=\"hidden\" name=\"_origin\" value=\"(.+)\">/i", $data, $matches); if (count($matches)>0) $origin=$matches[1]; preg_match("/<input type=\"hidden\" name=\"ip_h\" value=\"(.+)\" \/>/i", $data, $matches); if (count($matches)>0) $ip_h=$matches[1]; preg_match("/<input type=\"hidden\" name=\"lg_h\" value=\"(.+)\" \/>/i", $data, $matches); if (count($matches)>0) $lg_h=$matches[1]; preg_match("/<input type=\"hidden\" name=\"to\" value=\"(.+)\">/i", $data, $matches); if (count($matches)>0) $to=$matches[1]; if (isset($action) && isset($origin) && isset($ip_h) && isset($lg_h) && isset($to)) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $action ); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); curl_setopt($ch, CURLOPT_POST, 1); curl_setopt($ch, CURLOPT_POSTFIELDS, array( '_origin'=>$origin, 'ip_h'=>$ip_h, 'lg_h'=>$lg_h, 'to'=>$to, 'email'=>$vk['login'], 'pass'=>$vk['password'] )); $data = curl_exec($ch); curl_close($ch); preg_match('/Location: (http\:\/\/www\.truecaller\.com\/sign\-in\/vk\?code\=.+)\&state.+/', $data, $matches); if (count($matches)>0) $location=$matches[1]; if (isset($location)) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $location); curl_setopt($ch, CURLOPT_HEADER, 1); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file); curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file); $data = curl_exec($ch); curl_close($ch); if (preg_match("/\<title\>Signed In \| Truecaller\<\/title\>/i", $data)) { echo " VK \n"; return true; } else { echo " VK / \n"; return false; } } else { echo " VK / location \n"; return false; } } else { echo " VK / action='".$action."', origin='".$origin."', ip_h='".$ip_h."', lg_h='".$lg_h."', to='".$to."'\n"; return false; } } ?>
local call = {} call.cid_num = channel["CALLERID(num)"]:get() call.cid_name = "" -- local handle = io.popen("/var/lib/asterisk/agi-bin/phonebook.php "..call.cid_num) local founded_name = handle:read("*a") handle:close() app.Noop(founded_name) _, _, call.cid_name = string.find(founded_name,"%s%s.+%s'(.+)'") channel["CALLERID(name)"]:set(call.cid_name)
Source: https://habr.com/ru/post/266807/
All Articles