Changeset 13276

Show
Ignore:
Timestamp:
01/30/12 13:55:32 (1 year ago)
Author:
tm1000
Message:

Bring Endpoint Manager version up to 2.10.2.4. Fixes #5486

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • modules/branches/2.10/endpointman/ari/modules/phonesettings.module

    r12954 r13276  
    7272 
    7373        $sql = "SELECT mac_id, line FROM endpointman_line_list WHERE ext = '".$_SESSION['ari_user']['extension']."' "; 
    74         $mac_row =& $endpoint->db->getRow($sql, array(), DB_FETCHMODE_ASSOC); 
     74        $mac_row =& $endpoint->eda->sql($sql, 'getrow', DB_FETCHMODE_ASSOC); 
    7575 
    7676        $mac_id = $mac_row['mac_id']; 
     
    109109 
    110110        $sql = "UPDATE endpointman_mac_list SET global_user_cfg_data = '".$custom_cfg_data."' WHERE id = ". $mac_id; 
    111         $endpoint->db->query($sql); 
     111        $endpoint->eda->sql($sql); 
    112112 
    113113        $reboot = isset($_REQUEST['epm_reboot'])?$_REQUEST['epm_reboot']:null; 
     
    149149            die('No Database?'); 
    150150        } 
    151  
     151         
    152152  global $amp_conf; 
    153153 
     
    156156 
    157157        $endpoint = new endpointmanager(); 
    158  
     158         
    159159        $sql = "SELECT mac_id, line FROM endpointman_line_list WHERE ext = '".$_SESSION['ari_user']['extension']."' "; 
    160160 
    161         $res =& $endpoint->db->query($sql); 
     161        $res =& $endpoint->eda->sql($sql); 
    162162        if($res->numRows()) { 
    163163 
    164             $mac_row =& $endpoint->db->getRow($sql, array(), DB_FETCHMODE_ASSOC); 
     164            $mac_row =& $endpoint->eda->sql($sql, 'getrow', DB_FETCHMODE_ASSOC); 
    165165 
    166166            $mac_id = $mac_row['mac_id']; 
  • modules/branches/2.10/endpointman/functions.inc.php

    r12912 r13276  
    150150                        } 
    151151                    } 
    152                 } else { 
    153                     //Mac not set so delete 
    154                     $sql = "SELECT mac_id,luid FROM endpointman_line_list WHERE ext = ". $extdisplay; 
    155                     $macid = $endpoint->eda->sql($sql,'getRow',DB_FETCHMODE_ASSOC); 
    156                     if($macid) { 
    157                         //$endpoint->delete_line($macid['luid'], TRUE); 
    158                     } 
    159152                } 
    160153            } 
  • modules/branches/2.10/endpointman/includes/advanced.inc

    r12938 r13276  
    376376            $phone_config = new $class(); 
    377377 
    378  
    379             if((method_exists($phone_config,'display_options')) AND (method_exists($phone_config,'process_options'))) { 
    380                 if(isset($_REQUEST['phone_options'])) { 
    381                     $endpoint->tpl->assign("options", $phone_config->process_options()); 
    382                 } else { 
    383                     $endpoint->tpl->assign("options", $phone_config->display_options()); 
    384                 } 
    385             } 
    386  
    387  
    388  
     378            //TODO: remove 
    389379            $template_file_list[0]['value'] = "template_data_custom.xml"; 
    390380            $template_file_list[0]['text'] = "template_data_custom.xml"; 
     
    448438                $_POST['config_loc'] = $_POST['config_loc'] ."/"; 
    449439            } 
    450  
     440             
     441            $tftp_writable = FALSE; 
    451442            if((isset($_POST['config_loc'])) AND ($_POST['config_loc'] != "")) { 
    452443                if((file_exists($_POST['config_loc'])) AND (is_dir($_POST['config_loc']))) { 
     
    454445                        $sql="UPDATE endpointman_global_vars SET value='" . $_POST['config_loc'] . "' WHERE var_name='config_location'"; 
    455446                        $endpoint->eda->sql($sql); 
     447                        $tftp_writable = TRUE; 
    456448                    } else { 
    457449                        $endpoint->error['config_dir'] = "Directory Not Writable!"; 
    458450                    } 
    459451                } else { 
    460                     $endpoint->error['config_dir'] = "Not a Vaild Directory"; 
     452                    $endpoint->error['config_dir'] = "Not a Vaild Directory <br /> Try to run 'mkdir ".$_POST['config_loc']."' as root"; 
    461453                } 
    462454            } else { 
     
    464456            } 
    465457 
     458            //Check tftp server to make sure it's functioning if we are using it 
     459            if(($_POST['cfg_type'] == 'file') AND ($tftp_writable)) { 
     460                $endpoint->tftp_check(); 
     461            } 
     462             
    466463            if((isset($_POST['enable_ari'])) AND ($_POST['enable_ari'] == "on")) { 
    467464                $_POST['enable_ari'] = 1; 
  • modules/branches/2.10/endpointman/includes/functions.inc

    r12938 r13276  
    11<?php 
     2 
    23/** 
    34 * Endpoint Manager Functions File 
     
    89 */ 
    910class endpointmanager { 
     11 
    1012    //Load this class upon construction of the class 
    1113 
     
    2325     * @global object $db Pear DB information, we move this into a public variable 
    2426     */ 
     27 
    2528    function __construct() { 
    2629        global $amp_conf; 
    27         require('abstraction/freepbx.inc'); 
     30        require_once('abstraction/freepbx.inc'); 
     31        require_once('json.inc'); 
    2832        $this->eda = new epm_data_abstraction(); 
    2933 
     
    4650            $this->global_cfg['disable_epm'] = TRUE; 
    4751        } 
    48  
    49         //TODO: Possibly remove these as I don't believe anything uses them anymore 
     52         
    5053        define("UPDATE_PATH", $this->global_cfg['update_server']); 
    51         define("VER", $this->global_cfg['version']); 
    52         //end possible removal 
    53  
    54   define("MODULES_PATH", $amp_conf['AMPWEBROOT'] . '/admin/modules/'); 
    55                  
     54        define("MODULES_PATH", $amp_conf['AMPWEBROOT'] . '/admin/modules/'); 
     55 
    5656        //Determine if local path is correct! 
    57         if(file_exists(MODULES_PATH."endpointman/")) { 
    58             define("LOCAL_PATH", MODULES_PATH."endpointman/"); 
     57        if (file_exists(MODULES_PATH . "endpointman/")) { 
     58            define("LOCAL_PATH", MODULES_PATH . "endpointman/"); 
    5959        } else { 
    6060            die("Can't Load Local Endpoint Manager Directory!"); 
     
    6262 
    6363        //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 
    64         if(file_exists(MODULES_PATH."_ep_phone_modules/")) { 
    65             define("PHONE_MODULES_PATH", MODULES_PATH."_ep_phone_modules/"); 
    66         } else {  
    67             define("PHONE_MODULES_PATH", MODULES_PATH."_ep_phone_modules/"); 
     64        if (file_exists(MODULES_PATH . "_ep_phone_modules/")) { 
     65            define("PHONE_MODULES_PATH", MODULES_PATH . "_ep_phone_modules/"); 
     66        } else { 
     67            define("PHONE_MODULES_PATH", MODULES_PATH . "_ep_phone_modules/"); 
    6868            if (!file_exists(PHONE_MODULES_PATH)) { 
    6969                mkdir(PHONE_MODULES_PATH, 0764); 
     
    7777                mkdir(PHONE_MODULES_PATH . "temp/", 0764); 
    7878            } 
    79             if(!file_exists(MODULES_PATH."_ep_phone_modules/")) { 
     79            if (!file_exists(MODULES_PATH . "_ep_phone_modules/")) { 
    8080                die('Endpoint Manager can not create the modules folder!'); 
    8181            } 
     
    8383 
    8484        //include the local template class 
    85         if(file_exists(LOCAL_PATH."includes/rain.tpl.class.inc")) { 
    86             require(LOCAL_PATH."includes/rain.tpl.class.inc"); 
    87         } else { 
    88             die("Can't Load the Template Class"); 
     85        if (!class_exists('RainTPL')) { 
     86            if (file_exists(LOCAL_PATH . "includes/rain.tpl.class.inc")) { 
     87                require(LOCAL_PATH . "includes/rain.tpl.class.inc"); 
     88            } else { 
     89                die("Can't Load the Template Class"); 
     90            } 
    8991        } 
    9092 
    9193        //Define error reporting 
    92         if(($this->global_cfg['debug']) AND (!isset($_REQUEST['quietmode']))) { 
     94        if (($this->global_cfg['debug']) AND (!isset($_REQUEST['quietmode']))) { 
    9395            error_reporting(E_ALL); 
    9496            ini_set('display_errors', 1); 
     
    98100 
    99101        //Check if config location is writable and/or exists! 
    100         if(isset($this->global_cfg['config_location'])) { 
    101             if(is_dir($this->global_cfg['config_location'])) { 
    102                 if(!is_writeable($this->global_cfg['config_location'])) { 
     102        if (isset($this->global_cfg['config_location'])) { 
     103            if (is_dir($this->global_cfg['config_location'])) { 
     104                if (!is_writeable($this->global_cfg['config_location'])) { 
    103105                    $user = exec('whoami'); 
    104106                    $group = exec("groups"); 
    105                     $this->error['config_location'] = "Configuration Directory is not writable!"
    106                         "<br />Please change the location: <a href='config.php?type=tool&display=epm_advanced'>Here</a>"
    107                         "<br />Or run this command on SSH: 'chown -R ".$user.":".$group." ".$this->global_cfg['config_location']."' then 'chmod -R 777 ".$this->global_cfg['config_location']."'"; 
     107                    $this->error['config_location'] = "Configuration Directory is not writable!"
     108                            "<br />Please change the location: <a href='config.php?type=tool&display=epm_advanced'>Here</a>"
     109                            "<br />Or run this command on SSH: 'chown -hR root:" . $group . " " . $this->global_cfg['config_location'] . "' then 'chmod g+w " . $this->global_cfg['config_location'] . "'"; 
    108110                    $this->global_cfg['diable_epm'] = TRUE; 
    109111                } 
     
    113115            } 
    114116        } 
    115          
     117 
    116118        $sql = 'SELECT value FROM `admin` WHERE `variable` LIKE CONVERT(_utf8 \'version\' USING latin1) COLLATE latin1_swedish_ci'; 
    117          
     119 
    118120        preg_match('/^(\d*)\.(\d*)/', $this->eda->sql($sql, 'getone'), $versions); 
    119121 
     
    121123        $this->global_cfg['amp_ver']['minor'] = $versions[2]; 
    122124 
    123         $this->tpl = new RainTPL( LOCAL_PATH.'templates/freepbx', LOCAL_PATH.'templates/freepbx/compiled', '/admin/assets/endpointman/images' ); 
    124  
    125  
    126         if($this->global_cfg['disable_help']) { 
     125        $this->tpl = new RainTPL(LOCAL_PATH . 'templates/freepbx', LOCAL_PATH . 'templates/freepbx/compiled', '/admin/assets/endpointman/images'); 
     126 
     127        if ($this->global_cfg['disable_help']) { 
    127128            $this->tpl->assign("disable_help", 1); 
    128129        } 
    129130 
    130131        $this->tpl->assign("amp_ver", $this->global_cfg['amp_ver']['minor']); 
    131  
     132    } 
     133 
     134    /** 
     135     * @author http://www.php.net/manual/en/function.socket-create.php#43057 
     136     * @param type $host 
     137     * @param type $filename 
     138     * @return type  
     139     */ 
     140    function tftp_fetch($host, $filename) {  
     141        //first off let's check if this is installed or disabled 
     142        $socket = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP); 
     143 
     144        // create the request packet 
     145        $packet = chr(0) . chr(1) . $filename . chr(0) . 'octet' . chr(0); 
     146        // UDP is connectionless, so we just send on it. 
     147        socket_sendto($socket, $packet, strlen($packet), 0x100, $host, 69); 
     148 
     149        $buffer = ''; 
     150        $port = ''; 
     151        $ret = ''; 
     152        $time = time(); 
     153        do { 
     154            $new_time = time() - $time; 
     155            if ($new_time > 5) { 
     156                break; 
     157            } 
     158            // $buffer and $port both come back with information for the ack 
     159            // 516 = 4 bytes for the header + 512 bytes of data 
     160            socket_recvfrom($socket, $buffer, 516, 0, $host, $port); 
     161 
     162            // add the block number from the data packet to the ack packet 
     163            $packet = chr(0) . chr(4) . substr($buffer, 2, 2); 
     164            // send ack 
     165            socket_sendto($socket, $packet, strlen($packet), 0, $host, $port); 
     166 
     167            // append the data to the return variable 
     168            // for large files this function should take a file handle as an arg 
     169            $ret .= substr($buffer, 4); 
     170        } while (strlen($buffer) == 516);  // the first non-full packet is the last. 
     171        return $ret;  
     172    } 
     173     
     174    function tftp_check() { 
     175        $subject = shell_exec("netstat -luan --numeric-ports"); 
     176        if(preg_match('/:69\s/i', $subject)) { 
     177            $rand = md5(rand(10,2000)); 
     178            if(file_put_contents($this->global_cfg['config_location'].'TEST', $rand)) { 
     179                if($this->tftp_fetch('127.0.0.1','TEST') != $rand) { 
     180                    $this->error['tftp_check'] = 'Local TFTP Server is not correctly configured'; 
     181                } 
     182                unlink($this->global_cfg['config_location'].'TEST'); 
     183            } else { 
     184                $this->error['tftp_check'] = 'Unable to write to '.$this->global_cfg['config_location']; 
     185            } 
     186        } else { 
     187            $dis = FALSE; 
     188            if (file_exists('/etc/xinetd.d/tftp')) { 
     189                $contents = file_get_contents('/etc/xinetd.d/tftp'); 
     190                if (preg_match('/disable.*=.*yes/i', $contents)) { 
     191                    $this->error['tftp_check'] = 'Disabled is set to "yes" in /etc/xinetd.d/tftp. Please fix <br />Then restart your TFTP service'; 
     192                    $dis = TRUE; 
     193                } 
     194            } 
     195            if(!$dis) { 
     196                $this->error['tftp_check'] = 'TFTP Server is not running. <br />'. 
     197                'See here for instructions on how to install one: <a href="http://wiki.provisioner.net/index.php/Tftp" target="_blank">http://wiki.provisioner.net/index.php/Tftp</a>'; 
     198         
     199            } 
     200        } 
     201        
    132202    } 
    133203 
     
    150220     * @param <type> $data The config file's data 
    151221     */ 
    152     function submit_config($brand,$product,$orig_name,$data) { 
     222    function submit_config($brand, $product, $orig_name, $data) { 
    153223        $posturl = 'http://www.provisioner.net/submit_config.php'; 
    154224 
    155         $fp = fopen(LOCAL_PATH.'data.txt', 'w'); 
     225        $fp = fopen(LOCAL_PATH . 'data.txt', 'w'); 
    156226        fwrite($fp, $data); 
    157227        fclose($fp); 
    158         $file_name_with_full_path = LOCAL_PATH."data.txt"; 
    159  
    160         $postvars = array('brand' => $brand, 'product' => $product, 'origname' => htmlentities(addslashes($orig_name)), 'file_contents'=>'@'.$file_name_with_full_path); 
     228        $file_name_with_full_path = LOCAL_PATH . "data.txt"; 
     229 
     230        $postvars = array('brand' => $brand, 'product' => $product, 'origname' => htmlentities(addslashes($orig_name)), 'file_contents' => '@' . $file_name_with_full_path); 
    161231 
    162232        $ch = curl_init($posturl); 
     
    170240        ob_start(); 
    171241        header("Content-Type: text/html"); 
    172         $Final_Out=ob_get_clean(); 
     242        $Final_Out = ob_get_clean(); 
    173243        curl_close($ch); 
    174244        unlink($file_name_with_full_path); 
     
    179249    function prepare_message_box() { 
    180250        $error_message = NULL; 
    181         foreach($this->error as $key => $error) { 
     251        foreach ($this->error as $key => $error) { 
    182252            $error_message .= $error; 
    183             if($this->global_cfg['debug']) { 
    184                 $error_message .= " Function: [".$key."]"; 
     253            if ($this->global_cfg['debug']) { 
     254                $error_message .= " Function: [" . $key . "]"; 
    185255            } 
    186256            $error_message .= "<br />"; 
    187257        } 
    188258        $message = NULL; 
    189         foreach($this->message as $key => $error) { 
    190             if(is_array($error)) { 
    191                 foreach($error as $sub_error) { 
     259        foreach ($this->message as $key => $error) { 
     260            if (is_array($error)) { 
     261                foreach ($error as $sub_error) { 
    192262                    $message .= $sub_error; 
    193263                    if ($this->global_cfg['debug']) { 
     
    199269                $message .= $error; 
    200270                if ($this->global_cfg['debug']) { 
    201                         $message .= " Function: [" . $key . "]"; 
     271                    $message .= " Function: [" . $key . "]"; 
    202272                } 
    203273                $message .= "<br />"; 
     
    205275        } 
    206276 
    207         if(isset($message)) { 
    208             $this->display_message_box($message,0); 
    209         } 
    210  
    211         if(isset($error_message)) { 
    212             $this->display_message_box($error_message,1); 
     277        if (isset($message)) { 
     278            $this->display_message_box($message, 0); 
     279        } 
     280 
     281        if (isset($error_message)) { 
     282            $this->display_message_box($error_message, 1); 
    213283        } 
    214284    } 
     
    258328     * @return array 
    259329     */ 
    260     function get_brand_from_mac($mac)
     330    function get_brand_from_mac($mac)
    261331        //Check for valid mac address first 
    262         if(!$this->mac_check_clean($mac)) { 
     332        if (!$this->mac_check_clean($mac)) { 
    263333            return(FALSE); 
    264334        } 
    265335 
    266336        //Get the OUI only 
    267         $oui = substr($this->mac_check_clean($mac),0,6); 
     337        $oui = substr($this->mac_check_clean($mac), 0, 6); 
    268338        //Find the matching brand model to the oui 
    269         $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"; 
     339        $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"; 
    270340        $brand = $this->eda->sql($oui_sql, 'getRow', DB_FETCHMODE_ASSOC); 
    271341 
     
    275345        if (!$brand_count) { 
    276346            //oui doesn't have a matching mysql reference, probably a PC/router/wap/printer of some sort. 
    277            $phone_info['id'] = 0; 
    278            $phone_info['name'] = _("Unknown"); 
    279         } else { 
    280            $phone_info['id'] = $brand['id']; 
    281            $phone_info['name'] = $brand['name']; 
     347            $phone_info['id'] = 0; 
     348            $phone_info['name'] = _("Unknown"); 
     349        } else { 
     350            $phone_info['id'] = $brand['id']; 
     351            $phone_info['name'] = $brand['name']; 
    282352        } 
    283353 
     
    285355    } 
    286356 
    287     function add_device($mac,$model,$ext,$template=NULL,$line=NULL,$displayname=NULL) { 
     357    function add_device($mac, $model, $ext, $template=NULL, $line=NULL, $displayname=NULL) { 
    288358 
    289359        $mac = $this->mac_check_clean($mac); 
    290         if($mac) { 
    291             if(empty($model)) { 
    292                 $this->error['add_device'] = _("You Must Select A Model From the Drop Down")."!"; 
     360        if ($mac) { 
     361            if (empty($model)) { 
     362                $this->error['add_device'] = _("You Must Select A Model From the Drop Down") . "!"; 
    293363                return(FALSE); 
    294             } elseif(empty($ext)) { 
    295                 $this->error['add_device'] = _("You Must Select an Extension/Device From the Drop Down")."!"; 
     364            } elseif (empty($ext)) { 
     365                $this->error['add_device'] = _("You Must Select an Extension/Device From the Drop Down") . "!"; 
    296366                return(FALSE); 
    297367            } else { 
    298                 if($this->sync_model($model)) { 
    299                     $sql = "SELECT id,template_id FROM endpointman_mac_list WHERE mac = '".$mac."'"; 
    300                     $dup = $this->eda->sql($sql,'getRow',DB_FETCHMODE_ASSOC); 
    301  
    302                     if($dup) { 
    303                         if(!isset($template)) { 
     368                if ($this->sync_model($model)) { 
     369                    $sql = "SELECT id,template_id FROM endpointman_mac_list WHERE mac = '" . $mac . "'"; 
     370                    $dup = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
     371 
     372                    if ($dup) { 
     373                        if (!isset($template)) { 
    304374                            $template = $dup['template_id']; 
    305375                        } 
    306376 
    307                         $sql = "UPDATE endpointman_mac_list SET model = ".$model.", template_id =  ".$template." WHERE id = ".$dup['id']; 
     377                        $sql = "UPDATE endpointman_mac_list SET model = " . $model . ", template_id =  " . $template . " WHERE id = " . $dup['id']; 
    308378                        $this->eda->sql($sql); 
    309379                        $return = $this->add_line($dup['id'], $line, $ext); 
    310                         if($return) { 
     380                        if ($return) { 
    311381                            return($return); 
    312382                        } else { 
     
    314384                        } 
    315385                    } else { 
    316                         if(!isset($template)) { 
     386                        if (!isset($template)) { 
    317387                            $template = 0; 
    318388                        } 
    319389 
    320                         $sql = "SELECT mac_id FROM endpointman_line_list WHERE ext = ".$ext; 
    321                         $used = $this->eda->sql($sql,'getOne'); 
    322  
    323                         if(($used) AND (!$this->global_cfg['show_all_registrations'])) { 
     390                        $sql = "SELECT mac_id FROM endpointman_line_list WHERE ext = " . $ext; 
     391                        $used = $this->eda->sql($sql, 'getOne'); 
     392 
     393                        if (($used) AND (!$this->global_cfg['show_all_registrations'])) { 
    324394                            $this->error['add_device'] = "You can't assign the same user to multiple devices!"; 
    325395                            return(FALSE); 
    326396                        } 
    327397 
    328                         if(!isset($displayname)) { 
    329                             $sql = 'SELECT description FROM devices WHERE id = '.$ext; 
    330                             $name =& $this->eda->sql($sql,'getOne'); 
     398                        if (!isset($displayname)) { 
     399                            $sql = 'SELECT description FROM devices WHERE id = ' . $ext; 
     400                            $name = & $this->eda->sql($sql, 'getOne'); 
    331401                        } else { 
    332402                            $name = $displayname; 
    333403                        } 
    334404 
    335                         $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 =  \''.$model.'\' AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id'; 
    336  
    337                         $row =& $this->eda->sql($sql,'getRow',DB_FETCHMODE_ASSOC); 
    338  
    339                         $sql = "INSERT INTO `endpointman_mac_list` (`mac`, `model`, `template_id`) VALUES ('".$mac."', '".$model."', '".$template."')"; 
     405                        $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 =  \'' . $model . '\' AND endpointman_model_list.brand = endpointman_brand_list.id AND endpointman_model_list.product_id = endpointman_product_list.id'; 
     406 
     407                        $row = & $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
     408 
     409                        $sql = "INSERT INTO `endpointman_mac_list` (`mac`, `model`, `template_id`) VALUES ('" . $mac . "', '" . $model . "', '" . $template . "')"; 
    340410                        $this->eda->sql($sql); 
    341411 
    342412                        $sql = 'SELECT last_insert_id()'; 
    343                         $ext_id =& $this->eda->sql($sql,'getOne'); 
    344  
    345                         if(empty($line)) { 
     413                        $ext_id = & $this->eda->sql($sql, 'getOne'); 
     414 
     415                        if (empty($line)) { 
    346416                            $line = 1; 
    347417                        } 
    348418 
    349                         $sql = "INSERT INTO `endpointman_line_list` (`mac_id`, `ext`, `line`, `description`) VALUES ('".$ext_id."', '".$ext."', '".$line."', '".addslashes($name)."')"; 
     419                        $sql = "INSERT INTO `endpointman_line_list` (`mac_id`, `ext`, `line`, `description`) VALUES ('" . $ext_id . "', '" . $ext . "', '" . $line . "', '" . addslashes($name) . "')"; 
    350420                        $this->eda->sql($sql); 
    351421 
    352                         $this->message['add_device'][] = "Added ".$name." to line ".$line; 
     422                        $this->message['add_device'][] = "Added " . $name . " to line " . $line; 
    353423                        return($ext_id); 
    354424                    } 
    355425                } else { 
    356                     $this->error['Sync_Model'] = _("Invalid Model Selected, Can't Sync System")."!"; 
     426                    $this->error['Sync_Model'] = _("Invalid Model Selected, Can't Sync System") . "!"; 
    357427                    return(FALSE); 
    358428                } 
    359429            } 
    360430        } else { 
    361             $this->error['add_device'] = _("Invalid MAC Address")."!"; 
     431            $this->error['add_device'] = _("Invalid MAC Address") . "!"; 
    362432            return(FALSE); 
    363433        } 
    364434    } 
    365435 
    366     function add_line($mac_id,$line=NULL,$ext=NULL,$displayname=NULL) { 
    367         if((!isset($line)) AND (!isset($ext))) { 
    368             if($this->linesAvailable(NULL,$mac_id)) { 
    369                 if($this->eda->all_unused_registrations()) { 
    370                     $sql = 'SELECT * FROM endpointman_line_list WHERE mac_id = '. $mac_id; 
    371                     $lines_list = $this->eda->sql($sql,'getAll',DB_FETCHMODE_ASSOC); 
    372  
    373                     foreach($lines_list as $row) { 
    374                         $sql = "SELECT description FROM devices WHERE id = ".$row['ext']; 
    375                         $name=$this->eda->sql($sql,'getOne'); 
    376  
    377                         $sql = "UPDATE endpointman_line_list SET line = '".$row['line']."', ext = ".$row['ext'].", description = '".addslashes($name)."' WHERE luid =  ". $row['luid']; 
     436    function add_line($mac_id, $line=NULL, $ext=NULL, $displayname=NULL) { 
     437        if ((!isset($line)) AND (!isset($ext))) { 
     438            if ($this->linesAvailable(NULL, $mac_id)) { 
     439                if ($this->eda->all_unused_registrations()) { 
     440                    $sql = 'SELECT * FROM endpointman_line_list WHERE mac_id = ' . $mac_id; 
     441                    $lines_list = $this->eda->sql($sql, 'getAll', DB_FETCHMODE_ASSOC); 
     442 
     443                    foreach ($lines_list as $row) { 
     444                        $sql = "SELECT description FROM devices WHERE id = " . $row['ext']; 
     445                        $name = $this->eda->sql($sql, 'getOne'); 
     446 
     447                        $sql = "UPDATE endpointman_line_list SET line = '" . $row['line'] . "', ext = " . $row['ext'] . ", description = '" . addslashes($name) . "' WHERE luid =  " . $row['luid']; 
    378448                        $this->eda->sql($sql); 
    379449                    } 
    380450 
    381451                    $reg = array_values($this->display_registration_list()); 
    382                     $lines = array_values($this->linesAvailable(NULL,$mac_id)); 
    383  
    384                     $sql = "SELECT description FROM devices WHERE id = ".$reg[0]['value']; 
    385                     $name = $this->eda->sql($sql,'getOne'); 
    386  
    387                     $sql = "INSERT INTO `endpointman_line_list` (`mac_id`, `ext`, `line`, `description`) VALUES ('".$mac_id."', '".$reg[0]['value']."', '".$lines[0]['value']."', '".addslashes($name)."')"; 
     452                    $lines = array_values($this->linesAvailable(NULL, $mac_id)); 
     453 
     454                    $sql = "SELECT description FROM devices WHERE id = " . $reg[0]['value']; 
     455                    $name = $this->eda->sql($sql, 'getOne'); 
     456 
     457                    $sql = "INSERT INTO `endpointman_line_list` (`mac_id`, `ext`, `line`, `description`) VALUES ('" . $mac_id . "', '" . $reg[0]['value'] . "', '" . $lines[0]['value'] . "', '" . addslashes($name) . "')"; 
    388458                    $this->eda->sql($sql); 
    389459 
    390                     $this->message['add_line'] = "Added '<i>".$name."</i>' to line '<i>".$lines[0]['value']."</i>' on device '<i>".$reg[0]['value']."</i>' <br/> Configuration Files will not be Generated until you click Save!"; 
     460                    $this->message['add_line'] = "Added '<i>" . $name . "</i>' to line '<i>" . $lines[0]['value'] . "</i>' on device '<i>" . $reg[0]['value'] . "</i>' <br/> Configuration Files will not be Generated until you click Save!"; 
    391461                    return($mac_id); 
    392462                } else { 
    393                     $this->error['add_line'] = _("No Devices/Extensions Left to Add")."!"; 
     463                    $this->error['add_line'] = _("No Devices/Extensions Left to Add") . "!"; 
    394464                    return(FALSE); 
    395465                } 
    396466            } else { 
    397                 $this->error['add_line'] = _("No Lines Left to Add")."!"; 
     467                $this->error['add_line'] = _("No Lines Left to Add") . "!"; 
    398468                return(FALSE); 
    399469            } 
    400         } elseif((!isset($line)) AND (isset($ext))) { 
    401             if($this->linesAvailable(NULL,$mac_id)) { 
    402                 if($this->eda->all_unused_registrations()) { 
    403                     $lines = array_values($this->linesAvailable(NULL,$mac_id)); 
    404  
    405                     $sql = "INSERT INTO `endpointman_line_list` (`mac_id`, `ext`, `line`, `description`) VALUES ('".$mac_id."', '".$ext."', '".$lines[0]['value']."', '".addslashes($displayname)."')"; 
     470        } elseif ((!isset($line)) AND (isset($ext))) { 
     471            if ($this->linesAvailable(NULL, $mac_id)) { 
     472                if ($this->eda->all_unused_registrations()) { 
     473                    $lines = array_values($this->linesAvailable(NULL, $mac_id)); 
     474 
     475                    $sql = "INSERT INTO `endpointman_line_list` (`mac_id`, `ext`, `line`, `description`) VALUES ('" . $mac_id . "', '" . $ext . "', '" . $lines[0]['value'] . "', '" . addslashes($displayname) . "')"; 
    406476                    $this->eda->sql($sql); 
    407477 
    408                     $this->message['add_line'] = "Added '<i>".$name."</i>' to line '<i>".$lines[0]['value']."</i>' on device '<i>".$reg[0]['value']."</i>' <br/> Configuration Files will not be Generated until you click Save!"; 
     478                    $this->message['add_line'] = "Added '<i>" . $name . "</i>' to line '<i>" . $lines[0]['value'] . "</i>' on device '<i>" . $reg[0]['value'] . "</i>' <br/> Configuration Files will not be Generated until you click Save!"; 
    409479                    return($mac_id); 
    410480                } else { 
    411                     $this->error['add_line'] = _("No Devices/Extensions Left to Add")."!"; 
     481                    $this->error['add_line'] = _("No Devices/Extensions Left to Add") . "!"; 
    412482                    return(FALSE); 
    413483                } 
    414484            } else { 
    415                 $this->error['add_line'] = _("No Lines Left to Add")."!"; 
     485                $this->error['add_line'] = _("No Lines Left to Add") . "!"; 
    416486                return(FALSE); 
    417487            } 
    418         } elseif((isset($line)) AND (isset($ext))) { 
    419             $sql = "SELECT luid FROM endpointman_line_list WHERE line = '".$line."' AND mac_id = ".$mac_id; 
    420             $luid = $this->eda->sql($sql,'getOne'); 
    421             if($luid) { 
     488        } elseif ((isset($line)) AND (isset($ext))) { 
     489            $sql = "SELECT luid FROM endpointman_line_list WHERE line = '" . $line . "' AND mac_id = " . $mac_id; 
     490            $luid = $this->eda->sql($sql, 'getOne'); 
     491            if ($luid) { 
    422492                $this->error['add_line'] = "This line has already been assigned!"; 
    423493                return(FALSE); 
    424494            } else { 
    425                 if(!isset($displayname)) { 
    426                     $sql = 'SELECT description FROM devices WHERE id = '.$ext; 
    427                     $name =& $this->eda->sql($sql,'getOne'); 
     495                if (!isset($displayname)) { 
     496                    $sql = 'SELECT description FROM devices WHERE id = ' . $ext; 
     497                    $name = & $this->eda->sql($sql, 'getOne'); 
    428498                } else { 
    429499                    $name = $displayname; 
    430500                } 
    431501 
    432                 $sql = "INSERT INTO `endpointman_line_list` (`mac_id`, `ext`, `line`, `description`) VALUES ('".$mac_id."', '".$ext."', '".$line."', '".addslashes($name)."')"; 
     502                $sql = "INSERT INTO `endpointman_line_list` (`mac_id`, `ext`, `line`, `description`) VALUES ('" . $mac_id . "', '" . $ext . "', '" . $line . "', '" . addslashes($name) . "')"; 
    433503                $this->eda->sql($sql); 
    434                 $this->message['add_line'] .= "Added ".$name." to line ".$line . "<br/>"; 
     504                $this->message['add_line'] .= "Added " . $name . " to line " . $line . "<br/>"; 
    435505                return($mac_id); 
    436506            } 
    437  
    438         } 
    439     } 
    440  
    441     function update_device($macid,$model,$template,$luid=NULL,$name=NULL,$line=NULL,$update_lines=TRUE) { 
    442         $sql = "UPDATE endpointman_mac_list SET model = ".$model.", template_id =  ".$template." WHERE id = ".$macid; 
     507        } 
     508    } 
     509 
     510    function update_device($macid, $model, $template, $luid=NULL, $name=NULL, $line=NULL, $update_lines=TRUE) { 
     511        $sql = "UPDATE endpointman_mac_list SET model = " . $model . ", template_id =  " . $template . " WHERE id = " . $macid; 
    443512        $this->eda->sql($sql); 
    444513 
    445         if($update_lines) { 
    446             if(isset($luid)) { 
    447                 $this->update_line($luid,NULL,$name,$line); 
     514        if ($update_lines) { 
     515            if (isset($luid)) { 
     516                $this->update_line($luid, NULL, $name, $line); 
    448517                return(TRUE); 
    449518            } else { 
    450                 $this->update_line(NULL,$macid); 
     519                $this->update_line(NULL, $macid); 
    451520                return(TRUE); 
    452521            } 
     
    454523    } 
    455524 
    456     function update_line($luid=NULL,$macid=NULL,$name=NULL,$line=NULL) { 
    457         if(isset($luid)) { 
    458             $sql = "SELECT * FROM endpointman_line_list WHERE luid = ".$luid; 
     525    function update_line($luid=NULL, $macid=NULL, $name=NULL, $line=NULL) { 
     526        if (isset($luid)) { 
     527            $sql = "SELECT * FROM endpointman_line_list WHERE luid = " . $luid; 
    459528            $row = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
    460529 
    461             if(!isset($name)) { 
    462                 $sql = "SELECT description FROM devices WHERE id = ".$row['ext']; 
    463                 $name=$this->eda->sql($sql,'getOne'); 
    464             } 
    465  
    466             if(!isset($line)) { 
     530            if (!isset($name)) { 
     531                $sql = "SELECT description FROM devices WHERE id = " . $row['ext']; 
     532                $name = $this->eda->sql($sql, 'getOne'); 
     533            } 
     534 
     535            if (!isset($line)) { 
    467536                $line = $row['line']; 
    468537            } 
    469             $sql = "UPDATE endpointman_line_list SET line = '".$line."', ext = '".$row['ext']."', description = '".$name."' WHERE luid =  ". $row['luid']; 
     538            $sql = "UPDATE endpointman_line_list SET line = '" . $line . "', ext = '" . $row['ext'] . "', description = '" . $name . "' WHERE luid =  " . $row['luid']; 
    470539            $this->eda->sql($sql); 
    471540            return(TRUE); 
    472541        } else { 
    473             $sql = "SELECT * FROM endpointman_line_list WHERE mac_id = ".$macid; 
     542            $sql = "SELECT * FROM endpointman_line_list WHERE mac_id = " . $macid; 
    474543            $lines_info = $this->eda->sql($sql, 'getAll', DB_FETCHMODE_ASSOC); 
    475             foreach($lines_info as $row) { 
    476                 $sql = "SELECT description FROM devices WHERE id = ".$row['ext']; 
    477                 $name=$this->eda->sql($sql,'getOne'); 
    478  
    479                 $sql = "UPDATE endpointman_line_list SET line = '".$row['line']."', ext = '".$row['ext']."', description = '".$name."' WHERE luid =  ". $row['luid']; 
     544            foreach ($lines_info as $row) { 
     545                $sql = "SELECT description FROM devices WHERE id = " . $row['ext']; 
     546                $name = $this->eda->sql($sql, 'getOne'); 
     547 
     548                $sql = "UPDATE endpointman_line_list SET line = '" . $row['line'] . "', ext = '" . $row['ext'] . "', description = '" . $name . "' WHERE luid =  " . $row['luid']; 
    480549                $this->eda->sql($sql); 
    481550            } 
     
    489558     * @return <type> 
    490559     */ 
    491     function delete_line($lineid,$allow_device_remove=FALSE) { 
    492         $sql = 'SELECT mac_id FROM endpointman_line_list WHERE luid = '.$lineid
    493         $mac_id = $this->eda->sql($sql,'getOne'); 
     560    function delete_line($lineid, $allow_device_remove=FALSE) { 
     561        $sql = 'SELECT mac_id FROM endpointman_line_list WHERE luid = ' . $lineid
     562        $mac_id = $this->eda->sql($sql, 'getOne'); 
    494563        $row = $this->get_phone_info($mac_id); 
    495564 
    496         $sql = 'SELECT COUNT(*) FROM endpointman_line_list WHERE mac_id = '.$mac_id; 
    497         $num_lines = $this->eda->sql($sql,'getOne'); 
    498         if($num_lines > 1) { 
    499             $sql = "DELETE FROM endpointman_line_list WHERE luid=".$lineid; 
     565        $sql = 'SELECT COUNT(*) FROM endpointman_line_list WHERE mac_id = ' . $mac_id; 
     566        $num_lines = $this->eda->sql($sql, 'getOne'); 
     567        if ($num_lines > 1) { 
     568            $sql = "DELETE FROM endpointman_line_list WHERE luid=" . $lineid; 
    500569            $this->eda->sql($sql); 
    501570            $this->message['delete_line'] = "Deleted!"; 
    502571            return(TRUE); 
    503572        } else { 
    504             if($allow_device_remove) { 
    505                 $sql = "DELETE FROM endpointman_line_list WHERE luid=".$lineid; 
     573            if ($allow_device_remove) { 
     574                $sql = "DELETE FROM endpointman_line_list WHERE luid=" . $lineid; 
    506575                $this->eda->sql($sql); 
    507576 
    508                 $sql = "DELETE FROM endpointman_mac_list WHERE id=". $mac_id; 
     577                $sql = "DELETE FROM endpointman_mac_list WHERE id=" . $mac_id; 
    509578                $this->eda->sql($sql); 
    510579                $this->message['delete_line'] = "Deleted!"; 
    511580                return(TRUE); 
    512581            } else { 
    513                 $this->error['delete_line'] = _("You can't remove the only line left")."!"; 
     582                $this->error['delete_line'] = _("You can't remove the only line left") . "!"; 
    514583                return(FALSE); 
    515584            } 
     
    518587 
    519588    function delete_device($mac_id) { 
    520         $sql = "DELETE FROM endpointman_mac_list WHERE id=".$mac_id; 
     589        $sql = "DELETE FROM endpointman_mac_list WHERE id=" . $mac_id; 
    521590        $this->eda->sql($sql); 
    522591 
    523         $sql = "DELETE FROM endpointman_line_list WHERE mac_id=".$mac_id; 
     592        $sql = "DELETE FROM endpointman_line_list WHERE mac_id=" . $mac_id; 
    524593        $this->eda->sql($sql); 
    525594        $this->message['delete_device'] = "Deleted!"; 
     
    528597 
    529598    function get_message($function_name) { 
    530         if(isset($this->message[$function_name])) { 
     599        if (isset($this->message[$function_name])) { 
    531600            return($this->message[$function_name]); 
    532601        } else { 
     
    534603        } 
    535604    } 
     605 
    536606    /** 
    537607     * Send this function an ID from the mac devices list table and you'll get all the information we have on that particular phone 
     
    586656            return(FALSE); 
    587657        } 
    588         $sql = "SELECT id FROM endpointman_mac_list WHERE model > 0 AND id =".$mac_id; 
     658        $sql = "SELECT id FROM endpointman_mac_list WHERE model > 0 AND id =" . $mac_id; 
    589659 
    590660        $res = $this->eda->sql($sql); 
    591         if($res->numRows()) { 
     661        if ($res->numRows()) { 
    592662            //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, 
    593             $sql = "SELECT endpointman_mac_list.specific_settings, endpointman_mac_list.config_files_override, endpointman_mac_list.global_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.template_id, endpointman_mac_list.global_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, endpointman_mac_list.global_settings_override FROM endpointman_line_list, endpointman_mac_list, endpointman_model_list, endpointman_brand_list, endpointman_product_list WHERE 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.id = endpointman_line_list.mac_id AND endpointman_mac_list.id = ". $mac_id; 
     663            $sql = "SELECT endpointman_mac_list.specific_settings, endpointman_mac_list.config_files_override, endpointman_mac_list.global_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.template_id, endpointman_mac_list.global_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, endpointman_mac_list.global_settings_override FROM endpointman_line_list, endpointman_mac_list, endpointman_model_list, endpointman_brand_list, endpointman_product_list WHERE 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.id = endpointman_line_list.mac_id AND endpointman_mac_list.id = " . $mac_id; 
    594664 
    595665            $phone_info = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
    596666 
    597             if(!$phone_info) { 
     667            if (!$phone_info) { 
    598668                $this->error['get_phone_info'] = "Error with SQL Statement"; 
    599669            } 
     
    601671            //If there is a template associated with this phone then pull that information and put it into the array 
    602672            if ($phone_info['template_id'] > 0) { 
    603                 $sql = "SELECT name, global_custom_cfg_data, config_files_override, global_settings_override FROM endpointman_template_list WHERE id = ".$phone_info['template_id']; 
     673                $sql = "SELECT name, global_custom_cfg_data, config_files_override, global_settings_override FROM endpointman_template_list WHERE id = " . $phone_info['template_id']; 
    604674 
    605675                $phone_info['template_data_info'] = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
    606  
    607             } 
    608  
    609             $sql = "SELECT endpointman_line_list.*, sip.data as secret, devices.*, endpointman_line_list.description AS epm_description FROM endpointman_line_list, sip, devices WHERE endpointman_line_list.ext = devices.id AND endpointman_line_list.ext = sip.id AND sip.keyword = 'secret' AND mac_id = ".$mac_id." ORDER BY endpointman_line_list.line ASC"; 
     676            } 
     677 
     678            $sql = "SELECT endpointman_line_list.*, sip.data as secret, devices.*, endpointman_line_list.description AS epm_description FROM endpointman_line_list, sip, devices WHERE endpointman_line_list.ext = devices.id AND endpointman_line_list.ext = sip.id AND sip.keyword = 'secret' AND mac_id = " . $mac_id . " ORDER BY endpointman_line_list.line ASC"; 
    610679            $lines_info = $this->eda->sql($sql, 'getAll', DB_FETCHMODE_ASSOC); 
    611             foreach($lines_info as $line) { 
     680            foreach ($lines_info as $line) { 
    612681                $phone_info['line'][$line['line']] = $line; 
    613682                $phone_info['line'][$line['line']]['description'] = $line['epm_description']; 
    614             } 
    615  
    616  
    617         } else { 
    618             $sql = "SELECT id, mac, ext FROM endpointman_mac_list WHERE id =".$mac_id; 
     683            }             
     684        } else { 
     685            $sql = "SELECT id, mac, ext FROM endpointman_mac_list WHERE id =" . $mac_id; 
    619686            //Phone is unknown, we need to display this to the end user so that they can make corrections 
    620687            $row = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
    621688 
    622689            $brand = $this->get_brand_from_mac($row['mac']); 
    623             if($brand) { 
     690            if ($brand) { 
    624691                $phone_info['brand_id'] = $brand['id']; 
    625692                $phone_info['name'] = $brand['name']; 
     
    634701            $phone_info['custom_cfg_template'] = 0; 
    635702            $phone_info['mac'] = $row['mac']; 
    636             $sql = "SELECT endpointman_line_list.*, sip.data as secret, devices.* FROM endpointman_line_list, sip, devices WHERE endpointman_line_list.ext = devices.id AND endpointman_line_list.ext = sip.id AND sip.keyword = 'secret' AND mac_id = ".$mac_id; 
     703            $sql = "SELECT endpointman_line_list.*, sip.data as secret, devices.* FROM endpointman_line_list, sip, devices WHERE endpointman_line_list.ext = devices.id AND endpointman_line_list.ext = sip.id AND sip.keyword = 'secret' AND mac_id = " . $mac_id; 
    637704            $lines_info = $this->eda->sql($sql, 'getAll', DB_FETCHMODE_ASSOC); 
    638             foreach($lines_info as $line) { 
     705            foreach ($lines_info as $line) { 
    639706                $phone_info['line'][$line['line']] = $line; 
    640707            } 
     
    655722        $alt_configs = NULL; 
    656723 
    657         if($custom == 0) { 
    658             $sql = "SELECT model_id FROM endpointman_template_list WHERE id=".$id; 
    659         } else { 
    660             $sql = "SELECT model FROM endpointman_mac_list WHERE id=".$id; 
    661         } 
    662  
    663         $model_id = $this->eda->sql($sql,'getOne'); 
     724        if ($custom == 0) { 
     725            $sql = "SELECT model_id FROM endpointman_template_list WHERE id=" . $id; 
     726        } else { 
     727            $sql = "SELECT model FROM endpointman_mac_list WHERE id=" . $id; 
     728        } 
     729 
     730        $model_id = $this->eda->sql($sql, 'getOne'); 
    664731 
    665732        //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 
    666         if(!$this->sync_model($model_id)) { 
    667             die("unable to sync local template files - TYPE:". $custom); 
     733        if (!$this->sync_model($model_id)) { 
     734            die("unable to sync local template files - TYPE:" . $custom); 
    668735        } 
    669736 
    670737 
    671738        //Determine if we are dealing with a general template or a specific [for that phone only] template (custom =0 means general) 
    672         if($custom == 0) { 
    673             $sql = "SELECT endpointman_model_list.max_lines, endpointman_model_list.model as model_name, endpointman_template_list.global_custom_cfg_data,  endpointman_product_list.config_files, endpointman_product_list.short_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; 
    674         } else { 
    675             $sql = "SELECT endpointman_model_list.max_lines, endpointman_model_list.model as model_name, endpointman_mac_list.global_custom_cfg_data, endpointman_product_list.config_files, endpointman_mac_list.*, endpointman_line_list.*, endpointman_model_list.id as model_id, endpointman_model_list.template_data, endpointman_product_list.id as product_id, endpointman_product_list.short_name, endpointman_product_list.cfg_dir, endpointman_brand_list.directory FROM endpointman_brand_list, endpointman_mac_list, endpointman_model_list, endpointman_product_list, endpointman_line_list WHERE endpointman_mac_list.id=".$id." AND endpointman_mac_list.id = endpointman_line_list.mac_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"; 
     739        if ($custom == 0) { 
     740            $sql = "SELECT endpointman_model_list.max_lines, endpointman_model_list.model as model_name, endpointman_template_list.global_custom_cfg_data,  endpointman_product_list.config_files, endpointman_product_list.short_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; 
     741        } else { 
     742            $sql = "SELECT endpointman_model_list.max_lines, endpointman_model_list.model as model_name, endpointman_mac_list.global_custom_cfg_data, endpointman_product_list.config_files, endpointman_mac_list.*, endpointman_line_list.*, endpointman_model_list.id as model_id, endpointman_model_list.template_data, endpointman_product_list.id as product_id, endpointman_product_list.short_name, endpointman_product_list.cfg_dir, endpointman_brand_list.directory FROM endpointman_brand_list, endpointman_mac_list, endpointman_model_list, endpointman_product_list, endpointman_line_list WHERE endpointman_mac_list.id=" . $id . " AND endpointman_mac_list.id = endpointman_line_list.mac_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"; 
    676743        } 
    677744 
     
    679746 
    680747        $this->tpl->assign("template_editor_display", 1); 
    681         echo $this->tpl->draw( 'global_header' ); 
     748        echo $this->tpl->draw('global_header'); 
    682749        //Let the template system know if we are working with a general template or a specific [for that phone only] template 
    683750        $this->tpl->assign("custom", $custom); 
    684         if($custom) { 
     751        if ($custom) { 
    685752            $this->tpl->assign("ext", $row['ext']); 
    686753        } else { 
     
    690757        $this->tpl->assign("model", $row['model_name']); 
    691758 
    692         if($ma = $this->models_available($row['model_id'], NULL, $row['product_id'])) { 
     759        if ($ma = $this->models_available($row['model_id'], NULL, $row['product_id'])) { 
    693760            $this->tpl->assign("models_ava", $ma); 
    694761        } 
    695762 
    696         if(isset($_REQUEST['maxlines'])) { 
    697             $areas = $this->areaAvailable($row['model_id'],$_REQUEST['maxlines']); 
     763        if (isset($_REQUEST['maxlines'])) { 
     764            $areas = $this->areaAvailable($row['model_id'], $_REQUEST['maxlines']); 
    698765        } else { 
    699766            $areas = $this->areaAvailable($row['model_id'], 3); 
     
    702769        $this->tpl->assign("area_ava", $areas); 
    703770        //Start the display of the html file in the product folder 
    704         if($row['config_files_override'] == "") { 
     771        if ($row['config_files_override'] == "") { 
    705772            $config_files_saved = ""; 
    706773        } else { 
    707774            $config_files_saved = unserialize($row['config_files_override']); 
    708775        } 
    709         $config_files_list = explode(",",$row['config_files']); 
     776        $config_files_list = explode(",", $row['config_files']); 
    710777        $i = 0; 
    711778        $alt = 0; 
    712779 
    713         $i=0; 
    714         $b=0; 
     780        $i = 0; 
     781        $b = 0; 
    715782        $only_configs = array(); 
    716         foreach($config_files_list as $files) { 
    717             $sql = "SELECT * FROM  endpointman_custom_configs WHERE product_id = '".$row['product_id']."' AND original_name = '".$files."'"; 
     783        foreach ($config_files_list as $files) { 
     784            $sql = "SELECT * FROM  endpointman_custom_configs WHERE product_id = '" . $row['product_id'] . "' AND original_name = '" . $files . "'"; 
    718785            $alt_configs_list_count = $this->eda->sql($sql); 
    719             if($alt_configs_list_count->numRows() > 0) { 
     786            if ($alt_configs_list_count->numRows() > 0) { 
    720787                $alt_configs_list = $this->eda->sql($sql, 'getAll', DB_FETCHMODE_ASSOC); 
    721788                $alt_configs[$i]['name'] = $files; 
    722                 $files = str_replace(".","_",$files); 
    723                 $h=0; 
    724                 foreach($alt_configs_list as $ccf) { 
     789                $files = str_replace(".", "_", $files); 
     790                $h = 0; 
     791                foreach ($alt_configs_list as $ccf) { 
    725792                    $alt_configs[$i]['list'][$h]['id'] = $ccf['id']; 
    726793                    $cf_key = $files; 
    727                     if((isset($config_files_saved[$cf_key])) AND (is_array($config_files_saved)) AND ($config_files_saved[$cf_key] == $ccf['id'])) { 
     794                    if ((isset($config_files_saved[$cf_key])) AND (is_array($config_files_saved)) AND ($config_files_saved[$cf_key] == $ccf['id'])) { 
    728795                        $alt_configs[$i]['list'][$h]['selected'] = 'selected'; 
    729796                    } 
     
    742809        $this->tpl->assign("alt_configs", $alt_configs); 
    743810        $this->tpl->assign("alt", $alt); 
    744         if(!isset($_REQUEST['maxlines'])) { 
     811        if (!isset($_REQUEST['maxlines'])) { 
    745812            $maxlines = 3; 
    746813        } else { 
    747814            $maxlines = $_REQUEST['maxlines']; 
    748815        } 
    749         if($row['template_data'] != "") { 
    750             $out = $this->generate_gui_html($row['template_data'],$row['global_custom_cfg_data'],TRUE, NULL, $maxlines); 
     816        if ($row['template_data'] != "") { 
     817            $out = $this->generate_gui_html($row['template_data'], $row['global_custom_cfg_data'], TRUE, NULL, $maxlines); 
    751818        } else { 
    752819            echo "No Template Data has been defined for this Product<br />"; 
     
    756823        $this->tpl->assign("hidden_id", $row['id']); 
    757824        $this->tpl->assign("hidden_custom", $custom); 
    758         echo $this->tpl->draw( 'template_editor' ); 
     825        echo $this->tpl->draw('template_editor'); 
    759826 
    760827        $this->tpl->assign("debug", ""); 
    761  
    762828    } 
    763829 
     
    770836     * @return <type> 
    771837     */ 
    772     function generate_gui_html($cfg_data,$custom_cfg_data=NULL, $admin=FALSE, $user_cfg_data=NULL,$max_lines=3,$ext=NULL) { 
     838    function generate_gui_html($cfg_data, $custom_cfg_data=NULL, $admin=FALSE, $user_cfg_data=NULL, $max_lines=3, $ext=NULL) { 
    773839        //take the data out of the database and turn it back into an array for use 
    774840        $cfg_data = unserialize($cfg_data); 
    775  
    776         $count = count($cfg_data); 
    777  
     841        $template_type = 'GENERAL'; 
    778842        //Check to see if there is a custom template for this phone already listed in the endpointman_mac_list database 
    779843        if (!empty($custom_cfg_data)) { 
    780844            $custom_cfg_data = unserialize($custom_cfg_data); 
    781             if(array_key_exists('data', $custom_cfg_data)) { 
    782                 $custom_cfg_data_ari = $custom_cfg_data['ari']; 
     845            if (array_key_exists('data', $custom_cfg_data)) { 
     846                if(array_key_exists('ari', $custom_cfg_data)) { 
     847                    $extra_data = $custom_cfg_data['ari']; 
     848                } else { 
     849                    $template_type = 'GLOBAL'; 
     850                    $extra_data = $custom_cfg_data['freepbx']; 
     851                } 
    783852                $custom_cfg_data = $custom_cfg_data['data']; 
    784853            } else { 
    785                 $custom_cfg_data_ari = array(); 
     854                $extra_data = array(); 
    786855            } 
    787856        } else { 
    788857            $custom_cfg_data = array(); 
    789             $custom_cfg_data_ari = array(); 
    790         } 
    791         if(isset($user_cfg_data)) { 
     858            $extra_data = array(); 
     859        } 
     860        if (isset($user_cfg_data)) { 
    792861            $user_cfg_data = unserialize($user_cfg_data); 
    793862        } 
     
    797866        $variables_count = 0; 
    798867 
    799         foreach($cfg_data as $data) { 
    800             $data = $this->fix_single_array_keys($data['category']); 
    801             foreach($data as $cats) { 
    802                 //We force the start of a new 'section' by increasing group_count and resetting variables_count to zero 
    803                 if($cats['name'] != 'lines') { 
    804                     $key = $this->arraysearchrecursive($cats['name'], $template_variables_array, 'title'); 
    805                     if(is_array($key)) { 
    806                         $group_count == $key[0]; 
    807                         $num = count($this->fix_single_array_keys($template_variables_array[$group_count]['data'])); 
    808                         $variables_count == $num; 
     868        foreach ($cfg_data['data'] as $cats_name => $cats) { 
     869            if ($admin) { 
     870                $group_count++; 
     871                $template_variables_array[$group_count]['title'] = $cats_name; 
     872            } else { 
     873                //Group all ARI stuff into one tab 
     874                $template_variables_array[$group_count]['title'] = "Your Phone Settings"; 
     875            } 
     876            foreach ($cats as $subcat_name => $subcats) { 
     877                foreach ($subcats as $item_var => $config_options) { 
     878                    if (preg_match('/(.*)\|(.*)/i', $item_var, $matches)) { 
     879                        $type = $matches[1]; 
     880                        $variable = $matches[2]; 
    809881                    } else { 
    810                         if($admin) { 
    811                             $group_count++; 
    812                             $variables_count = 0; 
     882                        die('no matches!'); 
     883                    } 
     884                    if ($admin) { 
     885                        //Administration View Only 
     886                        switch ($type) { 
     887                            case "lineloop": 
     888                                //line|1|display_name 
     889                                foreach ($config_options as $var_name => $var_items) { 
     890                                    $lcount = isset($var_items['line_count']) ? $var_items['line_count'] : $lcount; 
     891                                    $key = "line|" . $lcount . "|" . $var_name; 
     892                                    $items[$variables_count] = $items; 
     893                                    $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count, $var_items, $key, $custom_cfg_data, $admin, $user_cfg_data, $extra_data,$template_type); 
     894                                    $template_variables_array[$group_count]['data'][$variables_count]['looping'] = TRUE; 
     895                                    $variables_count++; 
     896                                } 
     897 
     898                                if ($lcount <= $max_lines) { 
     899                                    $template_variables_array[$group_count]['title'] = "Line Options for Line " . $lcount; 
     900                                    $group_count++; 
     901                                } else { 
     902                                    unset($template_variables_array[$group_count]); 
     903                                } 
     904 
     905                                continue 2; 
     906                            case "loop": 
     907                                foreach ($config_options as $var_name => $var_items) { 
     908                                    //loop|remotephonebook_url_0 
     909                                    $tv = explode('_', $variable); 
     910                                    $key = "loop|" . $tv[0] . "_" . $var_name . "_" . $var_items['loop_count']; 
     911                                    $items[$variables_count] = $var_items; 
     912                                    $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count, $var_items, $key, $custom_cfg_data, $admin, $user_cfg_data, $extra_data,$template_type); 
     913                                    $template_variables_array[$group_count]['data'][$variables_count]['looping'] = TRUE; 
     914                                    $variables_count++; 
     915                                } 
     916                                continue 2; 
    813917                        } 
    814                     }                     
    815                     $template_variables_array[$group_count]['title'] = $cats['name']; 
    816                 } 
    817                 $cats = $this->fix_single_array_keys($cats['subcategory']); 
    818                 foreach($cats as $subcats) { 
    819                     $items = $this->fix_single_array_keys($subcats['item']); 
    820                     foreach($items as $config_options) { 
    821                         if($admin) { 
    822                             //Administration View Only 
    823                             switch ($config_options['type']) { 
    824                                 case "loop_line_options": 
    825                                     for($a=1;$a <= $max_lines; $a++) { 
    826                                         $group_count++; 
    827                                         $variables_count = 0; 
    828                                         $template_variables_array[$group_count]['title'] = "Line Options for Line ".$a; 
    829                                         foreach($config_options['data']['item'] as $items) { 
    830                                             if(isset($items['description'])) { 
    831                                                 $items['description'] = str_replace('{$count}',$a,$items['description']); 
    832                                                 $key = "line|".$a."|".str_replace('$','',$items['variable']); 
    833                                                 if(array_key_exists($key,$custom_cfg_data)) { 
    834                                                     $custom_cfg_data[$key] = $custom_cfg_data[$key]; 
    835                                                 } else { 
    836                                                     $custom_cfg_data[$key] = str_replace('{$count}', $a, $this->fix_single_array_keys($items['default_value'])); 
    837                                                 } 
    838                                             } 
    839                                             $items[$variables_count] = $items; 
    840                                             $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count,$items,$key,$custom_cfg_data,$admin,$user_cfg_data,$custom_cfg_data_ari); 
    841                                             $template_variables_array[$group_count]['data'][$variables_count]['looping'] = TRUE; 
    842                                             $variables_count++; 
    843                                         } 
    844                                     } 
    845                                     continue 2; 
    846                                 case "loop": 
    847                                     //We force the start of a new 'section' by increasing group_count and resetting variables_count to zero 
    848                                     $loop_start = $config_options['loop_start']; 
    849                                     $loop_end = $config_options['loop_end']; 
    850                                     for($a=$loop_start;$a<=$loop_end;$a++) { 
    851                                         foreach($config_options['data']['item'] as $items) { 
    852                                             if(isset($items['description'])) { 
    853                                                 $items['description'] = str_replace('{$count}',$a,$items['description']); 
    854                                                 $key = "loop|".str_replace('$','',$items['variable'])."_".$a; 
    855                                                 if(array_key_exists($key,$custom_cfg_data)) { 
    856                                                     $custom_cfg_data[$key] = $custom_cfg_data[$key]; 
    857                                                 } else { 
    858                                                     $custom_cfg_data[$key] = str_replace('{$count}', $a, $this->fix_single_array_keys($items['default_value'])); 
    859                                                 } 
    860                                             } 
    861                                             $items[$variables_count] = $items; 
    862                                             $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count,$items,$key,$custom_cfg_data,$admin,$user_cfg_data,$custom_cfg_data_ari); 
    863                                             $template_variables_array[$group_count]['data'][$variables_count]['looping'] = TRUE; 
    864                                             $variables_count++; 
    865                                         } 
    866                                     } 
    867                                     continue 2; 
    868                             } 
    869                         } else { 
    870                             //ARI View Only 
    871                             $template_variables_array[$group_count]['title'] = "Your Phone Settings"; 
    872                             switch ($config_options['type']) { 
    873                                 case "loop_line_options": 
    874                                         //$a is the line number 
    875                                         $sql = "SELECT line FROM endpointman_line_list WHERE  ext = ".$ext; 
    876                                         $a = $this->eda->sql($sql,'getOne'); 
    877                                         $template_variables_array[$group_count]['title'] = "Line Options for Line ".$a; 
    878                                         foreach($config_options['data']['item'] as $items) { 
    879                                             if(isset($items['description'])) { 
    880                                                 $items['description'] = str_replace('{$count}',$a,$items['description']); 
    881                                                 $key = "line|".$a."|".str_replace('$','',$items['variable']); 
    882                                                 if(array_key_exists($key,$custom_cfg_data)) { 
    883                                                     $custom_cfg_data[$key] = $custom_cfg_data[$key]; 
    884                                                 } else { 
    885                                                     $custom_cfg_data[$key] = str_replace('{$count}', $a, $this->fix_single_array_keys($items['default_value'])); 
    886                                                 } 
    887                                             } 
    888                                             if(isset($custom_cfg_data_ari[$key])) { 
    889                                                 $items[$variables_count] = $items; 
    890                                                 $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count,$items,$key,$custom_cfg_data,$admin,$user_cfg_data,$custom_cfg_data_ari); 
    891                                                 $template_variables_array[$group_count]['data'][$variables_count]['looping'] = TRUE; 
    892                                                 $variables_count++; 
    893                                             } 
    894                                         } 
    895                                         $template_variables_array[$group_count]['data'][$variables_count]['type'] = "break"; 
    896                                         $variables_count++; 
    897                                     continue 2; 
    898                                 case "loop": 
    899                                     $template_variables_array[$group_count]['data'][$variables_count]['type'] = "break"; 
    900                                     $variables_count++; 
    901                                     $loop_start = $config_options['loop_start']; 
    902                                     $loop_end = $config_options['loop_end']; 
    903                                     for($a=$loop_start;$a<=$loop_end;$a++) { 
    904                                         foreach($config_options['data']['item'] as $items) { 
    905                                             if(isset($items['description'])) { 
    906                                                 $items['description'] = str_replace('{$count}',$a,$items['description']); 
    907                                                 $key = "loop|".str_replace('$','',$items['variable'])."_".$a; 
    908                                                 if(array_key_exists($key,$custom_cfg_data)) { 
    909                                                     $custom_cfg_data[$key] = $custom_cfg_data[$key]; 
    910                                                 } else { 
    911                                                     $custom_cfg_data[$key] = ''; 
    912                                                 } 
    913                                             } 
    914                                             if(isset($custom_cfg_data_ari[$key])) { 
    915                                                 $items[$variables_count] = $items; 
    916                                                 $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count,$items,$key,$custom_cfg_data,$admin,$user_cfg_data,$custom_cfg_data_ari); 
    917                                                 $template_variables_array[$group_count]['data'][$variables_count]['looping'] = TRUE; 
    918                                                 $variables_count++; 
    919                                             } 
    920                                         } 
    921                                     } 
    922                                     continue 2; 
    923                             } 
    924                         } 
    925                         //Both Views 
    926                         switch ($config_options['type']) { 
    927                             case "break": 
    928                                 $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count,$config_options,$key,$custom_cfg_data,$admin,$user_cfg_data,$custom_cfg_data_ari); 
     918                    } else { 
     919                        //ARI View Only 
     920                        switch ($type) { 
     921                            case "loop_line_options": 
     922                                //$a is the line number 
     923                                $sql = "SELECT line FROM endpointman_line_list WHERE  ext = " . $ext; 
     924                                $a = $this->eda->sql($sql, 'getOne'); 
     925                                //TODO: fix this area 
     926                                $template_variables_array[$group_count]['data'][$variables_count]['type'] = "break"; 
    929927                                $variables_count++; 
    930                                 break; 
    931                             default: 
    932                                 if(array_key_exists('variable',$config_options)) { 
    933                                     $key = str_replace('$','',$config_options['variable']); 
    934                                     //TODO: Move this into the sync function 
    935                                     //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! 
    936                                     if(!isset($custom_cfg_data[$key])) { 
    937                                         //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 
    938                                         if((array_key_exists('default_value',$config_options)) AND (is_array($config_options['default_value']))) {                                 
    939                                             $custom_cfg_data[$key] = ""; 
    940                                         } elseif((array_key_exists('default_value',$config_options)) AND (!is_array($config_options['default_value']))) { 
    941                                             $custom_cfg_data[$key] = $config_options['default_value']; 
    942                                         } 
    943                                     } 
    944                                     if((!$admin) AND (isset($custom_cfg_data_ari[$key]))) { 
    945                                         $custom_cfg_data[$key] = $user_cfg_data[$key]; 
    946                                         $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count,$config_options,$key,$custom_cfg_data,$admin,$user_cfg_data,$custom_cfg_data_ari); 
    947                                         $variables_count++; 
    948                                     } elseif($admin) { 
    949                                         $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count,$config_options,$key,$custom_cfg_data,$admin,$user_cfg_data,$custom_cfg_data_ari); 
     928                                continue 2; 
     929                            case "loop": 
     930                                foreach ($config_options as $var_name => $var_items) { 
     931                                    $tv = explode('_', $variable); 
     932                                    $key = "loop|" . $tv[0] . "_" . $var_name . "_" . $var_items['loop_count']; 
     933                                    if (isset($extra_data[$key])) { 
     934                                        $items[$variables_count] = $var_items; 
     935                                        $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count, $var_items, $key, $custom_cfg_data, $admin, $user_cfg_data, $extra_data,$template_type); 
     936                                        $template_variables_array[$group_count]['data'][$variables_count]['looping'] = TRUE; 
    950937                                        $variables_count++; 
    951938                                    } 
    952939                                } 
    953                                 break
     940                                continue 2
    954941                        } 
    955                         continue; 
    956                     } 
    957                 } 
    958             } 
    959         } 
     942                    } 
     943                    //Both Views 
     944                    switch ($config_options['type']) { 
     945                        case "break": 
     946                            $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count, $config_options, $key, $custom_cfg_data, $admin, $user_cfg_data, $extra_data,$template_type); 
     947                            $variables_count++; 
     948                            break; 
     949                        default: 
     950                            if (array_key_exists('variable', $config_options)) { 
     951                                $key = str_replace('$', '', $config_options['variable']); 
     952                                //TODO: Move this into the sync function 
     953                                //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! 
     954                                if (!isset($custom_cfg_data[$key])) { 
     955                                    //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 
     956                                    if ((array_key_exists('default_value', $config_options)) AND (is_array($config_options['default_value']))) { 
     957                                        $custom_cfg_data[$key] = ""; 
     958                                    } elseif ((array_key_exists('default_value', $config_options)) AND (!is_array($config_options['default_value']))) { 
     959                                        $custom_cfg_data[$key] = $config_options['default_value']; 
     960                                    } 
     961                                } 
     962                                if ((!$admin) AND (isset($extra_data[$key]))) { 
     963                                    $custom_cfg_data[$key] = $user_cfg_data[$key]; 
     964                                    $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count, $config_options, $key, $custom_cfg_data, $admin, $user_cfg_data, $extra_data,$template_type); 
     965                                    $variables_count++; 
     966                                } elseif ($admin) { 
     967                                    $template_variables_array[$group_count]['data'][$variables_count] = $this->generate_form_data($variables_count, $config_options, $key, $custom_cfg_data, $admin, $user_cfg_data, $extra_data,$template_type); 
     968                                    $variables_count++; 
     969                                } 
     970                            } 
     971                            break; 
     972                    } 
     973                    continue; 
     974                } 
     975            } 
     976        } 
     977 
    960978        return($template_variables_array); 
    961979    } 
     
    969987     * @return array 
    970988     */ 
    971     function generate_form_data ($i,$cfg_data,$key=NULL,$custom_cfg_data=NULL,$admin=FALSE,$user_cfg_data=NULL,$custom_cfg_data_ari=NULL) { 
     989    function generate_form_data($i, $cfg_data, $key=NULL, $custom_cfg_data=NULL, $admin=FALSE, $user_cfg_data=NULL, $extra_data=NULL,$template_type='GENERAL') { 
    972990        switch ($cfg_data['type']) { 
    973991            case "input": 
    974                 if((!$admin) && (isset($user_cfg_data[$key]))) { 
     992                if ((!$admin) && (isset($user_cfg_data[$key]))) { 
    975993                    $custom_cfg_data[$key] = $user_cfg_data[$key]; 
    976994                } 
    977995                $template_variables_array['type'] = "input"; 
    978                 if(isset($cfg_data['max_chars'])) { 
     996                if (isset($cfg_data['max_chars'])) { 
    979997                    $template_variables_array['max_chars'] = $cfg_data['max_chars']; 
    980998                } 
    981999                $template_variables_array['key'] = $key; 
    982                 $template_variables_array['value'] = $custom_cfg_data[$key]; 
     1000                $template_variables_array['value'] = isset($custom_cfg_data[$key]) ? $custom_cfg_data[$key] : $cfg_data['default_value']; 
    9831001                $template_variables_array['description'] = $cfg_data['description']; 
    9841002                break; 
    9851003            case "radio": 
    986                 if((!$admin) && (isset($user_cfg_data[$key]))) { 
     1004                if ((!$admin) && (isset($user_cfg_data[$key]))) { 
    9871005                    $custom_cfg_data[$key] = $user_cfg_data[$key]; 
    9881006                } 
    989                 $num = $custom_cfg_data[$key]; 
     1007                $num = isset($custom_cfg_data[$key]) ? $custom_cfg_data[$key] : $cfg_data['default_value']; 
    9901008                $template_variables_array['type'] = "radio"; 
    9911009                $template_variables_array['key'] = $key; 
    9921010                $template_variables_array['description'] = $cfg_data['description']; 
    9931011                $z = 0; 
    994                 while($z < count($cfg_data['data'])) { 
     1012                while ($z < count($cfg_data['data'])) { 
    9951013                    $template_variables_array['data'][$z]['key'] = $key; 
    9961014                    $template_variables_array['data'][$z]['value'] = $cfg_data['data'][$z]['value']; 
     
    10031021                break; 
    10041022            case "list": 
    1005                 if((!$admin) && (isset($user_cfg_data[$key]))) { 
     1023                if ((!$admin) && (isset($user_cfg_data[$key]))) { 
    10061024                    $custom_cfg_data[$key] = $user_cfg_data[$key]; 
    10071025                } 
    1008                 $num = $custom_cfg_data[$key]; 
     1026                $num = isset($custom_cfg_data[$key]) ? $custom_cfg_data[$key] : $cfg_data['default_value']; 
    10091027                $template_variables_array['type'] = "list"; 
    10101028                $template_variables_array['key'] = $key; 
    10111029                $template_variables_array['description'] = $cfg_data['description']; 
    10121030                $z = 0; 
    1013                 while($z < count($cfg_data['data'])) { 
     1031                while ($z < count($cfg_data['data'])) { 
    10141032                    $template_variables_array['data'][$z]['value'] = $cfg_data['data'][$z]['value']; 
    10151033                    $template_variables_array['data'][$z]['description'] = $cfg_data['data'][$z]['text']; 
     
    10291047                break; 
    10301048            case "checkbox": 
    1031                 if((!$admin) && (isset($user_cfg_data[$key]))) { 
     1049                if ((!$admin) && (isset($user_cfg_data[$key]))) { 
    10321050                    $custom_cfg_data[$key] = $user_cfg_data[$key]; 
    10331051                } 
    1034                 $num = $custom_cfg_data[$key]; 
     1052                $num = isset($custom_cfg_data[$key]) ? $custom_cfg_data[$key] : $cfg_data['default_value']; 
    10351053                $template_variables_array['type'] = "checkbox"; 
    10361054                $template_variables_array['key'] = $key; 
    10371055                $template_variables_array['description'] = $cfg_data['description']; 
    1038                 $z = 0; 
    1039                 while($z < count($cfg_data['data'])) { 
    1040                     $template_variables_array['data'][$z]['key'] = $key; 
    1041                     $template_variables_array['data'][$z]['value'] = $cfg_data['data'][$z]['value']; 
    1042                     $template_variables_array['data'][$z]['description'] = $cfg_data['data'][$z]['text']; 
    1043                     if ($cfg_data['data'][$z]['value'] == $num) { 
    1044                         $template_variables_array['data'][$z]['checked'] = 'checked'; 
    1045                     } 
    1046                     $z++; 
    1047                 } 
     1056                $template_variables_array['checked'] = $custom_cfg_data[$key] ? TRUE : NULL; 
     1057                $template_variables_array['value'] = $key; 
    10481058                break; 
    10491059            case "file"; 
    1050                 if((!$admin) && (isset($user_cfg_data[$key]))) { 
     1060                if ((!$admin) && (isset($user_cfg_data[$key]))) { 
    10511061                    $custom_cfg_data[$key] = $user_cfg_data[$key]; 
    10521062                } 
    10531063                $template_variables_array['type'] = "file"; 
    1054                 if(isset($cfg_data['max_chars'])) { 
     1064                if (isset($cfg_data['max_chars'])) { 
    10551065                    $template_variables_array['max_chars'] = $cfg_data['max_chars']; 
    10561066                } 
     
    10601070                break; 
    10611071            case "textarea": 
    1062                 if((!$admin) && (isset($user_cfg_data[$key]))) { 
     1072                if ((!$admin) && (isset($user_cfg_data[$key]))) { 
    10631073                    $custom_cfg_data[$key] = $user_cfg_data[$key]; 
    10641074                } 
    10651075                $template_variables_array['type'] = "textarea"; 
    1066                 if(isset($cfg_data['max_chars'])) { 
     1076                if (isset($cfg_data['max_chars'])) { 
    10671077                    $template_variables_array['max_chars'] = $cfg_data['max_chars']; 
    10681078                } 
    10691079                $template_variables_array['key'] = $key; 
    1070                 $template_variables_array['value'] = $custom_cfg_data[$key]; 
     1080                $template_variables_array['value'] = isset($custom_cfg_data[$key]) ? $custom_cfg_data[$key] : $cfg_data['default_value']; 
    10711081                $template_variables_array['description'] = $cfg_data['description']; 
    10721082                break; 
    10731083            case "break": 
    1074                if($admin) { 
     1084                if ($admin) { 
    10751085                    $template_variables_array['type'] = "break"; 
    1076                } else { 
     1086                } else { 
    10771087                    $template_variables_array['type'] = "NA"; 
    1078                
    1079                break; 
     1088               
     1089                break; 
    10801090            default: 
    10811091                $template_variables_array['type'] = "NA"; 
     
    10831093        } 
    10841094 
    1085  if(isset($cfg_data['description_attr']['tooltip'])) { 
    1086      $template_variables_array['tooltip'] = $cfg_data['description_attr']['tooltip']; 
    1087 
    1088  
    1089         if(($this->global_cfg['enable_ari']) AND ($admin) AND ($cfg_data['type'] != "break") AND ($cfg_data['type'] != "group")) { 
    1090             
     1095        if (isset($cfg_data['description_attr']['tooltip'])) { 
     1096            $template_variables_array['tooltip'] = $cfg_data['description_attr']['tooltip']; 
     1097       
     1098 
     1099        if (($this->global_cfg['enable_ari']) AND ($admin) AND ($cfg_data['type'] != "break") AND ($cfg_data['type'] != "group") AND ($template_type == 'GENERAL')) { 
     1100 
    10911101            $template_variables_array['aried'] = 1; 
    10921102            $template_variables_array['ari']['key'] = $key; 
    1093             if(isset($custom_cfg_data_ari[$key])) { 
     1103 
     1104            if (isset($extra_data[$key])) { 
    10941105                $template_variables_array['ari']['checked'] = "checked"; 
     1106            } 
     1107        } 
     1108         
     1109        if($template_type == 'GLOBAL') { 
     1110            $template_variables_array['freepbxed'] = 1; 
     1111            $template_variables_array['freepbx']['key'] = $key; 
     1112            if(empty($extra_data)) { 
     1113                $template_variables_array['freepbx']['checked'] = TRUE; 
     1114            } elseif (isset($extra_data[$key])) { 
     1115                $template_variables_array['freepbx']['checked'] = TRUE; 
    10951116            } 
    10961117        } 
     
    11081129        //Custom Means specific to that MAC 
    11091130        //This function is reversed. Not sure why 
    1110         if($custom != "0") { 
    1111             $sql = "SELECT endpointman_model_list.max_lines, 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"; 
    1112         } else { 
    1113             $sql = "SELECT endpointman_model_list.max_lines, 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; 
     1131        if ($custom != "0") { 
     1132            $sql = "SELECT endpointman_model_list.max_lines, 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"; 
     1133        } else { 
     1134            $sql = "SELECT endpointman_model_list.max_lines, 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; 
    11141135        } 
    11151136 
     
    11221143        $custom_cfg_data_ari = array(); 
    11231144 
    1124         foreach($cfg_data as $data) { 
    1125             $data = $this->fix_single_array_keys($data['category']); 
    1126             foreach($data as $cats) { 
    1127                 $cats = $this->fix_single_array_keys($cats['subcategory']); 
    1128                 foreach($cats as $subcats) { 
    1129                     $items = $this->fix_single_array_keys($subcats['item']); 
    1130                     foreach($items as $config_options) { 
    1131                         if(array_key_exists('variable',$config_options)) { 
    1132                             $temping = str_replace('$','',$config_options['variable']); 
    1133                             $temping_ari = "ari_" . $temping; 
    1134                             if(array_key_exists($temping, $_REQUEST)) { 
    1135                                 $custom_cfg_data[$temping] = $_REQUEST[$temping]; 
    1136                                 if(array_key_exists($temping_ari, $_REQUEST)) { 
    1137                                     if($_REQUEST[$temping_ari] == "on") { 
    1138                                         $custom_cfg_data_ari[$temping] = 1; 
    1139                                     } 
    1140                                 } 
    1141                             } 
    1142                         } elseif ($config_options['type'] == 'loop') { 
    1143                             $loop_start = $config_options['loop_start']; 
    1144                             $loop_end = $config_options['loop_end']; 
    1145                             $variables_count = 0; 
    1146                             for($a=$loop_start;$a<=$loop_end;$a++) { 
    1147                                 foreach($config_options['data']['item'] as $items) { 
    1148                                     if(isset($items['description'])) { 
    1149                                         $items['description'] = str_replace('{$count}',$a,$items['description']); 
    1150                                         $temping = "loop|".str_replace('$','',$items['variable'])."_".$a; 
    1151                                         $temping_ari = "ari_" . $temping; 
    1152                                         if(array_key_exists($temping, $_REQUEST)) { 
    1153                                             $custom_cfg_data[$temping] = $_REQUEST[$temping]; 
    1154                                             if(array_key_exists($temping_ari, $_REQUEST)) { 
    1155                                                 if($_REQUEST[$temping_ari] == "on") { 
    1156                                                     $custom_cfg_data_ari[$temping] = 1; 
    1157                                                 } 
    1158                                             } 
     1145        foreach ($cfg_data['data'] as $cats) { 
     1146            foreach ($cats as $items) { 
     1147                foreach ($items as $key_name => $config_options) { 
     1148                    if (preg_match('/(.*)\|(.*)/i', $key_name, $matches)) { 
     1149                        $type = $matches[1]; 
     1150                        $key = $matches[2]; 
     1151                    } else { 
     1152                        die('invalid'); 
     1153                    } 
     1154                    switch ($type) { 
     1155                        case "loop": 
     1156                            $stuffing = explode("_", $key); 
     1157                            $key2 = $stuffing[0]; 
     1158                            foreach ($config_options as $item_key => $item_data) { 
     1159                                $lc = isset($item_data['loop_count']) ? $item_data['loop_count'] : ''; 
     1160                                $key = 'loop|' . $key2 . '_' . $item_key . '_' . $lc; 
     1161                                if ((isset($item_data['loop_count'])) AND (isset($_REQUEST[$key]))) { 
     1162                                    $custom_cfg_data[$key] = $_REQUEST[$key]; 
     1163                                    $ari_key = "ari_" . $key; 
     1164                                    if (isset($_REQUEST[$ari_key])) { 
     1165                                        if ($_REQUEST[$ari_key] == "on") { 
     1166                                            $custom_cfg_data_ari[$key] = 1; 
    11591167                                        } 
    11601168                                    } 
    11611169                                } 
    11621170                            } 
    1163                         } elseif ($config_options['type'] == 'loop_line_options') { 
    1164                             for($a=1;$a<=$row['max_lines'];$a++) { 
    1165                                 foreach($config_options['data']['item'] as $items) { 
    1166                                     if(isset($items['description'])) { 
    1167                                         $items['description'] = str_replace('{$count}',$a,$items['description']); 
    1168                                         $temping = "line|".$a."|".str_replace('$','',$items['variable']); 
    1169                                         $temping_ari = "ari_" . $temping; 
    1170                                         if(array_key_exists($temping, $_REQUEST)) { 
    1171                                             $custom_cfg_data[$temping] = $_REQUEST[$temping]; 
    1172                                             if(array_key_exists($temping_ari, $_REQUEST)) { 
    1173                                                 if($_REQUEST[$temping_ari] == "on") { 
    1174                                                     $custom_cfg_data_ari[$temping] = 1; 
    1175                                                 } 
    1176                                             } 
     1171                            break; 
     1172                        case "lineloop": 
     1173                            foreach ($config_options as $item_key => $item_data) { 
     1174                                $lc = isset($item_data['line_count']) ? $item_data['line_count'] : ''; 
     1175                                $key = 'line|' . $lc . '|' . $item_key; 
     1176                                if ((isset($item_data['line_count'])) AND (isset($_REQUEST[$key]))) { 
     1177                                    $custom_cfg_data[$key] = $_REQUEST[$key]; 
     1178                                    $ari_key = "ari_" . $key; 
     1179                                    if (isset($_REQUEST[$ari_key])) { 
     1180                                        if ($_REQUEST[$ari_key] == "on") { 
     1181                                            $custom_cfg_data_ari[$key] = 1; 
    11771182                                        } 
    11781183                                    } 
    11791184                                } 
    11801185                            } 
    1181                         } 
    1182                     } 
    1183                 } 
    1184             } 
    1185         } 
    1186  
    1187         $config_files = explode(",",$row['config_files']); 
     1186                            break; 
     1187                        case "option": 
     1188                            if (isset($_REQUEST[$key])) { 
     1189                                $custom_cfg_data[$key] = $_REQUEST[$key]; 
     1190                                $ari_key = "ari_" . $key; 
     1191                                if (isset($_REQUEST[$ari_key])) { 
     1192                                    if ($_REQUEST[$ari_key] == "on") { 
     1193                                        $custom_cfg_data_ari[$key] = 1; 
     1194                                    } 
     1195                                } 
     1196                            } 
     1197                            break; 
     1198                        default: 
     1199                            break; 
     1200                    } 
     1201                } 
     1202            } 
     1203        } 
     1204 
     1205        $config_files = explode(",", $row['config_files']); 
    11881206 
    11891207        $i = 0; 
    1190         while($i < count($config_files)) { 
    1191             $config_files[$i] = str_replace(".","_",$config_files[$i]); 
    1192             if(isset($_REQUEST[$config_files[$i]])) { 
    1193                 $_REQUEST[$config_files[$i]] = explode("_",$_REQUEST[$config_files[$i]], 2); 
     1208        while ($i < count($config_files)) { 
     1209            $config_files[$i] = str_replace(".", "_", $config_files[$i]); 
     1210            if (isset($_REQUEST[$config_files[$i]])) { 
     1211                $_REQUEST[$config_files[$i]] = explode("_", $_REQUEST[$config_files[$i]], 2); 
    11941212                $_REQUEST[$config_files[$i]] = $_REQUEST[$config_files[$i]][0]; 
    1195                 if($_REQUEST[$config_files[$i]] > 0) { 
     1213                if ($_REQUEST[$config_files[$i]] > 0) { 
    11961214                    $config_files_selected[$config_files[$i]] = $_REQUEST[$config_files[$i]]; 
    11971215                } 
     
    12001218        } 
    12011219 
    1202         if(!isset($config_files_selected)) { 
     1220        if (!isset($config_files_selected)) { 
    12031221            $config_files_selected = ""; 
    12041222        } else { 
     
    12071225        $custom_cfg_data_temp['data'] = $custom_cfg_data; 
    12081226        $custom_cfg_data_temp['ari'] = $custom_cfg_data_ari; 
     1227 
    12091228        $save = serialize($custom_cfg_data_temp); 
    12101229 
    1211         if($custom == "0") { 
    1212             $sql = 'UPDATE endpointman_template_list SET config_files_override = \''.addslashes($config_files_selected).'\', global_custom_cfg_data = \''.addslashes($save).'\' WHERE id ='.$id; 
     1230        if ($custom == "0") { 
     1231            $sql = 'UPDATE endpointman_template_list SET config_files_override = \'' . addslashes($config_files_selected) . '\', global_custom_cfg_data = \'' . addslashes($save) . '\' WHERE id =' . $id; 
    12131232            $location = "template_manager"; 
    12141233        } else { 
    1215             $sql = 'UPDATE endpointman_mac_list SET config_files_override = \''.addslashes($config_files_selected).'\', template_id = 0, global_custom_cfg_data = \''.addslashes($save).'\' WHERE id ='.$id; 
     1234            $sql = 'UPDATE endpointman_mac_list SET config_files_override = \'' . addslashes($config_files_selected) . '\', template_id = 0, global_custom_cfg_data = \'' . addslashes($save) . '\' WHERE id =' . $id; 
    12161235            $location = "devices_manager"; 
    12171236        } 
    1218    
     1237 
    12191238        $this->eda->sql($sql); 
    12201239 
    12211240        $phone_info = array(); 
    12221241 
    1223         if($custom != 0) { 
     1242        if ($custom != 0) { 
    12241243            $phone_info = $this->get_phone_info($id); 
    1225             if(isset($_REQUEST['epm_reboot'])) { 
     1244            if (isset($_REQUEST['epm_reboot'])) { 
    12261245                $this->prepare_configs($phone_info); 
    12271246            } else { 
    1228                 $this->prepare_configs($phone_info,FALSE); 
    1229             } 
    1230         } else { 
    1231             $sql = 'SELECT id FROM endpointman_mac_list WHERE template_id = '.$id; 
     1247                $this->prepare_configs($phone_info, FALSE); 
     1248            } 
     1249        } else { 
     1250            $sql = 'SELECT id FROM endpointman_mac_list WHERE template_id = ' . $id; 
    12321251            $phones = $this->eda->sql($sql, 'getAll', DB_FETCHMODE_ASSOC); 
    1233             foreach($phones as $data) { 
     1252            foreach ($phones as $data) { 
    12341253                $phone_info = $this->get_phone_info($data['id']); 
    1235                 if(isset($_REQUEST['epm_reboot'])) { 
     1254                if (isset($_REQUEST['epm_reboot'])) { 
    12361255                    $this->prepare_configs($phone_info); 
    12371256                } else { 
    1238                     $this->prepare_configs($phone_info,FALSE); 
    1239                 } 
    1240             } 
    1241         } 
    1242  
    1243         if(isset($_REQUEST['silent_mode'])) { 
     1257                    $this->prepare_configs($phone_info, FALSE); 
     1258                } 
     1259            } 
     1260        } 
     1261 
     1262        if (isset($_REQUEST['silent_mode'])) { 
    12441263            echo '<script language="javascript" type="text/javascript">window.close();</script>'; 
    12451264        } else { 
    12461265            return($location); 
    12471266        } 
    1248  
     1267    } 
     1268 
     1269    function write_configs($provisioner_lib, $reboot, $write_path, $phone_info, $returned_data) { 
     1270        //Create Directory Structure (If needed) 
     1271        if (isset($provisioner_lib->directory_structure)) { 
     1272            foreach ($provisioner_lib->directory_structure as $data) { 
     1273                $dir = $write_path . $data; 
     1274                if (!file_exists($dir)) { 
     1275                    if (!@mkdir($dir, 0755)) { 
     1276                        $this->error['parse_configs'] = "Could Not Create Directory: " . $data; 
     1277                        return(FALSE); 
     1278                    } 
     1279                } 
     1280            } 
     1281        } 
     1282 
     1283        //Copy Files/Directories (If needed) 
     1284        if (isset($provisioner_lib->copy_files)) { 
     1285            foreach ($provisioner_lib->copy_files as $data) { 
     1286                if (((file_exists($write_path . $data)) AND (!in_array($data, $provisioner_lib->protected_files))) OR (!file_exists($write_path . $data))) { 
     1287                    if (is_dir(PHONE_MODULES_PATH . "endpoint/" . $phone_info['directory'] . "/" . $phone_info['cfg_dir'] . "/" . $data)) { 
     1288                        if (!file_exists($write_path . $data)) { 
     1289                            if (!@mkdir($write_path . $data, 0666)) { 
     1290                                $this->error['parse_configs'] = "Could Not Create Directory: " . $data; 
     1291                                return(FALSE); 
     1292                            } 
     1293                        } 
     1294                        $dir_iterator = new RecursiveDirectoryIterator(PHONE_MODULES_PATH . "endpoint/" . $phone_info['directory'] . "/" . $phone_info['cfg_dir'] . "/" . $data . "/"); 
     1295                        $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); 
     1296                        // could use CHILD_FIRST if you so wish 
     1297                        foreach ($iterator as $file) { 
     1298                            if (is_dir($file)) { 
     1299                                $dir = str_replace(PHONE_MODULES_PATH . "endpoint/" . $phone_info['directory'] . "/" . $phone_info['cfg_dir'] . "/" . $data . "/", "", $file); 
     1300                                if (!file_exists($write_path . $data . "/" . $dir)) { 
     1301                                    if (!@mkdir($write_path . $data . "/" . $dir, 0666)) { 
     1302                                        $this->error['parse_configs'] = "Could Not Create Directory: " . $data . "/" . $dir; 
     1303                                        return(FALSE); 
     1304                                    } 
     1305                                } 
     1306                            } else { 
     1307                                $dir = str_replace(PHONE_MODULES_PATH . "endpoint/" . $phone_info['directory'] . "/" . $phone_info['cfg_dir'] . "/" . $data . "/", "", $file); 
     1308                                if (!@copy($file, $write_path . $data . "/" . $dir)) { 
     1309                                    $this->error['parse_configs'] = "Could Not Copy File: " . $data . "/" . $dir; 
     1310                                    return(FALSE); 
     1311                                } else { 
     1312                                    chmod($write_path . $data . "/" . $dir, 0666); 
     1313                                } 
     1314                            } 
     1315                        } 
     1316                    } else { 
     1317                        copy(PHONE_MODULES_PATH . "endpoint/" . $phone_info['directory'] . "/" . $phone_info['cfg_dir'] . "/" . $data, $write_path . $data); 
     1318                        chmod($write_path . $data, 0666); 
     1319                    } 
     1320                } 
     1321            } 
     1322        } 
     1323 
     1324        foreach ($returned_data as $file => $data) { 
     1325            if (((file_exists($write_path . $file)) AND (is_writable($write_path . $file)) AND (!in_array($file, $provisioner_lib->protected_files))) OR (!file_exists($write_path . $file))) { 
     1326                file_put_contents($write_path . $file, $data); 
     1327                chmod($write_path . $file, 0666); 
     1328                if (!file_exists($write_path . $file)) { 
     1329                    $this->error['parse_configs'] = "File not written to hard drive!"; 
     1330                    return(FALSE); 
     1331                } 
     1332            } elseif (!in_array($file, $provisioner_lib->protected_files)) { 
     1333                $this->error['parse_configs'] = "File not written to hard drive!"; 
     1334                return(FALSE); 
     1335            } 
     1336        } 
     1337 
     1338        if ($reboot) { 
     1339            $provisioner_lib->reboot(); 
     1340        } 
     1341    } 
     1342 
     1343    function display_configs() { 
     1344         
    12491345    } 
    12501346 
     
    12551351     * @param bool  $write  Write out Directory structure. 
    12561352     */ 
    1257     function prepare_configs($phone_info,$reboot=TRUE,$write=TRUE) { 
    1258  
    1259         if(file_exists(PHONE_MODULES_PATH.'setup.php')) { 
    1260             if(!class_exists('ProvisionerConfig')) { 
    1261                 require(PHONE_MODULES_PATH.'setup.php'); 
     1353    function prepare_configs($phone_info, $reboot=TRUE, $write=TRUE) { 
     1354 
     1355        if (file_exists(PHONE_MODULES_PATH . 'setup.php')) { 
     1356            if (!class_exists('ProvisionerConfig')) { 
     1357                require(PHONE_MODULES_PATH . 'setup.php'); 
    12621358            } 
    12631359 
    12641360            //Load Provisioner 
    12651361            $class = "endpoint_" . $phone_info['directory'] . "_" . $phone_info['cfg_dir'] . '_phone'; 
    1266             $base_class = "endpoint_" . $phone_info['directory']. '_base'; 
     1362            $base_class = "endpoint_" . $phone_info['directory'] . '_base'; 
    12671363            $master_class = "endpoint_base"; 
    1268             /**Quick Fix for FreePBX Distro 
    1269       * I seriously want to figure out why ONLY the FreePBX Distro can't do autoloads. 
    1270       **/ 
    1271             if(!class_exists($master_class)) { 
     1364            if (!class_exists($master_class)) { 
    12721365                ProvisionerConfig::endpointsAutoload($master_class); 
    12731366            } 
    1274             if(!class_exists($base_class)) { 
     1367            if (!class_exists($base_class)) { 
    12751368                ProvisionerConfig::endpointsAutoload($base_class); 
    12761369            } 
    1277             if(!class_exists($class)) { 
     1370            if (!class_exists($class)) { 
    12781371                ProvisionerConfig::endpointsAutoload($class); 
    12791372            } 
    1280             //end quick fix 
    1281              
    1282             if(class_exists($class)) { 
     1373 
     1374            if (class_exists($class)) { 
    12831375                $provisioner_lib = new $class(); 
    12841376 
    12851377                //Determine if global settings have been overridden 
    1286                 if($phone_info['template_id'] > 0) { 
    1287                     if(isset($phone_info['template_data_info']['global_settings_override'])) { 
     1378                if ($phone_info['template_id'] > 0) { 
     1379                    if (isset($phone_info['template_data_info']['global_settings_override'])) { 
    12881380                        $settings = unserialize($phone_info['template_data_info']['global_settings_override']); 
    12891381                    } else { 
     
    12941386                    } 
    12951387                } else { 
    1296                     if(isset($phone_info['global_settings_override'])) { 
     1388                    if (isset($phone_info['global_settings_override'])) { 
    12971389                        $settings = unserialize($phone_info['global_settings_override']); 
    12981390                    } else { 
     
    13161408                $provisioner_lib->family_line = $phone_info['cfg_dir']; 
    13171409 
    1318                 //Mac Address 
    1319                 $provisioner_lib->mac = $phone_info['mac']; 
     1410 
    13201411 
    13211412                //Phone Model (Please reference family_data.xml in the family directory for a list of recognized models) 
     
    13271418                try { 
    13281419                    $provisioner_lib->DateTimeZone = new DateTimeZone($settings['tz']); 
    1329                 } catch(Exception $e) { 
    1330                     $this->error['parse_configs'] = 'Caught exception: '. $e->getMessage(); 
     1420                } catch (Exception $e) { 
     1421                    $this->error['parse_configs'] = 'Error Returned From Timezone Library: ' . $e->getMessage(); 
    13311422                    return(FALSE); 
    13321423                } 
    1333  
    1334                 //Network Time Server 
    1335                 $provisioner_lib->ntp = $settings['ntp']; 
    1336  
    1337                 //Server IP 
    1338                 $provisioner_lib->server[1]['ip'] = $settings['srvip']; 
    1339                 $provisioner_lib->server[1]['port'] = 5060; 
    13401424 
    13411425                $temp = ""; 
    13421426                $template_data = unserialize($phone_info['template_data']); 
    13431427                $global_user_cfg_data = unserialize($phone_info['global_user_cfg_data']); 
    1344                 if($phone_info['template_id'] > 0) { 
     1428                if ($phone_info['template_id'] > 0) { 
    13451429                    $global_custom_cfg_data = unserialize($phone_info['template_data_info']['global_custom_cfg_data']); 
    13461430                    //Provide alternate Configuration file instead of the one from the hard drive 
    1347                     if(!empty($phone_info['template_data_info']['config_files_override'])) { 
     1431                    if (!empty($phone_info['template_data_info']['config_files_override'])) { 
    13481432                        $temp = unserialize($phone_info['template_data_info']['config_files_override']); 
    1349                         foreach($temp as $list) { 
    1350                             $sql = "SELECT original_name,data FROM endpointman_custom_configs WHERE id = ".$list; 
     1433                        foreach ($temp as $list) { 
     1434                            $sql = "SELECT original_name,data FROM endpointman_custom_configs WHERE id = " . $list; 
    13511435                            $res = $this->eda->sql($sql); 
    1352                             if($res->numRows()) { 
    1353                                 $data = $this->eda->sql($sql, 'getRow',DB_FETCHMODE_ASSOC); 
     1436                            if ($res->numRows()) { 
     1437                                $data = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
    13541438                                $provisioner_lib->config_files_override[$data['original_name']] = $data['data']; 
    13551439                            } 
     
    13591443                    $global_custom_cfg_data = unserialize($phone_info['global_custom_cfg_data']); 
    13601444                    //Provide alternate Configuration file instead of the one from the hard drive 
    1361                     if(!empty($phone_info['config_files_override'])) { 
     1445                    if (!empty($phone_info['config_files_override'])) { 
    13621446                        $temp = unserialize($phone_info['config_files_override']); 
    1363                         foreach($temp as $list) { 
    1364                             $sql = "SELECT original_name,data FROM endpointman_custom_configs WHERE id = ".$list; 
     1447                        foreach ($temp as $list) { 
     1448                            $sql = "SELECT original_name,data FROM endpointman_custom_configs WHERE id = " . $list; 
    13651449                            $res = $this->eda->sql($sql); 
    1366                             if($res->numRows()) { 
    1367                                 $data = $this->eda->sql($sql, 'getRow',DB_FETCHMODE_ASSOC); 
     1450                            if ($res->numRows()) { 
     1451                                $data = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
    13681452                                $provisioner_lib->config_files_override[$data['original_name']] = $data['data']; 
    13691453                            } 
     
    13731457 
    13741458                if (!empty($global_custom_cfg_data)) { 
    1375                     if(array_key_exists('data', $global_custom_cfg_data)) { 
     1459                    if (array_key_exists('data', $global_custom_cfg_data)) { 
    13761460                        $global_custom_cfg_ari = $global_custom_cfg_data['ari']; 
    13771461                        $global_custom_cfg_data = $global_custom_cfg_data['data']; 
     
    13841468                $new_template_data = array(); 
    13851469                $line_ops = array(); 
    1386                 if(is_array($global_custom_cfg_data)) { 
    1387                     foreach($global_custom_cfg_data as $key => $data) { 
     1470                if (is_array($global_custom_cfg_data)) { 
     1471                    foreach ($global_custom_cfg_data as $key => $data) { 
     1472                        //TODO: clean up with reg-exp 
    13881473                        $full_key = $key; 
    1389                         $key = explode('|',$key); 
     1474                        $key = explode('|', $key); 
    13901475                        $count = count($key); 
    1391                         switch($count) { 
     1476                        switch ($count) { 
    13921477                            case 1: 
    1393                                 if(($this->global_cfg['enable_ari'] == 1) AND (isset($global_custom_cfg_ari[$full_key])) AND (isset($global_user_cfg_data[$full_key]))) { 
     1478                                if (($this->global_cfg['enable_ari'] == 1) AND (isset($global_custom_cfg_ari[$full_key])) AND (isset($global_user_cfg_data[$full_key]))) { 
    13941479                                    $new_template_data[$full_key] = $global_user_cfg_data[$full_key]; 
    13951480                                } else { 
     
    13981483                                break; 
    13991484                            case 2: 
    1400                                 $breaks = explode('_',$key[1]); 
    1401                                 if(($this->global_cfg['enable_ari'] == 1) AND (isset($global_custom_cfg_ari[$full_key])) AND (isset($global_user_cfg_data[$full_key]))) { 
    1402                                     $new_template_data[$breaks[0]][$breaks[2]][$breaks[1]] = $global_user_cfg_data[$full_key]; 
     1485                                $breaks = explode('_', $key[1]); 
     1486                                if (($this->global_cfg['enable_ari'] == 1) AND (isset($global_custom_cfg_ari[$full_key])) AND (isset($global_user_cfg_data[$full_key]))) { 
     1487                                    $new_template_data['loops'][$breaks[0]][$breaks[2]][$breaks[1]] = $global_user_cfg_data[$full_key]; 
    14031488                                } else { 
    1404                                     $new_template_data[$breaks[0]][$breaks[2]][$breaks[1]] = $global_custom_cfg_data[$full_key]; 
     1489                                    $new_template_data['loops'][$breaks[0]][$breaks[2]][$breaks[1]] = $global_custom_cfg_data[$full_key]; 
    14051490                                } 
    14061491                                break; 
    14071492                            case 3: 
    1408                                 if(($this->global_cfg['enable_ari'] == 1) AND (isset($global_custom_cfg_ari[$full_key])) AND (isset($global_user_cfg_data[$full_key]))) { 
     1493                                if (($this->global_cfg['enable_ari'] == 1) AND (isset($global_custom_cfg_ari[$full_key])) AND (isset($global_user_cfg_data[$full_key]))) { 
    14091494                                    $line_ops[$key[1]][$key[2]] = $global_user_cfg_data[$full_key]; 
    14101495                                } else { 
     
    14161501                } 
    14171502 
    1418                 //Loop through Lines! 
    1419                 foreach($phone_info['line'] as $line) { 
    1420                     $provisioner_lib->lines[$line['line']] = array('ext' => $line['ext'], 'secret' => $line['secret'], 'displayname' => $line['description']); 
    1421                 } 
    1422  
    1423         //testing this out 
    1424                 foreach ($line_ops as $key => $data) { 
    1425                     if (isset($line_ops[$key])) { 
    1426                         $provisioner_lib->lines[$key]['options'] = $line_ops[$key]; 
    1427                     } 
    1428                 } 
    1429  
    14301503                if (!$write) { 
    1431                     $provisioner_lib->server_type = 'dynamic'; 
    1432                     $provisioner_lib->provisioning_type = 'http'; 
    1433                     $new_template_data['provisioning_path'] = $_SERVER["SERVER_ADDR"]."/provisioning/"; 
    1434                 } 
     1504                    $new_template_data['provision']['type'] = 'dynamic'; 
     1505                    $new_template_data['provision']['protocol'] = 'http'; 
     1506                    $new_template_data['provision']['path'] = $_SERVER["SERVER_ADDR"] . dirname($_SERVER['REQUEST_URI']) . '/'; 
     1507                    $new_template_data['provision']['encryption'] = FALSE; 
     1508                } else { 
     1509                    $new_template_data['provision']['type'] = 'file'; 
     1510                    $new_template_data['provision']['protocol'] = 'tftp'; 
     1511                    $new_template_data['provision']['path'] = $_SERVER["SERVER_ADDR"]; 
     1512                    $new_template_data['provision']['encryption'] = FALSE; 
     1513                } 
     1514 
     1515                $new_template_data['ntp'] = $settings['ntp']; 
     1516 
     1517                $new_template_data['network']['dhcp'] = TRUE; 
     1518                $new_template_data['network']['ipv4'] = ''; 
     1519                $new_template_data['network']['ipv6'] = ''; 
     1520                $new_template_data['network']['subnet'] = '255.255.255.0'; 
     1521                $new_template_data['network']['gateway'] = ''; 
     1522                $new_template_data['network']['vlan']['id'] = ''; 
     1523                $new_template_data['network']['vlan']['qos'] = ''; 
     1524                 
     1525                //Overwrite all specific settings variables now 
     1526                if (!empty($phone_info['specific_settings'])) { 
     1527                    $specific_settings = unserialize($phone_info['specific_settings']); 
     1528                    $specific_settings = is_array($specific_settings) ? $specific_settings : array(); 
     1529                } else { 
     1530                    $specific_settings = array(); 
     1531                } 
     1532                 
    14351533 
    14361534                //Set Variables according to the template_data files included. We can include different template.xml files within family_data.xml also one can create 
    14371535                //template_data_custom.xml which will get included or template_data_<model_name>_custom.xml which will also get included 
    14381536                //line 'global' will set variables that aren't line dependant 
    1439                 $provisioner_lib->options = $new_template_data; 
     1537                
     1538                 
     1539                $provisioner_lib->settings = $new_template_data; 
     1540 
     1541                //Loop through Lines! 
     1542                $li = 0; 
     1543                foreach ($phone_info['line'] as $line) { 
     1544                    $line_options = is_array($line_ops[$line['line']]) ? $line_ops[$line['line']] : array(); 
     1545                    $line_statics = array('line' => $line['line'], 'username' => $line['ext'], 'authname' => $line['ext'], 'secret' => $line['secret'], 'displayname' => $line['description'], 'server_host' => $this->global_cfg['srvip'], 'server_port' => '5060'); 
     1546                     
     1547                    $provisioner_lib->settings['line'][$li] = array_merge($line_options, $line_statics); 
     1548                    $li++; 
     1549                } 
     1550                 
     1551                foreach($specific_settings['data'] as $key => $data) { 
     1552                    $default_exp = preg_split("/\|/i", $key); 
     1553                    if(isset($default_exp[2])) { 
     1554                        //lineloop 
     1555                        $var = $default_exp[2]; 
     1556                        $line = $default_exp[1]; 
     1557                        $loc = $this->arraysearchrecursive($line, $provisioner_lib->settings['line'], 'line'); 
     1558                        if($loc !== FALSE) { 
     1559                            $k = $loc[0]; 
     1560                            $provisioner_lib->settings['line'][$k][$var] = $data; 
     1561                        } else { 
     1562                            //Adding a new line-ish type options 
     1563                            if(isset($specific_settings['data']['line|'.$line.'|line_enabled'])) { 
     1564                                $lastkey = array_pop(array_keys($provisioner_lib->settings['line'])); 
     1565                                $lastkey++; 
     1566                                $provisioner_lib->settings['line'][$lastkey]['line'] = $line; 
     1567                                $provisioner_lib->settings['line'][$lastkey][$var] = $data; 
     1568                            } 
     1569                        } 
     1570                    } else { 
     1571                        $provisioner_lib->settings[$var] = $data; 
     1572                    } 
     1573                } 
     1574 
     1575                $provisioner_lib->settings['mac'] = $phone_info['mac']; 
     1576                $provisioner_lib->mac = $phone_info['mac']; 
    14401577 
    14411578                //Setting a line variable here...these aren't defined in the template_data.xml file yet. however they will still be parsed 
    14421579                //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 
    1443                 $provisioner_lib->processor_info = "EndPoint Manager Version ".$this->global_cfg['version']; 
     1580                $provisioner_lib->processor_info = "EndPoint Manager Version " . $this->global_cfg['version']; 
    14441581 
    14451582                // Because every brand is an extension (eventually) of endpoint, you know this function will exist regardless of who it is 
    14461583                //Start timer 
    14471584                $time_start = microtime(true); 
    1448                 $returned_data = $provisioner_lib->generate_config(); 
     1585 
     1586                $provisioner_lib->debug = TRUE; 
     1587 
     1588                try { 
     1589                    $returned_data = $provisioner_lib->generate_all_files(); 
     1590                } catch (Exception $e) { 
     1591                    $this->error['prepare_configs'] = 'Error Returned From Provisioner Library: '.  $e->getMessage(); 
     1592                    return(FALSE); 
     1593                } 
     1594                 
     1595 
     1596                //print_r($provisioner_lib->debug_return); 
    14491597                //End timer 
    14501598                $time_end = microtime(true); 
    14511599                $time = $time_end - $time_start; 
    1452                 if($time > 360) { 
    1453                     $this->error['generate_time'] = "It took an awfully long time to generate configs...(".round($time,2)." seconds)"; 
    1454                 } 
    1455                 if($write) { 
    1456                     //Create Directory Structure (If needed) 
    1457                     if(isset($provisioner_lib->directory_structure)) { 
    1458                         foreach($provisioner_lib->directory_structure as $data) { 
    1459                             $dir = $settings['config_location'] . $data; 
    1460                             if(!file_exists($dir)) { 
    1461                                 mkdir($dir, 0755); 
    1462                             } 
    1463                         } 
    1464                     } 
    1465  
    1466                     //Copy Files/Directories (If needed) 
    1467                     if(isset($provisioner_lib->copy_files)) { 
    1468                         foreach($provisioner_lib->copy_files as $data) { 
    1469                             if(((file_exists($settings['config_location'].$data)) AND (!in_array($data,$provisioner_lib->protected_files))) OR (!file_exists($settings['config_location'].$data))) { 
    1470                                 if(is_dir(PHONE_MODULES_PATH."endpoint/".$phone_info['directory']."/".$phone_info['cfg_dir']."/".$data)) { 
    1471                                     if(!file_exists($settings['config_location'].$data)) { 
    1472                                         if (!@mkdir($settings['config_location'].$data, 0666)) { 
    1473                                             $this->error['parse_configs'] = "Could Not Create Directory: ".$data; 
    1474                                             return(FALSE); 
    1475                                         } 
    1476                                     } 
    1477                                     $dir_iterator = new RecursiveDirectoryIterator(PHONE_MODULES_PATH."endpoint/".$phone_info['directory']."/".$phone_info['cfg_dir']."/".$data."/"); 
    1478                                     $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); 
    1479                                     // could use CHILD_FIRST if you so wish 
    1480                                     foreach ($iterator as $file) { 
    1481                                         if(is_dir($file)) { 
    1482                                             $dir = str_replace(PHONE_MODULES_PATH."endpoint/".$phone_info['directory']."/".$phone_info['cfg_dir']."/".$data."/", "", $file); 
    1483                                             if(!file_exists($settings['config_location'].$data."/".$dir)) { 
    1484                                                 if (!@mkdir($settings['config_location'].$data."/".$dir, 0666)) { 
    1485                                                     $this->error['parse_configs'] = "Could Not Create Directory: ".$data."/".$dir; 
    1486                                                     return(FALSE); 
    1487                                                 } 
    1488                                             } 
    1489                                         } else { 
    1490                                             $dir = str_replace(PHONE_MODULES_PATH."endpoint/".$phone_info['directory']."/".$phone_info['cfg_dir']."/".$data."/", "", $file); 
    1491                                             if(!@copy($file, $settings['config_location'].$data."/".$dir)) { 
    1492                                                 $this->error['parse_configs'] = "Could Not Copy File: ".$data."/".$dir; 
    1493                                                 return(FALSE); 
    1494                                             } else { 
    1495                                                 chmod($settings['config_location'].$data."/".$dir, 0666); 
    1496                                             } 
    1497                                         } 
    1498                                     } 
    1499                                 } else { 
    1500                                     copy(PHONE_MODULES_PATH."endpoint/".$phone_info['directory']."/".$phone_info['cfg_dir']."/".$data,$settings['config_location'].$data); 
    1501                                     chmod($settings['config_location'].$data, 0666); 
    1502                                 } 
    1503                             } 
    1504                         } 
    1505                     } 
    1506  
    1507                     //Generate Files 
    1508                     foreach($returned_data as $key => $data) { 
    1509                         if(((file_exists($settings['config_location'].$key)) AND (is_writable($settings['config_location'].$key)) AND (!in_array($key,$provisioner_lib->protected_files))) OR (!file_exists($settings['config_location'].$key))) { 
    1510                             $fp = fopen($settings['config_location'].$key, 'w'); 
    1511                             fwrite($fp, $data); 
    1512                             fclose($fp); 
    1513                             chmod($settings['config_location'].$key, 0666); 
    1514                             if(!file_exists($settings['config_location'].$key)) { 
    1515                                 $this->error['parse_configs'] = "File not written to hard drive!"; 
    1516                                 return(FALSE); 
    1517                             } 
    1518                         } elseif(!in_array($key,$provisioner_lib->protected_files)) { 
    1519                             $this->error['parse_configs'] = "File not written to hard drive!"; 
    1520                             return(FALSE); 
    1521                         } 
    1522                     } 
    1523                     if($reboot) { 
    1524                         $provisioner_lib->reboot(); 
    1525                     } 
     1600                if ($time > 360) { 
     1601                    $this->error['generate_time'] = "It took an awfully long time to generate configs...(" . round($time, 2) . " seconds)"; 
     1602                } 
     1603                if ($write) { 
     1604                    $this->write_configs($provisioner_lib, $reboot, $settings['config_location'], $phone_info, $returned_data); 
    15261605                } else { 
    1527                     if($reboot) { 
    1528                         $provisioner_lib->reboot(); 
    1529                     } 
    1530                     return($returned_data); 
     1606                    return ($returned_data); 
    15311607                } 
    15321608                return(TRUE); 
    15331609            } else { 
    1534                 $this->error['parse_configs'] = "Can't Load \"".$class."\" Class!"; 
     1610                $this->error['parse_configs'] = "Can't Load \"" . $class . "\" Class!"; 
    15351611                return(FALSE); 
    15361612            } 
     
    15471623     */ 
    15481624    function firmware_update_check($id=NULL) { 
    1549         $sql = "SELECT * FROM  endpointman_product_list WHERE  id ='". $id."'"; 
    1550         $row = $this->eda->sql($sql,'getRow',DB_FETCHMODE_ASSOC); 
    1551  
    1552         $sql = "SELECT directory FROM  endpointman_brand_list WHERE id =". $row['brand']; 
    1553         $brand_directory = $this->eda->sql($sql,'getOne'); 
     1625        $sql = "SELECT * FROM  endpointman_product_list WHERE  id ='" . $id . "'"; 
     1626        $row = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
     1627 
     1628        $sql = "SELECT directory FROM  endpointman_brand_list WHERE id =" . $row['brand']; 
     1629        $brand_directory = $this->eda->sql($sql, 'getOne'); 
    15541630 
    15551631        //config drive unknown! 
     
    15571633            return FALSE; 
    15581634        } else { 
    1559             $temp = $this->xml2array(PHONE_MODULES_PATH."endpoint/".$brand_directory."/".$row['cfg_dir']."/family_data.xml"); 
    1560             if((array_key_exists('data',$temp)) AND (!is_array($temp['data']['firmware_ver']))) { 
    1561                 if($row['firmware_vers'] < $temp['data']['firmware_ver']) { 
     1635            $temp = $this->file2json(PHONE_MODULES_PATH . "endpoint/" . $brand_directory . "/" . $row['cfg_dir'] . "/family_data.json"); 
     1636            if ((array_key_exists('data', $temp)) AND (!is_array($temp['data']['firmware_ver']))) { 
     1637                if ($row['firmware_vers'] < $temp['data']['firmware_ver']) { 
    15621638                    return $temp; 
    15631639                } else { 
     
    15681644            } 
    15691645        } 
    1570  
    15711646    } 
    15721647 
     
    15771652     */ 
    15781653    function firmware_local_check($id=NULL) { 
    1579         $sql = "SELECT * FROM  endpointman_product_list WHERE hidden = 0 AND id ='". $id ."'"; 
     1654        $sql = "SELECT * FROM  endpointman_product_list WHERE hidden = 0 AND id ='" . $id . "'"; 
    15801655        $res = $this->eda->sql($sql); 
    15811656 
    1582         if($res->numRows()) { 
    1583             $row = $this->eda->sql($sql,'getRow',DB_FETCHMODE_ASSOC); 
    1584  
    1585             $sql = "SELECT directory FROM  endpointman_brand_list WHERE hidden = 0 AND id =". $row['brand']; 
    1586             $brand_directory = $this->eda->sql($sql,'getOne'); 
     1657        if ($res->numRows()) { 
     1658            $row = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
     1659 
     1660            $sql = "SELECT directory FROM  endpointman_brand_list WHERE hidden = 0 AND id =" . $row['brand']; 
     1661            $brand_directory = $this->eda->sql($sql, 'getOne'); 
    15871662 
    15881663            //config drive unknown! 
     
    15901665                return("nothing"); 
    15911666            } else { 
    1592                 $temp = $this->xml2array(PHONE_MODULES_PATH."endpoint/".$brand_directory."/".$row['cfg_dir']."/family_data.xml"); 
    1593  
    1594                 if((isset ($temp['data']['firmware_ver'])) AND (!is_array($temp['data']['firmware_ver']))) { 
    1595                     if($row['firmware_vers'] == "") { 
     1667                $temp = $this->file2json(PHONE_MODULES_PATH . "endpoint/" . $brand_directory . "/" . $row['cfg_dir'] . "/family_data.json"); 
     1668 
     1669                if ($temp['data']['firmware_ver'] != NULL) { 
     1670                    if ($row['firmware_vers'] == "") { 
    15961671                        return("install"); 
    15971672                    } else { 
     
    16121687     */ 
    16131688    function remove_firmware($id) { 
    1614         $sql = "SELECT firmware_files FROM  endpointman_product_list WHERE  id =". $id; 
    1615         $files = $this->eda->sql($sql,'getOne'); 
    1616  
    1617         $file_list = explode(",",$files); 
     1689        $sql = "SELECT firmware_files FROM  endpointman_product_list WHERE  id =" . $id; 
     1690        $files = $this->eda->sql($sql, 'getOne'); 
     1691 
     1692        $file_list = explode(",", $files); 
    16181693        $i = 0; 
    1619         foreach($file_list as $file) { 
    1620             if(file_exists($this->global_cfg['config_location'].$file)) { 
    1621                 unlink($this->global_cfg['config_location'].$file); 
    1622             } 
    1623         } 
    1624         $sql = 'UPDATE endpointman_product_list SET firmware_files = "", firmware_vers = "" WHERE id = '.$id; 
     1694        foreach ($file_list as $file) { 
     1695            if (file_exists($this->global_cfg['config_location'] . $file)) { 
     1696                unlink($this->global_cfg['config_location'] . $file); 
     1697            } 
     1698        } 
     1699        $sql = 'UPDATE endpointman_product_list SET firmware_files = "", firmware_vers = "" WHERE id = ' . $id; 
    16251700        $this->eda->sql($sql); 
    16261701    } 
     
    16311706     */ 
    16321707    function install_firmware($product_id) { 
    1633         $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; 
     1708        $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; 
    16341709        $row = $this->eda->sql($sql, 'getRow', DB_FETCHMODE_ASSOC); 
    1635         $temp = $this->xml2array(PHONE_MODULES_PATH."endpoint/".$row['directory']."/".$row['cfg_dir']."/family_data.xml"); 
    1636         if($temp['data']['firmware_ver'] > $row['firmware_vers']) { 
     1710        $temp = $this->file2json(PHONE_MODULES_PATH . "endpoint/" . $row['directory'] . "/" . $row['cfg_dir'] . "/family_data.json"); 
     1711        if ($temp['data']['firmware_ver'] > $row['firmware_vers']) { 
    16371712            echo "<div style='float: left'>Downloading firmware...</div><div style='float: left' id=\"DivExample\">.</div>"; 
    1638             $this->download_file_with_progress_bar(UPDATE_PATH.$row['directory']."/". $temp['data']['firmware_pkg'], PHONE_MODULES_PATH."temp/".$temp['data']['firmware_pkg']); 
     1713            $this->download_file_with_progress_bar(UPDATE_PATH . $row['directory'] . "/" . $temp['data']['firmware_pkg'], PHONE_MODULES_PATH . "temp/" . $temp['data']['firmware_pkg']); 
    16391714            echo "<script type='text/javascript'>document.getElementById('DivExample').innerHTML='Done!';</script><br />"; 
    16401715            $md5_xml = $temp['data']['firmware_md5sum']; 
    1641             $md5_pkg = md5_file(PHONE_MODULES_PATH.'temp/'. $temp['data']['firmware_pkg']); 
     1716            $md5_pkg = md5_file(PHONE_MODULES_PATH . 'temp/' . $temp['data']['firmware_pkg']); 
    16421717            echo "Checking MD5sum of Package..."; 
    16431718            if ($md5_xml == $md5_pkg) { 
    16441719                echo "Matches!<br />"; 
    1645                 if(!file_exists(PHONE_MODULES_PATH."endpoint/".$row['directory']."/".$row['cfg_dir']."/firmware")) { 
    1646                     mkdir(PHONE_MODULES_PATH."endpoint/".$row['directory']."/".$row['cfg_dir']."/firmware"); 
     1720                if (!file_exists(PHONE_MODULES_PATH . "endpoint/" . $row['directory'] . "/" . $row['cfg_dir'] . "/firmware")) { 
     1721                    mkdir(PHONE_MODULES_PATH . "endpoint/" . $row['directory'] . "/" . $row['cfg_dir'] . "/firmware"); 
    16471722                } 
    16481723                echo "Installing Firmware..."; 
    1649                 exec("rm -Rf ".PHONE_MODULES_PATH."endpoint/".$row['directory']."/".$row['cfg_dir']."/firmware"); 
    1650                 mkdir(PHONE_MODULES_PATH."endpoint/".$row['directory']."/".$row['cfg_dir']."/firmware"); 
    1651                 exec("tar -xvf ".PHONE_MODULES_PATH.'temp/'. $temp['data']['firmware_pkg'] ." -C ".PHONE_MODULES_PATH."endpoint/".$row['directory']."/".$row['cfg_dir']); 
     1724                exec("rm -Rf " . PHONE_MODULES_PATH . "endpoint/" . $row['directory'] . "/" . $row['cfg_dir'] . "/firmware"); 
     1725                mkdir(PHONE_MODULES_PATH . "endpoint/" . $row['directory'] . "/" . $row['cfg_dir'] . "/firmware"); 
     1726                exec("tar -xvf " . PHONE_MODULES_PATH . 'temp/' . $temp['data']['firmware_pkg'] . " -C " . PHONE_MODULES_PATH . "endpoint/" . $row['directory'] . "/" . $row['cfg_dir']); 
    16521727                $i = 0; 
    1653                 foreach (glob(PHONE_MODULES_PATH."endpoint/".$row['directory']."/".$row['cfg_dir']."/firmware/*.*") as $filename) { 
     1728                foreach (glob(PHONE_MODULES_PATH . "endpoint/" . $row['directory'] . "/" . $row['cfg_dir'] . "/firmware/*.*") as $filename) { 
    16541729                    $file = basename($filename); 
    16551730                    $list[$i] = $file; 
    1656                     if (!@copy($filename, $this->global_cfg['config_location'].$file)) { 
     1731                    if (!@copy($filename, $this->global_cfg['config_location'] . $file)) { 
    16571732                        echo "<br />--Failed To Copy $file..."; 
    16581733                        $copy_error = TRUE; 
    1659                     } elseif($this->global_cfg['debug']) { 
    1660                         echo"<br />--Copied ".$file." to ". $this->global_cfg['config_location']; 
     1734                    } elseif ($this->global_cfg['debug']) { 
     1735                        echo"<br />--Copied " . $file . " to " . $this->global_cfg['config_location']; 
    16611736                    } 
    16621737                    $i++; 
    16631738                } 
    16641739                $list = implode(",", $list); 
    1665                 $sql = "UPDATE endpointman_product_list SET firmware_vers = '".$temp['data']['firmware_ver']."', firmware_files = '".$list."' WHERE id = ". $row['id']; 
     1740                $sql = "UPDATE endpointman_product_list SET firmware_vers = '" . $temp['data']['firmware_ver'] . "', firmware_files = '" . $list . "' WHERE id = " . $row['id']; 
    16661741                $this->eda->sql($sql); 
    1667                 if(isset($copy_error)) { 
     1742                if (isset($copy_error)) { 
    16681743                    echo "<BR />Copy Error Detected! Aborting Install!"; 
    16691744                    $this->remove_firmware($product_id); 
     
    16771752        } else { 
    16781753            echo "<BR />Your Firmware is already up to date"; 
    1679  
    1680         } 
    1681     } 
    1682  
    1683     /** 
    1684      * Fix arrays so that they don't return an empty array if the array is empty 
    1685      * Instead we will return an empty string 
    1686      * Also we fix the problem of wanting to get single arrays with keys but not getting them 
    1687      * @param array $array 
    1688      * @return array 
    1689      */ 
    1690     function fix_single_array_keys($array) { 
    1691         if((empty($array[0])) AND (!empty($array))) { 
    1692             $array_n[0] = $array; 
    1693             return($array_n); 
    1694         } elseif(!empty($array)) { 
    1695             return($array); 
    1696         //This is so stupid?! PHP gets confused. 
    1697         } elseif($array == '0') { 
    1698             return($array); 
    1699         } else { 
    1700             return(""); 
    1701         } 
    1702     } 
    1703  
    1704     /** 
    1705      * Download the requested XML configuration files from the web 
    1706      * @param string $location full URL of the XML file 
    1707      * @param string $directory Directory to place the XML file in relation to provisioner.net 
    1708      * @return bool True if downloaded, false if failed. 
    1709      */ 
    1710     function download_xml($location,$directory=NULL) { 
    1711         //If directory is not set then assume we are working with the master.xml file 
    1712         if(!isset($directory)) { 
    1713             $destination_file = PHONE_MODULES_PATH.'master.xml'; 
     1754        } 
     1755    } 
     1756 
     1757    function download_json($location, $directory=NULL) { 
     1758        if (!isset($directory)) { 
     1759            $destination_file = PHONE_MODULES_PATH . 'master.json'; 
    17141760            $directory = "master"; 
    17151761        } else { 
    1716             if(!file_exists(PHONE_MODULES_PATH.'/'.$directory)) { 
    1717                 mkdir(PHONE_MODULES_PATH.'/'.$directory,0764,TRUE); 
    1718             }  
    1719             $destination_file = PHONE_MODULES_PATH.'/'.$directory.'/brand_data.xml'; 
    1720         } 
    1721         $temp_file = PHONE_MODULES_PATH.'temp/'.$directory.'.xml'; 
     1762            if (!file_exists(PHONE_MODULES_PATH . '/' . $directory)) { 
     1763                mkdir(PHONE_MODULES_PATH . '/' . $directory, 0764, TRUE); 
     1764            } 
     1765            $destination_file = PHONE_MODULES_PATH . '/' . $directory . '/brand_data.json'; 
     1766        } 
     1767        $temp_file = PHONE_MODULES_PATH . 'temp/' . $directory . '.json'; 
    17221768        file_exists(dirname($temp_file)) ? '' : mkdir(dirname($temp_file)); 
    1723                  
    1724         if($this->download_xml_file($location, $temp_file)) { 
     1769 
     1770        if ($this->download_file($location, $temp_file)) { 
    17251771            $handle = fopen($temp_file, "rb"); 
    17261772            $contents = fread($handle, filesize($temp_file)); 
    17271773            fclose($handle); 
    1728              
    1729             @$a = simplexml_load_string($contents); 
    1730             if($a===FALSE) { 
     1774 
     1775            $a = $this->validate_json($contents); 
     1776            if ($a === FALSE) { 
    17311777                //Error with the internet....ABORRRTTTT THEEEEE DOWNLOAAAAADDDDDDDD! SCOTTYYYY!; 
    17321778                unlink($temp_file); 
     
    17341780            } else { 
    17351781                rename($temp_file, $destination_file); 
    1736                 chmod($destination_file,0764); 
     1782                chmod($destination_file, 0764); 
    17371783                return(TRUE); 
    17381784            } 
     
    17481794     */ 
    17491795    function update_check() { 
    1750         $master_result = $this->download_xml(UPDATE_PATH . "master.xml"); 
     1796        $master_result = $this->download_file(UPDATE_PATH . "master.json", PHONE_MODULES_PATH . "master.json"); 
     1797 
    17511798         
    17521799         
    1753         if(!$master_result) { 
     1800        if (!$master_result) { 
    17541801            $this->error['brand_update_check_master'] = "Not able to connect to repository. Using local master file instead."; 
    17551802        } 
    1756          
    1757         $temp = $this->xml2array(PHONE_MODULES_PATH.'master.xml'); 
     1803 
     1804        $temp = $this->file2json(PHONE_MODULES_PATH . 'master.json'); 
    17581805        $endpoint_package = $temp['data']['package']; 
    17591806        $endpoint_last_mod = $temp['data']['last_modified']; 
    17601807 
    17611808        $sql = "SELECT value FROM endpointman_global_vars WHERE var_name LIKE 'endpoint_vers'"; 
    1762         $data = $this->eda->sql($sql,'getOne'); 
    1763          
    1764         $contents = file_get_contents(UPDATE_PATH."/update_status"); 
    1765  
    1766          
    1767         if($contents != '1') { 
    1768             if(($data == "") OR ($data <= $endpoint_last_mod)) { 
    1769                 if((!$master_result) OR (!$this->download_file_no_progress_bar(UPDATE_PATH.'/'.$endpoint_package, PHONE_MODULES_PATH."temp/".$endpoint_package))) { 
    1770                     $this->error['brand_update_check_xml'] = "<br/>Not able to connect to repository. Using local Provisioner.net Package"; 
     1809        $data = $this->eda->sql($sql, 'getOne'); 
     1810 
     1811        $contents = file_get_contents(UPDATE_PATH . "/update_status"); 
     1812 
     1813 
     1814        if ($contents != '1') { 
     1815            if (($data == "") OR ($data <= $endpoint_last_mod)) { 
     1816                if ((!$master_result) OR (!$this->download_file(UPDATE_PATH . '/' . $endpoint_package, PHONE_MODULES_PATH . "temp/" . $endpoint_package))) { 
     1817                    $this->error['brand_update_check_json'] = "<br/>Not able to connect to repository. Using local Provisioner.net Package"; 
    17711818                } else { 
    1772                     exec("tar -xvf ".PHONE_MODULES_PATH.'temp/'. $endpoint_package ." -C ".PHONE_MODULES_PATH."temp/"); 
    1773  
    1774                     if(!file_exists(PHONE_MODULES_PATH."endpoint")) { 
    1775                         mkdir(PHONE_MODULES_PATH."endpoint"); 
    1776                     } 
    1777  
    1778                     unlink(PHONE_MODULES_PATH."temp/setup.php"); 
    1779                     rename(PHONE_MODULES_PATH."temp/endpoint/base.php", PHONE_MODULES_PATH."endpoint/base.php"); 
    1780  
    1781                     $sql = "UPDATE endpointman_global_vars SET value = '".$endpoint_last_mod."' WHERE var_name = 'endpoint_vers'"; 
     1819                    exec("tar -xvf " . PHONE_MODULES_PATH . 'temp/' . $endpoint_package . " -C " . PHONE_MODULES_PATH . "temp/"); 
     1820 
     1821                    if (!file_exists(PHONE_MODULES_PATH . "endpoint")) { 
     1822                        mkdir(PHONE_MODULES_PATH . "endpoint"); 
     1823                    } 
     1824 
     1825                    //TODO: Automate this somehow... 
     1826                    unlink(PHONE_MODULES_PATH . "temp/setup.php"); 
     1827                    rename(PHONE_MODULES_PATH . "temp/endpoint/base.php", PHONE_MODULES_PATH . "endpoint/base.php"); 
     1828                    rename(PHONE_MODULES_PATH . "temp/endpoint/global_template_data.json", PHONE_MODULES_PATH . "endpoint/global_template_data.json"); 
     1829 
     1830                    $sql = "UPDATE endpointman_global_vars SET value = '" . $endpoint_last_mod . "' WHERE var_name = 'endpoint_vers'"; 
    17821831                    $this->eda->sql($sql); 
    17831832                } 
     
    17881837 
    17891838            //Assume that if we can't connect and find the master.xml file then why should we try to find every other file. 
    1790             if($master_result) { 
     1839            if ($master_result) { 
    17911840                $row = $this->eda->sql('SELECT * FROM  endpointman_brand_list WHERE id > 0', 'getAll', DB_FETCHMODE_ASSOC); 
    17921841 
    1793                 foreach($out as $data) { 
    1794  
    1795                     $local = $this->eda->sql("SELECT local FROM endpointman_brand_list WHERE  directory =  '".$data['directory']."'",'getOne'); 
    1796  
    1797                     if(!$local) { 
    1798                         $result = $this->download_xml(UPDATE_PATH .$data['directory']."/".$data['directory'].".xml","endpoint/".$data['directory']); 
    1799                         if(!$result) { 
    1800                             $this->error['brand_update_check'] = "<br/>Not able to connect to repository. Using local brand [".$data['name']."] file instead."; 
     1842                foreach ($out as $data) { 
     1843 
     1844                    $local = $this->eda->sql("SELECT local FROM endpointman_brand_list WHERE  directory =  '" . $data['directory'] . "'", 'getOne'); 
     1845 
     1846                    if (!$local) { 
     1847                        $result = $this->download_file(UPDATE_PATH . $data['directory'] . "/" . $data['directory'] . ".json", PHONE_MODULES_PATH . "endpoint/" . $data['directory'] . "/brand_data.json"); 
     1848                        if (!$result) { 
     1849                            $this->error['brand_update_check'] = "<br/>Not able to connect to repository. Using local brand [" . $data['name'] . "] file instead."; 
    18011850                        } 
    18021851                    } 
    18031852 
    1804                     if(file_exists(PHONE_MODULES_PATH."endpoint/".$data['directory']."/brand_data.xml")) { 
    1805                         $temp = $this->xml2array(PHONE_MODULES_PATH."endpoint/".$data['directory']."/brand_data.xml"); 
     1853                    if (file_exists(PHONE_MODULES_PATH . "endpoint/" . $data['directory'] . "/brand_data.json")) { 
     1854                        $temp = $this->file2json(PHONE_MODULES_PATH . "endpoint/" . $data['directory'] . "/brand_data.json"); 
    18061855 
    18071856                        $temp = $temp['data']['brands']; 
    18081857 
    1809                         $temp['oui_list']['oui'] = $this->fix_single_array_keys($temp['oui_list']['oui']); 
    1810  
    1811                         foreach($temp['oui_list']['oui'] as $oui) { 
    1812                             $sql = "REPLACE INTO endpointman_oui_list (`oui`, `brand`, `custom`) VALUES ('".$oui."', '".$temp['brand_id']."', '0')"; 
     1858                        foreach ($temp['oui_list'] as $oui) { 
     1859                            $sql = "REPLACE INTO endpointman_oui_list (`oui`, `brand`, `custom`) VALUES ('" . $oui . "', '" . $temp['brand_id'] . "', '0')"; 
    18131860                            $this->eda->sql($sql); 
    1814  
    18151861                        } 
    18161862 
     
    18201866                        $last_mod = ""; 
    18211867 
    1822                         $temp['family_list']['family'] = $this->fix_single_array_keys($temp['family_list']['family']); 
    1823  
    1824                         foreach($temp['family_list']['family'] as $list) { 
     1868                        foreach ($temp['family_list'] as $list) { 
    18251869                            $last_mod = max($last_mod, $list['last_modified']); 
    18261870                        } 
     
    18291873                        $version[$brand_name] = $last_mod; 
    18301874 
    1831                         if(!($this->arraysearchrecursive($brand_name, $row, 'directory'))) { 
     1875                        if (!($this->arraysearchrecursive($brand_name, $row, 'directory'))) { 
    18321876                            //insert row 
    1833                             $sql = "INSERT INTO endpointman_brand_list (id, name, directory, cfg_ver) VALUES ('".$temp['brand_id']."', '".$temp['name']."', '".$temp['directory']."', '".$version[$brand_name]."')"; 
     1877                            $sql = "INSERT INTO endpointman_brand_list (id, name, directory, cfg_ver) VALUES ('" . $temp['brand_id'] . "', '" . $temp['name'] . "', '" . $temp['directory'] . "', '" . $version[$brand_name] . "')"; 
    18341878                            $this->eda->sql($sql); 
    18351879                        } else { 
     
    18371881                        } 
    18381882                    } else { 
    1839                         $this->error['brand_update_check_local_file'] = "<br/>Error: No Local File for ".$data['name']."!<br /><br />Learn how to manually upload packages here (it's easy!): <a href='http://projects.colsolgrp.net/documents/28' target='_blank'>Click Here!</a>"; 
    1840                     } 
    1841                 } 
    1842  
    1843                 foreach($row as $ava_brands) { 
     1883                        $this->error['brand_update_check_local_file'] = "<br/>Error: No Local File for " . $data['name'] . "!<br /><br />Learn how to manually upload packages here (it's easy!): <a href='http://wiki.provisioner.net/index.php/Endpoint_manager_manual_upload' target='_blank'>Click Here!</a>"; 
     1884                    } 
     1885                } 
     1886 
     1887                foreach ($row as $ava_brands) { 
    18441888                    $key = $this->arraysearchrecursive($ava_brands['directory'], $out, 'directory'); 
    1845                     if($key === FALSE) { 
     1889                    if ($key === FALSE) { 
    18461890                        $this->remove_brand($ava_brands['id']); 
    18471891                    } else { 
     
    18501894                        $brand_name = $ava_brands['directory']; 
    18511895 
    1852                         if($ava_brands['cfg_ver'] < $version[$brand_name]) { 
     1896                        //TODO: This seems old 
     1897                        if ($ava_brands['cfg_ver'] < $version[$brand_name]) { 
    18531898                            $out[$key]['update'] = 1; 
    18541899                            $out[$key]['update_vers'] = $version[$brand_name]; 
     
    18591904                } 
    18601905            } else { 
    1861                 $this->error['brand_update_check_master_file'] = "<br/>Aborting Brand Downloads. Can't Get Master File, Assuming Timeout Issues!<br /><br />Learn how to manually upload packages here (it's easy!): <a href='http://projects.colsolgrp.net/documents/28' target='_blank'>Click Here!</a>"; 
     1906                $this->error['brand_update_check_master_file'] = "<br/>Aborting Brand Downloads. Can't Get Master File, Assuming Timeout Issues!<br /><br />Learn how to manually upload packages here (it's easy!): <a href='http://wiki.provisioner.net/index.php/Endpoint_manager_manual_upload' target='_blank'>Click Here!</a>"; 
    18621907            } 
    18631908 
     
    18731918     */ 
    18741919    function download_brand($id) { 
    1875         $row = $this->eda->sql('SELECT * FROM  endpointman_brand_list WHERE id ='.$id, 'getAll', DB_FETCHMODE_ASSOC); 
    1876         echo "Downloading Brand XML....."; 
    1877         $result = $this->download_xml(UPDATE_PATH .$row[0]['directory']. "/".$row[0]['directory'].".xml","endpoint/".$row[0]['directory']); 
    1878  
    1879         if($result) { 
     1920        $row = $this->eda->sql('SELECT * FROM  endpointman_brand_list WHERE id =' . $id, 'getAll', DB_FETCHMODE_ASSOC); 
     1921        echo "Downloading Brand JSON....."; 
     1922        $result = $this->download_file(UPDATE_PATH . $row[0]['directory'] . "/" . $row[0]['directory'] . ".json", PHONE_MODULES_PATH . "endpoint/" . $row[0]['directory'] . "/brand_data.json"); 
     1923 
     1924        if ($result) { 
    18801925            echo "Done!<br/>"; 
    18811926 
    1882             $temp = $this->xml2array(PHONE_MODULES_PATH.'endpoint/'. $row[0]['directory'].'/brand_data.xml'); 
     1927            $temp = $this->file2json(PHONE_MODULES_PATH . 'endpoint/' . $row[0]['directory'] . '/brand_data.json'); 
    18831928            $package = $temp['data']['brands']['package']; 
    18841929 
    18851930            echo "<div style='float: left'>Downloading Brand Package...</div><div style='float: left' id=\"DivExample\">.</div>"; 
    1886             $this->download_file_with_progress_bar(UPDATE_PATH.$row[0]['directory'].'/'.$package, PHONE_MODULES_PATH."temp/".$package); 
     1931            $this->download_file_with_progress_bar(UPDATE_PATH . $row[0]['directory'] . '/' . $package, PHONE_MODULES_PATH . "temp/" . $package); 
    18871932            echo "<script type='text/javascript'>document.getElementById('DivExample').innerHTML='Done!';</script><br />"; 
    18881933 
    1889             if(file_exists(PHONE_MODULES_PATH.'temp/'. $package)) { 
     1934            if (file_exists(PHONE_MODULES_PATH . 'temp/' . $package)) { 
    18901935                $md5_xml = $temp['data']['brands']['md5sum']; 
    1891                 $md5_pkg = md5_file(PHONE_MODULES_PATH.'temp/'. $package); 
     1936                $md5_pkg = md5_file(PHONE_MODULES_PATH . 'temp/' . $package); 
    18921937 
    18931938                echo "Checking MD5sum of Package...."; 
    1894                 if($md5_xml == $md5_pkg) { 
     1939                if ($md5_xml == $md5_pkg) { 
    18951940                    echo "Done!<br/>"; 
    18961941                    echo "Extracting Tarball........"; 
    1897                     exec("tar -xvf ".PHONE_MODULES_PATH.'temp/'. $package ." -C ".PHONE_MODULES_PATH."temp/"); 
     1942                    exec("tar -xvf " . PHONE_MODULES_PATH . 'temp/' . $package . " -C " . PHONE_MODULES_PATH . "temp/"); 
    18981943                    echo "Done!<br />"; 
    18991944 
    1900                     //Update File in directory 
    1901                     copy(PHONE_MODULES_PATH.'endpoint/'. $row[0]['directory'].'/brand_data.xml', PHONE_MODULES_PATH.'temp/'. $row[0]['directory'].'/brand_data.xml'); 
    1902  
    1903                     $package = basename($package, ".tgz"); 
    1904                     $package = explode("-",$package); 
    1905                     $this->update_brand($package[0]); 
     1945                    //Update File in the temp directory 
     1946                    copy(PHONE_MODULES_PATH . 'endpoint/' . $row[0]['directory'] . '/brand_data.json', PHONE_MODULES_PATH . 'temp/' . $row[0]['directory'] . '/brand_data.json'); 
     1947 
     1948                    $this->update_brand($row[0]['directory'], TRUE); 
    19061949                } else { 
    19071950                    echo "MD5 Did not match!"; 
     
    19191962     * Still needs way to determine when models move...perhaps another function? 
    19201963     */ 
    1921     function update_brand($package,$remote=TRUE) { 
    1922         if(file_exists(PHONE_MODULES_PATH.'temp/'. $package.'/brand_data.xml')) { 
    1923             $temp = $this->xml2array(PHONE_MODULES_PATH.'temp/'. $package.'/brand_data.xml'); 
    1924             if(key_exists('directory', $temp['data']['brands'])) { 
    1925                 echo "Appears to be a valid Provisioner.net XML file.....Continuing<br />"; 
     1964    function update_brand($package, $remote=TRUE) { 
     1965        if (file_exists(PHONE_MODULES_PATH . 'temp/' . $package . '/brand_data.json')) { 
     1966            $temp = $this->file2json(PHONE_MODULES_PATH . 'temp/' . $package . '/brand_data.json'); 
     1967            if (key_exists('directory', $temp['data']['brands'])) { 
     1968                echo "Appears to be a valid Provisioner.net JSON file.....Continuing<br />"; 
    19261969 
    19271970                //Pull in all variables 
     
    19291972                $brand_name = $temp['data']['brands']['name']; 
    19301973                $brand_id = $temp['data']['brands']['brand_id']; 
    1931                 $brand_version = $temp['data']['brands']['version']; 
    1932                 $brand_last_mod = $temp['data']['brands']['last_modified']; 
     1974                $brand_version = $temp['data']['brands']['last_modified']; 
    19331975 
    19341976                //create directory structure and move files 
    19351977                echo "Creating Directory Structure/Moving Files.."; 
    19361978 
    1937                 @mkdir(PHONE_MODULES_PATH."endpoint/".$directory); 
    1938  
    1939                 $dir_iterator = new RecursiveDirectoryIterator(PHONE_MODULES_PATH."temp/".$directory."/"); 
     1979                if (!file_exists(PHONE_MODULES_PATH . "endpoint/" . $directory)) { 
     1980                    mkdir(PHONE_MODULES_PATH . "endpoint/" . $directory); 
     1981                } 
     1982 
     1983                $dir_iterator = new RecursiveDirectoryIterator(PHONE_MODULES_PATH . "temp/" . $directory . "/"); 
    19401984                $iterator = new RecursiveIteratorIterator($dir_iterator, RecursiveIteratorIterator::SELF_FIRST); 
    19411985                foreach ($iterator as $file) { 
    1942                     if(is_dir($file)) { 
    1943                         $dir = str_replace(PHONE_MODULES_PATH."temp/".$directory."/", "", $file); 
    1944                         if(!file_exists(PHONE_MODULES_PATH."endpoint/".$directory."/".$dir)) { 
    1945                             mkdir(PHONE_MODULES_PATH."endpoint/".$directory."/".$dir,0764, TRUE); 
     1986                    if (is_dir($file)) { 
     1987                        $dir = str_replace(PHONE_MODULES_PATH . "temp/" . $directory . "/", "", $file); 
     1988                        if (!file_exists(PHONE_MODULES_PATH . "endpoint/" . $directory . "/" . $dir)) { 
     1989                            mkdir(PHONE_MODULES_PATH . "endpoint/" . $directory . "/" . $dir, 0764, TRUE); 
    19461990                            echo "."; 
    19471991                        } 
    19481992                    } else { 
    1949                         if((basename($file) != "brand_data.xml") OR (!$remote)) { 
    1950                             $dir = str_replace(PHONE_MODULES_PATH."temp/".$directory."/", "", $file); 
    1951                             $stats = rename($file, PHONE_MODULES_PATH."endpoint/".$directory."/".$dir); 
    1952                             if($stats === FALSE) { 
    1953                                 echo "Error Moving ". basename($file); 
     1993                        if ((basename($file) != "brand_data.json") OR (!$remote)) { 
     1994                            $dir = str_replace(PHONE_MODULES_PATH . "temp/" . $directory . "/", "", $file); 
     1995                            $stats = rename($file, PHONE_MODULES_PATH . "endpoint/" . $directory . "/" . $dir); 
     1996                            if ($stats === FALSE) { 
     1997                                echo "Error Moving " . basename($file); 
    19541998                            } 
    1955                             chmod(PHONE_MODULES_PATH."endpoint/".$directory."/".$dir,0764); 
     1999                            chmod(PHONE_MODULES_PATH . "endpoint/" . $directory . "/" . $dir, 0764); 
    19562000                            echo "."; 
    19572001                        } 
     
    19602004                echo "Done!<br />"; 
    19612005 
    1962                 if($remote) { 
     2006                if ($remote) { 
    19632007                    $local = 0; 
    19642008                } else { 
    19652009                    $local = 1; 
    19662010                } 
    1967                 $brand_version = max($last_mod, $brand_last_mod); 
    1968  
    1969                 $b_data = $this->eda->sql("SELECT id FROM endpointman_brand_list WHERE id = '".$brand_id."'", 'getOne'); 
    1970                 if($b_data) { 
     2011 
     2012                $b_data = $this->eda->sql("SELECT id FROM endpointman_brand_list WHERE id = '" . $brand_id . "'", 'getOne'); 
     2013                if ($b_data) { 
    19712014                    echo "Updating data.........."; 
    1972                     $sql = "UPDATE endpointman_brand_list SET local = '".$local."', name = '".$brand_name."', cfg_ver = '".$brand_version."', installed = 1, hidden = 0 WHERE id = ".$brand_id; 
     2015                    $sql = "UPDATE endpointman_brand_list SET local = '" . $local . "', name = '" . $brand_name . "', cfg_ver = '" . $brand_version . "', installed = 1, hidden = 0 WHERE id = " . $brand_id; 
    19732016                    $this->eda->sql($sql); 
    19742017                } else { 
    1975                     $sql = "INSERT INTO endpointman_brand_list (id, name, directory, cfg_ver, local, installed) VALUES ('".$brand_id."', '".$brand_name."', '".$directory."', '".$brand_version."', '1', '1')"; 
     2018                    $sql = "INSERT INTO endpointman_brand_list (id, name, directory, cfg_ver, local, installed) VALUES ('" . $brand_id . "', '" . $brand_name . "', '" . $directory . "', '" . $brand_version . "', '1', '1')"; 
    19762019                    $this->eda->sql($sql); 
    19772020                } 
    19782021 
    19792022                $last_mod = ""; 
    1980                 $temp['data']['brands']['family_list']['family'] = $this->fix_single_array_keys($temp['data']['brands']['family_list']['family']); 
    1981                 foreach($temp['data']['brands']['family_list']['family'] as $family_list) { 
     2023                foreach ($temp['data']['brands']['family_list'] as $family_list) { 
    19822024                    echo "Updating Family Lines.................<br/>"; 
    19832025 
     
    19852027                    $last_mod = max($last_mod, $family_list['last_modified']); 
    19862028 
    1987                     $family_line_xml = $this->xml2array(PHONE_MODULES_PATH.'/endpoint/'.$directory.'/'.$family_list['directory'].'/family_data.xml'); 
    1988                                          
     2029                    $family_line_xml = $this->file2json(PHONE_MODULES_PATH . '/endpoint/' . $directory . '/' . $family_list['directory'] . '/family_data.json'); 
     2030 
     2031                    $family_line_xml['data']['last_modified'] = isset($family_line_xml['data']['last_modified']) ? $family_line_xml['data']['last_modified'] : ''; 
     2032 
    19892033                    $require_firmware = NULL; 
    1990                     if((key_exists('require_firmware', $family_line_xml['data'])) && ($remote) && ($family_line_xml['data']['require_firmware'] == "TRUE")) { 
     2034                    if ((key_exists('require_firmware', $family_line_xml['data'])) && ($remote) && ($family_line_xml['data']['require_firmware'] == "TRUE")) { 
    19912035                        echo "Firmware Requirment Detected!..........<br/>"; 
    19922036                        $this->install_firmware($family_line_xml['data']['id']); 
    19932037                    } 
    19942038 
    1995                     $data = $this->eda->sql("SELECT id FROM endpointman_product_list WHERE id='".$brand_id.$family_line_xml['data']['id']."'", 'getOne'); 
     2039                    $data = $this->eda->sql("SELECT id FROM endpointman_product_list WHERE id='" . $brand_id . $family_line_xml['data']['id'] . "'", 'getOne'); 
    19962040                    $short_name = preg_replace("/\[(.*?)\]/si", "", $family_line_xml['data']['name']); 
    1997                     if($data) { 
    1998                         $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']."' WHERE id = '".$brand_id.$family_line_xml['data']['id']."'"; 
     2041                    if ($data) { 
     2042                        $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'] . "' WHERE id = '" . $brand_id . $family_line_xml['data']['id'] . "'"; 
    19992043                    } else { 
    2000                         $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')"; 
     2044                        $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']['last_modified'] . "','" . $family_line_xml['data']['configuration_files'] . "', '0')"; 
    20012045                    } 
    20022046 
    20032047                    $this->eda->sql($sql); 
    2004                     $family_line_xml['data']['model_list'] = $this->fix_single_array_keys($family_line_xml['data']['model_list']); 
    20052048                    echo "--Updating Model Lines................<br/>"; 
    2006                     foreach($family_line_xml['data']['model_list'] as $model_list) { 
    2007                         if(is_array($model_list['template_data']['files'])) { 
    2008                             $template_list = implode(",",$model_list['template_data']['files']); 
    2009                         } else { 
    2010                             $template_list = $model_list['template_data']['files']; 
    2011                         } 
    2012  
    2013                         $model_final_id = $brand_id.$family_line_xml['data']['id'].$model_list['id']; 
    2014                         $sql = 'SELECT id, global_custom_cfg_data, global_user_cfg_data FROM endpointman_mac_list WHERE model = '.$model_final_id; 
     2049                    foreach ($family_line_xml['data']['model_list'] as $model_list) { 
     2050                        $template_list = implode(",", $model_list['template_data']); 
     2051 
     2052                        $model_final_id = $brand_id . $family_line_xml['data']['id'] . $model_list['id']; 
     2053                        $sql = 'SELECT id, global_custom_cfg_data, global_user_cfg_data FROM endpointman_mac_list WHERE model = ' . $model_final_id; 
    20152054                        $old_data = NULL; 
    20162055                        $old_data = $this->eda->sql($sql, 'getAll', DB_FETCHMODE_ASSOC); 
    2017                         foreach($old_data as $data) { 
     2056                        foreach ($old_data as $data) { 
    20182057                            $global_custom_cfg_data = unserialize($data['global_custom_cfg_data']); 
    2019                             if((is_array($global_custom_cfg_data)) AND (!array_key_exists('data', $global_custom_cfg_data))) { 
     2058                            if ((is_array($global_custom_cfg_data)) AND (!array_key_exists('data', $global_custom_cfg_data))) { 
    20202059                                echo "----Old Data Detected! Migrating......"; 
    20212060                                $new_data = array(); 
    20222061                                $new_ari = array(); 
    2023                                 foreach($global_custom_cfg_data as $key => $old_keys) { 
    2024                                     if(array_key_exists('value', $old_keys)) { 
     2062                                foreach ($global_custom_cfg_data as $key => $old_keys) { 
     2063                                    if (array_key_exists('value', $old_keys)) { 
    20252064                                        $new_data[$key] = $old_keys['value']; 
    20262065                                    } else { 
    20272066                                        $breaks = explode("_", $key); 
    2028                                         $new_data["loop|".$key] = $old_keys[$breaks[2]]; 
     2067                                        $new_data["loop|" . $key] = $old_keys[$breaks[2]]; 
    20292068                                    } 
    2030                                     if(array_key_exists('ari', $old_keys)) { 
     2069                                    if (array_key_exists('ari', $old_keys)) { 
    20312070                                        $new_ari[$key] = 1; 
    20322071                                    } 
     
    20362075                                $final_data['ari'] = $new_ari; 
    20372076                                $final_data = serialize($final_data); 
    2038                                 $sql = "UPDATE endpointman_mac_list SET  global_custom_cfg_data =  '".$final_data."' WHERE  id =".$data['id']; 
     2077                                $sql = "UPDATE endpointman_mac_list SET  global_custom_cfg_data =  '" . $final_data . "' WHERE  id =" . $data['id']; 
    20392078                                $this->eda->sql($sql); 
    20402079                                echo "Done!<br/>"; 
     
    20432082                            $global_user_cfg_data = unserialize($data['global_user_cfg_data']); 
    20442083                            $old_check = FALSE; 
    2045                             if(is_array($global_user_cfg_data)) { 
    2046                                 foreach($global_user_cfg_data as $stuff) { 
    2047                                     if(is_array($stuff)) { 
    2048                                         if(array_key_exists('value', $stuff)) { 
     2084                            if (is_array($global_user_cfg_data)) { 
     2085                                foreach ($global_user_cfg_data as $stuff) { 
     2086                                    if (is_array($stuff)) { 
     2087                                        if (array_key_exists('value', $stuff)) { 
    20492088                                            $old_check = TRUE; 
    20502089                                            break; 
     
    20572096                                } 
    20582097                            } 
    2059                             if((is_array($global_user_cfg_data)) AND ($old_check)) { 
     2098                            if ((is_array($global_user_cfg_data)) AND ($old_check)) { 
    20602099                                echo "Old Data Detected! Migrating......"; 
    20612100                                $new_data = array(); 
    2062                                 foreach($global_user_cfg_data as $key => $old_keys) { 
    2063                                     if(array_key_exists('value', $old_keys)) { 
    2064                                         $exploded = explode("_",$key); 
     2101                                foreach ($global_user_cfg_data as $key => $old_keys) { 
     2102                                    if (array_key_exists('value', $old_keys)) { 
     2103                                        $exploded = explode("_", $key); 
    20652104                                        $counted = count($exploded); 
    20662105                                        $counted = $counted - 1; 
    2067                                         if(is_numeric($exploded[$counted])) { 
    2068                                             $key = "loop|".$key; 
     2106                                        if (is_numeric($exploded[$counted])) { 
     2107                                            $key = "loop|" . $key; 
    20692108                                        } 
    20702109                                        $new_data[$key] = $old_keys['value']; 
     
    20722111                                } 
    20732112                                $final_data = serialize($new_data); 
    2074                                 $sql = "UPDATE endpointman_mac_list SET  global_user_cfg_data =  '".$final_data."' WHERE  id =".$data['id']; 
     2113                                $sql = "UPDATE endpointman_mac_list SET  global_user_cfg_data =  '" . $final_data . "' WHERE  id =" . $data['id']; 
    20752114                                $this->eda->sql($sql); 
    20762115                                echo "Done!<br/>"; 
     
    20782117                        } 
    20792118                        $old_data = NULL; 
    2080                         $sql = 'SELECT id, global_custom_cfg_data FROM endpointman_template_list WHERE model_id = '.$model_final_id; 
     2119                        $sql = 'SELECT id, global_custom_cfg_data FROM endpointman_template_list WHERE model_id = ' . $model_final_id; 
    20812120                        $old_data = $this->eda->sql($sql, 'getAll', DB_FETCHMODE_ASSOC); 
    2082                         foreach($old_data as $data) { 
     2121                        foreach ($old_data as $data) { 
    20832122                            $global_custom_cfg_data = unserialize($data['global_custom_cfg_data']); 
    2084                             if((is_array($global_custom_cfg_data)) AND (!array_key_exists('data', $global_custom_cfg_data))) { 
     2123                            if ((is_array($global_custom_cfg_data)) AND (!array_key_exists('data', $global_custom_cfg_data))) { 
    20852124                                echo "Old Data Detected! Migrating......"; 
    20862125                                $new_data = array(); 
    20872126                                $new_ari = array(); 
    2088                                 foreach($global_custom_cfg_data as $key => $old_keys) { 
    2089                                     if(array_key_exists('value', $old_keys)) { 
     2127                                foreach ($global_custom_cfg_data as $key => $old_keys) { 
     2128                                    if (array_key_exists('value', $old_keys)) { 
    20902129                                        $new_data[$key] = $old_keys['value']; 
    20912130                                    } else { 
    20922131                                        $breaks = explode("_", $key); 
    2093                                         $new_data["loop|".$key] = $old_keys[$breaks[2]]; 
     2132                                        $new_data["loop|" . $key] = $old_keys[$breaks[2]]; 
    20942133                                    } 
    2095                                     if(array_key_exists('ari', $old_keys)) { 
     2134                                    if (array_key_exists('ari', $old_keys)) { 
    20962135                                        $new_ari[$key] = 1; 
    20972136                                    } 
     
    21012140                                $final_data['ari'] = $new_ari; 
    21022141                                $final_data = serialize($final_data); 
    2103                                 $sql = "UPDATE endpointman_template_list SET  global_custom_cfg_data =  '".$final_data."' WHERE  id =".$data['id']; 
    2104                                 $this->db->query($sql); 
     2142                                $sql = "UPDATE endpointman_template_list SET  global_custom_cfg_data =  '" . $final_data . "' WHERE  id =" . $data['id']; 
     2143                                $this->eda->sql($sql); 
    21052144                                echo "Done!<br/>"; 
    21062145                            } 
    21072146                        } 
    21082147 
    2109                         $m_data = $this->eda->sql("SELECT id FROM endpointman_model_list WHERE id='".$brand_id.$family_line_xml['data']['id'].$model_list['id']."'", 'getone'); 
    2110                         if($m_data) { 
    2111                             $sql = "UPDATE endpointman_model_list SET max_lines = '".$model_list['lines']."', model = '".$model_list['model']."', template_list = '".$template_list."' WHERE id = '".$brand_id.$family_line_xml['data']['id'].$model_list['id']."'"; 
     2148                        $m_data = $this->eda->sql("SELECT id FROM endpointman_model_list WHERE id='" . $brand_id . $family_line_xml['data']['id'] . $model_list['id'] . "'", 'getone'); 
     2149                        if ($m_data) { 
     2150                            $sql = "UPDATE endpointman_model_list SET max_lines = '" . $model_list['lines'] . "', model = '" . $model_list['model'] . "', template_list = '" . $template_list . "' WHERE id = '" . $brand_id . $family_line_xml['data']['id'] . $model_list['id'] . "'"; 
    21122151                        } else { 
    2113                             $sql = "INSERT INTO endpointman_model_list (`id`, `brand`, `model`, `max_lines`, `product_id`, `template_list`, `enabled`, `hidden`) VALUES ('".$brand_id.$family_line_xml['data']['id'].$model_list['id']."', '".$brand_id."', '".$model_list['model']."', '".$model_list['lines']."', '".$brand_id.$family_line_xml['data']['id']."', '".$template_list."', '0', '0')"; 
     2152                            $sql = "INSERT INTO endpointman_model_list (`id`, `brand`, `model`, `max_lines`, `product_id`, `template_list`, `enabled`, `hidden`) VALUES ('" . $brand_id . $family_line_xml['data']['id'] . $model_list['id'] . "', '" . $brand_id . "', '" . $model_list['model'] . "', '" . $model_list['lines'] . "', '" . $brand_id . $family_line_xml['data']['id'] . "', '" . $template_list . "', '0', '0')"; 
    21142153                        } 
    21152154                        $this->eda->sql($sql); 
    21162155 
    2117                          
    2118                         if(!$this->sync_model($brand_id.$family_line_xml['data']['id'].$model_list['id'])) { 
     2156 
     2157                        if (!$this->sync_model($brand_id . $family_line_xml['data']['id'] . $model_list['id'])) { 
    21192158                            echo "System Error in Sync Model Function, Load Failure!<br />"; 
    21202159                        } 
     
    21232162                } 
    21242163 
    2125                 foreach($temp['data']['brands']['oui_list']['oui'] as $oui) { 
    2126                     $sql = "REPLACE INTO endpointman_oui_list (`oui`, `brand`, `custom`) VALUES ('".$oui."', '".$brand_id."', '0')"; 
     2164                foreach ($temp['data']['brands']['oui_list'] as $oui) { 
     2165                    $sql = "REPLACE INTO endpointman_oui_list (`oui`, `brand`, `custom`) VALUES ('" . $oui . "', '" . $brand_id . "', '0')"; 
    21272166                    $this->eda->sql($sql); 
    21282167                } 
     
    21372176 
    21382177        echo "Removing Temporary Files.............."; 
    2139         $this->rmrf(PHONE_MODULES_PATH."temp/" .$package); 
     2178        $this->rmrf(PHONE_MODULES_PATH . "temp/" . $package); 
    21402179        echo "Done!<br />"; 
    2141  
    2142     } 
    2143  
     2180    } 
    21442181 
    21452182    /** 
     
    21472184     * @param int $id Brand ID 
    21482185     */ 
    2149     function remove_brand($id=NULL,$remove_configs=FALSE) { 
    2150         $sql = 'SELECT id, firmware_vers FROM endpointman_product_list WHERE brand = '.$id; 
     2186    function remove_brand($id=NULL, $remove_configs=FALSE) { 
     2187        $sql = 'SELECT id, firmware_vers FROM endpointman_product_list WHERE brand = ' . $id; 
    21512188        $products = $this->eda->sql($sql, 'getall', DB_FETCHMODE_ASSOC); 
    2152         foreach($products as $data) { 
    2153             if($data['firmware_vers'] != "") { 
     2189        foreach ($products as $data) { 
     2190            if ($data['firmware_vers'] != "") { 
    21542191                $this->remove_firmware($data['id']); 
    21552192            } 
    21562193        } 
    21572194 
    2158         $brand_dir = $this->eda->sql("SELECT directory FROM endpointman_brand_list WHERE id=".$id,'getone'); 
    2159         $this->rmrf(PHONE_MODULES_PATH."endpoint/".$brand_dir); 
    2160  
    2161         $sql = "DELETE FROM endpointman_model_list WHERE brand = '". $id."'"; 
     2195        $brand_dir = $this->eda->sql("SELECT directory FROM endpointman_brand_list WHERE id=" . $id, 'getone'); 
     2196        $this->rmrf(PHONE_MODULES_PATH . "endpoint/" . $brand_dir); 
     2197 
     2198        $sql = "DELETE FROM endpointman_model_list WHERE brand = '" . $id . "'"; 
    21622199        $this->eda->sql($sql); 
    21632200 
    2164         $sql = "DELETE FROM endpointman_product_list WHERE brand = '". $id . "'"; 
     2201        $sql = "DELETE FROM endpointman_product_list WHERE brand = '" . $id . "'"; 
    21652202        $this->eda->sql($sql); 
    21662203 
    2167         $sql = "DELETE FROM endpointman_oui_list WHERE brand = '". $id . "'"; 
     2204        $sql = "DELETE FROM endpointman_oui_list WHERE brand = '" . $id . "'"; 
    21682205        $this->eda->sql($sql); 
    21692206 
    2170         $this->rmrf(PHONE_MODULES_PATH .$brand_dir); 
    2171         $sql = "DELETE FROM endpointman_brand_list WHERE id = ". $id; 
     2207        $this->rmrf(PHONE_MODULES_PATH . $brand_dir); 
     2208        $sql = "DELETE FROM endpointman_brand_list WHERE id = " . $id; 
    21722209 
    21732210        $this->eda->sql($sql); 
     2211    } 
     2212 
     2213    function merge_data($path, $template_list, $maxlines = 3) { 
     2214        //TODO: fix 
     2215        foreach ($template_list as $files_data) { 
     2216            $full_path = $path . $files_data; 
     2217            if (file_exists($full_path)) { 
     2218                $temp_files_data = $this->file2json($full_path); 
     2219                foreach ($temp_files_data['template_data']['category'] as $category) { 
     2220                    $category_name = $category['name']; 
     2221                    foreach ($category['subcategory'] as $subcategory) { 
     2222                        $subcategory_name = $subcategory['name']; 
     2223                        $items_fin = array(); 
     2224                        $items_loop = array(); 
     2225                        $break_count = 0; 
     2226                        foreach ($subcategory['item'] as $item) { 
     2227                            switch ($item['type']) { 
     2228                                case 'loop_line_options': 
     2229                                    for ($i = 1; $i <= $maxlines; $i++) { 
     2230                                        $var_nam = "lineloop|line_" . $i; 
     2231                                        foreach ($item['data']['item'] as $item_loop) { 
     2232                                            if ($item_loop['type'] != 'break') { 
     2233                                                $z = str_replace("\$", "", $item_loop['variable']); 
     2234                                                $items_loop[$var_nam][$z] = $item_loop; 
     2235                                                $items_loop[$var_nam][$z]['description'] = str_replace('{$count}', $i, $items_loop[$var_nam][$z]['description']); 
     2236                                                $items_loop[$var_nam][$z]['default_value'] = $items_loop[$var_nam][$z]['default_value']; 
     2237                                                $items_loop[$var_nam][$z]['default_value'] = str_replace('{$count}', $i, $items_loop[$var_nam][$z]['default_value']); 
     2238                                                $items_loop[$var_nam][$z]['line_loop'] = TRUE; 
     2239                                                $items_loop[$var_nam][$z]['line_count'] = $i; 
     2240                                            } elseif ($item_loop['type'] == 'break') { 
     2241                                                $items_loop[$var_nam]['break_' . $break_count]['type'] = 'break'; 
     2242                                                $break_count++; 
     2243                                            } 
     2244                                        } 
     2245                                    } 
     2246                                    $items_fin = array_merge($items_fin, $items_loop); 
     2247                                    break; 
     2248                                case 'loop': 
     2249                                    for ($i = $item['loop_start']; $i <= $item['loop_end']; $i++) { 
     2250                                        $name = explode("_", $item['data']['item'][0]['variable']); 
     2251                                        $var_nam = "loop|" . str_replace("\$", "", $name[0]) . "_" . $i; 
     2252                                        foreach ($item['data']['item'] as $item_loop) { 
     2253                                            if ($item_loop['type'] != 'break') { 
     2254                                                $z_tmp = explode("_", $item_loop['variable']); 
     2255                                                $z = $z_tmp[1]; 
     2256                                                $items_loop[$var_nam][$z] = $item_loop; 
     2257                                                $items_loop[$var_nam][$z]['description'] = str_replace('{$count}', $i, $items_loop[$var_nam][$z]['description']); 
     2258                                                $items_loop[$var_nam][$z]['variable'] = str_replace('_', '_' . $i . '_', $items_loop[$var_nam][$z]['variable']); 
     2259                                                $items_loop[$var_nam][$z]['default_value'] = isset($items_loop[$var_nam][$z]['default_value']) ? $items_loop[$var_nam][$z]['default_value'] : ''; 
     2260                                                $items_loop[$var_nam][$z]['loop'] = TRUE; 
     2261                                                $items_loop[$var_nam][$z]['loop_count'] = $i; 
     2262                                            } elseif ($item_loop['type'] == 'break') { 
     2263                                                $items_loop[$var_nam]['break_' . $break_count]['type'] = 'break'; 
     2264                                                $break_count++; 
     2265                                            } 
     2266                                        } 
     2267                                    } 
     2268                                    $items_fin = array_merge($items_fin, $items_loop); 
     2269                                    break; 
     2270                                case 'break': 
     2271                                    $items_fin['break|' . $break_count]['type'] = 'break'; 
     2272                                    $break_count++; 
     2273                                    break; 
     2274                                default: 
     2275                                    $var_nam = "option|" . str_replace("\$", "", $item['variable']); 
     2276                                    $items_fin[$var_nam] = $item; 
     2277                                    break; 
     2278                            } 
     2279                        } 
     2280                        if (isset($data['data'][$category_name][$subcategory_name])) { 
     2281                            $old_sc = $data['data'][$category_name][$subcategory_name]; 
     2282                            $sub_cat_data[$category_name][$subcategory_name] = array(); 
     2283                            $sub_cat_data[$category_name][$subcategory_name] = array_merge($old_sc, $items_fin); 
     2284                        } else { 
     2285                            $sub_cat_data[$category_name][$subcategory_name] = $items_fin; 
     2286                        } 
     2287                    } 
     2288                    if (isset($data['data'][$category_name])) { 
     2289                        $old_c = $data['data'][$category_name]; 
     2290                        $new_c = $sub_cat_data[$category_name]; 
     2291                        $sub_cat_data[$category_name] = array(); 
     2292                        $data['data'][$category_name] = array_merge($old_c, $new_c); 
     2293                    } else { 
     2294                        $data['data'][$category_name] = $sub_cat_data[$category_name]; 
     2295                    } 
     2296                } 
     2297            } 
     2298        } 
     2299        return($data); 
    21742300    } 
    21752301 
     
    22032329            } 
    22042330 
    2205             if (!file_exists(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/family_data.xml')) { 
    2206                 $this->error['sync_model'] = "File 'family_data.xml Doesn't exist in directory: " . PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir']; 
     2331            if (!file_exists(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/family_data.json')) { 
     2332                $this->error['sync_model'] = "File 'family_data.json Doesn't exist in directory: " . PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir']; 
    22072333                return(FALSE); 
    22082334            } 
    22092335 
    2210             $family_line_xml = $this->xml2array(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/family_data.xml'); 
    2211  
    2212             if ($product_row['cfg_ver'] <= $family_line_xml['data']['version']) { 
    2213                 if (array_key_exists('template_data', $family_line_xml['data']['model_list'])) { 
    2214                     if (is_array($family_line_xml['data']['model_list']['template_data']['files'])) { 
    2215                         $template_list = implode(",", $family_line_xml['data']['model_list']['template_data']['files']); 
    2216                         $template_list_array = $family_line_xml['data']['model_list']['template_data']['files']; 
    2217                     } else { 
    2218                         $template_list = $family_line_xml['data']['model_list']['template_data']['files']; 
    2219                         $template_list_array[0] = $family_line_xml['data']['model_list']['template_data']['files']; 
    2220                     } 
    2221                     $maxlines = $family_line_xml['data']['model_list']['lines']; 
    2222                 } else { 
    2223                     $key = $this->arraysearchrecursive($model_row['model'], $family_line_xml['data']['model_list'], 'model'); 
    2224                     if ($key === FALSE) { 
    2225                         $this->error['sync_model'] = "Can't locate model in family XML file"; 
    2226                         return(FALSE); 
    2227                     } else { 
    2228                         if (is_array($family_line_xml['data']['model_list'][$key[0]]['template_data']['files'])) { 
    2229                             $template_list = implode(",", $family_line_xml['data']['model_list'][$key[0]]['template_data']['files']); 
    2230                             $template_list_array = $family_line_xml['data']['model_list'][$key[0]]['template_data']['files']; 
    2231                         } else { 
    2232                             $template_list = $family_line_xml['data']['model_list'][$key[0]]['template_data']['files']; 
    2233                             $template_list_array[0] = $family_line_xml['data']['model_list'][$key[0]]['template_data']['files']; 
    2234                         } 
    2235                     } 
    2236                     $maxlines = $family_line_xml['data']['model_list'][$key[0]]['lines']; 
    2237                 } 
    2238  
    2239                 $sql = "UPDATE endpointman_model_list SET max_lines = '" . $maxlines . "', template_list = '" . $template_list . "' WHERE id = '" . $model . "'"; 
    2240                 $this->eda->sql($sql); 
    2241  
    2242                 $version = $family_line_xml['data']['version']; 
    2243                 $long_name = $family_line_xml['data']['name']; 
    2244                 $short_name = preg_replace("/\[(.*?)\]/si", "", $family_line_xml['data']['name']); 
    2245                 $configuration_files = $family_line_xml['data']['configuration_files']; 
    2246  
    2247                 $sql = "UPDATE endpointman_product_list SET long_name = '" . $long_name . "', short_name = '" . $short_name . "' , cfg_ver = '" . $version . "' WHERE id = '" . $product_row['id'] . "'"; 
    2248                 $this->eda->sql($sql); 
    2249  
    2250                 $template_data_array = array(); 
    2251                 foreach ($template_list_array as $data) { 
    2252                     if (file_exists(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/' . $data)) { 
    2253                         $template_data_xml = $this->xml2array(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/' . $data); 
    2254                         $template_data_xml = $this->fix_single_array_keys($template_data_xml['template_data']); 
    2255                         $template_data_array = array_merge($template_data_array, $template_data_xml); 
    2256                     } 
    2257                 } 
    2258  
    2259                 if (file_exists(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/template_data_custom.xml')) { 
    2260                     $template_data_multi = $this->xml2array(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/template_data_custom.xml'); 
    2261                     if ($template_data_multi) { 
    2262                         $template_data_multi = $this->fix_single_array_keys($template_data_multi['template_data']['item']); 
    2263                         $template_data_array = array_merge($template_data_array, $template_data_multi); 
    2264                     } 
    2265                 } 
    2266  
    2267                 if (file_exists(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/template_data_' . $model_row['model'] . '_custom.xml')) { 
    2268                     $template_data_multi = $this->xml2array(self::$modules_path . $this->brand_name . "/" . $this->family_line . "/template_data_" . $this->model . "_custom.xml"); 
    2269                     if ($template_data_multi) { 
    2270                         $template_data_multi = $this->fix_single_array_keys($template_data_multi['template_data']['item']); 
    2271                         $template_data_array = array_merge($template_data_array, $template_data_multi); 
    2272                     } 
    2273                 } 
    2274  
    2275                 if (empty($template_data_array)) { 
    2276                     $this->error['sync_model'] = "No Template Data Found"; 
    2277                     return(FALSE); 
    2278                 } 
    2279  
    2280                 $sql = "UPDATE endpointman_model_list SET template_data = '" . serialize($template_data_array) . "' WHERE id = '" . $model . "'"; 
    2281                 $this->eda->sql($sql); 
    2282             } 
    2283  
     2336            $family_line_json = $this->file2json(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/family_data.json'); 
     2337 
     2338            //TODO: Add local file checks to avoid slow reloading on PHP < 5.3 
     2339            $key = $this->arraysearchrecursive($model_row['model'], $family_line_json['data']['model_list'], 'model'); 
     2340            if ($key === FALSE) { 
     2341                $this->error['sync_model'] = "Can't locate model in family JSON file"; 
     2342                return(FALSE); 
     2343            } else { 
     2344                $template_list = implode(",", $family_line_json['data']['model_list'][$key[0]]['template_data']); 
     2345                $template_list_array = $family_line_json['data']['model_list'][$key[0]]['template_data']; 
     2346            } 
     2347            $maxlines = $family_line_json['data']['model_list'][$key[0]]['lines']; 
     2348 
     2349            $sql = "UPDATE endpointman_model_list SET max_lines = '" . $maxlines . "', template_list = '" . $template_list . "' WHERE id = '" . $model . "'"; 
     2350            $this->eda->sql($sql); 
     2351 
     2352            $version = isset($family_line_json['data']['last_modified']) ? $family_line_json['data']['last_modified'] : ''; 
     2353            $long_name = $family_line_json['data']['name']; 
     2354            $short_name = preg_replace("/\[(.*?)\]/si", "", $family_line_json['data']['name']); 
     2355            $configuration_files = $family_line_json['data']['configuration_files']; 
     2356 
     2357            $sql = "UPDATE endpointman_product_list SET long_name = '" . $long_name . "', short_name = '" . $short_name . "' , cfg_ver = '" . $version . "' WHERE id = '" . $product_row['id'] . "'"; 
     2358            $this->eda->sql($sql); 
     2359 
     2360            $template_data_array = array(); 
     2361 
     2362            $template_data_array = $this->merge_data(PHONE_MODULES_PATH . '/endpoint/' . $brand_row['directory'] . '/' . $product_row['cfg_dir'] . '/', $template_list_array); 
     2363 
     2364            if (empty($template_data_array)) { 
     2365                $this->error['sync_model'] = "No Template Data Found"; 
     2366                return(FALSE); 
     2367            } 
     2368 
     2369            $sql = "UPDATE endpointman_model_list SET template_data = '" . serialize($template_data_array) . "' WHERE id = '" . $model . "'"; 
     2370            $this->eda->sql($sql); 
    22842371            return(TRUE); 
    22852372        } else { 
     
    23002387     * @return array 
    23012388     */ 
    2302     function arraysearchrecursive($Needle,$Haystack,$NeedleKey="",$Strict=false,$Path=array()) { 
    2303         if(!is_array($Haystack)) 
     2389    function arraysearchrecursive($Needle, $Haystack, $NeedleKey="", $Strict=false, $Path=array()) { 
     2390        if (!is_array($Haystack)) 
    23042391            return false; 
    2305         foreach($Haystack as $Key => $Val) { 
    2306             if(is_array($Val)&& 
    2307                     $SubPath=$this->arraysearchrecursive($Needle,$Val,$NeedleKey,$Strict,$Path)) { 
    2308                 $Path=array_merge($Path,Array($Key),$SubPath); 
     2392        foreach ($Haystack as $Key => $Val) { 
     2393            if (is_array($Val) && 
     2394                    $SubPath = $this->arraysearchrecursive($Needle, $Val, $NeedleKey, $Strict, $Path)) { 
     2395                $Path = array_merge($Path, Array($Key), $SubPath); 
    23092396                return $Path; 
    2310             } 
    2311             elseif((!$Strict&&$Val==$Needle&& 
    2312                             $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))|| 
    2313                     ($Strict&&$Val===$Needle&& 
    2314                             $Key==(strlen($NeedleKey)>0?$NeedleKey:$Key))) { 
    2315                 $Path[]=$Key; 
     2397            } elseif ((!$Strict && $Val == $Needle && 
     2398                    $Key == (strlen($NeedleKey) > 0 ? $NeedleKey : $Key)) || 
     2399                    ($Strict && $Val === $Needle && 
     2400                    $Key == (strlen($NeedleKey) > 0 ? $NeedleKey : $Key))) { 
     2401                $Path[] = $Key; 
    23162402                return $Path; 
    23172403            } 
     
    23192405        return false; 
    23202406    } 
    2321      
     2407 
    23222408    function run_in_background($Command, $Priority = 0) { 
    23232409        if ($Priority) 
     
    23352421    function download_file_with_progress_bar($url_file, $destination_file) { 
    23362422        set_time_limit(0); 
    2337         $headers = get_headers($url_file, 1);  
    2338         $size = $headers['Content-Length'];  
    2339  
    2340         if(preg_match('/200/', $headers[0])) { 
    2341             $pid = $this->run_in_background("wget ". $url_file . " -O " . $destination_file); 
    2342  
    2343             while($this->is_process_running($pid)) {  
    2344  
    2345                 $out = 100 * round(filesize($destination_file) / $size,2); 
    2346                 echo '<script type="text/javascript">document.getElementById(\'DivExample\').innerHTML="%'.$out.'";</script>'; 
     2423        $headers = get_headers($url_file, 1); 
     2424        $size = $headers['Content-Length']; 
     2425 
     2426        if (preg_match('/200/', $headers[0])) { 
     2427            $pid = $this->run_in_background("wget " . $url_file . " -O " . $destination_file); 
     2428 
     2429            while ($this->is_process_running($pid)) { 
     2430 
     2431                $out = 100 * round(filesize($destination_file) / $size, 2); 
     2432                echo '<script type="text/javascript">document.getElementById(\'DivExample\').innerHTML="%' . $out . '";</script>'; 
    23472433 
    23482434                usleep('500'); 
     
    23572443    } 
    23582444 
    2359     function download_file_no_progress_bar($url_file, $destination_file) { 
     2445    function download_file($url_file, $destination_file) { 
    23602446        $contents = file_get_contents($url_file); 
    2361         if(preg_match('/200/', $http_response_header[0])) { 
    2362             file_put_contents($destination_file,$contents); 
     2447         
     2448        if (preg_match('/200/', $http_response_header[0])) { 
     2449            $dirname = dirname($destination_file); 
     2450            if (!file_exists($dirname)) { 
     2451                mkdir($dirname); 
     2452            } 
     2453            file_put_contents($destination_file, $contents); 
    23632454            //check file placement 
    2364             if(!file_exists($destination_file)) { 
    2365                 return false; 
    2366             } 
    2367             return true; 
    2368         } else { 
    2369             return false; 
    2370         } 
    2371     } 
    2372  
    2373     function download_xml_file($url_file, $destination_file) { 
    2374         $contents = file_get_contents($url_file); 
    2375         if(preg_match('/200/', $http_response_header[0])) { 
    2376             file_put_contents($destination_file,$contents); 
    2377             //check file placement 
    2378             if(!file_exists($destination_file)) { 
     2455            if (!file_exists($destination_file)) { 
    23792456                return false; 
    23802457            } 
     
    23872464    //This function looks in common linux directories for system executable files. Like ARP & NMAP 
    23882465    function find_exec($exec) { 
    2389         $usr_bin = glob("/usr/bin/".$exec); 
    2390         $usr_sbin = glob("/usr/sbin/".$exec); 
    2391         $sbin = glob("/sbin/".$exec); 
    2392         $bin = glob("/bin/".$exec); 
    2393         $etc = glob("/etc/".$exec); 
    2394         if(isset($usr_bin[0])) { 
    2395             return("/usr/bin/".$exec); 
    2396         } elseif(isset($usr_sbin[0])) { 
    2397             return("/usr/sbin/".$exec); 
    2398         } elseif(isset($sbin[0])) { 
    2399             return("/sbin/".$exec); 
    2400         } elseif(isset($bin[0])) { 
    2401             return("/bin/".$exec); 
    2402         } elseif(isset($etc[0])) { 
    2403             return("/etc/".$exec); 
     2466        $usr_bin = glob("/usr/bin/" . $exec); 
     2467        $usr_sbin = glob("/usr/sbin/" . $exec); 
     2468        $sbin = glob("/sbin/" . $exec); 
     2469        $bin = glob("/bin/" . $exec); 
     2470        $etc = glob("/etc/" . $exec); 
     2471        if (isset($usr_bin[0])) { 
     2472            return("/usr/bin/" . $exec); 
     2473        } elseif (isset($usr_sbin[0])) { 
     2474            return("/usr/sbin/" . $exec); 
     2475        } elseif (isset($sbin[0])) { 
     2476            return("/sbin/" . $exec); 
     2477        } elseif (isset($bin[0])) { 
     2478            return("/bin/" . $exec); 
     2479        } elseif (isset($etc[0])) { 
     2480            return("/etc/" . $exec); 
    24042481        } else { 
    24052482            return($exec); 
     
    24142491     */ 
    24152492    function rmrf($dir) { 
    2416         if(file_exists($dir)) { 
     2493        if (file_exists($dir)) { 
    24172494            $iterator = new RecursiveDirectoryIterator($dir); 
    24182495            foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) { 
     
    24342511     */ 
    24352512    function table_exists($table) { 
    2436  global $amp_conf; 
    2437         $sql = "SHOW TABLES FROM ".$amp_conf['AMPDBNAME']; 
    2438         $result = $this->eda->sql($sql,'getAll'); 
    2439  
    2440         foreach($result as $row) { 
     2513        global $amp_conf; 
     2514        $sql = "SHOW TABLES FROM " . $amp_conf['AMPDBNAME']; 
     2515        $result = $this->eda->sql($sql, 'getAll'); 
     2516 
     2517        foreach ($result as $row) { 
    24412518            if ($row[0] == $table) { 
    24422519                return TRUE; 
     
    24462523    } 
    24472524 
    2448     /** 
    2449      * xml2array() will convert the given XML text to an array in the XML structure. 
    2450      * @author http://www.bin-co.com/php/scripts/xml2array/ 
    2451      * @param <type> $url The XML file 
    2452      * @param <type> $get_attributes 1 or 0. If this is 1 the function will get the attributes as well as the tag values - this results in a different array structure in the return value. 
    2453      * @param <type> $priority Can be 'tag' or 'attribute'. This will change the way the resulting array structure. For 'tag', the tags are given more importance. 
    2454      * @param <type> $array_tags - any tag names listed here will allways be returned as an array, even if there is only one of them. 
    2455      * @return <type> The parsed XML in an array form. Use print_r() to see the resulting array structure. 
    2456      */ 
    2457     function xml2array($url, $get_attributes = 1, $priority = 'tag', $array_tags=array()) { 
    2458         $contents = ""; 
    2459         if (!function_exists('xml_parser_create')) { 
    2460             return array(); 
    2461         } 
    2462         $parser = xml_parser_create(''); 
    2463         if (!($fp = @ fopen($url, 'rb'))) { 
    2464             return array(); 
    2465         } 
    2466         while (!feof($fp)) { 
    2467             $contents .= fread($fp, 8192); 
    2468         } 
    2469         fclose($fp); 
    2470         xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); 
    2471         xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
    2472         xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
    2473         xml_parse_into_struct($parser, trim($contents), $xml_values); 
    2474         xml_parser_free($parser); 
    2475         if (!$xml_values) { 
    2476             return; //Hmm... 
    2477         } 
    2478         $xml_array = array(); 
    2479         $parents = array(); 
    2480         $opened_tags = array(); 
    2481         $arr = array(); 
    2482         $current = & $xml_array; 
    2483         $repeated_tag_index = array(); 
    2484         foreach ($xml_values as $data) { 
    2485             unset($attributes, $value); 
    2486             extract($data); 
    2487             $result = array(); 
    2488             $attributes_data = array(); 
    2489             if (isset($value)) { 
    2490                 if ($priority == 'tag') { 
    2491                     $result = $value; 
    2492                 } else { 
    2493                     $result['value'] = $value; 
    2494                 } 
    2495             } 
    2496             if (isset($attributes) and $get_attributes) { 
    2497                 foreach ($attributes as $attr => $val) { 
    2498                     if ($priority == 'tag') { 
    2499                         $attributes_data[$attr] = $val; 
    2500                     } else { 
    2501                         $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr' 
    2502                     } 
    2503                 } 
    2504             } 
    2505             if ($type == "open") { 
    2506                 $parent[$level - 1] = & $current; 
    2507     if (!is_array($current) or (!in_array($tag, array_keys($current)))) { 
    2508         if (in_array($tag,$array_tags)) { 
    2509                         $current[$tag][0] = $result; 
    2510                         $repeated_tag_index[$tag . '_' . $level]=1; 
    2511                       $current = & $current[$tag][0]; 
    2512         } else { 
    2513       $current[$tag] = $result; 
    2514       if ($attributes_data) { 
    2515         $current[$tag . '_attr'] = $attributes_data; 
    2516       } 
    2517       $repeated_tag_index[$tag . '_' . $level] = 1; 
    2518       $current = & $current[$tag]; 
    2519        } 
    2520                 } else { 
    2521                     if (isset($current[$tag][0])) { 
    2522                         $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; 
    2523                         $repeated_tag_index[$tag . '_' . $level]++; 
    2524                     } else { 
    2525                         $current[$tag] = array($current[$tag], $result); 
    2526                         $repeated_tag_index[$tag . '_' . $level] = 2; 
    2527                         if (isset($current[$tag . '_attr'])) { 
    2528                             $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
    2529                             unset($current[$tag . '_attr']); 
    2530                         } 
    2531                     } 
    2532                     $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1; 
    2533                     $current = & $current[$tag][$last_item_index]; 
    2534                 } 
    2535             } else if ($type == "complete") { 
    2536                 if (!isset($current[$tag])) { 
    2537                     $current[$tag] = $result; 
    2538                     $repeated_tag_index[$tag . '_' . $level] = 1; 
    2539                     if ($priority == 'tag' and $attributes_data) { 
    2540                         $current[$tag . '_attr'] = $attributes_data; 
    2541                     } 
    2542                 } else { 
    2543                     if (isset($current[$tag][0]) and is_array($current[$tag])) { 
    2544                         $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result; 
    2545                         if ($priority == 'tag' and $get_attributes and $attributes_data) { 
    2546                             $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; 
    2547                         } 
    2548                         $repeated_tag_index[$tag . '_' . $level]++; 
    2549                     } else { 
    2550                         $current[$tag] = array($current[$tag], $result); 
    2551                         $repeated_tag_index[$tag . '_' . $level] = 1; 
    2552                         if ($priority == 'tag' and $get_attributes) { 
    2553                             if (isset($current[$tag . '_attr'])) { 
    2554                                 $current[$tag]['0_attr'] = $current[$tag . '_attr']; 
    2555                                 unset($current[$tag . '_attr']); 
    2556                             } 
    2557                             if ($attributes_data) { 
    2558                                 $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data; 
    2559                             } 
    2560                         } 
    2561                         $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken 
    2562                     } 
    2563                 } 
    2564             } else if ($type == 'close') { 
    2565                 $current = & $parent[$level - 1]; 
    2566             } 
    2567         } 
    2568         return ($xml_array); 
     2525    function file2json($file) { 
     2526        if (file_exists($file)) { 
     2527            $json = file_get_contents($file); 
     2528            $data = json_decode($json, TRUE); 
     2529            return($data); 
     2530        } else { 
     2531            return(array()); 
     2532        } 
    25692533    } 
    25702534 
     
    25772541        if ((strlen($mac) == "17") OR (strlen($mac) == "12")) { 
    25782542            //It might be better to use switch here instead of these IF statements... 
    2579  
    25802543            //Is the mac separated by colons(:) or dashes(-)? 
    2581             if (preg_match("/[0-9a-f][0-9a-f][:-]"
    2582             "[0-9a-f][0-9a-f][:-]"
    2583             "[0-9a-f][0-9a-f][:-]"
    2584             "[0-9a-f][0-9a-f][:-]"
    2585             "[0-9a-f][0-9a-f][:-]"
    2586             "[0-9a-f][0-9a-f]/i", $mac)) { 
     2544            if (preg_match("/[0-9a-f][0-9a-f][:-]"
     2545                            "[0-9a-f][0-9a-f][:-]"
     2546                            "[0-9a-f][0-9a-f][:-]"
     2547                            "[0-9a-f][0-9a-f][:-]"
     2548                            "[0-9a-f][0-9a-f][:-]"
     2549                            "[0-9a-f][0-9a-f]/i", $mac)) { 
    25872550                return(strtoupper(str_replace(":", "", str_replace("-", "", $mac)))); 
    25882551                //Is the string exactly 12 characters? 
    2589             } elseif(strlen($mac) == "12") { 
     2552            } elseif (strlen($mac) == "12") { 
    25902553                //Now is the string a valid HEX mac address? 
    2591                 if (preg_match("/[0-9a-f][0-9a-f]"
    2592                 "[0-9a-f][0-9a-f]"
    2593                 "[0-9a-f][0-9a-f]"
    2594                 "[0-9a-f][0-9a-f]"
    2595                 "[0-9a-f][0-9a-f]"
    2596                 "[0-9a-f][0-9a-f]/i", $mac)) { 
     2554                if (preg_match("/[0-9a-f][0-9a-f]"
     2555                                "[0-9a-f][0-9a-f]"
     2556                                "[0-9a-f][0-9a-f]"
     2557                                "[0-9a-f][0-9a-f]"
     2558                                "[0-9a-f][0-9a-f]"
     2559                                "[0-9a-f][0-9a-f]/i", $mac)) { 
    25972560                    return(strtoupper($mac)); 
    25982561                } else { 
     
    26002563                } 
    26012564                //Is the mac separated by whitespaces? 
    2602             } elseif(preg_match("/[0-9a-f][0-9a-f][\s]"
    2603             "[0-9a-f][0-9a-f][\s]"
    2604             "[0-9a-f][0-9a-f][\s]"
    2605             "[0-9a-f][0-9a-f][\s]"
    2606             "[0-9a-f][0-9a-f][\s]"
    2607             "[0-9a-f][0-9a-f]/i", $mac)) { 
     2565            } elseif (preg_match("/[0-9a-f][0-9a-f][\s]"
     2566                            "[0-9a-f][0-9a-f][\s]"
     2567                            "[0-9a-f][0-9a-f][\s]"
     2568                            "[0-9a-f][0-9a-f][\s]"
     2569                            "[0-9a-f][0-9a-f][\s]"
     2570                            "[0-9a-f][0-9a-f]/i", $mac)) { 
    26082571                return(strtoupper(str_replace(" ", "", $mac))); 
    26092572            } else { 
     
    26422605 
    26432606        if (($use_nmap) AND (file_exists($this->global_cfg['nmap_location'])) AND ($this->validate_netmask($netmask))) { 
    2644             shell_exec($this->global_cfg['nmap_location'].' -v -sP '. $netmask); 
    2645         } elseif(!$this->validate_netmask($netmask)) { 
     2607            shell_exec($this->global_cfg['nmap_location'] . ' -v -sP ' . $netmask); 
     2608        } elseif (!$this->validate_netmask($netmask)) { 
    26462609            $this->error['discover_new'] = "Invalid Netmask"; 
    26472610            return(FALSE); 
    2648         } elseif(!file_exists($this->global_cfg['nmap_location'])) { 
     2611        } elseif (!file_exists($this->global_cfg['nmap_location'])) { 
    26492612            $this->error['discover_new'] = "Could Not Find NMAP, Using ARP Only"; 
    26502613            //return(FALSE); 
     
    26582621 
    26592622        //Find all references to active computers by searching out mac addresses. 
    2660         $temp = array_values(array_unique(preg_grep("/[0-9a-f][0-9a-f][:-]"
    2661                 "[0-9a-f][0-9a-f][:-]"
    2662                 "[0-9a-f][0-9a-f][:-]"
    2663                 "[0-9a-f][0-9a-f][:-]"
    2664                 "[0-9a-f][0-9a-f][:-]"
    2665                 "[0-9a-f][0-9a-f]/i", $arp_array))); 
     2623        $temp = array_values(array_unique(preg_grep("/[0-9a-f][0-9a-f][:-]"
     2624                                "[0-9a-f][0-9a-f][:-]"
     2625                                "[0-9a-f][0-9a-f][:-]"
     2626                                "[0-9a-f][0-9a-f][:-]"
     2627                                "[0-9a-f][0-9a-f][:-]"
     2628                                "[0-9a-f][0-9a-f]/i", $arp_array))); 
    26662629 
    26672630        //Go through each row of valid arp entries and pull out the information and add it into a nice array! 
     
    26702633 
    26712634            //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 
    2672             preg_match_all("/\((.*?)\)/",$value,$matches); 
     2635            preg_match_all("/\((.*?)\)/", $value, $matches); 
    26732636            $ip = $matches[1]; 
    26742637            $ip = $ip[0]; 
    26752638 
    26762639            //Pull out the mac address by looking for the delimiter 
    2677             $mac = substr($value, (strpos($value, ":") -2), 17); 
     2640            $mac = substr($value, (strpos($value, ":") - 2), 17); 
    26782641 
    26792642            //Get rid of the delimiter 
     
    26812644 
    26822645            //arp -n will return a MAC address of 000000000000 if no hardware was found, so we need to ignore it 
    2683             if($mac_strip != "000000000000") { 
     2646            if ($mac_strip != "000000000000") { 
    26842647                //only use the first 6 characters for the oui: http://en.wikipedia.org/wiki/Organizationally_Unique_Identifier 
    2685                 $oui = substr($mac_strip,0,6); 
     2648                $oui = substr($mac_strip, 0, 6); 
    26862649 
    26872650                //Find the matching brand model to the oui 
    2688                 $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"; 
     2651                $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"; 
    26892652 
    26902653                $brand = $this->eda->sql($oui_sql, 'getRow', DB_FETCHMODE_ASSOC); 
    2691                  
     2654 
    26922655                $res = $this->eda->sql($oui_sql); 
    26932656                $brand_count = $res->numRows(); 
    2694                  
     2657 
    26952658                if (!$brand_count) { 
    26962659                    //oui doesn't have a matching mysql reference, probably a PC/router/wap/printer of some sort. 
     
    27002663 
    27012664                //Find out if endpoint has already been configured for this mac address 
    2702                 $epm_sql = "SELECT * FROM endpointman_mac_list WHERE mac LIKE  '%". $mac_strip ."%'"; 
     2665                $epm_sql = "SELECT * FROM endpointman_mac_list WHERE mac LIKE  '%" . $mac_strip . "%'"; 
    27032666                $epm_row = $this->eda->sql($epm_sql, 'getRow', DB_FETCHMODE_ASSOC); 
    27042667 
     
    27202683        //$final = array_values($final); 
    27212684 
    2722         if(!is_array($final)) { 
     2685        if (!is_array($final)) { 
    27232686            return(FALSE); 
    27242687        } else { 
     
    27272690    } 
    27282691 
    2729     function areaAvailable($model,$area=NULL) { 
    2730         $sql = "SELECT max_lines FROM endpointman_model_list WHERE id = '". $model."'"; 
    2731         $count = $this->eda->sql($sql,'getOne'); 
    2732  
    2733         for($z=0;$z<$count;$z++) { 
     2692    function areaAvailable($model, $area=NULL) { 
     2693        $sql = "SELECT max_lines FROM endpointman_model_list WHERE id = '" . $model . "'"; 
     2694        $count = $this->eda->sql($sql, 'getOne'); 
     2695 
     2696        for ($z = 0; $z < $count; $z++) { 
    27342697            $result[$z]['id'] = $z + 1; 
    27352698            $result[$z]['model'] = $z + 1; 
     
    27372700 
    27382701        $i = 1; 
    2739         foreach($result as $row) { 
     2702        foreach ($result as $row) { 
    27402703            if ($row['id'] == $area) { 
    27412704                $temp[$i]['value'] = $row['id']; 
    27422705                $temp[$i]['text'] = $row['model']; 
    27432706                $temp[$i]['selected'] = 'selected'; 
    2744             }else { 
     2707            } else { 
    27452708                $temp[$i]['value'] = $row['id']; 
    27462709                $temp[$i]['text'] = $row['model']; 
     
    27612724     */ 
    27622725    function models_available($model=NULL, $brand=NULL, $product=NULL) { 
    2763          
     2726 
    27642727        if ((!isset($oui)) && (!isset($brand)) && (!isset($model))) { 
    27652728            $result1 = $this->eda->all_models(); 
    2766         }elseif((isset($brand)) && ($brand !=0)) { 
     2729        } elseif ((isset($brand)) && ($brand != 0)) { 
    27672730            $result1 = $this->eda->all_models_by_brand($brand); 
    2768         }elseif((isset($product)) && ($product !=0)) { 
     2731        } elseif ((isset($product)) && ($product != 0)) { 
    27692732            $result1 = $this->eda->all_models_by_product($product); 
    27702733        } else { 
     
    27732736 
    27742737        $i = 1; 
    2775         foreach($result1 as $row) { 
     2738        foreach ($result1 as $row) { 
    27762739            if ($row['id'] == $model) { 
    27772740                $temp[$i]['value'] = $row['id']; 
    27782741                $temp[$i]['text'] = $row['model']; 
    27792742                $temp[$i]['selected'] = 'selected'; 
    2780             }else { 
     2743            } else { 
    27812744                $temp[$i]['value'] = $row['id']; 
    27822745                $temp[$i]['text'] = $row['model']; 
     
    27862749        } 
    27872750 
    2788         if(!isset($temp)) { 
    2789             if(!isset($this->global_cfg['new'])) { 
     2751        if (!isset($temp)) { 
     2752            if (!isset($this->global_cfg['new'])) { 
    27902753                $this->error['modelsAvailable'] = "You need to enable at least ONE model"; 
    27912754            } 
     
    28002763        $it = new RecursiveIteratorIterator(new RecursiveArrayIterator($haystack)); 
    28012764 
    2802         foreach($it AS $element) { 
    2803             if($element == $needle) { 
     2765        foreach ($it AS $element) { 
     2766            if ($element == $needle) { 
    28042767                return TRUE; 
    28052768            } 
    2806         }  
     2769        } 
    28072770        return FALSE; 
    28082771    } 
    28092772 
    2810     function linesAvailable($lineid=NULL,$macid=NULL) { 
    2811         if(isset($lineid)) { 
    2812             $sql="SELECT max_lines FROM endpointman_model_list WHERE id = (SELECT endpointman_mac_list.model FROM endpointman_mac_list, endpointman_line_list WHERE endpointman_line_list.luid = ".$lineid." AND endpointman_line_list.mac_id = endpointman_mac_list.id)"; 
    2813  
    2814             $sql_l = "SELECT line, mac_id FROM `endpointman_line_list` WHERE luid = ".$lineid; 
     2773    function linesAvailable($lineid=NULL, $macid=NULL) { 
     2774        if (isset($lineid)) { 
     2775            $sql = "SELECT max_lines FROM endpointman_model_list WHERE id = (SELECT endpointman_mac_list.model FROM endpointman_mac_list, endpointman_line_list WHERE endpointman_line_list.luid = " . $lineid . " AND endpointman_line_list.mac_id = endpointman_mac_list.id)"; 
     2776 
     2777            $sql_l = "SELECT line, mac_id FROM `endpointman_line_list` WHERE luid = " . $lineid; 
    28152778            $line = $this->eda->sql($sql_l, 'getRow', DB_FETCHMODE_ASSOC); 
    28162779 
    2817             $sql_lu = "SELECT line FROM endpointman_line_list WHERE mac_id = ".$line['mac_id']; 
    2818         } elseif(isset($macid)) { 
    2819             $sql="SELECT max_lines FROM endpointman_model_list WHERE id = (SELECT model FROM endpointman_mac_list WHERE id =".$macid.")"; 
    2820             $sql_lu = "SELECT line FROM endpointman_line_list WHERE mac_id = ".$macid; 
    2821              
     2780            $sql_lu = "SELECT line FROM endpointman_line_list WHERE mac_id = " . $line['mac_id']; 
     2781        } elseif (isset($macid)) { 
     2782            $sql = "SELECT max_lines FROM endpointman_model_list WHERE id = (SELECT model FROM endpointman_mac_list WHERE id =" . $macid . ")"; 
     2783            $sql_lu = "SELECT line FROM endpointman_line_list WHERE mac_id = " . $macid; 
     2784 
    28222785            $line['line'] = 0; 
    28232786        } 
    28242787 
    2825         $max_lines = $this->eda->sql($sql,'getOne'); 
    2826         $lines_used = $this->eda->sql($sql_lu,'getAll'); 
    2827  
    2828         for($i = 1; $i <= $max_lines; $i++) {     
    2829             if($i == $line['line']) { 
     2788        $max_lines = $this->eda->sql($sql, 'getOne'); 
     2789        $lines_used = $this->eda->sql($sql_lu, 'getAll'); 
     2790 
     2791        for ($i = 1; $i <= $max_lines; $i++) { 
     2792            if ($i == $line['line']) { 
    28302793                $temp[$i]['value'] = $i; 
    28312794                $temp[$i]['text'] = $i; 
    28322795                $temp[$i]['selected'] = "selected"; 
    28332796            } else { 
    2834                 if(!$this->in_array_recursive($i,$lines_used)) { 
     2797                if (!$this->in_array_recursive($i, $lines_used)) { 
    28352798                    $temp[$i]['value'] = $i; 
    28362799                    $temp[$i]['text'] = $i; 
     
    28382801            } 
    28392802        } 
    2840         if(isset($temp)) { 
     2803        if (isset($temp)) { 
    28412804            return($temp); 
    28422805        } else { 
     
    28512814    function display_registration_list($line_id=NULL) { 
    28522815 
    2853         if(isset($line_id)) { 
     2816        if (isset($line_id)) { 
    28542817            $result = $this->eda->all_unused_registrations(); 
    28552818            $line_data = $this->eda->get_line_information($line_id); 
     
    28612824        $i = 1; 
    28622825        $temp = array(); 
    2863         foreach($result as $row) { 
     2826        foreach ($result as $row) { 
    28642827            $temp[$i]['value'] = $row['id']; 
    28652828            $temp[$i]['text'] = $row['id'] . " --- " . $row['description']; 
     
    28672830        } 
    28682831 
    2869         if(isset($line_data)) { 
     2832        if (isset($line_data)) { 
    28702833            $temp[$i]['value'] = $line_data['ext']; 
    28712834            $temp[$i]['text'] = $line_data['ext'] . " --- " . $line_data['description']; 
     
    28742837 
    28752838        return($temp); 
    2876  
    2877     } 
     2839    } 
     2840 
    28782841    /** 
    28792842     * Returns list of Brands that are installed and not hidden and that have at least one model enabled under them 
     
    28812844     * @return array Number array used to generate a select box 
    28822845     */ 
    2883     function brands_available ($selected = NULL,$show_blank=TRUE) { 
     2846    function brands_available($selected = NULL, $show_blank=TRUE) { 
    28842847        $data = $this->eda->all_active_brands(); 
    2885         if($show_blank) { 
     2848        if ($show_blank) { 
    28862849            $temp[0]['value'] = ""; 
    28872850            $temp[0]['text'] = ""; 
     
    28902853            $i = 0; 
    28912854        } 
    2892         foreach($data as $row) { 
     2855        foreach ($data as $row) { 
    28932856            $temp[$i]['value'] = $row['id']; 
    28942857            $temp[$i]['text'] = $row['name']; 
     
    29032866    } 
    29042867 
    2905     function display_templates($product_id,$temp_select = NULL) { 
     2868    function display_templates($product_id, $temp_select = NULL) { 
    29062869        $i = 0; 
    2907         $sql="SELECT id FROM  endpointman_product_list WHERE endpointman_product_list.id ='".$product_id."'"; 
    2908  
    2909         $id = $this->eda->sql($sql,'getOne'); 
    2910  
    2911         $sql="SELECT * FROM  endpointman_template_list WHERE  product_id = '".$id."'"; 
    2912  
    2913         $data = $this->eda->sql($sql,'getAll', DB_FETCHMODE_ASSOC); 
    2914         foreach($data as $row) { 
     2870        $sql = "SELECT id FROM  endpointman_product_list WHERE endpointman_product_list.id ='" . $product_id . "'"; 
     2871 
     2872        $id = $this->eda->sql($sql, 'getOne'); 
     2873 
     2874        $sql = "SELECT * FROM  endpointman_template_list WHERE  product_id = '" . $id . "'"; 
     2875 
     2876        $data = $this->eda->sql($sql, 'getAll', DB_FETCHMODE_ASSOC); 
     2877        foreach ($data as $row) { 
    29152878            $temp[$i]['value'] = $row['id']; 
    29162879            $temp[$i]['text'] = $row['name']; 
     
    29312894    } 
    29322895 
    2933     function listTZ($selected) {         
    2934         require('timezone.inc');         
     2896    function listTZ($selected) { 
     2897        require('timezone.inc'); 
    29352898        $data = DateTimeZone::listIdentifiers(); 
    29362899        $i = 0; 
    2937         foreach($data as $key => $row) { 
     2900        foreach ($data as $key => $row) { 
    29382901            $temp[$i]['value'] = $row; 
    29392902            $temp[$i]['text'] = $row; 
    29402903            if ($temp[$i]['value'] == $selected) { 
    29412904                $temp[$i]['selected'] = 1; 
    2942             }else { 
     2905            } else { 
    29432906                $temp[$i]['selected'] = 0; 
    29442907            } 
     
    29482911        return($temp); 
    29492912    } 
     2913 
     2914    function validate_json($json) { 
     2915        return(TRUE); 
     2916    } 
     2917 
    29502918} 
    29512919 
    2952 function endpointman_flush_buffers()
     2920function endpointman_flush_buffers()
    29532921    ob_end_flush(); 
    29542922    //ob_flush(); 
     
    29582926 
    29592927function endpointman_update_progress_bar($out) { 
    2960     echo '<script type="text/javascript">document.getElementById(\'DivExample\').innerHTML="%'.$out.'";</script>'; 
     2928    echo '<script type="text/javascript">document.getElementById(\'DivExample\').innerHTML="%' . $out . '";</script>'; 
    29612929} 
    29622930 
     
    29662934    $regs = ""; 
    29672935    preg_match("/(Content-Length:) (.*)/i", $string, $regs); 
    2968     if((isset($regs[2])) AND ($regs[2] <> "")) { 
     2936    if ((isset($regs[2])) AND ($regs[2] <> "")) { 
    29692937        $file_size = intval($regs[2]); 
    29702938    } 
     
    29802948    $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0); 
    29812949    $downloadProgress = 100 - $downloadProgress; 
    2982     if($lastseen <> $downloadProgress and $downloadProgress < 101) { 
    2983         if($progress_bar) { 
     2950    if ($lastseen <> $downloadProgress and $downloadProgress < 101) { 
     2951        if ($progress_bar) { 
    29842952            endpointman_update_progress_bar($downloadProgress); 
    29852953        } 
    29862954        $lastseen = $downloadProgress; 
    29872955    } 
    2988     if($fout) 
     2956    if ($fout) 
    29892957        fwrite($fout, $string); 
    29902958    //ob_flush(); 
  • modules/branches/2.10/endpointman/includes/popup.inc

    r12937 r13276  
    99 
    1010if($_REQUEST['pop_type'] == 'edit_specifics') { 
    11     $info = $endpoint->get_phone_info($_REQUEST['edit_id']); 
    12     if(isset($_REQUEST['button_save'])) { 
    13   $endpoint->message['advanced_settings'] = "Updated!"; 
    14   foreach($info['line'] as $data) { 
    15       $line = $data['line']; 
    16       $final['displayname_'.$line] = $_REQUEST['displayname_'.$line]; 
    17   } 
    18   $sql = "UPDATE endpointman_mac_list SET specific_settings = '".serialize($final)."' WHERE id =". $_REQUEST['edit_id']; 
    19   $endpoint->eda->sql($sql); 
    20     } 
    21  
    22     $info = $endpoint->get_phone_info($_REQUEST['edit_id']); 
    23     $specific_settings = !empty($info['specific_settings']) ? unserialize($info['specific_settings']) : array(); 
    24  
    25     $z = 0; 
    26     foreach($info['line'] as $data) { 
    27   $line = $data['line']; 
    28   $displayname[$z]['displayname'] = isset($specific_settings['displayname_'.$line]) ? $specific_settings['displayname_'.$line] : '{$displayname.line.'.$line.'}'; 
    29   $displayname[$z]['line'] = $line; 
    30   $z++; 
     11     
     12    if(file_exists(PHONE_MODULES_PATH . 'endpoint/global_template_data.json')) { 
     13        $template_list[0] = 'global_template_data.json'; 
     14                 
     15        $phone_info = $endpoint->get_phone_info($_REQUEST['edit_id']); 
     16         
     17        $sql = 'SELECT max_lines FROM endpointman_model_list WHERE id = '. $phone_info['model_id']; 
     18         
     19        $lines = $endpoint->eda->sql($sql, 'getOne'); 
     20        $merged = $endpoint->merge_data(PHONE_MODULES_PATH . 'endpoint/', $template_list,$lines); 
     21 
     22        //remove stuff that is meaningless in this context 
     23        unset($merged['data']['globals']['globals']['option|mac']); 
     24        unset($merged['data']['globals']['globals']['break|0']); 
     25        unset($merged['data']['globals']['globals']['option|provision_protocol']); 
     26         
     27        $html_array = $endpoint->generate_gui_html(serialize($merged), NULL, TRUE, NULL, $lines); 
     28         
     29        $specific_data = array(); 
     30        if (isset($_REQUEST['button_save'])) { 
     31            foreach ($_REQUEST as $key => $data) { 
     32                $key = str_replace('freepbx_', '', $key); 
     33                if (isset($_REQUEST['freepbx_' . $key])) { 
     34                    $specific_data['freepbx'][$key] = TRUE; 
     35                } 
     36                if ($endpoint->arraysearchrecursive($key, $html_array, 'key')) { 
     37                    if(!isset($specific_data['freepbx'][$key])) { 
     38                        $specific_data['data'][$key] = $data; 
     39                    } 
     40                } 
     41            } 
     42            $specific_data['data']['use_dhcp'] = isset($specific_data['data']['use_dhcp']) ? TRUE : FALSE; 
     43 
     44            $sql = "UPDATE endpointman_mac_list SET specific_settings = '" . serialize($specific_data) . "' WHERE id =" . $_REQUEST['edit_id']; 
     45            $endpoint->eda->sql($sql); 
     46        } else { 
     47            $sql = 'SELECT specific_settings FROM endpointman_mac_list WHERE id ='. $_REQUEST['edit_id']; 
     48            $specific_data = unserialize($endpoint->eda->sql($sql, 'getOne')); 
     49        } 
     50         
     51        foreach($phone_info['line'] as $data) { 
     52                $lid = $data['line']; 
     53                $specific_data['data']['line|'.$lid.'|displayname'] = !isset($specific_data['data']['line|'.$lid.'|displayname']) ? $data['description'] : $specific_data['data']['line|'.$lid.'|displayname']; 
     54                $specific_data['data']['line|'.$lid.'|username'] = !isset($specific_data['data']['line|'.$lid.'|username']) ? $data['ext'] : $specific_data['data']['line|'.$lid.'|username']; 
     55                $specific_data['data']['line|'.$lid.'|authname'] = !isset($specific_data['data']['line|'.$lid.'|authname']) ? $data['ext'] : $specific_data['data']['line|'.$lid.'|authname']; 
     56                $specific_data['data']['line|'.$lid.'|secret'] = !isset($specific_data['data']['line|'.$lid.'|secret']) ? $data['secret'] : $specific_data['data']['line|'.$lid.'|secret']; 
     57                $specific_data['data']['line|'.$lid.'|server_host'] = !isset($specific_data['data']['line|'.$lid.'|server_host']) ? $endpoint->global_cfg['srvip'] : $specific_data['data']['line|'.$lid.'|server_host']; 
     58                $specific_data['data']['line|'.$lid.'|server_port'] = !isset($specific_data['data']['line|'.$lid.'|server_port']) ? 5060 : $specific_data['data']['line|'.$lid.'|server_port']; 
     59                $specific_data['data']['line|'.$lid.'|line_enabled'] = !isset($specific_data['data']['line|'.$lid.'|line_enabled']) ? TRUE : $specific_data['data']['line|'.$lid.'|line_enabled']; 
     60        } 
     61 
     62        $specific_data['data']['ntp'] = isset($specific_data['data']['ntp']) ? $specific_data['data']['ntp'] : $endpoint->global_cfg['ntp']; 
     63        $specific_data['data']['use_dhcp'] = isset($specific_data['data']['use_dhcp']) ? TRUE : FALSE;   
     64         
     65        $html_array = $endpoint->generate_gui_html(serialize($merged), serialize($specific_data), TRUE, NULL, $lines); 
    3166    } 
    3267 
    3368    $endpoint->prepare_message_box(); 
    34     $endpoint->tpl->assign("display_name", $displayname); 
     69    $endpoint->tpl->assign("html_array", $html_array); 
    3570    echo $endpoint->tpl->draw( 'specifics_pop' ); 
    3671} 
  • modules/branches/2.10/endpointman/install.php

    r12911 r13276  
    135135     
    136136    out('Version Identified as '. $full_vers); 
     137    out('Internal Reference Number: '.$ver); 
    137138} else { 
    138139    $new_install = TRUE; 
     
    879880        $db->query($sql); 
    880881 
    881         $sql = "CREATE TABLE IF NOT EXISTS `endpointman_time_zones_desc` ( 
    882   `id` int(11) NOT NULL auto_increment, 
    883   `tid` int(11) NOT NULL, 
    884   `name` varchar(255) NOT NULL, 
    885   `description` varchar(255) NOT NULL, 
    886   PRIMARY KEY  (`id`) 
    887 ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=32"; 
    888         $db->query($sql); 
    889  
    890         $sql = "INSERT INTO `endpointman_time_zones_desc` (`id`, `tid`, `name`, `description`) VALUES 
    891 (1, 1, 'UTC', 'Universal Coordinated Time (and Greenwich Mean Time)'), 
    892 (2, 2, 'ECT', 'European Central Time'), 
    893 (3, 3, 'EET', 'Eastern European Time'), 
    894 (4, 3, 'ART', '(Arabic) Egypt Standard Time'), 
    895 (5, 4, 'EAT', 'Eastern African Time'), 
    896 (6, 5, 'MET', 'Middle East Time'), 
    897 (7, 6, 'NET', 'Near East Time'), 
    898 (8, 7, 'PLT', 'Pakistan Lahore Time'), 
    899 (9, 8, 'IST', 'India Standard Time'), 
    900 (10, 9, 'BST', 'Bangladesh Standard Time'), 
    901 (11, 10, 'VST', 'Vietnam Standard Time'), 
    902 (12, 11, 'CTT', 'China Taiwan Time'), 
    903 (13, 12, 'JST', 'Japan Standard Time'), 
    904 (14, 13, 'ACT', 'Australia Central Time'), 
    905 (15, 14, 'AET', 'Australia Eastern Time'), 
    906 (16, 15, 'SST', 'Solomon Standard Time'), 
    907 (17, 16, 'NST', 'New Zealand Standard Time'), 
    908 (18, 17, 'MIT', 'Midway Islands Time'), 
    909 (19, 18, 'HST', 'Hawaii Standard Time'), 
    910 (20, 19, 'AST', 'Alaska Standard Time'), 
    911 (21, 20, 'PST', 'Pacific Standard Time'), 
    912 (22, 21, 'PNT', 'Phoenix Standard Time'), 
    913 (23, 21, 'MST', 'Mountain Standard Time'), 
    914 (24, 22, 'CST', 'Central Standard Time'), 
    915 (25, 23, 'EST', 'Eastern Standard Time'), 
    916 (26, 23, 'IET', 'Indiana Eastern Standard Time'), 
    917 (27, 24, 'PRT', 'Puerto Rico and US Virgin Islands Time'), 
    918 (28, 25, 'CNT', 'Canada Newfoundland Time'), 
    919 (29, 26, 'AGT', 'Argentina Standard Time'), 
    920 (30, 26, 'BET', 'Brazil Eastern Time'), 
    921 (31, 27, 'CAT', 'Central African Time')"; 
    922         $db->query($sql); 
    923  
    924         $sql = "CREATE TABLE IF NOT EXISTS `endpointman_time_zones_new` ( 
    925   `id` int(11) NOT NULL auto_increment, 
    926   `gmt` varchar(255) NOT NULL, 
    927   `offset` int(11) NOT NULL, 
    928   PRIMARY KEY  (`id`) 
    929 ) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=28"; 
    930         $db->query($sql); 
    931  
    932         $sql = "INSERT INTO `endpointman_time_zones_new` (`id`, `gmt`, `offset`) VALUES 
    933 (1, 'GMT', 0), 
    934 (2, 'GMT+1:00', 3600), 
    935 (3, 'GMT+2:00', 7200), 
    936 (4, 'GMT+3:00', 10800), 
    937 (5, 'GMT+3:30', 12600), 
    938 (6, 'GMT+4:00', 14400), 
    939 (7, 'GMT+5:00', 18000), 
    940 (8, 'GMT+5:30', 19800), 
    941 (9, 'GMT+6:00', 21600), 
    942 (10, 'GMT+7:00', 25200), 
    943 (11, 'GMT+8:00', 28800), 
    944 (12, 'GMT+9:00', 32400), 
    945 (13, 'GMT+9:30', 34200), 
    946 (14, 'GMT+10:00', 36000), 
    947 (15, 'GMT+11:00', 39600), 
    948 (16, 'GMT+12:00', 43200), 
    949 (17, 'GMT-11:00', -39600), 
    950 (18, 'GMT-10:00', -36000), 
    951 (19, 'GMT-9:00', -32400), 
    952 (20, 'GMT-8:00', -28800), 
    953 (21, 'GMT-7:00', -25200), 
    954 (22, 'GMT-6:00', -21600), 
    955 (23, 'GMT-5:00', -18000), 
    956 (24, 'GMT-4:00', -14400), 
    957 (25, 'GMT-3:30', -12600), 
    958 (26, 'GMT-3:00', -10800), 
    959 (27, 'GMT-1:00', -3600)"; 
    960         $db->query($sql); 
    961  
    962882        out('Creating symlink to web provisioner'); 
    963883        if (!symlink(LOCAL_PATH . "provisioning", $amp_conf['AMPWEBROOT'] . "/provisioning")) { 
     
    981901    } 
    982902     
    983     if ($ver <= "21002") { 
     903    if($ver <= "21021") { 
    984904        out('Updating Mirror Location...again'); 
    985         $sql = "UPDATE endpointman_global_vars SET value = 'http://mirror.freepbx.org/provisioner/v2.5/' WHERE var_name ='update_server'"; 
     905        $sql = "UPDATE endpointman_global_vars SET value = 'http://mirror.freepbx.org/provisioner/v3/' WHERE var_name ='update_server'"; 
     906        $db->query($sql); 
     907                 
     908        out("Uninstalling All Installed Brands (You'll just simply have to update again, no loss of data)"); 
     909        $db->query("UPDATE endpointman_brand_list SET installed =  '0'"); 
     910        $db->query("TRUNCATE TABLE endpointman_brand_list"); 
     911         
     912        $sql = "UPDATE  endpointman_model_list SET  enabled =  '0', template_data = '".serialize(array())."'"; 
     913        $db->query($sql); 
     914 
     915        out("Moving old brand data for backups, its now in ". $amp_conf['AMPWEBROOT']."/admin/modules/_ep_phone_modules_old"); 
     916        exec("mv ". $amp_conf['AMPWEBROOT']."/admin/modules/_ep_phone_modules" . " ". $amp_conf['AMPWEBROOT']."/admin/modules/_ep_phone_modules_old"); 
     917 
     918        if(!file_exists(PHONE_MODULES_PATH)) { 
     919            mkdir(PHONE_MODULES_PATH, 0764); 
     920            out("Creating Phone Modules Directory"); 
     921        } 
     922 
     923        if(!file_exists(PHONE_MODULES_PATH."setup.php")) { 
     924            copy(LOCAL_PATH."install/setup.php",PHONE_MODULES_PATH."setup.php"); 
     925            out("Moving Auto Provisioner Class"); 
     926        } 
     927 
     928        if(!file_exists(PHONE_MODULES_PATH."temp/")) { 
     929            mkdir(PHONE_MODULES_PATH."temp/", 0764); 
     930            out("Creating temp folder"); 
     931        } 
     932    } 
     933     
     934    if($ver <= "21024") { 
     935        out('Updating Mirror Location...again'); 
     936        $sql = "UPDATE endpointman_global_vars SET value = 'http://mirror.freepbx.org/provisioner/v3/' WHERE var_name ='update_server'"; 
    986937        $db->query($sql); 
    987938    } 
     
    1040991            (4, 'gmthr', ''), 
    1041992            (5, 'config_location', '/tftpboot/'), 
    1042             (6, 'update_server', 'http://mirror.freepbx.org/provisioner/v2.5/'), 
     993            (6, 'update_server', 'http://mirror.freepbx.org/provisioner/v3/'), 
    1043994            (7, 'version', '".$xml_full_version."'), 
    1044995            (8, 'enable_ari', '0'), 
     
    11511102$db->query($sql); 
    11521103 
    1153 $sql = "UPDATE endpointman_global_vars SET value = 'http://mirror.freepbx.org/provisioner/v2.5/' WHERE var_name = 'update_server'"; 
     1104$sql = "UPDATE endpointman_global_vars SET value = 'http://www.provisioner.net/release/v3/' WHERE var_name = 'update_server'"; 
    11541105$db->query($sql); 
    11551106 
  • modules/branches/2.10/endpointman/module.xml

    r12940 r13276  
    33  <repo>standard</repo> 
    44  <name>PBX End Point Manager</name> 
    5   <version>2.10.1.3</version> 
     5  <version>2.10.2.4</version> 
    66  <type>setup</type> 
    77  <category>Connectivity</category> 
  • modules/branches/2.10/endpointman/provisioning/p.php

    r12938 r13276  
    5252    if(preg_match('/[0-9A-Fa-f]{12}/i', $strip, $matches) && !(preg_match('/[0]{10}[0-9]{2}/i',$strip))) { 
    5353        $mac_address = $matches[0]; 
    54  
     54         
    5555        $sql = 'SELECT id FROM `endpointman_mac_list` WHERE `mac` LIKE CONVERT(_utf8 \'%' . $mac_address . '%\' USING latin1) COLLATE latin1_swedish_ci'; 
    5656 
    57         $mac_id = $endpoint->db->getOne($sql); 
     57        $mac_id = $endpoint->eda->sql($sql, 'getOne'); 
    5858        $phone_info = $endpoint->get_phone_info($mac_id); 
    5959 
    60         if (file_exists(PROVISIONER_BASE . 'setup.php')) { 
    61             if (!class_exists('ProvisionerConfig')) { 
    62                 require(PROVISIONER_BASE . 'setup.php'); 
    63             } 
    64  
    65  
    66             //Load Provisioner 
    67             $class = "endpoint_" . $phone_info['directory'] . "_" . $phone_info['cfg_dir'] . '_phone'; 
    68             $base_class = "endpoint_" . $phone_info['directory'] . '_base'; 
    69             $master_class = "endpoint_base"; 
    70  
    71             if (!class_exists($master_class)) { 
    72                 ProvisionerConfig::endpointsAutoload($master_class); 
    73             } 
    74             if (!class_exists($base_class)) { 
    75                 ProvisionerConfig::endpointsAutoload($base_class); 
    76             } 
    77             if (!class_exists($class)) { 
    78                 ProvisionerConfig::endpointsAutoload($class); 
    79             } 
    80             //end quick fix 
    81  
    82             if (class_exists($class)) { 
    83                 $provisioner_libary = new $class(); 
    84                     //Determine if global settings have been overridden 
    85                 $settings = ''; 
    86                 if ($phone_info['template_id'] > 0) { 
    87                     if (isset($phone_info['template_data_info']['global_settings_override'])) { 
    88                         $settings = unserialize($phone_info['template_data_info']['global_settings_override']); 
    89                     } else { 
    90                         $settings['srvip'] = $endpoint->global_cfg['srvip']; 
    91                         $settings['ntp'] = $endpoint->global_cfg['ntp']; 
    92                         $settings['config_location'] = $endpoint->global_cfg['config_location']; 
    93                         $settings['tz'] = $endpoint->global_cfg['tz']; 
    94                     } 
    95                 } else { 
    96                     if (isset($phone_info['global_settings_override'])) { 
    97                         $settings = unserialize($phone_info['global_settings_override']); 
    98                     } else { 
    99                         $settings['srvip'] = $endpoint->global_cfg['srvip']; 
    100                         $settings['ntp'] = $endpoint->global_cfg['ntp']; 
    101                         $settings['config_location'] = $endpoint->global_cfg['config_location']; 
    102                         $settings['tz'] = $endpoint->global_cfg['tz']; 
    103                     } 
    104                 } 
    105  
    106                 //Tell the system who we are and were to find the data. 
    107                 $provisioner_libary->root_dir = PROVISIONER_BASE; 
    108                 $provisioner_libary->engine = 'asterisk'; 
    109                 $provisioner_libary->engine_location = $endpoint->global_cfg['asterisk_location']; 
    110                 $provisioner_libary->system = 'unix'; 
    111  
    112                 //have to because of versions less than php5.3 
    113                 $provisioner_libary->brand_name = $phone_info['directory']; 
    114                 $provisioner_libary->family_line = $phone_info['cfg_dir']; 
    115  
    116                 //Mac Address 
    117                 $provisioner_libary->mac = $phone_info['mac']; 
    118  
    119                 //Phone Model (Please reference family_data.xml in the family directory for a list of recognized models) 
    120                 //This has to match word for word. I really need to fix this.... 
    121                 $provisioner_libary->model = $phone_info['model']; 
    122  
    123                 //Timezone 
    124                 $http_provisioner->DateTimeZone = new DateTimeZone($settings['tz']); 
    125  
    126  
    127                 //Network Time Server 
    128                 $provisioner_libary->ntp = $settings['ntp']; 
    129  
    130                 //Server IP 
    131                 $provisioner_libary->server[1]['ip'] = $settings['srvip']; 
    132                 $provisioner_libary->server[1]['port'] = 5060; 
    133  
    134                 $temp = ""; 
    135                 $template_data = unserialize($phone_info['template_data']); 
    136                 $global_user_cfg_data = unserialize($phone_info['global_user_cfg_data']); 
    137                 if ($phone_info['template_id'] > 0) { 
    138                     $global_custom_cfg_data = unserialize($phone_info['template_data_info']['global_custom_cfg_data']); 
    139                     //Provide alternate Configuration file instead of the one from the hard drive 
    140                     if (!empty($phone_info['template_data_info']['config_files_override'])) { 
    141                         $temp = unserialize($phone_info['template_data_info']['config_files_override']); 
    142                         foreach ($temp as $list) { 
    143                             $sql = "SELECT original_name,data FROM endpointman_custom_configs WHERE id = " . $list; 
    144                             $res = $endpoint->db->query($sql); 
    145                             if ($res->numRows()) { 
    146                                 $data = $endpoint->db->getRow($sql, array(), DB_FETCHMODE_ASSOC); 
    147                                 $provisioner_libary->config_files_override[$data['original_name']] = $data['data']; 
    148                             } 
    149                         } 
    150                     } 
    151                 } else { 
    152                     $global_custom_cfg_data = unserialize($phone_info['global_custom_cfg_data']); 
    153                     //Provide alternate Configuration file instead of the one from the hard drive 
    154                     if (!empty($phone_info['config_files_override'])) { 
    155                         $temp = unserialize($phone_info['config_files_override']); 
    156                         foreach ($temp as $list) { 
    157                             $sql = "SELECT original_name,data FROM endpointman_custom_configs WHERE id = " . $list; 
    158                             $res = $endpoint->db->query($sql); 
    159                             if ($res->numRows()) { 
    160                                 $data = $endpoint->db->getRow($sql, array(), DB_FETCHMODE_ASSOC); 
    161                                 $provisioner_libary->config_files_override[$data['original_name']] = $data['data']; 
    162                             } 
    163                         } 
    164                     } 
    165                 } 
    166  
    167                 if (!empty($global_custom_cfg_data)) { 
    168                     if (array_key_exists('data', $global_custom_cfg_data)) { 
    169                         $global_custom_cfg_ari = $global_custom_cfg_data['ari']; 
    170                         $global_custom_cfg_data = $global_custom_cfg_data['data']; 
    171                     } else { 
    172                         $global_custom_cfg_data = array(); 
    173                         $global_custom_cfg_ari = array(); 
    174                     } 
    175                 } 
    176  
    177                 $new_template_data = array(); 
    178                 $line_ops = array(); 
    179                 if (is_array($global_custom_cfg_data)) { 
    180                     foreach ($global_custom_cfg_data as $key => $data) { 
    181                         $full_key = $key; 
    182                         $key = explode('|', $key); 
    183                         $count = count($key); 
    184                         switch ($count) { 
    185                             case 1: 
    186                                 if (($endpoint->global_cfg['enable_ari'] == 1) AND (isset($global_custom_cfg_ari[$full_key])) AND (isset($global_user_cfg_data[$full_key]))) { 
    187                                     $new_template_data[$full_key] = $global_user_cfg_data[$full_key]; 
    188                                 } else { 
    189                                     $new_template_data[$full_key] = $global_custom_cfg_data[$full_key]; 
    190                                 } 
    191                                 break; 
    192                             case 2: 
    193                                 $breaks = explode('_', $key[1]); 
    194                                 if (($endpoint->global_cfg['enable_ari'] == 1) AND (isset($global_custom_cfg_ari[$full_key])) AND (isset($global_user_cfg_data[$full_key]))) { 
    195                                     $new_template_data[$breaks[0]][$breaks[2]][$breaks[1]] = $global_user_cfg_data[$full_key]; 
    196                                 } else { 
    197                                     $new_template_data[$breaks[0]][$breaks[2]][$breaks[1]] = $global_custom_cfg_data[$full_key]; 
    198                                 } 
    199                                 break; 
    200                             case 3: 
    201                                 if (($endpoint->global_cfg['enable_ari'] == 1) AND (isset($global_custom_cfg_ari[$full_key])) AND (isset($global_user_cfg_data[$full_key]))) { 
    202                                     $line_ops[$key[1]][$key[2]] = $global_user_cfg_data[$full_key]; 
    203                                 } else { 
    204                                     $line_ops[$key[1]][$key[2]] = $global_custom_cfg_data[$full_key]; 
    205                                 } 
    206                                 break; 
    207                         } 
    208                     } 
    209                 } 
    210  
    211                 //Loop through Lines! 
    212                 foreach ($phone_info['line'] as $line) { 
    213                     $provisioner_libary->lines[$line['line']] = array('ext' => $line['ext'], 'secret' => $line['secret'], 'displayname' => $line['description']); 
    214                 } 
    215  
    216                 //testing this out 
    217                 foreach ($line_ops as $key => $data) { 
    218                     if (isset($line_ops[$key])) { 
    219                         $provisioner_libary->lines[$key]['options'] = $line_ops[$key]; 
    220                     } 
    221                 } 
    222  
    223                 $provisioner_libary->server_type = 'dynamic'; 
    224                 $provisioner_libary->provisioning_type = 'http'; 
    225                 $provisioner_libary->provisioning_path = $provis_ip.dirname($_SERVER['REQUEST_URI']); 
    226  
    227                 //Set Variables according to the template_data files included. We can include different template.xml files within family_data.xml also one can create 
    228                 //template_data_custom.xml which will get included or template_data_<model_name>_custom.xml which will also get included 
    229                 //line 'global' will set variables that aren't line dependant 
    230                 $provisioner_libary->options = $new_template_data; 
    231  
    232                 //Setting a line variable here...these aren't defined in the template_data.xml file yet. however they will still be parsed 
    233                 //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 
    234                 $provisioner_libary->processor_info = "EndPoint Manager Version " . $endpoint->global_cfg['version']; 
    235  
    236                 $files = $provisioner_libary->generate_config(); 
    237  
    238                 if(array_key_exists($filename, $files)) { 
    239                     echo $files[$filename]; 
    240                 } else { 
    241                     header("HTTP/1.0 404 Not Found"); 
    242                     die(); 
    243                 } 
    244             } else { 
    245                 header("HTTP/1.0 500 Internal Server Error"); 
    246                 die(); 
    247             } 
    248         } else { 
     60        $files = $endpoint->prepare_configs($phone_info,FALSE,FALSE); 
     61         
     62        if(!files) { 
    24963            header("HTTP/1.0 500 Internal Server Error"); 
    25064            die(); 
    25165        } 
     66         
     67        if (array_key_exists($filename, $files)) { 
     68            echo $files[$filename]; 
     69        } else { 
     70            header("HTTP/1.0 404 Not Found"); 
     71            die(); 
     72        } 
     73 
    25274    } else { 
    25375        require_once (PROVISIONER_BASE.'endpoint/base.php'); 
  • modules/branches/2.10/endpointman/templates/freepbx/advanced_settings_settings.html

    r12768 r13276  
    7979</tr> 
    8080<tr> 
    81   <td align='right'><?php echo _("Enable FreePBX ARI Module")?> (<a href="http://projects.colsolgrp.net/documents/29" target="_blank">What?</a>)</td> 
     81  <td align='right'><?php echo _("Enable FreePBX ARI Module")?> (<a href="http://wiki.provisioner.net/index.php/Endpoint_manager_manual_ari" target="_blank">What?</a>)</td> 
    8282  <td align='left'><label> 
    8383    <input type=checkbox name="enable_ari" {$ari_selected}> 
  • modules/branches/2.10/endpointman/templates/freepbx/devices_manager.html

    r12938 r13276  
    181181    function popitup(url, name, id) { 
    182182        if(id != '0') { 
    183             newwindow=window.open(url + '&model_list=' + document.getElementById('model_new').value + '&template_list=' + document.getElementById('template_list').value + '&rand=' + new Date().getTime(),'name2','height=1000,width=950'); 
     183            newwindow=window.open(url + '&model_list=' + document.getElementById('model_new').value + '&template_list=' + document.getElementById('template_list').value + '&rand=' + new Date().getTime(),'name2','height=1000,width=950,scrollbars=yes,location=no'); 
    184184            if (window.focus) {newwindow.focus()} 
    185185            return false; 
     
    188188 
    189189    function submit_stype(type,id) { 
    190         newwindow=window.open('config.php?display=epm_config&quietmode=1&handler=file&file=popup.html.php&module=endpointman&pop_type=edit_specifics&edit_id=' + id + '&rand=' + new Date().getTime(),'name2','height=700,width=750'); 
     190        newwindow=window.open('config.php?display=epm_config&quietmode=1&handler=file&file=popup.html.php&module=endpointman&pop_type=edit_specifics&edit_id=' + id + '&rand=' + new Date().getTime(),'name2','height=700,width=750,scrollbars=yes,location=no'); 
    191191        if (window.focus) {newwindow.focus()} 
    192192        return false; 
     
    441441        <td align='center'><h3>Extension Number</h3></td> 
    442442        <td align='center'><h3>Template</h3></td> 
    443         <!--<td align='center'><h3>Specific</h3></td>--
     443        <td align='center'><h3>Specific</h3></td
    444444        <td align='center'><h3>Edit</h3></td> 
    445445        <td align='center'><h3>Delete</h3></td> 
     
    454454            <td width="19%" align='center'><div id="demo"><a><img src="assets/endpointman/images/expand.png" id="img3rowGroup{$value.master_id}" onclick="toggleDisplay(document.getElementById('devList'),'rowGroup{$value.master_id}')" title="Click to Expand Line Information"></a></div></td> 
    455455            <td align='center' width='15%'>{$value.template_name}</td> 
    456             <!--<td align='center'><div id="demo"><a href="#" onclick="submit_stype('edit',{$value.id});"><img src='assets/endpointman/images/edit.png' ALT='<?php echo _('Edit')?>' title='Edit Device' border='0'></a></div></td>--
     456            <td align='center'><div id="demo"><a href="#" onclick="submit_stype('edit',{$value.id});"><img src='assets/endpointman/images/edit.png' ALT='<?php echo _('Edit')?>' title='Edit Device' border='0'></a></div></td
    457457            <td align='center' width='9%'><div id="demo"><a href="#" onclick="submit_wtype('edit',{$value.id});"><img src='assets/endpointman/images/edit.png' ALT='<?php echo _('Edit')?>' title='Edit Device' border='0'></a></div></td> 
    458458            <td align='center' width='7%'><div id="demo"><a href="#" onclick="delete_device({$value.id});"><img src='assets/endpointman/images/delete.png' ALT='<?php echo _('Delete')?>' title='Delete Device' border='0'></a></div></td> 
  • modules/branches/2.10/endpointman/templates/freepbx/specifics_pop.html

    r12831 r13276  
    1111                $('#myform').append('<input type="hidden" name="button_save" value="Save"/>'); 
    1212                document.myform.submit(); 
    13                 setTimeout("opener.Reload()",100); 
    1413            } 
    1514        </script> 
    1615    </head> 
    1716    <body face="Arial"> 
    18         <div id="spinner"> 
    19         </div> 
    2017        <br> 
    2118        <h1><face="Arial"><center><?php echo _('End Point Configuration Manager')?></center></h1> 
     
    2421  {include="message_box"} 
    2522  {/if} 
     23        {include="variables"} 
    2624      <form name="myform" id="myform" method="post" action=""><input type="hidden" id="value" name="value" value="{$value}" /> 
    2725    <table width="90%" border="0" cellspacing="0" cellpadding="0"> 
    28         <tr><td colspan="2"><u>General Settings</u></td></tr> 
    29         {loop name="display_name"} 
    30         <tr><td>Display Name Line {$value.line}:</td><td><input type="text" name="displayname_{$value.line}" value="{$value.displayname}" /></td></tr> 
    31         {/loop} 
    32         <tr><td colspan="2">&nbsp;</td></tr> 
    33         <tr><td colspan="2"><u>IP Settings</u></td></tr> 
    34         <tr><td>IP Type</td><td> 
    35 <select> 
    36   <option value="dhcp">DHCP</option> 
    37   <option value="static">Static</option> 
    38 </select> 
    39       </td></tr> 
    40         <tr><td width="30%">IPv4 Address:</td><td><input type="text" name="ipv4" value="" /></td></tr> 
    41         <tr><td>IPv6 Address:</td><td><input type="text" name="ipv46" value="" /></td></tr> 
    42         <tr><td>Subnet Mask Address:</td><td><input type="text" name="subnet" value="" /></td></tr> 
    43         <tr><td>Gateway Address:</td><td><input type="text" name="gateway" value="" /></td></tr> 
    44         <tr> 
    45       <td colspan="2"> 
    46           <br><div style="font-size: 15px"> 
    47         <label> 
    48             <input type="submit" name="button_save" value="<?php echo _('Save')?>" onClick="javascript:submitform();"><input type=button value="Close Window" onClick="javascript:Close();"> 
    49         </label> 
    50           </td> 
    51         </tr> 
     26{loop name="html_array"} 
     27<tr><td colspan="2">&nbsp;</td></tr> 
     28<tr><td colspan="2"><u><strong>{$value.title}</strong></u></td></tr> 
     29{loop name="value.data"} 
     30{if condition="$value.type != 'break'"} 
     31                                <tr> 
     32                                    <td width="50%" nowrap> 
     33          {if condition="$value.type == 'input'"}         
     34              <label>{if condition="isset($value.tooltip)"}<a href="#" class="info">{$value.description}<span>{$value.tooltip}</span></a>{else}{$value.description}{/if}: <input type='text' name='{$value.key}' id='{$value.key}' value='{$value.value}' size="{if condition="isset($value.max_chars)"}{$value.max_chars}{else}40{/if}"></label> 
     35          {elseif condition="$value.type == 'textarea'"} 
     36              {if condition="isset($value.tooltip)"}<a href="#" class="info">{$value.description}<span>{$value.tooltip}</span></a>{else}{$value.description}{/if}: <textarea rows="2" cols="20" name='{$value.key}' id='{$value.key}'>{$value.value}</textarea> 
     37          {elseif condition="$value.type == 'file'"} 
     38              <label>{if condition="isset($value.tooltip)"}<a href="#" class="info">{$value.description}<span>{$value.tooltip}</span></a>{else}{$value.description}{/if}: <input type='file' name='{$value.key}' id='{$value.key}' value='{$value.value}'></label> 
     39          {elseif condition="$value.type == 'radio'"} 
     40            {if condition="isset($value.tooltip)"}<a href="#" class="info">{$value.description}<span>{$value.tooltip}</span></a>{else}{$value.description}{/if} 
     41              {loop name="value.data"} 
     42                  <label>{if condition="isset($value.tooltip)"}<a href="#" class="info">{$value.description}<span>{$value.tooltip}</span></a>{else}{$value.description}{/if}: <input type='radio' name='{$value.key}' id='{$value.key}' value='{$value.value}' {if condition="array_key_exists('checked',$value)"}{$value.checked}{/if}></label> 
     43              {/loop} 
     44          {elseif condition="$value.type == 'list'"} 
     45              {if condition="isset($value.tooltip)"}<a href="#" class="info">{$value.description}<span>{$value.tooltip}</span></a>{else}{$value.description}{/if}: <select name='{$value.key}' id='{$value.key}'> 
     46              {loop name="value.data"} 
     47                  <option value='{$value.value}' {if condition="array_key_exists('selected',$value)"}{$value.selected}{/if}>{$value.description}</option> 
     48              {/loop} 
     49                                        </select> 
     50                                        {elseif condition="$value.type == 'checkbox'"} 
     51                                            <label>{if condition="isset($value.tooltip)"}<a href="#" class="info">{$value.description}<span>{$value.tooltip}</span></a>{else}{$value.description}{/if}: <input type='checkbox' name='{$value.key}' id='{$value.key}' value='{$value.value}' {if condition="$value.checked === TRUE"}checked{/if}></label> 
     52          {/if} 
     53                                    </td> 
     54                                    <td><label><input type='checkbox' name='freepbx_{$value.key}' id='freepbx_{$value.key}' value='freepbx_{$value.value}' {if condition="$value.freepbx.checked === TRUE"}checked{/if}> FreePBX Controlled</label></td> 
     55                                </tr> 
     56                                {/if} 
     57            {if condition="$value.type == 'break'"} 
     58                                <tr> 
     59                                    <td>&nbsp;</td> 
     60                                    <td>&nbsp;</td> 
     61                                </tr> 
     62            {/if} 
     63          {/loop} 
     64                                        {/loop} 
    5265    </table> 
    53       </form> 
     66      <input type="submit" value="Submit" onClick="javascript:submitform();" /><input type=button value="Close Window" onClick="javascript:Close();"> 
     67      </form> 
    5468  </body> 
    5569</html> 
  • modules/branches/2.10/endpointman/templates/freepbx/template_editor.html

    r12939 r13276  
    107107        {/loop} 
    108108            <br /> 
    109             <?php echo _('You can also use certain variables in your configs')?>:<br />{noparse} 
    110   -"{$server.ip.1}" = <?php echo _('Server 1 IP')?><br /> 
    111         -"{$server.port.1}" = <?php echo _('Server 1 Port')?><br /> 
    112   -"{$mac}" = <?php echo _('Device\'s Mac Address')?><br /> 
    113         -"{$model}" = <?php echo _('Device\'s Model')?><br /> 
    114   -"{$ext.line.1}" = <?php echo _('Device\'s Default Extension (Line 1 or Master)')?><br /> 
    115   -"{$displayname.line.1}" = <?php echo _('Device\'s Description in FreePBX (Usually the Full Name)')?><br />{/noparse} 
     109            {include="variables"} 
    116110            <br /> 
    117111            {/if} 
     
    144138              {/loop} 
    145139                                        </select> 
     140                                        {elseif condition="$value.type == 'checkbox'"} 
     141                                            <label>{if condition="isset($value.tooltip)"}<a href="#" class="info">{$value.description}<span>{$value.tooltip}</span></a>{else}{$value.description}{/if}: <input type='checkbox' name='{$value.key}' id='{$value.key}' value='{$value.value}'></label> 
    146142          {/if} 
    147143                                    </td><td width="90%">