MDL-71669 editor_atto: Fire custom event when toggling button highlight
[moodle.git] / lib / adodb / adodb-csvlib.inc.php
blobf1412582510cac661a8e5be13b10f38069428cb4
1 <?php
3 // security - hide paths
4 if (!defined('ADODB_DIR')) die();
6 global $ADODB_INCLUDED_CSV;
7 $ADODB_INCLUDED_CSV = 1;
9 /*
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.
25 /**
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);
37 // metadata setup
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();
43 } else
44 $sql .= ',,';
46 $text = "====-1,0,$sql\n";
47 return $text;
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') {
55 $rows = $rs->_array;
56 } else {
57 $rows = array();
58 while (!$rs->EOF) {
59 $rows[] = $rs->fields;
60 $rs->MoveNext();
64 for($i=0; $i < $max; $i++) {
65 $o = $rs->FetchField($i);
66 $flds[] = $o;
69 $savefetch = isset($rs->adodbFetchMode) ? $rs->adodbFetchMode : $rs->fetchMode;
70 $class = $rs->connection->arrayClass;
71 $rs2 = new $class();
72 $rs2->timeCreated = $rs->timeCreated; # memcache fix
73 $rs2->sql = $rs->sql;
74 $rs2->oldProvider = $rs->dataProvider;
75 $rs2->InitArrayFields($rows,$flds);
76 $rs2->fetchMode = $savefetch;
77 return $line.serialize($rs2);
81 /**
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')
94 $false = false;
95 $err = false;
96 $fp = @fopen($url,'rb');
97 if (!$fp) {
98 $err = $url.' file/URL not found';
99 return $false;
101 @flock($fp, LOCK_SH);
102 $arr = array();
103 $ttl = 0;
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));
109 fclose($fp);
110 return $false;
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";
121 fclose($fp);
122 return $false;
124 fclose($fp);
126 if ($timeout > 0) {
127 $err = " Illegal Timeout $timeout ";
128 return $false;
131 $rs = new $rsclass($val=true);
132 $rs->fields = array();
133 $rs->timeCreated = $meta[1];
134 $rs->EOF = true;
135 $rs->_numOfFields = 0;
136 $rs->sql = urldecode($meta[2]);
137 $rs->affectedrows = (integer)$meta[3];
138 $rs->insertid = $meta[4];
139 return $rs;
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) {
150 if($timeout >0){
151 $tdiff = (integer)( $meta[1]+$timeout - time());
152 if ($tdiff <= 2) {
153 switch($tdiff) {
154 case 4:
155 case 3:
156 if ((rand() & 31) == 0) {
157 fclose($fp);
158 $err = "Timeout 3";
159 return $false;
161 break;
162 case 2:
163 if ((rand() & 15) == 0) {
164 fclose($fp);
165 $err = "Timeout 2";
166 return $false;
168 break;
169 case 1:
170 if ((rand() & 3) == 0) {
171 fclose($fp);
172 $err = "Timeout 1";
173 return $false;
175 break;
176 default:
177 fclose($fp);
178 $err = "Timeout 0";
179 return $false;
180 } // switch
182 } // if check flush cache
183 }// (timeout>0)
184 $ttl = $meta[1];
186 //================================================
187 // new cache format - use serialize extensively...
188 if ($meta[0] === '====1') {
189 // slurp in the data
190 $MAXSIZE = 128000;
192 $text = fread($fp,$MAXSIZE);
193 if (strlen($text)) {
194 while ($txt = fread($fp,$MAXSIZE)) {
195 $text .= $txt;
198 fclose($fp);
199 $rs = unserialize($text);
200 if (is_object($rs)) $rs->timeCreated = $ttl;
201 else {
202 $err = "Unable to unserialize recordset";
203 //echo htmlspecialchars($text),' !--END--!<p>';
205 return $rs;
208 $meta = false;
209 $meta = fgetcsv($fp, 32000, ",");
210 if (!$meta) {
211 fclose($fp);
212 $err = "Unexpected EOF 1";
213 return $false;
217 // Get Column definitions
218 $flds = array();
219 foreach($meta as $o) {
220 $o2 = explode(':',$o);
221 if (sizeof($o2)!=3) {
222 $arr[] = $meta;
223 $flds = false;
224 break;
226 $fld = new ADOFieldObject();
227 $fld->name = urldecode($o2[0]);
228 $fld->type = $o2[1];
229 $fld->max_length = $o2[2];
230 $flds[] = $fld;
232 } else {
233 fclose($fp);
234 $err = "Recordset had unexpected EOF 2";
235 return $false;
238 // slurp in the data
239 $MAXSIZE = 128000;
241 $text = '';
242 while ($txt = fread($fp,$MAXSIZE)) {
243 $text .= $txt;
246 fclose($fp);
247 @$arr = unserialize($text);
248 //var_dump($arr);
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!";
252 return $false;
254 $rs = new $rsclass();
255 $rs->timeCreated = $ttl;
256 $rs->InitArrayFields($arr,$flds);
257 return $rs;
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.
273 # Risks:
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;
285 else $ok = false;
286 fclose($fd);
288 if ($ok) {
289 @chmod($tmpname,0644);
290 // the tricky moment
291 @unlink($filename);
292 if (!@rename($tmpname,$filename)) {
293 @unlink($tmpname);
294 $ok = 0;
296 if (!$ok) {
297 if ($debug) ADOConnection::outp( " Rename $tmpname ".($ok? 'ok' : 'failed'));
300 return $ok;
302 if (!($fd = @fopen($filename, 'a'))) return false;
303 if (flock($fd, LOCK_EX) && ftruncate($fd, 0)) {
304 if (fwrite( $fd, $contents )) $ok = true;
305 else $ok = false;
306 fclose($fd);
307 @chmod($filename,0644);
308 }else {
309 fclose($fd);
310 if ($debug)ADOConnection::outp( " Failed acquiring lock for $filename<br>\n");
311 $ok = false;
314 return $ok;