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