home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / adodb-csvlib.inc.php < prev    next >
Encoding:
PHP Script  |  2004-03-20  |  5.9 KB  |  235 lines

  1. <?php
  2. global $ADODB_INCLUDED_CSV;
  3. $ADODB_INCLUDED_CSV = 1;
  4.  
  5. /* 
  6.   V4.21 20 Mar 2004  (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
  7.   Released under both BSD license and Lesser GPL library license. 
  8.   Whenever there is any discrepancy between the two licenses, 
  9.   the BSD license will take precedence. See License.txt. 
  10.   Set tabs to 4 for best viewing.
  11.   
  12.   Latest version is available at http://php.weblogs.com/
  13.   
  14.   Library for CSV serialization. This is used by the csv/proxy driver and is the 
  15.   CacheExecute() serialization format. 
  16.   
  17.   ==== NOTE ====
  18.   Format documented at http://php.weblogs.com/ADODB_CSV
  19.   ==============
  20. */
  21.  
  22.     /**
  23.       * convert a recordset into special format
  24.      *
  25.      * @param rs    the recordset
  26.      *
  27.      * @return    the CSV formated data
  28.      */
  29.     function _rs2serialize(&$rs,$conn=false,$sql='')
  30.     {
  31.         $max = ($rs) ? $rs->FieldCount() : 0;
  32.         
  33.         if ($sql) $sql = urlencode($sql);
  34.         // metadata setup
  35.         
  36.         if ($max <= 0 || $rs->dataProvider == 'empty') { // is insert/update/delete
  37.             if (is_object($conn)) {
  38.                 $sql .= ','.$conn->Affected_Rows();
  39.                 $sql .= ','.$conn->Insert_ID();
  40.             } else
  41.                 $sql .= ',,';
  42.             
  43.             $text = "====-1,0,$sql\n";
  44.             return $text;
  45.         }
  46.         $tt = ($rs->timeCreated) ? $rs->timeCreated : time();
  47.         
  48.         ## changed format from ====0 to ====1
  49.         $line = "====1,$tt,$sql\n";
  50.         
  51.         if ($rs->databaseType == 'array') {
  52.             $rows =& $rs->_array;
  53.         } else {
  54.             $rows = array();
  55.             while (!$rs->EOF) {    
  56.                 $rows[] = $rs->fields;
  57.                 $rs->MoveNext();
  58.             } 
  59.         }
  60.         
  61.         for($i=0; $i < $max; $i++) {
  62.             $o =& $rs->FetchField($i);
  63.             $flds[] = $o;
  64.         }
  65.         
  66.         $rs =& new ADORecordSet_array();
  67.         $rs->InitArrayFields($rows,$flds);
  68.         return $line.serialize($rs);
  69.     }
  70.  
  71.     
  72. /**
  73. * Open CSV file and convert it into Data. 
  74. *
  75. * @param url          file/ftp/http url
  76. * @param err        returns the error message
  77. * @param timeout    dispose if recordset has been alive for $timeout secs
  78. *
  79. * @return        recordset, or false if error occured. If no
  80. *            error occurred in sql INSERT/UPDATE/DELETE, 
  81. *            empty recordset is returned
  82. */
  83.     function &csv2rs($url,&$err,$timeout=0)
  84.     {
  85.         $err = false;
  86.         $fp = @fopen($url,'r');
  87.         if (!$fp) {
  88.             $err = $url.' file/URL not found';
  89.             return false;
  90.         }
  91.         flock($fp, LOCK_SH);
  92.         $arr = array();
  93.         $ttl = 0;
  94.         
  95.         if ($meta = fgetcsv($fp, 32000, ",")) {
  96.             // check if error message
  97.             if (strncmp($meta[0],'****',4) === 0) {
  98.                 $err = trim(substr($meta[0],4,1024));
  99.                 fclose($fp);
  100.                 return false;
  101.             }
  102.             // check for meta data
  103.             // $meta[0] is -1 means return an empty recordset
  104.             // $meta[1] contains a time 
  105.     
  106.             if (strncmp($meta[0], '====',4) === 0) {
  107.             
  108.                 if ($meta[0] == "====-1") {
  109.                     if (sizeof($meta) < 5) {
  110.                         $err = "Corrupt first line for format -1";
  111.                         fclose($fp);
  112.                         return false;
  113.                     }
  114.                     fclose($fp);
  115.                     
  116.                     if ($timeout > 0) {
  117.                         $err = " Illegal Timeout $timeout ";
  118.                         return false;
  119.                     }
  120.                     $rs->fields = array();
  121.                     $rs->timeCreated = $meta[1];
  122.                     $rs =& new ADORecordSet($val=true);
  123.                     $rs->EOF = true;
  124.                     $rs->_numOfFields=0;
  125.                     $rs->sql = urldecode($meta[2]);
  126.                     $rs->affectedrows = (integer)$meta[3];
  127.                     $rs->insertid = $meta[4];    
  128.                     return $rs;
  129.                 } 
  130.             # Under high volume loads, we want only 1 thread/process to _write_file
  131.             # so that we don't have 50 processes queueing to write the same data.
  132.             # Would require probabilistic blocking write 
  133.             #
  134.             # -2 sec before timeout, give processes 1/16 chance of writing to file with blocking io
  135.             # -1 sec after timeout give processes 1/4 chance of writing with blocking
  136.             # +0 sec after timeout, give processes 100% chance writing with blocking
  137.                 if (sizeof($meta) > 1) {
  138.                     if($timeout >0){ 
  139.                         $tdiff = $meta[1]+$timeout - time();
  140.                         if ($tdiff <= 2) {
  141.                             switch($tdiff) {
  142.                             case 2: 
  143.                                 if ((rand() & 15) == 0) {
  144.                                     fclose($fp);
  145.                                     $err = "Timeout 2";
  146.                                     return false;
  147.                                 }
  148.                                 break;
  149.                             case 1:
  150.                                 if ((rand() & 3) == 0) {
  151.                                     fclose($fp);
  152.                                     $err = "Timeout 1";
  153.                                     return false;
  154.                                 }
  155.                                 break;
  156.                             default: 
  157.                                 fclose($fp);
  158.                                 $err = "Timeout 0";
  159.                                 return false;
  160.                             } // switch
  161.                             
  162.                         } // if check flush cache
  163.                     }// (timeout>0)
  164.                     $ttl = $meta[1];
  165.                 }
  166.                 //================================================
  167.                 // new cache format - use serialize extensively...
  168.                 if ($meta[0] === '====1') {
  169.                     // slurp in the data
  170.                     $MAXSIZE = 128000;
  171.                     
  172.                     $text = fread($fp,$MAXSIZE);
  173.                     if (strlen($text) === $MAXSIZE) {
  174.                         while ($txt = fread($fp,$MAXSIZE)) {
  175.                             $text .= $txt;
  176.                         }
  177.                     }
  178.                     fclose($fp);
  179.                     @$rs = unserialize($text);
  180.                     if (is_object($rs)) $rs->timeCreated = $ttl;
  181.                     return $rs;
  182.                 }
  183.                 
  184.                 $meta = false;
  185.                 $meta = fgetcsv($fp, 32000, ",");
  186.                 if (!$meta) {
  187.                     fclose($fp);
  188.                     $err = "Unexpected EOF 1";
  189.                     return false;
  190.                 }
  191.             }
  192.  
  193.             // Get Column definitions
  194.             $flds = array();
  195.             foreach($meta as $o) {
  196.                 $o2 = explode(':',$o);
  197.                 if (sizeof($o2)!=3) {
  198.                     $arr[] = $meta;
  199.                     $flds = false;
  200.                     break;
  201.                 }
  202.                 $fld =& new ADOFieldObject();
  203.                 $fld->name = urldecode($o2[0]);
  204.                 $fld->type = $o2[1];
  205.                 $fld->max_length = $o2[2];
  206.                 $flds[] = $fld;
  207.             }
  208.         } else {
  209.             fclose($fp);
  210.             $err = "Recordset had unexpected EOF 2";
  211.             return false;
  212.         }
  213.         
  214.         // slurp in the data
  215.         $MAXSIZE = 128000;
  216.         
  217.         $text = '';
  218.         while ($txt = fread($fp,$MAXSIZE)) {
  219.             $text .= $txt;
  220.         }
  221.             
  222.         fclose($fp);
  223.         @$arr = unserialize($text);
  224.         //var_dump($arr);
  225.         if (!is_array($arr)) {
  226.             $err = "Recordset had unexpected EOF (in serialized recordset)";
  227.             if (get_magic_quotes_runtime()) $err .= ". Magic Quotes Runtime should be disabled!";
  228.             return false;
  229.         }
  230.         $rs =& new ADORecordSet_array();
  231.         $rs->timeCreated = $ttl;
  232.         $rs->InitArrayFields($arr,$flds);
  233.         return $rs;
  234.     }
  235. ?>