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

Archive Download this file

Branches