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