cancel
Showing results for 
Search instead for 
Did you mean: 

How to set a custom document type?[Urgent]

maqsood
Confirmed Champ
Confirmed Champ
Hi

Can any one have a code or pointer on how to set custom document type and its properties in php. the following code i found on net to set an aspect.

// Add the two aspects
$newNode>
addAspect("{http://www.someco.com/model/content/1.0}webable");
$newNode>
addAspect("{http://www.someco.com/model/content/1.0}productRelated");

i have to retrieve a custom document type & then set its properties in php. The same thing i can do it in java using the following code:

CMLCreate create = new CMLCreate("1", companyHomeParent, companyHomeParent.getUuid(), Constants.ASSOC_CONTAINS, null, Constants.PROP_CONTENT, contentProps);
create.setType("{custom.Model}ProjProps");

Any help??
11 REPLIES 11

gyro_gearless
Champ in-the-making
Champ in-the-making
IIRR, you can set the created nodes type in the createChild() invocation:


$newNode = $folderNode->createChild('cm_content', 'cm_contains', "Hui_Buh__$timestamp");
$contentData = new AlfContentData($newNode, '{http://www.alfresco.org/model/content/1.0}content');
$contentData->setMimetype('application/pdf');
$contentData->setEncoding ('UTF-8');
$contentData->writeContentFromFile('4046.pdf');

// Set the properties
$props = $newNode->getProperties();
$props['{http://www.alfresco.org/model/content/1.0}content'] = $contentData;
$props['{http://www.alfresco.org/model/content/1.0}name'] = "Hui Buh (" . $timestamp . ").pdf";
$props['{http://www.alfresco.org/model/content/1.0}description'] = 'Ein doofer Test';
$props['{http://www.alfresco.org/model/content/1.0}title'] = 'Alles ueber Hui Buh';

$newNode->setProperties($props);

$session->save(false);

This should work for custom document types, too; but i'm not sure if i ever tried this  :roll:

Please be aware that Alfrescos PHP library does not deal well with custom namespaces (in fact, it doesnt even deal with Alfrescos own namespaces), so might need the tweaked version available from http://www.sugarforge.org/frs/download.php/6171/Alfredo-PHPAPI-5.2.0--2.0.0-TEST-r8433__2009-11-19-1...

maqsood
Confirmed Champ
Confirmed Champ
Thanks Gyro for your swift reply

Have tried executing your code, it executed well without errors. but file content is not uploaded and also the custom document type is not getting attached.

Can you please gudie what is wrong in below code?

$newNode = $home->createChild('cm_content', 'cm_contains', "cm_".$name);
$contentData = new ContentData($newNode, '{custom.model}TC');
            $contentData->setMimetype($_FILES['_asset']['type']);
            $contentData->setEncoding ('UTF-8');
            $contentData->writeContentFromFile($_FILES['_asset']['tmp_name']);
            
            // Set the properties
            $props = $newNode->getProperties();
            $props['{http://www.alfresco.org/model/content/1.0}content'] = $contentData;
            $props['{http://www.alfresco.org/model/content/1.0}name'] = $_POST['_name'];
            $props['{http://www.alfresco.org/model/content/1.0}description'] = $_POST['_desc'];
            $props['{http://www.alfresco.org/model/content/1.0}title'] = $_POST['_title'];
            $props['{http://www.alfresco.org/model/content/1.0}author'] = $_POST['_author'];
            $props['{custom.model}Asset Type'] = $_POST['_assetType'];
            $props['{custom.model}Board'] = $_POST['_board'];
            $props['{custom.model}Subject'] = $_POST['_subject'];
            $props['{custom.model}Age Group'] = $_POST['_ageGroup'];
            
            $newNode->setProperties($props);
            
            $session->save(false);

            

gyro_gearless
Champ in-the-making
Champ in-the-making
I suppose you should set your documents type as first parameter of ->createChild(); for new ContentData() use '{http://www.alfresco.org/model/content/1.0}content' as second parameter, as this the full name of the content property (you might have other content fields in custom document types, but this is perhaps what you want at this point…).

HTH
Gyro

maqsood
Confirmed Champ
Confirmed Champ
Thanks Gyro for your pointer, its works.

Need one more help, wanted to know, Is there any provision in alfresco php sdk to convert file/folder names into ISO 9705. if not, whats the alternate way for it. if i pass date or number in name while creating space/file, Alfresco server throws following error:

