dolibarr /htdocs/comm/action/class/ical.class.php

Language PHP Lines 383
MD5 Hash d369c51391aefd799be02a818014536d
Repository https://github.com/asterix14/dolibarr.git View Raw File
  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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
<?php
/* Copyright (C) 2006      Roman Ozana		    <ozana@omdesign.cz>
 * Copyright (C) 2011 	   Juanjo Menent        <jmenent@2byte.es>
 * Copyright (C) 2011 	   Laurent Destailleur  <eldy@users.sourceforge.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

/**
 *       \file       htdocs/comm/action/class/ical.class.php
 *       \ingroup    commercial
 *       \brief      File of class to parse ical calendars
 */
class ical
{
    var $file_text; // Text in file
    var $cal; // Array to save iCalendar parse data
    var $event_count; // Number of Events
    var $todo_count; // Number of Todos
    var $last_key; //Help variable save last key (multiline string)


	/**
	 * Constructor
	 */
	public function ical()
	{
	}

	/**
     * Read text file, icalender text file
     *
     * @param string $file
     * @return string
     */
    function read_file($file)
    {
        $this->file = $file;
        $file_text = join ("", file ($file)); //load file
        $file_text = preg_replace("/[\r\n]{1,} ([:;])/","\\1",$file_text);

        return $file_text; // return all text
    }

    /**
     * Returns the number of calendar events
     *
     * @return int
     */
    function get_event_count()
    {
        return $this->event_count;
    }

    /**
     * Returns the number of ToDo
     *
     * @return int
     */
    function get_todo_count()
    {
        return $this->todo_count;
    }

    /**
     * Translate Calendar
     *
     * @param string $uri
     * @return array
     */
    function parse($uri)
    {
        $this->cal = array(); // new empty array

        $this->event_count = -1;

        // read FILE text
        $this->file_text = $this->read_file($uri);

        $this->file_text = preg_split("[\n]", $this->file_text);

        // is this text vcalendar standart text ? on line 1 is BEGIN:VCALENDAR
        if (!stristr($this->file_text[0],'BEGIN:VCALENDAR')) return 'error not VCALENDAR';

        $insidealarm=0;
        foreach ($this->file_text as $text)
        {
            $text = trim($text); // trim one line
            if (!empty($text))
            {
                // get Key and Value VCALENDAR:Begin -> Key = VCALENDAR, Value = begin
                list($key, $value) = $this->retun_key_value($text);

                switch ($text) // search special string
                {
                    case "BEGIN:VTODO":
                        $this->todo_count = $this->todo_count+1; // new todo begin
                        $type = "VTODO";
                        break;

                    case "BEGIN:VEVENT":
                        $this->event_count = $this->event_count+1; // new event begin
                        $type = "VEVENT";
                        break;

                    case "BEGIN:VFREEBUSY":
                        $this->freebusy_count = $this->freebusy_count+1; // new event begin
                        $type = "VFREEBUSY";
                        break;

                    case "BEGIN:VCALENDAR": // all other special string
                    case "BEGIN:DAYLIGHT":
                    case "BEGIN:VTIMEZONE":
                    case "BEGIN:STANDARD":
                        $type = $value; // save tu array under value key
                        break;

                    case "END:VTODO": // end special text - goto VCALENDAR key
                    case "END:VEVENT":
                    case "END:VFREEBUSY":

                    case "END:VCALENDAR":
                    case "END:DAYLIGHT":
                    case "END:VTIMEZONE":
                    case "END:STANDARD":
                        $type = "VCALENDAR";
                        break;

                    // Manage VALARM that are inside a VEVENT to avoid fields of VALARM to overwrites fields of VEVENT
                    case "BEGIN:VALARM":
                        $insidealarm=1;
                        break;
                    case "END:VALARM":
                        $insidealarm=0;
                        break;

                    default: // no special string
                        if (! $insidealarm) $this->add_to_array($type, $key, $value); // add to array
                        break;
                }
            }
        }
        return $this->cal;
    }

