source: trunk/www.guidonia.net/wp/wp-content/plugins/fbconnect/fbConnectLogic.php@ 44

Last change on this file since 44 was 44, checked in by luciano, 15 years ago
File size: 13.7 KB
RevLine 
[44]1<?php
2/**
3 * @author: Javier Reyes Gomez (http://www.sociable.es)
4 * @date: 05/10/2008
5 * @license: GPLv2
6 */
7if (!class_exists('WPfbConnect_Logic')):
8
9/**
10 * Basic logic for wp-fbConnect plugin.
11 */
12class WPfbConnect_Logic {
13
14 function get_connected_friends($fb_uid) {
15 $results = array();
16 $query = 'SELECT uid, email_hashes, has_added_app FROM user WHERE has_added_app = 1 AND uid IN '.
17 '(SELECT uid2 FROM friend WHERE uid1 = '.$fb_uid.')';
18 $rows = fb_fql_query($query);
19
20 // Do filtering in PHP because the FQL doesn't allow it (yet)
21 if ($rows!=null && !empty($rows)) {
22 foreach ($rows as $row) {
23 if ((is_array($row['email_hashes']) && count($row['email_hashes']) > 0) ||
24 ($row['has_added_app'] == 1)) {
25 unset($row['has_added_app']);
26 $results[] = $row;
27 }
28 }
29 }
30
31 return $results;
32 }
33
34 /**
35 * Soft verification of plugin activation
36 *
37 * @return boolean if the plugin is okay
38 */
39 function uptodate() {
40 global $fbconnect;
41
42 if( get_option('fb_db_revision') != FBCONNECT_DB_REVISION ) {
43 $fbconnect->enabled = false;
44 update_option('fb_plugin_enabled', false);
45 return false;
46 }
47 $fbconnect->enabled = (get_option('fb_plugin_enabled') == true );
48 return $fbconnect->enabled;
49 }
50
51
52 /**
53 * Get the internal SQL Store. If it is not already initialized, do so.
54 *
55 * @return WPfbConnect_Store internal SQL store
56 */
57 function getStore() {
58 global $fbconnect;
59
60 if (!isset($fbconnect->store)) {
61 set_include_path( dirname(__FILE__) . PATH_SEPARATOR . get_include_path() );
62 require_once 'fbConnectStore.php';
63
64 $fbconnect->store = new WPfbConnect_Store($fbconnect);
65 if (null === $fbconnect->store) {
66 $fbconnect->enabled = false;
67 }
68 }
69
70 return $fbconnect->store;
71 }
72
73
74
75
76 /**
77 * Called on plugin activation.
78 *
79 * @see register_activation_hook
80 */
81 function activate_plugin() {
82 global $fbconnect;
83 //fbconnect_init();
84
85 $store =& WPfbConnect_Logic::getStore();
86 $store->create_tables();
87
88 }
89
90
91
92 /**
93 * Called on plugin deactivation. Cleanup tables.
94 *
95 * @see register_deactivation_hook
96 */
97 function deactivate_plugin() {
98 }
99
100
101 /**
102 * Facebook connect Login
103 */
104 function wp_login_fbconnect() {
105
106 global $wp_version;
107 $self = basename( $GLOBALS['pagenow'] );
108
109 $fb_user = fb_get_loggedin_user();
110
111 $user = wp_get_current_user();
112
113 if ( $fb_user && (!is_user_logged_in() || $user->fbconnect_userid != $fb_user)) { //Intenta hacer login estando registrado en facebook
114 require_once(ABSPATH . WPINC . '/registration.php');
115 $usersinfo = fb_user_getInfo($fb_user);
116
117 $wpid = "";
118 $fbwpuser = WPfbConnect_Logic::get_userbyFBID($fb_user);
119 $wpid = "";
120 if (!is_user_logged_in() && !$fbwpuser){
121 $userdata = get_userdatabylogin( "FB_".$fb_user );
122 if((!$userdata || $userdata=="") && isset($usersinfo) && $usersinfo!=""){
123 $user_data = array();
124 $user_data['user_login'] = "FB_".$fb_user;
125 $user_data['user_pass'] = substr( md5( uniqid( microtime() ).$_SERVER["REMOTE_ADDR"] ), 0, 15);
126 $user_data['user_nicename'] = $usersinfo["first_name"]." ".$usersinfo["last_name"];
127 $user_data['display_name'] = $usersinfo["first_name"]." ".$usersinfo["last_name"];
128 $user_data['user_url'] = $usersinfo["profile_url"];
129 $wpid = wp_insert_user($user_data);
130 update_usermeta( $wpid, "first_name", $usersinfo["first_name"] );
131 update_usermeta( $wpid, "last_name", $usersinfo["last_name"] );
132 //update_usermeta( $wpid, "description", $usersinfo["about_me"] );
133 //update_usermeta( $wpid, "birthday", $usersinfo["birthday"] );
134 //update_usermeta( $wpid, "current_location", $usersinfo["current_location"] );
135 //update_usermeta( $wpid, "sex", $usersinfo["sex"] );
136 WPfbConnect_Logic::set_userid_fbconnect($wpid,$fb_user);
137 }else{
138 WPfbConnect_Logic::set_userid_fbconnect($userdata->ID,$fb_user);
139 $wpid = $userdata->ID;
140 }
141 }elseif(is_user_logged_in() && $fbwpuser && $user->ID != $fbwpuser->ID){ // El usuario FB está asociado a un usaurio WP distinto al logeado
142 WPfbConnect_Logic::set_userid_fbconnect($user->ID,$fb_user);
143 WPfbConnect_Logic::set_userid_fbconnect($fbwpuser->ID,"0"); //Eliminamos la asociación anterior
144 $wpid = $user->ID;
145 }elseif(is_user_logged_in() && $user->fbconnect_userid != $fb_user){ // El usuario WP no está asociado al de FB
146 WPfbConnect_Logic::set_userid_fbconnect($user->ID,$fb_user);
147 $wpid = $user->ID;
148 }else{
149 $wpid = $user->ID;
150 }
151
152 //$userdata = get_userdatabylogin( "FB_".$fb_user );
153 $userdata = WPfbConnect_Logic::get_userbyFBID($fb_user);
154 WPfbConnect_Logic::set_lastlogin_fbconnect($userdata->ID);
155 global $current_user;
156// print_r($current_user);
157// print_r($userdata);
158 $current_user = null;
159
160 WPfbConnect_Logic::fb_set_current_user($userdata);
161 global $userdata;
162 if (isset($userdata) && $userdata!="")
163 $userdata->fbconnect_userid = $fb_user;
164 //wp_set_auth_cookie($userdata->ID, false);
165
166 }
167 }
168
169
170 function fb_set_current_user($userdata, $remember = true) {
171
172 $user = set_current_user($userdata->ID);
173
174 if (function_exists('wp_set_auth_cookie')) {
175 wp_set_auth_cookie($userdata->ID, $remember);
176 } else {
177 wp_setcookie($userdata->user_login, md5($userdata->user_pass), true, '', '', $remember);
178 }
179
180 //do_action('wp_login', $user->user_login);
181 }
182
183
184 //REYES
185 function fb_logout(){
186 fb_expire_session();
187 }
188
189
190 function comment_fbconnect($comment_ID) {
191 global $fbconnect;
192//REYES
193 $comment = WPfbConnect_Logic::get_comment_byID($comment_ID);
194 $fb_user = fb_get_loggedin_user();
195
196 if (is_user_logged_in() && $fb_user){
197
198 WPfbConnect_Logic::set_comment_fbconnect($comment_ID,$fb_user);
199
200 if ($_REQUEST["sendToFacebook"]){
201 $blogname = get_option('blogname');
202 $blogdesc = get_option('blogdescription');
203 $siteurl = get_option('siteurl');
204
205 $url = get_permalink($comment->comment_post_ID);
206
207 $template_data = array('post_title' => '<a href="'.$url.'">'.$comment->post_title.'</a>',
208 'body' => apply_filters( 'comment_text', $comment->comment_content),
209 'body_short' => substr(strip_tags(apply_filters( 'comment_text', $comment->comment_content)),0,255),
210 'post_permalink' => $url,
211 'blogname' => '<a href="'.$siteurl.'">'.$blogname.'</a>',
212 'blogdesc' => $blogdesc,
213 'siteurl' => $siteurl);
214 $_SESSION["template_data"]= $template_data;
215 //fb_feed_publishUserAction($template_data);
216 }
217 }
218
219 }
220
221
222 /**
223 * Mark the provided comment as an Facebook Connect comment
224 *
225 */
226 function set_comment_fbconnect($comment_ID,$fb_user = 0) {
227 global $wpdb, $fbconnect;
228
229 $comments_table = WPfbConnect::comments_table_name();
230 $wpdb->query("UPDATE $comments_table SET fbconnect='".$fb_user."' WHERE comment_ID='$comment_ID' LIMIT 1");
231 }
232
233 /**
234 * Update last user login date
235 *
236 */
237 function set_lastlogin_fbconnect($userID) {
238 global $wpdb, $fbconnect;
239 $users_table = WPfbConnect::users_table_name();
240 $wpdb->query("UPDATE $users_table SET fbconnect_lastlogin=".date("U")." WHERE ID='$userID' LIMIT 1");
241 }
242
243 /**
244 * Get last users
245 *
246 */
247 function get_lastusers_fbconnect($num = 10,$start=0) {
248 global $wpdb, $fbconnect;
249 $users_table = WPfbConnect::users_table_name();
250 $users = $wpdb->get_results("SELECT * FROM $users_table ORDER BY fbconnect_lastlogin DESC LIMIT ".$start.",".$num);
251 return $users;
252 }
253
254 /**
255 * User count
256 *
257 */
258 function get_count_users() {
259 global $wpdb, $fbconnect;
260 $users_table = WPfbConnect::users_table_name();
261 $users = $wpdb->get_results("SELECT count(ID) as userscount FROM $users_table");
262 if (count($users)>0){
263 return $users[0]->userscount;
264 }else{
265 return null;
266 }
267 }
268
269 /**
270 * Get user by fbid
271 *
272 */
273 function get_userbyFBID($fbid) {
274 global $wpdb, $fbconnect;
275 $users_table = WPfbConnect::users_table_name();
276 $users = $wpdb->get_results("SELECT * FROM $users_table WHERE fbconnect_userid = ".$fbid);
277 if (count($users)>0){
278 return $users[0];
279 }else{
280 return null;
281 }
282 }
283
284 /**
285 * Update Facebook userID
286 *
287 */
288 function set_userid_fbconnect($userID,$fbuserid) {
289 global $wpdb, $fbconnect;
290 $users_table = WPfbConnect::users_table_name();
291 $wpdb->query("UPDATE $users_table SET fbconnect_userid=".$fbuserid." WHERE ID='$userID' LIMIT 1");
292 }
293
294 /**
295 * Get community comments
296 *
297 */
298 function get_community_comments($limit=10) {
299 global $wpdb;
300 return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments wpcomments, $wpdb->posts posts WHERE wpcomments.comment_post_ID=posts.ID AND wpcomments.comment_approved = '1' ORDER BY comment_date DESC LIMIT ".$limit));
301 }
302
303 /**
304 * Get user comments
305 *
306 */
307 function get_user_comments($user_id,$limit=25) {
308 global $wpdb;
309 return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments wpcomments, $wpdb->posts posts WHERE wpcomments.comment_post_ID=posts.ID AND wpcomments.fbconnect = %s AND wpcomments.comment_approved = '1' ORDER BY comment_date DESC LIMIT ".$limit, $user_id));
310 }
311
312 /**
313 * Get user comments
314 *
315 */
316 function get_user_comments_byID($user_id,$limit=25) {
317 global $wpdb;
318 return $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments wpcomments, $wpdb->posts posts WHERE wpcomments.comment_post_ID=posts.ID AND wpcomments.user_id = %s AND wpcomments.comment_approved = '1' ORDER BY comment_date DESC LIMIT ".$limit, $user_id));
319 }
320
321 /**
322 * Get a comment by ID
323 *
324 */
325 function get_comment_byID($comment_id) {
326 global $wpdb;
327 $comments = $wpdb->get_results($wpdb->prepare("SELECT * FROM $wpdb->comments wpcomments, $wpdb->posts posts WHERE wpcomments.comment_post_ID=posts.ID AND wpcomments.comment_ID = %s", $comment_id));
328 if ($comments != ""){
329 return $comments[0];
330 }
331 }
332
333 /**
334 * This filter callback simply approves all Facebook Connect comments
335 *
336 * @param string $approved comment approval status
337 * @return string new comment approval status
338 */
339 function comment_approval($approved) {
340 $fb_user = fb_get_loggedin_user();
341 if ($fb_user) {
342 return 1;
343 }else{
344 return $approved;
345 }
346 }
347
348 function html_namespace($html_lang){
349 return "xmlns:fb=\"http://www.facebook.com/2008/fbml\" ".$html_lang;
350 }
351
352 function fb_get_avatar($avatar=null, $id_or_email = null, $size = null, $default=null){
353 $fbuser = "";
354 $username = "";
355 if ( is_numeric($id_or_email) ) {
356 $id = (int) $id_or_email;
357 $user = get_userdata($id);
358 $username = $user->display_name;
359 if ( $user && isset($user->fbconnect_userid) && $user->fbconnect_userid!=""){
360 $fbuser = $user->fbconnect_userid;
361 }
362 } elseif ( is_object($id_or_email) ) {
363 if ( !empty($id_or_email->fbconnect) && $id_or_email->fbconnect!="0" ) {
364 $id = (int) $id_or_email->user_id;
365 $fbuser = $id_or_email->fbconnect;
366 }else if ( !empty($id_or_email->user_id) ) {
367 $id = (int) $id_or_email->user_id;
368 $user = get_userdata($id);
369 $username = $user->display_name;
370 if ( $user && isset($user->fbconnect_userid) && $user->fbconnect_userid!=""){
371 $fbuser = $user->fbconnect_userid;
372 }
373 }
374 }else{
375 global $comment;
376 if (isset($comment) && $comment!="" && !empty($comment->fbconnect) && $comment->fbconnect!="0"){
377 $fbuser = $comment->fbconnect;
378 $id=$comment->user_id;
379 }
380 }
381 if ($fbuser != "" && $fbuser != 0){
382 return "<span class=\"avatar\"><a onclick=\"location.href='".get_option('siteurl')."/?fbconnect_action=myhome&amp;userid=".$id."';\" href=\"".get_option('siteurl')."/?fbconnect_action=myhome&amp;userid=".$id."\"><fb:profile-pic uid=\"".$fbuser."\" size=\"square\" linked=\"false\"></fb:profile-pic></a></span>";
383 }elseif($id!=""){
384 return "<a href=\"".get_option('siteurl')."/?fbconnect_action=myhome&amp;userid=".$id."\">".$avatar."</a>";
385 }
386
387 return $avatar;
388 }
389
390
391
392 function set_error($error) {
393 $_SESSION['fb_error'] = $error;
394 return;
395 }
396
397 function get_user(){
398 $fb_user ="";
399 $wpuser ="";
400 if (isset($_REQUEST['userid']) && $_REQUEST['userid']!=""){
401 $wpuser = $_REQUEST['userid'];
402 }elseif (isset($_REQUEST['fbuserid']) && $_REQUEST['fbuserid']!=""){
403 $fb_user = $_REQUEST['fbuserid'];
404 }else{
405 $fb_user = fb_get_loggedin_user();
406 }
407
408 if ($fb_user!=""){
409 $userprofile = WPfbConnect_Logic::get_userbyFBID($fb_user);
410 }elseif($wpuser){
411 $userprofile= get_userdata($wpuser);
412 }
413 return $userprofile;
414 }
415
416 function fbconnect_init_scripts(){
417 if (get_option('fb_use_ssl')){
418 echo "<script src='".get_ssl_root()."/js/api_lib/v0.4/FeatureLoader.js.php' type='text/javascript'></script>\n";
419 }else{
420 echo "<script src='".get_static_root()."/js/api_lib/v0.4/FeatureLoader.js.php' type='text/javascript'></script>\n";
421 }
422 echo "<script type='text/javascript'>\n";
423 echo "FB_RequireFeatures([\"XFBML\"], function(){\n";
424 if (get_option('fb_use_ssl')){
425 echo "FB.Facebook.init(\"".get_api_key()."\", \"".FBCONNECT_PLUGIN_URL. "/xd_receiver_ssl.htm\");\n";
426 }else{
427 echo "FB.Facebook.init(\"".get_api_key()."\", \"".FBCONNECT_PLUGIN_URL. "/xd_receiver.htm\");\n";
428 }
429 $fb_user = fb_get_loggedin_user();
430 $uri = "";
431 if (isset($_SERVER["REQUEST_URI"])){
432 $uri = $_SERVER["REQUEST_URI"];
433 }
434
435/* if (!$fb_user){
436 echo " FB.Facebook.get_sessionState().waitUntilReady(function(session){\n";
437 echo " var is_now_logged_into_facebook = session ? true : false;\n";
438 echo " alert(\">\"+ is_now_logged_into_facebook);\n" ;
439 echo " if (is_now_logged_into_facebook){\n";
440 echo " window.location = \"".$uri."\";}\n";
441 echo "});\n";
442 }*/
443 echo "});\n";
444 echo "</script>\n";
445 fb_showFeedDialog();
446 }
447
448//Replace user comments url with profile url
449function get_comment_author_url($url){
450 global $comment;
451 if ($comment->user_id!="" && $comment->user_id!="0"){
452 return get_option('siteurl')."/?fbconnect_action=myhome&amp;userid=".$comment->user_id;
453 }
454 return $url;
455}
456
457}
458endif; // end if-class-exists test
459
460?>
Note: See TracBrowser for help on using the repository browser.