Failed to parse query: PATH:"app:company_home/cm:Folder1/cm:2010-10-8""

i googled and found there are functions in php to encode the strings into ISO_8859-1 but not for ISO 9705.

Thanks in advance.

maqsood
Confirmed Champ
Confirmed Champ
Any body has any pointers??

any help appreciated.

ddanninger
Champ in-the-making
Champ in-the-making
Well actually there is not really a ISO 9705 Encoding….

BUT

In the PEAR Package: XML_Query2XML  is a ISO 9705 Mapper…. i extracted this part out of the Pear Package and made a quick and dirty non PEAR ISO 9705 Mapper (just commented out raiserrors of pear and modified pear functions)

ISO9705Mapper.php
<?php
/**
* This file contains the class XML_Query2XML_ISO9075Mapper.
*
* PHP version 5
*
* @category  XML
* @package   XML_Query2XML
* @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
* @copyright 2006 Lukas Feiler
* @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
* @version   CVS: $Id: ISO9075Mapper.php,v 1.3 2007/11/19 01:36:56 lukasfeiler Exp $
* @link      http://pear.php.net/package/XML_Query2XML
*/


/**
* I18N_UnicodeString is used for converting UTF-8 to Unicode and vice versa.
*/
require_once 'I18N_UnicodeString.php';

