helper.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Frank Karlitschek
  6. * @author Jakob Sack
  7. * @copyright 2012 Frank Karlitschek frank@owncloud.org
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  11. * License as published by the Free Software Foundation; either
  12. * version 3 of the License, or any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public
  20. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. /**
  24. * Collection of useful functions
  25. */
  26. class OC_Helper {
  27. private static $mimetypes=array();
  28. private static $tmpFiles=array();
  29. /**
  30. * @brief Creates an url using a defined route
  31. * @param $route
  32. * @param $parameters
  33. * @param $args array with param=>value, will be appended to the returned url
  34. * @returns the url
  35. *
  36. * Returns a url to the given app and file.
  37. */
  38. public static function linkToRoute( $route, $parameters = array() ) {
  39. $urlLinkTo = OC::getRouter()->generate($route, $parameters);
  40. return $urlLinkTo;
  41. }
  42. /**
  43. * @brief Creates an url
  44. * @param string $app app
  45. * @param string $file file
  46. * @param array $args array with param=>value, will be appended to the returned url
  47. * The value of $args will be urlencoded
  48. * @return string the url
  49. *
  50. * Returns a url to the given app and file.
  51. */
  52. public static function linkTo( $app, $file, $args = array() ) {
  53. if( $app != '' ) {
  54. $app_path = OC_App::getAppPath($app);
  55. // Check if the app is in the app folder
  56. if( $app_path && file_exists( $app_path.'/'.$file )) {
  57. if(substr($file, -3) == 'php' || substr($file, -3) == 'css') {
  58. $urlLinkTo = OC::$WEBROOT . '/index.php/apps/' . $app;
  59. $urlLinkTo .= ($file!='index.php') ? '/' . $file : '';
  60. }else{
  61. $urlLinkTo = OC_App::getAppWebPath($app) . '/' . $file;
  62. }
  63. }
  64. else{
  65. $urlLinkTo = OC::$WEBROOT . '/' . $app . '/' . $file;
  66. }
  67. }
  68. else{
  69. if( file_exists( OC::$SERVERROOT . '/core/'. $file )) {
  70. $urlLinkTo = OC::$WEBROOT . '/core/'.$file;
  71. }
  72. else{
  73. $urlLinkTo = OC::$WEBROOT . '/'.$file;
  74. }
  75. }
  76. if (!empty($args)) {
  77. $urlLinkTo .= '?';
  78. foreach($args as $k => $v) {
  79. $urlLinkTo .= '&'.$k.'='.urlencode($v);
  80. }
  81. }
  82. return $urlLinkTo;
  83. }
  84. /**
  85. * @brief Creates an absolute url
  86. * @param string $app app
  87. * @param string $file file
  88. * @param array $args array with param=>value, will be appended to the returned url
  89. * The value of $args will be urlencoded
  90. * @return string the url
  91. *
  92. * Returns a absolute url to the given app and file.
  93. */
  94. public static function linkToAbsolute( $app, $file, $args = array() ) {
  95. $urlLinkTo = self::linkTo( $app, $file, $args );
  96. return self::makeURLAbsolute($urlLinkTo);
  97. }
  98. /**
  99. * @brief Makes an $url absolute
  100. * @param string $url the url
  101. * @return string the absolute url
  102. *
  103. * Returns a absolute url to the given app and file.
  104. */
  105. public static function makeURLAbsolute( $url )
  106. {
  107. return OC_Request::serverProtocol(). '://' . OC_Request::serverHost() . $url;
  108. }
  109. /**
  110. * @brief Creates an url for remote use
  111. * @param string $service id
  112. * @return string the url
  113. *
  114. * Returns a url to the given service.
  115. */
  116. public static function linkToRemoteBase( $service ) {
  117. return self::linkTo( '', 'remote.php') . '/' . $service;
  118. }
  119. /**
  120. * @brief Creates an absolute url for remote use
  121. * @param string $service id
  122. * @return string the url
  123. *
  124. * Returns a absolute url to the given service.
  125. */
  126. public static function linkToRemote( $service, $add_slash = true ) {
  127. return self::makeURLAbsolute(self::linkToRemoteBase($service)) . (($add_slash && $service[strlen($service)-1]!='/')?'/':'');
  128. }
  129. /**
  130. * @brief Creates an absolute url for public use
  131. * @param string $service id
  132. * @return string the url
  133. *
  134. * Returns a absolute url to the given service.
  135. */
  136. public static function linkToPublic($service, $add_slash = false) {
  137. return self::linkToAbsolute( '', 'public.php') . '?service=' . $service . (($add_slash && $service[strlen($service)-1]!='/')?'/':'');
  138. }
  139. /**
  140. * @brief Creates path to an image
  141. * @param string $app app
  142. * @param string $image image name
  143. * @return string the url
  144. *
  145. * Returns the path to the image.
  146. */
  147. public static function imagePath( $app, $image ) {
  148. // Read the selected theme from the config file
  149. $theme=OC_Config::getValue( "theme" );
  150. // Check if the app is in the app folder
  151. if( file_exists( OC::$SERVERROOT."/themes/$theme/apps/$app/img/$image" )) {
  152. return OC::$WEBROOT."/themes/$theme/apps/$app/img/$image";
  153. }elseif( file_exists(OC_App::getAppPath($app)."/img/$image" )) {
  154. return OC_App::getAppWebPath($app)."/img/$image";
  155. }elseif( !empty( $app ) and file_exists( OC::$SERVERROOT."/themes/$theme/$app/img/$image" )) {
  156. return OC::$WEBROOT."/themes/$theme/$app/img/$image";
  157. }elseif( !empty( $app ) and file_exists( OC::$SERVERROOT."/$app/img/$image" )) {
  158. return OC::$WEBROOT."/$app/img/$image";
  159. }elseif( file_exists( OC::$SERVERROOT."/themes/$theme/core/img/$image" )) {
  160. return OC::$WEBROOT."/themes/$theme/core/img/$image";
  161. }elseif( file_exists( OC::$SERVERROOT."/core/img/$image" )) {
  162. return OC::$WEBROOT."/core/img/$image";
  163. }else{
  164. echo('image not found: image:'.$image.' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT);
  165. die();
  166. }
  167. }
  168. /**
  169. * @brief get path to icon of file type
  170. * @param string $mimetype mimetype
  171. * @return string the url
  172. *
  173. * Returns the path to the image of this file type.
  174. */
  175. public static function mimetypeIcon( $mimetype ) {
  176. $alias=array('application/xml'=>'code/xml');
  177. if(isset($alias[$mimetype])) {
  178. $mimetype=$alias[$mimetype];
  179. }
  180. // Replace slash with a minus
  181. $mimetype = str_replace( "/", "-", $mimetype );
  182. // Is it a dir?
  183. if( $mimetype == "dir" ) {
  184. return OC::$WEBROOT."/core/img/filetypes/folder.png";
  185. }
  186. // Icon exists?
  187. if( file_exists( OC::$SERVERROOT."/core/img/filetypes/$mimetype.png" )) {
  188. return OC::$WEBROOT."/core/img/filetypes/$mimetype.png";
  189. }
  190. //try only the first part of the filetype
  191. $mimetype=substr($mimetype, 0, strpos($mimetype, '-'));
  192. if( file_exists( OC::$SERVERROOT."/core/img/filetypes/$mimetype.png" )) {
  193. return OC::$WEBROOT."/core/img/filetypes/$mimetype.png";
  194. }
  195. else{
  196. return OC::$WEBROOT."/core/img/filetypes/file.png";
  197. }
  198. }
  199. /**
  200. * @brief Make a human file size
  201. * @param int $bytes file size in bytes
  202. * @return string a human readable file size
  203. *
  204. * Makes 2048 to 2 kB.
  205. */
  206. public static function humanFileSize( $bytes ) {
  207. if( $bytes < 1024 ) {
  208. return "$bytes B";
  209. }
  210. $bytes = round( $bytes / 1024, 1 );
  211. if( $bytes < 1024 ) {
  212. return "$bytes kB";
  213. }
  214. $bytes = round( $bytes / 1024, 1 );
  215. if( $bytes < 1024 ) {
  216. return "$bytes MB";
  217. }
  218. // Wow, heavy duty for owncloud
  219. $bytes = round( $bytes / 1024, 1 );
  220. return "$bytes GB";
  221. }
  222. /**
  223. * @brief Make a computer file size
  224. * @param string $str file size in a fancy format
  225. * @return int a file size in bytes
  226. *
  227. * Makes 2kB to 2048.
  228. *
  229. * Inspired by: http://www.php.net/manual/en/function.filesize.php#92418
  230. */
  231. public static function computerFileSize( $str ) {
  232. $str=strtolower($str);
  233. $bytes_array = array(
  234. 'b' => 1,
  235. 'k' => 1024,
  236. 'kb' => 1024,
  237. 'mb' => 1024 * 1024,
  238. 'm' => 1024 * 1024,
  239. 'gb' => 1024 * 1024 * 1024,
  240. 'g' => 1024 * 1024 * 1024,
  241. 'tb' => 1024 * 1024 * 1024 * 1024,
  242. 't' => 1024 * 1024 * 1024 * 1024,
  243. 'pb' => 1024 * 1024 * 1024 * 1024 * 1024,
  244. 'p' => 1024 * 1024 * 1024 * 1024 * 1024,
  245. );
  246. $bytes = floatval($str);
  247. if (preg_match('#([kmgtp]?b?)$#si', $str, $matches) && !empty($bytes_array[$matches[1]])) {
  248. $bytes *= $bytes_array[$matches[1]];
  249. }
  250. $bytes = round($bytes, 2);
  251. return $bytes;
  252. }
  253. /**
  254. * @brief Recursive editing of file permissions
  255. * @param string $path path to file or folder
  256. * @param int $filemode unix style file permissions
  257. * @return bool
  258. */
  259. static function chmodr($path, $filemode) {
  260. if (!is_dir($path))
  261. return chmod($path, $filemode);
  262. $dh = opendir($path);
  263. while (($file = readdir($dh)) !== false) {
  264. if($file != '.' && $file != '..') {
  265. $fullpath = $path.'/'.$file;
  266. if(is_link($fullpath))
  267. return false;
  268. elseif(!is_dir($fullpath) && !@chmod($fullpath, $filemode))
  269. return false;
  270. elseif(!self::chmodr($fullpath, $filemode))
  271. return false;
  272. }
  273. }
  274. closedir($dh);
  275. if(@chmod($path, $filemode))
  276. return true;
  277. else
  278. return false;
  279. }
  280. /**
  281. * @brief Recursive copying of folders
  282. * @param string $src source folder
  283. * @param string $dest target folder
  284. *
  285. */
  286. static function copyr($src, $dest) {
  287. if(is_dir($src)) {
  288. if(!is_dir($dest)) {
  289. mkdir($dest);
  290. }
  291. $files = scandir($src);
  292. foreach ($files as $file) {
  293. if ($file != "." && $file != "..") {
  294. self::copyr("$src/$file", "$dest/$file");
  295. }
  296. }
  297. }elseif(file_exists($src) && !OC_Filesystem::isFileBlacklisted($src)) {
  298. copy($src, $dest);
  299. }
  300. }
  301. /**
  302. * @brief Recursive deletion of folders
  303. * @param string $dir path to the folder
  304. * @return bool
  305. */
  306. static function rmdirr($dir) {
  307. if(is_dir($dir)) {
  308. $files=scandir($dir);
  309. foreach($files as $file) {
  310. if ($file != "." && $file != "..") {
  311. self::rmdirr("$dir/$file");
  312. }
  313. }
  314. rmdir($dir);
  315. }elseif(file_exists($dir)) {
  316. unlink($dir);
  317. }
  318. if(file_exists($dir)) {
  319. return false;
  320. }else{
  321. return true;
  322. }
  323. }
  324. /**
  325. * get the mimetype form a local file
  326. * @param string $path
  327. * @return string
  328. * does NOT work for ownClouds filesystem, use OC_FileSystem::getMimeType instead
  329. */
  330. static function getMimeType($path) {
  331. $isWrapped=(strpos($path, '://')!==false) and (substr($path, 0, 7)=='file://');
  332. if (@is_dir($path)) {
  333. // directories are easy
  334. return "httpd/unix-directory";
  335. }
  336. if(strpos($path, '.')) {
  337. //try to guess the type by the file extension
  338. if(!self::$mimetypes || self::$mimetypes != include 'mimetypes.list.php') {
  339. self::$mimetypes=include 'mimetypes.list.php';
  340. }
  341. $extension=strtolower(strrchr(basename($path), "."));
  342. $extension=substr($extension, 1);//remove leading .
  343. $mimeType=(isset(self::$mimetypes[$extension]))?self::$mimetypes[$extension]:'application/octet-stream';
  344. }else{
  345. $mimeType='application/octet-stream';
  346. }
  347. if($mimeType=='application/octet-stream' and function_exists('finfo_open') and function_exists('finfo_file') and $finfo=finfo_open(FILEINFO_MIME)) {
  348. $info = @strtolower(finfo_file($finfo, $path));
  349. if($info) {
  350. $mimeType=substr($info, 0, strpos($info, ';'));
  351. }
  352. finfo_close($finfo);
  353. }
  354. if (!$isWrapped and $mimeType=='application/octet-stream' && function_exists("mime_content_type")) {
  355. // use mime magic extension if available
  356. $mimeType = mime_content_type($path);
  357. }
  358. if (!$isWrapped and $mimeType=='application/octet-stream' && OC_Helper::canExecute("file")) {
  359. // it looks like we have a 'file' command,
  360. // lets see if it does have mime support
  361. $path=escapeshellarg($path);
  362. $fp = popen("file -i -b $path 2>/dev/null", "r");
  363. $reply = fgets($fp);
  364. pclose($fp);
  365. // we have smth like 'text/x-c++; charset=us-ascii\n'
  366. // and need to eliminate everything starting with semicolon including trailing LF
  367. $mimeType = preg_replace('/;.*/ms', '', trim($reply));
  368. }
  369. return $mimeType;
  370. }
  371. /**
  372. * get the mimetype form a data string
  373. * @param string $data
  374. * @return string
  375. */
  376. static function getStringMimeType($data) {
  377. if(function_exists('finfo_open') and function_exists('finfo_file')) {
  378. $finfo=finfo_open(FILEINFO_MIME);
  379. return finfo_buffer($finfo, $data);
  380. }else{
  381. $tmpFile=OC_Helper::tmpFile();
  382. $fh=fopen($tmpFile, 'wb');
  383. fwrite($fh, $data, 8024);
  384. fclose($fh);
  385. $mime=self::getMimeType($tmpFile);
  386. unset($tmpFile);
  387. return $mime;
  388. }
  389. }
  390. /**
  391. * @brief Checks $_REQUEST contains a var for the $s key. If so, returns the html-escaped value of this var; otherwise returns the default value provided by $d.
  392. * @param string $s name of the var to escape, if set.
  393. * @param string $d default value.
  394. * @return string the print-safe value.
  395. *
  396. */
  397. //FIXME: should also check for value validation (i.e. the email is an email).
  398. public static function init_var($s, $d="") {
  399. $r = $d;
  400. if(isset($_REQUEST[$s]) && !empty($_REQUEST[$s]))
  401. $r = stripslashes(htmlspecialchars($_REQUEST[$s]));
  402. return $r;
  403. }
  404. /**
  405. * returns "checked"-attribute if request contains selected radio element OR if radio element is the default one -- maybe?
  406. * @param string $s Name of radio-button element name
  407. * @param string $v Value of current radio-button element
  408. * @param string $d Value of default radio-button element
  409. */
  410. public static function init_radio($s, $v, $d) {
  411. if((isset($_REQUEST[$s]) && $_REQUEST[$s]==$v) || (!isset($_REQUEST[$s]) && $v == $d))
  412. print "checked=\"checked\" ";
  413. }
  414. /**
  415. * detect if a given program is found in the search PATH
  416. *
  417. * @param string $program name
  418. * @param string $optional search path, defaults to $PATH
  419. * @return bool true if executable program found in path
  420. */
  421. public static function canExecute($name, $path = false) {
  422. // path defaults to PATH from environment if not set
  423. if ($path === false) {
  424. $path = getenv("PATH");
  425. }
  426. // check method depends on operating system
  427. if (!strncmp(PHP_OS, "WIN", 3)) {
  428. // on Windows an appropriate COM or EXE file needs to exist
  429. $exts = array(".exe", ".com");
  430. $check_fn = "file_exists";
  431. } else {
  432. // anywhere else we look for an executable file of that name
  433. $exts = array("");
  434. $check_fn = "is_executable";
  435. }
  436. // Default check will be done with $path directories :
  437. $dirs = explode(PATH_SEPARATOR, $path);
  438. // WARNING : We have to check if open_basedir is enabled :
  439. $obd = ini_get('open_basedir');
  440. if($obd != "none") {
  441. $obd_values = explode(PATH_SEPARATOR, $obd);
  442. if(count($obd_values) > 0 and $obd_values[0]) {
  443. // open_basedir is in effect !
  444. // We need to check if the program is in one of these dirs :
  445. $dirs = $obd_values;
  446. }
  447. }
  448. foreach($dirs as $dir) {
  449. foreach($exts as $ext) {
  450. if($check_fn("$dir/$name".$ext))
  451. return true;
  452. }
  453. }
  454. return false;
  455. }
  456. /**
  457. * copy the contents of one stream to another
  458. * @param resource $source
  459. * @param resource $target
  460. * @return int the number of bytes copied
  461. */
  462. public static function streamCopy($source, $target) {
  463. if(!$source or !$target) {
  464. return false;
  465. }
  466. $count=0;
  467. while(!feof($source)) {
  468. $count+=fwrite($target, fread($source, 8192));
  469. }
  470. return $count;
  471. }
  472. /**
  473. * create a temporary file with an unique filename
  474. * @param string $postfix
  475. * @return string
  476. *
  477. * temporary files are automatically cleaned up after the script is finished
  478. */
  479. public static function tmpFile($postfix='') {
  480. $file=get_temp_dir().'/'.md5(time().rand()).$postfix;
  481. $fh=fopen($file, 'w');
  482. fclose($fh);
  483. self::$tmpFiles[]=$file;
  484. return $file;
  485. }
  486. /**
  487. * create a temporary file with an unique filename. It will not be deleted
  488. * automatically
  489. * @param string $postfix
  490. * @return string
  491. *
  492. */
  493. public static function tmpFileNoClean($postfix='') {
  494. $tmpDirNoClean=get_temp_dir().'/oc-noclean/';
  495. if (!file_exists($tmpDirNoClean) || !is_dir($tmpDirNoClean)) {
  496. if (file_exists($tmpDirNoClean)) {
  497. unlink($tmpDirNoClean);
  498. }
  499. mkdir($tmpDirNoClean);
  500. }
  501. $file=$tmpDirNoClean.md5(time().rand()).$postfix;
  502. $fh=fopen($file,'w');
  503. fclose($fh);
  504. return $file;
  505. }
  506. /**
  507. * create a temporary folder with an unique filename
  508. * @return string
  509. *
  510. * temporary files are automatically cleaned up after the script is finished
  511. */
  512. public static function tmpFolder() {
  513. $path=get_temp_dir().'/'.md5(time().rand());
  514. mkdir($path);
  515. self::$tmpFiles[]=$path;
  516. return $path.'/';
  517. }
  518. /**
  519. * remove all files created by self::tmpFile
  520. */
  521. public static function cleanTmp() {
  522. $leftoversFile=get_temp_dir().'/oc-not-deleted';
  523. if(file_exists($leftoversFile)) {
  524. $leftovers=file($leftoversFile);
  525. foreach($leftovers as $file) {
  526. self::rmdirr($file);
  527. }
  528. unlink($leftoversFile);
  529. }
  530. foreach(self::$tmpFiles as $file) {
  531. if(file_exists($file)) {
  532. if(!self::rmdirr($file)) {
  533. file_put_contents($leftoversFile, $file."\n", FILE_APPEND);
  534. }
  535. }
  536. }
  537. }
  538. /**
  539. * remove all files created by self::tmpFileNoClean
  540. */
  541. public static function cleanTmpNoClean() {
  542. $tmpDirNoCleanFile=get_temp_dir().'/oc-noclean/';
  543. if(file_exists($tmpDirNoCleanFile)) {
  544. self::rmdirr($tmpDirNoCleanFile);
  545. }
  546. }
  547. /**
  548. * Adds a suffix to the name in case the file exists
  549. *
  550. * @param $path
  551. * @param $filename
  552. * @return string
  553. */
  554. public static function buildNotExistingFileName($path, $filename) {
  555. if($path==='/') {
  556. $path='';
  557. }
  558. if ($pos = strrpos($filename, '.')) {
  559. $name = substr($filename, 0, $pos);
  560. $ext = substr($filename, $pos);
  561. } else {
  562. $name = $filename;
  563. $ext = '';
  564. }
  565. $newpath = $path . '/' . $filename;
  566. $counter = 2;
  567. while (OC_Filesystem::file_exists($newpath)) {
  568. $newname = $name . ' (' . $counter . ')' . $ext;
  569. $newpath = $path . '/' . $newname;
  570. $counter++;
  571. }
  572. return $newpath;
  573. }
  574. /*
  575. * checks if $sub is a subdirectory of $parent
  576. *
  577. * @param string $sub
  578. * @param string $parent
  579. * @return bool
  580. */
  581. public static function issubdirectory($sub, $parent) {
  582. if($sub == null || $sub == '' || $parent == null || $parent == '') {
  583. return false;
  584. }
  585. $realpath_sub = realpath($sub);
  586. $realpath_parent = realpath($parent);
  587. if(($realpath_sub == false && substr_count($realpath_sub, './') != 0) || ($realpath_parent == false && substr_count($realpath_parent, './') != 0)) { //it checks for both ./ and ../
  588. return false;
  589. }
  590. if($realpath_sub && $realpath_sub != '' && $realpath_parent && $realpath_parent != '') {
  591. if(substr($realpath_sub, 0, strlen($realpath_parent)) == $realpath_parent) {
  592. return true;
  593. }
  594. }else{
  595. if(substr($sub, 0, strlen($parent)) == $parent) {
  596. return true;
  597. }
  598. }
  599. /*echo 'SUB: ' . $sub . "\n";
  600. echo 'PAR: ' . $parent . "\n";
  601. echo 'REALSUB: ' . $realpath_sub . "\n";
  602. echo 'REALPAR: ' . $realpath_parent . "\n";
  603. echo substr($realpath_sub, 0, strlen($realpath_parent));
  604. exit;*/
  605. return false;
  606. }
  607. /**
  608. * @brief Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
  609. *
  610. * @param array $input The array to work on
  611. * @param int $case Either MB_CASE_UPPER or MB_CASE_LOWER (default)
  612. * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
  613. * @return array
  614. *
  615. * Returns an array with all keys from input lowercased or uppercased. Numbered indices are left as is.
  616. * based on http://www.php.net/manual/en/function.array-change-key-case.php#107715
  617. *
  618. */
  619. public static function mb_array_change_key_case($input, $case = MB_CASE_LOWER, $encoding = 'UTF-8') {
  620. $case = ($case != MB_CASE_UPPER) ? MB_CASE_LOWER : MB_CASE_UPPER;
  621. $ret = array();
  622. foreach ($input as $k => $v) {
  623. $ret[mb_convert_case($k, $case, $encoding)] = $v;
  624. }
  625. return $ret;
  626. }
  627. /**
  628. * @brief replaces a copy of string delimited by the start and (optionally) length parameters with the string given in replacement.
  629. *
  630. * @param string $input The input string. .Opposite to the PHP build-in function does not accept an array.
  631. * @param string $replacement The replacement string.
  632. * @param int $start If start is positive, the replacing will begin at the start'th offset into string. If start is negative, the replacing will begin at the start'th character from the end of string.
  633. * @param int $length Length of the part to be replaced
  634. * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
  635. * @return string
  636. *
  637. */
  638. public static function mb_substr_replace($string, $replacement, $start, $length = null, $encoding = 'UTF-8') {
  639. $start = intval($start);
  640. $length = intval($length);
  641. $string = mb_substr($string, 0, $start, $encoding) .
  642. $replacement .
  643. mb_substr($string, $start+$length, mb_strlen($string, 'UTF-8')-$start, $encoding);
  644. return $string;
  645. }
  646. /**
  647. * @brief Replace all occurrences of the search string with the replacement string
  648. *
  649. * @param string $search The value being searched for, otherwise known as the needle.
  650. * @param string $replace The replacement
  651. * @param string $subject The string or array being searched and replaced on, otherwise known as the haystack.
  652. * @param string $encoding The encoding parameter is the character encoding. Defaults to UTF-8
  653. * @param int $count If passed, this will be set to the number of replacements performed.
  654. * @return string
  655. *
  656. */
  657. public static function mb_str_replace($search, $replace, $subject, $encoding = 'UTF-8', &$count = null) {
  658. $offset = -1;
  659. $length = mb_strlen($search, $encoding);
  660. while(($i = mb_strrpos($subject, $search, $offset, $encoding)) !== false ) {
  661. $subject = OC_Helper::mb_substr_replace($subject, $replace, $i, $length);
  662. $offset = $i - mb_strlen($subject, $encoding);
  663. $count++;
  664. }
  665. return $subject;
  666. }
  667. /**
  668. * @brief performs a search in a nested array
  669. * @param array $haystack the array to be searched
  670. * @param string $needle the search string
  671. * @param string $index optional, only search this key name
  672. * @return mixed the key of the matching field, otherwise false
  673. *
  674. * performs a search in a nested array
  675. *
  676. * taken from http://www.php.net/manual/en/function.array-search.php#97645
  677. */
  678. public static function recursiveArraySearch($haystack, $needle, $index = null) {
  679. $aIt = new RecursiveArrayIterator($haystack);
  680. $it = new RecursiveIteratorIterator($aIt);
  681. while($it->valid()) {
  682. if (((isset($index) AND ($it->key() == $index)) OR (!isset($index))) AND ($it->current() == $needle)) {
  683. return $aIt->key();
  684. }
  685. $it->next();
  686. }
  687. return false;
  688. }
  689. /**
  690. * Shortens str to maxlen by replacing characters in the middle with '...', eg.
  691. * ellipsis('a very long string with lots of useless info to make a better example', 14) becomes 'a very ...example'
  692. * @param string $str the string
  693. * @param string $maxlen the maximum length of the result
  694. * @return string with at most maxlen characters
  695. */
  696. public static function ellipsis($str, $maxlen) {
  697. if (strlen($str) > $maxlen) {
  698. $characters = floor($maxlen / 2);
  699. return substr($str, 0, $characters) . '...' . substr($str, -1 * $characters);
  700. }
  701. return $str;
  702. }
  703. }