-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathUsersClass.inc
242 lines (212 loc) · 7.09 KB
/
UsersClass.inc
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
230
231
232
233
234
235
236
237
238
239
240
241
242
<?php
/**
* @file
* This is a collection of functions to handle getting
* user info from the LDAP server and turning it into
* a useful form
*/
class UsersClass {
public $givenname = NULL;
public $familyname = NULL;
public $mail = NULL;
public $cn = NULL;
public $uid = NULL;
public $fullname = NULL;
public $groupmembership = NULL;
public $telephonenumber = NULL;
/**
* Given a user this populates the object with details
* from the LDAP entry
* @param type $user
*/
public function __construct($user) {
$ldap_host = variable_get('scholar_ldap_host', '');
$ldap_port = variable_get('scholar_ldap_port', '389');
$ds = ldap_connect($ldap_host, $ldap_port) or die("Could not connect to ldap host!");
$basedn = variable_get('scholar_ldap_basedn', '');
$filter = "(&(objectClass=person)(uid=*)(cn=$user))";
$sr = ldap_search($ds, $basedn, $filter, array('uid', 'cn', 'fullname', 'mail', 'groupmembership', 'givenname', 'sn', 'telephonenumber'));
$list = ldap_get_entries($ds, $sr);
$this->uid = $list[0]['uid'][0];
$this->cn = $list[0]['cn'][0];
$this->fullname = $list[0]['fullname'][0];
$this->mail = $list[0]['mail'][0];
$this->groupmembership = $list[0]['groupmembership'];
$this->givenname = $list[0]['givenname'][0];
$this->familyname = $list[0]['sn'][0];
$this->telephonenumber = $list[0]['telephonenumber']['0'];
}
/**
* This builds the MADS file from the LDAP data
* @return DOMDocument
*/
public function ldap_to_mads() {
$dom = new DOMDocument();
$mads = $dom->createElementNS("http://www.loc.gov/mads/v2", 'mads');
$dom->appendChild($mads);
$mads->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$mads->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:xlink', 'http://www.w3.org/1999/xlink');
$mads->setAttributeNS('http://www.w3.org/2000/xmlns/', 'xmlns:mads', 'http://www.loc.gov/mads/v2');
$mads->setAttribute('xsi:schemaLocation', 'http://www.loc.gov/mads/v2 http://www.loc.gov/standards/mads/mads.xsd');
$authority = $dom->createElement("authority");
$mads->appendChild($authority);
$name = $dom->createElement("name");
$name->setAttribute("type", "personal");
$authority->appendChild($name);
$given = $dom->createElement("namePart", $this->givenname);
$given->setAttribute("type", "given");
$name->appendChild($given);
$family = $dom->createElement("namePart", $this->familyname);
$family->setAttribute("type", "family");
$name->appendChild($family);
// $filename = drupal_get_path('module', 'scholar') . '/inclusionList.txt';
// $inclusionlist = file_get_contents($filename);
// $inclusionlist = fix_encoding($inclusionlist);
// $inclusionlist = explode(",", $inclusionlist);
$u1 = $dom->createElement("identifier", $this->cn);
$u1->setAttribute("type", "u1");
$mads->appendChild($u1);
$affiliation = $dom->createElement("affiliation");
$mads->appendChild($affiliation);
if (isset($this->groupmembership)) {
foreach ($this->groupmembership as $group) {
$dept = strtok($group, ',');
$dept = ltrim($dept, 'cn=');
$dept = islandora_scholar_convert_dept($dept);
if (strlen($dept) > 1) {
$organisation = $dom->createElement("organization", $dept);
$affiliation->appendChild($organisation);
}
}
}
else {
$organisation = $dom->createElement("organization");
$affiliation->appendChild($organisation);
}
if (isset($this->telephonenumber)) {
$telephonenumber = $dom->createElement('phone', $this->telephonenumber);
$affiliation->appendChild($telephonenumber);
}
else {
$telephonenumber = $dom->createElement('phone');
$affiliation->appendChild($telephonenumber);
}
$email = $dom->createElement("email", str_replace('groupwise.', '', $this->mail));
$affiliation->appendChild($email);
$position = $dom->createElement('position');
$affiliation->appendChild($position);
$date_valid = $dom->createElement('dateValid');
$affiliation->appendChild($date_valid);
// $xml = $dom->saveXML();
return $dom;
}
/**
* Check whether the user already exists as an object in
* the repo
* @return type
*/
public function check_user() {
module_load_include('inc', 'fedora_repository', 'api/fedora_utils');
$user = $this->cn;
$query_string = 'select $object $title from <#ri>
where ($object <dc:title>$title
and $object <fedora-model:hasModel> <info:fedora/ir:researcherCModel>
and $object <fedora-model:state> <info:fedora/fedora-system:def/model#Active>
and $object <mulgara:is> <info:fedora/ir:' . strtolower($user) . '>)
order by $title';
//var_dump($query_string);
$query_string = htmlentities(urlencode($query_string));
$content = '';
$url = variable_get('fedora_repository_url', 'http://localhost:8080/fedora/risearch');
$url .= "?type=tuples&flush=TRUE&format=Sparql&limit=1000&offset=0&lang=itql&stream=on&query=" . $query_string;
$content .= do_curl($url);
if ($content == NULL) {
return FALSE;
}
$result = new DOMDocument();
$result->loadXML($content);
$entry = $result->getElementsByTagName('title')->item(0)->nodeValue;
$text = $result->saveXML();
// var_dump($content);
if (isset($entry)) {
return TRUE;
}
else {
return FALSE;
}
}
}
function islandora_scholar_convert_dept($dept) {
$find = array(
'Robertson Library',
'Anatomy Physiology',
'Biology',
'Business',
'Chemistry',
'Classics',
'Companion Animals',
'Computer Science and Info Technology',
'Economics',
'Education',
'Engineering',
'English',
'Health Management',
'History',
'Home Economics',
'Island Studies',
'Mathematics and Statistics',
'Modern Languages',
'Music',
'Nursing',
'Path Micro',
'Philosophy',
'Physics',
'Political Studies',
'Psychology',
'Religious Studies',
'Soc Anth',
'Womens Studies',
'Blood',
'School_of_School of Business',
);
$replace = array(
'Robertson Library',
'Biomedical Sciences',
'Biology',
'School of Business', //Is this correct?
'Chemistry',
'Classics',
'Companion Animals',
'Computer Science and Information Technology',
'Economics',
'Education',
'Engineering',
'English',
'Health Management',
'History',
'Applied Human Sciences', //Change
'Island Studies',
'Mathematics and Statistics',
'Modern Languages',
'Music',
'Nursing',
'Pathology and Microbiology',
'Philosophy',
'Physics',
'Political Science',
'Psychology',
'Religious Studies',
'Sociology and Anthropology',
'Women\'s Studies',
'Blood',
'School_of_Business',
); //Not sure about this one
$dept = str_replace($find, $replace, $dept);
if (in_array($dept, $replace)) {
$new_dept = $dept;
}
else {
$new_dept = NULL;
}
return $new_dept;
}