1+ <?php
2+
3+ /**
4+ * JSONDB - JSON Database Manager
5+ *
6+ * Manage local databases with JSON files and JSON Query Language (JQL)
7+ *
8+ * This content is released under the MIT License (MIT)
9+ *
10+ * Copyright (c) 2016, Centers Technologies
11+ *
12+ * Permission is hereby granted, free of charge, to any person obtaining a copy
13+ * of this software and associated documentation files (the "Software"), to deal
14+ * in the Software without restriction, including without limitation the rights
15+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+ * copies of the Software, and to permit persons to whom the Software is
17+ * furnished to do so, subject to the following conditions:
18+ *
19+ * The above copyright notice and this permission notice shall be included in
20+ * all copies or substantial portions of the Software.
21+ *
22+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
28+ * THE SOFTWARE.
29+ *
30+ * @package JSONDB
31+ * @author Nana Axel
32+ * @copyright Copyright (c) 2016, Centers Technologies
33+ * @license http://opensource.org/licenses/MIT MIT License
34+ * @filesource
35+ */
36+
37+ namespace JSONDB ;
38+
39+ /**
40+ * Class Configuration
41+ *
42+ * @package JSONDB
43+ * @subpackage Utilities
44+ * @category Configuration
45+ * @author Nana Axel
46+ */
47+ class Configuration
48+ {
49+ /**
50+ * Adds a user in the inner configuration file
51+ * @param string $server The path to the server
52+ * @param string $username The username
53+ * @param string $password The user's password
54+ */
55+ public function addUser ($ server , $ username , $ password )
56+ {
57+ $ this ->_writeConfig ('users ' , array_merge ($ this ->getConfig ('users ' ), array ($ server => array ('username ' => sha1 (md5 ($ username )), 'password ' => sha1 (md5 ($ password ))))));
58+ }
59+
60+ /**
61+ * Gets a JSONDB configuration file
62+ * @param string $filename The config file's name
63+ * @return array
64+ */
65+ public function getConfig ($ filename )
66+ {
67+ if ($ this ->_exists ($ filename )) {
68+ return json_decode (file_get_contents (realpath ( dirname (__DIR__ ) . "/config/ {$ filename }.json " )), TRUE );
69+ } else {
70+ $ this ->_writeConfig ($ filename , array ());
71+ return array ();
72+ }
73+ }
74+
75+ /**
76+ * Writes a config file
77+ * @param string $filename
78+ * @param array $config
79+ * @return bool
80+ */
81+ private function _writeConfig ($ filename , array $ config )
82+ {
83+ return (bool )file_put_contents (realpath ( dirname (__DIR__ ) . "/config/ {$ filename }.json " ), json_encode ($ config ));
84+ }
85+
86+ /**
87+ * Checks if a configuration file exist
88+ * @param string $filename
89+ * @return bool
90+ */
91+ private function _exists ($ filename )
92+ {
93+ return file_exists (realpath ( dirname (__DIR__ ) . "/config/ {$ filename }.json " ));
94+ }
95+ }
0 commit comments