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';
10 require_once 'include/items.php';
11 require_once 'include/text.php';
14 use Friendica\Core\Addon;
15 use Friendica\Core\L10n;
16 use Friendica\Core\Logger;
17 use Friendica\Core\PConfig;
18 use Friendica\Core\Protocol;
19 use Friendica\Database\DBA;
20 use Friendica\Model\Item;
21 use Friendica\Util\Strings;
23 function ifttt_install()
25 Addon::registerHook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
26 Addon::registerHook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
29 function ifttt_uninstall()
31 Addon::unregisterHook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
32 Addon::unregisterHook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
35 function ifttt_module()
40 function ifttt_content()
45 function ifttt_settings(App $a, &$s)
51 $key = PConfig::get(local_user(), 'ifttt', 'key');
54 $key = Strings::getRandomHex(20);
55 PConfig::set(local_user(), 'ifttt', 'key', $key);
58 $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
59 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n::t('IFTTT Mirror') . '</h3>';
61 $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
62 $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
63 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n::t('IFTTT Mirror') . '</h3>';
66 $s .= '<div id="ifttt-configuration-wrapper">';
67 $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>';
69 $s .= '<p>' . $a->getBaseURL() . '/ifttt/' . $a->user['nickname'] . '</p>';
70 $s .= '<h4>Method</h4>';
72 $s .= '<h4>Content Type</h4>';
73 $s .= '<p>application/x-www-form-urlencoded</p>';
74 $s .= '<h4>' . L10n::t('Body for "new status message"') . '</h4>';
75 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
76 $s .= '<h4>' . L10n::t('Body for "new photo upload"') . '</h4>';
77 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
78 $s .= '<h4>' . L10n::t('Body for "new link post"') . '</h4>';
79 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
80 $s .= '</div><div class="clear"></div>';
82 $s .= '<div id="ifttt-rekey-wrapper">';
83 $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . L10n::t('Generate new key') . '</label>';
84 $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
85 $s .= '</div><div class="clear"></div>';
87 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
91 function ifttt_settings_post()
93 if (x($_POST, 'ifttt-submit') && isset($_POST['ifttt-rekey'])) {
94 PConfig::delete(local_user(), 'ifttt', 'key');
98 function ifttt_post(App $a)
104 $nickname = $a->argv[1];
106 $user = DBA::selectFirst('user', ['uid'], ['nickname' => $nickname]);
107 if (!DBA::isResult($user)) {
108 Logger::log('User ' . $nickname . ' not found.', Logger::DEBUG);
114 Logger::log('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true), Logger::DEBUG);
116 if (!isset($_REQUEST['key'])) {
117 Logger::log('No key found.');
121 $key = $_REQUEST['key'];
124 if ($key != PConfig::get($uid, 'ifttt', 'key')) {
125 Logger::log('Invalid key for user ' . $uid, Logger::DEBUG);
131 if (isset($_REQUEST['type'])) {
132 $item['type'] = $_REQUEST['type'];
135 if (!in_array($item['type'], ['status', 'link', 'photo'])) {
136 Logger::log('Unknown item type ' . $item['type'], Logger::DEBUG);
140 if (isset($_REQUEST['link'])) {
141 $item['link'] = trim($_REQUEST['link']);
143 if (isset($_REQUEST['image'])) {
144 $item['image'] = trim($_REQUEST['image']);
146 if (isset($_REQUEST['title'])) {
147 $item['title'] = trim($_REQUEST['title']);
149 if (isset($_REQUEST['msg'])) {
150 $item['msg'] = trim($_REQUEST['msg']);
152 if (isset($_REQUEST['description'])) {
153 $item['description'] = trim($_REQUEST['description']);
155 if (isset($_REQUEST['date'])) {
156 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
158 if (isset($_REQUEST['url'])) {
159 $item['url'] = trim($_REQUEST['url']);
162 if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
163 $item['msg'] = substr($item['msg'], 3, -3);
166 ifttt_message($uid, $item);
169 function ifttt_message($uid, $item)
173 $_SESSION['authenticated'] = true;
174 $_SESSION['uid'] = $uid;
177 $_REQUEST['api_source'] = true;
178 $_REQUEST['profile_uid'] = $uid;
179 $_REQUEST['source'] = 'IFTTT';
180 $_REQUEST['title'] = '';
181 $_REQUEST['body'] = $item['msg'];
182 //$_REQUEST['date'] = $item['date'];
183 //$_REQUEST['uri'] = $item['url'];
185 if (!empty($item['url']) && strstr($item['url'], 'facebook.com')) {
186 $hash = hash('ripemd128', $item['url']);
187 $_REQUEST['extid'] = Protocol::FACEBOOK;
188 $_REQUEST['message_id'] = Item::newURI($uid, Protocol::FACEBOOK . ':' . $hash);
191 if ($item['type'] == 'link') {
192 $data = query_page_info($item['link']);
194 if (isset($item['title']) && (trim($item['title']) != '')) {
195 $data['title'] = $item['title'];
198 if (isset($item['description']) && (trim($item['description']) != '')) {
199 $data['text'] = $item['description'];
202 $_REQUEST['body'] .= add_page_info_data($data);
203 } elseif (($item['type'] == 'photo') && ($item['image'] != '')) {
204 $_REQUEST['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n";