Advertisement
Guest User

CID Superfecta - FOP2 Phonebook Lookup Latest Version

a guest
Mar 7th, 2024
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.50 KB | Software | 0 0
  1. <?php
  2.  
  3. /*** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
  4. * Developer Notes:
  5. * There may be some users who would want this module to do post processing to insert name/number into FOP2 address book
  6. * Versions prior to 2013-12-20 would abandon the search if there were fewer digits than specified, disabled by
  7.  
  8. * Version History:
  9. * 20xx-xx-xx    Initial migration to 2.11 platform
  10. * 2013-12-19    bug fix, CNAM_Type missing from Source Param user input - lgaetz
  11. * 2013-12-20    bug fix, don't modify $run_param variables inside get_caller_id function, fix filter length - lgaetz
  12. * 2014-01-02    add user setting to use native FreeBPX connection to the asterisk database.
  13. * 2014-08-19    removed reliance on deprecated mysql functions
  14. * 2014-09-07    Fixed minor bug with PDO
  15. * 2023-10-09    Adapted to work with FOP2 2.31.36 or later
  16. *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***/
  17.  
  18. class FOP2_Phonebook extends superfecta_base {
  19.     public $description,$source_param;
  20.     public $version_requirement = "2.11";
  21.  
  22.     public function __construct() {
  23.     $this->description = _("Look up First Name, Last Name, Company Name in the FOP2 visual address book MySQL table");
  24.     $this->source_param = array(
  25.         'FOP2_Version' => array(
  26.                 'description' => _('Specify the FOP2 version installed'),
  27.                 'type' => 'select',
  28.                 'option' => array(
  29.                     'new' => _('FOP2 with Advanced Phonebook (2.31.36 or later)'),             
  30.                     'old' => _('FOP2 with Simple Phonebook'),
  31.                 ),
  32.                 'default' => 'old',
  33.         ),
  34.         'Use_Native_FreePBX_Connection' => array(
  35.                 'description' => _('Database connection is automatically configured using native FreePBX connection to the `asterisk` MySQL database. If this setting does not work, disable and manually configure database access fields below.'),
  36.                 'type' => 'checkbox',
  37.                 'default' => 'on',
  38.             ),
  39.             'DB_Host' => array(
  40.                     'description' => _('Host address of the FOP2 database. (localhost if the database is on the same server as FreePBX). Only required when the `Native FreePBX Connection` above is not enabled.'),
  41.                     'type' => 'text',
  42.                     'default' => 'localhost',
  43.             ),
  44.             'DB_Name' => array(
  45.                     'description' => _('Database name of the FOP2 database. Only required when the `Native FreePBX Connection` above is not enabled.'),
  46.                     'type' => 'text',
  47.                     'default' => 'fop2',
  48.             ),
  49.             'DB_User' => array(
  50.                     'description' => _('Username used to connect to the FOP2 database. Only required when the `Native FreePBX Connection` above is not enabled.'),
  51.                     'type' => 'text',
  52.                     'default' => 'root',
  53.             ),
  54.             'DB_Password' => array(
  55.                     'description' => _('Password used to connect to the FOP2 database. Only required when the `Native FreePBX Connection` above is not enabled.'),
  56.                     'type' => 'password',
  57.                     'default' => 'passw0rd',
  58.             ),
  59.             'CNAM_Type' => array(
  60.                 'description' => _('Select how returned CNAM is prioritized'),
  61.                 'type' => 'select',
  62.                 'option' => array(
  63.                     '1' => _('Company_name then First_name Last_name'),
  64.                     '2' => _('First_name Last_name then Company_name'),
  65.                     '3' => _('Last_name Company_name if both exist'),
  66.                 ),
  67.                 'default' => '1',
  68.             ),
  69.             'Filter_Length' => array(
  70.                     'description' => _('The number of rightmost digits to check for a match. Enter zero to disable this setting'),
  71.                     'type' => 'number',
  72.                     'default' => 10
  73.             )
  74.         );
  75.     }
  76.  
  77.     function get_caller_id($thenumber, $run_param=array()) {
  78.     $this->DebugPrint(_("Searching FOP2..."));
  79.  
  80.     // Initialize variables
  81.     $caller_id = null;
  82.     $wquery_string = "";
  83.     $wquery_result = "";
  84.     $cnam_type = $run_param['CNAM_Type'];
  85.     $sql_params = array();
  86.  
  87.     //  trim incoming number to specified filter length
  88.     if ($run_param['Filter_Length'] != 0 && strlen($thenumber) > $run_param['Filter_Length']) {
  89.         $thenumber = substr($thenumber, (-1*$run_param['Filter_Length']));
  90.     }
  91.  
  92.     //  Build regular expression from modified $thenumber to avoid non-digit characters
  93.     $regex = "[^0-9]*";
  94.     for( $x=0; $x < ((strlen($thenumber))-1); $x++ ) {
  95.         $regex .=  substr($thenumber,$x,1)."[^0-9]*" ;
  96.     }
  97.     $regex = $regex.(substr($thenumber,-1))."([^0-9]+|$)";
  98.  
  99.     // Two different access methods to database. It was discovered that if FreePBX mysql credentials are used in Superfecta, it would error out because of the mysql_close line
  100.     if ($run_param['Use_Native_FreePBX_Connection'] != 'on') {
  101.         $sql_params[':regex'] = $regex;
  102.         if ($run_param['FOP2_Version'] == 'new') {
  103.             $wquery_string = '
  104.             SELECT vp.firstname, vp.lastname, vp.company
  105.             FROM visual_phonebook vp
  106.             JOIN visual_phonebook_phones vpp ON vp.id = vpp.contact_id
  107.             WHERE (vpp.number REGEXP :regex)
  108.             ORDER BY vp.id DESC';
  109.         } else {
  110.             $wquery_string = 'SELECT firstname, lastname, company FROM visual_phonebook WHERE (phone1 REGEXP :regex) OR (phone2 REGEXP :regex) ORDER BY id DESC';
  111.         }
  112.         //  Connect to database using user mysql settings
  113.         if(class_exists('PDO')) {
  114.             $this->DebugPrint(_("Connecting to Database with PDO driver..."));
  115.             try {
  116.                 $dbh = new PDO('mysql:dbname='.$run_param['DB_Name'].';host='.$run_param['DB_Host'], $run_param['DB_User'], $run_param['DB_Password']);
  117.             } catch (PDOException $e) {
  118.                 $this->DebugPrint(_('Connection failed').': ' . $e->getMessage());
  119.                 return null;
  120.             }
  121.         } else {
  122.             $this->DebugPrint(_("PDO not present on system...Skipping"));
  123.             return null;
  124.         }
  125.         $sth = $dbh->prepare("SET NAMES 'utf8'");
  126.         $sth->execute();
  127.         try {
  128.             $sth = $dbh->prepare($wquery_string);
  129.             $sth->execute($sql_params);
  130.             $wquery_row = $sth->fetch(PDO::FETCH_ASSOC);
  131.             if (is_array($wquery_row)) {
  132.                 $last_name = $wquery_row["lastname"];
  133.                 $first_name = $wquery_row["firstname"];
  134.                 $company_name = $wquery_row["company"];
  135.             }
  136.         } catch (PDOException $e) {
  137.             $this->DebugPrint(_('Connection failed').': ' . $e->getMessage());
  138.             return null;
  139.         }
  140.     } else {
  141.         global $db;
  142.         if ($run_param['FOP2_Version'] == 'new') {
  143.             $wquery_string = "
  144.             SELECT vp.firstname, vp.lastname, vp.company
  145.             FROM visual_phonebook vp
  146.             JOIN visual_phonebook_phones vpp ON vp.id = vpp.contact_id
  147.             WHERE (vpp.number REGEXP '".$db->escapeSimple($regex)."')
  148.             ORDER BY vp.id DESC";
  149.         } else {
  150.             $wquery_string = "SELECT firstname, lastname, company FROM visual_phonebook WHERE (phone1 REGEXP '".$db->escapeSimple($regex)."') OR (phone2 REGEXP '".$db->escapeSimple($regex)."') ORDER BY id DESC";
  151.         }
  152.         $wquery_row = sql($wquery_string, "getAll", DB_FETCHMODE_ASSOC);
  153.         if (is_array($wquery_row)) {
  154.             $last_name = $wquery_row[0]["lastname"];
  155.             $first_name = $wquery_row[0]["firstname"];
  156.             $company_name = $wquery_row[0]["company"];
  157.         }
  158.        
  159.     }
  160.    
  161.         if ($cnam_type == 3) {
  162.             if ($last_name != "" and $company_name != "") {
  163.                 $caller_id = $last_name." ".$company_name;
  164.             } else {
  165.                 $cnam_type = 1;
  166.             }
  167.         }
  168.         if ($cnam_type == 1) {
  169.             if ($company_name != "") {
  170.                 $caller_id = $company_name;
  171.             } else if ($first_name != "" and $last_name != "") {
  172.                 $caller_id = $first_name." ".$last_name;
  173.             } else if ($first_name != "") {
  174.                 $caller_id = $first_name;
  175.             } else {
  176.                 $caller_id = $last_name;
  177.             }
  178.         }
  179.         if ($cnam_type == 2) {
  180.             if ($first_name != "" and $last_name != "") {
  181.                 $caller_id = $first_name." ".$last_name;
  182.             } else if ($first_name != "") {
  183.                 $caller_id = $first_name;
  184.             } else if ($last_name != "") {
  185.                 $caller_id = $last_name;
  186.             } else {
  187.                 $caller_id = $company_name;
  188.             }
  189.         }
  190.  
  191.         if ($caller_id == "") {
  192.             $this->DebugPrint(_("Not Found"));
  193.         }
  194.         return(trim($caller_id));
  195.     }
  196. }
  197.  
Tags: FreePBX fop2
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement