helper.php 20 KB

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