Fatal error: Undefined class constant [duplicate] - php

This question already has answers here:
How to refer to a static constant member variable in PHP
(2 answers)
Closed 10 years ago.
So I'm trying to use this mysqli connection class (code below) but I am receiving the error message: Fatal error: Undefined class constant 'DBUSER' [...] I can't figure out why because I have set all the database connection credentials and included the config file.
My db.config.class.php:
class config {
public static $DBSERVER = "localhost"; // Set the IP or hostname of the database server you wish to connect to
public static $DBNAME = "**REMOVED**"; // Set the name of the database you wish to connect to
public static $DBUSER = "**REMOVED**"; // set the database user name you wish to use to connect to the database server
public static $DBPASSWORD = "**REMOVED**"; // set the password for the username above
public static $DBPORT = 3306;
public static $TABLEPREFIX = "";
}
mysqli.class.php:
include('db.config.class.php');
/**
* My-SQL database class
*
* #name mysql
* #version 2
* #author Leigh Edwards
* #category PHP
*/
class Dbconnect {
// leave blank if used for multiple users and call setUser method
private $sqlUser = "";
// leave blank if used for multiple users and call setPassword method
private $sqlPassword = "";
// set this to the database name you wish to use. If this class is used to access a number of Databases
// leave blank and call the select method to select the desired database
private $sqlDatabase = "";
// set this to the database server address. If you are using this class to connect to differant server
// leave blank and call the setHost method
private $sqlHost = "";
// Set this to the prefix of your tables if you set one while installing.
// default = ""
public $table_prefix = "";
private $result; // Query result
private $querycount; // Total queries executed
private $linkid;
/////////////////////////////////////////END CONFIG OPTIONS/////////////////////////////////////////////////////
function __construct() {
$this->loadDefaults ();
$this->connect ( $this->sqlHost, $this->sqlUser, $this->sqlPassword, $this->sqlDatabase );
$this->select ( $this->sqlDatabase );
}
/*
* method to load the object with the defaut settings
*/
private function loadDefaults() {
$this->sqlUser = config::DBUSER;
$this->sqlPassword = config::DBPASSWORD;
$this->sqlHost = config::DBSERVER;
$this->sqlDatabase = config::DBNAME;
$this->table_prefix = config::TABLEPREFIX;
}
public function getResult() {
return $this->result;
}
/**
* method to return the prefix for the sql tables
*
* #return = string $this->table_prefix
*/
public function get_tablePrefix() {
return $this->table_prefix;
}
/**
* function to return a string from within another string
* found between $beginning and $ending
*
* #param string $source
* #param string $beginning
* #param string $ending
* #param string $init_pos
*/
function get_middle($source, $beginning, $ending, $init_pos) {
$beginning_pos = strpos ( $source, $beginning, $init_pos );
$middle_pos = $beginning_pos + strlen ( $beginning );
$ending_pos = strpos ( $source, $ending, $beginning_pos + 1 );
$middle = substr ( $source, $middle_pos, $ending_pos - $middle_pos );
return $middle;
}
/**
* method to connect to the MySQL database server.
*
* #param string $sqlHost
* #param string $sqlUser
* #param string $sqlPassword
**/
function connect($sqlHost, $sqlUser, $sqlPassword, $sqlDatabase) {
try {
$this->linkid = mysqli_connect ( $sqlHost, $sqlUser, $sqlPassword, $sqlDatabase, 3306 );
if (! $this->linkid) {
die ( 'Connect Error (' . mysqli_connect_errno () . ') ' . mysqli_connect_error () );
}
} catch ( Exception $e ) {
die ( $e->getMessage () );
}
}
/**
* method to select the database to use
* #param string $sqlDatabase
*/
function select($sqlDatabase) {
try {
if (! #mysqli_select_db ( $sqlDatabase, $this->linkid )) {
throw new Exception ( "The Selected Database Can Not Be Found On the Database Server. $sqlDatabase (E2)" );
}
} catch ( Exception $e ) {
die ( $e->getMessage () );
}
}
/**
* method to query sql database
* take mysql query string
* returns false if no results or NULL result is returned by query
* if query action is not expected to return results eg delete
* returns false on sucess else returns result set
*
* NOTE: If you requier the the actual result set call one of the fetch methods
*
* #param string $query
* #return boolian true or false
*/
function query($query) {
// ensure clean results
unset ( $this->result );
// make query
$this->result = mysqli_query ( $query, $this->linkid );
if (! $this->result) {
echo "<br>Query faild: $query";
return FALSE;
} else {
return true;
}
}
/**
* method to return the number of rows affected by the
* last query exicuted
* #return int
*/
function affectedRows() {
$count = mysqli_affected_rows ( $this->linkid );
return $count;
}
/**
* method to return the number of rows in the result set
* returned by the last query
*/
function numRows() {
$count = #mysqli_num_rows ( $this->result );
return $count;
}
/**
* method to return the result row as an object
* #return object
*/
function fetchObject() {
$row = #mysqli_fetch_object ( $this->result );
return $row;
}
/**
* method to return the result row as an indexed array
* #return array
*/
function fetchRow() {
$row = #mysqli_fetch_row ( $this->result );
return $row;
}
/**
* method to return the result row as an associative array.
* #return array
**/
function fetchArray() {
$row = #mysqli_fetch_array ( $this->result, mysqli_ASSOC );
return $row;
}
/**
* method to return total number queries executed during
* the lifetime of this object.
*
* #return int
*/
function numQueries() {
return $this->querycount;
}
function setResult($resultSet) {
$this->result = $resultSet;
}
/**
* method to return the number of fields in a result set
* #return int
**/
function numberFields() {
return #mysqli_num_fields ( $this->result );
}
/**
* method to return a field name given an integer offset
* #return string
**/
function fieldName($offset) {
return #mysqli_field_name ( $this->result, $offset );
}
/**
* method to return the results of the last query
* in html table
*
* This method uses the $actions string to pass html code
* this is added to the table to enable display of images or links
* in the last columb of the table
*
* if boolian false is passed no html is add to the result table
* $startCol sets the col to start displaying 0 being the first
*
* #param int $startCol
* #param string or boolian false $actions
* #return string containing html code to dispay the table
*/
function getResultAsTable($startCol, $actions = "") {
if ($this->numrows () > 0) {
// Start the table
$resultHTML = "<table width=\"80%\" border=\"0\" align=\"center\" cellpadding=\"1\" cellspacing=\"0\"><tr>";
$resultHTML .= "<td><table width=\"100%\" border=\"0\" cellpadding=\"0\" cellspacing=\"1\"><tr>";
$x = $startCol;
// Output the table header
$fieldCount = $this->numberFields ();
for($i = $x; $i < $fieldCount; $i ++) {
$rowName = $this->fieldName ( $i );
$resultHTML .= "<th align=\"left\">$rowName</th>";
}
if (! $actions === false) {
$resultHTML .= "<th align=\"left\">actions</th>";
}
$resultHTML .= "</tr>";
while ( $row = $this->fetchRow () ) {
$resultHTML .= "<tr>";
for($i = $x; $i < $fieldCount; $i ++)
$resultHTML .= "<td align=\"left\">" . htmlentities ( $row [$i] ) . "</td>";
if (! $actions === false) {
// Replace VALUE with the correct primary key
$action = str_replace ( "VALUE", $row [0], $actions );
$resultHTML .= "<td nowrap align=\"left\">$action</td>";
}
$resultHTML .= "</tr>";
}
$resultHTML .= "</table></td></tr></table>";
} else {
$resultHTML = "";
}
return $resultHTML;
}
/**
* method to retun the value of a given colum using one where clause
* #param $table
* #param $col
* #param $val1
* #param $col2
*/
function getRow($table, $col, $val1, $col2) {
$query = "SELECT '$col2' FROM " . $this->table_prefix . $table . " WHERE $col = '$val1'";
$this->query ( $query );
$resultArray = $this->fetchArray ();
return $resultArray [$col2];
}
/**
* method to test if a row conatining $x in the feild $y exists in the given $table
* method returns true or false
* #param $table
* #param $col
* #param $val
* #return boolian
*/
function rowExistsInDB($table, $col, $val) {
$this->query ( "SELECT $col FROM '" . $this->table_prefix . $table . "' WHERE '$col' = '$val'" );
if ($this->numRows () > 0) {
return true;
} else {
return false;
}
}
function rowExistsInDB2($table, $col, $val, $col2, $val2) {
$query = "SELECT " . $col . " FROM " . $this->table_prefix . $table . " WHERE " . $col . " = '" . mysqli_real_escape_string ( $val ) . "' AND " . $col2 . " = '" . mysqli_real_escape_string ( $val2 ) . "'";
$this->query ( $query );
if ($this->numRows () > 0) {
return true;
} else {
return false;
}
}
/**
* method to delete all rows where $col=$val in $table
* returns int of number of affected rows or false on fail
*
* #param string $table
* #param string $col
* #param string $val
* #return int
*/
function deleteRow($table, $col, $val) {
$this->query ( "DELETE FROM '" . $this->table_prefix . $table . "' WHERE '$col' = '$val'" );
return $this->result;
}
// Misc methods to do some convertions and stuff
// round or pad to 2 decimal points
function formatNum($num, $dec = 2) {
for($x = 0; $x <= 5; $x ++) {
$num = sprintf ( "%01." . ($dec + $x) . "f", $num );
return $num;
}
}
/**
* method to reverse the order of a given date
* and fix to mysql date format
* so DD/MM/YYYY becomes YYYY-MM-DD
*
* #param string $date
* #return string
*/
function revDate($date) {
// first split the date string # / int o three parts
$dateArray = explode ( '/', $date, 3 );
// then reorder them to YYY-MM-DD
$revDate = array_reverse ( $dateArray );
$i = 0;
foreach ( $revDate as $eliment ) {
$correctDate .= $eliment;
if ($i < 2) {
$correctDate .= "-";
}
$i ++;
}
return $correctDate;
}
/**
* method to revers dates taken from sql database
* so YYYY-MM-DD becomes DD/MM/YYYY
*
* #param string $date
* #return string
*/
function revSqlDate($date) {
// first split the date string # / int o three parts
$dateArray = explode ( '-', $date, 3 );
// then reorder them to DD/MM/YYYY
$revDate = array_reverse ( $dateArray );
$i = 0;
foreach ( $revDate as $eliment ) {
$correctDate .= $eliment;
if ($i < 2) {
$correctDate .= "/";
}
$i ++;
}
return $correctDate;
}
}

Write as:
config::$DBUSER;
and etc.

Related

PHP Firestore cannot handle unknown field collection_Id on message google.firestore.v1.StructuredQuery.CollectionSelector

