[7589] | 1 | <?php |
---|
| 2 | /*********************************************** |
---|
| 3 | * File : requestprocessor.php |
---|
| 4 | * Project : Z-Push |
---|
| 5 | * Descr : This file provides/loads the handlers |
---|
| 6 | * for the different commands. |
---|
| 7 | * The request handlers are optimised |
---|
| 8 | * so that as little as possible |
---|
| 9 | * data is kept-in-memory, and all |
---|
| 10 | * output data is directly streamed |
---|
| 11 | * to the client, while also streaming |
---|
| 12 | * input data from the client. |
---|
| 13 | * |
---|
| 14 | * Created : 12.08.2011 |
---|
| 15 | * |
---|
| 16 | * Copyright 2007 - 2012 Zarafa Deutschland GmbH |
---|
| 17 | * |
---|
| 18 | * This program is free software: you can redistribute it and/or modify |
---|
| 19 | * it under the terms of the GNU Affero General Public License, version 3, |
---|
| 20 | * as published by the Free Software Foundation with the following additional |
---|
| 21 | * term according to sec. 7: |
---|
| 22 | * |
---|
| 23 | * According to sec. 7 of the GNU Affero General Public License, version 3, |
---|
| 24 | * the terms of the AGPL are supplemented with the following terms: |
---|
| 25 | * |
---|
| 26 | * "Zarafa" is a registered trademark of Zarafa B.V. |
---|
| 27 | * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH |
---|
| 28 | * The licensing of the Program under the AGPL does not imply a trademark license. |
---|
| 29 | * Therefore any rights, title and interest in our trademarks remain entirely with us. |
---|
| 30 | * |
---|
| 31 | * However, if you propagate an unmodified version of the Program you are |
---|
| 32 | * allowed to use the term "Z-Push" to indicate that you distribute the Program. |
---|
| 33 | * Furthermore you may use our trademarks where it is necessary to indicate |
---|
| 34 | * the intended purpose of a product or service provided you use it in accordance |
---|
| 35 | * with honest practices in industrial or commercial matters. |
---|
| 36 | * If you want to propagate modified versions of the Program under the name "Z-Push", |
---|
| 37 | * you may only do so if you have a written permission by Zarafa Deutschland GmbH |
---|
| 38 | * (to acquire a permission please contact Zarafa at trademark@zarafa.com). |
---|
| 39 | * |
---|
| 40 | * This program is distributed in the hope that it will be useful, |
---|
| 41 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 42 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 43 | * GNU Affero General Public License for more details. |
---|
| 44 | * |
---|
| 45 | * You should have received a copy of the GNU Affero General Public License |
---|
| 46 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
| 47 | * |
---|
| 48 | * Consult LICENSE file for details |
---|
| 49 | ************************************************/ |
---|
| 50 | |
---|
| 51 | abstract class RequestProcessor { |
---|
| 52 | static protected $backend; |
---|
| 53 | static protected $deviceManager; |
---|
| 54 | static protected $topCollector; |
---|
| 55 | static protected $decoder; |
---|
| 56 | static protected $encoder; |
---|
| 57 | static protected $userIsAuthenticated; |
---|
| 58 | static protected $specialHeaders; |
---|
| 59 | |
---|
| 60 | /** |
---|
| 61 | * Authenticates the remote user |
---|
| 62 | * The sent HTTP authentication information is used to on Backend->Logon(). |
---|
| 63 | * As second step the GET-User verified by Backend->Setup() for permission check |
---|
| 64 | * Request::GetGETUser() is usually the same as the Request::GetAuthUser(). |
---|
| 65 | * If the GETUser is different from the AuthUser, the AuthUser MUST HAVE admin |
---|
| 66 | * permissions on GETUsers data store. Only then the Setup() will be sucessfull. |
---|
| 67 | * This allows the user 'john' to do operations as user 'joe' if he has sufficient privileges. |
---|
| 68 | * |
---|
| 69 | * @access public |
---|
| 70 | * @return |
---|
| 71 | * @throws AuthenticationRequiredException |
---|
| 72 | */ |
---|
| 73 | static public function Authenticate() { |
---|
| 74 | self::$userIsAuthenticated = false; |
---|
| 75 | |
---|
| 76 | $backend = ZPush::GetBackend(); |
---|
| 77 | if($backend->Logon(Request::GetAuthUser(), Request::GetAuthDomain(), Request::GetAuthPassword()) == false) |
---|
| 78 | throw new AuthenticationRequiredException("Access denied. Username or password incorrect"); |
---|
| 79 | |
---|
| 80 | // mark this request as "authenticated" |
---|
| 81 | self::$userIsAuthenticated = true; |
---|
| 82 | |
---|
| 83 | // check Auth-User's permissions on GETUser's store |
---|
| 84 | if($backend->Setup(Request::GetGETUser(), true) == false) |
---|
| 85 | throw new AuthenticationRequiredException(sprintf("Not enough privileges of '%s' to setup for user '%s': Permission denied", Request::GetAuthUser(), Request::GetGETUser())); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * Indicates if the user was "authenticated" |
---|
| 90 | * |
---|
| 91 | * @access public |
---|
| 92 | * @return boolean |
---|
| 93 | */ |
---|
| 94 | static public function isUserAuthenticated() { |
---|
| 95 | if (!isset(self::$userIsAuthenticated)) |
---|
| 96 | return false; |
---|
| 97 | return self::$userIsAuthenticated; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | /** |
---|
| 101 | * Initialize the RequestProcessor |
---|
| 102 | * |
---|
| 103 | * @access public |
---|
| 104 | * @return |
---|
| 105 | */ |
---|
| 106 | static public function Initialize() { |
---|
| 107 | self::$backend = ZPush::GetBackend(); |
---|
| 108 | self::$deviceManager = ZPush::GetDeviceManager(); |
---|
| 109 | self::$topCollector = ZPush::GetTopCollector(); |
---|
| 110 | |
---|
| 111 | if (!ZPush::CommandNeedsPlainInput(Request::GetCommandCode())) |
---|
| 112 | self::$decoder = new WBXMLDecoder(Request::GetInputStream()); |
---|
| 113 | |
---|
| 114 | self::$encoder = new WBXMLEncoder(Request::GetOutputStream(), Request::GetGETAcceptMultipart()); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | /** |
---|
| 118 | * Loads the command handler and processes a command sent from the mobile |
---|
| 119 | * |
---|
| 120 | * @access public |
---|
| 121 | * @return boolean |
---|
| 122 | */ |
---|
| 123 | static public function HandleRequest() { |
---|
| 124 | $handler = ZPush::GetRequestHandlerForCommand(Request::GetCommandCode()); |
---|
| 125 | |
---|
| 126 | // TODO handle WBXML exceptions here and print stack |
---|
| 127 | return $handler->Handle(Request::GetCommandCode()); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | /** |
---|
| 131 | * Returns any additional headers which should be sent to the mobile |
---|
| 132 | * |
---|
| 133 | * @access public |
---|
| 134 | * @return array |
---|
| 135 | */ |
---|
| 136 | static public function GetSpecialHeaders() { |
---|
| 137 | if (!isset(self::$specialHeaders) || !is_array(self::$specialHeaders)) |
---|
| 138 | return array(); |
---|
| 139 | |
---|
| 140 | return self::$specialHeaders; |
---|
| 141 | } |
---|
| 142 | |
---|
| 143 | /** |
---|
| 144 | * Handles a command |
---|
| 145 | * |
---|
| 146 | * @param int $commandCode |
---|
| 147 | * |
---|
| 148 | * @access public |
---|
| 149 | * @return boolean |
---|
| 150 | */ |
---|
| 151 | abstract public function Handle($commandCode); |
---|
| 152 | } |
---|
| 153 | ?> |
---|