PageRenderTime 42ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/install/ajax/install.php

https://bitbucket.org/speedealing/speedealing
PHP | 385 lines | 255 code | 72 blank | 58 comment | 44 complexity | 643dc80e09f3ee7cc5c6f30bd0fbbf79 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2013 Regis Houssin <regis.houssin@capnetworks.com>
  3. * Copyright (C) 2013 Herve Prot <herve.prot@symeos.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/install/ajax/install.php
  20. * \brief File to get all status for install process
  21. */
  22. require '../inc.php';
  23. $default_lang = GETPOST('lang', 'alpha');
  24. $langs->setDefaultLang($default_lang);
  25. $langs->load("install");
  26. $langs->load("errors");
  27. $action = GETPOST('action', 'alpha');
  28. $out = array();
  29. /*
  30. * View
  31. */
  32. // For avoid error with destroy cookie
  33. ob_start();
  34. header('Content-type: application/json');
  35. // This variable are loaded by inc.php
  36. // $main_couchdb_host
  37. // $main_couchdb_port
  38. // Create config file
  39. if ($action == 'create_config') {
  40. $couchdb_host = GETPOST('couchdb_host', 'alpha');
  41. $couchdb_port = GETPOST('couchdb_port', 'int');
  42. $memcached_host = GETPOST('memcached_host', 'alpha');
  43. $memcached_port = GETPOST('memcached_port', 'int');
  44. // Save old conf file on disk
  45. if (file_exists("$conffile")) {
  46. // We must ignore errors as an existing old file may already exists and not be replacable or
  47. // the installer (like for ubuntu) may not have permission to create another file than conf.php.
  48. // Also no other process must be able to read file or we expose the new file, so content with password.
  49. @dol_copy($conffile, $conffile . '.old', '0600');
  50. }
  51. $ret = write_conf_file();
  52. if ($ret > 0)
  53. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('ConfFileCreated')));
  54. else
  55. echo json_encode(array('status' => 'error', 'value' => $langs->trans('ConfFileIsNotWritable', $conffile)));
  56. // Create sync user
  57. } else if ($action == 'create_syncuser') {
  58. $couchdb_user_sync = GETPOST('couchdb_user_sync', 'alpha');
  59. $couchdb_pass_sync = GETPOST('couchdb_pass_sync', 'alpha');
  60. // $main_couchdb_host
  61. // $main_couchdb_port
  62. sleep(1); // for test
  63. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('UserSyncCreated')));
  64. // Create system database
  65. } else if ($action == 'create_system_database') {
  66. $couchdb_name = 'system';
  67. $couch = new couchClient($main_couchdb_host . ':' . $main_couchdb_port . '/', $couchdb_name);
  68. if (!$couch->databaseExists()) {
  69. try {
  70. $couch->createDatabase();
  71. // Add role to the system database for security
  72. $admin = new couchAdmin($couch);
  73. $admin->addDatabaseReaderRole('speedealing');
  74. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('SystemDatabaseCreated')));
  75. } catch (Exception $e) {
  76. echo json_encode(array('status' => 'error', 'value' => $e->getMessage()));
  77. error_log($e->getMessage());
  78. }
  79. } else {
  80. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('WarningSystemDatabaseAlreadyExists'))); // system database already exists
  81. }
  82. // Create database
  83. } else if ($action == 'create_entity_database') {
  84. $couchdb_name = GETPOST('couchdb_name', 'alpha');
  85. $couch = new couchClient($main_couchdb_host . ':' . $main_couchdb_port . '/', $couchdb_name);
  86. if (!$couch->databaseExists()) {
  87. try {
  88. $couch->createDatabase();
  89. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('DatabaseCreated')));
  90. } catch (Exception $e) {
  91. echo json_encode(array('status' => 'error', 'value' => $e->getMessage()));
  92. error_log($e->getMessage());
  93. }
  94. } else {
  95. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('WarningDatabaseAlreadyExists'))); // database already exists
  96. }
  97. // Populate system database
  98. } else if ($action == 'populate_system_database' && !empty($jsonfiles)) {
  99. $errors = array();
  100. $return = array();
  101. $couchdb_name = GETPOST('couchdb_name', 'alpha');
  102. $couch = new couchClient($main_couchdb_host . ':' . $main_couchdb_port . '/', 'system');
  103. foreach($jsonfiles as $filename => $filepath) {
  104. $fp = fopen($filepath, "r");
  105. if ($fp) {
  106. $json = fread($fp, filesize($filepath));
  107. $obj = json_decode($json);
  108. unset($obj->_rev);
  109. if ($obj->_id == "const")
  110. unset($obj->MAIN_VERSION);
  111. try {
  112. $couch->storeDoc($obj);
  113. $return[] = $filename;
  114. } catch (Exception $e) {
  115. $error = 'File:' . $filename .' '. $e->getMessage();
  116. error_log($error);
  117. $errors[] = $error;
  118. }
  119. fclose($fp);
  120. } else {
  121. $error = "file not found : " . $filepath;
  122. error_log($error);
  123. $errors[] = $error;
  124. }
  125. }
  126. if (!empty($errors)) {
  127. echo json_encode(array('status' => 'error', 'value' => $errors));
  128. } else {
  129. echo json_encode(array('status' => 'ok', 'value' => $return));
  130. }
  131. // Create superadmin
  132. } else if ($action == 'create_admin') {
  133. $couchdb_name = GETPOST('couchdb_name', 'alpha');
  134. $couchdb_user_root = GETPOST('couchdb_user_root', 'alpha');
  135. $couchdb_pass_root = GETPOST('couchdb_pass_root', 'alpha');
  136. $couch = new couchClient($main_couchdb_host . ':' . $main_couchdb_port . '/', $couchdb_name);
  137. $admin = new couchAdmin($couch);
  138. try {
  139. // create couchdb admin user
  140. $admin->createAdmin($couchdb_user_root, $couchdb_pass_root);
  141. } catch (Exception $e) {
  142. // already exist or protected couchdb server
  143. }
  144. $host = substr($main_couchdb_host, 7);
  145. try {
  146. $couch = new couchClient('http://' . $couchdb_user_root . ':' . $couchdb_pass_root . '@' . $host . ':' . $main_couchdb_port . '/', $couchdb_name, array("cookie_auth" => TRUE));
  147. } catch (Exception $e) {
  148. error_log($e->getMessage());
  149. echo json_encode(array('status' => 'error', 'value' => $e->getMessage()));
  150. exit;
  151. }
  152. $found = false;
  153. try {
  154. $admin->getUser(trim($couchdb_user_root));
  155. $found = true;
  156. } catch (Exception $e) {
  157. // user not exit
  158. }
  159. if (!$found) {
  160. try {
  161. // create superadmin in system and _users databases
  162. $useradmin = new User();
  163. $useradmin->Lastname = "Admin";
  164. $useradmin->Firstname = "Admin";
  165. $useradmin->name = trim($couchdb_user_root);
  166. $useradmin->pass = trim($couchdb_pass_root);
  167. $useradmin->entity = $couchdb_name;
  168. $useradmin->admin = true;
  169. $useradmin->Status = 'ENABLE';
  170. $id = $useradmin->update("", 0, "install");
  171. } catch (Exception $e) {
  172. echo json_encode(array('status' => 'error', 'value' => $e->getMessage()));
  173. exit;
  174. }
  175. }
  176. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('AdminCreated')));
  177. // Create first user
  178. } else if ($action == 'create_user') {
  179. $couchdb_name = GETPOST('couchdb_name', 'alpha');
  180. $couchdb_user_root = GETPOST('couchdb_user_root', 'alpha');
  181. $couchdb_pass_root = GETPOST('couchdb_pass_root', 'alpha');
  182. $couchdb_user_firstname = GETPOST('couchdb_user_firstname', 'alpha');
  183. $couchdb_user_lastname = GETPOST('couchdb_user_lastname', 'alpha');
  184. $couchdb_user_login = GETPOST('couchdb_user_login', 'alpha');
  185. $couchdb_user_pass = GETPOST('couchdb_user_pass', 'alpha');
  186. $host = substr($main_couchdb_host, 7);
  187. try {
  188. $couch = new couchClient('http://' . $couchdb_user_root . ':' . $couchdb_pass_root . '@' . $host . ':' . $main_couchdb_port . '/', $couchdb_name, array("cookie_auth" => TRUE));
  189. $admin = new couchAdmin($couch);
  190. } catch (Exception $e) {
  191. error_log($e->getMessage());
  192. echo json_encode(array('status' => 'error', 'value' => $e->getMessage()));
  193. exit;
  194. }
  195. $found = false;
  196. try {
  197. $admin->getUser(trim($couchdb_user_login));
  198. $found = true;
  199. } catch (Exception $e) {
  200. // user not exit
  201. }
  202. if (!$found) {
  203. try {
  204. // create first user in system and _users databases
  205. $firstuser = new User();
  206. $firstuser->Lastname = trim($couchdb_user_lastname);
  207. $firstuser->Firstname = trim($couchdb_user_firstname);
  208. $firstuser->name = trim($couchdb_user_login);
  209. $firstuser->pass = trim($couchdb_user_pass);
  210. $firstuser->entity = $couchdb_name;
  211. $firstuser->admin = false;
  212. $firstuser->Status = 'DISABLE';
  213. $id = $firstuser->update("", 0, "add");
  214. } catch (Exception $e) {
  215. echo json_encode(array('status' => 'error', 'value' => $e->getMessage()));
  216. exit;
  217. }
  218. }
  219. // Add fisrt user to the database for security database
  220. $admin->addDatabaseReaderUser(trim($couchdb_user_login));
  221. // Add specific view in _users database
  222. $couch->useDatabase('_users');
  223. $filename = array(
  224. DOL_DOCUMENT_ROOT . "/user/json/_auth.view.json",
  225. DOL_DOCUMENT_ROOT . "/user/json/User.view.json"
  226. );
  227. foreach ($filename as $filepath) {
  228. $fp = fopen($filepath, "r");
  229. if ($fp) {
  230. $json = fread($fp, filesize($filepath));
  231. $obj = json_decode($json);
  232. unset($obj->_rev);
  233. try {
  234. $result = $couch->getDoc($obj->_id);
  235. $obj->_rev = $result->_rev;
  236. } catch (Exception $e) {
  237. // not exist
  238. }
  239. $couch->storeDoc($obj);
  240. fclose($fp);
  241. }
  242. }
  243. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('UserCreated')));
  244. // Create search engine user
  245. } else if ($action == 'create_searchengine_user') {
  246. $couchdb_name = GETPOST('couchdb_name', 'alpha');
  247. $couchdb_user_root = GETPOST('couchdb_user_root', 'alpha');
  248. $couchdb_pass_root = GETPOST('couchdb_pass_root', 'alpha');
  249. $couchdb_searchengine_login = GETPOST('couchdb_searchengine_login', 'alpha');
  250. $couchdb_searchengine_pass = GETPOST('couchdb_searchengine_pass', 'alpha');
  251. $host = substr($main_couchdb_host, 7);
  252. try {
  253. $couch = new couchClient('http://' . $couchdb_user_root . ':' . $couchdb_pass_root . '@' . $host . ':' . $main_couchdb_port . '/', $couchdb_name, array("cookie_auth" => TRUE));
  254. $admin = new couchAdmin($couch);
  255. } catch (Exception $e) {
  256. error_log($e->getMessage());
  257. echo json_encode(array('status' => 'error', 'value' => $e->getMessage()));
  258. exit;
  259. }
  260. $found = false;
  261. try {
  262. $admin->getUser(trim($couchdb_searchengine_login));
  263. $found = true;
  264. } catch (Exception $e) {
  265. // user not exit
  266. }
  267. if (!$found) {
  268. try {
  269. // create first user in system and _users databases
  270. $searchengine = new User();
  271. $searchengine->Lastname = 'Search';
  272. $searchengine->Firstname = 'Engine';
  273. $searchengine->name = trim($couchdb_searchengine_login);
  274. $searchengine->pass = trim($couchdb_searchengine_pass);
  275. $searchengine->admin = false;
  276. $searchengine->hide = true;
  277. $searchengine->Status = 'DISABLE';
  278. $id = $searchengine->update("", 0, "add");
  279. } catch (Exception $e) {
  280. echo json_encode(array('status' => 'error', 'value' => $e->getMessage()));
  281. exit;
  282. }
  283. }
  284. // Add search engine user to the database reader user
  285. $admin->addDatabaseReaderUser(trim($couchdb_searchengine_login));
  286. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('UserSearchEngineCreated')));
  287. // Install is finished, we create the lock file
  288. } else if ($action == 'lock_install') {
  289. $couchdb_name = GETPOST('couchdb_name', 'alpha');
  290. $couchdb_user_root = GETPOST('couchdb_user_root', 'alpha');
  291. $couchdb_pass_root = GETPOST('couchdb_pass_root', 'alpha');
  292. $host = substr($main_couchdb_host, 7);
  293. try {
  294. $couch = new couchClient('http://' . $couchdb_user_root . ':' . $couchdb_pass_root . '@' . $host . ':' . $main_couchdb_port . '/', $couchdb_name, array("cookie_auth" => TRUE));
  295. $admin = new couchAdmin($couch);
  296. } catch (Exception $e) {
  297. error_log($e->getMessage());
  298. echo json_encode(array('status' => 'error', 'value' => $e->getMessage()));
  299. exit;
  300. }
  301. // Increase timeout in couchdb to 3600s
  302. $admin->setConfig("couch_httpd_auth", "timeout", "3600");
  303. $ret = write_lock_file();
  304. if ($ret > 0)
  305. echo json_encode(array('status' => 'ok', 'value' => $langs->trans('LockFileCreated')));
  306. else
  307. echo json_encode(array('status' => 'error', 'value' => $langs->trans('LockFileCouldNotBeCreated')));
  308. // destroy couchdb cookie
  309. setcookie('AuthSession', '', 1, '/');
  310. }
  311. // For avoid error with destroy cookie
  312. ob_end_flush();
  313. ?>