/**
* Maps SQL identifiers to XML names according to Final Committee Draft for
* ISO/IEC 9075-14:2005, section "9.1 Mapping SQL <identifier>s to XML Names".
*
* ISO/IEC 9075-14:2005 is available online at
* http://www.sqlx.org/SQL-XML-documents/5FCD-14-XML-2004-07.pdf
*
* A lot of characters are legal in SQL identifiers but cannot be used within
* XML names. To begin with, SQL identifiers can contain any Unicode character
* while XML names are limited to a certain set of characters. E.g the
* SQL identifier "<21yrs in age" obviously is not a valid XML name.
* '#', '{', and '}' are also not allowed. Fully escaped SQL identifiers
* also must not contain a column (':') or start with "xml" (in any case
* combination). Illegal characters are mapped to a string of the form
* _xUUUU_ where UUUU is the Unicode value of the character.
*
* The following is a table of example mappings:
* <pre>
* +—————-+————————+————————————+
* | SQL-Identifier | Fully escaped XML name | Comment                            |
* +—————-+————————+————————————+
* | dept:id        | dept_x003A_id          | ":" is illegal                     |
* | xml_name       | _x0078_ml_name         | must not start with [Xx][Mm][Ll]   |
* | XML_name       | _x0058_ML_name         | must not start with [Xx][Mm][Ll]   |
* | hire date      | hire_x0020_date        | space is illegal too               |
* | Works@home     | Works_x0040_home       | "@" is illegal                     |
* | file_xls       | file_x005F_xls         | "_" gets mapped if followed by "x" |
* | FIRST_NAME     | FIRST_NAME             | no problem here                    |
* +—————-+————————+————————————+
* </pre>
*
* @category  XML
* @package   XML_Query2XML
* @author    Lukas Feiler <lukas.feiler@lukasfeiler.com>
* @copyright 2006 Lukas Feiler
* @license   http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
* @version   Release: 1.7.1
* @link      http://pear.php.net/package/XML_Query2XML
*/
class ISO9075Mapper
{
    /**
     * This method maps an SQL identifier to an XML name according to
     * FCD ISO/IEC 9075-14:2005.
     *
     * @param string $sqlIdentifier The SQL identifier as a UTF-8 string.
     *
     * @return string The fully escaped XML name.
     * @throws XML_Query2XML_ISO9075Mapper_Exception If $sqlIdentifier was a
     *                                               malformed UTF-8 string.
     */
    public static function map($sqlIdentifier)
    {
        /*
         * S as defined in section 9.1, paragraph 1 with the difference that
         * if N is the number of characters in SQLI the characters of SQLI,
         * in order from left to right are S[0], S[1], …, S[N-1].
         */
        $S = self::_utf8ToUnicode($sqlIdentifier);
       
        /*
         * X as defined in section 9.1, paragraph 4 with the differnce that
         * for each i between 0 (zero) and N-1, X[i] will be the Unicode
         * character string.
         */
        $X = array();
       
        /*
         * section 9.1, paragraph 4 lit a
         * a) If S[i] has no mapping to Unicode (i.e., TM(S[i]) is undefined),
         * then X[i] is implementation-defined.
         */
        for ($i = 0; $i < count($S); $i++) {
            if (self::_unicodeToUtf8($S[$i]) == ':') {
                // section 9.1, paragraph 4 lit b: If Si is <colon>, then
               
                if ($i == 0) {
                    // i) If i = 0 (zero), then let Xi be _x003A_.
                    $X[$i] = '_x003A_';
                } else {
                    // ii) If EV is fully escaped, then let Xi be _x003A_.
                    $X[$i] = '_x003A_';
                }
                /*
                 * iii) Otherwise, let X[i] be T[i]
                 * we always do a full escape - therefore we do
                 * not have to implement iii)
                 */
               
            } elseif (
                $i < count($S) - 1 &&
                self::_unicodeToUtf8($S[$i]) == '_' &&
                self::_unicodeToUtf8($S[$i+1]) == 'x'
            ) {
                /*
                 * section 9.1, paragraph 4 lit c: if i < N–1, S[i] is <underscore>,
                 * and S[i+1] is the lowercase letter x, then let X[i] be _x005F_.
                 */
                $X[$i] = '_x005F_';
               
            } elseif (
                !self::_isValidNameChar($S[$i]) ||
                $i == 0 &&
                !self::_isValidNameStartChar($S[$i])
            ) {
                /*
                 * section 9.1, paragraph 4 lit e: the SQL-implementation supports
                 * Feature X211, "XML 1.1 support", and either T[i] is not a valid
                 * XML 1.1 NameChar, or i = 0 (zerno) and T[0] is not a valid
                 * XML 1.1 NameStartChar
                 */
   
                $X[$i] = dechex($S[$i]);
                if (strlen($X[$i]) < 4) {
                    /*
                     * ii) 1) If U1 = 0 (zero), U2 = 0 (zero), U3 = 0 (zero), and
                     * U4 = 0 (zero), then let X[i} be _xU5U6U7U8_.
                     */
                    $X[$i] = str_pad($X[$i], 4, '0', STR_PAD_LEFT);
                } elseif (strlen($X[$i]) > 4) {
                    // ii) 2) Otherwise, let X[i] be _xU3U4U5U6U7U8_.
                    $X[$i] = str_pad($X[$i], 8, '0', STR_PAD_LEFT);
                }
                $X[$i] = '_x' . $X[$i] . '_';
            } else {
                /*
                 * section 9.1, paragraph 4 lit f: Otherwise, let X[i] be T[i].
                 * NOTE 21 — That is, any character in SQLI that does not occasion
                 * a problem as a character in an XML 1.0 NCName or XML 1.1 NCName
                 * is simply copied into the result.
                 */
                $X[$i] = self::_unicodeToUtf8($S[$i]);
            }
        }
       
        if (
            count($S) >=3 &&
            strpos(
                strtolower(
                    self::_unicodeToUtf8($S[0])
                    . self::_unicodeToUtf8($S[1])
                    . self::_unicodeToUtf8($S[2])
                ),
                'xml'
            ) === 0
        ) {
            /*
             * section 9.1, paragraph 4 lit d: if EV is fully escaped,
             * i = 0 (zero), N >= 3, S[0] is either the uppercase letter
             * X or the lowercase letter x, S[1] is either the uppercase
             * letter M or the lowercase letter m, and S[2] is either the
             * uppercase letter L or the lowercase letter l, then
             */
           
            if (self::_unicodeToUtf8($S[0]) == 'x') {
                // i) If S[0] is the lowercase letter x, then let X[0] be _x0078_.
                $X[0] = '_x0078_';
            } elseif (self::_unicodeToUtf8($S[0]) == 'X') {
                // ii) If S[0] is the uppercase letter X, then let X[0] be _x0058_.
                $X[0] = '_x0058_';
            }
        }
       
        /*
         * section 9.1, paragraph 5: let XMLN be the character string concatenation
         * of X[0], X[1], …, and X[N-1] in order from left to right.
         */
        $XMLN = '';
        for ($i = 0; $i < count($X); $i++) {
            $XMLN .= $X[$i];
        }
        return $XMLN;
    }
   
