~ubuntu-branches/ubuntu/saucy/horde3/saucy

« back to all changes in this revision

Viewing changes to lib/Horde/Data/pdb.php

  • Committer: Bazaar Package Importer
  • Author(s): Ola Lundqvist
  • Date: 2005-05-04 23:08:08 UTC
  • Revision ID: james.westby@ubuntu.com-20050504230808-p4hf3hk28o3v7wir
Tags: upstream-3.0.4
ImportĀ upstreamĀ versionĀ 3.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
 
 
3
/** We rely on the Data_palm:: abstract class. */
 
4
require_once dirname(__FILE__) . '/palm.php';
 
5
 
 
6
/**
 
7
 * Class to allow data exchange between the Horde applications and
 
8
 * palm pdb datebok file.
 
9
 *
 
10
 * $Horde: framework/Data/Data/pdb.php,v 1.7 2004/02/15 03:50:06 chuck Exp $
 
11
 *
 
12
 * TODO: export method
 
13
 *
 
14
 * @author  Mathieu Clabaut <mathieu.clabaut@free.fr>
 
15
 * @package Horde_Data
 
16
 */
 
17
class Data_pdb extends Data_palm {
 
18
 
 
19
    var $_extension = 'pdb';
 
20
    var $_pdb;
 
21
 
 
22
    function importFile($filename)
 
23
    {
 
24
        $this->_pdb = &new PalmDatebook();
 
25
        $fp = fopen($filename, 'r');
 
26
        if ($fp) {
 
27
            $this->_pdb->ReadFile($fp);
 
28
            fclose($fp);
 
29
            return $this->import();
 
30
        } else {
 
31
            return false;
 
32
        }
 
33
    }
 
34
 
 
35
    function importData()
 
36
    {
 
37
        $nbrec = $this->_pdb->GetRecordCount();
 
38
        $data = array();
 
39
 
 
40
        foreach ($this->_pdb->GetRecordIDs() as $id) {
 
41
            $row = array();
 
42
            $record = $this->_pdb->GetRecordRaw($id);
 
43
            $row['title'] = $record['Description'];
 
44
            $row['category'] = 'Palm';
 
45
            if (!empty($record['Note'])) {
 
46
                $row['description'] = $record['Note'];
 
47
            }
 
48
            $row['start_date'] = $record['Date'];
 
49
            $row['end_date'] = $row['start_date'];
 
50
            if (!empty($record['StartTime'])) {
 
51
                $row['start_time'] = $record['StartTime'] . ":00";
 
52
                if (!empty($record['EndTime'])) {
 
53
                    $row['end_time'] = $record['EndTime'] . ":00" ;
 
54
                }
 
55
            } else {
 
56
                $row['start_time'] = "00:00:00";
 
57
                $row['end_time'] = "00:00:00";
 
58
                $date = explode('-', $row['start_date']);
 
59
                $date[2] = 1 + $date[2];
 
60
                $row['end_date'] = implode('-', $date);
 
61
            }
 
62
            if (!empty($record['Alarm']) &&
 
63
                preg_match('/(\d+)([dmh])/', $record['Alarm'], $matches)) {
 
64
                switch ($matches[2]) {
 
65
                case 'm':
 
66
                    $row['alarm'] = $matches[1];
 
67
                    break;
 
68
 
 
69
                case 'h':
 
70
                    $row['alarm'] = $matches[1]*60;
 
71
                    break;
 
72
 
 
73
                case 'd':
 
74
                    $row['alarm'] = $matches[1]*60*24;
 
75
                    break;
 
76
                }
 
77
            }
 
78
 
 
79
            if (!empty($record['Repeat'])) {
 
80
                switch ($record['Repeat']['Type']) {
 
81
                case PDB_DATEBOOK_REPEAT_NONE:
 
82
                    $row['recur_type'] = KRONOLITH_RECUR_NONE;
 
83
                    break;
 
84
 
 
85
                case PDB_DATEBOOK_REPEAT_DAILY:
 
86
                    $row['recur_type'] = KRONOLITH_RECUR_DAILY;
 
87
                    if (!empty($record['Repeat']['Date'])) {
 
88
                        $row['recur_end_date'] = $record['Repeat']['Date'];
 
89
                    }
 
90
                    $row['recur_interval'] = $record['Repeat']['Frequency'];
 
91
                    break;
 
92
 
 
93
                case PDB_DATEBOOK_REPEAT_WEEKLY:
 
94
                    $row['recur_type'] = KRONOLITH_RECUR_WEEKLY;
 
95
                    if (!empty($record['Repeat']['Date'])) {
 
96
                        $row['recur_end_date'] = $record['Repeat']['Date'];
 
97
                    }
 
98
                    $row['recur_interval'] = $record['Repeat']['Frequency'];
 
99
                    if (!empty($record['Repeat']['Days'])) {
 
100
                        $days=0;
 
101
                        for ($c = 0; $c < strlen($record['Repeat']['Days']); $c++) {
 
102
                            $days = $days | (int)pow(2, (int)$record['Repeat']['Days']{$c});
 
103
                        }
 
104
                        $row['recur_data'] = $days;
 
105
                    }
 
106
                    break;
 
107
 
 
108
                case PDB_DATEBOOK_REPEAT_MONTH_BY_DAY:
 
109
                    // TODO : to be completed with
 
110
                    // $record['Repeat']['WeekNum'] and
 
111
                    // $record['Repeat']['DayNum'].
 
112
                    $row['recur_type'] = KRONOLITH_RECUR_DAY_OF_MONTH;
 
113
                    if (!empty($record['Repeat']['Date'])) {
 
114
                        $row['recur_end_date'] = $record['Repeat']['Date'];
 
115
                    }
 
116
                    $row['recur_interval'] = $record['Repeat']['Frequency'];
 
117
                    break;
 
118
 
 
119
                case PDB_DATEBOOK_REPEAT_MONTH_BY_DATE:
 
120
                    // TODO, verify the compliance with lib/Kronolith.php
 
121
                    $row['recur_type'] = KRONOLITH_RECUR_WEEK_OF_MONTH;
 
122
                    if (!empty($record['Repeat']['Date'])) {
 
123
                        $row['recur_end_date'] = $record['Repeat']['Date'];
 
124
                    }
 
125
                    $row['recur_interval'] = $record['Repeat']['Frequency'];
 
126
                    break;
 
127
 
 
128
                case PDB_DATEBOOK_REPEAT_YEARLY:
 
129
                    $row['recur_type'] = KRONOLITH_RECUR_YEARLY;
 
130
                    if (!empty($record['Repeat']['Date'])) {
 
131
                        $row['recur_end_date'] = $record['Repeat']['Date'];
 
132
                    }
 
133
                    $row['recur_interval'] = $record['Repeat']['Frequency'];
 
134
                    break;
 
135
                }
 
136
            }
 
137
            $data[] = $row;
 
138
        }
 
139
 
 
140
        return $data;
 
141
    }
 
142
 
 
143
}
 
