Location: PHPKode > scripts > IMC Objects > imc-objects/class.imcProperty.php
<?
/* vim: set expandtab tabstop=4 shiftwidth=4: */
// +----------------------------------------------------------------------+
// | PHP version 4                                                        |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group                                |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.0 of the PHP license,       |
// | that is bundled with this package in the file LICENSE, and is        |
// | available through the world-wide-web at                              |
// | http://www.php.net/license/2_02.txt.                                 |
// | If you did not receive a copy of the PHP license and are unable to   |
// | obtain it through the world-wide-web, please send a note to          |
// | hide@address.com so we can mail you a copy immediately.               |
// +----------------------------------------------------------------------+
// | Authors: Andreas Haberstroh <hide@address.com>                      |
// +----------------------------------------------------------------------+
//
// $Id$


function _quoted_printable_encode($string)
{ 
    $string = str_replace("\r", "=0D", $string);
    $string = str_replace("\n", "=0A", $string);
    $string = str_replace("\t", "=09", $string);
    return $string;
}


/**
*   imcProperty encapsulates the IMC's definition of a property for vCards and iCalendars.
*   In a data file, it is represented as:
*       PropertyName [‘;‘ PropertyParameters] ’:‘ PropertyValue
*   
*   Parameters contain type definitions, values and encoding mechanisms.
*   Some properties require special codecs for data. ie: the N/ADR property in vCards 
*   and RRULE property in iCalendars contain context sensitive data, which should be
*   parsed so the user interpret the data in a rational manner.
*/
class   imcProperty
{
    /**
     * Property TYPE parameters
     * @var array
     */
    var $paramType  = NULL;

    /**
     * Property VALUE parameters
     * @var array
     */
    var $paramValue = NULL;

    /**
     * Property ENCODING
     * @var string
     */
    var $paramEncoding = NULL;

    /**
     * Property Encoding function type.
     * We use this value to call the right codec on read/write to a data source.
     * @var string 
     */
    var $encoding_func ='none';

    /**
     * Property value.
     * @var mixed
     */
    var $value = NULL;

    
    function    imcProperty()
    {
    }

    // {{{ setParamType()
    /**
    *   Sets a list of parameter types for a property. The most common usage
    *   is vCard's ADR property. We can set the property type to be HOME.
    *
    *   @param  mixed   Can be an array of types or a single string, 
    *                   which is applicable to the property
    *   @see    getParamType
    */
    function    setParamType($type)
    {
        if( $type == '-1' )
            return;

        if( is_array($type) ) {
            // yes, we sort the array EVERY time, so searching can be
            // predictable
            sort($type);
        }
        $this->paramType = $type;
    }

    // {{{ getParamType()
    /**
    *   Returns the parameter types list for the property.
    *
    *   @return     array   Array of types
    *   @see        setParamType
    */
    function    getParamType()
    {
        return $this->paramType;
    }

    // {{{ setParamValue()
    /**
    *   Sets the parameter value for a property. This defines how the value
    *   should be interpreted. For instance, a PHOTO property's value can
    *   be a URI, in which case, the value needs to be interpreted as such.
    *   Value therefore hints how to interpret data
    *
    *   @param  string  Value definition for the property
    *   @see    getParamValue
    */
    function    setParamValue($value)
    {
        $this->paramValue = $value;
    }

    // {{{ getParamValue()
    /**
    *   Retrieves the value definition of the property.
    *
    *   @return string  Value definition for the property
    *   @see    setParamValue
    */
    function    getParamValue()
    {
        return $this->paramValue;
    }

    // {{{ setParamEncoding()
    /**
    *   Sets the property value's encoding method.
    *
    *   @param  string  Encoding method.
    *   @see    getParamEncoding
    */
    function    setParamEncoding($encoding)
    {
        // Now, set our internal codec's to the correct value!
        $this->_setEncodeType($encoding);
        $this->paramEncoding = $encoding;
    }

    // {{{ getParamEncoding()
    /**
    *   Gets the property value's encoding method.
    *
    *   @return  string  Encoding method.
    *   @see    setParamEncoding
    */
    function    getParamEncoding()
    {
        return $this->paramEncoding;
    }

