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.

util.php 37KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288
  1. <?php
  2. /**
  3. * Class for utility functions
  4. *
  5. */
  6. class OC_Util {
  7. public static $scripts=array();
  8. public static $styles=array();
  9. public static $headers=array();
  10. private static $rootMounted=false;
  11. private static $fsSetup=false;
  12. /**
  13. * @brief Can be set up
  14. * @param string $user
  15. * @return boolean
  16. * @description configure the initial filesystem based on the configuration
  17. */
  18. public static function setupFS( $user = '' ) {
  19. //setting up the filesystem twice can only lead to trouble
  20. if(self::$fsSetup) {
  21. return false;
  22. }
  23. // If we are not forced to load a specific user we load the one that is logged in
  24. if( $user == "" && OC_User::isLoggedIn()) {
  25. $user = OC_User::getUser();
  26. }
  27. // load all filesystem apps before, so no setup-hook gets lost
  28. OC_App::loadApps(array('filesystem'));
  29. // the filesystem will finish when $user is not empty,
  30. // mark fs setup here to avoid doing the setup from loading
  31. // OC_Filesystem
  32. if ($user != '') {
  33. self::$fsSetup=true;
  34. }
  35. $configDataDirectory = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
  36. //first set up the local "root" storage
  37. \OC\Files\Filesystem::initMounts();
  38. if(!self::$rootMounted) {
  39. \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir'=>$configDataDirectory), '/');
  40. self::$rootMounted = true;
  41. }
  42. if ($user != '' && !OCP\User::userExists($user)) {
  43. return false;
  44. }
  45. //if we aren't logged in, there is no use to set up the filesystem
  46. if( $user != "" ) {
  47. \OC\Files\Filesystem::addStorageWrapper(function($mountPoint, $storage){
  48. // set up quota for home storages, even for other users
  49. // which can happen when using sharing
  50. if ($storage instanceof \OC\Files\Storage\Home) {
  51. $user = $storage->getUser()->getUID();
  52. $quota = OC_Util::getUserQuota($user);
  53. if ($quota !== \OC\Files\SPACE_UNLIMITED) {
  54. return new \OC\Files\Storage\Wrapper\Quota(array('storage' => $storage, 'quota' => $quota, 'root' => 'files'));
  55. }
  56. }
  57. return $storage;
  58. });
  59. $userDir = '/'.$user.'/files';
  60. $userRoot = OC_User::getHome($user);
  61. $userDirectory = $userRoot . '/files';
  62. if( !is_dir( $userDirectory )) {
  63. mkdir( $userDirectory, 0755, true );
  64. OC_Util::copySkeleton($userDirectory);
  65. }
  66. //jail the user into his "home" directory
  67. \OC\Files\Filesystem::init($user, $userDir);
  68. $fileOperationProxy = new OC_FileProxy_FileOperations();
  69. OC_FileProxy::register($fileOperationProxy);
  70. OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $userDir));
  71. }
  72. return true;
  73. }
  74. /**
  75. * Get the quota of a user
  76. * @param string $user
  77. * @return int Quota bytes
  78. */
  79. public static function getUserQuota($user){
  80. $config = \OC::$server->getConfig();
  81. $userQuota = $config->getUserValue($user, 'files', 'quota', 'default');
  82. if($userQuota === 'default') {
  83. $userQuota = $config->getAppValue('files', 'default_quota', 'none');
  84. }
  85. if($userQuota === 'none') {
  86. return \OC\Files\SPACE_UNLIMITED;
  87. }else{
  88. return OC_Helper::computerFileSize($userQuota);
  89. }
  90. }
  91. /**
  92. * @brief copies the user skeleton files into the fresh user home files
  93. * @param string $userDirectory
  94. */
  95. public static function copySkeleton($userDirectory) {
  96. OC_Util::copyr(\OC::$SERVERROOT.'/core/skeleton' , $userDirectory);
  97. }
  98. /**
  99. * @brief copies a directory recursively
  100. * @param string $source
  101. * @param string $target
  102. * @return void
  103. */
  104. public static function copyr($source,$target) {
  105. $dir = opendir($source);
  106. @mkdir($target);
  107. while(false !== ( $file = readdir($dir)) ) {
  108. if ( !\OC\Files\Filesystem::isIgnoredDir($file) ) {
  109. if ( is_dir($source . '/' . $file) ) {
  110. OC_Util::copyr($source . '/' . $file , $target . '/' . $file);
  111. } else {
  112. copy($source . '/' . $file,$target . '/' . $file);
  113. }
  114. }
  115. }
  116. closedir($dir);
  117. }
  118. /**
  119. * @return void
  120. */
  121. public static function tearDownFS() {
  122. \OC\Files\Filesystem::tearDown();
  123. self::$fsSetup=false;
  124. self::$rootMounted=false;
  125. }
  126. /**
  127. * @brief get the current installed version of ownCloud
  128. * @return array
  129. */
  130. public static function getVersion() {
  131. OC_Util::loadVersion();
  132. return \OC::$server->getSession()->get('OC_Version');
  133. }
  134. /**
  135. * @brief get the current installed version string of ownCloud
  136. * @return string
  137. */
  138. public static function getVersionString() {
  139. OC_Util::loadVersion();
  140. return \OC::$server->getSession()->get('OC_VersionString');
  141. }
  142. /**
  143. * @description get the current installed edition of ownCloud. There is the community
  144. * edition that just returns an empty string and the enterprise edition
  145. * that returns "Enterprise".
  146. * @return string
  147. */
  148. public static function getEditionString() {
  149. OC_Util::loadVersion();
  150. return \OC::$server->getSession()->get('OC_Edition');
  151. }
  152. /**
  153. * @description get the update channel of the current installed of ownCloud.
  154. * @return string
  155. */
  156. public static function getChannel() {
  157. OC_Util::loadVersion();
  158. return \OC::$server->getSession()->get('OC_Channel');
  159. }
  160. /**
  161. * @description get the build number of the current installed of ownCloud.
  162. * @return string
  163. */
  164. public static function getBuild() {
  165. OC_Util::loadVersion();
  166. return \OC::$server->getSession()->get('OC_Build');
  167. }
  168. /**
  169. * @description load the version.php into the session as cache
  170. */
  171. private static function loadVersion() {
  172. $timestamp = filemtime(OC::$SERVERROOT.'/version.php');
  173. if(!\OC::$server->getSession()->exists('OC_Version') or OC::$server->getSession()->get('OC_Version_Timestamp') != $timestamp) {
  174. require 'version.php';
  175. $session = \OC::$server->getSession();
  176. /** @var $timestamp int */
  177. $session->set('OC_Version_Timestamp', $timestamp);
  178. /** @var $OC_Version string */
  179. $session->set('OC_Version', $OC_Version);
  180. /** @var $OC_VersionString string */
  181. $session->set('OC_VersionString', $OC_VersionString);
  182. /** @var $OC_Edition string */
  183. $session->set('OC_Edition', $OC_Edition);
  184. /** @var $OC_Channel string */
  185. $session->set('OC_Channel', $OC_Channel);
  186. /** @var $OC_Build string */
  187. $session->set('OC_Build', $OC_Build);
  188. }
  189. }
  190. /**
  191. * @brief add a javascript file
  192. *
  193. * @param string $application
  194. * @param string|null $file filename
  195. * @return void
  196. */
  197. public static function addScript( $application, $file = null ) {
  198. if ( is_null( $file )) {
  199. $file = $application;
  200. $application = "";
  201. }
  202. if ( !empty( $application )) {
  203. self::$scripts[] = "$application/js/$file";
  204. } else {
  205. self::$scripts[] = "js/$file";
  206. }
  207. }
  208. /**
  209. * @brief add a css file
  210. *
  211. * @param string $application
  212. * @param string|null $file filename
  213. * @return void
  214. */
  215. public static function addStyle( $application, $file = null ) {
  216. if ( is_null( $file )) {
  217. $file = $application;
  218. $application = "";
  219. }
  220. if ( !empty( $application )) {
  221. self::$styles[] = "$application/css/$file";
  222. } else {
  223. self::$styles[] = "css/$file";
  224. }
  225. }
  226. /**
  227. * @brief Add a custom element to the header
  228. * @param string $tag tag name of the element
  229. * @param array $attributes array of attributes for the element
  230. * @param string $text the text content for the element
  231. * @return void
  232. */
  233. public static function addHeader( $tag, $attributes, $text='') {
  234. self::$headers[] = array(
  235. 'tag'=>$tag,
  236. 'attributes'=>$attributes,
  237. 'text'=>$text
  238. );
  239. }
  240. /**
  241. * @brief formats a timestamp in the "right" way
  242. *
  243. * @param int $timestamp
  244. * @param bool $dateOnly option to omit time from the result
  245. * @return string timestamp
  246. * @description adjust to clients timezone if we know it
  247. */
  248. public static function formatDate( $timestamp, $dateOnly=false) {
  249. if(\OC::$session->exists('timezone')) {
  250. $systemTimeZone = intval(date('O'));
  251. $systemTimeZone = (round($systemTimeZone/100, 0)*60) + ($systemTimeZone%100);
  252. $clientTimeZone = \OC::$session->get('timezone')*60;
  253. $offset = $clientTimeZone - $systemTimeZone;
  254. $timestamp = $timestamp + $offset*60;
  255. }
  256. $l = OC_L10N::get('lib');
  257. return $l->l($dateOnly ? 'date' : 'datetime', $timestamp);
  258. }
  259. /**
  260. * @brief check if the current server configuration is suitable for ownCloud
  261. * @return array arrays with error messages and hints
  262. */
  263. public static function checkServer() {
  264. $errors = array();
  265. $CONFIG_DATADIRECTORY = OC_Config::getValue('datadirectory', OC::$SERVERROOT . '/data');
  266. if (!\OC::needUpgrade() && OC_Config::getValue('installed', false)) {
  267. // this check needs to be done every time
  268. $errors = self::checkDataDirectoryValidity($CONFIG_DATADIRECTORY);
  269. }
  270. // Assume that if checkServer() succeeded before in this session, then all is fine.
  271. if(\OC::$session->exists('checkServer_suceeded') && \OC::$session->get('checkServer_suceeded')) {
  272. return $errors;
  273. }
  274. $webServerRestart = false;
  275. //check for database drivers
  276. if(!(is_callable('sqlite_open') or class_exists('SQLite3'))
  277. and !is_callable('mysql_connect')
  278. and !is_callable('pg_connect')
  279. and !is_callable('oci_connect')) {
  280. $errors[] = array(
  281. 'error'=>'No database drivers (sqlite, mysql, or postgresql) installed.',
  282. 'hint'=>'' //TODO: sane hint
  283. );
  284. $webServerRestart = true;
  285. }
  286. //common hint for all file permissions error messages
  287. $permissionsHint = 'Permissions can usually be fixed by '
  288. .'<a href="' . OC_Helper::linkToDocs('admin-dir_permissions')
  289. .'" target="_blank">giving the webserver write access to the root directory</a>.';
  290. // Check if config folder is writable.
  291. if(!is_writable(OC::$configDir) or !is_readable(OC::$configDir)) {
  292. $errors[] = array(
  293. 'error' => "Can't write into config directory",
  294. 'hint' => 'This can usually be fixed by '
  295. .'<a href="' . OC_Helper::linkToDocs('admin-dir_permissions')
  296. .'" target="_blank">giving the webserver write access to the config directory</a>.'
  297. );
  298. }
  299. // Check if there is a writable install folder.
  300. if(OC_Config::getValue('appstoreenabled', true)) {
  301. if( OC_App::getInstallPath() === null
  302. || !is_writable(OC_App::getInstallPath())
  303. || !is_readable(OC_App::getInstallPath()) ) {
  304. $errors[] = array(
  305. 'error' => "Can't write into apps directory",
  306. 'hint' => 'This can usually be fixed by '
  307. .'<a href="' . OC_Helper::linkToDocs('admin-dir_permissions')
  308. .'" target="_blank">giving the webserver write access to the apps directory</a> '
  309. .'or disabling the appstore in the config file.'
  310. );
  311. }
  312. }
  313. // Create root dir.
  314. if(!is_dir($CONFIG_DATADIRECTORY)) {
  315. $success=@mkdir($CONFIG_DATADIRECTORY);
  316. if ($success) {
  317. $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
  318. } else {
  319. $errors[] = array(
  320. 'error' => "Can't create data directory (".$CONFIG_DATADIRECTORY.")",
  321. 'hint' => 'This can usually be fixed by '
  322. .'<a href="' . OC_Helper::linkToDocs('admin-dir_permissions')
  323. .'" target="_blank">giving the webserver write access to the root directory</a>.'
  324. );
  325. }
  326. } else if(!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) {
  327. $errors[] = array(
  328. 'error'=>'Data directory ('.$CONFIG_DATADIRECTORY.') not writable by ownCloud',
  329. 'hint'=>$permissionsHint
  330. );
  331. } else {
  332. $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
  333. }
  334. if(!OC_Util::isSetLocaleWorking()) {
  335. $errors[] = array(
  336. 'error' => 'Setting locale to en_US.UTF-8/fr_FR.UTF-8/es_ES.UTF-8/de_DE.UTF-8/ru_RU.UTF-8/pt_BR.UTF-8/it_IT.UTF-8/ja_JP.UTF-8/zh_CN.UTF-8 failed',
  337. 'hint' => 'Please install one of theses locales on your system and restart your webserver.'
  338. );
  339. }
  340. $moduleHint = "Please ask your server administrator to install the module.";
  341. // check if all required php modules are present
  342. if(!class_exists('ZipArchive')) {
  343. $errors[] = array(
  344. 'error'=>'PHP module zip not installed.',
  345. 'hint'=>$moduleHint
  346. );
  347. $webServerRestart = true;
  348. }
  349. if(!class_exists('DOMDocument')) {
  350. $errors[] = array(
  351. 'error' => 'PHP module dom not installed.',
  352. 'hint' => $moduleHint
  353. );
  354. $webServerRestart =true;
  355. }
  356. if(!function_exists('xml_parser_create')) {
  357. $errors[] = array(
  358. 'error' => 'PHP module libxml not installed.',
  359. 'hint' => $moduleHint
  360. );
  361. $webServerRestart = true;
  362. }
  363. if(!function_exists('mb_detect_encoding')) {
  364. $errors[] = array(
  365. 'error'=>'PHP module mb multibyte not installed.',
  366. 'hint'=>$moduleHint
  367. );
  368. $webServerRestart = true;
  369. }
  370. if(!function_exists('ctype_digit')) {
  371. $errors[] = array(
  372. 'error'=>'PHP module ctype is not installed.',
  373. 'hint'=>$moduleHint
  374. );
  375. $webServerRestart = true;
  376. }
  377. if(!function_exists('json_encode')) {
  378. $errors[] = array(
  379. 'error'=>'PHP module JSON is not installed.',
  380. 'hint'=>$moduleHint
  381. );
  382. $webServerRestart = true;
  383. }
  384. if(!extension_loaded('gd') || !function_exists('gd_info')) {
  385. $errors[] = array(
  386. 'error'=>'PHP module GD is not installed.',
  387. 'hint'=>$moduleHint
  388. );
  389. $webServerRestart = true;
  390. }
  391. if(!function_exists('gzencode')) {
  392. $errors[] = array(
  393. 'error'=>'PHP module zlib is not installed.',
  394. 'hint'=>$moduleHint
  395. );
  396. $webServerRestart = true;
  397. }
  398. if(!function_exists('iconv')) {
  399. $errors[] = array(
  400. 'error'=>'PHP module iconv is not installed.',
  401. 'hint'=>$moduleHint
  402. );
  403. $webServerRestart = true;
  404. }
  405. if(!function_exists('simplexml_load_string')) {
  406. $errors[] = array(
  407. 'error'=>'PHP module SimpleXML is not installed.',
  408. 'hint'=>$moduleHint
  409. );
  410. $webServerRestart = true;
  411. }
  412. if(version_compare(phpversion(), '5.3.3', '<')) {
  413. $errors[] = array(
  414. 'error'=>'PHP 5.3.3 or higher is required.',
  415. 'hint'=>'Please ask your server administrator to update PHP to the latest version.'
  416. .' Your PHP version is no longer supported by ownCloud and the PHP community.'
  417. );
  418. $webServerRestart = true;
  419. }
  420. if(!defined('PDO::ATTR_DRIVER_NAME')) {
  421. $errors[] = array(
  422. 'error'=>'PHP PDO module is not installed.',
  423. 'hint'=>$moduleHint
  424. );
  425. $webServerRestart = true;
  426. }
  427. if (((strtolower(@ini_get('safe_mode')) == 'on')
  428. || (strtolower(@ini_get('safe_mode')) == 'yes')
  429. || (strtolower(@ini_get('safe_mode')) == 'true')
  430. || (ini_get("safe_mode") == 1 ))) {
  431. $errors[] = array(
  432. 'error'=>'PHP Safe Mode is enabled. ownCloud requires that it is disabled to work properly.',
  433. 'hint'=>'PHP Safe Mode is a deprecated and mostly useless setting that should be disabled. '
  434. .'Please ask your server administrator to disable it in php.ini or in your webserver config.'
  435. );
  436. $webServerRestart = true;
  437. }
  438. if (get_magic_quotes_gpc() == 1 ) {
  439. $errors[] = array(
  440. 'error'=>'Magic Quotes is enabled. ownCloud requires that it is disabled to work properly.',
  441. 'hint'=>'Magic Quotes is a deprecated and mostly useless setting that should be disabled. '
  442. .'Please ask your server administrator to disable it in php.ini or in your webserver config.'
  443. );
  444. $webServerRestart = true;
  445. }
  446. if($webServerRestart) {
  447. $errors[] = array(
  448. 'error'=>'PHP modules have been installed, but they are still listed as missing?',
  449. 'hint'=>'Please ask your server administrator to restart the web server.'
  450. );
  451. }
  452. $errors = array_merge($errors, self::checkDatabaseVersion());
  453. // Cache the result of this function
  454. \OC::$session->set('checkServer_suceeded', count($errors) == 0);
  455. return $errors;
  456. }
  457. /**
  458. * Check the database version
  459. * @return array errors array
  460. */
  461. public static function checkDatabaseVersion() {
  462. $errors = array();
  463. $dbType = \OC_Config::getValue('dbtype', 'sqlite');
  464. if ($dbType === 'pgsql') {
  465. // check PostgreSQL version
  466. try {
  467. $result = \OC_DB::executeAudited('SHOW SERVER_VERSION');
  468. $data = $result->fetchRow();
  469. if (isset($data['server_version'])) {
  470. $version = $data['server_version'];
  471. if (version_compare($version, '9.0.0', '<')) {
  472. $errors[] = array(
  473. 'error' => 'PostgreSQL >= 9 required',
  474. 'hint' => 'Please upgrade your database version'
  475. );
  476. }
  477. }
  478. } catch (\Doctrine\DBAL\DBALException $e) {
  479. \OCP\Util::logException('core', $e);
  480. $errors[] = array(
  481. 'error' => 'Error occurred while checking PostgreSQL version',
  482. 'hint' => 'Please make sure you have PostgreSQL >= 9 or check the logs for more information about the error'
  483. );
  484. }
  485. }
  486. return $errors;
  487. }
  488. /**
  489. * @brief check if there are still some encrypted files stored
  490. * @return boolean
  491. */
  492. public static function encryptedFiles() {
  493. //check if encryption was enabled in the past
  494. $encryptedFiles = false;
  495. if (OC_App::isEnabled('files_encryption') === false) {
  496. $view = new OC\Files\View('/' . OCP\User::getUser());
  497. $keyfilePath = '/files_encryption/keyfiles';
  498. if ($view->is_dir($keyfilePath)) {
  499. $dircontent = $view->getDirectoryContent($keyfilePath);
  500. if (!empty($dircontent)) {
  501. $encryptedFiles = true;
  502. }
  503. }
  504. }
  505. return $encryptedFiles;
  506. }
  507. /**
  508. * @brief Check for correct file permissions of data directory
  509. * @param string $dataDirectory
  510. * @return array arrays with error messages and hints
  511. */
  512. public static function checkDataDirectoryPermissions($dataDirectory) {
  513. $errors = array();
  514. if (self::runningOnWindows()) {
  515. //TODO: permissions checks for windows hosts
  516. } else {
  517. $permissionsModHint = 'Please change the permissions to 0770 so that the directory'
  518. .' cannot be listed by other users.';
  519. $perms = substr(decoct(@fileperms($dataDirectory)), -3);
  520. if (substr($perms, -1) != '0') {
  521. chmod($dataDirectory, 0770);
  522. clearstatcache();
  523. $perms = substr(decoct(@fileperms($dataDirectory)), -3);
  524. if (substr($perms, 2, 1) != '0') {
  525. $errors[] = array(
  526. 'error' => 'Data directory ('.$dataDirectory.') is readable for other users',
  527. 'hint' => $permissionsModHint
  528. );
  529. }
  530. }
  531. }
  532. return $errors;
  533. }
  534. /**
  535. * Check that the data directory exists and is valid by
  536. * checking the existence of the ".ocdata" file.
  537. *
  538. * @param string $dataDirectory data directory path
  539. * @return bool true if the data directory is valid, false otherwise
  540. */
  541. public static function checkDataDirectoryValidity($dataDirectory) {
  542. $errors = array();
  543. if (!file_exists($dataDirectory.'/.ocdata')) {
  544. $errors[] = array(
  545. 'error' => 'Data directory (' . $dataDirectory . ') is invalid',
  546. 'hint' => 'Please check that the data directory contains a file' .
  547. ' ".ocdata" in its root.'
  548. );
  549. }
  550. return $errors;
  551. }
  552. /**
  553. * @param array $errors
  554. */
  555. public static function displayLoginPage($errors = array()) {
  556. $parameters = array();
  557. foreach( $errors as $value ) {
  558. $parameters[$value] = true;
  559. }
  560. if (!empty($_POST['user'])) {
  561. $parameters["username"] = $_POST['user'];
  562. $parameters['user_autofocus'] = false;
  563. } else {
  564. $parameters["username"] = '';
  565. $parameters['user_autofocus'] = true;
  566. }
  567. if (isset($_REQUEST['redirect_url'])) {
  568. $redirectUrl = $_REQUEST['redirect_url'];
  569. $parameters['redirect_url'] = urlencode($redirectUrl);
  570. }
  571. $parameters['alt_login'] = OC_App::getAlternativeLogIns();
  572. $parameters['rememberLoginAllowed'] = self::rememberLoginAllowed();
  573. OC_Template::printGuestPage("", "login", $parameters);
  574. }
  575. /**
  576. * @brief Check if the app is enabled, redirects to home if not
  577. * @param string $app
  578. * @return void
  579. */
  580. public static function checkAppEnabled($app) {
  581. if( !OC_App::isEnabled($app)) {
  582. header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php' ));
  583. exit();
  584. }
  585. }
  586. /**
  587. * Check if the user is logged in, redirects to home if not. With
  588. * redirect URL parameter to the request URI.
  589. * @return void
  590. */
  591. public static function checkLoggedIn() {
  592. // Check if we are a user
  593. if( !OC_User::isLoggedIn()) {
  594. header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php',
  595. array('redirect_url' => OC_Request::requestUri())
  596. ));
  597. exit();
  598. }
  599. }
  600. /**
  601. * @brief Check if the user is a admin, redirects to home if not
  602. * @return void
  603. */
  604. public static function checkAdminUser() {
  605. OC_Util::checkLoggedIn();
  606. if( !OC_User::isAdminUser(OC_User::getUser())) {
  607. header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php' ));
  608. exit();
  609. }
  610. }
  611. /**
  612. * Check if it is allowed to remember login.
  613. *
  614. * @note Every app can set 'rememberlogin' to 'false' to disable the remember login feature
  615. *
  616. * @return bool
  617. */
  618. public static function rememberLoginAllowed() {
  619. $apps = OC_App::getEnabledApps();
  620. foreach ($apps as $app) {
  621. $appInfo = OC_App::getAppInfo($app);
  622. if (isset($appInfo['rememberlogin']) && $appInfo['rememberlogin'] === 'false') {
  623. return false;
  624. }
  625. }
  626. return true;
  627. }
  628. /**
  629. * @brief Check if the user is a subadmin, redirects to home if not
  630. * @return null|boolean $groups where the current user is subadmin
  631. */
  632. public static function checkSubAdminUser() {
  633. OC_Util::checkLoggedIn();
  634. if(!OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
  635. header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php' ));
  636. exit();
  637. }
  638. return true;
  639. }
  640. /**
  641. * @brief Redirect to the user default page
  642. * @return void
  643. */
  644. public static function redirectToDefaultPage() {
  645. $urlGenerator = \OC::$server->getURLGenerator();
  646. if(isset($_REQUEST['redirect_url'])) {
  647. $location = urldecode($_REQUEST['redirect_url']);
  648. }
  649. else if (isset(OC::$REQUESTEDAPP) && !empty(OC::$REQUESTEDAPP)) {
  650. $location = $urlGenerator->getAbsoluteURL('/index.php/apps/'.OC::$REQUESTEDAPP.'/index.php');
  651. } else {
  652. $defaultPage = OC_Appconfig::getValue('core', 'defaultpage');
  653. if ($defaultPage) {
  654. $location = $urlGenerator->getAbsoluteURL($defaultPage);
  655. } else {
  656. $location = $urlGenerator->getAbsoluteURL('/index.php/files/index.php');
  657. }
  658. }
  659. OC_Log::write('core', 'redirectToDefaultPage: '.$location, OC_Log::DEBUG);
  660. header( 'Location: '.$location );
  661. exit();
  662. }
  663. /**
  664. * @brief get an id unique for this instance
  665. * @return string
  666. */
  667. public static function getInstanceId() {
  668. $id = OC_Config::getValue('instanceid', null);
  669. if(is_null($id)) {
  670. // We need to guarantee at least one letter in instanceid so it can be used as the session_name
  671. $id = 'oc' . self::generateRandomBytes(10);
  672. OC_Config::$object->setValue('instanceid', $id);
  673. }
  674. return $id;
  675. }
  676. /**
  677. * @brief Static lifespan (in seconds) when a request token expires.
  678. * @see OC_Util::callRegister()
  679. * @see OC_Util::isCallRegistered()
  680. * @description
  681. * Also required for the client side to compute the point in time when to
  682. * request a fresh token. The client will do so when nearly 97% of the
  683. * time span coded here has expired.
  684. */
  685. public static $callLifespan = 3600; // 3600 secs = 1 hour
  686. /**
  687. * @brief Register an get/post call. Important to prevent CSRF attacks.
  688. * @todo Write howto: CSRF protection guide
  689. * @return $token Generated token.
  690. * @description
  691. * Creates a 'request token' (random) and stores it inside the session.
  692. * Ever subsequent (ajax) request must use such a valid token to succeed,
  693. * otherwise the request will be denied as a protection against CSRF.
  694. * The tokens expire after a fixed lifespan.
  695. * @see OC_Util::$callLifespan
  696. * @see OC_Util::isCallRegistered()
  697. */
  698. public static function callRegister() {
  699. // Check if a token exists
  700. if(!\OC::$session->exists('requesttoken')) {
  701. // No valid token found, generate a new one.
  702. $requestToken = self::generateRandomBytes(20);
  703. \OC::$session->set('requesttoken', $requestToken);
  704. } else {
  705. // Valid token already exists, send it
  706. $requestToken = \OC::$session->get('requesttoken');
  707. }
  708. return($requestToken);
  709. }
  710. /**
  711. * @brief Check an ajax get/post call if the request token is valid.
  712. * @return boolean False if request token is not set or is invalid.
  713. * @see OC_Util::$callLifespan
  714. * @see OC_Util::callRegister()
  715. */
  716. public static function isCallRegistered() {
  717. return \OC::$server->getRequest()->passesCSRFCheck();
  718. }
  719. /**
  720. * @brief Check an ajax get/post call if the request token is valid. exit if not.
  721. * @todo Write howto
  722. * @return void
  723. */
  724. public static function callCheck() {
  725. if(!OC_Util::isCallRegistered()) {
  726. exit();
  727. }
  728. }
  729. /**
  730. * @brief Public function to sanitize HTML
  731. *
  732. * This function is used to sanitize HTML and should be applied on any
  733. * string or array of strings before displaying it on a web page.
  734. *
  735. * @param string|array of strings
  736. * @return array with sanitized strings or a single sanitized string, depends on the input parameter.
  737. */
  738. public static function sanitizeHTML( &$value ) {
  739. if (is_array($value)) {
  740. array_walk_recursive($value, 'OC_Util::sanitizeHTML');
  741. } else {
  742. //Specify encoding for PHP<5.4
  743. $value = htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
  744. }
  745. return $value;
  746. }
  747. /**
  748. * @brief Public function to encode url parameters
  749. *
  750. * This function is used to encode path to file before output.
  751. * Encoding is done according to RFC 3986 with one exception:
  752. * Character '/' is preserved as is.
  753. *
  754. * @param string $component part of URI to encode
  755. * @return string
  756. */
  757. public static function encodePath($component) {
  758. $encoded = rawurlencode($component);
  759. $encoded = str_replace('%2F', '/', $encoded);
  760. return $encoded;
  761. }
  762. /**
  763. * @brief Check if the .htaccess file is working
  764. * @throws OC\HintException If the testfile can't get written.
  765. * @return bool
  766. * @description Check if the .htaccess file is working by creating a test
  767. * file in the data directory and trying to access via http
  768. */
  769. public static function isHtaccessWorking() {
  770. if (!\OC_Config::getValue("check_for_working_htaccess", true)) {
  771. return true;
  772. }
  773. // testdata
  774. $fileName = '/htaccesstest.txt';
  775. $testContent = 'testcontent';
  776. // creating a test file
  777. $testFile = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ).'/'.$fileName;
  778. if(file_exists($testFile)) {// already running this test, possible recursive call
  779. return false;
  780. }
  781. $fp = @fopen($testFile, 'w');
  782. if (!$fp) {
  783. throw new OC\HintException('Can\'t create test file to check for working .htaccess file.',
  784. 'Make sure it is possible for the webserver to write to '.$testFile);
  785. }
  786. fwrite($fp, $testContent);
  787. fclose($fp);
  788. // accessing the file via http
  789. $url = OC_Helper::makeURLAbsolute(OC::$WEBROOT.'/data'.$fileName);
  790. $fp = @fopen($url, 'r');
  791. $content=@fread($fp, 2048);
  792. @fclose($fp);
  793. // cleanup
  794. @unlink($testFile);
  795. // does it work ?
  796. if($content==$testContent) {
  797. return false;
  798. } else {
  799. return true;
  800. }
  801. }
  802. /**
  803. * @brief test if webDAV is working properly
  804. * @return bool
  805. * @description
  806. * The basic assumption is that if the server returns 401/Not Authenticated for an unauthenticated PROPFIND
  807. * the web server it self is setup properly.
  808. *
  809. * Why not an authenticated PROPFIND and other verbs?
  810. * - We don't have the password available
  811. * - We have no idea about other auth methods implemented (e.g. OAuth with Bearer header)
  812. *
  813. */
  814. public static function isWebDAVWorking() {
  815. if (!function_exists('curl_init')) {
  816. return true;
  817. }
  818. if (!\OC_Config::getValue("check_for_working_webdav", true)) {
  819. return true;
  820. }
  821. $settings = array(
  822. 'baseUri' => OC_Helper::linkToRemote('webdav'),
  823. );
  824. $client = new \OC_DAVClient($settings);
  825. $client->setRequestTimeout(10);
  826. // for this self test we don't care if the ssl certificate is self signed and the peer cannot be verified.
  827. $client->setVerifyPeer(false);
  828. // also don't care if the host can't be verified
  829. $client->setVerifyHost(0);
  830. $return = true;
  831. try {
  832. // test PROPFIND
  833. $client->propfind('', array('{DAV:}resourcetype'));
  834. } catch (\Sabre_DAV_Exception_NotAuthenticated $e) {
  835. $return = true;
  836. } catch (\Exception $e) {
  837. OC_Log::write('core', 'isWebDAVWorking: NO - Reason: '.$e->getMessage(). ' ('.get_class($e).')', OC_Log::WARN);
  838. $return = false;
  839. }
  840. return $return;
  841. }
  842. /**
  843. * Check if the setlocal call does not work. This can happen if the right
  844. * local packages are not available on the server.
  845. * @return bool
  846. */
  847. public static function isSetLocaleWorking() {
  848. // setlocale test is pointless on Windows
  849. if (OC_Util::runningOnWindows() ) {
  850. return true;
  851. }
  852. \Patchwork\Utf8\Bootup::initLocale();
  853. if ('' === basename('§')) {
  854. return false;
  855. }
  856. return true;
  857. }
  858. /**
  859. * @brief Check if the PHP module fileinfo is loaded.
  860. * @return bool
  861. */
  862. public static function fileInfoLoaded() {
  863. return function_exists('finfo_open');
  864. }
  865. /**
  866. * @brief Check if a PHP version older then 5.3.8 is installed.
  867. * @return bool
  868. */
  869. public static function isPHPoutdated() {
  870. return version_compare(phpversion(), '5.3.8', '<');
  871. }
  872. /**
  873. * @brief Check if the ownCloud server can connect to the internet
  874. * @return bool
  875. */
  876. public static function isInternetConnectionWorking() {
  877. // in case there is no internet connection on purpose return false
  878. if (self::isInternetConnectionEnabled() === false) {
  879. return false;
  880. }
  881. // in case the connection is via proxy return true to avoid connecting to owncloud.org
  882. if(OC_Config::getValue('proxy', '') != '') {
  883. return true;
  884. }
  885. // try to connect to owncloud.org to see if http connections to the internet are possible.
  886. $connected = @fsockopen("www.owncloud.org", 80);
  887. if ($connected) {
  888. fclose($connected);
  889. return true;
  890. } else {
  891. // second try in case one server is down
  892. $connected = @fsockopen("apps.owncloud.com", 80);
  893. if ($connected) {
  894. fclose($connected);
  895. return true;
  896. } else {
  897. return false;
  898. }
  899. }
  900. }
  901. /**
  902. * @brief Check if the connection to the internet is disabled on purpose
  903. * @return string
  904. */
  905. public static function isInternetConnectionEnabled(){
  906. return \OC_Config::getValue("has_internet_connection", true);
  907. }
  908. /**
  909. * @brief clear all levels of output buffering
  910. * @return void
  911. */
  912. public static function obEnd(){
  913. while (ob_get_level()) {
  914. ob_end_clean();
  915. }
  916. }
  917. /**
  918. * @brief Generates a cryptographic secure pseudo-random string
  919. * @param Int $length of the random string
  920. * @return String
  921. * Please also update secureRNGAvailable if you change something here
  922. */
  923. public static function generateRandomBytes($length = 30) {
  924. // Try to use openssl_random_pseudo_bytes
  925. if (function_exists('openssl_random_pseudo_bytes')) {
  926. $pseudoByte = bin2hex(openssl_random_pseudo_bytes($length, $strong));
  927. if($strong == true) {
  928. return substr($pseudoByte, 0, $length); // Truncate it to match the length
  929. }
  930. }
  931. // Try to use /dev/urandom
  932. if (!self::runningOnWindows()) {
  933. $fp = @file_get_contents('/dev/urandom', false, null, 0, $length);
  934. if ($fp !== false) {
  935. $string = substr(bin2hex($fp), 0, $length);
  936. return $string;
  937. }
  938. }
  939. // Fallback to mt_rand()
  940. $characters = '0123456789';
  941. $characters .= 'abcdefghijklmnopqrstuvwxyz';
  942. $charactersLength = strlen($characters)-1;
  943. $pseudoByte = "";
  944. // Select some random characters
  945. for ($i = 0; $i < $length; $i++) {
  946. $pseudoByte .= $characters[mt_rand(0, $charactersLength)];
  947. }
  948. return $pseudoByte;
  949. }
  950. /**
  951. * @brief Checks if a secure random number generator is available
  952. * @return bool
  953. */
  954. public static function secureRNGAvailable() {
  955. // Check openssl_random_pseudo_bytes
  956. if(function_exists('openssl_random_pseudo_bytes')) {
  957. openssl_random_pseudo_bytes(1, $strong);
  958. if($strong == true) {
  959. return true;
  960. }
  961. }
  962. // Check /dev/urandom
  963. if (!self::runningOnWindows()) {
  964. $fp = @file_get_contents('/dev/urandom', false, null, 0, 1);
  965. if ($fp !== false) {
  966. return true;
  967. }
  968. }
  969. return false;
  970. }
  971. /**
  972. * @Brief Get file content via curl.
  973. * @param string $url Url to get content
  974. * @return string of the response or false on error
  975. * This function get the content of a page via curl, if curl is enabled.
  976. * If not, file_get_contents is used.
  977. */
  978. public static function getUrlContent($url) {
  979. if (function_exists('curl_init')) {
  980. $curl = curl_init();
  981. $max_redirects = 10;
  982. curl_setopt($curl, CURLOPT_HEADER, 0);
  983. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  984. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  985. curl_setopt($curl, CURLOPT_URL, $url);
  986. curl_setopt($curl, CURLOPT_USERAGENT, "ownCloud Server Crawler");
  987. if(OC_Config::getValue('proxy', '') != '') {
  988. curl_setopt($curl, CURLOPT_PROXY, OC_Config::getValue('proxy'));
  989. }
  990. if(OC_Config::getValue('proxyuserpwd', '') != '') {
  991. curl_setopt($curl, CURLOPT_PROXYUSERPWD, OC_Config::getValue('proxyuserpwd'));
  992. }
  993. if (ini_get('open_basedir') === '' && ini_get('safe_mode' === 'Off')) {
  994. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
  995. curl_setopt($curl, CURLOPT_MAXREDIRS, $max_redirects);
  996. $data = curl_exec($curl);
  997. } else {
  998. curl_setopt($curl, CURLOPT_FOLLOWLOCATION, false);
  999. $mr = $max_redirects;
  1000. if ($mr > 0) {
  1001. $newurl = curl_getinfo($curl, CURLINFO_EFFECTIVE_URL);
  1002. $rcurl = curl_copy_handle($curl);
  1003. curl_setopt($rcurl, CURLOPT_HEADER, true);
  1004. curl_setopt($rcurl, CURLOPT_NOBODY, true);
  1005. curl_setopt($rcurl, CURLOPT_FORBID_REUSE, false);
  1006. curl_setopt($rcurl, CURLOPT_RETURNTRANSFER, true);
  1007. do {
  1008. curl_setopt($rcurl, CURLOPT_URL, $newurl);
  1009. $header = curl_exec($rcurl);
  1010. if (curl_errno($rcurl)) {
  1011. $code = 0;
  1012. } else {
  1013. $code = curl_getinfo($rcurl, CURLINFO_HTTP_CODE);
  1014. if ($code == 301 || $code == 302) {
  1015. preg_match('/Location:(.*?)\n/', $header, $matches);
  1016. $newurl = trim(array_pop($matches));
  1017. } else {
  1018. $code = 0;
  1019. }
  1020. }
  1021. } while ($code && --$mr);
  1022. curl_close($rcurl);
  1023. if ($mr > 0) {
  1024. curl_setopt($curl, CURLOPT_URL, $newurl);
  1025. }
  1026. }
  1027. if($mr == 0 && $max_redirects > 0) {
  1028. $data = false;
  1029. } else {
  1030. $data = curl_exec($curl);
  1031. }
  1032. }
  1033. curl_close($curl);
  1034. } else {
  1035. $contextArray = null;
  1036. if(OC_Config::getValue('proxy', '') != '') {
  1037. $contextArray = array(
  1038. 'http' => array(
  1039. 'timeout' => 10,
  1040. 'proxy' => OC_Config::getValue('proxy')
  1041. )
  1042. );
  1043. } else {
  1044. $contextArray = array(
  1045. 'http' => array(
  1046. 'timeout' => 10
  1047. )
  1048. );
  1049. }
  1050. $ctx = stream_context_create(
  1051. $contextArray
  1052. );
  1053. $data = @file_get_contents($url, 0, $ctx);
  1054. }
  1055. return $data;
  1056. }
  1057. /**
  1058. * Checks whether the server is running on Windows
  1059. * @return bool true if running on Windows, false otherwise
  1060. */
  1061. public static function runningOnWindows() {
  1062. return (substr(PHP_OS, 0, 3) === "WIN");
  1063. }
  1064. /**
  1065. * Checks whether the server is running on Mac OS X
  1066. * @return bool true if running on Mac OS X, false otherwise
  1067. */
  1068. public static function runningOnMac() {
  1069. return (strtoupper(substr(PHP_OS, 0, 6)) === 'DARWIN');
  1070. }
  1071. /**
  1072. * Handles the case that there may not be a theme, then check if a "default"
  1073. * theme exists and take that one
  1074. * @return string the theme
  1075. */
  1076. public static function getTheme() {
  1077. $theme = OC_Config::getValue("theme", '');
  1078. if($theme === '') {
  1079. if(is_dir(OC::$SERVERROOT . '/themes/default')) {
  1080. $theme = 'default';
  1081. }
  1082. }
  1083. return $theme;
  1084. }
  1085. /**
  1086. * @brief Clear the opcode cache if one exists
  1087. * This is necessary for writing to the config file
  1088. * in case the opcode cache does not re-validate files
  1089. * @return void
  1090. */
  1091. public static function clearOpcodeCache() {
  1092. // APC
  1093. if (function_exists('apc_clear_cache')) {
  1094. apc_clear_cache();
  1095. }
  1096. // Zend Opcache
  1097. if (function_exists('accelerator_reset')) {
  1098. accelerator_reset();
  1099. }
  1100. // XCache
  1101. if (function_exists('xcache_clear_cache')) {
  1102. if (ini_get('xcache.admin.enable_auth')) {
  1103. OC_Log::write('core', 'XCache opcode cache will not be cleared because "xcache.admin.enable_auth" is enabled.', \OC_Log::WARN);
  1104. } else {
  1105. xcache_clear_cache(XC_TYPE_PHP, 0);
  1106. }
  1107. }
  1108. // Opcache (PHP >= 5.5)
  1109. if (function_exists('opcache_reset')) {
  1110. opcache_reset();
  1111. }
  1112. }
  1113. /**
  1114. * Normalize a unicode string
  1115. * @param string $value a not normalized string
  1116. * @return bool|string
  1117. */
  1118. public static function normalizeUnicode($value) {
  1119. if(class_exists('Patchwork\PHP\Shim\Normalizer')) {
  1120. $normalizedValue = \Patchwork\PHP\Shim\Normalizer::normalize($value);
  1121. if($normalizedValue === false) {
  1122. \OC_Log::write( 'core', 'normalizing failed for "' . $value . '"', \OC_Log::WARN);
  1123. } else {
  1124. $value = $normalizedValue;
  1125. }
  1126. }
  1127. return $value;
  1128. }
  1129. /**
  1130. * @param boolean|string $file
  1131. * @return string
  1132. */
  1133. public static function basename($file) {
  1134. $file = rtrim($file, '/');
  1135. $t = explode('/', $file);
  1136. return array_pop($t);
  1137. }
  1138. /**
  1139. * A human readable string is generated based on version, channel and build number
  1140. * @return string
  1141. */
  1142. public static function getHumanVersion() {
  1143. $version = OC_Util::getVersionString().' ('.OC_Util::getChannel().')';
  1144. $build = OC_Util::getBuild();
  1145. if(!empty($build) and OC_Util::getChannel() === 'daily') {
  1146. $version .= ' Build:' . $build;
  1147. }
  1148. return $version;
  1149. }
  1150. /**
  1151. * Returns whether the given file name is valid
  1152. * @param $file string file name to check
  1153. * @return bool true if the file name is valid, false otherwise
  1154. */
  1155. public static function isValidFileName($file) {
  1156. $trimmed = trim($file);
  1157. if ($trimmed === '') {
  1158. return false;
  1159. }
  1160. if ($trimmed === '.' || $trimmed === '..') {
  1161. return false;
  1162. }
  1163. foreach (str_split($trimmed) as $char) {
  1164. if (strpos(\OCP\FILENAME_INVALID_CHARS, $char) !== false) {
  1165. return false;
  1166. }
  1167. }
  1168. return true;
  1169. }
  1170. }