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.

455 lines
12 KiB

16 years ago
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. @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. require_once('lib_files.php');
  58. require_once('lib_log.php');
  59. require_once('lib_config.php');
  60. require_once('lib_user.php');
  61. if(OC_USER::isLoggedIn()){
  62. //jail the user in a seperate data folder
  63. $CONFIG_DATADIRECTORY=$SERVERROOT.'/data/'.$_SESSION['username_clean'];
  64. if(!is_dir($CONFIG_DATADIRECTORY)){
  65. mkdir($CONFIG_DATADIRECTORY);
  66. }
  67. }
  68. // load plugins
  69. $CONFIG_LOADPLUGINS='';
  70. $plugins=explode(' ',$CONFIG_LOADPLUGINS);
  71. if(isset($plugins[0]['url'])) foreach($plugins as $plugin) require_once('plugins/'.$plugin.'/lib_'.$plugin.'.php');
  72. // check if the server is correctly configured for ownCloud
  73. OC_UTIL::checkserver();
  74. // listen for login or logout actions
  75. OC_USER::logoutlisener();
  76. $loginresult=OC_USER::loginlisener();
  77. /**
  78. * Class for utility functions
  79. *
  80. */
  81. class OC_UTIL {
  82. public static $scripts=array();
  83. /**
  84. * add a javascript file
  85. *
  86. * @param url $url
  87. */
  88. public static function addscript($url){
  89. self::$scripts[]=$url;
  90. }
  91. /**
  92. * array to store all the optional navigation buttons of the plugins
  93. *
  94. */
  95. static private $NAVIGATION = array();
  96. /**
  97. * check if the current server configuration is suitable for ownCloud
  98. *
  99. */
  100. public static function checkserver(){
  101. global $SERVERROOT;
  102. $f=@fopen($SERVERROOT.'/config/config.php','a+');
  103. if(!$f) die('Error: Config file (config/config.php) is not writable for the webserver.');
  104. @fclose($f);
  105. }
  106. /**
  107. * show the header of the web GUI
  108. *
  109. */
  110. public static function showheader(){
  111. global $CONFIG_ADMINLOGIN;
  112. global $WEBROOT;
  113. require('templates/header.php');;
  114. }
  115. /**
  116. * show the footer of the web GUI
  117. *
  118. */
  119. public static function showfooter(){
  120. global $CONFIG_FOOTEROWNERNAME;
  121. global $CONFIG_FOOTEROWNEREMAIL;
  122. require('templates/footer.php');;
  123. }
  124. /**
  125. * add an navigationentry to the main navigation
  126. *
  127. * @param name $name
  128. * @param url $url
  129. */
  130. public static function addnavigationentry($name,$url) {
  131. $entry=array();
  132. $entry['name']=$name;
  133. $entry['url']=$url;
  134. OC_UTIL::$NAVIGATION[]=$entry;
  135. }
  136. /**
  137. * show the main navigation
  138. *
  139. */
  140. public static function shownavigation(){
  141. global $WEBROOT;
  142. global $SERVERROOT;
  143. echo('<table class="center" cellpadding="5" cellspacing="0" border="0"><tr>');
  144. echo('<td class="navigationitem1"><a href="'.$WEBROOT.'/">'.$_SESSION['username'].'</a></td>');
  145. 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>');
  146. foreach(OC_UTIL::$NAVIGATION as $NAVI) {
  147. 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>');
  148. }
  149. 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>');
  150. 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>');
  151. if(OC_USER::ingroup($_SESSION['username'],'admin')){
  152. 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>');
  153. }
  154. echo('<td class="navigationitem"><a href="?logoutbutton=1">Logout</a></td>');
  155. echo('</tr></table>');
  156. }
  157. /**
  158. * show the loginform
  159. *
  160. */
  161. public static function showloginform(){
  162. global $loginresult;
  163. require('templates/loginform.php');
  164. }
  165. /**
  166. * show an icon for a filetype
  167. *
  168. */
  169. public static function showicon($filetype){
  170. global $WEBROOT;
  171. if($filetype=='dir'){ echo('<td><img src="'.$WEBROOT.'/img/icons/folder.png" width="16" height="16"></td>');
  172. }elseif($filetype=='foo'){ echo('<td>foo</td>');
  173. }else{ echo('<td><img src="'.$WEBROOT.'/img/icons/other.png" width="16" height="16"></td>');
  174. }
  175. }
  176. }
  177. /**
  178. * Class for database access
  179. *
  180. */
  181. class OC_DB {
  182. /**
  183. * executes a query on the database
  184. *
  185. * @param string $cmd
  186. * @return result-set
  187. */
  188. static function query($cmd) {
  189. global $DOCUMENTROOT;
  190. global $SERVERROOT;
  191. global $DBConnection;
  192. global $CONFIG_DBNAME;
  193. global $CONFIG_DBHOST;
  194. global $CONFIG_DBUSER;
  195. global $CONFIG_DBPASSWORD;
  196. global $CONFIG_DBTYPE;
  197. if(!isset($DBConnection)) {
  198. if($CONFIG_DBTYPE=='sqlite'){
  199. $DBConnection = @new SQLiteDatabase($SERVERROOT.'/'.$CONFIG_DBNAME);
  200. }elseif($CONFIG_DBTYPE=='mysql'){
  201. $DBConnection = @new mysqli($CONFIG_DBHOST, $CONFIG_DBUSER, $CONFIG_DBPASSWORD,$CONFIG_DBNAME);
  202. }
  203. if (!$DBConnection) {
  204. @ob_end_clean();
  205. echo('<b>can not connect to database, using '.$CONFIG_DBTYPE.'.</center>');
  206. exit();
  207. }
  208. }
  209. $result = @$DBConnection->query($cmd);
  210. if (!$result) {
  211. if($CONFIG_DBTYPE=='sqlite'){
  212. $error=sqlite_error_string($DBConnection->lastError());
  213. }elseif($CONFIG_DBTYPE=='mysql'){
  214. print_r($DBConnection);
  215. $error=$DBConnection->error;
  216. }
  217. $entry='DB Error: "'.$error.'"<br />';
  218. $entry.='Offending command was: '.$cmd.'<br />';
  219. echo($entry);
  220. }
  221. return $result;
  222. }
  223. /**
  224. * executes a query on the database and returns the result in an array
  225. *
  226. * @param string $cmd
  227. * @return result-set
  228. */
  229. static function select($cmd) {
  230. global $CONFIG_DBTYPE;
  231. $result=OC_DB::query($cmd);
  232. if($result){
  233. $data=array();
  234. if($CONFIG_DBTYPE=='sqlite'){
  235. while($row=$result->fetch(SQLITE_ASSOC)){
  236. $data[]=$row;
  237. }
  238. }elseif($CONFIG_DBTYPE=='mysql'){
  239. while($row=$result->fetch_array(MYSQLI_ASSOC)){
  240. $data[]=$row;
  241. }
  242. }
  243. return $data;
  244. }else{
  245. return false;
  246. }
  247. }
  248. /**
  249. * executes multiply queries on the database
  250. *
  251. * @param string $cmd
  252. * @return result-set
  253. */
  254. static function multiquery($cmd) {
  255. global $DOCUMENTROOT;
  256. global $SERVERROOT;
  257. global $DBConnection;
  258. global $CONFIG_DBNAME;
  259. global $CONFIG_DBTYPE;
  260. global $CONFIG_DBHOST;
  261. global $CONFIG_DBUSER;
  262. global $CONFIG_DBPASSWORD;
  263. if(!isset($DBConnection)) {
  264. if($CONFIG_DBTYPE=='sqlite'){
  265. $DBConnection = new SQLiteDatabase($SERVERROOT.'/'.$CONFIG_DBNAME);
  266. }elseif($CONFIG_DBTYPE=='mysql'){
  267. $DBConnection = @new mysqli($CONFIG_DBHOST, $CONFIG_DBUSER, $CONFIG_DBPASSWORD,$CONFIG_DBNAME);
  268. }
  269. if (!$DBConnection) {
  270. @ob_end_clean();
  271. echo('<b>can not connect to database, using '.$CONFIG_DBTYPE.'.</center>');
  272. exit();
  273. }
  274. }
  275. if($CONFIG_DBTYPE=='sqlite'){
  276. $result = @$DBConnection->queryExec($cmd);
  277. }elseif($CONFIG_DBTYPE=='mysql'){
  278. $result = @$DBConnection->multi_query($cmd);
  279. }
  280. if (!$result) {
  281. if($CONFIG_DBTYPE=='sqlite'){
  282. $error=sqlite_error_string($DBConnection->lastError());
  283. }elseif($CONFIG_DBTYPE=='mysql'){
  284. $error=$DBConnection->error;
  285. }
  286. $entry='DB Error: "'.$error.'"<br />';
  287. $entry.='Offending command was: '.$cmd.'<br />';
  288. echo($entry);
  289. }
  290. return $result;
  291. }
  292. /**
  293. * closing a db connection
  294. *
  295. * @return bool
  296. */
  297. static function close() {
  298. global $CONFIG_DBTYPE;
  299. global $DBConnection;
  300. if(isset($DBConnection)) {
  301. return $DBConnection->close();
  302. } else {
  303. return(false);
  304. }
  305. }
  306. /**
  307. * Returning primarykey if last statement was an insert.
  308. *
  309. * @return primarykey
  310. */
  311. static function insertid() {
  312. global $DBConnection;
  313. global $CONFIG_DBTYPE;
  314. if($CONFIG_DBTYPE=='sqlite'){
  315. return $DBConnection->lastInsertRowid();
  316. }elseif($CONFIG_DBTYPE=='mysql'){
  317. return(mysqli_insert_id($DBConnection));
  318. }
  319. }
  320. /**
  321. * Returning number of rows in a result
  322. *
  323. * @param resultset $result
  324. * @return int
  325. */
  326. static function numrows($result) {
  327. if(!isset($result) or ($result == false)) return 0;
  328. global $CONFIG_DBTYPE;
  329. if($CONFIG_DBTYPE=='sqlite'){
  330. $num= $result->numRows();
  331. }elseif($CONFIG_DBTYPE=='mysql'){
  332. $num= mysqli_num_rows($result);
  333. }
  334. return($num);
  335. }
  336. /**
  337. * Returning number of affected rows
  338. *
  339. * @return int
  340. */
  341. static function affected_rows() {
  342. global $DBConnection;
  343. global $CONFIG_DBTYPE;
  344. if(!isset($DBConnection) or ($DBConnection==false)) return 0;
  345. if($CONFIG_DBTYPE=='sqlite'){
  346. $num= $DBConnection->changes();
  347. }elseif($CONFIG_DBTYPE=='mysql'){
  348. $num= mysqli_affected_rows($DBConnection);
  349. }
  350. return($num);
  351. }
  352. /**
  353. * get a field from the resultset
  354. *
  355. * @param resultset $result
  356. * @param int $i
  357. * @param int $field
  358. * @return unknown
  359. */
  360. static function result($result, $i, $field) {
  361. global $CONFIG_DBTYPE;
  362. if($CONFIG_DBTYPE=='sqlite'){
  363. $result->seek($i);
  364. $tmp=$result->fetch();
  365. }elseif($CONFIG_DBTYPE=='mysql'){
  366. mysqli_data_seek($result,$i);
  367. if (is_string($field))
  368. $tmp=mysqli_fetch_array($result,MYSQLI_BOTH);
  369. else
  370. $tmp=mysqli_fetch_array($result,MYSQLI_NUM);
  371. }
  372. $tmp=$tmp[$field];
  373. return($tmp);
  374. return($tmp);
  375. }
  376. /**
  377. * get data-array from resultset
  378. *
  379. * @param resultset $result
  380. * @return data
  381. */
  382. static function fetch_assoc($result) {
  383. global $CONFIG_DBTYPE;
  384. if($CONFIG_DBTYPE=='sqlite'){
  385. return $result->fetch(SQLITE_ASSOC);
  386. }elseif($CONFIG_DBTYPE=='mysql'){
  387. return mysqli_fetch_assoc($result);
  388. }
  389. }
  390. /**
  391. * Freeing resultset (performance)
  392. *
  393. * @param unknown_type $result
  394. * @return bool
  395. */
  396. static function free_result($result) {
  397. global $CONFIG_DBTYPE;
  398. if($CONFIG_DBTYPE=='sqlite'){
  399. $result = null; //No native way to do this
  400. return true;
  401. }elseif($CONFIG_DBTYPE=='mysql'){
  402. return @mysqli_free_result($result);
  403. }
  404. }
  405. }
  406. ?>