    /**
     * Returns whether $char is a valid XML 1.1. NameStartChar.
     * NameStartChar is defined as:
     * NameStartChar ::= ":" | [A-Z] | "_" | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] |
     *                   [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] |
     *                   [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] |
     *                   [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] |
     *                   [#x10000-#xEFFFF]
     *
     * @param int $c A unicode character as an integer.
     *
     * @return boolean Wheather $c is a valid NameStartChar.
     * @link http://www.w3.org/TR/xml11/
     */
    private static function _isValidNameStartChar($c)
    {
        return preg_match('/^[:A-Z_a-z]$/', self::_unicodeToUtf8($c)) !== 0 ||
               $c >= hexdec('C0') && $c <= hexdec('D6') ||
               $c >= hexdec('D8') && $c <= hexdec('F6') ||
               $c >= hexdec('F8') && $c <= hexdec('2FF') ||
               $c >= hexdec('370') && $c <= hexdec('37D') ||
               $c >= hexdec('37F') && $c <= hexdec('1FFF') ||
               $c >= hexdec('200C') && $c <= hexdec('200D') ||
               $c >= hexdec('2070') && $c <= hexdec('218F') ||
               $c >= hexdec('2C00') && $c <= hexdec('2FEF') ||
               $c >= hexdec('3001') && $c <= hexdec('D7FF') ||
               $c >= hexdec('F900') && $c <= hexdec('FDCF') ||
               $c >= hexdec('FDF0') && $c <= hexdec('FFFD') ||
               $c >= hexdec('10000') && $c <= hexdec('EFFFF');
    }
   
   
    /**
     * Returns whether $char is a valid XML 1.1. NameChar.
     * NameChar is defined as:
     * NameChar ::= NameStartChar | "-" | "." | [0-9] | #xB7 | [#x0300-#x036F] |
     *              [#x203F-#x2040]
     *
     * @param int $c A unicode character as an integer.
     *
     * @return boolean Wheather $char is a valid NameChar.
     * @link http://www.w3.org/TR/xml11/
     */
    private static function _isValidNameChar($c)
    {
        return self::_isValidNameStartChar($c) ||
               preg_match('/^[-\.0-9]$/', self::_unicodeToUtf8($c)) !== 0 ||
               $c == hexdec('B7') ||
               $c >= hexdec('0300') && $c <= hexdec('036F') ||
               $c >= hexdec('203F') && $c <= hexdec('2040');
    }
   
    /**
     * Converts a single unicode character represended by an integer
     * to an UTF-8 chracter
     *
     * @param int $char The unicode character as an integer
     *
     * @return string The UTF-8 character.
     */
    private static function _unicodeToUtf8($char)
    {
        return I18N_UnicodeString::unicodeCharToUtf8($char);
    }
   
    /**
     * Converts a UTF-8 string into unicode integers.
     *
     * @param string $string A string containing Unicode values encoded in UTF-8
     *
     * @return array The array of Unicode values.
     * @throws XML_Query2XML_ISO9075Mapper_Exception If a malformed UTF-8 string
     *                                               was passed as argument.
     */
    private static function _utf8ToUnicode($string)
    {
        $string = I18N_UnicodeString::utf8ToUnicode($string);
        if (strtolower(get_class($string)) == 'pear_error') {
            /*
             * unit tests:
             *  testMapException1()
             *  testMapException2()
             *  testMapException3()
             */
            throw new ISO9075Mapper_Exception(
                $string->getMessage()
            );
        }
        return $string;
    }
}

/**
* Only XML_Query2XML_ISO9075Mapper will throw this exception.
* It does not extend XML_Query2XML_Exception because the
* class XML_Query2XML_ISO9075Mapper should be usable without
* XML_Query2XML. XML_Query2XML itself will never throw this
* exception.
*
* @category XML
* @package  XML_Query2XML
* @author   Lukas Feiler <lukas.feiler@lukasfeiler.com>
* @license  http://www.gnu.org/copyleft/lesser.html  LGPL Version 2.1
* @link     http://pear.php.net/package/XML_Query2XML
*/
class ISO9075Mapper_Exception extends Exception
{
   
    /**
     * Constructor method
     *
     * @param string $message The error message.
     */
    public function __construct($message)
    {
        parent::__construct($message);
    }
}
?>

Require Class:

