root/freepbx/trunk/amp_conf/htdocs/admin/page.modules.php

Revision 1435, 12.1 kB (checked in by qldrob, 7 years ago)

It's not QUITE so funny if you have to go and fix it later. But still funny.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php /* $Id$ */
2
3 $extdisplay = isset($_REQUEST['extdisplay'])?$_REQUEST['extdisplay']:'';
4
5 if (isset($_POST['submit'])) { // if form has been submitted
6     switch ($_POST['modaction']) {
7         case "install":
8             if (runModuleSQL($_POST['modname'],$_POST['modaction']))
9                 installModule($_POST['modname'],$_POST['modversion']);
10             else
11                 echo "<div class=\"error\">"._("Module install script failed to run")."</div>";
12         break;
13         case "uninstall":
14             if (runModuleSQL($_POST['modname'],$_POST['modaction']))
15                 uninstallModule($_POST['modname']);
16             else
17                 echo "<div class=\"error\">"._("Module uninstall script failed to run")."</div>";
18         break;
19         case "enable":
20             enableModule($_POST['modname']);
21             echo "<script language=\"Javascript\">document.location='".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."&foo=1'</script>";
22         break;
23         case "disable":
24             disableModule($_POST['modname']);
25             echo "<script language=\"Javascript\">document.location='".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."&foo=2'</script>";
26         break;
27         case "delete":
28             deleteModule($_POST['modname']);
29             //echo "<script language=\"Javascript\">document.location='".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."'</script>";
30         break;
31         case "download":
32             fetchModule($_POST['location']);
33             //echo "<script language=\"Javascript\">document.location='".$_SERVER['PHP_SELF']."?".$_SERVER['QUERY_STRING']."'</script>";
34         break;
35     }
36 }
37 ?>
38
39 </div>
40 <div class="rnav">
41     <li><a id="<?php echo ($extdisplay=='' ? 'current':'') ?>" href="config.php?display=modules&type=tool&extdisplay="><?php echo _("Local Modules") ?></a></li>
42     <li><a id="<?php echo ($extdisplay=='online' ? 'current':'') ?>" href="config.php?display=modules&type=tool&extdisplay=online"><?php echo _("Online Modules") ?></a></li>
43 </div>
44 <div class="content">
45
46 <?php
47 switch($extdisplay) {
48     case "online": ?>
49         <h2><?php echo _("Online Modules")?></h2>
50         <table border="1" >
51 <tr>
52     <th><?php echo _("Module")?></th><th><?php echo _("Category")?></th><th><?php echo _("Version")?></th><th><?php echo _("Author")?></th><th><?php echo _("Status")?></th><th><?php echo _("Action")?></th>
53 </tr>
54 <?php
55         // determine which modules we have installed already
56         $installed = find_allmodules();
57         // determine what modules are available
58         $modules = getModuleXml();
59         //echo "<pre>"; print_r($modules); echo "</pre>";
60         // display the modules
61         displayModules($modules,$installed);
62     break;
63     default: ?>
64         <h2><?php echo _("Local Module Administration")?></h2>
65         <table border="1" >
66 <tr>
67     <th><?php echo _("Module")?></th><th><?php echo _("Category")?></th><th><?php echo _("Version")?></th><th><?php echo _("Type")?></th><th><?php echo _("Status")?></th><th><?php echo _("Action")?></th>
68 </tr>
69 <?php
70         $allmods = find_allmodules();
71         //echo "<pre>"; print_r($allmods); echo "</pre>";
72         foreach($allmods as $key => $mod) {
73             // sort the list in category / displayName order
74             // this is the only way i know how to do this...surely there is another way?
75             
76             // fields for sort
77             $displayName = isset($mod['displayName']) ? $mod['displayName'] : 'unknown';
78             $category = isset($mod['category']) ? $mod['category'] : 'unknown';   
79             // we want to sort on this so make it first in the new array
80             $newallmods[$key]['asort'] = $category.$displayName;
81         
82             // copy the rest of the array
83             $newallmods[$key]['displayName'] = $displayName;
84             $newallmods[$key]['category'] = $category;
85             $newallmods[$key]['version'] = isset($mod['version']) ? $mod['version'] : 'unknown';
86             $newallmods[$key]['type'] = isset($mod['type']) ? $mod['type'] : 'unknown';
87             $newallmods[$key]['status'] = isset($mod['status']) ? $mod['status'] : 0;
88             
89             asort($newallmods);   
90         }
91         foreach($newallmods as $key => $mod) {
92             
93             //dynamicatlly create a form based on status
94             if ($mod['status'] == 0) {
95                 $status = _("Not Installed");
96                 //install form
97                 $action = "<form method=\"POST\" action=\"{$_SERVER['REQUEST_URI']}\" style=display:inline>";
98                 $action .= "<input type=\"hidden\" name=\"modname\" value=\"{$key}\">";
99                 $action .= "<input type=\"hidden\" name=\"modversion\" value=\"{$mod['version']}\">";
100                 $action .= "<input type=\"hidden\" name=\"modaction\" value=\"install\">";
101                 $action .= "<input type=\"submit\" name=\"submit\" value=\""._("Install")."\">";
102                 $action .= "</form>";
103             } else if($mod['status'] == 1){
104                 $status = _("Disabled");
105                 //enable form
106                 $action = "<form method=\"POST\" action=\"{$_SERVER['REQUEST_URI']}\" style=display:inline>";
107                 $action .= "<input type=\"hidden\" name=\"modname\" value=\"{$key}\">";
108                 $action .= "<input type=\"hidden\" name=\"modaction\" value=\"enable\">";
109                 $action .= "<input type=\"submit\" name=\"submit\" value=\""._("Enable")."\">";
110                 $action .= "</form>";
111                 //uninstall form
112                 $action .= "<form method=\"POST\" action=\"{$_SERVER['REQUEST_URI']}\" style=display:inline>";
113                 $action .= "<input type=\"hidden\" name=\"modname\" value=\"{$key}\">";
114                 $action .= "<input type=\"hidden\" name=\"modaction\" value=\"uninstall\">";
115                 $action .= "<input type=\"submit\" name=\"submit\" value=\""._("Uninstall")."\">";
116                 $action .= "</form>";
117                 
118             } else if($mod['status'] == 2){
119                 $status = _("Enabled");
120                 //disable form
121                 $action = "<form method=\"POST\" action=\"{$_SERVER['REQUEST_URI']}\" style=display:inline>";
122                 $action .= "<input type=\"hidden\" name=\"modname\" value=\"{$key}\">";
123                 $action .= "<input type=\"hidden\" name=\"modaction\" value=\"disable\">";
124                 $action .= "<input type=\"submit\" name=\"submit\" value=\""._("Disable")."\">";
125                 $action .= "</form>";
126             } else if($mod['status'] == -1){
127                 $status = _("Broken");
128                 //disable form
129                 $action = "<form method=\"POST\" action=\"{$_SERVER['REQUEST_URI']}\" style=display:inline>";
130                 $action .= "<input type=\"hidden\" name=\"modname\" value=\"{$key}\">";
131                 $action .= "<input type=\"hidden\" name=\"modaction\" value=\"delete\">";
132                 $action .= "<input type=\"submit\" name=\"submit\" value=\""._("Delete")."\">";
133                 $action .= "</form>";
134             }
135             
136             echo "<tr>";
137             echo "<td>";
138             echo _($mod['displayName']);
139             echo "</td>";
140             echo "<td>";
141             echo $mod['category'];
142             echo "</td>";
143             echo "<td>";
144             echo $mod['version'];
145             echo "</td>";
146             echo "<td>";
147             echo _($mod['type']);
148             echo "</td>";
149             echo "<td>";
150             echo $status;
151             echo "</td>";
152             echo "<td>";
153             echo $action;
154             echo "</td>";
155             echo "</tr>";
156         }
157     break;
158 }
159 ?>
160
161 </table>
162
163 <?php
164
165 /* BEGIN FUNCTIONS */
166
167 function displayModules($arr,$installed) {
168     // So, we have an array with several:
169 /*
170     [phpinfo] => Array
171         (
172             [displayName] => PHP Info
173             [version] => 1.0
174             [type] => tool
175             [category] => Basic
176             [author] => Coalescent Systems
177             [email] => info@coalescentsystems.ca
178             [items] => Array
179                 (
180                     [PHPINFO] => PHP Info
181                     [PHPINFO2] => PHP Info2
182                 )
183
184             [requirements] => Array
185                 (
186                     [FILE] => /usr/sbin/asterisk
187                     [MODULE] => core
188                 )
189
190         )
191 */
192     foreach(array_keys($arr) as $arrkey) {
193         // Determine module status
194         if(array_key_exists($arrkey,$installed)) {
195             $status = "Local";
196             $action = "";
197         } else {
198             $status = "Online";
199             $action = "
200             <form action={$_SERVER['PHP_SELF']}?{$_SERVER['QUERY_STRING']} method=post>
201                 <input type=hidden name=modaction value=download>
202                 <input type=hidden name=location value={$arr[$arrkey]['location']}>
203                 <input type=submit name=submit value=Download>
204             </form>
205             ";
206         }
207     
208         // build author string/link
209         if (isset($arr[$arrkey]['email']))
210             $email = "<a href=\"mailto:".$arr[$arrkey]['email']."\">".$arr[$arrkey]['author']."</a>";
211         else
212             $email = $arr[$arrkey]['author'];
213             
214         print <<< End_of_Html
215         
216         <tr>
217             <td>{$arr[$arrkey]['displayName']} ({$arrkey})</td>
218             <td>{$arr[$arrkey]['type']}</td>
219             <td>{$arr[$arrkey]['version']}</td>
220             <td>{$email}</td>
221             <td>{$status}</td>
222             <td>{$action}</td>
223         </tr>
224         
225 End_of_Html;
226     }
227 }
228
229 function getModuleXml() {
230     //this should be in an upgrade file ... putting here for now.
231     sql('CREATE TABLE IF NOT EXISTS module_xml (time INT NOT NULL , data BLOB NOT NULL) TYPE = MYISAM ;');
232     
233     $result = sql('SELECT * FROM module_xml','getRow',DB_FETCHMODE_ASSOC);
234     // if the epoch in the db is more than 10 minutes old, then regrab xml
235     if((time() - $result['time']) > 600) {
236         $fn = "http://svn.sourceforge.net/svnroot/amportal/modules/trunk/modules.xml";
237         //$fn = "/usr/src/freepbx-modules/modules.xml";
238         $data = file_get_contents($fn);
239         // remove the old xml
240         sql('DELETE FROM module_xml');
241         // update the db with the new xml
242         $data4sql = (get_magic_quotes_gpc() ? $data : addslashes($data));
243         sql('INSERT INTO module_xml (time,data) VALUES ('.time().',"'.$data4sql.'")');
244     } else {
245         echo "using cache";
246         $data = $result['data'];
247     }
248     //echo time() - $result['time'];
249     $parser = new xml2ModuleArray($data);
250     $xmlarray = $parser->parseModulesXML($data);
251     //$modules = $xmlarray['XML']['MODULE'];
252     
253     //echo "<hr>Raw XML Data<pre>"; print_r(htmlentities($data)); echo "</pre>";
254     //echo "<hr>XML2ARRAY<pre>"; print_r($xmlarray); echo "</pre>";
255     
256     return $xmlarray;
257 }
258
259 // executes the SQL found in a module install.sql or uninstall.sql
260 function runModuleSQL($moddir,$type){
261     global $db;
262     global $amp_conf;
263     $data='';
264     $retval = false;
265     // if there is an sql file, run it
266     if (is_file("modules/{$moddir}/{$type}.sql")) {
267         // run sql script
268         $fd = fopen("modules/{$moddir}/{$type}.sql","r");
269         while (!feof($fd)) {
270             $data .= fread($fd, 1024);
271         }
272         fclose($fd);
273
274         preg_match_all("/((SELECT|INSERT|UPDATE|DELETE|CREATE|DROP).*);\s*\n/Us", $data, $matches);
275         
276         foreach ($matches[1] as $sql) {
277                 $result = $db->query($sql);
278                 if(DB::IsError($result)) {     
279                     return false;
280                 }
281         }
282         $retval = true;
283     }
284     // if there is a php file, run it
285     print "Looking at modules/{$moddir}/{$type}.php\n";
286     if (is_file("modules/{$moddir}/{$type}.php")) {
287         include("modules/{$moddir}/{$type}.php");
288         $retval = true;
289     }
290     return $retval;
291 }
292
293 function installModule($modname,$modversion)
294 {
295     global $db;
296     global $amp_conf;
297     
298     switch ($amp_conf["AMPDBENGINE"])
299     {
300         case "sqlite":
301             // to support sqlite2, we are not using autoincrement. we need to find the
302             // max ID available, and then insert it
303             $sql = "SELECT max(id) FROM modules;";
304             $results = $db->getRow($sql);
305             $new_id = $results[0];
306             $new_id ++;
307             $sql = "INSERT INTO modules (id,modulename, version,enabled) values ('{$new_id}','{$modname}','{$modversion}','0' );";
308             break;
309         
310         default:
311             $sql = "INSERT INTO modules (modulename, version) values ('{$modname}','{$modversion}');";
312         break;
313     }
314
315     $results = $db->query($sql);
316     if(DB::IsError($results)) {
317         die($results->getMessage());
318     }
319 }
320
321 function uninstallModule($modname) {
322     global $db;
323     $sql = "DELETE FROM modules WHERE modulename = '{$modname}'";
324     $results = $db->query($sql);
325     if(DB::IsError($results)) {
326         die($results->getMessage());
327     }
328 }
329
330 function enableModule($modname) {
331     global $db;
332     $sql = "UPDATE modules SET enabled = 1 WHERE modulename = '{$modname}'";
333     $results = $db->query($sql);
334     if(DB::IsError($results)) {
335         die($results->getMessage());
336     }
337 }
338
339 function disableModule($modname) {
340     global $db;
341     $sql = "UPDATE modules SET enabled = 0 WHERE modulename = '{$modname}'";
342     $results = $db->query($sql);
343     if(DB::IsError($results)) {
344         die($results->getMessage());
345     }
346 }
347
348 function deleteModule($modname) {
349     global $db;
350     $sql = "DELETE FROM modules WHERE modulename = '{$modname}' LIMIT 1";
351     $results = $db->query($sql);
352     if(DB::IsError($results)) {
353         die($results->getMessage());
354     }
355 }
356
357 //downloads a module, and extracts it into the module dir
358 function fetchModule($location) {
359     global $amp_conf;
360     $file = basename($location);
361     $url = "https://svn.sourceforge.net/svnroot/amportal/modules/trunk/".$location;
362     //save the file to /tmp
363     $filename = "/tmp/".$file;
364     $fp = @fopen($filename,"w");
365     fwrite($fp,file_get_contents($url));
366     fclose($fp);
367     if(!file_exists($filename)) {
368         echo "<div class=\"error\">"._("Unable to save")." {$filename}</div>";
369         return false;
370     }
371     // unarchive the module to the modules dir
372     system("tar zxf {$filename} --directory={$amp_conf['AMPWEBROOT']}/admin/modules/");
373     unlink($filename);
374     return true;
375 }
376
377 ?>
378
379
Note: See TracBrowser for help on using the browser.