    function    _setEncodeType($value)
    {
        switch( strtoupper($value) )
        {
        case 'QUOTED-PRINTABLE':
            $this->encoding_func = 'quoted_printable';
            break;
        case 'BASE64':
            $this->encoding_func = 'base64';
            break;
        case 'URL':
        case 'URI':
            $this->encoding_func = 'url';
            break;
        case 'ISO8601':
            $this->encoding_func = 'iso8601';
            break;
        default:
            $this->encoding_func = 'none';
        }
    }

    function    _getEncodeType()
    {
        return  $this->encoding_func;
    }


    function    _encode_none($value)
    {
        return $value;
    }
    function    _decode_none($value)
    {
        return $value;
    }

    function    _encode_base64($string)
    {
        return (string)base64_encode($string);
    }
    function    _decode_base64($string)
    {
        return (string)base64_decode($string);
    }

    function    _encode_quoted_printable($string)
    {
        return (string)_quoted_printable_encode($string);
    }
    function    _decode_quoted_printable($string)
    {
        return (string)quoted_printable_decode($string);
    }

    function    _encode_iso8601($integer)
    {
        return (string)iso8601_encode($integer,true);
    }
    function    _decode_iso8601($string)
    {
        return (integer)iso8601_decode($string,true);
    }

    function    _encode_url($string)
    {
        return (string)rawurlencode($string);
    }
    function    _decode_url($string)
    {
        return (string)rawurldecode($string);
    }

    function    _decodeValue($value)
    {
        $function = "_decode_".$this->encoding_func;

        return $this->$function($value);
    }

    function    _encodeValue($value)
    {
        $function = "_encode_".$this->encoding_func;

        return $this->$function($value);
    }

    function    setEncodedValue($value)
    {
        $this->setValue( $this->_decodeValue($value) );
    }

    function    getEncodedValue()
    {
        return $this->_encodeValue($this->value);
    }

    function    setValue($value)
    {
        $this->value = $value;
    }

    function    getValue()
    {
        return $this->value;
    }

}

// - Helper functions

/*
	Takes a ISO 8601 formated date and returns a unix time stamp.
*/

/**
* Takes a ISO 8601 formated string and returns a UNIX Timestamp.
*
* @param    string      $iso_date   String that should decoded
* @param    boolean     $utc        If the date should be returned as a UTC or local
* @return   unixtime                Returns a unix time stamp or -1 for invalid dates
*/
function	iso8601_decode($iso_date, $utc = false)
{
    $unix_time = -1;

    if( strstr($iso_date, 'T') )
    {
	    ereg ('([0-9]{4})([0-9]{2})([0-9]{2})T([0-9]{2})([0-9]{2})([0-9]{2})', $iso_date, $date_chunks);

        if( strstr($iso_date, 'Z') )
            $utc = true;

        if( $utc )
    		$unix_time = gmmktime( $date_chunks[4], $date_chunks[5], $date_chunks[6], $date_chunks[2], $date_chunks[3], $date_chunks[1] );
        else
            $unix_time = mktime( $date_chunks[4], $date_chunks[5], $date_chunks[6], $date_chunks[2], $date_chunks[3], $date_chunks[1] );
    }

	return $unix_time;

}


/**
* Takes a UNIX timestamp and encodes it into a ISO 8601 formated string
*
* @param unixtime   $unix_date  Unix timestamp that needs to be encoded
* @param boolean    $utc        If the timestamp is UTC or not
* @return string                ISO8601 string
*/
function	iso8601_encode($unix_date, $utc = false)
{
    if ($utc)
    {
		if (function_exists('gmstrftime'))
			$iso_string = gmstrftime("%Y%m%dT%H%M%SZ", $unix_date);
		else
			$iso_string = strftime("%Y%m%dT%H%M%SZ", $unix_date - date('Z'));
    }
    else
    {
		$iso_string = strftime("%Y%m%dT%H%M%S", $unix_date);
	}

	return $iso_string;
}


?>
Return current item: IMC Objects