I18N_UnicodeString.php
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4: */
/**
* Provides a method of storing and manipulating multibyte strings in PHP.
*
* PHP versions 4 and 5
*
* LICENSE: Copyright 2004-2006 John Downey. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* o Redistributions of source code must retain the above copyright notice, this
*   list of conditions and the following disclaimer.
* o Redistributions in binary form must reproduce the above copyright notice,
*   this list of conditions and the following disclaimer in the documentation
*   and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of The PEAR Group.
*
* @category  Internationalization
* @package   I18N_UnicodeString
* @author    John Downey <jdowney@gmail.com>
* @copyright 2004-2006 John Downey
* @license   http://www.freebsd.org/copyright/freebsd-license.html 2 Clause BSD License
* @version   CVS: $Id$
* @filesource
*/

/**
* Class provides a way to use and manipulate multibyte strings in PHP
*
* @package I18N_UnicodeString
* @author  John Downey <jdowney@gmail.com>
* @access  public
* @version Release: @package_version@
*/
class I18N_UnicodeString
{
    /**
    * The internal representation of the string as an array of numbers.
    * @access private
    * @var    array $_unicode
    */
    var $_unicode = array();
   
    /**
    * Converts an entire array of strings to Unicode capable strings.
    *
    * Useful for converting a GET/POST of all its Unicode values into a
    * workable and easily changed format. Takes an optional second
    * parameter similer to that of {@link setString()}.
    *
    * @static
    * @access public
    * @param  array $array An array of PHP variables.
    * @param  string $encoding The encoding the string values are in.
    * @return array The array with all of its string values converted to
    *               I18N_Unicode objects.
    * @see    setString()
    */
    function convertArray($array, $encoding = 'HTML') {
        foreach($array as $key => $value) {
            if (is_string($value)) {
                $array[$key] = new I18N_UnicodeString($value, $encoding);
            }
        }
       
        return $array;
    }
   
    /**
    * The constructor of the class string which can receive a new string in a
    * number of formats.
    *
    * @access public
    * @param  mixed $value A variable containing the Unicode string in one of
    *                      various encodings.
    * @param  string $encoding The encoding that the string is in.
    * @see    setString()
    */
    function I18N_UnicodeString($value = '', $encoding = 'UTF-8') {
        $this->setString($value, $encoding);
    }
   
    /**
    * Set the string to a value passed in one of many encodings.
    *
    * You may pass the encoding as an optional second parameter which defaults
    * to UTF-8 encoding. Possible encodings are:
    *
    * o <i>ASCII</i> - when you pass a normal 7 bit ASCII string
    * o <i>UTF-8</i> - when you pass a UTF-8 encoded string
    * o <i>HTML</i> - when you pass a string encoded with HTML entities, such
    *   as the kind received from a GET/POST
    * o <i>Unicode</i> or <i>UCS-4</i> - when passing an array of integer values representing
    *   each character
    *
    * @access public
    * @param  mixed $value A variable containing the Unicode string in one of
    *                      various encodings.
    * @param  string $encoding The encoding that the string is in.
    * @return mixed Returns true on success or PEAR_Error otherwise.
    */
    function setString($value, $encoding = 'UTF-8') {
           switch(strtoupper($encoding)) {
            case 'ASCII':
            case 'UTF8':
            case 'UTF-8':
                $this->_unicode = self::utf8ToUnicode($value);
                break;

            case 'HTML':
                $this->_unicode = $this->_stringFromHtml($value);
                break;

            case 'UTF32':
            case 'UTF-32':
            case 'UCS4':
            case 'UCS-4':
            case 'UNICODE':
                $this->_unicode = $value;
                break;

            default:
                return self::raiseError('Unrecognized encoding');
        }
       
        if (strtolower(get_class($this->_unicode)) == 'pear_error') {
            return $this->_unicode;
        }
       
        return true;
    }
   
    /**
    * Converts a string encoded with HTML entities into our internal
    * representation of an array of integers.
    *
    * @access private
    * @param  string $string A string containing Unicode values encoded as HTML
    *                        entities.
    * @return array The array of Unicode values.
    */
    function _stringFromHtml($string = '') {
        $parts   = explode('&#', $string);
        $unicode = array();
        foreach($parts as $part) {
            $text = strstr($part, ';');
           
            if (!empty($text)) {
                $value = substr($part, 0, strpos($part, ';'));

                /* Suggested by Jonathan Yavner to allow HTML to also be in
                   the form of &#xNNNN where NNNN is the hexidecimal of a
                   Unicode character */
                if (ord($value[0]) == 120) {
                    $unicode[] = intval(substr($value, 1), 16);
                } else {
                    $unicode[] = intval($value);
                }

                $text = substr($text, 1);

                for($i = 0, $max = strlen($text); $i < $max; $i++) {
                    $unicode[] = ord($text[$i]);
                }
            } else {
                for($i = 0, $max = strlen($part); $i < $max; $i++) {
                    $unicode[] = ord($part[$i]);
                }
            }
        }

        return $unicode;
    }
   
