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(_($help_context = "Install/Activate extensions"));
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 . "/includes/ui.inc");
23 simple_page_mode(true);
25 //---------------------------------------------------------------------------------------------
26 function update_extensions($extensions) {
27 global $db_connections;
29 if (!write_extensions($extensions)) {
30 display_notification(_("Cannot update system extensions list."));
34 // update per company files
35 $cnt = count($db_connections);
36 for($i = 0; $i < $cnt; $i++)
38 $newexts = $extensions;
39 // update 'active' status
40 $exts = get_company_extensions($i);
41 foreach ($exts as $key => $ext)
43 if (isset($newexts[$key]))
44 $newexts[$key]['active'] = $exts[$key]['active'];
46 if(!write_extensions($newexts, $i))
48 display_notification(sprintf(_("Cannot update extensions list for company '%s'."),
49 $db_connections[$i]['name']));
56 function check_data($id, $exts)
58 if ($_POST['name'] == "") {
59 display_error(_("Extension name cannot be empty."));
62 foreach($exts as $n =>$ext) {
63 if ($_POST['name'] == $ext['name'] && $id != $n) {
64 display_error(_("Extension name have to be unique."));
69 if ($_POST['title'] == "") {
70 display_error(_("Extension title cannot be empty."));
73 if ($_POST['path'] == "") {
74 display_error(_("Extension folder name cannot be empty."));
77 if ($id == -1 && !is_uploaded_file($_FILES['uploadfile']['tmp_name'])) {
78 display_error(_("You have to select plugin file to upload"));
84 //---------------------------------------------------------------------------------------------
86 function handle_submit()
88 global $path_to_root, $db_connections, $selected_id, $next_extension_id;
90 $extensions = get_company_extensions();
91 if (!check_data($selected_id, $extensions))
93 $id = $selected_id==-1 ? $next_extension_id : $selected_id;
95 if ($selected_id != -1 && $extensions[$id]['type'] != 'plugin') {
96 display_error(_('Module installation support is not implemented yet. You have to do it manually.'));
100 $extensions[$id]['tab'] = $_POST['tab'];
101 $extensions[$id]['name'] = $_POST['name'];
102 $extensions[$id]['path'] = $_POST['path'];
103 $extensions[$id]['title'] = $_POST['title'];
104 $extensions[$id]['active'] = check_value('active');
106 // Currently we support only plugin extensions here.
107 $extensions[$id]['type'] = 'plugin';
108 $directory = $path_to_root . "/modules/" . $_POST['path'];
109 if (!file_exists($directory))
113 if (is_uploaded_file($_FILES['uploadfile']['tmp_name']))
115 $extensions[$id]['filename'] = $_FILES['uploadfile']['name'];
116 $file1 = $_FILES['uploadfile']['tmp_name'];
117 $file2 = $directory . "/".$_FILES['uploadfile']['name'];
118 if (file_exists($file2))
120 move_uploaded_file($file1, $file2);
123 $extensions[$id]['filename'] = get_post('filename');
124 if (is_uploaded_file($_FILES['uploadfile2']['tmp_name']))
126 $file1 = $_FILES['uploadfile2']['tmp_name'];
127 $file2 = $directory . "/".$_FILES['uploadfile2']['name'];
128 if (file_exists($file2))
130 move_uploaded_file($file1, $file2);
131 $db_name = $_SESSION["wa_current_user"]->company;
132 db_import($file2, $db_connections[$db_name]);
135 if (is_uploaded_file($_FILES['uploadfile3']['tmp_name']))
137 $extensions[$id]['acc_file'] = $_FILES['uploadfile3']['name'];
138 $file1 = $_FILES['uploadfile3']['tmp_name'];
139 $file2 = $directory . "/".$_FILES['uploadfile3']['name'];
140 if (file_exists($file2))
142 move_uploaded_file($file1, $file2);
145 $extensions[$id]['acc_file'] = get_post('acc_file');
147 // security area guess for plugins
148 if ($extensions[$id]['type'] == 'plugin'){
149 $exttext = file_get_contents($path_to_root.'/modules/'
150 .$extensions[$id]['path'].'/'.$extensions[$id]['filename']);
152 if (preg_match('/.*\$page_security\s*=\s*[\'"]([^\'"]*)/', $exttext, $match)) {
153 $area = trim($match[1]);
155 $extensions[$id]['access'] = $area;
158 if ($selected_id == -1)
160 $next_extension_id++;
162 if (!update_extensions($extensions))
167 function handle_delete()
169 global $path_to_root, $db_connections, $selected_id;
171 $extensions = get_company_extensions();
175 $filename = $path_to_root
176 . ($extensions[$id]['type']=='plugin' ? "/modules/": '/')
177 . $extensions[$id]['path'];
179 flush_dir($filename);
181 unset($extensions[$id]);
182 if (update_extensions($extensions))
183 display_notification(_("Selected extension has been successfully deleted"));
187 //---------------------------------------------------------------------------------------------
189 function display_extensions()
193 start_table($table_style);
194 $th = array(_("Name"),_("Tab"), _("Link text"), _("Folder"), _("Filename"),
195 _("Access extensions"),"", "");
199 $mods = get_company_extensions();
200 $mods = array_natsort($mods, null, 'name');
202 foreach($mods as $i => $mod)
204 $is_mod = $mod['type'] == 'module';
205 alt_table_row_color($k);
206 label_cell($mod['name']);
207 label_cell( $is_mod ?
208 $mod['title'] : access_string($_SESSION['App']->applications[$mod['tab']]->name, true));
209 $ttl = access_string($mod['title']);
211 label_cell($mod['path']);
212 label_cell($mod['filename']);
213 label_cell(@$mod['acc_file']);
216 label_cell(''); // not implemented (yet)
220 edit_button_cell("Edit".$i, _("Edit"));
222 delete_button_cell("Delete".$i, _("Delete"));
223 submit_js_confirm('Delete'.$i, _('You are about to delete this extension\nDo you want to continue?'));
230 function company_extensions($id)
234 start_table($table_style);
236 $th = array(_("Name"),_("Tab"), _("Link text"), _("Active"));
238 // get all available extensions and display
239 // with current status stored in company directory.
241 $mods = get_company_extensions();
242 $exts = get_company_extensions($id);
243 foreach($mods as $key => $ins) {
244 foreach($exts as $ext)
245 if ($ext['name'] == $ins['name']) {
246 $mods[$key]['active'] = @$ext['active'];
250 $mods = array_natsort($mods, null, 'name');
253 foreach($mods as $i => $mod)
255 alt_table_row_color($k);
256 label_cell($mod['name']);
257 label_cell( $mod['type'] == 'module' ?
258 $mod['title'] : access_string($_SESSION['App']->applications[$mod['tab']]->name, true));
259 $ttl = access_string($mod['title']);
261 check_cells(null, 'Active'.$i, @$mod['active'] ? 1:0,
262 false, false, "align='center'");
267 submit_center('Update', _('Update'), true, false, 'default');
270 //---------------------------------------------------------------------------------------------
272 function display_ext_edit($selected_id)
274 global $table_style2, $Mode;
277 $extensions = get_company_extensions();
279 start_table($table_style2);
281 if ($selected_id != -1 && $extensions[$selected_id]['type'] == 'plugin')
283 if ($Mode == 'Edit') {
284 $mod = $extensions[$selected_id];
285 $_POST['tab'] = $mod['tab'];
286 $_POST['name'] = $mod['name'];
287 $_POST['title'] = $mod['title'];
288 $_POST['path'] = $mod['path'];
289 $_POST['filename'] = $mod['filename'];
290 $_POST['acc_file'] = @$mod['acc_file'];
291 hidden('filename', $_POST['filename']);
292 hidden('acc_file', $_POST['acc_file']);
294 hidden('selected_id', $selected_id);
296 text_row_ex(_("Name"), 'name', 30);
297 text_row_ex(_("Folder"), 'path', 20);
299 tab_list_row(_("Menu Tab"), 'tab', null, true);
300 text_row_ex(_("Menu Link Text"), 'title', 30);
302 record_status_list_row(_("Default status"), 'active');
304 label_row(_("Module File"), "<input name='uploadfile' type='file'>");
305 label_row(_("Access Levels Extensions"), "<input name='uploadfile3' type='file'>");
306 label_row(_("SQL File"), "<input name='uploadfile2' type='file'>");
309 display_note(_("Select your module PHP file from your local harddisk."), 0, 1);
310 submit_add_or_update_center($selected_id == -1, '', 'both');
313 //---------------------------------------------------------------------------------------------
314 if ($Mode=='ADD_ITEM' || $Mode == 'UPDATE_ITEM') {
315 if(handle_submit()) {
316 if ($selected_id != -1)
317 display_notification(_("Extension data has been updated."));
319 display_notification(_("Extension has been installed."));
323 if ($Mode == 'Delete')
328 if (get_post('Update')) {
329 $exts = get_company_extensions();
330 foreach($exts as $i => $ext) {
331 $exts[$i]['active'] = check_value('Active'.$i);
333 write_extensions($exts, get_post('extset'));
334 if (get_post('extset') == user_company())
335 $installed_extensions = $exts;
336 display_notification(_('Current active extensions set has been saved.'));
339 if ($Mode == 'RESET')
345 //---------------------------------------------------------------------------------------------
347 if (list_updated('extset'))
348 $Ajax->activate('_page_body');
350 echo "<center>" . _('Extensions:') . " ";
351 echo extset_list('extset', null, true);
352 echo "</center><br>";
354 $set = get_post('extset', -1);
356 display_extensions();
358 display_ext_edit($selected_id);
360 company_extensions($set);
362 //---------------------------------------------------------------------------------------------