-
Notifications
You must be signed in to change notification settings - Fork 0
/
interface.php
181 lines (162 loc) · 5.31 KB
/
interface.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
<?php
/**
* @package org_maemo_userdata
* @author The Midgard Project, http://www.midgard-project.org
* @copyright The Midgard Project, http://www.midgard-project.org
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*/
/**
* Basic component
*
* @package org_maemo_userdata
*/
class org_maemo_userdata extends midgardmvc_core_component_baseclass
{
/**
* @brief Generates a Universally Unique IDentifier, version 4.
*
* This function generates a truly random UUID. The built in CakePHP String::uuid() function
* is not cryptographically secure. You should uses this function instead.
*
* @see http://tools.ietf.org/html/rfc4122#section-4.4
* @see http://en.wikipedia.org/wiki/UUID
* @return string A UUID, made up of 32 hex digits and 4 hyphens.
* @author sean at seancolombo dot com (via http://docs.php.net/uniqid#88023)
*/
public static function generate_UUID()
{
$pr_bits = null;
$fp = @fopen('/dev/urandom','rb');
if ($fp !== false)
{
$pr_bits .= @fread($fp, 16);
@fclose($fp);
}
else
{
// If /dev/urandom isn't available (eg: in non-unix systems), use mt_rand().
$pr_bits = "";
for ($cnt=0; $cnt < 16; $cnt++)
{
$pr_bits .= chr(mt_rand(0, 255));
}
}
$time_low = bin2hex(substr($pr_bits,0, 4));
$time_mid = bin2hex(substr($pr_bits,4, 2));
$time_hi_and_version = bin2hex(substr($pr_bits,6, 2));
$clock_seq_hi_and_reserved = bin2hex(substr($pr_bits,8, 2));
$node = bin2hex(substr($pr_bits,10, 6));
/**
* Set the four most significant bits (bits 12 through 15) of the
* time_hi_and_version field to the 4-bit version number from
* Section 4.1.3.
* @see http://tools.ietf.org/html/rfc4122#section-4.1.3
*/
$time_hi_and_version = hexdec($time_hi_and_version);
$time_hi_and_version = $time_hi_and_version >> 4;
$time_hi_and_version = $time_hi_and_version | 0x4000;
/**
* Set the two most significant bits (bits 6 and 7) of the
* clock_seq_hi_and_reserved to zero and one, respectively.
*/
$clock_seq_hi_and_reserved = hexdec($clock_seq_hi_and_reserved);
$clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved >> 2;
$clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved | 0x8000;
return sprintf(
'%08s-%04s-%04x-%04x-%012s',
$time_low, $time_mid, $time_hi_and_version,
$clock_seq_hi_and_reserved,
$node
);
}
public static function userByUuid($uuid)
{
$qb = org_maemo_userdata_person::new_query_builder();
$qb->add_constraint('apiuuid', '=', $uuid);
if ($qb->count() != 1)
{
return null;
}
else
{
$result = $qb->execute();
return $result[0];
}
}
public static function getListOfUserFields()
{
// seems like midgard doesn't export these dynamically.
// so, list needs to be manually updated
return array(
"morgid",
"garageid",
"talkid",
"username",
"password",
"joindate",
"title",
"firstname",
"lastname",
"birthdate",
"street",
"postcode",
"city",
"country",
"ccode",
"email",
"phone",
"fax",
"homepage",
"jabber",
"icq",
"aim",
"yahoo",
"msn",
"skype",
);
}
public static function personToArray(org_maemo_userdata_person $person)
{
$fields = org_maemo_userdata::getListOfUserFields();
$result = array('uuid' => $person->apiuuid);
foreach ($fields as $field)
{
$value = $person->$field;
if ($value instanceof midgard_datetime)
{
$result[$field] = $value->format(DATE_W3C);
}
else
{
$result[$field] = $value;
}
}
return $result;
}
public static function registerTransaction(org_maemo_userdata_person $person, $action)
{
$trx = new org_maemo_userdata_transaction();
$trx->apiuuid = org_maemo_userdata::generate_UUID();
$trx->useruuid = $person->apiuuid;
$trx->action = $action;
$trx->create();
self::broadcastTransaction($trx);
}
private static function broadcastTransaction(org_maemo_userdata_transaction $trx)
{
$cfg = midgardmvc_core::get_instance()->configuration;
$user = self::userByUuid($trx->useruuid);
$data = json_encode(array(
'uuid' => $trx->apiuuid,
'action' => $trx->action,
'timestamp' => $trx->metadata->created->format(DATE_W3C),
'data' => self::personToArray($user),
));
foreach ($cfg->webhooks as $webhook) {
$que = new org_maemo_userdata_webhooks_queue();
$que->url = $webhook['url'];
$que->payload = $data;
$que->create();
}
}
}