I'm working on a project and using firestore with PHP. I created a Firestore class according to documentation below.
https://github.com/GoogleCloudPlatform/php-docs-samples/tree/4149481c23eeb314144ae15c74fa47c5eaf8f3a6/firestore/src
Project is working in localhost with xamp. But it's not working on the server. I didn't understand why it's not working :s Can you help me ?
Thank you.
Getting an error in the getWhere() function on this line :
$documents = $query->documents();
Function getWhere() in Firestore.php :
/**
* Get document(s) with where condition
* #param string $collectionName
* #param string $field
* #param string $operator
* #param $value
* #return array
*/
public function getWhere(string $nerden, string $collectionName, array $whereQuery, string $orderBy = "", string $orderBySort = "desc", int $limit = 0)
{
// echo $nerden . "<br>";
// echo $limit . "<br>";
// echo $orderBySort . "<br>";
// echo $orderBy . "<br>";
// echo "<pre>";
// print_r($whereQuery);
// echo "</pre>";
$arr = [];
$collectionRef = $this->db->collection($collectionName);
$query = null;
foreach ($whereQuery as $param) {
# code...
if ($query == null) {
$query = $collectionRef->where($param["field"], $param["operator"], $param["value"]);
} else {
$query = $query->where($param["field"], $param["operator"], $param["value"]);
}
}
if ($query != null) {
if ($orderBy != "") {
$query = $query->orderBy($orderBy, $orderBySort);
}
if ($limit > 0) {
$query = $query->limit($limit);
}
$documents = $query->documents();
$x = 0;
foreach ($documents as $document) {
if ($document->exists()) {
// printf('Document id for document %s:' . PHP_EOL, $document->id());
// print_r($document->data());
// printf(PHP_EOL);
$arr[$x]["data"] = $document->data();
$arr[$x]["id"] = $document->id();
$x++;
} else {
// printf('Document %s does not exist!' . PHP_EOL, $document->id());
}
}
return $arr;
}
return false;
}
Line 120 in yorumlari_listele.php
$egitici_aktiviteler = yorumlariGetir(1);
Function yorumlariGetir()
// durum parametresine bağlı olarak yorumları getirir
// durum : 0 -> Onay bekleyen yorumları getirir
// durum : 1 -> Onaylanan yorumları getirir
function yorumlariGetir($durum)
{
global $db_firebase;
// Onay bekleyen yorumları getir
$whereQuery = array(
array(
"field" => "durum",
"operator" => "=",
"value" => $durum,
),
);
$sonuc = $db_firebase->getWhere("yorumlariGetir", "reviews", $whereQuery, "tarih", "asc");
return $sonuc;
}
This is FirebaseDb Class:
/*
Yazar : Mustafa SOLAK
Tarih : 14.09.2022 00:02
*/
//require_once "vendor/autoload.php";
use Google\Cloud\Firestore\FirestoreClient;
use Google\Cloud\Firestore\FieldValue;
use Google\Cloud\Core\Timestamp;
use DateTime;
class FirebaseDB
{
private $db;
private $projectId = "*********";
private static $instance = null;
private function __construct()
{
$this->db = new FirestoreClient([
'projectId' => $this->projectId,
]);
// print("Created Cloud Firestore client with project ID: $this->projectId <br>");
}
public static function getInstance()
{
if (!isset(self::$instance)) {
self::$instance = new FirebaseDB();
}
return self::$instance;
}
public function getConnection()
{
return $this->db;
}
/**
* Create new document with data
* #param string $collectionName
* #param string $docName
* #param array $data
* #return bool|string
*/
public function newDocument(string $collectionName, string $docName, array $data = [])
{
try {
// set var olanı günceller. yoksa oluşturur.
$this->db->collection($collectionName)->document($docName)->set($data, ["merge" => true]);
return true;
} catch (Exception $exception) {
return $exception->getMessage();
}
}
/**
* Get document and all data with checking for exists
* #param string $collectionName
* #param string $docName
* #return array|null|string
*/
public function getDocument(string $collectionName, string $docName)
{
try {
if (empty($collectionName)) throw new Exception('Collection name missing');
else if (empty($docName)) throw new Exception('Document name missing');
if ($this->db->collection($collectionName)->document($docName)->snapshot()->exists()) {
return $this->db->collection($collectionName)->document($docName)->snapshot()->data();
} else {
throw new Exception('Document are not exists');
}
} catch (Exception $exception) {
return $exception->getMessage();
}
}
/**
* Get document(s) with where condition
* #param string $collectionName
* #param string $field
* #param string $operator
* #param $value
* #return array
*/
public function getWhere(string $nerden, string $collectionName, array $whereQuery, string $orderBy = "", string $orderBySort = "desc", int $limit = 0)
// public function getWhere(string $collectionName, string $field, string $operator, $value)
{
// echo $nerden . "<br>";
// echo $limit . "<br>";
// echo $orderBySort . "<br>";
// echo $orderBy . "<br>";
// echo "<pre>";
// print_r($whereQuery);
// echo "</pre>";
$arr = [];
$collectionRef = $this->db->collection($collectionName);
$query = null;
foreach ($whereQuery as $param) {
# code...
if ($query == null) {
$query = $collectionRef->where($param["field"], $param["operator"], $param["value"]);
} else {
$query = $query->where($param["field"], $param["operator"], $param["value"]);
}
}
if ($query != null) {
if ($orderBy != "") {
$query = $query->orderBy($orderBy, $orderBySort);
}
if ($limit > 0) {
$query = $query->limit($limit);
}
$documents = $query->documents();
$x = 0;
foreach ($documents as $document) {
if ($document->exists()) {
// printf('Document id for document %s:' . PHP_EOL, $document->id());
// print_r($document->data());
// printf(PHP_EOL);
$arr[$x]["data"] = $document->data();
$arr[$x]["id"] = $document->id();
$x++;
} else {
// printf('Document %s does not exist!' . PHP_EOL, $document->id());
}
}
return $arr;
}
return false;
}
/**
* Get documents under the specific collection
* #param string $collectionName
* #return array
*/
public function getCollection(string $collectionName, $orderBy = "", $orderBySort = "")
{
$arr = [];
$collectionRef = $this->db->collection($collectionName);
if ($orderBy != "") {
$query = $collectionRef->orderBy($orderBy, $orderBySort);
$documents = $query->documents();
}else{
$documents = $collectionRef->documents();
}
$x = 0;
foreach ($documents as $document) {
if ($document->exists()) {
// printf('Document id for document %s:' . PHP_EOL, $document->id());
// print_r($document->data());
// printf(PHP_EOL);
$arr[$x]["data"] = $document->data();
$arr[$x]["id"] = $document->id();
$x++;
} else {
// printf('Document %s does not exist!' . PHP_EOL, $document->id());
}
}
return $arr;
}
public function arrayContains()
{
$citiesRef = $this->db->collection('icerikler');
# [START fs_array_membership]
# [START firestore_query_filter_array_contains]
$containsQuery = $citiesRef->where('konu', 'array-contains', '3');
# [END firestore_query_filter_array_contains]
# [END fs_array_membership]
foreach ($containsQuery->documents() as $document) {
printf('Document %s returned by query regions array-contains west_coast' . PHP_EOL, $document->id());
}
}
/**
* Create new collection
* #param string $collectionName
* #param string $docName
* #param array $data
* #return bool|string
*/
public function newCollection(string $collectionName, string $docName, array $data = [])
{
try {
$this->db->collection($collectionName)->document($docName)->create($data);
return true;
} catch (Exception $exception) {
return $exception->getMessage();
}
}
/**
* checks whether collection is exists or not
* #param string $collectionName
* #return bool|string
*/
public function isCollectionExists(string $collectionName)
{
try {
$documents = $this->db->collection($collectionName)->limit(1)->documents();
// it means collection exists
if (!$documents->isEmpty())
return 1;
// oops! collection does not exists
else
return -1;
} catch (Exception $exception) {
return $exception->getMessage();
}
}
/**
* Drop exists document in collection
* #param string $collectionName
* #param string $docName
* #return void
*/
public function dropDocument(string $collectionName, string $docName)
{
try {
$this->db->collection($collectionName)->document($docName)->delete();
} catch (Exception $exception) {
return $exception->getMessage();
}
}
/**
* Update a document with increment operation
* Increases Field value if you use positive number
* Decreases Field value if you use negative number
* By the way you can use this function both operations
* #param string $collectionName
* #param string $docName
* #param string $field -> to be updated
* #param string $amount -> increment amount
* #return void
*/
public function fieldIncrementOrDecrement(string $collectionName, string $docName, $field, $amount)
{
$cityRef = $this->db->collection($collectionName)->document($docName);
try {
$cityRef->update([
['path' => $field, 'value' => FieldValue::increment($amount)]
]);
} catch (Exception $exception) {
return $exception->getMessage();
}
}
public function updateField(string $collectionName, string $docName, $field, $value)
{
try {
$cityRef = $this->db->collection($collectionName)->document($docName);
$cityRef->update([
['path' => $field, 'value' => $value]
]);
} catch (Exception $exception) {
return $exception->getMessage();
}
}
/**
* Set review count data
* #param string $collectionName
* #param string $docName
* #param array $data
* #return bool|string
*/
public function setReviewCounts($collectionName, $docName, $data, $field, $value)
{
try {
$data[$field] = FieldValue::increment($value);
// set var olanı günceller. yoksa oluşturur.
$this->db->collection($collectionName)->document($data["id"] . "_" . $data["type"])->set($data, ["merge" => true]);
return true;
} catch (Exception $exception) {
return $exception->getMessage();
}
}
}
Error Text :
Fatal error: Uncaught RuntimeException: cannot handle unknown field collection_Id on message google.firestore.v1.StructuredQuery.CollectionSelector in /home/evok9367/public_html/mustafasolakweb/vendor/google/gax/src/Serializer.php:391 Stack trace: #0 /home/evok9367/public_html/mustafasolakweb/vendor/google/gax/src/Serializer.php(369): Google\ApiCore\Serializer->decodeMessageImpl() #1 /home/evok9367/public_html/mustafasolakweb/vendor/google/gax/src/Serializer.php(413): Google\ApiCore\Serializer->decodeElement() #2 /home/evok9367/public_html/mustafasolakweb/vendor/google/gax/src/Serializer.php(127): Google\ApiCore\Serializer->decodeMessageImpl() #3 /home/evok9367/public_html/mustafasolakweb/vendor/google/cloud/Firestore/src/Connection/Grpc.php(215): Google\ApiCore\Serializer->decodeMessage() #4 /home/evok9367/public_html/mustafasolakweb/vendor/google/cloud/Firestore/src/Query.php(204): Google\Cloud\Firestore\Connection\Grpc->runQuery() #5 [internal function]: Google\Cloud\Firestore\Query->Google\Cloud\Firestore{closure}() #6 /home/evok9367/public_html/mustafasolakweb/vendor/google/cloud/Core/src/ExponentialBackoff.php(80): call_user_func_array() #7 /home/evok9367/public_html/mustafasolakweb/vendor/google/cloud/Firestore/src/Query.php(244): Google\Cloud\Core\ExponentialBackoff->execute() #8 /home/evok9367/public_html/mustafasolakweb/Firestore.php(127): Google\Cloud\Firestore\Query->documents() #9 /home/evok9367/public_html/mustafasolakweb/screens/yorumlar/yorumlar_listele.php(329): FirebaseDB->getWhere() #10 /home/evok9367/public_html/mustafasolakweb/screens/yorumlar/yorumlar_listele.php(120): yorumlariGetir() #11 /home/evok9367/public_html/mustafasolakweb/index.php(33): include('/home/evok9367/...') #12 {main} Next Google\ApiCore\ValidationException: Error decoding message: cannot handle unknown field collection_Id on message google.firestore.v1.StructuredQuery.CollectionSelector in /home/evok9367/public_html/mustafasolakweb/vendor/google/gax/src/Serializer.php:129 Stack trace: #0 /home/evok9367/public_html/mustafasolakweb/vendor/google/cloud/Firestore/src/Connection/Grpc.php(215): Google\ApiCore\Serializer->decodeMessage() #1 /home/evok9367/public_html/mustafasolakweb/vendor/google/cloud/Firestore/src/Query.php(204): Google\Cloud\Firestore\Connection\Grpc->runQuery() #2 [internal function]: Google\Cloud\Firestore\Query->Google\Cloud\Firestore{closure}() #3 /home/evok9367/public_html/mustafasolakweb/vendor/google/cloud/Core/src/ExponentialBackoff.php(80): call_user_func_array() #4 /home/evok9367/public_html/mustafasolakweb/vendor/google/cloud/Firestore/src/Query.php(244): Google\Cloud\Core\ExponentialBackoff->execute() #5 /home/evok9367/public_html/mustafasolakweb/Firestore.php(127): Google\Cloud\Firestore\Query->documents() #6 /home/evok9367/public_html/mustafasolakweb/screens/yorumlar/yorumlar_listele.php(329): FirebaseDB->getWhere() #7 /home/evok9367/public_html/mustafasolakweb/screens/yorumlar/yorumlar_listele.php(120): yorumlariGetir() #8 /home/evok9367/public_html/mustafasolakweb/index.php(33): include('/home/evok9367/...') #9 {main} thrown in /home/evok9367/public_html/mustafasolakweb/vendor/google/gax/src/Serializer.php on line 129
There are some Turkish words in the code and I added English translations, hope it helps.
nerden = fromWhere
yorumlari_listele = list_comments
egitici_aktiviteler = activities
yorumlariGetir = getComments
durum = state
sonuc = result
tarih = date
I tried all the things, changed the code like
https://github.com/GoogleCloudPlatform/php-docs-samples/tree/4149481c23eeb314144ae15c74fa47c5eaf8f3a6/firestore/src

Function mod_scorm_insert_scorm_tracks "Invalid parameter"

