2012-06-04 14:58:40 +00:00
<? php
/*
FusionPBX
Version: MPL 1.1
The contents of this file are subject to the Mozilla Public License Version
1.1 (the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
for the specific language governing rights and limitations under the
License.
The Original Code is FusionPBX
The Initial Developer of the Original Code is
Mark J Crane <markjcrane@fusionpbx.com>
2013-08-28 05:05:07 +00:00
Copyright (C) 2008-2013 All Rights Reserved.
2012-06-04 14:58:40 +00:00
Contributor(s):
Mark J Crane <markjcrane@fusionpbx.com>
*/
include "root.php" ;
2013-07-06 06:03:27 +00:00
require_once "resources/require.php" ;
2013-07-06 05:50:55 +00:00
require_once "resources/check_auth.php" ;
2012-06-04 14:58:40 +00:00
if ( if_group ( "admin" ) || if_group ( "superadmin" )) {
//access granted
}
else {
echo "access denied" ;
exit ;
}
//set default variables
$dir_count = 0 ;
$file_count = 0 ;
$row_count = 0 ;
$tmp_array = '' ;
//get the hardware phone list
2013-04-09 22:33:42 +00:00
$sql = "select * from v_devices " ;
2012-06-04 14:58:40 +00:00
$sql .= "where domain_uuid = ' $domain_uuid ' " ;
$prep_statement = $db -> prepare ( check_sql ( $sql ));
$prep_statement -> execute ();
$result = $prep_statement -> fetchAll ( PDO :: FETCH_NAMED );
foreach ( $result as & $row ) {
2013-05-27 22:35:35 +00:00
$device_uuid = $row [ "device_uuid" ];
2013-04-09 22:33:42 +00:00
$device_mac_address = $row [ "device_mac_address" ];
$device_label = $row [ "device_label" ];
2013-11-01 18:51:52 +00:00
$device_vendor = strtolower ( $row [ "device_vendor" ]);
2013-04-09 22:33:42 +00:00
$device_model = $row [ "device_model" ];
$device_firmware_version = $row [ "device_firmware_version" ];
$device_provision_enable = $row [ "device_provision_enable" ];
$device_template = $row [ "device_template" ];
$device_username = $row [ "device_username" ];
$device_password = $row [ "device_password" ];
$device_time_zone = $row [ "device_time_zone" ];
$device_description = $row [ "device_description" ];
2012-06-04 14:58:40 +00:00
2012-07-05 18:22:02 +00:00
//use the mac address to find the vendor
2013-04-09 22:33:42 +00:00
if ( strlen ( $device_vendor ) == 0 ) {
2013-09-17 19:07:11 +00:00
switch ( substr ( $device_mac_address , 0 , 6 )) {
2012-07-05 18:22:02 +00:00
case "00085d" :
2013-04-09 22:33:42 +00:00
$device_vendor = "aastra" ;
2012-07-05 18:22:02 +00:00
break ;
case "000e08" :
2013-04-09 22:33:42 +00:00
$device_vendor = "linksys" ;
2012-07-05 18:22:02 +00:00
break ;
case "0004f2" :
2013-04-09 22:33:42 +00:00
$device_vendor = "polycom" ;
2012-07-05 18:22:02 +00:00
break ;
case "00907a" :
2013-04-09 22:33:42 +00:00
$device_vendor = "polycom" ;
2012-07-05 18:22:02 +00:00
break ;
2013-08-30 00:47:58 +00:00
case "0080f0" :
$device_vendor = "panasonic" ;
break ;
2012-07-05 18:22:02 +00:00
case "001873" :
2013-04-09 22:33:42 +00:00
$device_vendor = "cisco" ;
2012-07-05 18:22:02 +00:00
break ;
2013-08-30 00:47:58 +00:00
case "a44c11" :
$device_vendor = "cisco" ;
break ;
2013-07-29 17:22:30 +00:00
case "0021A0" :
$device_vendor = "cisco" ;
break ;
2013-08-30 00:47:58 +00:00
case "30e4db" :
$device_vendor = "cisco" ;
break ;
2013-07-29 17:22:30 +00:00
case "002155" :
$device_vendor = "cisco" ;
break ;
2013-08-30 00:47:58 +00:00
case "68efbd" :
$device_vendor = "cisco" ;
break ;
2012-07-05 18:22:02 +00:00
case "00045a" :
2013-04-09 22:33:42 +00:00
$device_vendor = "linksys" ;
2012-07-05 18:22:02 +00:00
break ;
case "000625" :
2013-04-09 22:33:42 +00:00
$device_vendor = "linksys" ;
2012-07-05 18:22:02 +00:00
break ;
case "001565" :
2013-04-09 22:33:42 +00:00
$device_vendor = "yealink" ;
2012-07-05 18:22:02 +00:00
break ;
case "000413" :
2013-04-09 22:33:42 +00:00
$device_vendor = "snom" ;
2013-08-30 00:47:58 +00:00
break ;
case "000b82" :
$device_vendor = "grandstream" ;
break ;
case "00177d" :
$device_vendor = "konftel" ;
break ;
2012-07-05 18:22:02 +00:00
default :
2013-04-09 22:33:42 +00:00
$device_vendor = "" ;
2012-07-05 18:22:02 +00:00
}
}
//set the mac address in the correct format
2013-04-09 22:33:42 +00:00
switch ( $device_vendor ) {
2012-07-05 18:22:02 +00:00
case "aastra" :
2013-10-23 21:20:07 +00:00
//upper case no formatting
2013-09-04 16:13:07 +00:00
$device_mac_address = strtoupper ( $device_mac_address );
2012-07-05 18:22:02 +00:00
break ;
2013-10-23 21:20:07 +00:00
case "polycom" :
//lower case no formatting
$device_mac_address = strtolower ( $device_mac_address );
$device_mac_address = str_replace ( "-" , "" , $device_mac_address );
break ;
2012-07-05 18:22:02 +00:00
case "snom" :
2013-10-23 21:20:07 +00:00
//upper case with dashes
2013-09-04 16:13:07 +00:00
$device_mac_address = strtoupper ( $device_mac_address );
$device_mac_address = str_replace ( "-" , "" , $device_mac_address );
2012-07-05 18:22:02 +00:00
default :
2013-10-23 21:20:07 +00:00
//lower case with dashes
2013-09-04 16:13:07 +00:00
$device_mac_address = strtolower ( $device_mac_address );
$device_mac_address = substr ( $device_mac_address , 0 , 2 ) . '-' . substr ( $device_mac_address , 2 , 2 ) . '-' . substr ( $device_mac_address , 4 , 2 ) . '-' . substr ( $device_mac_address , 6 , 2 ) . '-' . substr ( $device_mac_address , 8 , 2 ) . '-' . substr ( $device_mac_address , 10 , 2 );
2012-07-05 18:22:02 +00:00
}
2012-06-04 14:58:40 +00:00
//loop through the provision template directory
2013-06-09 05:05:17 +00:00
$provision_template_dir = $_SERVER [ "DOCUMENT_ROOT" ] . PROJECT_PATH . "/resources/templates/provision/" . $device_template ;
2012-06-04 14:58:40 +00:00
clearstatcache ();
$dir_list = '' ;
$file_list = '' ;
$dir_list = opendir ( $provision_template_dir );
$dir_array = array ();
while ( false !== ( $file = readdir ( $dir_list ))) {
if ( $file != "." AND $file != ".." ){
$new_path = $dir . '/' . $file ;
$level = explode ( '/' , $new_path );
if ( substr ( $new_path , - 4 ) == ".svn" ) {
//ignore .svn dir and subdir
}
elseif ( substr ( $new_path , - 3 ) == ".db" ) {
//ignore .db files
}
else {
$dir_array [] = $new_path ;
}
if ( $x > 1000 ) { break ; };
$x ++ ;
}
}
//asort($dir_array);
foreach ( $dir_array as $new_path ){
$level = explode ( '/' , $new_path );
if ( is_dir ( $new_path )) {
//$mod_array[] = array(
//'level'=>count($level)-1,
//'path'=>$new_path,
//'name'=>end($level),
//'type'=>'dir',
//'mod_time'=>filemtime($new_path),
//'size'=>'');
//$mod_array[] = recur_dir($new_path);
$dir_name = end ( $level );
//$file_list .= "$dir_name\n";
//$dir_list .= recur_dir($new_path);
}
else {
//$mod_array[] = array(
//'level'=>count($level)-1,
//'path'=>$new_path,
//'name'=>end($level),
//'type'=>'dir',
//'mod_time'=>filemtime($new_path),
//'size'=>'');
//$mod_array[] = recur_dir($new_path);
$file_name = end ( $level );
$file_size = round ( filesize ( $new_path ) / 1024 , 2 );
//get the contents of the template
2013-06-09 05:05:17 +00:00
$file_contents = file_get_contents ( $_SERVER [ "DOCUMENT_ROOT" ] . PROJECT_PATH . "/resources/templates/provision/" . $device_template . "/" . $file_name );
2012-06-04 14:58:40 +00:00
//prepare the files
//replace the variables in the template in the future loop through all the line numbers to do a replace for each possible line number
2013-11-10 07:26:18 +00:00
$file_contents = str_replace ( "{ \$ mac}" , $device_mac_address , $file_contents );
2013-04-09 22:33:42 +00:00
$file_contents = str_replace ( "{v_label}" , $device_label , $file_contents );
$file_contents = str_replace ( "{v_firmware_version}" , $device_firmware_version , $file_contents );
$file_contents = str_replace ( "{domain_time_zone}" , $device_time_zone , $file_contents );
2012-06-04 14:58:40 +00:00
$file_contents = str_replace ( "{domain_name}" , $_SESSION [ 'domain_name' ], $file_contents );
$file_contents = str_replace ( "{v_server1_address}" , $server1_address , $file_contents );
$file_contents = str_replace ( "{v_proxy1_address}" , $proxy1_address , $file_contents );
2012-08-11 22:52:50 +00:00
//replace the dynamic provision variables that are defined in 'default settings' and 'domain settings';
//example: category=provision, subcategory=sip_transport, name=var, value=tls - used in the template as {v_sip_transport}
foreach ( $_SESSION [ 'provision' ] as $key => $value ) {
2013-11-10 07:26:18 +00:00
$file_contents = str_replace ( '{$' . $key . '}' , $value [ 'var' ], $file_contents );
2012-06-04 14:58:40 +00:00
}
2012-07-05 18:56:26 +00:00
//create a mac address with back slashes for backwards compatability
2013-04-09 22:33:42 +00:00
$mac_dash = substr ( $device_mac_address , 0 , 2 ) . '-' . substr ( $device_mac_address , 2 , 2 ) . '-' . substr ( $device_mac_address , 4 , 2 ) . '-' . substr ( $device_mac_address , 6 , 2 ) . '-' . substr ( $device_mac_address , 8 , 2 ) . '-' . substr ( $device_mac_address , 10 , 2 );
2012-07-05 18:56:26 +00:00
2012-06-04 14:58:40 +00:00
//lookup the provisioning information for this MAC address.
2013-04-28 07:24:41 +00:00
$sql = "SELECT e.extension, e.password, e.effective_caller_id_name, d.device_extension_uuid, d.extension_uuid, d.device_line " ;
$sql .= "FROM v_device_extensions as d, v_extensions as e " ;
$sql .= "WHERE e.extension_uuid = d.extension_uuid " ;
$sql .= "AND d.device_uuid = '" . $device_uuid . "' " ;
$sql .= "AND d.domain_uuid = '" . $_SESSION [ 'domain_uuid' ] . "' " ;
$sql .= "and e.enabled = 'true' " ;
2013-08-28 05:05:07 +00:00
$sub_prep_statement = $db -> prepare ( check_sql ( $sql ));
$sub_prep_statement -> execute ();
$sub_result = $sub_prep_statement -> fetchAll ( PDO :: FETCH_NAMED );
2013-05-27 22:35:35 +00:00
foreach ( $sub_result as $field ) {
$line_number = $field [ 'device_line' ];
2013-04-28 07:24:41 +00:00
$file_contents = str_replace ( "{v_line" . $line_number . "_server_address}" , $_SESSION [ 'domain_name' ], $file_contents );
2013-05-27 22:35:35 +00:00
$file_contents = str_replace ( "{v_line" . $line_number . "_displayname}" , $field [ "effective_caller_id_name" ], $file_contents );
$file_contents = str_replace ( "{v_line" . $line_number . "_shortname}" , $field [ "extension" ], $file_contents );
$file_contents = str_replace ( "{v_line" . $line_number . "_user_id}" , $field [ "extension" ], $file_contents );
$file_contents = str_replace ( "{v_line" . $line_number . "_user_password}" , $field [ "password" ], $file_contents );
2012-06-04 14:58:40 +00:00
}
2013-08-28 05:05:07 +00:00
unset ( $sub_prep_statement );
2012-06-04 14:58:40 +00:00
2013-06-09 21:39:18 +00:00
//get the provisioning information from device lines table
$sql = "SELECT * FROM v_device_lines " ;
$sql .= "WHERE device_uuid = '" . $device_uuid . "' " ;
$sql .= "AND domain_uuid = '" . $_SESSION [ 'domain_uuid' ] . "' " ;
2013-08-28 05:05:07 +00:00
$sub_prep_statement = $db -> prepare ( check_sql ( $sql ));
$sub_prep_statement -> execute ();
$sub_result = $sub_prep_statement -> fetchAll ( PDO :: FETCH_NAMED );
foreach ( $sub_result as $field ) {
$line_number = $field [ 'line_number' ];
$file_contents = str_replace ( "{v_line" . $line_number . "_server_address}" , $field [ "server_address" ], $file_contents );
$file_contents = str_replace ( "{v_line" . $line_number . "_outbound_proxy}" , $field [ "outbound_proxy" ], $file_contents );
$file_contents = str_replace ( "{v_line" . $line_number . "_displayname}" , $field [ "display_name" ], $file_contents );
$file_contents = str_replace ( "{v_line" . $line_number . "_user_id}" , $field [ "user_id" ], $file_contents );
$file_contents = str_replace ( "{v_line" . $line_number . "_auth_id}" , $field [ "auth_id" ], $file_contents );
$file_contents = str_replace ( "{v_line" . $line_number . "_user_password}" , $field [ "password" ], $file_contents );
2013-06-09 21:39:18 +00:00
}
2013-08-28 05:05:07 +00:00
unset ( $sub_prep_statement );
2013-06-09 21:39:18 +00:00
2012-06-04 14:58:40 +00:00
//cleanup any remaining variables
for ( $i = 1 ; $i <= 100 ; $i ++ ) {
$file_contents = str_replace ( "{v_line" . $i . "_server_address}" , "" , $file_contents );
2013-08-28 05:05:07 +00:00
$file_contents = str_replace ( "{v_line" . $i . "_outbound_proxy}" , "" , $file_contents );
2012-06-04 14:58:40 +00:00
$file_contents = str_replace ( "{v_line" . $i . "_displayname}" , "" , $file_contents );
$file_contents = str_replace ( "{v_line" . $i . "_shortname}" , "" , $file_contents );
$file_contents = str_replace ( "{v_line" . $i . "_user_id}" , "" , $file_contents );
2013-08-28 05:05:07 +00:00
$file_contents = str_replace ( "{v_line" . $i . "_auth_id}" , "" , $file_contents );
2012-06-04 14:58:40 +00:00
$file_contents = str_replace ( "{v_line" . $i . "_user_password}" , "" , $file_contents );
}
2013-11-10 07:26:18 +00:00
//replace {$mac} in the file name
2013-07-29 17:22:30 +00:00
if ( $device_vendor == "aastra" || $device_vendor == "cisco" ) {
2012-06-04 14:58:40 +00:00
//upper case the mac address for aastra phones
2013-11-10 07:26:18 +00:00
$file_name = str_replace ( "{ \$ mac}" , strtoupper ( $device_mac_address ), $file_name );
2012-06-04 14:58:40 +00:00
}
else {
//all other phones
2013-11-10 07:26:18 +00:00
$file_name = str_replace ( "{ \$ mac}" , $device_mac_address , $file_name );
2012-06-04 14:58:40 +00:00
}
//write the configuration to the directory
if ( strlen ( $_SESSION [ 'switch' ][ 'provision' ][ 'dir' ]) > 0 ) {
$dir_array = explode ( ";" , $_SESSION [ 'switch' ][ 'provision' ][ 'dir' ]);
foreach ( $dir_array as $directory ) {
2013-08-28 05:05:07 +00:00
//echo $directory.'/'.$file_name."<br />\n";
2012-06-04 14:58:40 +00:00
$fh = fopen ( $directory . '/' . $file_name , "w" ) or die ( "Unable to write to $directory for provisioning. Make sure the path exists and permissons are set correctly." );
fwrite ( $fh , $file_contents );
fclose ( $fh );
}
unset ( $file_name );
}
}
} //end for each
closedir ( $dir_list );
}
unset ( $prep_statement );
2013-07-29 17:22:30 +00:00
?>