|
21 | 21 | require('views/error404.php'); |
22 | 22 | die; |
23 | 23 | } |
| 24 | + |
| 25 | +$is_target_active_user = $active_user && $active_user->entity_id == $user->entity_id; |
| 26 | +$can_admin_add_for_user = $active_user && $active_user->admin && !$is_target_active_user; |
| 27 | +$can_submit_key = $is_target_active_user || $can_admin_add_for_user; |
| 28 | + |
| 29 | +if(isset($_POST['add_public_key'])) { |
| 30 | + if(!$can_submit_key) { |
| 31 | + require('views/error403.php'); |
| 32 | + die; |
| 33 | + } |
| 34 | + try { |
| 35 | + $public_key = new PublicKey; |
| 36 | + $public_key->import($_POST['add_public_key'], $user->uid); |
| 37 | + $user->add_public_key($public_key); |
| 38 | + redirect(); |
| 39 | + } catch(InvalidArgumentException $e) { |
| 40 | + global $config; |
| 41 | + $content = new PageSection('key_upload_fail'); |
| 42 | + $error_message = $e->getMessage(); |
| 43 | + if(preg_match('/^Insufficient bits in public key: (\d+) < (\d+)$/', $error_message, $matches)) { |
| 44 | + $actual_bits = $matches[1]; |
| 45 | + $required_bits = $matches[2]; |
| 46 | + $content->set('message', "The public key you submitted is of insufficient strength; it has {$actual_bits} bits but must be at least {$required_bits} bits."); |
| 47 | + } else { |
| 48 | + $content->set('message', "The public key you submitted doesn't look valid."); |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
24 | 53 | $pubkeys = $user->list_public_keys(); |
25 | 54 | if(isset($router->vars['format']) && $router->vars['format'] == 'txt') { |
26 | 55 | $page = new PageSection('entity_pubkeys_txt'); |
|
33 | 62 | header('Content-type: application/json; charset=utf-8'); |
34 | 63 | echo $page->generate(); |
35 | 64 | } else { |
36 | | - $content = new PageSection('user_pubkeys'); |
37 | | - $content->set('user', $user); |
38 | | - $content->set('pubkeys', $pubkeys); |
39 | | - $content->set('admin', $active_user->admin); |
| 65 | + $head = '<link rel="alternate" type="application/json" href="pubkeys.json" title="JSON for this page">' . "\n"; |
| 66 | + $head .= '<link rel="alternate" type="text/plain" href="pubkeys.txt" title="TXT format for this page">' . "\n"; |
40 | 67 |
|
41 | | - $head = '<link rel="alternate" type="application/json" href="pubkeys.json" title="JSON for this page">'."\n"; |
42 | | - $head .= '<link rel="alternate" type="text/plain" href="pubkeys.txt" title="TXT format for this page">'."\n"; |
| 68 | + if(!isset($content)) { |
| 69 | + $content = new PageSection('user_pubkeys'); |
| 70 | + $content->set('user', $user); |
| 71 | + $content->set('pubkeys', $pubkeys); |
| 72 | + $content->set('admin', $active_user ? $active_user->admin : false); |
| 73 | + $content->set('allow_admin_add', $can_admin_add_for_user); |
| 74 | + } |
43 | 75 |
|
44 | 76 | $page = new PageSection('base'); |
45 | | - $page->set('title', 'Public keys for '.$user->name); |
| 77 | + $page->set('title', 'Public keys for ' . $user->name); |
46 | 78 | $page->set('head', $head); |
47 | 79 | $page->set('content', $content); |
48 | 80 | $page->set('alerts', $active_user->pop_alerts()); |
|
0 commit comments