Merge pull request #976 from annando/tags
[friendica-addons.git/.git] / ifttt / ifttt.php
1 <?php
2
3 /**
4  * Name: IFTTT Receiver
5  * Description: Receives a post from https://ifttt.com/ and distributes it.
6  * Version: 0.1
7  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8  */
9 require_once 'mod/item.php';
10 use Friendica\App;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Protocol;
14 use Friendica\Database\DBA;
15 use Friendica\DI;
16 use Friendica\Model\Item;
17 use Friendica\Util\Strings;
18
19 function ifttt_install()
20 {
21         Hook::register('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
22         Hook::register('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
23 }
24
25 function ifttt_uninstall()
26 {
27         Hook::unregister('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
28         Hook::unregister('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
29 }
30
31 function ifttt_module()
32 {
33
34 }
35
36 function ifttt_content()
37 {
38
39 }
40
41 function ifttt_settings(App $a, &$s)
42 {
43         if (!local_user()) {
44                 return;
45         }
46
47         $key = DI::pConfig()->get(local_user(), 'ifttt', 'key');
48
49         if (!$key) {
50                 $key = Strings::getRandomHex(20);
51                 DI::pConfig()->set(local_user(), 'ifttt', 'key', $key);
52         }
53
54         $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
55         $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . DI::l10n()->t('IFTTT Mirror') . '</h3>';
56         $s .= '</span>';
57         $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
58         $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
59         $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . DI::l10n()->t('IFTTT Mirror') . '</h3>';
60         $s .= '</span>';
61
62         $s .= '<div id="ifttt-configuration-wrapper">';
63         $s .= '<p>' . DI::l10n()->t('Create an account at <a href="http://www.ifttt.com">IFTTT</a>. Create three Facebook recipes that are connected with <a href="https://ifttt.com/maker">Maker</a> (In the form "if Facebook then Maker") with the following parameters:') . '</p>';
64         $s .= '<h4>URL</h4>';
65         $s .= '<p>' . DI::baseUrl()->get() . '/ifttt/' . $a->user['nickname'] . '</p>';
66         $s .= '<h4>Method</h4>';
67         $s .= '<p>POST</p>';
68         $s .= '<h4>Content Type</h4>';
69         $s .= '<p>application/x-www-form-urlencoded</p>';
70         $s .= '<h4>' . DI::l10n()->t('Body for "new status message"') . '</h4>';
71         $s .= '<p><code>' . htmlentities('key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
72         $s .= '<h4>' . DI::l10n()->t('Body for "new photo upload"') . '</h4>';
73         $s .= '<p><code>' . htmlentities('key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
74         $s .= '<h4>' . DI::l10n()->t('Body for "new link post"') . '</h4>';
75         $s .= '<p><code>' . htmlentities('key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
76         $s .= '</div><div class="clear"></div>';
77
78         $s .= '<div id="ifttt-rekey-wrapper">';
79         $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . DI::l10n()->t('Generate new key') . '</label>';
80         $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
81         $s .= '</div><div class="clear"></div>';
82
83         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div>';
84         $s .= '</div>';
85 }
86
87 function ifttt_settings_post()
88 {
89         if (!empty($_POST['ifttt-submit']) && isset($_POST['ifttt-rekey'])) {
90                 DI::pConfig()->delete(local_user(), 'ifttt', 'key');
91         }
92 }
93
94 function ifttt_post(App $a)
95 {
96         if ($a->argc != 2) {
97                 return;
98         }
99
100         $nickname = $a->argv[1];
101
102         $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]);
103         if (!DBA::isResult($user)) {
104                 Logger::log('User ' . $nickname . ' not found.', Logger::DEBUG);
105                 return;
106         }
107
108         $uid = $user['uid'];
109
110         Logger::log('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true), Logger::DEBUG);
111
112         if (!isset($_REQUEST['key'])) {
113                 Logger::log('No key found.');
114                 return;
115         }
116
117         $key = $_REQUEST['key'];
118
119         // Check the key
120         if ($key != DI::pConfig()->get($uid, 'ifttt', 'key')) {
121                 Logger::log('Invalid key for user ' . $uid, Logger::DEBUG);
122                 return;
123         }
124
125         $item = [];
126
127         if (isset($_REQUEST['type'])) {
128                 $item['type'] = $_REQUEST['type'];
129         }
130
131         if (!in_array($item['type'], ['status', 'link', 'photo'])) {
132                 Logger::log('Unknown item type ' . $item['type'], Logger::DEBUG);
133                 return;
134         }
135
136         if (isset($_REQUEST['link'])) {
137                 $item['link'] = trim($_REQUEST['link']);
138         }
139         if (isset($_REQUEST['image'])) {
140                 $item['image'] = trim($_REQUEST['image']);
141         }
142         if (isset($_REQUEST['title'])) {
143                 $item['title'] = trim($_REQUEST['title']);
144         }
145         if (isset($_REQUEST['msg'])) {
146                 $item['msg'] = trim($_REQUEST['msg']);
147         }
148         if (isset($_REQUEST['description'])) {
149                 $item['description'] = trim($_REQUEST['description']);
150         }
151         if (isset($_REQUEST['date'])) {
152                 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
153         }
154         if (isset($_REQUEST['url'])) {
155                 $item['url'] = trim($_REQUEST['url']);
156         }
157
158         if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
159                 $item['msg'] = substr($item['msg'], 3, -3);
160         }
161
162         ifttt_message($uid, $item);
163 }
164
165 function ifttt_message($uid, $item)
166 {
167         $a = DI::app();
168
169         $_SESSION['authenticated'] = true;
170         $_SESSION['uid'] = $uid;
171
172         unset($_REQUEST);
173         $_REQUEST['api_source'] = true;
174         $_REQUEST['profile_uid'] = $uid;
175         $_REQUEST['source'] = 'IFTTT';
176         $_REQUEST['title'] = '';
177         $_REQUEST['body'] = $item['msg'];
178         //$_REQUEST['date'] = $item['date'];
179         //$_REQUEST['uri'] = $item['url'];
180
181         if (!empty($item['url']) && strstr($item['url'], 'facebook.com')) {
182                 $hash = hash('ripemd128', $item['url']);
183                 $_REQUEST['extid'] = Protocol::FACEBOOK;
184                 $_REQUEST['message_id'] = Item::newURI($uid, Protocol::FACEBOOK . ':' . $hash);
185         }
186
187         if ($item['type'] == 'link') {
188                 $data = query_page_info($item['link']);
189
190                 if (isset($item['title']) && (trim($item['title']) != '')) {
191                         $data['title'] = $item['title'];
192                 }
193
194                 if (isset($item['description']) && (trim($item['description']) != '')) {
195                         $data['text'] = $item['description'];
196                 }
197
198                 $_REQUEST['body'] .= add_page_info_data($data);
199         } elseif (($item['type'] == 'photo') && ($item['image'] != '')) {
200                 $_REQUEST['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n";
201         }
202
203         item_post($a);
204 }