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.

lib_base.php 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578
  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. error_reporting(E_ALL); // MDB2 gives loads of strict error, disabling for now
  26. date_default_timezone_set('Europe/Berlin');
  27. ini_set('arg_separator.output','&amp;');
  28. ini_set('session.cookie_httponly','1;');
  29. session_start();
  30. // calculate the documentroot
  31. $SERVERROOT=substr(__FILE__,0,-17);
  32. $DOCUMENTROOT=$_SERVER['DOCUMENT_ROOT'];
  33. $SERVERROOT=str_replace("\\",'/',$SERVERROOT);
  34. $count=strlen($DOCUMENTROOT);
  35. $WEBROOT=substr($SERVERROOT,$count);
  36. if($WEBROOT{0}!=='/'){
  37. $WEBROOT='/'.$WEBROOT;
  38. }
  39. // set the right include path
  40. // set_include_path(get_include_path().PATH_SEPARATOR.$SERVERROOT.PATH_SEPARATOR.$SERVERROOT.'/inc'.PATH_SEPARATOR.$SERVERROOT.'/config');
  41. // define default config values
  42. $CONFIG_INSTALLED=false;
  43. $CONFIG_DATADIRECTORY=$SERVERROOT.'/data';
  44. $CONFIG_BACKUPDIRECTORY=$SERVERROOT.'/backup';
  45. $CONFIG_HTTPFORCESSL=false;
  46. $CONFIG_ENABLEBACKUP=false;
  47. $CONFIG_DATEFORMAT='j M Y G:i';
  48. $CONFIG_DBNAME='owncloud';
  49. $CONFIG_DBTYPE='sqlite';
  50. // include the generated configfile
  51. @include_once($SERVERROOT.'/config/config.php');
  52. $CONFIG_DATADIRECTORY_ROOT=$CONFIG_DATADIRECTORY;// store this in a seperate variable so we can change the data directory to jail users.
  53. // redirect to https site if configured
  54. if(isset($CONFIG_HTTPFORCESSL) and $CONFIG_HTTPFORCESSL){
  55. if(!isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] != 'on') {
  56. $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  57. header("Location: $url");
  58. exit;
  59. }
  60. }
  61. // load core libs
  62. oc_require_once('lib_files.php');
  63. oc_require_once('lib_filesystem.php');
  64. oc_require_once('lib_filestorage.php');
  65. oc_require_once('lib_fileobserver.php');
  66. oc_require_once('lib_log.php');
  67. oc_require_once('lib_config.php');
  68. oc_require_once('lib_user.php');
  69. oc_require_once('lib_ocs.php');
  70. @oc_require_once('MDB2.php');
  71. oc_require_once('lib_connect.php');
  72. if(!is_dir($CONFIG_DATADIRECTORY_ROOT)){
  73. mkdir($CONFIG_DATADIRECTORY_ROOT);
  74. }
  75. if(OC_USER::isLoggedIn()){
  76. //jail the user in a seperate data folder
  77. $CONFIG_DATADIRECTORY=$CONFIG_DATADIRECTORY_ROOT.'/'.$_SESSION['username_clean'];
  78. if(!is_dir($CONFIG_DATADIRECTORY)){
  79. mkdir($CONFIG_DATADIRECTORY);
  80. }
  81. $rootStorage=new OC_FILESTORAGE_LOCAL(array('datadir'=>$CONFIG_DATADIRECTORY));
  82. if($CONFIG_ENABLEBACKUP){
  83. if(!is_dir($CONFIG_BACKUPDIRECTORY)){
  84. mkdir($CONFIG_BACKUPDIRECTORY);
  85. }
  86. if(!is_dir($CONFIG_BACKUPDIRECTORY.'/'.$_SESSION['username_clean'])){
  87. mkdir($CONFIG_BACKUPDIRECTORY.'/'.$_SESSION['username_clean']);
  88. }
  89. $backupStorage=new OC_FILESTORAGE_LOCAL(array('datadir'=>$CONFIG_BACKUPDIRECTORY.'/'.$_SESSION['username_clean']));
  90. $backup=new OC_FILEOBSERVER_BACKUP(array('storage'=>$backupStorage));
  91. $rootStorage->addObserver($backup);
  92. }
  93. OC_FILESYSTEM::mount($rootStorage,'/');
  94. }
  95. // load plugins
  96. $CONFIG_LOADPLUGINS='';
  97. $plugins=explode(' ',$CONFIG_LOADPLUGINS);
  98. if(isset($plugins[0]['url'])) foreach($plugins as $plugin) require_once('plugins/'.$plugin.'/lib_'.$plugin.'.php');
  99. // check if the server is correctly configured for ownCloud
  100. OC_UTIL::checkserver();
  101. // listen for login or logout actions
  102. OC_USER::logoutlisener();
  103. $loginresult=OC_USER::loginlisener();
  104. /**
  105. * Class for utility functions
  106. *
  107. */
  108. class OC_UTIL {
  109. public static $scripts=array();
  110. /**
  111. * add a javascript file
  112. *
  113. * @param url $url
  114. */
  115. public static function addscript($url){
  116. self::$scripts[]=$url;
  117. }
  118. /**
  119. * array to store all the optional navigation buttons of the plugins
  120. *
  121. */
  122. static private $NAVIGATION = array();
  123. /**
  124. * check if the current server configuration is suitable for ownCloud
  125. *
  126. */
  127. public static function checkserver(){
  128. global $SERVERROOT;
  129. global $CONFIG_DATADIRECTORY_ROOT;
  130. global $CONFIG_BACKUPDIRECTORY;
  131. global $CONFIG_ENABLEBACKUP;
  132. $error='';
  133. $f=@fopen($SERVERROOT.'/config/config.php','a+');
  134. if(!$f) $error.='Error: Config file (config/config.php) is not writable for the webserver.<br/>';
  135. @fclose($f);
  136. if(!is_callable('sqlite_open') and !is_callable('mysql_connect')){
  137. $error.='No database drivers (sqlite or mysql) installed.<br/>';
  138. }
  139. global $CONFIG_DBTYPE;
  140. global $CONFIG_DBNAME;
  141. if($CONFIG_DBTYPE=='sqlite'){
  142. $file=$SERVERROOT.'/'.$CONFIG_DBNAME;
  143. $prems=substr(decoct(fileperms($file)),-3);
  144. if(substr($prems,2,1)!='0'){
  145. @chmod($file,0660);
  146. clearstatcache();
  147. $prems=substr(decoct(fileperms($file)),-3);
  148. if(substr($prems,2,1)!='0'){
  149. $error.='SQLite database file ('.$file.') is readable from the web<br/>';
  150. }
  151. }
  152. }
  153. $prems=substr(decoct(fileperms($CONFIG_DATADIRECTORY_ROOT)),-3);
  154. if(substr($CONFIG_DATADIRECTORY_ROOT,2,1)!='0'){
  155. chmodr($CONFIG_DATADIRECTORY_ROOT,0770);
  156. clearstatcache();
  157. $prems=substr(decoct(fileperms($CONFIG_DATADIRECTORY_ROOT)),-3);
  158. if(substr($prems,2,1)!='0'){
  159. $error.='Data directory ('.$CONFIG_DATADIRECTORY_ROOT.') is readable from the web<br/>';
  160. }
  161. }
  162. if($CONFIG_ENABLEBACKUP){
  163. $prems=substr(decoct(fileperms($CONFIG_BACKUPDIRECTORY)),-3);
  164. if(substr($CONFIG_BACKUPDIRECTORY,2,1)!='0'){
  165. chmodr($CONFIG_BACKUPDIRECTORY,0770);
  166. clearstatcache();
  167. $prems=substr(decoct(fileperms($CONFIG_BACKUPDIRECTORY)),-3);
  168. if(substr($prems,2,1)!='0'){
  169. $error.='Data directory ('.$CONFIG_BACKUPDIRECTORY.') is readable from the web<br/>';
  170. }
  171. }
  172. }
  173. if($error){
  174. die($error);
  175. }
  176. }
  177. /**
  178. * show the header of the web GUI
  179. *
  180. */
  181. public static function showheader(){
  182. global $CONFIG_ADMINLOGIN;
  183. global $WEBROOT;
  184. oc_require('templates/header.php');;
  185. }
  186. /**
  187. * check if we need to use the layout optimized for smaller screen, currently only checks for iPhone/Android
  188. * @return bool
  189. */
  190. public static function hasSmallScreen(){
  191. $userAgent=strtolower($_SERVER['HTTP_USER_AGENT']);
  192. if(strpos($userAgent,'android') or strpos($userAgent,'iphone') or strpos($userAgent,'ipod')){//todo, add support for more devices
  193. return true;
  194. }
  195. return false;
  196. }
  197. /**
  198. * show the footer of the web GUI
  199. *
  200. */
  201. public static function showfooter(){
  202. global $CONFIG_FOOTEROWNERNAME;
  203. global $CONFIG_FOOTEROWNEREMAIL;
  204. oc_require('templates/footer.php');;
  205. }
  206. /**
  207. * add an navigationentry to the main navigation
  208. *
  209. * @param name $name
  210. * @param url $url
  211. */
  212. public static function addnavigationentry($name,$url) {
  213. $entry=array();
  214. $entry['name']=$name;
  215. $entry['url']=$url;
  216. OC_UTIL::$NAVIGATION[]=$entry;
  217. }
  218. /**
  219. * show the main navigation
  220. *
  221. */
  222. public static function shownavigation(){
  223. global $WEBROOT;
  224. global $SERVERROOT;
  225. echo('<table class="center" cellpadding="5" cellspacing="0" border="0"><tr>');
  226. echo('<td class="navigationitem1"><a href="'.$WEBROOT.'/">'.$_SESSION['username'].'</a></td>');
  227. 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>');
  228. foreach(OC_UTIL::$NAVIGATION as $NAVI) {
  229. 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>');
  230. }
  231. 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>');
  232. 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>');
  233. if(OC_USER::ingroup($_SESSION['username'],'admin')){
  234. 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>');
  235. }
  236. echo('<td class="navigationitem"><a href="?logoutbutton=1">Logout</a></td>');
  237. echo('</tr></table>');
  238. }
  239. /**
  240. * show the loginform
  241. *
  242. */
  243. public static function showloginform(){
  244. global $loginresult;
  245. oc_require('templates/loginform.php');
  246. }
  247. /**
  248. * show an icon for a filetype
  249. *
  250. */
  251. public static function showicon($filetype){
  252. global $WEBROOT;
  253. if($filetype=='dir'){ echo('<td><img src="'.$WEBROOT.'/img/icons/folder.png" width="16" height="16"></td>');
  254. }elseif($filetype=='foo'){ echo('<td>foo</td>');
  255. }else{ echo('<td><img src="'.$WEBROOT.'/img/icons/other.png" width="16" height="16"></td>');
  256. }
  257. }
  258. }
  259. /**
  260. * Class for database access
  261. *
  262. */
  263. class OC_DB {
  264. static private $DBConnection=false;
  265. /**
  266. * connect to the datbase if not already connected
  267. */
  268. public static function connect(){
  269. global $CONFIG_DBNAME;
  270. global $CONFIG_DBHOST;
  271. global $CONFIG_DBUSER;
  272. global $CONFIG_DBPASSWORD;
  273. global $CONFIG_DBTYPE;
  274. global $DOCUMENTROOT;
  275. global $SERVERROOT;
  276. if(!self::$DBConnection){
  277. $options = array(
  278. 'debug' => 0,
  279. 'portability' => MDB2_PORTABILITY_ALL,
  280. );
  281. if($CONFIG_DBTYPE=='sqlite'){
  282. $dsn = array(
  283. 'phptype' => 'sqlite',
  284. 'database' => $SERVERROOT.'/'.$CONFIG_DBNAME,
  285. 'mode' => '0644',
  286. );
  287. }elseif($CONFIG_DBTYPE=='mysql'){
  288. $dsn = array(
  289. 'phptype' => 'mysql',
  290. 'username' => $CONFIG_DBUSER,
  291. 'password' => $CONFIG_DBPASSWORD,
  292. 'hostspec' => $CONFIG_DBHOST,
  293. 'database' => $CONFIG_DBNAME,
  294. );
  295. }
  296. self::$DBConnection=MDB2::connect($dsn,$options);
  297. if (@PEAR::isError(self::$DBConnection)) {
  298. echo('<b>can not connect to database, using '.$CONFIG_DBTYPE.'. ('.self::$DBConnection->getUserInfo().')</center>');
  299. die(self::$DBConnection->getMessage());
  300. }
  301. self::$DBConnection->setFetchMode(MDB2_FETCHMODE_ASSOC);
  302. // self::$DBConnection->loadModule('Manager');
  303. }
  304. }
  305. /**
  306. * executes a query on the database
  307. *
  308. * @param string $cmd
  309. * @return result-set
  310. */
  311. static function query($cmd){
  312. global $CONFIG_DBTYPE;
  313. if(!trim($cmd)){
  314. return false;
  315. }
  316. OC_DB::connect();
  317. if($CONFIG_DBTYPE=='sqlite'){//fix differences between sql versions
  318. $cmd=str_replace('`','',$cmd);
  319. }
  320. $result=self::$DBConnection->query($cmd);
  321. if (PEAR::isError($result)) {
  322. $entry='DB Error: "'.$result->getMessage().'"<br />';
  323. $entry.='Offending command was: '.$cmd.'<br />';
  324. die($entry);
  325. }
  326. return $result;
  327. }
  328. /**
  329. * executes a query on the database and returns the result in an array
  330. *
  331. * @param string $cmd
  332. * @return result-set
  333. */
  334. static function select($cmd){
  335. OC_DB::connect();
  336. return self::$DBConnection->queryAll($cmd);
  337. }
  338. /**
  339. * executes multiply queries on the database
  340. *
  341. * @param string $cmd
  342. * @return result-set
  343. */
  344. static function multiquery($cmd) {
  345. $queries=explode(';',$cmd);
  346. foreach($queries as $query){
  347. OC_DB::query($query);
  348. }
  349. return true;
  350. }
  351. /**
  352. * closing a db connection
  353. *
  354. * @return bool
  355. */
  356. static function close() {
  357. self::$DBConnection->disconnect();
  358. self::$DBConnection=false;
  359. }
  360. /**
  361. * Returning primarykey if last statement was an insert.
  362. *
  363. * @return primarykey
  364. */
  365. static function insertid() {
  366. global $CONFIG_DBTYPE;
  367. if($CONFIG_DBTYPE=='sqlite'){
  368. return self::$DBConnection->lastInsertRowid();
  369. }elseif($CONFIG_DBTYPE=='mysql'){
  370. return(mysqli_insert_id(self::$DBConnection));
  371. }
  372. }
  373. /**
  374. * Returning number of rows in a result
  375. *
  376. * @param resultset $result
  377. * @return int
  378. */
  379. static function numrows($result) {
  380. $result->numRows();
  381. }
  382. /**
  383. * Returning number of affected rows
  384. *
  385. * @return int
  386. */
  387. static function affected_rows() {
  388. self::$DBConnection->affectedRows();
  389. }
  390. /**
  391. * Freeing resultset (performance)
  392. *
  393. * @param unknown_type $result
  394. * @return bool
  395. */
  396. static function free_result($result) {
  397. $result->free();
  398. }
  399. static public function disconnect(){
  400. if(self::$DBConnection){
  401. self::$DBConnection->disconnect();
  402. self::$DBConnection=false;
  403. }
  404. }
  405. static public function createTable($name,$definition){
  406. self::connect();
  407. self::$DBConnection->createTable($name,$definition);
  408. }
  409. static public function createConstraint($table,$name,$definition){
  410. self::connect();
  411. self::$DBConnection->createConstraint($table,$name,$definition);
  412. }
  413. }
  414. //custom require/include functions because not all hosts allow us to set the include path
  415. function oc_require($file){
  416. global $SERVERROOT;
  417. global $DOCUMENTROOT;
  418. global $WEBROOT;
  419. global $CONFIG_DBNAME;
  420. global $CONFIG_DBHOST;
  421. global $CONFIG_DBUSER;
  422. global $CONFIG_DBPASSWORD;
  423. global $CONFIG_DBTYPE;
  424. global $CONFIG_DATADIRECTORY;
  425. global $CONFIG_HTTPFORCESSL;
  426. global $CONFIG_DATEFORMAT;
  427. global $CONFIG_INSTALLED;
  428. if(is_file($file)){
  429. return require($file);
  430. }elseif(is_file($SERVERROOT.'/'.$file)){
  431. return require($SERVERROOT.'/'.$file);
  432. }elseif(is_file($SERVERROOT.'/inc/'.$file)){
  433. return require($SERVERROOT.'/inc/'.$file);
  434. }
  435. }
  436. function oc_require_once($file){
  437. global $SERVERROOT;
  438. global $DOCUMENTROOT;
  439. global $WEBROOT;
  440. global $CONFIG_DBNAME;
  441. global $CONFIG_DBHOST;
  442. global $CONFIG_DBUSER;
  443. global $CONFIG_DBPASSWORD;
  444. global $CONFIG_DBTYPE;
  445. global $CONFIG_DATADIRECTORY;
  446. global $CONFIG_HTTPFORCESSL;
  447. global $CONFIG_DATEFORMAT;
  448. global $CONFIG_INSTALLED;
  449. if(is_file($file)){
  450. return require_once($file);
  451. }elseif(is_file($SERVERROOT.'/'.$file)){
  452. return require_once($SERVERROOT.'/'.$file);
  453. }elseif(is_file($SERVERROOT.'/inc/'.$file)){
  454. return require_once($SERVERROOT.'/inc/'.$file);
  455. }
  456. }
  457. function oc_include($file){
  458. global $SERVERROOT;
  459. global $DOCUMENTROOT;
  460. global $WEBROOT;
  461. global $CONFIG_DBNAME;
  462. global $CONFIG_DBHOST;
  463. global $CONFIG_DBUSER;
  464. global $CONFIG_DBPASSWORD;
  465. global $CONFIG_DBTYPE;
  466. global $CONFIG_DATADIRECTORY;
  467. global $CONFIG_HTTPFORCESSL;
  468. global $CONFIG_DATEFORMAT;
  469. global $CONFIG_INSTALLED;
  470. if(is_file($file)){
  471. return include($file);
  472. }elseif(is_file($SERVERROOT.'/'.$file)){
  473. return include($SERVERROOT.'/'.$file);
  474. }elseif(is_file($SERVERROOT.'/inc/'.$file)){
  475. return include($SERVERROOT.'/inc/'.$file);
  476. }
  477. }
  478. function oc_include_once($file){
  479. global $SERVERROOT;
  480. global $DOCUMENTROOT;
  481. global $WEBROOT;
  482. global $CONFIG_DBNAME;
  483. global $CONFIG_DBHOST;
  484. global $CONFIG_DBUSER;
  485. global $CONFIG_DBPASSWORD;
  486. global $CONFIG_DBTYPE;
  487. global $CONFIG_DATADIRECTORY;
  488. global $CONFIG_HTTPFORCESSL;
  489. global $CONFIG_DATEFORMAT;
  490. global $CONFIG_INSTALLED;
  491. if(is_file($file)){
  492. return include_once($file);
  493. }elseif(is_file($SERVERROOT.'/'.$file)){
  494. return include_once($SERVERROOT.'/'.$file);
  495. }elseif(is_file($SERVERROOT.'/inc/'.$file)){
  496. return include_once($SERVERROOT.'/inc/'.$file);
  497. }
  498. }
  499. function chmodr($path, $filemode) {
  500. // echo "$path<br/>";
  501. if (!is_dir($path))
  502. return chmod($path, $filemode);
  503. $dh = opendir($path);
  504. while (($file = readdir($dh)) !== false) {
  505. if($file != '.' && $file != '..') {
  506. $fullpath = $path.'/'.$file;
  507. if(is_link($fullpath))
  508. return FALSE;
  509. elseif(!is_dir($fullpath) && !chmod($fullpath, $filemode))
  510. return FALSE;
  511. elseif(!chmodr($fullpath, $filemode))
  512. return FALSE;
  513. }
  514. }
  515. closedir($dh);
  516. if(chmod($path, $filemode))
  517. return TRUE;
  518. else
  519. return FALSE;
  520. }
  521. ?>