4 * Description: Receives a post from https://ifttt.com/ and distributes it.
6 * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
9 require_once("mod/item.php");
10 require_once("include/items.php");
12 use Friendica\Core\PConfig;
14 function ifttt_install() {
15 register_hook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
16 register_hook('connector_settings_post','addon/ifttt/ifttt.php', 'ifttt_settings_post');
19 function ifttt_uninstall() {
20 unregister_hook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
21 unregister_hook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
24 function ifttt_module() {
27 function ifttt_content(&$a) {
30 function ifttt_settings(&$a,&$s) {
35 $key = PConfig::get(local_user(),'ifttt','key');
38 $key = substr(random_string(),0,20);
39 PConfig::set(local_user(),'ifttt','key', $key);
42 $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
43 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">'. t('IFTTT Mirror').'</h3>';
45 $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
46 $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
47 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">'. t('IFTTT Mirror').'</h3>';
50 $s .= '<div id="ifttt-configuration-wrapper">';
51 $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>';
53 $s .= '<p>' . $a->get_baseurl() . '/ifttt/' . $a->user['nickname'] . '</p>';
54 $s .= '<h4>Method</h4>';
56 $s .= '<h4>Content Type</h4>';
57 $s .= '<p>application/x-www-form-urlencoded</p>';
58 $s .= '<h4>'.t("Body for 'new status message'").'</h4>';
59 $s .= '<p><code>'.htmlentities('key='.$key.'&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>').'</code></p>';
60 $s .= '<h4>'.t("Body for 'new photo upload'").'</h4>';
61 $s .= '<p><code>'.htmlentities('key='.$key.'&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>').'</code></p>';
62 $s .= '<h4>'.t("Body for 'new link post'").'</h4>';
63 $s .= '<p><code>'.htmlentities('key='.$key.'&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>').'</code></p>';
64 $s .= '</div><div class="clear"></div>';
66 $s .= '<div id="ifttt-rekey-wrapper">';
67 $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . t('Generate new key') . '</label>';
68 $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
69 $s .= '</div><div class="clear"></div>';
71 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
76 function ifttt_settings_post(&$a,&$b) {
78 if(x($_POST,'ifttt-submit'))
79 if (isset($_POST['ifttt-rekey']))
80 PConfig::delete(local_user(), 'ifttt', 'key');
83 function ifttt_post(&$a) {
89 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($user));
91 logger("User ".$user." not found.", LOGGER_DEBUG);
97 logger("Received a post for user ".$uid." from ifttt ".print_r($_REQUEST, true), LOGGER_DEBUG);
99 if (!isset($_REQUEST["key"])) {
100 logger("No key found.");
104 $key = $_REQUEST["key"];
107 if ($key != PConfig::get($uid,'ifttt','key')) {
108 logger("Invalid key for user ".$uid, LOGGER_DEBUG);
114 if (isset($_REQUEST["type"]))
115 $item["type"] = $_REQUEST["type"];
117 if (!in_array($item["type"], array("status", "link", "photo"))) {
118 logger("Unknown item type ".$item["type"], LOGGER_DEBUG);
122 if (isset($_REQUEST["link"]))
123 $item["link"] = trim($_REQUEST["link"]);
124 if (isset($_REQUEST["image"]))
125 $item["image"] = trim($_REQUEST["image"]);
126 if (isset($_REQUEST["title"]))
127 $item["title"] = trim($_REQUEST["title"]);
128 if (isset($_REQUEST["msg"]))
129 $item["msg"] = trim($_REQUEST["msg"]);
130 if (isset($_REQUEST["description"]))
131 $item["description"] = trim($_REQUEST["description"]);
132 if (isset($_REQUEST["date"]))
133 $item["date"] = date("c", strtotime($date = str_replace(" at ", ", ", $_REQUEST["date"])));
134 if (isset($_REQUEST["url"]))
135 $item["url"] = trim($_REQUEST["url"]);
137 if ((substr($item["msg"], 0, 3) == "<<<") && (substr($item["msg"], -3, 3) == ">>>"))
138 $item["msg"] = substr($item["msg"], 3, -3);
140 ifttt_message($uid, $item);
143 function ifttt_message($uid, $item) {
147 $_SESSION["authenticated"] = true;
148 $_SESSION["uid"] = $uid;
151 $_REQUEST["type"] = "wall";
152 $_REQUEST["api_source"] = true;
153 $_REQUEST["profile_uid"] = $uid;
154 $_REQUEST["source"] = "IFTTT";
155 $_REQUEST["title"] = "";
156 $_REQUEST["body"] = $item["msg"];
157 //$_REQUEST["date"] = $item["date"];
158 //$_REQUEST["uri"] = $item["url"];
160 if (strstr($item["url"], "facebook.com")) {
161 $hash = hash("ripemd128", item["url"]);
162 $_REQUEST["extid"] = NETWORK_FACEBOOK;
163 $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_FACEBOOK.":".$hash);
166 if ($item["type"] == "link") {
167 $data = query_page_info($item["link"]);
169 if (isset($item["title"]) && (trim($item["title"]) != ""))
170 $data["title"] = $item["title"];
172 if (isset($item["description"]) && (trim($item["description"]) != ""))
173 $data["text"] = $item["description"];
175 $_REQUEST["body"] .= add_page_info_data($data);
176 } elseif (($item["type"] == "photo") && ($item["image"] != ""))
177 $_REQUEST["body"] .= "\n\n[img]".$item["image"]."[/img]\n";
179 //print_r($_REQUEST);