I have problem with a WebService function for moodle callen "mod_scorm_insert_scorm_tracks"
This function is used for inserting track information (i.e. star time) of a user in his SCORM progress.
Part of the estructure of this function is
scoid= int
attempt= int
tracks[0][element]= string
tracks[0][value]= string
NEW
PHP structe has to look like this
[tracks] =>
Array
(
[0] =>
Array
(
[element] => string
[value] => string
)
)
I have used one of the examples they had in his website everything was fine until I got this error
<b>Notice</b>: Array to string conversion in <b>C:\xampp\htdocs\otros\PHP-REST\curl.php</b> on line <b>247</b><br />
<?xml version="1.0" encoding="UTF-8" ?>
<EXCEPTION class="invalid_parameter_exception">
<ERRORCODE>invalidparameter</ERRORCODE>
<MESSAGE>Invalid parameter value detected</MESSAGE>
<DEBUGINFO>tracks => Invalid parameter value detected: Only arrays accepted. The bad value is: 'Array'</DEBUGINFO>
</EXCEPTION>
And the problem seems to be here:
$item1 = new stdClass();
$item1->scoid = '2';
$item1->attempt = '1';
$item1->tracks = array(
array(
array(
'element' => 'x.start.time',
'value' => '1473102672'
),
),
array(
array(
'element' => 'x.start.time',
'value' => '1473102680'
),
),
);
I tried in many ways
$item1 = new stdClass();
$item1->scoid = '2';
$item1->attempt = '1';
$item1->tracks = array('element' => 'x.start.time','value' => '1473102672');
or
$item1 = new stdClass();
$item1->scoid = '2';
$item1->attempt = '1';
$item1->tracks = array(array ('element' => 'x.start.time','value' => '1473102672'));
And still getting the same message, I'm pretty that is problema with my wyntax but I have tried in many ways and still not working I hope yo can help me.
Complete Code:
/// SETUP - NEED TO BE CHANGED
$token = '481bf3d85a7eb539e37eabc88feccb3c';
$domainname = 'http://localhost/moodle';
//$functionname = 'mod_scorm_launch_sco';
$functionname = 'mod_scorm_insert_scorm_tracks';
//$functionname ='mod_scorm_view_scorm';
// REST RETURNED VALUES FORMAT
$restformat = 'xml'; //Also possible in Moodle 2.2 and later: 'json'
//Setting it to 'json' will fail all calls on earlier Moodle version
$item1 = new stdClass();
$item1->scoid = '2';
$item1->attempt = '1';
$item1->tracks = array(
array(
array(
'element' => 'x.start.time',
'value' => 1473102672
),
),
array(
array(
'element' => 'x.start.time',
'value' => 1473102680
),
),
);
$params = $item1;
/// REST CALL
header('Content-Type: text/plain');
$serverurl = $domainname . '/webservice/rest/server.php'. '?wstoken=' . $token . '&wsfunction='.$functionname;
require_once('./curl.php');
$curl = new curl;
//if rest format == 'xml', then we do not add the param for backward compatibility with Moodle < 2.2
$restformat = ($restformat == 'json')?'&moodlewsrestformat=' . $restformat:'';
$resp = $curl->post($serverurl . $restformat, $params);
print_r($resp);
curl.php
<?php
/**
* cURL class
*
* This is a wrapper class for curl, it is quite easy to use:
* <code>
* $c = new curl;
* // enable cache
* $c = new curl(array('cache'=>true));
* // enable cookie
* $c = new curl(array('cookie'=>true));
* // enable proxy
* $c = new curl(array('proxy'=>true));
*
* // HTTP GET Method
* $html = $c->get('http://example.com');
* // HTTP POST Method
* $html = $c->post('http://example.com/', array('q'=>'words', 'name'=>'moodle'));
* // HTTP PUT Method
* $html = $c->put('http://example.com/', array('file'=>'/var/www/test.txt');
* </code>
*
* #author Dongsheng Cai <dongsheng#moodle.com> - https://github.com/dongsheng/cURL
* #license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
class curl {
/** #var bool */
public $cache = false;
public $proxy = false;
/** #var array */
public $response = array();
public $header = array();
/** #var string */
public $info;
public $error;
/** #var array */
private $options;
/** #var string */
private $proxy_host = '';
private $proxy_auth = '';
private $proxy_type = '';
/** #var bool */
private $debug = false;
private $cookie = false;
private $count = 0;
/**
* #param array $options
*/
public function __construct($options = array()){
if (!function_exists('curl_init')) {
$this->error = 'cURL module must be enabled!';
trigger_error($this->error, E_USER_ERROR);
return false;
}
// the options of curl should be init here.
$this->resetopt();
if (!empty($options['debug'])) {
$this->debug = true;
}
if(!empty($options['cookie'])) {
if($options['cookie'] === true) {
$this->cookie = 'curl_cookie.txt';
} else {
$this->cookie = $options['cookie'];
}
}
if (!empty($options['cache'])) {
if (class_exists('curl_cache')) {
$this->cache = new curl_cache();
}
}
}
/**
* Resets the CURL options that have already been set
*/
public function resetopt(){
$this->options = array();
$this->options['CURLOPT_USERAGENT'] = 'MoodleBot/1.0';
// True to include the header in the output
$this->options['CURLOPT_HEADER'] = 0;
// True to Exclude the body from the output
$this->options['CURLOPT_NOBODY'] = 0;
// TRUE to follow any "Location: " header that the server
// sends as part of the HTTP header (note this is recursive,
// PHP will follow as many "Location: " headers that it is sent,
// unless CURLOPT_MAXREDIRS is set).
//$this->options['CURLOPT_FOLLOWLOCATION'] = 1;
$this->options['CURLOPT_MAXREDIRS'] = 10;
$this->options['CURLOPT_ENCODING'] = '';
// TRUE to return the transfer as a string of the return
// value of curl_exec() instead of outputting it out directly.
$this->options['CURLOPT_RETURNTRANSFER'] = 1;
$this->options['CURLOPT_BINARYTRANSFER'] = 0;
$this->options['CURLOPT_SSL_VERIFYPEER'] = 0;
$this->options['CURLOPT_SSL_VERIFYHOST'] = 2;
$this->options['CURLOPT_CONNECTTIMEOUT'] = 30;
}
/**
* Reset Cookie
*/
public function resetcookie() {
if (!empty($this->cookie)) {
if (is_file($this->cookie)) {
$fp = fopen($this->cookie, 'w');
if (!empty($fp)) {
fwrite($fp, '');
fclose($fp);
}
}
}
}
/**
* Set curl options
*
* #param array $options If array is null, this function will
* reset the options to default value.
*
*/
public function setopt($options = array()) {
if (is_array($options)) {
foreach($options as $name => $val){
if (stripos($name, 'CURLOPT_') === false) {
$name = strtoupper('CURLOPT_'.$name);
}
$this->options[$name] = $val;
}
}
}
/**
* Reset http method
*
*/
public function cleanopt(){
unset($this->options['CURLOPT_HTTPGET']);
unset($this->options['CURLOPT_POST']);
unset($this->options['CURLOPT_POSTFIELDS']);
unset($this->options['CURLOPT_PUT']);
unset($this->options['CURLOPT_INFILE']);
unset($this->options['CURLOPT_INFILESIZE']);
unset($this->options['CURLOPT_CUSTOMREQUEST']);
}
/**
* Set HTTP Request Header
*
* #param array $headers
*
*/
public function setHeader($header) {
if (is_array($header)){
foreach ($header as $v) {
$this->setHeader($v);
}
} else {
$this->header[] = $header;
}
}
/**
* Set HTTP Response Header
*
*/
public function getResponse(){
return $this->response;
}
/**
* private callback function
* Formatting HTTP Response Header
*
* #param mixed $ch Apparently not used
* #param string $header
* #return int The strlen of the header
*/
private function formatHeader($ch, $header)
{
$this->count++;
if (strlen($header) > 2) {
list($key, $value) = explode(" ", rtrim($header, "\r\n"), 2);
$key = rtrim($key, ':');
if (!empty($this->response[$key])) {
if (is_array($this->response[$key])){
$this->response[$key][] = $value;
} else {
$tmp = $this->response[$key];
$this->response[$key] = array();
$this->response[$key][] = $tmp;
$this->response[$key][] = $value;
}
} else {
$this->response[$key] = $value;
}
}
return strlen($header);
}
/**
* Set options for individual curl instance
*
* #param object $curl A curl handle
* #param array $options
* #return object The curl handle
*/
private function apply_opt($curl, $options) {
// Clean up
$this->cleanopt();
// set cookie
if (!empty($this->cookie) || !empty($options['cookie'])) {
$this->setopt(array('cookiejar'=>$this->cookie,
'cookiefile'=>$this->cookie
));
}
// set proxy
if (!empty($this->proxy) || !empty($options['proxy'])) {
$this->setopt($this->proxy);
}
$this->setopt($options);
// reset before set options
curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this,'formatHeader'));
// set headers
if (empty($this->header)){
$this->setHeader(array(
'User-Agent: MoodleBot/1.0',
'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7',
'Connection: keep-alive'
));
}
curl_setopt($curl, CURLOPT_HTTPHEADER, $this->header);
if ($this->debug){
echo '<h1>Options</h1>';
var_dump($this->options);
echo '<h1>Header</h1>';
var_dump($this->header);
}
// set options
foreach($this->options as $name => $val) {
if (is_string($name)) {
$name = constant(strtoupper($name));
}
curl_setopt($curl, $name, $val);
}
return $curl;
}
/**
* Download multiple files in parallel
*
* Calls {#link multi()} with specific download headers
*
* <code>
* $c = new curl;
* $c->download(array(
* array('url'=>'http://localhost/', 'file'=>fopen('a', 'wb')),
* array('url'=>'http://localhost/20/', 'file'=>fopen('b', 'wb'))
* ));
* </code>
*
* #param array $requests An array of files to request
* #param array $options An array of options to set
* #return array An array of results
*/
public function download($requests, $options = array()) {
$options['CURLOPT_BINARYTRANSFER'] = 1;
$options['RETURNTRANSFER'] = false;
return $this->multi($requests, $options);
}
/*
* Mulit HTTP Requests
* This function could run multi-requests in parallel.
*
* #param array $requests An array of files to request
* #param array $options An array of options to set
* #return array An array of results
*/
protected function multi($requests, $options = array()) {
$count = count($requests);
$handles = array();
$results = array();
$main = curl_multi_init();
for ($i = 0; $i < $count; $i++) {
$url = $requests[$i];
foreach($url as $n=>$v){
$options[$n] = $url[$n];
}
$handles[$i] = curl_init($url['url']);
$this->apply_opt($handles[$i], $options);
curl_multi_add_handle($main, $handles[$i]);
}
$running = 0;
do {
curl_multi_exec($main, $running);
} while($running > 0);
for ($i = 0; $i < $count; $i++) {
if (!empty($options['CURLOPT_RETURNTRANSFER'])) {
$results[] = true;
} else {
$results[] = curl_multi_getcontent($handles[$i]);
}
curl_multi_remove_handle($main, $handles[$i]);
}
curl_multi_close($main);
return $results;
}
/**
* Single HTTP Request
*
* #param string $url The URL to request
* #param array $options
* #return bool
*/
protected function request($url, $options = array()){
// create curl instance
$curl = curl_init($url);
$options['url'] = $url;
$this->apply_opt($curl, $options);
if ($this->cache && $ret = $this->cache->get($this->options)) {
return $ret;
} else {
$ret = curl_exec($curl);
if ($this->cache) {
$this->cache->set($this->options, $ret);
}
}
$this->info = curl_getinfo($curl);
$this->error = curl_error($curl);
if ($this->debug){
echo '<h1>Return Data</h1>';
var_dump($ret);
echo '<h1>Info</h1>';
var_dump($this->info);
echo '<h1>Error</h1>';
var_dump($this->error);
}
curl_close($curl);
if (empty($this->error)){
return $ret;
} else {
return $this->error;
// exception is not ajax friendly
//throw new moodle_exception($this->error, 'curl');
}
}
/**
* HTTP HEAD method
*
* #see request()
*
* #param string $url
* #param array $options
* #return bool
*/
public function head($url, $options = array()){
$options['CURLOPT_HTTPGET'] = 0;
$options['CURLOPT_HEADER'] = 1;
$options['CURLOPT_NOBODY'] = 1;
return $this->request($url, $options);
}
/**
* Recursive function formating an array in POST parameter
* #param array $arraydata - the array that we are going to format and add into &$data array
* #param string $currentdata - a row of the final postdata array at instant T
* when finish, it's assign to $data under this format: name[keyname][][]...[]='value'
* #param array $data - the final data array containing all POST parameters : 1 row = 1 parameter
*/
function format_array_postdata_for_curlcall($arraydata, $currentdata, &$data) {
foreach ($arraydata as $k=>$v) {
$newcurrentdata = $currentdata;
if (is_object($v)) {
$v = (array) $v;
}
if (is_array($v)) { //the value is an array, call the function recursively
$newcurrentdata = $newcurrentdata.'['.urlencode($k).']';
$this->format_array_postdata_for_curlcall($v, $newcurrentdata, $data);
} else { //add the POST parameter to the $data array
$data[] = $newcurrentdata.'['.urlencode($k).']='.urlencode($v);
}
}
}
/**
* Transform a PHP array into POST parameter
* (see the recursive function format_array_postdata_for_curlcall)
* #param array $postdata
* #return array containing all POST parameters (1 row = 1 POST parameter)
*/
function format_postdata_for_curlcall($postdata) {
if (is_object($postdata)) {
$postdata = (array) $postdata;
}
$data = array();
foreach ($postdata as $k=>$v) {
if (is_object($v)) {
$v = (array) $v;
}
if (is_array($v)) {
$currentdata = urlencode($k);
$this->format_array_postdata_for_curlcall($v, $currentdata, $data);
} else {
$data[] = urlencode($k).'='.urlencode($v);
}
}
$convertedpostdata = implode('&', $data);
return $convertedpostdata;
}
/**
* HTTP POST method
*
* #param string $url
* #param array|string $params
* #param array $options
* #return bool
*/
public function post($url, $params = '', $options = array()){
$options['CURLOPT_POST'] = 1;
if (is_array($params)) {
$params = $this->format_postdata_for_curlcall($params);
}
$options['CURLOPT_POSTFIELDS'] = $params;
return $this->request($url, $options);
}
/**
* HTTP GET method
*
* #param string $url
* #param array $params
* #param array $options
* #return bool
*/
public function get($url, $params = array(), $options = array()){
$options['CURLOPT_HTTPGET'] = 1;
if (!empty($params)){
$url .= (stripos($url, '?') !== false) ? '&' : '?';
$url .= http_build_query($params, '', '&');
}
return $this->request($url, $options);
}
/**
* HTTP PUT method
*
* #param string $url
* #param array $params
* #param array $options
* #return bool
*/
public function put($url, $params = array(), $options = array()){
$file = $params['file'];
if (!is_file($file)){
return null;
}
$fp = fopen($file, 'r');
$size = filesize($file);
$options['CURLOPT_PUT'] = 1;
$options['CURLOPT_INFILESIZE'] = $size;
$options['CURLOPT_INFILE'] = $fp;
if (!isset($this->options['CURLOPT_USERPWD'])){
$this->setopt(array('CURLOPT_USERPWD'=>'anonymous: noreply#moodle.org'));
}
$ret = $this->request($url, $options);
fclose($fp);
return $ret;
}
/**
* HTTP DELETE method
*
* #param string $url
* #param array $params
* #param array $options
* #return bool
*/
public function delete($url, $param = array(), $options = array()){
$options['CURLOPT_CUSTOMREQUEST'] = 'DELETE';
if (!isset($options['CURLOPT_USERPWD'])) {
$options['CURLOPT_USERPWD'] = 'anonymous: noreply#moodle.org';
}
$ret = $this->request($url, $options);
return $ret;
}
/**
* HTTP TRACE method
*
* #param string $url
* #param array $options
* #return bool
*/
public function trace($url, $options = array()){
$options['CURLOPT_CUSTOMREQUEST'] = 'TRACE';
$ret = $this->request($url, $options);
return $ret;
}
/**
* HTTP OPTIONS method
*
* #param string $url
* #param array $options
* #return bool
*/
public function options($url, $options = array()){
$options['CURLOPT_CUSTOMREQUEST'] = 'OPTIONS';
$ret = $this->request($url, $options);
return $ret;
}
public function get_info() {
return $this->info;
}
}
/**
* This class is used by cURL class, use case:
*
* <code>
*
* $c = new curl(array('cache'=>true), 'module_cache'=>'repository');
* $ret = $c->get('http://www.google.com');
* </code>
*
* #package core
* #subpackage file
* #copyright 1999 onwards Martin Dougiamas {#link http://moodle.com}
* #license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class curl_cache {
/** #var string */
public $dir = '';
/**
*
* #param string #module which module is using curl_cache
*
*/
function __construct() {
$this->dir = '/tmp/';
if (!file_exists($this->dir)) {
mkdir($this->dir, 0700, true);
}
$this->ttl = 1200;
}
/**
* Get cached value
*
* #param mixed $param
* #return bool|string
*/
public function get($param){
$this->cleanup($this->ttl);
$filename = 'u_'.md5(serialize($param));
if(file_exists($this->dir.$filename)) {
$lasttime = filemtime($this->dir.$filename);
if(time()-$lasttime > $this->ttl)
{
return false;
} else {
$fp = fopen($this->dir.$filename, 'r');
$size = filesize($this->dir.$filename);
$content = fread($fp, $size);
return unserialize($content);
}
}
return false;
}
/**
* Set cache value
*
* #param mixed $param
* #param mixed $val
*/
public function set($param, $val){
$filename = 'u_'.md5(serialize($param));
$fp = fopen($this->dir.$filename, 'w');
fwrite($fp, serialize($val));
fclose($fp);
}
/**
* Remove cache files
*
* #param int $expire The number os seconds before expiry
*/
public function cleanup($expire){
if($dir = opendir($this->dir)){
while (false !== ($file = readdir($dir))) {
if(!is_dir($file) && $file != '.' && $file != '..') {
$lasttime = #filemtime($this->dir.$file);
if(time() - $lasttime > $expire){
#unlink($this->dir.$file);
}
}
}
}
}
/**
* delete current user's cache file
*
*/
public function refresh(){
if($dir = opendir($this->dir)){
while (false !== ($file = readdir($dir))) {
if(!is_dir($file) && $file != '.' && $file != '..') {
if(strpos($file, 'u_')!==false){
#unlink($this->dir.$file);
}
}
}
}
}
}
Thanks!
Well after some research I finally took plan B
I wrote tracks in a different variable:
$tracks = array();
$tracks[] = array(
'element' => 'cmi.core.lesson_status',
'value' => 'completed'
);
And I followed curl.php array set option:
$arrayName = array('' => , );
Then when I inserted scoid and attemps as single variables in the array:
$params = array('scoid' => '2', 'attempt' => '1', 'tracks' => $tracks);
and boala!the record is on my table:

