3 // security - hide paths
4 if (!defined('ADODB_DIR')) die();
6 global $ADODB_INCLUDED_CSV;
7 $ADODB_INCLUDED_CSV = 1;
11 @version v5.20.16 12-Jan-2020
12 @copyright (c) 2000-2013 John Lim (jlim#natsoft.com). All rights reserved.
13 @copyright (c) 2014 Damien Regad, Mark Newnham and the ADOdb community
14 Released under both BSD license and Lesser GPL library license.
15 Whenever there is any discrepancy between the two licenses,
16 the BSD license will take precedence. See License.txt.
17 Set tabs to 4 for best viewing.
19 Latest version is available at http://adodb.org/
21 Library for CSV serialization. This is used by the csv/proxy driver and is the
22 CacheExecute() serialization format.
26 * convert a recordset into special format
28 * @param rs the recordset
30 * @return the CSV formated data
32 function _rs2serialize(&$rs,$conn=false,$sql='')
34 $max = ($rs) ?
$rs->FieldCount() : 0;
36 if ($sql) $sql = urlencode($sql);
39 if ($max <= 0 ||
$rs->dataProvider
== 'empty') { // is insert/update/delete
40 if (is_object($conn)) {
41 $sql .= ','.$conn->Affected_Rows();
42 $sql .= ','.$conn->Insert_ID();
46 $text = "====-1,0,$sql\n";
49 $tt = ($rs->timeCreated
) ?
$rs->timeCreated
: time();
51 ## changed format from ====0 to ====1
52 $line = "====1,$tt,$sql\n";
54 if ($rs->databaseType
== 'array') {
59 $rows[] = $rs->fields
;
64 for($i=0; $i < $max; $i++
) {
65 $o = $rs->FetchField($i);
69 $savefetch = isset($rs->adodbFetchMode
) ?
$rs->adodbFetchMode
: $rs->fetchMode
;
70 $class = $rs->connection
->arrayClass
;
72 $rs2->timeCreated
= $rs->timeCreated
; # memcache fix
74 $rs2->oldProvider
= $rs->dataProvider
;
75 $rs2->InitArrayFields($rows,$flds);
76 $rs2->fetchMode
= $savefetch;
77 return $line.serialize($rs2);
82 * Open CSV file and convert it into Data.
84 * @param url file/ftp/http url
85 * @param err returns the error message
86 * @param timeout dispose if recordset has been alive for $timeout secs
88 * @return recordset, or false if error occured. If no
89 * error occurred in sql INSERT/UPDATE/DELETE,
90 * empty recordset is returned
92 function csv2rs($url,&$err,$timeout=0, $rsclass='ADORecordSet_array')
96 $fp = @fopen
($url,'rb');
98 $err = $url.' file/URL not found';
101 @flock
($fp, LOCK_SH
);
105 if ($meta = fgetcsv($fp, 32000, ",")) {
106 // check if error message
107 if (strncmp($meta[0],'****',4) === 0) {
108 $err = trim(substr($meta[0],4,1024));
112 // check for meta data
113 // $meta[0] is -1 means return an empty recordset
114 // $meta[1] contains a time
116 if (strncmp($meta[0], '====',4) === 0) {
118 if ($meta[0] == "====-1") {
119 if (sizeof($meta) < 5) {
120 $err = "Corrupt first line for format -1";
127 $err = " Illegal Timeout $timeout ";
131 $rs = new $rsclass($val=true);
132 $rs->fields
= array();
133 $rs->timeCreated
= $meta[1];
135 $rs->_numOfFields
= 0;
136 $rs->sql
= urldecode($meta[2]);
137 $rs->affectedrows
= (integer)$meta[3];
138 $rs->insertid
= $meta[4];
141 # Under high volume loads, we want only 1 thread/process to _write_file
142 # so that we don't have 50 processes queueing to write the same data.
143 # We use probabilistic timeout, ahead of time.
145 # -4 sec before timeout, give processes 1/32 chance of timing out
146 # -2 sec before timeout, give processes 1/16 chance of timing out
147 # -1 sec after timeout give processes 1/4 chance of timing out
148 # +0 sec after timeout, give processes 100% chance of timing out
149 if (sizeof($meta) > 1) {
151 $tdiff = (integer)( $meta[1]+
$timeout - time());
156 if ((rand() & 31) == 0) {
163 if ((rand() & 15) == 0) {
170 if ((rand() & 3) == 0) {
182 } // if check flush cache
186 //================================================
187 // new cache format - use serialize extensively...
188 if ($meta[0] === '====1') {
192 $text = fread($fp,$MAXSIZE);
194 while ($txt = fread($fp,$MAXSIZE)) {
199 $rs = unserialize($text);
200 if (is_object($rs)) $rs->timeCreated
= $ttl;
202 $err = "Unable to unserialize recordset";
203 //echo htmlspecialchars($text),' !--END--!<p>';
209 $meta = fgetcsv($fp, 32000, ",");
212 $err = "Unexpected EOF 1";
217 // Get Column definitions
219 foreach($meta as $o) {
220 $o2 = explode(':',$o);
221 if (sizeof($o2)!=3) {
226 $fld = new ADOFieldObject();
227 $fld->name
= urldecode($o2[0]);
229 $fld->max_length
= $o2[2];
234 $err = "Recordset had unexpected EOF 2";
242 while ($txt = fread($fp,$MAXSIZE)) {
247 @$arr = unserialize($text);
249 if (!is_array($arr)) {
250 $err = "Recordset had unexpected EOF (in serialized recordset)";
251 if (get_magic_quotes_runtime()) $err .= ". Magic Quotes Runtime should be disabled!";
254 $rs = new $rsclass();
255 $rs->timeCreated
= $ttl;
256 $rs->InitArrayFields($arr,$flds);
262 * Save a file $filename and its $contents (normally for caching) with file locking
263 * Returns true if ok, false if fopen/fwrite error, 0 if rename error (eg. file is locked)
265 function adodb_write_file($filename, $contents,$debug=false)
267 # http://www.php.net/bugs.php?id=9203 Bug that flock fails on Windows
268 # So to simulate locking, we assume that rename is an atomic operation.
269 # First we delete $filename, then we create a $tempfile write to it and
270 # rename to the desired $filename. If the rename works, then we successfully
271 # modified the file exclusively.
272 # What a stupid need - having to simulate locking.
274 # 1. $tempfile name is not unique -- very very low
275 # 2. unlink($filename) fails -- ok, rename will fail
276 # 3. adodb reads stale file because unlink fails -- ok, $rs timeout occurs
277 # 4. another process creates $filename between unlink() and rename() -- ok, rename() fails and cache updated
278 if (strncmp(PHP_OS
,'WIN',3) === 0) {
279 // skip the decimal place
280 $mtime = substr(str_replace(' ','_',microtime()),2);
281 // getmypid() actually returns 0 on Win98 - never mind!
282 $tmpname = $filename.uniqid($mtime).getmypid();
283 if (!($fd = @fopen
($tmpname,'w'))) return false;
284 if (fwrite($fd,$contents)) $ok = true;
289 @chmod
($tmpname,0644);
292 if (!@rename
($tmpname,$filename)) {
297 if ($debug) ADOConnection
::outp( " Rename $tmpname ".($ok?
'ok' : 'failed'));
302 if (!($fd = @fopen
($filename, 'a'))) return false;
303 if (flock($fd, LOCK_EX
) && ftruncate($fd, 0)) {
304 if (fwrite( $fd, $contents )) $ok = true;
307 @chmod
($filename,0644);
310 if ($debug)ADOConnection
::outp( " Failed acquiring lock for $filename<br>\n");