Support new security model in the formSubmit function - bug fix
[openemr.git] / library / adodb / drivers / adodb-ads.inc.php
blob0de57ca7a01e7475c14b3a24c4fddc501bd8690c
1 <?php
2 /*
3 (c) 2000-2007 John Lim (jlim#natsoft.com.my). All rights reserved.
4 Portions Copyright (c) 2007-2009, iAnywhere Solutions, Inc.
5 All rights reserved. All unpublished rights reserved.
7 Released under both BSD license and Lesser GPL library license.
8 Whenever there is any discrepancy between the two licenses,
9 the BSD license will take precedence.
11 Set tabs to 4 for best viewing.
14 NOTE: This driver requires the Advantage PHP client libraries, which
15 can be downloaded for free via:
16 http://devzone.advantagedatabase.com/dz/content.aspx?key=20
18 DELPHI FOR PHP USERS:
19 The following steps can be taken to utilize this driver from the
20 CodeGear Delphi for PHP product:
21 1 - See note above, download and install the Advantage PHP client.
22 2 - Copy the following files to the Delphi for PHP\X.X\php\ext directory:
23 ace32.dll
24 axcws32.dll
25 adsloc32.dll
26 php_advantage.dll (rename the existing php_advantage.dll.5.x.x file)
27 3 - Add the following line to the Delphi for PHP\X.X\php\php.ini.template file:
28 extension=php_advantage.dll
29 4 - To use: enter "ads" as the DriverName on a connection component, and set
30 a Host property similar to "DataDirectory=c:\". See the Advantage PHP
31 help file topic for ads_connect for details on connection path options
32 and formatting.
33 5 - (optional) - Modify the Delphi for PHP\X.X\vcl\packages\database.packages.php
34 file and add ads to the list of strings returned when registering the
35 Database object's DriverName property.
38 // security - hide paths
39 if (!defined('ADODB_DIR')) die();
41 define("_ADODB_ADS_LAYER", 2 );
43 /*--------------------------------------------------------------------------------------
44 --------------------------------------------------------------------------------------*/
47 class ADODB_ads extends ADOConnection {
48 var $databaseType = "ads";
49 var $fmt = "'m-d-Y'";
50 var $fmtTimeStamp = "'Y-m-d H:i:s'";
51 var $concat_operator = '';
52 var $replaceQuote = "''"; // string to use to replace quotes
53 var $dataProvider = "ads";
54 var $hasAffectedRows = true;
55 var $binmode = ODBC_BINMODE_RETURN;
56 var $useFetchArray = false; // setting this to true will make array elements in FETCH_ASSOC mode case-sensitive
57 // breaking backward-compat
58 //var $longreadlen = 8000; // default number of chars to return for a Blob/Long field
59 var $_bindInputArray = false;
60 var $curmode = SQL_CUR_USE_DRIVER; // See sqlext.h, SQL_CUR_DEFAULT == SQL_CUR_USE_DRIVER == 2L
61 var $_genSeqSQL = "create table %s (id integer)";
62 var $_autocommit = true;
63 var $_haserrorfunctions = true;
64 var $_has_stupid_odbc_fetch_api_change = true;
65 var $_lastAffectedRows = 0;
66 var $uCaseTables = true; // for meta* functions, uppercase table names
69 function ADODB_ads()
71 $this->_haserrorfunctions = ADODB_PHPVER >= 0x4050;
72 $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
75 // returns true or false
76 function _connect($argDSN, $argUsername, $argPassword, $argDatabasename)
78 global $php_errormsg;
80 if (!function_exists('ads_connect')) return null;
82 if ($this->debug && $argDatabasename && $this->databaseType != 'vfp') {
83 ADOConnection::outp("For Advantage Connect(), $argDatabasename is not used. Place dsn in 1st parameter.");
85 if (isset($php_errormsg)) $php_errormsg = '';
86 if ($this->curmode === false) $this->_connectionID = ads_connect($argDSN,$argUsername,$argPassword);
87 else $this->_connectionID = ads_connect($argDSN,$argUsername,$argPassword,$this->curmode);
88 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
89 if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
91 return $this->_connectionID != false;
94 // returns true or false
95 function _pconnect($argDSN, $argUsername, $argPassword, $argDatabasename)
97 global $php_errormsg;
99 if (!function_exists('ads_connect')) return null;
101 if (isset($php_errormsg)) $php_errormsg = '';
102 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
103 if ($this->debug && $argDatabasename) {
104 ADOConnection::outp("For PConnect(), $argDatabasename is not used. Place dsn in 1st parameter.");
106 // print "dsn=$argDSN u=$argUsername p=$argPassword<br>"; flush();
107 if ($this->curmode === false) $this->_connectionID = ads_connect($argDSN,$argUsername,$argPassword);
108 else $this->_connectionID = ads_pconnect($argDSN,$argUsername,$argPassword,$this->curmode);
110 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
111 if ($this->_connectionID && $this->autoRollback) @ads_rollback($this->_connectionID);
112 if (isset($this->connectStmt)) $this->Execute($this->connectStmt);
114 return $this->_connectionID != false;
117 // returns the Server version and Description
118 function ServerInfo()
121 if (!empty($this->host) && ADODB_PHPVER >= 0x4300) {
122 $stmt = $this->Prepare('EXECUTE PROCEDURE sp_mgGetInstallInfo()');
123 $res = $this->Execute($stmt);
124 if(!$res)
125 print $this->ErrorMsg();
126 else{
127 $ret["version"]= $res->fields[3];
128 $ret["description"]="Advantage Database Server";
129 return $ret;
132 else {
133 return ADOConnection::ServerInfo();
138 // returns true or false
139 function CreateSequence( $seqname,$start=1)
141 $res = $this->Execute("CREATE TABLE $seqname ( ID autoinc( 1 ) ) IN DATABASE");
142 if(!$res){
143 print $this->ErrorMsg();
144 return false;
146 else
147 return true;
151 // returns true or false
152 function DropSequence($seqname)
154 $res = $this->Execute("DROP TABLE $seqname");
155 if(!$res){
156 print $this->ErrorMsg();
157 return false;
159 else
160 return true;
164 // returns the generated ID or false
165 // checks if the table already exists, else creates the table and inserts a record into the table
166 // and gets the ID number of the last inserted record.
167 function GenID($seqname,$start=1)
169 $go = $this->Execute("select * from $seqname");
170 if (!$go){
171 $res = $this->Execute("CREATE TABLE $seqname ( ID autoinc( 1 ) ) IN DATABASE");
172 if(!res){
173 print $this->ErrorMsg();
174 return false;
177 $res = $this->Execute("INSERT INTO $seqname VALUES( DEFAULT )");
178 if(!$res){
179 print $this->ErrorMsg();
180 return false;
182 else{
183 $gen = $this->Execute("SELECT LastAutoInc( STATEMENT ) FROM system.iota");
184 $ret = $gen->fields[0];
185 return $ret;
193 function ErrorMsg()
195 if ($this->_haserrorfunctions) {
196 if ($this->_errorMsg !== false) return $this->_errorMsg;
197 if (empty($this->_connectionID)) return @ads_errormsg();
198 return @ads_errormsg($this->_connectionID);
199 } else return ADOConnection::ErrorMsg();
203 function ErrorNo()
206 if ($this->_haserrorfunctions) {
207 if ($this->_errorCode !== false) {
208 // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
209 return (strlen($this->_errorCode)<=2) ? 0 : $this->_errorCode;
212 if (empty($this->_connectionID)) $e = @ads_error();
213 else $e = @ads_error($this->_connectionID);
215 // bug in 4.0.6, error number can be corrupted string (should be 6 digits)
216 // so we check and patch
217 if (strlen($e)<=2) return 0;
218 return $e;
219 } else return ADOConnection::ErrorNo();
224 function BeginTrans()
226 if (!$this->hasTransactions) return false;
227 if ($this->transOff) return true;
228 $this->transCnt += 1;
229 $this->_autocommit = false;
230 return ads_autocommit($this->_connectionID,false);
233 function CommitTrans($ok=true)
235 if ($this->transOff) return true;
236 if (!$ok) return $this->RollbackTrans();
237 if ($this->transCnt) $this->transCnt -= 1;
238 $this->_autocommit = true;
239 $ret = ads_commit($this->_connectionID);
240 ads_autocommit($this->_connectionID,true);
241 return $ret;
244 function RollbackTrans()
246 if ($this->transOff) return true;
247 if ($this->transCnt) $this->transCnt -= 1;
248 $this->_autocommit = true;
249 $ret = ads_rollback($this->_connectionID);
250 ads_autocommit($this->_connectionID,true);
251 return $ret;
255 // Returns tables,Views or both on succesfull execution. Returns
256 // tables by default on succesfull execustion.
257 function &MetaTables($ttype)
259 $recordSet1 = $this->Execute("select * from system.tables");
260 if(!$recordSet1){
261 print $this->ErrorMsg();
262 return false;
264 $recordSet2 = $this->Execute("select * from system.views");
265 if(!$recordSet2){
266 print $this->ErrorMsg();
267 return false;
269 $i=0;
270 while (!$recordSet1->EOF){
271 $arr["$i"] = $recordSet1->fields[0];
272 $recordSet1->MoveNext();
273 $i=$i+1;
275 if($ttype=='FALSE'){
276 while (!$recordSet2->EOF){
277 $arr["$i"] = $recordSet2->fields[0];
278 $recordSet2->MoveNext();
279 $i=$i+1;
281 return $arr;
283 elseif($ttype=='VIEWS'){
284 while (!$recordSet2->EOF){
285 $arrV["$i"] = $recordSet2->fields[0];
286 $recordSet2->MoveNext();
287 $i=$i+1;
289 return $arrV;
291 else{
292 return $arr;
297 function &MetaPrimaryKeys($table)
299 $recordSet = $this->Execute("select table_primary_key from system.tables where name='$table'");
300 if(!$recordSet){
301 print $this->ErrorMsg();
302 return false;
304 $i=0;
305 while (!$recordSet->EOF){
306 $arr["$i"] = $recordSet->fields[0];
307 $recordSet->MoveNext();
308 $i=$i+1;
310 return $arr;
314 See http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odbc/htm/odbcdatetime_data_type_changes.asp
315 / SQL data type codes /
316 #define SQL_UNKNOWN_TYPE 0
317 #define SQL_CHAR 1
318 #define SQL_NUMERIC 2
319 #define SQL_DECIMAL 3
320 #define SQL_INTEGER 4
321 #define SQL_SMALLINT 5
322 #define SQL_FLOAT 6
323 #define SQL_REAL 7
324 #define SQL_DOUBLE 8
325 #if (ODBCVER >= 0x0300)
326 #define SQL_DATETIME 9
327 #endif
328 #define SQL_VARCHAR 12
331 / One-parameter shortcuts for date/time data types /
332 #if (ODBCVER >= 0x0300)
333 #define SQL_TYPE_DATE 91
334 #define SQL_TYPE_TIME 92
335 #define SQL_TYPE_TIMESTAMP 93
337 #define SQL_UNICODE (-95)
338 #define SQL_UNICODE_VARCHAR (-96)
339 #define SQL_UNICODE_LONGVARCHAR (-97)
341 function ODBCTypes($t)
343 switch ((integer)$t) {
344 case 1:
345 case 12:
346 case 0:
347 case -95:
348 case -96:
349 return 'C';
350 case -97:
351 case -1: //text
352 return 'X';
353 case -4: //image
354 return 'B';
356 case 9:
357 case 91:
358 return 'D';
360 case 10:
361 case 11:
362 case 92:
363 case 93:
364 return 'T';
366 case 4:
367 case 5:
368 case -6:
369 return 'I';
371 case -11: // uniqidentifier
372 return 'R';
373 case -7: //bit
374 return 'L';
376 default:
377 return 'N';
381 function &MetaColumns($table)
383 global $ADODB_FETCH_MODE;
385 $false = false;
386 if ($this->uCaseTables) $table = strtoupper($table);
387 $schema = '';
388 $this->_findschema($table,$schema);
390 $savem = $ADODB_FETCH_MODE;
391 $ADODB_FETCH_MODE = ADODB_FETCH_NUM;
393 /*if (false) { // after testing, confirmed that the following does not work becoz of a bug
394 $qid2 = ads_tables($this->_connectionID);
395 $rs = new ADORecordSet_ads($qid2);
396 $ADODB_FETCH_MODE = $savem;
397 if (!$rs) return false;
398 $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
399 $rs->_fetch();
401 while (!$rs->EOF) {
402 if ($table == strtoupper($rs->fields[2])) {
403 $q = $rs->fields[0];
404 $o = $rs->fields[1];
405 break;
407 $rs->MoveNext();
409 $rs->Close();
411 $qid = ads_columns($this->_connectionID,$q,$o,strtoupper($table),'%');
412 } */
414 switch ($this->databaseType) {
415 case 'access':
416 case 'vfp':
417 $qid = ads_columns($this->_connectionID);#,'%','',strtoupper($table),'%');
418 break;
421 case 'db2':
422 $colname = "%";
423 $qid = ads_columns($this->_connectionID, "", $schema, $table, $colname);
424 break;
426 default:
427 $qid = @ads_columns($this->_connectionID,'%','%',strtoupper($table),'%');
428 if (empty($qid)) $qid = ads_columns($this->_connectionID);
429 break;
431 if (empty($qid)) return $false;
433 $rs = new ADORecordSet_ads($qid);
434 $ADODB_FETCH_MODE = $savem;
436 if (!$rs) return $false;
437 $rs->_has_stupid_odbc_fetch_api_change = $this->_has_stupid_odbc_fetch_api_change;
438 $rs->_fetch();
440 $retarr = array();
443 $rs->fields indices
444 0 TABLE_QUALIFIER
445 1 TABLE_SCHEM
446 2 TABLE_NAME
447 3 COLUMN_NAME
448 4 DATA_TYPE
449 5 TYPE_NAME
450 6 PRECISION
451 7 LENGTH
452 8 SCALE
453 9 RADIX
454 10 NULLABLE
455 11 REMARKS
457 while (!$rs->EOF) {
458 // adodb_pr($rs->fields);
459 if (strtoupper(trim($rs->fields[2])) == $table && (!$schema || strtoupper($rs->fields[1]) == $schema)) {
460 $fld = new ADOFieldObject();
461 $fld->name = $rs->fields[3];
462 $fld->type = $this->ODBCTypes($rs->fields[4]);
464 // ref: http://msdn.microsoft.com/library/default.asp?url=/archive/en-us/dnaraccgen/html/msdn_odk.asp
465 // access uses precision to store length for char/varchar
466 if ($fld->type == 'C' or $fld->type == 'X') {
467 if ($this->databaseType == 'access')
468 $fld->max_length = $rs->fields[6];
469 else if ($rs->fields[4] <= -95) // UNICODE
470 $fld->max_length = $rs->fields[7]/2;
471 else
472 $fld->max_length = $rs->fields[7];
473 } else
474 $fld->max_length = $rs->fields[7];
475 $fld->not_null = !empty($rs->fields[10]);
476 $fld->scale = $rs->fields[8];
477 $retarr[strtoupper($fld->name)] = $fld;
478 } else if (sizeof($retarr)>0)
479 break;
480 $rs->MoveNext();
482 $rs->Close(); //-- crashes 4.03pl1 -- why?
484 if (empty($retarr)) $retarr = false;
485 return $retarr;
488 // Returns an array of columns names for a given table
489 function &MetaColumnNames($table)
491 $recordSet = $this->Execute("select name from system.columns where parent='$table'");
492 if(!$recordSet){
493 print $this->ErrorMsg();
494 return false;
496 else{
497 $i=0;
498 while (!$recordSet->EOF){
499 $arr["FIELD$i"] = $recordSet->fields[0];
500 $recordSet->MoveNext();
501 $i=$i+1;
503 return $arr;
508 function Prepare($sql)
510 if (! $this->_bindInputArray) return $sql; // no binding
511 $stmt = ads_prepare($this->_connectionID,$sql);
512 if (!$stmt) {
513 // we don't know whether odbc driver is parsing prepared stmts, so just return sql
514 return $sql;
516 return array($sql,$stmt,false);
519 /* returns queryID or false */
520 function _query($sql,$inputarr=false)
522 GLOBAL $php_errormsg;
523 if (isset($php_errormsg)) $php_errormsg = '';
524 $this->_error = '';
526 if ($inputarr) {
527 if (is_array($sql)) {
528 $stmtid = $sql[1];
529 } else {
530 $stmtid = ads_prepare($this->_connectionID,$sql);
532 if ($stmtid == false) {
533 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
534 return false;
538 if (! ads_execute($stmtid,$inputarr)) {
539 //@ads_free_result($stmtid);
540 if ($this->_haserrorfunctions) {
541 $this->_errorMsg = ads_errormsg();
542 $this->_errorCode = ads_error();
544 return false;
547 } else if (is_array($sql)) {
548 $stmtid = $sql[1];
549 if (!ads_execute($stmtid)) {
550 //@ads_free_result($stmtid);
551 if ($this->_haserrorfunctions) {
552 $this->_errorMsg = ads_errormsg();
553 $this->_errorCode = ads_error();
555 return false;
557 } else
560 $stmtid = ads_exec($this->_connectionID,$sql);
564 $this->_lastAffectedRows = 0;
566 if ($stmtid)
569 if (@ads_num_fields($stmtid) == 0) {
570 $this->_lastAffectedRows = ads_num_rows($stmtid);
571 $stmtid = true;
573 } else {
575 $this->_lastAffectedRows = 0;
576 ads_binmode($stmtid,$this->binmode);
577 ads_longreadlen($stmtid,$this->maxblobsize);
581 if ($this->_haserrorfunctions)
584 $this->_errorMsg = '';
585 $this->_errorCode = 0;
587 else
588 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
590 else
592 if ($this->_haserrorfunctions) {
593 $this->_errorMsg = ads_errormsg();
594 $this->_errorCode = ads_error();
595 } else
596 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
599 return $stmtid;
604 Insert a null into the blob field of the table first.
605 Then use UpdateBlob to store the blob.
607 Usage:
609 $conn->Execute('INSERT INTO blobtable (id, blobcol) VALUES (1, null)');
610 $conn->UpdateBlob('blobtable','blobcol',$blob,'id=1');
612 function UpdateBlob($table,$column,$val,$where,$blobtype='BLOB')
614 $sql = "UPDATE $table SET $column=? WHERE $where";
615 $stmtid = ads_prepare($this->_connectionID,$sql);
616 if ($stmtid == false){
617 $this->_errorMsg = isset($php_errormsg) ? $php_errormsg : '';
618 return false;
620 if (! ads_execute($stmtid,array($val),array(SQL_BINARY) )){
621 if ($this->_haserrorfunctions){
622 $this->_errorMsg = ads_errormsg();
623 $this->_errorCode = ads_error();
625 return false;
627 return TRUE;
630 // returns true or false
631 function _close()
633 $ret = @ads_close($this->_connectionID);
634 $this->_connectionID = false;
635 return $ret;
638 function _affectedrows()
640 return $this->_lastAffectedRows;
645 /*--------------------------------------------------------------------------------------
646 Class Name: Recordset
647 --------------------------------------------------------------------------------------*/
649 class ADORecordSet_ads extends ADORecordSet {
651 var $bind = false;
652 var $databaseType = "ads";
653 var $dataProvider = "ads";
654 var $useFetchArray;
655 var $_has_stupid_odbc_fetch_api_change;
657 function ADORecordSet_ads($id,$mode=false)
659 if ($mode === false) {
660 global $ADODB_FETCH_MODE;
661 $mode = $ADODB_FETCH_MODE;
663 $this->fetchMode = $mode;
665 $this->_queryID = $id;
667 // the following is required for mysql odbc driver in 4.3.1 -- why?
668 $this->EOF = false;
669 $this->_currentRow = -1;
670 //$this->ADORecordSet($id);
674 // returns the field object
675 function &FetchField($fieldOffset = -1)
678 $off=$fieldOffset+1; // offsets begin at 1
680 $o= new ADOFieldObject();
681 $o->name = @ads_field_name($this->_queryID,$off);
682 $o->type = @ads_field_type($this->_queryID,$off);
683 $o->max_length = @ads_field_len($this->_queryID,$off);
684 if (ADODB_ASSOC_CASE == 0) $o->name = strtolower($o->name);
685 else if (ADODB_ASSOC_CASE == 1) $o->name = strtoupper($o->name);
686 return $o;
689 /* Use associative array to get fields array */
690 function Fields($colname)
692 if ($this->fetchMode & ADODB_FETCH_ASSOC) return $this->fields[$colname];
693 if (!$this->bind) {
694 $this->bind = array();
695 for ($i=0; $i < $this->_numOfFields; $i++) {
696 $o = $this->FetchField($i);
697 $this->bind[strtoupper($o->name)] = $i;
701 return $this->fields[$this->bind[strtoupper($colname)]];
705 function _initrs()
707 global $ADODB_COUNTRECS;
708 $this->_numOfRows = ($ADODB_COUNTRECS) ? @ads_num_rows($this->_queryID) : -1;
709 $this->_numOfFields = @ads_num_fields($this->_queryID);
710 // some silly drivers such as db2 as/400 and intersystems cache return _numOfRows = 0
711 if ($this->_numOfRows == 0) $this->_numOfRows = -1;
712 //$this->useFetchArray = $this->connection->useFetchArray;
713 $this->_has_stupid_odbc_fetch_api_change = ADODB_PHPVER >= 0x4200;
716 function _seek($row)
718 return false;
721 // speed up SelectLimit() by switching to ADODB_FETCH_NUM as ADODB_FETCH_ASSOC is emulated
722 function &GetArrayLimit($nrows,$offset=-1)
724 if ($offset <= 0) {
725 $rs =& $this->GetArray($nrows);
726 return $rs;
728 $savem = $this->fetchMode;
729 $this->fetchMode = ADODB_FETCH_NUM;
730 $this->Move($offset);
731 $this->fetchMode = $savem;
733 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
734 $this->fields =& $this->GetRowAssoc(ADODB_ASSOC_CASE);
737 $results = array();
738 $cnt = 0;
739 while (!$this->EOF && $nrows != $cnt) {
740 $results[$cnt++] = $this->fields;
741 $this->MoveNext();
744 return $results;
748 function MoveNext()
750 if ($this->_numOfRows != 0 && !$this->EOF) {
751 $this->_currentRow++;
753 if ($this->_has_stupid_odbc_fetch_api_change)
754 $rez = @ads_fetch_into($this->_queryID,$this->fields);
755 else {
756 $row = 0;
757 $rez = @ads_fetch_into($this->_queryID,$row,$this->fields);
759 if ($rez) {
760 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
761 $this->fields =& $this->GetRowAssoc(ADODB_ASSOC_CASE);
763 return true;
766 $this->fields = false;
767 $this->EOF = true;
768 return false;
771 function _fetch()
774 if ($this->_has_stupid_odbc_fetch_api_change)
775 $rez = @ads_fetch_into($this->_queryID,$this->fields);
776 else {
777 $row = 0;
778 $rez = @ads_fetch_into($this->_queryID,$row,$this->fields);
780 if ($rez) {
781 if ($this->fetchMode & ADODB_FETCH_ASSOC) {
782 $this->fields =& $this->GetRowAssoc(ADODB_ASSOC_CASE);
784 return true;
786 $this->fields = false;
787 return false;
790 function _close()
792 return @ads_free_result($this->_queryID);