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.

410 lines
13 KiB

  1. <?php
  2. class OC_CONFIG{
  3. /**
  4. * show the configform
  5. *
  6. */
  7. public static function showconfigform(){
  8. global $CONFIG_ADMINLOGIN;
  9. global $CONFIG_ADMINPASSWORD;
  10. global $CONFIG_DATADIRECTORY;
  11. global $CONFIG_HTTPFORCESSL;
  12. global $CONFIG_DATEFORMAT;
  13. global $CONFIG_DBNAME;
  14. oc_require('templates/configform.php');
  15. }
  16. /**
  17. * show the configform
  18. *
  19. */
  20. public static function showadminform(){
  21. global $CONFIG_ADMINLOGIN;
  22. global $CONFIG_ADMINPASSWORD;
  23. global $CONFIG_DATADIRECTORY;
  24. global $CONFIG_HTTPFORCESSL;
  25. global $CONFIG_DATEFORMAT;
  26. global $CONFIG_DBNAME;
  27. global $CONFIG_INSTALLED;
  28. $allow=false;
  29. if(!$CONFIG_INSTALLED){
  30. $allow=true;
  31. }elseif(OC_USER::isLoggedIn()){
  32. if(OC_USER::ingroup($_SESSION['username'],'admin')){
  33. $allow=true;
  34. }
  35. }
  36. if($allow){
  37. oc_require('templates/adminform.php');
  38. }
  39. }
  40. public static function createuserlisener(){
  41. if(OC_USER::isLoggedIn()){
  42. if(OC_USER::ingroup($_SESSION['username'],'admin')){
  43. if(isset($_POST['new_username']) and isset($_POST['new_password'])){
  44. if(OC_USER::createuser($_POST['new_username'],$_POST['new_password'])){
  45. return 'user successfully created';
  46. }else{
  47. return 'error while trying to create user';
  48. }
  49. }else{
  50. return false;
  51. }
  52. }else{
  53. return false;
  54. }
  55. }
  56. }
  57. public static function creategrouplisener(){
  58. if(OC_USER::isLoggedIn()){
  59. if(isset($_POST['creategroup']) and $_POST['creategroup']==1){
  60. if(OC_USER::creategroup($_POST['groupname'])){
  61. if(OC_USER::addtogroup($_SESSION['username'],$_POST['groupname'])){
  62. return 'group successfully created';
  63. }else{
  64. return 'error while trying to add user to the new created group';
  65. }
  66. }else{
  67. return 'error while trying to create group';
  68. }
  69. }else{
  70. return false;
  71. }
  72. }else{
  73. return false;
  74. }
  75. }
  76. /**
  77. * lisen for configuration changes
  78. *
  79. */
  80. public static function configlisener(){
  81. if(OC_USER::isLoggedIn()){
  82. if(isset($_POST['config']) and $_POST['config']==1){
  83. $error='';
  84. if(!OC_USER::checkpassword($_SESSION['username'],$_POST['currentpassword'])){
  85. $error.='wrong password<br />';
  86. }else{
  87. if(isset($_POST['changepass']) and $_POST['changepass']==1){
  88. if(!isset($_POST['password']) or empty($_POST['password'])) $error.='password not set<br />';
  89. if(!isset($_POST['password2']) or empty($_POST['password2'])) $error.='retype password not set<br />';
  90. if($_POST['password']<>$_POST['password2'] ) $error.='passwords are not the same<br />';
  91. if(empty($error)){
  92. if(!OC_USER::setpassword($_SESSION['username'],$_POST['password'])){
  93. $error.='error while trying to set password<br />';
  94. }
  95. }
  96. }
  97. }
  98. return $error;
  99. }else{
  100. return false;
  101. }
  102. }else{
  103. return false;
  104. }
  105. }
  106. /**
  107. * lisen for admin configuration changes and write it to the file
  108. *4bd0be1185e76
  109. */
  110. public static function writeadminlisener(){
  111. global $CONFIG_INSTALLED;
  112. $allow=false;
  113. if(!$CONFIG_INSTALLED){
  114. $allow=true;
  115. }elseif(OC_USER::isLoggedIn()){
  116. if(OC_USER::ingroup($_SESSION['username'],'admin')){
  117. $allow=true;
  118. }
  119. }
  120. if($allow){
  121. global $DOCUMENTROOT;
  122. global $SERVERROOT;
  123. global $WEBROOT;
  124. global $CONFIG_DBHOST;
  125. global $CONFIG_DBNAME;
  126. global $CONFIG_DBUSER;
  127. global $CONFIG_DBPASSWORD;
  128. global $CONFIG_DBTYPE;
  129. global $CONFIG_ADMINLOGIN;
  130. global $CONFIG_ADMINPASSWORD;
  131. if(isset($_POST['set_config'])){
  132. //checkdata
  133. $error='';
  134. $FIRSTRUN=empty($CONFIG_ADMINLOGIN);
  135. if(!$FIRSTRUN){
  136. if($_POST['currentpassword']!=$CONFIG_ADMINPASSWORD){
  137. $error.='wrong password<br />';
  138. }
  139. }
  140. if(!isset($_POST['adminlogin']) or empty($_POST['adminlogin'])) $error.='admin login not set<br />';
  141. if((!isset($_POST['adminpassword']) or empty($_POST['adminpassword'])) and $FIRSTRUN) $error.='admin password not set<br />';
  142. if((!isset($_POST['adminpassword2']) or empty($_POST['adminpassword2'])) and $FIRSTRUN) $error.='retype admin password not set<br />';
  143. if(!isset($_POST['datadirectory']) or empty($_POST['datadirectory'])) $error.='data directory not set<br />';
  144. if(!isset($_POST['dateformat']) or empty($_POST['dateformat'])) $error.='dateformat not set<br />';
  145. if(!isset($_POST['dbname']) or empty($_POST['dbname'])) $error.='databasename not set<br />';
  146. if($_POST['adminpassword']<>$_POST['adminpassword2'] ) $error.='admin passwords are not the same<br />';
  147. $dbtype=$_POST['dbtype'];
  148. if($dbtype=='mysql'){
  149. if(!isset($_POST['dbhost']) or empty($_POST['dbhost'])) $error.='database host not set<br />';
  150. if(!isset($_POST['dbuser']) or empty($_POST['dbuser'])) $error.='database user not set<br />';
  151. if($_POST['dbpassword']<>$_POST['dbpassword2'] ) $error.='database passwords are not the same<br />';
  152. }
  153. if(!$FIRSTRUN){
  154. if(!isset($_POST['adminpassword']) or empty($_POST['adminpassword'])){
  155. $_POST['adminpassword']=$CONFIG_ADMINPASSWORD;
  156. }
  157. if(!isset($_POST['dbpassword']) or empty($_POST['dbpassword'])){
  158. $_POST['dbpassword']=$CONFIG_DBPASSWORD;
  159. }
  160. }
  161. if(!is_dir($_POST['datadirectory'])){
  162. try{
  163. mkdir($_POST['datadirectory']);
  164. }catch(Exception $e){
  165. $error.='error while trying to create data directory<br/>';
  166. }
  167. }
  168. if(empty($error)) {
  169. //create/fill database
  170. $CONFIG_DBTYPE=$dbtype;
  171. $CONFIG_DBNAME=$_POST['dbname'];
  172. if($dbtype=='mysql'){
  173. $CONFIG_DBHOST=$_POST['dbhost'];
  174. $CONFIG_DBUSER=$_POST['dbuser'];
  175. $CONFIG_DBPASSWORD=$_POST['dbpassword'];
  176. }
  177. try{
  178. if(isset($_POST['createdatabase']) and $CONFIG_DBTYPE=='mysql'){
  179. self::createdatabase($_POST['dbadminuser'],$_POST['dbadminpwd']);
  180. }
  181. }catch(Exception $e){
  182. $error.='error while trying to create the database<br/>';
  183. }
  184. if($CONFIG_DBTYPE=='sqlite'){
  185. $f=@fopen($SERVERROOT.'/'.$CONFIG_DBNAME,'a+');
  186. if(!$f){
  187. $error.='path of sqlite database not writable by server<br/>';
  188. }
  189. }
  190. try{
  191. if(isset($_POST['filldb'])){
  192. self::filldatabase();
  193. }
  194. }catch(Exception $e){
  195. echo 'testin';
  196. $error.='error while trying to fill the database<br/>';
  197. }
  198. if(!OC_USER::createuser($_POST['adminlogin'],$_POST['adminpassword']) && !OC_USER::login($_POST['adminlogin'],$_POST['adminpassword'])){
  199. $error.='error while trying to create the admin user<br/>';
  200. }
  201. if(OC_USER::getgroupid('admin')==0){
  202. if(!OC_USER::creategroup('admin')){
  203. $error.='error while trying to create the admin group<br/>';
  204. }
  205. }
  206. if(!OC_USER::addtogroup($_POST['adminlogin'],'admin')){
  207. $error.='error while trying to add the admin user to the admin group<br/>';
  208. }
  209. //storedata
  210. $config='<?php '."\n";
  211. // $config.='$CONFIG_ADMINLOGIN=\''.$_POST['adminlogin']."';\n";
  212. // $config.='$CONFIG_ADMINPASSWORD=\''.$_POST['adminpassword']."';\n";
  213. $config.='$CONFIG_INSTALLED=true;'."\n";
  214. $config.='$CONFIG_DATADIRECTORY=\''.$_POST['datadirectory']."';\n";
  215. if(isset($_POST['forcessl'])) $config.='$CONFIG_HTTPFORCESSL=true'.";\n"; else $config.='$CONFIG_HTTPFORCESSL=false'.";\n";
  216. $config.='$CONFIG_DATEFORMAT=\''.$_POST['dateformat']."';\n";
  217. $config.='$CONFIG_DBTYPE=\''.$dbtype."';\n";
  218. $config.='$CONFIG_DBNAME=\''.$_POST['dbname']."';\n";
  219. if($dbtype=='mysql'){
  220. $config.='$CONFIG_DBHOST=\''.$_POST['dbhost']."';\n";
  221. $config.='$CONFIG_DBUSER=\''.$_POST['dbuser']."';\n";
  222. $config.='$CONFIG_DBPASSWORD=\''.$_POST['dbpassword']."';\n";
  223. }
  224. $config.='?> ';
  225. $filename=$SERVERROOT.'/config/config.php';
  226. if(empty($error)){
  227. header("Location: ".$WEBROOT."/");
  228. try{
  229. file_put_contents($filename,$config);
  230. }catch(Exception $e){
  231. $error.='error while trying to save the configuration file<br/>';
  232. return $error;
  233. }
  234. }else{
  235. return $error;
  236. }
  237. }
  238. return($error);
  239. }
  240. }
  241. }
  242. /**
  243. * Fills the database with the initial tables
  244. * Note: while the AUTO_INCREMENT function is not supported by SQLite
  245. * the same effect can be achieved by accessing the SQLite pseudo-column
  246. * "rowid"
  247. */
  248. private static function filldatabase(){
  249. global $CONFIG_DBTYPE;
  250. if($CONFIG_DBTYPE=='sqlite'){
  251. $query="CREATE TABLE 'locks' (
  252. 'token' VARCHAR(255) NOT NULL DEFAULT '',
  253. 'path' varchar(200) NOT NULL DEFAULT '',
  254. 'created' int(11) NOT NULL DEFAULT '0',
  255. 'modified' int(11) NOT NULL DEFAULT '0',
  256. 'expires' int(11) NOT NULL DEFAULT '0',
  257. 'owner' varchar(200) DEFAULT NULL,
  258. 'recursive' int(11) DEFAULT '0',
  259. 'writelock' int(11) DEFAULT '0',
  260. 'exclusivelock' int(11) NOT NULL DEFAULT '0',
  261. PRIMARY KEY ('token'),
  262. UNIQUE ('token')
  263. );
  264. CREATE TABLE 'log' (
  265. 'timestamp' int(11) NOT NULL,
  266. 'user' varchar(250) NOT NULL,
  267. 'type' int(11) NOT NULL,
  268. 'message' varchar(250) NOT NULL
  269. );
  270. CREATE TABLE 'properties' (
  271. 'path' varchar(255) NOT NULL DEFAULT '',
  272. 'name' varchar(120) NOT NULL DEFAULT '',
  273. 'ns' varchar(120) NOT NULL DEFAULT 'DAV:',
  274. 'value' text,
  275. PRIMARY KEY ('path','name','ns')
  276. );
  277. CREATE TABLE 'users' (
  278. 'user_id' int(11) NOT NULL,
  279. 'user_name' varchar(64) NOT NULL DEFAULT '',
  280. 'user_name_clean' varchar(64) NOT NULL DEFAULT '',
  281. 'user_password' varchar(40) NOT NULL DEFAULT '',
  282. PRIMARY KEY ('user_id'),
  283. UNIQUE ('user_name' ,'user_name_clean')
  284. );
  285. ";
  286. }elseif($CONFIG_DBTYPE=='mysql'){
  287. $query="SET SQL_MODE='NO_AUTO_VALUE_ON_ZERO';
  288. CREATE TABLE IF NOT EXISTS `locks` (
  289. `token` varchar(255) NOT NULL DEFAULT '',
  290. `path` varchar(200) NOT NULL DEFAULT '',
  291. `created` int(11) NOT NULL DEFAULT '0',
  292. `modified` int(11) NOT NULL DEFAULT '0',
  293. `expires` int(11) NOT NULL DEFAULT '0',
  294. `owner` varchar(200) DEFAULT NULL,
  295. `recursive` int(11) DEFAULT '0',
  296. `writelock` int(11) DEFAULT '0',
  297. `exclusivelock` int(11) NOT NULL DEFAULT '0',
  298. PRIMARY KEY (`token`),
  299. UNIQUE KEY `token` (`token`),
  300. KEY `path` (`path`),
  301. KEY `path_2` (`path`),
  302. KEY `path_3` (`path`,`token`),
  303. KEY `expires` (`expires`)
  304. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  305. CREATE TABLE IF NOT EXISTS `log` (
  306. `id` int(11) NOT NULL AUTO_INCREMENT,
  307. `timestamp` int(11) NOT NULL,
  308. `user` varchar(250) NOT NULL,
  309. `type` int(11) NOT NULL,
  310. `message` varchar(250) NOT NULL,
  311. PRIMARY KEY (`id`)
  312. ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=15 ;
  313. CREATE TABLE IF NOT EXISTS `properties` (
  314. `path` varchar(255) NOT NULL DEFAULT '',
  315. `name` varchar(120) NOT NULL DEFAULT '',
  316. `ns` varchar(120) NOT NULL DEFAULT 'DAV:',
  317. `value` text,
  318. PRIMARY KEY (`path`,`name`,`ns`),
  319. KEY `path` (`path`)
  320. ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
  321. CREATE TABLE IF NOT EXISTS `users` (
  322. `user_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  323. `user_name` VARCHAR( 64 ) NOT NULL ,
  324. `user_name_clean` VARCHAR( 64 ) NOT NULL ,
  325. `user_password` VARCHAR( 340) NOT NULL ,
  326. UNIQUE (
  327. `user_name` ,
  328. `user_name_clean`
  329. )
  330. ) ENGINE = MYISAM ;
  331. CREATE TABLE IF NOT EXISTS `groups` (
  332. `group_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  333. `group_name` VARCHAR( 64 ) NOT NULL ,
  334. UNIQUE (
  335. `group_name`
  336. )
  337. ) ENGINE = MYISAM ;
  338. CREATE TABLE IF NOT EXISTS `user_group` (
  339. `user_group_id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
  340. `user_id` VARCHAR( 64 ) NOT NULL ,
  341. `group_id` VARCHAR( 64 ) NOT NULL
  342. ) ENGINE = MYISAM ;
  343. ";
  344. }
  345. OC_DB::multiquery($query);
  346. }
  347. /**
  348. * Create the database and user
  349. * @param string adminUser
  350. * @param string adminPwd
  351. *
  352. */
  353. private static function createdatabase($adminUser,$adminPwd){
  354. global $CONFIG_DBHOST;
  355. global $CONFIG_DBNAME;
  356. global $CONFIG_DBUSER;
  357. global $CONFIG_DBPWD;
  358. //we cant user OC_BD functions here because we need to connect as the administrative user.
  359. $connection = @new mysqli($CONFIG_DBHOST, $adminUser, $adminPwd);
  360. if (mysqli_connect_errno()) {
  361. @ob_end_clean();
  362. echo('<html><head></head><body bgcolor="#F0F0F0"><br /><br /><center><b>can not connect to database as administrative user.</center></body></html>');
  363. exit();
  364. }
  365. $query="CREATE USER '{$_POST['dbuser']}' IDENTIFIED BY '{$_POST['dbpassword']}';
  366. CREATE DATABASE IF NOT EXISTS `{$_POST['dbname']}` ;
  367. GRANT ALL PRIVILEGES ON `{$_POST['dbname']}` . * TO '{$_POST['dbuser']}';";
  368. $result = @$connection->multi_query($query);
  369. if (!$result) {
  370. $entry='DB Error: "'.$connection->error.'"<br />';
  371. $entry.='Offending command was: '.$query.'<br />';
  372. echo($entry);
  373. }
  374. $connection->close();
  375. }
  376. }
  377. ?>