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.

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