[ Index ]
 

Code source de eGroupWare 1.2.106-2

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/addressbook/inc/ -> class.pdb.inc.php (source)

   1  <?php
   2    /**************************************************************************\
   3    * eGroupWare API - Palm Database Access                                    *
   4    * This file written by Miles Lott <milos@groupwhere.org>                   *
   5    * Access to palm OS database structures (?)                                *
   6    * ------------------------------------------------------------------------ *
   7    * Portions of code from ToPTIP                                             *
   8    *   Copyright (C) 2000-2001 Pierre Dittgen                                 *
   9    *   This file is a translation of the txt2pdbdoc tool                      *
  10    *   written in C Paul J. Lucas (plj@best.com)                              *
  11    * ------------------------------------------------------------------------ *
  12    * This library may be part of the eGroupWare API                           *
  13    * ------------------------------------------------------------------------ *
  14    * This library is free software; you can redistribute it and/or modify it  *
  15    * under the terms of the GNU Lesser General Public License as published by *
  16    * the Free Software Foundation; either version 2.1 of the License,         *
  17    * or any later version.                                                    *
  18    * This library is distributed in the hope that it will be useful, but      *
  19    * WITHOUT ANY WARRANTY; without even the implied warranty of               *
  20    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                     *
  21    * See the GNU Lesser General Public License for more details.              *
  22    * You should have received a copy of the GNU Lesser General Public License *
  23    * along with this library; if not, write to the Free Software Foundation,  *
  24    * Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA            *
  25    \**************************************************************************/
  26  
  27    /* $Id: class.pdb.inc.php 16494 2004-08-18 08:32:59Z ralfbecker $ */
  28  
  29      // ONLY THE FETCH FUNCTION SHOULD BE CALLED AT THIS TIME:
  30      //     $pdb = CreateObject('addressbook.pdb');
  31      //     $pdb->fetch($content, $title, $document);
  32      //
  33      // This will force a download of the pdb file.
  34      //
  35      // READ DOES NOT WORK
  36      // FORMAT OF FILE IS A DOC, NOT A TRUE PALM ADDRESS BOOK
  37      class pdb
  38      {
  39          var $record_size            = 4096;    // Size of text record
  40          var $pdb_header_size        = 78;    // Size of the file header (don't touch!)
  41          var $pdb_record_header_size    = 8;    // Size of a text record header (don't touch either!)
  42  
  43          /**
  44          * Convert a integer value to a two bytes value string
  45          */
  46  		function int2($value)
  47          {
  48              return sprintf("%c%c", $value / 256, $value % 256);
  49          }
  50  
  51          /**
  52          * Convert a integer value to a four bytes value string
  53          */
  54  		function int4($value)
  55          {
  56              for($i=0; $i<4; $i++)
  57              {
  58                  $b[$i] = $value % 256;
  59                  $value = (int)(($value - $b[$i]) / 256);
  60              }
  61              return sprintf("%c%c%c%c", $b[3], $b[2], $b[1], $b[0]);
  62          }
  63  
  64          /**
  65          * Writes the header of the pdb file containing the title of the document
  66          * and different parameters including its size
  67          */
  68  		function write_header($fd, $title, $content_length)
  69          {
  70              // ============ File header =========================================
  71              // Title of the document, it's limited to 31 characters
  72              if(strlen($title) > 31)
  73              {
  74                  $title = substr($title, 0, 31);
  75              }
  76              fwrite($fd, $title);
  77  
  78              // Completion with null '\0' characters
  79              for($i=0; $i<32-strlen($title); $i++)
  80              {
  81                  fwrite($fd, sprintf("%c", 0), 1);
  82              }
  83  
  84              // attributes & version fields
  85              fwrite($fd, $this->int2(0));
  86              fwrite($fd, $this->int2(0));
  87  
  88              // create & modify time
  89              fwrite($fd, sprintf("%c%c%c%c", 6, 209, 68, 174), 4);
  90              fwrite($fd, sprintf("%c%c%c%c", 6, 209, 68, 174), 4);
  91  
  92              // backup time, modification number, app Info Id, sort info Id
  93              fwrite($fd, $this->int4(0));
  94              fwrite($fd, $this->int4(0));
  95              fwrite($fd, $this->int4(0));
  96              fwrite($fd, $this->int4(0));
  97  
  98              // Type & creator
  99              fwrite($fd, 'TEXt', 4);
 100              fwrite($fd, 'REAd', 4);
 101  
 102              // Id seed & next record list
 103              fwrite($fd, $this->int4(0));
 104              fwrite($fd, $this->int4(0));
 105  
 106              // Number of text records
 107              $full_tr    = (int)($content_length / $this->record_size);
 108              $notfull_tr    = $content_length % $this->record_size;
 109              $num_records = $full_tr;
 110              if($notfull_tr != 0)
 111              {
 112                  $num_records++;
 113              }
 114  
 115              // + 1 cause of record 0
 116              fwrite($fd, $this->int2($num_records + 1));
 117  
 118              // From here...
 119              $num_offsets = $num_records + 1;
 120              $offset = $this->pdb_header_size + $this->pdb_record_header_size * $num_offsets;
 121              $index    = 0x40 << 24 | 0x6F8000; // Why not!
 122  
 123              fwrite($fd, $this->int4($offset));
 124              fwrite($fd, $this->int4($index++));
 125  
 126              $val    = 110 + ($num_offsets - 2) * 8;
 127              while(--$num_offsets != 0)
 128              {
 129                  fwrite($fd, $this->int4($val));
 130                  $val += 4096;
 131                  fwrite($fd, $this->int4($index++));
 132              }
 133              // To here
 134              // Don't ask me how it works ;-)
 135  
 136              // ====== Write record 0 ============
 137              fwrite($fd, $this->int2(1));                // Version
 138              fwrite($fd, $this->int2(0));                // Reserved1
 139              fwrite($fd, $this->int4($content_length));    // doc size
 140              fwrite($fd, $this->int2($num_records));    // num records
 141              fwrite($fd, $this->int2($this->record_size));    // record size
 142              fwrite($fd, $this->int4(0));                // Reserved2
 143  
 144          }
 145  
 146          /**
 147          * Writes the given text, title on the given file descriptor
 148          * Note: It's saved as uncompressed doc
 149          * Note2: File descriptor is not closed at the end of the function
 150          */
 151  		function write($fd, $content, $title)
 152          {
 153              // Write header
 154              $this->write_header($fd, $title, strlen($content));
 155  
 156              // Write content
 157              fwrite($fd, $content);
 158  
 159              // And flushes all
 160              fflush($fd);
 161          }
 162  
 163          /**
 164          * Reads the header of the pdb file
 165          * and different parameters including its size
 166          */
 167  		function read_header($fd)
 168          {
 169              // ============ File header =========================================
 170              // Title of the document, it's limited to 31 characters
 171              $title = fread(31,$fd);
 172  
 173              // Completion with null '\0' characters
 174              for($i=0; $i<32-strlen($title); $i++)
 175              {
 176                  fwrite($fd, sprintf("%c", 0), 1);
 177              }
 178  
 179              // attributes & version fields
 180              fwrite($fd, $this->int2(0));
 181              fwrite($fd, $this->int2(0));
 182  
 183              // create & modify time
 184              fwrite($fd, sprintf("%c%c%c%c", 6, 209, 68, 174), 4);
 185              fwrite($fd, sprintf("%c%c%c%c", 6, 209, 68, 174), 4);
 186  
 187              // backup time, modification number, app Info Id, sort info Id
 188              fwrite($fd, $this->int4(0));
 189              fwrite($fd, $this->int4(0));
 190              fwrite($fd, $this->int4(0));
 191              fwrite($fd, $this->int4(0));
 192  
 193              // Type & creator
 194              fwrite($fd, 'TEXt', 4);
 195              fwrite($fd, 'REAd', 4);
 196  
 197              // Id seed & next record list
 198              fwrite($fd, $this->int4(0));
 199              fwrite($fd, $this->int4(0));
 200  
 201              // Number of text records
 202              $full_tr    = (int)($content_length / $this->record_size);
 203              $notfull_tr    = $content_length % $this->record_size;
 204              $num_records = $full_tr;
 205              if($notfull_tr != 0)
 206              {
 207                  $num_records++;
 208              }
 209  
 210              // + 1 cause of record 0
 211              fwrite($fd, $this->int2($num_records + 1));
 212  
 213              // From here...
 214              $num_offsets = $num_records + 1;
 215              $offset = $this->pdb_header_size + $this->pdb_record_header_size * $num_offsets;
 216              $index    = 0x40 << 24 | 0x6F8000; // Why not!
 217  
 218              fwrite($fd, $this->int4($offset));
 219              fwrite($fd, $this->int4($index++));
 220  
 221              $val    = 110 + ($num_offsets - 2) * 8;
 222              while(--$num_offsets != 0)
 223              {
 224                  fwrite($fd, $this->int4($val));
 225                  $val += 4096;
 226                  fwrite($fd, $this->int4($index++));
 227              }
 228              // To here
 229              // Don't ask me how it works ;-)
 230  
 231              // ====== Write record 0 ============
 232              fwrite($fd, $this->int2(1));                // Version
 233              fwrite($fd, $this->int2(0));                // Reserved1
 234              fwrite($fd, $this->int4($content_length));    // doc size
 235              fwrite($fd, $this->int2($num_records));    // num records
 236              fwrite($fd, $this->int2($this->record_size));    // record size
 237              fwrite($fd, $this->int4(0));                // Reserved2
 238          }
 239  
 240          /**
 241          * Reads a pdb from the given file descriptor
 242          * Note2: File descriptor is not closed at the end of the function
 243          */
 244  		function read($fd)
 245          {
 246              // Read header
 247              $header = $this->read_header($fd);
 248  
 249              // Read content
 250              $content = fread($fd);
 251  
 252              // And flushes all
 253              flush($fd);
 254          }
 255  
 256          /**
 257          * Creates a palmdoc and force the user to download it
 258          */
 259  		function fetch($content, $title, $document)
 260          {
 261              // Creates a temp file to put the current doc
 262              $tempfile = tempnam(".", "palm");
 263  
 264              // Creates the doc from the content
 265              $fd = fopen($tempfile, 'w');
 266              $this->write($fd, $content, $title);
 267              fclose($fd);
 268  
 269              // Forces the download
 270              header("Content-Type: application/force-download");
 271              header("Content-Disposition: attachment; filename=\"$document\"");
 272  
 273              // And writes the file content on stdout
 274              $fd = fopen("$tempfile", 'r');
 275              $content = fread($fd, filesize($tempfile));
 276              fclose($fd);
 277              print($content);
 278  
 279              // Cleaning
 280              unlink($tempfile);
 281              exit;
 282          }
 283      }
 284  ?>


Généré le : Sun Feb 25 17:20:01 2007 par Balluche grâce à PHPXref 0.7