Facebook to email

Facebook to email Git Source Tree

Root/index.php

1<?php
2/**
3 * Copyright 2014 Grégory Soutadé
4 *
5 * This file is part of f2email.
6 *
7 * f2email is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * f2email is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with f2email. If not, see <http://www.gnu.org/licenses/>.
19 */
20
21require_once "run.php";
22
23function cmp_names($a, $b)
24{
25 return strnatcmp($a['name'], $b['name']);
26}
27
28function display_array($likes, $plugins, $assoc, $remove_button=false)
29{
30 usort($likes, "cmp_names");
31
32 for($i=0; $i<count($likes); $i++)
33 {
34 echo '<div class="fb_object">';
35 echo '<div class="fb_name">';
36 echo "<a href='https://www.facebook.com/" . $likes[$i]["id"] . "'><img src='https://graph.facebook.com/" . $likes[$i]["id"] . "/picture'>". $likes[$i]["name"] . "</a>\n";
37 echo '<select id="select_' . $likes[$i]["id"] . '" onchange="link_fb_object(' . $likes[$i]["id"] . ', this.options[this.selectedIndex].value);">\n';
38 $found = false;
39 for($a=0; $a<sizeof($plugins); $a++)
40 {
41 $name = $plugins[$a]->getName();
42 $id = $plugins[$a]->getID();
43 if (array_key_exists("".$likes[$i]["id"]."", $assoc) &&
44 $id == $assoc[$likes[$i]["id"]])
45 {
46 echo '<option selected="1" value="' . $id . '">' . $name . "</option>\n";
47 $found = $plugins[$a];
48 }
49 else
50 echo '<option value="' . $id . '">' . $name . "</option>\n";
51 }
52 if ($found == false)
53 echo '<option value="-1" selected="1">None</option>\n';
54 else
55 echo '<option value="-1">None</option>\n';
56 echo "</select>\n";
57 echo '</div><div class="preference">';
58 if ($found == false)
59 echo '<input id="local_pref_' . $likes[$i]["id"] . '" type="text"/>';
60 else
61 echo '<input id="local_pref_' . $likes[$i]["id"] . '" type="text" value="'. $found->getLocalPref($likes[$i]["id"]) . '"/>';
62 echo '<input type="button" value="Set preference" onclick="set_local_pref('. $likes[$i]["id"] . ');"/>';
63 if ($remove_button)
64 echo '<form action="/f2email/" method="post"><input type="hidden" name="remove_custom" value="'. $likes[$i]["id"] . ' "/><input type="submit" value="Remove" onclick="return confirm(\'Do you really want to remove this item ?\')"/></form>';
65 echo '</div></div>';
66
67 if ((($i+1) % 3) == 0)
68 echo "<br/>";
69
70 }
71}
72
73function get_plugin($plugins, $plugin_id)
74{
75 foreach($plugins as $plugin)
76 {
77 if ($plugin->getID() == $plugin_id)
78 return $plugin;
79 }
80
81 return null;
82}
83
84$plugins = array();
85$db = new Database($DATABASE_ADDRESS, $DATABASE_USER, $DATABASE_PASSWORD, $DATABASE_DATABASE);
86
87// Create our Application instance (replace this with your appId and secret).
88$facebook = new Facebook(array(
89 'appId' => $APP_ID,
90 'secret' => $SECRET,
91));
92
93if (array_key_exists("logout", $_GET))
94{
95 $facebook->api("/me/permissions/email", "DELETE");
96 $facebook->api("/me/permissions/user_friends", "DELETE");
97 $facebook->api("/me/permissions/user_likes", "DELETE");
98 session_destroy();
99 session_start();
100 $facebook->destroySession();
101}
102
103$user = $facebook->getUser();
104
105if ($user) {
106 try {
107 $user_profile = $facebook->api('/me?fields=id,email,name,likes,friends');
108
109 $_SESSION['plugins'] = $plugins = load_modules($db, $user_profile);
110
111 if (array_key_exists("remove", $_GET))
112 {
113 $db->remove_user($user_profile);
114 $facebook->api("/me/permissions/email", "DELETE");
115 $facebook->api("/me/permissions/user_friends", "DELETE");
116 $facebook->api("/me/permissions/user_likes", "DELETE");
117 session_destroy();
118 session_start();
119 $facebook->destroySession();
120 $user = null;
121 }
122 else if (array_key_exists("link_fb_object", $_POST))
123 {
124 $ret = $db->link_fb_object($user_profile, $_POST["fb_object_id"], $_POST["value"]);
125 $error = $db->get_error();
126 echo ($error != "") ? "Error" : "OK";
127 return ;
128 }
129 else if (array_key_exists("add_custom", $_POST))
130 {
131 $id = $db->escape($_POST["add_custom"]);
132 $ret = $facebook->api("/$id");
133 if (array_key_exists("error", $ret))
134 {
135 echo "<b>" . $ret['message'] . "</b>";
136 return;
137 }
138 $db->add_custom($user_profile['id'], $ret['id']);
139 }
140 else if (array_key_exists("remove_custom", $_POST))
141 {
142 $id = $db->escape($_POST["remove_custom"]);
143 $db->remove_custom($user_profile['id'], $id);
144 }
145 else if (array_key_exists("set_global_pref", $_POST))
146 {
147 $p = get_plugin($_SESSION['plugins'], $db->escape($POST['module_id']));
148
149 if ($p == null)
150 {
151 echo "No plugin found";
152 }
153 else
154 {
155 if (!$p->hasGlobalPref())
156 {
157 echo "No global preferences required !";
158 return;
159 }
160
161 $pref = $db->escape($_POST["value"]);
162 if ($pref != "" && !$p->validatePref($pref))
163 {
164 echo "Invalid preference !";
165 return;
166 }
167
168 $ret = $db->set_module_global_pref($user_profile, $pref);
169 $error = $db->get_error();
170 echo ($error != "") ? "Error" : "OK";
171 }
172 return ;
173 }
174 else if (array_key_exists("set_local_pref", $_POST))
175 {
176 $target_module = $db->get_module_for_fb_object($user_profile, $_POST["fb_object_id"]);
177 if ($target_module == -1)
178 {
179 echo "No module selected for " . $_POST["fb_object_id"];
180 return;
181 }
182
183 $p = get_plugin($_SESSION['plugins'], $target_module);
184
185 if ($p == null)
186 {
187 echo "No plugin found";
188 return ;
189 }
190 else
191 {
192 $pref = $db->escape($_POST["value"]);
193 if (!$p->hasLocalPref())
194 {
195 echo "No local preferences required !";
196 return;
197 }
198
199 if ($pref != "" && !$p->validatePref($pref))
200 {
201 echo "Invalid preference !";
202 return;
203 }
204
205 $ret = $db->set_module_local_pref($user_profile, $_POST["fb_object_id"], $p->getID(), $pref);
206 $error = $db->get_error();
207 echo ($error != "") ? "Error" : "OK";
208 }
209 return ;
210 }
211 else if (array_key_exists("test_configuration", $_POST))
212 {
213 echo run($user_profile['id']);
214 return;
215 }
216 else
217 {
218 if ($facebook->setExtendedAccessToken())
219 {
220 $db->insert_user_or_update_token($user_profile, $facebook->getAccessToken());
221 }
222 else
223 echo "Extended access token failed";
224 }
225 } catch (FacebookApiException $e) {
226 error_log($e);
227 $user = null;
228 }
229}
230
231if ($user) {
232 $logoutUrl = $WEBSITE_URL . "/?logout=1";
233 $removeAccountUrl = $WEBSITE_URL . "/?remove=1";
234 /* $p = array("next" => "http://soutade.fr/f2email/?logout=1"); */
235 /* $logoutUrl = $facebook->getLogoutUrl($p); */
236 /* $p = array("next" => "http://soutade.fr/f2email/?remove=1"); */
237 /* $removeAccountUrl = $facebook->getLogoutUrl($p); */
238} else {
239 $loginUrl = $facebook->getLoginUrl(array("scope" => "user_friends,user_likes,email,read_stream,export_stream"));
240 /* "redirect_uri" => "http://soutade.fr/f2email")); */
241 $loginUrl = str_replace(urlencode("?logout=1"), "", $loginUrl);
242 $loginUrl = str_replace(urlencode("?remove=1"), "", $loginUrl);
243}
244?>
245
246<!doctype html>
247<html xmlns:fb="http://www.facebook.com/2008/fbml">
248 <head>
249 <meta http-equiv="content-type" content="text/html; charset=utf-8" />
250 <title>f2email</title>
251 <link rel="stylesheet" type="text/css" href="ressources/f2email.css" />
252 <script language="javascript">
253 var local_prefs = new Object();
254<?php
255if ($user)
256{
257 $assoc = $db->get_module_per_fb_id($user_profile);
258 foreach($assoc as $fb_object_id => $module_id)
259 {
260 $module = get_plugin($plugins, $module_id);
261 if ($module->getLocalPref($fb_object_id) != "")
262 echo "local_prefs['$fb_object_id" . "_" . "$module_id'] = '" . $module->getLocalPref($fb_object_id) . "' ;\n";
263 }
264}
265?>
266 </script>
267 <script src="ressources/f2email.js"></script>
268 </head>
269 <body>
270 <div id="overlay"></div>
271 <center><h1><a class="title_link" href="https://indefero.soutade.fr/p/f2email">Facebook to email</a></h1></center>
272
273 <?php if ($user): ?>
274 <a href="<?php echo $logoutUrl; ?>">Logout</a>&nbsp;&nbsp;&nbsp;
275 <a href="<?php echo $removeAccountUrl; ?>" onclick="return confirm('Do you really want to delete your account ?')">Remove my account</a>
276 <?php else: ?>
277 <div>
278 Login using OAuth 2.0 :
279 <a href="<?php echo $loginUrl; ?>">Login with Facebook</a>
280 </div>
281 <br/>
282 <?php endif ?>
283
284 <!--<h2>PHP Session</h2>-->
285 <!--<pre><?php /*print_r($_SESSION);*/ ?></pre>-->
286
287 <?php if ($user): ?>
288 <pre><?php /*print_r($user_profile);*/ ?></pre>
289 <h2>Modules</h2>
290 <?php
291 for($i=0; $i<sizeof($plugins); $i++)
292 {
293 $p = $plugins[$i];
294 echo "<b>" . $p->getName() . "</b><br/><br/>" ;
295 echo '<div class="module_desc">';
296 echo $p->getDescription();
297 echo '</div><div class="module_help">';
298 echo $p->getHelp();
299 echo "</div>";
300 echo '<form style="display:inline;"><input id="global_pref_' . $p->getID() . '" value="' . $p->getGlobalPref() . '"></form>';
301 echo '<input type="button" onclick="set_global_pref('.$p->getID().');" value="Set global preference"/>';
302 echo "<br/>";
303 }
304echo "<br/><br/>";
305echo '<input type="button" onclick="test_configuration();" value="Test configuration"/><a href="#" class="bulle">[?]
306<span>
307Test selected modules with last feed
308</span>
309</a>'
310
311 ?>
312 <h2>You</h2>
313<a class="title_link" href="https://www.facebook.com/<?php echo $user ?>"><img src="https://graph.facebook.com/<?php echo $user; ?>/picture"></a>
314
315 <!--<pre><?php /*print_r($user_profile);*/ ?></pre>-->
316 <h2>Customs</h2>
317 Add custom Facebook ID (eg. <span style="color:#ff66cc">JeanDujardinOfficiel</span> or <span style="color:#003399">344294141127</span>)<form action="/f2email/" method="post"><input type="text" name="add_custom"/><input type="submit" value="Add"/></form><br/>
318<?php
319 $customs = array();
320 $customs_id = $db->get_customs($user_profile["id"]);
321foreach($customs_id as $id)
322{
323 array_push($customs, $facebook->api("/$id"));
324}
325display_array($customs, $plugins, $assoc, true);
326
327echo "<h2>Likes</h2>";
328if (array_key_exists("likes", $user_profile))
329 display_array($user_profile["likes"]["data"], $plugins, $assoc);
330/* echo "<h2>Groups</h2>"; */
331/* if (array_key_exists("groups", $user_profile)) */
332/* display_array($user_profile["groups"]["data"], $plugins, $assoc); */
333echo "<h2>Friends</h2>";
334if (array_key_exists("friends", $user_profile))
335 display_array($user_profile["friends"]["data"], $plugins, $assoc);
336?>
337 <pre><?php /* print_r($likes) */; ?></pre>
338 <?php else: ?>
339 <strong><em>You are not Connected.</em></strong>
340 <?php endif ?>
341 </body>
342</html>

Archive Download this file

Branches