Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

113 lines
2.7KB

  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. $processes = 0;
  19. $states = array();
  20. foreach ($r AS $process) {
  21. $state = trim($process["State"]);
  22. // Filter out all non blocking processes
  23. if (!in_array($state, array("", "init", "statistics", "updating"))) {
  24. ++$states[$state];
  25. ++$processes;
  26. }
  27. }
  28. $statelist = "";
  29. foreach ($states AS $state => $usage) {
  30. if ($statelist != "")
  31. $statelist .= ", ";
  32. $statelist .= $state.": ".$usage;
  33. }
  34. return(array("list" => $statelist, "amount" => $processes));
  35. }
  36. /**
  37. * Checks if $array is a filled array with at least one entry.
  38. *
  39. * @param $array mixed A filled array with at least one entry
  40. * @return Whether $array is a filled array
  41. */
  42. public static function is_result($array) {
  43. // It could be a return value from an update statement
  44. if (is_bool($array)) {
  45. return $array;
  46. }
  47. if (is_object($array)) {
  48. return true;
  49. }
  50. return (is_array($array) && count($array) > 0);
  51. }
  52. /**
  53. * @brief Callback function for "esc_array"
  54. *
  55. * @param mixed $value Array value
  56. * @param string $key Array key
  57. * @param boolean $add_quotation add quotation marks for string values
  58. */
  59. private static function esc_array_callback(&$value, $key, $add_quotation) {
  60. if (!$add_quotation) {
  61. if (is_bool($value)) {
  62. $value = ($value ? '1' : '0');
  63. } else {
  64. $value = dbesc($value);
  65. }
  66. return;
  67. }
  68. if (is_bool($value)) {
  69. $value = ($value ? 'true' : 'false');
  70. } elseif (is_float($value) OR is_integer($value)) {
  71. $value = (string)$value;
  72. } else {
  73. $value = "'".dbesc($value)."'";
  74. }
  75. }
  76. /**
  77. * @brief Escapes a whole array
  78. *
  79. * @param mixed $arr Array with values to be escaped
  80. * @param boolean $add_quotation add quotation marks for string values
  81. */
  82. public static function esc_array(&$arr, $add_quotation = false) {
  83. array_walk($arr, 'self::esc_array_callback', $add_quotation);
  84. }
  85. /**
  86. * Checks Converts any date string into a SQL compatible date string
  87. *
  88. * @param string $date a date string in any format
  89. * @return string SQL style date string
  90. */
  91. public static function date($date = 'now') {
  92. $timestamp = strtotime($date);
  93. // Don't allow lower date strings as '0001-01-01 00:00:00'
  94. if ($timestamp < -62135596800) {
  95. $timestamp = -62135596800;
  96. }
  97. return date('Y-m-d H:i:s', $timestamp);
  98. }
  99. }