root/contributed_modules/modules/endpointman/includes/functions.inc

Revision 10220, 73.9 kB (checked in by tm1000, 3 years ago)

-Errors after 'check for updates'

-Now Return is in the right place
-tftpboot won't have any files until after you generate a phone configuration. I don't even put polycom defaults in there until the first phone is generated. Perhaps we can discuss if you think this is needed
-array_key_exists() errors are now gone
-Undefined variable custom_cfg_data is now gone
-Fixed Polycom config files to not reference polycom folders.

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