upgrade.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. <?php
  2. /*
  3. FusionPBX
  4. Version: MPL 1.1
  5. The contents of this file are subject to the Mozilla Public License Version
  6. 1.1 (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.mozilla.org/MPL/
  9. Software distributed under the License is distributed on an "AS IS" basis,
  10. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. for the specific language governing rights and limitations under the
  12. License.
  13. The Original Code is FusionPBX
  14. The Initial Developer of the Original Code is
  15. Mark J Crane <[email protected]>
  16. Portions created by the Initial Developer are Copyright (C) 2008-2024
  17. the Initial Developer. All Rights Reserved.
  18. Contributor(s):
  19. Mark J Crane <[email protected]>
  20. */
  21. //include file
  22. require dirname(__DIR__, 2) . "/resources/require.php";
  23. //if the config file doesn't exist and the config.php does exist use it to write a new config file
  24. if (isset($config_exists) && !$config_exists && file_exists("/etc/fusionpbx/config.php")) {
  25. //include the config.php
  26. include("/etc/fusionpbx/config.php");
  27. //set the default config file location
  28. if (stristr(PHP_OS, 'BSD')) {
  29. $config_path = '/usr/local/etc/fusionpbx';
  30. $config_file = $config_path.'/config.conf';
  31. $document_root = '/usr/local/www/fusionpbx';
  32. $conf_dir = '/usr/local/etc/freeswitch';
  33. $sounds_dir = '/usr/share/freeswitch/sounds';
  34. $database_dir = '/var/lib/freeswitch/db';
  35. $recordings_dir = '/var/lib/freeswitch/recordings';
  36. $storage_dir = '/var/lib/freeswitch/storage';
  37. $voicemail_dir = '/var/lib/freeswitch/storage/voicemail';
  38. $scripts_dir = '/usr/share/freeswitch/scripts';
  39. $php_dir = PHP_BINDIR;
  40. $cache_location = '/var/cache/fusionpbx';
  41. }
  42. if (stristr(PHP_OS, 'Linux')) {
  43. $config_path = '/etc/fusionpbx/';
  44. $config_file = $config_path.'/config.conf';
  45. $document_root = '/var/www/fusionpbx';
  46. $conf_dir = '/etc/freeswitch';
  47. $sounds_dir = '/usr/share/freeswitch/sounds';
  48. $database_dir = '/var/lib/freeswitch/db';
  49. $recordings_dir = '/var/lib/freeswitch/recordings';
  50. $storage_dir = '/var/lib/freeswitch/storage';
  51. $voicemail_dir = '/var/lib/freeswitch/storage/voicemail';
  52. $scripts_dir = '/usr/share/freeswitch/scripts';
  53. $php_dir = PHP_BINDIR;
  54. $cache_location = '/var/cache/fusionpbx';
  55. }
  56. if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
  57. $system_drive = getenv('SystemDrive');
  58. $config_path = $system_drive . DIRECTORY_SEPARATOR . 'ProgramData' . DIRECTORY_SEPARATOR . 'fusionpbx' ;
  59. $config_file = $config_path.DIRECTORY_SEPARATOR.'config.conf';
  60. $document_root = $_SERVER["DOCUMENT_ROOT"];
  61. $conf_dir = $_SERVER['ProgramFiles'].DIRECTORY_SEPARATOR.'freeswitch'.DIRECTORY_SEPARATOR.'conf';
  62. $sounds_dir = $_SERVER['ProgramFiles'].DIRECTORY_SEPARATOR.'freeswitch'.DIRECTORY_SEPARATOR.'sounds';
  63. $database_dir = $_SERVER['ProgramFiles'].DIRECTORY_SEPARATOR.'freeswitch'.DIRECTORY_SEPARATOR.'db';
  64. $recordings_dir = $_SERVER['ProgramFiles'].DIRECTORY_SEPARATOR.'freeswitch'.DIRECTORY_SEPARATOR.'recordings';
  65. $storage_dir = $_SERVER['ProgramFiles'].DIRECTORY_SEPARATOR.'freeswitch'.DIRECTORY_SEPARATOR.'storage';
  66. $voicemail_dir = $_SERVER['ProgramFiles'].DIRECTORY_SEPARATOR.'freeswitch'.DIRECTORY_SEPARATOR.'voicemail';
  67. $scripts_dir = $_SERVER['ProgramFiles'].DIRECTORY_SEPARATOR.'freeswitch'.DIRECTORY_SEPARATOR.'scripts';
  68. $php_dir = dirname(PHP_BINARY);
  69. $cache_location = dirname($_SERVER['DOCUMENT_ROOT']).DIRECTORY_SEPARATOR.'cache'.DIRECTORY_SEPARATOR.'fusionpbx';
  70. }
  71. //make the config directory
  72. if (isset($config_path)) {
  73. system('mkdir -p '.$config_path);
  74. }
  75. else {
  76. echo "config directory not found\n";
  77. exit;
  78. }
  79. //build the config file
  80. $conf = "\n";
  81. $conf .= "#database system settings\n";
  82. $conf .= "database.0.type = ".$db_type."\n";
  83. $conf .= "database.0.host = ".$db_host."\n";
  84. $conf .= "database.0.port = ".$db_port."\n";
  85. $conf .= "database.0.sslmode = prefer\n";
  86. $conf .= "database.0.name = ".$db_name."\n";
  87. $conf .= "database.0.username = ".$db_username."\n";
  88. $conf .= "database.0.password = ".$db_password."\n";
  89. $conf .= "\n";
  90. $conf .= "#database switch settings\n";
  91. $conf .= "database.1.type = sqlite\n";
  92. $conf .= "database.1.path = ".$database_dir."\n";
  93. $conf .= "database.1.name = core.db\n";
  94. $conf .= "\n";
  95. $conf .= "#general settings\n";
  96. $conf .= "document.root = ".$document_root."\n";
  97. $conf .= "project.path =\n";
  98. $conf .= "temp.dir = /tmp\n";
  99. $conf .= "php.dir = ".$php_dir."\n";
  100. $conf .= "php.bin = php\n";
  101. $conf .= "\n";
  102. $conf .= "#cache settings\n";
  103. $conf .= "cache.method = file\n";
  104. $conf .= "cache.location = ".$cache_location."\n";
  105. $conf .= "cache.settings = true\n";
  106. $conf .= "\n";
  107. $conf .= "#switch settings\n";
  108. $conf .= "switch.conf.dir = ".$conf_dir."\n";
  109. $conf .= "switch.sounds.dir = ".$sounds_dir."\n";
  110. $conf .= "switch.database.dir = ".$database_dir."\n";
  111. $conf .= "switch.recordings.dir = ".$recordings_dir."\n";
  112. $conf .= "switch.storage.dir = ".$storage_dir."\n";
  113. $conf .= "switch.voicemail.dir = ".$voicemail_dir."\n";
  114. $conf .= "switch.scripts.dir = ".$scripts_dir."\n";
  115. $conf .= "\n";
  116. $conf .= "#switch xml handler\n";
  117. $conf .= "xml_handler.fs_path = false\n";
  118. $conf .= "xml_handler.reg_as_number_alias = false\n";
  119. $conf .= "xml_handler.number_as_presence_id = true\n";
  120. $conf .= "\n";
  121. $conf .= "#error reporting hide show all errors except notices and warnings\n";
  122. $conf .= "error.reporting = 'E_ALL ^ E_NOTICE ^ E_WARNING'\n";
  123. //write the config file
  124. $file_handle = fopen($config_file,"w");
  125. if(!$file_handle){ return; }
  126. fwrite($file_handle, $conf);
  127. fclose($file_handle);
  128. }
  129. //include files
  130. require dirname(__DIR__, 2) . "/resources/require.php";
  131. //check the permission
  132. if(defined('STDIN')) {
  133. $display_type = 'text'; //html, text
  134. }
  135. else {
  136. require_once "resources/check_auth.php";
  137. if (permission_exists('upgrade_schema') || permission_exists('upgrade_source') || if_group("superadmin")) {
  138. //echo "access granted";
  139. }
  140. else {
  141. echo "access denied";
  142. exit;
  143. }
  144. $display_type = 'html'; //html, text
  145. }
  146. //set the default upgrade type
  147. $upgrade_type = 'defaults';
  148. //get the command line arguments
  149. if(defined('STDIN')) {
  150. //$application_name = $argv[0];
  151. if (isset($argv[1])) {
  152. $upgrade_type = $argv[1];
  153. }
  154. }
  155. //check for the upgrade menu option first
  156. if ($upgrade_type == 'menu') {
  157. require __DIR__ . '/upgrade_menu.php';
  158. exit();
  159. }
  160. //get the version of the software
  161. if ($upgrade_type == 'version') {
  162. echo software::version()."\n";
  163. }
  164. //run all app_defaults.php files
  165. if ($upgrade_type == 'domains') {
  166. require_once "resources/classes/config.php";
  167. require_once "resources/classes/domains.php";
  168. $domain = new domains;
  169. $domain->display_type = $display_type;
  170. $domain->upgrade();
  171. }
  172. //upgrade schema and/or data_types
  173. if ($upgrade_type == 'schema') {
  174. //get the database schema put it into an array then compare and update the database as needed.
  175. require_once "resources/classes/schema.php";
  176. $obj = new schema;
  177. if (isset($argv[2]) && $argv[2] == 'data_types') {
  178. $obj->data_types = true;
  179. }
  180. echo $obj->schema($format ?? '');
  181. }
  182. //restore the default menu
  183. if ($upgrade_type == 'menus') {
  184. //connect to the database
  185. $database = new database;
  186. //get the menu uuid and language
  187. $sql = "select menu_uuid, menu_language from v_menus ";
  188. $sql .= "where menu_name = :menu_name ";
  189. $parameters['menu_name'] = 'default';
  190. $row = $database->select($sql, $parameters, 'row');
  191. if (is_array($row) && sizeof($row) != 0) {
  192. $menu_uuid = $row["menu_uuid"];
  193. $menu_language = $row["menu_language"];
  194. }
  195. unset($sql, $parameters, $row);
  196. //show the menu
  197. if (isset($argv[2]) && $argv[2] == 'view') {
  198. print_r($_SESSION["menu"]);
  199. }
  200. //set the menu back to default
  201. if (!isset($argv[2]) || $argv[2] == 'default') {
  202. //restore the menu
  203. $included = true;
  204. require_once("core/menu/menu_restore_default.php");
  205. unset($sel_menu);
  206. //use upgrade language file
  207. $language = new text;
  208. $text = $language->get(null, 'core/upgrade');
  209. //send message to the console
  210. echo $text['message-upgrade_menu']."\n";
  211. }
  212. }
  213. //restore the default permissions
  214. if ($upgrade_type == 'permissions') {
  215. //default the groups in case they are missing
  216. (new groups())->defaults();
  217. //default the permissions
  218. $included = true;
  219. require_once("core/groups/permissions_default.php");
  220. //use upgrade language file
  221. $language = new text;
  222. $text = $language->get(null, 'core/upgrade');
  223. //send message to the console
  224. echo $text['message-upgrade_permissions']."\n";
  225. }
  226. //default upgrade schema and app defaults
  227. if ($upgrade_type == 'defaults') {
  228. //add multi-lingual support
  229. $language = new text;
  230. $text = $language->get(null, 'core/upgrade');
  231. //show the title
  232. if ($display_type == 'text') {
  233. echo "\n";
  234. echo $text['label-upgrade']."\n";
  235. echo "-----------------------------------------\n";
  236. echo "\n";
  237. echo $text['label-database']."\n";
  238. }
  239. //make sure the database schema and installation have performed all necessary tasks
  240. $obj = new schema;
  241. echo $obj->schema("text");
  242. //run all app_defaults.php files
  243. $domain = new domains;
  244. $domain->upgrade();
  245. //show the content
  246. if ($display_type == 'html') {
  247. echo "<div align='center'>\n";
  248. echo "<table width='40%'>\n";
  249. echo "<tr>\n";
  250. echo "<th align='left'>".$text['header-message']."</th>\n";
  251. echo "</tr>\n";
  252. echo "<tr>\n";
  253. echo "<td class='row_style1'><strong>".$text['message-upgrade']."</strong></td>\n";
  254. echo "</tr>\n";
  255. echo "</table>\n";
  256. echo "</div>\n";
  257. echo "<br />\n";
  258. echo "<br />\n";
  259. echo "<br />\n";
  260. echo "<br />\n";
  261. echo "<br />\n";
  262. echo "<br />\n";
  263. echo "<br />\n";
  264. }
  265. elseif ($display_type == 'text') {
  266. echo "\n";
  267. }
  268. //include the footer
  269. if ($display_type == "html") {
  270. require_once "resources/footer.php";
  271. }
  272. }
  273. //upgrade optional apps
  274. if ($upgrade_type == 'repos') {
  275. $app_list = git_find_repos($_SERVER["PROJECT_ROOT"]."/app");
  276. if (!is_array($app_list)) {
  277. exit;
  278. }
  279. print_r($app_list);exit;
  280. foreach ($app_list as $repo => $apps) {
  281. $path = $repo;
  282. $git_result = git_pull($path);
  283. foreach ($git_result['message'] as $response_line) {
  284. echo $repo . ": " . $response_line . "\n";
  285. }
  286. }
  287. }
  288. ?>