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