From 85ec17f3e14d4f779a539ea63f595928a1dc8472 Mon Sep 17 00:00:00 2001
From: Daniel Barbero Martin <daniel.barbero@artica.es>
Date: Fri, 26 Feb 2021 08:36:48 +0100
Subject: [PATCH] WIP Command center

---
 pandora_console/include/db/mysql.php     | 67 ++++++++++++++++++++++++
 pandora_console/include/functions_db.php | 17 ++++++
 2 files changed, 84 insertions(+)

diff --git a/pandora_console/include/db/mysql.php b/pandora_console/include/db/mysql.php
index 13e59f7247..6f997e6d96 100644
--- a/pandora_console/include/db/mysql.php
+++ b/pandora_console/include/db/mysql.php
@@ -1499,3 +1499,70 @@ function db_run_sql_file($location)
         return false;
     }
 }
+
+
+/**
+ * Inserts multiples strings into database.
+ *
+ * @param string $table  Table to insert into.
+ * @param mixed  $values A single value or array of values to insert
+ *  (can be a multiple amount of rows).
+ *
+ * @return mixed False in case of error or invalid values passed.
+ * Affected rows otherwise.
+ */
+function mysql_db_process_sql_insert_multiple($table, $values)
+{
+    // Empty rows or values not processed.
+    if (empty($values) === true || is_array($values) === false) {
+        return false;
+    }
+
+    $query = sprintf('INSERT INTO `%s`', $table);
+
+    $j = 1;
+    $max_total = count($values);
+    foreach ($values as $key => $value) {
+        $fields = [];
+        $values_str = '';
+        $i = 1;
+        $max = count($value);
+        foreach ($value as $k => $v) {
+            if ($j === 1) {
+                // Add the correct escaping to values.
+                $field = sprintf('`%s`', $k);
+                array_push($fields, $field);
+            }
+
+            if (isset($v) === false) {
+                $values_str .= 'NULL';
+            } else if (is_int($v) || is_bool($v)) {
+                $values_str .= sprintf('%d', $v);
+            } else if (is_float($v) || is_double($v)) {
+                $values_str .= sprintf('%f', $v);
+            } else {
+                $values_str .= sprintf("'%s'", $v);
+            }
+
+            if ($i < $max) {
+                $values_str .= ',';
+            }
+
+            $i++;
+        }
+
+        if ($j === 1) {
+            $query .= sprintf(' (%s) VALUES', implode(', ', $fields));
+        }
+
+        $query .= ' ('.$values_str.')';
+
+        if ($j < $max_total) {
+            $query .= ',';
+        }
+
+        $j++;
+    }
+
+    return db_process_sql($query, 'insert_id');
+}
diff --git a/pandora_console/include/functions_db.php b/pandora_console/include/functions_db.php
index f7d264d0c4..601d2a8f05 100644
--- a/pandora_console/include/functions_db.php
+++ b/pandora_console/include/functions_db.php
@@ -2162,3 +2162,20 @@ function db_release_lock($lockname)
         )
     );
 }
+
+
+/**
+ * Inserts multiples strings into database
+ *
+ * @param string $table  Table to insert into
+ * @param mixed  $values A single value or array of values to insert
+ *  (can be a multiple amount of rows).
+ *
+ * @return mixed False in case of error or invalid values passed.
+ * Affected rows otherwise.
+ */
+function db_process_sql_insert_multiple($table, $values)
+{
+    global $config;
+    return mysql_db_process_sql_insert_multiple($table, $values);
+}