    /**
    * Converts a UTF-8 string into our representation of an array of integers.
    *
    * Method was made static by suggestion of Lukas Feiler (#7429)
    *
    * @static
    * @access public
    * @param  string $string A string containing Unicode values encoded in UTF-8
    * @return array The array of Unicode values.
    */
    function utf8ToUnicode($string = '') {
        $unicode = array();
        $values  = array();
        $search  = 1;
       
        for($count = 0, $length = strlen($string); $count < $length; $count++) {
            $value = ord($string[$count]);

            if ($value < 128) {
                // if the value is an ASCII char then just go ahead and add it on
                $unicode[] = $value;
            } else {
                // if not then we need to know how many more bytes make up this character
                if (count($values) == 0) {
                    if ($value >> 5 == 6) {
                        $values[] = ($value - 192) << 6;
                        $search   = 2;
                    } elseif ($value >> 4 == 14) {
                        $values[] = ($value - 224) << 12;
                        $search   = 3;
                    } elseif ($value >> 3 == 30) {
                        $values[] = ($value - 240) << 18;
                        $search   = 4;
                    } elseif ($value >> 2 == 62) {
                        $values[] = ($value - 248) << 24;
                        $search   = 5;
                    } elseif ($value >> 1 == 126) {
                        $values[] = ($value - 252) << 30;
                        $search   = 6;
                    } else {
                        return self::raiseError('Malformed UTF-8 string');
                    }
                } else {
                    if ($value >> 6 == 2) {
                        $values[] = $value - 128;
                    } else {
                        return self::raiseError('Malformed UTF-8 string');
                    }

                    if (count($values) == $search) {
                        // if we have all of our bytes then go ahead an encode it in unicode
                        $value = $values[0];
                        for($i = 1; $i < $search; $i++) {
                            $value += ($values[$i] << ((($search - $i) - 1) * 6));
                        }
                       
                        $unicode[] = $value;

                        $values = array();
                        $search = 1;
                    }
                }
            }
        }
       
        return $unicode;
    }

    /**
     * Transforms a single unicode character represented by an integer to a UTF-8 string.
     *
     * Suggested by Lukas Feiler (#7429)
     *
     * @static
     * @access public
     * @param  integer $int A unicode character as an integer
     * @return string The unicode character converted to a UTF-8 string.
     */
    function unicodeCharToUtf8($char) {
        $string = '';
        if ($char < 128) {
            // its an ASCII char no encoding needed
            $string .= chr($char);
        } elseif ($char < 1 << 11) {
            // its a 2 byte UTF-8 char
            $string .= chr(192 + ($char >> 6));
            $string .= chr(128 + ($char & 63));
        } elseif ($char < 1 << 16) {
            // its a 3 byte UTF-8 char
            $string .= chr(224 + ($char >> 12));
            $string .= chr(128 + (($char >> 6) & 63));
            $string .= chr(128 + ($char & 63));
        } elseif ($char < 1 << 21) {
            // its a 4 byte UTF-8 char
            $string .= chr(240 + ($char >> 18));
            $string .= chr(128 + (($char >> 12) & 63));
            $string .= chr(128 + (($char >>  6) & 63));
            $string .= chr(128 + ($char & 63));
        } elseif ($char < 1 << 26) {
            // its a 5 byte UTF-8 char
            $string .= chr(248 + ($char >> 24));
            $string .= chr(128 + (($char >> 18) & 63));
            $string .= chr(128 + (($char >> 12) & 63));
            $string .= chr(128 + (($char >> 6) & 63));
            $string .= chr(128 + ($char & 63));
        } else {
            // its a 6 byte UTF-8 char
            $string .= chr(252 + ($char >> 30));
            $string .= chr(128 + (($char >> 24) & 63));
            $string .= chr(128 + (($char >> 18) & 63));
            $string .= chr(128 + (($char >> 12) & 63));
            $string .= chr(128 + (($char >> 6) & 63));
            $string .= chr(128 + ($char & 63));
        }

        return $string;
    }
   
