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\Core\Renderer;
15 use Friendica\Database\DBA;
17 use Friendica\Model\Item;
18 use Friendica\Model\Post;
19 use Friendica\Util\Strings;
21 function ifttt_install()
23 Hook::register('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
24 Hook::register('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
28 * This is a statement rather than an actual function definition. The simple
29 * existence of this method is checked to figure out if the addon offers a
32 function ifttt_module() {}
34 function ifttt_content() {}
36 function ifttt_settings(App $a, array &$data)
42 $key = DI::pConfig()->get(local_user(), 'ifttt', 'key');
44 $key = Strings::getRandomHex(20);
45 DI::pConfig()->set(local_user(), 'ifttt', 'key', $key);
48 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/ifttt/');
49 $html = Renderer::replaceMacros($t, [
51 'intro' => 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:'),
52 'url' => DI::l10n()->t('URL'),
53 'method' => DI::l10n()->t('Method'),
54 'content_type' => DI::l10n()->t('Content Type'),
55 'new_status_message_body' => DI::l10n()->t('Body for "new status message"'),
56 'new_photo_upload_body' => DI::l10n()->t('Body for "new photo upload"'),
57 'new_link_post_body' => DI::l10n()->t('Body for "new link post"'),
59 '$url' => DI::baseUrl()->get() . '/ifttt/' . $a->getLoggedInUserNickname(),
60 '$new_status_message_body' => 'key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>',
61 '$new_photo_upload_body' => 'key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>',
62 '$new_link_post_body' => 'key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>',
66 'connector' => 'ifttt',
67 'title' => DI::l10n()->t('IFTTT Mirror'),
68 'image' => 'addon/ifttt/ifttt.png',
70 'submit' => DI::l10n()->t('Generate new key'),
74 function ifttt_settings_post()
76 if (!empty($_POST['ifttt-submit'])) {
77 DI::pConfig()->delete(local_user(), 'ifttt', 'key');
81 function ifttt_post(App $a)
83 if (DI::args()->getArgc() != 2) {
87 $nickname = DI::args()->getArgv()[1];
89 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]);
90 if (!DBA::isResult($user)) {
91 Logger::info('User ' . $nickname . ' not found.');
97 Logger::info('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true));
99 if (!isset($_REQUEST['key'])) {
100 Logger::notice('No key found.');
104 $key = $_REQUEST['key'];
107 if ($key != DI::pConfig()->get($uid, 'ifttt', 'key')) {
108 Logger::info('Invalid key for user ' . $uid);
114 if (isset($_REQUEST['type'])) {
115 $item['type'] = $_REQUEST['type'];
118 if (!in_array($item['type'], ['status', 'link', 'photo'])) {
119 Logger::info('Unknown item type ' . $item['type']);
123 if (isset($_REQUEST['link'])) {
124 $item['link'] = trim($_REQUEST['link']);
126 if (isset($_REQUEST['image'])) {
127 $item['image'] = trim($_REQUEST['image']);
129 if (isset($_REQUEST['title'])) {
130 $item['title'] = trim($_REQUEST['title']);
132 if (isset($_REQUEST['msg'])) {
133 $item['msg'] = trim($_REQUEST['msg']);
135 if (isset($_REQUEST['description'])) {
136 $item['description'] = trim($_REQUEST['description']);
138 if (isset($_REQUEST['date'])) {
139 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
141 if (isset($_REQUEST['url'])) {
142 $item['url'] = trim($_REQUEST['url']);
145 if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
146 $item['msg'] = substr($item['msg'], 3, -3);
149 ifttt_message($uid, $item);
152 function ifttt_message($uid, $item)
158 $post['app'] = 'IFTTT';
160 $post['body'] = $item['msg'];
161 //$post['date'] = $item['date'];
162 //$post['uri'] = $item['url'];
164 if ($item['type'] == 'link') {
165 $link = $item['link'];
166 $data = PageInfo::queryUrl($item['link']);
168 if (isset($item['title']) && (trim($item['title']) != '')) {
169 $data['title'] = $item['title'];
172 if (isset($item['description']) && (trim($item['description']) != '')) {
173 $data['text'] = $item['description'];
176 $post['body'] .= "\n" . PageInfo::getFooterFromData($data);
177 } elseif (($item['type'] == 'photo') && ($item['image'] != '')) {
178 $link = $item['image'];
179 $post['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n";
180 } elseif (!empty($item['url'])) {
181 $link = $item['url'];
183 $link = hash('ripemd128', $item['msg']);
186 Post\Delayed::add($link, $post, PRIORITY_MEDIUM, Post\Delayed::UNPREPARED);