]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/WikiUser/Facebook.php
Reformat code
[SourceForge/phpwiki.git] / lib / WikiUser / Facebook.php
1 <?php
2
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     /**
34      * Preferences are handled in _PassUser
35      */
36     function checkPass($password)
37     {
38         $userid = $this->_userid;
39         if (!loadPhpExtension('openssl')) {
40             trigger_error(
41                 sprintf(_("The PECL %s extension cannot be loaded."), "openssl")
42                     . sprintf(_(" %s AUTH ignored."), 'Facebook'),
43                 E_USER_WARNING);
44             return $this->_tryNextUser();
45         }
46         $web = new HttpClient("www.facebook.com", 80);
47         if (DEBUG & _DEBUG_LOGIN) $web->setDebug(true);
48         // collect cookies from http://www.facebook.com/login.php
49         $web->persist_cookies = true;
50         $web->cookie_host = 'www.facebook.com';
51         $firstlogin = $web->get("/login.php");
52         if (!$firstlogin) {
53             if (DEBUG & (_DEBUG_LOGIN | _DEBUG_VERBOSE))
54                 trigger_error(sprintf(_("Facebook connect failed with %d %s"),
55                         $web->status, $web->errormsg),
56                     E_USER_WARNING);
57         }
58         // Switch from http to https://login.facebook.com/login.php
59         $web->port = 443;
60         $web->host = 'login.facebook.com';
61         if (!($retval = $web->post("/login.php", array('user' => $userid, 'pass' => $password)))) {
62             if (DEBUG & (_DEBUG_LOGIN | _DEBUG_VERBOSE))
63                 trigger_error(sprintf(_("Facebook login failed with %d %s"),
64                         $web->status, $web->errormsg),
65                     E_USER_WARNING);
66         }
67         $this->_authmethod = 'Facebook';
68         if (DEBUG & _DEBUG_LOGIN) trigger_error(get_class($this) . "::checkPass => $retval",
69             E_USER_WARNING);
70         if ($retval) {
71             $this->_level = WIKIAUTH_USER;
72         } else {
73             $this->_level = WIKIAUTH_ANON;
74         }
75         return $this->_level;
76     }
77
78     // TODO: msearch facebook for the username
79     function userExists()
80     {
81         if (!loadPhpExtension('openssl')) {
82             trigger_error(
83                 sprintf(_("The PECL %s extension cannot be loaded."), "openssl")
84                     . sprintf(_(" %s AUTH ignored."), 'Facebook'),
85                 E_USER_WARNING);
86             return $this->_tryNextUser();
87         }
88         if (DEBUG & _DEBUG_LOGIN)
89             trigger_error(get_class($this) . "::userExists => true (dummy)", E_USER_WARNING);
90         return true;
91     }
92 }
93
94 // Local Variables:
95 // mode: php
96 // tab-width: 8
97 // c-basic-offset: 4
98 // c-hanging-comment-ender-p: nil
99 // indent-tabs-mode: nil
100 // End: