@@ -27,58 +27,58 @@ |
||
27 | 27 | |
28 | 28 | class LocalAuthentificator implements IAuthentificator { |
29 | 29 | |
30 | - public function __construct() { |
|
31 | - // |
|
32 | - } |
|
30 | + public function __construct() { |
|
31 | + // |
|
32 | + } |
|
33 | 33 | |
34 | - /** |
|
35 | - * check password of user and import user, if neccessary |
|
36 | - * |
|
37 | - * @param $username string name of user |
|
38 | - * @param $password string password of user |
|
39 | - * |
|
40 | - * @return userID or -1, if credentials are wrong |
|
41 | - */ |
|
42 | - public function checkPasswordAndImport(string $username, string $password) : int { |
|
43 | - $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array( |
|
44 | - 'username' => &$username |
|
45 | - )); |
|
34 | + /** |
|
35 | + * check password of user and import user, if neccessary |
|
36 | + * |
|
37 | + * @param $username string name of user |
|
38 | + * @param $password string password of user |
|
39 | + * |
|
40 | + * @return userID or -1, if credentials are wrong |
|
41 | + */ |
|
42 | + public function checkPasswordAndImport(string $username, string $password) : int { |
|
43 | + $row = Database::getInstance()->getRow("SELECT * FROM `{praefix}user` WHERE `username` = :username AND `activated` = '1'; ", array( |
|
44 | + 'username' => &$username |
|
45 | + )); |
|
46 | 46 | |
47 | - if (!$row) { |
|
48 | - //user doesnt exists |
|
49 | - return -1; |
|
50 | - } |
|
47 | + if (!$row) { |
|
48 | + //user doesnt exists |
|
49 | + return -1; |
|
50 | + } |
|
51 | 51 | |
52 | - //get salt |
|
53 | - $salt = $row['salt']; |
|
52 | + //get salt |
|
53 | + $salt = $row['salt']; |
|
54 | 54 | |
55 | - //add salt to password |
|
56 | - $password .= $salt; |
|
55 | + //add salt to password |
|
56 | + $password .= $salt; |
|
57 | 57 | |
58 | - //verify password |
|
59 | - if (password_verify($password, $row['password'])) { |
|
60 | - //correct password |
|
58 | + //verify password |
|
59 | + if (password_verify($password, $row['password'])) { |
|
60 | + //correct password |
|
61 | 61 | |
62 | - //check, if a newer password algorithmus is available --> rehash required |
|
63 | - if (password_needs_rehash($row['password'], PASSWORD_DEFAULT)) { |
|
64 | - //rehash password |
|
65 | - $new_hash = self::hashPassword($password, $salt); |
|
62 | + //check, if a newer password algorithmus is available --> rehash required |
|
63 | + if (password_needs_rehash($row['password'], PASSWORD_DEFAULT)) { |
|
64 | + //rehash password |
|
65 | + $new_hash = self::hashPassword($password, $salt); |
|
66 | 66 | |
67 | - //update password in database |
|
68 | - Database::getInstance()->execute("UPDATE `{praefix}user` SET `password` = :password WHERE `userID` = :userID; ", array( |
|
69 | - 'password' => $new_hash, |
|
70 | - 'userID' => array( |
|
71 | - 'type' => PDO::PARAM_INT, |
|
72 | - 'value' => $row['userID'] |
|
73 | - ) |
|
74 | - )); |
|
75 | - } |
|
67 | + //update password in database |
|
68 | + Database::getInstance()->execute("UPDATE `{praefix}user` SET `password` = :password WHERE `userID` = :userID; ", array( |
|
69 | + 'password' => $new_hash, |
|
70 | + 'userID' => array( |
|
71 | + 'type' => PDO::PARAM_INT, |
|
72 | + 'value' => $row['userID'] |
|
73 | + ) |
|
74 | + )); |
|
75 | + } |
|
76 | 76 | |
77 | - return $row['userID']; |
|
78 | - } else { |
|
79 | - return -1; |
|
80 | - } |
|
81 | - } |
|
77 | + return $row['userID']; |
|
78 | + } else { |
|
79 | + return -1; |
|
80 | + } |
|
81 | + } |
|
82 | 82 | |
83 | 83 | } |
84 | 84 |
@@ -27,49 +27,49 @@ |
||
27 | 27 | |
28 | 28 | class SettingsInstaller extends PluginInstaller_Plugin { |
29 | 29 | |
30 | - public function install(Plugin $plugin, array $install_json): bool { |
|
31 | - if (isset($install_json['set-settings'])) { |
|
32 | - $prefs = new Preferences("plugin_" . $plugin->getName() . "_uninstall"); |
|
30 | + public function install(Plugin $plugin, array $install_json): bool { |
|
31 | + if (isset($install_json['set-settings'])) { |
|
32 | + $prefs = new Preferences("plugin_" . $plugin->getName() . "_uninstall"); |
|
33 | 33 | |
34 | - foreach ($install_json['set-settings'] as $key=>$value) { |
|
35 | - if (Settings::contains($key)) { |
|
36 | - //backup old value |
|
37 | - $old_value = Settings::get($key); |
|
38 | - $prefs->put($key, $old_value); |
|
39 | - } else { |
|
40 | - $prefs->put($key, null); |
|
41 | - } |
|
34 | + foreach ($install_json['set-settings'] as $key=>$value) { |
|
35 | + if (Settings::contains($key)) { |
|
36 | + //backup old value |
|
37 | + $old_value = Settings::get($key); |
|
38 | + $prefs->put($key, $old_value); |
|
39 | + } else { |
|
40 | + $prefs->put($key, null); |
|
41 | + } |
|
42 | 42 | |
43 | - //set new value |
|
44 | - Settings::set($key, $value); |
|
45 | - } |
|
43 | + //set new value |
|
44 | + Settings::set($key, $value); |
|
45 | + } |
|
46 | 46 | |
47 | - $prefs->save(); |
|
48 | - } |
|
47 | + $prefs->save(); |
|
48 | + } |
|
49 | 49 | |
50 | - return true; |
|
51 | - } |
|
50 | + return true; |
|
51 | + } |
|
52 | 52 | |
53 | - public function uninstall(Plugin $plugin, array $install_json): bool { |
|
54 | - //restore old values |
|
53 | + public function uninstall(Plugin $plugin, array $install_json): bool { |
|
54 | + //restore old values |
|
55 | 55 | |
56 | - $prefs = new Preferences("plugin_" . $plugin->getName() . "_uninstall"); |
|
56 | + $prefs = new Preferences("plugin_" . $plugin->getName() . "_uninstall"); |
|
57 | 57 | |
58 | - foreach ($prefs->listAll() as $key=>$value) { |
|
59 | - if ($value != null) { |
|
60 | - Settings::set($key, $value); |
|
61 | - } |
|
62 | - } |
|
58 | + foreach ($prefs->listAll() as $key=>$value) { |
|
59 | + if ($value != null) { |
|
60 | + Settings::set($key, $value); |
|
61 | + } |
|
62 | + } |
|
63 | 63 | |
64 | - //clear preferences |
|
65 | - $prefs->clearAll(); |
|
64 | + //clear preferences |
|
65 | + $prefs->clearAll(); |
|
66 | 66 | |
67 | - return true; |
|
68 | - } |
|
67 | + return true; |
|
68 | + } |
|
69 | 69 | |
70 | - public function upgrade(Plugin $plugin, array $install_json): bool { |
|
71 | - return $this->install($plugin, $install_json); |
|
72 | - } |
|
70 | + public function upgrade(Plugin $plugin, array $install_json): bool { |
|
71 | + return $this->install($plugin, $install_json); |
|
72 | + } |
|
73 | 73 | |
74 | 74 | } |
75 | 75 |
@@ -55,12 +55,12 @@ discard block |
||
55 | 55 | $this->conn = new PDO("mysql:host=" . $this->host . ";port=" . $this->port . ";dbname=" . $this->database . "", $this->username, $this->password, $this->options); |
56 | 56 | |
57 | 57 | //throw exception |
58 | - $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
58 | + $this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
59 | 59 | |
60 | 60 | if (DEBUG_MODE) { |
61 | - //$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
62 | - //$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
63 | - } |
|
61 | + //$this->conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); |
|
62 | + //$this->conn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false); |
|
63 | + } |
|
64 | 64 | } catch (PDOException $e) { |
65 | 65 | echo "Couldnt connect to database!"; |
66 | 66 | echo $e->getTraceAsString(); |
@@ -106,18 +106,18 @@ discard block |
||
106 | 106 | $res = $stmt->execute(); |
107 | 107 | |
108 | 108 | if (!$res) { |
109 | - if (DEBUG_MODE) { |
|
110 | - echo "SQL Query: " + $sql; |
|
111 | - } else { |
|
112 | - echo "Debug Mode is disabled. You can enable it in config/config.php file.<br />"; |
|
113 | - } |
|
109 | + if (DEBUG_MODE) { |
|
110 | + echo "SQL Query: " + $sql; |
|
111 | + } else { |
|
112 | + echo "Debug Mode is disabled. You can enable it in config/config.php file.<br />"; |
|
113 | + } |
|
114 | 114 | |
115 | 115 | //TODO: throw exception instead |
116 | 116 | |
117 | 117 | print_r($stmt->errorInfo()); |
118 | 118 | |
119 | - flush(); |
|
120 | - ob_end_flush(); |
|
119 | + flush(); |
|
120 | + ob_end_flush(); |
|
121 | 121 | exit; |
122 | 122 | } |
123 | 123 | |
@@ -129,7 +129,7 @@ discard block |
||
129 | 129 | exit; |
130 | 130 | } |
131 | 131 | |
132 | - print_r($e); |
|
132 | + print_r($e); |
|
133 | 133 | |
134 | 134 | echo "<br /><br /><b>Query</b>: " . $sql . ", parameters: "; |
135 | 135 | var_dump($params); |
@@ -148,7 +148,7 @@ discard block |
||
148 | 148 | exit; |
149 | 149 | } |
150 | 150 | |
151 | - print_r($e); |
|
151 | + print_r($e); |
|
152 | 152 | |
153 | 153 | echo "<br /><br /><b>Query</b>: " . $sql . ", parameters: "; |
154 | 154 | var_dump($params); |
@@ -212,8 +212,8 @@ discard block |
||
212 | 212 | |
213 | 213 | $sql = str_replace("{DBPRAEFIX}", $this->praefix, $sql); |
214 | 214 | $sql = str_replace("{praefix}", $this->praefix, $sql); |
215 | - $sql = str_replace("{prefix}", $this->praefix, $sql); |
|
216 | - $sql = str_replace("{PREFIX}", $this->praefix, $sql); |
|
215 | + $sql = str_replace("{prefix}", $this->praefix, $sql); |
|
216 | + $sql = str_replace("{PREFIX}", $this->praefix, $sql); |
|
217 | 217 | return str_replace("{PRAEFIX}", $this->praefix, $sql); |
218 | 218 | } |
219 | 219 | |
@@ -261,10 +261,10 @@ discard block |
||
261 | 261 | $res = $stmt->execute(); |
262 | 262 | |
263 | 263 | if (!$res) { |
264 | - if (DEBUG_MODE) { |
|
265 | - echo "SQL Query: " . $sql . "<br />"; |
|
266 | - var_dump($params); |
|
267 | - } |
|
264 | + if (DEBUG_MODE) { |
|
265 | + echo "SQL Query: " . $sql . "<br />"; |
|
266 | + var_dump($params); |
|
267 | + } |
|
268 | 268 | |
269 | 269 | throw new PDOException("PDOException while getRow(): " . ($this->getErrorInfo())[3] . "\n" . ($stmt->errorInfo())[2] . ""); |
270 | 270 | } |
@@ -302,9 +302,9 @@ discard block |
||
302 | 302 | $rows = $stmt->fetchAll(PDO::FETCH_ASSOC); |
303 | 303 | |
304 | 304 | if ($rows === FALSE) { |
305 | - //failure, http://php.net/manual/de/pdostatement.fetchall.php |
|
306 | - throw new PDOException("Failure while listRows(): " . print_r($this->getErrorInfo(), true)); |
|
307 | - } |
|
305 | + //failure, http://php.net/manual/de/pdostatement.fetchall.php |
|
306 | + throw new PDOException("Failure while listRows(): " . print_r($this->getErrorInfo(), true)); |
|
307 | + } |
|
308 | 308 | |
309 | 309 | return $rows; |
310 | 310 | } |
@@ -350,9 +350,9 @@ discard block |
||
350 | 350 | } |
351 | 351 | } |
352 | 352 | |
353 | - public function lastInsertId(): int { |
|
354 | - return $this->conn->lastInsertId(); |
|
355 | - } |
|
353 | + public function lastInsertId(): int { |
|
354 | + return $this->conn->lastInsertId(); |
|
355 | + } |
|
356 | 356 | |
357 | 357 | public function listQueryHistory() : array { |
358 | 358 | return self::$query_history; |
@@ -27,106 +27,106 @@ |
||
27 | 27 | |
28 | 28 | class Preferences { |
29 | 29 | |
30 | - protected $area = ""; |
|
31 | - protected $prefs = array(); |
|
32 | - protected $changed_prefs = array(); |
|
30 | + protected $area = ""; |
|
31 | + protected $prefs = array(); |
|
32 | + protected $changed_prefs = array(); |
|
33 | 33 | |
34 | - public function __construct(string $area) { |
|
35 | - $area = strtolower($area); |
|
34 | + public function __construct(string $area) { |
|
35 | + $area = strtolower($area); |
|
36 | 36 | |
37 | - if (!PHPUtils::startsWith($area, "plugin_") && !PHPUtils::startsWith($area, "style_")) { |
|
38 | - throw new IllegalArgumentException("preferences area name should start with 'plugin_' or 'style_'´."); |
|
39 | - } |
|
37 | + if (!PHPUtils::startsWith($area, "plugin_") && !PHPUtils::startsWith($area, "style_")) { |
|
38 | + throw new IllegalArgumentException("preferences area name should start with 'plugin_' or 'style_'´."); |
|
39 | + } |
|
40 | 40 | |
41 | - $this->area = $area; |
|
41 | + $this->area = $area; |
|
42 | 42 | |
43 | - if (Cache::contains("preferences", "preferences-" . $area)) { |
|
44 | - $this->prefs = Cache::get("preferences", "preferences-" . $area); |
|
45 | - } else { |
|
46 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}preferences` WHERE `area` = :area; ", array('area' => $area)); |
|
43 | + if (Cache::contains("preferences", "preferences-" . $area)) { |
|
44 | + $this->prefs = Cache::get("preferences", "preferences-" . $area); |
|
45 | + } else { |
|
46 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}preferences` WHERE `area` = :area; ", array('area' => $area)); |
|
47 | 47 | |
48 | - foreach ($rows as $row) { |
|
49 | - $this->prefs[$row['key']] = unserialize($row['value']); |
|
50 | - } |
|
48 | + foreach ($rows as $row) { |
|
49 | + $this->prefs[$row['key']] = unserialize($row['value']); |
|
50 | + } |
|
51 | 51 | |
52 | - //cache preferences |
|
53 | - Cache::put("preferences", "preferences-" . $area, $this->prefs); |
|
54 | - } |
|
55 | - } |
|
52 | + //cache preferences |
|
53 | + Cache::put("preferences", "preferences-" . $area, $this->prefs); |
|
54 | + } |
|
55 | + } |
|
56 | 56 | |
57 | - public function contains (string $key) : bool { |
|
58 | - return isset($this->prefs[$key]); |
|
59 | - } |
|
57 | + public function contains (string $key) : bool { |
|
58 | + return isset($this->prefs[$key]); |
|
59 | + } |
|
60 | 60 | |
61 | - public function get (string $key, $default = null) { |
|
62 | - if (!isset($this->prefs[$key])) { |
|
63 | - return $default; |
|
64 | - } |
|
61 | + public function get (string $key, $default = null) { |
|
62 | + if (!isset($this->prefs[$key])) { |
|
63 | + return $default; |
|
64 | + } |
|
65 | 65 | |
66 | - return $this->prefs[$key]; |
|
67 | - } |
|
66 | + return $this->prefs[$key]; |
|
67 | + } |
|
68 | 68 | |
69 | - public function put (string $key, $value, bool $auto_save = false) { |
|
70 | - $contains_key = $this->contains($key); |
|
69 | + public function put (string $key, $value, bool $auto_save = false) { |
|
70 | + $contains_key = $this->contains($key); |
|
71 | 71 | |
72 | - $this->prefs[$key] = $value; |
|
72 | + $this->prefs[$key] = $value; |
|
73 | 73 | |
74 | - if ($auto_save) { |
|
75 | - //update database |
|
76 | - Database::getInstance()->execute("INSERT INTO `{praefix}preferences` ( |
|
74 | + if ($auto_save) { |
|
75 | + //update database |
|
76 | + Database::getInstance()->execute("INSERT INTO `{praefix}preferences` ( |
|
77 | 77 | `key`, `area`, `value` |
78 | 78 | ) VALUES ( |
79 | 79 | :key, :area, :value |
80 | 80 | ) ON DUPLICATE KEY UPDATE `value` = :value; ", array( |
81 | - 'key' => $key, |
|
82 | - 'area' => $this->area, |
|
83 | - 'value' => serialize($value) |
|
84 | - )); |
|
85 | - } else { |
|
86 | - $this->changed_prefs[$key] = $contains_key; |
|
87 | - } |
|
88 | - } |
|
81 | + 'key' => $key, |
|
82 | + 'area' => $this->area, |
|
83 | + 'value' => serialize($value) |
|
84 | + )); |
|
85 | + } else { |
|
86 | + $this->changed_prefs[$key] = $contains_key; |
|
87 | + } |
|
88 | + } |
|
89 | 89 | |
90 | - public function listAll () : array { |
|
91 | - return $this->prefs; |
|
92 | - } |
|
90 | + public function listAll () : array { |
|
91 | + return $this->prefs; |
|
92 | + } |
|
93 | 93 | |
94 | - public function clearAll () { |
|
95 | - //delete from database |
|
96 | - Database::getInstance()->execute("DELETE FROM `{praefix}preferences` WHERE `area` = :area; ", array('area' => $this->area)); |
|
94 | + public function clearAll () { |
|
95 | + //delete from database |
|
96 | + Database::getInstance()->execute("DELETE FROM `{praefix}preferences` WHERE `area` = :area; ", array('area' => $this->area)); |
|
97 | 97 | |
98 | - $this->prefs = array(); |
|
99 | - $this->changed_prefs = array(); |
|
98 | + $this->prefs = array(); |
|
99 | + $this->changed_prefs = array(); |
|
100 | 100 | |
101 | - //clear cache |
|
102 | - Cache::clear("preferences", "preferences-" . $this->area); |
|
103 | - } |
|
101 | + //clear cache |
|
102 | + Cache::clear("preferences", "preferences-" . $this->area); |
|
103 | + } |
|
104 | 104 | |
105 | - /** |
|
106 | - * write all values to database |
|
107 | - */ |
|
108 | - public function save () { |
|
109 | - $lines = array(); |
|
110 | - $values = array(); |
|
105 | + /** |
|
106 | + * write all values to database |
|
107 | + */ |
|
108 | + public function save () { |
|
109 | + $lines = array(); |
|
110 | + $values = array(); |
|
111 | 111 | |
112 | - $values['area'] = $this->area; |
|
112 | + $values['area'] = $this->area; |
|
113 | 113 | |
114 | - $i = 1; |
|
114 | + $i = 1; |
|
115 | 115 | |
116 | - foreach ($this->changed_prefs as $key=>$contains_key) { |
|
117 | - $lines[] = "(:key" . $i . ", :area, :value" . $i . ")"; |
|
118 | - $values['key' . $i] = $key; |
|
119 | - $values['value' . $i] = serialize($this->prefs[$key]); |
|
116 | + foreach ($this->changed_prefs as $key=>$contains_key) { |
|
117 | + $lines[] = "(:key" . $i . ", :area, :value" . $i . ")"; |
|
118 | + $values['key' . $i] = $key; |
|
119 | + $values['value' . $i] = serialize($this->prefs[$key]); |
|
120 | 120 | |
121 | - $i++; |
|
122 | - } |
|
121 | + $i++; |
|
122 | + } |
|
123 | 123 | |
124 | - $lines_str = implode(",\r\n", $lines); |
|
124 | + $lines_str = implode(",\r\n", $lines); |
|
125 | 125 | |
126 | - Database::getInstance()->execute("INSERT INTO `{praefix}preferences` ( |
|
126 | + Database::getInstance()->execute("INSERT INTO `{praefix}preferences` ( |
|
127 | 127 | `key`, `area`, `value` |
128 | 128 | ) VALUES " . $lines_str . " ON DUPLICATE KEY UPDATE `value` = VALUES(value); ", $values); |
129 | - } |
|
129 | + } |
|
130 | 130 | |
131 | 131 | } |
132 | 132 |
@@ -7,38 +7,38 @@ |
||
7 | 7 | */ |
8 | 8 | |
9 | 9 | $ldap_config = array( |
10 | - 'enabled' => false, |
|
10 | + 'enabled' => false, |
|
11 | 11 | |
12 | - // active directory server |
|
13 | - 'host' => "localhost", |
|
14 | - 'port' => 389, |
|
12 | + // active directory server |
|
13 | + 'host' => "localhost", |
|
14 | + 'port' => 389, |
|
15 | 15 | |
16 | - 'use_uri' => true, |
|
17 | - 'user_prefix' => "uid=", |
|
16 | + 'use_uri' => true, |
|
17 | + 'user_prefix' => "uid=", |
|
18 | 18 | |
19 | - 'ssl' => false, |
|
19 | + 'ssl' => false, |
|
20 | 20 | |
21 | - 'auth' => true, |
|
22 | - 'user' => "uname",//ldap rdn or dn, |
|
23 | - 'password' => "admin", |
|
21 | + 'auth' => true, |
|
22 | + 'user' => "uname",//ldap rdn or dn, |
|
23 | + 'password' => "admin", |
|
24 | 24 | |
25 | - // domain, for purposes of constructing $user |
|
26 | - 'ldap_usr_dom' => "",//e.q. @college.school.edu --> user "user1" => [email protected] |
|
25 | + // domain, for purposes of constructing $user |
|
26 | + 'ldap_usr_dom' => "",//e.q. @college.school.edu --> user "user1" => [email protected] |
|
27 | 27 | |
28 | - // active directory DN (base location of ldap search) |
|
29 | - 'dn' => "",//$dn = "OU=Departments,DC=college,DC=school,DC=edu"; |
|
28 | + // active directory DN (base location of ldap search) |
|
29 | + 'dn' => "",//$dn = "OU=Departments,DC=college,DC=school,DC=edu"; |
|
30 | 30 | |
31 | - 'readonly' => true,//only readonly access |
|
31 | + 'readonly' => true,//only readonly access |
|
32 | 32 | |
33 | - //https://www.forumsys.com/tutorials/integration-how-to/ldap/api-identity-management-ldap-server/ |
|
33 | + //https://www.forumsys.com/tutorials/integration-how-to/ldap/api-identity-management-ldap-server/ |
|
34 | 34 | |
35 | - //https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/ |
|
35 | + //https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/ |
|
36 | 36 | |
37 | - //ldap params |
|
38 | - 'params' => array( |
|
39 | - LDAP_OPT_PROTOCOL_VERSION => 3, |
|
40 | - LDAP_OPT_REFERRALS => 0 |
|
41 | - ), |
|
37 | + //ldap params |
|
38 | + 'params' => array( |
|
39 | + LDAP_OPT_PROTOCOL_VERSION => 3, |
|
40 | + LDAP_OPT_REFERRALS => 0 |
|
41 | + ), |
|
42 | 42 | ); |
43 | 43 | |
44 | 44 | ?> |
@@ -27,137 +27,137 @@ discard block |
||
27 | 27 | |
28 | 28 | class LDAPClient { |
29 | 29 | |
30 | - //host and port |
|
31 | - protected $host = ""; |
|
32 | - protected $port = 389; |
|
33 | - |
|
34 | - //flag, if uri is used instead of host |
|
35 | - protected $uri_used = false; |
|
36 | - protected $uri = ""; |
|
30 | + //host and port |
|
31 | + protected $host = ""; |
|
32 | + protected $port = 389; |
|
33 | + |
|
34 | + //flag, if uri is used instead of host |
|
35 | + protected $uri_used = false; |
|
36 | + protected $uri = ""; |
|
37 | 37 | |
38 | - protected $conn = null; |
|
39 | - protected $res = null; |
|
38 | + protected $conn = null; |
|
39 | + protected $res = null; |
|
40 | 40 | |
41 | - protected $dn = ""; |
|
42 | - |
|
43 | - protected $ldap_config = array(); |
|
44 | - |
|
45 | - //flag, if connection is readonly |
|
46 | - protected $readonly = false; |
|
47 | - |
|
48 | - public function __construct (string $host = "", int $port = 0, bool $ssl = false) { |
|
49 | - $ldap_config = array( |
|
50 | - 'enabled' => true, |
|
51 | - 'ssl' => $ssl |
|
52 | - ); |
|
53 | - |
|
54 | - if (empty($host)) { |
|
55 | - //load local config |
|
56 | - if (!file_exists(CONFIG_PATH . "ldap.php")) { |
|
57 | - throw new IllegalStateException("No ldap configuration file config/ldap.php exists!"); |
|
58 | - } |
|
41 | + protected $dn = ""; |
|
42 | + |
|
43 | + protected $ldap_config = array(); |
|
44 | + |
|
45 | + //flag, if connection is readonly |
|
46 | + protected $readonly = false; |
|
47 | + |
|
48 | + public function __construct (string $host = "", int $port = 0, bool $ssl = false) { |
|
49 | + $ldap_config = array( |
|
50 | + 'enabled' => true, |
|
51 | + 'ssl' => $ssl |
|
52 | + ); |
|
53 | + |
|
54 | + if (empty($host)) { |
|
55 | + //load local config |
|
56 | + if (!file_exists(CONFIG_PATH . "ldap.php")) { |
|
57 | + throw new IllegalStateException("No ldap configuration file config/ldap.php exists!"); |
|
58 | + } |
|
59 | 59 | |
60 | - //override $ldap_config |
|
61 | - require(CONFIG_PATH . "ldap.php"); |
|
60 | + //override $ldap_config |
|
61 | + require(CONFIG_PATH . "ldap.php"); |
|
62 | 62 | |
63 | - //check, if ldap is enabled |
|
64 | - if ($ldap_config['enabled'] == false) { |
|
65 | - throw new IllegalStateException("LDAP is disabled. Enable ldap in file config/ldap.php ."); |
|
66 | - } |
|
63 | + //check, if ldap is enabled |
|
64 | + if ($ldap_config['enabled'] == false) { |
|
65 | + throw new IllegalStateException("LDAP is disabled. Enable ldap in file config/ldap.php ."); |
|
66 | + } |
|
67 | 67 | |
68 | - $this->host = $ldap_config['host']; |
|
69 | - $this->port = intval($ldap_config['port']); |
|
68 | + $this->host = $ldap_config['host']; |
|
69 | + $this->port = intval($ldap_config['port']); |
|
70 | 70 | |
71 | - $this->readonly = boolval($ldap_config['readonly']); |
|
72 | - } else { |
|
73 | - $this->host = $host; |
|
74 | - $this->port = $port; |
|
75 | - } |
|
71 | + $this->readonly = boolval($ldap_config['readonly']); |
|
72 | + } else { |
|
73 | + $this->host = $host; |
|
74 | + $this->port = $port; |
|
75 | + } |
|
76 | 76 | |
77 | - if (isset($this->ldap_config['use_uri']) && $this->ldap_config['use_uri']) { |
|
78 | - $this->uri = "ldap://" . $this->host . ":" . $this->port; |
|
77 | + if (isset($this->ldap_config['use_uri']) && $this->ldap_config['use_uri']) { |
|
78 | + $this->uri = "ldap://" . $this->host . ":" . $this->port; |
|
79 | 79 | |
80 | - //set flag, that uri is used |
|
81 | - $this->uri_used = true; |
|
82 | - } |
|
80 | + //set flag, that uri is used |
|
81 | + $this->uri_used = true; |
|
82 | + } |
|
83 | 83 | |
84 | - //check, if SSL is enabled |
|
85 | - if ($ldap_config['ssl'] == true) { |
|
86 | - //use OpenLDAP 2.x.x URI instead of host |
|
87 | - $this->uri = "ldaps://" . $this->host . ":" . $this->port; |
|
84 | + //check, if SSL is enabled |
|
85 | + if ($ldap_config['ssl'] == true) { |
|
86 | + //use OpenLDAP 2.x.x URI instead of host |
|
87 | + $this->uri = "ldaps://" . $this->host . ":" . $this->port; |
|
88 | 88 | |
89 | - //set flag, that uri is used |
|
90 | - $this->uri_used = true; |
|
91 | - } |
|
89 | + //set flag, that uri is used |
|
90 | + $this->uri_used = true; |
|
91 | + } |
|
92 | 92 | |
93 | - //check, if host / uri is valide (this statement doesnt connect to server!) - see also http://php.net/manual/de/function.ldap-connect.php |
|
94 | - if ($this->uri_used) { |
|
95 | - $this->conn = ldap_connect($this->uri); |
|
96 | - } else { |
|
97 | - $this->conn = ldap_connect($this->host, $this->port); |
|
98 | - } |
|
93 | + //check, if host / uri is valide (this statement doesnt connect to server!) - see also http://php.net/manual/de/function.ldap-connect.php |
|
94 | + if ($this->uri_used) { |
|
95 | + $this->conn = ldap_connect($this->uri); |
|
96 | + } else { |
|
97 | + $this->conn = ldap_connect($this->host, $this->port); |
|
98 | + } |
|
99 | 99 | |
100 | - if ($this->conn === FALSE) { |
|
101 | - $error_str = ($this->uri_used ? "URI: " . $this->uri : " Host: " . $this->host . ", port: " . $this->port); |
|
100 | + if ($this->conn === FALSE) { |
|
101 | + $error_str = ($this->uri_used ? "URI: " . $this->uri : " Host: " . $this->host . ", port: " . $this->port); |
|
102 | 102 | |
103 | - throw new IllegalStateException("LDAP connection parameters (host or port) are invalide." . (DEBUG_MODE ? " " . $error_str : "")); |
|
104 | - } |
|
103 | + throw new IllegalStateException("LDAP connection parameters (host or port) are invalide." . (DEBUG_MODE ? " " . $error_str : "")); |
|
104 | + } |
|
105 | 105 | |
106 | - $this->dn = $ldap_config['dn']; |
|
106 | + $this->dn = $ldap_config['dn']; |
|
107 | 107 | |
108 | - //set ldap params |
|
109 | - if (isset($ldap_config['params'])) { |
|
110 | - foreach ($ldap_config['params'] as $key=>$value) { |
|
111 | - // configure ldap params |
|
112 | - ldap_set_option($this->conn,$key, $value); |
|
113 | - } |
|
114 | - } |
|
108 | + //set ldap params |
|
109 | + if (isset($ldap_config['params'])) { |
|
110 | + foreach ($ldap_config['params'] as $key=>$value) { |
|
111 | + // configure ldap params |
|
112 | + ldap_set_option($this->conn,$key, $value); |
|
113 | + } |
|
114 | + } |
|
115 | 115 | |
116 | - $this->ldap_config = $ldap_config; |
|
117 | - } |
|
116 | + $this->ldap_config = $ldap_config; |
|
117 | + } |
|
118 | 118 | |
119 | - public function bind (string $username = null, string $password = null) : bool { |
|
120 | - if (is_null($username) && isset($this->ldap_config['user'])) { |
|
121 | - $username = $this->ldap_config['user']; |
|
122 | - $password = $this->ldap_config['password']; |
|
123 | - } |
|
119 | + public function bind (string $username = null, string $password = null) : bool { |
|
120 | + if (is_null($username) && isset($this->ldap_config['user'])) { |
|
121 | + $username = $this->ldap_config['user']; |
|
122 | + $password = $this->ldap_config['password']; |
|
123 | + } |
|
124 | 124 | |
125 | - $ldap_usr_dom = ""; |
|
126 | - $ldap_usr_prefix = (isset($this->ldap_config['user_prefix']) ? $this->ldap_config['user_prefix'] : ""); |
|
125 | + $ldap_usr_dom = ""; |
|
126 | + $ldap_usr_prefix = (isset($this->ldap_config['user_prefix']) ? $this->ldap_config['user_prefix'] : ""); |
|
127 | 127 | |
128 | - if (isset($this->ldap_config['ldap_usr_dom'])) { |
|
129 | - $ldap_usr_dom = $this->ldap_config['ldap_usr_dom']; |
|
130 | - } |
|
128 | + if (isset($this->ldap_config['ldap_usr_dom'])) { |
|
129 | + $ldap_usr_dom = $this->ldap_config['ldap_usr_dom']; |
|
130 | + } |
|
131 | 131 | |
132 | - if ($this->conn === FALSE) { |
|
133 | - throw new IllegalStateException("ldap connection check failed."); |
|
134 | - } |
|
132 | + if ($this->conn === FALSE) { |
|
133 | + throw new IllegalStateException("ldap connection check failed."); |
|
134 | + } |
|
135 | 135 | |
136 | - //http://www.selfadsi.de/ads-attributes/user-sAMAccountName.htm |
|
136 | + //http://www.selfadsi.de/ads-attributes/user-sAMAccountName.htm |
|
137 | 137 | |
138 | - //connect and bind to ldap server |
|
139 | - if (!is_null($username)) { |
|
140 | - //with authentification |
|
141 | - $this->res = @ldap_bind($this->conn, $ldap_usr_prefix . $username . $ldap_usr_dom, $password); |
|
142 | - } else { |
|
143 | - //anonymous binding |
|
144 | - $this->res = @ldap_bind($this->conn); |
|
145 | - } |
|
146 | - |
|
147 | - return $this->res !== FALSE; |
|
148 | - } |
|
149 | - |
|
150 | - public function listGroupsOfUser (string $user) : array { |
|
151 | - // check presence in groups |
|
152 | - //$filter = "(sAMAccountName=" . $user . ")"; |
|
153 | - $filter = "(uid=" . $user . ")"; |
|
154 | - $attr = array("memberof"); |
|
155 | - |
|
156 | - //https://samjlevy.com/php-ldap-login/ |
|
157 | - |
|
158 | - $result = ldap_search($this->conn, $this->dn, $filter, $attr) or exit("Unable to search LDAP server"); |
|
138 | + //connect and bind to ldap server |
|
139 | + if (!is_null($username)) { |
|
140 | + //with authentification |
|
141 | + $this->res = @ldap_bind($this->conn, $ldap_usr_prefix . $username . $ldap_usr_dom, $password); |
|
142 | + } else { |
|
143 | + //anonymous binding |
|
144 | + $this->res = @ldap_bind($this->conn); |
|
145 | + } |
|
146 | + |
|
147 | + return $this->res !== FALSE; |
|
148 | + } |
|
149 | + |
|
150 | + public function listGroupsOfUser (string $user) : array { |
|
151 | + // check presence in groups |
|
152 | + //$filter = "(sAMAccountName=" . $user . ")"; |
|
153 | + $filter = "(uid=" . $user . ")"; |
|
154 | + $attr = array("memberof"); |
|
155 | + |
|
156 | + //https://samjlevy.com/php-ldap-login/ |
|
157 | + |
|
158 | + $result = ldap_search($this->conn, $this->dn, $filter, $attr) or exit("Unable to search LDAP server"); |
|
159 | 159 | |
160 | - /* |
|
160 | + /* |
|
161 | 161 | *return_value["count"] = number of entries in the result |
162 | 162 | * return_value[0] : refers to the details of first entry |
163 | 163 | * return_value[i]["dn"] = DN of the ith entry in the result |
@@ -167,9 +167,9 @@ discard block |
||
167 | 167 | * attribute in ith entry |
168 | 168 | * return_value[i]["attribute"][j] = jth value of attribute in ith entry |
169 | 169 | */ |
170 | - $entries = ldap_get_entries($this->conn, $result); |
|
170 | + $entries = ldap_get_entries($this->conn, $result); |
|
171 | 171 | |
172 | - /*$array = array(); |
|
172 | + /*$array = array(); |
|
173 | 173 | |
174 | 174 | $count = intval($entries['count']); |
175 | 175 | |
@@ -183,57 +183,57 @@ discard block |
||
183 | 183 | ); |
184 | 184 | }*/ |
185 | 185 | |
186 | - //https://stackoverflow.com/questions/7187994/memberof-vs-groupmembership-in-ldap-liferay |
|
186 | + //https://stackoverflow.com/questions/7187994/memberof-vs-groupmembership-in-ldap-liferay |
|
187 | 187 | |
188 | - $groups = array(); |
|
188 | + $groups = array(); |
|
189 | 189 | |
190 | - //print_r($entries); |
|
190 | + //print_r($entries); |
|
191 | 191 | |
192 | - if (!isset($entries[0]) || !isset($entries[0]['memberof'])) { |
|
193 | - //ldap server doesnt contains information about user groups |
|
194 | - return $groups; |
|
195 | - } |
|
192 | + if (!isset($entries[0]) || !isset($entries[0]['memberof'])) { |
|
193 | + //ldap server doesnt contains information about user groups |
|
194 | + return $groups; |
|
195 | + } |
|
196 | 196 | |
197 | - foreach($entries[0]['memberof'] as $grps) { |
|
198 | - $groups[] = $grps; |
|
199 | - } |
|
197 | + foreach($entries[0]['memberof'] as $grps) { |
|
198 | + $groups[] = $grps; |
|
199 | + } |
|
200 | 200 | |
201 | - /* |
|
201 | + /* |
|
202 | 202 | * isMemberOf: cn=Dynamic Home Directories,ou=groups,dc=example,dc=com |
203 | 203 | * isMemberOf: cn=bellevue,ou=groups,dc=example,dc=com |
204 | 204 | * isMemberOf: cn=shadow entries,ou=groups,dc=example,dc=com |
205 | 205 | * isMemberOf: cn=persons,ou=groups,dc=example,dc=com |
206 | 206 | */ |
207 | 207 | |
208 | - return $groups; |
|
209 | - } |
|
208 | + return $groups; |
|
209 | + } |
|
210 | 210 | |
211 | - public function listAllAttributesOfUser (string $user) : array { |
|
212 | - $filter = "(uid=" . $user . ")"; |
|
211 | + public function listAllAttributesOfUser (string $user) : array { |
|
212 | + $filter = "(uid=" . $user . ")"; |
|
213 | 213 | |
214 | - $result = ldap_search($this->conn, $this->dn, $filter/*, $attr*/) or exit("Unable to search LDAP server"); |
|
214 | + $result = ldap_search($this->conn, $this->dn, $filter/*, $attr*/) or exit("Unable to search LDAP server"); |
|
215 | 215 | |
216 | - $entries = ldap_get_entries($this->conn, $result); |
|
216 | + $entries = ldap_get_entries($this->conn, $result); |
|
217 | 217 | |
218 | - if (count($entries) == 0) { |
|
219 | - //no user found in ldap server |
|
218 | + if (count($entries) == 0) { |
|
219 | + //no user found in ldap server |
|
220 | 220 | |
221 | - //throw new IllegalStateException("user (uid=" . $user . ") not found in ldap server."); |
|
221 | + //throw new IllegalStateException("user (uid=" . $user . ") not found in ldap server."); |
|
222 | 222 | |
223 | - return array(); |
|
224 | - } |
|
223 | + return array(); |
|
224 | + } |
|
225 | 225 | |
226 | - return $entries[0]; |
|
227 | - } |
|
226 | + return $entries[0]; |
|
227 | + } |
|
228 | 228 | |
229 | - public function unbind () { |
|
230 | - //disconnect from ldap server |
|
231 | - ldap_unbind($this->conn); |
|
232 | - } |
|
229 | + public function unbind () { |
|
230 | + //disconnect from ldap server |
|
231 | + ldap_unbind($this->conn); |
|
232 | + } |
|
233 | 233 | |
234 | - public function getConnection () { |
|
235 | - return $this->conn; |
|
236 | - } |
|
234 | + public function getConnection () { |
|
235 | + return $this->conn; |
|
236 | + } |
|
237 | 237 | |
238 | 238 | } |
239 | 239 |
@@ -35,91 +35,91 @@ |
||
35 | 35 | |
36 | 36 | class LDAPAuthentificator implements IAuthentificator { |
37 | 37 | |
38 | - public function __construct() { |
|
39 | - // |
|
40 | - } |
|
41 | - |
|
42 | - /** |
|
43 | - * check password of user and import user, if neccessary |
|
44 | - * |
|
45 | - * @param $username string name of user |
|
46 | - * @param $password string password of user |
|
47 | - * |
|
48 | - * @return userID or -1, if credentials are wrong |
|
49 | - */ |
|
50 | - public function checkPasswordAndImport(string $username, string $password): int { |
|
51 | - //https://samjlevy.com/php-ldap-login/ |
|
52 | - |
|
53 | - //Free test ldap server: https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/ |
|
54 | - |
|
55 | - //https://www.experts-exchange.com/questions/23969673/Using-PHP-with-LDAP-to-connect-to-Active-Directory-on-another-machine.html |
|
56 | - |
|
57 | - //http://www.devshed.com/c/a/php/using-php-with-ldap-part-1/3/ |
|
58 | - |
|
59 | - //check, if username contains a komma (because komma is not allowed here) |
|
60 | - if (strpos($username, ",") !== FALSE) { |
|
61 | - throw new IllegalArgumentException("',' is not allowed in username."); |
|
62 | - return -1; |
|
63 | - } |
|
64 | - |
|
65 | - $ldap_client = new LDAPClient(); |
|
66 | - |
|
67 | - //try to login user on ldap server |
|
68 | - $res = $ldap_client->bind($username, $password); |
|
69 | - |
|
70 | - if (!$res) { |
|
71 | - //user doesnt exists or credentials are wrong |
|
72 | - return -1; |
|
73 | - } |
|
74 | - |
|
75 | - //TODO: set user groups |
|
76 | - |
|
77 | - //get attributes of user |
|
78 | - $attributes = $ldap_client->listAllAttributesOfUser($username); |
|
79 | - |
|
80 | - $mail = ""; |
|
81 | - |
|
82 | - //get mail of user |
|
83 | - if (isset($attributes['mail'])) { |
|
84 | - //get first mail |
|
85 | - $mail = $attributes['mail'][0]; |
|
86 | - } else { |
|
87 | - //generate random local mail |
|
88 | - $mail = md5(PHPUtils::randomString(10) . time()) . "@local"; |
|
89 | - } |
|
90 | - |
|
91 | - $common_name = ""; |
|
92 | - |
|
93 | - if (isset($attributes['cn'])) { |
|
94 | - $common_name = $attributes['cn'][0]; |
|
95 | - } else { |
|
96 | - $common_name = $username; |
|
97 | - } |
|
98 | - |
|
99 | - //get surname |
|
100 | - $surname = ""; |
|
101 | - |
|
102 | - if (isset($attributes['sn'])) { |
|
103 | - $surname = $attributes['sn'][0]; |
|
104 | - } |
|
105 | - |
|
106 | - //unbind |
|
107 | - $ldap_client->unbind(); |
|
108 | - |
|
109 | - //check, if we have to import user |
|
110 | - if (!User::existsUsername($username)) { |
|
111 | - //generate random password |
|
112 | - $password = md5(PHPUtils::randomString(16) . time()); |
|
113 | - |
|
114 | - //import user and create user in database |
|
115 | - $res = User::create($username, $password, $mail, PHPUtils::getClientIP(), 2, "none", 1, "Plugin\\LDAPLogin\\LDAPAuthentificator"); |
|
116 | - |
|
117 | - return $res['userID']; |
|
118 | - } else { |
|
119 | - //return userID |
|
120 | - return User::getIDByUsernameFromDB($username); |
|
121 | - } |
|
122 | - } |
|
38 | + public function __construct() { |
|
39 | + // |
|
40 | + } |
|
41 | + |
|
42 | + /** |
|
43 | + * check password of user and import user, if neccessary |
|
44 | + * |
|
45 | + * @param $username string name of user |
|
46 | + * @param $password string password of user |
|
47 | + * |
|
48 | + * @return userID or -1, if credentials are wrong |
|
49 | + */ |
|
50 | + public function checkPasswordAndImport(string $username, string $password): int { |
|
51 | + //https://samjlevy.com/php-ldap-login/ |
|
52 | + |
|
53 | + //Free test ldap server: https://www.forumsys.com/tutorials/integration-how-to/ldap/online-ldap-test-server/ |
|
54 | + |
|
55 | + //https://www.experts-exchange.com/questions/23969673/Using-PHP-with-LDAP-to-connect-to-Active-Directory-on-another-machine.html |
|
56 | + |
|
57 | + //http://www.devshed.com/c/a/php/using-php-with-ldap-part-1/3/ |
|
58 | + |
|
59 | + //check, if username contains a komma (because komma is not allowed here) |
|
60 | + if (strpos($username, ",") !== FALSE) { |
|
61 | + throw new IllegalArgumentException("',' is not allowed in username."); |
|
62 | + return -1; |
|
63 | + } |
|
64 | + |
|
65 | + $ldap_client = new LDAPClient(); |
|
66 | + |
|
67 | + //try to login user on ldap server |
|
68 | + $res = $ldap_client->bind($username, $password); |
|
69 | + |
|
70 | + if (!$res) { |
|
71 | + //user doesnt exists or credentials are wrong |
|
72 | + return -1; |
|
73 | + } |
|
74 | + |
|
75 | + //TODO: set user groups |
|
76 | + |
|
77 | + //get attributes of user |
|
78 | + $attributes = $ldap_client->listAllAttributesOfUser($username); |
|
79 | + |
|
80 | + $mail = ""; |
|
81 | + |
|
82 | + //get mail of user |
|
83 | + if (isset($attributes['mail'])) { |
|
84 | + //get first mail |
|
85 | + $mail = $attributes['mail'][0]; |
|
86 | + } else { |
|
87 | + //generate random local mail |
|
88 | + $mail = md5(PHPUtils::randomString(10) . time()) . "@local"; |
|
89 | + } |
|
90 | + |
|
91 | + $common_name = ""; |
|
92 | + |
|
93 | + if (isset($attributes['cn'])) { |
|
94 | + $common_name = $attributes['cn'][0]; |
|
95 | + } else { |
|
96 | + $common_name = $username; |
|
97 | + } |
|
98 | + |
|
99 | + //get surname |
|
100 | + $surname = ""; |
|
101 | + |
|
102 | + if (isset($attributes['sn'])) { |
|
103 | + $surname = $attributes['sn'][0]; |
|
104 | + } |
|
105 | + |
|
106 | + //unbind |
|
107 | + $ldap_client->unbind(); |
|
108 | + |
|
109 | + //check, if we have to import user |
|
110 | + if (!User::existsUsername($username)) { |
|
111 | + //generate random password |
|
112 | + $password = md5(PHPUtils::randomString(16) . time()); |
|
113 | + |
|
114 | + //import user and create user in database |
|
115 | + $res = User::create($username, $password, $mail, PHPUtils::getClientIP(), 2, "none", 1, "Plugin\\LDAPLogin\\LDAPAuthentificator"); |
|
116 | + |
|
117 | + return $res['userID']; |
|
118 | + } else { |
|
119 | + //return userID |
|
120 | + return User::getIDByUsernameFromDB($username); |
|
121 | + } |
|
122 | + } |
|
123 | 123 | } |
124 | 124 | |
125 | 125 | ?> |
@@ -27,59 +27,59 @@ |
||
27 | 27 | |
28 | 28 | class ApiAuth { |
29 | 29 | |
30 | - protected static $instance = null; |
|
31 | - |
|
32 | - protected $oauth = null; |
|
33 | - protected $is_authentificated = false;//flag, if user is authentificated |
|
34 | - |
|
35 | - /** |
|
36 | - * ApiAuth constructor. |
|
37 | - */ |
|
38 | - public function __construct() { |
|
39 | - // |
|
40 | - } |
|
41 | - |
|
42 | - public function init () { |
|
43 | - //check for secret token |
|
44 | - if (isset($_REQUEST['access_token'])) { |
|
45 | - $access_token = Validator_String::get($_REQUEST['access_token']); |
|
46 | - |
|
47 | - //try to verify access token |
|
48 | - $oauth = new ApiOAuth(); |
|
49 | - if (!$oauth->load($access_token)) { |
|
50 | - //api token isn't valide |
|
51 | - return; |
|
52 | - } |
|
53 | - |
|
54 | - //user is authentificated |
|
55 | - $this->is_authentificated = true; |
|
56 | - $this->oauth = $oauth; |
|
57 | - |
|
58 | - //set userID and load user |
|
59 | - User::current()->load($oauth->getUserID()); |
|
60 | - } |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * check, if user is authentificated |
|
65 | - * |
|
66 | - * @return bool |
|
67 | - */ |
|
68 | - public function isAuthentificated () : bool { |
|
69 | - return $this->is_authentificated; |
|
70 | - } |
|
71 | - |
|
72 | - public function getOAuth () : OAuth { |
|
73 | - return $this->oauth; |
|
74 | - } |
|
75 | - |
|
76 | - public static function &getInstance () : ApiAuth { |
|
77 | - if (self::$instance == null) { |
|
78 | - self::$instance = new ApiAuth(); |
|
79 | - } |
|
80 | - |
|
81 | - return self::$instance; |
|
82 | - } |
|
30 | + protected static $instance = null; |
|
31 | + |
|
32 | + protected $oauth = null; |
|
33 | + protected $is_authentificated = false;//flag, if user is authentificated |
|
34 | + |
|
35 | + /** |
|
36 | + * ApiAuth constructor. |
|
37 | + */ |
|
38 | + public function __construct() { |
|
39 | + // |
|
40 | + } |
|
41 | + |
|
42 | + public function init () { |
|
43 | + //check for secret token |
|
44 | + if (isset($_REQUEST['access_token'])) { |
|
45 | + $access_token = Validator_String::get($_REQUEST['access_token']); |
|
46 | + |
|
47 | + //try to verify access token |
|
48 | + $oauth = new ApiOAuth(); |
|
49 | + if (!$oauth->load($access_token)) { |
|
50 | + //api token isn't valide |
|
51 | + return; |
|
52 | + } |
|
53 | + |
|
54 | + //user is authentificated |
|
55 | + $this->is_authentificated = true; |
|
56 | + $this->oauth = $oauth; |
|
57 | + |
|
58 | + //set userID and load user |
|
59 | + User::current()->load($oauth->getUserID()); |
|
60 | + } |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * check, if user is authentificated |
|
65 | + * |
|
66 | + * @return bool |
|
67 | + */ |
|
68 | + public function isAuthentificated () : bool { |
|
69 | + return $this->is_authentificated; |
|
70 | + } |
|
71 | + |
|
72 | + public function getOAuth () : OAuth { |
|
73 | + return $this->oauth; |
|
74 | + } |
|
75 | + |
|
76 | + public static function &getInstance () : ApiAuth { |
|
77 | + if (self::$instance == null) { |
|
78 | + self::$instance = new ApiAuth(); |
|
79 | + } |
|
80 | + |
|
81 | + return self::$instance; |
|
82 | + } |
|
83 | 83 | |
84 | 84 | } |
85 | 85 |
@@ -27,187 +27,187 @@ |
||
27 | 27 | |
28 | 28 | class JSBuilder { |
29 | 29 | |
30 | - protected $content = ""; |
|
30 | + protected $content = ""; |
|
31 | 31 | |
32 | - protected static $benchmark = array(); |
|
32 | + protected static $benchmark = array(); |
|
33 | 33 | |
34 | - public function __construct() { |
|
35 | - // |
|
36 | - } |
|
34 | + public function __construct() { |
|
35 | + // |
|
36 | + } |
|
37 | 37 | |
38 | - public function generateJS (string $style_name, string $media = "ALL", string $position = "header") { |
|
39 | - if (ACTIVATE_BENCHMARK) { |
|
40 | - $start_time = microtime(true); |
|
41 | - } |
|
38 | + public function generateJS (string $style_name, string $media = "ALL", string $position = "header") { |
|
39 | + if (ACTIVATE_BENCHMARK) { |
|
40 | + $start_time = microtime(true); |
|
41 | + } |
|
42 | 42 | |
43 | - //validate values |
|
44 | - $style_name = Validator_Filename::get($style_name); |
|
45 | - $media = strtoupper(Validator_Filename::get($media)); |
|
46 | - $position = strtoupper(Validator_Filename::get($position)); |
|
43 | + //validate values |
|
44 | + $style_name = Validator_Filename::get($style_name); |
|
45 | + $media = strtoupper(Validator_Filename::get($media)); |
|
46 | + $position = strtoupper(Validator_Filename::get($position)); |
|
47 | 47 | |
48 | - $suffix = ""; |
|
48 | + $suffix = ""; |
|
49 | 49 | |
50 | - if ($position !== "HEADER") { |
|
51 | - $suffix = "_" . strtolower($position); |
|
52 | - } |
|
50 | + if ($position !== "HEADER") { |
|
51 | + $suffix = "_" . strtolower($position); |
|
52 | + } |
|
53 | 53 | |
54 | - $js_files = array(); |
|
54 | + $js_files = array(); |
|
55 | 55 | |
56 | - //get css files from style.json |
|
57 | - if (file_exists(STYLE_PATH . $style_name . "/style.json")) { |
|
58 | - $json = json_decode(file_get_contents(STYLE_PATH . $style_name . "/style.json"), true); |
|
56 | + //get css files from style.json |
|
57 | + if (file_exists(STYLE_PATH . $style_name . "/style.json")) { |
|
58 | + $json = json_decode(file_get_contents(STYLE_PATH . $style_name . "/style.json"), true); |
|
59 | 59 | |
60 | - if (isset($json['js' . $suffix]) && is_array($json['js' . $suffix])) { |
|
61 | - foreach ($json['js' . $suffix] as $js_file) { |
|
62 | - $full_path = STYLE_PATH . $style_name . "/" . $js_file; |
|
63 | - $js_files[] = $full_path; |
|
64 | - } |
|
65 | - } |
|
66 | - } |
|
60 | + if (isset($json['js' . $suffix]) && is_array($json['js' . $suffix])) { |
|
61 | + foreach ($json['js' . $suffix] as $js_file) { |
|
62 | + $full_path = STYLE_PATH . $style_name . "/" . $js_file; |
|
63 | + $js_files[] = $full_path; |
|
64 | + } |
|
65 | + } |
|
66 | + } |
|
67 | 67 | |
68 | - //load js files from database |
|
69 | - $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}js_files` WHERE (`style` = :style OR `style` = 'ALL') AND (`media` = :media OR `media` = 'ALL') AND `position` = :position AND `activated` = '1'; ", array( |
|
70 | - 'style' => $style_name, |
|
71 | - 'media' => $media, |
|
72 | - 'position' => $position |
|
73 | - )); |
|
68 | + //load js files from database |
|
69 | + $rows = Database::getInstance()->listRows("SELECT * FROM `{praefix}js_files` WHERE (`style` = :style OR `style` = 'ALL') AND (`media` = :media OR `media` = 'ALL') AND `position` = :position AND `activated` = '1'; ", array( |
|
70 | + 'style' => $style_name, |
|
71 | + 'media' => $media, |
|
72 | + 'position' => $position |
|
73 | + )); |
|
74 | 74 | |
75 | - foreach ($rows as $row) { |
|
76 | - $js_files[] = $row['js_file']; |
|
77 | - } |
|
75 | + foreach ($rows as $row) { |
|
76 | + $js_files[] = $row['js_file']; |
|
77 | + } |
|
78 | 78 | |
79 | - $buffer = ""; |
|
79 | + $buffer = ""; |
|
80 | 80 | |
81 | - foreach ($js_files as $js_file) { |
|
82 | - //first check, if file exists |
|
83 | - if (!file_exists($js_file)) { |
|
84 | - if (DEBUG_MODE) { |
|
85 | - echo "Coulnd't found javascript file (style: " . $style_name . "): " . $js_file; |
|
81 | + foreach ($js_files as $js_file) { |
|
82 | + //first check, if file exists |
|
83 | + if (!file_exists($js_file)) { |
|
84 | + if (DEBUG_MODE) { |
|
85 | + echo "Coulnd't found javascript file (style: " . $style_name . "): " . $js_file; |
|
86 | 86 | |
87 | - ob_end_flush(); |
|
88 | - exit; |
|
89 | - } else { |
|
90 | - continue; |
|
91 | - } |
|
92 | - } |
|
87 | + ob_end_flush(); |
|
88 | + exit; |
|
89 | + } else { |
|
90 | + continue; |
|
91 | + } |
|
92 | + } |
|
93 | 93 | |
94 | - //add file content to buffer |
|
95 | - $buffer .= file_get_contents($js_file) . "\n"; |
|
96 | - } |
|
94 | + //add file content to buffer |
|
95 | + $buffer .= file_get_contents($js_file) . "\n"; |
|
96 | + } |
|
97 | 97 | |
98 | - //$code = preg_replace("/\s\s+/", " ", $code); |
|
98 | + //$code = preg_replace("/\s\s+/", " ", $code); |
|
99 | 99 | |
100 | - //https://github.com/matthiasmullie/minify |
|
100 | + //https://github.com/matthiasmullie/minify |
|
101 | 101 | |
102 | - //https://github.com/tchwork/jsqueeze |
|
102 | + //https://github.com/tchwork/jsqueeze |
|
103 | 103 | |
104 | - //https://ourcodeworld.com/articles/read/350/how-to-minify-javascript-and-css-using-php |
|
104 | + //https://ourcodeworld.com/articles/read/350/how-to-minify-javascript-and-css-using-php |
|
105 | 105 | |
106 | - //compress js: https://github.com/tedious/JShrink |
|
106 | + //compress js: https://github.com/tedious/JShrink |
|
107 | 107 | |
108 | - //remove comments |
|
109 | - //$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); |
|
108 | + //remove comments |
|
109 | + //$buffer = preg_replace('!/\*[^*]*\*+([^/][^*]*\*+)*/!', '', $buffer); |
|
110 | 110 | |
111 | - // remove space after colons |
|
112 | - //$buffer = str_replace(': ', ':', $buffer); |
|
111 | + // remove space after colons |
|
112 | + //$buffer = str_replace(': ', ':', $buffer); |
|
113 | 113 | |
114 | - //remove whitespace |
|
115 | - //$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); |
|
114 | + //remove whitespace |
|
115 | + //$buffer = str_replace(array("\r\n", "\r", "\n", "\t", ' ', ' ', ' '), '', $buffer); |
|
116 | 116 | |
117 | - //replace template variables |
|
118 | - $buffer = str_replace("{STYLE_PATH}", DomainUtils::getBaseURL() . "/styles/" . $style_name . "/", $buffer); |
|
117 | + //replace template variables |
|
118 | + $buffer = str_replace("{STYLE_PATH}", DomainUtils::getBaseURL() . "/styles/" . $style_name . "/", $buffer); |
|
119 | 119 | |
120 | - //compress code |
|
121 | - $jz = new JSqueeze(); |
|
122 | - $buffer = $jz->squeeze( |
|
123 | - $buffer, |
|
124 | - true, // $singleLine |
|
125 | - false, // $keepImportantComments |
|
126 | - false // $specialVarRx |
|
127 | - ); |
|
120 | + //compress code |
|
121 | + $jz = new JSqueeze(); |
|
122 | + $buffer = $jz->squeeze( |
|
123 | + $buffer, |
|
124 | + true, // $singleLine |
|
125 | + false, // $keepImportantComments |
|
126 | + false // $specialVarRx |
|
127 | + ); |
|
128 | 128 | |
129 | - //set flag, if buffer is empty |
|
130 | - $empty_flag = empty($buffer); |
|
129 | + //set flag, if buffer is empty |
|
130 | + $empty_flag = empty($buffer); |
|
131 | 131 | |
132 | - //add comment so md5 hash will change |
|
133 | - $buffer = "/* generated by jsbuilder on " . gmdate("D, d M Y H:i:s", time()) . ", empty flag: " . ($empty_flag ? "true" : "false") . " */" . $buffer; |
|
132 | + //add comment so md5 hash will change |
|
133 | + $buffer = "/* generated by jsbuilder on " . gmdate("D, d M Y H:i:s", time()) . ", empty flag: " . ($empty_flag ? "true" : "false") . " */" . $buffer; |
|
134 | 134 | |
135 | - //create cache directory, if neccessary |
|
136 | - if (!file_exists(CACHE_PATH . "jsbuilder/")) { |
|
137 | - mkdir(CACHE_PATH . "jsbuilder/"); |
|
138 | - } |
|
135 | + //create cache directory, if neccessary |
|
136 | + if (!file_exists(CACHE_PATH . "jsbuilder/")) { |
|
137 | + mkdir(CACHE_PATH . "jsbuilder/"); |
|
138 | + } |
|
139 | 139 | |
140 | - //cache buffer |
|
141 | - file_put_contents($this->getCachePath($style_name, $media, $position), $buffer); |
|
140 | + //cache buffer |
|
141 | + file_put_contents($this->getCachePath($style_name, $media, $position), $buffer); |
|
142 | 142 | |
143 | - Cache::put("jsbuilder", "hash_" . $style_name . "_" . $media . "_" . strtolower($position), md5($buffer)); |
|
144 | - Cache::put("jsbuilder", "meta_" . $style_name . "_" . $media . "_" . strtolower($position), array('empty_flag' => $empty_flag)); |
|
143 | + Cache::put("jsbuilder", "hash_" . $style_name . "_" . $media . "_" . strtolower($position), md5($buffer)); |
|
144 | + Cache::put("jsbuilder", "meta_" . $style_name . "_" . $media . "_" . strtolower($position), array('empty_flag' => $empty_flag)); |
|
145 | 145 | |
146 | - $this->content = $buffer; |
|
146 | + $this->content = $buffer; |
|
147 | 147 | |
148 | - if (ACTIVATE_BENCHMARK) { |
|
149 | - $end_time = microtime(true); |
|
150 | - $exec_time = $end_time - $start_time; |
|
148 | + if (ACTIVATE_BENCHMARK) { |
|
149 | + $end_time = microtime(true); |
|
150 | + $exec_time = $end_time - $start_time; |
|
151 | 151 | |
152 | - self::$benchmark[$style_name . "_" . $media] = $exec_time; |
|
153 | - } |
|
152 | + self::$benchmark[$style_name . "_" . $media] = $exec_time; |
|
153 | + } |
|
154 | 154 | |
155 | - return $buffer; |
|
156 | - } |
|
155 | + return $buffer; |
|
156 | + } |
|
157 | 157 | |
158 | - public function getCachePath (string $style, string $media = "ALL", string $position = "header") : string { |
|
159 | - $position = strtolower($position); |
|
158 | + public function getCachePath (string $style, string $media = "ALL", string $position = "header") : string { |
|
159 | + $position = strtolower($position); |
|
160 | 160 | |
161 | - $md5_filename = md5("js_" . $style . "_" . $media . "_" . $position); |
|
162 | - $js_cache_path = CACHE_PATH . "jsbuilder/" . $md5_filename . ".js"; |
|
161 | + $md5_filename = md5("js_" . $style . "_" . $media . "_" . $position); |
|
162 | + $js_cache_path = CACHE_PATH . "jsbuilder/" . $md5_filename . ".js"; |
|
163 | 163 | |
164 | - return $js_cache_path; |
|
165 | - } |
|
164 | + return $js_cache_path; |
|
165 | + } |
|
166 | 166 | |
167 | - public function existsCache (string $style, string $media = "ALL", string $position = "header") : bool { |
|
168 | - return file_exists($this->getCachePath($style, $media, $position)); |
|
169 | - } |
|
167 | + public function existsCache (string $style, string $media = "ALL", string $position = "header") : bool { |
|
168 | + return file_exists($this->getCachePath($style, $media, $position)); |
|
169 | + } |
|
170 | 170 | |
171 | - public function getHash (string $style, string $media = "ALL", string $position = "header") : string { |
|
172 | - if (!$this->existsCache($style, $media, $position)) { |
|
173 | - //generate cached js file |
|
174 | - $this->generateJS($style, $media, $position); |
|
175 | - } |
|
171 | + public function getHash (string $style, string $media = "ALL", string $position = "header") : string { |
|
172 | + if (!$this->existsCache($style, $media, $position)) { |
|
173 | + //generate cached js file |
|
174 | + $this->generateJS($style, $media, $position); |
|
175 | + } |
|
176 | 176 | |
177 | - if (!Cache::contains("jsbuilder", "hash_" . $style . "_" . $media . "_" . $position)) { |
|
178 | - throw new IllegalStateException("cached js file 'hash_" . $style . "_" . $media . "_" . $position . "' doesnt exists."); |
|
179 | - } |
|
177 | + if (!Cache::contains("jsbuilder", "hash_" . $style . "_" . $media . "_" . $position)) { |
|
178 | + throw new IllegalStateException("cached js file 'hash_" . $style . "_" . $media . "_" . $position . "' doesnt exists."); |
|
179 | + } |
|
180 | 180 | |
181 | - return Cache::get("jsbuilder", "hash_" . $style . "_" . $media . "_" . $position); |
|
182 | - } |
|
181 | + return Cache::get("jsbuilder", "hash_" . $style . "_" . $media . "_" . $position); |
|
182 | + } |
|
183 | 183 | |
184 | - public function isEmpty (string $style, string $media = "ALL", string $position = "header") : bool { |
|
185 | - if (!Cache::contains("jsbuilder", "meta_" . $style . "_" . $media . "_" . $position)) { |
|
186 | - throw new IllegalStateException("cached js file 'meta_" . $style . "_" . $media . "_" . $position . "' doesnt exists."); |
|
187 | - } |
|
184 | + public function isEmpty (string $style, string $media = "ALL", string $position = "header") : bool { |
|
185 | + if (!Cache::contains("jsbuilder", "meta_" . $style . "_" . $media . "_" . $position)) { |
|
186 | + throw new IllegalStateException("cached js file 'meta_" . $style . "_" . $media . "_" . $position . "' doesnt exists."); |
|
187 | + } |
|
188 | 188 | |
189 | - $array = Cache::get("jsbuilder", "meta_" . $style . "_" . $media . "_" . $position); |
|
189 | + $array = Cache::get("jsbuilder", "meta_" . $style . "_" . $media . "_" . $position); |
|
190 | 190 | |
191 | - return $array['empty_flag'] == true; |
|
192 | - } |
|
191 | + return $array['empty_flag'] == true; |
|
192 | + } |
|
193 | 193 | |
194 | - public function load (string $style, string $media = "ALL", string $position = "header") { |
|
195 | - $cache_path = $this->getCachePath($style, $media, $position); |
|
194 | + public function load (string $style, string $media = "ALL", string $position = "header") { |
|
195 | + $cache_path = $this->getCachePath($style, $media, $position); |
|
196 | 196 | |
197 | - if (!$this->existsCache($style, $media, $position)) { |
|
198 | - $this->generateJS($style, $media, $position); |
|
199 | - } else { |
|
200 | - $this->content = file_get_contents($cache_path); |
|
201 | - } |
|
202 | - } |
|
197 | + if (!$this->existsCache($style, $media, $position)) { |
|
198 | + $this->generateJS($style, $media, $position); |
|
199 | + } else { |
|
200 | + $this->content = file_get_contents($cache_path); |
|
201 | + } |
|
202 | + } |
|
203 | 203 | |
204 | - public function getBuffer () : string { |
|
205 | - return $this->content; |
|
206 | - } |
|
204 | + public function getBuffer () : string { |
|
205 | + return $this->content; |
|
206 | + } |
|
207 | 207 | |
208 | - public static function listBenchmarks () { |
|
209 | - return self::$benchmark; |
|
210 | - } |
|
208 | + public static function listBenchmarks () { |
|
209 | + return self::$benchmark; |
|
210 | + } |
|
211 | 211 | |
212 | 212 | } |
213 | 213 |