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\PConfig;
16 use Friendica\Database\DBM;
18 function ifttt_install()
20 Addon::registerHook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
21 Addon::registerHook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
24 function ifttt_uninstall()
26 Addon::unregisterHook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
27 Addon::unregisterHook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
30 function ifttt_module()
35 function ifttt_content()
40 function ifttt_settings(App $a, &$s)
46 $key = PConfig::get(local_user(), 'ifttt', 'key');
49 $key = random_string(20);
50 PConfig::set(local_user(), 'ifttt', 'key', $key);
53 $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
54 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . t('IFTTT Mirror') . '</h3>';
56 $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
57 $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
58 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . t('IFTTT Mirror') . '</h3>';
61 $s .= '<div id="ifttt-configuration-wrapper">';
62 $s .= '<p>' . 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>';
64 $s .= '<p>' . $a->get_baseurl() . '/ifttt/' . $a->user['nickname'] . '</p>';
65 $s .= '<h4>Method</h4>';
67 $s .= '<h4>Content Type</h4>';
68 $s .= '<p>application/x-www-form-urlencoded</p>';
69 $s .= '<h4>' . t('Body for "new status message"') . '</h4>';
70 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
71 $s .= '<h4>' . t('Body for "new photo upload"') . '</h4>';
72 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
73 $s .= '<h4>' . t('Body for "new link post"') . '</h4>';
74 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
75 $s .= '</div><div class="clear"></div>';
77 $s .= '<div id="ifttt-rekey-wrapper">';
78 $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . t('Generate new key') . '</label>';
79 $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
80 $s .= '</div><div class="clear"></div>';
82 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
86 function ifttt_settings_post()
88 if (x($_POST, 'ifttt-submit') && isset($_POST['ifttt-rekey'])) {
89 PConfig::delete(local_user(), 'ifttt', 'key');
93 function ifttt_post(App $a)
99 $nickname = $a->argv[1];
101 $user = dba::selectFirst('user', ['uid'], ['nickname' => $nickname]);
102 if (!DBM::is_result($user)) {
103 logger('User ' . $nickname . ' not found.', LOGGER_DEBUG);
109 logger('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true), LOGGER_DEBUG);
111 if (!isset($_REQUEST['key'])) {
112 logger('No key found.');
116 $key = $_REQUEST['key'];
119 if ($key != PConfig::get($uid, 'ifttt', 'key')) {
120 logger('Invalid key for user ' . $uid, LOGGER_DEBUG);
126 if (isset($_REQUEST['type'])) {
127 $item['type'] = $_REQUEST['type'];
130 if (!in_array($item['type'], ['status', 'link', 'photo'])) {
131 logger('Unknown item type ' . $item['type'], LOGGER_DEBUG);
135 if (isset($_REQUEST['link'])) {
136 $item['link'] = trim($_REQUEST['link']);
138 if (isset($_REQUEST['image'])) {
139 $item['image'] = trim($_REQUEST['image']);
141 if (isset($_REQUEST['title'])) {
142 $item['title'] = trim($_REQUEST['title']);
144 if (isset($_REQUEST['msg'])) {
145 $item['msg'] = trim($_REQUEST['msg']);
147 if (isset($_REQUEST['description'])) {
148 $item['description'] = trim($_REQUEST['description']);
150 if (isset($_REQUEST['date'])) {
151 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
153 if (isset($_REQUEST['url'])) {
154 $item['url'] = trim($_REQUEST['url']);
157 if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
158 $item['msg'] = substr($item['msg'], 3, -3);
161 ifttt_message($uid, $item);
164 function ifttt_message($uid, $item)
168 $_SESSION['authenticated'] = true;
169 $_SESSION['uid'] = $uid;
172 $_REQUEST['type'] = 'wall';
173 $_REQUEST['api_source'] = true;
174 $_REQUEST['profile_uid'] = $uid;
175 $_REQUEST['source'] = 'IFTTT';
176 $_REQUEST['title'] = '';
177 $_REQUEST['body'] = $item['msg'];
178 //$_REQUEST['date'] = $item['date'];
179 //$_REQUEST['uri'] = $item['url'];
181 if (strstr($item['url'], 'facebook.com')) {
182 $hash = hash('ripemd128', item['url']);
183 $_REQUEST['extid'] = NETWORK_FACEBOOK;
184 $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_FACEBOOK . ':' . $hash);
187 if ($item['type'] == 'link') {
188 $data = query_page_info($item['link']);
190 if (isset($item['title']) && (trim($item['title']) != '')) {
191 $data['title'] = $item['title'];
194 if (isset($item['description']) && (trim($item['description']) != '')) {
195 $data['text'] = $item['description'];
198 $_REQUEST['body'] .= add_page_info_data($data);
199 } elseif (($item['type'] == 'photo') && ($item['image'] != '')) {
200 $_REQUEST['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n";