/**
  * @return waModel
  * @throws waException
  */
 private function getSourceModel()
 {
     if (!$this->source) {
         $this->source_path = $this->option('path');
         if (substr($this->source_path, -1) != '/') {
             $this->source_path .= '/';
         }
         if (!file_exists($this->source_path)) {
             throw new waException(sprintf(_wp('Invalid PATH %s; %s'), $this->source_path, _wp('directory not exists')));
         }
         if (!file_exists($this->source_path . 'kernel/wbs.xml')) {
             throw new waException(sprintf(_wp('Invalid PATH %s; %s'), $this->source_path, _wp('file kernel/wbs.xml not found')));
         }
         /**
          *
          * @var SimpleXMLElement $wbs
          */
         $wbs = simplexml_load_file($this->source_path . 'kernel/wbs.xml');
         $this->dbkey = (string) $wbs->FRONTEND['dbkey'];
         $dkey_path = $this->source_path . 'dblist/' . $this->dbkey . '.xml';
         if (empty($this->dbkey) || !file_exists($dkey_path)) {
             throw new waException(sprintf(_wp('Invalid PATH %s; %s'), $this->source_path, sprintf(_wp('invalid file %s'), 'dblist/' . $this->dbkey . '.xml')));
         }
         /**
          *
          * @var SimpleXMLElement $dblist
          */
         $dblist = simplexml_load_file($dkey_path);
         $host_name = (string) $dblist->DBSETTINGS['SQLSERVER'];
         $host = $wbs->xPath('/WBS/SQLSERVERS/SQLSERVER[@NAME="' . htmlentities($host_name, ENT_QUOTES, 'utf-8') . '"]');
         if (!count($host)) {
             throw new waException(_wp('Invalid SQL server name'));
         }
         $host = $host[0];
         $port = (string) $host['PORT'];
         $this->sql_options = array('host' => (string) $host['HOST'] . ($port ? ':' . $port : ''), 'user' => (string) $dblist->DBSETTINGS['DB_USER'], 'password' => (string) $dblist->DBSETTINGS['DB_PASSWORD'], 'database' => (string) $dblist->DBSETTINGS['DB_NAME'], 'type' => function_exists('mysqli_connect') ? 'mysqli' : 'mysql');
         $this->source = new waModel($this->sql_options);
     } else {
         $this->source->ping();
     }
     return $this->source;
 }
    protected function getContactByEmail($emails = array())
    {
        static $model;
        $sql = <<<SQL
SELECT
    wa_contact.id AS id,
    LOWER(wa_contact_emails.email) AS email
FROM
    wa_contact
JOIN
    wa_contact_emails
ON
    (wa_contact_emails.contact_id = wa_contact.id)
WHERE
    (wa_contact.is_user = 1)
AND
    (LOWER(wa_contact_emails.email) IN (s:emails))
SQL;
        if (!isset($model)) {
            $model = new waModel();
        }
        $model->ping();
        $contacts = array();
        if ($emails && ($contacts = $model->query($sql, array('emails' => $emails))->fetchAll('id', true))) {
            foreach ($contacts as $id => $email) {
                $contacts[$email] = $id;
            }
        }
        return $contacts;
    }