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\Core\Worker;
16 use Friendica\Database\DBA;
18 use Friendica\Model\Item;
19 use Friendica\Model\Post;
20 use Friendica\Util\Strings;
22 function ifttt_install()
24 Hook::register('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
25 Hook::register('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
29 * This is a statement rather than an actual function definition. The simple
30 * existence of this method is checked to figure out if the addon offers a
33 function ifttt_module() {}
35 function ifttt_content() {}
37 function ifttt_settings(App $a, array &$data)
39 if (!DI::userSession()->getLocalUserId()) {
43 $key = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ifttt', 'key');
45 $key = Strings::getRandomHex(20);
46 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'ifttt', 'key', $key);
49 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/ifttt/');
50 $html = Renderer::replaceMacros($t, [
52 '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:'),
53 'url' => DI::l10n()->t('URL'),
54 'method' => DI::l10n()->t('Method'),
55 'content_type' => DI::l10n()->t('Content Type'),
56 'new_status_message_body' => DI::l10n()->t('Body for "new status message"'),
57 'new_photo_upload_body' => DI::l10n()->t('Body for "new photo upload"'),
58 'new_link_post_body' => DI::l10n()->t('Body for "new link post"'),
60 '$url' => DI::baseUrl()->get() . '/ifttt/' . $a->getLoggedInUserNickname(),
61 '$new_status_message_body' => 'key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>',
62 '$new_photo_upload_body' => 'key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>',
63 '$new_link_post_body' => 'key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>',
67 'connector' => 'ifttt',
68 'title' => DI::l10n()->t('IFTTT Mirror'),
69 'image' => 'addon/ifttt/ifttt.png',
71 'submit' => DI::l10n()->t('Generate new key'),
75 function ifttt_settings_post()
77 if (!empty($_POST['ifttt-submit'])) {
78 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'ifttt', 'key');
82 function ifttt_post(App $a)
84 if (DI::args()->getArgc() != 2) {
88 $nickname = DI::args()->getArgv()[1];
90 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]);
91 if (!DBA::isResult($user)) {
92 Logger::info('User ' . $nickname . ' not found.');
98 Logger::info('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true));
100 if (!isset($_REQUEST['key'])) {
101 Logger::notice('No key found.');
105 $key = $_REQUEST['key'];
108 if ($key != DI::pConfig()->get($uid, 'ifttt', 'key')) {
109 Logger::info('Invalid key for user ' . $uid);
115 if (isset($_REQUEST['type'])) {
116 $item['type'] = $_REQUEST['type'];
119 if (!in_array($item['type'], ['status', 'link', 'photo'])) {
120 Logger::info('Unknown item type ' . $item['type']);
124 if (isset($_REQUEST['link'])) {
125 $item['link'] = trim($_REQUEST['link']);
127 if (isset($_REQUEST['image'])) {
128 $item['image'] = trim($_REQUEST['image']);
130 if (isset($_REQUEST['title'])) {
131 $item['title'] = trim($_REQUEST['title']);
133 if (isset($_REQUEST['msg'])) {
134 $item['msg'] = trim($_REQUEST['msg']);
136 if (isset($_REQUEST['description'])) {
137 $item['description'] = trim($_REQUEST['description']);
139 if (isset($_REQUEST['date'])) {
140 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
142 if (isset($_REQUEST['url'])) {
143 $item['url'] = trim($_REQUEST['url']);
146 if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
147 $item['msg'] = substr($item['msg'], 3, -3);
150 ifttt_message($uid, $item);
153 function ifttt_message($uid, $item)
159 $post['app'] = 'IFTTT';
161 $post['body'] = $item['msg'];
162 //$post['date'] = $item['date'];
163 //$post['uri'] = $item['url'];
165 if ($item['type'] == 'link') {
166 $link = $item['link'];
167 $data = PageInfo::queryUrl($item['link']);
169 if (isset($item['title']) && (trim($item['title']) != '')) {
170 $data['title'] = $item['title'];
173 if (isset($item['description']) && (trim($item['description']) != '')) {
174 $data['text'] = $item['description'];
177 $post['body'] .= "\n" . PageInfo::getFooterFromData($data);
178 } elseif (($item['type'] == 'photo') && ($item['image'] != '')) {
179 $link = $item['image'];
180 $post['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n";
181 } elseif (!empty($item['url'])) {
182 $link = $item['url'];
184 $link = hash('ripemd128', $item['msg']);
187 Post\Delayed::add($link, $post, Worker::PRIORITY_MEDIUM, Post\Delayed::UNPREPARED);