]> git.mxchange.org Git - friendica-addons.git/blob - ifttt/ifttt.php
Changes:
[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 use Friendica\App;
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;
16 use Friendica\DI;
17 use Friendica\Model\Post;
18 use Friendica\Util\Strings;
19
20 function ifttt_install()
21 {
22         Hook::register('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
23         Hook::register('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
24 }
25
26 /**
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
29  * module.
30  */
31 function ifttt_module() {}
32
33 function ifttt_content() {}
34
35 function ifttt_settings(array &$data)
36 {
37         if (!DI::userSession()->getLocalUserId()) {
38                 return;
39         }
40
41         $key = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'ifttt', 'key');
42         if (!$key) {
43                 $key = Strings::getRandomHex(20);
44                 DI::pConfig()->set(DI::userSession()->getLocalUserId(), 'ifttt', 'key', $key);
45         }
46
47         $t    = Renderer::getMarkupTemplate('connector_settings.tpl', 'addon/ifttt/');
48         $html = Renderer::replaceMacros($t, [
49                 '$l10n'                    => [
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"'),
57                 ],
58                 '$url'                     => DI::baseUrl() . '/ifttt/' . DI::userSession()->getLocalUserNickname(),
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}}>>>',
62         ]);
63
64         $data = [
65                 'connector' => 'ifttt',
66                 'title'     => DI::l10n()->t('IFTTT Mirror'),
67                 'image'     => 'addon/ifttt/ifttt.png',
68                 'html'      => $html,
69                 'submit'    => DI::l10n()->t('Generate new key'),
70         ];
71 }
72
73 function ifttt_settings_post()
74 {
75         if (!empty($_POST['ifttt-submit'])) {
76                 DI::pConfig()->delete(DI::userSession()->getLocalUserId(), 'ifttt', 'key');
77         }
78 }
79
80 function ifttt_post()
81 {
82         if (DI::args()->getArgc() != 2) {
83                 return;
84         }
85
86         $nickname = DI::args()->getArgv()[1];
87
88         $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]);
89         if (!DBA::isResult($user)) {
90                 Logger::info('User ' . $nickname . ' not found.');
91                 return;
92         }
93
94         $uid = $user['uid'];
95
96         Logger::info('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true));
97
98         if (!isset($_REQUEST['key'])) {
99                 Logger::notice('No key found.');
100                 return;
101         }
102
103         $key = $_REQUEST['key'];
104
105         // Check the key
106         if ($key != DI::pConfig()->get($uid, 'ifttt', 'key')) {
107                 Logger::info('Invalid key for user ' . $uid);
108                 return;
109         }
110
111         $item = [];
112
113         if (isset($_REQUEST['type'])) {
114                 $item['type'] = $_REQUEST['type'];
115         }
116
117         if (!in_array($item['type'], ['status', 'link', 'photo'])) {
118                 Logger::info('Unknown item type ' . $item['type']);
119                 return;
120         }
121
122         if (isset($_REQUEST['link'])) {
123                 $item['link'] = trim($_REQUEST['link']);
124         }
125         if (isset($_REQUEST['image'])) {
126                 $item['image'] = trim($_REQUEST['image']);
127         }
128         if (isset($_REQUEST['title'])) {
129                 $item['title'] = trim($_REQUEST['title']);
130         }
131         if (isset($_REQUEST['msg'])) {
132                 $item['msg'] = trim($_REQUEST['msg']);
133         }
134         if (isset($_REQUEST['description'])) {
135                 $item['description'] = trim($_REQUEST['description']);
136         }
137         if (isset($_REQUEST['date'])) {
138                 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
139         }
140         if (isset($_REQUEST['url'])) {
141                 $item['url'] = trim($_REQUEST['url']);
142         }
143
144         if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
145                 $item['msg'] = substr($item['msg'], 3, -3);
146         }
147
148         ifttt_message($uid, $item);
149 }
150
151 function ifttt_message($uid, $item)
152 {
153         $post = [];
154         $post['uid'] = $uid;
155         $post['app'] = 'IFTTT';
156         $post['title'] = '';
157         $post['body'] = $item['msg'];
158         //$post['date'] = $item['date'];
159         //$post['uri'] = $item['url'];
160
161         if ($item['type'] == 'link') {
162                 $link = $item['link'];
163                 $data = PageInfo::queryUrl($item['link']);
164
165                 if (isset($item['title']) && (trim($item['title']) != '')) {
166                         $data['title'] = $item['title'];
167                 }
168
169                 if (isset($item['description']) && (trim($item['description']) != '')) {
170                         $data['text'] = $item['description'];
171                 }
172
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'];
179         } else {
180                 $link = hash('ripemd128', $item['msg']);
181         }
182
183         Post\Delayed::add($link, $post, Worker::PRIORITY_MEDIUM, Post\Delayed::PREPARED);
184 }