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