-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.php
More file actions
162 lines (136 loc) · 4.92 KB
/
Copy pathlib.php
File metadata and controls
162 lines (136 loc) · 4.92 KB
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
<?php
class Cleaner {
// Days since last user activity
public static $DAYS = 0;
public static $DELETION_LOG_FILE = "local_cleanupusers_deletedUsers.js";
public static $LOG_FILE = "local_cleanupusers_log.txt";
public static $DATE_FORMAT = "d.m.Y H:i:s";
private $inactiveUsers = null;
public $usersToBeDeleted = array();
function __construct() {
$this->getInactiveUsers();
$this->getUsersToDelete();
//$this->printArray($this->usersToDelete);
}
public function printDeletedUsersJSON() {
global $CFG;
$fullpath = $CFG->dataroot . '/' . Cleaner::$DELETION_LOG_FILE;
try {
$filecontent = file_get_contents($fullpath);
} catch (Exception $e) {
// Datei nicht vorhanden
echo json_encode([]);
return;
}
echo $filecontent;
return;
}
private function addUserToLogFile($user) {
global $CFG;
$fullpath = $CFG->dataroot . '/' . Cleaner::$DELETION_LOG_FILE;
try {
$filecontent = file_get_contents($fullpath);
} catch (Exception $e) {
}
// Datei nicht vorhanden => anlegen
$json = null;
if (!$filecontent) {
echo "Lege Datei neu an...\n";
$json = json_encode(array($user->id => $user));
} else {
$obj = json_decode($filecontent);
$key = $user->id;
$obj->$key = $user;
$json = json_encode($obj);
}
file_put_contents($fullpath, $json);
}
public static function log($msg) {
$msg = date(Cleaner::$DATE_FORMAT) . ": " . $msg;
// Ausgabe
echo $msg;
global $CFG;
$fullpath = $CFG->dataroot . '/' . Cleaner::$LOG_FILE;
file_put_contents($fullpath, $msg, FILE_APPEND);
}
public static function userToString($user) {
return $user->firstname . " " .
$user->lastname . " (" .
$user->username . ")";
}
public function deleteUsers() {
global $CFG;
require_once $CFG->libdir . '/moodlelib.php';
$noUsers = count($this->usersToBeDeleted);
Cleaner::log($noUsers . " Nutzer werden gelöscht:\n");
$count = 1;
$undeleteableUsers = [];
foreach ($this->usersToBeDeleted as $userid => $user) {
try {
delete_user($user);
$user->deletedOn = time();
$this->addUserToLogFile($user);
$log = "[" . $count . "/" . $noUsers . "] Lösche User:\t" .
Cleaner::userToString($user) . "\n";
Cleaner::log($log);
$count++;
} catch (Exception $e) {
$undeleteableUsers[$userid] = $user;
$log = "[!] User " . Cleaner::userToString($user) . " konnte NICHT geloescht werden!\n";
Cleaner::log($log);
}
}
if(count($undeleteableUsers) > 0) {
$log = "[!] " . count($undeleteableUsers) . " konnten NICHT geloescht werden!";
}
}
private function checkUsersTUIDs($users) {
$ldap = ldap_connect("ldaps://ldap.hrz.tu-darmstadt.de:636") or die("Error connecting LDAP Server: " . ldap_error($ldap));
$bind = ldap_bind($ldap);
$basedn = array('OU=USER,O=TU', 'OU=stud,O=tu', 'OU=GUP,O=TU');
$attributes = array('cn', 'userId');
$filter = '';
if ($ldap) {
foreach ($users as $userid => $user) {
$filter = '(cn=' . $user->username . ')';
$search = ldap_search($ldap, $basedn, $filter, $attributes) or die("Error in search Query: " . ldap_error($ldap));
$result = ldap_get_entries($ldap, $search);
if (!isset($result) OR $result['count'] < 1) {
$users[$userid]->valid = 0;
} else {
$users[$userid]->valid = 1;
}
}
ldap_close($ldap);
} else {
$this->error("Can't connect with LDAP-Server");
}
return $users;
}
private function getInactiveUsers() {
$seconds = Cleaner::$DAYS * 24 * 60 * 60;
$time = time() - $seconds;
global $CFG, $DB;
$sql = "SELECT "
. "id,username,firstname,lastname,lastaccess "
. "FROM {user} "
. "WHERE auth = 'cas' AND deleted=0 AND lastaccess > 0 AND lastaccess < " . $time;
$this->inactiveUsers = $this->checkUsersTUIDs($DB->get_records_sql($sql));
}
private function getUsersToDelete() {
foreach ($this->inactiveUsers as $userid => $user) {
if ($user->valid === 0) {
$this->usersToBeDeleted[$user->id] = $user;
}
}
}
public function printArray($array) {
echo print_r($array, true);
}
public function error($msg) {
echo "[!!] " . $msg . "\n";
}
public function info($msg) {
echo "[*] " . $msg . "\n";
}
}