/**
  * the singleton pattern
  *
  * @return Sipgate_Backend_Api
  */
 public static function getInstance()
 {
     if (self::$_instance === NULL) {
         self::$_instance = new Sipgate_Backend_Api();
     }
     return self::$_instance;
 }
Ejemplo n.º 2
0
 /**
  * the singleton pattern
  *
  * @return Sipgate_Backend_Api
  */
 public static function getInstance($_username, $_password, $_url)
 {
     if (self::$_instance === NULL) {
         self::$_instance = new Sipgate_Backend_Api($_username, $_password, $_url);
     }
     return self::$_instance;
 }
Ejemplo n.º 3
0
 /**
  * factory function to return a selected phone backend class
  *
  * @param   string $_type
  * @return  Sipgate_Backend_Interface
  * @throws  Sipgate_Exception_InvalidArgument
  * @throws  Sipgate_Exception_NotFound
  */
 public static function factory()
 {
     if (isset(Tinebase_Core::getConfig()->sipgate)) {
         $sipgateConfig = Tinebase_Core::getConfig()->sipgate;
         $username = $sipgateConfig->api_username;
         $password = $sipgateConfig->api_password;
         $url = $sipgateConfig->api_url;
     } else {
         throw new Sipgate_Exception_Backend('No settings found for sipgate backend in config file!');
     }
     $instance = Sipgate_Backend_Api::getInstance($username, $password, $url);
     return $instance;
 }
 /**
  * sendds an sms
  * 
  * @param array $values
  * @param string $lineId
  * 
  * @return array
  */
 public function sendMessage($values, $lineId)
 {
     $line = $this->get($lineId);
     $response = Sipgate_Backend_Api::getInstance()->connect($line->account_id)->sendSms($values['own_number'], $values['recipient_number'], $values['message']);
     $ret = array('success' => false, 'result' => $response, 'values' => $values);
     if ($response['StatusCode'] == 200) {
         $ret['success'] = true;
     }
     return $ret;
 }
 /**
  * sync line
  * @param Sipgate_Model_Line $_line
  * @param Tinebase_DateTime $_from
  * @param Tinebase_DateTime $_to
  * @param boolean $verbose
  */
 public function syncLine(Sipgate_Model_Line $_line, Tinebase_DateTime $_from = NULL, Tinebase_DateTime $_to = NULL, $verbose = false)
 {
     $app = Tinebase_Application::getInstance()->getApplicationByName('Sipgate');
     if (!Tinebase_Core::getUser()->hasRight($app->getId(), Sipgate_Acl_Rights::SYNC_LINES)) {
         throw new Tinebase_Exception_AccessDenied('You are not allowed to sync lines!');
     }
     if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
         Tinebase_Core::getLogger()->debug('Synchronizing Line ' . $_line->sip_uri . '...');
     }
     $transactionId = Tinebase_TransactionManager::getInstance()->startTransaction(Tinebase_Core::getDb());
     $count = 0;
     if ($_from == null) {
         if ($_line->last_sync == null) {
             $_from = new Tinebase_DateTime();
             $_from->subMonth(2);
         } else {
             $_from = $_line->last_sync;
         }
     }
     $_to = $_to ? $_to : new Tinebase_DateTime();
     // timezone corrections
     $_from->setTimezone(Tinebase_Core::getUserTimezone());
     $_to->setTimezone(Tinebase_Core::getUserTimezone());
     if ($verbose) {
         echo 'Syncing line ' . $_line->sip_uri . ' from ' . $_from->getIso() . ' to ' . $_to->getIso() . PHP_EOL;
     }
     try {
         if (!$this->_apiBackend) {
             $this->_apiBackend = Sipgate_Backend_Api::getInstance()->connect($_line->account_id, $sipgate_user, $sipgate_pwd);
         }
         $response = $this->_apiBackend->getCallHistory($_line->__get('sip_uri'), $_from, $_to, 0, 1000);
         if (is_array($response['history']) && $response['totalcount'] > 0) {
             $paging = new Tinebase_Model_Pagination();
             // find already synced entries
             foreach ($response['history'] as $call) {
                 $entryIds[] = $call['EntryID'];
             }
             $filter = new Sipgate_Model_ConnectionFilter(array(), 'AND');
             $filter->addFilter(new Tinebase_Model_Filter_Id('entry_id', 'in', $entryIds));
             $result = Sipgate_Controller_Connection::getInstance()->search($filter, $paging, false, false);
             $oldEntries = array();
             foreach ($result->getIterator() as $connection) {
                 $oldEntries[] = $connection->entry_id;
             }
             unset($result, $connection, $filter);
             $count = 0;
             foreach ($response['history'] as $call) {
                 // skip sync if already in db
                 if (in_array($call['EntryID'], $oldEntries)) {
                     if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                         Tinebase_Core::getLogger()->debug('Skipping call Id' . $call['EntryID'] . '.');
                     }
                     continue;
                 }
                 $localNumber = $this->_getLocalNumber($call['LocalUri']);
                 $remoteNumber = $this->_getRemoteNumber($call['RemoteUri'], $localNumber, true);
                 $filter = new Addressbook_Model_ContactFilter(array(array("field" => "telephone", "operator" => "contains", "value" => $remoteNumber)));
                 $s = Addressbook_Controller_Contact::getInstance()->search($filter, $paging);
                 $now = new Tinebase_DateTime();
                 $connection = new Sipgate_Model_Connection(array('tos' => $call['TOS'], 'entry_id' => $call['EntryID'], 'local_uri' => $call['LocalUri'], 'remote_uri' => $call['RemoteUri'], 'status' => $call['Status'], 'local_number' => '+' . $localNumber, 'remote_number' => $remoteNumber == 'anonymous' ? 'anonymous' : '+' . $remoteNumber, 'line_id' => $_line->getId(), 'timestamp' => strtotime($call['Timestamp']), 'creation_time' => $now, 'contact_id' => $s->getFirstRecord() ? $s->getFirstRecord()->getId() : null, 'contact_name' => $s->getFirstRecord() ? $s->getFirstRecord()->n_fn : ''));
                 $count++;
                 $this->create($connection);
                 if (Tinebase_Core::isLogLevel(Zend_Log::DEBUG)) {
                     Tinebase_Core::getLogger()->debug('Creating call Id' . $call['EntryID'] . '.');
                 }
             }
         }
     } catch (Exception $e) {
         Tinebase_TransactionManager::getInstance()->rollBack();
         throw $e;
     }
     $_to->setTimezone('UTC');
     $_line->last_sync = $_to;
     Sipgate_Controller_Line::getInstance()->update($_line);
     Tinebase_TransactionManager::getInstance()->commitTransaction($transactionId);
     if (Tinebase_Core::isLogLevel(Zend_Log::INFO)) {
         Tinebase_Core::getLogger()->debug('Synchronizing Line ' . $_line->sip_uri . ' completed. Got ' . $count . ' new connections.');
     }
     return $count;
 }
 /**
  * @param   Array $account
  */
 public function validateAccount($account)
 {
     $b = Sipgate_Backend_Api::getInstance();
     if (empty($account['data']['username'])) {
         $b->connect($account['data']['id']);
     } else {
         $b->connect(NULL, $account['data']['username'], $account['data']['password'], $account['data']['accounttype']);
     }
     return true;
 }