base.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. * SPL autoload
  58. */
  59. public static function autoload($className){
  60. if(array_key_exists($className,OC::$CLASSPATH)){
  61. require_once OC::$CLASSPATH[$className];
  62. }
  63. elseif(strpos($className,'OC_')===0){
  64. require_once strtolower(str_replace('_','/',substr($className,3)) . '.php');
  65. }
  66. elseif(strpos($className,'Sabre_')===0) {
  67. require_once str_replace('_','/',$className) . '.php';
  68. }
  69. }
  70. /**
  71. * autodetects the formfactor of the used device
  72. * default -> the normal desktop browser interface
  73. * mobile -> interface for smartphones
  74. * tablet -> interface for tablets
  75. * 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.
  76. */
  77. public static function detectFormfactor(){
  78. // please add more useragent strings for other devices
  79. if(isset($_SERVER['HTTP_USER_AGENT'])){
  80. if(stripos($_SERVER['HTTP_USER_AGENT'],'ipad')>0) {
  81. $mode='tablet';
  82. }elseif(stripos($_SERVER['HTTP_USER_AGENT'],'iphone')>0){
  83. $mode='mobile';
  84. }elseif((stripos($_SERVER['HTTP_USER_AGENT'],'N9')>0) and (stripos($_SERVER['HTTP_USER_AGENT'],'nokia')>0)){
  85. $mode='mobile';
  86. }else{
  87. $mode='default';
  88. }
  89. }else{
  90. $mode='default';
  91. }
  92. return($mode);
  93. }
  94. public static function init(){
  95. // register autoloader
  96. spl_autoload_register(array('OC','autoload'));
  97. // set some stuff
  98. //ob_start();
  99. error_reporting(E_ALL | E_STRICT);
  100. if (defined('DEBUG') && DEBUG){
  101. ini_set('display_errors', 1);
  102. }
  103. date_default_timezone_set('Europe/Berlin');
  104. ini_set('arg_separator.output','&amp;');
  105. //set http auth headers for apache+php-cgi work around
  106. if (isset($_SERVER['HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['HTTP_AUTHORIZATION'], $matches))
  107. {
  108. list($name, $password) = explode(':', base64_decode($matches[1]));
  109. $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
  110. $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
  111. }
  112. //set http auth headers for apache+php-cgi work around if variable gets renamed by apache
  113. if (isset($_SERVER['REDIRECT_HTTP_AUTHORIZATION']) && preg_match('/Basic\s+(.*)$/i', $_SERVER['REDIRECT_HTTP_AUTHORIZATION'], $matches))
  114. {
  115. list($name, $password) = explode(':', base64_decode($matches[1]));
  116. $_SERVER['PHP_AUTH_USER'] = strip_tags($name);
  117. $_SERVER['PHP_AUTH_PW'] = strip_tags($password);
  118. }
  119. // calculate the documentroot
  120. OC::$DOCUMENTROOT=realpath($_SERVER['DOCUMENT_ROOT']);
  121. OC::$SERVERROOT=str_replace("\\",'/',substr(__FILE__,0,-13));
  122. OC::$SUBURI=substr(realpath($_SERVER["SCRIPT_FILENAME"]),strlen(OC::$SERVERROOT));
  123. $scriptName=$_SERVER["SCRIPT_NAME"];
  124. if(substr($scriptName,-1)=='/'){
  125. $scriptName.='index.php';
  126. }
  127. OC::$WEBROOT=substr($scriptName,0,strlen($scriptName)-strlen(OC::$SUBURI));
  128. if(OC::$WEBROOT!='' and OC::$WEBROOT[0]!=='/'){
  129. OC::$WEBROOT='/'.OC::$WEBROOT;
  130. }
  131. // set the right include path
  132. set_include_path(OC::$SERVERROOT.'/lib'.PATH_SEPARATOR.OC::$SERVERROOT.'/config'.PATH_SEPARATOR.OC::$SERVERROOT.'/3rdparty'.PATH_SEPARATOR.get_include_path().PATH_SEPARATOR.OC::$SERVERROOT);
  133. // Redirect to installer if not installed
  134. if (!OC_Config::getValue('installed', false) && OC::$SUBURI != '/index.php') {
  135. $url = 'http://'.$_SERVER['SERVER_NAME'].OC::$WEBROOT.'/index.php';
  136. header("Location: $url");
  137. exit();
  138. }
  139. // redirect to https site if configured
  140. if( OC_Config::getValue( "forcessl", false )){
  141. ini_set("session.cookie_secure", "on");
  142. if(!isset($_SERVER['HTTPS']) or $_SERVER['HTTPS'] != 'on') {
  143. $url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
  144. header("Location: $url");
  145. exit();
  146. }
  147. }
  148. if(OC_Config::getValue('installed', false)){
  149. $installedVersion=OC_Config::getValue('version','0.0.0');
  150. $currentVersion=implode('.',OC_Util::getVersion());
  151. if (version_compare($currentVersion, $installedVersion, '>')) {
  152. $result=OC_DB::updateDbFromStructure(OC::$SERVERROOT.'/db_structure.xml');
  153. if(!$result){
  154. echo 'Error while upgrading the database';
  155. die();
  156. }
  157. OC_Config::setValue('version',implode('.',OC_Util::getVersion()));
  158. }
  159. OC_App::updateApps();
  160. }
  161. ini_set('session.cookie_httponly','1;');
  162. session_start();
  163. // if the formfactor is not yet autodetected do the autodetection now. For possible forfactors check the detectFormfactor documentation
  164. if(!isset($_SESSION['formfactor'])){
  165. $_SESSION['formfactor']=OC::detectFormfactor();
  166. }
  167. // allow manual override via GET parameter
  168. if(isset($_GET['formfactor'])){
  169. $_SESSION['formfactor']=$_GET['formfactor'];
  170. }
  171. // Add the stuff we need always
  172. OC_Util::addScript( "jquery-1.6.4.min" );
  173. OC_Util::addScript( "jquery-ui-1.8.16.custom.min" );
  174. OC_Util::addScript( "jquery-showpassword" );
  175. OC_Util::addScript( "jquery.infieldlabel.min" );
  176. OC_Util::addScript( "jquery-tipsy" );
  177. OC_Util::addScript( "js" );
  178. OC_Util::addScript( "eventsource" );
  179. //OC_Util::addScript( "multiselect" );
  180. OC_Util::addScript('search','result');
  181. OC_Util::addStyle( "styles" );
  182. OC_Util::addStyle( "multiselect" );
  183. OC_Util::addStyle( "jquery-ui-1.8.16.custom" );
  184. OC_Util::addStyle( "jquery-tipsy" );
  185. $errors=OC_Util::checkServer();
  186. if(count($errors)>0) {
  187. OC_Template::printGuestPage('', 'error', array('errors' => $errors));
  188. exit;
  189. }
  190. // TODO: we should get rid of this one, too
  191. // WARNING: to make everything even more confusing,
  192. // DATADIRECTORY is a var that changes and DATADIRECTORY_ROOT
  193. // stays the same, but is set by "datadirectory".
  194. // Any questions?
  195. OC::$CONFIG_DATADIRECTORY = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
  196. // User and Groups
  197. if( !OC_Config::getValue( "installed", false )){
  198. $_SESSION['user_id'] = '';
  199. }
  200. OC_User::useBackend( OC_Config::getValue( "userbackend", "database" ));
  201. OC_Group::setBackend( OC_Config::getValue( "groupbackend", "database" ));
  202. // Set up file system unless forbidden
  203. global $RUNTIME_NOSETUPFS;
  204. if(!$RUNTIME_NOSETUPFS ){
  205. OC_Util::setupFS();
  206. }
  207. // Load Apps
  208. // This includes plugins for users and filesystems as well
  209. global $RUNTIME_NOAPPS;
  210. if(!$RUNTIME_NOAPPS ){
  211. OC_App::loadApps();
  212. }
  213. // Last part: connect some hooks
  214. OC_HOOK::connect('OC_User', 'post_createUser', 'OC_Connector_Sabre_Principal', 'addPrincipal');
  215. OC_HOOK::connect('OC_User', 'post_deleteUser', 'OC_Connector_Sabre_Principal', 'deletePrincipal');
  216. }
  217. }
  218. // define runtime variables - unless this already has been done
  219. if( !isset( $RUNTIME_NOSETUPFS )){
  220. $RUNTIME_NOSETUPFS = false;
  221. }
  222. if( !isset( $RUNTIME_NOAPPS )){
  223. $RUNTIME_NOAPPS = false;
  224. }
  225. if(!function_exists('get_temp_dir')) {
  226. function get_temp_dir() {
  227. if( $temp=ini_get('upload_tmp_dir') ) return $temp;
  228. if( $temp=getenv('TMP') ) return $temp;
  229. if( $temp=getenv('TEMP') ) return $temp;
  230. if( $temp=getenv('TMPDIR') ) return $temp;
  231. $temp=tempnam(__FILE__,'');
  232. if (file_exists($temp)) {
  233. unlink($temp);
  234. return dirname($temp);
  235. }
  236. return null;
  237. }
  238. }
  239. OC::init();
  240. require_once('fakedirstream.php');
  241. // FROM search.php
  242. new OC_Search_Provider_File();