]> git.mxchange.org Git - friendica-addons.git/blob - ifttt/ifttt.php
Revert to stable version 3.5.4
[friendica-addons.git] / ifttt / ifttt.php
1 <?php
2 /**
3  * Name: IFTTT Receiver
4  * Description: Receives a post from https://ifttt.com/ and distributes it.
5  * Version: 0.1
6  * Author: Michael Vogel <https://pirati.ca/profile/heluecht>
7  */
8
9 require_once("mod/item.php");
10 require_once("include/items.php");
11
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');
15 }
16
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');
20 }
21
22 function ifttt_module() {
23 }
24
25 function ifttt_content(&$a) {
26 }
27
28 function ifttt_settings(&$a,&$s) {
29
30         if(! local_user())
31                 return;
32
33         $key = get_pconfig(local_user(),'ifttt','key');
34
35         if (!$key) {
36                 $key = substr(random_string(),0,20);
37                 set_pconfig(local_user(),'ifttt','key', $key);
38         }
39
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>';
42         $s .= '</span>';
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>';
46         $s .= '</span>';
47
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 <a href='https://ifttt.com/maker'>Maker</a> (In the form 'if Facebook then Maker') with the following parameters:").'</p>';
50         $s .= '<h4>URL</h4>';
51         $s .= '<p>' . $a->get_baseurl() . '/ifttt/' . $a->user['nickname'] . '</p>';
52         $s .= '<h4>Method</h4>';
53         $s .= '<p>POST</p>';
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>';
63
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>';
68
69         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
70         $s .= '</div>';
71
72 }
73
74 function ifttt_settings_post(&$a,&$b) {
75
76         if(x($_POST,'ifttt-submit'))
77                 if (isset($_POST['ifttt-rekey']))
78                         del_pconfig(local_user(), 'ifttt', 'key');
79 }
80
81 function ifttt_post(&$a) {
82         if ($a->argc != 2)
83                 return;
84
85         $user = $a->argv[1];
86
87         $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($user));
88         if (!$r) {
89                 logger("User ".$user." not found.", LOGGER_DEBUG);
90                 return;
91         }
92
93         $uid = $r[0]["uid"];
94
95         logger("Received a post for user ".$uid." from ifttt ".print_r($_REQUEST, true), LOGGER_DEBUG);
96
97         if (!isset($_REQUEST["key"])) {
98                 logger("No key found.");
99                 return;
100         }
101
102         $key = $_REQUEST["key"];
103
104         // Check the key
105         if ($key != get_pconfig($uid,'ifttt','key')) {
106                 logger("Invalid key for user ".$uid, LOGGER_DEBUG);
107                 return;
108         }
109
110         $item = array();
111
112         if (isset($_REQUEST["type"]))
113                 $item["type"] = $_REQUEST["type"];
114
115         if (!in_array($item["type"], array("status", "link", "photo"))) {
116                 logger("Unknown item type ".$item["type"], LOGGER_DEBUG);
117                 return;
118         }
119
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"]);
134
135         if ((substr($item["msg"], 0, 3) == "<<<") && (substr($item["msg"], -3, 3) == ">>>"))
136                 $item["msg"] = substr($item["msg"], 3, -3);
137
138         ifttt_message($uid, $item);
139 }
140
141 function ifttt_message($uid, $item) {
142
143         $a = get_app();
144
145         $_SESSION["authenticated"] = true;
146         $_SESSION["uid"] = $uid;
147
148         unset($_REQUEST);
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"];
157
158         if (strstr($item["url"], "facebook.com")) {
159                 $hash = hash("ripemd128", item["url"]);
160                 $_REQUEST["extid"] = NETWORK_FACEBOOK;
161                 $_REQUEST['message_id'] = item_new_uri($a->get_hostname(), $uid, NETWORK_FACEBOOK.":".$hash);
162         }
163
164         if ($item["type"] == "link") {
165                 $data = query_page_info($item["link"]);
166
167                 if (isset($item["title"]) && (trim($item["title"]) != ""))
168                         $data["title"] = $item["title"];
169
170                 if (isset($item["description"]) && (trim($item["description"]) != ""))
171                         $data["text"] = $item["description"];
172
173                 $_REQUEST["body"] .=  add_page_info_data($data);
174         } elseif (($item["type"] == "photo") && ($item["image"] != ""))
175                 $_REQUEST["body"] .= "\n\n[img]".$item["image"]."[/img]\n";
176
177         //print_r($_REQUEST);
178         item_post($a);
179 }