]> git.mxchange.org Git - friendica-addons.git/blob - ifttt/ifttt.php
Merge branch '3.6-rc'
[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 require_once 'mod/item.php';
9 require_once 'include/items.php';
10 require_once 'include/text.php';
11
12 use Friendica\App;
13 use Friendica\Core\Addon;
14 use Friendica\Core\L10n;
15 use Friendica\Core\PConfig;
16 use Friendica\Database\DBM;
17
18 function ifttt_install()
19 {
20         Addon::registerHook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
21         Addon::registerHook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
22 }
23
24 function ifttt_uninstall()
25 {
26         Addon::unregisterHook('connector_settings', 'addon/ifttt/ifttt.php', 'ifttt_settings');
27         Addon::unregisterHook('connector_settings_post', 'addon/ifttt/ifttt.php', 'ifttt_settings_post');
28 }
29
30 function ifttt_module() {
31 }
32
33 function ifttt_content(&$a) {
34 }
35
36 function ifttt_settings(&$a,&$s) {
37
38         if(! local_user())
39                 return;
40
41         $key = get_pconfig(local_user(),'ifttt','key');
42
43         if (!$key) {
44                 $key = substr(random_string(),0,20);
45                 set_pconfig(local_user(),'ifttt','key', $key);
46         }
47
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>';
50         $s .= '</span>';
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>';
54         $s .= '</span>';
55
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>';
58         $s .= '<h4>URL</h4>';
59         $s .= '<p>' . $a->get_baseurl() . '/ifttt/' . $a->user['nickname'] . '</p>';
60         $s .= '<h4>Method</h4>';
61         $s .= '<p>POST</p>';
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>';
71
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>';
76
77         $s .= '<div class="settings-submit-wrapper" ><input type="submit" name="ifttt-submit" class="settings-submit" value="' . L10n::t('Save Settings') . '" /></div>';
78         $s .= '</div>';
79 }
80
81 function ifttt_settings_post(&$a,&$b) {
82
83         if(x($_POST,'ifttt-submit'))
84                 if (isset($_POST['ifttt-rekey']))
85                         del_pconfig(local_user(), 'ifttt', 'key');
86 }
87
88 function ifttt_post(&$a) {
89         if ($a->argc != 2)
90                 return;
91
92         $user = $a->argv[1];
93
94         $r = q("SELECT `uid` FROM `user` WHERE `nickname` = '%s' LIMIT 1", dbesc($user));
95         if (!$r) {
96                 logger("User ".$user." not found.", LOGGER_DEBUG);
97                 return;
98         }
99
100         $uid = $r[0]["uid"];
101
102         logger("Received a post for user ".$uid." from ifttt ".print_r($_REQUEST, true), LOGGER_DEBUG);
103
104         if (!isset($_REQUEST["key"])) {
105                 logger("No key found.");
106                 return;
107         }
108
109         $key = $_REQUEST["key"];
110
111         // Check the key
112         if ($key != get_pconfig($uid,'ifttt','key')) {
113                 logger("Invalid key for user ".$uid, LOGGER_DEBUG);
114                 return;
115         }
116
117         $item = array();
118
119         if (isset($_REQUEST["type"]))
120                 $item["type"] = $_REQUEST["type"];
121
122         if (!in_array($item["type"], array("status", "link", "photo"))) {
123                 logger("Unknown item type ".$item["type"], LOGGER_DEBUG);
124                 return;
125         }
126
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"]);
141
142         if ((substr($item["msg"], 0, 3) == "<<<") && (substr($item["msg"], -3, 3) == ">>>"))
143                 $item["msg"] = substr($item["msg"], 3, -3);
144
145         ifttt_message($uid, $item);
146 }
147
148 function ifttt_message($uid, $item) {
149
150         $a = get_app();
151
152         $_SESSION["authenticated"] = true;
153         $_SESSION["uid"] = $uid;
154
155         unset($_REQUEST);
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"];
164
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);
169         }
170
171         if ($item["type"] == "link") {
172                 $data = query_page_info($item["link"]);
173
174                 if (isset($item["title"]) && (trim($item["title"]) != ""))
175                         $data["title"] = $item["title"];
176
177                 if (isset($item["description"]) && (trim($item["description"]) != ""))
178                         $data["text"] = $item["description"];
179
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";
183
184         //print_r($_REQUEST);
185         item_post($a);
186 }