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