    /**
     * Add to $this->ical array one value and key. Type is VTODO, VEVENT, VFREEBUSY, VCALENDAR ... .
     *
     * @param string $type
     * @param string $key
     * @param string $value
     */
    function add_to_array($type, $key, $value)
    {

        //print 'type='.$type.' key='.$key.' value='.$value.'<br>'."\n";

        if ($key == false)
        {
            $key = $this->last_key;
            switch ($type)
            {
                case 'VEVENT': $value = $this->cal[$type][$this->event_count][$key].$value;break;
                case 'VFREEBUSY': $value = $this->cal[$type][$this->freebusy_count][$key].$value;break;
                case 'VTODO': $value = $this->cal[$type][$this->todo_count][$key].$value;break;
            }
        }

        if (($key == "DTSTAMP") or ($key == "LAST-MODIFIED") or ($key == "CREATED")) $value = $this->ical_date_to_unix($value);
        if ($key == "RRULE" ) $value = $this->ical_rrule($value);

        if (stristr($key,"DTSTART") or stristr($key,"DTEND") or stristr($key,"DTSTART;VALUE=DATE") or stristr($key,"DTEND;VALUE=DATE"))
        {
        	if (stristr($key,"DTSTART;VALUE=DATE") or stristr($key,"DTEND;VALUE=DATE"))
        	{
        		list($key,$value) = array($key,$value);
        	}
        	else
        	{
        		list($key,$value) = $this->ical_dt_date($key,$value);
        	}
        }

        switch ($type)
        {
            case "VTODO":
                $this->cal[$type][$this->todo_count][$key] = $value;
                break;

            case "VEVENT":
                $this->cal[$type][$this->event_count][$key] = $value;
                break;

            case "VFREEBUSY":
                $this->cal[$type][$this->freebusy_count][$key] = $value;
                break;

            default:
                $this->cal[$type][$key] = $value;
                break;
        }
        $this->last_key = $key;
    }

    /**
     * Parse text "XXXX:value text some with : " and return array($key = "XXXX", $value="value");
     *
     * @param string $text
     * @return array
     */
    function retun_key_value($text)
    {
        preg_match("/([^:]+)[:]([\w\W]+)/", $text, $matches);

        if (empty($matches))
        {
            return array(false,$text);
        }
        else
        {
            $matches = array_splice($matches, 1, 2);
            return $matches;
        }

    }

    /**
     * Parse RRULE  return array
     *
     * @param string $value
     * @return array
     */
    function ical_rrule($value)
    {
        $rrule = explode(';',$value);
        foreach ($rrule as $line)
        {
            $rcontent = explode('=', $line);
            $result[$rcontent[0]] = $rcontent[1];
        }
        return $result;
    }
    /**
     * Return Unix time from ical date time fomrat (YYYYMMDD[T]HHMMSS[Z] or YYYYMMDD[T]HHMMSS)
     *
     * @param unknown_type $ical_date
     * @return timestamp
     */
    function ical_date_to_unix($ical_date)
    {
        $ical_date = str_replace('T', '', $ical_date);
        $ical_date = str_replace('Z', '', $ical_date);

        $ntime=0;
        // TIME LIMITED EVENT
        if (preg_match('/([0-9]{4})([0-9]{2})([0-9]{2})([0-9]{0,2})([0-9]{0,2})([0-9]{0,2})/', $ical_date, $date))
            $ntime=dol_mktime($date[4], $date[5], $date[6], $date[2],$date[3], $date[1], true);

        //if (empty($date[4])) print 'Error bad date: '.$ical_date.' - date1='.$date[1];
        //print dol_print_date($ntime,'dayhour');exit;
        return $ntime;      // ntime is a GTM time
    }

    /**
     * Return unix date from iCal date format
     *
     * @param string $key
     * @param string $value
     * @return array
     */
    function ical_dt_date($key, $value)
    {
        $value = $this->ical_date_to_unix($value);

        // Analyse TZID
        $temp = explode(";",$key);

        if (empty($temp[1])) // not TZID
        {
            $value = str_replace('T', '', $value);
            return array($key,$value);
        }
        // adding $value and $tzid
        $key =     $temp[0];
        $temp = explode("=", $temp[1]);
        $return_value[$temp[0]] = $temp[1];
        $return_value['unixtime'] = $value;

        return array($key,$return_value);
    }

    /**
     * Return sorted eventlist as array or false if calenar is empty
     *
     * @return array
     */
    function get_sort_event_list()
    {
        $temp = $this->get_event_list();
        if (!empty($temp))
        {
            usort($temp, array(&$this, "ical_dtstart_compare"));
            return $temp;
        } else
        {
            return false;
        }
    }

    /**
     * Compare two unix timestamp
     *
     * @param array $a
     * @param array $b
     * @return integer
     */
    function ical_dtstart_compare($a, $b)
    {
        return strnatcasecmp($a['DTSTART']['unixtime'], $b['DTSTART']['unixtime']);
    }

    /**
     * Return eventlist array (not sort eventlist array)
     *
     * @return array
     */
    function get_event_list()
    {
        return $this->cal['VEVENT'];
    }

    /**
     * Return eventlist array (not sort eventlist array)
     *
     * @return array
     */
    function get_freebusy_list()
    {
        return $this->cal['VFREEBUSY'];
    }

    /**
     * Return todo array (not sort todo array)
     *
     * @return array
     */
    function get_todo_list()
    {
        return $this->cal['VTODO'];
    }

    /**
     * Return base calendar data
     *
     * @return array
     */
    function get_calender_data()
    {
        return $this->cal['VCALENDAR'];
    }

    /**
     * Return array with all data
     *
     * @return array
     */
    function get_all_data()
    {
        return $this->cal;
    }
}
?>
Back to Top