You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setup.php 22KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 install($options) {
  18. $error = array();
  19. $dbtype = $options['dbtype'];
  20. if(empty($options['adminlogin'])) {
  21. $error[] = 'Set an admin username.';
  22. }
  23. if(empty($options['adminpass'])) {
  24. $error[] = 'Set an admin password.';
  25. }
  26. if(empty($options['directory'])) {
  27. $error[] = 'Specify a data folder.';
  28. }
  29. if($dbtype=='mysql' or $dbtype == 'pgsql' or $dbtype == 'oci') { //mysql and postgresql needs more config options
  30. if($dbtype=='mysql')
  31. $dbprettyname = 'MySQL';
  32. else if($dbtype=='pgsql')
  33. $dbprettyname = 'PostgreSQL';
  34. else
  35. $dbprettyname = 'Oracle';
  36. if(empty($options['dbuser'])) {
  37. $error[] = "$dbprettyname enter the database username.";
  38. }
  39. if(empty($options['dbname'])) {
  40. $error[] = "$dbprettyname enter the database name.";
  41. }
  42. if(substr_count($options['dbname'], '.') >= 1) {
  43. $error[] = "$dbprettyname you may not use dots in the database name";
  44. }
  45. if($dbtype != 'oci' && empty($options['dbhost'])) {
  46. $error[] = "$dbprettyname set the database host.";
  47. }
  48. }
  49. if(count($error) == 0) { //no errors, good
  50. $username = htmlspecialchars_decode($options['adminlogin']);
  51. $password = htmlspecialchars_decode($options['adminpass']);
  52. $datadir = htmlspecialchars_decode($options['directory']);
  53. //use sqlite3 when available, otherise sqlite2 will be used.
  54. if($dbtype=='sqlite' and class_exists('SQLite3')) {
  55. $dbtype='sqlite3';
  56. }
  57. //generate a random salt that is used to salt the local user passwords
  58. $salt = OC_Util::generate_random_bytes(30);
  59. OC_Config::setValue('passwordsalt', $salt);
  60. //write the config file
  61. OC_Config::setValue('datadirectory', $datadir);
  62. OC_Config::setValue('dbtype', $dbtype);
  63. OC_Config::setValue('version', implode('.', OC_Util::getVersion()));
  64. if($dbtype == 'mysql') {
  65. $dbuser = $options['dbuser'];
  66. $dbpass = $options['dbpass'];
  67. $dbname = $options['dbname'];
  68. $dbhost = $options['dbhost'];
  69. $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_';
  70. OC_Config::setValue('dbname', $dbname);
  71. OC_Config::setValue('dbhost', $dbhost);
  72. OC_Config::setValue('dbtableprefix', $dbtableprefix);
  73. try {
  74. self::setupMySQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username);
  75. } catch (DatabaseSetupException $e) {
  76. $error[] = array(
  77. 'error' => $e->getMessage(),
  78. 'hint' => $e->getHint()
  79. );
  80. return($error);
  81. } catch (Exception $e) {
  82. $error[] = array(
  83. 'error' => $e->getMessage(),
  84. 'hint' => ''
  85. );
  86. return($error);
  87. }
  88. }
  89. elseif($dbtype == 'pgsql') {
  90. $dbuser = $options['dbuser'];
  91. $dbpass = $options['dbpass'];
  92. $dbname = $options['dbname'];
  93. $dbhost = $options['dbhost'];
  94. $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_';
  95. OC_Config::setValue('dbname', $dbname);
  96. OC_Config::setValue('dbhost', $dbhost);
  97. OC_Config::setValue('dbtableprefix', $dbtableprefix);
  98. try {
  99. self::setupPostgreSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username);
  100. } catch (Exception $e) {
  101. $error[] = array(
  102. 'error' => 'PostgreSQL username and/or password not valid',
  103. 'hint' => 'You need to enter either an existing account or the administrator.'
  104. );
  105. return $error;
  106. }
  107. }
  108. elseif($dbtype == 'oci') {
  109. $dbuser = $options['dbuser'];
  110. $dbpass = $options['dbpass'];
  111. $dbname = $options['dbname'];
  112. $dbtablespace = $options['dbtablespace'];
  113. $dbhost = isset($options['dbhost'])?$options['dbhost']:'';
  114. $dbtableprefix = isset($options['dbtableprefix']) ? $options['dbtableprefix'] : 'oc_';
  115. OC_Config::setValue('dbname', $dbname);
  116. OC_Config::setValue('dbtablespace', $dbtablespace);
  117. OC_Config::setValue('dbhost', $dbhost);
  118. OC_Config::setValue('dbtableprefix', $dbtableprefix);
  119. try {
  120. self::setupOCIDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $dbtablespace, $username);
  121. } catch (Exception $e) {
  122. $error[] = array(
  123. 'error' => 'Oracle username and/or password not valid',
  124. 'hint' => 'You need to enter either an existing account or the administrator.'
  125. );
  126. return $error;
  127. }
  128. }
  129. else {
  130. //delete the old sqlite database first, might cause infinte loops otherwise
  131. if(file_exists("$datadir/owncloud.db")) {
  132. unlink("$datadir/owncloud.db");
  133. }
  134. //in case of sqlite, we can always fill the database
  135. OC_DB::createDbFromStructure('db_structure.xml');
  136. }
  137. //create the user and group
  138. try {
  139. OC_User::createUser($username, $password);
  140. }
  141. catch(Exception $exception) {
  142. $error[] = $exception->getMessage();
  143. }
  144. if(count($error) == 0) {
  145. OC_Appconfig::setValue('core', 'installedat', microtime(true));
  146. OC_Appconfig::setValue('core', 'lastupdatedat', microtime(true));
  147. OC_AppConfig::setValue('core', 'remote_core.css', '/core/minimizer.php');
  148. OC_AppConfig::setValue('core', 'remote_core.js', '/core/minimizer.php');
  149. OC_Group::createGroup('admin');
  150. OC_Group::addToGroup($username, 'admin');
  151. OC_User::login($username, $password);
  152. //guess what this does
  153. OC_Installer::installShippedApps();
  154. //create htaccess files for apache hosts
  155. if (isset($_SERVER['SERVER_SOFTWARE']) && strstr($_SERVER['SERVER_SOFTWARE'], 'Apache')) {
  156. self::createHtaccess();
  157. }
  158. //and we are done
  159. OC_Config::setValue('installed', true);
  160. }
  161. }
  162. return $error;
  163. }
  164. private static function setupMySQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) {
  165. //check if the database user has admin right
  166. $connection = @mysql_connect($dbhost, $dbuser, $dbpass);
  167. if(!$connection) {
  168. throw new DatabaseSetupException('MySQL username and/or password not valid','You need to enter either an existing account or the administrator.');
  169. }
  170. $oldUser=OC_Config::getValue('dbuser', false);
  171. $query="SELECT user FROM mysql.user WHERE user='$dbuser'"; //this should be enough to check for admin rights in mysql
  172. if(mysql_query($query, $connection)) {
  173. //use the admin login data for the new database user
  174. //add prefix to the mysql user name to prevent collisions
  175. $dbusername=substr('oc_'.$username, 0, 16);
  176. if($dbusername!=$oldUser) {
  177. //hash the password so we don't need to store the admin config in the config file
  178. $dbpassword=md5(time().$dbpass);
  179. self::createDBUser($dbusername, $dbpassword, $connection);
  180. OC_Config::setValue('dbuser', $dbusername);
  181. OC_Config::setValue('dbpassword', $dbpassword);
  182. }
  183. //create the database
  184. self::createMySQLDatabase($dbname, $dbusername, $connection);
  185. }
  186. else {
  187. if($dbuser!=$oldUser) {
  188. OC_Config::setValue('dbuser', $dbuser);
  189. OC_Config::setValue('dbpassword', $dbpass);
  190. }
  191. //create the database
  192. self::createMySQLDatabase($dbname, $dbuser, $connection);
  193. }
  194. //fill the database if needed
  195. $query="select count(*) from information_schema.tables where table_schema='$dbname' AND table_name = '{$dbtableprefix}users';";
  196. $result = mysql_query($query, $connection);
  197. if($result) {
  198. $row=mysql_fetch_row($result);
  199. }
  200. if(!$result or $row[0]==0) {
  201. OC_DB::createDbFromStructure('db_structure.xml');
  202. }
  203. mysql_close($connection);
  204. }
  205. private static function createMySQLDatabase($name, $user, $connection) {
  206. //we cant use OC_BD functions here because we need to connect as the administrative user.
  207. $query = "CREATE DATABASE IF NOT EXISTS `$name`";
  208. $result = mysql_query($query, $connection);
  209. if(!$result) {
  210. $entry='DB Error: "'.mysql_error($connection).'"<br />';
  211. $entry.='Offending command was: '.$query.'<br />';
  212. echo($entry);
  213. }
  214. $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'";
  215. $result = mysql_query($query, $connection); //this query will fail if there aren't the right permissons, ignore the error
  216. }
  217. private static function createDBUser($name, $password, $connection) {
  218. // we need to create 2 accounts, one for global use and one for local user. if we don't specify the local one,
  219. // the anonymous user would take precedence when there is one.
  220. $query = "CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
  221. $result = mysql_query($query, $connection);
  222. if (!$result) {
  223. throw new DatabaseSetupException("MySQL user '" . "$name" . "'@'localhost' already exists","Delete this user from MySQL.");
  224. }
  225. $query = "CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
  226. $result = mysql_query($query, $connection);
  227. if (!$result) {
  228. throw new DatabaseSetupException("MySQL user '" . "$name" . "'@'%' already exists","Delete this user from MySQL.");
  229. }
  230. }
  231. private static function setupPostgreSQLDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $username) {
  232. $e_host = addslashes($dbhost);
  233. $e_user = addslashes($dbuser);
  234. $e_password = addslashes($dbpass);
  235. //check if the database user has admin rights
  236. $connection_string = "host='$e_host' dbname=postgres user='$e_user' password='$e_password'";
  237. $connection = @pg_connect($connection_string);
  238. if(!$connection) {
  239. throw new Exception('PostgreSQL username and/or password not valid');
  240. }
  241. $e_user = pg_escape_string($dbuser);
  242. //check for roles creation rights in postgresql
  243. $query="SELECT 1 FROM pg_roles WHERE rolcreaterole=TRUE AND rolname='$e_user'";
  244. $result = pg_query($connection, $query);
  245. if($result and pg_num_rows($result) > 0) {
  246. //use the admin login data for the new database user
  247. //add prefix to the postgresql user name to prevent collisions
  248. $dbusername='oc_'.$username;
  249. //create a new password so we don't need to store the admin config in the config file
  250. $dbpassword=md5(time());
  251. self::pg_createDBUser($dbusername, $dbpassword, $connection);
  252. OC_Config::setValue('dbuser', $dbusername);
  253. OC_Config::setValue('dbpassword', $dbpassword);
  254. //create the database
  255. self::pg_createDatabase($dbname, $dbusername, $connection);
  256. }
  257. else {
  258. OC_Config::setValue('dbuser', $dbuser);
  259. OC_Config::setValue('dbpassword', $dbpass);
  260. //create the database
  261. self::pg_createDatabase($dbname, $dbuser, $connection);
  262. }
  263. // the connection to dbname=postgres is not needed anymore
  264. pg_close($connection);
  265. // connect to the ownCloud database (dbname=$dbname) and check if it needs to be filled
  266. $dbuser = OC_Config::getValue('dbuser');
  267. $dbpass = OC_Config::getValue('dbpassword');
  268. $e_host = addslashes($dbhost);
  269. $e_dbname = addslashes($dbname);
  270. $e_user = addslashes($dbuser);
  271. $e_password = addslashes($dbpass);
  272. $connection_string = "host='$e_host' dbname='$e_dbname' user='$e_user' password='$e_password'";
  273. $connection = @pg_connect($connection_string);
  274. if(!$connection) {
  275. throw new Exception('PostgreSQL username and/or password not valid');
  276. }
  277. $query = "select count(*) FROM pg_class WHERE relname='{$dbtableprefix}users' limit 1";
  278. $result = pg_query($connection, $query);
  279. if($result) {
  280. $row = pg_fetch_row($result);
  281. }
  282. if(!$result or $row[0]==0) {
  283. OC_DB::createDbFromStructure('db_structure.xml');
  284. }
  285. }
  286. private static function pg_createDatabase($name, $user, $connection) {
  287. //we cant use OC_BD functions here because we need to connect as the administrative user.
  288. $e_name = pg_escape_string($name);
  289. $e_user = pg_escape_string($user);
  290. $query = "select datname from pg_database where datname = '$e_name'";
  291. $result = pg_query($connection, $query);
  292. if(!$result) {
  293. $entry='DB Error: "'.pg_last_error($connection).'"<br />';
  294. $entry.='Offending command was: '.$query.'<br />';
  295. echo($entry);
  296. }
  297. if(! pg_fetch_row($result)) {
  298. //The database does not exists... let's create it
  299. $query = "CREATE DATABASE \"$e_name\" OWNER \"$e_user\"";
  300. $result = pg_query($connection, $query);
  301. if(!$result) {
  302. $entry='DB Error: "'.pg_last_error($connection).'"<br />';
  303. $entry.='Offending command was: '.$query.'<br />';
  304. echo($entry);
  305. }
  306. else {
  307. $query = "REVOKE ALL PRIVILEGES ON DATABASE \"$e_name\" FROM PUBLIC";
  308. $result = pg_query($connection, $query);
  309. }
  310. }
  311. }
  312. private static function pg_createDBUser($name, $password, $connection) {
  313. $e_name = pg_escape_string($name);
  314. $e_password = pg_escape_string($password);
  315. $query = "select * from pg_roles where rolname='$e_name';";
  316. $result = pg_query($connection, $query);
  317. if(!$result) {
  318. $entry='DB Error: "'.pg_last_error($connection).'"<br />';
  319. $entry.='Offending command was: '.$query.'<br />';
  320. echo($entry);
  321. }
  322. if(! pg_fetch_row($result)) {
  323. //user does not exists let's create it :)
  324. $query = "CREATE USER \"$e_name\" CREATEDB PASSWORD '$e_password';";
  325. $result = pg_query($connection, $query);
  326. if(!$result) {
  327. $entry='DB Error: "'.pg_last_error($connection).'"<br />';
  328. $entry.='Offending command was: '.$query.'<br />';
  329. echo($entry);
  330. }
  331. }
  332. else { // change password of the existing role
  333. $query = "ALTER ROLE \"$e_name\" WITH PASSWORD '$e_password';";
  334. $result = pg_query($connection, $query);
  335. if(!$result) {
  336. $entry='DB Error: "'.pg_last_error($connection).'"<br />';
  337. $entry.='Offending command was: '.$query.'<br />';
  338. echo($entry);
  339. }
  340. }
  341. }
  342. private static function setupOCIDatabase($dbhost, $dbuser, $dbpass, $dbname, $dbtableprefix, $dbtablespace, $username) {
  343. $e_host = addslashes($dbhost);
  344. $e_dbname = addslashes($dbname);
  345. //check if the database user has admin right
  346. if ($e_host == '') {
  347. $easy_connect_string = $e_dbname; // use dbname as easy connect name
  348. } else {
  349. $easy_connect_string = '//'.$e_host.'/'.$e_dbname;
  350. }
  351. $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string);
  352. if(!$connection) {
  353. $e = oci_error();
  354. throw new Exception('Oracle username and/or password not valid');
  355. }
  356. //check for roles creation rights in oracle
  357. $query="SELECT count(*) FROM user_role_privs, role_sys_privs WHERE user_role_privs.granted_role = role_sys_privs.role AND privilege = 'CREATE ROLE'";
  358. $stmt = oci_parse($connection, $query);
  359. if (!$stmt) {
  360. $entry='DB Error: "'.oci_last_error($connection).'"<br />';
  361. $entry.='Offending command was: '.$query.'<br />';
  362. echo($entry);
  363. }
  364. $result = oci_execute($stmt);
  365. if($result) {
  366. $row = oci_fetch_row($stmt);
  367. }
  368. if($result and $row[0] > 0) {
  369. //use the admin login data for the new database user
  370. //add prefix to the oracle user name to prevent collisions
  371. $dbusername='oc_'.$username;
  372. //create a new password so we don't need to store the admin config in the config file
  373. $dbpassword=md5(time().$dbpass);
  374. //oracle passwords are treated as identifiers:
  375. // must start with aphanumeric char
  376. // needs to be shortened to 30 bytes, as the two " needed to escape the identifier count towards the identifier length.
  377. $dbpassword=substr($dbpassword, 0, 30);
  378. self::oci_createDBUser($dbusername, $dbpassword, $dbtablespace, $connection);
  379. OC_Config::setValue('dbuser', $dbusername);
  380. OC_Config::setValue('dbname', $dbusername);
  381. OC_Config::setValue('dbpassword', $dbpassword);
  382. //create the database not neccessary, oracle implies user = schema
  383. //self::oci_createDatabase($dbname, $dbusername, $connection);
  384. } else {
  385. OC_Config::setValue('dbuser', $dbuser);
  386. OC_Config::setValue('dbname', $dbname);
  387. OC_Config::setValue('dbpassword', $dbpass);
  388. //create the database not neccessary, oracle implies user = schema
  389. //self::oci_createDatabase($dbname, $dbuser, $connection);
  390. }
  391. //FIXME check tablespace exists: select * from user_tablespaces
  392. // the connection to dbname=oracle is not needed anymore
  393. oci_close($connection);
  394. // connect to the oracle database (schema=$dbuser) an check if the schema needs to be filled
  395. $dbuser = OC_Config::getValue('dbuser');
  396. //$dbname = OC_Config::getValue('dbname');
  397. $dbpass = OC_Config::getValue('dbpassword');
  398. $e_host = addslashes($dbhost);
  399. $e_dbname = addslashes($dbname);
  400. if ($e_host == '') {
  401. $easy_connect_string = $e_dbname; // use dbname as easy connect name
  402. } else {
  403. $easy_connect_string = '//'.$e_host.'/'.$e_dbname;
  404. }
  405. $connection = @oci_connect($dbuser, $dbpass, $easy_connect_string);
  406. if(!$connection) {
  407. throw new Exception('Oracle username and/or password not valid');
  408. }
  409. $query = "SELECT count(*) FROM user_tables WHERE table_name = :un";
  410. $stmt = oci_parse($connection, $query);
  411. $un = $dbtableprefix.'users';
  412. oci_bind_by_name($stmt, ':un', $un);
  413. if (!$stmt) {
  414. $entry='DB Error: "'.oci_last_error($connection).'"<br />';
  415. $entry.='Offending command was: '.$query.'<br />';
  416. echo($entry);
  417. }
  418. $result = oci_execute($stmt);
  419. if($result) {
  420. $row = oci_fetch_row($stmt);
  421. }
  422. if(!$result or $row[0]==0) {
  423. OC_DB::createDbFromStructure('db_structure.xml');
  424. }
  425. }
  426. /**
  427. *
  428. * @param String $name
  429. * @param String $password
  430. * @param String $tablespace
  431. * @param resource $connection
  432. */
  433. private static function oci_createDBUser($name, $password, $tablespace, $connection) {
  434. $query = "SELECT * FROM all_users WHERE USERNAME = :un";
  435. $stmt = oci_parse($connection, $query);
  436. if (!$stmt) {
  437. $entry='DB Error: "'.oci_error($connection).'"<br />';
  438. $entry.='Offending command was: '.$query.'<br />';
  439. echo($entry);
  440. }
  441. oci_bind_by_name($stmt, ':un', $name);
  442. $result = oci_execute($stmt);
  443. if(!$result) {
  444. $entry='DB Error: "'.oci_error($connection).'"<br />';
  445. $entry.='Offending command was: '.$query.'<br />';
  446. echo($entry);
  447. }
  448. if(! oci_fetch_row($stmt)) {
  449. //user does not exists let's create it :)
  450. //password must start with alphabetic character in oracle
  451. $query = 'CREATE USER '.$name.' IDENTIFIED BY "'.$password.'" DEFAULT TABLESPACE '.$tablespace; //TODO set default tablespace
  452. $stmt = oci_parse($connection, $query);
  453. if (!$stmt) {
  454. $entry='DB Error: "'.oci_error($connection).'"<br />';
  455. $entry.='Offending command was: '.$query.'<br />';
  456. echo($entry);
  457. }
  458. //oci_bind_by_name($stmt, ':un', $name);
  459. $result = oci_execute($stmt);
  460. if(!$result) {
  461. $entry='DB Error: "'.oci_error($connection).'"<br />';
  462. $entry.='Offending command was: '.$query.', name:'.$name.', password:'.$password.'<br />';
  463. echo($entry);
  464. }
  465. } else { // change password of the existing role
  466. $query = "ALTER USER :un IDENTIFIED BY :pw";
  467. $stmt = oci_parse($connection, $query);
  468. if (!$stmt) {
  469. $entry='DB Error: "'.oci_error($connection).'"<br />';
  470. $entry.='Offending command was: '.$query.'<br />';
  471. echo($entry);
  472. }
  473. oci_bind_by_name($stmt, ':un', $name);
  474. oci_bind_by_name($stmt, ':pw', $password);
  475. $result = oci_execute($stmt);
  476. if(!$result) {
  477. $entry='DB Error: "'.oci_error($connection).'"<br />';
  478. $entry.='Offending command was: '.$query.'<br />';
  479. echo($entry);
  480. }
  481. }
  482. // grant neccessary roles
  483. $query = 'GRANT CREATE SESSION, CREATE TABLE, CREATE SEQUENCE, CREATE TRIGGER, UNLIMITED TABLESPACE TO '.$name;
  484. $stmt = oci_parse($connection, $query);
  485. if (!$stmt) {
  486. $entry='DB Error: "'.oci_error($connection).'"<br />';
  487. $entry.='Offending command was: '.$query.'<br />';
  488. echo($entry);
  489. }
  490. $result = oci_execute($stmt);
  491. if(!$result) {
  492. $entry='DB Error: "'.oci_error($connection).'"<br />';
  493. $entry.='Offending command was: '.$query.', name:'.$name.', password:'.$password.'<br />';
  494. echo($entry);
  495. }
  496. }
  497. /**
  498. * create .htaccess files for apache hosts
  499. */
  500. private static function createHtaccess() {
  501. $content = "<IfModule mod_fcgid.c>\n";
  502. $content.= "<IfModule mod_setenvif.c>\n";
  503. $content.= "<IfModule mod_headers.c>\n";
  504. $content.= "SetEnvIfNoCase ^Authorization$ \"(.+)\" XAUTHORIZATION=$1\n";
  505. $content.= "RequestHeader set XAuthorization %{XAUTHORIZATION}e env=XAUTHORIZATION\n";
  506. $content.= "</IfModule>\n";
  507. $content.= "</IfModule>\n";
  508. $content.= "</IfModule>\n";
  509. $content.= "ErrorDocument 403 ".OC::$WEBROOT."/core/templates/403.php\n";//custom 403 error page
  510. $content.= "ErrorDocument 404 ".OC::$WEBROOT."/core/templates/404.php\n";//custom 404 error page
  511. $content.= "<IfModule mod_php5.c>\n";
  512. $content.= "php_value upload_max_filesize 512M\n";//upload limit
  513. $content.= "php_value post_max_size 512M\n";
  514. $content.= "php_value memory_limit 512M\n";
  515. $content.= "<IfModule env_module>\n";
  516. $content.= " SetEnv htaccessWorking true\n";
  517. $content.= "</IfModule>\n";
  518. $content.= "</IfModule>\n";
  519. $content.= "<IfModule mod_rewrite.c>\n";
  520. $content.= "RewriteEngine on\n";
  521. $content.= "RewriteRule .* - [env=HTTP_AUTHORIZATION:%{HTTP:Authorization}]\n";
  522. $content.= "RewriteRule ^.well-known/host-meta /public.php?service=host-meta [QSA,L]\n";
  523. $content.= "RewriteRule ^.well-known/carddav /remote.php/carddav/ [R]\n";
  524. $content.= "RewriteRule ^.well-known/caldav /remote.php/caldav/ [R]\n";
  525. $content.= "RewriteRule ^apps/([^/]*)/(.*\.(css|php))$ index.php?app=$1&getfile=$2 [QSA,L]\n";
  526. $content.= "RewriteRule ^remote/(.*) remote.php [QSA,L]\n";
  527. $content.= "</IfModule>\n";
  528. $content.= "<IfModule mod_mime.c>\n";
  529. $content.= "AddType image/svg+xml svg svgz\n";
  530. $content.= "AddEncoding gzip svgz\n";
  531. $content.= "</IfModule>\n";
  532. $content.= "Options -Indexes\n";
  533. @file_put_contents(OC::$SERVERROOT.'/.htaccess', $content); //supress errors in case we don't have permissions for it
  534. self::protectDataDirectory();
  535. }
  536. public static function protectDataDirectory() {
  537. $content = "deny from all\n";
  538. $content.= "IndexIgnore *";
  539. file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/.htaccess', $content);
  540. file_put_contents(OC_Config::getValue('datadirectory', OC::$SERVERROOT.'/data').'/index.html', '');
  541. }
  542. }