144
 
 
145
/**
 
146
 * Class extender for PalmOS Datebook files
 
147
 *
 
148
 * Copyright (C) 2001 - PHP-PDB development team
 
149
 * Licensed under the GNU LGPL
 
150
 * See the doc/LEGAL file for more information
 
151
 * See http://php-pdb.sourceforge.net/ for more information about the library
 
152
 */
 
153
 
 
154
// Repeat types
 
155
define('PDB_DATEBOOK_REPEAT_NONE', 0);
 
156
define('PDB_DATEBOOK_REPEAT_DAILY', 1);
 
157
define('PDB_DATEBOOK_REPEAT_WEEKLY', 2);
 
158
define('PDB_DATEBOOK_REPEAT_MONTH_BY_DAY', 3);
 
159
define('PDB_DATEBOOK_REPEAT_MONTH_BY_DATE', 4);
 
160
define('PDB_DATEBOOK_REPEAT_YEARLY', 5);
 
161
 
 
162
 
 
163
// Record flags
 
164
define('PDB_DATEBOOK_FLAG_DESCRIPTION', 1024); // Record has description
 
165
                                            // (mandatory, as far as I know)
 
166
define('PDB_DATEBOOK_FLAG_EXCEPTIONS', 2048); // Are there any exceptions?
 
167
define('PDB_DATEBOOK_FLAG_NOTE', 4096); // Is there an associated note?
 
168
define('PDB_DATEBOOK_FLAG_REPEAT', 8192); // Does the event repeat?
 
