]> git.mxchange.org Git - friendica.git/blob - mod/fsuggest.php
Merge pull request #6483 from MrPetovan/bug/fixes-after-2019-03-develop-rebase
[friendica.git] / mod / fsuggest.php
1 <?php
2 /**
3  * @file mod/fsuggest.php
4  */
5
6 use Friendica\App;
7 use Friendica\Core\ACL;
8 use Friendica\Core\L10n;
9 use Friendica\Core\Worker;
10 use Friendica\Database\DBA;
11 use Friendica\Util\DateTimeFormat;
12 use Friendica\Util\Strings;
13
14 function fsuggest_post(App $a)
15 {
16         if (! local_user()) {
17                 return;
18         }
19
20         if ($a->argc != 2) {
21                 return;
22         }
23
24         $contact_id = intval($a->argv[1]);
25
26         $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user()]);
27         if (! DBA::isResult($contact)) {
28                 notice(L10n::t('Contact not found.') . EOL);
29                 return;
30         }
31
32         $new_contact = intval($_POST['suggest']);
33
34         $hash = Strings::getRandomHex();
35
36         $note = Strings::escapeHtml(trim(defaults($_POST, 'note', '')));
37
38         if ($new_contact) {
39                 $r = q("SELECT * FROM `contact` WHERE `id` = %d AND `uid` = %d LIMIT 1",
40                         intval($new_contact),
41                         intval(local_user())
42                 );
43                 if (DBA::isResult($r)) {
44                         q("INSERT INTO `fsuggest` ( `uid`,`cid`,`name`,`url`,`request`,`photo`,`note`,`created`)
45                                 VALUES ( %d, %d, '%s','%s','%s','%s','%s','%s')",
46                                 intval(local_user()),
47                                 intval($contact_id),
48                                 DBA::escape($contact['name']),
49                                 DBA::escape($contact['url']),
50                                 DBA::escape($contact['request']),
51                                 DBA::escape($contact['photo']),
52                                 DBA::escape($hash),
53                                 DBA::escape(DateTimeFormat::utcNow())
54                         );
55                         $r = q("SELECT `id` FROM `fsuggest` WHERE `note` = '%s' AND `uid` = %d LIMIT 1",
56                                 DBA::escape($hash),
57                                 intval(local_user())
58                         );
59                         if (DBA::isResult($r)) {
60                                 $fsuggest_id = $contact['id'];
61                                 q("UPDATE `fsuggest` SET `note` = '%s' WHERE `id` = %d AND `uid` = %d",
62                                         DBA::escape($note),
63                                         intval($fsuggest_id),
64                                         intval(local_user())
65                                 );
66                                 Worker::add(PRIORITY_HIGH, 'Notifier', 'suggest', $fsuggest_id);
67                         }
68
69                         info(L10n::t('Friend suggestion sent.') . EOL);
70                 }
71         }
72 }
73
74 function fsuggest_content(App $a)
75 {
76         if (! local_user()) {
77                 notice(L10n::t('Permission denied.') . EOL);
78                 return;
79         }
80
81         if ($a->argc != 2) {
82                 return;
83         }
84
85         $contact_id = intval($a->argv[1]);
86
87         $contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user()]);
88         if (! DBA::isResult($contact)) {
89                 notice(L10n::t('Contact not found.') . EOL);
90                 return;
91         }
92
93         $o = '<h3>' . L10n::t('Suggest Friends') . '</h3>';
94
95         $o .= '<div id="fsuggest-desc" >' . L10n::t('Suggest a friend for %s', $contact['name']) . '</div>';
96
97         $o .= '<form id="fsuggest-form" action="fsuggest/' . $contact_id . '" method="post" >';
98
99         $o .= ACL::getSuggestContactSelectHTML(
100                 'suggest',
101                 'suggest-select',
102                 ['size' => 4, 'exclude' => $contact_id, 'networks' => 'DFRN_ONLY', 'single' => true]
103         );
104
105
106         $o .= '<div id="fsuggest-submit-wrapper"><input id="fsuggest-submit" type="submit" name="submit" value="' . L10n::t('Submit') . '" /></div>';
107         $o .= '</form>';
108
109         return $o;
110 }