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