root/contributed_modules/modules/bulkextensions/functions.inc.php

Revision 10599, 17.1 kB (checked in by mickecarlsson, 2 years ago)

Re #4649 updated bulkextensions for FreePBX 2.9

Line 
1 <?php
2 //This file is part of FreePBX.
3 //
4 //    FreePBX is free software: you can redistribute it and/or modify
5 //    it under the terms of the GNU General Public License as published by
6 //    the Free Software Foundation, either version 2 of the License, or
7 //    (at your option) any later version.
8 //
9 //    FreePBX is distributed in the hope that it will be useful,
10 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
11 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 //    GNU General Public License for more details.
13 //
14 //    You should have received a copy of the GNU General Public License
15 //    along with FreePBX.  If not, see <http://www.gnu.org/licenses/>.
16 //
17 //    Copyright 2008 sasargen
18 //    Portions Copyright 2009, 2010 Mikael Carlsson, mickecamino@gmail.com
19 //
20
21 /* functions.inc.php - functions for BulkExtensions module. */
22 if (file_exists("modules/voicemail/functions.inc.php")) {
23     include_once("modules/voicemail/functions.inc.php");  // for using voicemail module functions to retrieve voicemail settings
24     };
25 if (file_exists("modules/dictate/functions.inc.php")) {
26     include_once("modules/dictate/functions.inc.php");    // for using dictation services functions to retrieve dictation settings
27     };
28 if (file_exists("modules/languages/functions.inc.php")) {
29     include_once("modules/languages/functions.inc.php");  // for using languages functions to retrieve language setting
30     };
31 if (file_exists("modules/findmefollow/functions.inc.php")) {
32     include_once("modules/findmefollow/functions.inc.php"); // for using findmefollow functions to retreive follow me settings
33     };
34 if (file_exists("modules/fax/functions.inc.php")) {
35     include_once("modules/fax/functions.inc.php");             // for using fax functions to retreive fax settings
36     };
37
38 /* Verify existence of voicemail, dictate, languages and findmefollow functions. */
39 if (function_exists("voicemail_mailbox_get") && function_exists("voicemail_mailbox_add") && function_exists("voicemail_mailbox_del") && function_exists("voicemail_mailbox_remove") && class_exists("vmxObject")) {
40   $vm_exists  = TRUE;
41 } else {
42   $vm_exists  = FALSE;
43 }
44 if (function_exists("dictate_get") && function_exists("dictate_update") && function_exists("dictate_del")) {
45   $dict_exists  = TRUE;
46 } else {
47   $dict_exists  = FALSE;
48 }
49 if (function_exists("languages_user_get") && function_exists("languages_user_update") && function_exists("languages_user_del")) {
50   $lang_exists  = TRUE;
51 } else {
52   $lang_exists  = FALSE;
53 }
54 if (function_exists("findmefollow_get") && function_exists("findmefollow_add") && function_exists("findmefollow_del")) {
55   $findme_exists  = TRUE;
56 } else {
57   $findme_exists  = FALSE;
58 }
59 if (function_exists("fax_get_user") && function_exists("fax_save_user") && function_exists("fax_delete_user")) {
60        $fax_exists     = TRUE;
61 } else {
62        $fax_exists     = FALSE;
63 }
64
65 function exportextensions_allusers() {
66   global $db;
67   global $vm_exists;
68   global $dict_exists;
69   global $lang_exists;
70   global $findme_exists;
71   global $fax_exists;
72   $action   = "edit";
73   $fname    = "bulkext__" .  (string) time() . $_SERVER["SERVER_NAME"] . ".csv";
74   $csv_header   = "action,extension,name,cid_masquerade,sipname,outboundcid,ringtimer,callwaiting,call_screen,pinless,password,noanswer_dest,noanswer_cid,busy_dest,busy_cid,chanunavail_dest,chanunavail_cid,emergency_cid,tech,hardware,devinfo_channel,devinfo_secret,devinfo_notransfer,devinfo_dtmfmode,devinfo_canreinvite,devinfo_context,devinfo_immediate,devinfo_signalling,devinfo_echocancel,devinfo_echocancelwhenbrdiged,devinfo_echotraining,devinfo_busydetect,devinfo_busycount,devinfo_callprogress,devinfo_host,devinfo_type,devinfo_nat,devinfo_port,devinfo_qualify,devinfo_callgroup,devinfo_pickupgroup,devinfo_disallow,devinfo_allow,devinfo_dial,devinfo_accountcode,devinfo_mailbox,devinfo_deny,devinfo_permit,devicetype,deviceid,deviceuser,description,dictenabled,dictformat,dictemail,langcode,record_in,record_out,vm,vmpwd,email,pager,attach,saycid,envelope,delete,options,vmcontext,vmx_state,vmx_unavail_enabled,vmx_busy_enabled,vmx_play_instructions,vmx_option_0_sytem_default,vmx_option_0_number,vmx_option_1_system_default,vmx_option_1_number,vmx_option_2_number,account,ddial,pre_ring,strategy,grptime,grplist,annmsg_id,ringing,grppre,dring,needsconf,remotealert_id,toolate_id,postdest,faxenabled,faxemail\n";
75   $data     = $csv_header;
76   $exts     = get_all_exts();
77
78   foreach ($exts as $ext) {
79     $e  = $ext[0];
80     $u_info = core_users_get($e);
81     $d_info = core_devices_get($e);
82     if ($vm_exists) {
83       $v_info = voicemail_mailbox_get($e);
84     } else {
85       $v_info = NULL;
86     }
87     /* To properly obtain voicemail information, detect enabled/disabled vm value.   */
88     /* Parse extra voicemail options.            */
89     if ($v_info == NULL) {
90       $v_enabled  = "disabled";
91     } else {
92       $v_enabled  = "enabled";
93       $v_options  = isset($v_info["options"])?$v_info["options"]:"";
94       $vm_other_opts  = "";
95       $i    = 0;
96       $first    = TRUE;
97       $c    = count($v_options);
98       reset($v_options);
99       while ($i < $c) {
100         if ((key($v_options) != "attach") && (key($v_options) != "saycid") && (key($v_options) != "envelope") && (key($v_options) != "delete")) {
101           if ($first) {
102             $vm_other_opts  = key($v_options) . "=" . $v_options[key($v_options)];
103             $first    = false;
104           } else {
105             $vm_other_opts .=  "|" . key($v_options) . "=" . $v_options[key($v_options)];
106           }
107         }
108         $i++;
109         next($v_options);
110       }
111     }
112     /* Obtain vmx settings. */
113     if ($vm_exists) {
114       $vmxobj   = new vmxObject($e);
115     } else {
116       $vmxobj   = NULL;
117     }
118    
119     if (is_object($vmxobj)) {
120       $vmx_state    = ($vmxobj->isEnabled())?"checked":"";
121       $vmx_unavail_enabled  = ($vmxobj->getState("unavail")=="enabled")?"checked":"";
122       $vmx_busy_enabled   = ($vmxobj->getState("busy")=="enabled")?"checked":"";
123       $vmx_play_instructions  = ($vmxobj->getVmPlay())?"checked":"";
124       $vmx_option_0_number  = $vmxobj->getMenuOpt(0);
125       if ($vmx_option_0_number == "") {
126         $vmx_option_0_system_default = "checked";
127       } else {
128         $vmx_option_0_system_default = "";
129       }
130       if (is_object($vmxobj)) {
131         if ($vmxobj->hasFollowMe() && $vmxobj->isFollowMe()) {
132           $vmx_option_1_system_default  = "checked";
133           $vmx_option_1_number    = "";
134         } else {
135           $vmx_option_1_system_default  = "";
136           $vmx_option_1_number    = $vmxobj->getMenuOpt(1);
137         }
138         $vmx_option_2_number      = $vmxobj->getMenuOpt(2);
139       }
140     }
141      
142     /* Obtain dictation services settings. */
143     if ($dict_exists) {
144       $dictate_settings = dictate_get($e);
145     }
146
147     /* Obtain language code. */
148     if ($lang_exists) {
149       $langcode = languages_user_get($e);
150     }
151
152     /* Obtain follow me settings. */
153     if ($findme_exists) {
154       $followme_settings = findmefollow_get($u_info["extension"], TRUE);
155     }
156     if (isset($followme_settings)) {
157       $account  = isset($followme_settings["grpnum"])?$followme_settings["grpnum"]:"";
158       $strategy = isset($followme_settings["strategy"])?$followme_settings["strategy"]:"";
159       $grptime  = isset($followme_settings["grptime"])?$followme_settings["grptime"]:"";
160       $grppre   = isset($followme_settings["grppre"])?$followme_settings["grppre"]:"";
161       $grplist  = isset($followme_settings["grplist"])?$followme_settings["grplist"]:"";
162       $annmsg_id  = isset($followme_settings["annmsg_id"])?$followme_settings["annmsg_id"]:"";
163       $postdest = isset($followme_settings["postdest"])?$followme_settings["postdest"]:"";
164       $dring    = isset($followme_settings["dring"])?$followme_settings["dring"]:"";
165       $needsconf  = isset($followme_settings["needsconf"])?$followme_settings["needsconf"]:"";
166       $remotealert_id = isset($followme_settings["remotealert_id"])?$followme_settings["remotealert_id"]:"";
167       $toolate_id   = isset($followme_settings["toolate_id"])?$followme_settings["toolate_id"]:"";
168       $ringing  = isset($followme_settings["ringing"])?$followme_settings["ringing"]:"";
169       $pre_ring   = isset($followme_settings["pre_ring"])?$followme_settings["pre_ring"]:"";
170       $ddial    = isset($followme_settings["ddial"])?$followme_settings["ddial"]:"";
171     }
172
173     /* Obtain fax settings */
174     if ($fax_exists) {
175         $fax_settings = fax_get_user($e);
176         }
177     if (isset($fax_settings)) {
178     $faxenabled     = isset($fax_settings["faxenabled"])?$fax_settings["faxenabled"]:"";
179     $faxemail       = isset($fax_settings["faxemail"])?$fax_settings["faxemail"]:"";
180     }
181     $csv_line[0]  = $action;
182     $csv_line[1]  = isset($u_info["extension"])?$u_info["extension"]:"";
183     $csv_line[2]  = isset($u_info["name"])?$u_info["name"]:"";
184     $csv_line[3]  = isset($u_info["cid_masquerade"])?$u_info["cid_masquerade"]:"";
185     $csv_line[4]  = isset($u_info["sipname"])?$u_info["sipname"]:"";
186     $csv_line[5]  = isset($u_info["outboundcid"])?$u_info["outboundcid"]:"";
187     $csv_line[6]  = isset($u_info["ringtimer"])?$u_info["ringtimer"]:"";
188     $csv_line[7]  = isset($u_info["callwaiting"])?$u_info["callwaiting"]:"";
189     $csv_line[8]  = isset($u_info["call_screen"])?$u_info["call_screen"]:"0";
190     $csv_line[9]  = isset($u_info["pinless"])?$u_info["pinless"]:"";
191     $csv_line[10] = isset($u_info["password"])?$u_info["password"]:"";
192     $csv_line[11]   = isset($u_info["noanswer_dest"])?$u_info["noanswer_dest"]:"";
193     $csv_line[12]   = isset($u_info["noanswer_cid"])?$u_info["noanswer_cid"]:"";
194     $csv_line[13]   = isset($u_info["busy_dest"])?$u_info["busy_dest"]:"";
195     $csv_line[14]   = isset($u_info["busy_cid"])?$u_info["busy_cid"]:"";
196     $csv_line[15] = isset($u_info["chanunavail_dest"])?$u_info["chanunavail_dest"]:"";   
197     $csv_line[16]  = isset($u_info["chanunavail_cid"])?$u_info["chanunavail_cid"]:"";   
198     $csv_line[17] = isset($d_info["emergency_cid"])?$d_info["emergency_cid"]:"";
199     $csv_line[18] = isset($d_info["tech"])?$d_info["tech"]:"";
200     $csv_line[19] = "";   // hardware
201     $csv_line[20] = isset($d_info["channel"])?$d_info["channel"]:"";
202     $csv_line[21] = isset($d_info["secret"])?$d_info["secret"]:"";
203     $csv_line[22] = isset($d_info["notransfer"])?$d_info["notransfer"]:"";
204     $csv_line[23] = isset($d_info["dtmfmode"])?$d_info["dtmfmode"]:"";
205     $csv_line[24] = isset($d_info["canreinvite"])?$d_info["canreinvite"]:"";
206     $csv_line[25] = isset($d_info["context"])?$d_info["context"]:"";
207     $csv_line[26] = isset($d_info["immediate"])?$d_info["immediate"]:"";
208     $csv_line[27] = isset($d_info["signalling"])?$d_info["signalling"]:"";
209     $csv_line[28] = isset($d_info["echocancel"])?$d_info["echocancel"]:"";
210     $csv_line[29] = isset($d_info["echocancelwhenbridged"])?$d_info["echocancelwhenbridged"]:"";
211     $csv_line[30] = isset($d_info["echotraining"])?$d_info["echotraining"]:"";
212     $csv_line[31] = isset($d_info["busydetect"])?$d_info["busydetect"]:"";
213     $csv_line[32] = isset($d_info["busycount"])?$d_info["busycount"]:"";
214     $csv_line[33] = isset($d_info["callprogress"])?$d_info["callprogress"]:"";
215     $csv_line[34] = isset($d_info["host"])?$d_info["host"]:"";
216     $csv_line[35] = isset($d_info["type"])?$d_info["type"]:"";
217     $csv_line[36] = isset($d_info["nat"])?$d_info["nat"]:"";
218     $csv_line[37] = isset($d_info["port"])?$d_info["port"]:"";
219     $csv_line[38] = isset($d_info["qualify"])?$d_info["qualify"]:"";
220     $csv_line[39] = isset($d_info["callgroup"])?$d_info["callgroup"]:"";
221     $csv_line[40] = isset($d_info["pickupgroup"])?$d_info["pickupgroup"]:"";
222     $csv_line[41] = isset($d_info["disallow"])?$d_info["disallow"]:"";
223     $csv_line[42] = isset($d_info["allow"])?$d_info["allow"]:"";
224     $csv_line[43] = isset($d_info["dial"])?$d_info["dial"]:"";
225     $csv_line[44] = isset($d_info["accountcode"])?$d_info["accountcode"]:"";
226     $csv_line[45] = isset($d_info["mailbox"])?$d_info["mailbox"]:"";
227     $csv_line[46] = isset($d_info["deny"])?$d_info["deny"]:"";
228     $csv_line[47] = isset($d_info["permit"])?$d_info["permit"]:"";
229     $csv_line[48] = isset($d_info["devicetype"])?$d_info["devicetype"]:"fixed";
230     $csv_line[49] = (isset($d_info["deviceid"]) || ($d_info["deviceid"]==""))?$d_info["deviceid"]:(isset($u_info["extension"])?$u_info["extension"]:"");
231     $csv_line[50] = (isset($d_info["deviceuser"]) && ($d_info["deviceuser"] != ""))?$d_info["deviceuser"]:(isset($u_info["extension"])?$u_info["extension"]:"none");
232     $csv_line[51] = isset($d_info["description"])?$d_info["description"]:(isset($u_info["name"])?$u_info["name"]:"");
233
234     $csv_line[52] = isset($dictate_settings["enabled"])?$dictate_settings["enabled"]:"disabled";  // dictenabled
235     $csv_line[53] = isset($dictate_settings["format"])?$dictate_settings["format"]:"ogg";   // dictformat (ogg is default)
236     $csv_line[54] = isset($dictate_settings["email"])?$dictate_settings["email"]:"";    // dictemail
237     $csv_line[55] = isset($langcode)?$langcode:"";
238     $csv_line[56] = isset($d_info["record_in"])?$d_info["record_in"]:"";
239     $csv_line[57] = isset($d_info["record_out"])?$d_info["record_out"]:"";
240     $csv_line[58] = $v_enabled; // vm
241     $csv_line[59] = isset($v_info["pwd"])?$v_info["pwd"]:"";
242     $csv_line[60] = isset($v_info["email"])?$v_info["email"]:"";
243     $csv_line[61] = isset($v_info["pager"])?$v_info["pager"]:"";
244     $csv_line[62] = isset($v_info["options"]["attach"])?("attach=" . $v_info["options"]["attach"]):"attach=no";
245     $csv_line[63] = isset($v_info["options"]["saycid"])?("saycid=" . $v_info["options"]["saycid"]):"saycid=no";
246     $csv_line[64] = isset($v_info["options"]["envelope"])?("envelope=" . $v_info["options"]["envelope"]):"envelope=no";
247     $csv_line[65] = isset($v_info["options"]["delete"])?("delete=". $v_info["options"]["delete"]):"delete=no";
248     $csv_line[66] = isset($vm_other_opts)?$vm_other_opts:""; // additional options
249     $csv_line[67] = isset($v_info["vmcontext"])?$v_info["vmcontext"]:"";
250     $csv_line[68] = isset($vmx_state)?$vmx_state:"";
251     $csv_line[69] = isset($vmx_unavail_enabled)?$vmx_unavail_enabled:"";
252     $csv_line[70] = isset($vmx_busy_enabled)?$vmx_busy_enabled:"";
253     $csv_line[71] = isset($vmx_play_instructions)?$vmx_play_instructions:"";
254     $csv_line[72] = isset($vmx_option_0_system_default)?$vmx_option_0_system_default:"";
255     $csv_line[73] = isset($vmx_option_0_number)?$vmx_option_0_number:"";
256     $csv_line[74] = isset($vmx_option_1_system_default)?$vmx_option_1_system_default:"";
257     $csv_line[75] = isset($vmx_option_1_number)?$vmx_option_1_number:"";
258     $csv_line[76] = isset($vmx_option_2_number)?$vmx_option_2_number:"";
259     $csv_line[77] = isset($account)?$account:"";
260     $csv_line[78] = isset($ddial)?$ddial:"";
261     $csv_line[79] = isset($pre_ring)?$pre_ring:"";
262     $csv_line[80] = isset($strategy)?$strategy:"";
263     $csv_line[81] = isset($grptime)?$grptime:"";
264     $csv_line[82] = isset($grplist)?$grplist:"";
265     $csv_line[83] = isset($annmsg_id)?$annmsg_id:"";
266     $csv_line[84] = isset($ringing)?$ringing:"";
267     $csv_line[85] = isset($grppre)?$grppre:"";
268     $csv_line[86] = isset($dring)?$dring:"";
269     $csv_line[87] = isset($needsconf)?$needsconf:"";
270     $csv_line[88] = isset($remotealert_id)?$remotealert_id:"";
271     $csv_line[89] = isset($toolate_id)?$toolate_id:"";
272     $csv_line[90] = isset($postdest)?$postdest:"";
273     $csv_line[91]   = isset($faxenabled)?$faxenabled:"";
274     $csv_line[92]   = isset($faxemail)?$faxemail:"";
275
276     for ($i = 0; $i < count($csv_line); $i++) {
277       /* If the string contains a comma, enclose it in double-quotes. */
278       if (strpos($csv_line[$i], ",") !== FALSE) {
279         $csv_line[$i] = "\"" . $csv_line[$i] . "\"";
280       }
281       if ($i != count($csv_line) - 1) {
282         $data = $data . $csv_line[$i] . ",";
283       } else {
284         $data = $data . $csv_line[$i];
285       }
286     }
287     $data = $data . "\n";
288     unset($csv_line);
289   }
290   force_download($data, $fname);
291   return;
292 }
293
294 function get_all_exts() {
295   $sql  = "SELECT extension FROM users ORDER BY extension";
296   $extens = sql($sql,"getAll");
297   if (isset($extens)) {
298     return $extens;
299   } else {
300     return null;
301   }
302 }
303
304 function force_download ($data, $name, $mimetype="", $filesize=false) {
305     // File size not set?
306     if ($filesize == false OR !is_numeric($filesize)) {
307         $filesize = strlen($data);
308     }
309     // Mimetype not set?
310     if (empty($mimetype)) {
311         $mimetype = "application/octet-stream";
312     }
313     // Make sure there's not anything else left
314     ob_clean_all();
315     // Start sending headers
316     header("Pragma: public"); // required
317     header("Expires: 0");
318     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
319     header("Cache-Control: private",false); // required for certain browsers
320     header("Content-Transfer-Encoding: binary");
321     header("Content-Type: " . $mimetype);
322     header("Content-Length: " . $filesize);
323     header("Content-Disposition: attachment; filename=\"" . $name . "\";" );
324     // Send data
325     echo $data;
326     die();
327 }
328
329 function ob_clean_all () {
330     $ob_active = ob_get_length () !== false;
331     while($ob_active) {
332         ob_end_clean();
333         $ob_active = ob_get_length () !== false;
334     }
335     return true;
336 }
337
338 function generate_table_rows() {
339   $langcookie =  $_COOKIE['lang'];
340   if (file_exists("modules/bulkextensions/i18n/$langcookie/LC_MESSAGES/table.csv")) {   // check if translated file exists
341     $fh = fopen("modules/bulkextensions/i18n/$langcookie/LC_MESSAGES/table.csv", "r");  // open it
342         } else {                    // nope, no translated file was found, open the default one
343           $fh = fopen("modules/bulkextensions/table.csv", "r");
344         }
345         if ($fh == NULL) {
346                 return NULL;
347   }
348   $k = 0;
349   while (($csv_data = fgetcsv($fh, 1000, ",", "\"")) !== FALSE) {
350     $k++;
351     /* Name,Default,Allowed,On Extensions page,Details */
352     for ($i = 0; $i < 5; $i++) {
353       if (isset($csv_data[$i])) {
354             $table[$k][$i] = $csv_data[$i];
355       } else {
356         $table[$k][$i] = "";
357       }
358     }
359   }
360   fclose($fh);
361   return $table;
362 }
363 ?>
Note: See TracBrowser for help on using the browser.