ContentLine.php 8.27 KB
Newer Older
mose's avatar
mose committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
<?php

/*
    PHP iCal Interface Library
    Copyright (C) 2005  Gregory Szorc <gregory.szorc@case.edu>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2.1 of the License, or (at your option) any later version.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

*/
/**
 *  iCal (RFC 2245) class definition
 *
 *  PHP version 5
 *
 *  @package    iCal
 *  @subpackage Parser
 *  @author     Gregory Szorc <gregory.szorc@case.edu>
 *  @license    http://www.gnu.org/copyleft/lesser.html  LGPL License 2.1
 */

/**
 * File_iCal_ContentLine uses File_iCal_Property internally
 */
require_once('Property.php');

/**
 * File_iCal_ContentLine uses File_iCal_Parameter internally
 */
require_once('Parameter.php');

/**
 *  File_iCal_ContentLine is a helper class that stores information about one line in an iCal file
 *
 *  @category File
 *  @package iCal
 */
class File_iCal_ContentLine {
    /**
     * The contentline stores data in a File_iCal_Property
     *
     * The ContentLine and the Property have an interesting relationship.
     * A ContnetLine is principally a helper class.  It serves as an interface
     * to create properties transparently.
     *
     * @access protected
     * @var File_iCal_Property  Property representation of this contentline
     */
    protected $_property;


    /**
     * Create a new ContentLine
     *
     * @access  public
     * @param   mixed   $line   The parameter can be of a few types:
     *                          1) a File_iCal_Property object
     *                          2) a line of text in an iCal file
     */
    public function __construct($line)
    {
        //if we ar econstructing a contentline out of an existing property
        if (is_a($line, "File_iCal_Property")) {
            $this->_property = $line;
        }
        else {

            //remove all special characters from beginning and end
            $line = trim($line);

            //now it is time again to rewrite the tokenizer (for the 4th time)

            $tokens = array();  //array of strings which represent tokens

            $i = 0; //string position
            $l = strlen($line);

            //every entry will start looking for a new token
            do {
                $t = "";    //our new token
                $begin = $i;

                //behave differently depending on what type of character we encounter
                switch ($line{$i}) {
                    case '\"';  //encounter the beginning of a double quoted string
                        $valid = true;

                        while ($valid) {
                            $i = strpos($line, '\"', $i);
                            if ($line{$i-1} == '\\') $valid = false;
                        }

                        $t = substr($line, $begin, $i - $begin);
                        $i++;
                        break;

                    case ':';
                    case ';';
                        $t = $line{$i};
                        $i++;
                        break;


                    default:
                        //if we don't have a semicolon or colon in the tokens array yet, we are reading the name string
                        $present_colon = in_array(':', $tokens);
                        $present_semi = in_array(';', $tokens);


                        if (!$present_colon && !$present_semi) {
                            $m;
                            preg_match("/^([\\w\\d-]+)[;:]?/", $line, $m);
                            //$m[1] contains the name
                            $t = $m[1];
                            $i += strlen($t);

                        }
                        else if ($present_colon && $tokens[count($tokens) -1] == ':') {
                            //if the last token is a colon, read out the value
                            $t = substr($line, $i);
                            $i += strlen($t);

                        }
                        else if ($present_semi && $tokens[count($tokens) -1] == ';') {
                            //if the last token is a semicolon
                            $t = substr($line, $i, strpos($line, ':', $i) - $i);
                            $i += strlen($t);

                        }
                        else {
                            $t = $line{$i};
                            $i++;
                        }

                }

                $tokens[] = $t;
            } while ($i < $l);

            //print_r($tokens);

            $colon_key = array_search(':', $tokens);

            $name_string = $tokens[0];
            $value_string = $tokens[$colon_key+1];

            $params = array();

            if ($tokens[1] == ';') {
                $arr = explode(';', $tokens[2]);

                foreach ($arr as $v) {
                    $val = explode('=', $v);

                    $name = $val[0];
                    $values = array($val[1]);

                    $params[] = File_iCal_Parameter::getParameter($name, $values);

                }
            }

            $this->_property = File_iCal_Property::getProperty($name_string, $params, $value_string);

        }

    }


    /**
     * Get the name associate with this ContentLine
     *
     * @access  public
     * @deprecated
     * @return  string  the string value of the property name
     */
    public function name()
    {
            if (is_a($this->_property, "File_iCal_Property")) {
                return $this->_property->getName();
            }
    }

    /**
     * Gets the parameters from this ContentLine
     *
     * @access  public
     * @return  array   An array of File_iCal_Parameter
     */
    public function params()
    {
            return $this->_property->getParams();
    }

    /**
     * @deprecated
     */
    public function value()
    {
            return $this->_property->getValue();
    }


    /**
     * Returns the property this contentline represents
     *
     * @access  public
     * @return  File_iCal_Property
     */
    public function getProperty() {
        return $this->_property;
    }

    /**
     * returns a contentLine formatted for an iCal file
     *
     * The function will return a property as it is to be formatted for an iCal file
     *
     * @access  public
     * @return  string
     * @param   bool    Whether to wrap long lines
     */
    public function getLine($wrap = true)
    {
        $s = $this->_property->getName();

        if ($params = $this->_property->getParams()) {
            foreach ($params as $p) {
                $s .= ';'.$p->getName().'='.$p->getValuesString();
            }
        }

        $s .= ":";

        //echo "$s\n";

        $values = array();
        foreach ($this->_property->getValues() as $v) {
            //$values[] = self::encodeString($v->getValue());
            $values[] = $v->getValue();
        }

        $s .= implode($this->_property->getValueSeparator(), $values);

        //$s .= "\r\n";
        return $s;
    }

    /**
     * Returns an string encoded for presense on a contentline
     *
     * Some characters in the iCal file format are special and need to be delimited.  This function
     * will handle the delimination
     *
     * @access  private
     * @return  string  Encoded string
     * @param   string  String to encode
     */
    private function encodeString($s) {
        return str_replace(array("\\", ",", "\"", "'", ";", ":"), array("\\\\", "\\,", "\\\"", "\\\'", "\\;", "\\:"), $s);
    }

    /**
     * Returns a string that has been decoded
     *
     * @access  private
     * @return  string  Decoded string suitable for storage in data classes
     * @param   string  String to decode
     */
    private function decodeString($s) {
        return str_replace(array("\\\\", "\\,", "\\\"", "\\\'", "\\;", "\\:"), array("\\", ",", "\"", "'", ";", ":"), $s);
    }
}