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.

552 lines
15 KiB

16 years ago
16 years ago
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2010 Frank Karlitschek karlitschek@kde.org
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. // set some stuff
  23. ob_start();
  24. error_reporting(E_ALL | E_STRICT);
  25. date_default_timezone_set('Europe/Berlin');
  26. ini_set('arg_separator.output','&amp;');
  27. ini_set('session.cookie_httponly','1;');
  28. session_start();
  29. // calculate the documentroot
  30. $SERVERROOT=substr(__FILE__,0,-17);
  31. $DOCUMENTROOT=$_SERVER['DOCUMENT_ROOT'];
  32. $count=strlen($DOCUMENTROOT);
  33. $WEBROOT=substr($SERVERROOT,$count);
  34. if($WEBROOT{0}!=='/'){
  35. $WEBROOT='/'.$WEBROOT;
  36. }
  37. // set the right include path
  38. // set_include_path(get_include_path().PATH_SEPARATOR.$SERVERROOT.PATH_SEPARATOR.$SERVERROOT.'/inc'.PATH_SEPARATOR.$SERVERROOT.'/config');
  39. // define default config values
  40. $CONFIG_INSTALLED=false;
  41. $CONFIG_DATADIRECTORY=$SERVERROOT.'/data';
  42. $CONFIG_HTTPFORCESSL=false;
  43. $CONFIG_DATEFORMAT='j M Y G:i';
  44. $CONFIG_DBNAME='owncloud';
  45. $CONFIG_DBTYPE='sqlite';
  46. // include the generated configfile
  47. @oc_include_once('config.php');
  48. // redirect to https site if configured
  49. if(isset($CONFIG_HTTPFORCESSL) and $CONFIG_HTTPFORCESSL){
  50. if(!isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] != 'on') {
  51. $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  52. header("Location: $url");
  53. exit;
  54. }
  55. }
  56. // load core libs
  57. oc_require_once('lib_files.php');
  58. oc_require_once('lib_log.php');
  59. oc_require_once('lib_config.php');
  60. oc_require_once('lib_user.php');
  61. oc_require_once('lib_ocs.php');
  62. if(OC_USER::isLoggedIn()){
  63. //jail the user in a seperate data folder
  64. $CONFIG_DATADIRECTORY=$SERVERROOT.'/data/'.$_SESSION['username_clean'];
  65. if(!is_dir($CONFIG_DATADIRECTORY)){
  66. mkdir($CONFIG_DATADIRECTORY);
  67. }
  68. }
  69. // load plugins
  70. $CONFIG_LOADPLUGINS='';
  71. $plugins=explode(' ',$CONFIG_LOADPLUGINS);
  72. if(isset($plugins[0]['url'])) foreach($plugins as $plugin) require_once('plugins/'.$plugin.'/lib_'.$plugin.'.php');
  73. // check if the server is correctly configured for ownCloud
  74. OC_UTIL::checkserver();
  75. // listen for login or logout actions
  76. OC_USER::logoutlisener();
  77. $loginresult=OC_USER::loginlisener();
  78. /**
  79. * Class for utility functions
  80. *
  81. */
  82. class OC_UTIL {
  83. public static $scripts=array();
  84. /**
  85. * add a javascript file
  86. *
  87. * @param url $url
  88. */
  89. public static function addscript($url){
  90. self::$scripts[]=$url;
  91. }
  92. /**
  93. * array to store all the optional navigation buttons of the plugins
  94. *
  95. */
  96. static private $NAVIGATION = array();
  97. /**
  98. * check if the current server configuration is suitable for ownCloud
  99. *
  100. */
  101. public static function checkserver(){
  102. global $SERVERROOT;
  103. $f=@fopen($SERVERROOT.'/config/config.php','a+');
  104. if(!$f) die('Error: Config file (config/config.php) is not writable for the webserver.');
  105. @fclose($f);
  106. }
  107. /**
  108. * show the header of the web GUI
  109. *
  110. */
  111. public static function showheader(){
  112. global $CONFIG_ADMINLOGIN;
  113. global $WEBROOT;
  114. oc_require('templates/header.php');;
  115. }
  116. /**
  117. * show the footer of the web GUI
  118. *
  119. */
  120. public static function showfooter(){
  121. global $CONFIG_FOOTEROWNERNAME;
  122. global $CONFIG_FOOTEROWNEREMAIL;
  123. oc_require('templates/footer.php');;
  124. }
  125. /**
  126. * add an navigationentry to the main navigation
  127. *
  128. * @param name $name
  129. * @param url $url
  130. */
  131. public static function addnavigationentry($name,$url) {
  132. $entry=array();
  133. $entry['name']=$name;
  134. $entry['url']=$url;
  135. OC_UTIL::$NAVIGATION[]=$entry;
  136. }
  137. /**
  138. * show the main navigation
  139. *
  140. */
  141. public static function shownavigation(){
  142. global $WEBROOT;
  143. global $SERVERROOT;
  144. echo('<table class="center" cellpadding="5" cellspacing="0" border="0"><tr>');
  145. echo('<td class="navigationitem1"><a href="'.$WEBROOT.'/">'.$_SESSION['username'].'</a></td>');
  146. if($_SERVER['SCRIPT_NAME']==$WEBROOT.'/index.php') echo('<td class="navigationitemselected"><a href="'.$WEBROOT.'/">Files</a></td>'); else echo('<td class="navigationitem"><a href="'.$WEBROOT.'/">Files</a></td>');
  147. foreach(OC_UTIL::$NAVIGATION as $NAVI) {
  148. if(dirname($_SERVER['SCRIPT_NAME'])==$WEBROOT.$NAVI['url']) echo('<td class="navigationitemselected"><a href="'.$WEBROOT.$NAVI['url'].'">'.$NAVI['name'].'</a></td>'); else echo('<td class="navigationitem"><a href="'.$WEBROOT.$NAVI['url'].'">'.$NAVI['name'].'</a></td>');
  149. }
  150. if($_SERVER['SCRIPT_NAME']==$WEBROOT.'/log/index.php') echo('<td class="navigationitemselected"><a href="'.$WEBROOT.'/log">Log</a></td>'); else echo('<td class="navigationitem"><a href="'.$WEBROOT.'/log">Log</a></td>');
  151. if($_SERVER['SCRIPT_NAME']==$WEBROOT.'/settings/index.php') echo('<td class="navigationitemselected"><a href="'.$WEBROOT.'/settings">Settings</a></td>'); else echo('<td class="navigationitem"><a href="'.$WEBROOT.'/settings">Settings</a></td>');
  152. if(OC_USER::ingroup($_SESSION['username'],'admin')){
  153. if($_SERVER['SCRIPT_NAME']==$WEBROOT.'/admin/index.php') echo('<td class="navigationitemselected"><a href="'.$WEBROOT.'/admin">Admin Panel</a></td>'); else echo('<td class="navigationitem"><a href="'.$WEBROOT.'/admin">Admin Panel</a></td>');
  154. }
  155. echo('<td class="navigationitem"><a href="?logoutbutton=1">Logout</a></td>');
  156. echo('</tr></table>');
  157. }
  158. /**
  159. * show the loginform
  160. *
  161. */
  162. public static function showloginform(){
  163. global $loginresult;
  164. oc_require('templates/loginform.php');
  165. }
  166. /**
  167. * show an icon for a filetype
  168. *
  169. */
  170. public static function showicon($filetype){
  171. global $WEBROOT;
  172. if($filetype=='dir'){ echo('<td><img src="'.$WEBROOT.'/img/icons/folder.png" width="16" height="16"></td>');
  173. }elseif($filetype=='foo'){ echo('<td>foo</td>');
  174. }else{ echo('<td><img src="'.$WEBROOT.'/img/icons/other.png" width="16" height="16"></td>');
  175. }
  176. }
  177. }
  178. /**
  179. * Class for database access
  180. *
  181. */
  182. class OC_DB {
  183. /**
  184. * executes a query on the database
  185. *
  186. * @param string $cmd
  187. * @return result-set
  188. */
  189. static function query($cmd) {
  190. global $DOCUMENTROOT;
  191. global $SERVERROOT;
  192. global $DBConnection;
  193. global $CONFIG_DBNAME;
  194. global $CONFIG_DBHOST;
  195. global $CONFIG_DBUSER;
  196. global $CONFIG_DBPASSWORD;
  197. global $CONFIG_DBTYPE;
  198. if(!isset($DBConnection)) {
  199. if($CONFIG_DBTYPE=='sqlite'){
  200. $DBConnection = @new SQLiteDatabase($SERVERROOT.'/'.$CONFIG_DBNAME);
  201. }elseif($CONFIG_DBTYPE=='mysql'){
  202. $DBConnection = @new mysqli($CONFIG_DBHOST, $CONFIG_DBUSER, $CONFIG_DBPASSWORD,$CONFIG_DBNAME);
  203. }
  204. if (!$DBConnection) {
  205. @ob_end_clean();
  206. echo('<b>can not connect to database, using '.$CONFIG_DBTYPE.'.</center>');
  207. exit();
  208. }
  209. }
  210. $result = @$DBConnection->query($cmd);
  211. if (!$result) {
  212. if($CONFIG_DBTYPE=='sqlite'){
  213. $error=sqlite_error_string($DBConnection->lastError());
  214. }elseif($CONFIG_DBTYPE=='mysql'){
  215. print_r($DBConnection);
  216. $error=$DBConnection->error;
  217. }
  218. $entry='DB Error: "'.$error.'"<br />';
  219. $entry.='Offending command was: '.$cmd.'<br />';
  220. echo($entry);
  221. }
  222. return $result;
  223. }
  224. /**
  225. * executes a query on the database and returns the result in an array
  226. *
  227. * @param string $cmd
  228. * @return result-set
  229. */
  230. static function select($cmd) {
  231. global $CONFIG_DBTYPE;
  232. $result=OC_DB::query($cmd);
  233. if($result){
  234. $data=array();
  235. if($CONFIG_DBTYPE=='sqlite'){
  236. while($row=$result->fetch(SQLITE_ASSOC)){
  237. $data[]=$row;
  238. }
  239. }elseif($CONFIG_DBTYPE=='mysql'){
  240. while($row=$result->fetch_array(MYSQLI_ASSOC)){
  241. $data[]=$row;
  242. }
  243. }
  244. return $data;
  245. }else{
  246. return false;
  247. }
  248. }
  249. /**
  250. * executes multiply queries on the database
  251. *
  252. * @param string $cmd
  253. * @return result-set
  254. */
  255. static function multiquery($cmd) {
  256. global $DOCUMENTROOT;
  257. global $SERVERROOT;
  258. global $DBConnection;
  259. global $CONFIG_DBNAME;
  260. global $CONFIG_DBTYPE;
  261. global $CONFIG_DBHOST;
  262. global $CONFIG_DBUSER;
  263. global $CONFIG_DBPASSWORD;
  264. if(!isset($DBConnection)) {
  265. if($CONFIG_DBTYPE=='sqlite'){
  266. $DBConnection = new SQLiteDatabase($SERVERROOT.'/'.$CONFIG_DBNAME);
  267. }elseif($CONFIG_DBTYPE=='mysql'){
  268. $DBConnection = @new mysqli($CONFIG_DBHOST, $CONFIG_DBUSER, $CONFIG_DBPASSWORD,$CONFIG_DBNAME);
  269. }
  270. if (!$DBConnection) {
  271. @ob_end_clean();
  272. echo('<b>can not connect to database, using '.$CONFIG_DBTYPE.'.</center>');
  273. exit();
  274. }
  275. }
  276. if($CONFIG_DBTYPE=='sqlite'){
  277. $result = @$DBConnection->queryExec($cmd);
  278. }elseif($CONFIG_DBTYPE=='mysql'){
  279. $result = @$DBConnection->multi_query($cmd);
  280. }
  281. if (!$result) {
  282. if($CONFIG_DBTYPE=='sqlite'){
  283. $error=sqlite_error_string($DBConnection->lastError());
  284. }elseif($CONFIG_DBTYPE=='mysql'){
  285. $error=$DBConnection->error;
  286. }
  287. $entry='DB Error: "'.$error.'"<br />';
  288. $entry.='Offending command was: '.$cmd.'<br />';
  289. echo($entry);
  290. }
  291. return $result;
  292. }
  293. /**
  294. * closing a db connection
  295. *
  296. * @return bool
  297. */
  298. static function close() {
  299. global $CONFIG_DBTYPE;
  300. global $DBConnection;
  301. if(isset($DBConnection)) {
  302. return $DBConnection->close();
  303. } else {
  304. return(false);
  305. }
  306. }
  307. /**
  308. * Returning primarykey if last statement was an insert.
  309. *
  310. * @return primarykey
  311. */
  312. static function insertid() {
  313. global $DBConnection;
  314. global $CONFIG_DBTYPE;
  315. if($CONFIG_DBTYPE=='sqlite'){
  316. return $DBConnection->lastInsertRowid();
  317. }elseif($CONFIG_DBTYPE=='mysql'){
  318. return(mysqli_insert_id($DBConnection));
  319. }
  320. }
  321. /**
  322. * Returning number of rows in a result
  323. *
  324. * @param resultset $result
  325. * @return int
  326. */
  327. static function numrows($result) {
  328. if(!isset($result) or ($result == false)) return 0;
  329. global $CONFIG_DBTYPE;
  330. if($CONFIG_DBTYPE=='sqlite'){
  331. $num= $result->numRows();
  332. }elseif($CONFIG_DBTYPE=='mysql'){
  333. $num= mysqli_num_rows($result);
  334. }
  335. return($num);
  336. }
  337. /**
  338. * Returning number of affected rows
  339. *
  340. * @return int
  341. */
  342. static function affected_rows() {
  343. global $DBConnection;
  344. global $CONFIG_DBTYPE;
  345. if(!isset($DBConnection) or ($DBConnection==false)) return 0;
  346. if($CONFIG_DBTYPE=='sqlite'){
  347. $num= $DBConnection->changes();
  348. }elseif($CONFIG_DBTYPE=='mysql'){
  349. $num= mysqli_affected_rows($DBConnection);
  350. }
  351. return($num);
  352. }
  353. /**
  354. * get a field from the resultset
  355. *
  356. * @param resultset $result
  357. * @param int $i
  358. * @param int $field
  359. * @return unknown
  360. */
  361. static function result($result, $i, $field) {
  362. global $CONFIG_DBTYPE;
  363. if($CONFIG_DBTYPE=='sqlite'){
  364. $result->seek($i);
  365. $tmp=$result->fetch();
  366. }elseif($CONFIG_DBTYPE=='mysql'){
  367. mysqli_data_seek($result,$i);
  368. if (is_string($field))
  369. $tmp=mysqli_fetch_array($result,MYSQLI_BOTH);
  370. else
  371. $tmp=mysqli_fetch_array($result,MYSQLI_NUM);
  372. }
  373. $tmp=$tmp[$field];
  374. return($tmp);
  375. return($tmp);
  376. }
  377. /**
  378. * get data-array from resultset
  379. *
  380. * @param resultset $result
  381. * @return data
  382. */
  383. static function fetch_assoc($result) {
  384. global $CONFIG_DBTYPE;
  385. if($CONFIG_DBTYPE=='sqlite'){
  386. return $result->fetch(SQLITE_ASSOC);
  387. }elseif($CONFIG_DBTYPE=='mysql'){
  388. return mysqli_fetch_assoc($result);
  389. }
  390. }
  391. /**
  392. * Freeing resultset (performance)
  393. *
  394. * @param unknown_type $result
  395. * @return bool
  396. */
  397. static function free_result($result) {
  398. global $CONFIG_DBTYPE;
  399. if($CONFIG_DBTYPE=='sqlite'){
  400. $result = null; //No native way to do this
  401. return true;
  402. }elseif($CONFIG_DBTYPE=='mysql'){
  403. return @mysqli_free_result($result);
  404. }
  405. }
  406. }
  407. //custom require/include functions because not all hosts allow us to set the include path
  408. function oc_require($file){
  409. global $SERVERROOT;
  410. global $DOCUMENTROOT;
  411. global $WEBROOT;
  412. global $CONFIG_DBNAME;
  413. global $CONFIG_DBHOST;
  414. global $CONFIG_DBUSER;
  415. global $CONFIG_DBPASSWORD;
  416. global $CONFIG_DBTYPE;
  417. global $CONFIG_DATADIRECTORY;
  418. global $CONFIG_HTTPFORCESSL;
  419. global $CONFIG_DATEFORMAT;
  420. global $CONFIG_INSTALLED;
  421. if(is_file($file)){
  422. require($file);
  423. }elseif(is_file($SERVERROOT.'/'.$file)){
  424. require($SERVERROOT.'/'.$file);
  425. }elseif(is_file($SERVERROOT.'/inc/'.$file)){
  426. require($SERVERROOT.'/inc/'.$file);
  427. }elseif(is_file($SERVERROOT.'/config/'.$file)){
  428. require($SERVERROOT.'/config/'.$file);
  429. }
  430. }
  431. function oc_require_once($file){
  432. global $SERVERROOT;
  433. global $DOCUMENTROOT;
  434. global $WEBROOT;
  435. global $CONFIG_DBNAME;
  436. global $CONFIG_DBHOST;
  437. global $CONFIG_DBUSER;
  438. global $CONFIG_DBPASSWORD;
  439. global $CONFIG_DBTYPE;
  440. global $CONFIG_DATADIRECTORY;
  441. global $CONFIG_HTTPFORCESSL;
  442. global $CONFIG_DATEFORMAT;
  443. global $CONFIG_INSTALLED;
  444. if(is_file($file)){
  445. require_once($file);
  446. }elseif(is_file($SERVERROOT.'/'.$file)){
  447. require_once($SERVERROOT.'/'.$file);
  448. }elseif(is_file($SERVERROOT.'/inc/'.$file)){
  449. require_once($SERVERROOT.'/inc/'.$file);
  450. }elseif(is_file($SERVERROOT.'/config/'.$file)){
  451. require_once($SERVERROOT.'/config/'.$file);
  452. }
  453. }
  454. function oc_include($file){
  455. global $SERVERROOT;
  456. global $DOCUMENTROOT;
  457. global $WEBROOT;
  458. global $CONFIG_DBNAME;
  459. global $CONFIG_DBHOST;
  460. global $CONFIG_DBUSER;
  461. global $CONFIG_DBPASSWORD;
  462. global $CONFIG_DBTYPE;
  463. global $CONFIG_DATADIRECTORY;
  464. global $CONFIG_HTTPFORCESSL;
  465. global $CONFIG_DATEFORMAT;
  466. global $CONFIG_INSTALLED;
  467. if(is_file($file)){
  468. include($file);
  469. }elseif(is_file($SERVERROOT.'/'.$file)){
  470. include($SERVERROOT.'/'.$file);
  471. }elseif(is_file($SERVERROOT.'/inc/'.$file)){
  472. include($SERVERROOT.'/inc/'.$file);
  473. }elseif(is_file($SERVERROOT.'/config/'.$file)){
  474. include($SERVERROOT.'/config/'.$file);
  475. }
  476. }
  477. function oc_include_once($file){
  478. global $SERVERROOT;
  479. global $DOCUMENTROOT;
  480. global $WEBROOT;
  481. global $CONFIG_DBNAME;
  482. global $CONFIG_DBHOST;
  483. global $CONFIG_DBUSER;
  484. global $CONFIG_DBPASSWORD;
  485. global $CONFIG_DBTYPE;
  486. global $CONFIG_DATADIRECTORY;
  487. global $CONFIG_HTTPFORCESSL;
  488. global $CONFIG_DATEFORMAT;
  489. global $CONFIG_INSTALLED;
  490. if(is_file($file)){
  491. include_once($file);
  492. }elseif(is_file($SERVERROOT.'/'.$file)){
  493. include_once($SERVERROOT.'/'.$file);
  494. }elseif(is_file($SERVERROOT.'/inc/'.$file)){
  495. include_once($SERVERROOT.'/inc/'.$file);
  496. }elseif(is_file($SERVERROOT.'/config/'.$file)){
  497. include_once($SERVERROOT.'/config/'.$file);
  498. }
  499. }
  500. ?>