Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Renamed Query to DB
  • Loading branch information
mevdschee committed Aug 21, 2014
1 parent d10fce6 commit bf0f09c
Show file tree
Hide file tree
Showing 14 changed files with 132 additions and 119 deletions.
12 changes: 6 additions & 6 deletions config/config.php.template
Expand Up @@ -13,13 +13,13 @@ class Session
public static $sessionName = 'mindaphp';
}

class Query
class DB
{
public static $host = '{{Query_HOST}}'; // default: 'localhost'
public static $username = '{{Query_USER}}'; // default: 'mindaphp'
public static $password = '{{Query_PASS}}'; // choose a strong password
public static $database = '{{Query_NAME}}'; // default: 'mindaphp'
public static $port = {{Query_PORT}}; // default: 3306
public static $host = '{{DB_HOST}}'; // default: 'localhost'
public static $username = '{{DB_USER}}'; // default: 'mindaphp'
public static $password = '{{DB_PASS}}'; // choose a strong password
public static $database = '{{DB_NAME}}'; // default: 'mindaphp'
public static $port = {{DB_PORT}}; // default: 3306
}

class Auth
Expand Down
2 changes: 1 addition & 1 deletion pages/admin/index().php
@@ -1,4 +1,4 @@
<?php
if (!isset($_SESSION['user'])) Router::redirect('login');
$user = $_SESSION['user'];
$users = Query::records('select * from users');
$users = DB::select('select * from users');
15 changes: 8 additions & 7 deletions pages/docs/api(docs).phtml
Expand Up @@ -5,13 +5,14 @@ Type Function Location Purpose Lev
=====================================================================================================
e($variable) Template/View Output Public
d($variable) Everywhere Debugging Public
array Query::records($sql,...) Action Database query Public
array Query::one($sql,...) Action Database query Public
array Query::pairs($sql,...) Action Database query Public
string Query::value($sql,...) Action Database query Public
integer Query::insert($sql,...) Action Database query Public
integer Query::update($sql,...) Action Database query Public
integer Query::delete($sql,...) Action Database query Public
array DB::select($sql,...) Action Database query Public
array DB::selectOne($sql,...) Action Database query Public
string DB::selectValue($sql,...) Action Database query Public
array DB::selectPairs($sql,...) Action Database query Public
array DB::selectValues($sql,...) Action Database query Public
integer DB::insert($sql,...) Action Database query Public
integer DB::update($sql,...) Action Database query Public
integer DB::delete($sql,...) Action Database query Public
bool Auth::login($username,$password) Action Logging in Public
bool Auth::logout() Action Logging out Public
bool Auth::register($username,$password) Action Adding users Public
Expand Down
44 changes: 21 additions & 23 deletions pages/docs/database(docs).phtml
@@ -1,49 +1,47 @@
<h1>Database</h1>
<p>This class provides 4 public methods and the parameters are:</p>
<p>These functions can be statically accessed from the global "Query" class.</p>
<h2>Query</h2>
<pre>Query::records($sql,$arg1,$arg2,...)</pre>
<p>Executes SQL containing "?" symbols. Every questionmark must be matched by an extra argument. Example:</p>
<p>This class provides 8 public methods and the parameters are:</p>
<p>These functions can be statically accessed from the global "DB" class.</p>
<h2>Select</h2>
<pre>DB::select($sql,$arg1,$arg2,...)</pre>
<p>Executes SQL containing "?" symbols. Every questionmark must be matched by an extra argument.
The following query retrieves all users that have an username starting with a 'M':</p>
<pre>
$query = 'insert into users (username,password,salt,created) values (?,sha1(concat(?,?)),?,NOW())';
$success = Query::records($query,$username,$salt,$password,$salt);
$users = DB::select('select * from users where username LIKE ?','M%');
</pre>
<p>Or when you want to iterate over records:</p>
<p>In the view (.phtml) file we can then print yhe usernames in a list using:</p>
<pre>
$users = Query::records('select * from users');

