Switch to new access levels system
[fa-stable.git] / admin / inst_module.php
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 $page_security = 'SA_CREATEMODULES';
13 $path_to_root="..";
14 include_once($path_to_root . "/includes/session.inc");
15
16 page(_("Install/Update Modules"));
17
18 include_once($path_to_root . "/includes/date_functions.inc");
19 include_once($path_to_root . "/admin/db/company_db.inc");
20 include_once($path_to_root . "/admin/db/maintenance_db.inc");
21 include_once($path_to_root . "/modules/installed_modules.php");
22 include_once($path_to_root . "/includes/ui.inc");
23
24 //---------------------------------------------------------------------------------------------
25
26 if (isset($_GET['selected_id']))
27 {
28         $selected_id = $_GET['selected_id'];
29 }
30 elseif (isset($_POST['selected_id']))
31 {
32         $selected_id = $_POST['selected_id'];
33 }
34 else
35         $selected_id = -1;
36
37 //---------------------------------------------------------------------------------------------
38
39 function check_data()
40 {
41         if ($_POST['name'] == "" || $_POST['path'] == "")
42                 return false;
43         return true;
44 }
45
46 /**
47  * @return Returns the array sorted as required
48  * @param $aryData Array containing data to sort
49  * @param $strIndex name of column to use as an index
50  * @param $strSortBy Column to sort the array by
51  * @param $strSortType String containing either asc or desc [default to asc]
52  * @desc Naturally sorts an array using by the column $strSortBy
53  */
54 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
55 {
56    //    if the parameters are invalid
57    if (!is_array($aryData) || !$strIndex || !$strSortBy)
58        //    return the array
59        return $aryData;
60
61    //    create our temporary arrays
62    $arySort = $aryResult = array();
63
64    //    loop through the array
65    foreach ($aryData as $aryRow)
66        //    set up the value in the array
67        $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
68
69    //    apply the natural sort
70    natsort($arySort);
71
72    //    if the sort type is descending
73    if ($strSortType=="desc")
74        //    reverse the array
75        arsort($arySort);
76
77    //    loop through the sorted and original data
78    foreach ($arySort as $arySortKey => $arySorted)
79        foreach ($aryData as $aryOriginal)
80            //    if the key matches
81            if ($aryOriginal[$strIndex]==$arySortKey)
82                //    add it to the output array
83                array_push($aryResult, $aryOriginal);
84
85    //    return the return
86    return $aryResult;
87 }
88
89 function write_modules()
90 {
91         global $path_to_root, $installed_modules;
92
93         $mods = array_natsort($installed_modules, 'tab', 'tab');
94         $installed_modules = $mods;
95         //reset($installed_languages);
96         $n = count($installed_modules);
97         $msg = "<?php\n\n";
98
99         $msg .= "/*****************************************************************\n";
100         $msg .= "External modules for FrontAccounting\n";
101         $msg .= "******************************************************************/\n";
102         $msg .= "\n\n";
103
104         $msg .= "\$installed_modules = array (\n";
105         if ($n > 0)
106             $msg .= "\t0 => ";
107         for ($i = 0; $i < $n; $i++)
108         {
109                 if ($i > 0)
110                         $msg .= "\t\tarray ";
111                 else
112                         $msg .= "array ";
113                 $msg .= "('tab' => '" . $installed_modules[$i]['tab'] . "', ";
114                 $msg .= "'name' => '" . $installed_modules[$i]['name'] . "', ";
115                 $msg .= "'path' => '" . $installed_modules[$i]['path'] . "', ";
116                 $msg .= "'filename' => '" . $installed_modules[$i]['filename'] . "'";
117                 $msg .= "),\n";
118         }
119         $msg .= "\t);\n?>";
120
121         $filename = $path_to_root . "/modules/installed_modules.php";
122         // Check if the file exists and is writable first.
123         if (file_exists($filename) && is_writable($filename))
124         {
125                 if (!$zp = fopen($filename, 'w'))
126                 {
127                         display_error(_("Cannot open the modules file - ") . $filename);
128                         return false;
129                 }
130                 else
131                 {
132                         if (!fwrite($zp, $msg))
133                         {
134                                 display_error(_("Cannot write to the modules file - ") . $filename);
135                                 fclose($zp);
136                                 return false;
137                         }
138                         // Close file
139                         fclose($zp);
140                 }
141         }
142         else
143         {
144                 display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
145                 return false;
146         }
147         return true;
148 }
149
150 //---------------------------------------------------------------------------------------------
151
152 function handle_submit()
153 {
154         global $path_to_root, $installed_modules, $db_connections;
155
156         if (!check_data())
157                 return false;
158
159         $id = $_GET['id'];
160
161         $installed_modules[$id]['tab'] = $_POST['tab'];
162         $installed_modules[$id]['name'] = $_POST['name'];
163         $installed_modules[$id]['path'] = $_POST['path'];
164         $directory = $path_to_root . "/modules/" . $_POST['path'];
165         if (!file_exists($directory))
166         {
167                 mkdir($directory);
168         }
169         if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
170         {
171                 $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
172                 $file1 = $_FILES['uploadfile']['tmp_name'];
173                 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
174                 if (file_exists($file2))
175                         unlink($file2);
176                 move_uploaded_file($file1, $file2);
177         }
178         else
179                 $installed_modules[$id]['filename'] = $_POST['filename'];
180         if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
181         {
182                 $file1 = $_FILES['uploadfile2']['tmp_name'];
183                 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
184                 if (file_exists($file2))
185                         unlink($file2);
186                 move_uploaded_file($file1, $file2);
187                 $db_name = $_SESSION["wa_current_user"]->company;
188                 db_import($file2, $db_connections[$db_name]);
189         }
190         if (!write_modules())
191                 return false;
192         return true;
193 }
194
195 //---------------------------------------------------------------------------------------------
196
197 function handle_delete()
198 {
199         global  $path_to_root, $installed_modules;
200
201         $id = $_GET['id'];
202
203         $path = $installed_modules[$id]['path'];
204         $filename = "$path_to_root/modules/$path";
205         if ($h = opendir($filename))
206         {
207                 while (($file = readdir($h)) !== false)
208                 {
209                         if (is_file("$filename/$file"))
210                         unlink("$filename/$file");
211                 }
212                 closedir($h);
213         }
214         rmdir($filename);
215
216         unset($installed_modules[$id]);
217         $mods = array_values($installed_modules);
218         $installed_modules = $mods;
219
220         if (!write_modules())
221                 return;
222         meta_forward($_SERVER['PHP_SELF']);
223 }
224
225 //---------------------------------------------------------------------------------------------
226
227 function display_modules()
228 {
229         global $table_style, $installed_modules, $tabs;
230
231         echo "
232                 <script language='javascript'>
233                 function deleteModule(id, name) {
234                         if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
235                                 return
236                         document.location.replace('inst_module.php?c=df&id='+id)
237                 }
238                 </script>";
239         start_table($table_style);
240         $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), "", "");
241         table_header($th);
242
243         $k = 0;
244         $mods = $installed_modules;
245         $n = count($mods);
246         for ($i = 0; $i < $n; $i++)
247         {
248                 alt_table_row_color($k);
249
250                 label_cell($tabs[$mods[$i]['tab']]);
251                 label_cell($mods[$i]['name']);
252                 label_cell($mods[$i]['path']);
253                 label_cell($mods[$i]['filename']);
254                 $edit = _("Edit");
255                 $delete = _("Delete");
256                 if (user_graphic_links())
257                 {
258                         $edit = set_icon(ICON_EDIT, $edit);
259                         $delete = set_icon(ICON_DELETE, $delete);
260                 }
261         label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
262                 label_cell("<a href='javascript:deleteModule(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
263                 end_row();
264         }
265
266         end_table();
267 }
268
269 //---------------------------------------------------------------------------------------------
270
271 function display_module_edit($selected_id)
272 {
273         global $installed_modules, $table_style2;
274
275         if ($selected_id != -1)
276                 $n = $selected_id;
277         else
278                 $n = count($installed_modules);
279
280         start_form(true);
281
282         echo "
283                 <script language='javascript'>
284                 function updateModule() {
285                         document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
286                         document.forms[0].submit()
287                 }
288                 </script>";
289
290         start_table($table_style2);
291
292         if ($selected_id != -1)
293         {
294                 $mod = $installed_modules[$selected_id];
295                 $_POST['tab']  = $mod['tab'];
296                 $_POST['name'] = $mod['name'];
297                 $_POST['path'] = $mod['path'];
298                 $_POST['filename'] = $mod['filename'];
299                 hidden('selected_id', $selected_id);
300                 hidden('filename', $_POST['filename']);
301         }
302         tab_list_row(_("Menu Tab"), 'tab', null);
303         text_row_ex(_("Name"), 'name', 30);
304         text_row_ex(_("Folder"), 'path', 20);
305
306         label_row(_("Module File"), "<input name='uploadfile' type='file'>");
307         label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
308
309         end_table(0);
310         display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
311         echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
312
313
314         end_form();
315 }
316
317
318 //---------------------------------------------------------------------------------------------
319
320 if (isset($_GET['c']))
321 {
322         if ($_GET['c'] == 'df')
323         {
324                 handle_delete();
325         }
326
327         if ($_GET['c'] == 'u')
328         {
329                 if (handle_submit())
330                 {
331                         //meta_forward($_SERVER['PHP_SELF']);
332                 }
333         }
334 }
335
336 //---------------------------------------------------------------------------------------------
337
338 display_modules();
339
340 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
341
342 display_module_edit($selected_id);
343
344 //---------------------------------------------------------------------------------------------
345
346 end_page();
347
348 ?>