/** * the singleton pattern * * @return Expressomail_Controller */ public static function getInstance() { if (self::$_instance === NULL) { self::$_instance = new Expressomail_Controller(); } return self::$_instance; }
/** * init config settings * - save default values at database * - based from the code of class Addressbook_Setup_Initialize * (non-PHPdoc) @see tine20/Addressbook/Setup/Initialize::setDefaultInternalAddressbook() */ protected function _initializeConfig() { $properties = Expressomail_Config::getProperties(); $property_imapSearchMaxResults = $properties[Expressomail_Config::IMAPSEARCHMAXRESULTS]; $default_value_imapSearchMaxResults = $property_imapSearchMaxResults['default']; $config = array(Expressomail_Config::IMAPSEARCHMAXRESULTS => $default_value_imapSearchMaxResults); $property_autoSaveDraftsInterval = $properties[Expressomail_Config::AUTOSAVEDRAFTSINTERVAL]; $default_value_autoSaveDraftsInterval = $property_autoSaveDraftsInterval['default']; $config[Expressomail_Config::AUTOSAVEDRAFTSINTERVAL] = $default_value_autoSaveDraftsInterval; $property_reportPhishingEmail = $properties[Expressomail_Config::REPORTPHISHINGEMAIL]; $default_value_reportPhishingEmail = $property_reportPhishingEmail['default']; $config[Expressomail_Config::REPORTPHISHINGEMAIL] = $default_value_reportPhishingEmail; Expressomail_Controller::getInstance()->saveConfigSettings($config); }
/** * update to 0.5 * add Expressomail config parameter AUTOSAVEDRAFTSINTERVAL * * @return void */ public function update_4() { $settings = Expressomail_Config::getInstance()->get(Expressomail_Config::EXPRESSOMAIL_SETTINGS); if (!array_key_exists(Expressomail_Config::REPORTPHISHINGEMAIL, $settings)) { try { $properties = Expressomail_Config::getProperties(); $property = $properties[Expressomail_Config::REPORTPHISHINGEMAIL]; $default_value = $property['default']; $settings[Expressomail_Config::REPORTPHISHINGEMAIL] = $default_value; Expressomail_Controller::getInstance()->saveConfigSettings($settings); } catch (Tinebase_Exception_NotFound $tenf) { // do nothing } } $this->setApplicationVersion('Expressomail', '0.5'); }
/** * Search for records matching given filter * * @param Tinebase_Model_Filter_FilterGroup $_filter * @param Tinebase_Model_Pagination $_pagination * @param array|string|boolean $_cols columns to get, * per default / use self::IDCOL or TRUE to get only ids * @return Tinebase_Record_RecordSet|array * * @todo implement optimizations on flags and security sorting * @todo implement messageuid,account_id search */ public function search(Tinebase_Model_Filter_FilterGroup $_filter = NULL, Tinebase_Model_Pagination $_pagination = NULL, $_cols = '*') { $return = null; $resultIds = array(); $messages = array(); $filterObjects = $_filter->getFilterObjects(); $imapFilters = $this->_parseFilterGroup($_filter, $_pagination); $pagination = !$_pagination ? new Tinebase_Model_Pagination(NULL, TRUE) : $_pagination; $searchTotalCount = 0; if ($imapFilters['filters'] == 'Id') { $ids = $filterObjects[0]->getValue(); $ids = $this->_doPagination($ids, $pagination); if ($_cols === TRUE) { return empty($ids) ? array() : $ids; } else { return empty($ids) ? $this->_rawDataToRecordSet(array()) : $this->getMultiple($ids); } } else { $settings = Expressomail_Controller::getInstance()->getConfigSettings(TRUE); $maxresults = $settings[Expressomail_Config::IMAPSEARCHMAXRESULTS]; if (empty($imapFilters['paths'])) { $imapFilters['paths'] = $this->_getAllFolders(); } // get Summarys and merge results foreach (array_keys($imapFilters['paths']) as $folderId) { if (isset($imapFilters['paths'][$folderId]['isSelectable']) && $imapFilters['paths'][$folderId]['isSelectable'] === false) { continue; } $folder = Expressomail_Backend_Folder::decodeFolderUid($folderId); $accountId = $folder['accountId']; $globalname = $folder['globalName']; $imap = Expressomail_Backend_ImapFactory::factory($accountId); $imap->selectFolder($globalname); $sort = $this->_getImapSortParams($_pagination); $idsInFolder = $imap->sort((array) $sort, (array) $imapFilters['filters']); if ($_cols === true) { foreach ($idsInFolder as $idInFolder) { $resultIds[] = self::createMessageId($folder['accountId'], $folderId, $idInFolder); } unset($idsInFolder); unset($idInFolder); } else { $searchTotalCount += count($idsInFolder); if (count($imapFilters['paths']) !== 1 && $searchTotalCount > $maxresults) { throw new Expressomail_Exception_IMAPCacheTooMuchResults(); } if (count($imapFilters['paths']) === 1 && count($_cols) == 2 && $_cols[0] == '_id_' && $_cols[1] == 'messageuid') { $return = array(); $aux = Expressomail_Backend_Folder::decodeFolderUid($folderId); foreach ($idsInFolder as $value) { $messageId = self::createMessageId($aux['accountId'], $folderId, $value); $return[$messageId] = $value; } return $return; } $idsInFolder = count($imapFilters['paths']) === 1 ? $this->_doPagination($idsInFolder, $_pagination) : $idsInFolder; // do pagination early $messagesInFolder = $this->_getSummary($imap, $idsInFolder, $folderId); //$messagesInFolder = $imap->getSummary($idsInFolder, null, null, $folderId); if (count($imapFilters['paths']) === 1) { $tmp = array(); // We cannot trust the order we get from getSummary(), so we'll have to // put it the right order, defined by $idsInFolder // TODO: Put it into Felamilail_Backend_Imap->getSummary()???? foreach ($idsInFolder as $id) { $tmp[$id] = $messagesInFolder[$id]; } $messagesInFolder = $tmp; unset($tmp); } unset($idsInFolder); $messages = array_merge($messages, $messagesInFolder); unset($messagesInFolder); } } if ($_cols === true) { return $resultIds; } if (count($imapFilters['paths']) === 1 && !in_array($pagination->sort, $this->_imapSortParams) || count($imapFilters['paths']) > 1) { $callback = new Expressomail_Backend_MessageComparator($pagination); uasort($messages, array($callback, 'compare')); } } if (empty($messages)) { return $this->_rawDataToRecordSet(array()); } // Apply Pagination and get the resulting summary $messages = count($imapFilters['paths']) === 1 ? $messages : $this->_doPagination($messages, $_pagination); $return = empty($messages) ? $this->_rawDataToRecordSet(array()) : $this->_rawDataToRecordSet($this->_createModelMessageArray($messages), $searchTotalCount); return $return; }
/** * update to 0.6 * add Expressomail domain config file parameter ENABLEMAILDIREXPORT * add Expressomail table 'expressomail_maildirexport_queue' * insert Expressomail applications table new record * * @return void * @throws Tinebase_Exception_NotFound|Tinebase_Exception_Backend_Database|Tinebase_Exception */ public function update_5() { //Add new setup entry at domain setup configuration file $settings = Expressomail_Config::getInstance()->get(Expressomail_Config::EXPRESSOMAIL_SETTINGS); if (!array_key_exists(Expressomail_Config::ENABLEMAILDIREXPORT, $settings)) { try { $properties = Expressomail_Config::getProperties(); $property = $properties[Expressomail_Config::ENABLEMAILDIREXPORT]; $defaultValue = $property['default']; $settings[Expressomail_Config::ENABLEMAILDIREXPORT] = $defaultValue; Expressomail_Controller::getInstance()->saveConfigSettings($settings); Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 successfully added new entry at domain configuration file: "' . $settings[Expressomail_Config::ENABLEMAILDIREXPORT] . '"'); } catch (Tinebase_Exception_NotFound $tenf) { Tinebase_Core::getLogger()->error(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 fails to add new domain entry: "' . $settings[Expressomail_Config::ENABLEMAILDIREXPORT] . '"'); } } else { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 attempted to created a new domain config file entry, but it already exists! "' . Expressomail_Config::ENABLEMAILDIREXPORT . '"'); } //Add table used by queue script for export mail dir data $newTable = 'expressomail_backup_scheduler'; if (!$this->_backend->tableExists($newTable)) { try { $table = Setup_Backend_Schema_Table_Factory::factory('String', ' <table> <name>' . $newTable . '</name> <version>1</version> <declaration> <field> <name>id</name> <type>text</type> <length>40</length> <notnull>true</notnull> </field> <field> <name>account_id</name> <type>text</type> <length>40</length> <notnull>true</notnull> </field> <field> <name>folder</name> <type>text</type> <length>250</length> <notnull>true</notnull> </field> <field> <name>scheduler_time</name> <type>datetime</type> <notnull>true</notnull> </field> <field> <name>start_time</name> <type>datetime</type> </field> <field> <name>end_time</name> <type>datetime</type> </field> <field> <name>status</name> <type>text</type> <length>40</length> <notnull>true</notnull> <default>PENDING</default> </field> <field> <name>is_deleted</name> <type>boolean</type> <default>false</default> </field> <field> <name>deleted_time</name> <type>datetime</type> </field> <field> <name>deleted_by</name> <type>text</type> <length>40</length> </field> <field> <name>priority</name> <type>integer</type> <notnull>true</notnull> <default>5</default> </field> <field> <name>expunged_time</name> <type>datetime</type> </field> <index> <name>id</name> <field> <name>id</name> </field> </index> <index> <name>account_id</name> <field> <name>account_id</name> </field> </index> <index> <name>folder</name> <field> <name>folder</name> </field> </index> <index> <name>status</name> <field> <name>status</name> </field> </index> <index> <name>scheduler_time</name> <field> <name>scheduler_time</name> </field> </index> <index> <name>is_deleted</name> <field> <name>is_deleted</name> </field> </index> <index> <name>priority</name> <field> <name>priority</name> </field> </index> <index> <name>id</name> <primary>true</primary> <field> <name>id</name> </field> </index> <index> <name>account_id--folder--status--is_deleted</name> <unique>true</unique> <field> <name>account_id</name> </field> <field> <name>folder</name> </field> <field> <name>status</name> </field> <field> <name>is_deleted</name> </field> </index> <index> <name>' . $newTable . '::account_id--accounts::id</name> <field> <name>account_id</name> </field> <foreign>true</foreign> <reference> <table>accounts</table> <field>id</field> <ondelete>CASCADE</ondelete> </reference> </index> </declaration> </table> '); $this->_backend->createTable($table); Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade 0.6 successfully created a new table at data base schema: "' . $newTable . '"'); //Insert new Expressomail application table record try { //Fetch application id to perform insert operation $selectId = $this->_db->select()->from(SQL_TABLE_PREFIX . 'applications', 'id')->where($this->_db->quoteIdentifier('name') . ' = ?', 'Expressomail'); $resultId = $this->_db->fetchAll($selectId); $appId = $resultId[0]["id"]; //Check basic data consistence at table application if (count($resultId) != 1) { Tinebase_Core::getLogger()->error(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 inconsistent data at applications table for appName "ExpressoMail"'); throw new Tinebase_Exception_Backend_Database("Application 'Expressomail' inconsistent data at applications table!"); } //Try to find out if some corrupted event have already iserted this data before $selectAppTable = $this->_db->select()->from(SQL_TABLE_PREFIX . 'application_tables', 'name')->where($this->_db->quoteIdentifier('name') . ' = ?', $newTable); $resultAppTable = $this->_db->fetchAll($selectAppTable); if (count($resultAppTable) != 0) { Tinebase_Core::getLogger()->error(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 inconsistent data at applications table for appName "ExpressoMail": the new table "' . $newTable . '" already is there!'); throw new Tinebase_Exception_Backend_Database("Application 'Expressomail' inconsistent data at applications table: new table '{$newTable}' relationship already exists at 'application_tables'!"); } //Follows to add new record $appRecord = new SimpleXMLElement("\n <record>\n <table>\n <name>application_tables</name>\n </table>\n <field>\n <name>application_id</name>\n <value>{$appId}</value>\n </field>\n <field>\n <name>name</name>\n <value>{$newTable}</value>\n </field>\n <field>\n <name>version</name>\n <value>1</value>\n </field>\n </record>\n "); //Performs insert new record $this->_backend->execInsertStatement($appRecord); Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 successfully inserted a new record at "application_tables" table: "' . $newTable . '"'); try { //Fetch update action $this->setApplicationVersion('Expressomail', '0.6'); Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 successfully finished.'); } catch (Tinebase_Exception $updateException) { Tinebase_Core::getLogger()->error(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 fails to be up to dated: ' . $updateException->getMessage()); throw new Tinebase_Exception_Backend_Database("Application 'Expressomail' fails to update to 0.6 at 'setApplicationVersion':" . $updateException->getMessage()); } } catch (Tinebase_Exception_Backend_Database $insertAppException) { Tinebase_Core::getLogger()->error(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 fails to insert new applications table: ' . $insertAppException->getMessage()); throw new Tinebase_Exception_Backend_Database("Application 'Expressomail' fails to update to 0.6 at insert applicatons table new record:" . $insertAppException->getMessage()); } } catch (Tinebase_Exception_Backend_Database $createException) { Tinebase_Core::getLogger()->error(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 fails to create new table: ' . $createException->getMessage()); throw new Tinebase_Exception_Backend_Database("Application 'Expressomail' fails to update to 0.6 at create new table:" . $createException->getMessage()); } } else { Tinebase_Core::getLogger()->error(__METHOD__ . '::' . __LINE__ . ' ExpressoMail upgrade to 0.6 fails to process new table creation: table "' . $newTable . '" already exists at application schema database!'); throw new Tinebase_Exception_Backend_Database("Application 'Expressomail' fails to update to 0.6: base table '{$newTable}' already exists at database schema!"); } }
/** * Returns registry data of expressomail. * * @see Tinebase_Application_Json_Abstract * * @return mixed array 'variable name' => 'data' * * @todo get default account data (host, port, ...) from preferences? */ public function getRegistryData() { try { $accounts = $this->searchAccounts(''); } catch (Exception $e) { Tinebase_Core::getLogger()->warn(__METHOD__ . '::' . __LINE__ . ' Could not get accounts: ' . $e->getMessage()); if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $e->getTraceAsString()); } $accounts = array('results' => array(), 'totalcount' => 0); } $supportedFlags = Expressomail_Controller_Message_Flags::getInstance()->getSupportedFlags(); $extraSenderAccounts = array(); foreach ($accounts['results'] as $key => $account) { try { // build a imap backend so the system folder can be created if necessary $accountModel = Expressomail_Controller_Account::getInstance()->get($account['id']); $accountModel->resolveCredentials(FALSE); // force update the user credentials $imapConfig = $imapConfig = Tinebase_Config::getInstance()->get(Tinebase_Config::IMAP, new Tinebase_Config_Struct()); $config = new stdClass(); $config->{'host'} = $imapConfig->{'host'}; $config->{'port'} = $imapConfig->{'port'}; $config->{'ssl'} = $imapConfig->{'ssl'}; $config->{'user'} = $accountModel->getUsername(); $config->{'password'} = $accountModel->{'password'}; $imap = Expressomail_Backend_ImapFactory::factory($account['id']); if ($imap->createDefaultImapSystemFoldersIfNecessary($config)) { try { // add the namespace 'INBOX/' to the new folders $capabilities = $imap->getCapabilityAndNamespace(); Expressomail_Controller_Account::getInstance()->updateNamespacesAndDelimiter($accountModel, $capabilities); // update account info in backend and session if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . ' Updating capabilities for account: ' . $accountModel->name); } Expressomail_Controller_Account::getInstance()->getBackend()->update($accountModel); // save capabilities in SESSION Expressomail_Session::getSessionNamespace()->account[$accountModel->getId()] = $capabilities; } catch (Exception $zdse) { if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' ' . $zdse->getTraceAsString()); } } } } catch (Exception $e) { if (Tinebase_Core::isLogLevel(Zend_Log::ERR)) { Tinebase_Core::getLogger()->debug(__METHOD__ . '::' . __LINE__ . ' Exception: ' . $e->getMessage()); } } try { $extraSenderAccounts = Expressomail_Controller_Folder::getInstance()->getUsersWithSendAsAcl($account['id']); } catch (Expressomail_Exception_IMAPFolderNotFound $ex) { if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) { Tinebase_Core::getLogger()->info(__METHOD__ . '::' . __LINE__ . $ex->getMessage()); } // Ignore this exception here, it happens when INBOX folder is unaccessible. } catch (Expressomail_Exception_IMAPServiceUnavailable $ex) { // Ignoring this Exception here. } unset($account['host']); unset($account['port']); unset($account['ssl']); unset($account['smtp_hostname']); unset($account['smtp_port']); unset($account['smtp_ssl']); unset($account['smtp_auth']); $accounts['results'][$key] = $account; } $result = array('extraSenderAccounts' => $extraSenderAccounts, 'accounts' => $accounts, 'supportedFlags' => array('results' => $supportedFlags, 'totalcount' => count($supportedFlags)), 'aspellDicts' => Tinebase_Core::getConfig()->aspellDicts); // TODO: get balanceid cookie name from config $balanceIdCookieName = 'BALANCEID'; if (isset($_COOKIE[$balanceIdCookieName])) { $result['balanceId'] = array('cookieName' => $balanceIdCookieName, 'cookieValue' => $_COOKIE[$balanceIdCookieName]); } $result['vacationTemplates'] = $this->getVacationMessageTemplates(); $config = Tinebase_Core::getConfig(); $result['useKeyEscrow'] = $config->certificate->active && $config->certificate->useKeyEscrow; $config = Expressomail_Controller::getInstance()->getConfigSettings(false); // add autoSaveDraftsInterval to client registry $result['autoSaveDraftsInterval'] = $config->autoSaveDraftsInterval; // add reportPhishingEmail to client registry $result['reportPhishingEmail'] = $config->reportPhishingEmail; return $result; }