| 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 [email protected] so we can mail you a copy immediately. |
|---|
| 16 | * |
|---|
| 17 | * @category Authentication |
|---|
| 18 | * @package Auth |
|---|
| 19 | * @author Stoyan Stefanov <[email protected]> |
|---|
| 20 | * @author Adam Ashley <[email protected]> |
|---|
| 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 8713 2006-12-01 05:08:34Z kakinaka $ |
|---|
| 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 <[email protected]> |
|---|
| 48 | * @author Adam Ashley <[email protected]> |
|---|
| 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.4.2 File: $Revision: 8713 $ |
|---|
| 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 | $connection_options = $this->options; |
|---|
| 120 | $connection_options['USER'] = $username; |
|---|
| 121 | $connection_options['PASSWD'] = $password; |
|---|
| 122 | $rfc = saprfc_open($connection_options); |
|---|
| 123 | if (!$rfc) { |
|---|
| 124 | $message = "Couldn't connect to the SAP system."; |
|---|
| 125 | $error = $this->getError(); |
|---|
| 126 | if ($error['message']) { |
|---|
| 127 | $message .= ': ' . $error['message']; |
|---|
| 128 | } |
|---|
| 129 | PEAR::raiseError($message, null, null, null, @$erorr['all']); |
|---|
| 130 | return false; |
|---|
| 131 | } else { |
|---|
| 132 | if (!empty($this->options['GETSSO2'])) { |
|---|
| 133 | if ($ticket = @saprfc_get_ticket($rfc)) { |
|---|
| 134 | $this->options['MYSAPSSO2'] = $ticket; |
|---|
| 135 | unset($this->options['GETSSO2']); |
|---|
| 136 | $this->_auth_obj->setAuthData('sap', $this->options); |
|---|
| 137 | } else { |
|---|
| 138 | PEAR::raiseError("SSO ticket retrieval failed"); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | @saprfc_close($rfc); |
|---|
| 142 | return true; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | // }}} |
|---|
| 148 | // {{{ getError() |
|---|
| 149 | |
|---|
| 150 | /** |
|---|
| 151 | * Retrieves the last error from the SAP connection |
|---|
| 152 | * and returns it as an array. |
|---|
| 153 | * |
|---|
| 154 | * @return array Array of error information |
|---|
| 155 | */ |
|---|
| 156 | function getError() |
|---|
| 157 | { |
|---|
| 158 | |
|---|
| 159 | $error = array(); |
|---|
| 160 | $sap_error = saprfc_error(); |
|---|
| 161 | if (empty($err)) { |
|---|
| 162 | return $error; |
|---|
| 163 | } |
|---|
| 164 | $err = explode("n", $sap_error); |
|---|
| 165 | foreach ($err AS $line) { |
|---|
| 166 | $item = split(':', $line); |
|---|
| 167 | $error[strtolower(trim($item[0]))] = trim($item[1]); |
|---|
| 168 | } |
|---|
| 169 | $error['all'] = $sap_error; |
|---|
| 170 | return $error; |
|---|
| 171 | } |
|---|
| 172 | |
|---|
| 173 | // }}} |
|---|
| 174 | |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | ?> |
|---|