]> git.mxchange.org Git - friendica-addons.git/blob - ifttt/ifttt.php
Issue 3873
[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 use Friendica\Core\PConfig;
13
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');
17 }
18
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');
22 }
23
24 function ifttt_module() {
25 }
26
27 function ifttt_content(&$a) {
28 }
29
30 function ifttt_settings(&$a,&$s) {
31
32         if(! local_user())
33                 return;
34
35         $key = PConfig::get(local_user(),'ifttt','key');
36
37         if (!$key) {
38                 $key = substr(random_string(),0,20);
39                 PConfig::set(local_user(),'ifttt','key', $key);
40         }
41
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>';
44         $s .= '</span>';
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>';
48         $s .= '</span>';
49
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>';
52         $s .= '<h4>URL</h4>';
53         $s .= '<p>' . $a->get_baseurl() . '/ifttt/' . $a->user['nickname'] . '</p>';
54         $s .= '<h4>Method</h4>';
55         $s .= '<p>POST</p>';
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>';
65
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>';
70
71         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . t('Save Settings') . '" /></div>';
72         $s .= '</div>';
73
74 }
75
76 function ifttt_settings_post(&$a,&$b) {
77
78         if(x($_POST,'ifttt-submit'))
79                 if (isset($_POST['ifttt-rekey']))
80                         del_pconfig(local_user(), 'ifttt', 'key');
81 }
82
83 function ifttt_post(&$a) {
84         if ($a->argc != 2)
85                 return;
86
87         $user = $a->argv[1];
88
89         $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($user));
90         if (!$r) {
91                 logger("User ".$user." not found.", LOGGER_DEBUG);
92                 return;
93         }
94
95         $uid = $r[0]["uid"];
96
97         logger("Received a post for user ".$uid." from ifttt ".print_r($_REQUEST, true), LOGGER_DEBUG);
98
99         if (!isset($_REQUEST["key"])) {
100                 logger("No key found.");
101                 return;
102         }
103
104         $key = $_REQUEST["key"];
105
106         // Check the key
107         if ($key != PConfig::get($uid,'ifttt','key')) {
108                 logger("Invalid key for user ".$uid, LOGGER_DEBUG);
109                 return;
110         }
111
112         $item = array();
113
114         if (isset($_REQUEST["type"]))
115                 $item["type"] = $_REQUEST["type"];
116
117         if (!in_array($item["type"], array("status", "link", "photo"))) {
118                 logger("Unknown item type ".$item["type"], LOGGER_DEBUG);
119                 return;
120         }
121
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"]);
136
137         if ((substr($item["msg"], 0, 3) == "<<<") && (substr($item["msg"], -3, 3) == ">>>"))
138                 $item["msg"] = substr($item["msg"], 3, -3);
139
140         ifttt_message($uid, $item);
141 }
142
143 function ifttt_message($uid, $item) {
144
145         $a = get_app();
146
147         $_SESSION["authenticated"] = true;
148         $_SESSION["uid"] = $uid;
149
150         unset($_REQUEST);
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"];
159
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);
164         }
165
166         if ($item["type"] == "link") {
167                 $data = query_page_info($item["link"]);
168
169                 if (isset($item["title"]) && (trim($item["title"]) != ""))
170                         $data["title"] = $item["title"];
171
172                 if (isset($item["description"]) && (trim($item["description"]) != ""))
173                         $data["text"] = $item["description"];
174
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";
178
179         //print_r($_REQUEST);
180         item_post($a);
181 }