92ea632470648b26a0eab379946e953c9b3d7a8a
[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($id)
40 {
41         if ($_POST['name'] == "") {
42                 display_error(_("Module name cannot be empty."));
43                 return false;
44         }
45         if ($_POST['path'] == "") {
46                 display_error(_("Module folder name cannot be empty."));
47                 return false;
48         }
49         if ($id == -1 && !is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
50                 display_error(_("You have to select module file to upload"));
51                 return false; 
52         }
53         return true;
54 }
55
56 /**
57  * @return Returns the array sorted as required
58  * @param $aryData Array containing data to sort
59  * @param $strIndex name of column to use as an index
60  * @param $strSortBy Column to sort the array by
61  * @param $strSortType String containing either asc or desc [default to asc]
62  * @desc Naturally sorts an array using by the column $strSortBy
63  */
64 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
65 {
66    //    if the parameters are invalid
67    if (!is_array($aryData) || !$strIndex || !$strSortBy)
68        //    return the array
69        return $aryData;
70
71    //    create our temporary arrays
72    $arySort = $aryResult = array();
73
74    //    loop through the array
75    foreach ($aryData as $aryRow)
76        //    set up the value in the array
77        $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
78
79    //    apply the natural sort
80    natsort($arySort);
81
82    //    if the sort type is descending
83    if ($strSortType=="desc")
84        //    reverse the array
85        arsort($arySort);
86
87    //    loop through the sorted and original data
88    foreach ($arySort as $arySortKey => $arySorted)
89        foreach ($aryData as $aryOriginal)
90            //    if the key matches
91            if ($aryOriginal[$strIndex]==$arySortKey)
92                //    add it to the output array
93                array_push($aryResult, $aryOriginal);
94
95    //    return the return
96    return $aryResult;
97 }
98
99 function write_modules()
100 {
101         global $path_to_root, $installed_modules;
102
103         $mods = array_natsort($installed_modules, 'tab', 'tab');
104         $installed_modules = $mods;
105         //reset($installed_languages);
106         $n = count($installed_modules);
107         $msg = "<?php\n\n";
108
109         $msg .= "/*****************************************************************\n";
110         $msg .= "External modules for FrontAccounting\n";
111         $msg .= "******************************************************************/\n";
112         $msg .= "\n\n";
113
114         $msg .= "\$installed_modules = array (\n";
115         if ($n > 0)
116             $msg .= "\t0 => ";
117         for ($i = 0; $i < $n; $i++)
118         {
119                 if ($i > 0)
120                         $msg .= "\t\tarray ";
121                 else
122                         $msg .= "array ";
123                 $msg .= "('tab' => '" . $installed_modules[$i]['tab'] . "', ";
124                 $msg .= "'name' => '" . $installed_modules[$i]['name'] . "', ";
125                 $msg .= "'path' => '" . $installed_modules[$i]['path'] . "', ";
126                 $msg .= "'filename' => '" . $installed_modules[$i]['filename'] . "',";
127                 $msg .= "'acc_file' => '" . $installed_modules[$i]['acc_file'] . "'";
128                 $msg .= "),\n";
129         }
130         $msg .= "\t);\n?>";
131
132         $filename = $path_to_root . "/modules/installed_modules.php";
133         // Check if the file exists and is writable first.
134         if (file_exists($filename) && is_writable($filename))
135         {
136                 if (!$zp = fopen($filename, 'w'))
137                 {
138                         display_error(_("Cannot open the modules file - ") . $filename);
139                         return false;
140                 }
141                 else
142                 {
143                         if (!fwrite($zp, $msg))
144                         {
145                                 display_error(_("Cannot write to the modules file - ") . $filename);
146                                 fclose($zp);
147                                 return false;
148                         }
149                         // Close file
150                         fclose($zp);
151                 }
152         }
153         else
154         {
155                 display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
156                 return false;
157         }
158         return true;
159 }
160
161 //---------------------------------------------------------------------------------------------
162
163 function handle_submit()
164 {
165         global $path_to_root, $installed_modules, $db_connections, $selected_id;
166
167         if (!check_data($selected_id))
168                 return false;
169
170         $id = $_GET['id'];
171
172         $installed_modules[$id]['tab'] = $_POST['tab'];
173         $installed_modules[$id]['name'] = $_POST['name'];
174         $installed_modules[$id]['path'] = $_POST['path'];
175         $directory = $path_to_root . "/modules/" . $_POST['path'];
176         if (!file_exists($directory))
177         {
178                 mkdir($directory);
179         }
180         if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
181         {
182                 $installed_modules[$id]['filename'] = $_FILES['uploadfile']['name'];
183                 $file1 = $_FILES['uploadfile']['tmp_name'];
184                 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
185                 if (file_exists($file2))
186                         unlink($file2);
187                 move_uploaded_file($file1, $file2);
188         }
189         else
190                 $installed_modules[$id]['filename'] = get_post('filename');
191         if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
192         {
193                 $file1 = $_FILES['uploadfile2']['tmp_name'];
194                 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
195                 if (file_exists($file2))
196                         unlink($file2);
197                 move_uploaded_file($file1, $file2);
198                 $db_name = $_SESSION["wa_current_user"]->company;
199                 db_import($file2, $db_connections[$db_name]);
200         }
201         
202         if (is_uploaded_file($_FILES['uploadfile3']['tmp_name']))
203         {
204                 $installed_modules[$id]['acc_file'] = $_FILES['uploadfile3']['name'];
205                 $file1 = $_FILES['uploadfile3']['tmp_name'];
206                 $file2 = $directory . "/".$_FILES['uploadfile3']['name'];
207                 if (file_exists($file2))
208                         unlink($file2);
209                 move_uploaded_file($file1, $file2);
210         }
211         else
212                 $installed_modules[$id]['acc_file'] = get_post('acc_file');
213         
214         if (!write_modules())
215                 return false;
216         return true;
217 }
218
219 //---------------------------------------------------------------------------------------------
220
221 function handle_delete()
222 {
223         global  $path_to_root, $installed_modules;
224
225         $id = $_GET['id'];
226
227         $path = $installed_modules[$id]['path'];
228         $filename = "$path_to_root/modules/$path";
229         if ($h = opendir($filename))
230         {
231                 while (($file = readdir($h)) !== false)
232                 {
233                         if (is_file("$filename/$file"))
234                         unlink("$filename/$file");
235                 }
236                 closedir($h);
237         }
238         rmdir($filename);
239
240         unset($installed_modules[$id]);
241         $mods = array_values($installed_modules);
242         $installed_modules = $mods;
243
244         if (!write_modules())
245                 return;
246         meta_forward($_SERVER['PHP_SELF']);
247 }
248
249 //---------------------------------------------------------------------------------------------
250
251 function display_modules()
252 {
253         global $table_style, $installed_modules, $tabs;
254
255         echo "
256                 <script language='javascript'>
257                 function deleteModule(id, name) {
258                         if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
259                                 return
260                         document.location.replace('inst_module.php?c=df&id='+id)
261                 }
262                 </script>";
263         start_table($table_style);
264         $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), _("Access extensions"),"", "");
265         table_header($th);
266
267         $k = 0;
268         $mods = $installed_modules;
269         $n = count($mods);
270         for ($i = 0; $i < $n; $i++)
271         {
272                 alt_table_row_color($k);
273
274                 label_cell($tabs[$mods[$i]['tab']]);
275                 label_cell($mods[$i]['name']);
276                 label_cell($mods[$i]['path']);
277                 label_cell($mods[$i]['filename']);
278                 label_cell(@$mods[$i]['acc_file']);
279                 $edit = _("Edit");
280                 $delete = _("Delete");
281                 if (user_graphic_links())
282                 {
283                         $edit = set_icon(ICON_EDIT, $edit);
284                         $delete = set_icon(ICON_DELETE, $delete);
285                 }
286         label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
287                 label_cell("<a href='javascript:deleteModule(".$i.", \"" . $mods[$i]['name'] . "\")'>$delete</a>");
288                 end_row();
289         }
290
291         end_table();
292 }
293
294 //---------------------------------------------------------------------------------------------
295
296 function display_module_edit($selected_id)
297 {
298         global $installed_modules, $table_style2;
299
300         if ($selected_id != -1)
301                 $n = $selected_id;
302         else
303                 $n = count($installed_modules);
304
305         start_form(true);
306
307         echo "
308                 <script language='javascript'>
309                 function updateModule() {
310                         document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
311                         document.forms[0].submit()
312                 }
313                 </script>";
314
315         start_table($table_style2);
316
317         if ($selected_id != -1)
318         {
319                 $mod = $installed_modules[$selected_id];
320                 $_POST['tab']  = $mod['tab'];
321                 $_POST['name'] = $mod['name'];
322                 $_POST['path'] = $mod['path'];
323                 $_POST['filename'] = $mod['filename'];
324                 $_POST['acc_file'] = @$mod['acc_file'];
325                 hidden('selected_id', $selected_id);
326                 hidden('filename', $_POST['filename']);
327                 hidden('acc_file', $_POST['acc_file']);
328         }
329         tab_list_row(_("Menu Tab"), 'tab', null);
330         text_row_ex(_("Name"), 'name', 30);
331         text_row_ex(_("Folder"), 'path', 20);
332
333         label_row(_("Module File"), "<input name='uploadfile' type='file'>");
334         label_row(_("Access Levels Extensions"), "<input name='uploadfile3' type='file'>");
335         label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
336
337         end_table(0);
338         display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
339         echo "<center><input onclick='javascript:updateModule()' type='button' style='width:150px' value='". _("Save"). "'></center>";
340
341
342         end_form();
343 }
344
345 //---------------------------------------------------------------------------------------------
346
347 if (isset($_GET['c']))
348 {
349         if ($_GET['c'] == 'df')
350         {
351                 handle_delete();
352         }
353
354         if ($_GET['c'] == 'u')
355         {
356                 if (handle_submit())
357                 {
358                         if ($selected_id != -1)
359                                 display_notification(_("Module data has been updated."));
360                         else
361                                 display_notification(_("Module has been installed."));
362                 }
363         }
364 }
365
366 //---------------------------------------------------------------------------------------------
367
368 display_modules();
369
370 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
371
372 display_module_edit($selected_id);
373
374 //---------------------------------------------------------------------------------------------
375
376 end_page();
377
378 ?>