How to do robust PHP database modeling

I am trying to figure out best practices for writing robust object oriented database models in PHP. I would like to use a data access layer and then have an abstract model class that my actual models would be able to extend. I was hoping to be able to encapsulate all basic CRUD functionality in these layers so I don't have to write lots of redundant code. I am having trouble finding tutorial on how to do this. Does anyone know of a tutorial or have example code on how to do this?
If you want to abstract this further to just make procedural calls to DbMgr.php above,
you can use the below code. This will help you develop project fast and anyone who doesn't have database knowledge can also use below methods to develop php mysql application.
CALL THIS FILE : DbMgrInterface.php
//Includes complete implementation of DB operations
require_once __DIR__.'/DbMgr.php';
$perform_Database_Operation = null; // Global variable to reuse the connection
$error_code = null; // Global variable holding the error_code for a transaction
function getDBConfig($config = ''){
$source = 'Func';
$type = 'mysql';
if(isset($config['source'])){
$source = $config['source'];
}
if( (strcasecmp("Func",$source) == 0) && (function_exists('get_DbConfig')) ) {
$config = get_DbConfig();
}
if(isset($config['type'])){
$type = $config['type'];
}
$config['dbType'] = strtolower($type);
$config['servername'] = $config['host'];
$config['Port'] = $config['port'];
$config['userName'] = $config['username'];
$config['passWord'] = $config['password'];
$config['DatabaseName'] = $config['database'];
return $config;
}
/**
Logic: If config array is passed to this method then definitely the new handle is required.
Else, If DBMgrHandle already exists then return the existing handle.
Else, get the default DB config, and instantiate a new DBMgr Handle.
*/
function DBMgr_Handle($config = '') {
global $perform_Database_Operation;
$className = 'DBMgr';
if(is_array($config)){
$config = getDBConfig($config);
if(strcasecmp($config['dbType'], 'mssql') == 0){
$className = 'SSRV';
}
$perform_Database_Operation = new $className($config);
return $perform_Database_Operation;
}
if(isset($perform_Database_Operation))
return $perform_Database_Operation;
$config = getDBConfig($config);
if(strcasecmp($config['dbType'], 'mssql') == 0){
$className = 'SSRV';
}
if(function_exists('getclassObject'))
$perform_Database_Operation = getclassObject($className, $config);
else
$perform_Database_Operation = new $className($config);
return $perform_Database_Operation;
}
/*=====================================================READ==============================================================*/
/*
* #access public
* #param associative_Array $readInput. Format is described below:
* array(
* 'Fields'=> 'Field1, Field2, Field3', //Mandatory
* 'Table'=> 'TableName', //Mandatory
* 'clause'=> 'FieldName = FieldValue', //optional
* 'order' => 'FieldName DESC' //optional
* )
clause refers exact/valid values as mysql query accepts.
clause is a condition to filter output. e.g. 'FieldName = DesiredValue' would return entries where FieldName has DesiredValue value only.
Order refers exact/valid values as mysql query accepts and is used to sort data selection. example value can be 'FieldName ASC' or 'FieldName DESC'.
* #param string $outputFormat. Values can be one of 'RESULT, NUM_ROWS, NUM_ARR, ASSOC', where ASSOC is default value.
* It defines whether the read should return 'mysql result resource/ Numbers of rows in result set / Numbered array / Associative array
* #param string $DataType. Value can only be 'JSON' else ''. Use this to get data set returned as json.
* #param string $keyField_Output. This can be a field name so that indexes of assoc array can be defined with value of the field passed.
*
* #return false, else 0(zero- for no corresponding entry), else output in described format.
* If mysql error is to be accessed, it is available with a aglobal variable $DB_OperationError
*/
function DB_Read($readInput, $outputFormat = "ASSOC", $DataType = "", $keyField_Output = '') {
$perform_Database_Operation = DBMgr_Handle();
return $perform_Database_Operation->Read($readInput, $outputFormat, $DataType, $keyField_Output);
}
/*=====================================================INSERT==============================================================*/
/*
* #access public
* #param associative_Array $insertInput. Format is described below:
* array(
* 'Table'=> 'TableName', //Mandatory
* 'Fields'=> array( //Mandatory
'FieldName1' =>Value1,
'FieldName2' =>Value2,
'FieldName_n'=>Value_n
)
* )
* So in above associative array the element refered by key 'Fields' is itself an associative array which would specify DbField and corresponding Value to be stored
* #return Inserted Id on success, else false on failure. If mysql error is to be accessed, it is available with a aglobal variable $DB_OperationError
*/
function DB_Insert($insertInput) {
$perform_Database_Operation = DBMgr_Handle();
return $perform_Database_Operation->Insert($insertInput);
}
/*=====================================================UPDATE==============================================================*/
/*
* #access public
* #param associative_Array $updateInput. Format is described below:
* array(
* 'Table'=> 'TableName', //Mandatory
* 'Fields'=> array( //Mandatory
'FieldName1' =>Value1,
'FieldName2' =>Value2,
'FieldName_n'=>Value_n
),
* 'clause'=> 'FieldName = FieldValue', //optional
* )
* So in above associative array the element refered by key 'Fields' is itself an associative array which would specify DbField and corresponding Value to be stored
* #return true on success, else false. If mysql error is to be accessed, it is available with a aglobal variable $DB_OperationError
*/
function DB_Update($updateInput) {
$perform_Database_Operation = DBMgr_Handle();
return $perform_Database_Operation->Update($updateInput);
}
/*=====================================================DELETE==============================================================*/
/*
* #access public
* #param associative_Array $deleteInput. Format is described below:
* array(
* 'Table'=> 'TableName', //Mandatory
* 'clause'=> 'FieldName = FieldValue', //OPTIONAL. But if not specified all the data from database would be deleted
* )
* So in above associative array the element refered by key 'Fields' is itself an associative array which would specify DbField and corresponding Value to be stored
* #return true on success, else false on failure. If mysql error is to be accessed, it is available with a aglobal variable $DB_OperationError
*/
function DB_Delete($deleteInput) {
$perform_Database_Operation = DBMgr_Handle();
return $perform_Database_Operation->Delete($deleteInput);
}
/*=====================================================RUN ABSOLUTE QUERY==============================================================*/
/*
* #access public
* #param Query as a string. Query can be of any type
* #param string $outputFormat. Values can be one of 'RESULT, NUM_ROWS, NUM_ARR, ASSOC', where ASSOC is default value.
* It defines whether the read should return 'mysql result resource/ Numbers of rows in result set / Numbered array / Associative array
* #param string $DataType. Value can only be 'JSON' else ''. Use this to get data set returned as json.
* #param string $keyField_Output. This can be a field name so that indexes of assoc array can be defined with value of the field passed.
*
* #return false, else 0(zero- for no corresponding entry), else output in described format. If mysql error is to be accessed, it is available with a aglobal variable $DB_OperationError
*/
function DB_Query($query, $outputFormat = "ASSOC", $DataType = "", $keyField_Output = '') {
$perform_Database_Operation = DBMgr_Handle();
return $perform_Database_Operation->Query($query, $outputFormat, $DataType, $keyField_Output);
}
/**
* The function makes a complete database dump of dump for selective tables, depending upon the TABLE_LIST. If TABLE_LIST is empty,
* a complete database dump is made, else dump for selective tables is carried out.
* #param unknown $ExportDBArray
* $ExportDBArray = array(
'DUMP_FILE_NAME' => 'dump.sql', // optional
'TABLE_LIST' => array( // optional
'Table1' => 'userinfo',
'Table2' => 'usageinfo'
)
);
* #return boolean
*/
function DB_ExportTable($ExportDBArray) {
$perform_Database_Operation = DBMgr_Handle();
return $perform_Database_Operation->Export($ExportDBArray);
}
/**
* The function imports SQL Dump from the given file path.
* #param unknown $ImportDBArray
* $ImportDBArray = array(
'COMPLETE_PATH' => __DIR__ . "\\database_dump.sql"
);
*/
function DB_ImportTable($ImportDBArray) {
$perform_Database_Operation = DBMgr_Handle();
return $perform_Database_Operation->Import($ImportDBArray);
}
/**
*
* #param unknown $multipleFieldsArray
* $multipleFieldsArray = array(
'TABLE_NAME' => 'userinfo',
'ID_LIST' => true ,
'FIELD_DETAILS' => array(
array('Username' => 'bjam123','Password' =>md5('password123'), 'UserType' => 1),
array('Username' => 'ppu12', 'Password' => md5('password1234'), 'UserType' => 2),
array('Username' => 'ppu13', 'Password' => md5('password12345'), 'UserType' => 3),
array('Username' => 'ppu14', 'Password' => md5('password123456'), 'UserType' => 4)
)
);
* #return array containing the insert_id
*/
function DB_InsertMultipleRows($multipleFieldsArray) {
$perform_Database_Operation = DBMgr_Handle();
return $perform_Database_Operation->InsertMR($multipleFieldsArray);
}
/**
* Call this function to perform MSSQL Server specific transactions
* #param unknown $fieldValueArray
* Format is described below:
* array(
* 'Fields'=> 'Field1, Field2, Field3', //Mandatory
* 'Table'=> 'TableName', //Mandatory
* 'clause'=> 'FieldName = FieldValue', //optional
* 'order' => 'FieldName DESC' //optional
* )
clause refers exact/valid values as mysql query accepts.
clause is a condition to filter output. e.g. 'FieldName = DesiredValue' would return entries where FieldName has DesiredValue value only.
Order refers exact/valid values as mysql query accepts and is used to sort data selection. example value can be 'FieldName ASC' or 'FieldName DESC'.
* #param unknown $operationType
* Possible Values - READ, INSERT, UPDATE, DELETE
* #param unknown $outputFormat
* Possible Values - ASSOC, NUM_ARR, NUM_ROWS
*/
function DB_SSRV_Transact($fieldValueArray, $operationType, $outputFormat) {
$perform_Database_Operation = SSRV_Handle();
$perform_Database_Operation->transact($fieldValueArray, $operation, $outputFormat);
}
/*=====================================================Close mysql connection or destroy mysqli object==============================================================*/
/*
* #access public
*/
function DB_Close()
{
$perform_Database_Operation = DBMgr_Handle();
$perform_Database_Operation->closeConnection();
global $perform_Database_Operation;
$perform_Database_Operation = null;
return;
}
?>
Something on these lines:
This is abstraction that uses mysqli extensions of php
CALL THIS FILE : DbMgr.php
<?php
class DBMgr{
private $mysql_Host, $Port, $userName, $passWord, $DatabaseName, $connection, $dbType;
//Constructor function to connect and select database.
/*
* The argument is a assoc array with possible following structure
* array(
* 'source' => 'value is a string. Possible values are Func/Coded', Default value is Func. For Func, the application must have already defined a functiobn name get_DbConfig, which will return assoc array with following structure,
* array(
* 'host' => '',
* 'port' => '',
* 'username' => '',
* 'password' => '',
* 'database' => '',
* 'type' => 'optional. The default value is mysql'
* );
* 'type' => 'mandatory/Required only for Coded Source' Default value is mysql. Other possible values are nssql,postgresql.
* 'host' => 'mandatory/Required only for Coded Source'
* 'port' => 'mandatory/Required only for Coded Source'
* 'username' => 'mandatory/Required only for Coded Source'
* 'password' => 'mandatory/Required only for Coded Source'
* 'database' => 'mandatory/Required only for Coded Source. This is the database name.'
* );
*
*/
function __construct($config)
{
$this->dbType = $config['dbType'];
$this->mysql_Host = $config['host'];
$this->Port = $config['port'];
$this->userName = $config['username'];
$this->passWord = $config['password'];
$this->DatabaseName = $config['database'];
$this->connect_SpecificDatabase();
}
function __destruct() {
$this->connection = null;
}
private function connect_SpecificDatabase(){
if(!isset($this->connection)) {
switch ($this->dbType) {
case 'mysql':
$db = new mysqli('p:'.$this->mysql_Host, $this->userName, $this->passWord, $this->DatabaseName, $this->Port); // Make a connection my MySQL
$this->connection = $db;
break;
case 'postgresql':
// Make a connection to PostgreSQL
$connectionString = "host = '".$this->mysql_Host."' port = '".$this->Port."' dbname = '".$this->DatabaseName."' user='".$this->userName."' password = '".$this->passWord."'";
$db = pg_connect($connection_string);
$this->connection = $db;
break;
}
}
else {
$db = $this->connection;
}
if (!$db)
return "Error: Connection failed '".$this->set_dbError()."'";
}
private function set_dbError($Query, &$DB_OperationError = '')
{
include_once __DIR__.'./../ErrorHandling.php';
$DB_OperationError = "";
global $error_code;
switch ($this->dbType) {
case 'mysql':
$DB_OperationError = mysqli_error($this->connection);
$error_code = mysqli_errno($this->connection);
break;
case 'mssql':
$DB_OperationError = mssql_get_last_message();
break;
case 'postgresql':
$DB_OperationError = pg_last_error($this->connection);
break;
}
ErrorLogging('query: --'.$Query.' -- '.'Error: --'.$DB_OperationError);
}
private function FieldValuePair_ToString($FieldValueArray,$onlyValueString=false)
{
$FieldsAsString = "";
foreach ($FieldValueArray as $FieldName => $value) {
if($FieldsAsString != "")
$FieldsAsString .= ', ';
/* if(strpos($value, '+') > 0 || strpos($value, '-') > 0)
$FieldsAsString .= $FieldName." = ".$value;
*/
if(strpos($value,'\'') !== false || strpos($value,'\"') !== false) {
$value = addslashes($value);
}
if($onlyValueString){
if($value != 'now()')
$FieldsAsString .= "'".$value."'";
else
$FieldsAsString .= $value;
}
else{
if($value != 'now()')
$FieldsAsString .= $FieldName." = '".$value."'";
else
$FieldsAsString .= $FieldName." = ".$value;
}
}
return $FieldsAsString;
}
public function Prepare_Output($output, $output_Format, $keyField_Output = '')
{
$output_Format = strtolower($output_Format); // Convert output_Format to lowercase
if(!is_object($output))
return $output;
elseif($output->num_rows == 0)
return 0;
switch($output_Format)
{
case 'result':
return $output;
break;
case 'num_rows':
return $output->num_rows;
break;
case 'num_arr':
while ($row = $output->fetch_array(MYSQLI_NUM)) {
$output_arr[] = $row;
}
return $output_arr;
break;
case 'assoc':
default:
while ($row = $output->fetch_assoc()) {
if($keyField_Output != '' && isset($row[$keyField_Output])){
$output_arr[strval($row[$keyField_Output])] = $row;
}
else
$output_arr[] = $row;
}
return $output_arr;
break;
}
}
private function Prepare_Query($input_array, $prepare_For)
{
$Query = "";
switch($prepare_For)
{
case 'READ':
if($input_array['Fields'] == "" || $input_array['Fields'] == NULL || $input_array['Table'] == "" || $input_array['Table'] == NULL)
return false;
$Query .= "SELECT ";
$Query .= $input_array['Fields'];
$Query .= " FROM ";
$Query .= $input_array['Table'];
if(isset($input_array['clause']) && $input_array['clause'] != "" && $input_array['clause'] !== NULL)
{
$Query .= " WHERE ";
$Query .= $input_array['clause'];
}
if(isset($input_array['order']) && $input_array['order'] != "" && $input_array['order'] !== NULL)
{
$Query .= " ORDER BY ";
$Query .= $input_array['order'];
}
break;
case 'INSERT':
if($input_array['Fields'] == "" || $input_array['Fields'] == NULL || $input_array['Table'] == "" || $input_array['Table'] == NULL)
return false;
$Query .= "INSERT INTO ";
$Query .= $input_array['Table'];
$Query .= " SET ";
$Query .= $this->FieldValuePair_ToString($input_array['Fields']);
break;
case 'UPDATE':
if($input_array['Fields'] == "" || $input_array['Fields'] == NULL || $input_array['Table'] == "" || $input_array['Table'] == NULL)
return false;
$Query .= "UPDATE ";
$Query .= $input_array['Table'];
$Query .= " SET ";
$Query .= $this->FieldValuePair_ToString($input_array['Fields']);
if(isset($input_array['clause']) && $input_array['clause'] != "" && $input_array['clause'] !== NULL)
{
$Query .= " WHERE ";
$Query .= $input_array['clause'];
}
break;
case 'DELETE':
if($input_array['Table'] == "" || $input_array['Table'] == NULL)
return false;
$Query .= "DELETE FROM ";
$Query .= $input_array['Table']."";
if($input_array['clause'] != "" && $input_array['clause'] !== NULL)
{
$Query .= " WHERE ";
$Query .= $input_array['clause'];
}
break;
case 'INSERT_MR':
$vstring = ""; // Field String
if(is_array($input_array)) {
$tableName = $input_array['TABLE_NAME'];
$input_array = $input_array['FIELD_ARRAY'];
for($i = 0 ;$i < count($input_array); $i++) {
$vstring .= "(" . $this->FieldValuePair_ToString($input_array[$i],true) . ")";
if(! ($i == (count($input_array) - 1))) $vstring .= ",";
}
// Column String
$column_string = implode("," , array_keys($input_array[0])); // Get the column names
$Query = "INSERT INTO ".$tableName." (" . $column_string . ") VALUES ".$vstring.";"; // Form Query String
}
break;
case 'IMPORT_DB':
if(is_array($input_array)) {
$completePathOfSQLFile = "\"" . $input_array['COMPLETE_PATH'];
$Query = "mysql --user" . $this->userName . " --password " . $this->DatabaseName . " < " . $completePathOfSQLFile;
}
break;
case 'EXPORT_DB':
if(is_array($input_array)) {
$TableArray = $input_array['TABLE_LIST'];
$dumpFileName = $input_array['DUMP_FILE_NAME'];
$dQuery = "mysqldump -h ".$this->mysql_Host." --port=".$this->Port." --user=".$this->userName." --password=".$this->passWord;
$dumpFilePath = __DIR__;
$dumpFileName = $dumpFileName == "" ? time().'mysqlDump.sql': $dumpFileName;
if(($TableArray == null) || count($TableArray) == 0 ) {
// Export all tables
$Query = $dQuery . " --databases ".$this->DatabaseName." > ".$dumpFilePath."\\".$dumpFileName;
} else {
$tableNames = "";
// Export selective tables
foreach($TableArray as $k => $k_value) {
$tableNames .= " " . $$TableArray[$k];
}
$Query = $dQuery . " " . $this->DatabaseNames." " . $tableNames . " > \"" . $dumpFilePath."\\".$dumpFileName."\"";
}
}
break;
}
return $Query;
}
public function Read($input_array, $outputFormat, $DataType = "", $keyField_Output = '')
{
$Query = $this->Prepare_Query($input_array, 'READ');
$output = "";
if(!$Query)
$output= 'INVALID';
else
{
$result = $this->connection->query($Query);
if (!$result)
{
$this->set_dbError($Query);
$output = false;
}
else
{
$output = $this->Prepare_Output($result, $outputFormat, $keyField_Output);
if( ($DataType != "") && (strcasecmp(strtolower($DataType) , 'as_json') == 0) )
$output = json_encode($output);
}
}
return $output;
}
public function Insert($input_array)
{
$Query = $this->Prepare_Query($input_array, 'INSERT');
$output = "";
if(!$Query)
$output= 'INVALID';
else
{
$result = $this->connection->query($Query);
if (!$result)
{
$this->set_dbError($Query);
$output = false;
}
else
{
$output = mysqli_insert_id($this->connection);
if(!$output)
return true;
}
}
return $output;
}
public function Update($input_array)
{
$Query = $this->Prepare_Query($input_array, 'UPDATE');
$output = "";
if(!$Query)
$output= 'INVALID';
else
{
$result = $this->connection->query($Query);
if (!$result)
{
$this->set_dbError($Query);
$output = false;
}
else
{
$output = true;
}
}
return $output;
}
public function Delete($input_array)
{
$Query = $this->Prepare_Query($input_array, 'DELETE');
$output = "";
if(!$Query)
$output= 'INVALID';
else
{
$result = $this->connection->query($Query);
if (!$result)
{
$this->set_dbError($Query);
$output = false;
}
else
{
$output = true;
}
}
return $output;
}
public function Query($Query, $outputFormat = 'ASSOC', $DataType = "", $keyField_Output = '')
{
$result = $this->connection->query($Query);
if (!$result)
{
$this->set_dbError($Query);
$output = false;
}
else
{
$output = $this->Prepare_Output($result, $outputFormat, $keyField_Output);
if($DataType != "")
$output = json_encode($output);
}
return $output;
}
public function Export($ExportDBArray) {
$Query = $this->Prepare_Query($input_array, 'EXPORT_DB');
$op = '';
$REtVal = '';
$query = 'dir';
$resultQuery = exec($query,$op,$REtVal);
if($resultQuery) return true;
else return false;
}
public function Import($ImportDBArray) {
$Query = $this->Prepare_Query($ImportDBArray, 'IMPORT_DB');
$resultQuery = exec($Query);
}
public function InsertMR($multipleFieldsArray) {
$tableName = $multipleFieldsArray['TABLE_NAME'];
$listOfID = $multipleFieldsArray['ID_LIST'];
$FieldsArray = $multipleFieldsArray['FIELD_DETAILS'];
$input_array = array('FIELD_ARRAY' => $FieldsArray, 'TABLE_NAME' => $tableName);
$Query = $this->Prepare_Query($input_array, 'INSERT_MR');
$result = $this->connection->query($Query); // Run Query
$output = array();
if (!$result) {
$this->set_dbError($Query);
} else {
$insert_id = mysqli_insert_id($this->connection);
if($listOfID) {
for($i=0; $i<count($FieldsArray); $i++) {
$output[$i] = $insert_id;
$insert_id++;
}
} else {
$output[0] = $insert_id;
}
}
return $output;
}
public function closeConnection() {
if(isset($this->connection)) {
mysqli_close($this->connection);
}
}
};
?>