    /**
    * Retrieves the string and returns it as a UTF-8 encoded string.
    *
    * @access public
    * @return string A string with the Unicode values encoded in UTF-8.
    */
    function toUtf8String() {
        $string = '';

        foreach($this->_unicode as $char) {
            $string .= $this->unicodeCharToUtf8($char);
        }

        return $string;
    }
   
    /**
    * Retrieves the string and returns it as a string encoded with HTML
    * entities.
    *
    * @access public
    * @return string A string with the Unicode values encoded as HTML entities.
    */
    function toHtmlEntitiesString() {
        $string = '';

        foreach($this->_unicode as $char) {
            if ($char > 127) {
                $string .= '&#' . $char . ';';
            } else {
                $string .= chr($char);
            }
        }

        return $string;
    }
   
    /**
    * Retrieve the length of the string in characters.
    *
    * @access public
    * @return integer The length of the string.
    */
    function length() {
        return count($this->_unicode);
    }
   
    /**
    * Works exactly like PHP's substr function only it works on Unicode
    * strings.
    *
    * @access public
    * @param  integer $begin The beginning of the substring.
    * @param  integer $length The length to read. Defaults to the rest of the
    *                         string.
    * @return I18N_UnicodeString A new I18N_UnicodeString class containing the
    *                            substring or a PEAR_Error if an error is
    *                            thrown.
    */
    function subString($begin, $length = null) {
        $unicode = array();
       
        if (is_null($length)) {
            $length = $this->length() - $begin;
        }
       
        if (($begin + $length) > $this->length()) {
            return self::raiseError('Cannot read past end of string.');       
        }
       
        if ($begin > $this->length()) {
            return self::raiseError('Beginning extends past end of string.');
        }
       
        for($i = $begin, $max_length = ($begin + $length); $i < $max_length; $i++) {
            array_push($unicode, $this->_unicode[$i]);
        }

        return new I18N_UnicodeString($unicode, 'Unicode');
    }
   
    /**
    * Works like PHP's substr_replace function.
    *
    * @access public
    * @param  I18N_UnicodeString $find The string to replaced
    * @param  I18N_UnicodeString $replace The string to replace $find with
    * @param  integer $start The position in the string to start replacing at
    * @param  integer $length The length from the starting to position to stop
    *                         replacing at
    * @return I18N_UnicodeString The current string with all $find replaced by
    *                            $replace
    * @see    stringReplace()
    */
    function subStringReplace(&$find, &$replace, $start, $length = null) {
        if (is_null($length)) {
            $length = $this->length() - $start;
        }
       
        $begin  = $this->subString(0, $start);
        $string = $this->subString($start, $length);
        if (!method_exists($string, 'stringReplace')) {
            // $string is a PEAR_Error, return it
            return $string;
        } else {
            $string = $string->stringReplace($find, $replace);
            $after  = $this->subString($start + $length);
           
            return new I18N_UnicodeString(array_merge($begin->_unicode, $string->_unicode, $after->_unicode), 'Unicode');
        }
    }
   
    /**
    * Works like PHP's str_replace function.
    *
    * @access public
    * @param  I18N_UnicodeString $find The string to replaced
    * @param  I18N_UnicodeString $replace The string to replace $find with
    * @return I18N_UnicodeString The current string with all $find replaced by
    *                            $replace
    * @see    subStringReplace()
    */
    function stringReplace(&$find, &$replace) {
        $return = new I18N_UnicodeString($this->_unicode, 'Unicode');
       
        while($return->strStr($find) !== false) {
            $after = $return->strStr($find);
            $begin = $return->subString(0, $return->length() - $after->length());
            $after = $after->subString($find->length());
           
            $return = new I18N_UnicodeString(array_merge($begin->_unicode, $replace->_unicode, $after->_unicode), 'Unicode');
        }
       
        return $return;
    }
   
