setup.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878
  1. <?php
  2. class DatabaseSetupException extends Exception
  3. {
  4. private $hint;
  5. public function __construct($message, $hint, $code = 0, Exception $previous = null) {
  6. $this->hint = $hint;
  7. parent::__construct($message, $code, $previous);
  8. }
  9. public function __toString() {
  10. return __CLASS__ . ": [{$this->code}]: {$this->message} ({$this->hint})\n";
  11. }
  12. public function getHint() {
  13. return $this->hint;
  14. }
  15. }
  16. class OC_Setup {
  17. public static function getTrans(){
  18. return OC_L10N::get('lib');
  19. }
  20. public static function install($options) {
  21. $l = self::getTrans();
  22. $error = array();
  23. $dbtype = $options['dbtype'];
  24. if(empty($options['adminlogin'])) {
  25. $error[] = $l->t('Set an admin username.');
  26. }
  27. if(empty($options['adminpass'])) {
  28. $error[] = $l->t('Set an admin password.');
  29. }
  30. if(empty($options['directory'])) {
  31. $options['directory'] = OC::$SERVERROOT."/data";
  32. }
  33. if($dbtype == 'mysql' or $dbtype == 'pgsql' or $dbtype == 'oci' or $dbtype == 'mssql') { //mysql and postgresql needs more config options
  34. if($dbtype == 'mysql')
  35. $dbprettyname = 'MySQL';
  36. else if($dbtype == 'pgsql')
  37. $dbprettyname = 'PostgreSQL';
  38. else if ($dbtype == 'mssql')
  39. $dbprettyname = 'MS SQL Server';
  40. else
  41. $dbprettyname = 'Oracle';
  42. if(empty($options['dbuser'])) {
  43. $error[] = $l->t("%s enter the database username.", array($dbprettyname));
  44. }
  45. if(empty($options['dbname'])) {
  46. $error[] = $l->t("%s enter the database name.", array($dbprettyname));
  47. }
  48. if(substr_count($options['dbname'], '.') >= 1) {
  49. $error[] = $l->t("%s you may not use dots in the database name", array($dbprettyname));
  50. }
  51. if($dbtype != 'oci' && empty($options['dbhost'])) {
  52. $error[] = $l->t("%s set the database host.", array($dbprettyname));
  53. }
  54. }
  55. if(count($error) == 0) { //no errors, good
  56. $username = htmlspecialchars_decode($options['adminlogin']);
  57. $password = htmlspecialchars_decode($options['adminpass']);
  58. $datadir = htmlspecialchars_decode($options['directory']);
  59. if (OC_Util::runningOnWindows()) {
  60. $datadir = rtrim(realpath($datadir), '\\');
  61. }
  62. //use sqlite3 when available, otherise sqlite2 will be used.
  63. if($dbtype=='sqlite' and class_exists('SQLite3')) {
  64. $dbtype='sqlite3';
  65. }
  66. //generate a random salt that is used to salt the local user passwords
  67. $salt = OC_Util::generate_random_bytes(30);
  68. OC_Config::setValue('passwordsalt', $salt);
  69. //write the config file
  70. OC_Config::setValue('datadirectory', $datadir);
  71. OC_Config::setValue('dbtype', $dbtype);
  72. OC_Config::setValue('version', implode('.', OC_Util::getVersion()));
  73. if($dbtype == 'mysql') {
  74. $dbuser = $options['dbuser'];
  75. $dbpass = $options['dbpass'];
  76. $dbname = $options['dbname'];
  77. $dbhost = $options['dbhost'];
  78. $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_';
  79. OC_Config::setValue('dbname', $dbname);
  80. OC_Config::setValue('dbhost', $dbhost);
  81. OC_Config::setValue('dbtableprefix', $dbtableprefix);
  82. try {
  83. self::setupMySQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username);
  84. } catch (DatabaseSetupException $e) {
  85. $error[] = array(
  86. 'error' => $e->getMessage(),
  87. 'hint' => $e->getHint()
  88. );
  89. return($error);
  90. } catch (Exception $e) {
  91. $error[] = array(
  92. 'error' => $e->getMessage(),
  93. 'hint' => ''
  94. );
  95. return($error);
  96. }
  97. }
  98. elseif($dbtype == 'pgsql') {
  99. $dbuser = $options['dbuser'];
  100. $dbpass = $options['dbpass'];
  101. $dbname = $options['dbname'];
  102. $dbhost = $options['dbhost'];
  103. $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_';
  104. OC_Config::setValue('dbname', $dbname);
  105. OC_Config::setValue('dbhost', $dbhost);
  106. OC_Config::setValue('dbtableprefix', $dbtableprefix);
  107. try {
  108. self::setupPostgreSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username);
  109. } catch (Exception $e) {
  110. $error[] = array(
  111. 'error' => $l->t('PostgreSQL username and/or password not valid'),
  112. 'hint' => $l->t('You need to enter either an existing account or the administrator.')
  113. );
  114. return $error;
  115. }
  116. }
  117. elseif($dbtype == 'oci') {
  118. $dbuser = $options['dbuser'];
  119. $dbpass = $options['dbpass'];
  120. $dbname = $options['dbname'];
  121. $dbtablespace = $options['dbtablespace'];
  122. $dbhost = isset($options['dbhost'])?$options['dbhost']:'';
  123. $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_';
  124. OC_Config::setValue('dbname', $dbname);
  125. OC_Config::setValue('dbtablespace', $dbtablespace);
  126. OC_Config::setValue('dbhost', $dbhost);
  127. OC_Config::setValue('dbtableprefix', $dbtableprefix);
  128. try {
  129. self::setupOCIDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $dbtablespace, $username);
  130. } catch (Exception $e) {
  131. $error[] = array(
  132. 'error' => $l->t('Oracle connection could not be established'),
  133. 'hint' => $e->getMessage().' Check environment: ORACLE_HOME='.getenv('ORACLE_HOME')
  134. .' ORACLE_SID='.getenv('ORACLE_SID')
  135. .' LD_LIBRARY_PATH='.getenv('LD_LIBRARY_PATH')
  136. .' NLS_LANG='.getenv('NLS_LANG')
  137. .' tnsnames.ora is '.(is_readable(getenv('ORACLE_HOME').'/network/admin/tnsnames.ora')?'':'not ').'readable'
  138. );
  139. return $error;
  140. }
  141. }
  142. elseif ($dbtype == 'mssql') {
  143. $dbuser = $options['dbuser'];
  144. $dbpass = $options['dbpass'];
  145. $dbname = $options['dbname'];
  146. $dbhost = $options['dbhost'];
  147. $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_';
  148. OC_Config::setValue('dbname', $dbname);
  149. OC_Config::setValue('dbhost', $dbhost);
  150. OC_Config::setValue('dbuser', $dbuser);
  151. OC_Config::setValue('dbpassword', $dbpass);
  152. OC_Config::setValue('dbtableprefix', $dbtableprefix);
  153. try {
  154. self::setupMSSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix);
  155. } catch (Exception $e) {
  156. $error[] = array(
  157. 'error' => 'MS SQL username and/or password not valid',
  158. 'hint' => 'You need to enter either an existing account or the administrator.'
  159. );
  160. return $error;
  161. }
  162. }
  163. else {
  164. //delete the old sqlite database first, might cause infinte loops otherwise
  165. if(file_exists("$datadir/owncloud.db")) {
  166. unlink("$datadir/owncloud.db");
  167. }
  168. //in case of sqlite, we can always fill the database
  169. error_log("creating sqlite db");
  170. OC_DB::createDbFromStructure('db_structure.xml');
  171. }
  172. //create the user and group
  173. try {
  174. OC_User::createUser($username, $password);
  175. }
  176. catch(Exception $exception) {
  177. $error[] = 'Error while trying to create admin user: ' . $exception->getMessage();
  178. }
  179. if(count($error) == 0) {
  180. OC_Appconfig::setValue('core', 'installedat', microtime(true));
  181. OC_Appconfig::setValue('core', 'lastupdatedat', microtime(true));
  182. OC_AppConfig::setValue('core', 'remote_core.css', '/core/minimizer.php');
  183. OC_AppConfig::setValue('core', 'remote_core.js', '/core/minimizer.php');
  184. OC_Group::createGroup('admin');
  185. OC_Group::addToGroup($username, 'admin');
  186. OC_User::login($username, $password);
  187. //guess what this does
  188. OC_Installer::installShippedApps();
  189. //create htaccess files for apache hosts
  190. if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
  191. self::createHtaccess();
  192. }
  193. //and we are done
  194. OC_Config::setValue('installed', true);
  195. }
  196. }
  197. return $error;
  198. }
  199. private static function setupMySQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) {
  200. //check if the database user has admin right
  201. $l = self::getTrans();
  202. $connection = @mysql_connect($dbhost, $dbuser, $dbpass);
  203. if(!$connection) {
  204. throw new DatabaseSetupException($l->t('MySQL username and/or password not valid'),
  205. $l->t('You need to enter either an existing account or the administrator.'));
  206. }
  207. $oldUser=OC_Config::getValue('dbuser', false);
  208. //this should be enough to check for admin rights in mysql
  209. $query="SELECT user FROM mysql.user WHERE user='$dbuser'";
  210. if(mysql_query($query, $connection)) {
  211. //use the admin login data for the new database user
  212. //add prefix to the mysql user name to prevent collisions
  213. $dbusername=substr('oc_'.$username, 0, 16);
  214. if($dbusername!=$oldUser) {
  215. //hash the password so we don't need to store the admin config in the config file
  216. $dbpassword=OC_Util::generate_random_bytes(30);
  217. self::createDBUser($dbusername, $dbpassword, $connection);
  218. OC_Config::setValue('dbuser', $dbusername);
  219. OC_Config::setValue('dbpassword', $dbpassword);
  220. }
  221. //create the database
  222. self::createMySQLDatabase($dbname, $dbusername, $connection);
  223. }
  224. else {
  225. if($dbuser!=$oldUser) {
  226. OC_Config::setValue('dbuser', $dbuser);
  227. OC_Config::setValue('dbpassword', $dbpass);
  228. }
  229. //create the database
  230. self::createMySQLDatabase($dbname, $dbuser, $connection);
  231. }
  232. //fill the database if needed
  233. $query='select count(*) from information_schema.tables'
  234. ." where table_schema='$dbname' AND table_name = '{$dbtableprefix}users';";
  235. $result = mysql_query($query, $connection);
  236. if($result) {
  237. $row=mysql_fetch_row($result);
  238. }
  239. if(!$result or $row[0]==0) {
  240. OC_DB::createDbFromStructure('db_structure.xml');
  241. }
  242. mysql_close($connection);
  243. }
  244. private static function createMySQLDatabase($name, $user, $connection) {
  245. //we cant use OC_BD functions here because we need to connect as the administrative user.
  246. $l = self::getTrans();
  247. $query = "CREATE DATABASE IF NOT EXISTS `$name`";
  248. $result = mysql_query($query, $connection);
  249. if(!$result) {
  250. $entry = $l->t('DB Error: "%s"', array(mysql_error($connection))) . '<br />';
  251. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  252. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  253. }
  254. $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'";
  255. //this query will fail if there aren't the right permissions, ignore the error
  256. mysql_query($query, $connection);
  257. }
  258. private static function createDBUser($name, $password, $connection) {
  259. // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
  260. // the anonymous user would take precedence when there is one.
  261. $l = self::getTrans();
  262. $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
  263. $result = mysql_query($query, $connection);
  264. if (!$result) {
  265. throw new DatabaseSetupException($l->t("MySQL user '%s'@'localhost' exists already.",
  266. array($name)), $l->t("Drop this user from MySQL", array($name)));
  267. }
  268. $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
  269. $result = mysql_query($query, $connection);
  270. if (!$result) {
  271. throw new DatabaseSetupException($l->t("MySQL user '%s'@'%%' already exists", array($name)),
  272. $l->t("Drop this user from MySQL."));
  273. }
  274. }
  275. private static function setupPostgreSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) {
  276. $e_host = addslashes($dbhost);
  277. $e_user = addslashes($dbuser);
  278. $e_password = addslashes($dbpass);
  279. $l = self::getTrans();
  280. //check if the database user has admin rights
  281. $connection_string = "host='$e_host' dbname=postgres user='$e_user' password='$e_password'";
  282. $connection = @pg_connect($connection_string);
  283. if(!$connection) {
  284. throw new Exception($l->t('PostgreSQL username and/or password not valid'));
  285. }
  286. $e_user = pg_escape_string($dbuser);
  287. //check for roles creation rights in postgresql
  288. $query="SELECT 1 FROM pg_roles WHERE rolcreaterole=TRUE AND rolname='$e_user'";
  289. $result = pg_query($connection, $query);
  290. if($result and pg_num_rows($result) > 0) {
  291. //use the admin login data for the new database user
  292. //add prefix to the postgresql user name to prevent collisions
  293. $dbusername='oc_'.$username;
  294. //create a new password so we don't need to store the admin config in the config file
  295. $dbpassword=OC_Util::generate_random_bytes(30);
  296. self::pg_createDBUser($dbusername, $dbpassword, $connection);
  297. OC_Config::setValue('dbuser', $dbusername);
  298. OC_Config::setValue('dbpassword', $dbpassword);
  299. //create the database
  300. self::pg_createDatabase($dbname, $dbusername, $connection);
  301. }
  302. else {
  303. OC_Config::setValue('dbuser', $dbuser);
  304. OC_Config::setValue('dbpassword', $dbpass);
  305. //create the database
  306. self::pg_createDatabase($dbname, $dbuser, $connection);
  307. }
  308. // the connection to dbname=postgres is not needed anymore
  309. pg_close($connection);
  310. // connect to the ownCloud database (dbname=$dbname) and check if it needs to be filled
  311. $dbuser = OC_Config::getValue('dbuser');
  312. $dbpass = OC_Config::getValue('dbpassword');
  313. $e_host = addslashes($dbhost);
  314. $e_dbname = addslashes($dbname);
  315. $e_user = addslashes($dbuser);
  316. $e_password = addslashes($dbpass);
  317. $connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' password='$e_password'";
  318. $connection = @pg_connect($connection_string);
  319. if(!$connection) {
  320. throw new Exception($l->t('PostgreSQL username and/or password not valid'));
  321. }
  322. $query = "select count(*) FROM pg_class WHERE relname='{$dbtableprefix}users' limit 1";
  323. $result = pg_query($connection, $query);
  324. if($result) {
  325. $row = pg_fetch_row($result);
  326. }
  327. if(!$result or $row[0]==0) {
  328. OC_DB::createDbFromStructure('db_structure.xml');
  329. }
  330. }
  331. private static function pg_createDatabase($name, $user, $connection) {
  332. //we cant use OC_BD functions here because we need to connect as the administrative user.
  333. $l = self::getTrans();
  334. $e_name = pg_escape_string($name);
  335. $e_user = pg_escape_string($user);
  336. $query = "select datname from pg_database where datname = '$e_name'";
  337. $result = pg_query($connection, $query);
  338. if(!$result) {
  339. $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  340. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  341. \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
  342. }
  343. if(! pg_fetch_row($result)) {
  344. //The database does not exists... let's create it
  345. $query = "CREATE DATABASE \"$e_name\" OWNER \"$e_user\"";
  346. $result = pg_query($connection, $query);
  347. if(!$result) {
  348. $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  349. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  350. \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
  351. }
  352. else {
  353. $query = "REVOKE ALL PRIVILEGES ON DATABASE \"$e_name\" FROM PUBLIC";
  354. pg_query($connection, $query);
  355. }
  356. }
  357. }
  358. private static function pg_createDBUser($name, $password, $connection) {
  359. $l = self::getTrans();
  360. $e_name = pg_escape_string($name);
  361. $e_password = pg_escape_string($password);
  362. $query = "select * from pg_roles where rolname='$e_name';";
  363. $result = pg_query($connection, $query);
  364. if(!$result) {
  365. $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  366. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  367. \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
  368. }
  369. if(! pg_fetch_row($result)) {
  370. //user does not exists let's create it :)
  371. $query = "CREATE USER \"$e_name\" CREATEDB PASSWORD '$e_password';";
  372. $result = pg_query($connection, $query);
  373. if(!$result) {
  374. $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  375. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  376. \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
  377. }
  378. }
  379. else { // change password of the existing role
  380. $query = "ALTER ROLE \"$e_name\" WITH PASSWORD '$e_password';";
  381. $result = pg_query($connection, $query);
  382. if(!$result) {
  383. $entry = $l->t('DB Error: "%s"', array(pg_last_error($connection))) . '<br />';
  384. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  385. \OC_Log::write('setup.pg', $entry, \OC_Log::WARN);
  386. }
  387. }
  388. }
  389. private static function setupOCIDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $dbtablespace,
  390. $username) {
  391. $l = self::getTrans();
  392. $e_host = addslashes($dbhost);
  393. $e_dbname = addslashes($dbname);
  394. //check if the database user has admin right
  395. if ($e_host == '') {
  396. $easy_connect_string = $e_dbname; // use dbname as easy connect name
  397. } else {
  398. $easy_connect_string = '//'.$e_host.'/'.$e_dbname;
  399. }
  400. \OC_Log::write('setup oracle', 'connect string: ' . $easy_connect_string, \OC_Log::DEBUG);
  401. $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string);
  402. if(!$connection) {
  403. $e = oci_error();
  404. if (is_array ($e) && isset ($e['message'])) {
  405. throw new Exception($e['message']);
  406. }
  407. throw new Exception($l->t('Oracle username and/or password not valid'));
  408. }
  409. //check for roles creation rights in oracle
  410. $query='SELECT count(*) FROM user_role_privs, role_sys_privs'
  411. ." WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'";
  412. $stmt = oci_parse($connection, $query);
  413. if (!$stmt) {
  414. $entry = $l->t('DB Error: "%s"', array(oci_last_error($connection))) . '<br />';
  415. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  416. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  417. }
  418. $result = oci_execute($stmt);
  419. if($result) {
  420. $row = oci_fetch_row($stmt);
  421. }
  422. if($result and $row[0] > 0) {
  423. //use the admin login data for the new database user
  424. //add prefix to the oracle user name to prevent collisions
  425. $dbusername='oc_'.$username;
  426. //create a new password so we don't need to store the admin config in the config file
  427. $dbpassword=OC_Util::generate_random_bytes(30);
  428. //oracle passwords are treated as identifiers:
  429. // must start with aphanumeric char
  430. // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length.
  431. $dbpassword=substr($dbpassword, 0, 30);
  432. self::oci_createDBUser($dbusername, $dbpassword, $dbtablespace, $connection);
  433. OC_Config::setValue('dbuser', $dbusername);
  434. OC_Config::setValue('dbname', $dbusername);
  435. OC_Config::setValue('dbpassword', $dbpassword);
  436. //create the database not neccessary, oracle implies user = schema
  437. //self::oci_createDatabase($dbname, $dbusername, $connection);
  438. } else {
  439. OC_Config::setValue('dbuser', $dbuser);
  440. OC_Config::setValue('dbname', $dbname);
  441. OC_Config::setValue('dbpassword', $dbpass);
  442. //create the database not neccessary, oracle implies user = schema
  443. //self::oci_createDatabase($dbname, $dbuser, $connection);
  444. }
  445. //FIXME check tablespace exists: select * from user_tablespaces
  446. // the connection to dbname=oracle is not needed anymore
  447. oci_close($connection);
  448. // connect to the oracle database (schema=$dbuser) an check if the schema needs to be filled
  449. $dbuser = OC_Config::getValue('dbuser');
  450. //$dbname = OC_Config::getValue('dbname');
  451. $dbpass = OC_Config::getValue('dbpassword');
  452. $e_host = addslashes($dbhost);
  453. $e_dbname = addslashes($dbname);
  454. if ($e_host == '') {
  455. $easy_connect_string = $e_dbname; // use dbname as easy connect name
  456. } else {
  457. $easy_connect_string = '//'.$e_host.'/'.$e_dbname;
  458. }
  459. $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string);
  460. if(!$connection) {
  461. throw new Exception($l->t('Oracle username and/or password not valid'));
  462. }
  463. $query = "SELECT count(*) FROM user_tables WHERE table_name = :un";
  464. $stmt = oci_parse($connection, $query);
  465. $un = $dbtableprefix.'users';
  466. oci_bind_by_name($stmt, ':un', $un);
  467. if (!$stmt) {
  468. $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
  469. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  470. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  471. }
  472. $result = oci_execute($stmt);
  473. if($result) {
  474. $row = oci_fetch_row($stmt);
  475. }
  476. if(!$result or $row[0]==0) {
  477. OC_DB::createDbFromStructure('db_structure.xml');
  478. }
  479. }
  480. /**
  481. *
  482. * @param String $name
  483. * @param String $password
  484. * @param String $tablespace
  485. * @param resource $connection
  486. */
  487. private static function oci_createDBUser($name, $password, $tablespace, $connection) {
  488. $l = self::getTrans();
  489. $query = "SELECT * FROM all_users WHERE USERNAME = :un";
  490. $stmt = oci_parse($connection, $query);
  491. if (!$stmt) {
  492. $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
  493. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  494. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  495. }
  496. oci_bind_by_name($stmt, ':un', $name);
  497. $result = oci_execute($stmt);
  498. if(!$result) {
  499. $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
  500. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  501. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  502. }
  503. if(! oci_fetch_row($stmt)) {
  504. //user does not exists let's create it :)
  505. //password must start with alphabetic character in oracle
  506. $query = 'CREATE USER '.$name.' IDENTIFIED BY "'.$password.'" DEFAULT TABLESPACE '.$tablespace; //TODO set default tablespace
  507. $stmt = oci_parse($connection, $query);
  508. if (!$stmt) {
  509. $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
  510. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  511. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  512. }
  513. //oci_bind_by_name($stmt, ':un', $name);
  514. $result = oci_execute($stmt);
  515. if(!$result) {
  516. $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
  517. $entry .= $l->t('Offending command was: "%s", name: %s, password: %s',
  518. array($query, $name, $password)) . '<br />';
  519. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  520. }
  521. } else { // change password of the existing role
  522. $query = "ALTER USER :un IDENTIFIED BY :pw";
  523. $stmt = oci_parse($connection, $query);
  524. if (!$stmt) {
  525. $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
  526. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  527. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  528. }
  529. oci_bind_by_name($stmt, ':un', $name);
  530. oci_bind_by_name($stmt, ':pw', $password);
  531. $result = oci_execute($stmt);
  532. if(!$result) {
  533. $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
  534. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  535. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  536. }
  537. }
  538. // grant necessary roles
  539. $query = 'GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, CREATE TRIGGER, UNLIMITED TABLESPACE TO '.$name;
  540. $stmt = oci_parse($connection, $query);
  541. if (!$stmt) {
  542. $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
  543. $entry .= $l->t('Offending command was: "%s"', array($query)) . '<br />';
  544. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  545. }
  546. $result = oci_execute($stmt);
  547. if(!$result) {
  548. $entry = $l->t('DB Error: "%s"', array(oci_error($connection))) . '<br />';
  549. $entry .= $l->t('Offending command was: "%s", name: %s, password: %s',
  550. array($query, $name, $password)) . '<br />';
  551. \OC_Log::write('setup.oci', $entry, \OC_Log::WARN);
  552. }
  553. }
  554. private static function setupMSSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix) {
  555. $l = self::getTrans();
  556. //check if the database user has admin right
  557. $masterConnectionInfo = array( "Database" => "master", "UID" => $dbuser, "PWD" => $dbpass);
  558. $masterConnection = @sqlsrv_connect($dbhost, $masterConnectionInfo);
  559. if(!$masterConnection) {
  560. $entry = null;
  561. if( ($errors = sqlsrv_errors() ) != null) {
  562. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  563. } else {
  564. $entry = '';
  565. }
  566. throw new Exception($l->t('MS SQL username and/or password not valid: %s', array($entry)));
  567. }
  568. OC_Config::setValue('dbuser', $dbuser);
  569. OC_Config::setValue('dbpassword', $dbpass);
  570. self::mssql_createDBLogin($dbuser, $dbpass, $masterConnection);
  571. self::mssql_createDatabase($dbname, $masterConnection);
  572. self::mssql_createDBUser($dbuser, $dbname, $masterConnection);
  573. sqlsrv_close($masterConnection);
  574. self::mssql_createDatabaseStructure($dbhost, $dbname, $dbuser, $dbpass, $dbtableprefix);
  575. }
  576. private static function mssql_createDBLogin($name, $password, $connection) {
  577. $query = "SELECT * FROM master.sys.server_principals WHERE name = '".$name."';";
  578. $result = sqlsrv_query($connection, $query);
  579. if ($result === false) {
  580. if ( ($errors = sqlsrv_errors() ) != null) {
  581. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  582. } else {
  583. $entry = '';
  584. }
  585. $entry.='Offending command was: '.$query.'<br />';
  586. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  587. } else {
  588. $row = sqlsrv_fetch_array($result);
  589. if ($row === false) {
  590. if ( ($errors = sqlsrv_errors() ) != null) {
  591. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  592. } else {
  593. $entry = '';
  594. }
  595. $entry.='Offending command was: '.$query.'<br />';
  596. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  597. } else {
  598. if ($row == null) {
  599. $query = "CREATE LOGIN [".$name."] WITH PASSWORD = '".$password."';";
  600. $result = sqlsrv_query($connection, $query);
  601. if (!$result or $result === false) {
  602. if ( ($errors = sqlsrv_errors() ) != null) {
  603. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  604. } else {
  605. $entry = '';
  606. }
  607. $entry.='Offending command was: '.$query.'<br />';
  608. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  609. }
  610. }
  611. }
  612. }
  613. }
  614. private static function mssql_createDBUser($name, $dbname, $connection) {
  615. $query = "SELECT * FROM [".$dbname."].sys.database_principals WHERE name = '".$name."';";
  616. $result = sqlsrv_query($connection, $query);
  617. if ($result === false) {
  618. if ( ($errors = sqlsrv_errors() ) != null) {
  619. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  620. } else {
  621. $entry = '';
  622. }
  623. $entry.='Offending command was: '.$query.'<br />';
  624. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  625. } else {
  626. $row = sqlsrv_fetch_array($result);
  627. if ($row === false) {
  628. if ( ($errors = sqlsrv_errors() ) != null) {
  629. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  630. } else {
  631. $entry = '';
  632. }
  633. $entry.='Offending command was: '.$query.'<br />';
  634. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  635. } else {
  636. if ($row == null) {
  637. $query = "USE [".$dbname."]; CREATE USER [".$name."] FOR LOGIN [".$name."];";
  638. $result = sqlsrv_query($connection, $query);
  639. if (!$result || $result === false) {
  640. if ( ($errors = sqlsrv_errors() ) != null) {
  641. $entry = 'DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  642. } else {
  643. $entry = '';
  644. }
  645. $entry.='Offending command was: '.$query.'<br />';
  646. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  647. }
  648. }
  649. $query = "USE [".$dbname."]; EXEC sp_addrolemember 'db_owner', '".$name."';";
  650. $result = sqlsrv_query($connection, $query);
  651. if (!$result || $result === false) {
  652. if ( ($errors = sqlsrv_errors() ) != null) {
  653. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  654. } else {
  655. $entry = '';
  656. }
  657. $entry.='Offending command was: '.$query.'<br />';
  658. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  659. }
  660. }
  661. }
  662. }
  663. private static function mssql_createDatabase($dbname, $connection) {
  664. $query = "CREATE DATABASE [".$dbname."];";
  665. $result = sqlsrv_query($connection, $query);
  666. if (!$result || $result === false) {
  667. if ( ($errors = sqlsrv_errors() ) != null) {
  668. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  669. } else {
  670. $entry = '';
  671. }
  672. $entry.='Offending command was: '.$query.'<br />';
  673. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  674. }
  675. }
  676. private static function mssql_createDatabaseStructure($dbhost, $dbname, $dbuser, $dbpass, $dbtableprefix) {
  677. $connectionInfo = array( "Database" => $dbname, "UID" => $dbuser, "PWD" => $dbpass);
  678. $connection = @sqlsrv_connect($dbhost, $connectionInfo);
  679. //fill the database if needed
  680. $query = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '{$dbname}' AND TABLE_NAME = '{$dbtableprefix}users'";
  681. $result = sqlsrv_query($connection, $query);
  682. if ($result === false) {
  683. if ( ($errors = sqlsrv_errors() ) != null) {
  684. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  685. } else {
  686. $entry = '';
  687. }
  688. $entry.='Offending command was: '.$query.'<br />';
  689. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  690. } else {
  691. $row = sqlsrv_fetch_array($result);
  692. if ($row === false) {
  693. if ( ($errors = sqlsrv_errors() ) != null) {
  694. $entry='DB Error: "'.print_r(sqlsrv_errors()).'"<br />';
  695. } else {
  696. $entry = '';
  697. }
  698. $entry.='Offending command was: '.$query.'<br />';
  699. \OC_Log::write('setup.mssql', $entry, \OC_Log::WARN);
  700. } else {
  701. if ($row == null) {
  702. OC_DB::createDbFromStructure('db_structure.xml');
  703. }
  704. }
  705. }
  706. sqlsrv_close($connection);
  707. }
  708. /**
  709. * create .htaccess files for apache hosts
  710. */
  711. private static function createHtaccess() {
  712. $content = "<IfModule mod_fcgid.c>\n";
  713. $content.= "<IfModule mod_setenvif.c>\n";
  714. $content.= "<IfModule mod_headers.c>\n";
  715. $content.= "SetEnvIfNoCase ^Authorization$ \"(.+)\" XAUTHORIZATION=$1\n";
  716. $content.= "RequestHeader set XAuthorization %{XAUTHORIZATION}e env=XAUTHORIZATION\n";
  717. $content.= "</IfModule>\n";
  718. $content.= "</IfModule>\n";
  719. $content.= "</IfModule>\n";
  720. $content.= "ErrorDocument 403 ".OC::$WEBROOT."/core/templates/403.php\n";//custom 403 error page
  721. $content.= "ErrorDocument 404 ".OC::$WEBROOT."/core/templates/404.php\n";//custom 404 error page
  722. $content.= "<IfModule mod_php5.c>\n";
  723. $content.= "php_value upload_max_filesize 512M\n";//upload limit
  724. $content.= "php_value post_max_size 512M\n";
  725. $content.= "php_value memory_limit 512M\n";
  726. $content.= "php_value mbstring.func_overload 0\n";
  727. $content.= "<IfModule env_module>\n";
  728. $content.= " SetEnv htaccessWorking true\n";
  729. $content.= "</IfModule>\n";
  730. $content.= "</IfModule>\n";
  731. $content.= "<IfModule mod_rewrite.c>\n";
  732. $content.= "RewriteEngine on\n";
  733. $content.= "RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]\n";
  734. $content.= "RewriteRule ^.well-known/host-meta /public.php?service=host-meta [QSA,L]\n";
  735. $content.= "RewriteRule ^.well-known/carddav /remote.php/carddav/ [R]\n";
  736. $content.= "RewriteRule ^.well-known/caldav /remote.php/caldav/ [R]\n";
  737. $content.= "RewriteRule ^apps/([^/]*)/(.*\.(css|php))$ index.php?app=$1&getfile=$2 [QSA,L]\n";
  738. $content.= "RewriteRule ^remote/(.*) remote.php [QSA,L]\n";
  739. $content.= "</IfModule>\n";
  740. $content.= "<IfModule mod_mime.c>\n";
  741. $content.= "AddType image/svg+xml svg svgz\n";
  742. $content.= "AddEncoding gzip svgz\n";
  743. $content.= "</IfModule>\n";
  744. $content.= "<IfModule dir_module>\n";
  745. $content.= "DirectoryIndex index.php index.html\n";
  746. $content.= "</IfModule>\n";
  747. $content.= "AddDefaultCharset utf-8\n";
  748. $content.= "Options -Indexes\n";
  749. @file_put_contents(OC::$SERVERROOT.'/.htaccess', $content); //supress errors in case we don't have permissions for it
  750. self::protectDataDirectory();
  751. }
  752. public static function protectDataDirectory() {
  753. $content = "deny from all\n";
  754. $content.= "IndexIgnore *";
  755. file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/.htaccess', $content);
  756. file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/index.html', '');
  757. }
  758. /**
  759. * @brief Post installation checks
  760. */
  761. public static function postSetupCheck($params) {
  762. // setup was successful -> webdav testing now
  763. $l = self::getTrans();
  764. if (OC_Util::isWebDAVWorking()) {
  765. header("Location: ".OC::$WEBROOT.'/');
  766. } else {
  767. $error = $l->t('Your web server is not yet properly setup to allow files synchronization because the WebDAV interface seems to be broken.');
  768. $hint = $l->t('Please double check the <a href=\'%s\'>installation guides</a>.',
  769. 'http://doc.owncloud.org/server/5.0/admin_manual/installation.html');
  770. $tmpl = new OC_Template('', 'error', 'guest');
  771. $tmpl->assign('errors', array(1 => array('error' => $error, 'hint' => $hint)));
  772. $tmpl->printPage();
  773. exit();
  774. }
  775. }
  776. }