4 * Description: Receives a post from https://ifttt.com/ and distributes it.
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
8 require_once 'mod/item.php';
9 require_once 'include/items.php';
10 require_once 'include/text.php';
13 use Friendica\Core\Addon;
14 use Friendica\Core\L10n;
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() {
33 function ifttt_content(&$a) {
36 function ifttt_settings(&$a,&$s) {
41 $key = get_pconfig(local_user(),'ifttt','key');
44 $key = substr(random_string(),0,20);
45 set_pconfig(local_user(),'ifttt','key', $key);
48 $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
49 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n::t('IFTTT Mirror') . '</h3>';
51 $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
52 $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
53 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">' . L10n::t('IFTTT Mirror') . '</h3>';
56 $s .= '<div id="ifttt-configuration-wrapper">';
57 $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>';
59 $s .= '<p>' . $a->get_baseurl() . '/ifttt/' . $a->user['nickname'] . '</p>';
60 $s .= '<h4>Method</h4>';
62 $s .= '<h4>Content Type</h4>';
63 $s .= '<p>application/x-www-form-urlencoded</p>';
64 $s .= '<h4>' . L10n::t('Body for "new status message"') . '</h4>';
65 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
66 $s .= '<h4>' . L10n::t('Body for "new photo upload"') . '</h4>';
67 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
68 $s .= '<h4>' . L10n::t('Body for "new link post"') . '</h4>';
69 $s .= '<p><code>' . htmlentities('key=' . $key . '&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>') . '</code></p>';
70 $s .= '</div><div class="clear"></div>';
72 $s .= '<div id="ifttt-rekey-wrapper">';
73 $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . L10n::t('Generate new key') . '</label>';
74 $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
75 $s .= '</div><div class="clear"></div>';
77 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
81 function ifttt_settings_post(&$a,&$b) {
83 if(x($_POST,'ifttt-submit'))
84 if (isset($_POST['ifttt-rekey']))
85 del_pconfig(local_user(), 'ifttt', 'key');
88 function ifttt_post(&$a) {
94 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($user));
96 logger("User ".$user." not found.", LOGGER_DEBUG);
102 logger("Received a post for user ".$uid." from ifttt ".print_r($_REQUEST, true), LOGGER_DEBUG);
104 if (!isset($_REQUEST["key"])) {
105 logger("No key found.");
109 $key = $_REQUEST["key"];
112 if ($key != get_pconfig($uid,'ifttt','key')) {
113 logger("Invalid key for user ".$uid, LOGGER_DEBUG);
119 if (isset($_REQUEST["type"]))
120 $item["type"] = $_REQUEST["type"];
122 if (!in_array($item["type"], array("status", "link", "photo"))) {
123 logger("Unknown item type ".$item["type"], LOGGER_DEBUG);
127 if (isset($_REQUEST["link"]))
128 $item["link"] = trim($_REQUEST["link"]);
129 if (isset($_REQUEST["image"]))
130 $item["image"] = trim($_REQUEST["image"]);
131 if (isset($_REQUEST["title"]))
132 $item["title"] = trim($_REQUEST["title"]);
133 if (isset($_REQUEST["msg"]))
134 $item["msg"] = trim($_REQUEST["msg"]);
135 if (isset($_REQUEST["description"]))
136 $item["description"] = trim($_REQUEST["description"]);
137 if (isset($_REQUEST["date"]))
138 $item["date"] = date("c", strtotime($date = str_replace(" at ", ", ", $_REQUEST["date"])));
139 if (isset($_REQUEST["url"]))
140 $item["url"] = trim($_REQUEST["url"]);
142 if ((substr($item["msg"], 0, 3) == "<<<") && (substr($item["msg"], -3, 3) == ">>>"))
143 $item["msg"] = substr($item["msg"], 3, -3);
145 ifttt_message($uid, $item);
148 function ifttt_message($uid, $item) {
152 $_SESSION["authenticated"] = true;
153 $_SESSION["uid"] = $uid;
156 $_REQUEST["type"] = "wall";
157 $_REQUEST["api_source"] = true;
158 $_REQUEST["profile_uid"] = $uid;
159 $_REQUEST["source"] = "IFTTT";
160 $_REQUEST["title"] = "";
161 $_REQUEST["body"] = $item["msg"];
162 //$_REQUEST["date"] = $item["date"];
163 //$_REQUEST["uri"] = $item["url"];
165 if (strstr($item["url"], "facebook.com")) {
166 $hash = hash("ripemd128", item["url"]);
167 $_REQUEST["extid"] = NETWORK_FACEBOOK;
168 $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_FACEBOOK.":".$hash);
171 if ($item["type"] == "link") {
172 $data = query_page_info($item["link"]);
174 if (isset($item["title"]) && (trim($item["title"]) != ""))
175 $data["title"] = $item["title"];
177 if (isset($item["description"]) && (trim($item["description"]) != ""))
178 $data["text"] = $item["description"];
180 $_REQUEST["body"] .= add_page_info_data($data);
181 } elseif (($item["type"] == "photo") && ($item["image"] != ""))
182 $_REQUEST["body"] .= "\n\n[img]".$item["image"]."[/img]\n";
184 //print_r($_REQUEST);