5 * Description: Receives a post from https://ifttt.com/ and distributes it.
7 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
9 require_once 'mod/item.php';
11 use Friendica\Core\Hook;
12 use Friendica\Core\L10n;
13 use Friendica\Core\Logger;
14 use Friendica\Core\PConfig;
15 use Friendica\Core\Protocol;
16 use Friendica\Database\DBA;
17 use Friendica\Model\Item;
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');
26 function ifttt_uninstall()
28 Hook::unregister('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
29 Hook::unregister('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
32 function ifttt_module()
37 function ifttt_content()
42 function ifttt_settings(App $a, &$s)
48 $key = PConfig::get(local_user(), 'ifttt', 'key');
51 $key = Strings::getRandomHex(20);
52 PConfig::set(local_user(), 'ifttt', 'key', $key);
55 $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
56 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n::t('IFTTT Mirror') . '</h3>';
58 $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
59 $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
60 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n::t('IFTTT Mirror') . '</h3>';
63 $s .= '<div id="ifttt-configuration-wrapper">';
64 $s .= '<p>' . 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:') . '</p>';
66 $s .= '<p>' . $a->getBaseURL() . '/ifttt/' . $a->user['nickname'] . '</p>';
67 $s .= '<h4>Method</h4>';
69 $s .= '<h4>Content Type</h4>';
70 $s .= '<p>application/x-www-form-urlencoded</p>';
71 $s .= '<h4>' . L10n::t('Body for "new status message"') . '</h4>';
72 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
73 $s .= '<h4>' . L10n::t('Body for "new photo upload"') . '</h4>';
74 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
75 $s .= '<h4>' . L10n::t('Body for "new link post"') . '</h4>';
76 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
77 $s .= '</div><div class="clear"></div>';
79 $s .= '<div id="ifttt-rekey-wrapper">';
80 $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . L10n::t('Generate new key') . '</label>';
81 $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
82 $s .= '</div><div class="clear"></div>';
84 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
88 function ifttt_settings_post()
90 if (!empty($_POST['ifttt-submit']) && isset($_POST['ifttt-rekey'])) {
91 PConfig::delete(local_user(), 'ifttt', 'key');
95 function ifttt_post(App $a)
101 $nickname = $a->argv[1];
103 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]);
104 if (!DBA::isResult($user)) {
105 Logger::log('User ' . $nickname . ' not found.', Logger::DEBUG);
111 Logger::log('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true), Logger::DEBUG);
113 if (!isset($_REQUEST['key'])) {
114 Logger::log('No key found.');
118 $key = $_REQUEST['key'];
121 if ($key != PConfig::get($uid, 'ifttt', 'key')) {
122 Logger::log('Invalid key for user ' . $uid, Logger::DEBUG);
128 if (isset($_REQUEST['type'])) {
129 $item['type'] = $_REQUEST['type'];
132 if (!in_array($item['type'], ['status', 'link', 'photo'])) {
133 Logger::log('Unknown item type ' . $item['type'], Logger::DEBUG);
137 if (isset($_REQUEST['link'])) {
138 $item['link'] = trim($_REQUEST['link']);
140 if (isset($_REQUEST['image'])) {
141 $item['image'] = trim($_REQUEST['image']);
143 if (isset($_REQUEST['title'])) {
144 $item['title'] = trim($_REQUEST['title']);
146 if (isset($_REQUEST['msg'])) {
147 $item['msg'] = trim($_REQUEST['msg']);
149 if (isset($_REQUEST['description'])) {
150 $item['description'] = trim($_REQUEST['description']);
152 if (isset($_REQUEST['date'])) {
153 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
155 if (isset($_REQUEST['url'])) {
156 $item['url'] = trim($_REQUEST['url']);
159 if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
160 $item['msg'] = substr($item['msg'], 3, -3);
163 ifttt_message($uid, $item);
166 function ifttt_message($uid, $item)
170 $_SESSION['authenticated'] = true;
171 $_SESSION['uid'] = $uid;
174 $_REQUEST['api_source'] = true;
175 $_REQUEST['profile_uid'] = $uid;
176 $_REQUEST['source'] = 'IFTTT';
177 $_REQUEST['title'] = '';
178 $_REQUEST['body'] = $item['msg'];
179 //$_REQUEST['date'] = $item['date'];
180 //$_REQUEST['uri'] = $item['url'];
182 if (!empty($item['url']) && strstr($item['url'], 'facebook.com')) {
183 $hash = hash('ripemd128', $item['url']);
184 $_REQUEST['extid'] = Protocol::FACEBOOK;
185 $_REQUEST['message_id'] = Item::newURI($uid, Protocol::FACEBOOK . ':' . $hash);
188 if ($item['type'] == 'link') {
189 $data = query_page_info($item['link']);
191 if (isset($item['title']) && (trim($item['title']) != '')) {
192 $data['title'] = $item['title'];
195 if (isset($item['description']) && (trim($item['description']) != '')) {
196 $data['text'] = $item['description'];
199 $_REQUEST['body'] .= add_page_info_data($data);
200 } elseif (($item['type'] == 'photo') && ($item['image'] != '')) {
201 $_REQUEST['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n";