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)
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 $facebook = new Facebook(array(
86 'appId' => $APP_ID,
87 'secret' => $SECRET
88 ));
89
90 $facebook->setAccessToken($user['access_token']);
91
92 $user_profile = $facebook->api('/me?fields=id,likes');
93 if (array_key_exists("error", $user_profile))
94 {
95 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.');
96
97 $db->disable_user($user);
98 continue;
99 }
100
101 $plugins = load_modules($db, $user_profile);
102
103 $assoc = $db->get_module_per_fb_id($user_profile);
104 foreach($assoc as $fb_object_id => $module_id)
105 {
106 // Look for corresponding plugin
107 $plugin = null;
108 for($a=0; $a<count($plugins); $a++)
109 {
110 if ($plugins[$a]->getID() == $module_id)
111 {
112 $plugin = $plugins[$a];
113 break;
114 }
115 }
116
117 if ($plugin == null)
118 {
119 $ret .= "No plugin found for ID " . $fb_object_id . "\n";
120 continue;
121 }
122
123 $feeds = $facebook->api("/" . $fb_object_id . "?fields=feed.limit(20)");
124 $fb_object = $facebook->api("/" . $fb_object_id);
125
126 if (!array_key_exists("feed", $feeds))
127 {
128 $ret .= "Empty feeds for $fb_object_id\n";
129 if (PHP_SAPI === 'cli')
130 echo "Empty feeds for $fb_object_id\n";
131 continue;
132 }
133 /* print_r($feeds); */
134 $feeds = $feeds["feed"]["data"];
135
136 // Test mode
137 if ($user_id != 0)
138 {
139 if (count($feeds) > 0)
140 {
141 /* $ret .= "Use plugin " . $plugin->getName() . " for ID " . $fb_object_id . "\n" . print_r($feed["feed"]["data"][0], true); */
142 $plugin->manageNotification($fb_object, $feeds[0]);
143 }
144 continue;
145 }
146
147 $last_fetch = $db->get_last_fetch($user_profile, $fb_object_id);
148 $new_fetch = array();
149 $notifications = array();
150 /* echo "Last fetch :" . count($last_fetch); */
151 /* print_r($last_fetch); */
152 /* print "AA\n"; */
153 /* print_r($feeds); */
154 for($a=0; $a<count($feeds); $a++)
155 {
156 $end = false;
157 // Look for last fetch
158 for($b=0; $b<count($last_fetch); $b++)
159 {
160 if ($feeds[$a]["id"] == $last_fetch[$b])
161 {
162 $end = true;
163 break;
164 }
165 }
166 if ($end) break;
167 array_push($notifications, $feeds[$a]);
168 array_push($new_fetch, $feeds[$a]["id"]);
169 if (count($new_fetch) >= 5) break;
170 }
171 /* print "New fetches " . count($new_fetch); */
172 /* print_r($new_fetch); */
173 if (count($last_fetch) > 0)
174 {
175 for($a=count($new_fetch); $a<5; $a++)
176 array_push($new_fetch, $last_fetch[$a-count($new_fetch)]);
177 }
178 $db->set_last_fetch($user_profile, $fb_object_id, $new_fetch);
179 for($a=count($notifications)-1; $a>=0; $a--)
180 {
181 /* print_r($notifications[$a]); */
182 $plugin->manageNotification($fb_object, $notifications[$a]);
183 }
184 }
185 }
186 }
187 else
188 $ret .= "No user with ID " . $user_id . "\n";
189
190 if ($ret == "") $ret = "OK";
191
192 return $ret;
193}
194
195if (PHP_SAPI === 'cli')
196 run(0);
197
198?>

Archive Download this file

Branches