&lt;?php foreach ($users as $user): ?&gt;
&lt;li&gt;&lt;?php e($user['username']); ?&gt;&lt;/li&gt;
&lt;?php endforeach; ?&gt;
</pre>
<h2>Query one</h2>
<pre>Query::one($sql,$arg1,$arg2,...)</pre>
<p>Or when you want to iterate over records:</p>
<pre>
</pre>
<h2>Select one</h2>
<pre>DB::selectOne($sql,$arg1,$arg2,...)</pre>
<p>Same as "records", but only returns the first record or false. Example:</p>
<pre>
$query = 'select * from users where username = ? and sha1(concat(salt,?)) = password limit 1';
$user = Query::one($query,$username,$password);
$user = DB::selectOne($query,$username,$password);
</pre>
<h2>Insert</h2>
<pre>Query::insert()</pre>
<pre>DB::insert()</pre>
<p>Executes SQL "INSERT" statement and returns the "insert id" from the last executed SQL query. Example:</p>
<pre>
$query = 'insert into users (username,password,salt,created) values (?,sha1(concat(?,?)),?,NOW())';
$userId = Query::insert($query,$username,$salt,$password,$salt);
$userId = DB::insert($query,$username,$salt,$password,$salt);
</pre>
<h2>Update</h2>
<pre>Query::update()</pre>
<pre>DB::update()</pre>
<p>Executes SQL "UPDATE" statement and returns the "affected rows" from the last executed SQL query. Example:</p>
<pre>
$query = 'update users set username = ? where id = ?';
$affectedRows = Query::update($query,$username,$userId);
$affectedRows = DB::update($query,$username,$userId);
</pre>
<h2>Delete</h2>
<pre>Query::delete()</pre>
<pre>DB::delete()</pre>
<p>Executes SQL "DELETE" statement and returns the "affected rows" from the last executed SQL query. Example:</p>
<pre>
$query = 'delete from users where username = ?';
$affectedRows = Query::delete($query,$username);
</pre>
<h2>Raw engine access</h2>
<pre>Query::handle()</pre>
<p>Returns the handle to the (e.g. MySQLi) engine. You normally do not need this.</p>
$affectedRows = DB::delete($query,$username);
</pre>
7 changes: 4 additions & 3 deletions pages/docs/overview(docs).phtml
Expand Up @@ -69,9 +69,10 @@ to use a template you can use the "none" template. In this case the view will be

<h2>Database abstraction layer</h2>

<p>The "Query" class holds your database connection. It allows you to execute SQL queries very
simple (using the "records" and "one" methods). It protects you against SQL injection attacks. Note that these
methods are not suited for large datasets that exceed the PHP memory limit (streaming output).</p>
<p>The "DB" class holds your database connection. It allows you to execute SQL queries very
simple (using the "select", "insert", "update" and "delete" methods). It protects you against
SQL injection attacks. Note that these methods are not suited for large datasets that exceed
the PHP memory limit (streaming output).</p>

<h2>Authentication</h2>

Expand Down
2 changes: 1 addition & 1 deletion publish.sh
@@ -1,2 +1,2 @@
#!/bin/bash
rsync -axv --delete --exclude='.git' . lx55.nlware.com:public_html
rsync -axv --delete --exclude=.git --exclude=config/config.php . lx55.nlware.com:public_html
10 changes: 5 additions & 5 deletions tools/conventionist.php
Expand Up @@ -35,8 +35,8 @@ public static function text()
}

