Subversion Repositories Sites.obs-saisons.fr

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 aurelien 1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
3
 
4
/**
5
 * Storage driver for use against PEAR website
6
 *
7
 * PHP versions 4 and 5
8
 *
9
 * LICENSE: This source file is subject to version 3.01 of the PHP license
10
 * that is available through the world-wide-web at the following URI:
11
 * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
12
 * the PHP License and are unable to obtain it through the web, please
13
 * send a note to license@php.net so we can mail you a copy immediately.
14
 *
15
 * @category   Authentication
16
 * @package    Auth
17
 * @author     Yavor Shahpasov <yavo@netsmart.com.cy>
18
 * @author     Adam Ashley <aashley@php.net>
19
 * @copyright  2001-2006 The PHP Group
20
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
21
 * @version    CVS: $Id: PEAR.php,v 1.13 2008/04/04 00:53:53 aashley Exp $
22
 * @link       http://pear.php.net/package/Auth
23
 * @since      File available since Release 1.3.0
24
 */
25
 
26
/**
27
 * Include PEAR HTTP_Client.
28
 */
29
require_once 'HTTP/Client.php';
30
/**
31
 * Include Auth_Container base class
32
 */
33
require_once 'Auth/Container.php';
34
 
35
/**
36
 * Storage driver for authenticating against PEAR website
37
 *
38
 * This driver provides a method for authenticating against the pear.php.net
39
 * authentication system.
40
 *
41
 * Supports two options:
42
 * - "url": The base URL with schema to authenticate against
43
 * - "karma": An array of karma levels which the user needs one of.
44
 *            When empty, no karma level is required.
45
 *
46
 * @category   Authentication
47
 * @package    Auth
48
 * @author     Yavor Shahpasov <yavo@netsmart.com.cy>
49
 * @author     Adam Ashley <aashley@php.net>
50
 * @author     Adam Harvey <aharvey@php.net>
51
 * @copyright  2001-2007 The PHP Group
52
 * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
53
 * @version    Release: 1.6.1  File: $Revision: 1.13 $
54
 * @link       http://pear.php.net/package/Auth
55
 * @since      Class available since Release 1.3.0
56
 */
57
class Auth_Container_Pear extends Auth_Container
58
{
59
    // {{{ properties
60
 
61
    /**
62
     * URL to connect to, with schema
63
     *
64
     * @var string
65
     */
66
    var $url = 'https://pear.php.net/rest-login.php/';
67
 
68
    /**
69
     * Array of karma levels the user can have.
70
     * A user needs only one of the levels to succeed login.
71
     * No levels mean that only username and password need to match
72
     *
73
     * @var array
74
     */
75
    var $karma = array();
76
 
77
    // }}}
78
    // {{{ Auth_Container_Pear() [constructor]
79
 
80
    /**
81
     * Constructor
82
     *
83
     * Accepts options "url" and "karma", see class docs.
84
     *
85
     * @param array $data Array of options
86
     *
87
     * @return void
88
     */
89
    function Auth_Container_Pear($data = null)
90
    {
91
        if (!is_array($data)) {
92
            PEAR::raiseError('The options for Auth_Container_Pear must be an array');
93
        }
94
        if (isset($data['karma'])) {
95
            if (is_array($data['karma'])) {
96
                $this->karma = $data['karma'];
97
            } else {
98
                $this->karma = array($data['karma']);
99
            }
100
        }
101
 
102
        if (isset($data['url'])) {
103
            $this->url = $data['url'];
104
        }
105
    }
106
 
107
    // }}}
108
    // {{{ fetchData()
109
 
110
    /**
111
     * Get user information from pear.php.net
112
     *
113
     * This function uses the given username and password to authenticate
114
     * against the pear.php.net website
115
     *
116
     * @param string    Username
117
     * @param string    Password
118
     * @return mixed    Error object or boolean
119
     */
120
    function fetchData($username, $password)
121
    {
122
        $this->log('Auth_Container_PEAR::fetchData() called.', AUTH_LOG_DEBUG);
123
 
124
        $client = new HTTP_Client;
125
 
126
        $this->log('Auth_Container_PEAR::fetchData() getting salt.', AUTH_LOG_DEBUG);
127
        $code = $client->get($this->url . '/getsalt');
128
        if ($code != 200) {
129
            return PEAR::raiseError('Bad response to salt request.', $code);
130
        }
131
        $resp = $client->currentResponse();
132
        $salt = $resp['body'];
133
 
134
        $this->log('Auth_Container_PEAR::fetchData() calling validate.', AUTH_LOG_DEBUG);
135
        $postOptions = array(
136
            'username' => $username,
137
            'password' => md5($salt . md5($password))
138
        );
139
        if (is_array($this->karma) && count($this->karma) > 0) {
140
            $postOptions['karma'] = implode(',', $this->karma);
141
        }
142
 
143
        $code = $client->post($this->url . '/validate', $postOptions);
144
        if ($code != 200) {
145
            return PEAR::raiseError('Bad response to validate request.', $code);
146
        }
147
        $resp = $client->currentResponse();
148
 
149
        list($code, $message) = explode(' ', $resp['body'], 1);
150
        if ($code != 8) {
151
            return PEAR::raiseError($message, $code);
152
        }
153
        return true;
154
    }
155
 
156
    // }}}
157
 
158
}
159
?>