Formating

This commit is contained in:
Przemek Grondek 2014-10-06 09:49:52 +02:00
parent b99a29ac86
commit 00322f7da8

455
c.php
View File

@ -1,275 +1,288 @@
<?php <?php
class connector{ class connector
public $action; {
public $action;
public $server; public $server;
public $user; public $user;
public $password; public $password;
private $database; private $database;
private $link; private $link;
private $is_connected; private $is_connected;
function connector($user, $password, $server){ function connector($user, $password, $server)
$this->user = $user; {
$this->password = $password; $this->user = $user;
$this->server = $server; $this->password = $password;
$this->is_connected = false; $this->server = $server;
} $this->is_connected = false;
}
function connect(){ function connect()
if(!$this->is_connected){ {
$this->link = @mysql_connect($this->server, $this->user, $this->password); if (!$this->is_connected) {
if(!$this->link){ $this->link = @mysql_connect($this->server, $this->user, $this->password);
return 'CANNOT connect: '.mysql_error(); if (!$this->link) {
} return 'CANNOT connect: ' . mysql_error();
else{ } else {
$this->is_connected = true; $this->is_connected = true;
mysql_set_charset("utf8"); mysql_set_charset("utf8");
return 'OK'; return 'OK';
} }
} }
} }
function selectDb($database){ function selectDb($database)
$this->database = $database; {
$this->database = $database;
mysql_select_db($this->database); mysql_select_db($this->database);
} }
function dbList(){ function dbList()
$db_list = mysql_list_dbs($this->link); {
$db_list = mysql_list_dbs($this->link);
for($i = 0; $row = mysql_fetch_object($db_list);$i++) for ($i = 0; $row = mysql_fetch_object($db_list); $i++)
$list[$i] = $row->Database; $list[$i] = $row->Database;
return $list; return $list;
} }
function tableList($database){ function tableList($database)
$result = mysql_list_tables($database); {
$num_rows = mysql_num_rows($result); $result = mysql_list_tables($database);
$num_rows = mysql_num_rows($result);
for($i=0;$i<$num_rows;$i++) for ($i = 0; $i < $num_rows; $i++)
$list[$i] = mysql_tablename($result, $i); $list[$i] = mysql_tablename($result, $i);
return $list; return $list;
} }
function query($query){ function query($query)
$result_id = mysql_query($query); {
$result_id = mysql_query($query);
if( !$result_id ){ if (!$result_id) {
echo mysql_error()."\n"; echo mysql_error() . "\n";
} else if ( $info = mysql_info() ){ } else if ($info = mysql_info()) {
echo $info; echo $info;
} else { } else {
echo OK."\n"; echo OK . "\n";
$numOfCols = mysql_num_fields($result_id); $numOfCols = mysql_num_fields($result_id);
if($numOfCols > 0 ) { if ($numOfCols > 0) {
for($i=0;$i<$numOfCols;$i++) for ($i = 0; $i < $numOfCols; $i++)
$field[$i] = mysql_field_name($result_id, $i); $field[$i] = mysql_field_name($result_id, $i);
echo json_encode($field)."\n"; echo json_encode($field) . "\n";
for($i=0;$row=mysql_fetch_row($result_id);$i++) for ($i = 0; $row = mysql_fetch_row($result_id); $i++)
$result[$i] = $row; $result[$i] = $row;
echo json_encode($result); echo json_encode($result);
} }
} }
} }
function disconnect(){ function disconnect()
if($this->link) {
mysql_close($this->link); if ($this->link)
} mysql_close($this->link);
}
function selectTable($table, $start, $limit){ function selectTable($table, $start, $limit)
$query = "SELECT * FROM `$table` LIMIT $start, $limit"; {
$result_id = mysql_query($query); $query = "SELECT * FROM `$table` LIMIT $start, $limit";
$result_id = mysql_query($query);
for($i=0;$row=mysql_fetch_row($result_id);$i++) for ($i = 0; $row = mysql_fetch_row($result_id); $i++)
$result[$i] = $row; $result[$i] = $row;
return $result; return $result;
} }
function fieldList($table){ function fieldList($table)
$result_id = mysql_list_fields($this->database, $table); {
$result_id = mysql_list_fields($this->database, $table);
$numOfCols = mysql_num_fields($result_id); $numOfCols = mysql_num_fields($result_id);
for($i=0;$i<$numOfCols;$i++) for ($i = 0; $i < $numOfCols; $i++)
$result[$i] = mysql_field_name($result_id, $i); $result[$i] = mysql_field_name($result_id, $i);
return $result; return $result;
} }
function numRows($table){ function numRows($table)
$result_id = mysql_query("SELECT COUNT(*) as total_count FROM `$table`"); {
$result_id = mysql_query("SELECT COUNT(*) as total_count FROM `$table`");
$ret = mysql_fetch_object($result_id); $ret = mysql_fetch_object($result_id);
return $ret->total_count; return $ret->total_count;
} }
function updateElement($table, $headerJSON, $oldValuesJSON, $newValuesJSON){ function updateElement($table, $headerJSON, $oldValuesJSON, $newValuesJSON)
$header = json_decode($headerJSON); {
$oldValues = json_decode($oldValuesJSON); $header = json_decode($headerJSON);
$newValues = json_decode($newValuesJSON); $oldValues = json_decode($oldValuesJSON);
$query = "UPDATE `$table` SET "; $newValues = json_decode($newValuesJSON);
$query = "UPDATE `$table` SET ";
for($i = 0;$i < sizeof($header); $i++){ for ($i = 0; $i < sizeof($header); $i++) {
$query .= "`".$header[$i]."` = '".$newValues[$i]."' "; $query .= "`" . $header[$i] . "` = '" . $newValues[$i] . "' ";
if($i+1 < sizeof($header)) if ($i + 1 < sizeof($header))
$query .= ", "; $query .= ", ";
} }
$query .= " WHERE ( "; $query .= " WHERE ( ";
for($i = 0;$i < sizeof($header); $i++){ for ($i = 0; $i < sizeof($header); $i++) {
$query .= "`".$header[$i]."` = '".$oldValues[$i]."' "; $query .= "`" . $header[$i] . "` = '" . $oldValues[$i] . "' ";
if($i+1 < sizeof($header)) if ($i + 1 < sizeof($header))
$query .= " && "; $query .= " && ";
} }
$query .= ");"; $query .= ");";
$result_id = mysql_query($query); $result_id = mysql_query($query);
if( !$result_id ){ if (!$result_id) {
echo mysql_error(); echo mysql_error();
} }
$info = mysql_info(); $info = mysql_info();
echo $info ; echo $info;
} }
function addElement($table, $headerJSON, $valuesJSON){ function addElement($table, $headerJSON, $valuesJSON)
$header = json_decode($headerJSON); {
$values = json_decode($valuesJSON); $header = json_decode($headerJSON);
$query = "INSERT INTO `$table` ("; $values = json_decode($valuesJSON);
$query = "INSERT INTO `$table` (";
for($i = 0;$i < sizeof($header); $i++){ for ($i = 0; $i < sizeof($header); $i++) {
$query .= "`".$header[$i]."`"; $query .= "`" . $header[$i] . "`";
if($i+1 < sizeof($header)) if ($i + 1 < sizeof($header))
$query .= ", "; $query .= ", ";
} }
$query .= ") VALUES ( "; $query .= ") VALUES ( ";
for($i = 0;$i < sizeof($header); $i++){ for ($i = 0; $i < sizeof($header); $i++) {
$query .= "'".$values[$i]."' "; $query .= "'" . $values[$i] . "' ";
if($i+1 < sizeof($header)) if ($i + 1 < sizeof($header))
$query .= " , "; $query .= " , ";
} }
$query .= ");"; $query .= ");";
$result_id = mysql_query($query); $result_id = mysql_query($query);
if( !$result_id ){ if (!$result_id) {
echo mysql_error(); echo mysql_error();
} else { } else {
echo OK; echo OK;
} }
$info = mysql_info(); $info = mysql_info();
echo $info ; echo $info;
} }
function removeElement($table, $headerJSON, $valuesJSON){ function removeElement($table, $headerJSON, $valuesJSON)
$header = json_decode($headerJSON); {
$values = json_decode($valuesJSON); $header = json_decode($headerJSON);
$query = "DELETE FROM `$table` WHERE ("; $values = json_decode($valuesJSON);
$query = "DELETE FROM `$table` WHERE (";
for($i = 0;$i < sizeof($header); $i++){ for ($i = 0; $i < sizeof($header); $i++) {
$query .= "`".$header[$i]."` = '".$values[$i]."' "; $query .= "`" . $header[$i] . "` = '" . $values[$i] . "' ";
if($i+1!=sizeof($header)) if ($i + 1 != sizeof($header))
$query .= " AND "; $query .= " AND ";
} }
$query .= ");"; $query .= ");";
$result_id = mysql_query($query); $result_id = mysql_query($query);
if( !$result_id ){ if (!$result_id) {
echo mysql_error(); echo mysql_error();
} else { } else {
echo OK; echo OK;
} }
} }
} }
// DEBUG!!! // DEBUG!!!
// $_POST = $_GET; // $_POST = $_GET;
$action = $_POST['a']; $action = $_POST['a'];
$server = 'localhost'; $server = 'localhost';
$user = $_POST['u']; $user = $_POST['u'];
$password = $_POST['p']; $password = $_POST['p'];
$database = $_POST['d']; $database = $_POST['d'];
$table = $_POST['t']; $table = $_POST['t'];
$start = $_POST['s']; $start = $_POST['s'];
$limit = $_POST['l']; $limit = $_POST['l'];
$query = $_POST['q']; $query = $_POST['q'];
$header = $_POST['h']; $header = $_POST['h'];
$values = $_POST['v']; $values = $_POST['v'];
$oldValues = $_POST['o']; $oldValues = $_POST['o'];
if(($user == null) || ($password == null) || ($action == null)) if (($user == null) || ($password == null) || ($action == null))
die('Nope'); die('Nope');
$con = new connector($user, $password, $server); $con = new connector($user, $password, $server);
switch ($action){ switch ($action) {
case "login": case "login":
echo $con->connect(); echo $con->connect();
break; break;
case "dblist": case "dblist":
$con->connect(); $con->connect();
$list = $con->dbList(); $list = $con->dbList();
echo json_encode($list); echo json_encode($list);
break; break;
case "tablelist": case "tablelist":
$con->connect(); $con->connect();
echo json_encode($con->tableList($database)); echo json_encode($con->tableList($database));
break; break;
case "fieldlist": case "fieldlist":
$con->connect(); $con->connect();
$con->selectDb($database); $con->selectDb($database);
echo json_encode($con->fieldList($table)); echo json_encode($con->fieldList($table));
break; break;
case "numrows": case "numrows":
$con->connect(); $con->connect();
$con->selectDb($database); $con->selectDb($database);
echo $con->numRows($table); echo $con->numRows($table);
break; break;
case "query": case "query":
$con->connect(); $con->connect();
if(isset($database)) if (isset($database))
$con->selectDb($database); $con->selectDb($database);
$con->query($query); $con->query($query);
break; break;
case "getrows": case "getrows":
$con->connect(); $con->connect();
$con->selectDb($database); $con->selectDb($database);
echo json_encode($con->selectTable($table, $start, $limit)); echo json_encode($con->selectTable($table, $start, $limit));
break; break;
case "updateelement": case "updateelement":
$con->connect(); $con->connect();
$con->selectDb($database); $con->selectDb($database);
$con->updateElement($table, $header, $oldValues, $values); $con->updateElement($table, $header, $oldValues, $values);
break; break;
case "addelement": case "addelement":
$con->connect(); $con->connect();
$con->selectDb($database); $con->selectDb($database);
$con->addElement($table, $header, $values); $con->addElement($table, $header, $values);
break; break;
case "removeelement": case "removeelement":
$con->connect(); $con->connect();
$con->selectDb($database); $con->selectDb($database);
//print_r($_GET); //print_r($_GET);
$con->removeElement($table, $header, $values); $con->removeElement($table, $header, $values);
} }
$con->disconnect(); $con->disconnect();
?> ?>