Initialization of Logs/4Chan_Bans_Log-Reverse_Chrono-400.json
[Backup_bans.verniy.xyz.git] / Class / database-connection.php
blob5b5949460adb3e47788ae070cbb9d27270543147
1 <?php
2 class DatabaseConnection{
3 private $sql_data = array();
4 private $connection = null;
6 public $die_state = array();
7 public $comment_error = false;
8 public $delete_status = false;
10 private $path_prefix="";
12 public $alphabet = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p","q", "r", "s", "t", "u", "v", "w", "x", "y", "z"];
14 function __construct($path_prefix = ""){
15 $this->path_prefix = $path_prefix;
16 $sql_ini = fopen($path_prefix . "Settings/sql.ini", "r");
17 if(!$sql_ini) $sql_ini = fopen($path_prefix . "settings/sql.ini", "r");
18 while(!feof($sql_ini)){
19 $line = fgets($sql_ini);
20 $key = substr($line, 0, strpos($line, "="));
21 $value = trim(substr($line, strpos($line, "=")+1));
22 $this->sql_data[$key] = $value;
24 $this->connectToDatabase();
27 function connectToDatabase(){
28 try {
29 $this->connection = new PDO ("mysql:dbname=" . $this->sql_data["database"] . ";host=" . $this->sql_data["connection"],
30 $this->sql_data["user"], $this->sql_data["pass"]);
31 $this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
33 } catch (PDOException $e) {
34 $this->logsys .= "Failed to get DB handle: " . $e->getMessage() . "\n";
39 function addToTable($tablename, $paramaters){
40 $param_len = sizeof($paramaters);
41 $bind_string = "";
42 $table_string= "";
43 $first_comma = false;
44 foreach($paramaters as $key => $param){
45 if(!$first_comma){
46 $bind_string = ":$key";
47 $table_string = "`$key`";
48 $first_comma = true;
50 else{
51 $bind_string .= ",:$key";
52 $table_string .= ",`$key`";
55 $statement = $this->connection->prepare("INSERT INTO `".$this->sql_data["database"] ."`.`$tablename`($table_string) VALUES(" . $bind_string . ")");
57 $index = 0;
58 foreach($paramaters as $key => $param){
59 $success = $statement->bindParam(":" . $key , $paramaters[$key]);
60 $index++;
62 try{
63 $statement->execute();
64 }catch(Exception $e){
65 echo "<strong>" . $e->getMessage() . "</strong><br/>";
69 function massAddToTable($tablename, $all_paramaters){
70 $value_string = "";
71 $table_string= "";
72 $update_string = "";
73 $first_comma = false;
74 $first_value_comma = false;
75 $table_string_created = false;
76 foreach($all_paramaters as $index=>$paramaters){
77 if(!$first_value_comma){
78 $value_string = "(";
80 else{
81 $value_string .= ",(";
83 foreach($paramaters as $key => $param){
84 if(!$first_comma){
85 $value_string .= ":$key$index";
86 if(!$table_string_created){
87 $update_string = "$key=VALUES($key)";
88 $table_string = "`$key`";
90 $first_comma = true;
92 else{
93 $value_string .= ",:$key$index";
94 if(!$table_string_created){
95 $update_string .= ",$key=VALUES($key)";
96 $table_string .= ",`$key`";
100 $value_string .= ")";
101 $first_comma = false;
102 $first_value_comma = true;
103 $table_string_created = true;
106 $statement = $this->connection->prepare("INSERT INTO `".$this->sql_data["database"] ."`.`$tablename`($table_string) VALUES $value_string
107 ON DUPLICATE KEY UPDATE $update_string");
108 foreach($all_paramaters as $index=>$paramaters){
109 foreach($paramaters as $key => $param){
110 $success =$statement->bindParam(":$key$index" , $paramaters[$key]);
113 try{
114 $statement->execute();
115 }catch(Exception $e){
116 echo "<strong>" . $e->getMessage() . "</strong><br/>";
120 function getPostDetails($table, $refining_paramater="", $bind_val=""){
121 $statement = NULL;
122 if($refining_paramater == "" || $bind_val == ""){
123 $statement = $this->connection->prepare("SELECT * FROM `$table`");
125 else{
126 $statement = $this->connection->prepare("SELECT * FROM `$table` WHERE `$table`.`$refining_paramater` = :bindval");
127 $statement->bindParam(":bindval", $bind_val);
129 try{
130 $response = $statement->execute();
131 return $statement->fetchAll();
132 }catch(Exception $e){
133 echo "<strong>" . $e->getMessage() . "</strong><br/>";
137 function getPostDetailsAllUpperLower($table, $boundry_param, $lower_limit){
138 $statement = $this->connection->prepare("SELECT * FROM `$table` WHERE $boundry_param > :lower_limit AND $boundry_param <= :upper_limit ORDER BY $boundry_param ASC");
139 $statement->bindParam(":lower_limit", $lower_limit);
140 $upper_limit = $lower_limit + 1000;
141 $statement->bindParam(":upper_limit", $upper_limit);
142 try{
143 $response = $statement->execute();
144 return $statement->fetchAll();
145 }catch(Exception $e){
146 echo "<strong>" . $e->getMessage() . "</strong><br/>";
150 function getPostDetailsLimit($table, $boundry_param, $lower_limit, $added_search, $added_search_value){
151 $statement = $this->connection->prepare("SELECT * FROM `$table` WHERE $added_search = :added_search_value ORDER BY $boundry_param ASC LIMIT :lower_limit, 1000");
152 $statement->bindParam(":added_search_value", $added_search_value);
154 $statement->bindParam(":lower_limit", $lower_limit , PDO::PARAM_INT);
155 try{
156 $response = $statement->execute();
157 return $statement->fetchAll();
158 }catch(Exception $e){
159 echo "<strong>" . $e->getMessage() . "</strong><br/>";
162 function getPostDetailsAllSettingsLimit($table, $boundry_param, $lower_limit, $board, $com, $reason){
163 $statement = $this->connection->prepare("SELECT * FROM `$table` WHERE board LIKE :board AND com LIKE :com AND reason LIKE :reason ORDER BY $boundry_param ASC LIMIT :lower_limit, 1000");
164 if($board == "") $board = "%";
165 else $board = "$board";
166 $com = "%$com%";
167 $reason = "%$reason%";
168 $statement->bindParam(":board", $board);
169 $statement->bindParam(":com", $com);
170 $statement->bindParam(":reason", $reason);
171 $statement->bindParam(":lower_limit", $lower_limit, PDO::PARAM_INT);
172 try{
173 $response = $statement->execute();
174 return $statement->fetchAll();
175 }catch(Exception $e){
176 echo "<strong>" . $e->getMessage() . "</strong><br/>";
180 function getCountOf($table, $count_refine, $count_refine_value=null){
181 $statement = null;
182 if($count_refine_value == null){
183 $statement = $this->connection->prepare("SELECT COUNT(*) FROM `$table`");
185 else{
186 $statement = $this->connection->prepare("SELECT COUNT(*) FROM `$table` WHERE $count_refine LIKE :count_refine_value");
187 $statement->bindParam(":count_refine_value", $count_refine_value);
189 //var_dump($statement);
190 try{
191 $response = $statement->execute();
192 return $statement->fetchAll();
193 }catch(Exception $e){
194 echo "<strong>" . $e->getMessage() . "</strong><br/>";
198 function getCountOfAllSettings($table, $board, $com, $reason){
199 $statement = $this->connection->prepare("SELECT COUNT(*) FROM `$table` WHERE board LIKE :board AND com LIKE :com AND reason LIKE :reason");
200 if($board == "") $board = "%";
201 else $board = "$board";
202 $com = "%$com%";
203 $reason = "%$reason%";
204 $statement->bindParam(":board", $board);
205 $statement->bindParam(":com", $com);
206 $statement->bindParam(":reason", $reason);
207 try{
208 $response = $statement->execute();
209 return $statement->fetchAll();
210 }catch(Exception $e){
211 echo "<strong>" . $e->getMessage() . "</strong><br/>";
215 function updatePost($table, $table_search, $table_search_value, $keyed_params){
216 $first_entry = true;
217 $set_string = "";
218 foreach($keyed_params as $key=>$value){
219 if($first_entry){
220 $set_string = "$key=:$key";
222 else $set_string .= ",$key=:$key";
225 $statement = $this->connection->prepare("UPDATE `$table` SET $set_string WHERE $table_search=:param_to_find");
226 $statement->bindParam(":param_to_find", $table_search_value);
228 foreach($keyed_params as $key=>$value){
229 $statement->bindParam(":$key", $value);
232 try{
233 $response = $statement->execute();
234 }catch(Exception $e){
235 echo "<strong>" . $e->getMessage() . "</strong><br/>";