Back to index

d-push  2.0
combined.php
Go to the documentation of this file.
00001 <?php
00002 /***********************************************
00003 * File      :   backend/combined/combined.php
00004 * Project   :   Z-Push
00005 * Descr     :   Combines several backends. Each type of message
00006 *               (Emails, Contacts, Calendar, Tasks) can be handled by
00007 *               a separate backend.
00008 *               As the CombinedBackend is a subclass of the default Backend
00009 *               class, it returns by that the supported AS version is 2.5.
00010 *               The method GetSupportedASVersion() could be implemented
00011 *               here, checking the version with all backends.
00012 *               But still, the lowest version in common must be
00013 *               returned, even if some backends support a higher version.
00014 *
00015 * Created   :   29.11.2010
00016 *
00017 * Copyright 2007 - 2011 Zarafa Deutschland GmbH
00018 *
00019 * This program is free software: you can redistribute it and/or modify
00020 * it under the terms of the GNU Affero General Public License, version 3,
00021 * as published by the Free Software Foundation with the following additional
00022 * term according to sec. 7:
00023 *
00024 * According to sec. 7 of the GNU Affero General Public License, version 3,
00025 * the terms of the AGPL are supplemented with the following terms:
00026 *
00027 * "Zarafa" is a registered trademark of Zarafa B.V.
00028 * "Z-Push" is a registered trademark of Zarafa Deutschland GmbH
00029 * The licensing of the Program under the AGPL does not imply a trademark license.
00030 * Therefore any rights, title and interest in our trademarks remain entirely with us.
00031 *
00032 * However, if you propagate an unmodified version of the Program you are
00033 * allowed to use the term "Z-Push" to indicate that you distribute the Program.
00034 * Furthermore you may use our trademarks where it is necessary to indicate
00035 * the intended purpose of a product or service provided you use it in accordance
00036 * with honest practices in industrial or commercial matters.
00037 * If you want to propagate modified versions of the Program under the name "Z-Push",
00038 * you may only do so if you have a written permission by Zarafa Deutschland GmbH
00039 * (to acquire a permission please contact Zarafa at trademark@zarafa.com).
00040 *
00041 * This program is distributed in the hope that it will be useful,
00042 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00043 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00044 * GNU Affero General Public License for more details.
00045 *
00046 * You should have received a copy of the GNU Affero General Public License
00047 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00048 *
00049 * Consult LICENSE file for details
00050 ************************************************/
00051 
00052 // default backend
00053 include_once('lib/default/backend.php');
00054 
00055 //include the CombinedBackend's own config file
00056 require_once("backend/combined/config.php");
00057 require_once("backend/combined/importer.php");
00058 require_once("backend/combined/exporter.php");
00059 
00060 class BackendCombined extends Backend {
00061     public $config;
00062     public $backends;
00063     private $activeBackend;
00064 
00070     public function BackendCombined() {
00071         parent::Backend();
00072         $this->config = BackendCombinedConfig::GetBackendCombinedConfig();
00073 
00074         foreach ($this->config['backends'] as $i => $b){
00075             // load and instatiate backend
00076             ZPush::IncludeBackend($b['name']);
00077             $this->backends[$i] = new $b['name']($b['config']);
00078         }
00079         ZLog::Write(LOGLEVEL_INFO, sprintf("Combined %d backends loaded.", count($this->backends)));
00080     }
00081 
00092     public function Logon($username, $domain, $password) {
00093         ZLog::Write(LOGLEVEL_DEBUG, sprintf("Combined->Logon('%s', '%s',***))", $username, $domain));
00094         if(!is_array($this->backends)){
00095             return false;
00096         }
00097         foreach ($this->backends as $i => $b){
00098             $u = $username;
00099             $d = $domain;
00100             $p = $password;
00101             if(isset($this->config['backends'][$i]['users'])){
00102                 if(!isset($this->config['backends'][$i]['users'][$username])){
00103                     unset($this->backends[$i]);
00104                     continue;
00105                 }
00106                 if(isset($this->config['backends'][$i]['users'][$username]['username']))
00107                     $u = $this->config['backends'][$i]['users'][$username]['username'];
00108                 if(isset($this->config['backends'][$i]['users'][$username]['password']))
00109                     $p = $this->config['backends'][$i]['users'][$username]['password'];
00110                 if(isset($this->config['backends'][$i]['users'][$username]['domain']))
00111                     $d = $this->config['backends'][$i]['users'][$username]['domain'];
00112             }
00113             if($this->backends[$i]->Logon($u, $d, $p) == false){
00114                 ZLog::Write(LOGLEVEL_DEBUG, sprintf("Combined->Logon() failed on %s ", $this->config['backends'][$i]['name']));
00115                 return false;
00116             }
00117         }
00118         ZLog::Write(LOGLEVEL_INFO, "Combined->Logon() success");
00119         return true;
00120     }
00121 
00141     public function Setup($store, $checkACLonly = false, $folderid = false) {
00142         ZLog::Write(LOGLEVEL_DEBUG, sprintf("Combined->Setup('%s', '%s', '%s')", $store, Utils::PrintAsString($checkACLonly), $folderid));
00143         if(!is_array($this->backends)){
00144             return false;
00145         }
00146         foreach ($this->backends as $i => $b){
00147             $u = $store;
00148             if(isset($this->config['backends'][$i]['users']) && isset($this->config['backends'][$i]['users'][$store]['username'])){
00149                 $u = $this->config['backends'][$i]['users'][$store]['username'];
00150             }
00151             if($this->backends[$i]->Setup($u, $checkACLonly, $folderid) == false){
00152                 ZLog::Write(LOGLEVEL_WARN, "Combined->Setup() failed");
00153                 return false;
00154             }
00155         }
00156         ZLog::Write(LOGLEVEL_INFO, "Combined->Setup() success");
00157         return true;
00158     }
00159 
00166     public function Logoff() {
00167         ZLog::Write(LOGLEVEL_DEBUG, "Combined->Logoff()");
00168         foreach ($this->backends as $i => $b){
00169             $this->backends[$i]->Logoff();
00170         }
00171         ZLog::Write(LOGLEVEL_DEBUG, "Combined->Logoff() success");
00172         return true;
00173     }
00174 
00184     public function GetHierarchy(){
00185         ZLog::Write(LOGLEVEL_DEBUG, "Combined->GetHierarchy()");
00186         $ha = array();
00187         foreach ($this->backends as $i => $b){
00188             if(!empty($this->config['backends'][$i]['subfolder'])){
00189                 $f = new SyncFolder();
00190                 $f->serverid = $i.$this->config['delimiter'].'0';
00191                 $f->parentid = '0';
00192                 $f->displayname = $this->config['backends'][$i]['subfolder'];
00193                 $f->type = SYNC_FOLDER_TYPE_OTHER;
00194                 $ha[] = $f;
00195             }
00196             $h = $this->backends[$i]->GetHierarchy();
00197             if(is_array($h)){
00198                 foreach($h as $j => $f){
00199                     $h[$j]->serverid = $i.$this->config['delimiter'].$h[$j]->serverid;
00200                     if($h[$j]->parentid != '0' || !empty($this->config['backends'][$i]['subfolder'])){
00201                         $h[$j]->parentid = $i.$this->config['delimiter'].$h[$j]->parentid;
00202                     }
00203                     if(isset($this->config['folderbackend'][$h[$j]->type]) && $this->config['folderbackend'][$h[$j]->type] != $i){
00204                         $h[$j]->type = SYNC_FOLDER_TYPE_OTHER;
00205                     }
00206                 }
00207                 $ha = array_merge($ha, $h);
00208             }
00209         }
00210         ZLog::Write(LOGLEVEL_DEBUG, "Combined->GetHierarchy() success");
00211         return $ha;
00212     }
00213 
00222     public function GetImporter($folderid = false) {
00223         if($folderid !== false) {
00224             ZLog::Write(LOGLEVEL_DEBUG, sprintf("Combined->GetImporter() Content: ImportChangesCombined:('%s')", $folderid));
00225 
00226             // get the contents importer from the folder in a backend
00227             // the importer is wrapped to check foldernames in the ImportMessageMove function
00228             $backend = $this->GetBackend($folderid);
00229             if($backend === false)
00230                 return false;
00231             $importer = $backend->GetImporter($this->GetBackendFolder($folderid));
00232             if($importer){
00233                 return new ImportChangesCombined($this, $folderid, $importer);
00234             }
00235             return false;
00236         }
00237         else {
00238             ZLog::Write(LOGLEVEL_DEBUG, "Combined->GetImporter() -> Hierarchy: ImportChangesCombined()");
00239             //return our own hierarchy importer which send each change to the right backend
00240             return new ImportChangesCombined($this);
00241         }
00242     }
00243 
00253     public function GetExporter($folderid = false){
00254         ZLog::Write(LOGLEVEL_DEBUG, sprintf("Combined->GetExporter('%s')", $folderid));
00255         if($folderid){
00256             $backend = $this->GetBackend($folderid);
00257             if($backend == false)
00258                 return false;
00259             return $backend->GetExporter($this->GetBackendFolder($folderid));
00260         }
00261         return new ExportChangesCombined($this);
00262     }
00263 
00274     public function SendMail($sm) {
00275         ZLog::Write(LOGLEVEL_DEBUG, "Combined->SendMail()");
00276         foreach ($this->backends as $i => $b){
00277             if($this->backends[$i]->SendMail($sm) == true){
00278                 return true;
00279             }
00280         }
00281         return false;
00282     }
00283 
00295     public function Fetch($folderid, $id, $contentparameters) {
00296         ZLog::Write(LOGLEVEL_DEBUG, sprintf("Combined->Fetch('%s', '%s', CPO)", $folderid, $id));
00297         $backend = $this->GetBackend($folderid);
00298         if($backend == false)
00299             return false;
00300         return $backend->Fetch($this->GetBackendFolder($folderid), $id, $contentparameters);
00301     }
00302 
00311     function GetWasteBasket(){
00312         ZLog::Write(LOGLEVEL_DEBUG, "Combined->GetWasteBasket()");
00313         if (isset($this->activeBackend))
00314             return $this->activeBackend->GetWasteBasket();
00315 
00316         return false;
00317     }
00318 
00329     public function GetAttachmentData($attname) {
00330         ZLog::Write(LOGLEVEL_DEBUG, sprintf("Combined->GetAttachmentData('%s')", $attname));
00331         foreach ($this->backends as $i => $b) {
00332             try {
00333                 $attachment = $this->backends[$i]->GetAttachmentData($attname);
00334                 if ($attachment instanceof SyncItemOperationsAttachment)
00335                     return $attachment;
00336             }
00337             catch (StatusException $s) {
00338                 // backends might throw StatusExceptions if it's not their attachment
00339             }
00340         }
00341         throw new StatusException("Combined->GetAttachmentData(): no backend found", SYNC_ITEMOPERATIONSSTATUS_INVALIDATT);
00342     }
00343 
00355     public function MeetingResponse($requestid, $folderid, $error) {
00356         $backend = $this->GetBackend($folderid);
00357         if($backend === false)
00358             return false;
00359         return $backend->MeetingResponse($requestid, $this->GetBackendFolder($folderid), $error);
00360     }
00361 
00370     public function GetBackend($folderid){
00371         $pos = strpos($folderid, $this->config['delimiter']);
00372         if($pos === false)
00373             return false;
00374         $id = substr($folderid, 0, $pos);
00375         if(!isset($this->backends[$id]))
00376             return false;
00377 
00378         $this->activeBackend = $this->backends[$id];
00379         return $this->backends[$id];
00380     }
00381 
00390     public function GetBackendFolder($folderid){
00391         $pos = strpos($folderid, $this->config['delimiter']);
00392         if($pos === false)
00393             return false;
00394         return substr($folderid,$pos + strlen($this->config['delimiter']));
00395     }
00396 
00405     public function GetBackendId($folderid){
00406         $pos = strpos($folderid, $this->config['delimiter']);
00407         if($pos === false)
00408             return false;
00409         return substr($folderid,0,$pos);
00410     }
00411 }
00412 ?>