root/modules/branches/2.10/endpointman/provisioning/p.php

Revision 12938, 13.5 kB (checked in by tm1000, 2 years ago)

Merge branch 'develop' into freepbx210_local

  • Property svn:executable set to *
Line 
1 <?PHP
2 $bootstrap_settings['freepbx_auth'] = false;
3 if (!@include_once(getenv('FREEPBX_CONF') ? getenv('FREEPBX_CONF') : '/etc/freepbx.conf')) {
4     include_once('/etc/asterisk/freepbx.conf');
5 }
6
7 define('PROVISIONER_BASE', $amp_conf['AMPWEBROOT'].'/admin/modules/_ep_phone_modules/');
8 require($amp_conf['AMPWEBROOT'].'/admin/modules/endpointman/includes/functions.inc');
9 require($amp_conf['AMPWEBROOT'].'/admin/modules/endpointman/includes/timezone.inc');
10
11 $endpoint = new endpointmanager();
12
13 //Check if it's allowed in FreePBX through Endpoint Manager first
14 if ((!isset($endpoint->global_cfg['server_type'])) OR ($endpoint->global_cfg['server_type'] != 'http')) {
15     header('HTTP/1.1 403 Forbidden');
16     die();
17 }
18
19 $provis_ip = $endpoint->global_cfg['srvip'];
20
21 if((isset($_REQUEST['putfile'])) && ((getMethod() == 'PUT') OR (getMethod() == 'POST'))) {
22     //write log files or other files to drive. not sussed out yet completely.
23     header('HTTP/1.1 200 OK');
24     die();
25 }
26
27 if(getMethod() == "GET") {
28     # Workaround for SPAs that don't actually request their type of device
29     # Assume they're 504G's. Faulty in firmware 7.4.3a
30     $filename = basename($_SERVER["REQUEST_URI"]);
31     $web_path = 'http://'.$_SERVER["SERVER_NAME"].dirname($_SERVER["PHP_SELF"]).'/';
32     /*
33     if ($filename == "p.php") {
34             $filename = "spa502G.cfg";
35             $_SERVER['REQUEST_URI']=$_SERVER['REQUEST_URI']."/spa502G.cfg";
36             $web_path = $web_path."p.php/";
37     }
38      *
39      */
40
41     # Firmware Linksys/SPA504G-7.4.3a is broken and MUST be upgraded.
42     if (preg_match('/7.4.3a/', $_SERVER['HTTP_USER_AGENT'])) {
43             $str = '<flat-profile><Upgrade_Enable group="Provisioning/Firmware_Upgrade">Yes</Upgrade_Enable>';
44             $str .= '<Upgrade_Rule group="Provisioning/Firmware_Upgrade">http://'.$provis_ip.'/current.bin</Upgrade_Rule></flat-profile>';
45             echo $str;
46             exit;
47     }
48
49     $filename = str_replace('p.php/','', $filename);
50     $strip = str_replace('spa', '', $filename);
51     
52     if(preg_match('/[0-9A-Fa-f]{12}/i', $strip, $matches) && !(preg_match('/[0]{10}[0-9]{2}/i',$strip))) {
53         $mac_address = $matches[0];
54
55         $sql = 'SELECT id FROM `endpointman_mac_list` WHERE `mac` LIKE CONVERT(_utf8 \'%' . $mac_address . '%\' USING latin1) COLLATE latin1_swedish_ci';
56
57         $mac_id = $endpoint->db->getOne($sql);
58         $phone_info = $endpoint->get_phone_info($mac_id);
59
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 {
249             header("HTTP/1.0 500 Internal Server Error");
250             die();
251         }
252     } else {
253         require_once (PROVISIONER_BASE.'endpoint/base.php');
254         $data = Provisioner_Globals::dynamic_global_files($filename,$endpoint->global_cfg['config_location'],$web_path);
255         if($data !== FALSE) {
256             echo $data;
257         } else {
258             header("HTTP/1.0 404 Not Found");
259         }
260     }
261 } else {
262     header('HTTP/1.1 403 Forbidden');
263     die();
264 }
265
266
267 function getMethod() {
268         $method = $_SERVER['REQUEST_METHOD'];
269         $override = isset($_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE']) ? $_SERVER['HTTP_X_HTTP_METHOD_OVERRIDE'] : (isset($_GET['method']) ? $_GET['method'] : '');
270         if ($method == 'POST' && strtoupper($override) == 'PUT') {
271                 $method = 'PUT';
272         } elseif ($method == 'POST' && strtoupper($override) == 'DELETE') {
273                 $method = 'DELETE';
274         }
275         return $method;
276 }
Note: See TracBrowser for help on using the browser.