PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/inst_upgrade.php

https://bitbucket.org/boamaod/frontaccounting
PHP | 179 lines | 136 code | 20 blank | 23 comment | 30 complexity | 3d445e85355bf047af58293214085e51 MD5 | raw file
Possible License(s): GPL-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_SOFTWAREUPGRADE';
  13. $path_to_root="..";
  14. include_once($path_to_root . "/includes/session.inc");
  15. page(_($help_context = "Software Upgrade"));
  16. include_once($path_to_root . "/includes/date_functions.inc");
  17. include_once($path_to_root . "/admin/db/company_db.inc");
  18. include_once($path_to_root . "/admin/db/maintenance_db.inc");
  19. include_once($path_to_root . "/includes/ui.inc");
  20. //
  21. // Creates table of installer objects sorted by version.
  22. //
  23. function get_installers()
  24. {
  25. global $path_to_root;
  26. $patchdir = $path_to_root."/sql/";
  27. $upgrades = array();
  28. $datadir = @opendir($patchdir);
  29. if ($datadir)
  30. {
  31. while(false !== ($fname = readdir($datadir)))
  32. { // check all php files but index.php
  33. if (!is_dir($patchdir . $fname) && ($fname != 'index.php')
  34. && stristr($fname, '.php') != false && $fname[0] != '.')
  35. {
  36. unset($install);
  37. include_once($patchdir . $fname);
  38. if (isset($install)) // add installer if found
  39. $upgrades[$install->version] = $install;
  40. }
  41. }
  42. ksort($upgrades); // sort by file name
  43. $upgrades = array_values($upgrades);
  44. }
  45. return $upgrades;
  46. }
  47. //
  48. // Apply one differential data set.
  49. //
  50. function upgrade_step($index, $conn)
  51. {
  52. global $path_to_root, $installers;
  53. $inst = $installers[$index];
  54. $pref = $conn['tbpref'];
  55. $ret = true;
  56. $force = get_post('force_'.$index);
  57. if ($force || get_post('install_'.$index))
  58. {
  59. $state = $inst->installed($pref);
  60. if (!$state || $force)
  61. {
  62. if (!$inst->pre_check($pref, $force)) return false;
  63. $sql = $inst->sql;
  64. error_log(sprintf(_("Database upgrade for company '%s' (%s:%s*) started..."),
  65. $conn['name'], $conn['dbname'], $conn['tbpref']));
  66. if ($sql != '')
  67. $ret &= db_import($path_to_root.'/sql/'.$sql, $conn, $force);
  68. $ret &= $inst->install($pref, $force);
  69. error_log(_("Database upgrade finished."));
  70. } else
  71. if ($state!==true) {
  72. display_error(_("Upgrade cannot be done because database has been already partially upgraded. Please downgrade database to clean previous version or try forced upgrade."));
  73. $ret = false;
  74. }
  75. }
  76. return $ret;
  77. }
  78. $installers = get_installers();
  79. if (get_post('Upgrade'))
  80. {
  81. $ret = true;
  82. foreach ($db_connections as $comp => $conn)
  83. {
  84. // connect to database
  85. if (!(set_global_connection($comp)))
  86. {
  87. display_error(_("Cannot connect to database for company")
  88. ." '".$conn['name']."'");
  89. continue;
  90. }
  91. // create security backup
  92. db_backup($conn, 'no', 'Security backup before upgrade', $conn['tbpref']);
  93. // apply all upgrade data
  94. foreach ($installers as $i => $inst)
  95. {
  96. $ret = upgrade_step($i, $conn);
  97. if (!$ret)
  98. display_error(
  99. sprintf(_("Database upgrade to version %s failed for company '%s'."),
  100. $inst->version, $conn['name'])
  101. .'<br>'
  102. ._('You should restore company database from latest backup file'));
  103. }
  104. // db_close($conn); ?
  105. if (!$ret) break;
  106. }
  107. set_global_connection();
  108. if($ret)
  109. { // re-read the prefs
  110. global $path_to_root;
  111. include_once($path_to_root . "/admin/db/users_db.inc");
  112. $user = get_user_by_login($_SESSION["wa_current_user"]->username);
  113. $_SESSION["wa_current_user"]->prefs = new user_prefs($user);
  114. display_notification(_('All companies data has been successfully updated'));
  115. }
  116. refresh_sys_prefs(); // re-read system setup
  117. $Ajax->activate('_page_body');
  118. }
  119. start_form();
  120. start_table(TABLESTYLE);
  121. $th = array(_("Version"), _("Description"), _("Sql file"), _("Install"),
  122. _("Force upgrade"));
  123. table_header($th);
  124. $k = 0; //row colour counter
  125. $partial = 0;
  126. foreach($installers as $i => $inst)
  127. {
  128. alt_table_row_color($k);
  129. start_row();
  130. label_cell($inst->version);
  131. label_cell($inst->description);
  132. label_cell($inst->sql ? $inst->sql : '<i>'._('None').'</i>', 'align=center');
  133. // this is checked only for first (site admin) company,
  134. // but in fact we should always upgrade all data sets after
  135. // source upgrade.
  136. $check = $inst->installed(TB_PREF);
  137. if ($check === true)
  138. label_cell(_("Installed"));
  139. else
  140. if (!$check)
  141. check_cells(null,'install_'.$i, 0);
  142. else {
  143. label_cell("<span class=redfg>"
  144. . sprintf(_("Partially installed (%s)"), $check) . "</span>");
  145. $partial++;
  146. }
  147. check_cells(null,'force_'.$i, 0);
  148. end_row();
  149. }
  150. end_table(1);
  151. if ($partial!=0) {
  152. display_note(_("Database upgrades marked as partially installed cannot be installed automatically.
  153. You have to clean database manually to enable them, or try to perform forced upgrade."));
  154. br();
  155. }
  156. submit_center('Upgrade', _('Upgrade system'), true, _('Save database and perform upgrade'), 'process');
  157. end_form();
  158. end_page();
  159. ?>