/JACKED/MySQL.php
PHP | 286 lines | 210 code | 35 blank | 41 comment | 23 complexity | b7e03a5af8a99ef48423658834c94505 MD5 | raw file
Possible License(s): Apache-2.0
- <?php
- class MySQL extends JACKEDModule{
- const moduleName = 'MySQL';
- const moduleVersion = 2.0;
- const dependencies = '';
- const optionalDependencies = '';
-
- private $mysql_link = NULL;
-
- public function __destruct(){
- mysql_close($this->mysql_link);
- $this->mysql_link = NULL;
- }
-
- //LOOK ITS STUFF TO MAKEHAS WORKING
- private function isLinkOpen($link = NULL){
- $link = $link? $link : $this->mysql_link;
- return $this->mysql_link;
- }
-
- private function openLink(){
- $this->mysql_link = mysql_connect($this->config->db_host, $this->config->db_user, $this->config->db_pass);
- if($this->mysql_link){
- if(mysql_select_db($this->config->db_name)){
- return $this->mysql_link;
- }else{
- $this->isModuleEnabled = false;
- throw new Exception('Could not select the database.');
- }
- }else{
- $this->isModuleEnabled = false;
- throw new Exception('Could not connect to the database.');
- }
- }
-
- private function getLink(){
- if($this->isLinkOpen()){
- return $this->mysql_link;
- }else{
- return $this->openLink();
- }
- }
-
- /////////////////////////////
- //actual public mysql stuff//
- /////////////////////////////
-
- //maybe handle some better mysql sanitizing later or something
- //takes a value, returns a sanitized version of it for mysql
- public function sanitize($value, $link = NULL){
- $link = $link? $link : $this->getLink();
- return mysql_real_escape_string($value, $link);
- }
-
- //should probably make a generic paginator function:
- //paginator(howmany, page)
- ////return the LIMIT string
- public function paginator($howMany, $page){
- return " LIMIT " . ($howMany * ($page - 1)) . ", " . $howMany;
- }
-
- //takes an array of fields, checks against an array of allowed fields,
- ////returns a string of csv fields suitable for mysql SELECT
- public function getFieldString($fields, $allowedFields = false){
- $fieldschecked = array();
- if($allowedFields){
- if($fields){
- foreach($fields as $field){
- if(in_array($field, $allowedFields))
- $fieldschecked[] = '`' . $field . '`';
- }
- }
- }
- if(empty($fieldschecked))
- $fieldstring = "*";
- else
- $fieldstring = implode(", ", $fieldschecked);
-
- return $fieldstring;
- }
-
- //SELECT val FROM table WHERE cond
- //val is just one field, and you only get the first result
- ////default link can be overridden
- public function getVal($val, $table, $cond = null, $link = NULL){
- $link = $link? $link : $this->getLink();
- if(stripos($val, "function:") === 0){
- $val = substr($val, 9); //function: ends at 9, lol.
- $query = "SELECT " . $val . " FROM `" . $table . "`";
- }else
- $query = "SELECT `" . $val . "` FROM `" . $table . "`";
- if($cond)
- $query .= " WHERE " . $cond;
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
- $row = mysql_fetch_array($result, MYSQL_NUM);
-
- if($result && mysql_num_rows($result) > 0)
- $final = stripslashes($row[0]);
- else
- $final = false;
-
- mysql_free_result($result);
- return $final;
- }
-
- //SELECT vals FROM table WHERE cond
- ////default link can be overridden
- public function getRowVals($vals, $table, $cond, $result_type = MYSQL_BOTH, $link = NULL){
- $link = $link? $link : $this->getLink();
- $query = "SELECT $vals FROM `" . $table . "` WHERE " . $cond;
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
- $row = mysql_fetch_array($result, $result_type);
-
- if($result && mysql_num_rows($result) > 0)
- $final = array_map("stripslashes", $row);
- else
- $final = false;
-
- mysql_free_result($result);
- return $final;
- }
-
- //SELECT * FROM table WHERE cond
- ////default link can be overridden
- public function getRow($table, $cond, $result_type = MYSQL_BOTH, $link = NULL){
- $link = $link? $link : $this->getLink();
- $query = "SELECT * FROM `" . $table . "` WHERE " . $cond;
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
- $row = mysql_fetch_array($result, $result_type);
-
- if($result && mysql_num_rows($result) > 0)
- $final = array_map("stripslashes", $row);
- else
- $final = false;
-
- mysql_free_result($result);
- return $final;
- }
-
- //SELECT vals FROM table WHERE cond
- //vals is an array of field names
- //returns an array of vals
- ////default link can be overridden
- public function getAllVals($vals, $table, $cond, $link = NULL){
- $link = $link? $link : $this->getLink();
- if(is_array($vals)){
- $query = "SELECT " . implode(",", $vals) . " FROM `" . $table . "` WHERE " . $cond;
- }else{
- $query = "SELECT * FROM `" . $table . "` WHERE " . $cond;
- }
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
- if($result && mysql_num_rows($result) > 0){
- $final = array();
- if(is_array($vals)){
- while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
- $newrow = array();
- foreach($vals as $fieldname){
- $newrow[$fieldname] = stripslashes($row[$fieldname]);
- }
- $final[] = $newrow;
- }
- }else{
- while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
- $newrow = array();
- foreach($row as $fieldname => $value){
- $newrow[$fieldname] = stripslashes($row[$fieldname]);
- }
- $final[] = $newrow;
- }
- }
- }else
- $final = false;
-
- mysql_free_result($result);
- return $final;
- }
-
- //SELECT * FROM table WHERE cond
- ////default link can be overridden
- ////returns the result
- public function getResult($table, $cond, $link = NULL){
- $link = $link? $link : $this->getLink();
- $query = "SELECT * FROM `" . $table . "` WHERE " . $cond;
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
-
- if($result && mysql_num_rows($result) > 0)
- $final = $result;
- else
- $final = false;
-
- mysql_free_result($result);
- return $final;
- }
-
- //make does do a query!
- public function query($query, $link = NULL){
- $link = $link? $link : $this->getLink();
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
-
- if($result && mysql_num_rows($result) > 0)
- $final = $result;
- else
- $final = false;
-
- return $final;
- }
-
- //INSERT INTO table (fields) VALUES (values)
- ///$data is an associative array where $field=>$value
- ////default link can be overridden
- ////returns bool whether it worked
- public function insertValues($table, $data, $link = NULL){
- $link = $link? $link : $this->getLink();
- $fields = array();
- $values = array();
- foreach($data as $field => $value){
- $fields[] = $this->sanitize($field, $link);
- $values[] = $this->sanitize($value, $link);
- }
- $query = "INSERT INTO $table (`" . implode($fields, '`, `') . "`) VALUES ('" . implode($values, '\', \'') . "')";
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
- $done = ($result)? mysql_insert_id($link) : $result;
- mysql_free_result($result);
- return $done;
- }
-
- //UPDATE table SET field1 = value1, ... fieldn = value1 WHERE cond
- ///$data is an associative array where $field=>$value
- ////default link can be overridden
- ////returns bool whether it worked
- public function update($table, $data, $cond, $link = NULL){
- $link = $link? $link : $this->getLink();
- $fields = array();
- $values = array();
- foreach($data as $field => $value){
- $pairs[] = "`" . $this->sanitize($field, $link) . "` = '" . $this->sanitize($value, $link) . "'";
- }
-
- $query = "UPDATE $table SET " . implode($pairs, ', ') . " WHERE " . $cond;
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
- $done = $result;
- mysql_free_result($result);
- return $done;
- }
-
- //REPLACE INTO table (field1, ... fieldn) VALUES (value1, ... value1)
- ///$data is an associative array where $field=>$value
- ////default link can be overridden
- ////returns bool whether it worked
- public function replace($table, $data, $link = NULL){
- $link = $link? $link : $this->getLink();
- $fields = array();
- $values = array();
- foreach($data as $field => $value){
- $fields[] = $this->sanitize($field, $link);
- $values[] = $this->sanitize($value, $link);
- }
-
- $query = "REPLACE INTO $table (`" . implode($fields, '`, `') . "`) VALUES ('" . implode($values, '\', \'') . "')";
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
- return $result;
- }
-
- //DELETE FROM table WHERE cond
- ////default link can be overridden
- ////returns bool whether it worked
- public function delete($table, $cond, $link = NULL){
- $link = $link? $link : $this->getLink();
- $query = 'DELETE FROM ' . $this->sanitize($table) . ' WHERE ' . $cond;
- JACKED::debug_dump($query);
- $result = mysql_query($query, $link);
- return $result;
- }
- }
- ?>