File inc/cache.inc.php added (mode: 100644) (index 0000000..9625809) |
|
1 |
|
<?php |
|
2 |
|
// |
|
3 |
|
// This functions are used for background tasks: the tasks that the user should |
|
4 |
|
// not wait to happen in the browser: e-mails, keyring regeneration etc. |
|
5 |
|
// |
|
6 |
|
require_once($INC . "/util.inc.php"); |
|
7 |
|
require_once($INC . "/log.inc.php"); |
|
8 |
|
require_once($INC . "/sql.inc.php"); |
|
9 |
|
require_once($INC . "/prof.inc.php"); |
|
10 |
|
|
|
11 |
|
// timeout in miliseconds |
|
12 |
|
$rg_cache_timeout = 100; |
|
13 |
|
|
|
14 |
|
if (!isset($rg_cache_socket)) |
|
15 |
|
$rg_cache_socket = "/var/lib/rocketgit/sockets/cache.sock"; |
|
16 |
|
|
|
17 |
|
$rg_cache = array( |
|
18 |
|
"normal" => array() |
|
19 |
|
); |
|
20 |
|
|
|
21 |
|
$rg_cache_error = ""; |
|
22 |
|
|
|
23 |
|
function rg_cache_set_error($str) |
|
24 |
|
{ |
|
25 |
|
global $rg_cache_error; |
|
26 |
|
$rg_cache_error = $str; |
|
27 |
|
rg_log($str); |
|
28 |
|
} |
|
29 |
|
|
|
30 |
|
function rg_cache_error() |
|
31 |
|
{ |
|
32 |
|
global $rg_cache_error; |
|
33 |
|
return $rg_cache_error; |
|
34 |
|
} |
|
35 |
|
|
|
36 |
|
/* |
|
37 |
|
* Dump all tables |
|
38 |
|
*/ |
|
39 |
|
function rg_cache_server_dump() |
|
40 |
|
{ |
|
41 |
|
global $rg_cache; |
|
42 |
|
|
|
43 |
|
rg_log_ml("rg_cache:\n" . print_r($rg_cache, TRUE)); |
|
44 |
|
} |
|
45 |
|
|
|
46 |
|
/* |
|
47 |
|
* Returns the pointer to a tree |
|
48 |
|
* TODO: Prevent cache to grow and grow. |
|
49 |
|
*/ |
|
50 |
|
function rg_cache_server_set($ns_var, $value) |
|
51 |
|
{ |
|
52 |
|
global $rg_cache; |
|
53 |
|
|
|
54 |
|
$tree = &$rg_cache; |
|
55 |
|
$t = explode("::", $ns_var); |
|
56 |
|
$var = array_pop($t); |
|
57 |
|
foreach ($t as $token) { |
|
58 |
|
if (!isset($tree[$token])) |
|
59 |
|
$tree[$token] = array(); |
|
60 |
|
|
|
61 |
|
$tree = &$tree[$token]; |
|
62 |
|
} |
|
63 |
|
|
|
64 |
|
$tree[$var] = $value; |
|
65 |
|
} |
|
66 |
|
|
|
67 |
|
/* |
|
68 |
|
* Retrieve a variable from cache |
|
69 |
|
*/ |
|
70 |
|
function rg_cache_server_get($ns_var) |
|
71 |
|
{ |
|
72 |
|
global $rg_cache; |
|
73 |
|
|
|
74 |
|
$tree = &$rg_cache; |
|
75 |
|
$t = explode("::", $ns_var); |
|
76 |
|
$var = array_pop($t); |
|
77 |
|
foreach ($t as $token) { |
|
78 |
|
if (!isset($tree[$token])) |
|
79 |
|
return FALSE; |
|
80 |
|
|
|
81 |
|
$tree = &$tree[$token]; |
|
82 |
|
} |
|
83 |
|
|
|
84 |
|
if (isset($tree[$var])) |
|
85 |
|
return $tree[$var]; |
|
86 |
|
|
|
87 |
|
return FALSE; |
|
88 |
|
} |
|
89 |
|
|
|
90 |
|
/* |
|
91 |
|
* Unset a variable in cache |
|
92 |
|
* Returns FALSE if not found, else TRUE. |
|
93 |
|
*/ |
|
94 |
|
function rg_cache_server_unset($ns_var) |
|
95 |
|
{ |
|
96 |
|
global $rg_cache; |
|
97 |
|
|
|
98 |
|
$tree = &$rg_cache; |
|
99 |
|
$t = explode("::", $ns_var); |
|
100 |
|
$var = array_pop($t); |
|
101 |
|
foreach ($t as $token) { |
|
102 |
|
if (!isset($tree[$token])) |
|
103 |
|
return FALSE; |
|
104 |
|
|
|
105 |
|
$tree = &$tree[$token]; |
|
106 |
|
} |
|
107 |
|
|
|
108 |
|
if (isset($tree[$var])) { |
|
109 |
|
unset($tree[$var]); |
|
110 |
|
return TRUE; |
|
111 |
|
} |
|
112 |
|
|
|
113 |
|
return FALSE; |
|
114 |
|
} |
|
115 |
|
|
|
116 |
|
/* |
|
117 |
|
* Push a variable in a queue |
|
118 |
|
*/ |
|
119 |
|
function rg_cache_server_apush($ns_var, $value) |
|
120 |
|
{ |
|
121 |
|
global $rg_cache; |
|
122 |
|
|
|
123 |
|
$tree = &$rg_cache; |
|
124 |
|
$t = explode("::", $ns_var); |
|
125 |
|
$var = array_pop($t); |
|
126 |
|
foreach ($t as $token) { |
|
127 |
|
if (!isset($tree[$token])) |
|
128 |
|
$tree[$token] = array(); |
|
129 |
|
|
|
130 |
|
$tree = &$tree[$token]; |
|
131 |
|
} |
|
132 |
|
|
|
133 |
|
if (!isset($tree[$var])) |
|
134 |
|
$tree[$var] = array(); |
|
135 |
|
|
|
136 |
|
array_push($tree[$var], $value); |
|
137 |
|
} |
|
138 |
|
|
|
139 |
|
/* |
|
140 |
|
* Pop a variable from the end of a queue |
|
141 |
|
*/ |
|
142 |
|
function rg_cache_server_apop($ns_var) |
|
143 |
|
{ |
|
144 |
|
global $rg_cache; |
|
145 |
|
|
|
146 |
|
$tree = &$rg_cache; |
|
147 |
|
$t = explode("::", $ns_var); |
|
148 |
|
$var = array_pop($t); |
|
149 |
|
foreach ($t as $token) { |
|
150 |
|
if (!isset($tree[$token])) |
|
151 |
|
return FALSE; |
|
152 |
|
|
|
153 |
|
$tree = &$tree[$token]; |
|
154 |
|
} |
|
155 |
|
|
|
156 |
|
if (!isset($tree[$var])) |
|
157 |
|
return FALSE; |
|
158 |
|
|
|
159 |
|
if (empty($tree[$var])) |
|
160 |
|
return FALSE; |
|
161 |
|
|
|
162 |
|
return array_pop($tree[$var]); |
|
163 |
|
} |
|
164 |
|
|
|
165 |
|
/* |
|
166 |
|
* Pop a variable from the begining of a queue |
|
167 |
|
*/ |
|
168 |
|
function rg_cache_server_ashift($ns_var) |
|
169 |
|
{ |
|
170 |
|
global $rg_cache; |
|
171 |
|
|
|
172 |
|
$tree = &$rg_cache; |
|
173 |
|
$t = explode("::", $ns_var); |
|
174 |
|
$var = array_pop($t); |
|
175 |
|
foreach ($t as $token) { |
|
176 |
|
if (!isset($tree[$token])) |
|
177 |
|
return FALSE; |
|
178 |
|
|
|
179 |
|
$tree = &$tree[$token]; |
|
180 |
|
} |
|
181 |
|
|
|
182 |
|
if (!isset($tree[$var])) |
|
183 |
|
return FALSE; |
|
184 |
|
|
|
185 |
|
if (empty($tree[$var])) |
|
186 |
|
return FALSE; |
|
187 |
|
|
|
188 |
|
return array_shift($tree[$var]); |
|
189 |
|
} |
|
190 |
|
|
|
191 |
|
/* |
|
192 |
|
* Dumps a queue |
|
193 |
|
*/ |
|
194 |
|
function rg_cache_server_adump($ns_var) |
|
195 |
|
{ |
|
196 |
|
global $rg_cache; |
|
197 |
|
|
|
198 |
|
$tree = &$rg_cache; |
|
199 |
|
$t = explode("::", $ns_var); |
|
200 |
|
$var = array_pop($t); |
|
201 |
|
foreach ($t as $token) { |
|
202 |
|
if (!isset($tree[$token])) |
|
203 |
|
return FALSE; |
|
204 |
|
|
|
205 |
|
$tree = &$tree[$token]; |
|
206 |
|
} |
|
207 |
|
|
|
208 |
|
if (!isset($tree[$var])) |
|
209 |
|
return FALSE; |
|
210 |
|
|
|
211 |
|
return implode(",", $tree[$var]); |
|
212 |
|
} |
|
213 |
|
|
|
214 |
|
|
|
215 |
|
/********************************* Client side functions */ |
|
216 |
|
|
|
217 |
|
/* |
|
218 |
|
* This is used as another cache layer, local to script |
|
219 |
|
*/ |
|
220 |
|
$rg_cache_local = array(); |
|
221 |
|
|
|
222 |
|
/* |
|
223 |
|
* Returns a variable from the cache daemon |
|
224 |
|
* @timeout_in_ms is the connection timeout not variable timeout. |
|
225 |
|
*/ |
|
226 |
|
function rg_cache_get($ns_var) |
|
227 |
|
{ |
|
228 |
|
global $rg_cache_socket; |
|
229 |
|
global $rg_cache_timeout; |
|
230 |
|
global $rg_cache_local; |
|
231 |
|
|
|
232 |
|
rg_prof_start("cache_get"); |
|
233 |
|
|
|
234 |
|
$ret = FALSE; |
|
235 |
|
do { |
|
236 |
|
if (isset($rg_cache_local[$ns_var])) { |
|
237 |
|
$ret = $rg_cache_local[$ns_var]; |
|
238 |
|
break; |
|
239 |
|
} |
|
240 |
|
|
|
241 |
|
$c = rg_socket($rg_cache_socket, |
|
242 |
|
"GET " . $ns_var . "\n", $rg_cache_timeout); |
|
243 |
|
if ($c === FALSE) |
|
244 |
|
break; |
|
245 |
|
|
|
246 |
|
$t = explode(" ", $c, 2); |
|
247 |
|
if (strcmp($t[0], "OK") != 0) |
|
248 |
|
break; |
|
249 |
|
|
|
250 |
|
if (isset($t[1])) |
|
251 |
|
$ret = trim($t[1]); |
|
252 |
|
else |
|
253 |
|
$ret = ""; |
|
254 |
|
$rg_cache_local[$ns_var] = $ret; |
|
255 |
|
} while (0); |
|
256 |
|
|
|
257 |
|
rg_prof_end("cache_get"); |
|
258 |
|
return $ret; |
|
259 |
|
} |
|
260 |
|
|
|
261 |
|
/* |
|
262 |
|
* Sets a variable in the cache daemon |
|
263 |
|
*/ |
|
264 |
|
function rg_cache_set($ns_var, $value) |
|
265 |
|
{ |
|
266 |
|
global $rg_cache_socket; |
|
267 |
|
global $rg_cache_timeout; |
|
268 |
|
global $rg_cache_local; |
|
269 |
|
|
|
270 |
|
rg_prof_start("cache_set"); |
|
271 |
|
|
|
272 |
|
$ret = FALSE; |
|
273 |
|
do { |
|
274 |
|
$c = rg_socket($rg_cache_socket, |
|
275 |
|
"SET " . $ns_var . "=" . $value . "\n", $rg_cache_timeout); |
|
276 |
|
if ($c === FALSE) |
|
277 |
|
break; |
|
278 |
|
|
|
279 |
|
if (strncmp($c, "OK", 2) != 0) |
|
280 |
|
break; |
|
281 |
|
|
|
282 |
|
$rg_cache_local[$ns_var] = $value; |
|
283 |
|
$ret = TRUE; |
|
284 |
|
} while (0); |
|
285 |
|
|
|
286 |
|
rg_prof_end("cache_set"); |
|
287 |
|
return $ret; |
|
288 |
|
} |
|
289 |
|
|
|
290 |
|
/* |
|
291 |
|
* Unsets a variable in the cache daemon |
|
292 |
|
*/ |
|
293 |
|
function rg_cache_unset($ns_var) |
|
294 |
|
{ |
|
295 |
|
global $rg_cache_socket; |
|
296 |
|
global $rg_cache_timeout; |
|
297 |
|
global $rg_cache_local; |
|
298 |
|
|
|
299 |
|
rg_prof_start("cache_unset"); |
|
300 |
|
|
|
301 |
|
unset($rg_cache_local[$ns_var]); |
|
302 |
|
|
|
303 |
|
$ret = FALSE; |
|
304 |
|
do { |
|
305 |
|
$ret = rg_socket($rg_cache_socket, |
|
306 |
|
"UNSET " . $ns_var . "\n", $rg_cache_timeout); |
|
307 |
|
if ($ret === FALSE) |
|
308 |
|
break; |
|
309 |
|
|
|
310 |
|
// TODO: return old value? |
|
311 |
|
if (strncmp($ret, "OK", 2) != 0) |
|
312 |
|
break; |
|
313 |
|
|
|
314 |
|
$ret = TRUE; |
|
315 |
|
} while (0); |
|
316 |
|
|
|
317 |
|
rg_prof_end("cache_unset"); |
|
318 |
|
return $ret; |
|
319 |
|
} |
|
320 |
|
|
|
321 |
|
?> |
File inc/plan.inc.php copied from file inc/user.inc.php (similarity 89%) (mode: 100644) (index e9616f4..180476b) |
... |
... |
require_once($INC . "/sql.inc.php"); |
5 |
5 |
require_once($INC . "/sess.inc.php"); |
require_once($INC . "/sess.inc.php"); |
6 |
6 |
require_once($INC . "/rights.inc.php"); |
require_once($INC . "/rights.inc.php"); |
7 |
7 |
require_once($INC . "/events.inc.php"); |
require_once($INC . "/events.inc.php"); |
|
8 |
|
require_once($INC . "/cache.inc.php"); |
8 |
9 |
|
|
9 |
10 |
$rg_user_rights = array( |
$rg_user_rights = array( |
10 |
11 |
"C" => "Create repositories", |
"C" => "Create repositories", |
|
... |
... |
function rg_user_ok($user) |
226 |
227 |
*/ |
*/ |
227 |
228 |
function rg_user_lookup_by_old_name($db, $old_name) |
function rg_user_lookup_by_old_name($db, $old_name) |
228 |
229 |
{ |
{ |
229 |
|
rg_prof_start("rg_user_lookup_by_old_name"); |
|
|
230 |
|
rg_prof_start("user_lookup_by_old_name"); |
230 |
231 |
rg_log("user_lookup_by_old_name: old_name=$old_name"); |
rg_log("user_lookup_by_old_name: old_name=$old_name"); |
231 |
232 |
|
|
232 |
233 |
$ret = FALSE; |
$ret = FALSE; |
233 |
234 |
do { |
do { |
|
235 |
|
$x = rg_cache_get("old_name::" . $old_name); |
|
236 |
|
if ($x !== FALSE) { |
|
237 |
|
$ret = $x; |
|
238 |
|
break; |
|
239 |
|
} |
|
240 |
|
|
234 |
241 |
$e_old_name = rg_sql_escape($db, $old_name); |
$e_old_name = rg_sql_escape($db, $old_name); |
235 |
242 |
$sql = "SELECT uid FROM users_renames" |
$sql = "SELECT uid FROM users_renames" |
236 |
243 |
. " WHERE old_name = '$e_old_name'"; |
. " WHERE old_name = '$e_old_name'"; |
|
... |
... |
function rg_user_lookup_by_old_name($db, $old_name) |
248 |
255 |
$ret = 0; |
$ret = 0; |
249 |
256 |
else |
else |
250 |
257 |
$ret = $row['uid']; |
$ret = $row['uid']; |
|
258 |
|
|
|
259 |
|
rg_cache_set("old_name::" . $old_name, $ret); |
251 |
260 |
} while (0); |
} while (0); |
252 |
261 |
|
|
253 |
|
rg_prof_end("rg_user_lookup_by_old_name"); |
|
|
262 |
|
rg_prof_end("user_lookup_by_old_name"); |
254 |
263 |
return $ret; |
return $ret; |
255 |
264 |
} |
} |
256 |
265 |
|
|
|
... |
... |
function rg_user_insert_rename($db, $uid, $old_name) |
290 |
299 |
break; |
break; |
291 |
300 |
} |
} |
292 |
301 |
|
|
|
302 |
|
rg_cache_set("old_name::" . $old_name, $uid); |
|
303 |
|
|
293 |
304 |
$ret = TRUE; |
$ret = TRUE; |
294 |
305 |
} while (0); |
} while (0); |
295 |
306 |
|
|
|
... |
... |
function rg_user_rename($db, $ui, $new_name) |
383 |
394 |
*/ |
*/ |
384 |
395 |
function rg_user_edit($db, $d) |
function rg_user_edit($db, $d) |
385 |
396 |
{ |
{ |
386 |
|
global $rg_user_info_cache; // TODO: what we do with this? |
|
387 |
|
|
|
388 |
397 |
rg_prof_start("user_edit"); |
rg_prof_start("user_edit"); |
389 |
398 |
rg_log("user_edit: data: " . rg_array2string($d)); |
rg_log("user_edit: data: " . rg_array2string($d)); |
390 |
399 |
|
|
|
... |
... |
function rg_user_edit($db, $d) |
458 |
467 |
rg_sql_free_result($res); |
rg_sql_free_result($res); |
459 |
468 |
|
|
460 |
469 |
// invalidate cache |
// invalidate cache |
461 |
|
$rg_user_info_cache = array(); |
|
|
470 |
|
rg_cache_unset("user::" . $d['uid']); |
462 |
471 |
|
|
463 |
472 |
if ($d['uid'] == 0) { // add |
if ($d['uid'] == 0) { // add |
464 |
473 |
$event = array("category" => 2000, "prio" => 50, |
$event = array("category" => 2000, "prio" => 50, |
|
... |
... |
function rg_user_edit($db, $d) |
485 |
494 |
*/ |
*/ |
486 |
495 |
function rg_user_remove($db, $uid) |
function rg_user_remove($db, $uid) |
487 |
496 |
{ |
{ |
488 |
|
global $rg_user_info_cache; |
|
489 |
|
|
|
490 |
497 |
rg_prof_start("user_remove"); |
rg_prof_start("user_remove"); |
491 |
498 |
rg_log("user_remove: uid=$uid"); |
rg_log("user_remove: uid=$uid"); |
492 |
499 |
|
|
|
... |
... |
function rg_user_remove($db, $uid) |
503 |
510 |
rg_sql_free_result($res); |
rg_sql_free_result($res); |
504 |
511 |
|
|
505 |
512 |
// invalidate cache |
// invalidate cache |
506 |
|
$rg_user_info_cache = array(); |
|
|
513 |
|
rg_cache_unset("user::" . $uid); |
507 |
514 |
|
|
508 |
515 |
$ret = TRUE; |
$ret = TRUE; |
509 |
516 |
} while (0); |
} while (0); |
|
... |
... |
function rg_user_remove($db, $uid) |
515 |
522 |
/* |
/* |
516 |
523 |
* Returns info about a user (by uid, user or e-mail) |
* Returns info about a user (by uid, user or e-mail) |
517 |
524 |
*/ |
*/ |
518 |
|
$rg_user_info_cache = array(); |
|
519 |
525 |
function rg_user_info($db, $uid, $user, $email) |
function rg_user_info($db, $uid, $user, $email) |
520 |
526 |
{ |
{ |
521 |
|
global $rg_user_info_cache; |
|
522 |
|
|
|
523 |
|
$key = $uid . "__" . $user . "__" . $email; |
|
524 |
|
if (isset($rg_user_info_cache[$key])) |
|
525 |
|
return $rg_user_info_cache[$key]; |
|
526 |
|
|
|
527 |
527 |
rg_prof_start("user_info"); |
rg_prof_start("user_info"); |
528 |
|
rg_log("user_info: uid/user/email=$uid/$user/$email..."); |
|
529 |
528 |
|
|
530 |
529 |
$ret = array(); |
$ret = array(); |
531 |
530 |
$ret['ok'] = 0; |
$ret['ok'] = 0; |
|
... |
... |
function rg_user_info($db, $uid, $user, $email) |
533 |
532 |
$ret['uid'] = 0; |
$ret['uid'] = 0; |
534 |
533 |
$ret['is_admin'] = 0; |
$ret['is_admin'] = 0; |
535 |
534 |
|
|
536 |
|
// in case we return error |
|
537 |
|
$rg_user_info_cache[$key] = $ret; |
|
|
535 |
|
$set_cache = FALSE; |
|
536 |
|
$set_cache_user = FALSE; |
|
537 |
|
$set_cache_email = FALSE; |
|
538 |
|
while (1) { |
|
539 |
|
//rg_log("user_info: uid=$uid user=$user email=$email."); |
|
540 |
|
|
|
541 |
|
if ($uid > 0) { |
|
542 |
|
$c = rg_cache_get("user::" . $uid); |
|
543 |
|
if ($c !== FALSE) { |
|
544 |
|
$ret = unserialize($c); |
|
545 |
|
if ($ret !== FALSE) |
|
546 |
|
break; |
|
547 |
|
} |
|
548 |
|
$add = " AND uid = " . sprintf("%u", $uid); |
|
549 |
|
$set_cache = TRUE; |
|
550 |
|
} else if (!empty($user)) { |
|
551 |
|
if (rg_user_ok($user) !== TRUE) |
|
552 |
|
break; |
538 |
553 |
|
|
539 |
|
if ($uid > 0) { |
|
540 |
|
$add = " AND uid = " . sprintf("%u", $uid); |
|
541 |
|
} else if (!empty($user)) { |
|
542 |
|
if (rg_user_ok($user) !== TRUE) |
|
543 |
|
return FALSE; |
|
|
554 |
|
$c = rg_cache_get("username_to_uid::" . $user); |
|
555 |
|
if ($c !== FALSE) { |
|
556 |
|
$uid = $c; |
|
557 |
|
continue; |
|
558 |
|
} |
544 |
559 |
|
|
545 |
|
$e_user = rg_sql_escape($db, $user); |
|
546 |
|
$add = " AND username = '$e_user'"; |
|
547 |
|
} else if (!empty($email)) { |
|
548 |
|
$e_email = rg_sql_escape($db, $email); |
|
549 |
|
$add = " AND email = '$e_email'"; |
|
550 |
|
} else { |
|
551 |
|
return FALSE; |
|
552 |
|
} |
|
|
560 |
|
$e_user = rg_sql_escape($db, $user); |
|
561 |
|
$add = " AND username = '$e_user'"; |
|
562 |
|
$set_cache_user = TRUE; |
|
563 |
|
} else if (!empty($email)) { |
|
564 |
|
$c = rg_cache_get("email_to_uid::" . $email); |
|
565 |
|
if ($c != FALSE) { |
|
566 |
|
$uid = $c; |
|
567 |
|
continue; |
|
568 |
|
} |
553 |
569 |
|
|
554 |
|
$sql = "SELECT * FROM users WHERE 1 = 1" . $add; |
|
555 |
|
$res = rg_sql_query($db, $sql); |
|
556 |
|
if ($res === FALSE) { |
|
557 |
|
rg_user_set_error("cannot get info (" . rg_sql_error() . ")"); |
|
558 |
|
return $ret; |
|
559 |
|
} |
|
|
570 |
|
$e_email = rg_sql_escape($db, $email); |
|
571 |
|
$add = " AND email = '$e_email'"; |
|
572 |
|
$set_cache_email = TRUE; |
|
573 |
|
} |
560 |
574 |
|
|
561 |
|
$ret['ok'] = 1; |
|
562 |
|
$rows = rg_sql_num_rows($res); |
|
563 |
|
if ($rows > 0) |
|
564 |
|
$row = rg_sql_fetch_array($res); |
|
565 |
|
rg_sql_free_result($res); |
|
566 |
|
if ($rows == 0) { |
|
567 |
|
rg_user_set_error("user not found"); |
|
568 |
|
return $ret; |
|
|
575 |
|
$sql = "SELECT * FROM users WHERE 1 = 1" . $add; |
|
576 |
|
$res = rg_sql_query($db, $sql); |
|
577 |
|
if ($res === FALSE) { |
|
578 |
|
rg_user_set_error("cannot get info (" . rg_sql_error() . ")"); |
|
579 |
|
break; |
|
580 |
|
} |
|
581 |
|
|
|
582 |
|
$ret['ok'] = 1; |
|
583 |
|
$rows = rg_sql_num_rows($res); |
|
584 |
|
if ($rows > 0) |
|
585 |
|
$row = rg_sql_fetch_array($res); |
|
586 |
|
rg_sql_free_result($res); |
|
587 |
|
if ($rows == 0) { |
|
588 |
|
rg_user_set_error("user not found"); |
|
589 |
|
break; |
|
590 |
|
} |
|
591 |
|
|
|
592 |
|
$row['ok'] = 1; |
|
593 |
|
$row['exists'] = 1; |
|
594 |
|
|
|
595 |
|
$ret = $row; |
|
596 |
|
break; |
|
597 |
|
}; |
|
598 |
|
|
|
599 |
|
if ($ret['exists'] == 1) { |
|
600 |
|
if ($set_cache) |
|
601 |
|
rg_cache_set("user::" . $ret['uid'], serialize($ret)); |
569 |
602 |
} |
} |
570 |
603 |
|
|
571 |
|
$row['ok'] = 1; |
|
572 |
|
$row['exists'] = 1; |
|
573 |
|
rg_log("\tUser found."); |
|
|
604 |
|
if ($ret['exists'] == 1) { |
|
605 |
|
if ($set_cache_user) |
|
606 |
|
rg_cache_set("username_to_uid::" . $ret['username'], $ret['uid']); |
574 |
607 |
|
|
575 |
|
$rg_user_info_cache[$key] = $row; |
|
|
608 |
|
if ($set_cache_email) |
|
609 |
|
rg_cache_set("email_to_uid::" . $ret['email'], $ret['uid']); |
|
610 |
|
} |
576 |
611 |
|
|
577 |
612 |
rg_prof_end("user_info"); |
rg_prof_end("user_info"); |
578 |
|
return $row; |
|
|
613 |
|
return $ret; |
579 |
614 |
} |
} |
580 |
615 |
|
|
581 |
616 |
/* |
/* |
|
... |
... |
function rg_user_login_by_sid($db, $sid, &$ui) |
596 |
631 |
return FALSE; |
return FALSE; |
597 |
632 |
} |
} |
598 |
633 |
|
|
599 |
|
$uid = rg_sess_valid($db, $sid); |
|
600 |
|
if ($uid == 0) |
|
|
634 |
|
$sess = rg_sess_valid($db, $sid); |
|
635 |
|
if ($sess == FALSE) |
601 |
636 |
return FALSE; |
return FALSE; |
602 |
637 |
|
|
|
638 |
|
$uid = $sess['uid']; |
603 |
639 |
$ui = rg_user_info($db, $uid, "", ""); |
$ui = rg_user_info($db, $uid, "", ""); |
604 |
640 |
if ($ui['exists'] != 1) { |
if ($ui['exists'] != 1) { |
605 |
641 |
rg_log("\tUid $uid does not exists (" . rg_user_error() . ")!"); |
rg_log("\tUid $uid does not exists (" . rg_user_error() . ")!"); |
|
... |
... |
function rg_user_login_by_sid($db, $sid, &$ui) |
607 |
643 |
return FALSE; |
return FALSE; |
608 |
644 |
} |
} |
609 |
645 |
|
|
610 |
|
rg_sess_update($db, $sid); |
|
|
646 |
|
rg_sess_update($db, $sess); |
611 |
647 |
|
|
612 |
648 |
rg_user_set_last_seen($db, $ui['uid']); |
rg_user_set_last_seen($db, $ui['uid']); |
613 |
649 |
|
|
|
... |
... |
function rg_user_pass_valid($db, $uid, $pass) |
645 |
681 |
/* |
/* |
646 |
682 |
* Auto login the user |
* Auto login the user |
647 |
683 |
*/ |
*/ |
648 |
|
function rg_user_auto_login($db, $uid, &$ui) |
|
|
684 |
|
function rg_user_auto_login($db, $uid, $lock_ip, &$ui) |
649 |
685 |
{ |
{ |
650 |
686 |
$ui = rg_user_info($db, $uid, "", ""); |
$ui = rg_user_info($db, $uid, "", ""); |
651 |
687 |
if ($ui['ok'] != 1) |
if ($ui['ok'] != 1) |
|
... |
... |
function rg_user_auto_login($db, $uid, &$ui) |
659 |
695 |
$secure = TRUE; |
$secure = TRUE; |
660 |
696 |
|
|
661 |
697 |
$sid = rg_id(40); |
$sid = rg_id(40); |
662 |
|
rg_sess_add($db, $uid, $sid, $ui['session_time']); |
|
|
698 |
|
rg_sess_add($db, $uid, $sid, $ui['session_time'], $lock_ip); |
663 |
699 |
setcookie("sid", $sid, 0, "/", $_SERVER['SERVER_NAME'], |
setcookie("sid", $sid, 0, "/", $_SERVER['SERVER_NAME'], |
664 |
700 |
$secure, TRUE /* httponly */); |
$secure, TRUE /* httponly */); |
665 |
701 |
|
|
|
... |
... |
function rg_user_auto_login($db, $uid, &$ui) |
669 |
705 |
/* |
/* |
670 |
706 |
* Test if login is OK |
* Test if login is OK |
671 |
707 |
*/ |
*/ |
672 |
|
function rg_user_login_by_user_pass($db, $user, $pass, &$ui) |
|
|
708 |
|
function rg_user_login_by_user_pass($db, $user, $pass, $lock_ip, &$ui) |
673 |
709 |
{ |
{ |
674 |
|
rg_log("user_login_by_user_pass: user=$user, pass=$pass..."); |
|
|
710 |
|
rg_log("user_login_by_user_pass: user=$user, pass=$pass lock_ip=$lock_ip"); |
675 |
711 |
|
|
676 |
712 |
$ui = array(); |
$ui = array(); |
677 |
713 |
$ui['uid'] = 0; |
$ui['uid'] = 0; |
|
... |
... |
function rg_user_login_by_user_pass($db, $user, $pass, &$ui) |
709 |
745 |
} |
} |
710 |
746 |
|
|
711 |
747 |
$ui = $ui0; |
$ui = $ui0; |
712 |
|
rg_user_auto_login($db, $ui['uid'], $ui); |
|
|
748 |
|
rg_user_auto_login($db, $ui['uid'], $lock_ip, $ui); |
713 |
749 |
|
|
714 |
750 |
rg_user_set_last_seen($db, $ui['uid']); |
rg_user_set_last_seen($db, $ui['uid']); |
715 |
751 |
|
|
|
... |
... |
function rg_user_suspend($db, $uid, $op) |
739 |
775 |
} |
} |
740 |
776 |
rg_sql_free_result($res); |
rg_sql_free_result($res); |
741 |
777 |
|
|
|
778 |
|
// Invalidate cache. |
|
779 |
|
rg_cache_unset("user::" . $uid); |
|
780 |
|
|
742 |
781 |
return TRUE; |
return TRUE; |
743 |
782 |
} |
} |
744 |
783 |
|
|
|
... |
... |
function rg_user_make_admin($db, $uid, $op) |
758 |
797 |
} |
} |
759 |
798 |
rg_sql_free_result($res); |
rg_sql_free_result($res); |
760 |
799 |
|
|
|
800 |
|
// Invalidate cache. |
|
801 |
|
rg_cache_unset("user::" . $uid); |
|
802 |
|
|
761 |
803 |
return TRUE; |
return TRUE; |
762 |
804 |
} |
} |
763 |
805 |
|
|
|
... |
... |
function rg_user_set_last_seen($db, $uid) |
778 |
820 |
} |
} |
779 |
821 |
rg_sql_free_result($res); |
rg_sql_free_result($res); |
780 |
822 |
|
|
|
823 |
|
// TODO: check if we need to invalidate cache |
|
824 |
|
|
781 |
825 |
return TRUE; |
return TRUE; |
782 |
826 |
} |
} |
783 |
827 |
|
|
|
... |
... |
function rg_user_set_pass($db, $uid, $pass) |
1062 |
1106 |
} |
} |
1063 |
1107 |
rg_sql_free_result($res); |
rg_sql_free_result($res); |
1064 |
1108 |
|
|
|
1109 |
|
// Invalidate cache. |
|
1110 |
|
rg_cache_unset("user::" . $uid); |
|
1111 |
|
|
1065 |
1112 |
return TRUE; |
return TRUE; |
1066 |
1113 |
} |
} |
1067 |
1114 |
|
|
|
... |
... |
function rg_user_confirm($db, $token) |
1129 |
1176 |
} |
} |
1130 |
1177 |
rg_sql_free_result($res); |
rg_sql_free_result($res); |
1131 |
1178 |
|
|
|
1179 |
|
// TODO: invalidate cache? |
|
1180 |
|
|
1132 |
1181 |
return $uid; |
return $uid; |
1133 |
1182 |
} |
} |
1134 |
1183 |
|
|
File scripts/cache.php copied from file scripts/events.php (similarity 54%) (mode: 100644) (index 550012b..9ee3448) |
1 |
1 |
<?php |
<?php |
2 |
2 |
// This is called by cron, and is persistent. |
// This is called by cron, and is persistent. |
3 |
|
// It takes care of any background job received. |
|
|
3 |
|
// It takes care fast caching, like memcache. |
4 |
4 |
// It will receive signals using a UNIX socket. |
// It will receive signals using a UNIX socket. |
5 |
|
// TODO: This will obsolete q.php |
|
6 |
5 |
error_reporting(E_ALL); |
error_reporting(E_ALL); |
7 |
6 |
ini_set("track_errors", "On"); |
ini_set("track_errors", "On"); |
8 |
7 |
|
|
|
8 |
|
// Increment this if we need to restart this daemon (protocol changes etc.) |
|
9 |
|
$rg_cache_version = 1; |
|
10 |
|
|
9 |
11 |
$now = time(); |
$now = time(); |
10 |
12 |
$_s = microtime(TRUE); |
$_s = microtime(TRUE); |
11 |
13 |
|
|
|
... |
... |
require_once($INC . "/fixes.inc.php"); |
27 |
29 |
|
|
28 |
30 |
rg_prof_start("MAIN"); |
rg_prof_start("MAIN"); |
29 |
31 |
|
|
30 |
|
rg_log_set_file($rg_log_dir . "/events.log"); |
|
|
32 |
|
rg_log_set_file($rg_log_dir . "/cache.log"); |
31 |
33 |
rg_log_set_sid("000000"); // to spread the logs |
rg_log_set_sid("000000"); // to spread the logs |
32 |
34 |
|
|
33 |
35 |
rg_log("Start..."); |
rg_log("Start..."); |
34 |
36 |
|
|
|
37 |
|
// TODO: really needed db connection? |
35 |
38 |
$db = rg_sql_open($rg_sql); |
$db = rg_sql_open($rg_sql); |
36 |
39 |
if ($db === FALSE) { |
if ($db === FALSE) { |
37 |
40 |
rg_internal_error("Cannot connect to database!"); |
rg_internal_error("Cannot connect to database!"); |
38 |
41 |
exit(1); |
exit(1); |
39 |
42 |
} |
} |
40 |
43 |
|
|
41 |
|
$r = rg_sql_struct_update($db, 0); |
|
42 |
|
if ($r !== TRUE) |
|
43 |
|
exit(1); |
|
44 |
|
|
|
45 |
|
$r = rg_fixes_update($db); |
|
46 |
|
if ($r !== TRUE) |
|
47 |
|
exit(1); |
|
48 |
|
|
|
49 |
44 |
// Remove the socket, else we will get error |
// Remove the socket, else we will get error |
50 |
|
if (file_exists($rg_event_socket)) |
|
51 |
|
unlink($rg_event_socket); |
|
|
45 |
|
if (file_exists($rg_cache_socket)) |
|
46 |
|
unlink($rg_cache_socket); |
52 |
47 |
|
|
53 |
|
// Prepare socket for signaling |
|
54 |
48 |
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0); |
$socket = socket_create(AF_UNIX, SOCK_STREAM, 0); |
55 |
49 |
if ($socket === FALSE) { |
if ($socket === FALSE) { |
56 |
50 |
rg_internal_error("Cannot create events socket!"); |
rg_internal_error("Cannot create events socket!"); |
57 |
51 |
exit(1); |
exit(1); |
58 |
52 |
} |
} |
59 |
53 |
|
|
60 |
|
$r = socket_bind($socket, $rg_event_socket); |
|
|
54 |
|
$r = socket_bind($socket, $rg_cache_socket); |
61 |
55 |
if ($r === FALSE) { |
if ($r === FALSE) { |
62 |
56 |
rg_internal_error("Cannot bind socket!"); |
rg_internal_error("Cannot bind socket!"); |
63 |
57 |
exit(1); |
exit(1); |
|
... |
... |
if ($r === FALSE) { |
69 |
63 |
exit(1); |
exit(1); |
70 |
64 |
} |
} |
71 |
65 |
|
|
72 |
|
// Allow apache to connect to socket |
|
73 |
|
$r = chmod($rg_event_socket, 0666); |
|
|
66 |
|
// Allow apache (at least) to connect to socket |
|
67 |
|
// TODO: this is a security hole. Take care. Hm. How?! |
|
68 |
|
$r = chmod($rg_cache_socket, 0666); |
74 |
69 |
if ($r === FALSE) { |
if ($r === FALSE) { |
75 |
|
rg_internal_error("Cannot set rights on event socket!"); |
|
|
70 |
|
rg_internal_error("Cannot set rights on cache socket!"); |
76 |
71 |
exit(1); |
exit(1); |
77 |
72 |
} |
} |
78 |
73 |
|
|
79 |
|
$r = socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array("sec" => 0, "usec" => 0)); |
|
80 |
|
if ($r === FALSE) { |
|
81 |
|
rg_internal_error("Cannot set SO_RCVTIMEO!"); |
|
82 |
|
exit(1); |
|
83 |
|
} |
|
84 |
|
|
|
85 |
|
$original_mtime = filemtime(__FILE__); |
|
86 |
74 |
$notify_list = array(); |
$notify_list = array(); |
|
75 |
|
$r = array(); $w = array(); |
|
76 |
|
$r['master'] = $socket; |
87 |
77 |
do { |
do { |
88 |
|
// Check our mtime so we can upgrade the software and this script |
|
89 |
|
// will restart. |
|
90 |
|
clearstatcache(); |
|
91 |
|
$mtime = filemtime(__FILE__); |
|
92 |
|
rg_log("mtime=$mtime, original_mtime=$original_mtime"); |
|
93 |
|
if ($mtime != $original_mtime) { |
|
94 |
|
rg_log("File changed. Exiting..."); |
|
|
78 |
|
$new_ver = file_get_contents(__FILE__); |
|
79 |
|
$new_ver = preg_match("/rg_cache_version = (.*);/", $new_ver, $matches); |
|
80 |
|
$new_ver = $matches[1]; |
|
81 |
|
if ($rg_cache_version != $new_ver) { |
|
82 |
|
rg_log("Version upgraded. Exiting..."); |
95 |
83 |
break; |
break; |
96 |
84 |
} |
} |
97 |
85 |
|
|
98 |
|
// check machine load - if too big we will delay |
|
99 |
|
$load = rg_load(); |
|
100 |
|
if ($load > 10) { |
|
101 |
|
rg_log("\tLoad too big! Sleeping 10s."); |
|
102 |
|
sleep(10); |
|
103 |
|
continue; |
|
104 |
|
} |
|
|
86 |
|
rg_log_buffer_clear(); |
105 |
87 |
|
|
106 |
|
do { |
|
107 |
|
$r = rg_event_process_queue($db, $notify_list); |
|
108 |
|
if ($r === FALSE) |
|
109 |
|
break; |
|
110 |
|
} while ($r > 0); |
|
|
88 |
|
$r2 = $r; $w2 = $w; $ex = array(); |
|
89 |
|
$r = socket_select($r2, $w2, $ex, 5); |
111 |
90 |
if ($r === FALSE) |
if ($r === FALSE) |
112 |
|
break; |
|
|
91 |
|
rg_fatal("Cannot select (" . socket_strerror(socket_last_error()) . ")!"); |
|
92 |
|
|
|
93 |
|
rg_log("select returned $r."); |
|
94 |
|
|
|
95 |
|
print_r($r); exit(0); |
|
96 |
|
foreach ($r as $k) { |
|
97 |
|
} |
113 |
98 |
|
|
114 |
|
// Wait for signal |
|
115 |
|
rg_log("Waiting for signal..."); |
|
116 |
99 |
$client = socket_accept($socket); |
$client = socket_accept($socket); |
117 |
100 |
if ($client === FALSE) { |
if ($client === FALSE) { |
118 |
101 |
rg_log("Connection seems broken!"); |
rg_log("Connection seems broken!"); |
|
... |
... |
do { |
137 |
120 |
$close = 0; |
$close = 0; |
138 |
121 |
|
|
139 |
122 |
// It is possible that we already executed the task |
// It is possible that we already executed the task |
140 |
|
rg_event_notify($notify_list, $ev_id); |
|
|
123 |
|
rg_event_notify($notify_list, $ev_id, ""); |
|
124 |
|
} else if (strncmp($buf, "SET ", 4) == 0) { |
|
125 |
|
$_t = explode("=", substr($buf, 4), 2); |
|
126 |
|
$ns_var = trim($_t[0]); |
|
127 |
|
$value = trim($_t[1]); |
|
128 |
|
$_t = explode("::", $ns_var, 2); |
|
129 |
|
$ns = trim($_t[0]); |
|
130 |
|
$var = trim($_t[1]); |
|
131 |
|
rg_event_set($ns, $var, $value); |
|
132 |
|
rg_event_notify($notify_list, $ns . "::" . $var, $value); |
|
133 |
|
} else if (strncmp($buf, "GET ", 4) == 0) { |
|
134 |
|
$_t = explode("::", substr($buf, 4), 2); |
|
135 |
|
$ns = trim($_t[0]); |
|
136 |
|
$var = trim($_t[1]); |
|
137 |
|
$out = rg_event_get($ns, $var); |
|
138 |
|
if ($out === FALSE) |
|
139 |
|
$out = "NOT_FOUND"; |
|
140 |
|
$r = socket_send($client, $out, strlen($out), 0); |
|
141 |
|
} else if (strncmp($buf, "UNSET ", 6) == 0) { |
|
142 |
|
$_t = explode("::", substr($buf, 6), 2); |
|
143 |
|
$ns = trim($_t[0]); |
|
144 |
|
$var = trim($_t[1]); |
|
145 |
|
rg_event_unset($ns, $var); |
|
146 |
|
$r = socket_send($client, $out, strlen($out), 0); |
|
147 |
|
rg_event_notify($notify_list, $ns . "::" . $var, ""); // TODO: need to signal FALSE |
141 |
148 |
} |
} |
142 |
149 |
} |
} |
143 |
150 |
|
|