/**
  * Converts a string from one charset to another.
  *
  * Uses the iconv or the mbstring extensions.
  * The original string is returned if conversion failed or none
  * of the extensions were available.
  *
  * @param mixed $input    The data to be converted. If $input is an an
  *                        array, the array's values get converted
  *                        recursively.
  * @param string $from    The string's current charset.
  * @param string $to      The charset to convert the string to.
  * @param boolean $force  Force conversion?
  *
  * @return mixed  The converted input data.
  */
 public static function convertCharset($input, $from, $to, $force = false)
 {
     /* Don't bother converting numbers. */
     if (is_numeric($input)) {
         return $input;
     }
     /* If the from and to character sets are identical, return now. */
     if (!$force && $from == $to) {
         return $input;
     }
     $from = self::lower($from);
     $to = self::lower($to);
     if (!$force && $from == $to) {
         return $input;
     }
     if (is_array($input)) {
         $tmp = array();
         reset($input);
         while (list($key, $val) = each($input)) {
             $tmp[self::_convertCharset($key, $from, $to)] = self::convertCharset($val, $from, $to, $force);
         }
         return $tmp;
     }
     if (is_object($input)) {
         // PEAR_Error/Exception objects are almost guaranteed to contain
         // recursion, which will cause a segfault in PHP. We should never
         // reach this line, but add a check.
         if ($input instanceof Exception || $input instanceof PEAR_Error) {
             return '';
         }
         $input = Horde_Util::cloneObject($input);
         $vars = get_object_vars($input);
         while (list($key, $val) = each($vars)) {
             $input->{$key} = self::convertCharset($val, $from, $to, $force);
         }
         return $input;
     }
     if (!is_string($input)) {
         return $input;
     }
     return self::_convertCharset($input, $from, $to);
 }
Esempio n. 2
0
 /**
  * Retrieve a specific MIME part.
  *
  * @param string $id The MIME_Part ID string.
  *
  * @return MIME_Part  The MIME_Part requested, or false if the part
  *                    doesn't exist.
  */
 public function getPart($id)
 {
     $mimeid = $this->getMIMEId();
     /* This will convert '#.0' to simply '#', which is how the part is
      * internally stored. */
     $search_id = $id;
     if (($str = strrchr($id, '.')) && $str == '.0') {
         $search_id = substr($search_id, 0, -2);
     }
     /* Return this part if:
        1) There is only one part (e.g. the MIME ID is 0, or the
           MIME ID is 1 and there are no subparts.
        2) $id matches this parts MIME ID. */
     if ($search_id == 0 || $search_id == 1 && !count($this->_parts) || !empty($mimeid) && $search_id == $mimeid) {
         $part = $this;
     } else {
         $part = $this->_partFind($id, $this->_parts);
     }
     if ($part && $search_id != $id && $part->getType() == 'message/rfc822') {
         $ret_part = Horde_Util::cloneObject($part);
         $ret_part->_parts = array();
         return $ret_part;
     }
     return $part;
 }