2016-06-01 07:04:31 +02:00
|
|
|
<?php
|
2016-06-06 06:29:03 +02:00
|
|
|
/**
|
|
|
|
* @brief This class contain functions for the database management
|
|
|
|
*
|
2017-04-25 18:05:26 +02:00
|
|
|
* This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
|
2016-06-06 06:29:03 +02:00
|
|
|
*/
|
2016-06-01 07:04:31 +02:00
|
|
|
class dbm {
|
2016-06-06 06:29:03 +02:00
|
|
|
/**
|
|
|
|
* @brief Return a list of database processes
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
* 'list' => List of processes, separated in their different states
|
|
|
|
* 'amount' => Number of concurrent database processes
|
|
|
|
*/
|
2016-06-01 07:04:31 +02:00
|
|
|
public static function processlist() {
|
|
|
|
$r = q("SHOW PROCESSLIST");
|
|
|
|
$s = array();
|
|
|
|
|
2016-06-04 11:04:26 +02:00
|
|
|
$processes = 0;
|
2016-06-01 07:04:31 +02:00
|
|
|
$states = array();
|
|
|
|
foreach ($r AS $process) {
|
|
|
|
$state = trim($process["State"]);
|
2016-06-06 06:29:03 +02:00
|
|
|
|
2016-09-03 17:06:42 +02:00
|
|
|
// Filter out all non blocking processes
|
|
|
|
if (!in_array($state, array("", "init", "statistics", "updating"))) {
|
2016-06-01 07:04:31 +02:00
|
|
|
++$states[$state];
|
2016-06-04 11:04:26 +02:00
|
|
|
++$processes;
|
|
|
|
}
|
2016-06-01 07:04:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$statelist = "";
|
|
|
|
foreach ($states AS $state => $usage) {
|
|
|
|
if ($statelist != "")
|
|
|
|
$statelist .= ", ";
|
|
|
|
$statelist .= $state.": ".$usage;
|
|
|
|
}
|
2016-06-03 23:10:23 +02:00
|
|
|
return(array("list" => $statelist, "amount" => $processes));
|
2016-06-01 07:04:31 +02:00
|
|
|
}
|
2016-07-02 14:00:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if $array is a filled array with at least one entry.
|
|
|
|
*
|
|
|
|
* @param $array mixed A filled array with at least one entry
|
2017-08-15 08:08:26 +02:00
|
|
|
* @return Whether $array is a filled array or an object with rows
|
2016-07-02 14:00:42 +02:00
|
|
|
*/
|
2016-07-25 11:46:14 +02:00
|
|
|
public static function is_result($array) {
|
2016-10-14 07:45:32 +02:00
|
|
|
// It could be a return value from an update statement
|
2016-10-22 12:14:41 +02:00
|
|
|
if (is_bool($array)) {
|
2016-10-14 07:45:32 +02:00
|
|
|
return $array;
|
2016-10-22 12:14:41 +02:00
|
|
|
}
|
2017-04-24 01:15:38 +02:00
|
|
|
|
|
|
|
if (is_object($array)) {
|
2017-08-15 08:08:26 +02:00
|
|
|
return dba::num_rows($array) > 0;
|
2017-04-24 01:15:38 +02:00
|
|
|
}
|
|
|
|
|
2017-08-15 08:08:26 +02:00
|
|
|
return (is_array($array) && (count($array) > 0));
|
2016-07-02 14:00:42 +02:00
|
|
|
}
|
2017-01-28 13:19:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Callback function for "esc_array"
|
|
|
|
*
|
|
|
|
* @param mixed $value Array value
|
|
|
|
* @param string $key Array key
|
2017-01-28 14:38:18 +01:00
|
|
|
* @param boolean $add_quotation add quotation marks for string values
|
2017-01-28 13:19:04 +01:00
|
|
|
*/
|
|
|
|
private static function esc_array_callback(&$value, $key, $add_quotation) {
|
|
|
|
|
|
|
|
if (!$add_quotation) {
|
|
|
|
if (is_bool($value)) {
|
|
|
|
$value = ($value ? '1' : '0');
|
|
|
|
} else {
|
|
|
|
$value = dbesc($value);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_bool($value)) {
|
|
|
|
$value = ($value ? 'true' : 'false');
|
2017-06-08 04:00:59 +02:00
|
|
|
} elseif (is_float($value) || is_integer($value)) {
|
2017-01-28 13:19:04 +01:00
|
|
|
$value = (string)$value;
|
|
|
|
} else {
|
|
|
|
$value = "'".dbesc($value)."'";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Escapes a whole array
|
|
|
|
*
|
|
|
|
* @param mixed $arr Array with values to be escaped
|
2017-01-28 14:38:18 +01:00
|
|
|
* @param boolean $add_quotation add quotation marks for string values
|
2017-01-28 13:19:04 +01:00
|
|
|
*/
|
|
|
|
public static function esc_array(&$arr, $add_quotation = false) {
|
|
|
|
array_walk($arr, 'self::esc_array_callback', $add_quotation);
|
|
|
|
}
|
2017-02-22 23:04:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks Converts any date string into a SQL compatible date string
|
|
|
|
*
|
|
|
|
* @param string $date a date string in any format
|
|
|
|
* @return string SQL style date string
|
|
|
|
*/
|
2017-02-23 06:45:06 +01:00
|
|
|
public static function date($date = 'now') {
|
2017-02-22 23:04:56 +01:00
|
|
|
$timestamp = strtotime($date);
|
2017-02-23 08:09:55 +01:00
|
|
|
|
2017-03-18 12:42:54 +01:00
|
|
|
// Don't allow lower date strings as '0001-01-01 00:00:00'
|
2017-02-23 08:09:55 +01:00
|
|
|
if ($timestamp < -62135596800) {
|
2017-03-18 12:42:54 +01:00
|
|
|
$timestamp = -62135596800;
|
2017-02-23 08:09:55 +01:00
|
|
|
}
|
|
|
|
|
2017-02-23 06:45:06 +01:00
|
|
|
return date('Y-m-d H:i:s', $timestamp);
|
2017-02-22 23:04:56 +01:00
|
|
|
}
|
2016-06-01 07:04:31 +02:00
|
|
|
}
|