3 // security - hide paths
4 if (!defined('ADODB_DIR')) die();
6 global $ADODB_INCLUDED_CSV;
7 $ADODB_INCLUDED_CSV = 1;
11 V4.94 23 Jan 2007 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
12 Released under both BSD license and Lesser GPL library license.
13 Whenever there is any discrepancy between the two licenses,
14 the BSD license will take precedence. See License.txt.
15 Set tabs to 4 for best viewing.
17 Latest version is available at http://adodb.sourceforge.net
19 Library for CSV serialization. This is used by the csv/proxy driver and is the
20 CacheExecute() serialization format.
23 Format documented at http://php.weblogs.com/ADODB_CSV
28 * convert a recordset into special format
30 * @param rs the recordset
32 * @return the CSV formated data
34 function _rs2serialize(&$rs,$conn=false,$sql='')
36 $max = ($rs) ?
$rs->FieldCount() : 0;
38 if ($sql) $sql = urlencode($sql);
41 if ($max <= 0 ||
$rs->dataProvider
== 'empty') { // is insert/update/delete
42 if (is_object($conn)) {
43 $sql .= ','.$conn->Affected_Rows();
44 $sql .= ','.$conn->Insert_ID();
48 $text = "====-1,0,$sql\n";
51 $tt = ($rs->timeCreated
) ?
$rs->timeCreated
: time();
53 ## changed format from ====0 to ====1
54 $line = "====1,$tt,$sql\n";
56 if ($rs->databaseType
== 'array') {
61 $rows[] = $rs->fields
;
66 for($i=0; $i < $max; $i++
) {
67 $o =& $rs->FetchField($i);
71 $savefetch = isset($rs->adodbFetchMode
) ?
$rs->adodbFetchMode
: $rs->fetchMode
;
72 $class = $rs->connection
->arrayClass
;
75 $rs2->oldProvider
= $rs->dataProvider
;
76 $rs2->InitArrayFields($rows,$flds);
77 $rs2->fetchMode
= $savefetch;
78 return $line.serialize($rs2);
83 * Open CSV file and convert it into Data.
85 * @param url file/ftp/http url
86 * @param err returns the error message
87 * @param timeout dispose if recordset has been alive for $timeout secs
89 * @return recordset, or false if error occured. If no
90 * error occurred in sql INSERT/UPDATE/DELETE,
91 * empty recordset is returned
93 function &csv2rs($url,&$err,$timeout=0, $rsclass='ADORecordSet_array')
97 $fp = @fopen
($url,'rb');
99 $err = $url.' file/URL not found';
102 @flock
($fp, LOCK_SH
);
106 if ($meta = fgetcsv($fp, 32000, ",")) {
107 // check if error message
108 if (strncmp($meta[0],'****',4) === 0) {
109 $err = trim(substr($meta[0],4,1024));
113 // check for meta data
114 // $meta[0] is -1 means return an empty recordset
115 // $meta[1] contains a time
117 if (strncmp($meta[0], '====',4) === 0) {
119 if ($meta[0] == "====-1") {
120 if (sizeof($meta) < 5) {
121 $err = "Corrupt first line for format -1";
128 $err = " Illegal Timeout $timeout ";
132 $rs = new $rsclass($val=true);
133 $rs->fields
= array();
134 $rs->timeCreated
= $meta[1];
136 $rs->_numOfFields
= 0;
137 $rs->sql
= urldecode($meta[2]);
138 $rs->affectedrows
= (integer)$meta[3];
139 $rs->insertid
= $meta[4];
142 # Under high volume loads, we want only 1 thread/process to _write_file
143 # so that we don't have 50 processes queueing to write the same data.
144 # We use probabilistic timeout, ahead of time.
146 # -4 sec before timeout, give processes 1/32 chance of timing out
147 # -2 sec before timeout, give processes 1/16 chance of timing out
148 # -1 sec after timeout give processes 1/4 chance of timing out
149 # +0 sec after timeout, give processes 100% chance of timing out
150 if (sizeof($meta) > 1) {
152 $tdiff = (integer)( $meta[1]+
$timeout - time());
157 if ((rand() & 31) == 0) {
164 if ((rand() & 15) == 0) {
171 if ((rand() & 3) == 0) {
183 } // if check flush cache
187 //================================================
188 // new cache format - use serialize extensively...
189 if ($meta[0] === '====1') {
193 $text = fread($fp,$MAXSIZE);
195 while ($txt = fread($fp,$MAXSIZE)) {
200 $rs = unserialize($text);
201 if (is_object($rs)) $rs->timeCreated
= $ttl;
203 $err = "Unable to unserialize recordset";
204 //echo htmlspecialchars($text),' !--END--!<p>';
210 $meta = fgetcsv($fp, 32000, ",");
213 $err = "Unexpected EOF 1";
218 // Get Column definitions
220 foreach($meta as $o) {
221 $o2 = explode(':',$o);
222 if (sizeof($o2)!=3) {
227 $fld = new ADOFieldObject();
228 $fld->name
= urldecode($o2[0]);
230 $fld->max_length
= $o2[2];
235 $err = "Recordset had unexpected EOF 2";
243 while ($txt = fread($fp,$MAXSIZE)) {
248 @$arr = unserialize($text);
250 if (!is_array($arr)) {
251 $err = "Recordset had unexpected EOF (in serialized recordset)";
252 if (get_magic_quotes_runtime()) $err .= ". Magic Quotes Runtime should be disabled!";
255 $rs = new $rsclass();
256 $rs->timeCreated
= $ttl;
257 $rs->InitArrayFields($arr,$flds);
263 * Save a file $filename and its $contents (normally for caching) with file locking
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,'a'))) return false;
284 $ok = ftruncate($fd,0);
285 if (!fwrite($fd,$contents)) $ok = false;
287 chmod($tmpname,0644);
290 if (!@rename
($tmpname,$filename)) {
295 if ($debug) ADOConnection
::outp( " Rename $tmpname ".($ok?
'ok' : 'failed'));
299 if (!($fd = @fopen
($filename, 'a'))) return false;
300 if (flock($fd, LOCK_EX
) && ftruncate($fd, 0)) {
301 $ok = fwrite( $fd, $contents );
303 chmod($filename,0644);
306 if ($debug)ADOConnection
::outp( " Failed acquiring lock for $filename<br>\n");