helper.php 20 KB

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