Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/lib/adodb/adodb-csvlib.inc.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // security - hide paths
00004 if (!defined('ADODB_DIR')) die();
00005 
00006 global $ADODB_INCLUDED_CSV;
00007 $ADODB_INCLUDED_CSV = 1;
00008 
00009 /* 
00010 
00011   V5.14 8 Sept 2011   (c) 2000-2011 John Lim (jlim#natsoft.com). All rights reserved.
00012   Released under both BSD license and Lesser GPL library license. 
00013   Whenever there is any discrepancy between the two licenses, 
00014   the BSD license will take precedence. See License.txt. 
00015   Set tabs to 4 for best viewing.
00016   
00017   Latest version is available at http://adodb.sourceforge.net
00018   
00019   Library for CSV serialization. This is used by the csv/proxy driver and is the 
00020   CacheExecute() serialization format. 
00021   
00022   ==== NOTE ====
00023   Format documented at http://php.weblogs.com/ADODB_CSV
00024   ==============
00025 */
00026 
00034         function _rs2serialize(&$rs,$conn=false,$sql='')
00035         {
00036                 $max = ($rs) ? $rs->FieldCount() : 0;
00037                 
00038                 if ($sql) $sql = urlencode($sql);
00039                 // metadata setup
00040                 
00041                 if ($max <= 0 || $rs->dataProvider == 'empty') { // is insert/update/delete
00042                         if (is_object($conn)) {
00043                                 $sql .= ','.$conn->Affected_Rows();
00044                                 $sql .= ','.$conn->Insert_ID();
00045                         } else
00046                                 $sql .= ',,';
00047                         
00048                         $text = "====-1,0,$sql\n";
00049                         return $text;
00050                 }
00051                 $tt = ($rs->timeCreated) ? $rs->timeCreated : time();
00052                 
00053                 ## changed format from ====0 to ====1
00054                 $line = "====1,$tt,$sql\n";
00055                 
00056                 if ($rs->databaseType == 'array') {
00057                         $rows = $rs->_array;
00058                 } else {
00059                         $rows = array();
00060                         while (!$rs->EOF) {     
00061                                 $rows[] = $rs->fields;
00062                                 $rs->MoveNext();
00063                         } 
00064                 }
00065                 
00066                 for($i=0; $i < $max; $i++) {
00067                         $o = $rs->FetchField($i);
00068                         $flds[] = $o;
00069                 }
00070         
00071                 $savefetch = isset($rs->adodbFetchMode) ? $rs->adodbFetchMode : $rs->fetchMode;
00072                 $class = $rs->connection->arrayClass;
00073                 $rs2 = new $class();
00074                 $rs2->timeCreated = $rs->timeCreated; # memcache fix
00075                 $rs2->sql = $rs->sql;
00076                 $rs2->oldProvider = $rs->dataProvider; 
00077                 $rs2->InitArrayFields($rows,$flds);
00078                 $rs2->fetchMode = $savefetch;
00079                 return $line.serialize($rs2);
00080         }
00081 
00082         
00094         function csv2rs($url,&$err,$timeout=0, $rsclass='ADORecordSet_array')
00095         {
00096                 $false = false;
00097                 $err = false;
00098                 $fp = @fopen($url,'rb');
00099                 if (!$fp) {
00100                         $err = $url.' file/URL not found';
00101                         return $false;
00102                 }
00103                 @flock($fp, LOCK_SH);
00104                 $arr = array();
00105                 $ttl = 0;
00106                 
00107                 if ($meta = fgetcsv($fp, 32000, ",")) {
00108                         // check if error message
00109                         if (strncmp($meta[0],'****',4) === 0) {
00110                                 $err = trim(substr($meta[0],4,1024));
00111                                 fclose($fp);
00112                                 return $false;
00113                         }
00114                         // check for meta data
00115                         // $meta[0] is -1 means return an empty recordset
00116                         // $meta[1] contains a time 
00117         
00118                         if (strncmp($meta[0], '====',4) === 0) {
00119                         
00120                                 if ($meta[0] == "====-1") {
00121                                         if (sizeof($meta) < 5) {
00122                                                 $err = "Corrupt first line for format -1";
00123                                                 fclose($fp);
00124                                                 return $false;
00125                                         }
00126                                         fclose($fp);
00127                                         
00128                                         if ($timeout > 0) {
00129                                                 $err = " Illegal Timeout $timeout ";
00130                                                 return $false;
00131                                         }
00132                                         
00133                                         $rs = new $rsclass($val=true);
00134                                         $rs->fields = array();
00135                                         $rs->timeCreated = $meta[1];
00136                                         $rs->EOF = true;
00137                                         $rs->_numOfFields = 0;
00138                                         $rs->sql = urldecode($meta[2]);
00139                                         $rs->affectedrows = (integer)$meta[3];
00140                                         $rs->insertid = $meta[4];       
00141                                         return $rs;
00142                                 } 
00143                         # Under high volume loads, we want only 1 thread/process to _write_file
00144                         # so that we don't have 50 processes queueing to write the same data.
00145                         # We use probabilistic timeout, ahead of time.
00146                         #
00147                         # -4 sec before timeout, give processes 1/32 chance of timing out
00148                         # -2 sec before timeout, give processes 1/16 chance of timing out
00149                         # -1 sec after timeout give processes 1/4 chance of timing out
00150                         # +0 sec after timeout, give processes 100% chance of timing out
00151                                 if (sizeof($meta) > 1) {
00152                                         if($timeout >0){ 
00153                                                 $tdiff = (integer)( $meta[1]+$timeout - time());
00154                                                 if ($tdiff <= 2) {
00155                                                         switch($tdiff) {
00156                                                         case 4:
00157                                                         case 3:
00158                                                                 if ((rand() & 31) == 0) {
00159                                                                         fclose($fp);
00160                                                                         $err = "Timeout 3";
00161                                                                         return $false;
00162                                                                 }
00163                                                                 break;
00164                                                         case 2: 
00165                                                                 if ((rand() & 15) == 0) {
00166                                                                         fclose($fp);
00167                                                                         $err = "Timeout 2";
00168                                                                         return $false;
00169                                                                 }
00170                                                                 break;
00171                                                         case 1:
00172                                                                 if ((rand() & 3) == 0) {
00173                                                                         fclose($fp);
00174                                                                         $err = "Timeout 1";
00175                                                                         return $false;
00176                                                                 }
00177                                                                 break;
00178                                                         default: 
00179                                                                 fclose($fp);
00180                                                                 $err = "Timeout 0";
00181                                                                 return $false;
00182                                                         } // switch
00183                                                         
00184                                                 } // if check flush cache
00185                                         }// (timeout>0)
00186                                         $ttl = $meta[1];
00187                                 }
00188                                 //================================================
00189                                 // new cache format - use serialize extensively...
00190                                 if ($meta[0] === '====1') {
00191                                         // slurp in the data
00192                                         $MAXSIZE = 128000;
00193                                         
00194                                         $text = fread($fp,$MAXSIZE);
00195                                         if (strlen($text)) {
00196                                                 while ($txt = fread($fp,$MAXSIZE)) {
00197                                                         $text .= $txt;
00198                                                 }
00199                                         }
00200                                         fclose($fp);
00201                                         $rs = unserialize($text);
00202                                         if (is_object($rs)) $rs->timeCreated = $ttl;
00203                                         else {
00204                                                 $err = "Unable to unserialize recordset";
00205                                                 //echo htmlspecialchars($text),' !--END--!<p>';
00206                                         }
00207                                         return $rs;
00208                                 }
00209                                 
00210                                 $meta = false;
00211                                 $meta = fgetcsv($fp, 32000, ",");
00212                                 if (!$meta) {
00213                                         fclose($fp);
00214                                         $err = "Unexpected EOF 1";
00215                                         return $false;
00216                                 }
00217                         }
00218 
00219                         // Get Column definitions
00220                         $flds = array();
00221                         foreach($meta as $o) {
00222                                 $o2 = explode(':',$o);
00223                                 if (sizeof($o2)!=3) {
00224                                         $arr[] = $meta;
00225                                         $flds = false;
00226                                         break;
00227                                 }
00228                                 $fld = new ADOFieldObject();
00229                                 $fld->name = urldecode($o2[0]);
00230                                 $fld->type = $o2[1];
00231                                 $fld->max_length = $o2[2];
00232                                 $flds[] = $fld;
00233                         }
00234                 } else {
00235                         fclose($fp);
00236                         $err = "Recordset had unexpected EOF 2";
00237                         return $false;
00238                 }
00239                 
00240                 // slurp in the data
00241                 $MAXSIZE = 128000;
00242                 
00243                 $text = '';
00244                 while ($txt = fread($fp,$MAXSIZE)) {
00245                         $text .= $txt;
00246                 }
00247                         
00248                 fclose($fp);
00249                 @$arr = unserialize($text);
00250                 //var_dump($arr);
00251                 if (!is_array($arr)) {
00252                         $err = "Recordset had unexpected EOF (in serialized recordset)";
00253                         if (get_magic_quotes_runtime()) $err .= ". Magic Quotes Runtime should be disabled!";
00254                         return $false;
00255                 }
00256                 $rs = new $rsclass();
00257                 $rs->timeCreated = $ttl;
00258                 $rs->InitArrayFields($arr,$flds);
00259                 return $rs;
00260         }
00261         
00262 
00267         function adodb_write_file($filename, $contents,$debug=false)
00268         { 
00269         # http://www.php.net/bugs.php?id=9203 Bug that flock fails on Windows
00270         # So to simulate locking, we assume that rename is an atomic operation.
00271         # First we delete $filename, then we create a $tempfile write to it and 
00272         # rename to the desired $filename. If the rename works, then we successfully 
00273         # modified the file exclusively.
00274         # What a stupid need - having to simulate locking.
00275         # Risks:
00276         # 1. $tempfile name is not unique -- very very low
00277         # 2. unlink($filename) fails -- ok, rename will fail
00278         # 3. adodb reads stale file because unlink fails -- ok, $rs timeout occurs
00279         # 4. another process creates $filename between unlink() and rename() -- ok, rename() fails and  cache updated
00280                 if (strncmp(PHP_OS,'WIN',3) === 0) {
00281                         // skip the decimal place
00282                         $mtime = substr(str_replace(' ','_',microtime()),2); 
00283                         // getmypid() actually returns 0 on Win98 - never mind!
00284                         $tmpname = $filename.uniqid($mtime).getmypid();
00285                         if (!($fd = @fopen($tmpname,'w'))) return false;
00286                         if (fwrite($fd,$contents)) $ok = true;
00287                         else $ok = false;
00288                         fclose($fd);
00289                         
00290                         if ($ok) {
00291                                 @chmod($tmpname,0644);
00292                                 // the tricky moment
00293                                 @unlink($filename);
00294                                 if (!@rename($tmpname,$filename)) {
00295                                         unlink($tmpname);
00296                                         $ok = 0;
00297                                 }
00298                                 if (!$ok) {
00299                                         if ($debug) ADOConnection::outp( " Rename $tmpname ".($ok? 'ok' : 'failed'));
00300                                 }
00301                         }
00302                         return $ok;
00303                 }
00304                 if (!($fd = @fopen($filename, 'a'))) return false;
00305                 if (flock($fd, LOCK_EX) && ftruncate($fd, 0)) {
00306                         if (fwrite( $fd, $contents )) $ok = true;
00307                         else $ok = false;
00308                         fclose($fd);
00309                         @chmod($filename,0644);
00310                 }else {
00311                         fclose($fd);
00312                         if ($debug)ADOConnection::outp( " Failed acquiring lock for $filename<br>\n");
00313                         $ok = false;
00314                 }
00315         
00316                 return $ok;
00317         }
00318 ?>
 All Data Structures Namespaces Files Functions Variables Enumerations