1<?php
2/**
3 * Facebook authentication backend
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Václav Voborník <nomail@vobornik.eu>
7 */
8
9define('DOKU_AUTH', dirname(__FILE__));
10require_once(DOKU_AUTH.'/basic.class.php');
11require_once(DOKU_AUTH.'/../../lib/plugins/fblogin/lib/facebook.php');
12
13#define('AUTH_USERFILE',DOKU_CONF.'users.auth.php');
14
15class auth_facebook extends auth_basic {
16
17  var $users = null;
18  var $_pattern = array();
19
20  var $fbsession = array();
21
22  /**
23   * Constructor
24   *
25   * Carry out sanity checks to ensure the object is
26   * able to operate. Set capabilities.
27   *
28   * @author  Václav Voborník <nomail@vobornik.eu>
29   */
30  function auth_facebook() {
31    global $conf;
32    $this->cando['external']     = true;
33
34    if (!isset($conf['plugin']['fblogin']['applicationID']) || empty($conf['plugin']['fblogin']['applicationID'])) {
35      $this->success = false;
36      return;
37    }
38
39    if (!isset($conf['plugin']['fblogin']['applicationSecret']) || empty($conf['plugin']['fblogin']['applicationSecret'])) {
40      $this->success = false;
41      return;
42    }
43
44
45
46    $this->success = true;
47    return;
48
49  }
50  function trustExternal($user,$pass,$sticky=true ){
51    global $USERINFO;
52    global $conf;
53    $sticky ? $sticky = true : $sticky = false; //sanity check
54
55
56
57  if ($conf['plugin']['fblogin']['applicationID'] && $conf['plugin']['fblogin']['applicationSecret']) {
58
59    $facebook = new Facebook(array(
60      'appId'      => $conf['plugin']['fblogin']['applicationID'],
61      'secret'     => $conf['plugin']['fblogin']['applicationSecret'],
62      'cookie'     => true,
63    ));
64    $fbsession = $facebook->getUser();
65
66      if($_REQUEST['do'] == 'logout'){
67        $logoutUrl = $facebook->getLogoutUrl(
68          array(
69            'next' => $_SERVER['HTTP_REFERER'],
70          )
71        );
72        unset($fbsession);
73#        unset($_SESSION[DOKU_COOKIE]['auth']['user']);
74#        unset($_SESSION[DOKU_COOKIE]['auth']['buid']);
75#        unset($_SESSION[DOKU_COOKIE]['auth']['pass']);
76#        unset($_SESSION[DOKU_COOKIE]['auth']['info']);
77        session_destroy();
78        error_log('fblogin : authenticated user redirected for logout to '.$logoutUrl);
79        header("Location: ".$logoutUrl);
80        exit;
81      }
82    if ($fbsession) {
83      try {
84        $me = $facebook->api('/me');
85#        $friends = $facebook->api('/me/friends');  // for future usage
86
87      } catch (FacebookApiException $e) {
88        error_log($e);
89      }
90      if ($me) {
91        $conf['superuser']   = $conf['plugin']['fblogin']['superuser'];
92
93        $USERINFO['name'] = $me['name'];
94        $USERINFO['mail'] = $me['email'];
95        $USERINFO['grps'] = array('user');
96        $user = $me['id'];
97        $pass = '';
98        $_SERVER['REMOTE_USER'] = $user;
99        $_SESSION[DOKU_COOKIE]['auth']['user'] = $user;
100        $_SESSION[DOKU_COOKIE]['auth']['pass'] = $pass;
101        $_SESSION[DOKU_COOKIE]['auth']['info'] = $USERINFO;
102      } //me
103    }  // FB session
104
105
106    if($_REQUEST['do'] == 'login'){
107      $loginUrl = $facebook->getLoginUrl(
108        array(
109          'next' => $_SERVER['HTTP_REFERER'],
110          'redirect_uri' => $conf['baseurl'],
111          'canvas'    => 0,
112          'fbconnect' => 1,
113#          'req_perms' => 'publish_stream,status_update'   //for future usage
114        )
115      );
116      header("Location: ".$loginUrl);
117      exit;
118    }
119  }
120  return true;
121}
122
123    /**
124     * Check user+password [required auth function]
125     *
126     * Checks if the given user exists and the given
127     * plaintext password is correct
128     *
129     * @author
130     * @return  bool
131     */
132    function checkPass($user,$pass){
133
134    }
135
136    /**
137     * Return user info
138     *
139     * Returns info about the given user needs to contain
140     * at least these fields:
141     *
142     * name string  full name of the user
143     * mail string  email addres of the user
144     * grps array   list of groups the user is in
145     *
146     * @author  Václav Voborník <nomail@vobornik.eu>
147     */
148  function getUserData($user){
149    global $conf;
150
151
152    if (!$conf['plugin']['fblogin']['applicationID'] || !$conf['plugin']['fblogin']['applicationSecret']) {
153      return false;
154    }
155    $facebook = new Facebook(array(
156      'appId'      => $conf['plugin']['fblogin']['applicationID'],
157      'secret'     => $conf['plugin']['fblogin']['applicationSecret'],
158      'cookie'     => true,
159    ));
160    $fbsession = $facebook->getUser();
161
162    if ($fbsession) {
163      try {
164        $fbuser = $facebook->api("/$user");
165      } catch (FacebookApiException $e) {
166        error_log($e);
167      }
168      if ($fbuser) {
169        $row['name'] = $fbuser['name'];
170        $row['mail'] = $fbuser['mail'];
171        $row['grps'] = array ('users');
172
173        return $row;
174      }
175    }
176  }
177    /**
178     * Create a new User
179     *
180     * Returns false if the user already exists, null when an error
181     * occurred and true if everything went well.
182     *
183     * The new user will be added to the default group by this
184     * function if grps are not specified (default behaviour).
185     *
186     * @author
187     */
188    function createUser($user,$pwd,$name,$mail,$grps=null){
189    }
190
191    /**
192     * Modify user data
193     *
194     * @author
195     * @param   $user      nick of the user to be changed
196     * @param   $changes   array of field/value pairs to be changed (password will be clear text)
197     * @return  bool
198     */
199    function modifyUser($user, $changes) {
200      global $conf;
201      global $ACT;
202      global $INFO;
203
204    }
205
206    /**
207     *  Remove one or more users from the list of registered users
208     *
209     *  @author
210     *  @param   array  $users   array of users to be deleted
211     *  @return  int             the number of users deleted
212     */
213    function deleteUsers($users) {
214
215    }
216
217    /**
218     * Return a count of the number of user which meet $filter criteria
219     *
220     * @author
221     */
222    function getUserCount($filter=array()) {
223
224    }
225
226    /**
227     * Bulk retrieval of user data
228     *
229     * @author
230     * @param   start     index of first user to be returned
231     * @param   limit     max number of users to be returned
232     * @param   filter    array of field/pattern pairs
233     * @return  array of userinfo (refer getUserData for internal userinfo details)
234     */
235    function retrieveUsers($start=0,$limit=0,$filter=array()) {
236
237
238    }
239
240    /**
241     * Only valid pageid's (no namespaces) for usernames
242     */
243    function cleanUser($user){
244        global $conf;
245        return cleanID(str_replace(':',$conf['sepchar'],$user));
246    }
247
248    /**
249     * Only valid pageid's (no namespaces) for groupnames
250     */
251    function cleanGroup($group){
252        global $conf;
253        return cleanID(str_replace(':',$conf['sepchar'],$group));
254    }
255
256
257}
258
259//Setup VIM: ex: et ts=2 enc=utf-8 :
260