Skip to content

Commit d27b484

Browse files
committed
Improve the main JSONDB class
* Use the class PreparedQueryStatement for prepared queries. * Move methods used for prepared queries in class PreparedQueryStatement. * Create a .htaccess file in each created servers with user's credentials. * Fix bug with server's creation (server's path and user's credentials wasn't saved)
1 parent 32eb788 commit d27b484

File tree

1 file changed

+39
-80
lines changed

1 file changed

+39
-80
lines changed

src/JSONDB/JSONDB.php

Lines changed: 39 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,6 @@ class JSONDB
144144
*/
145145
private $parsedQuery;
146146

147-
/**
148-
* An array of keys inserted in the
149-
* query.
150-
* @var array
151-
*/
152-
private $preparedQueryKeys = array();
153-
154147
/**
155148
* Store the result of a query.
156149
*
@@ -303,6 +296,18 @@ public function createServer($path, $username, $password, $connect = FALSE)
303296

304297
chmod($path, 0777);
305298

299+
$htaccess = fopen($path . '/.htaccess', 'a+');
300+
foreach(array('AuthType Basic', 'AuthName "JSONDB Server Access"', 'AuthUserFile "' . realpath(dirname(__DIR__) . '/config/.htpasswd') . '"', 'Require user ' . $username) as $line) {
301+
fwrite($htaccess, $line . "\n");
302+
}
303+
fclose($htaccess);
304+
305+
$htpasswd = fopen(realpath(dirname(__DIR__) . '/config/.htpasswd'), 'a+');
306+
fwrite($htpasswd, $username . ':' . crypt($password) . "\n");
307+
fclose($htpasswd);
308+
309+
$this->config->addUser($path, $username, $password);
310+
306311
if ($connect) {
307312
$this->connect($path, $username, $password);
308313
}
@@ -371,7 +376,7 @@ public function connect($server, $username, $password, $database = NULL)
371376
throw new Exception("JSONDB Error: There is no registered server with the path \"{$server}\".");
372377
}
373378

374-
if ($config[$server]['username'] !== sha1(md5($username)) || $config[$server]['password'] !== sha1(md5($password))) {
379+
if ($config[$server]['username'] !== Util::crypt($username) || $config[$server]['password'] !== Util::crypt($password)) {
375380
$this->benchmark->mark('jsondb_(connect)_end');
376381
throw new Exception("JSONDB Error: User's authentication failed for user \"{$username}\" on server \"{$server}\". Access denied.");
377382
}
@@ -482,57 +487,23 @@ public function query($query)
482487
}
483488

484489
$this->queryString = $query;
490+
$this->queryIsExecuted = FALSE;
491+
$this->queryIsPrepared = FALSE;
485492

486493
return $this->execute();
487494
}
488495

489496
/**
490497
* Sends a prepared query.
491498
* @param string $query The query
492-
* @return JSONDB
499+
* @return PreparedQueryStatement
493500
*/
494501
public function prepare($query)
495502
{
496503
$this->queryString = $query;
497-
return $this->_prepareQuery();
498-
}
499-
500-
/**
501-
* Binds a value in a prepared query.
502-
* @param string $key The parameter's key
503-
* @param string|int|bool $value The parameter's value
504-
* @param int $parse_method The parse method to use
505-
* @throws Exception
506-
*/
507-
public function bindValue($key, $value, $parse_method = JSONDB::PARAM_STRING)
508-
{
509-
if ($this->queryIsPrepared()) {
510-
if (in_array($key, $this->preparedQueryKeys, TRUE)) {
511-
switch ($parse_method) {
512-
default:
513-
case self::PARAM_STRING:
514-
$value = self::quote((string)$value);
515-
break;
516-
517-
case self::PARAM_INT:
518-
$value = (int)$value;
519-
break;
520-
521-
case self::PARAM_BOOL:
522-
$value = ((string)((int)$value)) . ':JSONDB::TO_BOOL:';
523-
break;
504+
$this->queryIsPrepared = TRUE;
524505

525-
case self::PARAM_NULL:
526-
$value = (string)$value . ':JSONDB::TO_NULL:';
527-
break;
528-
}
529-
$this->queryString = str_replace($key, $value, $this->queryString);
530-
} else {
531-
throw new Exception("JSONDB Error: Can't bind the value \"{$value}\" for the key \"{$key}\". The key isn't in the query.");
532-
}
533-
} else {
534-
throw new Exception("JSONDB Error: Can't use JSONDB::bindValue() with non prepared queries. Send your query with JSONDB::prepare() first.");
535-
}
506+
return new PreparedQueryStatement($query, $this);
536507
}
537508

538509
/**
@@ -550,31 +521,32 @@ public static function quote($value)
550521
* @return mixed
551522
* @throws Exception
552523
*/
553-
public function execute()
524+
private function execute()
554525
{
555-
if (NULL === $this->database || NULL === $this->parsedQuery) {
556-
throw new Exception("JSONDB Error: Can't execute the query. No database/table selected or internal error.");
557-
}
526+
if (!$this->queryIsExecuted()) {
527+
if (NULL === $this->database || NULL === $this->parsedQuery) {
528+
throw new Exception("JSONDB Error: Can't execute the query. No database/table selected or internal error.");
529+
}
558530

559-
if ($this->queryIsPrepared()) {
560-
$this->queryIsPrepared = FALSE;
561-
return $this->query($this->queryString);
562-
}
531+
$this->setTable($this->parsedQuery['table']);
532+
$table_path = $this->_getTablePath();
533+
if (!file_exists($table_path) || !is_readable($table_path) || !is_writable($table_path)) {
534+
throw new Exception("JSONDB Error: Can't execute the query. The table \"{$this->table}\" doesn't exists in database \"{$this->database}\" or file access denied.");
535+
}
563536

564-
$this->setTable($this->parsedQuery['table']);
565-
$table_path = $this->_getTablePath();
566-
if (!file_exists($table_path) || !is_readable($table_path) || !is_writable($table_path)) {
567-
throw new Exception("JSONDB Error: Can't execute the query. The table \"{$this->table}\" doesn't exists in database \"{$this->database}\" or file access denied.");
568-
}
537+
$json_array = $this->cache->get($table_path);
538+
$method = "_{$this->parsedQuery['action']}";
569539

570-
$json_array = $this->cache->get($table_path);
571-
$method = "_{$this->parsedQuery['action']}";
540+
$this->benchmark->mark('jsondb_(query)_start');
541+
$return = $this->$method($json_array);
542+
$this->benchmark->mark('jsondb_(query)_end');
572543

573-
$this->benchmark->mark('jsondb_(query)_start');
574-
$return = $this->$method($json_array);
575-
$this->benchmark->mark('jsondb_(query)_end');
544+
$this->queryIsExecuted = TRUE;
576545

577-
return $return;
546+
return $return;
547+
} else {
548+
throw new Exception('JSONDB Error: There is no query to execute, or the query is already executed.');
549+
}
578550
}
579551

580552
/**
@@ -605,19 +577,6 @@ public function queryIsExecuted()
605577
return $this->queryIsExecuted === TRUE;
606578
}
607579

608-
/**
609-
* Prepare a query.
610-
* @return JSONDB
611-
*/
612-
private function _prepareQuery()
613-
{
614-
$this->queryIsPrepared = TRUE;
615-
$query = $this->queryString;
616-
preg_match_all('#(:[\w]+)#', $query, $keys);
617-
$this->preparedQueryKeys = $keys[0];
618-
return static::getInstance();
619-
}
620-
621580
/**
622581
* @param $data
623582
* @param bool $min
@@ -798,7 +757,7 @@ protected function _select($data)
798757
$result = $temp;
799758

800759
$this->queryResults = $result;
801-
$this->queryIsExecuted = TRUE;
760+
802761
return new QueryResult($this->queryResults, $this);
803762
}
804763

0 commit comments

Comments
 (0)