Changeset 10198

Show
Ignore:
Timestamp:
07/29/10 05:32:51 (3 years ago)
Author:
tm1000
Message:

-Lots of changes to endpoint manager. Now it will reference provisioner.net where it will reference a master repository of phone configuration modules that are run by Darren & Team and Andrew. Project Reference: https://www.assembla.com/code/provisioner/git/nodes?rev=master
--Reasoning for doing this project to to stop having different distributions and locations for users to get working configuration files. Whether a user uses FreePBX V2, V3 or something else they will know that their phones will work correctly and one change made to the repository on provisioner will change on all the clients/software using this package
--Removed local _ep_phone_modules. It was out of date and is now at https://www.assembla.com/code/provisioner/git/nodes?rev=master. This is a configuration file generator only. The full EPM will be hosted here on the SVN.

-Uses only Pear::DB now. No more direct MySQL References.

-Needs install script to be updated to reference new database structure. Old DB structure won't work. Working on this next.

-Supports custom template_data.xml files. Also can define these files in each family_data.xml file in the provisioner.net repository

-Everything is now referenced by models instead of product_line (family_line). This allows users to have template files all the way down to specific models.
--Need to add a way to combine models in the template system so that users aren't so restricted to model lines when creating global configurations...looking into way to fix this.

-Removing need for direct Internet access. If the XML files can't be downloaded or are corrupt then endpoint manager will use local files instead.
--Furthermore will add option to allow users to upload the provisioner.net repo incase they will never have Internet on said machine

-Still need to fix: Product Options/Configuration Editor, Import/Export Devices My Devices List, Manual Endpoint Modules Upload and Show/Hide Brands/Models

