2003-10-19 21:05:23 +02:00
|
|
|
<?php
|
2004-07-10 09:19:40 +02:00
|
|
|
|
|
|
|
// security - hide paths
|
|
|
|
if (!defined('ADODB_DIR')) die();
|
|
|
|
|
2003-10-19 21:05:23 +02:00
|
|
|
global $ADODB_INCLUDED_CSV;
|
|
|
|
$ADODB_INCLUDED_CSV = 1;
|
|
|
|
|
|
|
|
/*
|
2004-08-15 20:45:20 +02:00
|
|
|
V4.52 10 Aug 2004 (c) 2000-2004 John Lim (jlim@natsoft.com.my). All rights reserved.
|
2003-10-19 21:05:23 +02:00
|
|
|
Released under both BSD license and Lesser GPL library license.
|
|
|
|
Whenever there is any discrepancy between the two licenses,
|
|
|
|
the BSD license will take precedence. See License.txt.
|
|
|
|
Set tabs to 4 for best viewing.
|
|
|
|
|
2004-07-10 09:19:40 +02:00
|
|
|
Latest version is available at http://adodb.sourceforge.net
|
2003-10-19 21:05:23 +02:00
|
|
|
|
|
|
|
Library for CSV serialization. This is used by the csv/proxy driver and is the
|
|
|
|
CacheExecute() serialization format.
|
|
|
|
|
|
|
|
==== NOTE ====
|
|
|
|
Format documented at http://php.weblogs.com/ADODB_CSV
|
|
|
|
==============
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* convert a recordset into special format
|
|
|
|
*
|
|
|
|
* @param rs the recordset
|
|
|
|
*
|
|
|
|
* @return the CSV formated data
|
|
|
|
*/
|
|
|
|
function _rs2serialize(&$rs,$conn=false,$sql='')
|
|
|
|
{
|
|
|
|
$max = ($rs) ? $rs->FieldCount() : 0;
|
|
|
|
|
|
|
|
if ($sql) $sql = urlencode($sql);
|
|
|
|
// metadata setup
|
|
|
|
|
|
|
|
if ($max <= 0 || $rs->dataProvider == 'empty') { // is insert/update/delete
|
|
|
|
if (is_object($conn)) {
|
|
|
|
$sql .= ','.$conn->Affected_Rows();
|
|
|
|
$sql .= ','.$conn->Insert_ID();
|
|
|
|
} else
|
|
|
|
$sql .= ',,';
|
|
|
|
|
|
|
|
$text = "====-1,0,$sql\n";
|
|
|
|
return $text;
|
|
|
|
}
|
2004-06-08 16:09:55 +02:00
|
|
|
$tt = ($rs->timeCreated) ? $rs->timeCreated : time();
|
2003-10-19 21:05:23 +02:00
|
|
|
|
2004-06-08 16:09:55 +02:00
|
|
|
## changed format from ====0 to ====1
|
|
|
|
$line = "====1,$tt,$sql\n";
|
2003-10-19 21:05:23 +02:00
|
|
|
|
|
|
|
if ($rs->databaseType == 'array') {
|
2004-06-08 16:09:55 +02:00
|
|
|
$rows =& $rs->_array;
|
2003-10-19 21:05:23 +02:00
|
|
|
} else {
|
|
|
|
$rows = array();
|
|
|
|
while (!$rs->EOF) {
|
|
|
|
$rows[] = $rs->fields;
|
|
|
|
$rs->MoveNext();
|
|
|
|
}
|
|
|
|
}
|
2004-06-08 16:09:55 +02:00
|
|
|
|
|
|
|
for($i=0; $i < $max; $i++) {
|
|
|
|
$o =& $rs->FetchField($i);
|
|
|
|
$flds[] = $o;
|
|
|
|
}
|
|
|
|
|
|
|
|
$rs =& new ADORecordSet_array();
|
|
|
|
$rs->InitArrayFields($rows,$flds);
|
|
|
|
return $line.serialize($rs);
|
2003-10-19 21:05:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Open CSV file and convert it into Data.
|
|
|
|
*
|
|
|
|
* @param url file/ftp/http url
|
|
|
|
* @param err returns the error message
|
|
|
|
* @param timeout dispose if recordset has been alive for $timeout secs
|
|
|
|
*
|
|
|
|
* @return recordset, or false if error occured. If no
|
|
|
|
* error occurred in sql INSERT/UPDATE/DELETE,
|
|
|
|
* empty recordset is returned
|
|
|
|
*/
|
|
|
|
function &csv2rs($url,&$err,$timeout=0)
|
|
|
|
{
|
|
|
|
$err = false;
|
2004-07-10 09:19:40 +02:00
|
|
|
$fp = @fopen($url,'rb');
|
2003-10-19 21:05:23 +02:00
|
|
|
if (!$fp) {
|
|
|
|
$err = $url.' file/URL not found';
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
flock($fp, LOCK_SH);
|
|
|
|
$arr = array();
|
|
|
|
$ttl = 0;
|
|
|
|
|
2004-06-08 16:09:55 +02:00
|
|
|
if ($meta = fgetcsv($fp, 32000, ",")) {
|
2003-10-19 21:05:23 +02:00
|
|
|
// check if error message
|
2004-06-08 16:09:55 +02:00
|
|
|
if (strncmp($meta[0],'****',4) === 0) {
|
2003-10-19 21:05:23 +02:00
|
|
|
$err = trim(substr($meta[0],4,1024));
|
|
|
|
fclose($fp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// check for meta data
|
|
|
|
// $meta[0] is -1 means return an empty recordset
|
|
|
|
// $meta[1] contains a time
|
|
|
|
|
2004-06-08 16:09:55 +02:00
|
|
|
if (strncmp($meta[0], '====',4) === 0) {
|
2003-10-19 21:05:23 +02:00
|
|
|
|
|
|
|
if ($meta[0] == "====-1") {
|
|
|
|
if (sizeof($meta) < 5) {
|
|
|
|
$err = "Corrupt first line for format -1";
|
|
|
|
fclose($fp);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
fclose($fp);
|
|
|
|
|
|
|
|
if ($timeout > 0) {
|
|
|
|
$err = " Illegal Timeout $timeout ";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$rs->fields = array();
|
|
|
|
$rs->timeCreated = $meta[1];
|
2004-06-08 16:09:55 +02:00
|
|
|
$rs =& new ADORecordSet($val=true);
|
2003-10-19 21:05:23 +02:00
|
|
|
$rs->EOF = true;
|
|
|
|
$rs->_numOfFields=0;
|
|
|
|
$rs->sql = urldecode($meta[2]);
|
|
|
|
$rs->affectedrows = (integer)$meta[3];
|
|
|
|
$rs->insertid = $meta[4];
|
|
|
|
return $rs;
|
2004-06-08 16:09:55 +02:00
|
|
|
}
|
2003-10-19 21:05:23 +02:00
|
|
|
# Under high volume loads, we want only 1 thread/process to _write_file
|
|
|
|
# so that we don't have 50 processes queueing to write the same data.
|
2004-06-08 16:09:55 +02:00
|
|
|
# We use probabilistic timeout, ahead of time.
|
2003-10-19 21:05:23 +02:00
|
|
|
#
|
2004-06-08 16:09:55 +02:00
|
|
|
# -4 sec before timeout, give processes 1/32 chance of timing out
|
|
|
|
# -2 sec before timeout, give processes 1/16 chance of timing out
|
|
|
|
# -1 sec after timeout give processes 1/4 chance of timing out
|
|
|
|
# +0 sec after timeout, give processes 100% chance of timing out
|
2003-10-19 21:05:23 +02:00
|
|
|
if (sizeof($meta) > 1) {
|
|
|
|
if($timeout >0){
|
2004-06-08 16:09:55 +02:00
|
|
|
$tdiff = (integer)( $meta[1]+$timeout - time());
|
2003-10-19 21:05:23 +02:00
|
|
|
if ($tdiff <= 2) {
|
|
|
|
switch($tdiff) {
|
2004-06-08 16:09:55 +02:00
|
|
|
case 4:
|
|
|
|
case 3:
|
|
|
|
if ((rand() & 31) == 0) {
|
|
|
|
fclose($fp);
|
|
|
|
$err = "Timeout 3";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2003-10-19 21:05:23 +02:00
|
|
|
case 2:
|
|
|
|
if ((rand() & 15) == 0) {
|
|
|
|
fclose($fp);
|
|
|
|
$err = "Timeout 2";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
if ((rand() & 3) == 0) {
|
|
|
|
fclose($fp);
|
|
|
|
$err = "Timeout 1";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
fclose($fp);
|
|
|
|
$err = "Timeout 0";
|
|
|
|
return false;
|
|
|
|
} // switch
|
|
|
|
|
|
|
|
} // if check flush cache
|
|
|
|
}// (timeout>0)
|
|
|
|
$ttl = $meta[1];
|
|
|
|
}
|
2004-06-08 16:09:55 +02:00
|
|
|
//================================================
|
|
|
|
// new cache format - use serialize extensively...
|
|
|
|
if ($meta[0] === '====1') {
|
|
|
|
// slurp in the data
|
|
|
|
$MAXSIZE = 128000;
|
|
|
|
|
|
|
|
$text = fread($fp,$MAXSIZE);
|
2004-07-10 09:19:40 +02:00
|
|
|
if (strlen($text)) {
|
2004-06-08 16:09:55 +02:00
|
|
|
while ($txt = fread($fp,$MAXSIZE)) {
|
|
|
|
$text .= $txt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fclose($fp);
|
2004-07-10 09:19:40 +02:00
|
|
|
$rs = unserialize($text);
|
2004-06-08 16:09:55 +02:00
|
|
|
if (is_object($rs)) $rs->timeCreated = $ttl;
|
2004-07-10 09:19:40 +02:00
|
|
|
else {
|
|
|
|
$err = "Unable to unserialize recordset";
|
|
|
|
//echo htmlspecialchars($text),' !--END--!<p>';
|
|
|
|
}
|
2004-06-08 16:09:55 +02:00
|
|
|
return $rs;
|
|
|
|
}
|
|
|
|
|
2003-10-19 21:05:23 +02:00
|
|
|
$meta = false;
|
2004-06-08 16:09:55 +02:00
|
|
|
$meta = fgetcsv($fp, 32000, ",");
|
2003-10-19 21:05:23 +02:00
|
|
|
if (!$meta) {
|
|
|
|
fclose($fp);
|
|
|
|
$err = "Unexpected EOF 1";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get Column definitions
|
|
|
|
$flds = array();
|
|
|
|
foreach($meta as $o) {
|
|
|
|
$o2 = explode(':',$o);
|
|
|
|
if (sizeof($o2)!=3) {
|
|
|
|
$arr[] = $meta;
|
|
|
|
$flds = false;
|
|
|
|
break;
|
|
|
|
}
|
2004-06-08 16:09:55 +02:00
|
|
|
$fld =& new ADOFieldObject();
|
2003-10-19 21:05:23 +02:00
|
|
|
$fld->name = urldecode($o2[0]);
|
|
|
|
$fld->type = $o2[1];
|
|
|
|
$fld->max_length = $o2[2];
|
|
|
|
$flds[] = $fld;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fclose($fp);
|
|
|
|
$err = "Recordset had unexpected EOF 2";
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// slurp in the data
|
|
|
|
$MAXSIZE = 128000;
|
|
|
|
|
|
|
|
$text = '';
|
|
|
|
while ($txt = fread($fp,$MAXSIZE)) {
|
|
|
|
$text .= $txt;
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($fp);
|
2004-06-08 16:09:55 +02:00
|
|
|
@$arr = unserialize($text);
|
2003-10-19 21:05:23 +02:00
|
|
|
//var_dump($arr);
|
|
|
|
if (!is_array($arr)) {
|
|
|
|
$err = "Recordset had unexpected EOF (in serialized recordset)";
|
|
|
|
if (get_magic_quotes_runtime()) $err .= ". Magic Quotes Runtime should be disabled!";
|
|
|
|
return false;
|
|
|
|
}
|
2004-06-08 16:09:55 +02:00
|
|
|
$rs =& new ADORecordSet_array();
|
2003-10-19 21:05:23 +02:00
|
|
|
$rs->timeCreated = $ttl;
|
|
|
|
$rs->InitArrayFields($arr,$flds);
|
|
|
|
return $rs;
|
|
|
|
}
|
2004-08-02 10:30:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Save a file $filename and its $contents (normally for caching) with file locking
|
|
|
|
*/
|
|
|
|
function adodb_write_file($filename, $contents,$debug=false)
|
|
|
|
{
|
|
|
|
# http://www.php.net/bugs.php?id=9203 Bug that flock fails on Windows
|
|
|
|
# So to simulate locking, we assume that rename is an atomic operation.
|
|
|
|
# First we delete $filename, then we create a $tempfile write to it and
|
|
|
|
# rename to the desired $filename. If the rename works, then we successfully
|
|
|
|
# modified the file exclusively.
|
|
|
|
# What a stupid need - having to simulate locking.
|
|
|
|
# Risks:
|
|
|
|
# 1. $tempfile name is not unique -- very very low
|
|
|
|
# 2. unlink($filename) fails -- ok, rename will fail
|
|
|
|
# 3. adodb reads stale file because unlink fails -- ok, $rs timeout occurs
|
|
|
|
# 4. another process creates $filename between unlink() and rename() -- ok, rename() fails and cache updated
|
|
|
|
if (strncmp(PHP_OS,'WIN',3) === 0) {
|
|
|
|
// skip the decimal place
|
|
|
|
$mtime = substr(str_replace(' ','_',microtime()),2);
|
|
|
|
// getmypid() actually returns 0 on Win98 - never mind!
|
|
|
|
$tmpname = $filename.uniqid($mtime).getmypid();
|
|
|
|
if (!($fd = fopen($tmpname,'a'))) return false;
|
|
|
|
$ok = ftruncate($fd,0);
|
|
|
|
if (!fwrite($fd,$contents)) $ok = false;
|
|
|
|
fclose($fd);
|
|
|
|
chmod($tmpname,0644);
|
|
|
|
// the tricky moment
|
|
|
|
@unlink($filename);
|
|
|
|
if (!@rename($tmpname,$filename)) {
|
|
|
|
unlink($tmpname);
|
|
|
|
$ok = false;
|
|
|
|
}
|
|
|
|
if (!$ok) {
|
|
|
|
if ($debug) ADOConnection::outp( " Rename $tmpname ".($ok? 'ok' : 'failed'));
|
|
|
|
}
|
|
|
|
return $ok;
|
|
|
|
}
|
|
|
|
if (!($fd = fopen($filename, 'a'))) return false;
|
|
|
|
if (flock($fd, LOCK_EX) && ftruncate($fd, 0)) {
|
|
|
|
$ok = fwrite( $fd, $contents );
|
|
|
|
fclose($fd);
|
|
|
|
chmod($filename,0644);
|
|
|
|
}else {
|
|
|
|
fclose($fd);
|
|
|
|
if ($debug)ADOConnection::outp( " Failed acquiring lock for $filename<br>\n");
|
|
|
|
$ok = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $ok;
|
|
|
|
}
|
2003-10-19 21:05:23 +02:00
|
|
|
?>
|