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

Archive Download this file

Branches