Facebook to email

Facebook to email Git Source Tree

Root/run.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 "conf.php";
22require_once "modules/module.php";
23require_once "facebook.php";
24
25function send_email($to, $subject, $message, $extra_header="")
26{
27 global $FROM_ADDRESS;
28 $headers = 'From: "No-reply" <' . $FROM_ADDRESS . '>' . "\r\n" .
29 "MIME-Version: 1.0\r\n" .
30 "Content-Type: text/html; charset=utf-8\r\n" .
31 'X-Mailer: PHP/' . phpversion() ;
32 if ($extra_header != "")
33 $headers .= "\r\n" . $extra_header;
34
35 mail($to, "=?UTF-8?B?" . base64_encode($subject) . "?=", $message, $headers);
36
37 if (PHP_SAPI === 'cli')
38 {
39 echo "Mail to $to\n";
40 echo $message;
41 }
42}
43
44function endsWith( $str, $sub ) {
45 return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub );
46}
47
48function load_modules($db, $user_profile)
49{
50 $plugins = array();
51
52 $files = scandir("modules");
53 for($i=0; $i<count($files); $i++)
54 {
55 if (is_dir($files[$i])) continue;
56 if (endswith($files[$i], ".php") && $files[$i] != "module.php")
57 require_once "modules/$files[$i]";
58 }
59
60 $classes = get_declared_classes();
61 foreach($classes as $class) {
62 $reflectClass = new ReflectionClass($class) ;
63 if($reflectClass->implementsInterface('Plugin') &&
64 $reflectClass->getName() != "Module" ) {
65 array_push($plugins, $reflectClass->newInstance($db, $user_profile));
66 }
67 }
68
69 return $plugins;
70}
71
72function token_error($db, $user)
73{
74 global $WEBSITE_URL;
75 print "Token expired for user " . $user['id'] . "\n";
76
77 send_email($user['email'], "f2email : Access token expired", 'Your Facebook token has expired, please reconnect to <a href="'.$WEBSITE_URL.'">'.$WEBSITE_URL.'</a>.<br/>Your account will be automatically removed within a month if not.');
78
79 $db->disable_user($user);
80}
81
82function run($user_id = 0, $target_object_id = 0)
83{
84 global $DATABASE_ADDRESS, $DATABASE_USER, $DATABASE_PASSWORD, $DATABASE_DATABASE;
85 global $APP_ID, $SECRET, $WEBSITE_URL, $FROM_ADDRESS, $ADMIN_EMAIL;
86
87 $plugins = array();
88 $db = new Database($DATABASE_ADDRESS, $DATABASE_USER, $DATABASE_PASSWORD, $DATABASE_DATABASE);
89
90 $db->remove_expired_users();
91
92 $result = $db->get_users($user_id);
93 $ret = "";
94
95 if ($result)
96 {
97 while ($user = $result->fetch_assoc())
98 {
99 $user_profile = null;
100
101 if ($user['token_expired']) continue;
102
103 if ($user_id != 0 && $user_id != $user['id']) continue;
104
105 try {
106 $facebook = new Facebook(array(
107 'appId' => $APP_ID,
108 'secret' => $SECRET
109 ));
110
111 $facebook->setAccessToken($user['access_token']);
112
113 $user_profile = $facebook->api('/me?fields=id,likes');
114 }
115 catch(FacebookApiException $e)
116 {
117 token_error($db, $user);
118 continue;
119 }
120 catch(Exception $e)
121 {
122 send_email($ADMIN_EMAIL, "f2email : Exception raised", $e->getMessage());
123 }
124
125 if (array_key_exists("error", $user_profile))
126 {
127 token_error($db, $user);
128 continue;
129 }
130
131 $plugins = load_modules($db, $user_profile);
132
133 $assoc = $db->get_module_per_fb_id($user_profile);
134 foreach($assoc as $fb_object_id => $module_id)
135 {
136 if ($target_object_id != 0 && $target_object_id != $fb_object_id)
137 continue;
138
139 // Look for corresponding plugin
140 $plugin = null;
141 for($a=0; $a<count($plugins); $a++)
142 {
143 if ($plugins[$a]->getID() == $module_id)
144 {
145 $plugin = $plugins[$a];
146 break;
147 }
148 }
149
150 if ($plugin == null)
151 {
152 $ret .= "No plugin found for ID " . $fb_object_id . "\n";
153 if (PHP_SAPI === 'cli')
154 print "No plugin found for ID " . $fb_object_id . "\n";
155 continue;
156 }
157
158 $feeds = $facebook->api("/" . $fb_object_id . "?fields=feed.limit(20)");
159 $fb_object = $facebook->api("/" . $fb_object_id);
160
161 if (!array_key_exists("feed", $feeds))
162 {
163 $ret .= "Empty feeds for $fb_object_id\n";
164 if (PHP_SAPI === 'cli')
165 echo "Empty feeds for $fb_object_id\n";
166 continue;
167 }
168 /* print_r($feeds); */
169 $feeds = $feeds["feed"]["data"];
170
171 // Test mode
172 if ($user_id != 0)
173 {
174 if (count($feeds) > 0)
175 {
176 /* $ret .= "Use plugin " . $plugin->getName() . " for ID " . $fb_object_id . "\n" . print_r($feed["feed"]["data"][0], true); */
177 $plugin->manageNotification($fb_object, $feeds[0]);
178 }
179 if ($target_object_id != 0)
180 break;
181 continue;
182 }
183
184 $last_fetch = $db->get_last_fetch($user_profile, $fb_object_id);
185 $new_fetch = array();
186 $notifications = array();
187 /* echo "Last fetch :" . count($last_fetch); */
188 /* print_r($last_fetch); */
189 /* print "AA\n"; */
190 /* print_r($feeds); */
191 for($a=0; $a<count($feeds); $a++)
192 {
193 $end = false;
194 // Look for last fetch
195 for($b=0; $b<count($last_fetch); $b++)
196 {
197 if ($feeds[$a]["id"] == $last_fetch[$b])
198 {
199 $end = true;
200 break;
201 }
202 }
203 if ($end) break;
204 array_push($notifications, $feeds[$a]);
205 array_push($new_fetch, $feeds[$a]["id"]);
206 if (count($new_fetch) >= 5) break;
207 }
208 /* print "New fetches " . count($new_fetch); */
209 /* print_r($new_fetch); */
210 if (count($last_fetch) > 0)
211 {
212 for($a=count($new_fetch); $a<5; $a++)
213 array_push($new_fetch, $last_fetch[$a-count($new_fetch)]);
214 }
215 $db->set_last_fetch($user_profile, $fb_object_id, $new_fetch);
216 for($a=count($notifications)-1; $a>=0; $a--)
217 {
218 /* print_r($notifications[$a]); */
219 $plugin->manageNotification($fb_object, $notifications[$a]);
220 }
221 }
222 }
223 }
224 else
225 {
226 $ret .= "No user with ID " . $user_id . "\n";
227 if (PHP_SAPI === 'cli')
228 echo "No user with ID " . $user_id . "\n";
229 }
230
231 if ($ret == "") $ret = "OK";
232
233 return $ret;
234}
235
236if (PHP_SAPI === 'cli')
237 run(0, 1268300218);
238//run();
239
240?>

Archive Download this file

Branches