Skip to content

Commit 8eef901

Browse files
committed
Now use a default folder to store all servers
* All servers created by the user are now stored in a default folder managed by JSONDB. * To create a server, the user have to give the name of this server instead of the path * The same way is used to connect to a server
1 parent 222ceab commit 8eef901

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/JSONDB/JSONDB.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -273,16 +273,17 @@ protected static function &getInstance()
273273

274274
/**
275275
* Creates a new server.
276-
* @param string $path The server's path
276+
* @param string $name The server's path
277277
* @param string $username The server's username
278278
* @param string $password The server's user password
279279
* @param bool $connect If JSONDB connects directly to the server after creation
280280
* @return JSONDB
281281
* @throws Exception
282282
*/
283-
public function createServer($path, $username, $password, $connect = FALSE)
283+
public function createServer($name, $username, $password, $connect = FALSE)
284284
{
285285
$this->benchmark->mark('jsondb_(createServer)_start');
286+
$path = dirname(dirname(__DIR__)) . '/servers/' . $name;
286287
if (isset($path, $username, $password)) {
287288
if (file_exists($path) || is_dir($path)) {
288289
$this->benchmark->mark('jsondb_(createServer)_end');
@@ -297,19 +298,19 @@ public function createServer($path, $username, $password, $connect = FALSE)
297298
chmod($path, 0777);
298299

299300
$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+
foreach(array('AuthType Basic', 'AuthName "JSONDB Server Access"', 'AuthUserFile "' . realpath(dirname(dirname(__DIR__)) . '/config/.htpasswd') . '"', 'Require user ' . $username) as $line) {
301302
fwrite($htaccess, $line . "\n");
302303
}
303304
fclose($htaccess);
304305

305-
$htpasswd = fopen(realpath(dirname(__DIR__) . '/config/.htpasswd'), 'a+');
306+
$htpasswd = fopen(realpath(dirname(dirname(__DIR__)) . '/config/.htpasswd'), 'a+');
306307
fwrite($htpasswd, $username . ':' . crypt($password) . "\n");
307308
fclose($htpasswd);
308309

309-
$this->config->addUser(realpath($path), $username, $password);
310+
$this->config->addUser($name, $username, $password);
310311

311312
if ($connect) {
312-
$this->connect($path, $username, $password);
313+
$this->connect($name, $username, $password);
313314
}
314315
}
315316
$this->benchmark->mark('jsondb_(createServer)_end');
@@ -370,19 +371,18 @@ public function connect($server, $username, $password, $database = NULL)
370371
{
371372
$this->benchmark->mark('jsondb_(connect)_start');
372373
$config = $this->config->getConfig('users');
373-
$server = realpath($server);
374374

375375
if (!array_key_exists($server, $config)) {
376376
$this->benchmark->mark('jsondb_(connect)_end');
377-
throw new Exception("JSONDB Error: There is no registered server with the path \"{$server}\".");
377+
throw new Exception("JSONDB Error: There is no registered server with the name \"{$server}\".");
378378
}
379379

380380
if ($config[$server]['username'] !== Util::crypt($username) || $config[$server]['password'] !== Util::crypt($password)) {
381381
$this->benchmark->mark('jsondb_(connect)_end');
382382
throw new Exception("JSONDB Error: User's authentication failed for user \"{$username}\" on server \"{$server}\". Access denied.");
383383
}
384384

385-
$this->server = $server;
385+
$this->server = realpath(dirname(dirname(__DIR__)) . '/servers/' . $server);
386386
$this->database = $database;
387387
$this->username = $username;
388388
$this->password = $password;

0 commit comments

Comments
 (0)