updated copyright for 2013
[e-DoKo.git] / include / login.php
1 <?php
2 /* Copyright 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013 Arun Persaud <arun@nubati.net>
3  *
4  *   This file is part of e-DoKo.
5  *
6  *   e-DoKo is free software: you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation, either version 3 of the License, or
9  *   (at your option) any later version.
10  *
11  *   e-DoKo is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with e-DoKo.  If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20
21 /* make sure that we are not called from outside the scripts,
22  * use a variable defined in config.php to check this
23  */
24 if(!isset($HOST))
25   exit;
26
27 include_once('openid.php');
28
29 function escape($thing) {
30     return htmlentities($thing);
31 }
32
33 /* check for openid stuff */
34 if($OPENIDPATH && myisset('openid_identity') && $_REQUEST['openid_identity']!='')
35   {
36     /* what openid is being used? */
37     $openid_url = OpenIDUrlEncode($_REQUEST['openid_identity']);
38     /* get the userid from the database, openids need to be registered within E-DoKo */
39     $data = OpenIDVerify();
40     $ok  = 0;
41
42     /* verify ok? */
43     if($data)
44       {
45         /* do we know this openid?*/
46         $myid = DB_GetUserId($openid_url);
47
48         if(!$myid)
49           {
50             /* openid unknown, perhaps not registered? */
51             echo "<p>Openid ok, but not registered with any account. If you have an account ".
52               "on E-DoKo, please log in and add your openid in your preferences first. </p>\n";
53
54
55             /* or perhaps a new user...*/
56             $email = $data['email'];
57             $name  = $data['fullname'];
58             echo "<p>If you wan to register a new account with this OpenID, please follow this ".
59               "<a href=\"index.php?action=register&amp;openid_url=".$openid_url.
60               "&amp;openidname=$name&amp;openidemail=$email\">link</a>.</p>";
61           }
62         else
63           $ok=1;
64       }
65
66     if($ok)
67       {
68         /* user information is ok, set session variabel */
69         $email  = DB_get_email('userid',$myid);
70         $myname = DB_get_name('email',$email);
71         $password =  DB_get_passwd_by_userid($myid);
72         $_SESSION['name'] = $myname;
73         $_SESSION['id']   = $myid;
74         $_SESSION['pass'] = $password;
75       }
76   }
77 else if($OPENIDPATH && myisset('openid_url') && $_REQUEST['openid_url']!='')
78   {
79     OpenIDAskForVerification(OpenIDUrlEncode($_REQUEST['openid_url']));
80   }
81 /* check if normal login information is present */
82 else if(myisset('email','password'))
83   {
84     $email     = $_REQUEST['email'];
85     $password  = $_REQUEST['password'];
86
87     /* verify password and email */
88
89     $ok  = 1;
90     $myid = DB_get_userid('email',$email);
91
92     $result = verify_password($email, $password);
93     switch($result)
94       {
95       case 0:
96         /* user information is ok, set session variable */
97         $myname         = DB_get_name('email',$email);
98         $hashedpassword = DB_get_passwd_by_userid($myid);
99         $_SESSION['name'] = $myname;
100         $_SESSION['id']   = $myid;
101         $_SESSION['pass'] = $hashedpassword;
102         break;
103       case 1:
104         echo "Can't find you in the database\n";
105         break;
106       case 2:
107         echo "Problem creating password hash, please contact $ADMIN at $ADMIN_EMAIL\n";
108         break;
109       }
110   }
111 else
112   {
113     echo "can't log you in... missing login information.";
114   }
115 ?>