root/contributed_modules/modules/bulkdids/page.bulkdids.php

Revision 8084, 7.3 kB (checked in by p_lindheimer, 3 years ago)

Auto Check-in of any outstanding patches

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 2006 Seth Sargent, Steven Ward
18 //    Portions Copyright 2009 Mikael Carlsson, mickecamino@gmail.com
19 //    Portions Copyright 2009 Schmooze Communications LLC
20 //
21 set_time_limit(3000);
22
23 function fatal($text)  {
24   $clean = str_replace("</script>","",str_replace("<script>javascript:alert('","",$text));
25   return "\t".$clean."\n";
26 }
27
28 // $change is used as a flag whether or not a reload is needed. If no changes
29 // are made, no reload will be prompted.
30 $change = false;
31 $output = "";
32
33 if ($_REQUEST["csv_type"] == "output") {
34   exportdids_all();
35 } elseif ($_REQUEST["csv_type"] == "input") {
36
37     $aFields = array (
38       "action" => array(false, -1),
39       "DID" => array(false, -1),
40       "description" => array(false, -1),
41       "destination" => array(false, -1),
42       "cidnum" => array(false, -1),
43       "alertinfo" => array(false, -1),
44       "grppre" => array(false, -1),
45       "mohclass" => array(false, -1),
46       "ringing" => array(false, -1),
47       "privacyman" => array(false, -1)
48       );
49
50       $fh = fopen($_FILES["csvFile"]["tmp_name"], "r");
51       if ($fh == NULL) {
52         $file_ok = FALSE;
53       } else {
54         $file_ok = TRUE;
55       }
56
57       $k = 0;
58
59       while ($file_ok && (($aInfo = fgetcsv($fh, 2000, ",", "\"")) !== FALSE)) {
60               $k++;
61         if (empty($aInfo[0])) {
62           continue;
63         }
64
65         // If this is the first row then we need to check each field listed (these are the headings)
66         if ($i==0) {
67           for ($j=0; $j<count($aInfo); $j++) {
68             $aKeys = array_keys($aFields);
69             foreach ($aKeys as $sKey) {
70               if ($aInfo[$j] == $sKey) {
71                 $aFields[$sKey][0] = true;
72                 $aFields[$sKey][1] = $j;
73               }
74             }
75           }
76           $i++;
77           $output .= "<BR><BR>Row $k: Headers parsed. <BR>";
78           continue;
79         }
80
81         if ($aFields["action"][0]) {
82           $vars["action"] = trim($aInfo[$aFields["action"][1]]);
83         }
84
85         if ($aFields["DID"][0]) {
86           $vars["extension"]  = trim($aInfo[$aFields["DID"][1]]);
87         }
88
89         if ($aFields["description"][0]) {
90           $vars["description"] = trim($aInfo[$aFields["description"][1]]);
91         }
92
93         if ($aFields["destination"][0]) {
94           $vars["destination"] = trim($aInfo[$aFields["destination"][1]]);
95         }
96
97         if ($aFields["cidnum"][0]) {
98           $vars["cidnum"] = trim($aInfo[$aFields["cidnum"][1]]);
99         }
100
101         if ($aFields["alertinfo"][0]) {
102           $vars["alertinfo"] = trim($aInfo[$aFields["alertinfo"][1]]);
103         }
104
105         if ($aFields["grppre"][0]) {
106           $vars["grppre"] = trim($aInfo[$aFields["grppre"][1]]);
107         }
108
109               if ($aFields["mohclass"][0] && $aInfo[$aFields["mohclass"][1]]) {
110           $vars["mohclass"] = trim($aInfo[$aFields["mohclass"][1]]);
111         }
112         else  {
113           $vars["mohclass"] = "default";
114         }
115         if ($aFields["ringing"][0]) {
116           $vars["ringing"] = trim($aInfo[$aFields["ringing"][1]]);
117         }
118
119         if ($aFields["privacyman"][0]) {
120           $vars["privacyman"] = trim($aInfo[$aFields["privacyman"][1]]);
121         }
122         $vars["faxexten"] = "default";
123         $vars["display"]  = "bulkdids";
124         $vars["type"] = "tool";
125
126         $_REQUEST = $vars;
127
128         if (checkRange($vars["extension"])) {
129
130           switch ($vars["action"]) {
131             case "add":
132         ob_start("fatal");
133         if(!core_did_add($vars,($vars["destination"]?$vars["destination"]:false)))  {
134           $output .= "ERROR: ".$vars["extension"]." ".$vars["description"].". See error above<br>";
135
136         }
137         else  {   
138           $output .= "Row $k: Added: " . $vars["extension"];
139           $output .= "<br />";
140         }
141         ob_end_flush();
142
143         // begin status output for this row
144         $change = true;
145         break;
146       case "edit":
147         if (core_did_get($vars["extension"],$vars["cidnum"])) {
148           core_did_del($vars["extension"],$vars["cidnum"]);
149           $error = ob_start("fatal");
150           if(!core_did_add($vars,($vars["destination"]?$vars["destination"]:false)))  {
151             $output .= "ERROR: ".$vars["extension"]." ".$vars["description"].". See error above<br>";
152
153           }
154           else  {
155             $output .= "Row $k: Edited: " . $vars["extension"] . "<BR>";
156           }
157           ob_end_flush();
158           $change = true;
159         }
160         break;
161       case "del":
162         if (core_did_get($vars["extension"],$vars["cidnum"])) {
163           core_did_del($vars["extension"],$vars["cidnum"]);
164           $change = true;
165         }
166         $output .= "Row $k: Deleted: " . $vars["extension"] . "<BR>";
167         break;
168       default:
169         $output .= "Row $k: Unrecognized action: the only actions recognized are add, edit, del.\n";
170         break;
171           }
172  
173           if ($change) {
174         needreload();
175           }
176         } else {
177           $output .= "Row $k: Access denied to did " . $vars["extension"] . ".  No action performed.<BR>";
178         }
179       } // while loop
180        print $output;
181
182 } else
183 {
184   $table_output = "";
185   $table_rows = bulkdids_generate_table_rows();
186   if ($table_rows === NULL) {
187     $table_output = "Table unavailable";
188   } else {
189     $table_output .=  "<table cellspacing='0' cellpadding='4' rules='rows'>";
190     $table_output .=  "<tr valign='top'>
191             <th align='left' valign='top'>#</th>
192             <th align='left' valign='top'>Field</th>
193             <th align='left' valign='top'>Default</th>
194             <th align='left' valign='top'>Allowed</th>
195             <th align='left' valign='top'>Field Details</th>
196             <th align='left' valign='top'>Description</th>
197           </tr>";
198     $i = 1;
199     foreach ($table_rows as $row) {
200       $table_output .= "<tr>";
201       $table_output .= "<td valign='top'>" . $i . "</td>";
202       $i++;
203       foreach ($row as $col) {
204         $table_output .= "<td valign='top'>" . $col . "</td>";
205       }
206       $table_output .= "</tr>";
207     }
208     $table_output .= "</table>";
209   }
210
211 ?>
212 <h1>Bulk DIDs</h1>
213
214 <h2>Manage DIDs in bulk using CSV files.</h2>
215
216 <p>
217 Start by downloading the
218 <a href="modules/bulkdids/template.csv">Template CSV file</a>
219 (right-click > save as) or clicking the Export DIDs button.
220 </p>
221 <p>
222 Modify the CSV file to add, edit, or delete DIDs as desired. Then load
223 the CSV file. After the CSV file is processed, the action taken for each row
224 will be displayed.
225 </p>
226 <p>
227
228 <form action="<?php $_SERVER["PHP_SELF"] ?>" name="uploadcsv" method="post" enctype="multipart/form-data">
229 <input id="csv_type" name="csv_type" type="hidden" value="none" />
230 <input type="submit" onclick="document.getElementById('csv_type').value='output';" value="Export DIDs" />
231 &nbsp;&nbsp;CSV File to Load: <input name="csvFile" type="file" />
232 <input type="submit" onclick="document.getElementById('csv_type').value='input';"  value="Load File" />
233 <hr />
234 </form>
235 <hr />
236 <h3>Bulk DIDs CSV File Columns</h3>
237 <p>
238 The table below explains each column in the CSV file. You can change the column
239 order of the CSV file as you like, however, the column names must be preserved.
240 </p>
241 <?php
242   print $table_output;
243 }
244 ?>
Note: See TracBrowser for help on using the browser.