private static function check()
{ $tables = Query::records("SELECT TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_COLLATION FROM information_schema.TABLES WHERE table_schema=DATABASE() AND table_name NOT like '%_history' AND table_name NOT like 'history'");
$foreign_keys = Query::pairs("select concat(table_name, '.', column_name) as 'foreign_key', concat(referenced_table_name, '.', referenced_column_name) as 'references' from information_schema.key_column_usage where referenced_table_name is not null and table_schema=DATABASE()");
{ $tables = DB::select("SELECT TABLE_NAME,TABLE_TYPE,ENGINE,TABLE_COLLATION FROM information_schema.TABLES WHERE table_schema=DATABASE() AND table_name NOT like '%_history' AND table_name NOT like 'history'");
$foreign_keys = DB::selectPairs("select concat(table_name, '.', column_name) as 'foreign_key', concat(referenced_table_name, '.', referenced_column_name) as 'references' from information_schema.key_column_usage where referenced_table_name is not null and table_schema=DATABASE()");
$errors = array();
$fieldsets = array();
$tableNames = array();
Expand All @@ -46,7 +46,7 @@ private static function check()
}
for ($i=0;$i<count($tableNames);$i++)
{ $table = $tables[$i]['TABLE_NAME'];
$fields=Query::records("SELECT COLUMN_NAME,COLUMN_KEY,EXTRA FROM information_schema.COLUMNS WHERE table_schema=DATABASE() and table_name = ?",$table);
$fields=DB::select("SELECT COLUMN_NAME,COLUMN_KEY,EXTRA FROM information_schema.COLUMNS WHERE table_schema=DATABASE() and table_name = ?",$table);
foreach ($fields as $j=>$field)
{ $fields[$j] = $field['COLUMNS'];
}
Expand All @@ -57,8 +57,8 @@ private static function check()
if (!preg_match('/^[a-z_]+$/',$table,$matches))
{ $errors[] = array('type'=>'warning','table'=>$table,'message'=>'invalid table name');
}
if ($tables[$i]['TABLE_TYPE']=="BASE TABLE" && $tables[$i]['ENGINE']!="InnoQuery")
{ $errors[] = array('type'=>'error','table'=>$table,'message'=>'type must be InnoQuery');
if ($tables[$i]['TABLE_TYPE']=="BASE TABLE" && $tables[$i]['ENGINE']!="InnoDB")
{ $errors[] = array('type'=>'error','table'=>$table,'message'=>'type must be InnoDB');
}
if (!preg_match('/^utf8/i',$tables[$i]['TABLE_COLLATION'],$matches))
{ $errors[] = array('type'=>'warning','table'=>$table,'message'=>'collation should be utf8');
Expand Down
38 changes: 19 additions & 19 deletions tools/requirements.php
Expand Up @@ -18,11 +18,11 @@
if ($i=='/') return $i;
$u=parse_url($i); return '/'.trim($u['path'], '/').'/';
}),
array('Query_HOST','What is the MySQL hostname?','localhost',null),
array('Query_USER','What is the MySQL username?','root',null),
array('Query_PASS','What is the MySQL password?','',null),
array('Query_NAME','What is the MySQL database?','mindaphp',null),
array('Query_PORT','What is the MySQL port?','3306',null),
array('DB_HOST','What is the MySQL hostname?','localhost',null),
array('DB_USER','What is the MySQL username?','root',null),
array('DB_PASS','What is the MySQL password?','',null),
array('DB_NAME','What is the MySQL database?','mindaphp',null),
array('DB_PORT','What is the MySQL port?','3306',null),
);
$parameters = array();
$c = count($questions);
Expand All @@ -32,59 +32,59 @@
echo "[$n/$c] $question [$default] ";
$parameters[$name] = trim(fgets(STDIN))?:$default;
}
$mysqli = new mysqli($parameters['Query_HOST'], $parameters['Query_USER'], $parameters['Query_PASS']);
$mysqli = new mysqli($parameters['DB_HOST'], $parameters['DB_USER'], $parameters['DB_PASS']);
if ($mysqli->connect_error) {
echo "ERROR: MySQL connect: ($mysqli->connect_errno) $mysqli->connect_error\n";
exit(1);
}
echo "INFO: MySQL connected\n";
$sql = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$parameters[Query_NAME]';";
$sql = "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '$parameters[DB_NAME]';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL database check: $mysqli->error\n";
exit(1);
} elseif (!$result->num_rows) {
if ($parameters['Query_USER'] != 'root') {
echo "ERROR: MySQL database not found: $parameters[Query_NAME]\n";
if ($parameters['DB_USER'] != 'root') {
echo "ERROR: MySQL database not found: $parameters[DB_NAME]\n";
exit(1);
}
$sql = "CREATE DATABASE `$parameters[Query_NAME]` COLLATE 'utf8_bin';";
$sql = "CREATE DATABASE `$parameters[DB_NAME]` COLLATE 'utf8_bin';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL database create: $mysqli->error\n";
exit(1);
}
echo "INFO: MySQL database created\n";
$host = $parameters['Query_HOST']=='localhost'?'localhost':'%';
$pass = base64_encode(sha1(rand() . time(true) . $parameters['Query_NAME'], true));
$sql = "CREATE USER '$parameters[Query_NAME]'@'$host' IDENTIFIED BY '$pass';";
$host = $parameters['DB_HOST']=='localhost'?'localhost':'%';
$pass = base64_encode(sha1(rand() . time(true) . $parameters['DB_NAME'], true));
$sql = "CREATE USER '$parameters[DB_NAME]'@'$host' IDENTIFIED BY '$pass';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL user create: $mysqli->error\n";
exit(1);
}
echo "INFO: MySQL user created\n";
$sql = "GRANT ALL PRIVILEGES ON `$parameters[Query_NAME]`.* TO '$parameters[Query_NAME]'@'$host';";
$sql = "GRANT ALL PRIVILEGES ON `$parameters[DB_NAME]`.* TO '$parameters[DB_NAME]'@'$host';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL grant user: $mysqli->error\n";
exit(1);
}
echo "INFO: MySQL user granted\n";
$parameters['Query_USER'] = $parameters['Query_NAME'];
$parameters['Query_PASS'] = $pass;
$parameters['DB_USER'] = $parameters['DB_NAME'];
$parameters['DB_PASS'] = $pass;
}
$sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$parameters[Query_NAME]' AND TABLE_NAME = 'users';";
$sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '$parameters[DB_NAME]' AND TABLE_NAME = 'users';";
if (!$result = $mysqli->query($sql)) {
echo "ERROR: MySQL table check: $mysqli->error\n";
exit(1);
} elseif (!$result->num_rows) {
$sql = <<<END_OF_SQL
CREATE TABLE `$parameters[Query_NAME]`.`users` (
CREATE TABLE `$parameters[DB_NAME]`.`users` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` varchar(255) COLLATE utf8_bin NOT NULL,
`password` varchar(255) COLLATE utf8_bin NOT NULL,
`salt` varchar(255) COLLATE utf8_bin NOT NULL,
`created` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `username` (`username`)
) ENGINE=InnoQuery DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
END_OF_SQL;
if (!$mysqli->query($sql)) {
echo "ERROR: MySQL create table: $mysqli->error\n";
Expand Down
4 changes: 2 additions & 2 deletions vendor/mindaphp/Auth.php
Expand Up @@ -16,7 +16,7 @@ static function login($username,$password)
static::$usernameField,
static::$saltField,
static::$passwordField);
$user = Query::one($query,$username,$password);
$user = DB::selectOne($query,$username,$password);
if ($user) {
session_regenerate_id(true);
$_SESSION['user'] = $user['users'];
Expand All @@ -42,7 +42,7 @@ static function register($username,$password)
static::$passwordField,
static::$saltField,
static::$createdField);
return Query::insert($query,$username,$salt,$password,$salt)!==false;
return DB::insert($query,$username,$salt,$password,$salt)!==false;
}

}

0 comments on commit bf0f09c

Please sign in to comment.