[1349] | 1 | <?php |
---|
| 2 | |
---|
| 3 | /**************************************************************************\ |
---|
| 4 | * eGroupWare * |
---|
| 5 | * http://www.egroupware.org * |
---|
| 6 | * -------------------------------------------- * |
---|
| 7 | * This program is free software; you can redistribute it and/or modify it * |
---|
| 8 | * under the terms of the GNU General Public License as published by the * |
---|
| 9 | * Free Software Foundation; either version 2 of the License, or (at your * |
---|
| 10 | * option) any later version. * |
---|
| 11 | \**************************************************************************/ |
---|
| 12 | |
---|
| 13 | /** |
---|
| 14 | * Camada Model das Aplicações Externas |
---|
| 15 | * @package Workflow |
---|
| 16 | * @author Sidnei Augusto Drovetto Jr. - drovetto@gmail.com |
---|
| 17 | * @license http://www.gnu.org/copyleft/gpl.html GPL |
---|
| 18 | */ |
---|
| 19 | class so_external_applications |
---|
| 20 | { |
---|
| 21 | /** |
---|
| 22 | * @var bool True se o usuário for administrador do expresso |
---|
| 23 | * @access private |
---|
| 24 | */ |
---|
| 25 | private $isAdmin; |
---|
| 26 | |
---|
| 27 | /** |
---|
| 28 | * @var object Link para a ACL do Workflow |
---|
| 29 | * @access private |
---|
| 30 | */ |
---|
| 31 | private $acl; |
---|
| 32 | |
---|
| 33 | /** |
---|
| 34 | * @var object Link para o Banco de Dados do Expresso |
---|
| 35 | * @access private |
---|
| 36 | */ |
---|
| 37 | private $db; |
---|
| 38 | |
---|
| 39 | /** |
---|
| 40 | * @var object Link para o Banco de Dados do Expresso |
---|
| 41 | * @access private |
---|
| 42 | */ |
---|
| 43 | private $EXTERNAL_APPLICATION_PATH; |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * Checa se o usuário possui acesso à administração das Aplicações Externas |
---|
| 47 | * @return void |
---|
| 48 | * @access private |
---|
| 49 | */ |
---|
| 50 | private function _checkAccess() |
---|
| 51 | { |
---|
| 52 | /* the user is an administrator */ |
---|
| 53 | if ($this->isAdmin) |
---|
| 54 | return true; |
---|
| 55 | else |
---|
| 56 | die(serialize("Você não tem permissão para executar este procedimento!")); |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | /** |
---|
| 60 | * Verifica se houve erro em alguma query do Banco de Dados |
---|
| 61 | * @param object $result O resultado de alguma query |
---|
| 62 | * @return void |
---|
| 63 | * @access private |
---|
| 64 | */ |
---|
| 65 | private function _checkError($result) |
---|
| 66 | { |
---|
| 67 | if ($result === false) |
---|
| 68 | die(serialize("Ocorreu um erro ao se tentar executar a operação solicitada.")); |
---|
| 69 | } |
---|
| 70 | |
---|
| 71 | /** |
---|
| 72 | * Grava a imagem no diretório de aplicações externas |
---|
| 73 | * @param string $filename O nome da imagem |
---|
| 74 | * @param string $contents O conteúdo (binário) da imagem |
---|
| 75 | * @return bool Informa se a imagem foi gravada com sucesso ou não |
---|
| 76 | * @access private |
---|
| 77 | */ |
---|
| 78 | private function _saveImage($filename, $contents) |
---|
| 79 | { |
---|
| 80 | /* avoid writes outside the external_applications directory */ |
---|
| 81 | if (strpos($filename, '/') !== false) |
---|
| 82 | return false; |
---|
| 83 | |
---|
| 84 | /* create the directories if they don't exist */ |
---|
| 85 | @mkdir($this->EXTERNAL_APPLICATION_PATH, 0770, true); |
---|
| 86 | |
---|
| 87 | /* perform the write operations */ |
---|
| 88 | $handler = fopen($this->EXTERNAL_APPLICATION_PATH . '/' . $filename, 'w'); |
---|
| 89 | if ($handler) |
---|
| 90 | { |
---|
| 91 | fwrite($handler, $contents); |
---|
| 92 | fclose($handler); |
---|
| 93 | return true; |
---|
| 94 | } |
---|
| 95 | else |
---|
| 96 | return false; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /** |
---|
| 100 | * Verifica a presença de valores inválidos em alguns campos da aplicação externa |
---|
| 101 | * @param string $name O nome da aplicação externa |
---|
| 102 | * @param string $address O endereço da aplicação externa |
---|
| 103 | * @return array Uma array contendo os erros encontrados |
---|
| 104 | * @access private |
---|
| 105 | */ |
---|
| 106 | private function checkExternalApplicationData($name, $address) |
---|
| 107 | { |
---|
| 108 | $output = array(); |
---|
| 109 | $name = trim($name); |
---|
| 110 | $address = trim($address); |
---|
| 111 | |
---|
| 112 | if (empty($name)) |
---|
| 113 | $output[] = 'O nome da aplicação externa não pode ser vazio.'; |
---|
| 114 | |
---|
| 115 | if (empty($address)) |
---|
| 116 | $output[] = 'O endereço da aplicação externa não pode ser vazio.'; |
---|
| 117 | |
---|
| 118 | if (preg_match('/^[a-z]+:\/\//i', $address) == 0) |
---|
| 119 | $output[] = 'Aparentemente a URL informada não está formatada corretamente.'; |
---|
| 120 | |
---|
| 121 | return $output; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /** |
---|
| 125 | * Construtor da classe so_external_applications |
---|
| 126 | * @return object |
---|
| 127 | */ |
---|
| 128 | function so_external_applications() |
---|
| 129 | { |
---|
| 130 | $this->isAdmin = $_SESSION['phpgw_info']['workflow']['user_is_admin']; |
---|
| 131 | $this->acl = &$GLOBALS['ajax']->acl; |
---|
| 132 | $this->db = &$GLOBALS['workflow']['workflowObjects']->getDBGalaxia()->Link_ID; |
---|
| 133 | $this->EXTERNAL_APPLICATION_PATH = $_SESSION['phpgw_info']['workflow']['server']['files_dir'] . '/workflow//workflow/external_applications'; |
---|
| 134 | } |
---|
| 135 | |
---|
| 136 | /** |
---|
| 137 | * Lista todas as aplicações externas |
---|
| 138 | * @return array Lista de aplicações externas |
---|
| 139 | * @access public |
---|
| 140 | */ |
---|
| 141 | function getExternalApplications() |
---|
| 142 | { |
---|
| 143 | $this->_checkAccess(); |
---|
| 144 | $query = "SELECT external_application_id, name FROM egw_wf_external_application ORDER BY name"; |
---|
| 145 | |
---|
| 146 | $result = $this->db->query($query); |
---|
| 147 | $this->_checkError($result); |
---|
| 148 | |
---|
| 149 | $output = $result->GetArray(-1); |
---|
| 150 | for ($i = 0; $i < count($output); $i++) |
---|
| 151 | for ($j = 0; $j < $result->_numOfFields; $j++) |
---|
| 152 | unset($output[$i][$j]); |
---|
| 153 | |
---|
| 154 | return $output; |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | /** |
---|
| 158 | * Retornar informações sobre uma aplicação externa |
---|
| 159 | * @param int $externalApplicationID O ID da aplicação externa |
---|
| 160 | * @return array Array contento informações sobre a aplicação externa |
---|
| 161 | * @access public |
---|
| 162 | */ |
---|
| 163 | function getExternalApplication($externalApplicationID) |
---|
| 164 | { |
---|
| 165 | $this->_checkAccess(); |
---|
| 166 | $query = "SELECT external_application_id, name, description, image, address, authentication, post, intranet_only FROM egw_wf_external_application WHERE (external_application_id = ?)"; |
---|
| 167 | |
---|
| 168 | $result = $this->db->query($query, array($externalApplicationID)); |
---|
| 169 | $this->_checkError($result); |
---|
| 170 | |
---|
| 171 | $output = $result->GetArray(-1); |
---|
| 172 | for ($i = 0; $i < count($output); $i++) |
---|
| 173 | for ($j = 0; $j < $result->_numOfFields; $j++) |
---|
| 174 | unset($output[$i][$j]); |
---|
| 175 | |
---|
| 176 | return isset($output[0]) ? $output[0] : false; |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | /** |
---|
| 180 | * Adiciona uma aplicação externa |
---|
| 181 | * @param string $name O nome da aplicação externa |
---|
| 182 | * @param string $description A descrição da aplicação externa |
---|
| 183 | * @param string $address O endereço da aplicação externa |
---|
| 184 | * @param string $image O nome da imagem da aplicação externa |
---|
| 185 | * @param int $authentication Indica se a aplicação externa autentica (1) ou não (0) |
---|
| 186 | * @param string $post Os dados que são postados para a aplicação externa (caso necessite de autenticação) |
---|
| 187 | * @param int $intranetOnly Indica se a aplicação externa só será visível na Intranet (1 somente Intranet e 2 para cliente de qualquer origem) |
---|
| 188 | * @return bool TRUE se a ação foi concluída com êxito e FALSE caso contrário |
---|
| 189 | * @access public |
---|
| 190 | */ |
---|
| 191 | function addExternalApplication($name, $description, $address, $image, $authentication, $post, $intranetOnly) |
---|
| 192 | { |
---|
| 193 | $this->_checkAccess(); |
---|
| 194 | |
---|
| 195 | if (count($errors = $this->checkExternalApplicationData($name, $address)) > 0) |
---|
| 196 | return array('error' => implode("\n", $errors)); |
---|
| 197 | |
---|
| 198 | /* decode the supplied image */ |
---|
| 199 | if ($image != '') |
---|
| 200 | { |
---|
| 201 | $image = str_replace(' ', '+', $image); |
---|
| 202 | $imageData = base64_decode($image); |
---|
| 203 | if ($imageData !== false) |
---|
| 204 | $imageData = unserialize($imageData); |
---|
| 205 | if ($imageData !== false) |
---|
| 206 | { |
---|
| 207 | $image = strtolower(end(explode('.', $imageData['name']))); |
---|
| 208 | if (($image != 'png') && ($image != 'jpg') && ($image != 'gif')) |
---|
| 209 | return array('error' => 'A imagem enviada não é do tipo JPG, PNG ou GIF'); |
---|
| 210 | } |
---|
| 211 | else |
---|
| 212 | $image = ''; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | if (strlen($image) > 0) |
---|
| 216 | $query = "INSERT INTO egw_wf_external_application(name, description, address, image, authentication, post, intranet_only) VALUES(?, ?, ?, currVAL('seq_egw_wf_external_application') || '.$image', ?, ?, ?)"; |
---|
| 217 | else |
---|
| 218 | $query = "INSERT INTO egw_wf_external_application(name, description, address, authentication, post, intranet_only) VALUES(?, ?, ?, ?, ?, ?)"; |
---|
| 219 | |
---|
| 220 | $this->db->StartTrans(); |
---|
| 221 | $result = $this->db->query($query, array($name, $description, $address, $authentication, $post, $intranetOnly)); |
---|
| 222 | |
---|
| 223 | if ((strlen($image) > 0) && ($result !== false)) |
---|
| 224 | { |
---|
| 225 | $currentID = $this->db->getOne("SELECT currVAL('seq_egw_wf_external_application')"); |
---|
| 226 | $this->_saveImage($currentID . '.' . $image, $imageData['contents']); |
---|
| 227 | } |
---|
| 228 | |
---|
| 229 | if ($result === false) |
---|
| 230 | $this->db->FailTrans(); |
---|
| 231 | else |
---|
| 232 | $this->db->CompleteTrans(); |
---|
| 233 | |
---|
| 234 | $this->_checkError($result); |
---|
| 235 | return (($result === false) ? false : true); |
---|
| 236 | } |
---|
| 237 | |
---|
| 238 | /** |
---|
| 239 | * Atualiza uma aplicação externa |
---|
| 240 | * @param int $externalApplicationID O ID da aplicação externa |
---|
| 241 | * @param string $name O nome da aplicação externa |
---|
| 242 | * @param string $description A descrição da aplicação externa |
---|
| 243 | * @param string $address O endereço da aplicação externa |
---|
| 244 | * @param string $image O nome da imagem da aplicação externa |
---|
| 245 | * @param int $authentication Indica se a aplicação externa autentica (1) ou não (0) |
---|
| 246 | * @param string $post Os dados que são postados para a aplicação externa (caso necessite de autenticação) |
---|
| 247 | * @param int $removeCurrentImage Indica se a imagem atual da aplicação externa será removida (1 para remover e 0 para não remover) |
---|
| 248 | * @param int $intranetOnly Indica se a aplicação externa só será visível na Intranet (1 somente Intranet e 2 para cliente de qualquer origem) |
---|
| 249 | * @return bool TRUE se a ação foi concluída com êxito e FALSE caso contrário |
---|
| 250 | * @access public |
---|
| 251 | */ |
---|
| 252 | function updateExternalApplication($externalApplicationID, $name, $description, $address, $image, $authentication, $post, $removeCurrentImage, $intranetOnly) |
---|
| 253 | { |
---|
| 254 | $this->_checkAccess(); |
---|
| 255 | |
---|
| 256 | if (count($errors = $this->checkExternalApplicationData($name, $address)) > 0) |
---|
| 257 | return array('error' => implode("\n", $errors)); |
---|
| 258 | |
---|
| 259 | /* if a new image is supplied, decode the data */ |
---|
| 260 | if ($image != '') |
---|
| 261 | { |
---|
| 262 | $image = str_replace(' ', '+', $image); |
---|
| 263 | $imageData = base64_decode($image); |
---|
| 264 | if ($imageData !== false) |
---|
| 265 | $imageData = unserialize($imageData); |
---|
| 266 | if ($imageData !== false) |
---|
| 267 | { |
---|
| 268 | $image = strtolower(end(explode('.', $imageData['name']))); |
---|
| 269 | if (($image != 'png') && ($image != 'jpg') && ($image != 'gif')) |
---|
| 270 | return array('error' => 'A imagem enviada não é do tipo JPG, PNG ou GIF'); |
---|
| 271 | $image = $externalApplicationID . '.' . $image; |
---|
| 272 | } |
---|
| 273 | else |
---|
| 274 | $image = null; |
---|
| 275 | } |
---|
| 276 | else |
---|
| 277 | $image = null; |
---|
| 278 | |
---|
| 279 | /* get the current image */ |
---|
| 280 | $currentImage = $this->db->getOne('SELECT image FROM egw_wf_external_application WHERE (external_application_id = ?)', array($externalApplicationID)); |
---|
| 281 | |
---|
| 282 | /* if necessary, remove the current image */ |
---|
| 283 | if ((($removeCurrentImage == '1') || (!is_null($image))) && ($currentImage)) |
---|
| 284 | if (file_exists($this->EXTERNAL_APPLICATION_PATH . '/' . $currentImage)) |
---|
| 285 | unlink($this->EXTERNAL_APPLICATION_PATH . '/' . $currentImage); |
---|
| 286 | |
---|
| 287 | /* if supplied, save the new image */ |
---|
| 288 | if (!is_null($image)) |
---|
| 289 | $this->_saveImage($image, $imageData['contents']); |
---|
| 290 | else |
---|
| 291 | if ($removeCurrentImage == '0') |
---|
| 292 | $image = $currentImage; |
---|
| 293 | |
---|
| 294 | /* update the external application */ |
---|
| 295 | $query = "UPDATE egw_wf_external_application SET name = ?, description = ?, address = ?, image = ?, authentication = ?, post = ?, intranet_only = ? WHERE (external_application_id = ?)"; |
---|
| 296 | $result = $this->db->query($query, array($name, $description, $address, $image, $authentication, $post, $intranetOnly, $externalApplicationID)); |
---|
| 297 | $this->_checkError($result); |
---|
| 298 | |
---|
| 299 | return (($result === false) ? false : true); |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | /** |
---|
| 303 | * Remove uma aplicação externa |
---|
| 304 | * @param int $externalApplicationID O ID da aplicação externa. |
---|
| 305 | * @return bool TRUE se a ação foi concluída com êxito e FALSE caso contrário. |
---|
| 306 | * @access public |
---|
| 307 | */ |
---|
| 308 | function removeExternalApplication($externalApplicationID) |
---|
| 309 | { |
---|
| 310 | $this->_checkAccess(); |
---|
| 311 | |
---|
| 312 | /* remove the current image */ |
---|
| 313 | $currentImage = $this->db->getOne('SELECT image FROM egw_wf_external_application WHERE (external_application_id = ?)', array($externalApplicationID)); |
---|
| 314 | if ($currentImage) |
---|
| 315 | if (file_exists($this->EXTERNAL_APPLICATION_PATH . '/' . $currentImage)) |
---|
| 316 | unlink($this->EXTERNAL_APPLICATION_PATH . '/' . $currentImage); |
---|
| 317 | |
---|
| 318 | /* remove the external application */ |
---|
| 319 | $result = $this->db->query('DELETE FROM egw_wf_external_application WHERE (external_application_id = ?)', array($externalApplicationID)); |
---|
| 320 | $this->_checkError($result); |
---|
| 321 | |
---|
| 322 | $this->acl->removeAdminsFromResource('APX', $externalApplicationID); |
---|
| 323 | |
---|
| 324 | return (($result === false) ? false : true); |
---|
| 325 | } |
---|
| 326 | } |
---|
| 327 | ?> |
---|