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\PConfig;
15 use Friendica\Database\DBM;
17 function ifttt_install()
19 register_hook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
20 register_hook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
23 function ifttt_uninstall()
25 unregister_hook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
26 unregister_hook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
29 function ifttt_module()
34 function ifttt_content()
39 function ifttt_settings(App $a, &$s)
45 $key = PConfig::get(local_user(), 'ifttt', 'key');
48 $key = random_string(20);
49 PConfig::set(local_user(), 'ifttt', 'key', $key);
52 $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
53 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . t('IFTTT Mirror') . '</h3>';
55 $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
56 $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
57 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . t('IFTTT Mirror') . '</h3>';
60 $s .= '<div id="ifttt-configuration-wrapper">';
61 $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>';
63 $s .= '<p>' . $a->get_baseurl() . '/ifttt/' . $a->user['nickname'] . '</p>';
64 $s .= '<h4>Method</h4>';
66 $s .= '<h4>Content Type</h4>';
67 $s .= '<p>application/x-www-form-urlencoded</p>';
68 $s .= '<h4>' . t('Body for "new status message"') . '</h4>';
69 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
70 $s .= '<h4>' . t('Body for "new photo upload"') . '</h4>';
71 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
72 $s .= '<h4>' . t('Body for "new link post"') . '</h4>';
73 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
74 $s .= '</div><div class="clear"></div>';
76 $s .= '<div id="ifttt-rekey-wrapper">';
77 $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . t('Generate new key') . '</label>';
78 $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
79 $s .= '</div><div class="clear"></div>';
81 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
85 function ifttt_settings_post()
87 if (x($_POST, 'ifttt-submit') && isset($_POST['ifttt-rekey'])) {
88 PConfig::delete(local_user(), 'ifttt', 'key');
92 function ifttt_post(App $a)
98 $nickname = $a->argv[1];
100 $user = dba::select('user', ['uid'], ['nickname' => $nickname], ['limit' => 1]);
101 if (!DBM::is_result($user)) {
102 logger('User ' . $nickname . ' not found.', LOGGER_DEBUG);
108 logger('Received a post for user ' . $uid . ' from ifttt ' . print_r($_REQUEST, true), LOGGER_DEBUG);
110 if (!isset($_REQUEST['key'])) {
111 logger('No key found.');
115 $key = $_REQUEST['key'];
118 if ($key != PConfig::get($uid, 'ifttt', 'key')) {
119 logger('Invalid key for user ' . $uid, LOGGER_DEBUG);
125 if (isset($_REQUEST['type'])) {
126 $item['type'] = $_REQUEST['type'];
129 if (!in_array($item['type'], array('status', 'link', 'photo'))) {
130 logger('Unknown item type ' . $item['type'], LOGGER_DEBUG);
134 if (isset($_REQUEST['link'])) {
135 $item['link'] = trim($_REQUEST['link']);
137 if (isset($_REQUEST['image'])) {
138 $item['image'] = trim($_REQUEST['image']);
140 if (isset($_REQUEST['title'])) {
141 $item['title'] = trim($_REQUEST['title']);
143 if (isset($_REQUEST['msg'])) {
144 $item['msg'] = trim($_REQUEST['msg']);
146 if (isset($_REQUEST['description'])) {
147 $item['description'] = trim($_REQUEST['description']);
149 if (isset($_REQUEST['date'])) {
150 $item['date'] = date('c', strtotime($date = str_replace(' at ', ', ', $_REQUEST['date'])));
152 if (isset($_REQUEST['url'])) {
153 $item['url'] = trim($_REQUEST['url']);
156 if ((substr($item['msg'], 0, 3) == '<<<') && (substr($item['msg'], -3, 3) == '>>>')) {
157 $item['msg'] = substr($item['msg'], 3, -3);
160 ifttt_message($uid, $item);
163 function ifttt_message($uid, $item)
167 $_SESSION['authenticated'] = true;
168 $_SESSION['uid'] = $uid;
171 $_REQUEST['type'] = 'wall';
172 $_REQUEST['api_source'] = true;
173 $_REQUEST['profile_uid'] = $uid;
174 $_REQUEST['source'] = 'IFTTT';
175 $_REQUEST['title'] = '';
176 $_REQUEST['body'] = $item['msg'];
177 //$_REQUEST['date'] = $item['date'];
178 //$_REQUEST['uri'] = $item['url'];
180 if (strstr($item['url'], 'facebook.com')) {
181 $hash = hash('ripemd128', item['url']);
182 $_REQUEST['extid'] = NETWORK_FACEBOOK;
183 $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_FACEBOOK . ':' . $hash);
186 if ($item['type'] == 'link') {
187 $data = query_page_info($item['link']);
189 if (isset($item['title']) && (trim($item['title']) != '')) {
190 $data['title'] = $item['title'];
193 if (isset($item['description']) && (trim($item['description']) != '')) {
194 $data['text'] = $item['description'];
197 $_REQUEST['body'] .= add_page_info_data($data);
198 } elseif (($item['type'] == 'photo') && ($item['image'] != '')) {
199 $_REQUEST['body'] .= "\n\n[img]" . $item['image'] . "[/img]\n";