169
define('PDB_DATEBOOK_FLAG_ALARM', 16384); // Is there an alarm set?
 
170
define('PDB_DATEBOOK_FLAG_WHEN', 32768); // Was the 'when' updated?
 
171
                                         // (Internal use only?)
 
172
 
 
173
 
 
174
/* The data for SetRecordRaw and from GetRecordRaw should be/return a
 
175
 * special array, detailed below.  Optional values can be set to '' or not
 
176
 * defined.  If they are anything else (including zero), they are considered
 
177
 * to be 'set'.  Optional values are marked with a ^.
 
178
 *
 
179
 * Key           Example          Description
 
180
 * ------------------------------------------
 
181
 * StartTime     2:00             Starting time of event, 24 hour format
 
182
 * EndTime       13:00            Ending time of event, 24 hour format
 
183
 * Date          2001-01-23       Year-Month-Day of event
 
184
 * Description   Title            This is the title of the event
 
185
 * Alarm         5d               ^ Number of units (m=min, h=hours, d=days)
 
186
 * Repeat        ???              ^ Repeating event data (array)
 
187
 * Note          NoteNote         ^ A note for the event
 
188
 * Exceptions    ???              ^ Exceptions to the event
 
189
 * WhenChanged   ???              ^ True if "when info" for event has changed
 
190
 * Flags         3                ^ User flags (highest bit allowed is 512)
 
191
 *
 
192
 * EndTime must happen at or after StartTime.  The time the event occurs
 
193
 * may not pass midnight (0:00).  If the event doesn't have a time, use ''
 
194
 * or do not define StartTime and EndTime.
 
195
 *
 
196
 * Repeating events:
 
197
 *
 
198
 *    No repeat (or leave the array undefined):
 
199
 *       $repeat['Type'] = PDB_DATEBOOK_REPEAT_NONE;
 
200
 *
 
201
 *    Daily repeating events:
 
202
 *       $repeat['Type'] = PDB_DATEBOOK_REPEAT_DAILY;
 
203
 *       $repeat['Frequency'] = FREQ;  // Explained below
 
204
 *       $repeat['End'] = END_DATE;  // Explained below
 
205
 *
 
206
 *    Weekly repeating events:
 
207
 *       $repeat['Type'] = PDB_DATEBOOK_REPEAT_WEEKLY;
 
208
 *       $repeat['Frequency'] = FREQ;  // Explained below
 
209
 *       $repeat['Days'] = DAYS; // Explained below
 
210
 *       $repeat['End'] = END_DATE;  // Explained below
 
211
 *       $repeat['StartOfWeek'] = SOW; // Explained below
 
212
 *
 
213
 *    "Monthly by day" repeating events:
 
214
 *       $repeat['Type'] = PDB_DATEBOOK_REPEAT_MONTH_BY_DAY;
 
215
 *       $repeat['WeekNum'] = WEEKNUM;  // Explained below
 
216
 *       $repeat['DayNum'] = DAYNUM;  // Explained below
 
217
 *       $repeat['Frequency'] = FREQ;  // Explained below
 
218
 *       $repeat['End'] = END_DATE;  // Explained below
 
219
 *
 
220
 *    "Monthly by date" repeating events:
 
221
 *       $repeat['Type'] = PDB_DATEBOOK_REPEAT_MONTH_BY_DATE;
 
222
 *       $repeat['Frequency'] = FREQ;  // Explained below
 
223
 *       $repeat['End'] = END_DATE;  // Explained below
 
224
 *
 
225
 *    Yearly repeating events:
 
226
 *       $repeat['Type'] = PDB_DATEBOOK_REPEAT_YEARLY;
 
227
 *       $repeat['Frequency'] = FREQ;  // Explained below
 
228
 *       $repeat['End'] = END_DATE;  // Explained below
 
229
 *
 
230
 *    There is also two mysterious 'unknown' fields for the repeat event that
 
231
 *    will be populated if the database is loaded from a file.  They will
 
232
 *    otherwise default to 0.  They are 'unknown1' and 'unknown2'.
 
233
 *
 
234
 *    FREQ = Frequency of the event.  If it is a daily event and you want it
 
235
 *           to happen every other day, set Frequency to 2.  This will default
 
236
 *           to 1 if not specified.
 
237
 *    END_DATE = The last day, month, and year on which the event occurs.
 
238
 *               Format is YYYY-MM-DD.  If not specified, no end date will
 
239
 *               be set.
 
240
 *    DAYS = What days during the week the event occurs.  This is a string of
 
241
 *           numbers from 0 - 6.  I'm not sure if 0 = Sunday or if 0 =
 
242
 *           start of week from the preferences.
 
243
 *    SOW = As quoted from P5-Palm:  "I'm not sure what this is, but the
 
244
 *          Datebook app appears to perform some hairy calculations
 
245
 *          involving this."
 
246
 *    WEEKNUM = The number of the week on which the event occurs.  5 is the
 
247
 *              last week of the month.
 
248
 *    DAYNUM = The day of the week on which the event occurs.  Again, I don't
 
249
 *             know if 0 = Sunday or if 0 = start of week from the prefs.
 
250
 *
 
251
 * Exceptions are specified in an array consisting of dates the event occured
 
252
 * and did not happen or should not be shown.  Dates are in the format
 
253
 * YYYY-MM-DD
 
254
 *
 
255
 * @package Horde_Data
 
256
 */
 
