Documentation TYPO3 par Ameos |
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 V4.93 10 Oct 2006 (c) 2000-2006 John Lim (jlim#natsoft.com.my). 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->sql = $rs->sql; 00075 $rs2->oldProvider = $rs->dataProvider; 00076 $rs2->InitArrayFields($rows,$flds); 00077 $rs2->fetchMode = $savefetch; 00078 return $line.serialize($rs2); 00079 } 00080 00081 00093 function &csv2rs($url,&$err,$timeout=0, $rsclass='ADORecordSet_array') 00094 { 00095 $false = false; 00096 $err = false; 00097 $fp = @fopen($url,'rb'); 00098 if (!$fp) { 00099 $err = $url.' file/URL not found'; 00100 return $false; 00101 } 00102 @flock($fp, LOCK_SH); 00103 $arr = array(); 00104 $ttl = 0; 00105 00106 if ($meta = fgetcsv($fp, 32000, ",")) { 00107 // check if error message 00108 if (strncmp($meta[0],'****',4) === 0) { 00109 $err = trim(substr($meta[0],4,1024)); 00110 fclose($fp); 00111 return $false; 00112 } 00113 // check for meta data 00114 // $meta[0] is -1 means return an empty recordset 00115 // $meta[1] contains a time 00116 00117 if (strncmp($meta[0], '====',4) === 0) { 00118 00119 if ($meta[0] == "====-1") { 00120 if (sizeof($meta) < 5) { 00121 $err = "Corrupt first line for format -1"; 00122 fclose($fp); 00123 return $false; 00124 } 00125 fclose($fp); 00126 00127 if ($timeout > 0) { 00128 $err = " Illegal Timeout $timeout "; 00129 return $false; 00130 } 00131 00132 $rs = new $rsclass($val=true); 00133 $rs->fields = array(); 00134 $rs->timeCreated = $meta[1]; 00135 $rs->EOF = true; 00136 $rs->_numOfFields = 0; 00137 $rs->sql = urldecode($meta[2]); 00138 $rs->affectedrows = (integer)$meta[3]; 00139 $rs->insertid = $meta[4]; 00140 return $rs; 00141 } 00142 # Under high volume loads, we want only 1 thread/process to _write_file 00143 # so that we don't have 50 processes queueing to write the same data. 00144 # We use probabilistic timeout, ahead of time. 00145 # 00146 # -4 sec before timeout, give processes 1/32 chance of timing out 00147 # -2 sec before timeout, give processes 1/16 chance of timing out 00148 # -1 sec after timeout give processes 1/4 chance of timing out 00149 # +0 sec after timeout, give processes 100% chance of timing out 00150 if (sizeof($meta) > 1) { 00151 if($timeout >0){ 00152 $tdiff = (integer)( $meta[1]+$timeout - time()); 00153 if ($tdiff <= 2) { 00154 switch($tdiff) { 00155 case 4: 00156 case 3: 00157 if ((rand() & 31) == 0) { 00158 fclose($fp); 00159 $err = "Timeout 3"; 00160 return $false; 00161 } 00162 break; 00163 case 2: 00164 if ((rand() & 15) == 0) { 00165 fclose($fp); 00166 $err = "Timeout 2"; 00167 return $false; 00168 } 00169 break; 00170 case 1: 00171 if ((rand() & 3) == 0) { 00172 fclose($fp); 00173 $err = "Timeout 1"; 00174 return $false; 00175 } 00176 break; 00177 default: 00178 fclose($fp); 00179 $err = "Timeout 0"; 00180 return $false; 00181 } // switch 00182 00183 } // if check flush cache 00184 }// (timeout>0) 00185 $ttl = $meta[1]; 00186 } 00187 //================================================ 00188 // new cache format - use serialize extensively... 00189 if ($meta[0] === '====1') { 00190 // slurp in the data 00191 $MAXSIZE = 128000; 00192 00193 $text = fread($fp,$MAXSIZE); 00194 if (strlen($text)) { 00195 while ($txt = fread($fp,$MAXSIZE)) { 00196 $text .= $txt; 00197 } 00198 } 00199 fclose($fp); 00200 $rs = unserialize($text); 00201 if (is_object($rs)) $rs->timeCreated = $ttl; 00202 else { 00203 $err = "Unable to unserialize recordset"; 00204 //echo htmlspecialchars($text),' !--END--!<p>'; 00205 } 00206 return $rs; 00207 } 00208 00209 $meta = false; 00210 $meta = fgetcsv($fp, 32000, ","); 00211 if (!$meta) { 00212 fclose($fp); 00213 $err = "Unexpected EOF 1"; 00214 return $false; 00215 } 00216 } 00217 00218 // Get Column definitions 00219 $flds = array(); 00220 foreach($meta as $o) { 00221 $o2 = explode(':',$o); 00222 if (sizeof($o2)!=3) { 00223 $arr[] = $meta; 00224 $flds = false; 00225 break; 00226 } 00227 $fld = new ADOFieldObject(); 00228 $fld->name = urldecode($o2[0]); 00229 $fld->type = $o2[1]; 00230 $fld->max_length = $o2[2]; 00231 $flds[] = $fld; 00232 } 00233 } else { 00234 fclose($fp); 00235 $err = "Recordset had unexpected EOF 2"; 00236 return $false; 00237 } 00238 00239 // slurp in the data 00240 $MAXSIZE = 128000; 00241 00242 $text = ''; 00243 while ($txt = fread($fp,$MAXSIZE)) { 00244 $text .= $txt; 00245 } 00246 00247 fclose($fp); 00248 @$arr = unserialize($text); 00249 //var_dump($arr); 00250 if (!is_array($arr)) { 00251 $err = "Recordset had unexpected EOF (in serialized recordset)"; 00252 if (get_magic_quotes_runtime()) $err .= ". Magic Quotes Runtime should be disabled!"; 00253 return $false; 00254 } 00255 $rs = new $rsclass(); 00256 $rs->timeCreated = $ttl; 00257 $rs->InitArrayFields($arr,$flds); 00258 return $rs; 00259 } 00260 00261 00265 function adodb_write_file($filename, $contents,$debug=false) 00266 { 00267 # http://www.php.net/bugs.php?id=9203 Bug that flock fails on Windows 00268 # So to simulate locking, we assume that rename is an atomic operation. 00269 # First we delete $filename, then we create a $tempfile write to it and 00270 # rename to the desired $filename. If the rename works, then we successfully 00271 # modified the file exclusively. 00272 # What a stupid need - having to simulate locking. 00273 # Risks: 00274 # 1. $tempfile name is not unique -- very very low 00275 # 2. unlink($filename) fails -- ok, rename will fail 00276 # 3. adodb reads stale file because unlink fails -- ok, $rs timeout occurs 00277 # 4. another process creates $filename between unlink() and rename() -- ok, rename() fails and cache updated 00278 if (strncmp(PHP_OS,'WIN',3) === 0) { 00279 // skip the decimal place 00280 $mtime = substr(str_replace(' ','_',microtime()),2); 00281 // getmypid() actually returns 0 on Win98 - never mind! 00282 $tmpname = $filename.uniqid($mtime).getmypid(); 00283 if (!($fd = @fopen($tmpname,'a'))) return false; 00284 $ok = ftruncate($fd,0); 00285 if (!fwrite($fd,$contents)) $ok = false; 00286 fclose($fd); 00287 chmod($tmpname,0644); 00288 // the tricky moment 00289 @unlink($filename); 00290 if (!@rename($tmpname,$filename)) { 00291 unlink($tmpname); 00292 $ok = false; 00293 } 00294 if (!$ok) { 00295 if ($debug) ADOConnection::outp( " Rename $tmpname ".($ok? 'ok' : 'failed')); 00296 } 00297 return $ok; 00298 } 00299 if (!($fd = @fopen($filename, 'a'))) return false; 00300 if (flock($fd, LOCK_EX) && ftruncate($fd, 0)) { 00301 $ok = fwrite( $fd, $contents ); 00302 fclose($fd); 00303 chmod($filename,0644); 00304 }else { 00305 fclose($fd); 00306 if ($debug)ADOConnection::outp( " Failed acquiring lock for $filename<br>\n"); 00307 $ok = false; 00308 } 00309 00310 return $ok; 00311 } 00312 ?>