PageRenderTime 28ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/create_coy.php

https://bitbucket.org/meerdevelopersoffice/bolivia
PHP | 389 lines | 307 code | 52 blank | 30 comment | 98 complexity | de19e24eed25f229a0d09e110bd167c8 MD5 | raw file
Possible License(s): Apache-2.0, GPL-3.0, LGPL-3.0
  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_CREATECOMPANY';
  13. $path_to_root="..";
  14. include_once($path_to_root . "/includes/session.inc");
  15. include_once($path_to_root . "/includes/date_functions.inc");
  16. include_once($path_to_root . "/admin/db/company_db.inc");
  17. include_once($path_to_root . "/admin/db/maintenance_db.inc");
  18. include_once($path_to_root . "/includes/ui.inc");
  19. page(_($help_context = "Create/Update Company"));
  20. $comp_subdirs = array('images', 'pdf_files', 'backup','js_cache', 'reporting', 'attachments');
  21. //---------------------------------------------------------------------------------------------
  22. if (isset($_GET['selected_id']))
  23. {
  24. $selected_id = (int)$_GET['selected_id'];
  25. }
  26. elseif (isset($_POST['selected_id']))
  27. {
  28. $selected_id = (int)$_POST['selected_id'];
  29. }
  30. else
  31. $selected_id = -1;
  32. //---------------------------------------------------------------------------------------------
  33. function check_data()
  34. {
  35. global $db_connections, $tb_pref_counter, $selected_id;
  36. if($selected_id != -1) {
  37. if ($_POST['name'] == "")
  38. {
  39. display_error(_("Database settings are not specified."));
  40. return false;
  41. }
  42. } else {
  43. if ($_POST['name'] == "" || $_POST['host'] == "" || $_POST['dbuser'] == "" || $_POST['dbname'] == "")
  44. {
  45. display_error(_("Database settings are not specified."));
  46. return false;
  47. }
  48. foreach($db_connections as $id=>$con)
  49. {
  50. if($id != $selected_id && $_POST['host'] == $con['host']
  51. && $_POST['dbname'] == $con['dbname'])
  52. {
  53. if ($_POST['tbpref'] == $con['tbpref'])
  54. {
  55. display_error(_("This database settings are already used by another company."));
  56. return false;
  57. }
  58. if (($_POST['tbpref'] == 0) ^ ($con['tbpref'] == ''))
  59. {
  60. display_error(_("You cannot have table set without prefix together with prefixed sets in the same database."));
  61. return false;
  62. }
  63. }
  64. }
  65. }
  66. return true;
  67. }
  68. //---------------------------------------------------------------------------------------------
  69. function remove_connection($id) {
  70. global $db_connections;
  71. $err = db_drop_db($db_connections[$id]);
  72. unset($db_connections[$id]);
  73. $conn = array_values($db_connections);
  74. $db_connections = $conn;
  75. //$$db_connections = array_values($db_connections);
  76. return $err;
  77. }
  78. //---------------------------------------------------------------------------------------------
  79. function handle_submit()
  80. {
  81. global $db_connections, $def_coy, $tb_pref_counter, $db,
  82. $comp_subdirs, $path_to_root, $selected_id;
  83. $error = false;
  84. if (!check_data())
  85. return false;
  86. if ($selected_id==-1)
  87. $selected_id = count($db_connections);
  88. $new = !isset($db_connections[$selected_id]);
  89. if ((bool)$_POST['def'] == true)
  90. $def_coy = $selected_id;
  91. $db_connections[$selected_id]['name'] = $_POST['name'];
  92. if($new) {
  93. $db_connections[$selected_id]['host'] = $_POST['host'];
  94. $db_connections[$selected_id]['dbuser'] = $_POST['dbuser'];
  95. $db_connections[$selected_id]['dbpassword'] = $_POST['dbpassword'];
  96. $db_connections[$selected_id]['dbname'] = $_POST['dbname'];
  97. if (is_numeric($_POST['tbpref']))
  98. {
  99. $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'] == 1 ?
  100. $tb_pref_counter."_" : '';
  101. }
  102. else if ($_POST['tbpref'] != "")
  103. $db_connections[$selected_id]['tbpref'] = $_POST['tbpref'];
  104. else
  105. $db_connections[$selected_id]['tbpref'] = "";
  106. $conn = $db_connections[$selected_id];
  107. if (($db = db_create_db($conn)) == 0)
  108. {
  109. display_error(_("Error creating Database: ") . $conn['dbname'] . _(", Please create it manually"));
  110. $error = true;
  111. } else {
  112. if (!db_import($path_to_root.'/sql/'.get_post('coa'), $conn, $selected_id)) {
  113. display_error(_('Cannot create new company due to bugs in sql file.'));
  114. $error = true;
  115. }
  116. else
  117. {
  118. if (!isset($_POST['admpassword']) || $_POST['admpassword'] == "")
  119. $_POST['admpassword'] = "password";
  120. update_admin_password($conn, md5($_POST['admpassword']));
  121. }
  122. }
  123. set_global_connection();
  124. if ($error) {
  125. remove_connection($selected_id);
  126. return false;
  127. }
  128. }
  129. $error = write_config_db($new);
  130. if ($error == -1)
  131. display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
  132. else if ($error == -2)
  133. display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
  134. else if ($error == -3)
  135. display_error(_("The configuration file ") . $path_to_root . "/config_db.php" . _(" is not writable. Change its permissions so it is, then re-run the operation."));
  136. if ($error != 0)
  137. {
  138. return false;
  139. }
  140. if ($new)
  141. {
  142. create_comp_dirs(company_path($selected_id), $comp_subdirs);
  143. }
  144. $exts = get_company_extensions();
  145. write_extensions($exts, $selected_id);
  146. display_notification($new ? _('New company has been created.') : _('Company has been updated.'));
  147. return true;
  148. }
  149. //---------------------------------------------------------------------------------------------
  150. function handle_delete()
  151. {
  152. global $def_coy, $db_connections, $comp_subdirs, $path_to_root;
  153. $id = (int)$_GET['id'];
  154. // First make sure all company directories from the one under removal are writable.
  155. // Without this after operation we end up with changed per-company owners!
  156. for($i = $id; $i < count($db_connections); $i++) {
  157. $comp_path = company_path($i);
  158. if (!is_dir($comp_path) || !is_writable($comp_path)) {
  159. display_error(_('Broken company subdirectories system. You have to remove this company manually.'));
  160. return;
  161. }
  162. }
  163. // make sure config file is writable
  164. if (!is_writeable($path_to_root . "/config_db.php"))
  165. {
  166. display_error(_("The configuration file ") . $path_to_root . "/config_db.php" . _(" is not writable. Change its permissions so it is, then re-run the operation."));
  167. return;
  168. }
  169. // rename directory to temporary name to ensure all
  170. // other subdirectories will have right owners even after
  171. // unsuccessfull removal.
  172. $cdir = company_path($id);
  173. $tmpname = company_path('/old_'.$id);
  174. if (!@rename($cdir, $tmpname)) {
  175. display_error(_('Cannot rename subdirectory to temporary name.'));
  176. return;
  177. }
  178. // 'shift' company directories names
  179. for ($i = $id+1; $i < count($db_connections); $i++) {
  180. if (!rename(company_path($i), company_path($i-1))) {
  181. display_error(_("Cannot rename company subdirectory"));
  182. return;
  183. }
  184. }
  185. $err = remove_connection($id);
  186. if ($err == 0)
  187. display_error(_("Error removing Database: ") . $dbase . _(", please remove it manually"));
  188. if ($def_coy == $id)
  189. $def_coy = 0;
  190. $error = write_config_db();
  191. if ($error == -1)
  192. display_error(_("Cannot open the configuration file - ") . $path_to_root . "/config_db.php");
  193. else if ($error == -2)
  194. display_error(_("Cannot write to the configuration file - ") . $path_to_root . "/config_db.php");
  195. else if ($error == -3)
  196. display_error(_("The configuration file ") . $path_to_root . "/config_db.php" . _(" is not writable. Change its permissions so it is, then re-run the operation."));
  197. if ($error != 0) {
  198. @rename($tmpname, $cdir);
  199. return;
  200. }
  201. // finally remove renamed company directory
  202. @flush_dir($tmpname, true);
  203. if (!@rmdir($tmpname))
  204. {
  205. display_error(_("Cannot remove temporary renamed company data directory ") . $tmpname);
  206. return;
  207. }
  208. display_notification(_("Selected company has been deleted"));
  209. }
  210. //---------------------------------------------------------------------------------------------
  211. function display_companies()
  212. {
  213. global $def_coy, $db_connections;
  214. $coyno = $_SESSION["wa_current_user"]->company;
  215. echo "
  216. <script language='javascript'>
  217. function deleteCompany(id, name) {
  218. if (!confirm('" . _("Are you sure you want to delete company no. ") . "'+id+' '+name+'?'))
  219. return
  220. document.location.replace('create_coy.php?c=df&id='+id)
  221. }
  222. </script>";
  223. start_table(TABLESTYLE);
  224. $th = array(_("Company"), _("Database Host"), _("Database User"),
  225. _("Database Name"), _("Table Pref"), _("Default"), "", "");
  226. table_header($th);
  227. $k=0;
  228. $conn = $db_connections;
  229. $n = count($conn);
  230. for ($i = 0; $i < $n; $i++)
  231. {
  232. if ($i == $def_coy)
  233. $what = _("Yes");
  234. else
  235. $what = _("No");
  236. if ($i == $coyno)
  237. start_row("class='stockmankobg'");
  238. else
  239. alt_table_row_color($k);
  240. label_cell($conn[$i]['name']);
  241. label_cell($conn[$i]['host']);
  242. label_cell($conn[$i]['dbuser']);
  243. label_cell($conn[$i]['dbname']);
  244. label_cell($conn[$i]['tbpref']);
  245. label_cell($what);
  246. $edit = _("Edit");
  247. $delete = _("Delete");
  248. if (user_graphic_links())
  249. {
  250. $edit = set_icon(ICON_EDIT, $edit);
  251. $delete = set_icon(ICON_DELETE, $delete);
  252. }
  253. label_cell("<a href='" . $_SERVER['PHP_SELF']. "?selected_id=$i'>$edit</a>");
  254. $name = "\"".$conn[$i]['name']."\"";
  255. label_cell( $i == $coyno ? '' :
  256. "<a href='javascript:deleteCompany($i, $name)'>$delete</a>");
  257. end_row();
  258. }
  259. end_table();
  260. display_note(_("The marked company is the current company which cannot be deleted."), 0, 0, "class='currentfg'");
  261. display_note(_("If no Admin Password is entered, the new Admin Password will be '<b>password</b>' by default "), 1, 0, "class='currentfg'");
  262. }
  263. //---------------------------------------------------------------------------------------------
  264. function display_company_edit($selected_id)
  265. {
  266. global $def_coy, $db_connections, $tb_pref_counter;
  267. start_form();
  268. start_table(TABLESTYLE2);
  269. if ($selected_id != -1)
  270. {
  271. $conn = $db_connections[$selected_id];
  272. $_POST['name'] = $conn['name'];
  273. $_POST['host'] = $conn['host'];
  274. $_POST['dbuser'] = $conn['dbuser'];
  275. $_POST['dbpassword'] = $conn['dbpassword'];
  276. $_POST['dbname'] = $conn['dbname'];
  277. $_POST['tbpref'] = $conn['tbpref'];
  278. if ($selected_id == $def_coy)
  279. $_POST['def'] = true;
  280. else
  281. $_POST['def'] = false;
  282. $_POST['dbcreate'] = false;
  283. hidden('selected_id', $selected_id);
  284. hidden('tbpref', $_POST['tbpref']);
  285. hidden('dbpassword', $_POST['dbpassword']);
  286. }
  287. else
  288. {
  289. $_POST['tbpref'] = $tb_pref_counter."_";
  290. // Insert the current settings as default
  291. $conn = $db_connections[user_company()];
  292. $_POST['name'] = '';
  293. $_POST['host'] = $conn['host'];
  294. $_POST['dbuser'] = $conn['dbuser'];
  295. $_POST['dbpassword'] = $conn['dbpassword'];
  296. $_POST['dbname'] = $conn['dbname'];
  297. }
  298. text_row_ex(_("Company"), 'name', 50);
  299. if ($selected_id == -1)
  300. {
  301. text_row_ex(_("Host"), 'host', 30, 60);
  302. text_row_ex(_("Database User"), 'dbuser', 30);
  303. text_row_ex(_("Database Password"), 'dbpassword', 30);
  304. text_row_ex(_("Database Name"), 'dbname', 30);
  305. yesno_list_row(_("Table Pref"), 'tbpref', 1, $_POST['tbpref'], _("None"), false);
  306. } else {
  307. label_row(_("Host"), $_POST['host']);
  308. label_row(_("Database User"), $_POST['dbuser']);
  309. label_row(_("Database Name"), $_POST['dbname']);
  310. label_row(_("Table Pref"), $_POST['tbpref']);
  311. }
  312. yesno_list_row(_("Default"), 'def', null, "", "", false);
  313. if ($selected_id == -1)
  314. {
  315. coa_list_row(_("Database Script"), 'coa');
  316. text_row_ex(_("New script Admin Password"), 'admpassword', 20);
  317. }
  318. end_table(1);
  319. submit_center('save', _("Save"));
  320. end_form();
  321. }
  322. //---------------------------------------------------------------------------------------------
  323. if (isset($_GET['c']) && $_GET['c'] == 'df') {
  324. handle_delete();
  325. $selected_id = -1;
  326. }
  327. if (get_post('save')) {
  328. if (handle_submit())
  329. $selected_id = -1;
  330. }
  331. //---------------------------------------------------------------------------------------------
  332. display_companies();
  333. hyperlink_no_params($_SERVER['PHP_SELF'], _("Create a new company"));
  334. display_company_edit($selected_id);
  335. //---------------------------------------------------------------------------------------------
  336. end_page();
  337. ?>