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