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 function ifttt_install() {
13 register_hook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
14 register_hook('connector_settings_post','addon/ifttt/ifttt.php', 'ifttt_settings_post');
17 function ifttt_uninstall() {
18 unregister_hook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
19 unregister_hook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
22 function ifttt_module() {
25 function ifttt_content(&$a) {
28 function ifttt_settings(&$a,&$s) {
33 $key = get_pconfig(local_user(),'ifttt','key');
36 $key = substr(random_string(),0,20);
37 set_pconfig(local_user(),'ifttt','key', $key);
40 $s .= '<span id="settings_ifttt_inflated" class="settings-block fakelink" style="display: block;" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
41 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">'. t('IFTTT Mirror').'</h3>';
43 $s .= '<div id="settings_ifttt_expanded" class="settings-block" style="display: none;">';
44 $s .= '<span class="fakelink" onclick="openClose(\'settings_ifttt_expanded\'); openClose(\'settings_ifttt_inflated\');">';
45 $s .= '<img class="connector" src="addon/ifttt/ifttt.png" /><h3 class="connector">'. t('IFTTT Mirror').'</h3>';
48 $s .= '<div id="ifttt-configuration-wrapper">';
49 $s .= '<p>'.t("Create an account at <a href='http://www.ifttt.com'>IFTTT</a>. Create three Facebook recipes that are connected with 'Maker' with the following parameters:").'</p>';
51 $s .= '<p>'.$a->get_baseurl()."/ifttt/".'</p>';
52 $s .= '<h4>Method</h4>';
54 $s .= '<h4>Content Type</h4>';
55 $s .= '<p>application/x-www-form-urlencoded</p>';
56 $s .= '<h4>'.t("Body for 'new status message'").'</h4>';
57 $s .= '<p><code>'.htmlentities('key='.$key.'&type=status&msg=<<<{{Message}}>>>&date=<<<{{UpdatedAt}}>>>&url=<<<{{PageUrl}}>>>').'</code></p>';
58 $s .= '<h4>'.t("Body for 'new photo upload'").'</h4>';
59 $s .= '<p><code>'.htmlentities('key='.$key.'&type=photo&link=<<<{{Link}}>>>&image=<<<{{ImageSource}}>>>&msg=<<<{{Caption}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>').'</code></p>';
60 $s .= '<h4>'.t("Body for 'new link post'").'</h4>';
61 $s .= '<p><code>'.htmlentities('key='.$key.'&type=link&link=<<<{{Link}}>>>&title=<<<{{Title}}>>>&msg=<<<{{Message}}>>>&description=<<<{{Description}}>>>&date=<<<{{CreatedAt}}>>>&url=<<<{{PageUrl}}>>>').'</code></p>';
62 $s .= '</div><div class="clear"></div>';
64 $s .= '<div id="ifttt-rekey-wrapper">';
65 $s .= '<label id="ifttt-rekey-label" for="ifttt-checkbox">' . t('Generate new key') . '</label>';
66 $s .= '<input id="ifttt-checkbox" type="checkbox" name="ifttt-rekey" value="1" />';
67 $s .= '</div><div class="clear"></div>';
69 $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
74 function ifttt_settings_post(&$a,&$b) {
76 if(x($_POST,'ifttt-submit'))
77 if (isset($_POST['ifttt-rekey']))
78 del_pconfig(local_user(), 'ifttt', 'key');
81 function ifttt_post(&$a) {
87 $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($user));
89 logger("User ".$user." not found.", LOGGER_DEBUG);
95 logger("Received a post for user ".$uid." from ifttt ".print_r($_REQUEST, true), LOGGER_DEBUG);
97 if (!isset($_REQUEST["key"])) {
98 logger("No key found.");
102 $key = $_REQUEST["key"];
105 if ($key != get_pconfig($uid,'ifttt','key')) {
106 logger("Invalid key for user ".$uid, LOGGER_DEBUG);
112 if (isset($_REQUEST["type"]))
113 $item["type"] = $_REQUEST["type"];
115 if (!in_array($item["type"], array("status", "link", "photo"))) {
116 logger("Unknown item type ".$item["type"], LOGGER_DEBUG);
120 if (isset($_REQUEST["link"]))
121 $item["link"] = trim($_REQUEST["link"]);
122 if (isset($_REQUEST["image"]))
123 $item["image"] = trim($_REQUEST["image"]);
124 if (isset($_REQUEST["title"]))
125 $item["title"] = trim($_REQUEST["title"]);
126 if (isset($_REQUEST["msg"]))
127 $item["msg"] = trim($_REQUEST["msg"]);
128 if (isset($_REQUEST["description"]))
129 $item["description"] = trim($_REQUEST["description"]);
130 if (isset($_REQUEST["date"]))
131 $item["date"] = date("c", strtotime($date = str_replace(" at ", ", ", $_REQUEST["date"])));
132 if (isset($_REQUEST["url"]))
133 $item["url"] = trim($_REQUEST["url"]);
135 if ((substr($item["msg"], 0, 3) == "<<<") AND (substr($item["msg"], -3, 3) == ">>>"))
136 $item["msg"] = substr($item["msg"], 3, -3);
138 ifttt_message($uid, $item);
141 function ifttt_message($uid, $item) {
145 $_SESSION["authenticated"] = true;
146 $_SESSION["uid"] = $uid;
149 $_REQUEST["type"] = "wall";
150 $_REQUEST["api_source"] = true;
151 $_REQUEST["profile_uid"] = $uid;
152 $_REQUEST["source"] = "IFTTT";
153 $_REQUEST["title"] = "";
154 $_REQUEST["body"] = $item["msg"];
155 //$_REQUEST["date"] = $item["date"];
156 //$_REQUEST["uri"] = $item["url"];
158 if (strstr($item["url"], "facebook.com"))
159 $_REQUEST["extid"] = NETWORK_FACEBOOK;
161 if ($item["type"] == "link") {
162 $data = query_page_info($item["link"]);
164 if (isset($item["title"]) AND (trim($item["title"]) != ""))
165 $data["title"] = $item["title"];
167 if (isset($item["description"]) AND (trim($item["description"]) != ""))
168 $data["text"] = $item["description"];
170 $_REQUEST["body"] .= add_page_info_data($data);
171 } elseif (($item["type"] == "photo") AND ($item["image"] != ""))
172 $_REQUEST["body"] .= "\n\n[img]".$item["image"]."[/img]\n";
174 //print_r($_REQUEST);