    /**
    * Works like PHP's strstr function by returning the string from $find on.
    *
    * @access public
    * @param  I18N_UnicodeString $find The string to found
    * @return I18N_UnicodeString The current string from $find on to the end
    */
    function strStr(&$find) {
        $found = false;
        $after = $find->_unicode;
       
        for($i = 0, $length = $this->length(); $i < $length; $i++) {
            if ($found) {
                $after[] = $this->_unicode[$i];
            } else {
                if ($this->_unicode[$i] == $find->_unicode[0]) {
                    if ($i + $find->length() > $length) {
                        break;
                    }
                   
                    $found = true;
                    for($c = 1, $max = $find->length(); $c < $max; $c++) {
                        if ($this->_unicode[++$i] != $find->_unicode[$c]) {
                            $found = false;
                            break;
                        }
                    }
                }
            }
        }
       
        if ($found) {
            return new I18N_UnicodeString($after, 'Unicode');
        } else {
            return false;
        }
    }

    /**
    * Returns the position of a character much like PHP's strpos function.
    *
    * @access public
    * @param  mixed $char A Unicode char represented as either an integer or a
    *                     UTF-8 char.
    * @return integer The location of the character in the string.
    */
    function indexOf($char) {
        if (!is_int($char)) {
            if (strlen($char) > 1) {
                $char = array_shift(self::utf8ToUnicode($char));
            } else {
                $char = ord($char);
            }
        }
       
        for($i = 0, $length = $this->length(); $i < $length; $i++) {
            if ($this->_unicode[$i] == $char) {
                return $i;
            }
        }
       
        return -1;
    }
   
    /**
    * Returns the last position of a character much like PHP's strrpos function.
    *
    * @access public
    * @param  mixed $char A Unicode char represented as either an integer or a
    *                     UTF-8 char.
    * @return integer The last location of the character in the string.
    */
    function lastIndexOf($char) {
        if (!is_int($char)) {
            if (strlen($char) > 1) {
                $char = array_shift($this->utf8ToUnicode($char));
            } else {
                $char = ord($char);
            }
        }
       
        for($i = $this->length() - 1; $i >= 0; $i–) {
            if ($this->_unicode[$i] == $char) {
                return $i;
            }
        }
       
        return -1;   
    }
   
    /**
    * Determines if two Unicode strings are equal
    *
    * @access public
    * @param  I18N_UnicodeString $unicode The string to compare to.
    * @return boolean True if they are equal, false otherwise.
    */
    function equals(&$unicode) {
        if ($this->length() != $unicode->length()) {
            // if they arn't even the same length no need to even check
            return false;
        }
       
        return ($this->_unicode == $unicode->_unicode);
    }
   
    /**
    * Appends a given Unicode string to the end of the current one.
    *
    * @access public
    * @param  I18N_UnicodeString $unicode The string to append.
    * @return I18N_UnicodeString The new string created from the appension.
    */
    function append(&$unicode) {
        return new I18N_UnicodeString(array_merge($this->_unicode, $unicode->_unicode), 'Unicode');
    }
   
    /**
    * Used to raise a PEAR_Error.
    *
    * Hopefully this method is never called, but when it is it will include the
    * PEAR class and return a new PEAR_Error.
    *
    * @static
    * @access public
    * @param string $message The error message to raise.
    * @return PEAR_Error A PEAR error message.
    */
    function raiseError($message) {
        /*include_once('PEAR.php');*/
   
        //return PEAR::raiseError($message);
    }
}
?>

Should work with static function:

ISO9075Mapper::map("string goes here");

maqsood
Confirmed Champ
Confirmed Champ
Hi ddanninger,

Thanks for the reply.

Using your code I could get the encoded file name. while uploading into alfresco i want the file to be uploaded with its original name which is not happening at present. For example, if i upload a file "My Test.txt" It should get uploaded with the same name in alfresco, but what happening is:

1. Name gets encoded
     $name = ISO9075Mapper::map($_FILES['_asset']['name']);
     echo $name;  ==> My_x0020_Test.txt

2. File is uploaded with the encoded name. [which i don't want]

If i search for this file thru web service, I couldn't find it.

Hope i am clear with my problem, please suggest what went wrong in the encoding processs.

Thanks for your help.

ddanninger
Champ in-the-making
Champ in-the-making
Actually you shouldnt encode the name as iso 9705

just
 $name = "My test.txt";

upload this file then and search then with

$searchName = ISO9075Mapper::map($name);

then you should be able to find it.

maqsood
Confirmed Champ
Confirmed Champ
have already tried the way you said, its not working.

if there is any special character in file name, it won't be able to search for it. but it can search a file name separted with an space.

I didn't get why it could not search for special character. Any idea?