diff --git a/lib/adodb/adodb-active-record.inc.php b/lib/adodb/adodb-active-record.inc.php index ae58564ab24..a7f0adf7f78 100644 --- a/lib/adodb/adodb-active-record.inc.php +++ b/lib/adodb/adodb-active-record.inc.php @@ -1,7 +1,7 @@ _GenFields($flds); // genfields can return FALSE at times if ($lines == null) $lines = array(); - list(,$first) = each($lines); + $first = current($lines); list(,$column_def) = preg_split("/[\t ]+/",$first,2); } return array(sprintf($this->renameColumn,$tabname,$this->NameQuote($oldcolumn),$this->NameQuote($newcolumn),$column_def)); diff --git a/lib/adodb/adodb-error.inc.php b/lib/adodb/adodb-error.inc.php index 3f2ab90cbfa..18f944b7029 100644 --- a/lib/adodb/adodb-error.inc.php +++ b/lib/adodb/adodb-error.inc.php @@ -1,6 +1,6 @@ DB_ERROR_SERIALIZATION_FAILURE ); reset($error_regexps); - while (list($regexp,$code) = each($error_regexps)) { + foreach ($error_regexps as $regexp => $code) { if (preg_match("/$regexp/mi", $errormsg)) { return $code; } diff --git a/lib/adodb/adodb-errorhandler.inc.php b/lib/adodb/adodb-errorhandler.inc.php index e8dcaab32d3..7f36ba1ee22 100644 --- a/lib/adodb/adodb-errorhandler.inc.php +++ b/lib/adodb/adodb-errorhandler.inc.php @@ -1,6 +1,6 @@ RecordCount() is used. @@ -1164,8 +1164,7 @@ if (!defined('_ADODB_LAYER')) { foreach($inputarr as $arr) { $sql = ''; $i = 0; - //Use each() instead of foreach to reduce memory usage -mikefedyk - while(list(, $v) = each($arr)) { + foreach ($arr as $v) { $sql .= $sqlarr[$i]; // from Ron Baldwin // Only quote string types diff --git a/lib/adodb/datadict/datadict-access.inc.php b/lib/adodb/datadict/datadict-access.inc.php index 25a72a8d10f..c1459154375 100644 --- a/lib/adodb/datadict/datadict-access.inc.php +++ b/lib/adodb/datadict/datadict-access.inc.php @@ -1,7 +1,7 @@ connection->Execute( + "SELECT name FROM sys.default_constraints + WHERE object_name(parent_object_id) = '$tabname' + AND col_name(parent_object_id, parent_column_id) = '$colname'" + ); + if ( is_object($rs) ) { + $row = $rs->FetchRow(); + $constraintname = $row['name']; + } + return $constraintname; + } + + function AlterColumnSQL($tabname, $flds, $tableflds='',$tableoptions='') { $tabname = $this->TableName ($tabname); $sql = array(); - list($lines,$pkey) = $this->_GenFields($flds); - foreach($lines as $v) { - $sql[] = "ALTER TABLE $tabname $this->alterCol $v"; - } + list($lines,$pkey,$idxs) = $this->_GenFields($flds); + $alter = 'ALTER TABLE ' . $tabname . $this->alterCol . ' '; + foreach($lines as $v) { + $not_null = false; + if ($not_null = preg_match('/NOT NULL/i',$v)) { + $v = preg_replace('/NOT NULL/i','',$v); + } + if (preg_match('/^([^ ]+) .*DEFAULT (\'[^\']+\'|\"[^\"]+\"|[^ ]+)/',$v,$matches)) { + list(,$colname,$default) = $matches; + $v = preg_replace('/^' . preg_quote($colname) . '\s/', '', $v); + $t = trim(str_replace('DEFAULT '.$default,'',$v)); + if ( $constraintname = $this->DefaultConstraintname($tabname,$colname) ) { + $sql[] = 'ALTER TABLE '.$tabname.' DROP CONSTRAINT '. $constraintname; + } + if ($not_null) { + $sql[] = $alter . $colname . ' ' . $t . ' NOT NULL'; + } else { + $sql[] = $alter . $colname . ' ' . $t ; + } + $sql[] = 'ALTER TABLE ' . $tabname + . ' ADD CONSTRAINT DF__' . $tabname . '__' . $colname . '__' . dechex(rand()) + . ' DEFAULT ' . $default . ' FOR ' . $colname; + } else { + $colname = strtok($v," "); + if ( $constraintname = $this->DefaultConstraintname($tabname,$colname) ) { + $sql[] = 'ALTER TABLE '.$tabname.' DROP CONSTRAINT '. $constraintname; + } + if ($not_null) { + $sql[] = $alter . $v . ' NOT NULL'; + } else { + $sql[] = $alter . $v; + } + } + } + if (is_array($idxs)) { + foreach($idxs as $idx => $idxdef) { + $sql_idxs = $this->CreateIndexSql($idx, $tabname, $idxdef['cols'], $idxdef['opts']); + $sql = array_merge($sql, $sql_idxs); + } + } return $sql; } - */ + /** * Drop a column, syntax is ALTER TABLE table DROP COLUMN column,column @@ -176,10 +226,12 @@ class ADODB2_mssqlnative extends ADODB_DataDict { if (!is_array($flds)) $flds = explode(',',$flds); $f = array(); - $s = 'ALTER TABLE ' . $tabname . ' DROP COLUMN '; + $s = 'ALTER TABLE ' . $tabname; foreach($flds as $v) { - //$f[] = "\n$this->dropCol ".$this->NameQuote($v); - $f[] = $this->NameQuote($v); + if ( $constraintname = $this->DefaultConstraintname($tabname,$v) ) { + $sql[] = 'ALTER TABLE ' . $tabname . ' DROP CONSTRAINT ' . $constraintname; + } + $f[] = ' DROP COLUMN ' . $this->NameQuote($v); } $s .= implode(', ',$f); $sql[] = $s; diff --git a/lib/adodb/datadict/datadict-mysql.inc.php b/lib/adodb/datadict/datadict-mysql.inc.php index 01489152a88..00a43a2a10f 100644 --- a/lib/adodb/datadict/datadict-mysql.inc.php +++ b/lib/adodb/datadict/datadict-mysql.inc.php @@ -1,7 +1,7 @@ CommandText = $sql; $oCmd->CommandType = 1; - while(list(, $val) = each($inputarr)) { + foreach ($inputarr as $val) { $type = gettype($val); $len=strlen($val); if ($type == 'boolean') diff --git a/lib/adodb/drivers/adodb-ado_access.inc.php b/lib/adodb/drivers/adodb-ado_access.inc.php index fa1629b07f0..c167ce63ad7 100644 --- a/lib/adodb/drivers/adodb-ado_access.inc.php +++ b/lib/adodb/drivers/adodb-ado_access.inc.php @@ -1,6 +1,6 @@ ServerInfo(); - if (preg_match('/^09/',$data['version'])){ - /* - * SQL Server 2005 - */ - $this->mssql_version = 9; - } elseif (preg_match('/^10/',$data['version'])){ - /* - * SQL Server 2008 - */ - $this->mssql_version = 10; - } elseif (preg_match('/^11/',$data['version'])){ - /* - * SQL Server 2012 - */ - $this->mssql_version = 11; - } else + preg_match('/^\d{2}/', $data['version'], $matches); + $version = (int)reset($matches); + + // We only support SQL Server 2005 and up + if($version < 9) { die("SQL SERVER VERSION {$data['version']} NOT SUPPORTED IN mssqlnative DRIVER"); + } + + $this->mssql_version = $version; } function ServerInfo() { @@ -211,26 +209,26 @@ class ADODB_mssqlnative extends ADOConnection { switch($this->mssql_version){ case 9: case 10: - return $this->GenID2008(); + return $this->GenID2008($seq, $start); break; - case 11: - return $this->GenID2012(); + default: + return $this->GenID2012($seq, $start); break; } } function CreateSequence($seq='adodbseq',$start=1) { - if (!$this->mssql_vesion) + if (!$this->mssql_version) $this->ServerVersion(); switch($this->mssql_version){ case 9: case 10: - return $this->CreateSequence2008(); + return $this->CreateSequence2008($seq, $start); break; - case 11: - return $this->CreateSequence2012(); + default: + return $this->CreateSequence2012($seq, $start); break; } @@ -258,7 +256,7 @@ class ADODB_mssqlnative extends ADOConnection { /** * Proper Sequences Only available to Server 2012 and up */ - function CreateSequence2012($seq='adodb',$start=1){ + function CreateSequence2012($seq='adodbseq',$start=1){ if (!$this->sequences){ $sql = "SELECT name FROM sys.sequences"; $this->sequences = $this->GetCol($sql); @@ -289,7 +287,7 @@ class ADODB_mssqlnative extends ADOConnection { } $num = $this->GetOne("select id from $seq"); sqlsrv_commit($this->_connectionID); - return true; + return $num; } /** * Only available to Server 2012 and up @@ -313,7 +311,7 @@ class ADODB_mssqlnative extends ADOConnection { } if (!is_array($this->sequences) || is_array($this->sequences) && !in_array($seq,$this->sequences)){ - $this->CreateSequence2012($seq='adodbseq',$start=1); + $this->CreateSequence2012($seq, $start); } $num = $this->GetOne("SELECT NEXT VALUE FOR $seq"); @@ -468,10 +466,9 @@ class ADODB_mssqlnative extends ADOConnection { function ErrorNo() { - if ($this->_logsql && $this->_errorCode !== false) return $this->_errorCode; $err = sqlsrv_errors(SQLSRV_ERR_ALL); if($err[0]) return $err[0]['code']; - else return -1; + else return 0; } // returns true or false @@ -569,7 +566,7 @@ class ADODB_mssqlnative extends ADOConnection { $insert = false; // handle native driver flaw for retrieving the last insert ID - if(preg_match('/^\W*insert\s(?:(?:(?:\'\')*\'[^\']+\'(?:\'\')*)|[^;\'])*;?$/i', $sql)) { + if(preg_match('/^\W*insert[\s\w()",.]+values\s*\((?:[^;\']|\'\'|(?:(?:\'\')*\'[^\']+\'(?:\'\')*))*;?$/i', $sql)) { $insert = true; $sql .= '; '.$this->identitySQL; // select scope_identity() } diff --git a/lib/adodb/drivers/adodb-mssqlpo.inc.php b/lib/adodb/drivers/adodb-mssqlpo.inc.php index e99f31e3a45..cd6a2850a93 100644 --- a/lib/adodb/drivers/adodb-mssqlpo.inc.php +++ b/lib/adodb/drivers/adodb-mssqlpo.inc.php @@ -1,6 +1,6 @@ firstrows) { if ($nrows > 500 && $nrows < 1000) { @@ -731,20 +741,13 @@ END; if ($offset > 0) { $nrows += $offset; } - //$inputarr['adodb_rownum'] = $nrows; - if ($this->databaseType == 'oci8po') { - $sql = "select * from (".$sql.") where rownum <= ?"; - } else { - $sql = "select * from (".$sql.") where rownum <= :adodb_offset"; - } + $sql = "select * from (".$sql.") where rownum <= :adodb_offset"; $inputarr['adodb_offset'] = $nrows; $nrows = -1; } // note that $nrows = 0 still has to work ==> no rows returned - $rs = ADOConnection::SelectLimit($sql,$nrows,$offset,$inputarr,$secs2cache); - return $rs; - + return ADOConnection::SelectLimit($sql, $nrows, $offset, $inputarr, $secs2cache); } else { // Algorithm by Tomas V V Cox, from PEAR DB oci8.php @@ -758,13 +761,19 @@ END; if (is_array($inputarr)) { foreach($inputarr as $k => $v) { + $i=0; + if ($this->databaseType == 'oci8po') { + $bv_name = ":".$i++; + } else { + $bv_name = ":".$k; + } if (is_array($v)) { // suggested by g.giunta@libero. if (sizeof($v) == 2) { - oci_bind_by_name($stmt,":$k",$inputarr[$k][0],$v[1]); + oci_bind_by_name($stmt,$bv_name,$inputarr[$k][0],$v[1]); } else { - oci_bind_by_name($stmt,":$k",$inputarr[$k][0],$v[1],$v[2]); + oci_bind_by_name($stmt,$bv_name,$inputarr[$k][0],$v[1],$v[2]); } } else { $len = -1; @@ -774,7 +783,7 @@ END; if (isset($bindarr)) { // is prepared sql, so no need to oci_bind_by_name again $bindarr[$k] = $v; } else { // dynamic sql, so rebind every time - oci_bind_by_name($stmt,":$k",$inputarr[$k],$len); + oci_bind_by_name($stmt,$bv_name,$inputarr[$k],$len); } } } @@ -801,24 +810,19 @@ END; } $offset += 1; // in Oracle rownum starts at 1 - if ($this->databaseType == 'oci8po') { - $sql = "SELECT $hint $fields FROM". - "(SELECT rownum as adodb_rownum, $fields FROM". - " ($sql) WHERE rownum <= ?". - ") WHERE adodb_rownum >= ?"; - } else { - $sql = "SELECT $hint $fields FROM". - "(SELECT rownum as adodb_rownum, $fields FROM". - " ($sql) WHERE rownum <= :adodb_nrows". - ") WHERE adodb_rownum >= :adodb_offset"; - } - $inputarr['adodb_nrows'] = $nrows; - $inputarr['adodb_offset'] = $offset; + $sql = "SELECT $hint $fields FROM". + "(SELECT rownum as adodb_rownum, $fields FROM". + " ($sql) WHERE rownum <= :adodb_nrows". + ") WHERE adodb_rownum >= :adodb_offset"; + $inputarr['adodb_nrows'] = $nrows; + $inputarr['adodb_offset'] = $offset; if ($secs2cache > 0) { $rs = $this->CacheExecute($secs2cache, $sql,$inputarr); } - else $rs = $this->Execute($sql,$inputarr); + else { + $rs = $this->Execute($sql, $inputarr); + } return $rs; } } diff --git a/lib/adodb/drivers/adodb-oci805.inc.php b/lib/adodb/drivers/adodb-oci805.inc.php index e2979f5bf3a..112e9eccdbc 100644 --- a/lib/adodb/drivers/adodb-oci805.inc.php +++ b/lib/adodb/drivers/adodb-oci805.inc.php @@ -1,6 +1,6 @@ 1) { + $sql = $sqlarr[0]; - foreach($inputarr as $k => $v) { - $sql .= ":$k" . $sqlarr[++$i]; + foreach ($inputarr as $k => $v) { + $sql .= ":$k" . $sqlarr[++$i]; + } } $sql = str_replace('-QUESTIONMARK-', '?', $sql); diff --git a/lib/adodb/drivers/adodb-oci8quercus.inc.php b/lib/adodb/drivers/adodb-oci8quercus.inc.php index 341e2fc6e74..1940e802e3c 100644 --- a/lib/adodb/drivers/adodb-oci8quercus.inc.php +++ b/lib/adodb/drivers/adodb-oci8quercus.inc.php @@ -1,6 +1,6 @@ _origarray); - while (list($k_arr,$arr) = each($this->_origarray)) { + foreach ($this->_origarray as $arr) { if ($i == 0 && $this->_skiprow1) $where_arr[] = $arr; @@ -247,7 +247,7 @@ class ADODB_text extends ADOConnection { $i = 0; $n = ''; reset($this->_colnames); - while (list($k_n,$n) = each($this->_colnames)) { + foreach ($this->_colnames as $n) { if ($col == strtoupper(trim($n))) break; $i += 1; @@ -302,7 +302,7 @@ class ADODB_text extends ADOConnection { if ($at == 0) { $i = 0; reset($projnames); - while (list($k_n,$n) = each($projnames)) { + foreach ($projnames as $n) { if (strtoupper(trim($n)) == $col) { $at = $i+1; break; diff --git a/lib/adodb/drivers/adodb-vfp.inc.php b/lib/adodb/drivers/adodb-vfp.inc.php index 0b0e8d9a206..840c9c10974 100644 --- a/lib/adodb/drivers/adodb-vfp.inc.php +++ b/lib/adodb/drivers/adodb-vfp.inc.php @@ -1,6 +1,6 @@ license.txt + Added: * index.html - prevent directory browsing on misconfigured servers * readme_moodle.txt - this file ;-) @@ -26,6 +29,8 @@ Added: Our changes: * Removed random seed initialization from lib/adodb/adodb.inc.php:216 (see 038f546 and MDL-41198). * MDL-52286 Added muting erros in ADORecordSet::__destruct(). - Check if fixed upstream during the next upgrade and remove this note. + Check if fixed upstream during the next upgrade and remove this note. (8638b3f1441d4b928) + * MDL-58546 replaced each() with foreach for PHP 7.2 compatibility. + pull request upstream: https://github.com/ADOdb/ADOdb/pull/373 -skodak, iarenaza, moodler, stronk7, abgreeve, lameze +skodak, iarenaza, moodler, stronk7, abgreeve, lameze, ankitagarwal, marinaglancy diff --git a/lib/adodb/rsfilter.inc.php b/lib/adodb/rsfilter.inc.php index 33439835bcb..4b609b98bf9 100644 --- a/lib/adodb/rsfilter.inc.php +++ b/lib/adodb/rsfilter.inc.php @@ -1,6 +1,6 @@ name : 'Field'.($i++); if ($escquote) $v = str_replace($quote,$escquotequote,$v); diff --git a/lib/adodb/tohtml.inc.php b/lib/adodb/tohtml.inc.php index e859907f266..d39ed2919bc 100644 --- a/lib/adodb/tohtml.inc.php +++ b/lib/adodb/tohtml.inc.php @@ -1,6 +1,6 @@ adodb AdoDB BSD/GPL - 5.20.7 + 5.20.9 2.1+