base.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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 Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. /**
  23. * Class that is a namespace for all global OC variables
  24. * No, we can not put this class in its own file because it is used by
  25. * OC_autoload!
  26. */
  27. class OC{
  28. /**
  29. * Assoziative array for autoloading. classname => filename
  30. */
  31. public static $CLASSPATH = array();
  32. /**
  33. * $_SERVER['DOCUMENTROOT'] but without symlinks
  34. */
  35. public static $DOCUMENTROOT = '';
  36. /**
  37. * The installation path for owncloud on the server (e.g. /srv/http/owncloud)
  38. */
  39. public static $SERVERROOT = '';
  40. /**
  41. * the current request path relative to the owncloud root (e.g. files/index.php)
  42. */
  43. public static $SUBURI = '';
  44. /**
  45. * the owncloud root path for http requests (e.g. owncloud/)
  46. */
  47. public static $WEBROOT = '';
  48. /**
  49. * the folder that stores that data files for the filesystem of the user (e.g. /srv/http/owncloud/data/myusername/files)
  50. */
  51. public static $CONFIG_DATADIRECTORY = '';
  52. /**
  53. * the folder that stores the data for the root filesystem (e.g. /srv/http/owncloud/data)
  54. */
  55. public static $CONFIG_DATADIRECTORY_ROOT = '';
  56. /**
  57. * The installation path of the 3rdparty folder on the server (e.g. /srv/http/owncloud/3rdparty)
  58. */
  59. public static $THIRDPARTYROOT = '';
  60. /**
  61. * the root path of the 3rdparty folder for http requests (e.g. owncloud/3rdparty)
  62. */
  63. public static $THIRDPARTYWEBROOT = '';
  64. /**
  65. * The installation path of the apps folder on the server (e.g. /srv/http/owncloud)
  66. */
  67. public static $APPSROOT = '';
  68. /**
  69. * the root path of the apps folder for http requests (e.g. owncloud)
  70. */
  71. public static $APPSWEBROOT = '';
  72. /*
  73. * requested app
  74. */
  75. public static $REQUESTEDAPP = '';
  76. /*
  77. * requested file of app
  78. */
  79. public static $REQUESTEDFILE = '';
  80. /**
  81. * SPL autoload
  82. */
  83. public static function autoload($className){
  84. if(array_key_exists($className,OC::$CLASSPATH)){
  85. require_once OC::$CLASSPATH[$className];
  86. }
  87. elseif(strpos($className,'OC_')===0){
  88. require_once strtolower(str_replace('_','/',substr($className,3)) . '.php');
  89. }
  90. elseif(strpos($className,'OCP\\')===0){
  91. require_once 'public/'.strtolower(str_replace('\\','/',substr($className,3)) . '.php');
  92. }
  93. elseif(strpos($className,'Sabre_')===0) {
  94. require_once str_replace('_','/',$className) . '.php';
  95. }
  96. elseif(strpos($className,'Test_')===0){
  97. require_once 'tests/lib/'.strtolower(str_replace('_','/',substr($className,5)) . '.php');
  98. }
  99. }
  100. /**
  101. * autodetects the formfactor of the used device
  102. * default -> the normal desktop browser interface
  103. * mobile -> interface for smartphones
  104. * tablet -> interface for tablets
  105. * standalone -> the default interface but without header, footer and sidebar. just the application. useful to ue just a specific app on the desktop in a standalone window.
  106. */
  107. public static function detectFormfactor(){
  108. // please add more useragent strings for other devices
  109. if(isset($_SERVER['HTTP_USER_AGENT'])){
  110. if(stripos($_SERVER['HTTP_USER_AGENT'],'ipad')>0) {
  111. $mode='tablet';
  112. }elseif(stripos($_SERVER['HTTP_USER_AGENT'],'iphone')>0){
  113. $mode='mobile';
  114. }elseif((stripos($_SERVER['HTTP_USER_AGENT'],'N9')>0) and (stripos($_SERVER['HTTP_USER_AGENT'],'nokia')>0)){
  115. $mode='mobile';
  116. }else{
  117. $mode='default';
  118. }
  119. }else{
  120. $mode='default';
  121. }
  122. return($mode);
  123. }
  124. public static function initPaths(){
  125. // calculate the documentroot
  126. OC::$DOCUMENTROOT=realpath($_SERVER['DOCUMENT_ROOT']);
  127. OC::$SERVERROOT=str_replace("\\",'/',substr(__FILE__,0,-13));
  128. OC::$SUBURI=substr(realpath($_SERVER["SCRIPT_FILENAME"]),strlen(OC::$SERVERROOT));
  129. $scriptName=$_SERVER["SCRIPT_NAME"];
  130. if(substr($scriptName,-1)=='/'){
  131. $scriptName.='index.php';
  132. //make sure suburi follows the same rules as scriptName
  133. if(substr(OC::$SUBURI,-9)!='index.php'){
  134. if(substr(OC::$SUBURI,-1)!='/'){
  135. OC::$SUBURI=OC::$SUBURI.'/';
  136. }
  137. OC::$SUBURI=OC::$SUBURI.'index.php';
  138. }
  139. }
  140. OC::$WEBROOT=substr($scriptName,0,strlen($scriptName)-strlen(OC::$SUBURI));
  141. // try a new way to detect the WEBROOT which is simpler and also works with the app directory outside the owncloud folder. let´s see if this works for everybody
  142. // OC::$WEBROOT=substr(OC::$SERVERROOT,strlen(OC::$DOCUMENTROOT));
  143. if(OC::$WEBROOT!='' and OC::$WEBROOT[0]!=='/'){
  144. OC::$WEBROOT='/'.OC::$WEBROOT;
  145. }
  146. // ensure we can find OC_Config
  147. set_include_path(
  148. OC::$SERVERROOT.'/lib'.PATH_SEPARATOR.
  149. get_include_path()
  150. );
  151. // search the 3rdparty folder
  152. if(OC_Config::getValue('3rdpartyroot', '')<>'' and OC_Config::getValue('3rdpartyurl', '')<>''){
  153. OC::$THIRDPARTYROOT=OC_Config::getValue('3rdpartyroot', '');
  154. OC::$THIRDPARTYWEBROOT=OC_Config::getValue('3rdpartyurl', '');
  155. }elseif(file_exists(OC::$SERVERROOT.'/3rdparty')){
  156. OC::$THIRDPARTYROOT=OC::$SERVERROOT;
  157. OC::$THIRDPARTYWEBROOT=OC::$WEBROOT;
  158. }elseif(file_exists(OC::$SERVERROOT.'/../3rdparty')){
  159. OC::$THIRDPARTYWEBROOT=rtrim(dirname(OC::$WEBROOT), '/');
  160. OC::$THIRDPARTYROOT=rtrim(dirname(OC::$SERVERROOT), '/');
  161. }else{
  162. echo("3rdparty directory not found! Please put the ownCloud 3rdparty folder in the ownCloud folder or the folder above. You can also configure the location in the config.php file.");
  163. exit;
  164. }
  165. // search the apps folder
  166. if(OC_Config::getValue('appsroot', '')<>''){
  167. OC::$APPSROOT=OC_Config::getValue('appsroot', '');
  168. OC::$APPSWEBROOT=OC_Config::getValue('appsurl', '');
  169. }elseif(file_exists(OC::$SERVERROOT.'/apps')){
  170. OC::$APPSROOT=OC::$SERVERROOT;
  171. OC::$APPSWEBROOT=OC::$WEBROOT;
  172. }elseif(file_exists(OC::$SERVERROOT.'/../apps')){
  173. OC::$APPSROOT=rtrim(dirname(OC::$SERVERROOT), '/');
  174. OC::$APPSWEBROOT=rtrim(dirname(OC::$WEBROOT), '/');
  175. }else{
  176. echo("apps directory not found! Please put the ownCloud apps folder in the ownCloud folder or the folder above. You can also configure the location in the config.php file.");
  177. exit;
  178. }
  179. // set the right include path
  180. set_include_path(
  181. OC::$SERVERROOT.'/lib'.PATH_SEPARATOR.
  182. OC::$SERVERROOT.'/config'.PATH_SEPARATOR.
  183. OC::$THIRDPARTYROOT.'/3rdparty'.PATH_SEPARATOR.
  184. OC::$APPSROOT.PATH_SEPARATOR.
  185. OC::$APPSROOT.'/apps'.PATH_SEPARATOR.
  186. get_include_path().PATH_SEPARATOR.
  187. OC::$SERVERROOT
  188. );
  189. }
  190. public static function checkInstalled() {
  191. // Redirect to installer if not installed
  192. if (!OC_Config::getValue('installed', false) && OC::$SUBURI != '/index.php') {
  193. $url = 'http://'.$_SERVER['SERVER_NAME'].OC::$WEBROOT.'/index.php';
  194. header("Location: $url");
  195. exit();
  196. }
  197. }
  198. public static function checkSSL() {
  199. // redirect to https site if configured
  200. if( OC_Config::getValue( "forcessl", false )){
  201. ini_set("session.cookie_secure", "on");
  202. if(!isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] != 'on') {
  203. $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  204. header("Location: $url");
  205. exit();
  206. }
  207. }
  208. }
  209. public static function checkUpgrade() {
  210. if(OC_Config::getValue('installed', false)){
  211. $installedVersion=OC_Config::getValue('version','0.0.0');
  212. $currentVersion=implode('.',OC_Util::getVersion());
  213. if (version_compare($currentVersion, $installedVersion, '>')) {
  214. $result=OC_DB::updateDbFromStructure(OC::$SERVERROOT.'/db_structure.xml');
  215. if(!$result){
  216. echo 'Error while upgrading the database';
  217. die();
  218. }
  219. if(file_exists(OC::$SERVERROOT."/config/config.php") and !is_writable(OC::$SERVERROOT."/config/config.php")) {
  220. $tmpl = new OC_Template( '', 'error', 'guest' );
  221. $tmpl->assign('errors',array(1=>array('error'=>"Can't write into config directory 'config'",'hint'=>"You can usually fix this by giving the webserver user write access to the config directory in owncloud")));
  222. $tmpl->printPage();
  223. exit;
  224. }
  225. OC_Config::setValue('version',implode('.',OC_Util::getVersion()));
  226. }
  227. OC_App::updateApps();
  228. }
  229. }
  230. public static function initTemplateEngine() {
  231. // if the formfactor is not yet autodetected do the autodetection now. For possible forfactors check the detectFormfactor documentation
  232. if(!isset($_SESSION['formfactor'])){
  233. $_SESSION['formfactor']=OC::detectFormfactor();
  234. }
  235. // allow manual override via GET parameter
  236. if(isset($_GET['formfactor'])){
  237. $_SESSION['formfactor']=$_GET['formfactor'];
  238. }
  239. // Add the stuff we need always
  240. OC_Util::addScript( "jquery-1.6.4.min" );
  241. OC_Util::addScript( "jquery-ui-1.8.16.custom.min" );
  242. OC_Util::addScript( "jquery-showpassword" );
  243. OC_Util::addScript( "jquery.infieldlabel.min" );
  244. OC_Util::addScript( "jquery-tipsy" );
  245. OC_Util::addScript( "oc-dialogs" );
  246. OC_Util::addScript( "js" );
  247. OC_Util::addScript( "eventsource" );
  248. OC_Util::addScript( "config" );
  249. //OC_Util::addScript( "multiselect" );
  250. OC_Util::addScript('search','result');
  251. OC_Util::addStyle( "styles" );
  252. OC_Util::addStyle( "multiselect" );
  253. OC_Util::addStyle( "jquery-ui-1.8.16.custom" );
  254. OC_Util::addStyle( "jquery-tipsy" );
  255. }
  256. public static function initSession() {
  257. ini_set('session.cookie_httponly','1;');
  258. session_start();
  259. }
  260. public static function loadapp(){
  261. if(file_exists(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/index.php')){
  262. require_once(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/index.php');
  263. }else{
  264. trigger_error('The requested App was not found.', E_USER_ERROR);//load default app instead?
  265. }
  266. }
  267. public static function loadfile(){
  268. if(file_exists(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/' . OC::$REQUESTEDFILE)){
  269. if(substr(OC::$REQUESTEDFILE, -3) == 'css'){
  270. $appswebroot = (string) OC::$APPSWEBROOT;
  271. $webroot = (string) OC::$WEBROOT;
  272. $cssfile = file_get_contents(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/' . OC::$REQUESTEDFILE);
  273. $cssfile = str_replace('%appswebroot%', $appswebroot, $cssfile);
  274. $cssfile = str_replace('%webroot%', $webroot, $cssfile);
  275. header('Content-Type: text/css');
  276. echo $cssfile;
  277. exit;
  278. }elseif(substr(OC::$REQUESTEDFILE, -3) == 'php'){
  279. require_once(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/' . OC::$REQUESTEDFILE);
  280. }
  281. }else{
  282. header('HTTP/1.0 404 Not Found');
  283. exit;
  284. }
  285. }
  286. public static function init(){
  287. // register autoloader
  288. spl_autoload_register(array('OC','autoload'));
  289. setlocale(LC_ALL, 'en_US.UTF-8');
  290. // set some stuff
  291. //ob_start();
  292. error_reporting(E_ALL | E_STRICT);
  293. if (defined('DEBUG') && DEBUG){
  294. ini_set('display_errors', 1);
  295. }
  296. date_default_timezone_set('Europe/Berlin');
  297. ini_set('arg_separator.output','&amp;');
  298. //try to configure php to enable big file uploads.
  299. //this doesn´t work always depending on the webserver and php configuration.
  300. //Let´s try to overwrite some defaults anyways
  301. //try to set the maximum execution time to 60min
  302. @set_time_limit(3600);
  303. @ini_set('max_execution_time',3600);
  304. @ini_set('max_input_time',3600);
  305. //try to set the maximum filesize to 10G
  306. @ini_set('upload_max_filesize','10G');
  307. @ini_set('post_max_size','10G');
  308. @ini_set('file_uploads','50');
  309. //try to set the session lifetime to 60min
  310. @ini_set('gc_maxlifetime','3600');
  311. //set http auth headers for apache+php-cgi work around
  312. if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'], $matches))
  313. {
  314. list($name, $password) = explode(':', base64_decode($matches[1]));
  315. $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
  316. $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
  317. }
  318. //set http auth headers for apache+php-cgi work around if variable gets renamed by apache
  319. if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches))
  320. {
  321. list($name, $password) = explode(':', base64_decode($matches[1]));
  322. $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
  323. $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
  324. }
  325. self::initPaths();
  326. // register the stream wrappers
  327. require_once('streamwrappers.php');
  328. stream_wrapper_register("fakedir", "OC_FakeDirStream");
  329. stream_wrapper_register('static', 'OC_StaticStreamWrapper');
  330. stream_wrapper_register('close', 'OC_CloseStreamWrapper');
  331. self::checkInstalled();
  332. self::checkSSL();
  333. // CSRF protection
  334. if(isset($_SERVER['HTTP_REFERER'])) $referer=$_SERVER['HTTP_REFERER']; else $referer='';
  335. if(isset($_SERVER['HTTPS']) and $_SERVER['HTTPS']<>'') $protocol='https://'; else $protocol='http://';
  336. $server=$protocol.$_SERVER['SERVER_NAME'];
  337. if(($_SERVER['REQUEST_METHOD']=='POST') and (substr($referer,0,strlen($server))<>$server)) {
  338. $url = $protocol.$_SERVER['SERVER_NAME'].OC::$WEBROOT.'/index.php';
  339. header("Location: $url");
  340. exit();
  341. }
  342. self::initSession();
  343. self::initTemplateEngine();
  344. self::checkUpgrade();
  345. $errors=OC_Util::checkServer();
  346. if(count($errors)>0) {
  347. OC_Template::printGuestPage('', 'error', array('errors' => $errors));
  348. exit;
  349. }
  350. // TODO: we should get rid of this one, too
  351. // WARNING: to make everything even more confusing,
  352. // DATADIRECTORY is a var that changes and DATADIRECTORY_ROOT
  353. // stays the same, but is set by "datadirectory".
  354. // Any questions?
  355. OC::$CONFIG_DATADIRECTORY = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
  356. // User and Groups
  357. if( !OC_Config::getValue( "installed", false )){
  358. $_SESSION['user_id'] = '';
  359. }
  360. OC_User::useBackend( OC_Config::getValue( "userbackend", "database" ));
  361. OC_Group::useBackend(new OC_Group_Database());
  362. // Set up file system unless forbidden
  363. global $RUNTIME_NOSETUPFS;
  364. if(!$RUNTIME_NOSETUPFS ){
  365. OC_Util::setupFS();
  366. }
  367. // Load Apps
  368. // This includes plugins for users and filesystems as well
  369. global $RUNTIME_NOAPPS;
  370. global $RUNTIME_APPTYPES;
  371. if(!$RUNTIME_NOAPPS ){
  372. if($RUNTIME_APPTYPES){
  373. OC_App::loadApps($RUNTIME_APPTYPES);
  374. }else{
  375. OC_App::loadApps();
  376. }
  377. }
  378. // Check for blacklisted files
  379. OC_Hook::connect('OC_Filesystem','write','OC_Filesystem','isBlacklisted');
  380. //make sure temporary files are cleaned up
  381. register_shutdown_function(array('OC_Helper','cleanTmp'));
  382. //parse the given parameters
  383. self::$REQUESTEDAPP = (isset($_GET['app'])?strip_tags($_GET['app']):'files');
  384. if(substr_count(self::$REQUESTEDAPP, '?') != 0){
  385. $app = substr(self::$REQUESTEDAPP, 0, strpos(self::$REQUESTEDAPP, '?'));
  386. $param = substr(self::$REQUESTEDAPP, strpos(self::$REQUESTEDAPP, '?') + 1);
  387. parse_str($param, $get);
  388. $_GET = array_merge($_GET, $get);
  389. self::$REQUESTEDAPP = $app;
  390. $_GET['app'] = $app;
  391. }
  392. self::$REQUESTEDFILE = (isset($_GET['getfile'])?$_GET['getfile']:null);
  393. if(substr_count(self::$REQUESTEDFILE, '?') != 0){
  394. $file = substr(self::$REQUESTEDFILE, 0, strpos(self::$REQUESTEDFILE, '?'));
  395. $param = substr(self::$REQUESTEDFILE, strpos(self::$REQUESTEDFILE, '?') + 1);
  396. parse_str($param, $get);
  397. $_GET = array_merge($_GET, $get);
  398. self::$REQUESTEDFILE = $file;
  399. $_GET['getfile'] = $file;
  400. }
  401. if(!is_null(self::$REQUESTEDFILE)){
  402. $subdir = OC::$APPSROOT . '/' . self::$REQUESTEDAPP . '/' . self::$REQUESTEDFILE;
  403. $parent = OC::$APPSROOT . '/' . self::$REQUESTEDAPP;
  404. if(!OC_Helper::issubdirectory($subdir, $parent)){
  405. self::$REQUESTEDFILE = null;
  406. header('HTTP/1.0 404 Not Found');
  407. exit;
  408. }
  409. }
  410. }
  411. }
  412. // define runtime variables - unless this already has been done
  413. if( !isset( $RUNTIME_NOSETUPFS )){
  414. $RUNTIME_NOSETUPFS = false;
  415. }
  416. if( !isset( $RUNTIME_NOAPPS )){
  417. $RUNTIME_NOAPPS = false;
  418. }
  419. if(!function_exists('get_temp_dir')) {
  420. function get_temp_dir() {
  421. if( $temp=ini_get('upload_tmp_dir') ) return $temp;
  422. if( $temp=getenv('TMP') ) return $temp;
  423. if( $temp=getenv('TEMP') ) return $temp;
  424. if( $temp=getenv('TMPDIR') ) return $temp;
  425. $temp=tempnam(__FILE__,'');
  426. if (file_exists($temp)) {
  427. unlink($temp);
  428. return dirname($temp);
  429. }
  430. if( $temp=sys_get_temp_dir()) return $temp;
  431. return null;
  432. }
  433. }
  434. OC::init();