This repository was archived by the owner on Jun 27, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_davical_operations.php
346 lines (300 loc) · 9.17 KB
/
test_davical_operations.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<?php
/**
* \file test_davical_operations.php
* \brief Contains all the operations on DAViCal’s database.
*/
ini_set('display_errors', 1);
error_reporting(E_ALL);
// Flora NOTE: vous devez écrire dans le fichier de votre serveur virtuel la ligne
// php_value include_path /usr/share/davical/inc:/usr/share/awl/inc
require_once("/usr/share/davical/htdocs/always.php");
require_once("auth-functions.php");
require_once("DAVPrincipal.php");
require_once("classes/C_Secretary.php");
require_once("classes/C_Teacher.php");
require_once("classes/C_Administrator.php");
require_once("classes/C_Head.php");
require_once("liste_personnes.php");
// Créer un calendrier de nom, $calendarNameGiven pour l'utilisateur d'identifiant $username
function CreateCalendar($userName, $calendarNameGiven, $default_timezone = NULL)
{
global $session, $c;
if (!isset($c->default_collections))
{
$c->default_collections = array();
}
$principal = new Principal('username', $userName);
$userFullName = $principal->fullname;
$userReverseFullName = implode(' ', array_reverse(explode(' ', $principal->fullname))); // user fullname in reverse order
$sqlQuery = 'INSERT INTO collection (user_no, parent_container, dav_name, dav_etag, dav_displayname, is_calendar, is_addressbook, default_privileges, created, modified, resourcetypes) ';
$sqlQuery .= 'VALUES( :user_no, :parent_container, :collection_path, :dav_etag, :displayname, :is_calendar, :is_addressbook, :privileges::BIT(24), current_timestamp, current_timestamp, :resourcetypes );';
// foreach ($c->default_collections as $v) {}
if (FALSE)
{
/* if (!empty($v['name']))
{
$awlQuery = new AwlQuery('SELECT 1 FROM collection WHERE dav_name = :dav_name', array(':dav_name' => $principal->dav_name().$v['name'].'/'));
}
if (!$awlQuery->Exec())
{
$c->messages[] = i18n('There was an error reading from the database.');
return FALSE;
}
if ($awlQuery->rows() > 0)
{
$c->messages[] = i18n('Home ' . $calendarGivenName . ' already exists.');
return TRUE;
}
*/
}
else
{
$params[':user_no'] = $principal->user_no();
$params[':parent_container'] = $principal->dav_name();
$params[':dav_etag'] = '-1';
$params[':collection_path'] = $principal->dav_name() . $calendarNameGiven . '/';
// $params[':displayname'] = $userFullName . " " . $calendarNameGiven;
$params[':displayname'] = $calendarNameGiven;
$params[':resourcetypes'] = '<DAV::collection/><urn:ietf:params:xml:ns:caldav:calendar/>';
$params[':is_calendar'] = TRUE;
$params[':is_addressbook'] = FALSE;
$params[':privileges'] = NULL;
$awlQuery = new AwlQuery($sqlQuery, $params);
if ($awlQuery->Exec())
{
$c->messages[] = i18n('Calendar added.');
dbg_error_log("User", ":Write: Created user's calendar at '%s'", $params[':collection_path']);
$DB = new Database("davical_app", "davical");
$result = $DB->executeQuery("SELECT collection_id FROM collection ORDER BY created DESC;");
if ($result)
{
$result = pg_fetch_assoc($result);
return $result['collection_id'];
}
else
{
$DB->showError("ligne n° " . __LINE__ . " //fonction : " . __FUNCTION__);
return FALSE;
}
}
else
{
$c->messages[] = i18n('There was an error writing to the database.');
return FALSE;
}
}
return TRUE;
}
function getDAVPrincipalNoFromLogin($login)
{
$DB = new Database("davical_app", "davical");
$query = "SELECT user_no FROM dav_principal WHERE username = '" . pg_escape_string($login) . "';";
$result = $DB->executeQuery($query);
if (!$result)
{
$BDD->showError("ligne n° " . __LINE__ . " //fonction : " . __FUNCTION__);
return FALSE;
}
else
{
$result = pg_fetch_assoc($result);
return ((int) $result['user_no']);
}
}
function CreateUserAccount($userName, $fullName, $password, $email = NULL, $privileges = NULL)
{
$param['username'] = $userName;
$param['fullname'] = $fullName;
$param['displayname'] = $fullName;
$param['password'] = $password;
$param['email'] = $email;
$param['type_id'] = 1; // type Person
// $param['default_privileges'] = privilege_to_bits(array('all')) ; // ne compile pas
$aPerson = new DAVPrincipal($param);
$aPerson->password = $password; // ne marche pas
$aPerson->privileges = $privileges; // ne marche pas
$aPerson->Create($param);
if ($privileges)
{
$DB = new Database("davical_app", "davical");
$params2[] = privilege_to_bits($privileges);
$params2[] = $userName;
$query = "UPDATE dav_principal SET default_privileges = $1 WHERE username = $2;";
$result = $DB->executeQuery($query, $params2);
$DB->close();
if (!$result)
{
$DB->showError();
}
}
return $aPerson;
}
function CreateGroupAccount($className, $password, $email = NULL, $privilege = NULL) // pour l'instant le mot de passe n'est pas utile, on n'est pas censé se connecter en tant que class
{
$param['username'] = $className;
$param['fullname'] = $className;
$param['displayname'] = $className;
$param['password'] = $password; // ne marche pas
$param['email'] = $email;
$param['type_id'] = 3; // type Groupe;
$aClass = new DAVPrincipal($param);
$aClass->Create($param);
return $aClass;
}
if (isset($_POST['action']))
{
if ($_POST['action'] == 'add_subject')
{
$aGroup = new Group();
if ($aGroup->loadFromDB($_POST['groupname']))
{
$aSubject = new Subject($_POST['subjectname'], $aGroup);
if ($_POST['speaker1'] != "--")
{
$res = Database::currentDB()->executeQuery(query_person_by_fullname($_POST['speaker1']));
if ($res)
{
$result = pg_fetch_assoc($res);
$aPerson = new Person();
$aPerson->loadFromDB($result['id']);
$aSubject->addTeacher($aPerson);
}
else
{
Database::currentDB()->showError();
}
}
if ($_POST['speaker2'] != "--")
{
$res = Database::currentDB()->executeQuery(query_person_by_fullname($_POST['speaker2']));
if ($res)
{
$result = pg_fetch_assoc($res);
$aPerson = new Person();
$aPerson->loadFromDB($result['id']);
$aSubject->addTeacher($aPerson);
}
else
{
Database::currentDB()->showError();
}
}
if ($_POST['speaker3'] != "--")
{
$res = Database::currentDB()->executeQuery(query_person_by_fullname($_POST['speaker3']));
if ($res)
{
$result = pg_fetch_assoc($res);
$aPerson = new Person();
$aPerson->loadFromDB($result['id']);
$aSubject->addTeacher($aPerson);
}
else
{
Database::currentDB()->showError();
}
}
}
header('Location: ./admin_panel.php');
die;
}
if ($_POST['action'] == 'add_user')
{
if ($_POST['password'] != $_POST['password2'])
{
header('Location: ./admin_panel.php?GMESSAGE_ERROR=DIFFERENT_PASS');
die;
}
else
{
if ($_POST['status'] == 'secretary')
{
$aUser = new Secretary($_POST['familyname'], $_POST['firstname'], $_POST['login'], $_POST['password'], $_POST['email']);
}
else if ($_POST['status'] == 'teacher')
{
$aUser = new Teacher($_POST['familyname'], $_POST['firstname'], $_POST['login'], $_POST['password'], $_POST['email']);
}
else if ($_POST['status'] == 'head')
{
$aUser = new Head($_POST['familyname'], $_POST['firstname'], $_POST['login'], $_POST['password'], $_POST['email']);
}
else if ($_POST['status'] == 'administrator')
{
$aUser = new Administrator($_POST['familyname'], $_POST['firstname'], $_POST['login'], $_POST['password'], $_POST['email']);
}
echo($aUser->toHTML());
header('Location: ./admin_panel.php');
die;
}
}
if ($_POST['action'] == 'add_group')
{
$aGroup = new Group($_POST['name'], $_POST['isaclass']);
header('Location: ./admin_panel.php');
die;
}
if ($_POST['action'] == 'add_person')
{
$aPerson = new Person($_POST['familyname'], $_POST['firstname'], $_POST['email']);
if ($_POST['status'] == 'student')
{
$aPerson->addStatus(new PersonStatus(PersonStatus::STUDENT));
}
else if ($_POST['status'] == 'speaker')
{
$aPerson->addStatus(new PersonStatus(PersonStatus::SPEAKER));
}
echo($aPerson->toHTML());
header('Location: ./admin_panel.php');
die;
}
if ($_POST['action'] == 'delete_person')
{
$aPerson = new Person();
$aPerson->loadFromDB(intval($_POST['id']));
$aPerson->removeFromDB();
header('Location: ./admin_panel.php');
die;
}
if ($_POST['action'] == 'clear_db')
{
Database::currentDB()->clear();
header('Location: ./admin_panel.php');
die;
}
if ($_POST['action'] == 'init_db')
{
Database::currentDB()->initialize();
header('Location: ./admin_panel.php');
die;
}
if ($_POST['action'] == 'delete_group')
{
$aGroup = new Group();
if (!$aGroup->loadFromDB(intval($_POST['id'])))
{
die('Group not found');
}
else
{
echo("G id/name= " . $aGroup->getSqlId() . " / " . $aGroup->getName());
}
$aGroup->removeFromDB();
header('Location: ./admin_panel.php');
die;
}
if ($_POST['action'] == 'modify_group')
{
$aGroup = new Group();
if (!$aGroup->loadFromDB(intval($_POST['id'])))
{
die('Error: Group' . $_POST['id'] . 'not found.');
}
else
{
echo("G id/name = " . $aGroup->getSqlId() . " / " . $aGroup->getName());
}
}
}
?>