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
|
<?php
/**
* ical_export.php - provides functions to handle the export of events
* over a short url
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* @author Peter Thienel, data-quest GmbH <thienel@data-quest.de>
* @license http://www.gnu.org/licenses/gpl-2.0.html GPL version 2
* @category Stud.IP
*/
class IcalExport
{
private static $id_length = 8;
/**
* Sets the lentgh of the key
*
* @param int $length
*/
public static function setKeyLength($length)
{
self::$id_length = $length;
}
/**
* Returns a key string.
*
* @return string
*/
public static function makeKey()
{
$length = self::$id_length;
$ret = '';
$rejected = [
'A' => 2,
'E' => 2,
'I' => 2,
'O' => 2,
'U' => 2,
'a' => 2,
'e' => 2,
'i' => 2,
'o' => 2,
'u' => 2];
while ($length--) {
while (1) {
$rnd = rand(48, 122);
if ($rnd < 48)
continue;
if ($rnd > 57 && $rnd < 65)
continue;
if ($rnd > 90 && $rnd < 97)
continue;
if ($rnd > 122)
continue;
$char = chr($rnd);
if ($rejected[$char] > 1) {
continue;
}
$rejected[$char]++;
$ret .= $char;
break;
}
}
return $ret;
}
/**
* Returns the key by given user_id. Returns false if no valid key was found.
*
* @param string $user_id
* @return mixed
*/
public static function getKeyByUser($user_id)
{
return UserConfig::get($user_id)->getValue('ICAL_EXPORT_KEY');
}
/**
* Returns user_id by given key. Returns false if no valid user_id was found.
*
* @param type $short_id
* @return mixed
*/
public static function getUserIdByKey($key)
{
$where = "field = 'ICAL_EXPORT_KEY' AND value = " . DBManager::get()->quote($key);
$user_config_entries = ConfigValue::findBySql($where);
if (isset($user_config_entries[0])) {
return $user_config_entries[0]->getValue('range_id');
} else {
return false;
}
}
/**
* Sets a new key for the user with the given user_id.
*
* @param type $user_id
* @return string the new key
*/
public static function setKey($user_id)
{
// delete old key
$key = self::getKeyByUser($user_id);
if ($key) {
self::deleteKey($user_id);
}
// make new unique key
do {
$key = self::makeKey();
} while (self::getUserIdByKey($key));
UserConfig::get($user_id)->store('ICAL_EXPORT_KEY', $key);
return $key;
}
/**
* Deletes the key for the user with the given user_id.
*
* @param type $user_id
*/
public static function deleteKey($user_id)
{
UserConfig::get($user_id)->delete('ICAL_EXPORT_KEY');
}
}
|