| @@ -1,9 +1,8 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | 3 | namespace BitCode\BitForm\Core\Database; |
| 4 | 4 | |
| 5 | -use BitCode\BitForm\Core\Database\Model; | |
| 6 | 5 | use BitCode\BitForm\Core\Util\IpTool; |
| 7 | 6 | |
| 8 | 7 | /** |
| 9 | 8 | * Undocumented class |
| @@ -10,111 +9,178 @@ | ||
| 10 | 9 | */ |
| 11 | 10 | |
| 12 | 11 | class ApiModel extends Model |
| 13 | 12 | { |
| 14 | - public function __construct() | |
| 15 | - { | |
| 16 | - global $wpdb; | |
| 17 | - $this->_wpdb = $wpdb; | |
| 18 | - } | |
| 19 | - public function getForm() | |
| 20 | - { | |
| 13 | + public $_wpdb; | |
| 21 | 14 | |
| 22 | - $result = $this->_wpdb->get_results( | |
| 23 | - " | |
| 15 | + public function __construct() | |
| 16 | + { | |
| 17 | + global $wpdb; | |
| 18 | + $this->_wpdb = $wpdb; | |
| 19 | + } | |
| 20 | + | |
| 21 | + public function getForm() | |
| 22 | + { | |
| 23 | + $result = $this->_wpdb->get_results( | |
| 24 | + " | |
| 24 | 25 | SELECT form_name,id FROM `{$this->_wpdb->prefix}bitforms_form` WHERE `status`=1 order By created_at DESC |
| 25 | 26 | " |
| 26 | - ); | |
| 27 | - return $result; | |
| 28 | - } | |
| 27 | + ); | |
| 28 | + return $result; | |
| 29 | + } | |
| 29 | 30 | |
| 30 | - public function getField($id) | |
| 31 | - { | |
| 32 | - $result = $this->_wpdb->get_results( | |
| 33 | - " | |
| 34 | - SELECT form_content,id FROM `{$this->_wpdb->prefix}bitforms_form` WHERE `status`=1 AND `id`='$id' | |
| 35 | - " | |
| 36 | - ); | |
| 37 | - return $result; | |
| 31 | + public function getField($id) | |
| 32 | + { | |
| 33 | + $result = $this->_wpdb->get_results( | |
| 34 | + $this->_wpdb->prepare( | |
| 35 | + "SELECT form_content,id FROM `{$this->_wpdb->prefix}bitforms_form` WHERE `status`=1 AND `id`=%d", | |
| 36 | + $id | |
| 37 | + ) | |
| 38 | + ); | |
| 39 | + return $result; | |
| 40 | + } | |
| 41 | + | |
| 42 | + public function editEntry($entryID) | |
| 43 | + { | |
| 44 | + $result = $this->_wpdb->get_results( | |
| 45 | + $this->_wpdb->prepare( | |
| 46 | + "SELECT bitforms_form_entry_id,meta_key,meta_value FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `bitforms_form_entry_id`=%d", | |
| 47 | + $entryID | |
| 48 | + ) | |
| 49 | + ); | |
| 50 | + return $result; | |
| 51 | + } | |
| 52 | + | |
| 53 | + public function entryDelete($entryID) | |
| 54 | + { | |
| 55 | + $sql = $this->_wpdb->prepare( | |
| 56 | + "DELETE FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `bitforms_form_entry_id` = %d", | |
| 57 | + $entryID | |
| 58 | + ); | |
| 59 | + $result = $this->_wpdb->query($sql); | |
| 60 | + return $result; | |
| 61 | + } | |
| 62 | + | |
| 63 | + public function findRecord($table_name, $column, $value) | |
| 64 | + { | |
| 65 | + // Identifiers (table/column) cannot be parameterized with wpdb placeholders. | |
| 66 | + // Sanitize identifiers to prevent SQL injection through dynamic identifiers. | |
| 67 | + $safeTable = preg_replace('/[^A-Za-z0-9_]/', '', (string) $table_name); | |
| 68 | + $safeColumn = preg_replace('/[^A-Za-z0-9_]/', '', (string) $column); | |
| 69 | + $table_name = $this->_wpdb->prefix . $safeTable; | |
| 70 | + $value = is_scalar($value) ? $value : ''; // Ensure value is scalar for placeholder. | |
| 71 | + if ('' === $safeTable || '' === $safeColumn) { | |
| 72 | + return []; | |
| 38 | 73 | } |
| 39 | 74 | |
| 40 | - public function editEntry($entryID) | |
| 41 | - { | |
| 42 | - $result = $this->_wpdb->get_results( | |
| 43 | - " | |
| 44 | - SELECT bitforms_form_entry_id,meta_key,meta_value FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `bitforms_form_entry_id`='$entryID' | |
| 45 | - " | |
| 46 | - ); | |
| 47 | - return $result; | |
| 48 | - } | |
| 75 | + $sql = $this->_wpdb->prepare( | |
| 76 | + 'SELECT `%1$s` FROM `%2$s` WHERE `%1$s`=%3$s', | |
| 77 | + $safeColumn, | |
| 78 | + $table_name, | |
| 79 | + $value | |
| 80 | + ); | |
| 81 | + return $this->_wpdb->get_results($sql); | |
| 82 | + } | |
| 49 | 83 | |
| 50 | - public function entryDelete( $entryID ) | |
| 51 | - { | |
| 52 | - $sql = "DELETE FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE `id` = $entryID"; | |
| 53 | - $result = $this->_wpdb->query($sql); | |
| 54 | - return $result; | |
| 55 | - } | |
| 84 | + public function noteCreate($formID, $entryID, $note_details) | |
| 85 | + { | |
| 86 | + $ipTool = new IpTool(); | |
| 87 | + $user_details = $ipTool->getUserDetail(); | |
| 88 | + $result = $this->_wpdb->insert( | |
| 89 | + "{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo", | |
| 90 | + [ | |
| 91 | + 'info_type' => 'note', | |
| 92 | + 'info_details' => $note_details, | |
| 93 | + 'form_id' => $formID, | |
| 94 | + 'entry_id' => $entryID, | |
| 95 | + 'user_id' => $user_details['id'], | |
| 96 | + 'user_ip' => $user_details['ip'], | |
| 97 | + 'created_at' => $user_details['time'], | |
| 98 | + ] | |
| 99 | + ); | |
| 100 | + return $result; | |
| 101 | + } | |
| 56 | 102 | |
| 57 | - public function findRecord($table_name, $column, $value) | |
| 58 | - { | |
| 103 | + public function noteList() | |
| 104 | + { | |
| 105 | + $result = $this->_wpdb->get_results("SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo` WHERE `status`=1"); | |
| 106 | + return $result; | |
| 107 | + } | |
| 59 | 108 | |
| 60 | - $result = $this->_wpdb->get_results( | |
| 61 | - " | |
| 62 | - SELECT $column FROM `{$this->_wpdb->prefix}$table_name` WHERE `$column`='$value' | |
| 63 | - " | |
| 64 | - ); | |
| 65 | - return $result; | |
| 66 | - } | |
| 109 | + public function getWorkFlow($formID) | |
| 110 | + { | |
| 111 | + $result = $this->_wpdb->get_results( | |
| 112 | + $this->_wpdb->prepare( | |
| 113 | + "SELECT workflow_name,id FROM `{$this->_wpdb->prefix}bitforms_workflows` WHERE `form_id` = %d", | |
| 114 | + $formID | |
| 115 | + ) | |
| 116 | + ); | |
| 117 | + return $result; | |
| 118 | + } | |
| 67 | 119 | |
| 68 | - public function noteCreate($formID, $entryID, $note_details) | |
| 69 | - { | |
| 120 | + public function noteUpdate($noteID, $note_details) | |
| 121 | + { | |
| 122 | + $data = ['info_details' => $note_details]; | |
| 123 | + $result = $this->_wpdb->update( | |
| 124 | + "{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo", | |
| 125 | + $data, | |
| 126 | + [ | |
| 127 | + 'id' => $noteID, | |
| 128 | + ] | |
| 129 | + ); | |
| 130 | + return $result; | |
| 131 | + } | |
| 70 | 132 | |
| 71 | - $ipTool = new IpTool(); | |
| 72 | - $user_details = $ipTool->getUserDetail(); | |
| 133 | + public function noteDelete($noteID) | |
| 134 | + { | |
| 135 | + $sql = $this->_wpdb->prepare( | |
| 136 | + "DELETE FROM `{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo` WHERE `id` = %d", | |
| 137 | + $noteID | |
| 138 | + ); | |
| 139 | + $result = $this->_wpdb->query($sql); | |
| 140 | + return $result; | |
| 141 | + } | |
| 73 | 142 | |
| 74 | - $result = $this->_wpdb->insert( | |
| 75 | - "{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo", | |
| 76 | - array( | |
| 77 | - 'info_type' => 'note', | |
| 78 | - 'info_details' => $note_details, | |
| 79 | - 'form_id' => $formID, | |
| 80 | - 'entry_id' => $entryID, | |
| 81 | - 'user_id' => $user_details['id'], | |
| 82 | - 'user_ip' => $user_details['ip'], | |
| 83 | - 'created_at' => $user_details['time'], | |
| 84 | - ) | |
| 85 | - ); | |
| 86 | - return $result; | |
| 87 | - } | |
| 143 | + public function get_form_value($entryID) | |
| 144 | + { | |
| 145 | + $sql = $this->_wpdb->prepare( | |
| 146 | + "SELECT `meta_key`,`meta_value` FROM `{$this->_wpdb->prefix}bitforms_form_entrymeta` WHERE bitforms_form_entry_id=%d", | |
| 147 | + $entryID | |
| 148 | + ); | |
| 149 | + $result = $this->_wpdb->get_results($sql); | |
| 150 | + return $result; | |
| 151 | + } | |
| 88 | 152 | |
| 89 | - public function noteList() | |
| 90 | - { | |
| 91 | - $result = $this->_wpdb->get_results( | |
| 92 | - " | |
| 93 | - SELECT * FROM `{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo` WHERE `status`=1 | |
| 94 | - " | |
| 95 | - ); | |
| 96 | - return $result; | |
| 153 | + public function logUpdate($updateValue, $logID) | |
| 154 | + { | |
| 155 | + if (empty($logID)) { | |
| 156 | + return false; | |
| 97 | 157 | } |
| 158 | + $sql = $this->_wpdb->prepare( | |
| 159 | + "UPDATE `{$this->_wpdb->prefix}bitforms_form_entry_log` SET content=%s WHERE id=%d", | |
| 160 | + $updateValue, | |
| 161 | + $logID | |
| 162 | + ); | |
| 163 | + $result = $this->_wpdb->get_results($sql); | |
| 164 | + return $result; | |
| 165 | + } | |
| 98 | 166 | |
| 99 | - public function noteUpdate($noteID, $note_details) | |
| 100 | - { | |
| 101 | - $data = array( | |
| 102 | - 'info_details' => $note_details, | |
| 103 | - ); | |
| 104 | - $result = $this->_wpdb->update( | |
| 105 | - "{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo", | |
| 106 | - $data, | |
| 107 | - array( | |
| 108 | - 'id' => $noteID, | |
| 109 | - ) | |
| 110 | - ); | |
| 111 | - return $result; | |
| 112 | - } | |
| 167 | + public function getFormId($formID) | |
| 168 | + { | |
| 169 | + $sql = $this->_wpdb->prepare( | |
| 170 | + "SELECT form_id FROM `{$this->_wpdb->prefix}bitforms_form_entries` WHERE id=%d", | |
| 171 | + $formID | |
| 172 | + ); | |
| 173 | + $result = $this->_wpdb->get_results($sql); | |
| 174 | + return $result; | |
| 175 | + } | |
| 113 | 176 | |
| 114 | - public function noteDelete($noteID) | |
| 115 | - { | |
| 116 | - $sql = "DELETE FROM `{$this->_wpdb->prefix}bitforms_form_entry_relatedinfo` WHERE `id` = $noteID"; | |
| 117 | - $result = $this->_wpdb->query($sql); | |
| 118 | - return $result; | |
| 119 | - } | |
| 177 | + public function getOnSubmitWorkflow($formID) | |
| 178 | + { | |
| 179 | + $sql = $this->_wpdb->prepare( | |
| 180 | + "SELECT `id`, `workflow_name`, `workflow_type`, `workflow_run`, `workflow_behaviour`, `workflow_status` FROM `{$this->_wpdb->prefix}bitforms_workflows` WHERE `form_id`=%d AND `workflow_type`='onsubmit' ORDER BY id DESC", | |
| 181 | + $formID | |
| 182 | + ); | |
| 183 | + $result = $this->_wpdb->get_results($sql); | |
| 184 | + return $result; | |
| 185 | + } | |
| 120 | 186 | } |