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