]> git.mxchange.org Git - friendica.git/blob - include/dbm.php
Relocate class files from /include to /src/
[friendica.git] / include / dbm.php
1 <?php
2 /**
3  * @brief This class contain functions for the database management
4  *
5  * This class contains functions that doesn't need to know if pdo, mysqli or whatever is used.
6  */
7 class dbm {
8         /**
9          * @brief Return a list of database processes
10          *
11          * @return array
12          *      'list' => List of processes, separated in their different states
13          *      'amount' => Number of concurrent database processes
14          */
15         public static function processlist() {
16                 $r = q("SHOW PROCESSLIST");
17                 $s = array();
18
19                 $processes = 0;
20                 $states = array();
21                 foreach ($r AS $process) {
22                         $state = trim($process["State"]);
23
24                         // Filter out all non blocking processes
25                         if (!in_array($state, array("", "init", "statistics", "updating"))) {
26                                 ++$states[$state];
27                                 ++$processes;
28                         }
29                 }
30
31                 $statelist = "";
32                 foreach ($states AS $state => $usage) {
33                         if ($statelist != "")
34                                 $statelist .= ", ";
35                         $statelist .= $state.": ".$usage;
36                 }
37                 return(array("list" => $statelist, "amount" => $processes));
38         }
39
40         /**
41          * Checks if $array is a filled array with at least one entry.
42          *
43          * @param       $array  mixed   A filled array with at least one entry
44          * @return      Whether $array is a filled array or an object with rows
45          */
46         public static function is_result($array) {
47                 // It could be a return value from an update statement
48                 if (is_bool($array)) {
49                         return $array;
50                 }
51
52                 if (is_object($array)) {
53                         return dba::num_rows($array) > 0;
54                 }
55
56                 return (is_array($array) && (count($array) > 0));
57         }
58
59         /**
60          * @brief Callback function for "esc_array"
61          *
62          * @param mixed $value Array value
63          * @param string $key Array key
64          * @param boolean $add_quotation add quotation marks for string values
65          */
66         private static function esc_array_callback(&$value, $key, $add_quotation) {
67
68                 if (!$add_quotation) {
69                         if (is_bool($value)) {
70                                 $value = ($value ? '1' : '0');
71                         } else {
72                                 $value = dbesc($value);
73                         }
74                         return;
75                 }
76
77                 if (is_bool($value)) {
78                         $value = ($value ? 'true' : 'false');
79                 } elseif (is_float($value) || is_integer($value)) {
80                         $value = (string)$value;
81                 } else {
82                          $value = "'".dbesc($value)."'";
83                 }
84         }
85
86         /**
87          * @brief Escapes a whole array
88          *
89          * @param mixed $arr Array with values to be escaped
90          * @param boolean $add_quotation add quotation marks for string values
91          */
92         public static function esc_array(&$arr, $add_quotation = false) {
93                 array_walk($arr, 'self::esc_array_callback', $add_quotation);
94         }
95
96         /**
97          * Checks Converts any date string into a SQL compatible date string
98          *
99          * @param string $date a date string in any format
100          * @return string SQL style date string
101          */
102         public static function date($date = 'now') {
103                 $timestamp = strtotime($date);
104
105                 // Don't allow lower date strings as '0001-01-01 00:00:00'
106                 if ($timestamp < -62135596800) {
107                         $timestamp = -62135596800;
108                 }
109
110                 return date('Y-m-d H:i:s', (int)$timestamp);
111         }
112 }