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.

702 lines
21 KiB

15 years ago
13 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
13 years ago
15 years ago
13 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
14 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
15 years ago
13 years ago
13 years ago
15 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
14 years ago
14 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
15 years ago
  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. public static $core_styles=array();
  13. public static $core_scripts=array();
  14. // Can be set up
  15. public static function setupFS( $user = '' ) {// configure the initial filesystem based on the configuration
  16. if(self::$fsSetup) {//setting up the filesystem twice can only lead to trouble
  17. return false;
  18. }
  19. // If we are not forced to load a specific user we load the one that is logged in
  20. if( $user == "" && OC_User::isLoggedIn()) {
  21. $user = OC_User::getUser();
  22. }
  23. // load all filesystem apps before, so no setup-hook gets lost
  24. if(!isset($RUNTIME_NOAPPS) || !$RUNTIME_NOAPPS) {
  25. OC_App::loadApps(array('filesystem'));
  26. }
  27. // the filesystem will finish when $user is not empty,
  28. // mark fs setup here to avoid doing the setup from loading
  29. // OC_Filesystem
  30. if ($user != '') {
  31. self::$fsSetup=true;
  32. }
  33. $CONFIG_DATADIRECTORY = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
  34. //first set up the local "root" storage
  35. if(!self::$rootMounted) {
  36. \OC\Files\Filesystem::mount('\OC\Files\Storage\Local', array('datadir'=>$CONFIG_DATADIRECTORY), '/');
  37. self::$rootMounted=true;
  38. }
  39. if( $user != "" ) { //if we aren't logged in, there is no use to set up the filesystem
  40. $user_dir = '/'.$user.'/files';
  41. $user_root = OC_User::getHome($user);
  42. $userdirectory = $user_root . '/files';
  43. if( !is_dir( $userdirectory )) {
  44. mkdir( $userdirectory, 0755, true );
  45. }
  46. //jail the user into his "home" directory
  47. \OC\Files\Filesystem::init($user_dir);
  48. $quotaProxy=new OC_FileProxy_Quota();
  49. $fileOperationProxy = new OC_FileProxy_FileOperations();
  50. OC_FileProxy::register($quotaProxy);
  51. OC_FileProxy::register($fileOperationProxy);
  52. OC_Hook::emit('OC_Filesystem', 'setup', array('user' => $user, 'user_dir' => $user_dir));
  53. }
  54. return true;
  55. }
  56. public static function tearDownFS() {
  57. \OC\Files\Filesystem::tearDown();
  58. self::$fsSetup=false;
  59. }
  60. /**
  61. * get the current installed version of ownCloud
  62. * @return array
  63. */
  64. public static function getVersion() {
  65. // hint: We only can count up. So the internal version number of ownCloud 4.5 will be 4.90.0. This is not visible to the user
  66. return array(4, 91, 9);
  67. }
  68. /**
  69. * get the current installed version string of ownCloud
  70. * @return string
  71. */
  72. public static function getVersionString() {
  73. return '5.0 pre alpha';
  74. }
  75. /**
  76. * get the current installed edition of ownCloud. There is the community edition that just returns an empty string and the enterprise edition that returns "Enterprise".
  77. * @return string
  78. */
  79. public static function getEditionString() {
  80. return '';
  81. }
  82. /**
  83. * add a javascript file
  84. *
  85. * @param appid $application
  86. * @param filename $file
  87. */
  88. public static function addScript( $application, $file = null ) {
  89. if( is_null( $file )) {
  90. $file = $application;
  91. $application = "";
  92. }
  93. if( !empty( $application )) {
  94. self::$scripts[] = "$application/js/$file";
  95. }else{
  96. self::$scripts[] = "js/$file";
  97. }
  98. }
  99. /**
  100. * add a css file
  101. *
  102. * @param appid $application
  103. * @param filename $file
  104. */
  105. public static function addStyle( $application, $file = null ) {
  106. if( is_null( $file )) {
  107. $file = $application;
  108. $application = "";
  109. }
  110. if( !empty( $application )) {
  111. self::$styles[] = "$application/css/$file";
  112. }else{
  113. self::$styles[] = "css/$file";
  114. }
  115. }
  116. /**
  117. * @brief Add a custom element to the header
  118. * @param string tag tag name of the element
  119. * @param array $attributes array of attributes for the element
  120. * @param string $text the text content for the element
  121. */
  122. public static function addHeader( $tag, $attributes, $text='') {
  123. self::$headers[] = array('tag'=>$tag, 'attributes'=>$attributes, 'text'=>$text);
  124. }
  125. /**
  126. * formats a timestamp in the "right" way
  127. *
  128. * @param int timestamp $timestamp
  129. * @param bool dateOnly option to omit time from the result
  130. */
  131. public static function formatDate( $timestamp, $dateOnly=false) {
  132. if(isset($_SESSION['timezone'])) {//adjust to clients timezone if we know it
  133. $systemTimeZone = intval(date('O'));
  134. $systemTimeZone=(round($systemTimeZone/100, 0)*60)+($systemTimeZone%100);
  135. $clientTimeZone=$_SESSION['timezone']*60;
  136. $offset=$clientTimeZone-$systemTimeZone;
  137. $timestamp=$timestamp+$offset*60;
  138. }
  139. $l=OC_L10N::get('lib');
  140. return $l->l($dateOnly ? 'date' : 'datetime', $timestamp);
  141. }
  142. /**
  143. * check if the current server configuration is suitable for ownCloud
  144. * @return array arrays with error messages and hints
  145. */
  146. public static function checkServer() {
  147. $errors=array();
  148. $web_server_restart= false;
  149. //check for database drivers
  150. if(!(is_callable('sqlite_open') or class_exists('SQLite3')) and !is_callable('mysql_connect') and !is_callable('pg_connect')) {
  151. $errors[]=array('error'=>'No database drivers (sqlite, mysql, or postgresql) installed.<br/>', 'hint'=>'');//TODO: sane hint
  152. $web_server_restart= true;
  153. }
  154. //common hint for all file permissons error messages
  155. $permissionsHint="Permissions can usually be fixed by giving the webserver write access to the ownCloud directory";
  156. // Check if config folder is writable.
  157. if(!is_writable(OC::$SERVERROOT."/config/") or !is_readable(OC::$SERVERROOT."/config/")) {
  158. $errors[]=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");
  159. }
  160. // Check if there is a writable install folder.
  161. if(OC_Config::getValue('appstoreenabled', true)) {
  162. if( OC_App::getInstallPath() === null || !is_writable(OC_App::getInstallPath()) || !is_readable(OC_App::getInstallPath()) ) {
  163. $errors[]=array('error'=>"Can't write into apps directory", 'hint'=>"You can usually fix this by giving the webserver user write access to the apps directory
  164. in owncloud or disabling the appstore in the config file.");
  165. }
  166. }
  167. $CONFIG_DATADIRECTORY = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" );
  168. // Create root dir.
  169. if(!is_dir($CONFIG_DATADIRECTORY)) {
  170. $success=@mkdir($CONFIG_DATADIRECTORY);
  171. if ($success) {
  172. $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
  173. } else {
  174. $errors[]=array('error'=>"Can't create data directory (".$CONFIG_DATADIRECTORY.")", 'hint'=>"You can usually fix this by giving the webserver write access to the ownCloud directory '".OC::$SERVERROOT."' (in a terminal, use the command 'chown -R www-data:www-data /path/to/your/owncloud/install/data' ");
  175. }
  176. } else if(!is_writable($CONFIG_DATADIRECTORY) or !is_readable($CONFIG_DATADIRECTORY)) {
  177. $errors[]=array('error'=>'Data directory ('.$CONFIG_DATADIRECTORY.') not writable by ownCloud<br/>', 'hint'=>$permissionsHint);
  178. } else {
  179. $errors = array_merge($errors, self::checkDataDirectoryPermissions($CONFIG_DATADIRECTORY));
  180. }
  181. // check if all required php modules are present
  182. if(!class_exists('ZipArchive')) {
  183. $errors[]=array('error'=>'PHP module zip not installed.<br/>', 'hint'=>'Please ask your server administrator to install the module.');
  184. $web_server_restart= false;
  185. }
  186. if(!function_exists('mb_detect_encoding')) {
  187. $errors[]=array('error'=>'PHP module mb multibyte not installed.<br/>', 'hint'=>'Please ask your server administrator to install the module.');
  188. $web_server_restart= false;
  189. }
  190. if(!function_exists('ctype_digit')) {
  191. $errors[]=array('error'=>'PHP module ctype is not installed.<br/>', 'hint'=>'Please ask your server administrator to install the module.');
  192. $web_server_restart= false;
  193. }
  194. if(!function_exists('json_encode')) {
  195. $errors[]=array('error'=>'PHP module JSON is not installed.<br/>', 'hint'=>'Please ask your server administrator to install the module.');
  196. $web_server_restart= false;
  197. }
  198. if(!function_exists('imagepng')) {
  199. $errors[]=array('error'=>'PHP module GD is not installed.<br/>', 'hint'=>'Please ask your server administrator to install the module.');
  200. $web_server_restart= false;
  201. }
  202. if(!function_exists('gzencode')) {
  203. $errors[]=array('error'=>'PHP module zlib is not installed.<br/>', 'hint'=>'Please ask your server administrator to install the module.');
  204. $web_server_restart= false;
  205. }
  206. if(!function_exists('iconv')) {
  207. $errors[]=array('error'=>'PHP module iconv is not installed.<br/>', 'hint'=>'Please ask your server administrator to install the module.');
  208. $web_server_restart= false;
  209. }
  210. if(!function_exists('simplexml_load_string')) {
  211. $errors[]=array('error'=>'PHP module SimpleXML is not installed.<br/>', 'hint'=>'Please ask your server administrator to install the module.');
  212. $web_server_restart= false;
  213. }
  214. if(floatval(phpversion())<5.3) {
  215. $errors[]=array('error'=>'PHP 5.3 is required.<br/>', 'hint'=>'Please ask your server administrator to update PHP to version 5.3 or higher. PHP 5.2 is no longer supported by ownCloud and the PHP community.');
  216. $web_server_restart= false;
  217. }
  218. if(!defined('PDO::ATTR_DRIVER_NAME')) {
  219. $errors[]=array('error'=>'PHP PDO module is not installed.<br/>', 'hint'=>'Please ask your server administrator to install the module.');
  220. $web_server_restart= false;
  221. }
  222. $handler = ini_get("session.save_handler");
  223. if($handler == "files") {
  224. $tmpDir = session_save_path();
  225. if($tmpDir != ""){
  226. if(!@is_writable($tmpDir)){
  227. $errors[]=array('error' => 'The temporary folder used by PHP to save the session data is either incorrect or not writable! Please check : '.session_save_path().'<br/>',
  228. 'hint'=>'Please ask your server administrator to grant write access or define another temporary folder.');
  229. }
  230. }
  231. }
  232. if($web_server_restart) {
  233. $errors[]=array('error'=>'PHP modules have been installed, but they are still listed as missing?<br/>', 'hint'=>'Please ask your server administrator to restart the web server.');
  234. }
  235. return $errors;
  236. }
  237. /**
  238. * Check for correct file permissions of data directory
  239. * @return array arrays with error messages and hints
  240. */
  241. public static function checkDataDirectoryPermissions($dataDirectory) {
  242. $errors = array();
  243. if (stristr(PHP_OS, 'WIN')) {
  244. //TODO: permissions checks for windows hosts
  245. } else {
  246. $permissionsModHint = 'Please change the permissions to 0770 so that the directory cannot be listed by other users.';
  247. $prems = substr(decoct(@fileperms($dataDirectory)), -3);
  248. if (substr($prems, -1) != '0') {
  249. OC_Helper::chmodr($dataDirectory, 0770);
  250. clearstatcache();
  251. $prems = substr(decoct(@fileperms($dataDirectory)), -3);
  252. if (substr($prems, 2, 1) != '0') {
  253. $errors[] = array('error' => 'Data directory ('.$dataDirectory.') is readable for other users<br/>', 'hint' => $permissionsModHint);
  254. }
  255. }
  256. }
  257. return $errors;
  258. }
  259. public static function displayLoginPage($errors = array()) {
  260. $parameters = array();
  261. foreach( $errors as $key => $value ) {
  262. $parameters[$value] = true;
  263. }
  264. if (!empty($_POST['user'])) {
  265. $parameters["username"] = OC_Util::sanitizeHTML($_POST['user']).'"';
  266. $parameters['user_autofocus'] = false;
  267. } else {
  268. $parameters["username"] = '';
  269. $parameters['user_autofocus'] = true;
  270. }
  271. if (isset($_REQUEST['redirect_url'])) {
  272. $redirect_url = OC_Util::sanitizeHTML($_REQUEST['redirect_url']);
  273. $parameters['redirect_url'] = urlencode($redirect_url);
  274. }
  275. $parameters['alt_login'] = OC_App::getAlternativeLogIns();
  276. OC_Template::printGuestPage("", "login", $parameters);
  277. }
  278. /**
  279. * Check if the app is enabled, redirects to home if not
  280. */
  281. public static function checkAppEnabled($app) {
  282. if( !OC_App::isEnabled($app)) {
  283. header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php' ));
  284. exit();
  285. }
  286. }
  287. /**
  288. * Check if the user is logged in, redirects to home if not. With
  289. * redirect URL parameter to the request URI.
  290. */
  291. public static function checkLoggedIn() {
  292. // Check if we are a user
  293. if( !OC_User::isLoggedIn()) {
  294. header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php', array('redirect_url' => OC_Request::requestUri())));
  295. exit();
  296. }
  297. }
  298. /**
  299. * Check if the user is a admin, redirects to home if not
  300. */
  301. public static function checkAdminUser() {
  302. if( !OC_User::isAdminUser(OC_User::getUser())) {
  303. header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php' ));
  304. exit();
  305. }
  306. }
  307. /**
  308. * Check if the user is a subadmin, redirects to home if not
  309. * @return array $groups where the current user is subadmin
  310. */
  311. public static function checkSubAdminUser() {
  312. if(!OC_SubAdmin::isSubAdmin(OC_User::getUser())) {
  313. header( 'Location: '.OC_Helper::linkToAbsolute( '', 'index.php' ));
  314. exit();
  315. }
  316. return true;
  317. }
  318. /**
  319. * Redirect to the user default page
  320. */
  321. public static function redirectToDefaultPage() {
  322. if(isset($_REQUEST['redirect_url'])) {
  323. $location = OC_Helper::makeURLAbsolute(urldecode($_REQUEST['redirect_url']));
  324. }
  325. else if (isset(OC::$REQUESTEDAPP) && !empty(OC::$REQUESTEDAPP)) {
  326. $location = OC_Helper::linkToAbsolute( OC::$REQUESTEDAPP, 'index.php' );
  327. }
  328. else {
  329. $defaultpage = OC_Appconfig::getValue('core', 'defaultpage');
  330. if ($defaultpage) {
  331. $location = OC_Helper::makeURLAbsolute(OC::$WEBROOT.'/'.$defaultpage);
  332. }
  333. else {
  334. $location = OC_Helper::linkToAbsolute( 'files', 'index.php' );
  335. }
  336. }
  337. OC_Log::write('core', 'redirectToDefaultPage: '.$location, OC_Log::DEBUG);
  338. header( 'Location: '.$location );
  339. exit();
  340. }
  341. /**
  342. * get an id unqiue for this instance
  343. * @return string
  344. */
  345. public static function getInstanceId() {
  346. $id=OC_Config::getValue('instanceid', null);
  347. if(is_null($id)) {
  348. $id=uniqid();
  349. OC_Config::setValue('instanceid', $id);
  350. }
  351. return $id;
  352. }
  353. /**
  354. * @brief Static lifespan (in seconds) when a request token expires.
  355. * @see OC_Util::callRegister()
  356. * @see OC_Util::isCallRegistered()
  357. * @description
  358. * Also required for the client side to compute the piont in time when to
  359. * request a fresh token. The client will do so when nearly 97% of the
  360. * timespan coded here has expired.
  361. */
  362. public static $callLifespan = 3600; // 3600 secs = 1 hour
  363. /**
  364. * @brief Register an get/post call. Important to prevent CSRF attacks.
  365. * @todo Write howto: CSRF protection guide
  366. * @return $token Generated token.
  367. * @description
  368. * Creates a 'request token' (random) and stores it inside the session.
  369. * Ever subsequent (ajax) request must use such a valid token to succeed,
  370. * otherwise the request will be denied as a protection against CSRF.
  371. * The tokens expire after a fixed lifespan.
  372. * @see OC_Util::$callLifespan
  373. * @see OC_Util::isCallRegistered()
  374. */
  375. public static function callRegister() {
  376. // Check if a token exists
  377. if(!isset($_SESSION['requesttoken'])) {
  378. // No valid token found, generate a new one.
  379. $requestToken = self::generate_random_bytes(20);
  380. $_SESSION['requesttoken']=$requestToken;
  381. } else {
  382. // Valid token already exists, send it
  383. $requestToken = $_SESSION['requesttoken'];
  384. }
  385. return($requestToken);
  386. }
  387. /**
  388. * @brief Check an ajax get/post call if the request token is valid.
  389. * @return boolean False if request token is not set or is invalid.
  390. * @see OC_Util::$callLifespan
  391. * @see OC_Util::callRegister()
  392. */
  393. public static function isCallRegistered() {
  394. if(isset($_GET['requesttoken'])) {
  395. $token=$_GET['requesttoken'];
  396. }elseif(isset($_POST['requesttoken'])) {
  397. $token=$_POST['requesttoken'];
  398. }elseif(isset($_SERVER['HTTP_REQUESTTOKEN'])) {
  399. $token=$_SERVER['HTTP_REQUESTTOKEN'];
  400. }else{
  401. //no token found.
  402. return false;
  403. }
  404. // Check if the token is valid
  405. if($token !== $_SESSION['requesttoken']) {
  406. // Not valid
  407. return false;
  408. } else {
  409. // Valid token
  410. return true;
  411. }
  412. }
  413. /**
  414. * @brief Check an ajax get/post call if the request token is valid. exit if not.
  415. * Todo: Write howto
  416. */
  417. public static function callCheck() {
  418. if(!OC_Util::isCallRegistered()) {
  419. exit;
  420. }
  421. }
  422. /**
  423. * @brief Public function to sanitize HTML
  424. *
  425. * This function is used to sanitize HTML and should be applied on any
  426. * string or array of strings before displaying it on a web page.
  427. *
  428. * @param string or array of strings
  429. * @return array with sanitized strings or a single sanitized string, depends on the input parameter.
  430. */
  431. public static function sanitizeHTML( &$value ) {
  432. if (is_array($value) || is_object($value)) {
  433. array_walk_recursive($value, 'OC_Util::sanitizeHTML');
  434. } else {
  435. $value = htmlentities($value, ENT_QUOTES, 'UTF-8'); //Specify encoding for PHP<5.4
  436. }
  437. return $value;
  438. }
  439. /**
  440. * Check if the htaccess file is working by creating a test file in the data directory and trying to access via http
  441. */
  442. public static function ishtaccessworking() {
  443. // testdata
  444. $filename='/htaccesstest.txt';
  445. $testcontent='testcontent';
  446. // creating a test file
  447. $testfile = OC_Config::getValue( "datadirectory", OC::$SERVERROOT."/data" ).'/'.$filename;
  448. if(file_exists($testfile)) {// already running this test, possible recursive call
  449. return false;
  450. }
  451. $fp = @fopen($testfile, 'w');
  452. @fwrite($fp, $testcontent);
  453. @fclose($fp);
  454. // accessing the file via http
  455. $url = OC_Helper::makeURLAbsolute(OC::$WEBROOT.'/data'.$filename);
  456. $fp = @fopen($url, 'r');
  457. $content=@fread($fp, 2048);
  458. @fclose($fp);
  459. // cleanup
  460. @unlink($testfile);
  461. // does it work ?
  462. if($content==$testcontent) {
  463. return(false);
  464. }else{
  465. return(true);
  466. }
  467. }
  468. /**
  469. * Check if the setlocal call doesn't work. This can happen if the right local packages are not available on the server.
  470. */
  471. public static function issetlocaleworking() {
  472. // setlocale test is pointless on Windows
  473. if (OC_Util::runningOnWindows() ) {
  474. return true;
  475. }
  476. $result=setlocale(LC_ALL, 'en_US.UTF-8');
  477. if($result==false) {
  478. return(false);
  479. }else{
  480. return(true);
  481. }
  482. }
  483. /**
  484. * Check if the PHP module fileinfo is loaded.
  485. * @return bool
  486. */
  487. public static function fileInfoLoaded() {
  488. return function_exists('finfo_open');
  489. }
  490. /**
  491. * Check if the ownCloud server can connect to the internet
  492. */
  493. public static function isinternetconnectionworking() {
  494. // try to connect to owncloud.org to see if http connections to the internet are possible.
  495. $connected = @fsockopen("www.owncloud.org", 80);
  496. if ($connected) {
  497. fclose($connected);
  498. return true;
  499. }else{
  500. // second try in case one server is down
  501. $connected = @fsockopen("apps.owncloud.com", 80);
  502. if ($connected) {
  503. fclose($connected);
  504. return true;
  505. }else{
  506. return false;
  507. }
  508. }
  509. }
  510. /**
  511. * clear all levels of output buffering
  512. */
  513. public static function obEnd(){
  514. while (ob_get_level()) {
  515. ob_end_clean();
  516. }
  517. }
  518. /**
  519. * @brief Generates a cryptographical secure pseudorandom string
  520. * @param Int with the length of the random string
  521. * @return String
  522. * Please also update secureRNG_available if you change something here
  523. */
  524. public static function generate_random_bytes($length = 30) {
  525. // Try to use openssl_random_pseudo_bytes
  526. if(function_exists('openssl_random_pseudo_bytes')) {
  527. $pseudo_byte = bin2hex(openssl_random_pseudo_bytes($length, $strong));
  528. if($strong == true) {
  529. return substr($pseudo_byte, 0, $length); // Truncate it to match the length
  530. }
  531. }
  532. // Try to use /dev/urandom
  533. $fp = @file_get_contents('/dev/urandom', false, null, 0, $length);
  534. if ($fp !== false) {
  535. $string = substr(bin2hex($fp), 0, $length);
  536. return $string;
  537. }
  538. // Fallback to mt_rand()
  539. $characters = '0123456789';
  540. $characters .= 'abcdefghijklmnopqrstuvwxyz';
  541. $charactersLength = strlen($characters)-1;
  542. $pseudo_byte = "";
  543. // Select some random characters
  544. for ($i = 0; $i < $length; $i++) {
  545. $pseudo_byte .= $characters[mt_rand(0, $charactersLength)];
  546. }
  547. return $pseudo_byte;
  548. }
  549. /**
  550. * @brief Checks if a secure random number generator is available
  551. * @return bool
  552. */
  553. public static function secureRNG_available() {
  554. // Check openssl_random_pseudo_bytes
  555. if(function_exists('openssl_random_pseudo_bytes')) {
  556. openssl_random_pseudo_bytes(1, $strong);
  557. if($strong == true) {
  558. return true;
  559. }
  560. }
  561. // Check /dev/urandom
  562. $fp = @file_get_contents('/dev/urandom', false, null, 0, 1);
  563. if ($fp !== false) {
  564. return true;
  565. }
  566. return false;
  567. }
  568. /**
  569. * @Brief Get file content via curl.
  570. * @param string $url Url to get content
  571. * @return string of the response or false on error
  572. * This function get the content of a page via curl, if curl is enabled.
  573. * If not, file_get_element is used.
  574. */
  575. public static function getUrlContent($url){
  576. if (function_exists('curl_init')) {
  577. $curl = curl_init();
  578. curl_setopt($curl, CURLOPT_HEADER, 0);
  579. curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
  580. curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 10);
  581. curl_setopt($curl, CURLOPT_URL, $url);
  582. curl_setopt($curl, CURLOPT_USERAGENT, "ownCloud Server Crawler");
  583. if(OC_Config::getValue('proxy','')<>'') {
  584. curl_setopt($curl, CURLOPT_PROXY, OC_Config::getValue('proxy'));
  585. }
  586. if(OC_Config::getValue('proxyuserpwd','')<>'') {
  587. curl_setopt($curl, CURLOPT_PROXYUSERPWD, OC_Config::getValue('proxyuserpwd'));
  588. }
  589. $data = curl_exec($curl);
  590. curl_close($curl);
  591. } else {
  592. $contextArray = null;
  593. if(OC_Config::getValue('proxy','')<>'') {
  594. $contextArray = array(
  595. 'http' => array(
  596. 'timeout' => 10,
  597. 'proxy' => OC_Config::getValue('proxy')
  598. )
  599. );
  600. } else {
  601. $contextArray = array(
  602. 'http' => array(
  603. 'timeout' => 10
  604. )
  605. );
  606. }
  607. $ctx = stream_context_create(
  608. $contextArray
  609. );
  610. $data=@file_get_contents($url, 0, $ctx);
  611. }
  612. return $data;
  613. }
  614. /**
  615. * @return bool - well are we running on windows or not
  616. */
  617. public static function runningOnWindows() {
  618. return (substr(PHP_OS, 0, 3) === "WIN");
  619. }
  620. }