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.

129 lines
4.5 KiB

  1. <?php
  2. if(isset($_POST['install']) and $_POST['install']=='true'){
  3. $errors=OC_INSTALLER::install($_POST);
  4. if(count($errors)>0){
  5. OC_TEMPLATE::printGuestPage( "", "error", array( "errors" => $errors ));
  6. }else{
  7. header( "Location: $WEBROOT");
  8. exit();
  9. }
  10. }
  11. class OC_INSTALLER{
  12. public static function install($options){
  13. $error=array();
  14. $dbtype=$options['dbtype'];
  15. if(empty($options['login'])){
  16. $error[]=array('error'=>'username not set');
  17. };
  18. if(empty($options['pass'])){
  19. $error[]=array('error'=>'password not set');
  20. };
  21. if(empty($options['directory'])){
  22. $error[]=array('error'=>'data directory not set');
  23. };
  24. if($dbtype=='mysql'){//mysql needs more config options
  25. if(empty($options['dbuser'])){
  26. $error[]=array('error'=>'database user not set');
  27. };
  28. if(empty($options['dbpass'])){
  29. $error[]=array('error'=>'database password not set');
  30. };
  31. if(empty($options['dbname'])){
  32. $error[]=array('error'=>'database name not set');
  33. };
  34. if(empty($options['dbhost'])){
  35. $error[]=array('error'=>'database host not set');
  36. };
  37. if(!isset($options['dbtableprefix'])){
  38. $error[]=array('error'=>'database table prefix not set');
  39. };
  40. }
  41. if(count($error)==0){ //no errors, good
  42. $username=$options['login'];
  43. $password=$options['pass'];
  44. $datadir=$options['directory'];
  45. //write the config file
  46. OC_CONFIG::setValue('datadirectory',$datadir);
  47. OC_CONFIG::setValue('dbtype',$dbtype);
  48. if($dbtype=='mysql'){
  49. $dbuser=$options['dbuser'];
  50. $dbpass=$options['dbpass'];
  51. $dbname=$options['dbname'];
  52. $dbhost=$options['dbhost'];
  53. $dbtableprefix=$options['dbtableprefix'];
  54. OC_CONFIG::setValue('dbname',$dbname);
  55. OC_CONFIG::setValue('dbhost',$dbhost);
  56. OC_CONFIG::setValue('dbtableprefix',$dbtableprefix);
  57. //check if the database user has admin right
  58. $connection=@mysql_connect($dbhost, $dbuser, $dbpass);
  59. if(!$connection) {
  60. $error[]=array('error'=>'mysql username and/or password not valid','hint'=>'you need to enter either an existing account, or the administrative account if you wish to create a new user for ownCloud');
  61. }else{
  62. $query="SELECT user FROM mysql.user WHERE user='$dbuser'";//this should be enough to check for admin rights in mysql
  63. if(mysql_query($query,$connection)){
  64. self::createDBUser($username,$password,$connection);
  65. //use the admin login data for the new database user
  66. OC_CONFIG::setValue('dbuser',$username);
  67. OC_CONFIG::setValue('dbpassword',$password);
  68. //create the database
  69. self::createDatabase($dbname,$username,$connection);
  70. }else{
  71. OC_CONFIG::setValue('dbuser',$dbuser);
  72. OC_CONFIG::setValue('dbpassword',$dbpass);
  73. //create the database
  74. self::createDatabase($dbname,$dbuser,$connection);
  75. }
  76. //fill the database if needed
  77. $query="SELECT * FROM $dbname.{$dbtableprefix}users";
  78. $result = mysql_query($query,$connection);
  79. if (!$result) {
  80. OC_DB::createDbFromStructure('db_structure.xml');
  81. }
  82. mysql_close($connection);
  83. }
  84. }else{
  85. //in case of sqlite, we can always fill the database
  86. OC_DB::createDbFromStructure('db_structure.xml');
  87. }
  88. if(count($error)==0){
  89. //create the user and group
  90. OC_USER::createUser($username,$password);
  91. OC_GROUP::createGroup('admin');
  92. OC_GROUP::addToGroup($username,'admin');
  93. //and we are done
  94. OC_CONFIG::setValue('installed',true);
  95. }
  96. }
  97. return $error;
  98. }
  99. public static function createDatabase($name,$user,$connection){
  100. //we cant user OC_BD functions here because we need to connect as the administrative user.
  101. $query="CREATE DATABASE IF NOT EXISTS `$name`";
  102. $result = mysql_query($query,$connection);
  103. if (!$result) {
  104. $entry='DB Error: "'.mysql_error($connection).'"<br />';
  105. $entry.='Offending command was: '.$query.'<br />';
  106. echo($entry);
  107. }
  108. $query="GRANT ALL PRIVILEGES ON `$name` . * TO '$user'";
  109. $result = mysql_query($query,$connection);//this query will fail if there aren't the right permissons, ignore the error
  110. }
  111. private static function createDBUser($name,$password,$connection){
  112. //we need to create 2 accounts, one for global use and one for local user. if we don't speccify the local one,
  113. // the anonymous user would take precedence when there is one.
  114. $query="CREATE USER '$name'@'localhost' IDENTIFIED BY '$password'";
  115. $result = mysql_query($query,$connection);
  116. $query="CREATE USER '$name'@'%' IDENTIFIED BY '$password'";
  117. $result = mysql_query($query,$connection);
  118. }
  119. }
  120. ?>