257
class PalmDatebook extends PalmDB {
 
258
   var $FirstDay;
 
259
 
 
260
 
 
261
   // Constructor -- initialize the default values
 
262
   function PalmDatebook () {
 
263
      PalmDB::PalmDB('DATA', 'date', 'DatebookDB');
 
264
      $this->FirstDay = 0;
 
265
   }
 
266
 
 
267
 
 
268
   // Returns an array with default data for a new record.
 
269
   // This doesn't actually add the record.
 
270
   function NewRecord() {
 
271
      // Default event is untimed
 
272
      // Event's date is today
 
273
      $Event['Date'] = date("Y-m-d");
 
274
 
 
275
      // Set an alarm 10 min before the event
 
276
      $Event['Alarm'] = '10m';
 
277
 
 
278
      return $Event;
 
279
   }
 
280
 
 
281
 
 
282
   // Converts a date string ( YYYY-MM-DD )( "2001-10-31" )
 
283
   // into bitwise ( YYYY YYYM MMMD DDDD )
 
284
   // Should only be used when saving
 
285
   function DateToInt16($date) {
 
286
      $YMD = explode('-', $date);
 
287
      return ($YMD[0] - 1904) * 512 + $YMD[1] * 32 + $YMD[2];
 
288
   }
 
289
 
 
290
 
 
291
   // Converts a bitwise date ( YYYY YYYM MMMD DDDD )
 
292
   // Into the human readable date string ( YYYY-MM-DD )( "2001-10-31" )
 
293
   // Should only be used when loading
 
294
   function Int16ToDate($number) {
 
295
      $year = $number / 512;
 
296
      settype($year, "integer");
 
297
      $year += 1904;
 
298
      $number = $number % 512;
 
299
      $month = $number / 32;
 
300
      settype($month, "integer");
 
301
      $day = $number % 32;
 
302
      return $year . '-' . $month . '-' . $day;
 
303
   }
 
304
 
 
305
 
 
306
   // Prepares the record flags for the specified record;
 
307
   // Should only be used when saving
 
308
   function GetRecordFlags(& $data) {
 
309
      if (! isset($data['Flags']))
 
310
         $data['Flags'] = 0;
 
311
      $Flags = $data['Flags'] % 1024;
 
312
      if (isset($data['Description']) && $data['Description'] != '')
 
313
         $Flags += PDB_DATEBOOK_FLAG_DESCRIPTION;
 
314
      if (isset($data['Exceptions']) && is_array($data['Exceptions']) &&
 
315
          count($data['Exceptions']) > 0)
 
316
     $Flags += PDB_DATEBOOK_FLAG_EXCEPTIONS;
 
317
      if (isset($data['Note']) && $data['Note'] != '')
 
318
         $Flags += PDB_DATEBOOK_FLAG_NOTE;
 
319
      if (isset($data['Repeat']) && is_array($data['Repeat']) &&
 
320
          count($data['Repeat']) > 0)
 
321
     $Flags += PDB_DATEBOOK_FLAG_REPEAT;
 
322
      if (isset($data['Alarm']) && $data['Alarm'] != '' &&
 
323
          preg_match('/^([0-9]+)([mMhHdD])$/', $data['Alarm'], $AlarmMatch))
 
324
         $Flags += PDB_DATEBOOK_FLAG_ALARM;
 
325
      if (isset($data['WhenChanged']) && $data['WhenChanged'] != '' &&
 
326
          $data['WhenChanged'])
 
327
     $Flags += PDB_DATEBOOK_FLAG_WHEN;
 
328
 
 
329
      $data['Flags'] = $Flags;
 
330
   }
 
331
 
 
332
 
 
333
   // Overrides the GetRecordSize method.
 
334
   // Probably should only be used when saving
 
335
   function GetRecordSize($num = false) {
 
336
      if ($num === false)
 
337
         $num = $this->CurrentRecord;
 
338
 
 
339
      if (! isset($this->Records[$num]) || ! is_array($this->Records[$num]))
 
340
         return PalmDB::GetRecordSize($num);
 
341
 
 
342
      $data = $this->Records[$num];
 
343
 
 
344
      // Start Time and End Time (4)
 
345
      // The day of the event (2)
 
346
      // Flags (2)
 
347
      $Bytes = 8;
 
348
      $this->GetRecordFlags($data);
 
349
 
 
350
      if ($data['Flags'] & PDB_DATEBOOK_FLAG_ALARM)
 
351
         $Bytes += 2;
 
352
 
 
353
      if ($data['Flags'] & PDB_DATEBOOK_FLAG_REPEAT)
 
354
     $Bytes += 8;
 
355
 
 
356
      if ($data['Flags'] & PDB_DATEBOOK_FLAG_EXCEPTIONS)
 
357
         $Bytes += 2 + count($data['Exceptions']) * 2;
 
358
 
 
359
      if ($data['Flags'] & PDB_DATEBOOK_FLAG_DESCRIPTION)
 
360
         $Bytes += strlen($data['Description']) + 1;
 
361
 
 
362
      if ($data['Flags'] & PDB_DATEBOOK_FLAG_NOTE)
 
363
         $Bytes += strlen($data['Note']) + 1;
 
364
 
 
365
      return $Bytes;
 
366
   }
 
367
 
 
368
 
 
369
   // Overrides the GetRecord method.  We store data in associative arrays.
 
370
   // Just convert the data into the proper format and then return the
 
371
   // generated string.
 
372
   function GetRecord($num = false) {
 
373
      if ($num === false)
 
374
         $num = $this->CurrentRecord;
 
375
 
 
376
      if (! isset($this->Records[$num]) || ! is_array($this->Records[$num]))
 
377
         return PalmDB::GetRecord($num);
 
378
 
 
379
      $data = $this->Records[$num];
 
380
      $RecordString = '';
 
381
 
 
382
 
 
383
      // Start Time and End Time
 
384
      // 4 bytes
 
385
      // 0xFFFFFFFF if the event has no time
 
386
      if (! isset($data['StartTime']) || ! isset($data['EndTime']) ||
 
387
          strpos($data['StartTime'], ':') === false ||
 
388
      strpos($data['EndTime'], ':') === false) {
 
389
     $RecordString .= $this->Int16(65535);
 
390
     $RecordString .= $this->Int16(65535);
 
391
      } else {
 
392
         list($StartH, $StartM) = explode(':', $data['StartTime']);
 
393
         list($EndH, $EndM) = explode(':', $data['EndTime']);
 
394
     if ($StartH < 0 || $StartH > 23 || $StartM < 0 || $StartM > 59 ||
 
395
         $EndH < 0 || $EndH > 23 || $EndM < 0 || $EndM > 59) {
 
396
        $RecordString .= $this->Int16(65535);
 
397
        $RecordString .= $this->Int16(65535);
 
398
     } else {
 
399
        if ($EndH < $StartH || ($EndH == $StartH && $EndM < $StartM)) {
 
400
           $EndM = $StartM;
 
401
           if ($StartH < 23)
 
402
              $EndH = $StartH + 1;
 
403
           else
 
404
              $EndH = $StartH;
 
405
        }
 
406
        $RecordString .= $this->Int8($StartH);
 
407
        $RecordString .= $this->Int8($StartM);
 
408
        $RecordString .= $this->Int8($EndH);
 
409
        $RecordString .= $this->Int8($EndM);
 
410
     }
 
411
      }
 
412
 
 
413
      // The day of the event
 
414
      // For repeating events, this is the first day the event occurs
 
415
      $RecordString .= $this->Int16($this->DateToInt16($data['Date']));
 
416
 
 
417
      // Flags
 
418
      $this->GetRecordFlags($data);
 
419
      $Flags = $data['Flags'];
 
420
      $RecordString .= $this->Int16($Flags);
 
421
 
 
422
      if ($Flags & PDB_DATEBOOK_FLAG_ALARM &&
 
423
          preg_match('/^([0-9]+)([mMhHdD])$/', $data['Alarm'], $AlarmMatch)) {
 
424
         $RecordString .= $this->Int8($AlarmMatch[1]);
 
425
     $AlarmMatch[2] = strtolower($AlarmMatch[2]);
 
426
     if ($AlarmMatch[2] == 'm')
 
427
        $RecordString .= $this->Int8(0);
 
428
     elseif ($AlarmMatch[2] == 'h')
 
429
        $RecordString .= $this->Int8(1);
 
430
     else
 
431
        $RecordString .= $this->Int8(2);
 
432
      }
 
433
 
 
434
      if ($Flags & PDB_DATEBOOK_FLAG_REPEAT) {
 
435
         $d = $data['Repeat'];
 
436
 
 
437
         $RecordString .= $this->Int8($d['Type']);
 
438
 
 
439
     if (! isset($d['unknown1']))
 
440
        $d['unknown1'] = 0;
 
441
     $RecordString .= $this->Int8($d['unknown1']);
 
442
 
 
443
     if (isset($d['End']))
 
444
        $RecordString .= $this->Int16($this->DateToInt16($d['End']));
 
445
     else
 
446
        $RecordString .= $this->Int16(65535);
 
447
 
 
448
     if (! isset($d['Frequency']))
 
449
        $d['Frequency'] = 1;
 
450
     $RecordString .= $this->Int8($d['Frequency']);
 
451
 
 
452
     if ($d['Type'] == PDB_DATEBOOK_REPEAT_WEEKLY) {
 
453
        $days = $d['Days'];
 
454
        $flags = 0;
 
455
        $QuickLookup = array(1, 2, 4, 8, 16, 32, 64);
 
456
        $i = 0;
 
457
        while ($i < strlen($days)) {
 
458
           $num = $days[$i];
 
459
           settype($num, 'integer');
 
460
           if (isset($QuickLookup[$num]))
 
461
              $flags = $flags | $QuickLookup[$num];
 
462
           $i ++;
 
463
        }
 
464
        $RecordString .= $this->Int8($flags);
 
465
        if (isset($d['StartOfWeek']) && $d['StartOfWeek'] != '')
 
466
           $RecordString .= $this->Int8($d['StartOfWeek']);
 
467
        else
 
468
           $RecordString .= $this->Int8(0);
 
469
     } elseif ($d['Type'] == PDB_DATEBOOK_REPEAT_MONTH_BY_DAY) {
 
470
        if ($d['WeekNum'] > 5)
 
471
           $d['WeekNum'] = 5;
 
472
        $RecordString .= $this->Int8($d['WeekNum'] * 7 + $d['DayNum']);
 
473
        $RecordString .= $this->Int8(0);
 
474
     } else {
 
475
        $RecordString .= $this->Int16(0);
 
476
     }
 
477
     if (! isset($d['unknown2']))
 
478
        $d['unknown2'] = 0;
 
479
     $RecordString .= $this->Int8($d['unknown2']);
 
480
      }
 
481
 
 
482
      if ($Flags & PDB_DATEBOOK_FLAG_EXCEPTIONS) {
 
483
         $d = $data['Exceptions'];
 
484
         $RecordString .= $this->Int16(count($d));
 
485
     foreach ($d as $exception) {
 
486
        $RecordString .= $this->Int16($this->DateToInt16($exception));
 
487
     }
 
488
      }
 
489
 
 
490
      if ($Flags & PDB_DATEBOOK_FLAG_DESCRIPTION) {
 
491
         $RecordString .= $this->String($data['Description']);
 
492
         $RecordString .= $this->Int8(0);
 
493
      }
 
494
 
 
495
      if ($Flags & PDB_DATEBOOK_FLAG_NOTE) {
 
496
         $RecordString .= $this->String($data['Note']);
 
497
     $RecordString .= $this->Int8(0);
 
498
      }
 
499
 
 
500
      return $RecordString;
 
501
   }
 
502
 
 
503
 
 
504
   // Returns the size of the AppInfo block.  It is the size of the
 
505
   // category list plus four bytes.
 
506
   function GetAppInfoSize() {
 
507
      return PDB_CATEGORY_SIZE + 4;
 
508
   }
 
509
 
 
510
 
 
511
   // Returns the AppInfo block.  It is composed of the category list (which
 
512
   // doesn't seem to be used and is just filled with NULL bytes) and four
 
513
   // bytes that specify the first day of the week.  Not sure what that
 
514
   // value is supposed to be, so I just use zero.
 
515
   function GetAppInfo() {
 
516
      // Category list (Nulls)
 
517
      $this->AppInfo = $this->PadString('', PDB_CATEGORY_SIZE);
 
518
 
 
519
      // Unknown thing (first_day_in_week)
 
520
      // 00 00 FD 00 == where FD is the first day in week.
 
521
      // I'm using 0 as the default value since I don't know what it should be
 
522
      $this->AppInfo .= $this->Int16(0);
 
523
      $this->AppInfo .= $this->Int8($this->FirstDay);
 
524
      $this->AppInfo .= $this->Int8(0);
 
525
 
 
526
      return $this->AppInfo;
 
527
   }
 
528
 
 
529
 
 
530
   // Parses $fileData for the information we need when loading a datebook
 
531
   // file
 
532
   function LoadAppInfo($fileData) {
 
533
      $fileData = substr($fileData, PDB_CATEGORY_SIZE + 2);
 
534
      if (strlen($fileData < 1))
 
535
         return;
 
536
      $this->FirstDay = $this->LoadInt8($fileData);
 
537
   }
 
538
 
 
539
 
 
540
   // Converts the datebook record data loaded from a file into the internal
 
541
   // storage method that is used for the rest of the class and for ease of
 
542
   // use.
 
543
   // Return false to signal no error
 
544
   function LoadRecord($fileData, $RecordInfo) {
 
545
      $this->RecordAttrs[$RecordInfo['UID']] = $RecordInfo['Attrs'];
 
546
 
 
547
      $NewRec = $this->NewRecord();
 
548
      $StartH = $this->LoadInt8(substr($fileData, 0, 1));
 
549
      $StartM = $this->LoadInt8(substr($fileData, 1, 1));
 
550
      $EndH = $this->LoadInt8(substr($fileData, 2, 1));
 
551
      $EndM = $this->LoadInt8(substr($fileData, 3, 1));
 
552
      if ($StartH != 255 && $StartM != 255) {
 
553
         $NewRec['StartTime'] = $StartH . ':';
 
554
     if ($StartM < 10)
 
555
        $NewRec['StartTime'] .= '0';
 
556
     $NewRec['StartTime'] .= $StartM;
 
557
      }
 
558
      if ($EndH != 255 && $EndM != 255) {
 
559
         $NewRec['EndTime'] = $EndH . ':';
 
560
     if ($EndM < 10)
 
561
        $NewRec['EndTime'] .= '0';
 
562
     $NewRec['EndTime'] .= $EndM;
 
563
      }
 
564
      $NewRec['Date'] = $this->LoadInt16(substr($fileData, 4, 2));
 
565
      $NewRec['Date'] = $this->Int16ToDate($NewRec['Date']);
 
566
      $Flags = $this->LoadInt16(substr($fileData, 6, 2));
 
567
      $NewRec['Flags'] = $Flags;
 
568
      $fileData = substr($fileData, 8);
 
569
 
 
570
      if ($Flags & PDB_DATEBOOK_FLAG_WHEN)
 
571
         $NewRec['WhenChanged'] = true;
 
572
 
 
573
      if ($Flags & PDB_DATEBOOK_FLAG_ALARM) {
 
574
         $amount = $this->LoadInt8(substr($fileData, 0, 1));
 
575
     $unit = $this->LoadInt8(substr($fileData, 1, 1));
 
576
     if ($unit == 0)
 
577
        $unit = 'm';
 
578
     elseif ($unit == 1)
 
579
        $unit = 'h';
 
580
     else
 
581
        $unit = 'd';
 
582
     $NewRec['Alarm'] = $amount . $unit;
 
583
     $fileData = substr($fileData, 2);
 
584
      } else
 
585
         unset($NewRec['Alarm']);
 
586
 
 
587
      if ($Flags & PDB_DATEBOOK_FLAG_REPEAT) {
 
588
         $Repeat = array();
 
589
     $Repeat['Type'] = $this->LoadInt8(substr($fileData, 0, 1));
 
590
     $Repeat['unknown1'] = $this->LoadInt8(substr($fileData, 1, 1));
 
591
     $End = $this->LoadInt16(substr($fileData, 2, 2));
 
592
     $Repeat['Frequency'] = $this->LoadInt8(substr($fileData, 4, 1));
 
593
     $RepeatOn = $this->LoadInt8(substr($fileData, 5, 1));
 
594
     $RepeatSoW = $this->LoadInt8(substr($fileData, 6, 1));
 
595
     $Repeat['unknown2'] = $this->LoadInt8(substr($fileData, 7, 1));
 
596
     $fileData = substr($fileData, 8);
 
597
 
 
598
     if ($End != 65535 && $End <= 0)
 
599
        $Repeat['End'] = $this->Int16ToDate($End);
 
600
 
 
601
         if ($Repeat['Type'] == PDB_DATEBOOK_REPEAT_WEEKLY) {
 
602
        $days = '';
 
603
        if ($RepeatOn & 64)
 
604
           $days .= '0';
 
605
        if ($RepeatOn & 32)
 
606
           $days .= '1';
 
607
        if ($RepeatOn & 16)
 
608
           $days .= '2';
 
609
        if ($RepeatOn & 8)
 
610
           $days .= '3';
 
611
        if ($RepeatOn & 4)
 
612
           $days .= '4';
 
613
        if ($RepeatOn & 2)
 
614
           $days .= '5';
 
615
        if ($RepeatOn & 1)
 
616
           $days .= '6';
 
617
        $Repeat['Days'] = $days;
 
618
        $Repeat['StartOfWeek'] = $RepeatSoW;
 
619
     } elseif ($Repeat['Type'] == PDB_DATEBOOK_REPEAT_MONTH_BY_DAY) {
 
620
        $Repeat['DayNum'] = $RepeatOn % 7;
 
621
        $RepeatOn /= 7;
 
622
        settype($RepeatOn, 'integer');
 
623
        $Repeat['WeekNum'] = $RepeatOn;
 
624
     }
 
625
 
 
626
     $NewRec['Repeat'] = $Repeat;
 
627
      }
 
628
 
 
629
      if ($Flags & PDB_DATEBOOK_FLAG_EXCEPTIONS) {
 
630
         $Exceptions = array();
 
631
     $number = $this->LoadInt16(substr($fileData, 0, 2));
 
632
     $fileData = substr($fileData, 2);
 
633
     while ($number --) {
 
634
        $Exc = $this->LoadInt16(substr($fileData, 0, 2));
 
635
        $Exceptions[] = $this->Int16ToDate($Exc);
 
636
        $fileData = substr($fileData, 2);
 
637
     }
 
638
     $NewRec['Exceptions'] = $Exceptions;
 
639
      }
 
640
 
 
641
      if ($Flags & PDB_DATEBOOK_FLAG_DESCRIPTION) {
 
642
         $i = 0;
 
643
     $NewRec['Description'] = '';
 
644
     while ($i < strlen($fileData) && $fileData[$i] != "\0") {
 
645
        $NewRec['Description'] .= $fileData[$i];
 
646
        $i ++;
 
647
     }
 
648
     $fileData = substr($fileData, $i + 1);
 
649
      }
 
650
 
 
651
      if ($Flags & PDB_DATEBOOK_FLAG_NOTE) {
 
652
         $i = 0;
 
653
     $NewRec['Note'] = '';
 
654
     while ($i < strlen($fileData) && $fileData[$i] != "\0") {
 
655
        $NewRec['Note'] .= $fileData[$i];
 
656
        $i ++;
 
657
     }
 
658
     $fileData = substr($fileData, 0, $i + 1);
 
659
      }
 
660
 
 
661
      $this->Records[$RecordInfo['UID']] = $NewRec;
 
662
      return false;
 
663
   }
 
664
 
 
665
}