f5f26563cf6e545ab46e95d01f2ff419ea7f5c37
[tine20] / tine20 / Tinebase / Server / Abstract.php
1 <?php
2 /**
3  * Tine 2.0
4  * 
5  * @package     Tinebase
6  * @subpackage  Server
7  * @license     http://www.gnu.org/licenses/agpl.html AGPL Version 3
8  * @copyright   Copyright (c) 2013-2014 Metaways Infosystems GmbH (http://www.metaways.de)
9  * @author      Philipp Schüle <p.schuele@metaways.de>
10  * 
11  */
12
13 /**
14  * Server Abstract with handle function
15  * 
16  * @package     Tinebase
17  * @subpackage  Server
18  */
19 abstract class Tinebase_Server_Abstract implements Tinebase_Server_Interface
20 {
21     /**
22      * the request
23      *
24      * @var \Zend\Http\PhpEnvironment\Request
25      */
26     protected $_request = NULL;
27     
28     /**
29      * the request body
30      * 
31      * @var stream|string
32      */
33     protected $_body;
34     
35     /**
36      * set to true if server supports sessions
37      * 
38      * @var boolean
39      */
40     protected $_supportsSessions = false;
41     
42     /**
43      * 
44      */
45     public function __construct()
46     {
47         if ($this->_supportsSessions) {
48             Tinebase_Session_Abstract::setSessionEnabled('TINE20SESSID');
49         }
50     }
51     
52     /**
53      * read auth data from all available sources
54      * 
55      * @param \Zend\Http\PhpEnvironment\Request $request
56      * @throws Tinebase_Exception_NotFound
57      * @return array
58      */
59     protected function _getAuthData(\Zend\Http\PhpEnvironment\Request $request)
60     {
61         if ($authData = $this->_getPHPAuthData($request)) {
62             return $authData;
63         }
64         
65         if ($authData = $this->_getBasicAuthData($request)) {
66             return $authData;
67         }
68         
69         throw new Tinebase_Exception_NotFound('No auth data found');
70     }
71     
72     /**
73      * fetch auch from PHP_AUTH*
74      * 
75      * @param  \Zend\Http\PhpEnvironment\Request  $request
76      * @return array
77      */
78     protected function _getPHPAuthData(\Zend\Http\PhpEnvironment\Request $request)
79     {
80         if ($request->getServer('PHP_AUTH_USER')) {
81             return array(
82                 $request->getServer('PHP_AUTH_USER'),
83                 $request->getServer('PHP_AUTH_PW')
84             );
85         }
86     }
87     
88     /**
89      * fetch basic auth credentials
90      * 
91      * @param  \Zend\Http\PhpEnvironment\Request  $request
92      * @return array
93      */
94     protected function _getBasicAuthData(\Zend\Http\PhpEnvironment\Request $request)
95     {
96         if ($header = $request->getHeaders('Authorization')) {
97             return explode(
98                 ":",
99                 base64_decode(substr($header->getFieldValue(), 6)),  // "Basic didhfiefdhfu4fjfjdsa34drsdfterrde..."
100                 2
101             );
102             
103         } elseif ($header = $request->getServer('HTTP_AUTHORIZATION')) {
104             return explode(
105                 ":",
106                 base64_decode(substr($header, 6)),  // "Basic didhfiefdhfu4fjfjdsa34drsdfterrde..."
107                 2
108             );
109             
110         } else {
111             // check if (REDIRECT_)*REMOTE_USER is found in SERVER vars
112             $name = 'REMOTE_USER';
113             
114             for ($i=0; $i<5; $i++) {
115                 if ($header = $request->getServer($name)) {
116                     return explode(
117                         ":",
118                         base64_decode(substr($header, 6)),  // "Basic didhfiefdhfu4fjfjdsa34drsdfterrde..."
119                         2
120                     );
121                 }
122                 
123                 $name = 'REDIRECT_' . $name;
124             }
125         }
126     }
127
128     /**
129      * get default modelconfig methods
130      *
131      * @return array of Zend_Server_Method_Definition
132      *
133      * // TODO add caching?
134      */
135     protected static function _getModelConfigMethods($frontend)
136     {
137         // get all apps user has RUN right for
138         try {
139             $userApplications = Tinebase_Core::getUser() ? Tinebase_Core::getUser()->getApplications() : array();
140         } catch (Tinebase_Exception_NotFound $tenf) {
141             // session might be invalid, destroy it
142             Tinebase_Session::destroyAndRemoveCookie();
143             $userApplications = array();
144         }
145
146         $definitions = array();
147         foreach ($userApplications as $application) {
148             try {
149                 $controller = Tinebase_Core::getApplicationInstance($application->name);
150                 $models = $controller->getModels();
151                 if (!$models) {
152                     continue;
153                 }
154             } catch (Exception $e) {
155                 Tinebase_Exception::log($e);
156                 continue;
157             }
158
159             foreach ($models as $model) {
160                 $config = $model::getConfiguration();
161                 if ($frontend::exposeApi($config)) {
162                     $simpleModelName = Tinebase_Record_Abstract::getSimpleModelName($application, $model);
163                     $commonApiMethods = $frontend::_getCommonApiMethods($application, $simpleModelName);
164
165                     foreach ($commonApiMethods as $name => $method) {
166                         $key = $application->name . '.' . $name . $simpleModelName . ($method['plural'] ? 's' : '');
167                         $object = $frontend::_getFrontend($application);
168
169                         $definitions[$key] = new Zend_Server_Method_Definition(array(
170                             'name'            => $key,
171                             'prototypes'      => array(array(
172                                 'returnType' => 'array',
173                                 'parameters' => $method['params']
174                             )),
175                             'methodHelp'      => $method['help'],
176                             'invokeArguments' => array(),
177                             'object'          => $object,
178                             'callback'        => array(
179                                 'type'   => 'instance',
180                                 'class'  => get_class($object),
181                                 'method' => $name . $simpleModelName . ($method['plural'] ? 's' : '')
182                             ),
183                         ));
184                     }
185                 }
186             }
187         }
188
189         if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__
190             . ' Got MC definitions: ' . print_r(array_keys($definitions), true));
191
192         return $definitions;
193     }
194 }