This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
/
class.cmsmsAuthDriver.php
229 lines (211 loc) · 9.07 KB
/
class.cmsmsAuthDriver.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
<?php
/*
* Copyright 2007-2011 Jean-Christophe Ghio (jissey)
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
use Pydio\Auth\Core\AbstractAuthDriver;
use Pydio\Core\Services\AuthService;
use Pydio\Core\Utils\Utils;
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* Abstract representation of an access to FEU athentification module(CMS Made Simple).
* like auth.remote:
* In slave mode, the login dialog is not displayed in AJXP.
* If the user directly go to the main page, (s)he's redirected to the LOGIN_URL.
* The logout button link to LOGOUT_URL.
* The user will log in on FEU, and the remote script will call us, as
* ajxpPath/index.php?get_action=login_cmsms&uid=usernam&sessionid=sessionid in FEU loggedin table
* You must modify conf.php like this:
* "AUTH_DRIVER" => array(
"NAME" => "cmsms",
"OPTIONS" => array(
"SQL_DRIVER" => Array(
'driver' => 'mysql',
'host' => 'localhost',
'username' => 'root',
'password' => '',
'database' => 'your_cmsms_db'
),
"PREFIX_TABLE" => 'your_prefix',
"LOGIN_URL" => 'http://url of FEU login form',
"LOGOUT_URL" => 'http://url you want with the logout button ',
"SECRET" => '1234' //the common secret code between the two application (store in cmsms database on the other side)
)
),
* @package AjaXplorer_Plugins
* @subpackage Auth
*/
class cmsmsAuthDriver extends AbstractAuthDriver
{
public $sqlDriver;
public $driverName = "cmsms";
public $slaveMode;
public $secret;
public $secret_cmsms;
public $urls;
public $prefix;
public $groupid;
public function init($options)
{
parent::init($options);
$this->sqlDriver = $this->options["SQL_DRIVER"];
try {
dibi::connect($this->sqlDriver);
} catch (DibiException $e) {
echo get_class($e), ': ', $e->getMessage(), "\n";
exit(1);
}
$this->secret = $this->options["SECRET"];
$this->prefix = $this->options["PREFIX_TABLE"];
$this->urls = array($this->options["LOGIN_URL"], $this->options["LOGOUT_URL"]);
$this->slaveMode = true;
$res = dibi::query("SELECT sitepref_value FROM [".$this->prefix."siteprefs] WHERE sitepref_name = 'FEUajaxplorer_mapi_pref_ajxp_auth_group'");
$grp = $res->fetchSingle();
$this->groupid = $grp;
$res2 = dibi::query("SELECT sitepref_value FROM [".$this->prefix."siteprefs] WHERE sitepref_name = 'FEUajaxplorer_mapi_pref_ajxp_secret'");
$sec = $res2->fetchSingle();
$this->secret_cmsms = trim($sec);
}
public function listUsers($baseGroup = "/", $recursive = true)
{
$res = dibi::query("SELECT * FROM [".$this->prefix."module_feusers_users], [".$this->prefix."module_feusers_belongs] WHERE id = userid AND groupid=%s", $this->groupid);
$pairs = $res->fetchPairs('username', 'password');
return $pairs;
}
public function userExists($login)
{
$res = dibi::query("SELECT * FROM [".$this->prefix."module_feusers_users], [".$this->prefix."module_feusers_belongs] WHERE [username]=%s AND id = userid AND groupid=%s", $login, $this->groupid);
return($res->getRowCount());
}
public function userIsConnected($username, $sessionid)
{
$userid = $this->getUserId($username);
$res = dibi::query("SELECT * FROM [".$this->prefix."module_feusers_loggedin] WHERE [userid]=%s AND sessionid=%s", $userid, $sessionid);
return($res->getRowCount());
}
// Never call if used with the initial cmsms ajxp module.
// We don't check the password because we use sessionid and userid from FEU loggedin table.
public function checkPassword($login, $pass, $seed)
{
$userStoredPass = $this->getUserPass($login);
if(!$userStoredPass) return false;
if (md5($pass) === $userStoredPass) {
$loggedinData['sessionid']=session_id();
$loggedinData['lastused']=time();
$loggedinData['userid']=$this->getUserId($login);
dibi::query('INSERT INTO ['.$this->prefix.'module_feusers_loggedin]', $loggedinData);
}
return ($userStoredPass === md5($pass));
}
public function usersEditable()
{
return true;
}
public function passwordsEditable()
{
return true;
}
public function createUser($login, $passwd)
{
$users = $this->listUsers();
if(!is_array($users)) $users = array();
if(array_key_exists($login, $users)) return "exists";
$userData = array("username" => $login);
$userData["password"] = md5($passwd);
$userData["id"]=$this->getUserSeq()+1;
$userData["createdate"]=date("Y-m-d H:i:s");
dibi::query('INSERT INTO ['.$this->prefix.'module_feusers_users]', $userData);
$this->setUserSeq($userData["id"]);
$belongsData["userid"]=$userData["id"];
$belongsData["groupid"]=$this->groupid;
dibi::query('INSERT INTO ['.$this->prefix.'module_feusers_belongs]', $belongsData);
}
public function changePassword($login, $newPass)
{
$users = $this->listUsers();
if(!is_array($users) || !array_key_exists($login, $users)) return ;
$userData = array("username" => $login);
$userData["password"] = md5($newPass);
dibi::query("UPDATE [".$this->prefix."module_feusers_users] SET ", $userData, "WHERE [username]=%s", $login);
}
public function deleteUser($login)
{
$uid=$this->getUserId($login);
//suppress all references in CMSMS FEU tables
dibi::query("DELETE FROM [".$this->prefix."module_feusers_users] WHERE [username]=%s", $login);
dibi::query("DELETE FROM [".$this->prefix."module_feusers_belongs] WHERE [userid]=%s", $uid);
dibi::query("DELETE FROM [".$this->prefix."module_feusers_properties] WHERE [userid]=%s", $uid);
}
public function getUserPass($login)
{
$res = dibi::query("SELECT [password] FROM [".$this->prefix."module_feusers_users], [".$this->prefix."module_feusers_belongs] WHERE [username]=%s AND id = userid AND groupid= %s", $login, $this->groupid);
$pass = $res->fetchSingle();
return $pass;
}
public function getUserSeq()
{
$res = dibi::query("SELECT [id] FROM [".$this->prefix."module_feusers_users_seq]");
$seq = $res->fetchSingle();
return $seq;
}
public function setUserSeq($num)
{
$res = dibi::query("UPDATE [".$this->prefix."module_feusers_users_seq] SET [id]=%s", $num);
}
public function getUserId($login)
{
$res = dibi::query("SELECT id FROM [".$this->prefix."module_feusers_users], [".$this->prefix."module_feusers_belongs] WHERE [username]=%s AND id = userid AND groupid= %s", $login, $this->groupid);
$uid = $res->fetchSingle();
return $uid;
}
public function listUsersSerial()
{
return Utils::loadSerialFile($this->usersSerFile);
}
public function getLoginRedirect()
{
if ($this->slaveMode) {
if (isset($_SESSION["AJXP_USER"])) return false;
return $this->urls[0];
}
return false;
}
public function getLogoutRedirect()
{
if ($this->slaveMode) {
return $this->urls[1];
}
return false;
}
// call by CMS Made Simple with the followings arguments:
// username : username of the FEU user
// sessionid : num. of session in the loggedin table for matching the logging the user here.
public function login_cmsms()
{
$username=strip_tags($_GET['username']);
$sessionid=strip_tags($_GET['sessionid']);
//verifying the CIA secret code...
if($this->secret != $this->secret_cmsms) exit( "secret");//return header("Location: ".$this->getLoginRedirect());
//verifying that the user exists (in the right group)
if(!$this->userExists($username)) exit( "exists");//return header("Location: ".$this->getLoginRedirect());
//verifying that the user is connected
if(!$this->userIsConnected($username, $sessionid)) exit( "connect");//return header("Location: ".$this->getLoginRedirect());
//all tests passed...we connect the user
if($log=AuthService::logUser($username,"",true)==1) return header("Location: index.php");
}
}