]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/Facebook.php
New FSF address
[SourceForge/phpwiki.git] / lib / WikiUser / Facebook.php
1 <?php //-*-php-*-
2 // $Id$
3 /*
4  * Copyright (C) 2009 Reini Urban
5  *
6  * This file is part of PhpWiki.
7  *
8  * PhpWiki is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * PhpWiki is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * From http://developeronline.blogspot.com/2008/10/using-perl-against-facebook-part-i.html:
23  * GET 'http://www.facebook.com/login.php', and rest our virtual browser there to collect the cookies
24  * POST to 'https://login.facebook.com/login.php' with the proper parameters
25  */
26
27 // requires the openssl extension
28 require_once("lib/HttpClient.php");
29
30 class _FacebookPassUser
31 extends _PassUser {
32     /**
33      * Preferences are handled in _PassUser
34      */
35     function checkPass($password) {
36         $userid = $this->_userid;
37         if (!loadPhpExtension('openssl')) {
38             trigger_error(
39                 sprintf(_("The PECL %s extension cannot be loaded."), "openssl")
40                  . sprintf(_(" %s AUTH ignored."), 'Facebook'),
41                  E_USER_WARNING);
42             return $this->_tryNextUser();
43         }
44         $web = new HttpClient("www.facebook.com", 80);
45         if (DEBUG & _DEBUG_LOGIN) $web->setDebug(true);
46         // collect cookies from http://www.facebook.com/login.php
47         $web->persist_cookies = true;
48         $web->cookie_host = 'www.facebook.com';
49         $firstlogin = $web->get("/login.php");
50         if (!$firstlogin) {
51             if (DEBUG & (_DEBUG_LOGIN | _DEBUG_VERBOSE))
52                 trigger_error(sprintf(_("Facebook connect failed with %d %s"),
53                                       $web->status, $web->errormsg),
54                               E_USER_WARNING);
55         }
56         // Switch from http to https://login.facebook.com/login.php
57         $web->port = 443;
58         $web->host = 'login.facebook.com';
59         if (!($retval = $web->post("/login.php", array('user'=>$userid, 'pass'=>$password)))) {
60             if (DEBUG & (_DEBUG_LOGIN | _DEBUG_VERBOSE))
61                 trigger_error(sprintf(_("Facebook login failed with %d %s"),
62                                       $web->status, $web->errormsg),
63                               E_USER_WARNING);
64         }
65         $this->_authmethod = 'Facebook';
66         if (DEBUG & _DEBUG_LOGIN) trigger_error(get_class($this)."::checkPass => $retval",
67                                                 E_USER_WARNING);
68         if ($retval) {
69             $this->_level = WIKIAUTH_USER;
70         } else {
71             $this->_level = WIKIAUTH_ANON;
72         }
73         return $this->_level;
74     }
75
76     // TODO: msearch facebook for the username
77     function userExists() {
78         if (!loadPhpExtension('openssl')) {
79             trigger_error(
80                 sprintf(_("The PECL %s extension cannot be loaded."), "openssl")
81                  . sprintf(_(" %s AUTH ignored."), 'Facebook'),
82                  E_USER_WARNING);
83             return $this->_tryNextUser();
84         }
85         if (DEBUG & _DEBUG_LOGIN)
86             trigger_error(get_class($this)."::userExists => true (dummy)", E_USER_WARNING);
87         return true;
88     }
89 }
90
91 // Local Variables:
92 // mode: php
93 // tab-width: 8
94 // c-basic-offset: 4
95 // c-hanging-comment-ender-p: nil
96 // indent-tabs-mode: nil
97 // End:
98 ?>