5 * Description: Receives a post from https://ifttt.com/ and distributes it.
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
10 use Friendica\Content\PageInfo;
11 use Friendica\Core\Hook;
12 use Friendica\Core\Logger;
13 use Friendica\Core\Protocol;
14 use Friendica\Database\DBA;
16 use Friendica\Model\Item;
17 use Friendica\Model\Post;
18 use Friendica\Util\Strings;
20 function ifttt_install()
22 Hook::register('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
23 Hook::register('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
26 function ifttt_module()
31 function ifttt_content()
36 function ifttt_settings(App $a, &$s)
42 $key = DI::pConfig()->get(local_user(), 'ifttt', 'key');
45 $key = Strings::getRandomHex(20);
46 DI::pConfig()->set(local_user(), 'ifttt', 'key', $key);
49 $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
50 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . DI::l10n()->t('IFTTT Mirror') . '</h3>';
52 $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
53 $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
54 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . DI::l10n()->t('IFTTT Mirror') . '</h3>';
57 $s .= '<div id="ifttt-configuration-wrapper">';
58 $s .= '<p>' . DI::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>';
60 $s .= '<p>' . DI::baseUrl()->get() . '/ifttt/' . $a->user['nickname'] . '</p>';
61 $s .= '<h4>Method</h4>';
63 $s .= '<h4>Content Type</h4>';
64 $s .= '<p>application/x-www-form-urlencoded</p>';
65 $s .= '<h4>' . DI::l10n()->t('Body for "new status message"') . '</h4>';
66 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
67 $s .= '<h4>' . DI::l10n()->t('Body for "new photo upload"') . '</h4>';
68 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
69 $s .= '<h4>' . DI::l10n()->t('Body for "new link post"') . '</h4>';
70 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
71 $s .= '</div><div class="clear"></div>';
73 $s .= '<div id="ifttt-rekey-wrapper">';
74 $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . DI::l10n()->t('Generate new key') . '</label>';
75 $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
76 $s .= '</div><div class="clear"></div>';
78 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . DI::l10n()->t('Save Settings') . '" /></div>';
82 function ifttt_settings_post()
84 if (!empty($_POST['ifttt-submit']) && isset($_POST['ifttt-rekey'])) {
85 DI::pConfig()->delete(local_user(), 'ifttt', 'key');
89 function ifttt_post(App $a)
95 $nickname = $a->argv[1];
97 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]);
98 if (!DBA::isResult($user)) {
99 Logger::log('User ' . $nickname . ' not found.', Logger::DEBUG);
105 Logger::log('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true), Logger::DEBUG);
107 if (!isset($_REQUEST['key'])) {
108 Logger::log('No key found.');
112 $key = $_REQUEST['key'];
115 if ($key != DI::pConfig()->get($uid, 'ifttt', 'key')) {
116 Logger::log('Invalid key for user ' . $uid, Logger::DEBUG);
122 if (isset($_REQUEST['type'])) {
123 $item['type'] = $_REQUEST['type'];
126 if (!in_array($item['type'], ['status', 'link', 'photo'])) {
127 Logger::log('Unknown item type ' . $item['type'], Logger::DEBUG);
131 if (isset($_REQUEST['link'])) {
132 $item['link'] = trim($_REQUEST['link']);
134 if (isset($_REQUEST['image'])) {
135 $item['image'] = trim($_REQUEST['image']);
137 if (isset($_REQUEST['title'])) {
138 $item['title'] = trim($_REQUEST['title']);
140 if (isset($_REQUEST['msg'])) {
141 $item['msg'] = trim($_REQUEST['msg']);
143 if (isset($_REQUEST['description'])) {
144 $item['description'] = trim($_REQUEST['description']);
146 if (isset($_REQUEST['date'])) {
147 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
149 if (isset($_REQUEST['url'])) {
150 $item['url'] = trim($_REQUEST['url']);
153 if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
154 $item['msg'] = substr($item['msg'], 3, -3);
157 ifttt_message($uid, $item);
160 function ifttt_message($uid, $item)
166 $post['app'] = 'IFTTT';
168 $post['body'] = $item['msg'];
169 //$post['date'] = $item['date'];
170 //$post['uri'] = $item['url'];
172 if ($item['type'] == 'link') {
173 $link = $item['link'];
174 $data = PageInfo::queryUrl($item['link']);
176 if (isset($item['title']) && (trim($item['title']) != '')) {
177 $data['title'] = $item['title'];
180 if (isset($item['description']) && (trim($item['description']) != '')) {
181 $data['text'] = $item['description'];
184 $post['body'] .= "\n" . PageInfo::getFooterFromData($data);
185 } elseif (($item['type'] == 'photo') && ($item['image'] != '')) {
186 $link = $item['image'];
187 $post['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n";
188 } elseif (!empty($item['url'])) {
189 $link = $item['url'];
191 $link = hash('ripemd128', $item['msg']);
194 Post\Delayed::add($link, $post, PRIORITY_MEDIUM, true);