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';
14 include_once($path_to_root . "/includes/session.inc");
16 page(_("Install/Update Modules"));
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");
24 //---------------------------------------------------------------------------------------------
26 if (isset($_GET['selected_id']))
28 $selected_id = $_GET['selected_id'];
30 elseif (isset($_POST['selected_id']))
32 $selected_id = $_POST['selected_id'];
37 //---------------------------------------------------------------------------------------------
39 function check_data($id)
41 if ($_POST['name'] == "") {
42 display_error(_("Module name cannot be empty."));
45 if ($_POST['path'] == "") {
46 display_error(_("Module folder name cannot be empty."));
49 if ($id == -1 && !is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
50 display_error(_("You have to select module file to upload"));
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
64 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false)
66 // if the parameters are invalid
67 if (!is_array($aryData) || !$strIndex || !$strSortBy)
71 // create our temporary arrays
72 $arySort = $aryResult = array();
74 // loop through the array
75 foreach ($aryData as $aryRow)
76 // set up the value in the array
77 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
79 // apply the natural sort
82 // if the sort type is descending
83 if ($strSortType=="desc")
87 // loop through the sorted and original data
88 foreach ($arySort as $arySortKey => $arySorted)
89 foreach ($aryData as $aryOriginal)
91 if ($aryOriginal[$strIndex]==$arySortKey)
92 // add it to the output array
93 array_push($aryResult, $aryOriginal);
99 function write_modules()
101 global $path_to_root, $installed_modules;
103 $mods = array_natsort($installed_modules, 'tab', 'tab');
104 $installed_modules = $mods;
105 //reset($installed_languages);
106 $n = count($installed_modules);
109 $msg .= "/*****************************************************************\n";
110 $msg .= "External modules for FrontAccounting\n";
111 $msg .= "******************************************************************/\n";
114 $msg .= "\$installed_modules = array (\n";
117 for ($i = 0; $i < $n; $i++)
120 $msg .= "\t\tarray ";
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'] . "'";
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))
136 if (!$zp = fopen($filename, 'w'))
138 display_error(_("Cannot open the modules file - ") . $filename);
143 if (!fwrite($zp, $msg))
145 display_error(_("Cannot write to the modules file - ") . $filename);
155 display_error(_("The modules file ") . $filename . _(" is not writable. Change its permissions so it is, then re-run the operation."));
161 //---------------------------------------------------------------------------------------------
163 function handle_submit()
165 global $path_to_root, $installed_modules, $db_connections, $selected_id;
167 if (!check_data($selected_id))
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))
180 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
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))
187 move_uploaded_file($file1, $file2);
190 $installed_modules[$id]['filename'] = get_post('filename');
191 if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
193 $file1 = $_FILES['uploadfile2']['tmp_name'];
194 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
195 if (file_exists($file2))
197 move_uploaded_file($file1, $file2);
198 $db_name = $_SESSION["wa_current_user"]->company;
199 db_import($file2, $db_connections[$db_name]);
202 if (is_uploaded_file($_FILES['uploadfile3']['tmp_name']))
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))
209 move_uploaded_file($file1, $file2);
212 $installed_modules[$id]['acc_file'] = get_post('acc_file');
214 if (!write_modules())
219 //---------------------------------------------------------------------------------------------
221 function handle_delete()
223 global $path_to_root, $installed_modules;
227 $path = $installed_modules[$id]['path'];
228 $filename = "$path_to_root/modules/$path";
229 if ($h = opendir($filename))
231 while (($file = readdir($h)) !== false)
233 if (is_file("$filename/$file"))
234 unlink("$filename/$file");
240 unset($installed_modules[$id]);
241 $mods = array_values($installed_modules);
242 $installed_modules = $mods;
244 if (!write_modules())
246 meta_forward($_SERVER['PHP_SELF']);
249 //---------------------------------------------------------------------------------------------
251 function display_modules()
253 global $table_style, $installed_modules, $tabs;
256 <script language='javascript'>
257 function deleteModule(id, name) {
258 if (!confirm('" . _("Are you sure you want to delete module: ") . "'+name))
260 document.location.replace('inst_module.php?c=df&id='+id)
263 start_table($table_style);
264 $th = array(_("Tab"), _("Name"), _("Folder"), _("Filename"), _("Access extensions"),"", "");
268 $mods = $installed_modules;
270 for ($i = 0; $i < $n; $i++)
272 alt_table_row_color($k);
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']);
280 $delete = _("Delete");
281 if (user_graphic_links())
283 $edit = set_icon(ICON_EDIT, $edit);
284 $delete = set_icon(ICON_DELETE, $delete);
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>");
294 //---------------------------------------------------------------------------------------------
296 function display_module_edit($selected_id)
298 global $installed_modules, $table_style2;
300 if ($selected_id != -1)
303 $n = count($installed_modules);
308 <script language='javascript'>
309 function updateModule() {
310 document.forms[0].action='inst_module.php?c=u&id=" . $n . "'
311 document.forms[0].submit()
315 start_table($table_style2);
317 if ($selected_id != -1)
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']);
329 tab_list_row(_("Menu Tab"), 'tab', null);
330 text_row_ex(_("Name"), 'name', 30);
331 text_row_ex(_("Folder"), 'path', 20);
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'>");
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>";
345 //---------------------------------------------------------------------------------------------
347 if (isset($_GET['c']))
349 if ($_GET['c'] == 'df')
354 if ($_GET['c'] == 'u')
358 if ($selected_id != -1)
359 display_notification(_("Module data has been updated."));
361 display_notification(_("Module has been installed."));
366 //---------------------------------------------------------------------------------------------
370 hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new module"));
372 display_module_edit($selected_id);
374 //---------------------------------------------------------------------------------------------