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
38function endsWith( $str, $sub ) {
39 return ( substr( $str, strlen( $str ) - strlen( $sub ) ) == $sub );
40}
41
42function load_modules($db, $user_profile)
43{
44 $plugins = array();
45
46 $files = scandir("modules");
47 for($i=0; $i<count($files); $i++)
48 {
49 if (is_dir($files[$i])) continue;
50 if (endswith($files[$i], ".php") && $files[$i] != "module.php")
51 require_once "modules/$files[$i]";
52 }
53
54 $classes = get_declared_classes();
55 foreach($classes as $class) {
56 $reflectClass = new ReflectionClass($class) ;
57 if($reflectClass->implementsInterface('Plugin') &&
58 $reflectClass->getName() != "Module" ) {
59 array_push($plugins, $reflectClass->newInstance($db, $user_profile));
60 }
61 }
62
63 return $plugins;
64}
65
66function run($user_id = 0, $target_object_id = 0)
67{
68 global $DATABASE_ADDRESS, $DATABASE_USER, $DATABASE_PASSWORD, $DATABASE_DATABASE;
69 global $APP_ID, $SECRET, $WEBSITE_URL, $FROM_ADDRESS;
70
71 $plugins = array();
72 $db = new Database($DATABASE_ADDRESS, $DATABASE_USER, $DATABASE_PASSWORD, $DATABASE_DATABASE);
73
74 $db->remove_expired_users();
75
76 $result = $db->get_users($user_id);
77 $ret = "";
78
79 if ($result)
80 {
81 while ($user = $result->fetch_assoc())
82 {
83 if ($user['token_expired']) continue;
84
85 if ($user_id != 0 && $user_id != $user['id']) continue;
86
87 $facebook = new Facebook(array(
88 'appId' => $APP_ID,
89 'secret' => $SECRET
90 ));
91
92 $facebook->setAccessToken($user['access_token']);
93
94 $user_profile = $facebook->api('/me?fields=id,likes');
95 if (array_key_exists("error", $user_profile))
96 {
97 send_email($user['email'], "f2email : Access token expired", 'Your Facebook token has expired, please reconnect to <a href="http://soutade.fr/f2email">http://soutade.fr/f2email</a>.<br/>Your account will be automatically removed within a month if not.');
98
99 $db->disable_user($user);
100 continue;
101 }
102
103 $plugins = load_modules($db, $user_profile);
104
105 $assoc = $db->get_module_per_fb_id($user_profile);
106 foreach($assoc as $fb_object_id => $module_id)
107 {
108 if ($target_object_id != 0 && $target_object_id != $fb_object_id)
109 continue;
110
111 // Look for corresponding plugin
112 $plugin = null;
113 for($a=0; $a<count($plugins); $a++)
114 {
115 if ($plugins[$a]->getID() == $module_id)
116 {
117 $plugin = $plugins[$a];
118 break;
119 }
120 }
121
122 if ($plugin == null)
123 {
124 $ret .= "No plugin found for ID " . $fb_object_id . "\n";
125 continue;
126 }
127
128 $feeds = $facebook->api("/" . $fb_object_id . "?fields=feed.limit(20)");
129 $fb_object = $facebook->api("/" . $fb_object_id);
130
131 if (!array_key_exists("feed", $feeds))
132 {
133 $ret .= "Empty feeds for $fb_object_id\n";
134 if (PHP_SAPI === 'cli')
135 echo "Empty feeds for $fb_object_id\n";
136 continue;
137 }
138 /* print_r($feeds); */
139 $feeds = $feeds["feed"]["data"];
140
141 // Test mode
142 if ($user_id != 0)
143 {
144 if (count($feeds) > 0)
145 {
146 /* $ret .= "Use plugin " . $plugin->getName() . " for ID " . $fb_object_id . "\n" . print_r($feed["feed"]["data"][0], true); */
147 $plugin->manageNotification($fb_object, $feeds[0]);
148 }
149 if ($target_object_id != 0)
150 break;
151 continue;
152 }
153
154 $last_fetch = $db->get_last_fetch($user_profile, $fb_object_id);
155 $new_fetch = array();
156 $notifications = array();
157 /* echo "Last fetch :" . count($last_fetch); */
158 /* print_r($last_fetch); */
159 /* print "AA\n"; */
160 /* print_r($feeds); */
161 for($a=0; $a<count($feeds); $a++)
162 {
163 $end = false;
164 // Look for last fetch
165 for($b=0; $b<count($last_fetch); $b++)
166 {
167 if ($feeds[$a]["id"] == $last_fetch[$b])
168 {
169 $end = true;
170 break;
171 }
172 }
173 if ($end) break;
174 array_push($notifications, $feeds[$a]);
175 array_push($new_fetch, $feeds[$a]["id"]);
176 if (count($new_fetch) >= 5) break;
177 }
178 /* print "New fetches " . count($new_fetch); */
179 /* print_r($new_fetch); */
180 if (count($last_fetch) > 0)
181 {
182 for($a=count($new_fetch); $a<5; $a++)
183 array_push($new_fetch, $last_fetch[$a-count($new_fetch)]);
184 }
185 $db->set_last_fetch($user_profile, $fb_object_id, $new_fetch);
186 for($a=count($notifications)-1; $a>=0; $a--)
187 {
188 /* print_r($notifications[$a]); */
189 $plugin->manageNotification($fb_object, $notifications[$a]);
190 }
191 }
192 }
193 }
194 else
195 $ret .= "No user with ID " . $user_id . "\n";
196
197 if ($ret == "") $ret = "OK";
198
199 return $ret;
200}
201
202if (PHP_SAPI === 'cli')
203 run(0);
204
205?>

Archive Download this file

Branches