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\Renderer;
14 use Friendica\Core\Worker;
15 use Friendica\Database\DBA;
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');
27 * This is a statement rather than an actual function definition. The simple
28 * existence of this method is checked to figure out if the addon offers a
31 function ifttt_module() {}
33 function ifttt_content() {}
35 function ifttt_settings(App $a, array &$data)
37 if (!DI::userSession()->getLocalUserId()) {
41 $key = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ifttt', 'key');
43 $key = Strings::getRandomHex(20);
44 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'ifttt', 'key', $key);
47 $t = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/ifttt/');
48 $html = Renderer::replaceMacros($t, [
50 '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:'),
51 'url' => DI::l10n()->t('URL'),
52 'method' => DI::l10n()->t('Method'),
53 'content_type' => DI::l10n()->t('Content Type'),
54 'new_status_message_body' => DI::l10n()->t('Body for "new status message"'),
55 'new_photo_upload_body' => DI::l10n()->t('Body for "new photo upload"'),
56 'new_link_post_body' => DI::l10n()->t('Body for "new link post"'),
58 '$url' => DI::baseUrl()->get() . '/ifttt/' . $a->getLoggedInUserNickname(),
59 '$new_status_message_body' => 'key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>',
60 '$new_photo_upload_body' => 'key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>',
61 '$new_link_post_body' => 'key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>',
65 'connector' => 'ifttt',
66 'title' => DI::l10n()->t('IFTTT Mirror'),
67 'image' => 'addon/ifttt/ifttt.png',
69 'submit' => DI::l10n()->t('Generate new key'),
73 function ifttt_settings_post()
75 if (!empty($_POST['ifttt-submit'])) {
76 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'ifttt', 'key');
80 function ifttt_post(App $a)
82 if (DI::args()->getArgc() != 2) {
86 $nickname = DI::args()->getArgv()[1];
88 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]);
89 if (!DBA::isResult($user)) {
90 Logger::info('User ' . $nickname . ' not found.');
96 Logger::info('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true));
98 if (!isset($_REQUEST['key'])) {
99 Logger::notice('No key found.');
103 $key = $_REQUEST['key'];
106 if ($key != DI::pConfig()->get($uid, 'ifttt', 'key')) {
107 Logger::info('Invalid key for user ' . $uid);
113 if (isset($_REQUEST['type'])) {
114 $item['type'] = $_REQUEST['type'];
117 if (!in_array($item['type'], ['status', 'link', 'photo'])) {
118 Logger::info('Unknown item type ' . $item['type']);
122 if (isset($_REQUEST['link'])) {
123 $item['link'] = trim($_REQUEST['link']);
125 if (isset($_REQUEST['image'])) {
126 $item['image'] = trim($_REQUEST['image']);
128 if (isset($_REQUEST['title'])) {
129 $item['title'] = trim($_REQUEST['title']);
131 if (isset($_REQUEST['msg'])) {
132 $item['msg'] = trim($_REQUEST['msg']);
134 if (isset($_REQUEST['description'])) {
135 $item['description'] = trim($_REQUEST['description']);
137 if (isset($_REQUEST['date'])) {
138 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
140 if (isset($_REQUEST['url'])) {
141 $item['url'] = trim($_REQUEST['url']);
144 if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
145 $item['msg'] = substr($item['msg'], 3, -3);
148 ifttt_message($uid, $item);
151 function ifttt_message($uid, $item)
155 $post['app'] = 'IFTTT';
157 $post['body'] = $item['msg'];
158 //$post['date'] = $item['date'];
159 //$post['uri'] = $item['url'];
161 if ($item['type'] == 'link') {
162 $link = $item['link'];
163 $data = PageInfo::queryUrl($item['link']);
165 if (isset($item['title']) && (trim($item['title']) != '')) {
166 $data['title'] = $item['title'];
169 if (isset($item['description']) && (trim($item['description']) != '')) {
170 $data['text'] = $item['description'];
173 $post['body'] .= "\n" . PageInfo::getFooterFromData($data);
174 } elseif (($item['type'] == 'photo') && ($item['image'] != '')) {
175 $link = $item['image'];
176 $post['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n";
177 } elseif (!empty($item['url'])) {
178 $link = $item['url'];
180 $link = hash('ripemd128', $item['msg']);
183 Post\Delayed::add($link, $post, Worker::PRIORITY_MEDIUM, Post\Delayed::PREPARED);