base.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @copyright 2012 Frank Karlitschek frank@owncloud.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. * The installation path for owncloud on the server (e.g. /srv/http/owncloud)
  34. */
  35. public static $SERVERROOT = '';
  36. /**
  37. * the current request path relative to the owncloud root (e.g. files/index.php)
  38. */
  39. private static $SUBURI = '';
  40. /**
  41. * the owncloud root path for http requests (e.g. owncloud/)
  42. */
  43. public static $WEBROOT = '';
  44. /**
  45. * The installation path of the 3rdparty folder on the server (e.g. /srv/http/owncloud/3rdparty)
  46. */
  47. public static $THIRDPARTYROOT = '';
  48. /**
  49. * the root path of the 3rdparty folder for http requests (e.g. owncloud/3rdparty)
  50. */
  51. public static $THIRDPARTYWEBROOT = '';
  52. /**
  53. * The installation path of the apps folder on the server (e.g. /srv/http/owncloud)
  54. */
  55. public static $APPSROOT = '';
  56. /**
  57. * the root path of the apps folder for http requests (e.g. owncloud)
  58. */
  59. public static $APPSWEBROOT = '';
  60. /*
  61. * requested app
  62. */
  63. public static $REQUESTEDAPP = '';
  64. /*
  65. * requested file of app
  66. */
  67. public static $REQUESTEDFILE = '';
  68. /**
  69. * check if owncloud runs in cli mode
  70. */
  71. public static $CLI = false;
  72. /**
  73. * SPL autoload
  74. */
  75. public static function autoload($className){
  76. if(array_key_exists($className,OC::$CLASSPATH)){
  77. require_once OC::$CLASSPATH[$className];
  78. }
  79. elseif(strpos($className,'OC_')===0){
  80. require_once strtolower(str_replace('_','/',substr($className,3)) . '.php');
  81. }
  82. elseif(strpos($className,'OCP\\')===0){
  83. require_once 'public/'.strtolower(str_replace('\\','/',substr($className,3)) . '.php');
  84. }
  85. elseif(strpos($className,'Sabre_')===0) {
  86. require_once str_replace('_','/',$className) . '.php';
  87. }
  88. elseif(strpos($className,'Test_')===0){
  89. require_once 'tests/lib/'.strtolower(str_replace('_','/',substr($className,5)) . '.php');
  90. }
  91. }
  92. public static function initPaths(){
  93. // calculate the root directories
  94. OC::$SERVERROOT=str_replace("\\",'/',substr(__FILE__,0,-13));
  95. OC::$SUBURI= str_replace("\\","/",substr(realpath($_SERVER["SCRIPT_FILENAME"]),strlen(OC::$SERVERROOT)));
  96. $scriptName=$_SERVER["SCRIPT_NAME"];
  97. if(substr($scriptName,-1)=='/'){
  98. $scriptName.='index.php';
  99. //make sure suburi follows the same rules as scriptName
  100. if(substr(OC::$SUBURI,-9)!='index.php'){
  101. if(substr(OC::$SUBURI,-1)!='/'){
  102. OC::$SUBURI=OC::$SUBURI.'/';
  103. }
  104. OC::$SUBURI=OC::$SUBURI.'index.php';
  105. }
  106. }
  107. OC::$WEBROOT=substr($scriptName,0,strlen($scriptName)-strlen(OC::$SUBURI));
  108. if(OC::$WEBROOT!='' and OC::$WEBROOT[0]!=='/'){
  109. OC::$WEBROOT='/'.OC::$WEBROOT;
  110. }
  111. // ensure we can find OC_Config
  112. set_include_path(
  113. OC::$SERVERROOT.'/lib'.PATH_SEPARATOR.
  114. get_include_path()
  115. );
  116. // search the 3rdparty folder
  117. if(OC_Config::getValue('3rdpartyroot', '')<>'' and OC_Config::getValue('3rdpartyurl', '')<>''){
  118. OC::$THIRDPARTYROOT=OC_Config::getValue('3rdpartyroot', '');
  119. OC::$THIRDPARTYWEBROOT=OC_Config::getValue('3rdpartyurl', '');
  120. }elseif(file_exists(OC::$SERVERROOT.'/3rdparty')){
  121. OC::$THIRDPARTYROOT=OC::$SERVERROOT;
  122. OC::$THIRDPARTYWEBROOT=OC::$WEBROOT;
  123. }elseif(file_exists(OC::$SERVERROOT.'/../3rdparty')){
  124. OC::$THIRDPARTYWEBROOT=rtrim(dirname(OC::$WEBROOT), '/');
  125. OC::$THIRDPARTYROOT=rtrim(dirname(OC::$SERVERROOT), '/');
  126. }else{
  127. 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.");
  128. exit;
  129. }
  130. // search the apps folder
  131. if(OC_Config::getValue('appsroot', '')<>''){
  132. OC::$APPSROOT=OC_Config::getValue('appsroot', '');
  133. OC::$APPSWEBROOT=OC_Config::getValue('appsurl', '');
  134. }elseif(file_exists(OC::$SERVERROOT.'/apps')){
  135. OC::$APPSROOT=OC::$SERVERROOT;
  136. OC::$APPSWEBROOT=OC::$WEBROOT;
  137. }elseif(file_exists(OC::$SERVERROOT.'/../apps')){
  138. OC::$APPSROOT=rtrim(dirname(OC::$SERVERROOT), '/');
  139. OC::$APPSWEBROOT=rtrim(dirname(OC::$WEBROOT), '/');
  140. }else{
  141. 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.");
  142. exit;
  143. }
  144. // set the right include path
  145. set_include_path(
  146. OC::$SERVERROOT.'/lib'.PATH_SEPARATOR.
  147. OC::$SERVERROOT.'/config'.PATH_SEPARATOR.
  148. OC::$THIRDPARTYROOT.'/3rdparty'.PATH_SEPARATOR.
  149. OC::$APPSROOT.PATH_SEPARATOR.
  150. OC::$APPSROOT.'/apps'.PATH_SEPARATOR.
  151. get_include_path().PATH_SEPARATOR.
  152. OC::$SERVERROOT
  153. );
  154. }
  155. public static function checkInstalled() {
  156. // Redirect to installer if not installed
  157. if (!OC_Config::getValue('installed', false) && OC::$SUBURI != '/index.php') {
  158. $url = 'http://'.$_SERVER['SERVER_NAME'].OC::$WEBROOT.'/index.php';
  159. header("Location: $url");
  160. exit();
  161. }
  162. }
  163. public static function checkSSL() {
  164. // redirect to https site if configured
  165. if( OC_Config::getValue( "forcessl", false )){
  166. ini_set("session.cookie_secure", "on");
  167. if(OC_Helper::serverProtocol()<>'https') {
  168. $url = "https://". OC_Helper::serverHost() . $_SERVER['REQUEST_URI'];
  169. header("Location: $url");
  170. exit();
  171. }
  172. }
  173. }
  174. public static function checkUpgrade() {
  175. if(OC_Config::getValue('installed', false)){
  176. $installedVersion=OC_Config::getValue('version','0.0.0');
  177. $currentVersion=implode('.',OC_Util::getVersion());
  178. if (version_compare($currentVersion, $installedVersion, '>')) {
  179. OC_Log::write('core','starting upgrade from '.$installedVersion.' to '.$currentVersion,OC_Log::DEBUG);
  180. $result=OC_DB::updateDbFromStructure(OC::$SERVERROOT.'/db_structure.xml');
  181. if(!$result){
  182. echo 'Error while upgrading the database';
  183. die();
  184. }
  185. if(file_exists(OC::$SERVERROOT."/config/config.php") and !is_writable(OC::$SERVERROOT."/config/config.php")) {
  186. $tmpl = new OC_Template( '', 'error', 'guest' );
  187. $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")));
  188. $tmpl->printPage();
  189. exit;
  190. }
  191. OC_Config::setValue('version',implode('.',OC_Util::getVersion()));
  192. OC_App::checkAppsRequirements();
  193. }
  194. OC_App::updateApps();
  195. }
  196. }
  197. public static function initTemplateEngine() {
  198. // Add the stuff we need always
  199. OC_Util::addScript( "jquery-1.7.2.min" );
  200. OC_Util::addScript( "jquery-ui-1.8.16.custom.min" );
  201. OC_Util::addScript( "jquery-showpassword" );
  202. OC_Util::addScript( "jquery.infieldlabel.min" );
  203. OC_Util::addScript( "jquery-tipsy" );
  204. OC_Util::addScript( "oc-dialogs" );
  205. OC_Util::addScript( "js" );
  206. OC_Util::addScript( "eventsource" );
  207. OC_Util::addScript( "config" );
  208. //OC_Util::addScript( "multiselect" );
  209. OC_Util::addScript('search','result');
  210. OC_Util::addStyle( "styles" );
  211. OC_Util::addStyle( "multiselect" );
  212. OC_Util::addStyle( "jquery-ui-1.8.16.custom" );
  213. OC_Util::addStyle( "jquery-tipsy" );
  214. }
  215. public static function initSession() {
  216. ini_set('session.cookie_httponly','1;');
  217. session_start();
  218. }
  219. public static function loadapp(){
  220. if(file_exists(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/index.php')){
  221. require_once(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/index.php');
  222. }else{
  223. trigger_error('The requested App was not found.', E_USER_ERROR);//load default app instead?
  224. }
  225. }
  226. public static function loadfile(){
  227. if(file_exists(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/' . OC::$REQUESTEDFILE)){
  228. if(substr(OC::$REQUESTEDFILE, -3) == 'css'){
  229. $file = 'apps/' . OC::$REQUESTEDAPP . '/' . OC::$REQUESTEDFILE;
  230. $minimizer = new OC_Minimizer_CSS();
  231. $minimizer->output(array(array(OC::$APPSROOT, OC::$APPSWEBROOT, $file)), $file);
  232. exit;
  233. }elseif(substr(OC::$REQUESTEDFILE, -3) == 'php'){
  234. require_once(OC::$APPSROOT . '/apps/' . OC::$REQUESTEDAPP . '/' . OC::$REQUESTEDFILE);
  235. }
  236. }else{
  237. header('HTTP/1.0 404 Not Found');
  238. exit;
  239. }
  240. }
  241. public static function init(){
  242. // register autoloader
  243. spl_autoload_register(array('OC','autoload'));
  244. setlocale(LC_ALL, 'en_US.UTF-8');
  245. // set some stuff
  246. //ob_start();
  247. error_reporting(E_ALL | E_STRICT);
  248. if (defined('DEBUG') && DEBUG){
  249. ini_set('display_errors', 1);
  250. }
  251. self::$CLI=(php_sapi_name() == 'cli');
  252. date_default_timezone_set('UTC');
  253. ini_set('arg_separator.output','&amp;');
  254. // try to switch magic quotes off.
  255. if(function_exists('set_magic_quotes_runtime')) {
  256. @set_magic_quotes_runtime(false);
  257. }
  258. //try to configure php to enable big file uploads.
  259. //this doesn´t work always depending on the webserver and php configuration.
  260. //Let´s try to overwrite some defaults anyways
  261. //try to set the maximum execution time to 60min
  262. @set_time_limit(3600);
  263. @ini_set('max_execution_time',3600);
  264. @ini_set('max_input_time',3600);
  265. //try to set the maximum filesize to 10G
  266. @ini_set('upload_max_filesize','10G');
  267. @ini_set('post_max_size','10G');
  268. @ini_set('file_uploads','50');
  269. //try to set the session lifetime to 60min
  270. @ini_set('gc_maxlifetime','3600');
  271. //set http auth headers for apache+php-cgi work around
  272. if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'], $matches))
  273. {
  274. list($name, $password) = explode(':', base64_decode($matches[1]));
  275. $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
  276. $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
  277. }
  278. //set http auth headers for apache+php-cgi work around if variable gets renamed by apache
  279. if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches))
  280. {
  281. list($name, $password) = explode(':', base64_decode($matches[1]));
  282. $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
  283. $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
  284. }
  285. self::initPaths();
  286. // register the stream wrappers
  287. require_once('streamwrappers.php');
  288. stream_wrapper_register("fakedir", "OC_FakeDirStream");
  289. stream_wrapper_register('static', 'OC_StaticStreamWrapper');
  290. stream_wrapper_register('close', 'OC_CloseStreamWrapper');
  291. self::checkInstalled();
  292. self::checkSSL();
  293. // CSRF protection
  294. if(isset($_SERVER['HTTP_REFERER'])) $referer=$_SERVER['HTTP_REFERER']; else $referer='';
  295. $refererhost=parse_url($referer);
  296. if(isset($refererhost['host'])) $refererhost=$refererhost['host']; else $refererhost='';
  297. $server=OC_Helper::serverHost();
  298. $serverhost=explode(':',$server);
  299. $serverhost=$serverhost['0'];
  300. if(!self::$CLI){
  301. if(($_SERVER['REQUEST_METHOD']=='POST') and ($refererhost<>$serverhost)) {
  302. $url = OC_Helper::serverProtocol().'://'.$server.OC::$WEBROOT.'/index.php';
  303. header("Location: $url");
  304. exit();
  305. }
  306. }
  307. self::initSession();
  308. self::initTemplateEngine();
  309. self::checkUpgrade();
  310. $errors=OC_Util::checkServer();
  311. if(count($errors)>0) {
  312. OC_Template::printGuestPage('', 'error', array('errors' => $errors));
  313. exit;
  314. }
  315. // User and Groups
  316. if( !OC_Config::getValue( "installed", false )){
  317. $_SESSION['user_id'] = '';
  318. }
  319. OC_User::useBackend( OC_Config::getValue( "userbackend", "database" ));
  320. OC_Group::useBackend(new OC_Group_Database());
  321. // Load Apps
  322. // This includes plugins for users and filesystems as well
  323. global $RUNTIME_NOAPPS;
  324. global $RUNTIME_APPTYPES;
  325. if(!$RUNTIME_NOAPPS ){
  326. if($RUNTIME_APPTYPES){
  327. OC_App::loadApps($RUNTIME_APPTYPES);
  328. }else{
  329. OC_App::loadApps();
  330. }
  331. }
  332. // Check for blacklisted files
  333. OC_Hook::connect('OC_Filesystem','write','OC_Filesystem','isBlacklisted');
  334. //make sure temporary files are cleaned up
  335. register_shutdown_function(array('OC_Helper','cleanTmp'));
  336. //parse the given parameters
  337. self::$REQUESTEDAPP = (isset($_GET['app']) && trim($_GET['app']) != '' && !is_null($_GET['app'])?str_replace(array('\0', '/', '\\', '..'), '', strip_tags($_GET['app'])):OC_Config::getValue('defaultapp', 'files'));
  338. if(substr_count(self::$REQUESTEDAPP, '?') != 0){
  339. $app = substr(self::$REQUESTEDAPP, 0, strpos(self::$REQUESTEDAPP, '?'));
  340. $param = substr(self::$REQUESTEDAPP, strpos(self::$REQUESTEDAPP, '?') + 1);
  341. parse_str($param, $get);
  342. $_GET = array_merge($_GET, $get);
  343. self::$REQUESTEDAPP = $app;
  344. $_GET['app'] = $app;
  345. }
  346. self::$REQUESTEDFILE = (isset($_GET['getfile'])?$_GET['getfile']:null);
  347. if(substr_count(self::$REQUESTEDFILE, '?') != 0){
  348. $file = substr(self::$REQUESTEDFILE, 0, strpos(self::$REQUESTEDFILE, '?'));
  349. $param = substr(self::$REQUESTEDFILE, strpos(self::$REQUESTEDFILE, '?') + 1);
  350. parse_str($param, $get);
  351. $_GET = array_merge($_GET, $get);
  352. self::$REQUESTEDFILE = $file;
  353. $_GET['getfile'] = $file;
  354. }
  355. if(!is_null(self::$REQUESTEDFILE)){
  356. $subdir = OC::$APPSROOT . '/apps/' . self::$REQUESTEDAPP . '/' . self::$REQUESTEDFILE;
  357. $parent = OC::$APPSROOT . '/apps/' . self::$REQUESTEDAPP;
  358. if(!OC_Helper::issubdirectory($subdir, $parent)){
  359. self::$REQUESTEDFILE = null;
  360. header('HTTP/1.0 404 Not Found');
  361. exit;
  362. }
  363. }
  364. }
  365. }
  366. // define runtime variables - unless this already has been done
  367. if( !isset( $RUNTIME_NOSETUPFS )){
  368. $RUNTIME_NOSETUPFS = false;
  369. }
  370. if( !isset( $RUNTIME_NOAPPS )){
  371. $RUNTIME_NOAPPS = false;
  372. }
  373. if(!function_exists('get_temp_dir')) {
  374. function get_temp_dir() {
  375. if( $temp=ini_get('upload_tmp_dir') ) return $temp;
  376. if( $temp=getenv('TMP') ) return $temp;
  377. if( $temp=getenv('TEMP') ) return $temp;
  378. if( $temp=getenv('TMPDIR') ) return $temp;
  379. $temp=tempnam(__FILE__,'');
  380. if (file_exists($temp)) {
  381. unlink($temp);
  382. return dirname($temp);
  383. }
  384. if( $temp=sys_get_temp_dir()) return $temp;
  385. return null;
  386. }
  387. }
  388. OC::init();