upload
[pear] / Auth / Container / SAP.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker: */
3
4 /**
5  * Storage driver for use against a SAP system using the SAPRFC PHP extension.
6  *
7  * Requires the SAPRFC ext available at http://saprfc.sourceforge.net/
8  *
9  * PHP version 5
10  *
11  * LICENSE: This source file is subject to version 3.01 of the PHP license
12  * that is available through the world-wide-web at the following URI:
13  * http://www.php.net/license/3_01.txt.  If you did not receive a copy of
14  * the PHP License and are unable to obtain it through the web, please
15  * send a note to license@php.net so we can mail you a copy immediately.
16  *
17  * @category   Authentication
18  * @package    Auth
19  * @author     Stoyan Stefanov <ssttoo@gmail.com>
20  * @author     Adam Ashley <aashley@php.net>
21  * @copyright  2001-2006 The PHP Group
22  * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
23  * @version    CVS: $Id: SAP.php 289654 2009-10-15 04:52:48Z aashley $
24  * @link       http://pear.php.net/package/Auth
25  * @since      File available since Release 1.4.0
26  */
27
28 /**
29  * Include Auth_Container base class
30  */
31 require_once 'Auth/Container.php';
32 /**
33  * Include PEAR for error handling
34  */
35 require_once 'PEAR.php';
36
37 /**
38  * Performs authentication against a SAP system using the SAPRFC PHP extension.
39  *
40  * When the option GETSSO2 is TRUE (default)
41  * the Single Sign-On (SSO) ticket is retrieved
42  * and stored as an Auth attribute called 'sap'
43  * in order to be reused for consecutive connections.
44  *
45  * @category   Authentication
46  * @package    Auth
47  * @author     Stoyan Stefanov <ssttoo@gmail.com>
48  * @author     Adam Ashley <aashley@php.net>
49  * @copyright  2001-2006 The PHP Group
50  * @license    http://www.php.net/license/3_01.txt  PHP License 3.01
51  * @version    Release: 1.6.2  File: $Revision: 289654 $
52  * @since      Class available since Release 1.4.0
53  */
54 class Auth_Container_SAP extends Auth_Container {
55
56     // {{{ properties
57
58     /**
59      * @var array Default options
60      */
61     var $options = array(
62         'CLIENT'    => '000',
63         'LANG'      => 'EN',
64         'GETSSO2'   => true,
65     );
66
67     // }}}
68     // {{{ Auth_Container_SAP()
69
70     /**
71      * Class constructor. Checks that required options
72      * are present and that the SAPRFC extension is loaded
73      *
74      * Options that can be passed and their defaults:
75      * <pre>
76      * array(
77      *   'ASHOST' => "",
78      *   'SYSNR'  => "",
79      *   'CLIENT' => "000",
80      *   'GWHOST' =>"",
81      *   'GWSERV' =>"",
82      *   'MSHOST' =>"",
83      *   'R3NAME' =>"",
84      *   'GROUP'  =>"",
85      *   'LANG'   =>"EN",
86      *   'TRACE'  =>"",
87      *   'GETSSO2'=> true
88      * )
89      * </pre>
90      *
91      * @param array array of options.
92      * @return void
93      */
94     function Auth_Container_SAP($options)
95     {
96         $saprfc_loaded = PEAR::loadExtension('saprfc');
97         if (!$saprfc_loaded) {
98             return PEAR::raiseError('Cannot use SAP authentication, '
99                     .'SAPRFC extension not loaded!');
100         }
101         if (empty($options['R3NAME']) && empty($options['ASHOST'])) {
102             return PEAR::raiseError('R3NAME or ASHOST required for authentication');
103         }
104         $this->options = array_merge($this->options, $options);
105     }
106
107     // }}}
108     // {{{ fetchData()
109
110     /**
111      * Performs username and password check
112      *
113      * @param string Username
114      * @param string Password
115      * @return boolean TRUE on success (valid user), FALSE otherwise
116      */
117     function fetchData($username, $password)
118     {
119         $this->log('Auth_Container_SAP::fetchData() called.', AUTH_LOG_DEBUG);
120         $connection_options = $this->options;
121         $connection_options['USER'] = $username;
122         $connection_options['PASSWD'] = $password;
123         $rfc = saprfc_open($connection_options);
124         if (!$rfc) {
125             $message = "Couldn't connect to the SAP system.";
126             $error = $this->getError();
127             if ($error['message']) {
128                 $message .= ': ' . $error['message'];
129             }
130             PEAR::raiseError($message, null, null, null, @$error['all']);
131             return false;
132         } else {
133             if (!empty($this->options['GETSSO2'])) {
134                 $this->log('Attempting to retrieve SSO2 ticket.', AUTH_LOG_DEBUG);
135                 if ($ticket = @saprfc_get_ticket($rfc)) {
136                     $this->options['MYSAPSSO2'] = $ticket;
137                     unset($this->options['GETSSO2']);
138                     $this->_auth_obj->setAuthData('sap', $this->options);
139                 } else {
140                     PEAR::raiseError("SSO ticket retrieval failed");
141                 }
142             }
143             @saprfc_close($rfc);
144             return true;
145         }
146
147     }
148
149     // }}}
150     // {{{ getError()
151
152     /**
153      * Retrieves the last error from the SAP connection
154      * and returns it as an array.
155      *
156      * @return array Array of error information
157      */
158     function getError()
159     {
160
161         $error = array();
162         $sap_error = saprfc_error();
163         if (empty($err)) {
164             return $error;
165         }
166         $err = explode("n", $sap_error);
167         foreach ($err AS $line) {
168             $item = split(':', $line);
169             $error[strtolower(trim($item[0]))] = trim($item[1]);
170         }
171         $error['all'] = $sap_error;
172         return $error;
173     }
174
175     // }}}
176
177 }
178
179 ?>