-Other Fixes I'm sure.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • contributed_modules/modules/endpointman/includes/advanced.inc

    r10052 r10198  
    2020http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
    2121*/ 
    22 global $type; 
    2322 
    2423$tpl = new RainTPL( LOCAL_PATH.'templates' ); 
     
    3231 
    3332if(!isset($_REQUEST['subpage'])) { 
    34  $_REQUEST['subpage'] = ""; 
     33    $_REQUEST['subpage'] = ""; 
    3534} 
    3635 
    3736switch($_REQUEST['subpage']) { 
    38   case "iedl": 
    39     $amp_serial = base64_encode(serialize($amp_conf)); 
    40     $amp_serial = base64_encode(serialize($amp_conf)); 
    41     $temp_amp['amp_serial'] = $amp_serial; 
    42     $temp_amp = base64_encode(serialize($temp_amp)); 
    43  
    44     $outfile=LOCAL_PATH."/includes/amp.ini"; 
    45     $wfh=fopen($outfile,'w'); 
    46     fwrite($wfh, $temp_amp); 
    47     fclose($wfh); 
    48     $tpl->assign("exporter_address", $_SERVER["HTTP_HOST"].WEB_PATH."includes/export.php"); 
    49     //Dave B's Q&D file upload security code (http://us2.php.net/manual/en/features.file-upload.php)  
    50     if((isset($_REQUEST['action'])) AND (isset($_REQUEST['button_import'])) AND ($_REQUEST['action'] == "import")) { 
    51       $allowedExtensions = array("csv","txt");  
    52       foreach ($_FILES as $file) {  
    53         if ($file['tmp_name'] > '') {  
    54           if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) {  
    55             $tpl->assign("message", "We support only CVS and TXT files"); 
    56           } else { 
    57             $uploaddir = LOCAL_PATH; 
    58             $uploadfile = $uploaddir . basename($_FILES['import_csv']['name']); 
    59             if (move_uploaded_file($_FILES['import_csv']['tmp_name'], $uploadfile)) { 
    60               //Parse the uploaded file 
    61               $handle = fopen(LOCAL_PATH.$_FILES['import_csv']['name'], "r"); 
    62               $i = 1; 
    63                 while (($device = fgetcsv($handle, filesize(LOCAL_PATH.$_FILES['import_csv']['name']))) !== FALSE) { 
    64                 if($device[0] != "") { 
    65                   if($mac = $endpoint->mac_check_clean($device[0])) { 
    66                     $sql = "SELECT id FROM endpointman_brand_list WHERE name LIKE '%".$device[1]."%' LIMIT 1"; 
    67                     $res_brand = mysql_query($sql); 
    68                     if(mysql_num_rows($res_brand)) { 
    69                       $brand_id = mysql_fetch_row($res_brand); 
    70                       $brand_id = $brand_id[0]; 
    71                     } else { 
    72                       $brand_id = 0; 
    73                     } 
    74                     $sql = "SELECT id FROM endpointman_model_list WHERE brand = ".$brand_id." AND model LIKE '%".$device[2]."%' LIMIT 1"; 
    75                     $res_model = mysql_query($sql); 
    76                     $sql = "SELECT extension, name FROM users WHERE extension LIKE '%".$device[3]."%' LIMIT 1"; 
    77                     $res_ext = mysql_query($sql); 
    78                     if(mysql_num_rows($res_model)) { 
    79                       $model_id = mysql_fetch_row($res_model); 
    80                       $model_id = $model_id[0]; 
    81                     } else { 
    82                       $model_id = 0; 
    83                     } 
    84                     if(mysql_num_rows($res_ext)) { 
    85                       $ext = mysql_fetch_row($res_ext); 
    86                       $description = $ext[1]; 
    87                       $ext = $ext[0]; 
    88                     } else { 
    89                       $ext = ''; 
    90                       $description = ''; 
    91                     } 
    92                     $sql = "INSERT INTO  endpointman_mac_list (mac, model, ext, description) VALUES ('".$mac."','".$model_id."','".$ext."','".$description."')";                     
    93                     mysql_query($sql); 
    94                     $message .= "Inserted Mac Address ".$mac."<br/>"; 
    95                   } else { 
    96                     $message .= "Invalid Mac on line ". $i. "<br />"; 
    97                   } 
    98                 } 
    99                 $i++; 
    100               } 
    101               fclose($handle); 
    102               unlink(LOCAL_PATH.$_FILES['import_csv']['name']); 
    103               $message .= "Please reboot & rebuild all imported phones<br />"; 
    104               $tpl->assign("message", $message);   
    105             } else { 
    106               $tpl->assign("message", "Possible file upload attack!"); 
    107              
    108             } 
    109           } 
    110         }  
    111       } 
    112     } 
    113     echo $tpl->draw( 'advanced_settings_iedl' ); 
    114     break; 
    115   case "sh_manager": 
    116     if(isset($_REQUEST['button_hide'])) { 
    117       if(isset($_REQUEST['model'])) { 
    118         $sql = "UPDATE endpointman_model_list SET hidden = 1 WHERE id = ".$_REQUEST['model']; 
    119       } elseif(isset($_REQUEST['brand'])) { 
    120         $sql = "UPDATE endpointman_brand_list SET hidden = 1 WHERE id = ".$_REQUEST['brand']; 
    121       } elseif(isset($_REQUEST['product'])) { 
    122         $sql = "UPDATE endpointman_product_list SET hidden = 1 WHERE id = ".$_REQUEST['product']; 
    123       } 
    124       mysql_query($sql); 
    125     }elseif(isset($_REQUEST['button_show'])) {       
    126       if(isset($_REQUEST['model'])) { 
    127         $sql = "UPDATE endpointman_model_list SET hidden = 0 WHERE id = ".$_REQUEST['model']; 
    128       } elseif(isset($_REQUEST['brand'])) { 
    129         $sql = "UPDATE endpointman_brand_list SET hidden = 0 WHERE id = ".$_REQUEST['brand']; 
    130       } elseif(isset($_REQUEST['product'])) { 
    131         $sql = "UPDATE endpointman_product_list SET hidden = 0 WHERE id = ".$_REQUEST['product']; 
    132       } 
    133       mysql_query($sql); 
    134     } 
    135     $sql="SELECT * from endpointman_brand_list WHERE id > 0 ORDER BY id ASC "; 
    136     $result=mysql_query($sql); 
    137     $i = 0; 
    138     while($row = mysql_fetch_assoc($result)) { 
    139       $row_out[$i] = $row; 
    140       $row_out[$i]['count'] = $i; 
    141       if($row['installed']){ 
    142         $j = 0; 
    143         $sql = 'SELECT * FROM endpointman_product_list WHERE brand = '.$row['id'].' ORDER BY long_name ASC'; 
    144         $result2=mysql_query($sql); 
    145         $xml_data = ""; 
    146         while($row2 = mysql_fetch_assoc($result2)) { 
    147           $row_out[$i]['products'][$j] = $row2; 
    148           if($row2['installed']) { 
    149             $sql = 'SELECT * FROM endpointman_model_list WHERE product_id = '.$row2['id']; 
    150             $result3=mysql_query($sql); 
    151             $k = 0; 
    152             while($row3 = mysql_fetch_assoc($result3)) { 
    153               $row_out[$i]['products'][$j]['models'][$k] = $row3; 
    154               $k++; 
    155             } 
    156           } 
    157           $j++; 
    158         } 
    159       } 
    160       $i++; 
    161     } 
    162     $tpl->assign("brand2_list", $row_out); 
    163     echo $tpl->draw( 'advanced_settings_sh_manager' ); 
    164     break; 
    165   case "oui_manager": 
    166     if((isset($_REQUEST['oui_sub'])) AND ($_REQUEST['rb_brand'] > 0) AND ($_REQUEST['oui'] != "")) { 
    167       $sql = "INSERT INTO  endpointman_oui_list (oui, brand, custom) VALUES ('".$_REQUEST['oui']."',  '".$_REQUEST['rb_brand']."',  '1')"; 
    168       mysql_query($sql); 
    169     } 
    170     if((isset($_REQUEST['delete'])) AND ($_REQUEST['id'] > 0)) { 
    171       $sql = "DELETE FROM endpointman_oui_list WHERE id = ". $_REQUEST['id']; 
    172       mysql_query($sql); 
    173     } 
    174     $result = mysql_query('SELECT endpointman_oui_list.id, endpointman_oui_list.oui , endpointman_brand_list.name FROM endpointman_oui_list , endpointman_brand_list WHERE endpointman_oui_list.brand = endpointman_brand_list.id AND endpointman_oui_list.custom = 0'); 
    175     $i = 0; 
    176     while($row = mysql_fetch_array($result)) { 
    177       $row_out[$i] = $row; 
    178       $i++; 
    179     } 
    180     $tpl->assign("oui_list", $row_out); 
    181     $result = mysql_query('SELECT endpointman_oui_list.id, endpointman_oui_list.oui , endpointman_brand_list.name FROM endpointman_oui_list , endpointman_brand_list WHERE endpointman_oui_list.brand = endpointman_brand_list.id AND endpointman_oui_list.custom = 1'); 
    182     if(mysql_num_rows($result) > 0) { 
    183       $i = 0; 
    184       while($row = mysql_fetch_array($result)) { 
    185         $row_out_custom[$i] = $row; 
    186         $i++; 
    187       } 
    188       $tpl->assign("oui_list_custom", $row_out_custom); 
    189     } 
    190     $tpl->assign("brand_ava", $endpoint->brandAvailable()); 
    191     echo $tpl->draw( 'advanced_settings_oui' ); 
    192     break; 
    193   case "poce": 
    194     $sql = 'SELECT * FROM `endpointman_product_list` WHERE `installed` = 1 AND `id` > 0'; 
    195     $result = mysql_query($sql); 
    196     $i = 0; 
    197     while($row = mysql_fetch_assoc($result)) { 
    198       $product_list[$i]['value'] = $row['id']; 
    199       $product_list[$i]['text'] = $row['long_name']; 
    200       if((isset($_REQUEST['product_select'])) AND ($_REQUEST['product_select'] == $row['id'])) { 
    201         $product_list[$i]['selected'] = 1; 
    202       } 
    203       $i++; 
    204     } 
    205     if(isset($_REQUEST['delete'])) { 
    206       $sql = "DELETE FROM endpointman_custom_configs WHERE id =". $_REQUEST['sql']; 
    207       mysql_query($sql); 
    208     } 
    209     if(isset($_REQUEST['file'])) { 
    210       $sql = 'SELECT cfg_dir,directory,config_files FROM endpointman_product_list,endpointman_brand_list WHERE endpointman_product_list.brand = endpointman_brand_list.id AND endpointman_product_list.id =' . $_REQUEST['product_select']; 
    211       $result = mysql_query($sql); 
    212       $row = mysql_fetch_assoc($result);       
    213       $config_files = explode(",",$row['config_files']); 
    214       $file=PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/".$config_files[$_REQUEST['file']]; 
    215       if(isset($_REQUEST['config_text'])) { 
    216         if(isset($_REQUEST['button_save'])) { 
    217           $wfh=fopen($file,'w'); 
    218           fwrite($wfh,$_REQUEST['config_text']); 
    219           fclose($wfh); 
    220         }elseif(isset($_REQUEST['button_save_as'])) { 
    221           $sql = 'INSERT INTO endpointman_custom_configs (name, original_name, product_id, data) VALUES ("'.addslashes($_REQUEST['save_as_name']).'","'.addslashes($config_files[$_REQUEST['file']]).'",'.$_REQUEST['product_select'].',"'.addslashes($_REQUEST['config_text']).'")'; 
    222           mysql_query($sql); 
    223         } 
    224       } 
    225       $handle = fopen($file, "rb"); 
    226       $contents = fread($handle, filesize($file)); 
    227       fclose($handle); 
    228       $tpl->assign("save_as_name_value", $config_files[$_REQUEST['file']]); 
    229       $tpl->assign("config_data", $contents); 
    230     } elseif(isset($_REQUEST['sql'])) { 
    231       if(isset($_REQUEST['config_text'])) { 
    232         if(isset($_REQUEST['button_save'])) { 
    233           $sql = "UPDATE endpointman_custom_configs SET data = '".addslashes($_REQUEST['config_text'])."' WHERE id = ".$_REQUEST['sql']; 
    234           mysql_query($sql); 
    235         }elseif(isset($_REQUEST['button_save_as'])) { 
    236           $sql = 'SELECT original_name FROM endpointman_custom_configs WHERE id = 11'; 
    237           $res = mysql_query($sql); 
    238           $file_name = mysql_fetch_array($res); 
    239            
    240           $sql = 'INSERT INTO endpointman_custom_configs (name, original_name, product_id, data) VALUES ("'.addslashes($_REQUEST['save_as_name']).'","'.addslashes($file_name['original_name']).'",'.$_REQUEST['product_select'].',"'.addslashes($_REQUEST['config_text']).'")'; 
    241           mysql_query($sql); 
    242         } 
    243       } 
    244       $sql = 'SELECT * FROM endpointman_custom_configs WHERE id =' . $_REQUEST['sql']; 
    245       $result = mysql_query($sql); 
    246       $row = mysql_fetch_assoc($result); 
    247       $tpl->assign("save_as_name_value", $row['name']); 
    248       $tpl->assign("config_data", $row['data']); 
    249     } 
    250     if(isset($_REQUEST['product_select'])) { 
    251       $endpoint->sync_product($_REQUEST['product_select']); 
    252       $sql = 'SELECT cfg_dir,directory,config_files FROM endpointman_product_list,endpointman_brand_list WHERE endpointman_product_list.brand = endpointman_brand_list.id AND endpointman_product_list.id =' . $_REQUEST['product_select']; 
    253       $result = mysql_query($sql); 
    254       $row = mysql_fetch_assoc($result);       
    255       $config_files = explode(",",$row['config_files']); 
    256       $i = 0; 
    257       while($i < count($config_files)) { 
    258         $file_list[$i]['value'] = $i; 
    259         $file_list[$i]['text'] = $config_files[$i]; 
    260         $i++; 
    261       } 
    262       $sql = 'SELECT * FROM `endpointman_custom_configs` WHERE `product_id` = '. $_REQUEST['product_select']; 
    263       $result = mysql_query($sql); 
    264       $i = 0; 
    265       if(mysql_num_rows($result)) { 
    266         while($row2 = mysql_fetch_assoc($result)) { 
    267           $sql_file_list[$i]['value'] = $row2['id']; 
    268           $sql_file_list[$i]['text'] = $row2['name']; 
    269           $sql_file_list[$i]['ref'] = $row2['original_name']; 
    270           $i++; 
    271         } 
    272       } else { 
    273         $sql_file_list = NULL; 
    274       } 
    275       require(PHONE_MODULES_PATH .$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    276  
    277       $phone_config = new $row['cfg_dir'](); 
    278       if((method_exists($phone_config,'display_options')) AND (method_exists($phone_config,'process_options'))) { 
    279         if(isset($_REQUEST['phone_options'])) { 
    280           $tpl->assign("options", $phone_config->process_options()); 
    281         } else { 
    282           $tpl->assign("options", $phone_config->display_options()); 
    283         } 
    284       } 
    285        
    286       $tpl->assign("file_list", $file_list); 
    287       $tpl->assign("sql_file_list", $sql_file_list); 
    288       $tpl->assign("product_selected", $_REQUEST['product_select']); 
    289     } 
    290     $tpl->assign("product_list", $product_list); 
    291     echo $tpl->draw( 'advanced_settings_poce' ); 
    292     break; 
    293   case "settings": 
    294   default: 
    295     global $global_cfg; 
    296     if(isset($_REQUEST['button_update_globals'])) { 
    297       $_POST['srvip'] = trim($_POST['srvip']);  #trim whitespace from IP address 
    298  
    299       $_POST['config_loc'] = trim($_POST['config_loc']);  #trim whitespace from Config Location 
    300  
    301       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['srvip'] . "' WHERE var_name='srvip'"; 
    302       mysql_query($sql); 
    303       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['tz'] . "' WHERE var_name='tz'"; 
    304       mysql_query($sql); 
    305  
    306       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['config_loc'] . "' WHERE var_name='config_location'"; 
    307       mysql_query($sql); 
    308  
    309       if((isset($_POST['enable_ari'])) AND ($_POST['enable_ari'] == "on")) { 
    310         $_POST['enable_ari'] = 1; 
    311       } else { 
    312         $_POST['enable_ari'] = 0; 
    313       } 
    314       if((isset($_POST['enable_debug'])) AND ($_POST['enable_debug'] == "on")) { 
    315         $_POST['enable_debug'] = 1; 
    316       } else { 
    317         $_POST['enable_debug'] = 0; 
    318       } 
    319        
    320       if((isset($_POST['disable_htaccess'])) AND ($_POST['disable_htaccess'] == "on")) { 
    321         $htaccess = "allow from all"; 
    322  
    323         $outfile = $amp_conf['AMPWEBROOT'].WEB_PATH.".htaccess"; 
    324         $wfh=fopen($outfile,'w'); 
    325         fwrite($wfh,$htaccess); 
    326         fclose($wfh); 
    327         $_POST['disable_htaccess'] = 1; 
    328       } else { 
    329         $htaccess = "allow from all 
    330 AuthName FreePBX-Admin-only 
    331 Require valid-user 
    332 AuthType Basic 
    333 AuthMySQLEnable\tOn 
    334 AuthMySQLHost\tlocalhost 
    335 AuthMySQLDB\tasterisk 
    336 AuthMySQLUserTable\tampusers 
    337 AuthMySQLUser\t".$amp_conf['AMPDBUSER']." 
    338 AuthMySQLPassword\t".$amp_conf['AMPDBPASS']." 
    339 AuthMySQLNameField\tusername 
    340 AuthMySQLPasswordField\tpassword 
    341 AuthMySQLAuthoritative\tOn 
    342 AuthMySQLPwEncryption\tnone 
    343 AuthMySQLUserCondition\t\"username = 'admin'\" 
    344  
    345 <Files .*> 
    346   deny from all 
    347 </Files> 
    348 "; 
    349  
    350         $outfile = $amp_conf['AMPWEBROOT'].WEB_PATH.".htaccess"; 
    351         $wfh=fopen($outfile,'w'); 
    352         fwrite($wfh,$htaccess); 
    353         fclose($wfh); 
    354         $_POST['disable_htaccess'] = 0; 
    355       } 
    356       if((isset($_POST['enable_updates'])) AND ($_POST['enable_updates'] == "on")) { 
    357         $_POST['enable_updates'] = 1; 
    358         $sql = "INSERT INTO cronmanager (module, id, time, freq, lasttime, command) VALUES ('endpointman', 'UPDATES', '23', '24', '0', 'php ".$amp_conf['AMPWEBROOT']."/admin/modules/endpointman/includes/update_check.php')"; 
    359         mysql_query($sql); 
    360       } else { 
    361         $_POST['enable_updates'] = 0; 
    362         $sql = "DELETE FROM `cronmanager` WHERE CONVERT(`cronmanager`.`module` USING utf8) = 'endpointman' AND CONVERT(`cronmanager`.`id` USING utf8) = 'UPDATES' LIMIT 1"; 
    363         mysql_query($sql); 
    364       } 
    365        
    366       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['enable_ari'] . "' WHERE var_name='enable_ari'"; 
    367       mysql_query($sql); 
    368    
    369       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['enable_debug'] . "' WHERE var_name='debug'"; 
    370       mysql_query($sql); 
    371  
    372       $sql="SELECT gmtoff,gmthr FROM endpointman_time_zones WHERE tz='" . $_POST['tz'] . "'"; 
    373       $result=mysql_query($sql); 
    374       $row=mysql_fetch_array($result); 
    375  
    376       $sql="UPDATE endpointman_global_vars SET value='" . $row['gmtoff'] . "' where var_name='gmtoff'"; 
    377       mysql_query($sql); 
    378       $sql="UPDATE endpointman_global_vars SET value='" . $row['gmthr'] . "' where var_name='gmthr'"; 
    379       mysql_query($sql); 
    380    
    381       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['asterisk_loc'] . "' WHERE var_name='asterisk_location'"; 
    382       mysql_query($sql); 
    383    
    384       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['enable_updates'] . "' WHERE var_name='check_updates'"; 
    385       mysql_query($sql); 
    386    
    387       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['nmap_loc'] . "' WHERE var_name='nmap_location'"; 
    388       mysql_query($sql); 
    389        
    390       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['arp_loc'] . "' WHERE var_name='arp_location'"; 
    391       mysql_query($sql); 
    392        
    393       $sql="UPDATE endpointman_global_vars SET value='" . $_POST['disable_htaccess'] . "' WHERE var_name='disable_htaccess'"; 
    394       mysql_query($sql); 
    395    
    396       $message = "<h3><center>Updated!</center></h3>"; 
    397     } 
    398     //Because we are working with global variables we probably updated them, so lets refresh those variables 
    399     $sql = "SELECT * FROM  endpointman_global_vars"; 
    400     $result = mysql_query($sql); 
    401     while($row = mysql_fetch_assoc($result)) { 
    402       $global_cfg[$row['var_name']] = $row['value']; 
    403     } 
    404      
    405     if($global_cfg['enable_ari']) { 
    406       $ari_selected = "checked"; 
    407     } else { 
    408       $ari_selected = ""; 
    409     } 
    410      
    411     if($global_cfg['disable_htaccess']) { 
    412       $htaccess_selected = "checked"; 
    413     } else { 
    414       $htaccess_selected = ""; 
    415     } 
    416      
    417     if($global_cfg['check_updates']) { 
    418       $updates_selected = "checked"; 
    419     } else { 
    420       $updates_selected = ""; 
    421     } 
    422     if($global_cfg['debug']) { 
    423       $debug_selected = "checked"; 
    424       global $debug; 
    425       $debug = $debug . print_r($_REQUEST,true); 
    426       $tpl->assign("debug", $debug); 
    427     } else { 
    428       $debug_selected = ""; 
    429     } 
    430     $tpl->assign("htaccess_selected", $htaccess_selected); 
    431     $tpl->assign("ari_selected", $ari_selected); 
    432     $tpl->assign("updates_selected", $updates_selected); 
    433     $tpl->assign("debug_selected", $debug_selected); 
    434     $tpl->assign("message", $message); 
    435     $tpl->assign("ip", $_SERVER["SERVER_ADDR"]); 
    436     $tpl->assign("srvip", $global_cfg['srvip']); 
    437     $tpl->assign("arp_location", $global_cfg['arp_location']); 
    438     $tpl->assign("nmap_location", $global_cfg['nmap_location']); 
    439     $tpl->assign("asterisk_location", $global_cfg['asterisk_location']); 
    440  
    441     $tpl->assign("config_location", $global_cfg['config_location']); 
    442     $tpl->assign("list_tz", $endpoint->listTZ($global_cfg['tz'])); 
    443     $tpl->assign("brand_list", $endpoint->brandAvailable()); 
    444     echo $tpl->draw( 'advanced_settings_settings' ); 
    445     break; 
    446  
    447    
    448 
    449 if($global_cfg['debug']) { 
    450   global $debug; 
    451   $tpl->assign("debug", $debug); 
     37    case "iedl": 
     38        $amp_serial = base64_encode(serialize($amp_conf)); 
     39        $amp_serial = base64_encode(serialize($amp_conf)); 
     40        $temp_amp['amp_serial'] = $amp_serial; 
     41        $temp_amp = base64_encode(serialize($temp_amp)); 
     42 
     43        $outfile=LOCAL_PATH."/includes/amp.ini"; 
     44        $wfh=fopen($outfile,'w'); 
     45        fwrite($wfh, $temp_amp); 
     46        fclose($wfh); 
     47        $tpl->assign("exporter_address", $_SERVER["HTTP_HOST"].WEB_PATH."includes/export.php"); 
     48        //Dave B's Q&D file upload security code (http://us2.php.net/manual/en/features.file-upload.php) 
     49        if((isset($_REQUEST['action'])) AND (isset($_REQUEST['button_import'])) AND ($_REQUEST['action'] == "import")) { 
     50            $allowedExtensions = array("csv","txt"); 
     51            foreach ($_FILES as $file) { 
     52                if ($file['tmp_name'] > '') { 
     53                    if (!in_array(end(explode(".", strtolower($file['name']))), $allowedExtensions)) { 
     54                        $tpl->assign("message", "We support only CVS and TXT files"); 
     55                    } else { 
     56                        $uploaddir = LOCAL_PATH; 
     57                        $uploadfile = $uploaddir . basename($_FILES['import_csv']['name']); 
     58                        if (move_uploaded_file($_FILES['import_csv']['tmp_name'], $uploadfile)) { 
     59                            //Parse the uploaded file 
     60                            $handle = fopen(LOCAL_PATH.$_FILES['import_csv']['name'], "r"); 
     61                            $i = 1; 
     62                            while (($device = fgetcsv($handle, filesize(LOCAL_PATH.$_FILES['import_csv']['name']))) !== FALSE) { 
     63                                if($device[0] != "") { 
     64                                    if($mac = $endpoint->mac_check_clean($device[0])) { 
     65                                        $sql = "SELECT id FROM endpointman_brand_list WHERE name LIKE '%".$device[1]."%' LIMIT 1"; 
     66                                        $res_brand = mysql_query($sql); 
     67                                        if(mysql_num_rows($res_brand)) { 
     68                                            $brand_id = mysql_fetch_row($res_brand); 
     69                                            $brand_id = $brand_id[0]; 
     70                                        } else { 
     71                                            $brand_id = 0; 
     72                                        } 
     73                                        $sql = "SELECT id FROM endpointman_model_list WHERE brand = ".$brand_id." AND model LIKE '%".$device[2]."%' LIMIT 1"; 
     74                                        $res_model = mysql_query($sql); 
     75                                        $sql = "SELECT extension, name FROM users WHERE extension LIKE '%".$device[3]."%' LIMIT 1"; 
     76                                        $res_ext = mysql_query($sql); 
     77                                        if(mysql_num_rows($res_model)) { 
     78                                            $model_id = mysql_fetch_row($res_model); 
     79                                            $model_id = $model_id[0]; 
     80                                        } else { 
     81                                            $model_id = 0; 
     82                                        } 
     83                                        if(mysql_num_rows($res_ext)) { 
     84                                            $ext = mysql_fetch_row($res_ext); 
     85                                            $description = $ext[1]; 
     86                                            $ext = $ext[0]; 
     87                                        } else { 
     88                                            $ext = ''; 
     89                                            $description = ''; 
     90                                        } 
     91                                        $sql = "INSERT INTO  endpointman_mac_list (mac, model, ext, description) VALUES ('".$mac."','".$model_id."','".$ext."','".$description."')"; 
     92                                        mysql_query($sql); 
     93                                        $message .= "Inserted Mac Address ".$mac."<br/>"; 
     94                                    } else { 
     95                                        $message .= "Invalid Mac on line ". $i. "<br />"; 
     96                                    } 
     97                                } 
     98                                $i++; 
     99                            } 
     100                            fclose($handle); 
     101                            unlink(LOCAL_PATH.$_FILES['import_csv']['name']); 
     102                            $message .= "Please reboot & rebuild all imported phones<br />"; 
     103                            $tpl->assign("message", $message); 
     104                        } else { 
     105                            $tpl->assign("message", "Possible file upload attack!"); 
     106 
     107                        } 
     108                    } 
     109                } 
     110            } 
     111        } 
     112        echo $tpl->draw( 'advanced_settings_iedl' ); 
     113        break; 
     114    case "sh_manager": 
     115        if(isset($_REQUEST['button_hide'])) { 
     116            if(isset($_REQUEST['model'])) { 
     117                $sql = "UPDATE endpointman_model_list SET hidden = 1 WHERE id = ".$_REQUEST['model']; 
     118            } elseif(isset($_REQUEST['brand'])) { 
     119                $sql = "UPDATE endpointman_brand_list SET hidden = 1 WHERE id = ".$_REQUEST['brand']; 
     120            } elseif(isset($_REQUEST['product'])) { 
     121                $sql = "UPDATE endpointman_product_list SET hidden = 1 WHERE id = ".$_REQUEST['product']; 
     122            } 
     123            mysql_query($sql); 
     124        }elseif(isset($_REQUEST['button_show'])) { 
     125            if(isset($_REQUEST['model'])) { 
     126                $sql = "UPDATE endpointman_model_list SET hidden = 0 WHERE id = ".$_REQUEST['model']; 
     127            } elseif(isset($_REQUEST['brand'])) { 
     128                $sql = "UPDATE endpointman_brand_list SET hidden = 0 WHERE id = ".$_REQUEST['brand']; 
     129            } elseif(isset($_REQUEST['product'])) { 
     130                $sql = "UPDATE endpointman_product_list SET hidden = 0 WHERE id = ".$_REQUEST['product']; 
     131            } 
     132            mysql_query($sql); 
     133        } 
     134        $sql="SELECT * from endpointman_brand_list WHERE id > 0 ORDER BY id ASC "; 
     135        $result=mysql_query($sql); 
     136        $i = 0; 
     137        while($row = mysql_fetch_assoc($result)) { 
     138            $row_out[$i] = $row; 
     139            $row_out[$i]['count'] = $i; 
     140            if($row['installed']) { 
     141                $j = 0; 
     142                $sql = 'SELECT * FROM endpointman_product_list WHERE brand = '.$row['id'].' ORDER BY long_name ASC'; 
     143                $result2=mysql_query($sql); 
     144                $xml_data = ""; 
     145                while($row2 = mysql_fetch_assoc($result2)) { 
     146                    $row_out[$i]['products'][$j] = $row2; 
     147                    if($row2['installed']) { 
     148                        $sql = 'SELECT * FROM endpointman_model_list WHERE product_id = '.$row2['id']; 
     149                        $result3=mysql_query($sql); 
     150                        $k = 0; 
     151                        while($row3 = mysql_fetch_assoc($result3)) { 
     152                            $row_out[$i]['products'][$j]['models'][$k] = $row3; 
     153                            $k++; 
     154                        } 
     155                    } 
     156                    $j++; 
     157                } 
     158            } 
     159            $i++; 
     160        } 
     161        $tpl->assign("brand2_list", $row_out); 
     162        echo $tpl->draw( 'advanced_settings_sh_manager' ); 
     163        break; 
     164    case "oui_manager": 
     165        if((isset($_REQUEST['oui_sub'])) AND ($_REQUEST['rb_brand'] > 0) AND ($_REQUEST['oui'] != "")) { 
     166            $sql = "INSERT INTO  endpointman_oui_list (oui, brand, custom) VALUES ('".$_REQUEST['oui']."',  '".$_REQUEST['rb_brand']."',  '1')"; 
     167            $endpoint->db->query($sql); 
     168        } 
     169        if((isset($_REQUEST['delete'])) AND ($_REQUEST['id'] > 0)) { 
     170            $sql = "DELETE FROM endpointman_oui_list WHERE id = ". $_REQUEST['id']; 
     171            $endpoint->db->query($sql); 
     172        } 
     173        $sql = 'SELECT endpointman_oui_list.id, endpointman_oui_list.oui , endpointman_brand_list.name FROM endpointman_oui_list , endpointman_brand_list WHERE endpointman_oui_list.brand = endpointman_brand_list.id AND endpointman_oui_list.custom = 0'; 
     174        $data =& $endpoint->db->getAll($sql, array(), DB_FETCHMODE_ASSOC); 
     175        $i = 0; 
     176        $row_out = array(); 
     177        foreach($data as $row) { 
     178            $row_out[$i] = $row; 
     179            $i++; 
     180        } 
     181        $tpl->assign("oui_list", $row_out); 
     182        $sql = 'SELECT endpointman_oui_list.id, endpointman_oui_list.oui , endpointman_brand_list.name FROM endpointman_oui_list , endpointman_brand_list WHERE endpointman_oui_list.brand = endpointman_brand_list.id AND endpointman_oui_list.custom = 1'; 
     183        $data =& $endpoint->db->getAll($sql, array(), DB_FETCHMODE_ASSOC); 
     184        $i = 0; 
     185        $row_out_custom = array(); 
     186        foreach($data as $row) { 
     187            $row_out_custom[$i] = $row; 
     188            $i++; 
     189        } 
     190        $tpl->assign("oui_list_custom", $row_out_custom); 
     191        $tpl->assign("brand_ava", $endpoint->brandAvailable()); 
     192        echo $tpl->draw( 'advanced_settings_oui' ); 
     193        break; 
     194    case "poce": 
     195        $sql = 'SELECT * FROM `endpointman_product_list` WHERE `hidden` = 0 AND `id` > 0'; 
     196        $result = mysql_query($sql); 
     197        $i = 0; 
     198        while($row = mysql_fetch_assoc($result)) { 
     199            $product_list[$i]['value'] = $row['id']; 
     200            $product_list[$i]['text'] = $row['long_name']; 
     201            if((isset($_REQUEST['product_select'])) AND ($_REQUEST['product_select'] == $row['id'])) { 
     202                $product_list[$i]['selected'] = 1; 
     203            } 
     204            $i++; 
     205        } 
     206        if(isset($_REQUEST['delete'])) { 
     207            $sql = "DELETE FROM endpointman_custom_configs WHERE id =". $_REQUEST['sql']; 
     208            mysql_query($sql); 
     209        } 
     210        if(isset($_REQUEST['file'])) { 
     211            $sql = 'SELECT cfg_dir,directory,config_files FROM endpointman_product_list,endpointman_brand_list WHERE endpointman_product_list.brand = endpointman_brand_list.id AND endpointman_product_list.id =' . $_REQUEST['product_select']; 
     212 
     213            $result = mysql_query($sql); 
     214            $row = mysql_fetch_assoc($result); 
     215            $config_files = explode(",",$row['config_files']); 
     216            $file=PHONE_MODULES_PATH.'endpoint/'.$row['directory']."/".$row['cfg_dir']."/".$config_files[$_REQUEST['file']]; 
     217            if(isset($_REQUEST['config_text'])) { 
     218                if(isset($_REQUEST['button_save'])) { 
     219                    $wfh=fopen($file,'w'); 
     220                    fwrite($wfh,$_REQUEST['config_text']); 
     221                    fclose($wfh); 
     222                }elseif(isset($_REQUEST['button_save_as'])) { 
     223                    $sql = 'INSERT INTO endpointman_custom_configs (name, original_name, product_id, data) VALUES ("'.addslashes($_REQUEST['save_as_name']).'","'.addslashes($config_files[$_REQUEST['file']]).'",'.$_REQUEST['product_select'].',"'.addslashes($_REQUEST['config_text']).'")'; 
     224                    mysql_query($sql); 
     225                } 
     226            } 
     227            $handle = fopen($file, "rb"); 
     228            $contents = fread($handle, filesize($file)); 
     229            fclose($handle); 
     230            $tpl->assign("save_as_name_value", $config_files[$_REQUEST['file']]); 
     231            $tpl->assign("config_data", $contents); 
     232        } elseif(isset($_REQUEST['sql'])) { 
     233            if(isset($_REQUEST['config_text'])) { 
     234                if(isset($_REQUEST['button_save'])) { 
     235                    $sql = "UPDATE endpointman_custom_configs SET data = '".addslashes($_REQUEST['config_text'])."' WHERE id = ".$_REQUEST['sql']; 
     236                    mysql_query($sql); 
     237                }elseif(isset($_REQUEST['button_save_as'])) { 
     238                    $sql = 'SELECT original_name FROM endpointman_custom_configs WHERE id = 11'; 
     239                    $res = mysql_query($sql); 
     240                    $file_name = mysql_fetch_array($res); 
     241 
     242                    $sql = 'INSERT INTO endpointman_custom_configs (name, original_name, product_id, data) VALUES ("'.addslashes($_REQUEST['save_as_name']).'","'.addslashes($file_name['original_name']).'",'.$_REQUEST['product_select'].',"'.addslashes($_REQUEST['config_text']).'")'; 
     243                    mysql_query($sql); 
     244                } 
     245            } 
     246            $sql = 'SELECT * FROM endpointman_custom_configs WHERE id =' . $_REQUEST['sql']; 
     247            $result = mysql_query($sql); 
     248            $row = mysql_fetch_assoc($result); 
     249            $tpl->assign("save_as_name_value", $row['name']); 
     250            $tpl->assign("config_data", $row['data']); 
     251        } 
     252        if(isset($_REQUEST['product_select'])) { 
     253            $sql = 'SELECT cfg_dir,directory,config_files FROM endpointman_product_list,endpointman_brand_list WHERE endpointman_product_list.brand = endpointman_brand_list.id AND endpointman_product_list.id =' . $_REQUEST['product_select']; 
     254            $result = mysql_query($sql); 
     255            $row = mysql_fetch_assoc($result); 
     256            $config_files = explode(",",$row['config_files']); 
     257            $i = 0; 
     258            while($i < count($config_files)) { 
     259                $file_list[$i]['value'] = $i; 
     260                $file_list[$i]['text'] = $config_files[$i]; 
     261                $i++; 
     262            } 
     263            $sql = 'SELECT * FROM `endpointman_custom_configs` WHERE `product_id` = '. $_REQUEST['product_select']; 
     264            $result = mysql_query($sql); 
     265            $i = 0; 
     266            if(mysql_num_rows($result)) { 
     267                while($row2 = mysql_fetch_assoc($result)) { 
     268                    $sql_file_list[$i]['value'] = $row2['id']; 
     269                    $sql_file_list[$i]['text'] = $row2['name']; 
     270                    $sql_file_list[$i]['ref'] = $row2['original_name']; 
     271                    $i++; 
     272                } 
     273            } else { 
     274                $sql_file_list = NULL; 
     275            } 
     276 
     277            require(PHONE_MODULES_PATH.'setup.php'); 
     278 
     279            $class = "endpoint_" . $row['directory'] . "_" . $row['cfg_dir'] . '_phone'; 
     280 
     281            $phone_config = new $class(); 
     282 
     283            if((method_exists($phone_config,'display_options')) AND (method_exists($phone_config,'process_options'))) { 
     284                if(isset($_REQUEST['phone_options'])) { 
     285                    $tpl->assign("options", $phone_config->process_options()); 
     286                } else { 
     287                    $tpl->assign("options", $phone_config->display_options()); 
     288                } 
     289            } 
     290 
     291            $tpl->assign("file_list", $file_list); 
     292            $tpl->assign("sql_file_list", $sql_file_list); 
     293            $tpl->assign("product_selected", $_REQUEST['product_select']); 
     294        } 
     295        $tpl->assign("product_list", $product_list); 
     296        echo $tpl->draw( 'advanced_settings_poce' ); 
     297        break; 
     298    case "settings": 
     299    default: 
     300        if(isset($_REQUEST['button_update_globals'])) { 
     301            $_POST['srvip'] = trim($_POST['srvip']);  #trim whitespace from IP address 
     302 
     303            $_POST['config_loc'] = trim($_POST['config_loc']);  #trim whitespace from Config Location 
     304 
     305            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['srvip'] . "' WHERE var_name='srvip'"; 
     306            $endpoint->db->query($sql); 
     307            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['tz'] . "' WHERE var_name='tz'"; 
     308            $endpoint->db->query($sql); 
     309 
     310            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['config_loc'] . "' WHERE var_name='config_location'"; 
     311            $endpoint->db->query($sql); 
     312 
     313            if((isset($_POST['enable_ari'])) AND ($_POST['enable_ari'] == "on")) { 
     314                $_POST['enable_ari'] = 1; 
     315            } else { 
     316                $_POST['enable_ari'] = 0; 
     317            } 
     318            if((isset($_POST['enable_debug'])) AND ($_POST['enable_debug'] == "on")) { 
     319                $_POST['enable_debug'] = 1; 
     320            } else { 
     321                $_POST['enable_debug'] = 0; 
     322            } 
     323            if((isset($_POST['enable_updates'])) AND ($_POST['enable_updates'] == "on")) { 
     324                $_POST['enable_updates'] = 1; 
     325                $sql = "INSERT INTO cronmanager (module, id, time, freq, lasttime, command) VALUES ('endpointman', 'UPDATES', '23', '24', '0', 'php ".$amp_conf['AMPWEBROOT']."/admin/modules/endpointman/includes/update_check.php')"; 
     326                $endpoint->db->query($sql); 
     327            } else { 
     328                $_POST['enable_updates'] = 0; 
     329                $sql = "DELETE FROM `cronmanager` WHERE CONVERT(`cronmanager`.`module` USING utf8) = 'endpointman' AND CONVERT(`cronmanager`.`id` USING utf8) = 'UPDATES' LIMIT 1"; 
     330                $endpoint->db->query($sql); 
     331            } 
     332 
     333            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['enable_ari'] . "' WHERE var_name='enable_ari'"; 
     334            $endpoint->db->query($sql); 
     335 
     336            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['enable_debug'] . "' WHERE var_name='debug'"; 
     337            $endpoint->db->query($sql); 
     338 
     339            $sql="SELECT gmtoff,gmthr FROM endpointman_time_zones WHERE tz='" . $_POST['tz'] . "'"; 
     340            $row =& $db->getRow($sql, array(), DB_FETCHMODE_ASSOC); 
     341 
     342            $sql="UPDATE endpointman_global_vars SET value='" . $row['gmtoff'] . "' where var_name='gmtoff'"; 
     343            $endpoint->db->query($sql); 
     344            $sql="UPDATE endpointman_global_vars SET value='" . $row['gmthr'] . "' where var_name='gmthr'"; 
     345            $endpoint->db->query($sql); 
     346 
     347            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['asterisk_loc'] . "' WHERE var_name='asterisk_location'"; 
     348            $endpoint->db->query($sql); 
     349 
     350            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['enable_updates'] . "' WHERE var_name='check_updates'"; 
     351            $endpoint->db->query($sql); 
     352 
     353            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['nmap_loc'] . "' WHERE var_name='nmap_location'"; 
     354            $endpoint->db->query($sql); 
     355 
     356            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['arp_loc'] . "' WHERE var_name='arp_location'"; 
     357            $endpoint->db->query($sql); 
     358 
     359            $sql="UPDATE endpointman_global_vars SET value='" . $_POST['disable_htaccess'] . "' WHERE var_name='disable_htaccess'"; 
     360            $endpoint->db->query($sql); 
     361 
     362            $message = "<h3><center>Updated!</center></h3>"; 
     363        } 
     364        //Because we are working with global variables we probably updated them, so lets refresh those variables 
     365        $endpoint->global_cfg =& $db->getAssoc("SELECT var_name, value FROM endpointman_global_vars"); 
     366 
     367 
     368        if($endpoint->global_cfg['enable_ari']) { 
     369            $ari_selected = "checked"; 
     370        } else { 
     371            $ari_selected = ""; 
     372        } 
     373 
     374        if($endpoint->global_cfg['disable_htaccess']) { 
     375            $htaccess_selected = "checked"; 
     376        } else { 
     377            $htaccess_selected = ""; 
     378        } 
     379 
     380        if($endpoint->global_cfg['check_updates']) { 
     381            $updates_selected = "checked"; 
     382        } else { 
     383            $updates_selected = ""; 
     384        } 
     385        if($endpoint->global_cfg['debug']) { 
     386            $debug_selected = "checked"; 
     387            global $debug; 
     388            $debug = $debug . print_r($_REQUEST,true); 
     389            $tpl->assign("debug", $debug); 
     390        } else { 
     391            $debug_selected = ""; 
     392        } 
     393        $tpl->assign("htaccess_selected", $htaccess_selected); 
     394        $tpl->assign("ari_selected", $ari_selected); 
     395        $tpl->assign("updates_selected", $updates_selected); 
     396        $tpl->assign("debug_selected", $debug_selected); 
     397        $tpl->assign("message", $message); 
     398        $tpl->assign("ip", $_SERVER["SERVER_ADDR"]); 
     399        $tpl->assign("srvip", $endpoint->global_cfg['srvip']); 
     400        $tpl->assign("arp_location", $endpoint->global_cfg['arp_location']); 
     401        $tpl->assign("nmap_location", $endpoint->global_cfg['nmap_location']); 
     402        $tpl->assign("asterisk_location", $endpoint->global_cfg['asterisk_location']); 
     403 
     404        $tpl->assign("config_location", $endpoint->global_cfg['config_location']); 
     405        $tpl->assign("list_tz", $endpoint->listTZ($endpoint->global_cfg['tz'])); 
     406        $tpl->assign("brand_list", $endpoint->brandAvailable()); 
     407        echo $tpl->draw( 'advanced_settings_settings' ); 
     408        break; 
    452409} 
    453410?> 
  • contributed_modules/modules/endpointman/includes/ajax_select.php

    r9907 r10198  
    2222include 'jsonwrapper.php'; 
    2323 
    24 $amp_conf = unserialize(base64_decode($_REQUEST['amp_conf'])); 
    25  
    26 $link = mysql_connect('localhost', $amp_conf['AMPDBUSER'], $amp_conf['AMPDBPASS']); 
    27 mysql_select_db($amp_conf['AMPDBNAME'], $link); 
    28  
    29  
    30 ini_set('display_errors', 1); 
    31 error_reporting(E_ALL); 
    3224if(($_REQUEST['id'] == "") OR ($_REQUEST['id'] == "0")) { 
    3325  $out[0]['optionValue'] = ""; 
     
    3729} 
    3830 
    39 if($_REQUEST['type'] == "model") { 
     31if($_REQUEST['atype'] == "model") { 
    4032  $sql = "SELECT * FROM endpointman_model_list WHERE enabled = 1 AND brand =". $_GET['id']; 
    41 } elseif ($_REQUEST['type'] == "template") { 
    42   $sql = "SELECT id, name as model FROM  endpointman_template_list WHERE  product_id =". $_GET['id']; 
    43 } elseif ($_REQUEST['type'] == "template2") { 
    44   $sql = "SELECT endpointman_template_list.id, endpointman_template_list.name as model FROM endpointman_template_list, endpointman_model_list, endpointman_product_list WHERE endpointman_template_list.product_id = endpointman_model_list.product_id AND endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_model_list.id = ". $_GET['id']; 
     33} elseif ($_REQUEST['atype'] == "template") { 
     34  $sql = "SELECT id, name as model FROM  endpointman_template_list WHERE  product_id = '". $_GET['id']."'"; 
     35} elseif ($_REQUEST['atype'] == "template2") { 
     36  $sql = "SELECT endpointman_template_list.id, endpointman_template_list.name as model FROM endpointman_template_list, endpointman_model_list, endpointman_product_list WHERE endpointman_template_list.product_id = endpointman_model_list.product_id AND endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_model_list.id = '". $_GET['id']."'"; 
     37} elseif ($_REQUEST['atype'] == "model_clone") { 
     38        $sql = "SELECT endpointman_model_list.id, endpointman_model_list.model as model FROM endpointman_model_list, endpointman_product_list WHERE endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_model_list.enabled = 1 AND endpointman_model_list.hidden = 0 AND product_id = '". $_GET['id']."'"; 
    4539} 
    4640 
    47 $result = mysql_query($sql); 
    48  
    49 if (($_REQUEST['type'] == "template") OR ($_REQUEST['type'] == "template2")) { 
     41if (($_REQUEST['atype'] == "template") OR ($_REQUEST['atype'] == "template2")) { 
    5042  $out[0]['optionValue'] = 0; 
    5143  $out[0]['optionDisplay'] = "Custom..."; 
    5244  $i=1; 
    53 } elseif ($_REQUEST['type'] == "model") { 
     45} elseif ($_REQUEST['atype'] == "model") { 
    5446  $out[0]['optionValue'] = 0; 
    5547  $out[0]['optionDisplay'] = ""; 
     
    5951} 
    6052 
     53$result = $db->getAll($sql,array(), DB_FETCHMODE_ASSOC); 
    6154 
    62 while ($row = mysql_fetch_assoc($result)) { 
     55foreach($result as $row) { 
    6356  $out[$i]['optionValue'] = $row['id']; 
    6457  $out[$i]['optionDisplay'] = $row['model']; 
  • contributed_modules/modules/endpointman/includes/brand_model_manager.inc

    r10052 r10198  
    2020http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
    2121*/ 
     22 
     23 
    2224$check_for_updates = FALSE; 
    2325global $type; 
     
    2729    $installer = array("type" => 'product', "id" => $_REQUEST['product']); 
    2830  } elseif(isset($_REQUEST['brand'])) { 
    29     $installer = array("type" => 'brand', "id" => $_REQUEST['brand']); 
     31    $installer = array("type" => 'brand', "id" => $_REQUEST['brand']);    
    3032  } 
    3133} elseif((isset($_REQUEST['button_install_firmware'])) OR (isset($_REQUEST['button_update_firmware'])) AND (isset($_REQUEST['product']))) { 
     
    3537} elseif(isset($_REQUEST['button_disable'])) { 
    3638  if(isset($_REQUEST['model'])) { 
    37     $sql = "UPDATE endpointman_model_list SET enabled = 0 WHERE id = ".$_REQUEST['model']
     39    $sql = "UPDATE endpointman_model_list SET enabled = 0 WHERE id = '".$_REQUEST['model']."'"
    3840  } elseif(isset($_REQUEST['brand'])) { 
    39     $sql = "UPDATE endpointman_brand_list SET enabled = 0 WHERE id = ".$_REQUEST['model']
     41    $sql = "UPDATE endpointman_brand_list SET enabled = 0 WHERE id = '".$_REQUEST['model']."'"
    4042  } 
    41   mysql_query($sql); 
     43  $endpoint->db->query($sql); 
    4244} elseif(isset($_REQUEST['button_enable'])) { 
    4345  if(isset($_REQUEST['model'])) { 
    44     $sql = "UPDATE endpointman_model_list SET enabled = 1 WHERE id = ".$_REQUEST['model']
     46    $sql = "UPDATE endpointman_model_list SET enabled = 1 WHERE id = '".$_REQUEST['model']."'"
    4547  } elseif(isset($_REQUEST['brand'])) { 
    46     $sql = "UPDATE endpointman_brand_list SET enabled = 1 WHERE id = ".$_REQUEST['model']
     48    $sql = "UPDATE endpointman_brand_list SET enabled = 1 WHERE id = '".$_REQUEST['model']."'"
    4749  } 
    48   mysql_query($sql); 
     50  $endpoint->db->query($sql); 
    4951} elseif(isset($_REQUEST['button_uninstall'])) { 
    5052  if(isset($_REQUEST['product'])) { 
     
    5961 
    6062$sql="SELECT * from endpointman_brand_list WHERE id > 0 AND hidden = 0 ORDER BY id ASC "; 
    61 $result=mysql_query($sql); 
     63$brand_list =& $endpoint->db->getAll($sql,array(),DB_FETCHMODE_ASSOC); 
    6264$i = 0; 
    6365$note = ""; 
    64 while($row = mysql_fetch_assoc($result)) { 
     66foreach($brand_list as $row) { 
    6567  $row_out[$i] = $row; 
    6668  $row_out[$i]['count'] = $i; 
     
    8385    $j = 0; 
    8486    $sql = 'SELECT * FROM endpointman_product_list WHERE hidden = 0 AND brand = '.$row['id'].' ORDER BY long_name ASC'; 
    85    $result2=mysql_query($sql); 
    86    while($row2 = mysql_fetch_assoc($result2)) { 
     87                $product_list =& $endpoint->db->getAll($sql,array(),DB_FETCHMODE_ASSOC); 
     88                foreach($product_list as $row2) { 
    8789      $row_out[$i]['products'][$j] = $row2; 
    88       if($row_out[$i]['products'][$j]['installed']){ 
    89         $row_out[$i]['products'][$j]['installed_checked'] = 'checked'; 
    90       } 
    9190      if($check_for_updates) { 
    92         if(($temp = $endpoint->product_update_check($row2['id'])) AND ($row2['installed'] == 1)) { 
    93           $row_out[$i]['products'][$j]['update'] = 1; 
    94           $row_out[$i]['products'][$j]['update_vers'] = $temp['data']['version']; 
    95           $row_out[$i]['products'][$j]['update_desc'] = $temp['data']['description']; 
    96           $endpoint->add_freepbx_notification("PU_".$row2['long_name'], '', "There is a Product update for ".$row2['long_name'], "Old Version: ".$row2['cfg_ver']." New Version: ".$temp['data']['version']."<br/>"."Changes: ". $temp['data']['description'].$note, ""); 
    97         } else { 
    98           $row_out[$i]['products'][$j]['update'] = 0; 
    99         } 
    10091        if(($temp = $endpoint->firmware_update_check($row2['id'])) AND (array_key_exists('firmware_vers', $row)) AND ($row['firmware_vers'] > 0)) { 
    10192          $row_out[$i]['products'][$j]['update_fw'] = 1; 
     
    111102 
    112103 
    113       if($row2['installed']) { 
    114         $sql = 'SELECT * FROM endpointman_model_list WHERE hidden = 0 AND product_id = '.$row2['id']
    115        $result3=mysql_query($sql); 
     104      if(1 == 1) { 
     105        $sql = "SELECT * FROM endpointman_model_list WHERE hidden = 0 AND product_id = '".$row2['id']."'"
     106                                $model_list =& $endpoint->db->getAll($sql,array(),DB_FETCHMODE_ASSOC); 
    116107        $k = 0; 
    117         while($row3 = mysql_fetch_assoc($result3)) { 
     108        foreach($model_list as $row3) { 
    118109          $row_out[$i]['products'][$j]['models'][$k] = $row3; 
    119110          if($row_out[$i]['products'][$j]['models'][$k]['enabled']){ 
     
    140131  $brand = $_POST['brand']; 
    141132} 
     133 
    142134if (isset($installer)) { 
    143   $data = base64_encode(serialize($installer)); 
    144   $amp_serial = base64_encode(serialize($amp_conf)); 
    145   $temp_amp['data'] = $data; 
    146   $temp_amp['amp_serial'] = $amp_serial; 
    147   $temp_amp = base64_encode(serialize($temp_amp)); 
    148  
    149   $outfile=LOCAL_PATH."/includes/amp.ini"; 
    150   $wfh=fopen($outfile,'w'); 
    151   fwrite($wfh, $temp_amp); 
    152   fclose($wfh); 
    153135  $tpl->assign("installer_address", $_SERVER["HTTP_HOST"].WEB_PATH."includes/installer.php"); 
     136  $tpl->assign("brand", $brand); 
     137   
    154138} 
    155139 
    156140global $debug, $global_cfg; 
    157 if($global_cfg['debug']) { 
    158   if(file_exists(LOCAL_PATH."debug_installer.log")) { 
    159     $outfile=LOCAL_PATH."debug_installer.log"; 
    160     $wfh=fopen($outfile,'r'); 
    161     $contents = fread($wfh, filesize($outfile)); 
    162     fclose($wfh); 
    163     unlink(LOCAL_PATH."debug_installer.log"); 
    164   } else { 
    165     $contents = ""; 
    166   } 
    167   $debug .= $contents; 
    168   $tpl->assign("debug", $debug); 
    169 
     141 
    170142$tpl->assign("error", $error); 
    171143$tpl->assign("web_var", "?type=$type"); 
    172144$tpl->assign("brand2_list", $row_out); 
     145$tpl->assign("error", $endpoint->error['brand_update_check']); 
     146 
    173147//draw the template  
    174148echo $tpl->draw( 'brand_model_manager' ); 
  • contributed_modules/modules/endpointman/includes/devices_manager.inc

    r10052 r10198  
    2020http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
    2121*/ 
    22 global $global_cfg, $amp_conf, $debug, $type; 
    2322 
    2423//Set opened variables 
     
    3130$mode = NULL; 
    3231 
    33 $sql = "SELECT * FROM endpointman_product_list WHERE id > 0"; 
    34 $result = mysql_query($sql); 
    35 $sql2 = "SELECT * FROM endpointman_mac_list"; 
    36 $result2 = mysql_query($sql2); 
    37 if((!mysql_num_rows($result)) && (!mysql_num_rows($result2))) { 
    38   $message = _("Welcome to Endpoint Manager")."!<br />"._("You have no products (Modules) installed, click")." <a href=\"config.php?display=epm_config&type=$type\">"._("here")."</a> "._("to install some"); 
    39   $no_add = TRUE; 
    40 } elseif(!mysql_num_rows($result)) { 
    41   $message = "Thanks for upgrading to version 2.0! Please head on over to <a href=\"config.php?display=epm_config&type=$type\">\"Brand Configurations/Setup\"</a> to setup and install phone configurations"; 
    42   $no_add = TRUE; 
    43 } elseif($global_cfg['srvip'] == "") { 
    44   $message = "Your Global Variables are not set! Please head on over to <a href=\"config.php?type=$type&display=epm_advanced\">\"Advanced Settings\"</a> to setup your configuration"; 
    45   $no_add = TRUE; 
    46 
     32$family_list =& $endpoint->db->getAll("SELECT * FROM endpointman_product_list WHERE id > 0",array(),DB_FETCHMODE_ASSOC); 
     33 
     34$mac_list =& $endpoint->db->getAll("SELECT * FROM endpointman_mac_list",array(),DB_FETCHMODE_ASSOC); 
     35 
     36if((!$family_list) && (!$mac_list)) { 
     37    $message = _("Welcome to Endpoint Manager")."!<br />"._("You have no products (Modules) installed, click")." <a href=\"config.php?display=epm_config&type=$type\">"._("here")."</a> "._("to install some"); 
     38    $no_add = TRUE; 
     39} elseif(!$family_list) { 
     40    $message = "Thanks for upgrading to version 2.0! Please head on over to <a href=\"config.php?display=epm_config&type=$type\">\"Brand Configurations/Setup\"</a> to setup and install phone configurations"; 
     41    $no_add = TRUE; 
     42} elseif($endpoint->global_cfg['srvip'] == "") { 
     43    $message = "Your Global Variables are not set! Please head on over to <a href=\"config.php?type=$type&display=epm_advanced\">\"Advanced Settings\"</a> to setup your configuration"; 
     44    $no_add = TRUE; 
     45
     46 
    4747if(isset($_REQUEST['button_add'])) { 
    48   if($mac = $endpoint->mac_check_clean($_REQUEST['mac'])) { 
    49     $sql = 'SELECT `name` FROM `users` WHERE `extension` = '.$_REQUEST['ext_list']; 
    50     $result=mysql_query($sql); 
    51     $name=mysql_fetch_array($result); 
    52    
    53     $sql = 'SELECT endpointman_product_list. * , endpointman_brand_list.directory FROM endpointman_model_list, endpointman_brand_list, endpointman_product_list WHERE endpointman_model_list.id =  \''.$_REQUEST['model_list'].'\' AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id'; 
    54     $result=mysql_query($sql); 
    55     $row=mysql_fetch_array($result); 
    56    
    57     $cfg_data = unserialize($row['cfg_data']); 
    58     $count = count($cfg_data); 
    59     $i = 0; 
    60     while($i < $count) { 
    61       if(array_key_exists('variable',$cfg_data[$i])) { 
    62         $temping = str_replace('$','',$cfg_data[$i]['variable']); 
    63         if(is_array($cfg_data[$i]['default_value'])) { 
    64           $custom_cfg_data[$temping]['value'] = ''; 
    65         } else { 
    66           $custom_cfg_data[$temping]['value'] = $cfg_data[$i]['default_value']; 
    67         } 
    68       } 
    69       $i++; 
    70     } 
    71     $custom_cfg_data = serialize($custom_cfg_data); 
    72    
    73     $sql = "INSERT INTO `endpointman_mac_list` (`mac`, `model`, `ext`, `description`, `custom_cfg_data`, `custom_cfg_template`) VALUES ('".$mac."', '".$_REQUEST['model_list']."', '".$_REQUEST['ext_list']."', '".$name['name']."', '".$custom_cfg_data."', '".$_REQUEST['template_list']."')"; 
    74     mysql_query($sql); 
    75        
    76     $sql = 'SELECT id FROM endpointman_mac_list WHERE ext ='. $_REQUEST['ext_list']; 
    77     $result=mysql_query($sql); 
    78     $ext_id=mysql_fetch_array($result); 
    79    
    80     require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    81  
    82     $phone_config = new $row['cfg_dir'](); 
    83     $phone_config->generate_config($ext_id['id']); 
    84     $message = _("Saved")."!"; 
    85   } else { 
    86     $message = _("Invalid MAC Address")."!"; 
    87   } 
     48    if($mac = $endpoint->mac_check_clean($_REQUEST['mac'])) { 
     49        $sql = 'SELECT `name` FROM `users` WHERE `extension` = '.$_REQUEST['ext_list']; 
     50        $name =& $endpoint->db->getOne($sql); 
     51 
     52        $sql = 'SELECT endpointman_product_list. * , endpointman_model_list.template_data, endpointman_brand_list.directory FROM endpointman_model_list, endpointman_brand_list, endpointman_product_list WHERE endpointman_model_list.id =  \''.$_REQUEST['model_list'].'\' AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id'; 
     53         
     54        $row =& $endpoint->db->getRow($sql,array(),DB_FETCHMODE_ASSOC); 
     55 
     56        $cfg_data = unserialize($row['template_data']); 
     57        $count = count($cfg_data); 
     58        $i = 0; 
     59        while($i < $count) { 
     60            if(array_key_exists('variable',$cfg_data[$i])) { 
     61                $temping = str_replace('$','',$cfg_data[$i]['variable']); 
     62                if(is_array($cfg_data[$i]['default_value'])) { 
     63                    $custom_cfg_data[$temping]['value'] = ''; 
     64                } else { 
     65                    $custom_cfg_data[$temping]['value'] = $cfg_data[$i]['default_value']; 
     66                } 
     67            } 
     68            $i++; 
     69        } 
     70        $custom_cfg_data = serialize($custom_cfg_data); 
     71 
     72        $sql = "INSERT INTO `endpointman_mac_list` (`mac`, `model`, `ext`, `description`, `custom_cfg_data`, `custom_cfg_template`) VALUES ('".$mac."', '".$_REQUEST['model_list']."', '".$_REQUEST['ext_list']."', '".$name."', '".$custom_cfg_data."', '".$_REQUEST['template_list']."')"; 
     73        $endpoint->db->query($sql); 
     74 
     75        $sql = 'SELECT id FROM endpointman_mac_list WHERE ext ='. $_REQUEST['ext_list']; 
     76        $ext_id =& $endpoint->db->getOne($sql); 
     77 
     78        $message = _("Saved")."!"; 
     79    } else { 
     80        $message = _("Invalid MAC Address")."!"; 
     81    } 
    8882} elseif(isset($_REQUEST['editready'])) { 
    89  $edit_row=$endpoint->get_phone_info($_REQUEST['id']); 
    90  $edit_row['id'] = $_REQUEST['id']; 
    91  $mode = "EDIT"; 
     83    $edit_row=$endpoint->get_phone_info($_REQUEST['id']); 
     84    $edit_row['id'] = $_REQUEST['id']; 
     85    $mode = "EDIT"; 
    9286} elseif(isset($_REQUEST['button_edit'])) { 
    93   $template_editor = TRUE; 
    94   if ($_REQUEST['template_list'] == 0) { 
    95     $endpoint->edit_template_display($_REQUEST['edit_id'],1); 
    96   } else { 
    97     $endpoint->edit_template_display($_REQUEST['template_list'],0); 
    98   } 
     87 
     88    if(empty($_REQUEST['edit_id'])) { 
     89        $message = _("No Device Selected to Edit!")."!"; 
     90    } else { 
     91        $template_editor = TRUE; 
     92        if ($_REQUEST['template_list'] == 0) { 
     93            $endpoint->edit_template_display($_REQUEST['edit_id'],1); 
     94        } else { 
     95            $endpoint->edit_template_display($_REQUEST['template_list'],0); 
     96        } 
     97    } 
    9998} elseif(isset($_REQUEST['button_save'])) { 
    100   $sql = "SELECT name FROM users WHERE extension = ".$_REQUEST['ext_list']; 
    101   $result=mysql_query($sql); 
    102   $name=mysql_fetch_array($result); 
    103  
    104   $sql = "UPDATE endpointman_mac_list SET custom_cfg_template = '".$_REQUEST['template_list']."', model = ".$_REQUEST['model_list'].", ext = ".$_REQUEST['ext_list'].", description = '".$name['name']."' WHERE id =  ". $_REQUEST['edit_id']; 
    105   $result=mysql_query($sql);     
    106  
    107   $row = $endpoint->get_phone_info($_REQUEST['edit_id']); 
    108  
    109   require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    110   $phone_config = new $row['cfg_dir'](); 
    111  
    112   $phone_config->generate_config($_REQUEST['edit_id']); 
    113   $message = _("Saved")."!"; 
     99    $sql = "SELECT name FROM users WHERE extension = ".$_REQUEST['ext_list']; 
     100    $name=$endpoint->db->getOne($sql); 
     101 
     102    $sql = "UPDATE endpointman_mac_list SET custom_cfg_template = '".$_REQUEST['template_list']."', model = '".$_REQUEST['model_list']."', ext = ".$_REQUEST['ext_list'].", description = '".$name."' WHERE id =  ". $_REQUEST['edit_id']; 
     103    $endpoint->db->query($sql); 
     104 
     105    $row = $endpoint->get_phone_info($_REQUEST['edit_id']); 
     106 
     107    $endpoint->prepare_configs($row); 
     108 
     109    $message = _("Saved")."!"; 
    114110} elseif(isset($_REQUEST['deleteready'])) { 
    115  $row = $endpoint->get_phone_info($_REQUEST['id']); 
    116  
    117  if(file_exists(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc')) { 
    118    require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    119  
    120    $phone_config = new $row['cfg_dir'](); 
    121    $phone_config->delete_config($_REQUEST['id']); 
    122    unset($phone_config); 
    123 
    124  
    125  $sql = "DELETE FROM endpointman_mac_list WHERE id=".$_REQUEST['id']; 
    126  $result=mysql_query($sql); 
     111    $row = $endpoint->get_phone_info($_REQUEST['id']); 
     112 
     113    if(file_exists(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc')) { 
     114        require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
     115 
     116        $phone_config = new $row['cfg_dir'](); 
     117        $phone_config->delete_config($_REQUEST['id']); 
     118        unset($phone_config); 
     119   
     120 
     121    $sql = "DELETE FROM endpointman_mac_list WHERE id=".$_REQUEST['id']; 
     122    $result=mysql_query($sql); 
    127123} elseif((isset($_REQUEST['button_add_selected_phones'])) AND (isset($_REQUEST['add']))) { 
    128  $list_num = count($_REQUEST['add']); 
    129  for($i=0;$i<$list_num;$i++) { 
    130    $num = $_REQUEST['add'][$i]; 
    131     if($mac = $endpoint->mac_check_clean($_REQUEST['mac_'.$num])) {            
    132      $sql = "SELECT * FROM endpointman_mac_list WHERE ext = ".$_REQUEST['ext_list_'.$num]; 
    133      $result=mysql_query($sql); 
    134      if(!mysql_num_rows($result)) { 
    135        $sql = 'SELECT `name` FROM `users` WHERE `extension` = '.$_REQUEST['ext_list_'.$num]; 
    136        $result=mysql_query($sql); 
    137        $name=mysql_fetch_array($result); 
    138  
    139        $sql = 'SELECT endpointman_product_list. * , endpointman_brand_list.directory FROM endpointman_model_list, endpointman_brand_list, endpointman_product_list WHERE endpointman_model_list.id =  \''.$_REQUEST['model_list_'.$num].'\' AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id'; 
    140        $result=mysql_query($sql); 
    141        $row=mysql_fetch_array($result); 
    142  
    143        $sql = "INSERT INTO `endpointman_mac_list` (`mac`, `model`, `ext`, `description`) VALUES ('".$mac."', '".$_REQUEST['model_list_'.$num]."', '".$_REQUEST['ext_list_'.$num]."', '".$name['name']."')"; 
    144        mysql_query($sql); 
    145  
    146        $sql = 'SELECT id FROM endpointman_mac_list WHERE ext ='. $_REQUEST['ext_list_'.$num]; 
    147        $result=mysql_query($sql); 
    148        $ext_id=mysql_fetch_array($result); 
    149          
    150        if(file_exists(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc')) { 
    151          require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    152          $phone_config = new $row['cfg_dir'](); 
    153          $phone_config->generate_config($ext_id['id']); 
    154          unset($phone_config); 
    155        
    156         $message = _("Saved")."!";       
    157      } else { 
    158        $message = _("One or more selected phones had the same extension as a phone already added, these devices were not added"); 
    159      
    160        
    161    } else { 
    162      $message = _("Invalid MAC Address"); 
    163    
    164 
     124    $list_num = count($_REQUEST['add']); 
     125    for($i=0;$i<$list_num;$i++) { 
     126        $num = $_REQUEST['add'][$i]; 
     127        if($mac = $endpoint->mac_check_clean($_REQUEST['mac_'.$num])) { 
     128            $sql = "SELECT * FROM endpointman_mac_list WHERE ext = ".$_REQUEST['ext_list_'.$num]; 
     129            $result=mysql_query($sql); 
     130            if(!mysql_num_rows($result)) { 
     131                $sql = 'SELECT `name` FROM `users` WHERE `extension` = '.$_REQUEST['ext_list_'.$num]; 
     132                $result=mysql_query($sql); 
     133                $name=mysql_fetch_array($result); 
     134 
     135                $sql = 'SELECT endpointman_product_list. * , endpointman_brand_list.directory FROM endpointman_model_list, endpointman_brand_list, endpointman_product_list WHERE endpointman_model_list.id =  \''.$_REQUEST['model_list_'.$num].'\' AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id'; 
     136                $result=mysql_query($sql); 
     137                $row=mysql_fetch_array($result); 
     138 
     139                $sql = "INSERT INTO `endpointman_mac_list` (`mac`, `model`, `ext`, `description`) VALUES ('".$mac."', '".$_REQUEST['model_list_'.$num]."', '".$_REQUEST['ext_list_'.$num]."', '".$name['name']."')"; 
     140                mysql_query($sql); 
     141 
     142                $sql = 'SELECT id FROM endpointman_mac_list WHERE ext ='. $_REQUEST['ext_list_'.$num]; 
     143                $result=mysql_query($sql); 
     144                $ext_id=mysql_fetch_array($result); 
     145 
     146                if(file_exists(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc')) { 
     147                    require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
     148                    $phone_config = new $row['cfg_dir'](); 
     149                    $phone_config->generate_config($ext_id['id']); 
     150                    unset($phone_config); 
     151               
     152                $message = _("Saved")."!"; 
     153            } else { 
     154                $message = _("One or more selected phones had the same extension as a phone already added, these devices were not added"); 
     155           
     156 
     157        } else { 
     158            $message = _("Invalid MAC Address"); 
     159       
     160   
    165161} elseif((isset($_REQUEST['button_update_phones'])) AND (isset($_REQUEST['selected']))) { 
    166   $list_num = count($_REQUEST['selected']); 
    167   for($i=0;$i<$list_num;$i++) { 
    168     if(($_REQUEST['brand_list_selected'] > 0) AND ($_REQUEST['model_list_selected'] > 0)) { 
    169       $sql = "UPDATE endpointman_mac_list SET custom_cfg_data = '', custom_cfg_template = '', user_cfg_data = '', config_files_override = '', model = ".$_REQUEST['model_list_selected']." WHERE id =  ". $_REQUEST['selected'][$i]; 
    170       $result=mysql_query($sql);     
    171  
    172       $row = $endpoint->get_phone_info($_REQUEST['selected'][$i]); 
    173  
    174       require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    175       $phone_config = new $row['cfg_dir'](); 
    176  
    177       $phone_config->generate_config($_REQUEST['selected'][$i]); 
    178       $message = _("Saved")."!";       
    179     } else { 
    180       $message = _("Please select a Brand and/or Model"); 
    181     } 
    182   } 
     162    if(($_REQUEST['brand_list_selected'] > 0) AND ($_REQUEST['model_list_selected'] > 0)) { 
     163 
     164        foreach($_REQUEST['selected'] as $key => $data) { 
     165            $sql = "UPDATE endpointman_mac_list SET custom_cfg_data = '', custom_cfg_template = '', user_cfg_data = '', config_files_override = '', model = '".$_REQUEST['model_list_selected']."' WHERE id =  ". $_REQUEST['selected'][$key]; 
     166            $endpoint->db->query($sql); 
     167 
     168            $phone_info = $endpoint->get_phone_info($_REQUEST['selected'][$key]); 
     169            $endpoint->prepare_configs($phone_info); 
     170 
     171            $message = _("Saved")."!"; 
     172 
     173        } 
     174    } else { 
     175        $message = _("Please select a Brand and/or Model"); 
     176    } 
    183177} elseif(isset($_REQUEST['button_delete_selected_phones'])) { 
    184   if(isset($_REQUEST['selected'])) { 
    185     $list_num = count($_REQUEST['selected']); 
    186     for($i=0;$i<$list_num;$i++) { 
    187       $row = $endpoint->get_phone_info($_REQUEST['selected'][$i]); 
    188  
    189       if(file_exists(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc')) { 
    190         require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    191  
    192         $phone_config = new $row['cfg_dir'](); 
    193         $phone_config->delete_config($_REQUEST['selected'][$i]); 
    194         unset($phone_config); 
    195       } 
    196  
    197       $sql = "DELETE FROM endpointman_mac_list WHERE id=".$_REQUEST['selected'][$i]; 
    198       $result=mysql_query($sql); 
    199     } 
    200   } else { 
    201     $message = _("No Phones Selected")."!"; 
    202   } 
     178    if(isset($_REQUEST['selected'])) { 
     179        foreach($_REQUEST['selected'] as $key => $data) { 
     180            $sql = "DELETE FROM endpointman_mac_list WHERE id=".$_REQUEST['selected'][$key]; 
     181            $endpoint->db->query($sql); 
     182        } 
     183    } else { 
     184        $message = _("No Phones Selected")."!"; 
     185    } 
    203186} elseif(isset($_REQUEST['button_go'])) { 
    204  if ($_REQUEST['nmap'] == 1) { 
    205    $temp = $endpoint->discover_new($_REQUEST['netmask']); 
    206  } else { 
    207    $temp = $endpoint->discover_new($_REQUEST['netmask'], FALSE); 
    208 
    209  $count = count($temp); 
    210  $i = 0; 
    211  while($i < $count) { 
    212    if (!$temp[$i]['endpoint_managed']) { 
    213      if ($temp[$i]['brand']) { 
    214        $final[$i] = $temp[$i]; 
    215        $final[$i]['id'] = $i; 
    216        $sqln = "SELECT * FROM endpointman_model_list WHERE enabled = 1 AND brand =".$temp[$i]['brand_id']; 
    217        $model_list=mysql_query($sqln); 
    218        $j = 0; 
    219        while($row = mysql_fetch_assoc($model_list)) { 
    220          $final[$i]['list'][$j] = $row; 
    221          $j++; 
    222        
    223      
    224    
    225    $i++; 
    226 
    227  if($final) { 
    228    $final = array_values($final); 
    229  } else { 
    230    $final = NULL; 
    231    $message = _("No Devices Found"); 
    232 
    233  $searched = 1; 
     187    if ($_REQUEST['nmap'] == 1) { 
     188        $temp = $endpoint->discover_new($_REQUEST['netmask']); 
     189    } else { 
     190        $temp = $endpoint->discover_new($_REQUEST['netmask'], FALSE); 
     191   
     192    $count = count($temp); 
     193    $i = 0; 
     194    while($i < $count) { 
     195        if (!$temp[$i]['endpoint_managed']) { 
     196            if ($temp[$i]['brand']) { 
     197                $final[$i] = $temp[$i]; 
     198                $final[$i]['id'] = $i; 
     199                $sqln = "SELECT * FROM endpointman_model_list WHERE enabled = 1 AND brand =".$temp[$i]['brand_id']; 
     200                $model_list=mysql_query($sqln); 
     201                $j = 0; 
     202                while($row = mysql_fetch_assoc($model_list)) { 
     203                    $final[$i]['list'][$j] = $row; 
     204                    $j++; 
     205               
     206           
     207       
     208        $i++; 
     209   
     210    if($final) { 
     211        $final = array_values($final); 
     212    } else { 
     213        $final = NULL; 
     214        $message = _("No Devices Found"); 
     215   
     216    $searched = 1; 
    234217} elseif(isset($_REQUEST['button_rebuild_configs_for_all_phones'])) { 
    235   $sql = "SELECT endpointman_brand_list.directory, endpointman_mac_list.id, endpointman_product_list.cfg_dir FROM endpointman_mac_list, endpointman_brand_list, endpointman_product_list, endpointman_model_list WHERE endpointman_brand_list.id = endpointman_product_list.brand AND endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_mac_list.model = endpointman_model_list.id ORDER BY endpointman_product_list.cfg_dir ASC"; 
    236   $result = mysql_query($sql); 
    237   while($row = mysql_fetch_assoc($result)) { 
    238     if(file_exists(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc')) { 
    239       require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    240       $phone_config = new $row['cfg_dir'](); 
    241       $phone_config->generate_config($row['id']); 
    242       unset($phone_config); 
    243     } 
    244   } 
     218    $sql = "SELECT endpointman_mac_list.id FROM endpointman_mac_list, endpointman_brand_list, endpointman_product_list, endpointman_model_list WHERE endpointman_brand_list.id = endpointman_product_list.brand AND endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_mac_list.model = endpointman_model_list.id ORDER BY endpointman_product_list.cfg_dir ASC"; 
     219    $mac_list =& $endpoint->db->getAll($sql,array(),DB_FETCHMODE_ASSOC); 
     220    foreach($mac_list as $data) { 
     221        $phone_info = $endpoint->get_phone_info($data['id']); 
     222        $endpoint->prepare_configs($phone_info); 
     223    } 
    245224} elseif(isset($_REQUEST['button_rebuild_reboot'])) { 
    246   if($_REQUEST['product_select'] == "") { 
    247     $message = _("Please select a product"); 
    248   } elseif($_REQUEST['template_selector'] == "") { 
    249     $message = _("Please select a template"); 
    250   } else { 
    251     $sql = "SELECT endpointman_brand_list.directory, endpointman_mac_list.id, endpointman_product_list.cfg_dir FROM endpointman_mac_list, endpointman_brand_list, endpointman_product_list, endpointman_model_list WHERE endpointman_brand_list.id = endpointman_product_list.brand AND endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_mac_list.model = endpointman_model_list.id AND endpointman_product_list.id = ".$_REQUEST['product_select']; 
    252     $result = mysql_query($sql); 
    253     while($row = mysql_fetch_assoc($result)) { 
    254       $sql = "UPDATE endpointman_mac_list SET custom_cfg_template = '".$_REQUEST['template_selector']."' WHERE id =  ". $row['id']; 
    255       mysql_query($sql); 
    256       if(file_exists(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc')) { 
    257         require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    258         $phone_config = new $row['cfg_dir'](); 
    259         $phone_config->generate_config($row['id']); 
    260         unset($phone_config); 
    261       } 
    262     } 
    263   } 
     225    if($_REQUEST['product_select'] == "") { 
     226        $message = _("Please select a product"); 
     227    } elseif($_REQUEST['template_selector'] == "") { 
     228        $message = _("Please select a template"); 
     229    } else { 
     230        $sql = "SELECT endpointman_mac_list.id FROM endpointman_mac_list, endpointman_brand_list, endpointman_product_list, endpointman_model_list WHERE endpointman_brand_list.id = endpointman_product_list.brand AND endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_mac_list.model = endpointman_model_list.id AND endpointman_product_list.id = '".$_REQUEST['product_select']."'"; 
     231        $data =& $endpoint->db->getAll($sql,array(),DB_FETCHMODE_ASSOC); 
     232        foreach($data as $row) { 
     233            $sql = "UPDATE endpointman_mac_list SET custom_cfg_template = '".$_REQUEST['template_selector']."' WHERE id =  ". $row['id']; 
     234            $endpoint->db->query($sql); 
     235            $phone_info = $endpoint->get_phone_info($row['id']); 
     236            $endpoint->prepare_configs($phone_info); 
     237        } 
     238    } 
    264239} elseif(isset($_REQUEST['button_rebuild_selected'])) { 
    265   if(isset($_REQUEST['selected'])) { 
    266     $list_num = count($_REQUEST['selected']); 
    267     for($i=0;$i<$list_num;$i++) { 
    268       $row = $endpoint->get_phone_info($_REQUEST['selected'][$i]); 
    269       if(file_exists(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc')) { 
    270         require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    271         $phone_config = new $row['cfg_dir'](); 
    272         $phone_config->generate_config($_REQUEST['selected'][$i]); 
    273         unset($phone_config); 
    274       } 
    275     } 
    276   } else { 
    277     $message = _("No Phones Selected")."!"; 
    278   } 
     240    if(isset($_REQUEST['selected'])) { 
     241        foreach($_REQUEST['selected'] as $key => $data) { 
     242            $phone_info = $endpoint->get_phone_info($_REQUEST['selected'][$key]); 
     243            $endpoint->prepare_configs($phone_info); 
     244        } 
     245    } else { 
     246        $message = _("No Phones Selected")."!"; 
     247    } 
    279248} elseif(isset($_REQUEST['button_reboot_this_brand'])) { 
    280   if($_REQUEST['rb_brand'] != "") { 
    281     $sql = 'SELECT endpointman_mac_list.id , endpointman_brand_list.directory , endpointman_product_list.cfg_dir FROM endpointman_mac_list , endpointman_model_list , endpointman_brand_list , endpointman_product_list WHERE endpointman_brand_list.id = endpointman_model_list.brand AND endpointman_model_list.id = endpointman_mac_list.model AND endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_brand_list.id = '.$_REQUEST['rb_brand'].' ORDER BY endpointman_product_list.cfg_dir ASC'; 
    282     $result = mysql_query($sql); 
    283     while($row = mysql_fetch_assoc($result)) { 
    284       if(file_exists(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc')) { 
    285         require_once(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    286         $phone_config = new $row['cfg_dir'](); 
    287         $phone_config->reboot($row['id']); 
    288         unset($phone_config); 
    289       } 
    290     } 
    291   } else { 
    292     $message = _("No Brand Selected for Reboot"); 
    293   } 
     249    if($_REQUEST['rb_brand'] != "") { 
     250        $sql = 'SELECT endpointman_mac_list.id FROM endpointman_mac_list , endpointman_model_list , endpointman_brand_list , endpointman_product_list WHERE endpointman_brand_list.id = endpointman_model_list.brand AND endpointman_model_list.id = endpointman_mac_list.model AND endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_brand_list.id = '.$_REQUEST['rb_brand'].' ORDER BY endpointman_product_list.cfg_dir ASC'; 
     251        $data =& $endpoint->db->getAll($sql,array(),DB_FETCHMODE_ASSOC); 
     252        foreach($data as $row) { 
     253            //TODO: Add Reboot back here 
     254        } 
     255    } else { 
     256        $message = _("No Brand Selected for Reboot"); 
     257    } 
    294258} 
    295259 
    296260$sql = 'SELECT endpointman_mac_list.id , endpointman_mac_list.mac , endpointman_model_list.model , endpointman_mac_list.ext , endpointman_mac_list.description , endpointman_brand_list.name, endpointman_mac_list.custom_cfg_data, endpointman_mac_list.custom_cfg_template FROM endpointman_mac_list , endpointman_model_list , endpointman_brand_list WHERE ( endpointman_model_list.id = endpointman_mac_list.model ) AND ( endpointman_model_list.brand = endpointman_brand_list.id )'; 
    297 $result=mysql_query($sql); 
     261 
     262$devices_list =& $endpoint->db->getAll($sql,array(),DB_FETCHMODE_ASSOC); 
    298263 
    299264 
    300265$i = 0; 
    301 while ($row = mysql_fetch_assoc($result)) { #Displays the phones in the database with edit and delete buttons 
    302   $list[$i] = $row; 
    303   if (($row['custom_cfg_template'] == 0) && (isset($row['custom_cfg_data'])) ) { 
    304     $list[$i]['template_name'] = "Custom-".$row['mac']; 
    305   } elseif((!isset($row['custom_cfg_data'])) && ($row['custom_cfg_template'] == 0)) { 
    306     $list[$i]['template_name'] = "N/A"; 
    307   } else { 
    308     $sql = "SELECT name FROM endpointman_template_list WHERE id =".$row['custom_cfg_template']; 
    309    $result1=mysql_query($sql)
    310    $row2 = mysql_fetch_assoc($result1); 
    311    $list[$i]['template_name'] = $row2['name']
    312 
    313  $i++; 
     266$list = array(); 
     267foreach($devices_list as $row) {  #Displays the phones in the database with edit and delete buttons 
     268    $list[$i] = $row; 
     269    if (($row['custom_cfg_template'] == 0) && (isset($row['custom_cfg_data'])) ) { 
     270        $list[$i]['template_name'] = "Custom-".$row['mac']; 
     271    } elseif((!isset($row['custom_cfg_data'])) && ($row['custom_cfg_template'] == 0)) { 
     272        $list[$i]['template_name'] = "N/A"; 
     273    } else { 
     274        $sql = "SELECT name FROM endpointman_template_list WHERE id =".$row['custom_cfg_template']
     275        $row2 =& $endpoint->db->getOne($sql); 
     276        $list[$i]['template_name'] = $row2
     277   
     278    $i++; 
    314279}   # End while 
    315280 
    316 $sql = 'SELECT * FROM  endpointman_mac_list WHERE  model =0'; 
    317 $result=mysql_query($sql); 
    318  
    319 while ($row = mysql_fetch_assoc($result)) { #Displays unknown phones in the database with edit and delete buttons 
    320   $list[$i] = $row; 
    321   $list[$i]['name'] = _("Unknown"); 
    322   $list[$i]['template_name'] = "N/A"; 
    323   $list[$i]['model'] = _("Unknown"); 
    324   $i++; 
     281 
     282$sql = 'SELECT * FROM  endpointman_mac_list WHERE model = 0'; 
     283 
     284$unknown_list =& $endpoint->db->getAll($sql,array(),DB_FETCHMODE_ASSOC); 
     285 
     286foreach($unknown_list as $row) {  #Displays unknown phones in the database with edit and delete buttons 
     287    $list[$i] = $row; 
     288    $list[$i]['name'] = _("Unknown"); 
     289    $list[$i]['template_name'] = "N/A"; 
     290    $list[$i]['model'] = _("Unknown"); 
     291    $i++; 
    325292} 
    326293 
     
    329296$amp_send['AMPDBNAME'] = $amp_conf['AMPDBNAME']; 
    330297 
    331 $sql = "SELECT DISTINCT endpointman_product_list.id, endpointman_product_list.long_name  FROM endpointman_product_list, endpointman_template_list WHERE endpointman_product_list.installed = 1 AND endpointman_template_list.product_id = endpointman_product_list.id"; 
    332 $result = mysql_query($sql); 
     298$sql = "SELECT DISTINCT endpointman_product_list.id, endpointman_product_list.long_name  FROM endpointman_product_list, endpointman_template_list WHERE endpointman_product_list.hidden = 0 AND endpointman_template_list.product_id = endpointman_product_list.id"; 
     299$result =& $endpoint->db->getAll($sql,array(),DB_FETCHMODE_ASSOC); 
     300 
    333301$product_list[0]['value'] = ""; 
    334302$product_list[0]['text'] = ""; 
    335303$i = 1; 
    336 while($row = mysql_fetch_assoc($result)) { 
    337   $product_list[$i]['value'] = $row['id']; 
    338   $product_list[$i]['text'] = $row['long_name']; 
    339   $i++; 
    340 
     304foreach($result as $row) { 
     305    $product_list[$i]['value'] = $row['id']; 
     306    $product_list[$i]['text'] = $row['long_name']; 
     307    $i++; 
     308
     309 
    341310//initialize a Rain TPL object 
    342311if (isset($template_editor)) { 
    343    
     312 
    344313} else { 
    345  $tpl = new RainTPL( LOCAL_PATH.'templates' ); 
    346  $tpl->assign("list", $list); 
    347  $tpl->assign("error", ""); 
    348  $tpl->assign("srvip", $_SERVER["SERVER_ADDR"]); 
    349   $tpl->assign("web_var", "?type=$type"); 
    350  if($ma = $endpoint->modelsAvailable()) { 
    351    $tpl->assign("models_ava", $ma); 
    352 
    353  $tpl->assign("web_path", WEB_PATH); 
    354  $tpl->assign("product_list", $product_list); 
    355  $tpl->assign("display_ext", $endpoint->displayExtension()); 
    356  $tpl->assign("brand_ava", $endpoint->brandAvailable()); 
    357  $tpl->assign("unmanaged", $final); 
    358  $tpl->assign("button", $button); 
    359  $tpl->assign("searched", $searched); 
    360  $tpl->assign("edit", $edit); 
    361  $tpl->assign("amp_conf_serial", base64_encode(serialize($amp_send))); 
    362  $tpl->assign("no_add", $no_add); 
    363  $tpl->assign("mode", $mode); 
    364    
    365  if(isset($final)) { 
    366    $tpl->assign("cached_devices", base64_encode(serialize($final))); 
    367 
    368  
    369  if (isset($mode) && ($mode == "EDIT")) { 
    370    if($ma = $endpoint->modelsAvailable($edit_row['model_id'],NULL,$edit_row['brand_id'])) { 
    371      $tpl->assign("mac", $edit_row['mac']); 
    372       $tpl->assign("name", $edit_row['name']);   
    373      $tpl->assign("models_ava", $ma); 
    374      $tpl->assign("display_ext", $endpoint->displayExtension($edit_row['ext'])); 
    375      $tpl->assign("display_templates", $endpoint->display_templates($edit_row['product_id'],$edit_row['custom_cfg_template'])); 
    376       $tpl->assign("edit_id", $edit_row['id']);      
    377    } else { 
    378      $message = _("You have disabled/removed all models that correspond to this brand. Please enable them in 'Brand Configurations/Setup' before trying to edit this phone"); 
    379      $tpl->assign("mode", NULL); 
    380    
    381 
    382    
    383    
    384  $tpl->assign("message", $message); 
    385  $tpl->assign("debug", $debug); 
    386    
    387  //draw the template 
    388  echo $tpl->draw( 'devices_manager' ); 
     314    $tpl = new RainTPL( LOCAL_PATH.'templates' ); 
     315    $tpl->assign("list", $list); 
     316    $tpl->assign("error", ""); 
     317    $tpl->assign("srvip", $_SERVER["SERVER_ADDR"]); 
     318    $tpl->assign("web_var", "?type=$type"); 
     319    if($ma = $endpoint->modelsAvailable()) { 
     320        $tpl->assign("models_ava", $ma); 
     321   
     322    $tpl->assign("web_path", WEB_PATH); 
     323    $tpl->assign("product_list", $product_list); 
     324    $tpl->assign("display_ext", $endpoint->displayExtension()); 
     325    $tpl->assign("brand_ava", $endpoint->brandAvailable()); 
     326    $tpl->assign("unmanaged", $final); 
     327    $tpl->assign("button", $button); 
     328    $tpl->assign("searched", $searched); 
     329    $tpl->assign("edit", $edit); 
     330    $tpl->assign("amp_conf_serial", base64_encode(serialize($amp_send))); 
     331    $tpl->assign("no_add", $no_add); 
     332    $tpl->assign("mode", $mode); 
     333 
     334    if(isset($final)) { 
     335        $tpl->assign("cached_devices", base64_encode(serialize($final))); 
     336   
     337 
     338    if (isset($mode) && ($mode == "EDIT")) { 
     339        if($ma = $endpoint->modelsAvailable($edit_row['model_id'],NULL,$edit_row['brand_id'])) { 
     340            $tpl->assign("mac", $edit_row['mac']); 
     341            $tpl->assign("name", $edit_row['name']); 
     342            $tpl->assign("models_ava", $ma); 
     343            $tpl->assign("display_ext", $endpoint->displayExtension($edit_row['ext'])); 
     344            $tpl->assign("display_templates", $endpoint->display_templates($edit_row['product_id'],$edit_row['custom_cfg_template'])); 
     345            $tpl->assign("edit_id", $edit_row['id']); 
     346        } else { 
     347            $message = _("You have disabled/removed all models that correspond to this brand. Please enable them in 'Brand Configurations/Setup' before trying to edit this phone"); 
     348            $tpl->assign("mode", NULL); 
     349       
     350   
     351 
     352 
     353    $tpl->assign("message", $message); 
     354    $tpl->assign("debug", $debug); 
     355 
     356    //draw the template 
     357    echo $tpl->draw( 'devices_manager' ); 
    389358} 
    390359?> 
  • contributed_modules/modules/endpointman/includes/functions.inc

    r10058 r10198  
    5656*/ 
    5757class endpointmanager { 
    58   //Load this class upon construction of the class 
     58    //Load this class upon construction of the class 
     59 
     60    public $db; 
     61    public $amp_conf; 
     62    public $global_cfg; 
     63    public $error; 
     64 
    5965    function __construct() { 
    60     global $amp_conf,$global_cfg, $endpoint_db; $type; 
    61     //attached to the end of every link so we can flow through FreePBX 
    62     //This is really hacky but I have to do it when people have different directories instead of admin 
    63     if((!array_key_exists('PHONE_MODULES_PATH',$amp_conf)) AND (dirname($_SERVER['SCRIPT_NAME']) != "/recordings")) { 
    64       //Define the location of phone modules, keeping it outside of the module directory so that when the user updates endpointmanager they don't lose all of their phones 
    65       define("PHONE_MODULES_PATH", dirname($_SERVER["SCRIPT_FILENAME"])."/modules/_ep_phone_modules/"); 
    66        
    67       //Get local path information 
    68       define("WEB_PATH", dirname($_SERVER['SCRIPT_NAME'])."/modules/endpointman/"); 
    69       define("LOCAL_PATH", dirname($_SERVER["SCRIPT_FILENAME"])."/modules/endpointman/");        
    70        
    71       //have to hack around freepbx.... 
    72       $amp_conf['PHONE_MODULES_PATH'] = PHONE_MODULES_PATH; 
    73       $amp_conf['LOCAL_ENDPOINT_PATH'] = LOCAL_PATH; 
    74     } elseif(dirname($_SERVER['SCRIPT_NAME']) == "/recordings") { 
    75       //Define the location of phone modules, keeping it outside of the module directory so that when the user updates endpointmanager they don't lose all of their phones 
    76       define("PHONE_MODULES_PATH", $amp_conf['AMPWEBROOT']."/admin/modules/_ep_phone_modules/"); 
    77        
    78       //Get local path information 
    79       define("WEB_PATH", "/admin/modules/endpointman/"); 
    80       define("LOCAL_PATH", $amp_conf['AMPWEBROOT']."/admin/modules/endpointman/");         
    81        
    82       //have to hack around freepbx.... 
    83       $amp_conf['PHONE_MODULES_PATH'] = PHONE_MODULES_PATH; 
    84       $amp_conf['LOCAL_ENDPOINT_PATH'] = LOCAL_PATH; 
    85     } else { 
    86       define("PHONE_MODULES_PATH", $amp_conf['PHONE_MODULES_PATH']); 
    87       define("LOCAL_PATH", $amp_conf['LOCAL_ENDPOINT_PATH']);    
    88     } 
    89          
    90     //Start Database Connections and keep them running this whoooooooole time 
    91     $this->dblogin();  
    92  
    93     //Set Global Variables 
    94     if($this->table_exists("endpointman_global_vars")) { 
    95       $result = mysql_query("SELECT var_name, value FROM endpointman_global_vars"); 
    96       while ($row=mysql_fetch_array($result)) { 
    97         $global_cfg[$row['var_name']] = $row['value']; 
    98       } 
    99      
    100       define("UPDATE_PATH", $global_cfg['update_server']); 
    101       define("VER", $global_cfg['version']); 
    102     } 
    103      
    104     //Define error reporting 
    105     if($global_cfg['debug']) { 
    106       error_reporting(E_ALL); 
    107       ini_set('display_errors', 1); 
    108     } else { 
    109       ini_set('display_errors', 0); 
    110     } 
    111     //include the local template class 
    112     include LOCAL_PATH."includes/rain.tpl.class.php"; 
    113   } 
    114    
    115   function add_freepbx_notification($id, $type, $display_text, $text, $link) { 
    116     $sql = "INSERT INTO notifications (module, id, level, display_text, link, candelete, timestamp, extended_text) VALUES ('endpointman', '".$id."', '500', '".$display_text."', '".$link."', '1', '".time()."', '".$text."')"; 
    117     mysql_query($sql); 
    118   } 
    119    
    120   //Send this function an ID from the mac devices list table and you'll get all the information we have on that particular phone 
    121   function get_phone_info($mac_id=NULL) { 
    122     //You could screw up a phone if the mac_id is blank 
    123     if (!isset($mac_id)) { 
    124       die('Can\'t get phone info!'); 
    125     } 
    126     $sql = "SELECT id,mac,model FROM  endpointman_mac_list WHERE model > 0 AND id =".$mac_id; 
    127     $result=mysql_query($sql); 
    128     if(mysql_num_rows($result)) { 
    129       //Returns Brand Name, Brand Directory, Model Name, Mac Address, Extension (FreePBX), Custom Configuration Template, Custom Configuration Data, Product Name, Product ID, Product Configuration Directory, Product Configuration Version, Product XML name, 
    130       $result=mysql_query("SELECT endpointman_mac_list.config_files_override, endpointman_mac_list.user_cfg_data, endpointman_model_list.id as model_id, endpointman_brand_list.id as brand_id, endpointman_brand_list.name, endpointman_brand_list.directory, endpointman_model_list.model, endpointman_mac_list.mac, endpointman_mac_list.ext, endpointman_mac_list.custom_cfg_template, endpointman_mac_list.custom_cfg_data, endpointman_product_list.long_name, endpointman_product_list.id as product_id, endpointman_product_list.cfg_dir, endpointman_product_list.cfg_ver, endpointman_product_list.xml_data, endpointman_product_list.cfg_data,  endpointman_product_list.installed, endpointman_model_list.enabled, users.name as description, sip.data as secret FROM endpointman_mac_list, endpointman_model_list, endpointman_brand_list, endpointman_product_list, sip, users WHERE endpointman_mac_list.id = ".$mac_id." AND endpointman_mac_list.model = endpointman_model_list.id AND endpointman_brand_list.id = endpointman_model_list.brand AND endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_mac_list.ext = sip.id AND sip.keyword = 'secret' AND endpointman_mac_list.ext = users.extension"); 
    131       $phone_info=mysql_fetch_array($result); 
    132  
    133       //If there is a template associated with this phone then pull that information and put it into the array 
    134       if ($phone_info['custom_cfg_template'] > 0) { 
    135         $result=mysql_query("SELECT name, custom_cfg_data, config_files_override FROM endpointman_template_list WHERE id = ".$phone_info['custom_cfg_template']); 
    136         $phone_info['template_data'] = mysql_fetch_array($result); 
    137    
    138       } 
    139     } else { 
    140       //Phone is unknown, we need to display this to the end user so that they can make corrections 
    141       $row=mysql_fetch_array($result); 
    142       $phone_info['id'] = $mac_id; 
    143       $phone_info['model_id'] = 0; 
    144       $phone_info['brand_id'] = 0; 
    145       $phone_info['ext'] = $row['ext']; 
    146       $phone_info['mac'] = $row['mac']; 
    147       $phone_info['name'] = 'Unknown'; 
    148     } 
    149     return $phone_info; 
    150   } 
    151    
    152   //Open configuration files and return the data from the file 
    153   function open_config_file($infile,$phone_info,$config_files_override){ 
    154     //Sometimes periods turn to underscores when periods are submitted through a form as a variable, we need to account for this 
    155     $key=str_replace('.', "_",$infile); 
    156     //from phone info open the required file for reading 
    157     $infile=PHONE_MODULES_PATH.$phone_info['directory']."/".$phone_info['cfg_dir']."/".$infile; 
    158     //if there is no configuration file over ridding the default then load up $contents with the file's information, where $key is the name of the default configuration file 
    159     if(!isset($config_files_override[$key])) { 
    160       //always use 'rb' says php.net 
    161       $handle = fopen($infile, "rb"); 
    162       $contents = fread($handle, filesize($infile)); 
    163       fclose($handle); 
    164     } else { 
    165       //$config_files_override has data in it so that means we have a configuration file over ride, lets pull the text out of the database 
    166       $sql = 'SELECT data FROM endpointman_custom_configs WHERE id = ' . $config_files_override[$key]; 
    167       $result = mysql_query($sql); 
    168       //$config_files_override said we had a file in the database but mySQL couldn't find anything so we default back to the local file 
    169       //This usually happens when the user deletes a custome configuration file but doesn't update their templates 
    170       if(mysql_num_rows($result) == 0) { 
    171         $handle = fopen($infile, "rb"); 
    172         $contents = fread($handle, filesize($infile)); 
    173         fclose($handle); 
    174       } else { 
    175         $data = mysql_fetch_assoc($result); 
    176         $contents = $data['data']; 
    177       } 
    178     } 
    179     return($contents); 
    180   } 
    181    
    182   //Write configuration files 
    183   function write_config_file($outfile,$phone_info,$contents,$custom_cfg_data){ 
    184     global $global_cfg; 
    185     //Get End User (through ARI) set variables 
    186     $user_cfg_data = unserialize($phone_info['user_cfg_data']); 
    187      
    188     //Determine if ARI is enabled, if not ignore any values present in $user_cfg_data, keep unknown variables, this is what 'TRUE' means 
    189     if(($global_cfg['enable_ari']) AND is_array($user_cfg_data)){ 
    190       $contents = $this->parse_config_values($user_cfg_data,$contents,TRUE,$phone_info); 
    191     } 
    192      
    193     //Parse normal config values and set all unknown variables in the file to nothing (Blanking them out), this is what 'FALSE' means 
    194     $contents = $this->parse_config_values($custom_cfg_data,$contents,FALSE,$phone_info); 
    195      
    196     $outfile=$global_cfg['config_location'].$outfile; 
    197     $wfh=fopen($outfile,'w'); 
    198     fwrite($wfh,$contents); 
    199     fclose($wfh); 
    200   } 
    201  
    202   /* 
    203   -Send this function the contents of a text file to $file_contents and then send an array to $custom_cfg_data 
    204   The key of the array is the name of the variable in the text file between "{$" and "}" 
    205   Then return the contents of the file with the values merged into it 
    206   -The $keep_unknown variable tells this function to ignore(TRUE) or blank out(FALSE) variables that it finds in $file_contents but can not find in $variables_array 
    207   */ 
    208   function parse_config_values($variables_array,$file_contents,$keep_unknown=FALSE, $phone_info=NULL) { 
    209     global $global_cfg; 
    210     //Find all matched variables in the text file between "{$" and "}" 
    211     preg_match_all('/[{\$](.*?)[}]/i',$file_contents,$match); 
    212     //Result without brackets (but with the $ variable identifier) 
    213     $no_brackets = array_values(array_unique($match[1])); 
    214     //Result with brackets 
    215     $brackets = array_values(array_unique($match[0])); 
    216     //Count how many variables were found in the file 
    217     $count = count($brackets); 
    218     $cfg_data = unserialize($phone_info['cfg_data']); 
    219      
    220     //loop though each variable found in the text file 
    221     for($i=0;$i<$count;$i++) { 
    222       $key = str_replace('$','',$no_brackets[$i]); 
    223       //If the variable we found in the text file exists in the variables array then replace the variable in the text file with the value under our key 
    224       if (isset($variables_array[$key])) { 
    225         $variables_array[$key]['value'] = htmlspecialchars($variables_array[$key]['value']); 
    226         //Variables the user defined in their custom configs 
    227         if(isset($phone_info)) { 
    228           $variables_array[$key]['value']=str_replace('{$srvip}', $global_cfg['srvip'], $variables_array[$key]['value']); 
    229           $variables_array[$key]['value']=str_replace('{$mac}', $phone_info['mac'], $variables_array[$key]['value']); 
    230           $variables_array[$key]['value']=str_replace('{$ext}', $phone_info['ext'], $variables_array[$key]['value']); 
    231           $variables_array[$key]['value']=str_replace('{$displayname}', $phone_info['description'], $variables_array[$key]['value']); 
    232         } 
    233         $file_contents=str_replace($brackets[$i], $variables_array[$key]['value'],$file_contents); 
    234       } else { 
    235         if(!$keep_unknown) { 
    236           //read default template values here, blank unknowns or arrays (which are blanks anyways) 
    237           $key1 = $this->arraysearchrecursive($no_brackets[$i], $cfg_data, 'variable'); 
    238           if(isset($phone_info)) { 
    239             $file_contents=str_replace('{$srvip}', $global_cfg['srvip'], $file_contents); 
    240             $file_contents=str_replace('{$mac}', $phone_info['mac'], $file_contents); 
    241             $file_contents=str_replace('{$ext}', $phone_info['ext'], $file_contents); 
    242             $file_contents=str_replace('{$displayname}', $phone_info['description'], $file_contents); 
    243           } 
    244           if((isset($cfg_data[$key1[0]]['default_value'])) AND (!is_array($cfg_data[$key1[0]]['default_value']))) { 
    245             $file_contents=str_replace($brackets[$i], $cfg_data[$key1[0]]['default_value'],$file_contents); 
    246           } else { 
    247             $file_contents=str_replace($brackets[$i], "",$file_contents); 
    248           } 
    249         } 
    250       } 
    251     } 
    252      
    253     if($global_cfg['debug']) { 
    254       global $debug; 
    255       $list = print_r($brackets, TRUE); 
    256       $debug .= "\nVariables found in file:\n".$list; 
    257     } 
    258      
    259     return $file_contents; 
    260   } 
    261  
    262   //Custom Means specific to that MAC 
    263   //id is either the mac ID (not address) or the template ID 
    264   function edit_template_display($id, $custom) { 
    265     global $global_cfg,$debug; 
    266     $alt_configs = NULL; 
    267     //Determine if we are dealing with a general template or a specific [for that phone only] template (custom =0 means general) 
    268     if($custom == 0) { 
    269       $sql = "SELECT endpointman_product_list.config_files, endpointman_template_list.*, endpointman_product_list.id as product_id, endpointman_product_list.long_name, endpointman_product_list.cfg_dir, endpointman_brand_list.directory FROM endpointman_template_list, endpointman_brand_list, endpointman_product_list WHERE endpointman_template_list.id = ".$id." AND endpointman_template_list.product_id = endpointman_product_list.id AND endpointman_product_list.brand = endpointman_brand_list.id"; 
    270     } else { 
    271       $sql = "SELECT endpointman_product_list.config_files, endpointman_mac_list.*, endpointman_product_list.id as product_id, endpointman_product_list.long_name, endpointman_product_list.cfg_dir, endpointman_brand_list.directory FROM endpointman_brand_list, endpointman_mac_list, endpointman_model_list, endpointman_product_list WHERE endpointman_mac_list.id=".$id." AND endpointman_mac_list.model = endpointman_model_list.id AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id"; 
    272     } 
    273     $result=mysql_query($sql); 
    274     $row=mysql_fetch_array($result); 
    275          
    276     //Make sure the model data from the local confg files are stored in the database and vice-versa. Serious errors will occur if the database is not in sync with the local file 
    277     if(!$this->sync_product($row['product_id'])) { 
    278       die("unable to sync local template files". $custom); 
    279     } 
    280      
    281     //Get default template config data from the database for this product 
    282     $sql = "SELECT cfg_data FROM endpointman_product_list WHERE id=". $row['product_id']; 
    283     $result=mysql_query($sql); 
    284     $product_row=mysql_fetch_array($result);     
    285      
    286     //inset it into our usable row 
    287     $row['cfg_data'] = $product_row['cfg_data']; 
    288    
    289     $brand_name = $row['directory']; 
    290     $model_name = $row['cfg_dir']; 
    291     $tpl = new RainTPL( LOCAL_PATH.'templates' ); 
    292     $tpl->assign("template_editor_display", 1); 
    293      
    294     echo $tpl->draw( 'global_header' ); 
    295     //Let the template system know if we are working with a general template or a specific [for that phone only] template 
    296     $tpl->assign("custom", $custom); 
    297     if($custom) 
    298     { 
    299       $tpl->assign("ext", $row['ext']); 
    300     } else { 
    301       $tpl->assign("template_name", $row['name']); 
    302     } 
    303     $tpl->assign("model", $row['long_name']); 
    304  
    305     //Start the display of the html file in the product folder 
    306     if($row['config_files_override'] == "") { 
    307       $config_files_saved = ""; 
    308     } else { 
    309       $config_files_saved = unserialize($row['config_files_override']); 
    310     } 
    311     $config_files = explode(",",$row['config_files']); 
    312     $i = 0; 
    313     $alt = 0; 
    314     while($i < count($config_files)) { 
    315       $sql = "SELECT * FROM  endpointman_custom_configs WHERE product_id = ".$row['product_id']." AND original_name = '".$config_files[$i]."'"; 
    316       $result = mysql_query($sql); 
    317       if(mysql_num_rows($result) > 0) { 
    318         $alt_configs .= $config_files[$i]; 
    319         $alt_configs .= '<select name="'.$config_files[$i].'">'; 
    320         $alt_configs .= '<option value="0">'.$config_files[$i].' (Original)</option>'; 
    321         $config_files[$i] = str_replace(".","_",$config_files[$i]); 
    322         while ($ccf=mysql_fetch_array($result)) { 
    323           $alt_configs .= '<option value="'.$ccf['id'].'" '; 
    324           $cf_key = $config_files[$i]; 
    325           if((is_array($config_files_saved)) AND ($config_files_saved[$cf_key] == $ccf['id'])) { 
    326             $alt_configs .= 'selected'; 
    327           } 
    328           $alt_configs .= '>'.$ccf['name'].'</option>'; 
    329         } 
    330         $alt_configs .= "</select>"; 
    331         $alt = 1; 
    332       } 
    333       $i++;      
    334     } 
    335     $tpl->assign("alt_configs", $alt_configs); 
    336     $tpl->assign("alt", $alt); 
    337      
    338     if($row['cfg_data'] != "") { 
    339       $out = $this->generate_gui_html($row['cfg_data'],$row['custom_cfg_data'],TRUE); 
    340     } else { 
    341       echo "No Template Data has been defined for this Product<br />"; 
    342     } 
    343      
    344     $tpl->assign("template_editor", $out); 
    345     $tpl->assign("hidden_id", $row['id']); 
    346     $tpl->assign("hidden_custom", $custom); 
    347     echo $tpl->draw( 'template_editor' ); 
    348  
    349     $tpl->assign("debug", $debug); 
    350    
    351   } 
    352  
    353   function generate_gui_html($cfg_data,$custom_cfg_data=NULL, $admin=FALSE, $user_cfg_data=NULL) { 
    354     global $global_cfg; 
    355     //take the data out of the database and turn it back into an array for use 
    356     $cfg_data = unserialize($cfg_data); 
    357     $count = count($cfg_data); 
    358  
    359     //Check to see if there is a custom template for this phone already listed in the endpointman_mac_list database 
    360     if (isset($custom_cfg_data)) { 
    361       $custom_cfg_data = unserialize($custom_cfg_data); 
    362     } else { 
    363       //No custom template so let's pull the default values for this model into the custom_cfg_data array and populate it from there so that we won't have to make two completely different functions below 
    364       for($i=0;$i<$count;$i++) { 
    365         $key_default = str_replace('$','',$cfg_data[$i]['variable']); 
    366         if(!is_array($cfg_data[$i]['default_value'])) { 
    367           $custom_cfg_data[$key_default]['value'] = $cfg_data[$i]['default_value']; 
    368  
    369         } 
    370       } 
    371     } 
    372     if(isset($user_cfg_data)) { 
    373       $user_cfg_data = unserialize($user_cfg_data); 
    374     } 
    375      
    376     $template_variables_array = array(); 
    377      
    378     $group_count = 0; 
    379     //Fill the html form data with values from either the database or the default values to display to the end user 
    380     for($i=0;$i<$count;$i++) { 
    381       if(array_key_exists('variable',$cfg_data[$i])) { 
    382         $key = str_replace('$','',$cfg_data[$i]['variable']); 
    383       } else { 
    384         $key = ""; 
    385       }      
    386       if(($admin) OR (isset($custom_cfg_data[$key]['ari']))) { 
    387         //Checks to see if values are defined in the database, if not then we assume this is a new option and we need a default value here! 
    388         if(!isset($custom_cfg_data[$key]['value'])) { 
    389           //xml2array will take values that have no data and turn them into arrays, we want to avoid the word 'array' as a default value, so we blank it out here if we are an array 
    390           if((array_key_exists('default_value',$cfg_data[$i])) AND (is_array($cfg_data[$i]['default_value']))) { 
    391             $custom_cfg_data[$key]['value'] = ""; 
    392           } elseif((array_key_exists('default_value',$cfg_data[$i])) AND (!is_array($cfg_data[$i]['default_value']))) { 
    393             $custom_cfg_data[$key]['value'] = $cfg_data[$i]['default_value']; 
    394           } 
    395         } 
    396         if ($cfg_data[$i]['type'] == "group") { 
    397           $group_count++; 
    398           $template_variables_array[$group_count]['title'] = $cfg_data[$i]['description']; 
    399           $variables_count = 0; 
    400         } elseif ($cfg_data[$i]['type'] == "input") { 
    401           if((!$admin) && (isset($user_cfg_data[$key]['value']))) { 
    402             $custom_cfg_data[$key]['value'] = $user_cfg_data[$key]['value']; 
    403           } 
    404           $template_variables_array[$group_count]['data'][$variables_count]['type'] = "input"; 
    405           $template_variables_array[$group_count]['data'][$variables_count]['key'] = $key; 
    406           $template_variables_array[$group_count]['data'][$variables_count]['value'] = $custom_cfg_data[$key]['value']; 
    407           $template_variables_array[$group_count]['data'][$variables_count]['description'] = $cfg_data[$i]['description']; 
    408         } elseif ($cfg_data[$i]['type'] == "radio") { 
    409           if((!$admin) && (isset($user_cfg_data[$key]['value']))) { 
    410             $custom_cfg_data[$key]['value'] = $user_cfg_data[$key]['value']; 
    411           } 
    412           $num = $custom_cfg_data[$key]['value'];  
    413           $template_variables_array[$group_count]['data'][$variables_count]['type'] = "radio"; 
    414           $template_variables_array[$group_count]['data'][$variables_count]['key'] = $key; 
    415           $template_variables_array[$group_count]['data'][$variables_count]['description'] = $cfg_data[$i]['description']; 
    416           $z = 0; 
    417           while($z < count($cfg_data[$i]['data'])) { 
    418             $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['key'] = $key; 
    419             $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['value'] = $cfg_data[$i]['data'][$z]['value']; 
    420             $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['description'] = $cfg_data[$i]['data'][$z]['text']; 
    421             if ($cfg_data[$i]['data'][$z]['value'] == $num) { 
    422               $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['checked'] = 'checked'; 
    423             } 
    424             $z++; 
    425           } 
    426         } elseif ($cfg_data[$i]['type'] == "list") { 
    427           if((!$admin) && (isset($user_cfg_data[$key]['value']))) { 
    428             $custom_cfg_data[$key]['value'] = $user_cfg_data[$key]['value']; 
    429           } 
    430           $num = $custom_cfg_data[$key]['value']; 
    431           $template_variables_array[$group_count]['data'][$variables_count]['type'] = "list"; 
    432           $template_variables_array[$group_count]['data'][$variables_count]['key'] = $key; 
    433           $template_variables_array[$group_count]['data'][$variables_count]['description'] = $cfg_data[$i]['description']; 
    434           $z = 0; 
    435           while($z < count($cfg_data[$i]['data'])) { 
    436             $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['value'] = $cfg_data[$i]['data'][$z]['value']; 
    437             $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['description'] = $cfg_data[$i]['data'][$z]['text']; 
    438             if ($cfg_data[$i]['data'][$z]['value'] == $num) { 
    439               $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['selected'] = 'selected'; 
    440             } 
    441             $z++; 
    442           } 
    443         } elseif ($cfg_data[$i]['type'] == "break") { 
    444           $template_variables_array[$group_count]['data'][$variables_count]['type'] = "break"; 
    445         } 
    446         if(($global_cfg['enable_ari']) AND ($admin) AND ($cfg_data[$i]['type'] != "break") AND ($cfg_data[$i]['type'] != "group")) { 
    447           $template_variables_array[$group_count]['data'][$variables_count]['aried'] = 1; 
    448           $template_variables_array[$group_count]['data'][$variables_count]['ari']['key'] = $key; 
    449           if(isset($custom_cfg_data[$key]['ari'])) { 
    450             $template_variables_array[$group_count]['data'][$variables_count]['ari']['checked'] = "checked"; 
    451           } 
    452         } 
    453         $variables_count++; 
    454       } 
    455     } 
    456     return($template_variables_array); 
    457   } 
    458  
    459   function save_template($id, $custom, $variables) { 
    460     //Custom Means specific to that MAC 
    461     if($custom == "0") { 
    462       $sql = "SELECT endpointman_product_list.config_files, endpointman_template_list.*, endpointman_product_list.long_name, endpointman_product_list.cfg_data, endpointman_product_list.cfg_dir, endpointman_brand_list.directory FROM endpointman_template_list, endpointman_brand_list, endpointman_product_list WHERE endpointman_template_list.id = ".$id." AND endpointman_template_list.product_id = endpointman_product_list.id AND endpointman_product_list.brand = endpointman_brand_list.id"; 
    463     } else { 
    464       $sql = "SELECT endpointman_product_list.config_files, endpointman_mac_list.*, endpointman_product_list.id as product_id, endpointman_product_list.long_name, endpointman_product_list.cfg_data, endpointman_product_list.cfg_dir, endpointman_brand_list.directory FROM endpointman_brand_list, endpointman_mac_list, endpointman_model_list, endpointman_product_list WHERE endpointman_mac_list.id=".$id." AND endpointman_mac_list.model = endpointman_model_list.id AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id"; 
    465     } 
    466     //Load template data 
    467     $result=mysql_query($sql); 
    468     $row=mysql_fetch_array($result); 
    469     $cfg_data = unserialize($row['cfg_data']); 
    470     $count = count($cfg_data); 
    471     for($i=0;$i<$count;$i++) { 
    472       if(array_key_exists('variable',$cfg_data[$i])) { 
    473         $temping = str_replace('$','',$cfg_data[$i]['variable']); 
    474         $temping_ari = "ari_" . $temping; 
    475         if(array_key_exists($temping, $_REQUEST)) { 
    476           $custom_cfg_data[$temping]['value'] = $_REQUEST[$temping]; 
    477           if(array_key_exists($temping_ari, $_REQUEST)) { 
    478             if($_REQUEST[$temping_ari] == "on") { 
    479               $custom_cfg_data[$temping]['ari'] = 1; 
    480             } 
    481           } 
    482         } 
    483       } 
    484     } 
    485      
    486     $config_files = explode(",",$row['config_files']); 
    487     $i = 0; 
    488     while($i < count($config_files)) { 
    489       $config_files[$i] = str_replace(".","_",$config_files[$i]); 
    490       if(isset($_REQUEST[$config_files[$i]])) { 
    491         if($_REQUEST[$config_files[$i]] > 0) { 
    492           $config_files_selected[$config_files[$i]] = $_REQUEST[$config_files[$i]]; 
    493         } 
    494       } 
    495       $i++; 
    496     } 
    497     if(!isset($config_files_selected)) { 
    498       $config_files_selected = ""; 
    499     } else { 
    500       $config_files_selected = serialize($config_files_selected); 
    501     } 
    502     $save = serialize($custom_cfg_data); 
    503    
    504     if($custom == "0") { 
    505       $sql = 'UPDATE endpointman_template_list SET config_files_override = \''.addslashes($config_files_selected).'\', custom_cfg_data = \''.addslashes($save).'\' WHERE id ='.$id; 
    506       $location = "template_manager"; 
    507     } else { 
    508       $sql = 'UPDATE endpointman_mac_list SET config_files_override = \''.addslashes($config_files_selected).'\', custom_cfg_template = 0, custom_cfg_data = \''.addslashes($save).'\' WHERE id ='.$id; 
    509       $location = "devices_manager"; 
    510     } 
    511     $result=mysql_query($sql); 
    512      
    513      
    514     require(PHONE_MODULES_PATH .$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    515  
    516     $phone_config = new $row['cfg_dir'](); 
    517     if($custom != 0) { 
    518       $phone_config->generate_config($id); 
    519     } else { 
    520       $sql = 'SELECT id FROM endpointman_mac_list WHERE custom_cfg_template = '.$id; 
    521       $result=mysql_query($sql); 
    522       while($row=mysql_fetch_array($result)) { 
    523         $phone_config->generate_config($row['id']); 
    524       } 
    525     } 
    526     return($location); 
    527      
    528   } 
    529    
    530   function firmware_update_check($id=NULL) { 
    531     $sql = "SELECT * FROM  endpointman_product_list WHERE  id =". $id; 
    532     $result = mysql_query($sql); 
    533     $row = mysql_fetch_array($result); 
    534    
    535     $sql = "SELECT directory FROM  endpointman_brand_list WHERE id =". $row['brand']; 
    536     $result = mysql_query($sql); 
    537     if(mysql_num_rows($result)) { 
    538       $brand_row = mysql_fetch_array($result); 
    539      
    540       //config drive unknown! 
    541       if ($row['cfg_dir'] == "") {   
    542         return FALSE; 
    543       } else { 
    544         $temp = $this->xml2array(PHONE_MODULES_PATH.$brand_row['directory']."/".$row['cfg_dir']."/template_data.xml"); 
    545         if((array_key_exists('data',$temp)) AND (!is_array($temp['data']['firmware_ver']))) { 
    546           if($row['firmware_vers'] < $temp['data']['firmware_ver']) {  
    547             return $temp; 
    548           } else { 
    549             return FALSE; 
    550           } 
    551         } else { 
    552           return FALSE; 
    553         } 
    554       } 
    555     } else { 
    556       return FALSE; 
    557     } 
    558   } 
    559    
    560   function firmware_local_check($id=NULL) { 
    561     $sql = "SELECT * FROM  endpointman_product_list WHERE installed = 1 AND id =". $id; 
    562     $result = mysql_query($sql); 
    563     if(mysql_num_rows($result)) { 
    564       $row = mysql_fetch_array($result); 
    565    
    566       $sql = "SELECT directory FROM  endpointman_brand_list WHERE installed = 1 AND id =". $row['brand']; 
    567       $result = mysql_query($sql); 
    568       $brand_row = mysql_fetch_array($result); 
    569      
    570       //config drive unknown! 
    571       if ($row['cfg_dir'] == "") {   
    572         return("nothing"); 
    573       } else { 
    574         $temp = $this->xml2array(PHONE_MODULES_PATH.$brand_row['directory']."/".$row['cfg_dir']."/template_data.xml"); 
    575         if(!is_array($temp['data']['firmware_ver'])) {     
    576           if($row['firmware_vers'] == "") {      
    577             return("install"); 
    578           } else { 
    579             return("remove"); 
    580           } 
    581         } else { 
    582           return("nothing"); 
    583         } 
    584       } 
    585     } else { 
    586       return("nothing"); 
    587     } 
    588   } 
    589    
    590   function remove_firmware($id=NULL) { 
    591     global $global_cfg; 
    592     $sql = "SELECT * FROM  endpointman_product_list WHERE  id =". $id; 
    593     $result = mysql_query($sql); 
    594     $row = mysql_fetch_array($result); 
    595    
    596     $file_list = explode(",",$row['firmware_files']); 
    597     $i = 0; 
    598     while($i < count($file_list)){ 
    599       unlink($global_cfg['config_location'].$file_list[$i]); 
    600       $i++; 
    601     } 
    602     $sql = 'UPDATE endpointman_product_list SET firmware_files = "", firmware_vers = "" WHERE id = '.$id; 
    603     mysql_query($sql); 
    604   } 
    605    
    606   function install_firmware($product_id) { 
    607     global $global_cfg, $debug; 
    608     $sql = 'SELECT endpointman_product_list.*, endpointman_brand_list.directory FROM endpointman_product_list, endpointman_brand_list WHERE endpointman_product_list.brand = endpointman_brand_list.id AND endpointman_product_list.id = '.$product_id; 
    609     $result = mysql_query($sql); 
    610     $row = mysql_fetch_array($result); 
    611     $temp = $this->xml2array(PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/template_data.xml"); 
    612     if($temp['data']['firmware_ver'] > $row['firmware_vers']) { 
    613       echo "<script>$('div.demo-container').text('Installing firmware');</script>"; 
    614       $this->download_file_with_progress_bar(UPDATE_PATH . $temp['data']['firmware_pkg'], PHONE_MODULES_PATH."temp/".$temp['data']['firmware_pkg']); 
    615       global $global_cfg, $endpoint; 
    616       if(!file_exists(PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware")) {mkdir(PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware");} 
    617       exec("unzip ".PHONE_MODULES_PATH."temp/" . $temp['data']['firmware_pkg'] ." -d ".PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware/", $debug_a = array()); 
    618       if ($handle = opendir(PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware/")) { 
    619           while (false !== ($file = readdir($handle))) { 
    620               if ($file != "." && $file != "..") { 
    621                   $list .= $file.","; 
    622               } 
    623           } 
    624           closedir($handle); 
    625       } 
    626       $list = substr_replace($list ,"",-1); 
    627       exec("cp -r " . PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware/*.* ". $global_cfg['config_location'], $debug_a2 = array()); 
    628       $sql = "UPDATE endpointman_product_list SET firmware_vers = '".$temp['data']['firmware_ver']."', firmware_files = '".$list."' WHERE id = ". $row['id']; 
    629       mysql_query($sql); 
    630       echo "<script>$('div.demo-container').text('Done!');</script>"; 
    631     } else { 
    632       echo "<script>$('div.demo-container').text('Your Firmware is already up to date');</script>"; 
    633        
    634     } 
    635     if($global_cfg['debug']) { 
    636       $debug = array_merge($debug_a, $debug_a2); 
    637       $outfile=LOCAL_PATH."debug_installer.log"; 
    638       $wfh=fopen($outfile,'a'); 
    639       fwrite($wfh,print_r($debug, TRUE)); 
    640       fclose($wfh); 
    641     } 
    642   } 
    643    
    644    
    645   //Check for new packges for brands. These packages will include phone models and such which the user can remove if they want 
    646   function brand_update_check() { 
    647     $location = UPDATE_PATH . "brands.xml"; 
    648     $temp = $this->xml2array($location); 
    649      
    650     if(!isset($temp['data']['brands'][0])) { 
    651       $temp2['data']['brands'][0] = $temp['data']['brands']; 
    652       $temp = $temp2; 
    653     } 
    654      
    655     $out = $temp['data']['brands']; 
    656     $count = count($out); 
    657  
    658     $sql = "SELECT * FROM  endpointman_brand_list WHERE id > 0"; 
    659     $result = mysql_query($sql);   
    660     while($row = mysql_fetch_assoc($result)){ 
    661       $key = $this->arraysearchrecursive($row['directory'], $out, 'directory'); 
    662       if($key === FALSE) { 
    663         $this->remove_brand($row['id']); 
    664       } 
    665        
    666       $key = $key[0]; 
    667        
    668       if($row['cfg_ver'] < $out[$key]['version']) { 
    669         $out[$key]['update'] = 1; 
    670         $out[$key]['update_vers'] = $out[$key]['version']; 
    671       } else { 
    672         $out[$key]['update'] = NULL; 
    673       } 
    674        
    675     } 
    676    
    677     for($i=0;$i<$count;$i++){ 
    678       if(!isset($out[$i]['update'])) { 
    679         //insert row 
    680         $sql = "INSERT INTO endpointman_brand_list (id, name, directory, cfg_ver) VALUES ('".$out[$i]['brand_id']."', '".$out[$i]['brand_name']."', '".$out[$i]['directory']."', '".$out[$i]['version']."')"; 
    681         mysql_query($sql); 
    682       } 
    683     } 
    684  
    685     return $out; 
    686   } 
    687    
    688   //This will update AND install brands! 
    689   //id is brand id 
    690   function update_brand($id = NULL) { 
    691     //Get the brands run down from the update server 
    692     $temp = $this->xml2array(UPDATE_PATH . "brands.xml"); 
    693  
    694     $key = $this->arraysearchrecursive($id, $temp['data']['brands'],'brand_id'); 
    695      
    696      
    697     $key = $key[0]; 
    698      
    699     if(!isset($temp['data']['brands'][$key]['product_list'][0])) { 
    700       $temp2 = $temp['data']['brands'][$key]['product_list']; 
    701       unset($temp['data']['brands'][$key]['product_list']); 
    702       $temp['data']['brands'][$key]['product_list'][0] = $temp2; 
    703     } 
    704      
    705     //Count how many products are avalible for this brand 
    706     $count2 = count($temp['data']['brands'][$key]['product_list']); 
    707      
    708     //Get the name of the brand (Grandstream, Polycom, etc) 
    709     $brand_name = $temp['data']['brands'][$key]['brand_name']; 
    710      
    711     //Though we already have the brand ID lets just pull it again to be sure 
    712     $brand_id = $temp['data']['brands'][$key]['brand_id']; 
    713    
    714     //Brand directory listing. We'll use this to put the generic files into place 
    715     $brand_directory = $temp['data']['brands'][$key]['directory']; 
    716      
    717     //Get the brand's config version 
    718     $cfg_ver = $temp['data']['brands'][$key]['version']; 
    719      
    720     //Update config version to the newest number 
    721     $sql = "UPDATE endpointman_brand_list SET name = '".$brand_name."', cfg_ver = '".$cfg_ver."', installed = 1 WHERE id = ". $brand_id; 
    722     mysql_query($sql); 
    723  
    724     //Run through the list of products supported under this brand by us. Then spit these values back out into an array where the array's key is equal to the phone's ID number 
    725     $sql = "SELECT * FROM  endpointman_product_list WHERE  brand = ". $brand_id; 
    726     $result = mysql_query($sql); 
    727     while($row = mysql_fetch_assoc($result)) { 
    728       $rid = $row['id']; 
    729       $row_out[$rid] = $row; 
    730     } 
    731      
    732     for($i=0;$i<$count2;$i++) { 
    733       $id = $temp['data']['brands'][$key]['product_list'][$i]['product_id']; 
    734       $long_name = $temp['data']['brands'][$key]['product_list'][$i]['product_name']; 
    735       $cfg_dir = $temp['data']['brands'][$key]['product_list'][$i]['directory']; 
    736       $xml_data = $temp['data']['brands'][$key]['product_list'][$i]['xml_name']; 
    737        
    738       if(!isset($row_out[$id])) { 
    739         $sql = "INSERT INTO endpointman_product_list (id, brand, long_name, cfg_dir, xml_data) VALUES ('".$id."','".$brand_id."','".$long_name."','".$cfg_dir."','".$xml_data."')"; 
    740         mysql_query($sql); 
    741       } else { 
    742         $sql = "UPDATE endpointman_product_list SET long_name = '".$long_name."', cfg_dir= '".$cfg_dir."', xml_data='".$xml_data."' WHERE id =". $id; 
    743         mysql_query($sql); 
    744       } 
    745     } 
    746     //Now run through the OUI list and add it in! 
    747     $result = mysql_query("SELECT * FROM  endpointman_oui_list"); 
    748     $i = 0; 
    749     $oui_out = array(); 
    750     while($row = mysql_fetch_assoc($result)) { 
    751       $oui_out[$i] = $row; 
    752       $i++; 
    753     } 
    754  
    755     if(!is_array($temp['data']['brands'][$key]['oui_list']['oui'])) { 
    756       $temp2 = $temp['data']['brands'][$key]['oui_list']['oui']; 
    757       unset($temp['data']['brands'][$key]['oui_list']); 
    758       $temp['data']['brands'][$key]['oui_list']['oui'][0] = $temp2; 
    759     } 
    760      
    761     $count = count($temp['data']['brands'][$key]['oui_list']['oui']); 
    762     for($i=0;$i < $count;$i++) { 
    763       if(!$this->arraysearchrecursive($temp['data']['brands'][$key]['oui_list']['oui'][$i],$oui_out)) { 
    764         $sql = "INSERT INTO endpointman_oui_list (oui, brand) VALUES ('".$temp['data']['brands'][$key]['oui_list']['oui'][$i]."', '".$brand_id."')"; 
    765         mysql_query($sql); 
    766       } 
    767     } 
    768     //Download the corresponding package and install it in the directory listed above 
    769     echo "<script>$('div.demo-container').text('Installing Module....');</script>"; 
    770     if(!file_exists(PHONE_MODULES_PATH."temp")) {mkdir(PHONE_MODULES_PATH."temp");} 
    771     if(!file_exists(PHONE_MODULES_PATH.$brand_directory)) {mkdir(PHONE_MODULES_PATH.$brand_directory);} 
    772     echo "<script>$('div.demo-container').text('Downloading Module...');</script>"; 
    773     $this->download_file_with_progress_bar(UPDATE_PATH . $temp['data']['brands'][$key]['package'], PHONE_MODULES_PATH."temp/".$temp['data']['brands'][$key]['package']); 
    774     echo "<script>$('div.demo-container').text('Installing Module...');</script>"; 
    775     exec("unzip ".PHONE_MODULES_PATH.'temp/'. $temp['data']['brands'][$key]['package'] ." -d ".PHONE_MODULES_PATH."temp/"); 
    776     exec("rm -R ".PHONE_MODULES_PATH."temp/__MACOSX"); 
    777     unlink(PHONE_MODULES_PATH.'temp/'. $temp['data']['brands'][$key]['package']); 
    778     exec("cp -R ".PHONE_MODULES_PATH."temp/" .$brand_directory . " " . PHONE_MODULES_PATH); 
    779     chmod(PHONE_MODULES_PATH.$brand_directory, 0777); 
    780     $this->deltree(PHONE_MODULES_PATH."temp/" .$brand_directory); 
    781     exec("chmod 777 -R ". PHONE_MODULES_PATH.$brand_directory); 
    782  
    783     //TODO: once iframe access is fixed to not go into the module, this should be $_SERVER['PHP_SELF']."?type=tool...", and type=tool should be passed in (in case it is not tool) 
    784     // 
    785     echo "<script>$('div.demo-container').html('<a href=\"#\" onclick=\"parent.location.href = \'/admin/config.php?type=tool&display=epm_config\';\">"._("Install Done, Click to Return")."</a>');</script>"; 
    786   } 
    787    
    788   function remove_brand($id=NULL) { 
    789     mysql_query("DELETE FROM endpointman_brand_list WHERE id = ". $id); 
    790   } 
    791    
    792   function product_update_check($id=NULL) { 
    793     $sql = "SELECT * FROM endpointman_product_list WHERE id =".$id; 
    794     $result = mysql_query($sql); 
    795     $row = mysql_fetch_array($result); 
    796     //config drive unknown! 
    797     if ($row['cfg_dir'] == "") {   
    798       return FALSE; 
    799     } else { 
    800       $temp = $this->xml2array(UPDATE_PATH . $row['xml_data']);    
    801       if($row['cfg_ver'] < $temp['data']['version']) {       
    802         return $temp; 
    803       } else { 
    804         return FALSE; 
    805       } 
    806     } 
    807   } 
    808  
    809   function sync_product($product=NULL) { 
    810     global $global_cfg, $debug; 
    811     $sql = "SELECT * FROM  endpointman_product_list WHERE  id =". $product; 
    812     $result = mysql_query($sql); 
    813     $row = mysql_fetch_array($result); 
    814    
    815     $sql = "SELECT directory FROM  endpointman_brand_list WHERE id =". $row['brand']; 
    816     $result = mysql_query($sql); 
    817     $brand_row = mysql_fetch_array($result);   
    818     //config drive unknown! 
    819     if ($row['cfg_dir'] == "") { 
    820       if($global_cfg['debug']) { 
    821         $debug .= "\nFunction sync_product XML Data:\n".$xml; 
    822       } 
    823       return FALSE; 
    824     } else { 
    825       //check for updates in template folder 
    826       $temp = $this->xml2array(PHONE_MODULES_PATH.$brand_row['directory']."/".$row['cfg_dir']."/template_data.xml"); 
    827       if (($temp['data']['version'] >= $row['cfg_ver']) OR ($row['cfg_data'] == "")) { 
    828         if(isset($temp['data']['item'])) { 
    829           $cfg_data = serialize($temp['data']['item']); 
    830         } else { 
    831           $cfg_data = NULL; 
    832         } 
    833         $sql = "UPDATE endpointman_product_list SET config_files=\"".addslashes($temp['data']['configuration_files'])."\",cfg_data = \"".addslashes($cfg_data)."\", cfg_ver ='".$temp['data']['version']."' WHERE id = ". $product; 
    834         mysql_query($sql); 
    835         if($global_cfg['debug']) { 
    836           $xml = print_r($temp, TRUE); 
    837           $debug .= "\nFunction sync_product XML Data:\n".$xml; 
    838         } 
    839         return TRUE; 
    840       } else { 
    841         if($global_cfg['debug']) { 
    842           $debug .= "\nFunction sync_product XML Data:Didn't run xml2array, local version less than sql version\n"; 
    843         } 
    844         return TRUE; 
    845       } 
    846     } 
    847   } 
    848    
    849   //id is model id 
    850   function install_product($id = NULL) { 
    851     //Update SQL below to include the brand and then use the brand_xml with the data returned to get new models are remove old ones....etc. 
    852     $sql = "SELECT endpointman_product_list.*, endpointman_brand_list.directory FROM  endpointman_product_list, endpointman_brand_list WHERE endpointman_brand_list.id = endpointman_product_list.brand AND  endpointman_product_list.id =". $id; 
    853     $result = mysql_query($sql); 
    854     $row = mysql_fetch_assoc($result); 
    855     $cfg_dir = $row['cfg_dir']; 
    856     $xml_data = $row['xml_data']; 
    857      
    858     echo "<script>$('div.demo-container').text('Getting XML data...');</script>"; 
    859      
    860     $product_xml = $this->xml2array(UPDATE_PATH . $xml_data); 
    861     $brand_xml = $this->xml2array(UPDATE_PATH . "brands.xml"); 
    862      
    863     $brand_id = $this->arraysearchrecursive($row['directory'], $brand_xml['data']['brands'], 'directory'); 
    864     $brand_id = $brand_id[0]; 
    865      
    866  
    867     if(!isset($brand_xml['data']['brands'][$brand_id]['product_list'][0])) { 
    868       $temp2 = $brand_xml['data']['brands'][$brand_id]['product_list']; 
    869       unset($brand_xml['data']['brands'][$brand_id]['product_list']); 
    870       $brand_xml['data']['brands'][$brand_id]['product_list'][0] = $temp2; 
    871     } 
    872      
    873     $product_id = $this->arraysearchrecursive($row['cfg_dir'], $brand_xml['data']['brands'][$brand_id]['product_list'],'directory'); 
    874     $product_id = $product_id[0]; 
    875     $cfg_ver = $product_xml['data']['version']; 
    876     $product_dir = $brand_xml['data']['brands'][$brand_id]['product_list'][$product_id]['directory']; 
    877  
    878     if ($cfg_ver > $row['cfg_ver']) { 
    879       $cfg_pkg = $temp['data']['package']; 
    880       if(!file_exists(PHONE_MODULES_PATH."temp")) {mkdir(PHONE_MODULES_PATH."temp");} 
    881       $this->download_file_with_progress_bar(UPDATE_PATH . $xml_data, PHONE_MODULES_PATH.'temp/'. $xml_data); 
    882       echo "<script>$('div.demo-container').text('Downloading Module...');</script>"; 
    883       $this->download_file_with_progress_bar(UPDATE_PATH . $product_xml['data']['package'], PHONE_MODULES_PATH.'temp/'. $product_xml['data']['package']);  
    884       echo "<script>$('div.demo-container').text('Installing Module...');</script>"; 
    885       exec("rm -R ".PHONE_MODULES_PATH.$row['directory']."/" .$product_dir, $debug_a = array()); 
    886       exec("unzip ".PHONE_MODULES_PATH.'temp/'. $product_xml['data']['package']." -d ".PHONE_MODULES_PATH."temp/", $debug_a2 = array()); 
    887       exec("rm -R ".PHONE_MODULES_PATH."temp/__MACOSX" , $debug_a3 = array()); 
    888       unlink(PHONE_MODULES_PATH.'temp/'. $product_xml['data']['package']);       
    889       exec("cp -R ".PHONE_MODULES_PATH."temp/" .$product_dir . " " . PHONE_MODULES_PATH.$row['directory'], $debug_a4 = array()); 
    890       chmod(PHONE_MODULES_PATH.$row['directory']."/".$product_dir, 0777); 
    891       rename(PHONE_MODULES_PATH."temp/" .$xml_data, PHONE_MODULES_PATH.$row['directory']."/".$product_dir."/template_data.xml"); 
    892       exec("rm -R ".PHONE_MODULES_PATH."temp/" .$product_dir, $debug_a5 = array()); 
    893       unlink(PHONE_MODULES_PATH."temp/" .$xml_data); 
    894       exec("chmod 777 -R ". PHONE_MODULES_PATH.$row['directory']."/". $cfg_dir, $debug_a6 = array()); 
    895     } 
    896      
    897     echo "<script>$('div.demo-container').text('Updating System...');</script>"; 
    898  
    899        
    900     $sql = 'SELECT * FROM endpointman_model_list WHERE product_id = '. $id; 
    901     $result = mysql_query($sql); 
    902     $i = 0; 
    903     while($row = mysql_fetch_assoc($result)) { 
    904       $row_o[$i] = $row; 
    905       $i++; 
    906     } 
    907      
    908     $count = count($brand_xml['data']['brands'][$brand_id]['product_list'][$product_id]['model_list']); 
    909  
    910     echo "<script>$('div.demo-container').text('Updating Models...');</script>"; 
    911  
    912     //Look Through remote values and compare them to local values, adding phones if needed and updating other phones if these exist already in our database 
    913     for($i=0;$i<$count;$i++) { 
    914       $model_name = $brand_xml['data']['brands'][$brand_id]['product_list'][$product_id]['model_list'][$i]['model']; 
    915       $model_id = $brand_xml['data']['brands'][$brand_id]['product_list'][$product_id]['model_list'][$i]['id']; 
    916       $brand_id2 = $brand_xml['data']['brands'][$brand_id]['brand_id']; 
    917       $temping = $this->arraysearchrecursive($model_name, $row_o); 
    918       if($temping === FALSE) { 
    919         $sql = "INSERT INTO endpointman_model_list (id, brand, model, product_id) VALUES ('".$model_id."','".$brand_id2."','".$model_name."','".$id."')"; 
    920       } else { 
    921         $sql = "UPDATE endpointman_model_list SET brand = '".$brand_id2."', model= '".$model_name."', product_id='".$id."' WHERE id =". $model_id; 
    922       } 
    923       mysql_query($sql); 
    924     } 
    925      
    926     //Look through the database and compare local values with remote values, removing those phones that are no longer supported. 
    927     $count = count($row_o); 
    928     for($i=0;$i<$count;$i++) { 
    929       $temping = $this->arraysearchrecursive($row_o['id'], $brand_xml); 
    930       if($temping === FALSE) { 
    931         $sql = "DELETE FROM endpointman_model_list WHERE id = " . $row_o['id']; 
    932         mysql_query("UPDATE endpointman_model_list SET model = 0 WHERE model = " . $row_o['id']); 
    933       } 
    934       mysql_query($sql); 
    935     } 
    936     $sql = "UPDATE endpointman_product_list SET installed = '1', cfg_ver= '".$cfg_ver."' WHERE id = " . $id; 
    937     mysql_query($sql); 
    938      
    939     echo "<script>$('div.demo-container').text('Syncing System...');</script>"; 
    940      
    941     $this->sync_product($id); 
    942      
    943     //TODO: once iframe access is fixed to not go into the module, this should be $_SERVER['PHP_SELF']."?type=tool...", and type=tool should be passed in (in case it is not tool) 
    944     // 
    945     echo "<script>$('div.demo-container').html('<a href=\"#\" onclick=\"parent.location.href = \'/admin/config.php?type=tool&display=epm_config\';\">"._("Install Done, Click to Return")."</a>');</script>"; 
    946      
    947     if($global_cfg['debug']) { 
    948       unlink(LOCAL_PATH."debug_installer.log"); 
    949       $debug = array_merge($debug_a, $debug_a2, $debug_a3, $debug_a4, $debug_a5, $debug_a6); 
    950       $outfile=LOCAL_PATH."debug_installer.log"; 
    951       $wfh=fopen($outfile,'a'); 
    952       fwrite($wfh,print_r($debug, TRUE)); 
    953       fclose($wfh); 
    954     } 
    955   } 
    956    
    957   function remove_product($id=NULL) { 
    958     $sql = 'SELECT endpointman_model_list.*, endpointman_product_list.cfg_dir, endpointman_brand_list.directory FROM endpointman_model_list, endpointman_product_list, endpointman_brand_list WHERE endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_brand_list.id = endpointman_product_list.brand AND endpointman_product_list.id = '.$id; 
    959  
    960     $result = mysql_query($sql); 
    961     while($row = mysql_fetch_assoc($result)) { 
    962       $sql = "UPDATE endpointman_mac_list SET model = 0 WHERE model = ".$row['id']; 
    963       mysql_query($sql); 
    964       $row_out = $row; 
    965     } 
    966      
    967     $this->deltree(PHONE_MODULES_PATH.$row_out['directory']."/". $row_out['cfg_dir']); 
    968     $sql = "UPDATE endpointman_product_list SET installed = 0, cfg_ver = '' WHERE id = ".$id; 
    969     mysql_query($sql); 
    970     $sql = "DELETE FROM endpointman_model_list WHERE product_id = ".$id; 
    971     mysql_query($sql);   
    972   } 
    973  
    974   function arraysearchrecursive($Needle,$Haystack,$NeedleKey="",$Strict=false,$Path=array()) { 
    975     if(!is_array($Haystack)) 
    976       return false; 
    977     foreach($Haystack as $Key => $Val) { 
    978       if(is_array($Val)&& 
    979          $SubPath=$this->arraysearchrecursive($Needle,$Val,$NeedleKey,$Strict,$Path)) { 
    980         $Path=array_merge($Path,Array($Key),$SubPath); 
    981         return $Path; 
    982       } 
    983       elseif((!$Strict&&$Val==$Needle&& 
    984               $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))|| 
    985               ($Strict&&$Val===$Needle&& 
    986                $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))) { 
    987         $Path[]=$Key; 
    988         return $Path; 
    989       } 
    990     } 
    991     return false; 
    992   } 
    993   //Below is pfSense code 
    994   function download_file_with_progress_bar($url_file, $destination_file) { 
    995     global $ch, $fout, $file_size, $downloaded, $pkg_interface; 
    996     set_time_limit(0); 
    997     $file_size  = 1; 
    998     $downloaded = 1; 
    999     /* open destination file */ 
    1000     $fout = fopen($destination_file, "wb"); 
    1001  
    1002     /* 
     66        global $amp_conf,$global_cfg, $endpoint_db, $db, $type; 
     67 
     68        $this->db = $db; 
     69        $this->amp_conf = $amp_conf; 
     70        $this->error = array(); 
     71 
     72 
     73        //attached to the end of every link so we can flow through FreePBX 
     74        //This is really hacky but I have to do it when people have different directories instead of admin 
     75        //Define the location of phone modules, keeping it outside of the module directory so that when the user updates endpointmanager they don't lose all of their phones 
     76        define("PHONE_MODULES_PATH", dirname($_SERVER["SCRIPT_FILENAME"])."/modules/_ep_phone_modules/"); 
     77 
     78        //Get local path information 
     79        define("WEB_PATH", dirname($_SERVER['SCRIPT_NAME'])."/modules/endpointman/"); 
     80        define("LOCAL_PATH", dirname($_SERVER["SCRIPT_FILENAME"])."/modules/endpointman/"); 
     81 
     82 
     83 
     84        mysql_close(); 
     85 
     86 
     87        $this->global_cfg =& $db->getAssoc("SELECT var_name, value FROM endpointman_global_vars"); 
     88 
     89        define("UPDATE_PATH", $this->global_cfg['update_server']); 
     90        define("VER", $this->global_cfg['version']); 
     91 
     92        //Define error reporting 
     93        if($this->global_cfg['debug']) { 
     94            error_reporting(E_ALL); 
     95            ini_set('display_errors', 1); 
     96        } else { 
     97            ini_set('display_errors', 0); 
     98        } 
     99        //include the local template class 
     100        include LOCAL_PATH."includes/rain.tpl.class.php"; 
     101    } 
     102 
     103    function add_freepbx_notification($id, $type, $display_text, $text, $link) { 
     104        $sql = "INSERT INTO notifications (module, id, level, display_text, link, candelete, timestamp, extended_text) VALUES ('endpointman', '".$id."', '500', '".$display_text."', '".$link."', '1', '".time()."', '".$text."')"; 
     105        mysql_query($sql); 
     106    } 
     107 
     108    /** 
     109     * Send this function an ID from the mac devices list table and you'll get all the information we have on that particular phone 
     110     * @param integer $mac_id ID number reference from the MySQL database referencing the table endpointman_mac_list 
     111     * @return <type> 
     112     */ 
     113    function get_phone_info($mac_id=NULL) { 
     114        //You could screw up a phone if the mac_id is blank 
     115        if (!isset($mac_id)) { 
     116            die("Can't get phone info!"); 
     117        } 
     118        $sql = "SELECT id,mac,model FROM  endpointman_mac_list WHERE model > 0 AND id =".$mac_id; 
     119        $res =& $this->db->query($sql); 
     120        if($res->numRows()) { 
     121            //Returns Brand Name, Brand Directory, Model Name, Mac Address, Extension (FreePBX), Custom Configuration Template, Custom Configuration Data, Product Name, Product ID, Product Configuration Directory, Product Configuration Version, Product XML name, 
     122            $sql = "SELECT endpointman_mac_list.config_files_override, endpointman_mac_list.user_cfg_data, endpointman_model_list.id as model_id, endpointman_brand_list.id as brand_id, endpointman_brand_list.name, endpointman_brand_list.directory, endpointman_model_list.model, endpointman_mac_list.mac, endpointman_mac_list.ext, endpointman_mac_list.custom_cfg_template, endpointman_mac_list.custom_cfg_data, endpointman_product_list.long_name, endpointman_product_list.id as product_id, endpointman_product_list.cfg_dir, endpointman_product_list.cfg_ver, endpointman_model_list.template_data, endpointman_model_list.enabled, users.name as description, sip.data as secret FROM endpointman_mac_list, endpointman_model_list, endpointman_brand_list, endpointman_product_list, sip, users WHERE endpointman_mac_list.id = ".$mac_id." AND endpointman_mac_list.model = endpointman_model_list.id AND endpointman_brand_list.id = endpointman_model_list.brand AND endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_mac_list.ext = sip.id AND sip.keyword = 'secret' AND endpointman_mac_list.ext = users.extension"; 
     123 
     124            $phone_info =& $this->db->getRow($sql, array(), DB_FETCHMODE_ASSOC); 
     125 
     126            if(!$phone_info) { 
     127                $this->error['get_phone_info'] = "Error with SQL Statement"; 
     128            } 
     129 
     130            //If there is a template associated with this phone then pull that information and put it into the array 
     131            if ($phone_info['custom_cfg_template'] > 0) { 
     132                $sql = "SELECT name, custom_cfg_data, config_files_override FROM endpointman_template_list WHERE id = ".$phone_info['custom_cfg_template']; 
     133 
     134                $phone_info['template_data'] =& $this->db->getRow($sql, array(), DB_FETCHMODE_ASSOC); 
     135 
     136            } 
     137        } else { 
     138            //Phone is unknown, we need to display this to the end user so that they can make corrections 
     139            $row=mysql_fetch_array($result); 
     140            $phone_info['id'] = $mac_id; 
     141            $phone_info['model_id'] = 0; 
     142            $phone_info['brand_id'] = 0; 
     143            $phone_info['ext'] = $row['ext']; 
     144            $phone_info['mac'] = $row['mac']; 
     145            $phone_info['name'] = 'Unknown'; 
     146        } 
     147 
     148        return $phone_info; 
     149    } 
     150 
     151    /** 
     152     * Custom Means specific to that MAC 
     153     * id is either the mac ID (not address) or the template ID 
     154     * @param integer $id 
     155     * @param integer $custom 
     156     */ 
     157    function edit_template_display($id, $custom) { 
     158        $alt_configs = NULL; 
     159 
     160        if($custom == 0) { 
     161            $sql = "SELECT model_id FROM endpointman_template_list WHERE id=".$id; 
     162        } else { 
     163            $sql = "SELECT model FROM endpointman_mac_list WHERE id=".$id; 
     164        } 
     165 
     166        $model_id = $this->db->getOne($sql); 
     167 
     168        //Make sure the model data from the local confg files are stored in the database and vice-versa. Serious errors will occur if the database is not in sync with the local file 
     169        if(!$this->sync_model($model_id)) { 
     170            die("unable to sync local template files - TYPE:". $custom); 
     171        } 
     172 
     173 
     174        //Determine if we are dealing with a general template or a specific [for that phone only] template (custom =0 means general) 
     175        if($custom == 0) { 
     176            $sql = "SELECT endpointman_product_list.config_files, endpointman_product_list.long_name, endpointman_model_list.template_data, endpointman_model_list.id as model_id, endpointman_template_list.* FROM endpointman_product_list, endpointman_model_list, endpointman_template_list WHERE endpointman_product_list.id = endpointman_template_list.product_id AND endpointman_template_list.model_id = endpointman_model_list.id AND endpointman_template_list.id = ".$id; 
     177        } else { 
     178            $sql = "SELECT endpointman_product_list.config_files, endpointman_mac_list.*, endpointman_model_list.id as model_id, endpointman_model_list.template_data, endpointman_product_list.id as product_id, endpointman_product_list.long_name, endpointman_product_list.cfg_dir, endpointman_brand_list.directory FROM endpointman_brand_list, endpointman_mac_list, endpointman_model_list, endpointman_product_list WHERE endpointman_mac_list.id=".$id." AND endpointman_mac_list.model = endpointman_model_list.id AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id"; 
     179        } 
     180 
     181        $row =& $this->db->getRow($sql, array(), DB_FETCHMODE_ASSOC); 
     182 
     183        $tpl = new RainTPL( LOCAL_PATH.'templates' ); 
     184        $tpl->assign("template_editor_display", 1); 
     185 
     186        echo $tpl->draw( 'global_header' ); 
     187        //Let the template system know if we are working with a general template or a specific [for that phone only] template 
     188        $tpl->assign("custom", $custom); 
     189        if($custom) { 
     190            $tpl->assign("ext", $row['ext']); 
     191        } else { 
     192            $tpl->assign("template_name", $row['name']); 
     193        } 
     194        $tpl->assign("model", $row['long_name']); 
     195 
     196        //Start the display of the html file in the product folder 
     197        if($row['config_files_override'] == "") { 
     198            $config_files_saved = ""; 
     199        } else { 
     200            $config_files_saved = unserialize($row['config_files_override']); 
     201        } 
     202        $config_files_list = explode(",",$row['config_files']); 
     203        $i = 0; 
     204        $alt = 0; 
     205        //TODO: Perhaps fix this alternative files list 
     206        foreach($config_files_list as $files) { 
     207            $sql = "SELECT * FROM  endpointman_custom_configs WHERE product_id = ".$row['id']." AND original_name = '".$files."'"; 
     208            $alt_configs_list_count =& $this->db->query($sql); 
     209            if($alt_configs_list_count->numRows() > 0) { 
     210                $alt_configs_list =& $this->db->getAll($sql, array(), DB_FETCHMODE_ASSOC); 
     211                $alt_configs .= $files; 
     212                $alt_configs .= '<select name="'.$files.'">'; 
     213                $alt_configs .= '<option value="0">'.$files.' (Original)</option>'; 
     214                $files = str_replace(".","_",$files); 
     215                foreach($alt_configs_list as $ccf) { 
     216                    $alt_configs .= '<option value="'.$ccf['id'].'" '; 
     217                    $cf_key = $files; 
     218                    if((is_array($config_files_saved)) AND ($config_files_saved[$cf_key] == $ccf['id'])) { 
     219                        $alt_configs .= 'selected'; 
     220                    } 
     221                    $alt_configs .= '>'.$ccf['name'].'</option>'; 
     222                } 
     223                $alt_configs .= "</select>"; 
     224                $alt = 1; 
     225            } 
     226            $i++; 
     227        } 
     228        $tpl->assign("alt_configs", $alt_configs); 
     229        $tpl->assign("alt", $alt); 
     230 
     231        if($row['template_data'] != "") { 
     232            $out = $this->generate_gui_html($row['template_data'],$row['custom_cfg_data'],TRUE); 
     233        } else { 
     234            echo "No Template Data has been defined for this Product<br />"; 
     235        } 
     236 
     237        $tpl->assign("template_editor", $out); 
     238        $tpl->assign("hidden_id", $row['id']); 
     239        $tpl->assign("hidden_custom", $custom); 
     240        echo $tpl->draw( 'template_editor' ); 
     241 
     242        $tpl->assign("debug", ""); 
     243 
     244    } 
     245 
     246    /** 
     247     * Generates the Visual Display for the end user 
     248     * @param <type> $cfg_data 
     249     * @param <type> $custom_cfg_data 
     250     * @param <type> $admin 
     251     * @param <type> $user_cfg_data 
     252     * @return <type> 
     253     */ 
     254    function generate_gui_html($cfg_data,$custom_cfg_data=NULL, $admin=FALSE, $user_cfg_data=NULL) { 
     255        //take the data out of the database and turn it back into an array for use 
     256        $cfg_data = unserialize($cfg_data); 
     257        $count = count($cfg_data); 
     258 
     259        //Check to see if there is a custom template for this phone already listed in the endpointman_mac_list database 
     260        if (isset($custom_cfg_data)) { 
     261            $custom_cfg_data = unserialize($custom_cfg_data); 
     262        } else { 
     263            //No custom template so let's pull the default values for this model into the custom_cfg_data array and populate it from there so that we won't have to make two completely different functions below 
     264            for($i=0;$i<$count;$i++) { 
     265                $key_default = str_replace('$','',$cfg_data[$i]['variable']); 
     266                if(!is_array($cfg_data[$i]['default_value'])) { 
     267                    $custom_cfg_data[$key_default]['value'] = $cfg_data[$i]['default_value']; 
     268 
     269                } 
     270            } 
     271        } 
     272        if(isset($user_cfg_data)) { 
     273            $user_cfg_data = unserialize($user_cfg_data); 
     274        } 
     275 
     276        $template_variables_array = array(); 
     277 
     278        $group_count = 0; 
     279        //Fill the html form data with values from either the database or the default values to display to the end user 
     280        for($i=0;$i<$count;$i++) { 
     281            if(array_key_exists('variable',$cfg_data[$i])) { 
     282                $key = str_replace('$','',$cfg_data[$i]['variable']); 
     283            } else { 
     284                $key = ""; 
     285            } 
     286            if(($admin) OR (isset($custom_cfg_data[$key]['ari']))) { 
     287                //Checks to see if values are defined in the database, if not then we assume this is a new option and we need a default value here! 
     288                if(!isset($custom_cfg_data[$key]['value'])) { 
     289                    //xml2array will take values that have no data and turn them into arrays, we want to avoid the word 'array' as a default value, so we blank it out here if we are an array 
     290                    if((array_key_exists('default_value',$cfg_data[$i])) AND (is_array($cfg_data[$i]['default_value']))) { 
     291                        $custom_cfg_data[$key]['value'] = ""; 
     292                    } elseif((array_key_exists('default_value',$cfg_data[$i])) AND (!is_array($cfg_data[$i]['default_value']))) { 
     293                        $custom_cfg_data[$key]['value'] = $cfg_data[$i]['default_value']; 
     294                    } 
     295                } 
     296                if ($cfg_data[$i]['type'] == "group") { 
     297                    $group_count++; 
     298                    $template_variables_array[$group_count]['title'] = $cfg_data[$i]['description']; 
     299                    $variables_count = 0; 
     300                } elseif ($cfg_data[$i]['type'] == "input") { 
     301                    if((!$admin) && (isset($user_cfg_data[$key]['value']))) { 
     302                        $custom_cfg_data[$key]['value'] = $user_cfg_data[$key]['value']; 
     303                    } 
     304                    $template_variables_array[$group_count]['data'][$variables_count]['type'] = "input"; 
     305                    $template_variables_array[$group_count]['data'][$variables_count]['key'] = $key; 
     306                    $template_variables_array[$group_count]['data'][$variables_count]['value'] = $custom_cfg_data[$key]['value']; 
     307                    $template_variables_array[$group_count]['data'][$variables_count]['description'] = $cfg_data[$i]['description']; 
     308                } elseif ($cfg_data[$i]['type'] == "radio") { 
     309                    if((!$admin) && (isset($user_cfg_data[$key]['value']))) { 
     310                        $custom_cfg_data[$key]['value'] = $user_cfg_data[$key]['value']; 
     311                    } 
     312                    $num = $custom_cfg_data[$key]['value']; 
     313                    $template_variables_array[$group_count]['data'][$variables_count]['type'] = "radio"; 
     314                    $template_variables_array[$group_count]['data'][$variables_count]['key'] = $key; 
     315                    $template_variables_array[$group_count]['data'][$variables_count]['description'] = $cfg_data[$i]['description']; 
     316                    $z = 0; 
     317                    while($z < count($cfg_data[$i]['data'])) { 
     318                        $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['key'] = $key; 
     319                        $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['value'] = $cfg_data[$i]['data'][$z]['value']; 
     320                        $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['description'] = $cfg_data[$i]['data'][$z]['text']; 
     321                        if ($cfg_data[$i]['data'][$z]['value'] == $num) { 
     322                            $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['checked'] = 'checked'; 
     323                        } 
     324                        $z++; 
     325                    } 
     326                } elseif ($cfg_data[$i]['type'] == "list") { 
     327                    if((!$admin) && (isset($user_cfg_data[$key]['value']))) { 
     328                        $custom_cfg_data[$key]['value'] = $user_cfg_data[$key]['value']; 
     329                    } 
     330                    $num = $custom_cfg_data[$key]['value']; 
     331                    $template_variables_array[$group_count]['data'][$variables_count]['type'] = "list"; 
     332                    $template_variables_array[$group_count]['data'][$variables_count]['key'] = $key; 
     333                    $template_variables_array[$group_count]['data'][$variables_count]['description'] = $cfg_data[$i]['description']; 
     334                    $z = 0; 
     335                    while($z < count($cfg_data[$i]['data'])) { 
     336                        $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['value'] = $cfg_data[$i]['data'][$z]['value']; 
     337                        $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['description'] = $cfg_data[$i]['data'][$z]['text']; 
     338                        if ($cfg_data[$i]['data'][$z]['value'] == $num) { 
     339                            $template_variables_array[$group_count]['data'][$variables_count]['data'][$z]['selected'] = 'selected'; 
     340                        } 
     341                        $z++; 
     342                    } 
     343                } elseif ($cfg_data[$i]['type'] == "break") { 
     344                    $template_variables_array[$group_count]['data'][$variables_count]['type'] = "break"; 
     345                } 
     346                if(($this->global_cfg['enable_ari']) AND ($admin) AND ($cfg_data[$i]['type'] != "break") AND ($cfg_data[$i]['type'] != "group")) { 
     347                    $template_variables_array[$group_count]['data'][$variables_count]['aried'] = 1; 
     348                    $template_variables_array[$group_count]['data'][$variables_count]['ari']['key'] = $key; 
     349                    if(isset($custom_cfg_data[$key]['ari'])) { 
     350                        $template_variables_array[$group_count]['data'][$variables_count]['ari']['checked'] = "checked"; 
     351                    } 
     352                } 
     353                $variables_count++; 
     354            } 
     355        } 
     356        return($template_variables_array); 
     357    } 
     358 
     359    function save_template($id, $custom, $variables) { 
     360        //Custom Means specific to that MAC 
     361        if($custom == "0") { 
     362            $sql = "SELECT endpointman_brand_list.directory, endpointman_product_list.cfg_dir, endpointman_product_list.config_files, endpointman_product_list.long_name, endpointman_model_list.template_data, endpointman_model_list.id as model_id, endpointman_template_list.* FROM endpointman_brand_list, endpointman_product_list, endpointman_model_list, endpointman_template_list WHERE endpointman_product_list.id = endpointman_template_list.product_id AND endpointman_brand_list.id = endpointman_product_list.brand AND endpointman_template_list.model_id = endpointman_model_list.id AND endpointman_template_list.id = ".$id; 
     363        } else { 
     364            $sql = "SELECT endpointman_product_list.config_files, endpointman_mac_list.*, endpointman_product_list.id as product_id, endpointman_product_list.long_name, endpointman_model_list.template_data, endpointman_product_list.cfg_dir, endpointman_brand_list.directory FROM endpointman_brand_list, endpointman_mac_list, endpointman_model_list, endpointman_product_list WHERE endpointman_mac_list.id=".$id." AND endpointman_mac_list.model = endpointman_model_list.id AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id"; 
     365        } 
     366 
     367        //Load template data 
     368        $row =& $this->db->getRow($sql, array(), DB_FETCHMODE_ASSOC); 
     369 
     370        $cfg_data = unserialize($row['template_data']); 
     371        $count = count($cfg_data); 
     372        for($i=0;$i<$count;$i++) { 
     373            if(array_key_exists('variable',$cfg_data[$i])) { 
     374                $temping = str_replace('$','',$cfg_data[$i]['variable']); 
     375                $temping_ari = "ari_" . $temping; 
     376                if(array_key_exists($temping, $_REQUEST)) { 
     377                    $custom_cfg_data[$temping]['value'] = $_REQUEST[$temping]; 
     378                    if(array_key_exists($temping_ari, $_REQUEST)) { 
     379                        if($_REQUEST[$temping_ari] == "on") { 
     380                            $custom_cfg_data[$temping]['ari'] = 1; 
     381                        } 
     382                    } 
     383                } 
     384            } 
     385        } 
     386 
     387        $config_files = explode(",",$row['config_files']); 
     388        $i = 0; 
     389        while($i < count($config_files)) { 
     390            $config_files[$i] = str_replace(".","_",$config_files[$i]); 
     391            if(isset($_REQUEST[$config_files[$i]])) { 
     392                if($_REQUEST[$config_files[$i]] > 0) { 
     393                    $config_files_selected[$config_files[$i]] = $_REQUEST[$config_files[$i]]; 
     394                } 
     395            } 
     396            $i++; 
     397        } 
     398        if(!isset($config_files_selected)) { 
     399            $config_files_selected = ""; 
     400        } else { 
     401            $config_files_selected = serialize($config_files_selected); 
     402        } 
     403        $save = serialize($custom_cfg_data); 
     404 
     405        if($custom == "0") { 
     406            $sql = 'UPDATE endpointman_template_list SET config_files_override = \''.addslashes($config_files_selected).'\', custom_cfg_data = \''.addslashes($save).'\' WHERE id ='.$id; 
     407            $location = "template_manager"; 
     408        } else { 
     409            $sql = 'UPDATE endpointman_mac_list SET config_files_override = \''.addslashes($config_files_selected).'\', custom_cfg_template = 0, custom_cfg_data = \''.addslashes($save).'\' WHERE id ='.$id; 
     410            $location = "devices_manager"; 
     411        } 
     412        $this->db->query($sql); 
     413 
     414        $phone_info = ""; 
     415 
     416        if($custom != 0) { 
     417            $phone_info = $this->get_phone_info($id); 
     418            $this->prepare_configs($phone_info); 
     419 
     420        } else { 
     421            $sql = 'SELECT id FROM endpointman_mac_list WHERE custom_cfg_template = '.$id; 
     422            foreach($phones as $data) { 
     423                $phone_info = $this->get_phone_info($data['id']); 
     424                $this->prepare_configs($phone_info); 
     425            } 
     426        } 
     427 
     428        return($location); 
     429 
     430    } 
     431 
     432    function prepare_configs($phone_info) { 
     433        require(PHONE_MODULES_PATH.'setup.php'); 
     434 
     435        $class = "endpoint_" . $phone_info['directory'] . "_" . $phone_info['cfg_dir'] . '_phone'; 
     436 
     437        $provisioner_lib = new $class(); 
     438 
     439        $provisioner_lib->root_dir = PHONE_MODULES_PATH; 
     440 
     441        //have to because of versions less than php5.3 
     442        $provisioner_lib->brand_name = $phone_info['directory']; 
     443        $provisioner_lib->family_line = $phone_info['cfg_dir']; 
     444 
     445        //Mac Address 
     446        $provisioner_lib->mac = $phone_info['mac']; 
     447 
     448        //Phone Model (Please reference family_data.xml in the family directory for a list of recognized models) 
     449        $provisioner_lib->model = $phone_info['model']; 
     450 
     451        //Timezone 
     452        $provisioner_lib->timezone = $this->global_cfg['gmthr']; 
     453 
     454        //Server IP 
     455        $provisioner_lib->server[1]['ip'] = $this->global_cfg['srvip']; 
     456        $provisioner_lib->server[1]['port'] = 5060; 
     457 
     458        //Provide alternate Configuration file instead of the one from the hard drive 
     459        //$endpoint->config_files_override['$mac.cfg'] = "{\$srvip}\n{\$admin_pass|0}\n{\$test.line.1}"; 
     460 
     461        //Pretend we have three lines, we could just have one line or 20...whatever the phone supports 
     462        $provisioner_lib->lines[1] = array('ext' => $phone_info['ext'], 'secret' => $phone_info['secret'], 'displayname' => $phone_info['description']); 
     463        //$endpoint->lines[2] = array('ext' => '104', 'secret' => 'blah4', 'displayname' => 'Display Name'); 
     464        //$endpoint->lines[3] = array('ext' => '105', 'secret' => 'blah5', 'displayname' => 'Other Account'); 
     465 
     466        if(is_array($phone_info['template_data'])) { 
     467            $template_data = unserialize($phone_info['template_data']['custom_cfg_data']); 
     468        } else { 
     469            $template_data = unserialize($phone_info['custom_cfg_data']); 
     470        } 
     471 
     472        $new_template_data = array(); 
     473        foreach($template_data as $key => $data) { 
     474            $new_template_data = array_merge($new_template_data, array($key => $data['value'])); 
     475        } 
     476 
     477        //Set Variables according to the template_data files included. We can include different template.xml files within family_data.xml also one can create 
     478        //template_data_custom.xml which will get included or template_data_<model_name>_custom.xml which will also get included 
     479        //line 'global' will set variables that aren't line dependant 
     480        $provisioner_lib->options = $new_template_data; 
     481        //Setting a line variable here...these aren't defined in the template_data.xml file yet. however they will still be parsed 
     482        //and if they have defaults assigned in a future template_data.xml or in the config file using pipes (|) those will be used, pipes take precedence 
     483 
     484        // Because every brand is an extension (eventually) of endpoint, you know this function will exist regardless of who it is 
     485        $returned_data = $provisioner_lib->generate_config(); 
     486 
     487        foreach($returned_data as $key => $data) { 
     488            $fp = fopen("/tftpboot/".$key, 'w'); 
     489            fwrite($fp, $data); 
     490            fclose($fp); 
     491        } 
     492 
     493        unset($provisioner_lib); 
     494    } 
     495 
     496    function firmware_update_check($id=NULL) { 
     497        $sql = "SELECT * FROM  endpointman_product_list WHERE  id =". $id; 
     498        $result = mysql_query($sql); 
     499        $row = mysql_fetch_array($result); 
     500 
     501        $sql = "SELECT directory FROM  endpointman_brand_list WHERE id =". $row['brand']; 
     502        $result = mysql_query($sql); 
     503        if(mysql_num_rows($result)) { 
     504            $brand_row = mysql_fetch_array($result); 
     505 
     506            //config drive unknown! 
     507            if ($row['cfg_dir'] == "") { 
     508                return FALSE; 
     509            } else { 
     510                $temp = $this->xml2array(PHONE_MODULES_PATH.$brand_row['directory']."/".$row['cfg_dir']."/template_data.xml"); 
     511                if((array_key_exists('data',$temp)) AND (!is_array($temp['data']['firmware_ver']))) { 
     512                    if($row['firmware_vers'] < $temp['data']['firmware_ver']) { 
     513                        return $temp; 
     514                    } else { 
     515                        return FALSE; 
     516                    } 
     517                } else { 
     518                    return FALSE; 
     519                } 
     520            } 
     521        } else { 
     522            return FALSE; 
     523        } 
     524    } 
     525 
     526    function firmware_local_check($id=NULL) { 
     527        $sql = "SELECT * FROM  endpointman_product_list WHERE hidden = 0 AND id =". $id; 
     528        $result = mysql_query($sql); 
     529        if(mysql_num_rows($result)) { 
     530            $row = mysql_fetch_array($result); 
     531 
     532            $sql = "SELECT directory FROM  endpointman_brand_list WHERE hidden = 0 AND id =". $row['brand']; 
     533            $result = mysql_query($sql); 
     534            $brand_row = mysql_fetch_array($result); 
     535 
     536            //config drive unknown! 
     537            if ($row['cfg_dir'] == "") { 
     538                return("nothing"); 
     539            } else { 
     540                $temp = $this->xml2array(PHONE_MODULES_PATH.$brand_row['directory']."/".$row['cfg_dir']."/template_data.xml"); 
     541                if((isset ($temp['data']['firmware_ver'])) AND (!is_array($temp['data']['firmware_ver']))) { 
     542                    if($row['firmware_vers'] == "") { 
     543                        return("install"); 
     544                    } else { 
     545                        return("remove"); 
     546                    } 
     547                } else { 
     548                    return("nothing"); 
     549                } 
     550            } 
     551        } else { 
     552            return("nothing"); 
     553        } 
     554    } 
     555 
     556    function remove_firmware($id=NULL) { 
     557        global $global_cfg; 
     558        $sql = "SELECT * FROM  endpointman_product_list WHERE  id =". $id; 
     559        $result = mysql_query($sql); 
     560        $row = mysql_fetch_array($result); 
     561 
     562        $file_list = explode(",",$row['firmware_files']); 
     563        $i = 0; 
     564        while($i < count($file_list)) { 
     565            unlink($global_cfg['config_location'].$file_list[$i]); 
     566            $i++; 
     567        } 
     568        $sql = 'UPDATE endpointman_product_list SET firmware_files = "", firmware_vers = "" WHERE id = '.$id; 
     569        mysql_query($sql); 
     570    } 
     571 
     572    function install_firmware($product_id) { 
     573        global $global_cfg, $debug; 
     574        $sql = 'SELECT endpointman_product_list.*, endpointman_brand_list.directory FROM endpointman_product_list, endpointman_brand_list WHERE endpointman_product_list.brand = endpointman_brand_list.id AND endpointman_product_list.id = '.$product_id; 
     575        $result = mysql_query($sql); 
     576        $row = mysql_fetch_array($result); 
     577        $temp = $this->xml2array(PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/template_data.xml"); 
     578        if($temp['data']['firmware_ver'] > $row['firmware_vers']) { 
     579            echo "<script>$('div.demo-container').text('Installing firmware');</script>"; 
     580            $this->download_file_with_progress_bar(UPDATE_PATH . $temp['data']['firmware_pkg'], PHONE_MODULES_PATH."temp/".$temp['data']['firmware_pkg']); 
     581            global $global_cfg, $endpoint; 
     582            if(!file_exists(PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware")) { 
     583                mkdir(PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware"); 
     584            } 
     585            exec("unzip ".PHONE_MODULES_PATH."temp/" . $temp['data']['firmware_pkg'] ." -d ".PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware/", $debug_a = array()); 
     586            if ($handle = opendir(PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware/")) { 
     587                while (false !== ($file = readdir($handle))) { 
     588                    if ($file != "." && $file != "..") { 
     589                        $list .= $file.","; 
     590                    } 
     591                } 
     592                closedir($handle); 
     593            } 
     594            $list = substr_replace($list ,"",-1); 
     595            exec("cp -r " . PHONE_MODULES_PATH.$row['directory']."/".$row['cfg_dir']."/firmware/*.* ". $global_cfg['config_location'], $debug_a2 = array()); 
     596            $sql = "UPDATE endpointman_product_list SET firmware_vers = '".$temp['data']['firmware_ver']."', firmware_files = '".$list."' WHERE id = ". $row['id']; 
     597            mysql_query($sql); 
     598            echo "<script>$('div.demo-container').text('Done!');</script>"; 
     599        } else { 
     600            echo "<script>$('div.demo-container').text('Your Firmware is already up to date');</script>"; 
     601 
     602        } 
     603        if($global_cfg['debug']) { 
     604            $debug = array_merge($debug_a, $debug_a2); 
     605            $outfile=LOCAL_PATH."debug_installer.log"; 
     606            $wfh=fopen($outfile,'a'); 
     607            fwrite($wfh,print_r($debug, TRUE)); 
     608            fclose($wfh); 
     609        } 
     610    } 
     611 
     612    function fix_single_array_keys($array) { 
     613        if((empty($array[0])) AND (!empty($array))) { 
     614            $array_n[0] = $array; 
     615            return($array_n); 
     616        } elseif(!empty($array)) { 
     617            return($array); 
     618        } else { 
     619            return(""); 
     620        } 
     621    } 
     622 
     623    function download_xml($location,$directory=NULL) { 
     624        $handle = fopen($location, "rb"); 
     625        if($handle === FALSE) { 
     626            //Error with the internet....ABORRRTTTT THEEEEE DOWNLOAAAAADDDDDDDD! SCOTTYYYY!; 
     627            return(FALSE); 
     628        } else { 
     629            $contents = ''; 
     630            while (!feof($handle)) { 
     631                $contents .= fread($handle, 8192); 
     632            } 
     633            @$a = simplexml_load_string($contents); 
     634            if($a===FALSE) { 
     635                //Error with the internet....ABORRRTTTT THEEEEE DOWNLOAAAAADDDDDDDD! SCOTTYYYY!; 
     636                return(FALSE); 
     637            } else { 
     638                if(!isset($directory)) { 
     639                    $fp = fopen(PHONE_MODULES_PATH.'master.xml', 'w'); 
     640                } else { 
     641                    if(!file_exists(PHONE_MODULES_PATH.'/'.$directory)) { 
     642                        mkdir(PHONE_MODULES_PATH.'/'.$directory); 
     643                    } 
     644                    $fp = fopen(PHONE_MODULES_PATH.'/'.$directory.'/brand_data.xml', 'w'); 
     645                } 
     646                fwrite($fp, $contents); 
     647                fclose($fp); 
     648                return(TRUE); 
     649            } 
     650        } 
     651        fclose($handle); 
     652 
     653    } 
     654 
     655    //Check for new packges for brands. These packages will include phone models and such which the user can remove if they want 
     656    function brand_update_check() { 
     657        $result = $this->download_xml(UPDATE_PATH . "master.xml"); 
     658 
     659        $message = NULL; 
     660 
     661        if(!$result) { 
     662            $message .= "Not able to connect to repository. Using local master file instead."; 
     663        } 
     664 
     665        $temp = $this->xml2array(PHONE_MODULES_PATH.'master.xml'); 
     666 
     667        $endpoint_package = $temp['data']['package']; 
     668        $endpoint_last_mod = $temp['data']['last_modified']; 
     669 
     670        $sql = "SELECT value FROM endpointman_global_vars WHERE var_name LIKE 'endpoint_vers'"; 
     671        $data =& $this->db->getOne($sql); 
     672 
     673        if(($data == "") OR ($data <= $endpoint_last_mod)) { 
     674            $this->download_file_with_progress_bar(UPDATE_PATH.'/'.$endpoint_package, PHONE_MODULES_PATH."temp/".$endpoint_package); 
     675            exec("tar -xvf ".PHONE_MODULES_PATH.'temp/'. $endpoint_package ." -C ".PHONE_MODULES_PATH."temp/"); 
     676 
     677            if(!file_exists(PHONE_MODULES_PATH."endpoint")) { 
     678                mkdir(PHONE_MODULES_PATH."endpoint"); 
     679            } 
     680 
     681            //rename(PHONE_MODULES_PATH."temp/setup.php", PHONE_MODULES_PATH."setup.php"); 
     682 
     683 
     684            rename(PHONE_MODULES_PATH."temp/endpoint/base.php", PHONE_MODULES_PATH."endpoint/base.php"); 
     685 
     686            $sql = "UPDATE endpointman_global_vars SET value = '".$endpoint_last_mod."' WHERE var_name = 'endpoint_vers'"; 
     687            $this->db->query($sql); 
     688        } 
     689 
     690 
     691        $out = $temp['data']['brands']; 
     692 
     693        $row =& $this->db->getAll('SELECT * FROM  endpointman_brand_list WHERE id > 0', array(), DB_FETCHMODE_ASSOC); 
     694 
     695        foreach($out as $data) { 
     696            //TODO: Make this pull from the local directory 
     697            $result = $this->download_xml(UPDATE_PATH .$data['directory']."/".$data['directory'].".xml","endpoint/".$data['directory']); 
     698            if(!$result) { 
     699                $message .= "<br/>Not able to connect to repository. Using local brand [".$data['name']."] file instead."; 
     700            } 
     701 
     702            if(file_exists(PHONE_MODULES_PATH."endpoint/".$data['directory']."/brand_data.xml")) { 
     703                $temp = $this->xml2array(PHONE_MODULES_PATH."endpoint/".$data['directory']."/brand_data.xml"); 
     704 
     705                $temp = $temp['data']['brands']; 
     706 
     707                $temp['oui_list']['oui'] = $this->fix_single_array_keys($temp['oui_list']['oui']); 
     708 
     709                foreach($temp['oui_list']['oui'] as $oui) { 
     710                    $sql = "INSERT INTO endpointman_oui_list (`oui`, `brand`, `custom`) VALUES ('".$oui."', '".$temp['brand_id']."', '0')"; 
     711                    $this->db->query($sql); 
     712 
     713                } 
     714 
     715                $brand_name = $data['directory']; 
     716                $version[$brand_name] = $temp['version']; 
     717 
     718                if(!($this->arraysearchrecursive($brand_name, $row, 'directory'))) { 
     719                    //insert row 
     720                    $sql = "INSERT INTO endpointman_brand_list (id, name, directory, cfg_ver) VALUES ('".$temp['brand_id']."', '".$temp['name']."', '".$temp['directory']."', '".$temp['version']."')"; 
     721                    $this->db->query($sql); 
     722                } else { 
     723                    //in database already! 
     724                } 
     725            } else { 
     726                $message .= "<br/>Error: No Local File for ".$data['name']."!"; 
     727            } 
     728        } 
     729 
     730        foreach($row as $ava_brands) { 
     731            $key = $this->arraysearchrecursive($ava_brands['directory'], $out, 'directory'); 
     732            if($key === FALSE) { 
     733                $this->remove_brand($ava_brands['id']); 
     734            } else { 
     735                $key = $key[0]; 
     736 
     737                $brand_name = $ava_brands['directory']; 
     738 
     739                if($ava_brands['cfg_ver'] < $version[$brand_name]) { 
     740                    $out[$key]['update'] = 1; 
     741                    $out[$key]['update_vers'] = $version[$brand_name]; 
     742                } else { 
     743                    $out[$key]['update'] = NULL; 
     744                } 
     745            } 
     746        } 
     747 
     748        $this->error['brand_update_check'] = $message; 
     749 
     750        return $out; 
     751    } 
     752 
     753    //Version 2.5 good 
     754    //Still needs way to determine when models move...perhaps another function? 
     755    function update_brand($id = NULL) { 
     756        $row =& $this->db->getAll('SELECT * FROM  endpointman_brand_list WHERE id ='.$id, array(), DB_FETCHMODE_ASSOC); 
     757 
     758        echo "Downloading Brand XML....."; 
     759        $result = $this->download_xml(UPDATE_PATH .$row[0]['directory']. "/".$row[0]['directory'].".xml","endpoint/".$row[0]['directory']); 
     760        if($result) { 
     761            echo "Done!<br/>"; 
     762 
     763            $temp = $this->xml2array(PHONE_MODULES_PATH.'/endpoint/'.$row[0]['directory'].'/brand_data.xml'); 
     764 
     765            $brand_name = $temp['data']['brands']['name']; 
     766            $brand_id = $temp['data']['brands']['brand_id']; 
     767            $brand_version = $temp['data']['brands']['version']; 
     768            $package = $temp['data']['brands']['package']; 
     769 
     770            echo "Updating data.........."; 
     771            $sql = "UPDATE endpointman_brand_list SET name = '".$brand_name."', cfg_ver = '".$brand_version."', installed = 1, hidden = 0 WHERE id = ".$id; 
     772            $this->db->query($sql); 
     773            echo "Done!<br/>"; 
     774 
     775            echo "Downloading Tarball......."; 
     776            $this->download_file_with_progress_bar(UPDATE_PATH.$row[0]['directory'].'/'.$package, PHONE_MODULES_PATH."temp/".$package); 
     777            echo "Done!<br />"; 
     778 
     779            echo "Extracting Tarball........"; 
     780            exec("tar -xvf ".PHONE_MODULES_PATH.'temp/'. $package ." -C ".PHONE_MODULES_PATH."temp/"); 
     781            echo "Done!<br />"; 
     782 
     783            echo "Creating Directory Structure/Moving Files..............."; 
     784            $dir_iterator = new RecursiveDirectoryIterator(PHONE_MODULES_PATH."temp/".$row[0]['directory']."/"); 
     785            $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); 
     786            // could use CHILD_FIRST if you so wish 
     787 
     788            foreach ($iterator as $file) { 
     789                if(is_dir($file)) { 
     790                    $dir = str_replace(PHONE_MODULES_PATH."temp/".$row[0]['directory']."/", "", $file); 
     791                    if(!file_exists(PHONE_MODULES_PATH."endpoint/".$row[0]['directory']."/".$dir)) { 
     792                        mkdir(PHONE_MODULES_PATH."endpoint/".$row[0]['directory']."/".$dir); 
     793                    } 
     794                } else { 
     795                    if(basename($file) != "brand_data.xml") { 
     796                        $dir = str_replace(PHONE_MODULES_PATH."temp/".$row[0]['directory']."/", "", $file); 
     797                        rename($file, PHONE_MODULES_PATH."endpoint/".$row[0]['directory']."/".$dir); 
     798                    } 
     799                } 
     800            } 
     801            echo "Done!<br />"; 
     802 
     803            echo "Removing Temporary Files.............."; 
     804            $this->deltree(PHONE_MODULES_PATH."temp/" .$row[0]['directory']); 
     805            unlink(PHONE_MODULES_PATH.'temp/'. $package); 
     806            echo "Done!<br />"; 
     807 
     808 
     809            $temp['data']['brands']['family_list']['family'] = $this->fix_single_array_keys($temp['data']['brands']['family_list']['family']); 
     810            foreach($temp['data']['brands']['family_list']['family'] as $family_list) { 
     811                echo "Updating Family Lines.................<br/>"; 
     812                $family_line_xml = $this->xml2array(PHONE_MODULES_PATH.'/endpoint/'.$row[0]['directory'].'/'.$family_list['directory'].'/family_data.xml'); 
     813                $data =& $this->db->getOne("SELECT id FROM endpointman_product_list WHERE id='".$brand_id."-".$family_line_xml['data']['id']."'", array(), DB_FETCHMODE_ASSOC); 
     814                $short_name = preg_replace("/\[(.*?)\]/si", "", $family_line_xml['data']['name']); 
     815                if($data) { 
     816                    $sql = "UPDATE endpointman_product_list SET short_name = '".$short_name."', long_name = '".$family_line_xml['data']['name']."', cfg_ver = '".$family_line_xml['data']['version']."', config_files='".$family_line_xml['data']['configuration_files']."', hidden = '0' WHERE id = '".$brand_id."-".$family_line_xml['data']['id']."'"; 
     817                } else { 
     818                    $sql = "INSERT INTO endpointman_product_list (`id`, `brand`, `short_name`, `long_name`, `cfg_dir`, `cfg_ver`, `config_files`, `hidden`) VALUES ('".$brand_id."-".$family_line_xml['data']['id']."', '".$brand_id."', '".$short_name."', '".$family_line_xml['data']['name']."', '".$family_line_xml['data']['directory']."', '".$family_line_xml['data']['version']."','".$family_line_xml['data']['configuration_files']."', '0')"; 
     819                } 
     820 
     821                $this->db->query($sql); 
     822                $family_line_xml['data']['model_list'] = $this->fix_single_array_keys($family_line_xml['data']['model_list']); 
     823                echo "--Updating Model Lines................<br/>"; 
     824                foreach($family_line_xml['data']['model_list'] as $model_list) { 
     825                    if(is_array($model_list['template_data']['files'])) { 
     826                        $template_list = implode(",",$model_list['template_data']['files']); 
     827                    } else { 
     828                        $template_list = $model_list['template_data']['files']; 
     829                    } 
     830                    $m_data =& $this->db->getOne("SELECT id FROM endpointman_model_list WHERE id='".$brand_id."-".$family_line_xml['data']['id']."-".$model_list['id']."'", array(), DB_FETCHMODE_ASSOC); 
     831                    if($m_data) { 
     832                        $sql = "UPDATE endpointman_model_list SET model = '".$model_list['model']."', template_list = '".$template_list."', enabled = '0', hidden = '0' WHERE id = '".$brand_id."-".$family_line_xml['data']['id']."'"; 
     833                    } else { 
     834                        $sql = "INSERT INTO endpointman_model_list (`id`, `brand`, `model`, `product_id`, `template_list`, `enabled`, `hidden`) VALUES ('".$brand_id."-".$family_line_xml['data']['id']."-".$model_list['id']."', '".$brand_id."', '".$model_list['model']."', '".$brand_id."-".$family_line_xml['data']['id']."', '".$template_list."', '0', '0')"; 
     835                    } 
     836                    $this->db->query($sql); 
     837                } 
     838            } 
     839        } else { 
     840            echo "<br/>Error Connecting to the Package Repository. Module not installed. Please Try again later."; 
     841        } 
     842    } 
     843 
     844    //Version 2.5 good 
     845    function remove_brand($id=NULL) { 
     846        $brand_dir =& $this->db->getOne("SELECT directory FROM endpointman_brand_list WHERE id=".$id); 
     847 
     848        $sql = "DELETE FROM endpointman_model_list WHERE brand = '". $id."'"; 
     849        $this->db->query($sql); 
     850 
     851        $sql = "DELETE FROM endpointman_product_list WHERE brand = '". $id . "'"; 
     852        $this->db->query($sql); 
     853 
     854        $sql = "DELETE FROM endpointman_oui_list WHERE brand = '". $id . "'"; 
     855        $this->db->query($sql); 
     856 
     857        $this->deltree(PHONE_MODULES_PATH .$brand_dir); 
     858        $sql = "DELETE FROM endpointman_brand_list WHERE id = ". $id; 
     859 
     860        $this->db->query($sql); 
     861    } 
     862 
     863    function sync_model($model=NULL) { 
     864 
     865        //TODO: combine these queries 
     866 
     867        $sql = "SELECT * FROM  endpointman_model_list WHERE id='".$model."'"; 
     868 
     869        $model_id = explode("-",$model); 
     870 
     871        $model_row =& $this->db->getRow($sql, array(),DB_FETCHMODE_ASSOC); 
     872 
     873        $sql = "SELECT * FROM  endpointman_product_list WHERE id='".$model_row['product_id']."'"; 
     874 
     875        $product_row =& $this->db->getRow($sql, array(),DB_FETCHMODE_ASSOC); 
     876 
     877        $sql = "SELECT * FROM  endpointman_brand_list WHERE id=".$model_row['brand']; 
     878 
     879 
     880        $brand_row =& $this->db->getRow($sql, array(),DB_FETCHMODE_ASSOC); 
     881 
     882 
     883        $family_line_xml = $this->xml2array(PHONE_MODULES_PATH.'/endpoint/'.$brand_row['directory'].'/'.$product_row['cfg_dir'].'/family_data.xml'); 
     884 
     885        if($product_row['cfg_ver'] <= $family_line_xml['data']['version']) { 
     886            $key = $this->arraysearchrecursive($model_id[2], $family_line_xml['data']['model_list'], 'id'); 
     887 
     888            if($key === FALSE) { 
     889                return(FALSE); 
     890            } else { 
     891                $key[0]; 
     892                if(is_array($family_line_xml['data']['model_list'][$key[0]]['template_data']['files'])) { 
     893                    $template_list = implode(",",$family_line_xml['data']['model_list'][$key[0]]['template_data']['files']); 
     894                    $template_list_array = $family_line_xml['data']['model_list'][$key[0]]['template_data']['files']; 
     895                } else { 
     896                    $template_list = $family_line_xml['data']['model_list'][$key[0]]['template_data']['files']; 
     897                    $template_list_array[0] = $family_line_xml['data']['model_list'][$key[0]]['template_data']['files']; 
     898                } 
     899            } 
     900 
     901            $sql = "UPDATE endpointman_model_list SET template_list = '".$template_list."' WHERE id = '".$model."'"; 
     902            $this->db->query($sql); 
     903 
     904            $version = $family_line_xml['data']['version']; 
     905            $long_name = $family_line_xml['data']['name']; 
     906            $short_name = preg_replace("/\[(.*?)\]/si", "", $family_line_xml['data']['name']); 
     907            $configuration_files = $family_line_xml['data']['configuration_files']; 
     908 
     909            $sql = "UPDATE endpointman_product_list SET long_name = '".$template_list."', short_name = '".$short_name."' , cfg_ver = '".$version."', WHERE id = '".$product_row['id']."'"; 
     910            $this->db->query($sql); 
     911 
     912            $template_data_array = array(); 
     913            foreach($template_list_array as $data) { 
     914                if(file_exists(PHONE_MODULES_PATH.'/endpoint/'.$brand_row['directory'].'/'.$product_row['cfg_dir'].'/'.$data)) { 
     915                    $template_data_xml = $this->xml2array(PHONE_MODULES_PATH.'/endpoint/'.$brand_row['directory'].'/'.$product_row['cfg_dir'].'/'.$data); 
     916                    $template_data_xml = $this->fix_single_array_keys($template_data_xml['template_data']['item']); 
     917                    $template_data_array = array_merge($template_data_array, $template_data_xml); 
     918                } 
     919            } 
     920 
     921            if (file_exists(PHONE_MODULES_PATH.'/endpoint/'.$brand_row['directory'].'/'.$product_row['cfg_dir'].'/template_data_custom.xml')) { 
     922                $template_data_multi = $this->xml2array(PHONE_MODULES_PATH.'/endpoint/'.$brand_row['directory'].'/'.$product_row['cfg_dir'].'/template_data_custom.xml'); 
     923                $template_data_multi = $this->fix_single_array_keys($template_data_multi['template_data']['item']); 
     924                $template_data_array = array_merge($template_data_array, $template_data_multi); 
     925            } 
     926 
     927            if (file_exists(PHONE_MODULES_PATH.'/endpoint/'.$brand_row['directory'].'/'.$product_row['cfg_dir'].'/template_data_' . $model_row['model'] . '_custom.xml')) { 
     928                $template_data_multi = $this->xml2array(self::$modules_path . $this->brand_name . "/" . $this->family_line . "/template_data_" . $this->model . "_custom.xml"); 
     929                $template_data_multi = $this->fix_single_array_keys($template_data_multi['template_data']['item']); 
     930                $template_data_array = array_merge($template_data_array, $template_data_multi); 
     931            } 
     932 
     933            if(empty($template_data_array)) { 
     934                return(FALSE); 
     935            } 
     936 
     937            $sql = "UPDATE endpointman_model_list SET template_data = '".serialize($template_data_array)."' WHERE id = '".$model."'"; 
     938            $this->db->query($sql); 
     939        } 
     940 
     941        return(TRUE); 
     942    } 
     943 
     944    function arraysearchrecursive($Needle,$Haystack,$NeedleKey="",$Strict=false,$Path=array()) { 
     945        if(!is_array($Haystack)) 
     946            return false; 
     947        foreach($Haystack as $Key => $Val) { 
     948            if(is_array($Val)&& 
     949                    $SubPath=$this->arraysearchrecursive($Needle,$Val,$NeedleKey,$Strict,$Path)) { 
     950                $Path=array_merge($Path,Array($Key),$SubPath); 
     951                return $Path; 
     952            } 
     953            elseif((!$Strict&&$Val==$Needle&& 
     954                            $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))|| 
     955                    ($Strict&&$Val===$Needle&& 
     956                            $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))) { 
     957                $Path[]=$Key; 
     958                return $Path; 
     959            } 
     960        } 
     961        return false; 
     962    } 
     963    //Below is pfSense code 
     964    function download_file_with_progress_bar($url_file, $destination_file) { 
     965        global $ch, $fout, $file_size, $downloaded, $pkg_interface; 
     966        set_time_limit(0); 
     967        $file_size  = 1; 
     968        $downloaded = 1; 
     969        /* open destination file */ 
     970        $fout = fopen($destination_file, "wb"); 
     971 
     972        /* 
    1003973     *  Originally by Author: Keyvan Minoukadeh 
    1004974     *  Modified by Scott Ullrich to return Content-Length size 
    1005        */ 
    1006  
    1007     $ch = curl_init(); 
    1008     curl_setopt($ch, CURLOPT_URL, $url_file); 
    1009     curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'endpointmanager_read_header'); 
    1010     curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'endpointmanager_read_body'); 
    1011     curl_setopt($ch, CURLOPT_NOPROGRESS, '1'); 
    1012     curl_setopt($ch, CURLOPT_TIMEOUT, 0);  
    1013  
    1014     curl_exec($ch); 
    1015     $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
    1016     if($fout) 
    1017       fclose($fout); 
    1018     curl_close($ch); 
    1019     return ($http_code == 200) ? true : $http_code; 
    1020   } 
    1021   //end pfSense code 
    1022  
    1023   //This function looks in common linux directories for system executable files. Like ARP & NMAP 
    1024   function find_exec($exec) { 
    1025     $usr_bin = glob("/usr/bin/".$exec); 
    1026     $usr_sbin = glob("/usr/sbin/".$exec); 
    1027     $sbin = glob("/sbin/".$exec); 
    1028     $bin = glob("/bin/".$exec); 
    1029     $etc = glob("/etc/".$exec); 
    1030     if(isset($usr_bin[0])) { 
    1031       return("/usr/bin/".$exec); 
    1032     } elseif(isset($usr_sbin[0])) { 
    1033       return("/usr/sbin/".$exec); 
    1034     } elseif(isset($sbin[0])) { 
    1035       return("/sbin/".$exec); 
    1036     } elseif(isset($bin[0])) { 
    1037       return("/bin/".$exec); 
    1038     } elseif(isset($etc[0])) { 
    1039       return("/etc/".$exec); 
    1040     } else { 
    1041       return($exec); 
    1042     } 
    1043   } 
    1044  
    1045   //Delete Directory Tree, Doesn't work. though 
    1046   function deltree($dir) {  
    1047     $this->rmrf($dir); 
    1048   } 
    1049    
    1050   function rmrf($dir) { 
    1051       foreach (glob($dir) as $file) { 
    1052           if (is_dir($file)) {  
    1053               $this->rmrf("$file/*"); 
    1054               rmdir($file); 
    1055           } else { 
    1056               unlink($file); 
    1057           } 
    1058       } 
    1059   } 
    1060  
    1061    
    1062   function table_exists ($table) {  
    1063     $sql = "SHOW TABLES FROM asterisk"; 
    1064     $result = mysql_query($sql); 
    1065  
    1066     while ($row = mysql_fetch_row($result)) { 
    1067       if ($row[0] == $table) { 
    1068         return TRUE; 
    1069       } 
    1070     } 
    1071     return FALSE; 
    1072   } 
    1073  
    1074   /** 
    1075   Parse XML file into an array 
    1076   Taken from Sister Project SuperFecta 
    1077   */ 
    1078   function xml2array($url, $get_attributes = 1, $priority = 'tag') 
    1079   { 
    1080     $contents = ""; 
    1081     if (!function_exists('xml_parser_create')) 
    1082     { 
    1083       return array (); 
    1084     } 
    1085     $parser = xml_parser_create(''); 
    1086     if(!($fp = @ fopen($url, 'rb'))) 
    1087     { 
    1088       return array (); 
    1089     } 
    1090     while(!feof($fp)) 
    1091     { 
    1092       $contents .= fread($fp, 8192); 
    1093     } 
    1094     fclose($fp); 
    1095     xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); 
    1096     xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
    1097     xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
    1098     xml_parse_into_struct($parser, trim($contents), $xml_values); 
    1099     xml_parser_free($parser); 
    1100     if(!$xml_values) 
    1101     { 
    1102       return; //Hmm... 
    1103     } 
    1104     $xml_array = array (); 
    1105     $parents = array (); 
    1106     $opened_tags = array (); 
    1107     $arr = array (); 
    1108     $current = & $xml_array; 
    1109     $repeated_tag_index = array ();  
    1110     foreach ($xml_values as $data) 
    1111     { 
    1112       unset ($attributes, $value); 
    1113       extract($data); 
    1114       $result = array (); 
    1115       $attributes_data = array (); 
    1116       if (isset ($value)) 
    1117       { 
    1118         if($priority == 'tag') 
    1119         { 
    1120           $result = $value; 
    1121         } 
    1122         else 
    1123         { 
    1124           $result['value'] = $value; 
    1125         } 
    1126       } 
    1127       if(isset($attributes) and $get_attributes) 
    1128       { 
    1129         foreach($attributes as $attr => $val) 
    1130         { 
    1131           if($priority == 'tag') 
    1132           { 
    1133             $attributes_data[$attr] = $val; 
    1134           } 
    1135           else 
    1136           { 
    1137             $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' 
    1138           } 
    1139         } 
    1140       } 
    1141       if ($type == "open") 
    1142       {  
    1143         $parent[$level -1] = & $current; 
    1144         if(!is_array($current) or (!in_array($tag, array_keys($current)))) 
    1145         { 
    1146           $current[$tag] = $result; 
    1147           if($attributes_data) 
    1148           { 
    1149             $current[$tag . '_attr'] = $attributes_data; 
    1150           } 
    1151           $repeated_tag_index[$tag . '_' . $level] = 1; 
    1152           $current = & $current[$tag]; 
    1153         } 
    1154         else 
    1155         { 
    1156           if (isset ($current[$tag][0])) 
    1157           { 
    1158             $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; 
    1159             $repeated_tag_index[$tag . '_' . $level]++; 
    1160           } 
    1161           else 
    1162           {  
    1163             $current[$tag] = array($current[$tag],$result);  
    1164             $repeated_tag_index[$tag . '_' . $level] = 2; 
    1165             if(isset($current[$tag . '_attr'])) 
    1166             { 
    1167               $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
    1168               unset ($current[$tag . '_attr']); 
    1169             } 
    1170           } 
    1171           $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; 
    1172           $current = & $current[$tag][$last_item_index]; 
    1173         } 
    1174       } 
    1175       else if($type == "complete") 
    1176       { 
    1177         if(!isset ($current[$tag])) 
    1178         { 
    1179           $current[$tag] = $result; 
    1180           $repeated_tag_index[$tag . '_' . $level] = 1; 
    1181           if($priority == 'tag' and $attributes_data) 
    1182           { 
    1183             $current[$tag . '_attr'] = $attributes_data; 
    1184           } 
    1185         } 
    1186         else 
    1187         { 
    1188           if (isset ($current[$tag][0]) and is_array($current[$tag])) 
    1189           { 
    1190             $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; 
    1191             if ($priority == 'tag' and $get_attributes and $attributes_data) 
    1192             { 
    1193               $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; 
    1194             } 
    1195             $repeated_tag_index[$tag . '_' . $level]++; 
    1196           } 
    1197           else 
    1198           { 
    1199             $current[$tag] = array($current[$tag],$result);  
    1200             $repeated_tag_index[$tag . '_' . $level] = 1; 
    1201             if ($priority == 'tag' and $get_attributes) 
    1202             { 
    1203               if (isset ($current[$tag . '_attr'])) 
    1204               {  
    1205                 $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
    1206                 unset ($current[$tag . '_attr']); 
    1207               } 
    1208               if ($attributes_data) 
    1209               { 
    1210                 $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; 
    1211               } 
    1212             } 
    1213             $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken 
    1214           } 
    1215         } 
    1216       } 
    1217       else if($type == 'close') 
    1218       { 
    1219         $current = & $parent[$level -1]; 
    1220       } 
    1221     } 
    1222     return ($xml_array); 
    1223   } 
    1224  
    1225   //This function takes a string and tries to determine if it's a valid mac addess, return FALSE if invalid 
    1226   function mac_check_clean($mac) { 
    1227     if ((strlen($mac) == "17") OR (strlen($mac) == "12")) { 
    1228       //It might be better to use switch here instead of these IF statements... 
    1229      
    1230       //Is the mac separated by colons(:)? 
    1231       if (preg_match("/[0-9a-f][0-9a-f][:-]". 
    1232                   "[0-9a-f][0-9a-f][:-]". 
    1233                   "[0-9a-f][0-9a-f][:-]". 
    1234                   "[0-9a-f][0-9a-f][:-]". 
    1235                   "[0-9a-f][0-9a-f][:-]". 
    1236                   "[0-9a-f][0-9a-f]/i", $mac)) { 
    1237         return(strtoupper(str_replace(":", "", $mac))); 
    1238       //Is the string exactly 12 characters? 
    1239       } elseif(strlen($mac) == "12") { 
    1240         //Now is the string a valid HEX mac address? 
    1241         if (preg_match("/[0-9a-f][0-9a-f]". 
    1242                     "[0-9a-f][0-9a-f]". 
    1243                     "[0-9a-f][0-9a-f]". 
    1244                     "[0-9a-f][0-9a-f]". 
    1245                     "[0-9a-f][0-9a-f]". 
    1246                     "[0-9a-f][0-9a-f]/i", $mac)) { 
    1247           return(strtoupper($mac)); 
    1248         } else { 
    1249           return(FALSE); 
    1250         } 
    1251       //Is the mac separated by whitespaces? 
    1252       } elseif(preg_match("/[0-9a-f][0-9a-f][\s]". 
    1253                 "[0-9a-f][0-9a-f][\s]". 
    1254                 "[0-9a-f][0-9a-f][\s]". 
    1255                 "[0-9a-f][0-9a-f][\s]". 
    1256                 "[0-9a-f][0-9a-f][\s]". 
    1257                 "[0-9a-f][0-9a-f]/i", $mac)) { 
    1258         return(strtoupper(str_replace(" ", "", $mac))); 
    1259       } else { 
    1260         return(FALSE); 
    1261       } 
    1262     } else { 
    1263       return(FALSE); 
    1264     }  
    1265   } 
    1266    
    1267   function validate_netmask($mask) { 
    1268     if (preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/(\d{1,2})$/", $mask)) { 
    1269       return(TRUE); 
    1270     } else { 
    1271       return(FALSE); 
    1272     } 
    1273   } 
    1274  
    1275   //Discover New Device/Hardware 
    1276   function discover_new($netmask, $use_nmap=TRUE) { 
    1277     global $global_cfg, $debug, $error; 
    1278       //Written by tm1000 
    1279       //Version 1.0 
    1280  
    1281       //nmap will actually discover 'unseen' devices that the VoIP server hasn't heard from 
    1282       //If the user just wishes to use the local arp cache they can tell the function to not use nmap 
    1283       //This results in a speed increase from 60 seconds to less than one second. 
    1284       if (($use_nmap) AND ($this->validate_netmask($netmask))) { 
    1285           shell_exec($global_cfg['nmap_location'].' -v -sP '. $netmask); 
    1286       } elseif(!$this->validate_netmask($netmask)) { 
    1287       return(FALSE); 
    1288     } 
    1289         //Get arp list 
    1290         $arp_list = shell_exec($global_cfg['arp_location'] . " -an"); 
    1291       //$arp_list = shell_exec("arp -an"); 
    1292       if($global_cfg['debug']) { 
    1293       $debug .= "ARP LIST:\n".$arp_list; 
    1294     } 
    1295       //Throw arp list into an array, break by new lines 
    1296       $arp_array = explode("\n", $arp_list); 
    1297      
    1298       //Find all references to active computers by searching out mac addresses. 
    1299       $temp = array_values(array_unique(preg_grep("/[0-9a-f][0-9a-f][:-]". 
    1300           "[0-9a-f][0-9a-f][:-]". 
    1301           "[0-9a-f][0-9a-f][:-]". 
    1302           "[0-9a-f][0-9a-f][:-]". 
    1303           "[0-9a-f][0-9a-f][:-]". 
    1304           "[0-9a-f][0-9a-f]/i", $arp_array))); 
    1305      
    1306       if($global_cfg['debug']) { 
    1307       $arp_arry = print_r($temp, true); 
    1308       $debug .= "ARP Array:\n".$arp_arry; 
    1309     } 
    1310       //Go through each row of valid arp entries and pull out the information and add it into a nice array! 
    1311       foreach ($temp as $key => &$value) { 
    1312          
    1313           //Pull out the IP address from row. It's always the first entry in the row and it can only be a max of 15 characters with the delimiters  
    1314           $ip = trim(substr($value, 0, 15)); 
    1315          
    1316           //Pull out the mac address by looking for the delimiter 
    1317           $mac = substr($value, (strpos($value, ":") -2), 17); 
    1318          
    1319           //Get rid of the delimiter 
    1320           $mac_strip = strtoupper(str_replace(":", "", $mac)); 
    1321          
    1322       //arp -n will return a MAC address of 000000000000 if no hardware was found, so we need to ignore it 
    1323       if($mac_strip != "000000000000") { 
    1324             //only use the first 6 characters for the oui: http://en.wikipedia.org/wiki/Organizationally_Unique_Identifier 
    1325             $oui = substr($mac_strip,0,6); 
    1326          
    1327             //Find the matching brand model to the oui 
    1328             $oui_sql = "SELECT endpointman_brand_list.name, endpointman_brand_list.id FROM endpointman_oui_list, endpointman_brand_list WHERE oui LIKE '%". $oui ."%' AND endpointman_brand_list.id = endpointman_oui_list.brand AND endpointman_brand_list.installed = 1 LIMIT 1"; 
    1329  
    1330  
    1331             $brand_result = mysql_query($oui_sql); 
    1332             $brand = mysql_fetch_row($brand_result); 
    1333             if ($brand == FALSE) { 
    1334                 //oui doesn't have a matching mysql reference, probably a PC/router/wap/printer of some sort. 
    1335                 $brand[0] = FALSE; 
    1336           $brand[1] = NULL; 
    1337             } 
    1338          
    1339             //Find out if endpoint has already been configured for this mac address 
    1340             $epm_sql = "SELECT * FROM endpointman_mac_list WHERE mac LIKE  '%". $mac_strip ."%'"; 
    1341             $epm_result = mysql_query($epm_sql); 
    1342             $epm_row = mysql_fetch_row($epm_result); 
    1343             if ($epm_row == TRUE) { 
    1344                 $epm = TRUE; 
    1345             } else { 
    1346                 $epm = FALSE; 
    1347             } 
    1348          
    1349             //Add into a final array 
    1350             $final[$key] = array("ip" => $ip, "mac" => $mac, "mac_strip" => $mac_strip, "oui" => $oui, "brand" => $brand[0], "brand_id" => $brand[1], "endpoint_managed" => $epm); 
    1351         } 
    1352     } 
    1353     $final = array_values($final); 
    1354       if($global_cfg['debug']) { 
    1355       $arp_arry = print_r($final, true); 
    1356       $debug .= "Final output from 'discover_new' function:\n".$arp_arry; 
    1357     } 
    1358     if(!is_array($final)) { 
    1359       return(FALSE); 
    1360     } else { 
    1361         return ($final); 
    1362     } 
    1363   } 
    1364  
    1365   //Function Works 
    1366   function dblogin() {  //Database login 
    1367     global $amp_conf, $db, $endpoint_db, $ep_link; 
    1368  
    1369     /* FUTURE 
    1370     if (is_object($db)) { 
    1371       //I 'think' we are freepbxv2 
    1372       include('databases/freepbxv2.inc'); 
    1373     } else { 
    1374       //Standalone 
    1375       include('databases/mysql.inc'); 
    1376     } 
    1377     */ 
    1378      
    1379     //Re-written to get rid of duplicate references to one database 
    1380  
    1381     //connect to the mysql system 
    1382     $ep_link=mysql_connect('localhost', $amp_conf['AMPDBUSER'], $amp_conf['AMPDBPASS']); 
    1383     if ($ep_link == FALSE) { 
    1384       die("Sorry, We can't connect to your database :-(\nTry checking /etc/amportal.conf for the correct settings"); 
    1385     } 
    1386     //connect to the only useable asterisk database, need to look this up in the amportal.conf really because someone could change it 
    1387     $success = mysql_select_db($amp_conf['AMPDBNAME'],$ep_link); 
    1388     if ($success== FALSE) { 
    1389       die("Sorry, We can't FIND your database, but we COULD connect to MySQL :-/\nDoes Asterisk & FreePBX exist on this server?"); 
    1390     } 
    1391   }  
    1392  
    1393   function modelsAvailable($model=NULL, $macAdd=NULL, $brand=NULL) { 
    1394     if (isset($macAdd)) {    
    1395       $oui=substr($macAdd,0,6); 
    1396     } 
    1397     if ((!isset($oui)) && (!isset($brand)) && (!isset($model))) {    
    1398       $sql="SELECT endpointman_model_list.* FROM endpointman_model_list, endpointman_product_list WHERE endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_model_list.enabled = 1 AND endpointman_product_list.installed = 1"; 
    1399     }elseif(isset($brand)){ 
    1400       $sql="SELECT endpointman_model_list.* FROM endpointman_model_list, endpointman_product_list WHERE endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_model_list.enabled = 1 AND endpointman_product_list.installed = 1 AND endpointman_model_list.brand = " . $brand; 
    1401     } 
    1402     $result1=mysql_query($sql); 
    1403     if(mysql_num_rows($result1)) { 
    1404       $i = 1; 
    1405       while ($row=mysql_fetch_array($result1)) { 
    1406         if ($row['id'] == $model) { 
    1407           $temp[$i]['value'] = $row['id']; 
    1408           $temp[$i]['text'] = $row['model']; 
    1409           $temp[$i]['selected'] = 'selected'; 
    1410         }else{ 
    1411           $temp[$i]['value'] = $row['id']; 
    1412           $temp[$i]['text'] = $row['model']; 
    1413           $temp[$i]['selected'] = 0; 
    1414         } 
    1415         $i++; 
    1416       } 
    1417     } 
    1418     if(!isset($temp)) { 
    1419       global $error, $global_cfg, $debug, $ep_link; 
    1420       if($global_cfg['debug']) { 
    1421         $debug .= "Error in function 'modelsAvailable'\nSQL:". $sql; 
    1422         $debug .= mysql_error($ep_link); 
    1423       } 
    1424       return(FALSE); 
    1425     } else { 
    1426       return($temp); 
    1427     } 
    1428   }  
    1429  
    1430   function displayExtension($ext = NULL) { 
    1431     if(!isset($ext)) { 
    1432       $not_added="SELECT devices.id, devices.description FROM devices WHERE devices.id not in (SELECT devices.id FROM devices, endpointman_mac_list WHERE tech='sip' AND devices.id = endpointman_mac_list.ext )"; 
    1433     } else { 
    1434       $not_added="SELECT devices.id, devices.description FROM devices WHERE devices.id not in (SELECT devices.id FROM devices, endpointman_mac_list WHERE tech='sip' AND devices.id = endpointman_mac_list.ext AND endpointman_mac_list.ext !=".$ext." )"; 
    1435     } 
    1436    
    1437     $result=mysql_query($not_added); 
    1438    
    1439     //$temp[0]['value'] = ""; 
    1440     //$temp[0]['text'] = " --- "; 
    1441     //$temp[0]['selected'] = 1; 
    1442     $i = 1; 
    1443     while ($row=mysql_fetch_array($result)) { 
    1444       $temp[$i]['value'] = $row['id']; 
    1445       $temp[$i]['text'] = $row['id'] . " --- " . $row['description']; 
    1446       if ($row['id'] == $ext) { 
    1447         $temp[$i]['selected'] = "selected"; 
    1448       } 
    1449       $i++; 
    1450     } 
    1451     return($temp); 
    1452  
    1453   }      
    1454  
    1455   function brandAvailable ($selected = NULL) {   
    1456       $i = 1; 
    1457       $sql="SELECT name, id FROM  endpointman_brand_list WHERE installed = 1"; 
    1458       $result=mysql_query($sql); 
    1459       $temp[0]['value'] = ""; 
    1460       $temp[0]['text'] = ""; 
    1461       while ($row=mysql_fetch_array($result)) { 
    1462         $temp[$i]['value'] = $row['id']; 
    1463         $temp[$i]['text'] = $row['name']; 
    1464         if ($row['id'] == $selected) { 
    1465           $temp[$i]['selected'] = TRUE; 
    1466         } else { 
    1467           $temp[$i]['selected'] = NULL; 
    1468         } 
    1469         $i++; 
    1470       } 
    1471       return($temp);   
    1472   } 
    1473  
    1474   function display_templates($product_id,$temp_select = NULL) { 
    1475     $i = 0; 
    1476     $sql="SELECT * FROM  endpointman_product_list WHERE endpointman_product_list.id =".$product_id; 
    1477     $result=mysql_query($sql); 
    1478     $row=mysql_fetch_array($result); 
    1479     $sql="SELECT * FROM  endpointman_template_list WHERE  product_id = '".$row['id']."'"; 
    1480     $result=mysql_query($sql); 
    1481     while ($row=mysql_fetch_array($result)) { 
    1482       $temp[$i]['value'] = $row['id']; 
    1483       $temp[$i]['text'] = $row['name']; 
    1484       if ($row['id'] == $temp_select) { 
    1485         $temp[$i]['selected'] = "selected"; 
    1486       } 
    1487       $i++; 
    1488     } 
    1489     $temp[$i]['value'] = 0; 
    1490     if ($temp_select == 0) { 
    1491       $temp[$i]['text'] = "Custom..."; 
    1492       $temp[$i]['selected'] = "selected"; 
    1493     } else { 
    1494       $temp[$i]['text'] = "Custom..."; 
    1495     } 
    1496     return($temp); 
    1497   } 
    1498  
    1499   function listTZ($selected) { 
    1500     $sql="SELECT tz from endpointman_time_zones"; 
    1501     $result=mysql_query($sql); 
    1502     $i = 0; 
    1503     while ($row=mysql_fetch_array($result)) { 
    1504       if ($row['tz'] == $selected) { 
    1505         $temp[$i]['value'] = $row['tz']; 
    1506         $temp[$i]['text'] = $row['tz']; 
    1507         $temp[$i]['selected'] = 1; 
    1508       }else{ 
    1509         $temp[$i]['value'] = $row['tz']; 
    1510         $temp[$i]['text'] = $row['tz']; 
    1511         $temp[$i]['selected'] = 0;   
    1512       } 
    1513       $i++; 
    1514     } 
    1515     return($temp); 
    1516   } 
    1517  
    1518   function rebootPhone($ext,$datav) {  
    1519     if (is_numeric($ext)) { 
    1520     } elseif ($ext == 'all') { 
    1521       $sql = "SELECT endpointman_mac_list.id , endpointman_brand_list.directory , endpointman_product_list.cfg_dir FROM endpointman_mac_list , endpointman_model_list , endpointman_brand_list, endpointman_product_list WHERE endpointman_mac_list.model = endpointman_model_list.id AND endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_model_list.brand =endpointman_brand_list.id"; 
    1522       $result = mysql_query($sql); 
    1523       while($row = mysql_fetch_assoc($result)) { 
    1524         if(($cfg_dir == $row['cfg_dir']) AND ($directory == $row['directory'])) { 
    1525           $phone_config->reboot($row['id']); 
    1526         } else { 
    1527           if(isset($phone_config)) { 
    1528             unset($phone_config); 
    1529           } 
    1530           $directory = $row['directory']; 
    1531           $cfg_dir = $row['cfg_dir']; 
    1532           require(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    1533           $phone_config = new $row['cfg_dir'](); 
    1534           $phone_config->reboot($row['id']); 
    1535         } 
    1536       }    
    1537     } else { 
    1538       $sql = "SELECT endpointman_brand_list.directory, endpointman_product_list.cfg_dir, endpointman_mac_list.id FROM endpointman_brand_list, endpointman_model_list, endpointman_mac_list, endpointman_product_list WHERE endpointman_brand_list.id = ".$datav['rb_brand']." AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.id = endpointman_mac_list.model AND endpointman_model_list.product_id = endpointman_product_list.id"; 
    1539       $result = mysql_query($sql); 
    1540       while($row = mysql_fetch_assoc($result)) { 
    1541         if(($cfg_dir == $row['cfg_dir']) AND ($directory == $row['directory'])) { 
    1542           $phone_config->reboot($row['id']); 
    1543         } else { 
    1544           if(isset($phone_config)) { 
    1545             unset($phone_config); 
    1546           } 
    1547           $directory = $row['directory']; 
    1548           $cfg_dir = $row['cfg_dir']; 
    1549           require(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
    1550           $phone_config = new $row['cfg_dir'](); 
    1551           $phone_config->reboot($row['id']);         
    1552         } 
    1553       } 
    1554     } 
    1555   } 
     975        */ 
     976 
     977        $ch = curl_init(); 
     978        curl_setopt($ch, CURLOPT_URL, $url_file); 
     979        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'endpointmanager_read_header'); 
     980        curl_setopt($ch, CURLOPT_WRITEFUNCTION, 'endpointmanager_read_body'); 
     981        curl_setopt($ch, CURLOPT_NOPROGRESS, '1'); 
     982        curl_setopt($ch, CURLOPT_TIMEOUT, 0); 
     983 
     984        curl_exec($ch); 
     985        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE); 
     986        if($fout) 
     987            fclose($fout); 
     988        curl_close($ch); 
     989        return ($http_code == 200) ? true : $http_code; 
     990    } 
     991    //end pfSense code 
     992 
     993    //This function looks in common linux directories for system executable files. Like ARP & NMAP 
     994    function find_exec($exec) { 
     995        $usr_bin = glob("/usr/bin/".$exec); 
     996        $usr_sbin = glob("/usr/sbin/".$exec); 
     997        $sbin = glob("/sbin/".$exec); 
     998        $bin = glob("/bin/".$exec); 
     999        $etc = glob("/etc/".$exec); 
     1000        if(isset($usr_bin[0])) { 
     1001            return("/usr/bin/".$exec); 
     1002        } elseif(isset($usr_sbin[0])) { 
     1003            return("/usr/sbin/".$exec); 
     1004        } elseif(isset($sbin[0])) { 
     1005            return("/sbin/".$exec); 
     1006        } elseif(isset($bin[0])) { 
     1007            return("/bin/".$exec); 
     1008        } elseif(isset($etc[0])) { 
     1009            return("/etc/".$exec); 
     1010        } else { 
     1011            return($exec); 
     1012        } 
     1013    } 
     1014 
     1015    //Delete Directory Tree, Doesn't work. though 
     1016    function deltree($dir) { 
     1017        $this->rmrf($dir); 
     1018    } 
     1019 
     1020    function rmrf($dir) { 
     1021        foreach (glob($dir) as $file) { 
     1022            if (is_dir($file)) { 
     1023                $this->rmrf("$file/*"); 
     1024                rmdir($file); 
     1025            } else { 
     1026                unlink($file); 
     1027            } 
     1028        } 
     1029    } 
     1030 
     1031 
     1032    function table_exists ($table) { 
     1033        $sql = "SHOW TABLES FROM asterisk"; 
     1034        $result = mysql_query($sql); 
     1035 
     1036        while ($row = mysql_fetch_row($result)) { 
     1037            if ($row[0] == $table) { 
     1038                return TRUE; 
     1039            } 
     1040        } 
     1041        return FALSE; 
     1042    } 
     1043 
     1044    /** 
     1045     Parse XML file into an array 
     1046     Taken from Sister Project SuperFecta 
     1047     */ 
     1048    function xml2array($url, $get_attributes = 1, $priority = 'tag') { 
     1049        $contents = ""; 
     1050        if (!function_exists('xml_parser_create')) { 
     1051            return array (); 
     1052        } 
     1053        $parser = xml_parser_create(''); 
     1054        if(!($fp = @ fopen($url, 'rb'))) { 
     1055            return array (); 
     1056        } 
     1057        while(!feof($fp)) { 
     1058            $contents .= fread($fp, 8192); 
     1059        } 
     1060        fclose($fp); 
     1061        xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); 
     1062        xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
     1063        xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
     1064        xml_parse_into_struct($parser, trim($contents), $xml_values); 
     1065        xml_parser_free($parser); 
     1066        if(!$xml_values) { 
     1067            return; //Hmm... 
     1068        } 
     1069        $xml_array = array (); 
     1070        $parents = array (); 
     1071        $opened_tags = array (); 
     1072        $arr = array (); 
     1073        $current = & $xml_array; 
     1074        $repeated_tag_index = array (); 
     1075        foreach ($xml_values as $data) { 
     1076            unset ($attributes, $value); 
     1077            extract($data); 
     1078            $result = array (); 
     1079            $attributes_data = array (); 
     1080            if (isset ($value)) { 
     1081                if($priority == 'tag') { 
     1082                    $result = $value; 
     1083                } 
     1084                else { 
     1085                    $result['value'] = $value; 
     1086                } 
     1087            } 
     1088            if(isset($attributes) and $get_attributes) { 
     1089                foreach($attributes as $attr => $val) { 
     1090                    if($priority == 'tag') { 
     1091                        $attributes_data[$attr] = $val; 
     1092                    } 
     1093                    else { 
     1094                        $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' 
     1095                    } 
     1096                } 
     1097            } 
     1098            if ($type == "open") { 
     1099                $parent[$level -1] = & $current; 
     1100                if(!is_array($current) or (!in_array($tag, array_keys($current)))) { 
     1101                    $current[$tag] = $result; 
     1102                    if($attributes_data) { 
     1103                        $current[$tag . '_attr'] = $attributes_data; 
     1104                    } 
     1105                    $repeated_tag_index[$tag . '_' . $level] = 1; 
     1106                    $current = & $current[$tag]; 
     1107                } 
     1108                else { 
     1109                    if (isset ($current[$tag][0])) { 
     1110                        $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; 
     1111                        $repeated_tag_index[$tag . '_' . $level]++; 
     1112                    } 
     1113                    else { 
     1114                        $current[$tag] = array($current[$tag],$result); 
     1115                        $repeated_tag_index[$tag . '_' . $level] = 2; 
     1116                        if(isset($current[$tag . '_attr'])) { 
     1117                            $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
     1118                            unset ($current[$tag . '_attr']); 
     1119                        } 
     1120                    } 
     1121                    $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; 
     1122                    $current = & $current[$tag][$last_item_index]; 
     1123                } 
     1124            } 
     1125            else if($type == "complete") { 
     1126                if(!isset ($current[$tag])) { 
     1127                    $current[$tag] = $result; 
     1128                    $repeated_tag_index[$tag . '_' . $level] = 1; 
     1129                    if($priority == 'tag' and $attributes_data) { 
     1130                        $current[$tag . '_attr'] = $attributes_data; 
     1131                    } 
     1132                } 
     1133                else { 
     1134                    if (isset ($current[$tag][0]) and is_array($current[$tag])) { 
     1135                        $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; 
     1136                        if ($priority == 'tag' and $get_attributes and $attributes_data) { 
     1137                            $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; 
     1138                        } 
     1139                        $repeated_tag_index[$tag . '_' . $level]++; 
     1140                    } 
     1141                    else { 
     1142                        $current[$tag] = array($current[$tag],$result); 
     1143                        $repeated_tag_index[$tag . '_' . $level] = 1; 
     1144                        if ($priority == 'tag' and $get_attributes) { 
     1145                            if (isset ($current[$tag . '_attr'])) { 
     1146                                $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
     1147                                unset ($current[$tag . '_attr']); 
     1148                            } 
     1149                            if ($attributes_data) { 
     1150                                $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; 
     1151                            } 
     1152                        } 
     1153                        $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken 
     1154                    } 
     1155                } 
     1156            } 
     1157            else if($type == 'close') { 
     1158                $current = & $parent[$level -1]; 
     1159            } 
     1160        } 
     1161        return ($xml_array); 
     1162    } 
     1163 
     1164    //This function takes a string and tries to determine if it's a valid mac addess, return FALSE if invalid 
     1165    function mac_check_clean($mac) { 
     1166        if ((strlen($mac) == "17") OR (strlen($mac) == "12")) { 
     1167            //It might be better to use switch here instead of these IF statements... 
     1168 
     1169            //Is the mac separated by colons(:)? 
     1170            if (preg_match("/[0-9a-f][0-9a-f][:-]". 
     1171            "[0-9a-f][0-9a-f][:-]". 
     1172            "[0-9a-f][0-9a-f][:-]". 
     1173            "[0-9a-f][0-9a-f][:-]". 
     1174            "[0-9a-f][0-9a-f][:-]". 
     1175            "[0-9a-f][0-9a-f]/i", $mac)) { 
     1176                return(strtoupper(str_replace(":", "", $mac))); 
     1177                //Is the string exactly 12 characters? 
     1178            } elseif(strlen($mac) == "12") { 
     1179                //Now is the string a valid HEX mac address? 
     1180                if (preg_match("/[0-9a-f][0-9a-f]". 
     1181                "[0-9a-f][0-9a-f]". 
     1182                "[0-9a-f][0-9a-f]". 
     1183                "[0-9a-f][0-9a-f]". 
     1184                "[0-9a-f][0-9a-f]". 
     1185                "[0-9a-f][0-9a-f]/i", $mac)) { 
     1186                    return(strtoupper($mac)); 
     1187                } else { 
     1188                    return(FALSE); 
     1189                } 
     1190                //Is the mac separated by whitespaces? 
     1191            } elseif(preg_match("/[0-9a-f][0-9a-f][\s]". 
     1192            "[0-9a-f][0-9a-f][\s]". 
     1193            "[0-9a-f][0-9a-f][\s]". 
     1194            "[0-9a-f][0-9a-f][\s]". 
     1195            "[0-9a-f][0-9a-f][\s]". 
     1196            "[0-9a-f][0-9a-f]/i", $mac)) { 
     1197                return(strtoupper(str_replace(" ", "", $mac))); 
     1198            } else { 
     1199                return(FALSE); 
     1200            } 
     1201        } else { 
     1202            return(FALSE); 
     1203        } 
     1204    } 
     1205 
     1206    function validate_netmask($mask) { 
     1207        if (preg_match("/^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\/(\d{1,2})$/", $mask)) { 
     1208            return(TRUE); 
     1209        } else { 
     1210            return(FALSE); 
     1211        } 
     1212    } 
     1213 
     1214    //Discover New Device/Hardware 
     1215    function discover_new($netmask, $use_nmap=TRUE) { 
     1216        global $global_cfg, $debug, $error; 
     1217        //Written by tm1000 
     1218        //Version 1.0 
     1219 
     1220        //nmap will actually discover 'unseen' devices that the VoIP server hasn't heard from 
     1221        //If the user just wishes to use the local arp cache they can tell the function to not use nmap 
     1222        //This results in a speed increase from 60 seconds to less than one second. 
     1223        if (($use_nmap) AND ($this->validate_netmask($netmask))) { 
     1224            shell_exec($global_cfg['nmap_location'].' -v -sP '. $netmask); 
     1225        } elseif(!$this->validate_netmask($netmask)) { 
     1226            return(FALSE); 
     1227        } 
     1228        //Get arp list 
     1229        $arp_list = shell_exec($global_cfg['arp_location'] . " -an"); 
     1230        //$arp_list = shell_exec("arp -an"); 
     1231        if($global_cfg['debug']) { 
     1232            $debug .= "ARP LIST:\n".$arp_list; 
     1233        } 
     1234        //Throw arp list into an array, break by new lines 
     1235        $arp_array = explode("\n", $arp_list); 
     1236 
     1237        //Find all references to active computers by searching out mac addresses. 
     1238        $temp = array_values(array_unique(preg_grep("/[0-9a-f][0-9a-f][:-]". 
     1239                "[0-9a-f][0-9a-f][:-]". 
     1240                "[0-9a-f][0-9a-f][:-]". 
     1241                "[0-9a-f][0-9a-f][:-]". 
     1242                "[0-9a-f][0-9a-f][:-]". 
     1243                "[0-9a-f][0-9a-f]/i", $arp_array))); 
     1244 
     1245        if($global_cfg['debug']) { 
     1246            $arp_arry = print_r($temp, true); 
     1247            $debug .= "ARP Array:\n".$arp_arry; 
     1248        } 
     1249        //Go through each row of valid arp entries and pull out the information and add it into a nice array! 
     1250        foreach ($temp as $key => &$value) { 
     1251 
     1252            //Pull out the IP address from row. It's always the first entry in the row and it can only be a max of 15 characters with the delimiters 
     1253            $ip = trim(substr($value, 0, 15)); 
     1254 
     1255            //Pull out the mac address by looking for the delimiter 
     1256            $mac = substr($value, (strpos($value, ":") -2), 17); 
     1257 
     1258            //Get rid of the delimiter 
     1259            $mac_strip = strtoupper(str_replace(":", "", $mac)); 
     1260 
     1261            //arp -n will return a MAC address of 000000000000 if no hardware was found, so we need to ignore it 
     1262            if($mac_strip != "000000000000") { 
     1263                //only use the first 6 characters for the oui: http://en.wikipedia.org/wiki/Organizationally_Unique_Identifier 
     1264                $oui = substr($mac_strip,0,6); 
     1265 
     1266                //Find the matching brand model to the oui 
     1267                $oui_sql = "SELECT endpointman_brand_list.name, endpointman_brand_list.id FROM endpointman_oui_list, endpointman_brand_list WHERE oui LIKE '%". $oui ."%' AND endpointman_brand_list.id = endpointman_oui_list.brand AND endpointman_brand_list.installed = 1 LIMIT 1"; 
     1268 
     1269 
     1270                $brand_result = mysql_query($oui_sql); 
     1271                $brand = mysql_fetch_row($brand_result); 
     1272                if ($brand == FALSE) { 
     1273                    //oui doesn't have a matching mysql reference, probably a PC/router/wap/printer of some sort. 
     1274                    $brand[0] = FALSE; 
     1275                    $brand[1] = NULL; 
     1276                } 
     1277 
     1278                //Find out if endpoint has already been configured for this mac address 
     1279                $epm_sql = "SELECT * FROM endpointman_mac_list WHERE mac LIKE  '%". $mac_strip ."%'"; 
     1280                $epm_result = mysql_query($epm_sql); 
     1281                $epm_row = mysql_fetch_row($epm_result); 
     1282                if ($epm_row == TRUE) { 
     1283                    $epm = TRUE; 
     1284                } else { 
     1285                    $epm = FALSE; 
     1286                } 
     1287 
     1288                //Add into a final array 
     1289                $final[$key] = array("ip" => $ip, "mac" => $mac, "mac_strip" => $mac_strip, "oui" => $oui, "brand" => $brand[0], "brand_id" => $brand[1], "endpoint_managed" => $epm); 
     1290            } 
     1291        } 
     1292        $final = array_values($final); 
     1293        if($global_cfg['debug']) { 
     1294            $arp_arry = print_r($final, true); 
     1295            $debug .= "Final output from 'discover_new' function:\n".$arp_arry; 
     1296        } 
     1297        if(!is_array($final)) { 
     1298            return(FALSE); 
     1299        } else { 
     1300            return ($final); 
     1301        } 
     1302    } 
     1303 
     1304    function modelsAvailable($model=NULL, $macAdd=NULL, $brand=NULL) { 
     1305        if (isset($macAdd)) { 
     1306            $oui=substr($macAdd,0,6); 
     1307        } 
     1308        if ((!isset($oui)) && (!isset($brand)) && (!isset($model))) { 
     1309            $sql="SELECT endpointman_model_list.* FROM endpointman_model_list, endpointman_product_list WHERE endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_model_list.enabled = 1 AND endpointman_product_list.hidden = 0"; 
     1310        }elseif((isset($brand)) && ($brand !=0)) { 
     1311            $sql="SELECT endpointman_model_list.* FROM endpointman_model_list, endpointman_product_list WHERE endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_model_list.enabled = 1 AND endpointman_product_list.hidden = 0 AND endpointman_model_list.brand = " . $brand; 
     1312        } else { 
     1313            $sql="SELECT endpointman_model_list.* FROM endpointman_model_list, endpointman_product_list WHERE endpointman_model_list.product_id = endpointman_product_list.id AND endpointman_model_list.enabled = 1 AND endpointman_product_list.hidden = 0"; 
     1314        } 
     1315 
     1316 
     1317        $result1 =& $this->db->getAll($sql, array(),DB_FETCHMODE_ASSOC); 
     1318 
     1319        $i = 1; 
     1320        foreach($result1 as $row) { 
     1321            if ($row['id'] == $model) { 
     1322                $temp[$i]['value'] = $row['id']; 
     1323                $temp[$i]['text'] = $row['model']; 
     1324                $temp[$i]['selected'] = 'selected'; 
     1325            }else { 
     1326                $temp[$i]['value'] = $row['id']; 
     1327                $temp[$i]['text'] = $row['model']; 
     1328                $temp[$i]['selected'] = 0; 
     1329            } 
     1330            $i++; 
     1331        } 
     1332 
     1333        if(!isset($temp)) { 
     1334            $this->error['modelsAvailable'] = "SQL Statement came out empty!"; 
     1335            return(FALSE); 
     1336        } else { 
     1337            return($temp); 
     1338        } 
     1339    } 
     1340 
     1341    function displayExtension($ext = NULL) { 
     1342        if(!isset($ext)) { 
     1343            $not_added="SELECT devices.id, devices.description FROM devices WHERE devices.id not in (SELECT devices.id FROM devices, endpointman_mac_list WHERE tech='sip' AND devices.id = endpointman_mac_list.ext )"; 
     1344        } else { 
     1345            $not_added="SELECT devices.id, devices.description FROM devices WHERE devices.id not in (SELECT devices.id FROM devices, endpointman_mac_list WHERE tech='sip' AND devices.id = endpointman_mac_list.ext AND endpointman_mac_list.ext !=".$ext." )"; 
     1346        } 
     1347        $result=mysql_query($not_added); 
     1348 
     1349        $result =& $this->db->getAll($not_added,array(), DB_FETCHMODE_ASSOC); 
     1350 
     1351 
     1352        $i = 1; 
     1353        $temp = array(); 
     1354        foreach($result as $row) { 
     1355            $temp[$i]['value'] = $row['id']; 
     1356            $temp[$i]['text'] = $row['id'] . " --- " . $row['description']; 
     1357            if ($row['id'] == $ext) { 
     1358                $temp[$i]['selected'] = "selected"; 
     1359            } 
     1360            $i++; 
     1361        } 
     1362 
     1363        return($temp); 
     1364 
     1365    } 
     1366    /** 
     1367     * Returns list of Brands that are installed and not hidden and that have at least one model enabled under them 
     1368     * @param integer $selected ID Number of the brand that is supposed to be selected in a drop-down list box 
     1369     * @return array Number array used to generate a select box 
     1370     */ 
     1371    function brandAvailable ($selected = NULL) { 
     1372        $sql="SELECT endpointman_brand_list.name, endpointman_brand_list.id FROM  endpointman_brand_list,endpointman_model_list WHERE endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.enabled = 1 AND endpointman_model_list.hidden = 0 AND endpointman_brand_list.installed = 1 AND endpointman_brand_list.hidden = 0"; 
     1373        $data =& $this->db->getAll($sql,array(), DB_FETCHMODE_ASSOC); 
     1374        $temp[0]['value'] = ""; 
     1375        $temp[0]['text'] = ""; 
     1376        $i = 1; 
     1377        foreach($data as $row) { 
     1378            $temp[$i]['value'] = $row['id']; 
     1379            $temp[$i]['text'] = $row['name']; 
     1380            if ($row['id'] == $selected) { 
     1381                $temp[$i]['selected'] = TRUE; 
     1382            } else { 
     1383                $temp[$i]['selected'] = NULL; 
     1384            } 
     1385            $i++; 
     1386        } 
     1387        return($temp); 
     1388    } 
     1389 
     1390    function display_templates($product_id,$temp_select = NULL) { 
     1391        $i = 0; 
     1392        $sql="SELECT id FROM  endpointman_product_list WHERE endpointman_product_list.id ='".$product_id."'"; 
     1393 
     1394        $id =& $this->db->getOne($sql); 
     1395 
     1396        $sql="SELECT * FROM  endpointman_template_list WHERE  product_id = '".$id."'"; 
     1397 
     1398        $data =& $this->db->getAll($sql,array(), DB_FETCHMODE_ASSOC); 
     1399        foreach($data as $row) { 
     1400            $temp[$i]['value'] = $row['id']; 
     1401            $temp[$i]['text'] = $row['name']; 
     1402            if ($row['id'] == $temp_select) { 
     1403                $temp[$i]['selected'] = "selected"; 
     1404            } 
     1405            $i++; 
     1406        } 
     1407        $temp[$i]['value'] = 0; 
     1408        if ($temp_select == 0) { 
     1409            $temp[$i]['text'] = "Custom..."; 
     1410            $temp[$i]['selected'] = "selected"; 
     1411        } else { 
     1412            $temp[$i]['text'] = "Custom..."; 
     1413        } 
     1414 
     1415        return($temp); 
     1416    } 
     1417 
     1418    function listTZ($selected) { 
     1419        $sql="SELECT tz FROM endpointman_time_zones"; 
     1420        $data =& $this->db->getAll($sql,array(), DB_FETCHMODE_ASSOC); 
     1421        $i = 0; 
     1422        foreach($data as $row) { 
     1423            if ($row['tz'] == $selected) { 
     1424                $temp[$i]['value'] = $row['tz']; 
     1425                $temp[$i]['text'] = $row['tz']; 
     1426                $temp[$i]['selected'] = 1; 
     1427            }else { 
     1428                $temp[$i]['value'] = $row['tz']; 
     1429                $temp[$i]['text'] = $row['tz']; 
     1430                $temp[$i]['selected'] = 0; 
     1431            } 
     1432            $i++; 
     1433        } 
     1434        return($temp); 
     1435    } 
     1436 
     1437    function rebootPhone($ext,$datav) { 
     1438        if (is_numeric($ext)) { 
     1439        } elseif ($ext == 'all') { 
     1440            $sql = "SELECT endpointman_mac_list.id , endpointman_brand_list.directory , endpointman_product_list.cfg_dir FROM endpointman_mac_list , endpointman_model_list , endpointman_brand_list, endpointman_product_list WHERE endpointman_mac_list.model = endpointman_model_list.id AND endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_model_list.brand =endpointman_brand_list.id"; 
     1441            $result = mysql_query($sql); 
     1442            while($row = mysql_fetch_assoc($result)) { 
     1443                if(($cfg_dir == $row['cfg_dir']) AND ($directory == $row['directory'])) { 
     1444                    $phone_config->reboot($row['id']); 
     1445                } else { 
     1446                    if(isset($phone_config)) { 
     1447                        unset($phone_config); 
     1448                    } 
     1449                    $directory = $row['directory']; 
     1450                    $cfg_dir = $row['cfg_dir']; 
     1451                    require(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
     1452                    $phone_config = new $row['cfg_dir'](); 
     1453                    $phone_config->reboot($row['id']); 
     1454                } 
     1455            } 
     1456        } else { 
     1457            $sql = "SELECT endpointman_brand_list.directory, endpointman_product_list.cfg_dir, endpointman_mac_list.id FROM endpointman_brand_list, endpointman_model_list, endpointman_mac_list, endpointman_product_list WHERE endpointman_brand_list.id = ".$datav['rb_brand']." AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.id = endpointman_mac_list.model AND endpointman_model_list.product_id = endpointman_product_list.id"; 
     1458            $result = mysql_query($sql); 
     1459            while($row = mysql_fetch_assoc($result)) { 
     1460                if(($cfg_dir == $row['cfg_dir']) AND ($directory == $row['directory'])) { 
     1461                    $phone_config->reboot($row['id']); 
     1462                } else { 
     1463                    if(isset($phone_config)) { 
     1464                        unset($phone_config); 
     1465                    } 
     1466                    $directory = $row['directory']; 
     1467                    $cfg_dir = $row['cfg_dir']; 
     1468                    require(PHONE_MODULES_PATH.$row['directory'].'/'.$row['cfg_dir'].'/functions.inc'); 
     1469                    $phone_config = new $row['cfg_dir'](); 
     1470                    $phone_config->reboot($row['id']); 
     1471                } 
     1472            } 
     1473        } 
     1474    } 
    15561475}  
    15571476 
    15581477function endpointman_update_progress_bar($out) { 
     1478    /* 
    15591479  echo "\n<script type=\"text/javascript\" language=\"javascript\">"; 
    15601480  echo 'document.getElementById("tb").style.width = "'.$out.'%";';   
    15611481  echo "\n</script>"; 
     1482    */ 
     1483    //echo "."; 
    15621484} 
    15631485 
    15641486function endpointmanager_read_header($ch, $string) { 
    1565   global $file_size, $fout; 
    1566   $length = strlen($string); 
    1567   $regs = ""; 
    1568   ereg("(Content-Length:) (.*)", $string, $regs); 
    1569   if($regs[2] <> "") { 
    1570   $file_size = intval($regs[2]); 
    1571   } 
    1572   ob_flush(); 
    1573   return $length; 
     1487    global $file_size, $fout; 
     1488    $length = strlen($string); 
     1489    $regs = ""; 
     1490    //ereg("(Content-Length:) (.*)", $string, $regs); 
     1491    preg_match("/(Content-Length:) (.*)/i", $string, $regs); 
     1492    if((isset($regs[2])) AND ($regs[2] <> "")) { 
     1493        $file_size = intval($regs[2]); 
     1494    } 
     1495    ob_flush(); 
     1496    return $length; 
    15741497} 
    15751498 
    15761499function endpointmanager_read_body($ch, $string) { 
    1577  //Andrew Nagy added the below echo, otherwise the progress bar never updated until the download was complete 
    1578  echo "&nbsp;"; 
    1579  //end Andrew code 
    1580  global $fout, $file_size, $downloaded, $lastseen; 
    1581  $length = strlen($string); 
    1582  $downloaded += intval($length); 
    1583  $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0); 
    1584  $downloadProgress = 100 - $downloadProgress; 
    1585  if($lastseen <> $downloadProgress and $downloadProgress < 101) { 
    1586    endpointman_update_progress_bar($downloadProgress); 
    1587    $lastseen = $downloadProgress; 
    1588 
    1589  if($fout) 
    1590    fwrite($fout, $string); 
    1591  ob_flush(); 
    1592  return $length; 
     1500    //Andrew Nagy added the below echo, otherwise the progress bar never updated until the download was complete 
     1501    //echo "&nbsp;"; 
     1502    //end Andrew code 
     1503    global $fout, $file_size, $downloaded, $lastseen; 
     1504    $length = strlen($string); 
     1505    $downloaded += intval($length); 
     1506    $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0); 
     1507    $downloadProgress = 100 - $downloadProgress; 
     1508    if($lastseen <> $downloadProgress and $downloadProgress < 101) { 
     1509        endpointman_update_progress_bar($downloadProgress); 
     1510        $lastseen = $downloadProgress; 
     1511   
     1512    if($fout) 
     1513        fwrite($fout, $string); 
     1514    ob_flush(); 
     1515    return $length; 
    15931516} 
    15941517?> 
  • contributed_modules/modules/endpointman/includes/installer.php

    r9907 r10198  
    1 <html> 
    2 <head> 
    3   <title>PBX Endpoint Configuration Manager</title>  
    4   <script type="text/javascript" src="/admin/modules/endpointman/templates/javascript/jquery.js"></script>  
    5   <script type="text/javascript" src="/admin/modules/endpointman/templates/javascript/js/jquery-1.3.2.min.js"></script> 
    6 </head> 
    7 <body> 
    8 <table width="10%" id="tb" border="1" cellspacing="0" cellpadding="0" bgcolor="#FF0000"> 
    9   <tr> 
    10     <td>&nbsp;</td> 
    11   </tr> 
    12 </table> 
    13 <div class="demo-container"></div> 
    141<?PHP 
    152/* 
     
    3320http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
    3421*/ 
    35 include 'functions.inc'; 
    36  
    37 if(file_exists("amp.ini")) { 
    38   $outfile="amp.ini"; 
    39   $wfh=fopen($outfile,'r'); 
    40   $contents = fread($wfh, filesize($outfile)); 
    41   fclose($wfh); 
    42   unlink("amp.ini"); 
    43 
    44  
    45 $temp_amp = unserialize(base64_decode($contents)); 
    46 $amp_conf = unserialize(base64_decode($temp_amp['amp_serial'])); 
    47 $data = unserialize(base64_decode($temp_amp['data'])); 
    48 global $amp_conf; 
     22require 'functions.inc'; 
    4923 
    5024global $endpoint; 
     
    5226$endpoint = new endpointmanager(); 
    5327 
    54 if($data['type'] == "product") { 
    55   $endpoint->install_product($data['id']); 
    56 } elseif($data['type'] == "brand") { 
    57   $endpoint->update_brand($data['id']); 
    58 } elseif($data['type'] == "firmware") { 
     28 
     29if($_REQUEST['type'] == "install") { 
     30  $endpoint->update_brand($_REQUEST['brand']); 
     31} elseif($_REQUEST['type'] == "firmware") { 
    5932  $endpoint->install_firmware($data['id']); 
    6033} 
    6134 
    6235?> 
    63 </body> 
    64 </html> 
  • contributed_modules/modules/endpointman/includes/template_manager.inc

    r10052 r10198  
    2020http://www.gnu.org/licenses/old-licenses/gpl-2.0.html 
    2121*/ 
    22 global $debug; 
    23 global $type; 
    2422$message = NULL; 
    2523$no_add = FALSE; 
    2624$default_display = FALSE; 
    27 $sql = "SELECT * FROM endpointman_product_list WHERE id > 0"; 
    28 $result = mysql_query($sql); 
    29 $sql2 = "SELECT * FROM endpointman_mac_list"; 
    30 $result2 = mysql_query($sql2); 
    31 if((!mysql_num_rows($result)) && (!mysql_num_rows($result2))) { 
     25 
     26$product_list = "SELECT * FROM endpointman_product_list WHERE id > 0"; 
     27$product_list =& $db->getAll($product_list,array(), DB_FETCHMODE_ASSOC); 
     28 
     29$mac_list = "SELECT * FROM endpointman_mac_list"; 
     30$mac_list =& $db->getAll($mac_list, array(), DB_FETCHMODE_ASSOC); 
     31 
     32if((!$product_list) && (!$mac_list)) { 
    3233  $message = _("Welcome to Endpoint Manager")."!<br />"._("You have no products (Modules) installed, click")." <a href=\"config.php?display=epm_config&type=$type\">"._("here")."</a> "._("to install some"); 
    3334  $no_add = TRUE; 
    34 } elseif(!mysql_num_rows($result)) { 
     35} elseif(!$product_list) { 
    3536  $message = "Thanks for upgrading to version 2.0! Please head on over to <a href=\"config.php?display=epm_config&type=$type\">\"Brand Configurations/Setup\"</a> to setup and install phone configurations"; 
    3637  $no_add = TRUE; 
     
    4041  $endpoint->edit_template_display($_REQUEST['id'],$_REQUEST['custom']); 
    4142} elseif(isset($_REQUEST['button_save'])) { 
    42   $sql = "INSERT INTO endpointman_template_list (product_id, name) VALUES ('".$_REQUEST['model_class']."','".$_REQUEST['template_name']."')"; 
    43   mysql_query($sql); 
    44   $sql = "SELECT id FROM endpointman_template_list WHERE product_id = '".$_REQUEST['model_class']."' AND name = '".$_REQUEST['template_name']."'"; 
    45   $result = mysql_query($sql); 
    46   $row = mysql_fetch_assoc($result); 
    47   $endpoint->edit_template_display($row['id'],0); 
     43        if(($_REQUEST['model_class'] == 0) OR (empty($_REQUEST['template_name'])) OR ($_REQUEST['model_clone'] == 0)) { 
     44            $message = "No Valid Model/Template/Name Selected!"; 
     45            $default_display = TRUE; 
     46        } else { 
     47            $sql = "INSERT INTO endpointman_template_list (product_id, name, model_id) VALUES ('".$_REQUEST['model_class']."','".$_REQUEST['template_name']."','".$_REQUEST['model_clone']."')"; 
     48            $endpoint->db->query($sql); 
     49            $sql = "SELECT id FROM endpointman_template_list WHERE product_id = '".$_REQUEST['model_class']."' AND name = '".$_REQUEST['template_name']."'"; 
     50            $id = $endpoint->db->getOne($sql); 
     51            $endpoint->edit_template_display($id,0); 
     52        } 
    4853} elseif(isset($_REQUEST['delete_template'])) { 
    4954  $sql = "DELETE FROM endpointman_template_list WHERE id = ". $_REQUEST['id']; 
    50   mysql_query($sql); 
     55  $endpoint->db->query($sql); 
     56        $sql = "UPDATE endpointman_mac_list SET custom_cfg_template = 0 WHERE custom_cfg_template = ".$_REQUEST['id']; 
     57  $endpoint->db->query($sql); 
    5158  $default_display = TRUE; 
    5259  $message = _('Deleted'); 
     
    6067 
    6168if($default_display) { 
    62   $sql = 'SELECT endpointman_template_list.*, endpointman_product_list.long_name as model_class FROM endpointman_template_list, endpointman_product_list WHERE endpointman_template_list.product_id = endpointman_product_list.id'; 
    63  $result = mysql_query($sql); 
     69  $sql = 'SELECT endpointman_template_list.*, endpointman_product_list.short_name as model_class, endpointman_model_list.model as model_clone FROM endpointman_template_list, endpointman_model_list, endpointman_product_list WHERE endpointman_model_list.enabled = 1 AND endpointman_model_list.hidden = 0 AND endpointman_template_list.model_id = endpointman_model_list.id AND endpointman_template_list.product_id = endpointman_product_list.id'; 
     70        $template_list =& $db->getAll($sql, array(), DB_FETCHMODE_ASSOC); 
    6471  $i = 0; 
    65  while($row = mysql_fetch_assoc($result)) { 
    66    $row_out[$i] = $row; 
     72        foreach($template_list as $row) { 
     73                $row_out[$i] = $row; 
    6774    $row_out[$i]['custom'] = 0; 
    6875    $i++; 
    6976  } 
    7077 
    71   $sql = 'SELECT endpointman_mac_list.id,endpointman_mac_list.mac,endpointman_mac_list.model,endpointman_mac_list.description,endpointman_product_list.long_name as model_class FROM endpointman_mac_list, endpointman_model_list, endpointman_product_list WHERE endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_mac_list.custom_cfg_data IS NOT NULL AND endpointman_model_list.id = endpointman_mac_list.model AND endpointman_mac_list.custom_cfg_template = 0'; 
    72  $result = mysql_query($sql); 
    73  while($row = mysql_fetch_assoc($result)) { 
     78  $sql = 'SELECT endpointman_mac_list.id,endpointman_mac_list.mac,endpointman_mac_list.model,endpointman_mac_list.description, endpointman_model_list.model as model_clone, endpointman_product_list.short_name as model_class FROM endpointman_mac_list, endpointman_model_list, endpointman_product_list WHERE endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_mac_list.custom_cfg_data IS NOT NULL AND endpointman_model_list.id = endpointman_mac_list.model AND endpointman_mac_list.custom_cfg_template = 0'; 
     79        $template_list =& $db->getAll($sql, array(), DB_FETCHMODE_ASSOC); 
     80        foreach($template_list as $row) { 
    7481    $row_out[$i] = $row; 
    7582    $row_out[$i]['custom'] = 1; 
     
    7885  } 
    7986 
    80   $sql = "SELECT * FROM  endpointman_product_list WHERE installed = 1 AND cfg_dir !=  ''"; 
    81   $result = mysql_query($sql); 
    82   $i = 0; 
    83   while($row = mysql_fetch_assoc($result)) { 
     87  $sql = "SELECT endpointman_product_list.* FROM endpointman_product_list, endpointman_model_list WHERE endpointman_product_list.id = endpointman_model_list.product_id AND endpointman_model_list.hidden = 0 AND endpointman_model_list.enabled = 1 AND endpointman_product_list.hidden != 1 AND endpointman_product_list.cfg_dir !=  ''"; 
     88        $template_list =& $db->getAll($sql, array(), DB_FETCHMODE_ASSOC); 
     89  $i = 1; 
     90        $class_row[0]['value'] = 0; 
     91  $class_row[0]['text'] = ""; 
     92        foreach($template_list as $row) { 
    8493    $class_row[$i]['value'] = $row['id']; 
    85     $class_row[$i]['text'] = $row['long_name']; 
     94    $class_row[$i]['text'] = $row['short_name']; 
    8695    $i++; 
    8796  } 
    8897  $tpl = new RainTPL( LOCAL_PATH.'templates' ); 
     98        $tpl->assign("amp_conf_serial", "1"); 
    8999  $tpl->assign("templates_list", $row_out); 
    90100  $tpl->assign("class_list", $class_row); 
     
    92102  $tpl->assign("debug", $debug); 
    93103  $tpl->assign("message", $message); 
    94   $tpl->assign("web_var", "?type=$type"); 
    95104  echo $tpl->draw( 'template_manager' ); 
    96105} 
  • contributed_modules/modules/endpointman/templates/advanced_settings_settings.html

    r10052 r10198  
     1<h3>{$message}</h3> 
    12<form action='config.php{$web_vars}&display=epm_advanced&subpage=settings' method='POST'> 
    23<table width='90%' align='center'> 
  • contributed_modules/modules/endpointman/templates/advanced_subheader.html

    r10052 r10198  
    44<td align='center'><h4><a href='{$web_vars}&display=epm_advanced&subpage=oui_manager'><?=_('OUI Manager')?></a></h4></td> 
    55<td align='center'><h4><a href='{$web_vars}&display=epm_advanced&subpage=poce'><?=_('Product Options/Configuration Editor')?></a></h4></td> 
    6 <td align='center'><h4><a href='{$web_vars}&display=epm_advanced&subpage=iedl'><?=_('Import/Export Devices My Devices List')?></a></h4></td> 
     6</tr> 
     7<tr> 
     8    <td align='center'><h4><a href='{$web_vars}&display=epm_advanced&subpage=iedl'><?=_('Import/Export Devices My Devices List')?></a></h4></td> 
     9<td align='center'><h4><a href='{$web_vars}&display=epm_advanced&subpage=sh_manager'><?=_('Manual Endpoint Modules Upload')?></a></h4></td> 
    710<td align='center'><h4><a href='{$web_vars}&display=epm_advanced&subpage=sh_manager'><?=_('Show/Hide Brands/Models')?></a></h4></td> 
    811</tr> 
  • contributed_modules/modules/endpointman/templates/brand_model_manager.html

    r10052 r10198  
    1919--> 
    2020</style> 
    21 <h1 align='center'><font color='red'>{$error}</font></h1
     21<h3 align='center'><font color='red'>{$error}</font></h3
    2222<center> 
    2323<form action='config.php{$web_var}&display=epm_config' method='POST'> 
     
    2929 
    3030{if condition="isset($installer_address)"} 
    31 <?=_('Please wait for the progress bar to show below (this might take a minute or two)')?> 
    32 <iframe src="http://{$installer_address}" frameborder=0 width="100%" height=70 scrolling="no"></iframe> 
     31<script> 
     32function process_module_actions(actions) { 
     33  freepbx_modal_show('moduleBox'); 
     34  urlStr = "config.php?type=tool&amp;display=epm_config&amp;quietmode=1&amp;handler=file&amp;file=installer.html.php&amp;module=endpointman&amp;type=install&amp;brand={$brand}"; 
     35  urlStr += "&amp;rand="+Math.random ( ); 
     36  for (var i in actions) { 
     37    urlStr += "&amp;moduleaction["+i+"]="+actions[i]; 
     38  } 
     39  $('#moduleBox').html('<iframe src="'+urlStr+'"></iframe><a href="#" onclick="close_module_actions(1)">Return</a>'); 
     40
     41function close_module_actions(goback) { 
     42  //freepbx_modal_close('moduleBox'); 
     43  freepbx_modal_hide('moduleBox'); 
     44  if (goback) { 
     45      location.href = 'config.php?display=epm_config&amp;type=tool'; 
     46  } 
     47
     48process_module_actions(); 
     49</script> 
     50 
     51<div id="moduleBox" style="display:none;"></div>  
     52 
    3353{/if} 
    34  
    3554 
    3655{loop name="brand2_list"} 
     
    4463  <table width="100%" class="product" border="0" cellspacing="0" cellpadding="0"> 
    4564      <tr> 
    46         <td colspan="2">{$value.long_name} {if condition="$value.cfg_ver != ''"}(<?=_('Version')?> [{$value.cfg_ver}]){/if} <form action='config.php{$web_var}&display=epm_config' method='POST'><input type="hidden" name="product" value="{$value.id}"><input type="submit" name="button_{if condition="$value.installed == '1'"}uninstall{else}install{/if}" class="button_{if condition="$value.installed == '1'"}Disable{else}Enable{/if}" value="{if condition="$value.installed == '1'"}<?=_('Uninstall')?>{else}<?=_('Install')?>{/if}">{if condition="array_key_exists('update',$value)"}{if condition="$value.update == 1"}<input type="submit" name="button_update" class="button_update" value="Update">{/if}{/if}{if condition="$value.fw_type == 'install'"}<input type="submit" name="button_install_firmware" class="button_update" value="<?=_('Install Firmware')?>">{elseif condition="$value.fw_type == 'remove'"}<input type="submit" name="button_remove_firmware" class="button_update" value="<?=_('Remove Firmware')?>">{/if}{if condition="array_key_exists('update_fw',$value)"}{if condition="$value.update_fw == 1"}<input type="submit" name="button_update_firmware" class="button_update" value="<?=_('Update Firmware')?>">{/if}{/if}</form>{if condition="array_key_exists('update',$value)"}{if condition="$value.update == 1"}New Version: <i>{$value.update_vers}</i> <br/>Changes: <i>{$value.update_desc}</i>{/if}{/if}</td> 
     65        <td colspan="2">{$value.long_name} {if condition="$value.cfg_ver != ''"}(<?=_('Version')?> [{$value.cfg_ver}]){/if} <form action='config.php{$web_var}&display=epm_config' method='POST'><input type="hidden" name="product" value="{$value.id}">{if condition="array_key_exists('update',$value)"}{if condition="$value.update == 1"}<input type="submit" name="button_update" class="button_update" value="Update">{/if}{/if}{if condition="$value.fw_type == 'install'"}<input type="submit" name="button_install_firmware" class="button_update" value="<?=_('Install Firmware')?>">{elseif condition="$value.fw_type == 'remove'"}<input type="submit" name="button_remove_firmware" class="button_update" value="<?=_('Remove Firmware')?>">{/if}{if condition="array_key_exists('update_fw',$value)"}{if condition="$value.update_fw == 1"}<input type="submit" name="button_update_firmware" class="button_update" value="<?=_('Update Firmware')?>">{/if}{/if}</form>{if condition="array_key_exists('update',$value)"}{if condition="$value.update == 1"}New Version: <i>{$value.update_vers}</i> <br/>Changes: <i>{$value.update_desc}</i>{/if}{/if}</td> 
    4766      </tr> 
    4867      <tr> 
  • contributed_modules/modules/endpointman/templates/devices_manager.html

    r10052 r10198  
    179179  <td width="19%" align='center'>{$value.ext} - {$value.description}</td> 
    180180  <td align='center' width='18%'>{$value.template_name}</td> 
    181   <td align='center' width='9%'><a href={$web_vars}&editready=true&display=epm_devices&id={$value.id}><img src='{$web_path}templates/images/edit.gif' ALT='<?=_('Edit')?>' border='0'></a></td> 
    182   <td align='center' width='5%'><a href={$web_vars}&deleteready=true&display=epm_devices&id={$value.id}><img src='{$web_path}templates/images/delete.gif' ALT='<?=_('Delete')?>' border='0'></a></td> 
     181  <td align='center' width='9%'><a href=config.php?type=tool&display=epm_devices&editready=true&display=epm_devices&id={$value.id}><img src='{$web_path}templates/images/edit.gif' ALT='<?=_('Edit')?>' border='0'></a></td> 
     182  <td align='center' width='5%'><a href=config.php?type=tool&display=epm_devices&deleteready=true&display=epm_devices&id={$value.id}><img src='{$web_path}templates/images/delete.gif' ALT='<?=_('Delete')?>' border='0'></a></td> 
    183183  </tr> 
    184184  {/loop} 
  • contributed_modules/modules/endpointman/templates/global_header.html

    r10031 r10198  
    1818    <!-- End JavaScript --> 
    1919  {/if} 
     20                <!-- config.php?type=tool&amp;display=epm_config&amp;quietmode=1&amp;handler=file&amp;module=endpointman&amp;file=ajax_select.php?amp_conf --> 
     21 
     22                 
     23 
    2024  {if condition="isset($amp_conf_serial)"} 
    2125    <script type="text/javascript" charset="utf-8">  
    2226    $(function(){ 
    2327      $("select#brand_edit").change(function(){ 
    24         $.getJSON("<?=WEB_PATH?>includes/ajax_select.php?amp_conf={$amp_conf_serial}&type=model",{id: $(this).val()}, function(j){ 
     28        $.getJSON("config.php?type=tool&quietmode=1&handler=file&module=endpointman&file=ajax_select.html.php&atype=model",{id: $(this).val()}, function(j){ 
    2529          var options = ''; 
    2630          for (var i = 0; i < j.length; i++) { 
     
    3640    $(function(){ 
    3741      $("select#product_select").change(function(){ 
    38         $.getJSON("<?=WEB_PATH?>includes/ajax_select.php?amp_conf={$amp_conf_serial}&type=template",{id: $(this).val()}, function(j){ 
     42        $.getJSON("config.php?type=tool&quietmode=1&handler=file&module=endpointman&file=ajax_select.html.php&atype=template",{id: $(this).val()}, function(j){ 
    3943          var options = ''; 
    4044          for (var i = 0; i < j.length; i++) { 
     
    4852    $(function(){ 
    4953      $("select#model_new").change(function(){ 
    50         $.getJSON("<?=WEB_PATH?>includes/ajax_select.php?amp_conf={$amp_conf_serial}&type=template2",{id: $(this).val()}, function(j){ 
     54        $.getJSON("config.php?type=tool&quietmode=1&handler=file&module=endpointman&file=ajax_select.html.php&atype=template2",{id: $(this).val()}, function(j){ 
    5155          var options = ''; 
    5256          for (var i = 0; i < j.length; i++) { 
     
    6064    $(function(){ 
    6165      $("select#brand_list_selected").change(function(){ 
    62         $.getJSON("<?=WEB_PATH?>includes/ajax_select.php?amp_conf={$amp_conf_serial}&type=model",{id: $(this).val()}, function(j){ 
     66        $.getJSON("config.php?type=tool&quietmode=1&handler=file&module=endpointman&file=ajax_select.html.php&atype=model",{id: $(this).val()}, function(j){ 
    6367          var options = ''; 
    6468          for (var i = 0; i < j.length; i++) { 
     
    7074      })       
    7175    }) 
     76                $(function(){ 
     77                        $("select#model_class").change(function(){ 
     78                                $.getJSON("config.php?type=tool&quietmode=1&handler=file&module=endpointman&file=ajax_select.html.php&atype=model_clone",{id: $(this).val()}, function(j){ 
     79                                        var options = ''; 
     80                                        for (var i = 0; i < j.length; i++) { 
     81                                                options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>'; 
     82                                        } 
     83                                        $("#model_clone").html(options); 
     84                                        $('#model_clone option:first').attr('selected', 'selected'); 
     85                                }) 
     86                        }) 
     87                }) 
    7288    </script> 
    7389  {/if} 
  • contributed_modules/modules/endpointman/templates/template_editor.html

    r10052 r10198  
    11{if condition="isset($in_ari)"} 
    2  <html> 
    3  <head> 
    4     <link href="/recordings/theme/coda-slider-2.0a.css" rel="stylesheet" type="text/css" />  
     2<html> 
     3    <head> 
     4        <link href="/recordings/theme/coda-slider-2.0a.css" rel="stylesheet" type="text/css" /> 
    55 
    66 
    7     <!-- Begin JavaScript -->  
    8       <script type="text/javascript" src="/recordings/theme/js/jquery.easing.1.3.js"></script>  
    9       <script type="text/javascript" src="/recordings/theme/js/jquery.coda-slider-2.0.js"></script>  
    10        <script type="text/javascript">  
    11              $().ready(function() { 
    12                $('#coda-slider-9').codaSlider({ 
    13            dynamicArrows: false 
    14          }); 
    15              }); 
    16            </script>  
    17    <!-- End JavaScript --> 
    18  </head> 
    19  <body> 
    20 {else} 
    21  <?=_('You are currently editing')?> 
     7        <!-- Begin JavaScript --> 
     8        <script type="text/javascript" src="/recordings/theme/js/jquery.easing.1.3.js"></script> 
     9        <script type="text/javascript" src="/recordings/theme/js/jquery.coda-slider-2.0.js"></script> 
     10        <script type="text/javascript"> 
     11            $().ready(function() { 
     12                $('#coda-slider-9').codaSlider({ 
     13                    dynamicArrows: false 
     14                }); 
     15            }); 
     16        </script> 
     17        <!-- End JavaScript --> 
     18    </head> 
     19    <body> 
     20        {else} 
     21        <?=_('You are currently editing')?> 
    2222  {if condition="$custom != 0"} 
    23  <?=_('a custom config template for extension')?> {$ext}, <?=_('Model')?>: {$model} 
     23        <?=_('a custom config template for extension')?> {$ext}, <?=_('Model')?>: {$model} 
    2424  {/if} 
    2525  {if condition="$custom == 0"} 
    26  <?=_('the template named')?> "{$template_name}" <?=_('for Model')?>: {$model} 
     26        <?=_('the template named')?> "{$template_name}" <?=_('for Model')?>: {$model} 
    2727  {/if} 
    28  <br /> 
    29  <form action="config.php{$web_vars}&display=epm_templates" method="post"> 
     28        <br /> 
     29        <form action="config.php?display=epm_templates" method="post"> 
    3030  {if condition="$alt != 0"} 
    31  <br/> 
    32  <br /> 
    33  <?=_('Select Alternative File Configurations:')?> 
     31            <br/> 
     32            <br /> 
     33            <?=_('Select Alternative File Configurations:')?> 
    3434  {$alt_configs} 
    35  <br/> 
     35            <br/> 
    3636  {/if} 
    37  <br /> 
    38  <?=_('You can also use certain variables in your configs')?>:<br />{noparse} 
     37            <br /> 
     38            <?=_('You can also use certain variables in your configs')?>:<br />{noparse} 
    3939  -"{$srvip}" = <?=_('Server IP')?><br /> 
    4040  -"{$mac}" = <?=_('Device\'s Mac Address')?><br /> 
    4141  -"{$ext}" = <?=_('Device\'s Default Extension (Line 1 or Master)')?><br /> 
    4242  -"{$diplayname}" = <?=_('Device\'s Description in FreePBX (Usually the Full Name)')?><br />{/noparse} 
    43  <br /> 
    44 {/if} 
     43            <br /> 
     44            {/if} 
    4545 
    46 <div class="coda-slider-wrapper">  
    47     <div class="coda-slider preload" id="coda-slider-9"> 
     46            <div class="coda-slider-wrapper"> 
     47                <div class="coda-slider preload" id="coda-slider-9"> 
    4848    {loop name="template_editor"}  
    49      <div class="panel"> 
    50         <div class="panel-wrapper">  
    51                <h2 class="title">{$value.title}</h2> 
    52          <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
     49                    <div class="panel"> 
     50                        <div class="panel-wrapper"> 
     51                            <h2 class="title">{$value.title}</h2> 
     52                            <table width="100%" border="0" cellspacing="0" cellpadding="0"> 
    5353          {loop name="value.data"} 
    54            <tr> 
    55              <td nowrap> 
     54                                <tr> 
     55                                    <td nowrap> 
    5656            {if condition="$value.type == 'input'"} 
    57              <label>{$value.description}: <input type='text' name='{$value.key}' value='{$value.value}' size="40"></label> 
     57                                        <label>{$value.description}: <input type='text' name='{$value.key}' value='{$value.value}' size="40"></label> 
    5858            {elseif condition="$value.type == 'radio'"} 
    5959              {$value.description} 
    6060              {loop name="value.data"} 
    61                <label>{$value.description}: <input type='radio' name='{$value.key}' value='{$value.value}' {if condition="array_key_exists('checked',$value)"}{$value.checked}{/if}></label> 
     61                                        <label>{$value.description}: <input type='radio' name='{$value.key}' value='{$value.value}' {if condition="array_key_exists('checked',$value)"}{$value.checked}{/if}></label> 
    6262              {/loop} 
    6363            {elseif condition="$value.type == 'list'"} 
    6464              {$value.description} <select name='{$value.key}'> 
    6565              {loop name="value.data"} 
    66                <option value='{$value.value}' {if condition="array_key_exists('selected',$value)"}{$value.selected}{/if}>{$value.description}</option> 
     66                                            <option value='{$value.value}' {if condition="array_key_exists('selected',$value)"}{$value.selected}{/if}>{$value.description}</option> 
    6767              {/loop} 
    68              </select> 
     68                                        </select> 
    6969            {/if} 
    70            </td><td width="90%"> 
     70                                    </td><td width="90%"> 
    7171            {if condition="isset($value.aried)"} 
    72              <label><input type='checkbox' name='ari_{$value.ari.key}' {$value.ari.checked}><?=_('End User Editable (Through ARI Module)')?></label> 
     72                                        <label><input type='checkbox' name='ari_{$value.ari.key}' {if condition="isset($value.ari.checked)"}{$value.ari.checked}{/if}><?=_('End User Editable (Through ARI Module)')?></label> 
    7373            {/if} 
    74            </td> 
    75            </tr> 
     74                                    </td> 
     75                                </tr> 
    7676            {if condition="$value.type == 'break'"} 
    77              <tr> 
    78                <td>&nbsp;</td> 
    79                <td>&nbsp;</td> 
    80              </tr> 
     77                                <tr> 
     78                                    <td>&nbsp;</td> 
     79                                    <td>&nbsp;</td> 
     80                                </tr> 
    8181            {/if} 
    8282          {/loop} 
    83          </table> 
    84         </div>  
    85       </div>  
     83                            </table> 
     84                        </div> 
     85                    </div> 
    8686    {/loop} 
    87   </div><!-- .coda-slider -->  
    88 </div><!-- .coda-slider-wrapper --> 
    89 <input name="id" type="hidden" value="{$hidden_id}"> 
    90 <input name="custom" type="hidden" value="{$hidden_custom}"> 
    91 {if condition="!isset($in_ari)"} 
    92  <input type="submit" name="button_save_template" value="<?=_('Save Template');?>"> 
    93  </form> 
    94 {/if} 
     87                </div><!-- .coda-slider --> 
     88            </div><!-- .coda-slider-wrapper --> 
     89            <input name="id" type="hidden" value="{$hidden_id}"> 
     90            <input name="custom" type="hidden" value="{$hidden_custom}"> 
     91            {if condition="!isset($in_ari)"} 
     92            <input type="submit" name="button_save_template" value="<?=_('Save Template');?>"> 
     93        </form> 
     94        {/if} 
  • contributed_modules/modules/endpointman/templates/template_manager.html

    r10052 r10198  
    33{if condition="$no_add == FALSE"} 
    44<center> 
    5   <h3><?=_('Add New Template')?>:</h3><form action="config.php{$web_vars}&Submit=EditTemplate&display=epm_templates" method="POST"> 
     5  <h3><?=_('Add New Template')?>:</h3><form action="config.php?display=epm_templates&Submit=EditTemplate" method="POST"> 
    66    <?=_('Template Name')?>: 
    77    <label> 
    8       <input type="text" name="template_name" id="textfield"> 
     8      <input type="text" name="template_name" id="template_name"> 
    99    <input type="hidden" name="page" value="template_manager"> 
    1010    </label> 
    1111  <?=_('Product Select')?>:  
    1212  <label> 
    13     <select name="model_class" id="select"> 
     13    <select name="model_class" id="model_class"> 
    1414    {loop name="class_list"} 
    1515      <option value="{$value.value}">{$value.text}</option> 
    1616    {/loop} 
     17    </select> 
     18  </label> 
     19        Clone Template From: 
     20        <label> 
     21    <select name="model_clone" id="model_clone"> 
    1722    </select> 
    1823  </label> 
     
    2126  </label> 
    2227  </form> 
    23 <table width="50%" border="0" cellspacing="0" cellpadding="0"> 
     28<table width="80%" border="0" cellspacing="0" cellpadding="0"> 
    2429  <tr> 
    2530    <td><h3><?=_('Template Name')?></h3></td> 
    2631    <td><h3><?=_('Model Classification')?></h3></td> 
     32    <td><h3><?=_('Model Clone')?></h3></td> 
    2733    <td><h3><?=_('Edit')?></h3></td> 
    2834    <td><h3><?=_('Delete')?></h3></td> 
     
    3238    <td>{$value.name}</td> 
    3339    <td>{$value.model_class}</td> 
    34   <td align='center' width='9%'><a href={$web_vars}&edit_template=true&display=epm_templates&custom={$value.custom}&id={$value.id}><img src='<?=WEB_PATH?>templates/images/edit.gif' ALT='<?=_('Edit')?>' border='0'></a></td> 
     40    <td>{$value.model_clone}</td> 
     41 
     42  <td align='center' width='9%'><a href="config.php?edit_template=true&display=epm_templates&custom={$value.custom}&id={$value.id}"><img src='<?=WEB_PATH?>templates/images/edit.gif' ALT='<?=_('Edit')?>' border='0'></a></td> 
    3543  <td align='center' width='5%'>{if condition="$value.custom == 0"} 
    36   <a href={$web_vars}&delete_template=true&display=epm_templates&id={$value.id}><img src='<?=WEB_PATH?>templates/images/delete.gif' ALT='<?=_('Delete')?>' border='0'></a>{/if}</td> 
     44  <a href="config.php?delete_template=true&display=epm_templates&id={$value.id}"><img src='<?=WEB_PATH?>templates/images/delete.gif' ALT='<?=_('Delete')?>' border='0'></a>{/if}</td> 
    3745  </tr> 
    3846{/loop}