]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/Facebook.php
rcs_id no longer makes sense with Subversion global version number
[SourceForge/phpwiki.git] / lib / WikiUser / Facebook.php
1 <?php //-*-php-*-
2 // rcs_id('$Id$');
3 /* Copyright (C) 2009 Reini Urban
4  * This file is part of PhpWiki. Terms and Conditions see LICENSE. (GPL2)
5  *
6  * From http://developeronline.blogspot.com/2008/10/using-perl-against-facebook-part-i.html:
7  * GET 'http://www.facebook.com/login.php', and rest our virtual browser there to collect the cookies
8  * POST to 'https://login.facebook.com/login.php' with the proper parameters
9  */
10
11 // requires the openssl extension
12 require_once("lib/HttpClient.php");
13
14 class _FacebookPassUser
15 extends _PassUser {
16     /**
17      * Preferences are handled in _PassUser
18      */
19     function checkPass($password) {
20         $userid = $this->_userid;
21         if (!loadPhpExtension('openssl')) {
22             trigger_error(_("The PECL openssl extension cannot be loaded. Facebook AUTH ignored."),
23                           E_USER_WARNING);
24             return $this->_tryNextUser();
25         }
26         $web = new HttpClient("www.facebook.com", 80);
27         if (DEBUG & _DEBUG_LOGIN) $web->setDebug(true);
28         // collect cookies from http://www.facebook.com/login.php
29         $web->persist_cookies = true;
30         $web->cookie_host = 'www.facebook.com';
31         $firstlogin = $web->get("/login.php");
32         if (!$firstlogin) {
33             if (DEBUG & (_DEBUG_LOGIN | _DEBUG_VERBOSE))
34                 trigger_error(sprintf(_("Facebook connect failed with %d %s"),
35                                       $web->status, $web->errormsg),
36                               E_USER_WARNING);
37         }
38         // Switch from http to https://login.facebook.com/login.php
39         $web->port = 443;
40         $web->host = 'login.facebook.com';
41         if (!($retval = $web->post("/login.php", array('user'=>$userid, 'pass'=>$password)))) {
42             if (DEBUG & (_DEBUG_LOGIN | _DEBUG_VERBOSE))
43                 trigger_error(sprintf(_("Facebook login failed with %d %s"),
44                                       $web->status, $web->errormsg),
45                               E_USER_WARNING);
46         }
47         $this->_authmethod = 'Facebook';
48         if (DEBUG & _DEBUG_LOGIN) trigger_error(get_class($this)."::checkPass => $retval",
49                                                 E_USER_WARNING);
50         if ($retval) {
51             $this->_level = WIKIAUTH_USER;
52         } else {
53             $this->_level = WIKIAUTH_ANON;
54         }
55         return $this->_level;
56     }
57
58     // TODO: msearch facebook for the username
59     function userExists() {
60         if (!loadPhpExtension('openssl')) {
61             trigger_error(_("The PECL openssl extension cannot be loaded. Facebook AUTH ignored."),
62                           E_USER_WARNING);
63             return $this->_tryNextUser();
64         }
65         if (DEBUG & _DEBUG_LOGIN)
66             trigger_error(get_class($this)."::userExists => true (dummy)", E_USER_WARNING);
67         return true;
68     }
69 }
70
71 // Local Variables:
72 // mode: php
73 // tab-width: 8
74 // c-basic-offset: 4
75 // c-hanging-comment-ender-p: nil
76 // indent-tabs-mode: nil
77 // End:
78 ?>