PHP load classes

I have a class loader like this :
function __autoload($classname){
require_once CLASSES_DIR . "/" . $classname . ".class.php";
}
and it correctly sets the URL of any classes.
The problem is that when an instance of a class is tried to be created in the index.php (line 13):
$pdo = PDOWrapper::instance();
The browser shows a page like this:
validateDriver($driver)) { throw new Exception('DATABASE WRAPPER::error, the database you wish to connect to is not supported by your install of PHP.'); } if (isset($this->pdo_master)) { error_log('DATABASE WRAPPER::warning, attempting to config master after connection exists'); } $this->config_master = array( 'driver' => $driver, 'host' => $host, 'name' => $name, 'user' => $user, 'password' => $password, 'port' => $port ); } /** * method configSlave * - configure a connection to a slave (can be called multiple times) * * #param host - the host name of the db to connect to * #param name - the database name * #param user - the user name * #param password - the users password * #param port (optional) - the port to connect using, default to 3306 * #param driver - the dsn prefix */ public function configSlave($host, $name, $user, $password, $port=null, $driver='mysql') { if (!$this->validateDriver($driver)) { throw new Exception('DATABASE WRAPPER::error, the database you wish to connect to is not supported by your install of PHP.'); } if (isset($this->pdo_slave)) { error_log('DATABASE WRAPPER::warning, attempting to config slave after connection exists'); } if (!isset($this->config_slaves)) { $this->config_slaves = array(); } $this->config_slaves[] = array( 'driver' => $driver, 'host' => $host, 'name' => $name, 'user' => $user, 'password' => $password, 'port' => $port ); } /** * method createConnection. * - create a PDO connection using the credentials provided * * #param driver - the dsn prefix * #param host - the host name of the db to connect to * #param name - the database name * #param user - the user name * #param password - the users password * #param port (optional) - the port to connect using, default to 3306 * #return PDO object with a connection to the database specified */ protected function createConnection($driver, $host, $name, $user, $password, $port=null) { die( var_dump(PDO::getAvailableDrivers())); if (!$this->validateDriver($driver)) { throw new Exception('DATABASE WRAPPER::error, the database you wish to connect to is not supported by your install of PHP.'); } // attempt to create pdo object and connect to the database try { //#TODO the following drivers are NOT supported yet: odbc, ibm, informix, 4D // build the connection string from static constants based on the selected PDO Driver. if ($driver == "sqlite" || $driver == "sqlite2") { $connection_string = $driver.':'.$host; } elseif ($driver == "sqlsrv") { $connection_string = "sqlsrv:Server=".$host.";Database=".$name; } elseif ($driver == "firebird" || $driver == "oci") { $connection_string = $driver.":dbname=".$name; } else { $connection_string = $driver.':host='.$host.';dbname='.$name; } // add the port if one was specified if (!empty($port)) { $connection_string .= "port=$port"; } // initialize the PDO object $new_connection = new PDO($connection_string, $user, $password, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8")); // set the error mode $new_connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // return the new connection return $new_connection; } // handle any exceptions by catching them and returning false catch (PDOException $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } catch(Exception $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } } /** * method getMaster. * - grab the PDO connection to the master DB */ protected function getMaster() { // if we have not been configured, use hard coded values if (!isset($this->config_master)) { $this->config_master = array( 'driver' => self::DB_DSN_PREFIX_MASTER, 'host' => self::DB_HOST_MASTER, 'name' => self::DB_NAME_MASTER, 'user' => self::DB_USER_MASTER, 'password' => self::DB_PASSWORD_MASTER, 'port' => self::DB_PORT_MASTER ); } // if we have not created the master db connection yet, create it now if (!isset($this->pdo_master)) { $this->pdo_master = $this->createConnection( $this->config_master['driver'], $this->config_master['host'], $this->config_master['name'], $this->config_master['user'], $this->config_master['password'], $this->config_master['port'] ); } return $this->pdo_master; } /** * method getSlave. * - grab the PDO connection to the slave DB, create it if not there */ protected function getSlave() { // if we have not created a slave db connection, create it now if (!isset($this->pdo_slave)) { // if no slaves were configured, use hardcoded values if (!isset($this->config_slaves)) { $i = 1; while (defined('self::SLAVE' . $i . '_HOST') && constant('self::SLAVE' . $i . '_HOST')) { $this->config_slaves[] = array( 'driver' => constant('self::SLAVE' . $i . '_DSN_PREFIX'), 'host' => constant('self::SLAVE' . $i . '_HOST'), 'name' => constant('self::SLAVE' . $i . '_NAME'), 'user' => constant('self::SLAVE' . $i . '_USER'), 'password' => constant('self::SLAVE' . $i . '_PASSWORD'), 'port' => constant('self::SLAVE' . $i . '_PORT'), ); $i++; } } // if no slaves are configured, use the master connection if (empty($this->config_slaves)) { $this->pdo_slave = $this->getMaster(); } // if we have slaves, randomly choose one to use for this request and connect else { $random_slave = $this->config_slaves[array_rand($this->config_slaves)]; $this->pdo_slave = $this->createConnection( $random_slave['driver'], $random_slave['host'], $random_slave['name'], $random_slave['user'], $random_slave['password'], $random_slave['port'] ); } } return $this->pdo_slave; } /** * method select. * - retrieve information from the database, as an array * * #param string $table - the name of the db table we are retreiving the rows from * #param array $params - associative array representing the WHERE clause filters * #param int $limit (optional) - the amount of rows to return * #param int $start (optional) - the row to start on, indexed by zero * #param array $order_by (optional) - an array with order by clause * #param bool $use_master (optional) - use the master db for this read * #return mixed - associate representing the fetched table row, false on failure */ public function select($table, $params = null, $limit = null, $start = null, $order_by=null, $use_master = false) { // building query string $sql_str = "SELECT * FROM $table"; // append WHERE if necessary $sql_str .= ( count($params)>0 ? ' WHERE ' : '' ); $add_and = false; // add each clause using parameter array if (empty($params)) { $params = array(); } foreach ($params as $key=>$val) { // only add AND after the first clause item has been appended if ($add_and) { $sql_str .= ' AND '; } else { $add_and = true; } // append clause item $sql_str .= "$key = :$key"; } // add the order by clause if we have one if (!empty($order_by)) { $sql_str .= ' ORDER BY'; $add_comma = false; foreach ($order_by as $column => $order) { if ($add_comma) { $sql_str .= ', '; } else { $add_comma = true; } $sql_str .= " $column $order"; } } // now we attempt to retrieve the row using the sql string try { // decide which database we are selecting from $pdo_connection = $use_master ? $this->getMaster() : $this->getSlave(); $pdoDriver = $pdo_connection->getAttribute(PDO::ATTR_DRIVER_NAME); //#TODO MS SQL Server & Oracle handle LIMITs differently, for now its disabled but we should address it later. $disableLimit = array("sqlsrv", "mssql", "oci"); // add the limit clause if we have one if (!is_null($limit) && !in_array($pdoDriver, $disableLimit)) { $sql_str .= ' LIMIT '.(!is_null($start) ? "$start, ": '')."$limit"; } $pstmt = $pdo_connection->prepare($sql_str); // bind each parameter in the array foreach ($params as $key=>$val) { $pstmt->bindValue(':'.$key, $val); } $pstmt->execute(); // now return the results, depending on if we want all or first row only if ( !is_null($limit) && $limit == 1 ) { return $pstmt->fetch(PDO::FETCH_ASSOC); } else { return $pstmt->fetchAll(PDO::FETCH_ASSOC); } } catch(PDOException $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } catch(Exception $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } } /** * method selectMaster. * - retrieve information from the master database, as an array * * #param table - the name of the db table we are retreiving the rows from * #param params - associative array representing the WHERE clause filters * #param int $limit (optional) - the amount of rows to return * #param int $start (optional) - the row to start on, indexed by zero * #param array $order_by (optional) - an array with order by clause * #return mixed - associate representing the fetched table row, false on failure */ public function selectMaster($table, $params = array(), $limit = null, $start = null, $order_by=null) { return $this->select($table, $params, $limit, $start, $order_by, true); } /** * method selectFirst. * - retrieve the first row returned from a select statement * * #param table - the name of the db table we are retreiving the rows from * #param params - associative array representing the WHERE clause filters * #param array $order_by (optional) - an array with order by clause * #return mixed - associate representing the fetched table row, false on failure */ public function selectFirst($table, $params = array(), $order_by=null) { return $this->select($table, $params, 1, null, $order_by); } /** * method selectFirstMaster. * - retrieve the first row returned from a select statement using the master database * * #param table - the name of the db table we are retreiving the rows from * #param params - associative array representing the WHERE clause filters * #param array $order_by (optional) - an array with order by clause * #return mixed - associate representing the fetched table row, false on failure */ public function selectFirstMaster($table, $params = array(), $order_by=null) { return $this->select($table, $params, 1, null, $order_by, true); } /** * method delete. * - deletes rows from a table based on the parameters * * #param table - the name of the db table we are deleting the rows from * #param params - associative array representing the WHERE clause filters * #return bool - associate representing the fetched table row, false on failure */ public function delete($table, $params = array()) { // building query string $sql_str = "DELETE FROM $table"; // append WHERE if necessary $sql_str .= ( count($params)>0 ? ' WHERE ' : '' ); $add_and = false; // add each clause using parameter array foreach ($params as $key=>$val) { // only add AND after the first clause item has been appended if ($add_and) { $sql_str .= ' AND '; } else { $add_and = true; } // append clause item $sql_str .= "$key = :$key"; } // now we attempt to retrieve the row using the sql string try { $pstmt = $this->getMaster()->prepare($sql_str); // bind each parameter in the array foreach ($params as $key=>$val) { $pstmt->bindValue(':'.$key, $val); } // execute the delete query $successful_delete = $pstmt->execute(); // if we were successful, return the amount of rows updated, otherwise return false return ($successful_delete == true) ? $pstmt->rowCount() : false; } catch(PDOException $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } catch(Exception $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } } /** * method update. * - updates a row to the specified table * * #param string $table - the name of the db table we are adding row to * #param array $params - associative array representing the columns and their respective values to update * #param array $wheres (Optional) - the where clause of the query * #param bool $timestamp_this (Optional) - if true we set date_created and date_modified values to now * #return int|bool - the amount of rows updated, false on failure */ public function update($table, $params, $wheres=array(), $timestamp_this=null) { if (is_null($timestamp_this)) { $timestamp_this = self::$TIMESTAMP_WRITES; } // build the set part of the update query by // adding each parameter into the set query string $add_comma = false; $set_string = ''; foreach ($params as $key=>$val) { // only add comma after the first parameter has been appended if ($add_comma) { $set_string .= ', '; } else { $add_comma = true; } // now append the parameter $set_string .= "$key=:param_$key"; } // add the timestamp columns if neccessary if ($timestamp_this === true) { $set_string .= ($add_comma ? ', ' : '') . 'date_modified='.time(); } // lets add our where clause if we have one $where_string = ''; if (!empty($wheres)) { // load each key value pair, and implode them with an AND $where_array = array(); foreach($wheres as $key => $val) { $where_array[] = "$key=:where_$key"; } // build the final where string $where_string = 'WHERE '.implode(' AND ', $where_array); } // build final update string $sql_str = "UPDATE $table SET $set_string $where_string"; // now we attempt to write this row into the database try { $pstmt = $this->getMaster()->prepare($sql_str); // bind each parameter in the array foreach ($params as $key=>$val) { $pstmt->bindValue(':param_'.$key, $val); } // bind each where item in the array foreach ($wheres as $key=>$val) { $pstmt->bindValue(':where_'.$key, $val); } // execute the update query $successful_update = $pstmt->execute(); // if we were successful, return the amount of rows updated, otherwise return false return ($successful_update == true) ? $pstmt->rowCount() : false; } catch(PDOException $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } catch(Exception $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } } /** * method insert. * - adds a row to the specified table * * #param string $table - the name of the db table we are adding row to * #param array $params - associative array representing the columns and their respective values * #param bool $timestamp_this (Optional), if true we set date_created and date_modified values to now * #return mixed - new primary key of inserted table, false on failure */ public function insert($table, $params = array(), $timestamp_this = null) { if (is_null($timestamp_this)) { $timestamp_this = self::$TIMESTAMP_WRITES; } // first we build the sql query string $columns_str = '('; $values_str = 'VALUES ('; $add_comma = false; // add each parameter into the query string foreach ($params as $key=>$val) { // only add comma after the first parameter has been appended if ($add_comma) { $columns_str .= ', '; $values_str .= ', '; } else { $add_comma = true; } // now append the parameter $columns_str .= "$key"; $values_str .= ":$key"; } // add the timestamp columns if neccessary if ($timestamp_this === true) { $columns_str .= ($add_comma ? ', ' : '') . 'date_created, date_modified'; $values_str .= ($add_comma ? ', ' : '') . 'UNIX_TIMESTAMP(), UNIX_TIMESTAMP()'; } // close the builder strings $columns_str .= ') '; $values_str .= ')'; // build final insert string $sql_str = "INSERT INTO $table $columns_str $values_str"; // now we attempt to write this row into the database try { $pstmt = $this->getMaster()->prepare($sql_str); // bind each parameter in the array foreach ($params as $key=>$val) { $pstmt->bindValue(':'.$key, $val); } $pstmt->execute(); $newID = $this->getMaster()->lastInsertId(); // return the new id return $newID; } catch(PDOException $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } catch(Exception $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); print_r($e); } $this->pdo_exception = $e; return false; } } /** * method insertMultiple. * - adds multiple rows to a table with a single query * * #param string $table - the name of the db table we are adding row to * #param array $columns - contains the column names * #param bool $timestamp_these (Optional), if true we set date_created and date_modified values to NOW() for each row * #return mixed - new primary key of inserted table, false on failure */ public function insertMultiple($table, $columns = array(), $rows = array(), $timestamp_these = null) { if (is_null($timestamp_these)) { $timestamp_these = self::$TIMESTAMP_WRITES; } // generate the columns portion of the insert statment // adding the timestamp fields if needs be if ($timestamp_these) { $columns[] = 'date_created'; $columns[] = 'date_modified'; } $columns_str = '(' . implode(',', $columns) . ') '; // generate the values portions of the string $values_str = 'VALUES '; $add_comma = false; foreach ($rows as $row_index => $row_values) { // only add comma after the first row has been added if ($add_comma) { $values_str .= ', '; } else { $add_comma = true; } // here we will create the values string for a single row $values_str .= '('; $add_comma_forvalue = false; foreach ($row_values as $value_index => $value) { if ($add_comma_forvalue) { $values_str .= ', '; } else { $add_comma_forvalue = true; } // generate the bind variable name based on the row and column index $values_str .= ':'.$row_index.'_'.$value_index; } // append timestamps if necessary if ($timestamp_these) { $values_str .= ($add_comma_forvalue ? ', ' : '') . time().', '.time(); } $values_str .= ')'; } // build final insert string $sql_str = "INSERT INTO $table $columns_str $values_str"; // now we attempt to write this multi inster query to the database using a transaction try { $this->getMaster()->beginTransaction(); $pstmt = $this->getMaster()->prepare($sql_str); // traverse the 2d array of rows and values to bind all parameters foreach ($rows as $row_index => $row_values) { foreach ($row_values as $value_index => $value) { $pstmt->bindValue(':'.$row_index.'_'.$value_index, $value); } } // now lets execute the statement, commit the transaction and return $pstmt->execute(); $this->getMaster()->commit(); return true; } catch(PDOException $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; $this->getMaster()->rollback(); return false; } catch(Exception $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; $this->getMaster()->rollback(); return false; } } /** * method execute. * - executes a query that modifies the database * * #param string $query - the SQL query we are executing * #param bool $use_master (Optional) - whether or not to use the master connection * #return mixed - the affected rows, false on failure */ public function execute($query, $params=array()) { try { // use the master connection $pdo_connection = $this->getMaster(); // prepare the statement $pstmt = $pdo_connection->prepare($query); // bind each parameter in the array foreach ((array)$params as $key=>$val) { $pstmt->bindValue($key, $val); } // execute the query $result = $pstmt->execute(); // only if return value is false did this query fail return ($result == true) ? $pstmt->rowCount() : false; } catch(PDOException $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } catch(Exception $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } } /** * method query. * - returns data from a free form select query * * #param string $query - the SQL query we are executing * #param array $params - a list of bind parameters * #param bool $use_master (Optional) - whether or not to use the master connection * #return mixed - the affected rows, false on failure */ public function query($query, $params=array(), $use_master=false) { try { // decide which database we are selecting from $pdo_connection = $use_master ? $this->getMaster() : $this->getSlave(); $pstmt = $pdo_connection->prepare($query); // bind each parameter in the array foreach ((array)$params as $key=>$val) { $pstmt->bindValue($key, $val); } // execute the query $pstmt->execute(); // now return the results return $pstmt->fetchAll(PDO::FETCH_ASSOC); } catch(PDOException $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } catch(Exception $e) { if (self::$LOG_ERRORS == true) { error_log('DATABASE WRAPPER::'.print_r($e, true)); } $this->pdo_exception = $e; return false; } } /** * method queryFirst. * - returns the first record from a free form select query * * #param string $query - the SQL query we are executing * #param array $params - a list of bind parameters * #param bool $use_master (Optional) - whether or not to use the master connection * #return mixed - the affected rows, false on failure */ public function queryFirst($query, $params=array(), $use_master=false) { $result = $this->query($query, $params, $use_master); if (empty($result)) { return false; } else { return $result[0]; } } /** * method getErrorMessage. * - returns the last error message caught */ public function getErrorMessage() { if ($this->pdo_exception) return $this->pdo_exception->getMessage(); else return 'Database temporarily unavailable'; } /** * method getError. * - returns the actual PDO exception */ public function getPDOException() { return $this->pdo_exception; } /** * Validate the database in question is supported by your installation of PHP. * #param string $driver The DSN prefix * #return boolean true, the database is supported; false, the database is not supported. */ private function validateDriver($driver) { if (!in_array($driver, PDO::getAvailableDrivers())) { return false; } else { return true; } } /** * Destructor. * - release the PDO db connections */ function __destruct() { unset($this->pdo_master); unset($this->pdo_slave); } }
Notice: Undefined index: language in C:\xampp\htdocs_\config.php on
line 133
Notice: Undefined variable: condo_name in
C:\xampp\htdocs_\language\fr.php on line 319
Notice: Undefined variable: condo_name in
C:\xampp\htdocs_\language\fr.php on line 320
Notice: Undefined variable: condo_name in
C:\xampp\htdocs_\language\fr.php on line 321
Notice: Undefined variable: claim_name in
C:\xampp\htdocs_\language\fr.php on line 327
Notice: Undefined variable: claim_name in
C:\xampp\htdocs_\language\fr.php on line 328
Notice: Undefined variable: tender in
C:\xampp\htdocs_\language\fr.php on line 329
Notice: Undefined variable: condo_name in
C:\xampp\htdocs_\language\fr.php on line 332
Notice: Undefined variable: condo_name in
C:\xampp\htdocs_\language\fr.php on line 333
Notice: Undefined variable: condo_name in
C:\xampp\htdocs_\language\fr.php on line 361
Fatal error: Class 'PDOWrapper' not found in
C:\xampp\htdocs_\index.php on line 13 etc. and a fatal error:
Fatal error: Class 'PDOWrapper' not found in C:\xampp\htdocs_\index.php on line 13.
So it seems strange that it can show some PDOWrapper code content and a fatal error to find it.
I don't know why the class is not found, and the browser shows the content of the class, you can see the write, throw new Exception . . .Etc.

Vqmod improve error logging, by adding the parent file name the error is referring too

A default error logging using vqmod is as follows:
---------- Date: 2012-10-09 19:46:06 ~ IP : 127.0.0.1 ----------
REQUEST URI : /oc/
MOD DETAILS:
modFile : C:\wamp\www\oc\vqmod\xml\templace.xml
id : Template
version : 1.5.2 - 1.5.2.1
vqmver : 1.0.8
author : templace.com
SEARCH NOT FOUND (ABORTING MOD): require_once(foo . 'library/template.php');
----------------------------------------------------------------------
Example vqmod causing the error
<modification>
<id>Templace</id>
<version>1.5.2 - 1.5.2.1</version>
<author>templace.com</author>
<vqmver>1.0.8</vqmver>
<file name="system/startup.php">
<operation>
<search position="before"><![CDATA[
require_once(foo . 'library/template.php');
]]></search>
<add><![CDATA[
require_once(DIR_SYSTEM . 'library/templace.php');
]]></add>
</operation>
</file>
</modification>
To resole this issue I would have to open the vqmod file templace.xml and search for the file[name] this error is referring too.
QUESTION: How could I add the parent file[name] the actual error is referring too?
E.g adding: "system/startup.php" to the error message to make it easier to debug.
vqmod.php
/**
* VQMod
* #description Main Object used
*/
final class VQMod {
private $_vqversion = '2.1.7';
private $_modFileList = array();
private $_mods = array();
private $_filesModded = array();
private $_cwd = '';
private $_doNotMod = array();
private $_virtualMode = true;
public $useCache = false;
public $logFilePath = 'vqmod/vqmod.log';
public $vqCachePath = 'vqmod/vqcache/';
public $protectedFilelist = 'vqmod/vqprotect.txt';
public $logging = true;
public $cacheTime = 5; // local=5secs live=60secs
public $log;
/**
* VQMod::__construct()
*
* #param bool $path File path to use
* #param bool $logging Enable/disabled logging
* #return null
* #description Startup of VQMod
*/
public function __construct($path = false, $logging = true) {
if(!class_exists('DOMDocument')) {
die('ERROR - YOU NEED DOMDocument INSTALLED TO USE VQMod');
}
if(!$path){
$path = dirname(dirname(__FILE__));
}
$this->_setCwd($path);
$this->logging = (bool) $logging;
$this->log = new VQModLog($this);
$this->_getMods();
$this->_loadProtected();
}
/**
* VQMod::modCheck()
*
* #param string $sourceFile path for file
* #return string
* #description Checks if a file has modifications and applies them, returning cache files or the file name
*/
public function modCheck($sourceFile) {
if(!preg_match('%^([a-z]:)?[\\\\/]%i', $sourceFile)) {
$sourcePath = $this->path($sourceFile);
} else {
$sourcePath = realpath($sourceFile);
}
if(!$sourcePath || is_dir($sourcePath) || in_array($sourcePath, $this->_doNotMod)) {
return $sourceFile;
}
$stripped_filename = preg_replace('~^' . preg_quote($this->getCwd(), '~') . '~', '', $sourcePath);
$cacheFile = $this->_cacheName($stripped_filename);
if($this->useCache && file_exists($cacheFile)) {
//return $cacheFile; // useCache being Deprecated in favor of cacheTime
}
if(isset($this->_filesModded[$sourcePath])) {
return $this->_filesModded[$sourcePath]['cached'] ? $cacheFile : $sourceFile;
}
$changed = false;
$fileHash = sha1_file($sourcePath);
$fileData = file_get_contents($sourcePath);
foreach($this->_mods as $modObject) {
foreach($modObject->mods as $path => $mods) {
if($this->_checkMatch($path, $sourcePath)) {
$modObject->applyMod($mods, $fileData);
}
}
}
// START QPHORIA CACHELOCK CODE
//
if (sha1($fileData) != $fileHash) {
$writePath = $cacheFile;
$cacheLock = false;
if(file_exists($writePath) && ((filemtime($writePath) + (float)$this->cacheTime) >= time())) {
$cacheLock = true;
$changed = true;
}
if(!$cacheLock && (!file_exists($writePath) || is_writable($writePath))) {
file_put_contents($writePath, $fileData);
$changed = true;
} else {
//file_put_contents('./cachelock.txt', "$writePath \r\n", FILE_APPEND); // debugging only.
}
//file_put_contents('./cachetotal.txt', "$writePath \r\n", FILE_APPEND);
} // END QPHORIA CACHELOCK CODE
/* Original Code
if(sha1($fileData) != $fileHash) {
$writePath = $this->_virtualMode ? $cacheFile : $sourcePath;
if(!file_exists($writePath) || is_writable($writePath)) {
file_put_contents($writePath, $fileData);
$changed = true;
}
}*/
$this->_filesModded[$sourcePath] = array('cached' => $changed);
return $changed ? $writePath : $sourcePath;
}
/**
* VQMod::path()
*
* #param string $path File path
* #param bool $skip_real If true path is full not relative
* #return bool, string
* #description Returns the full true path of a file if it exists, otherwise false
*/
public function path($path, $skip_real = false) {
$tmp = $this->_cwd . $path;
$realpath = $skip_real ? $tmp : realpath($tmp);
if(!$realpath) {
return false;
}
if(is_dir($realpath)) {
$realpath = rtrim($realpath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
return $realpath;
}
/**
* VQMod::getCwd()
*
* #return string
* #description Returns current working directory
*/
public function getCwd() {
return $this->_cwd;
}
/**
* VQMod::_getMods()
*
* #return null
* #description Gets list of XML files in vqmod xml folder for processing
*/
private function _getMods() {
$this->_modFileList = glob($this->path('vqmod/xml/') . '*.xml');
if($this->_modFileList) {
$this->_parseMods();
} else {
$this->log->write('NO MODS IN USE');
}
}
/**
* VQMod::_parseMods()
*
* #return null
* #description Loops through xml files and attempts to load them as VQModObject's
*/
private function _parseMods() {
$dom = new DOMDocument('1.0', 'UTF-8');
foreach($this->_modFileList as $modFileKey => $modFile) {
if(file_exists($modFile)) {
if(#$dom->load($modFile)) {
$mod = $dom->getElementsByTagName('modification')->item(0);
$this->_mods[] = new VQModObject($mod, $modFile, $this);
} else {
$this->log->write('DOM UNABLE TO LOAD: ' . $modFile);
}
} else {
$this->log->write('FILE NOT FOUND: ' . $modFile);
}
}
}
/**
* VQMod::_loadProtected()
*
* #return null
* #description Loads protected list and adds them to _doNotMod array
*/
private function _loadProtected() {
$file = $this->path($this->protectedFilelist);
if($file && is_file($file)) {
$protected = file_get_contents($file);
if(!empty($protected)) {
$protected = preg_replace('~\r?\n~', "\n", $protected);
$paths = explode("\n", $protected);
foreach($paths as $path) {
$fullPath = $this->path($path);
if($fullPath && !in_array($fullPath, $this->_doNotMod)) {
$this->_doNotMod[] = $fullPath;
}
}
}
}
}
/**
* VQMod::_cacheName()
*
* #param string $file Filename to be converted to cache filename
* #return string
* #description Returns cache file name for a path
*/
private function _cacheName($file) {
return $this->path($this->vqCachePath) . 'vq2-' . preg_replace('~[/\\\\]+~', '_', $file);
}
/**
* VQMod::_setCwd()
*
* #param string $path Path to be used as current working directory
* #return null
* #description Sets the current working directory variable
*/
private function _setCwd($path) {
$realpath = realpath($path);
if(!$realpath) {
die('COULDNT RESOLVE CWD REALPATH');
}
$this->_cwd = rtrim($realpath, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
/**
* VQMod::_checkMatch()
*
* #param string $modFilePath Modification path from a <file> node
* #param string $checkFilePath File path
* #return bool
* #description Checks a modification path against a file path
*/
private function _checkMatch($modFilePath, $checkFilePath) {
$modFilePath = str_replace('\\', '/', $modFilePath);
$checkFilePath = str_replace('\\', '/', $checkFilePath);
$modFilePath = preg_replace('/([^*]+)/e', 'preg_quote("$1", "~")', $modFilePath);
$modFilePath = str_replace('*', '[^/]*', $modFilePath);
$return = (bool) preg_match('~^' . $modFilePath . '$~', $checkFilePath);
return $return;
}
}
/**
* VQModLog
* #description Object to log information to a file
*/
class VQModLog {
private $_sep;
private $_vqmod;
private $_defhash = 'da39a3ee5e6b4b0d3255bfef95601890afd80709';
private $_logs = array();
/**
* VQModLog::__construct()
*
* #param VQMod $vqmod VQMod main class as reference
* #return null
* #description Object instantiation method
*/
public function __construct(VQMod $vqmod) {
$this->_vqmod = $vqmod;
$this->_sep = str_repeat('-', 70);
}
/**
* VQModLog::__destruct()
*
* #return null
* #description Logs any messages to the log file just before object is destroyed
*/
public function __destruct() {
if(empty($this->_logs) || $this->_vqmod->logging == false) {
return;
}
$txt = array();
$txt[] = str_repeat('-', 10) . ' Date: ' . date('Y-m-d H:i:s') . ' ~ IP : ' . (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : 'N/A') . ' ' . str_repeat('-', 10);
$txt[] = 'REQUEST URI : ' . $_SERVER['REQUEST_URI'];
foreach($this->_logs as $count => $log) {
if($log['obj']) {
$vars = get_object_vars($log['obj']);
$txt[] = 'MOD DETAILS:';
foreach($vars as $k => $v) {
if(is_string($v)) {
$txt[] = ' ' . str_pad($k, 10, ' ', STR_PAD_RIGHT) . ': ' . $v;
}
}
}
foreach($log['log'] as $msg) {
$txt[] = $msg;
}
if ($count > count($this->_logs)-1) {
$txt[] = '';
}
}
$txt[] = $this->_sep;
$txt[] = str_repeat(PHP_EOL, 2);
$logPath = $this->_vqmod->path($this->_vqmod->logFilePath, true);
if(!file_exists($logPath)) {
$res = file_put_contents($logPath, '');
if($res === false) {
die('COULD NOT WRITE TO LOG FILE');
}
}
file_put_contents($logPath, implode(PHP_EOL, $txt), FILE_APPEND);
}
/**
* VQModLog::write()
*
* #param string $data Text to be added to log file
* #param VQModObject $obj Modification the error belongs to
* #return null
* #description Adds error to log object ready to be output
*/
public function write($data, VQModObject $obj = NULL) {
if($obj) {
$hash = sha1($obj->id);
} else {
$hash = $this->_defhash;
}
if(empty($this->_logs[$hash])) {
$this->_logs[$hash] = array(
'obj' => $obj,
'log' => array()
);
}
$this->_logs[$hash]['log'][] = $data;
}
}
/**
* VQModObject
* #description Object for the <modification> that orchestrates each applied modification
*/
class VQModObject {
public $modFile = '';
public $id = '';
public $version = '';
public $vqmver = '';
public $author = '';
public $mods = array();
private $_vqmod;
private $_skip = false;
/**
* VQModObject::__construct()
*
* #param DOMNode $node <modification> node
* #param string $modFile File modification is from
* #param VQMod $vqmod VQMod object as reference
* #return null
* #description Loads modification meta information
*/
public function __construct(DOMNode $node, $modFile, VQmod $vqmod) {
if($node->hasChildNodes()) {
foreach($node->childNodes as $child) {
$name = (string) $child->nodeName;
if(isset($this->$name)) {
$this->$name = (string) $child->nodeValue;
}
}
}
$this->modFile = $modFile;
$this->_vqmod = $vqmod;
$this->_parseMods($node);
}
/**
* VQModObject::skip()
*
* #return bool
* #description Returns the skip status of a modification
*/
public function skip() {
return $this->_skip;
}
/**
* VQModObject::applyMod()
*
* #param array $mods Array of search add nodes
* #param string $data File contents to be altered
* #return null
* #description Applies all modifications to the text data
*/
public function applyMod($mods, &$data) {
if($this->_skip) return;
$tmp = $data;
foreach($mods as $mod) {
$indexCount = 0;
$tmp = $this->_explodeData($tmp);
$lineMax = count($tmp) - 1;
switch($mod['search']->position) {
case 'top':
$tmp[$mod['search']->offset] = $mod['add']->getContent() . $tmp[$mod['search']->offset];
break;
case 'bottom':
$offset = $lineMax - $mod['search']->offset;
if($offset < 0){
$tmp[-1] = $mod['add']->getContent();
} else {
$tmp[$offset] .= $mod['add']->getContent();
}
break;
case 'all':
$tmp = array($mod['add']->getContent());
break;
default:
$changed = false;
foreach($tmp as $lineNum => $line) {
if($mod['search']->regex == 'true') {
$pos = #preg_match($mod['search']->getContent(), $line);
if($pos === false) {
if($mod['error'] == 'log' || $mod['error'] == 'abort' ) {
$this->_vqmod->log->write('INVALID REGEX ERROR - ' . $mod['search']->getContent(), $this);
}
continue 2;
} elseif($pos == 0) {
$pos = false;
}
} else {
$pos = strpos($line, $mod['search']->getContent());
}
if($pos !== false) {
$indexCount++;
$changed = true;
if(!$mod['search']->indexes() || ($mod['search']->indexes() && in_array($indexCount, $mod['search']->indexes()))) {
switch($mod['search']->position) {
case 'before':
$offset = ($lineNum - $mod['search']->offset < 0) ? -1 : $lineNum - $mod['search']->offset;
$tmp[$offset] = empty($tmp[$offset]) ? $mod['add']->getContent() : $mod['add']->getContent() . "\n" . $tmp[$offset];
break;
case 'after':
$offset = ($lineNum + $mod['search']->offset > $lineMax) ? $lineMax : $lineNum + $mod['search']->offset;
$tmp[$offset] = $tmp[$offset] . "\n" . $mod['add']->getContent();
break;
default:
if(!empty($mod['search']->offset)) {
for($i = 1; $i <= $mod['search']->offset; $i++) {
if(isset($tmp[$lineNum + $i])) {
$tmp[$lineNum + $i] = '';
}
}
}
if($mod['search']->regex == 'true') {
$tmp[$lineNum] = preg_replace($mod['search']->getContent(), $mod['add']->getContent(), $line);
} else {
$tmp[$lineNum] = str_replace($mod['search']->getContent(), $mod['add']->getContent(), $line);
}
break;
}
}
}
}
if(!$changed) {
$skip = ($mod['error'] == 'skip' || $mod['error'] == 'log') ? ' (SKIPPED)' : ' (ABORTING MOD)';
if($mod['error'] == 'log' || $mod['error'] == 'abort') {
$this->_vqmod->log->write('SEARCH NOT FOUND' . $skip . ': ' . $mod['search']->getContent(), $this);
}
if($mod['error'] == 'abort') {
$this->_skip = true;
return;
}
}
break;
}
ksort($tmp);
$tmp = $this->_implodeData($tmp);
}
$data = $tmp;
}
/**
* VQModObject::_parseMods()
*
* #param DOMNode $node <modification> node to be parsed
* #return null
* #description Parses modifications in preparation for the applyMod method to work
*/
private function _parseMods(DOMNode $node){
$files = $node->getElementsByTagName('file');
foreach($files as $file) {
$fileToMod = $file->getAttribute('name');
$error = ($file->hasAttribute('error')) ? $file->getAttribute('error') : 'log';
$fullPath = $this->_vqmod->path($fileToMod);
if(!$fullPath){
if(strpos($fileToMod, '*') !== false) {
$fullPath = $this->_vqmod->getCwd() . $fileToMod;
} else {
if ($error == 'log' || $error == 'abort') {
$skip = ($error == 'log') ? ' (SKIPPED)' : ' (ABORTING MOD)';
$this->_vqmod->log->write('Could not resolve path for [' . $fileToMod . ']' . $skip, $this);
}
if ($error == 'log' || $error == 'skip') {
continue;
} elseif ($error == 'abort') {
return false;
}
}
}
$operations = $file->getElementsByTagName('operation');
foreach($operations as $operation) {
$error = ($operation->hasAttribute('error')) ? $operation->getAttribute('error') : 'abort';
$this->mods[$fullPath][] = array(
'search' => new VQSearchNode($operation->getElementsByTagName('search')->item(0)),
'add' => new VQAddNode($operation->getElementsByTagName('add')->item(0)),
'error' => $error
);
}
}
}
/**
* VQModObject::_explodeData()
*
* #param string $data File contents
* #return string
* #description Splits a file into an array of individual lines
*/
private function _explodeData($data) {
return explode("\n", $data);
}
/**
* VQModObject::_implodeData()
*
* #param array $data Array of lines
* #return string
* #description Joins an array of lines back into a text file
*/
private function _implodeData($data) {
return implode("\n", $data);
}
}
/**
* VQNode
* #description Basic node object blueprint
*/
class VQNode {
public $trim = 'false';
private $_content = '';
/**
* VQNode::__construct()
*
* #param DOMNode $node Search/add node
* #return null
* #description Parses the node attributes and sets the node property
*/
public function __construct(DOMNode $node) {
$this->_content = $node->nodeValue;
if($node->hasAttributes()) {
foreach($node->attributes as $attr) {
$name = $attr->nodeName;
if(isset($this->$name)) {
$this->$name = $attr->nodeValue;
}
}
}
}
/**
* VQNode::getContent()
*
* #return string
* #description Returns the content, trimmed if applicable
*/
public function getContent() {
$content = ($this->trim == 'true') ? trim($this->_content) : $this->_content;
return $content;
}
}
/**
* VQSearchNode
* #description Object for the <search> xml tags
*/
class VQSearchNode extends VQNode {
public $position = 'replace';
public $offset = 0;
public $index = 'false';
public $regex = 'false';
public $trim = 'true';
/**
* VQSearchNode::indexes()
*
* #return bool, array
* #description Returns the index values to use the search on, or false if none
*/
public function indexes() {
if($this->index == 'false') {
return false;
}
$tmp = explode(',', $this->index);
foreach($tmp as $k => $v) {
if(!is_int($v)) {
unset($k);
}
}
$tmp = array_unique($tmp);
return empty($tmp) ? false : $tmp;
}
}
/**
* VQAddNode
* #description Object for the <add> xml tags
*/
class VQAddNode extends VQNode {
}
Also couple of other ideas to make debugging even easier:
List any other vqmod files which have previously edited this same file.
This is another common issue where I find when two extensions are editing the same file and the latter is causing the error but it would be useful to know about any other vqmods editing the same file. Yes I suppose I could add error="skip" to everything but dont think this is the best approach to just hide all of the errors, the user should be made aware there is an error...
"Suggested Fix", maybe some smart way you can test what type of error it is.
Contradict what I said above but even at its most basic form you could suggest hiding the error if its not essential. So that anybody can read it and understand how it fix it.
E.g
OPEN: vqmod/xml/templace.xml (line:23)
FIND: <operation>
REPLACE <operation error="skip">
Adding the line number in the XML file the error is coming from. It would be lovely not having to search all of the time and could quickly go to the line number in the vqmod
The issue for the file being edited is certainly one that is way overdue and one I plan on adding in the next release of vQmod. As for the other suggestions
Interesting idea, and one that could certainly be considered. The only problem I see with this is that it would possibly make some log files enormous
This is going to be next to impossible to incorporate
This is impossible without some pretty expensive runtime. The error doesn't lie in the XML as such, so would require re-opening the xml that's been parsed, searching for the line in question line by line and then reporting that. it sounds simple, but you have to remember that xml's can have the same search parameter for multiple operations - so in that situation you'd be no better off than searching the file yourself

Categories