public function validate($data, $contact_id = null)
 {
     $errors = parent::validate($data, $contact_id);
     $email_model = new waContactEmailsModel();
     $contact_model = new waContactModel();
     if ($this->isMulti()) {
         if (!empty($data[0]) && $contact_id) {
             $c = $contact_model->getById($contact_id);
             if (!$c['password']) {
                 return $errors;
             }
             $value = $this->format($data[0], 'value');
             $id = $email_model->getContactWithPassword($value);
             if ($id && $id != $contact_id) {
                 $errors[0] = sprintf(_ws('User with the same %s is already registered'), 'email');
             }
         }
     } else {
         $value = $this->format($data, 'value');
         if ($value) {
             if ($contact_id) {
                 $c = $contact_model->getById($contact_id);
                 if (!$c['password']) {
                     return $errors;
                 }
             }
             $id = $email_model->getContactWithPassword($value);
             if ($id && $id != $contact_id) {
                 $errors = sprintf(_ws('User with the same %s is already registered'), 'email');
             }
         }
     }
     return $errors;
 }
 public function load(waContact $contact, $fields = null)
 {
     $this->getModel();
     $data = $this->model->getById($contact->getId());
     if (!$data) {
         throw new waException('Contact does not exist: ' . $contact->getId(), 404);
     }
     return $data;
 }
 public function execute()
 {
     if (!$this->getRequest()->request('json', 0)) {
         $action = new contactsContactsInfoAction();
         echo $action->display();
         return;
     }
     $m = new waContactModel();
     $contact_id = $this->getRequest()->request('id', 0, 'int');
     $contact = new waContact($contact_id);
     $values = $contact->load('js', true);
     if (isset($values['company_contact_id'])) {
         if (!$m->getById($values['company_contact_id'])) {
             $values['company_contact_id'] = 0;
             $contact->save(array('company_contact_id' => 0));
         }
     }
     $values['photo_url_96'] = $contact->getPhoto(96);
     $values['photo_url_20'] = $contact->getPhoto(20);
     $fields = waContactFields::getInfo($contact['is_company'] ? 'company' : 'person', true);
     echo json_encode(array('fields' => $fields, 'values' => $values, 'top' => $contact->getTopFields()));
 }
 public function updateLastTime($force = false)
 {
     $time = $this->storage->read('user_last_datetime');
     if (!$time || $force || $time == '0000-00-00 00:00:00' || time() - strtotime($time) > 120) {
         try {
             $login_log_model = new waLoginLogModel();
             $last_activity = $login_log_model->getCurrent($this->id);
         } catch (waDbException $e) {
             if ($e->getCode() == 1146) {
                 waSystem::getInstance()->getAuth()->clearAuth();
                 header("Location: " . wa()->getConfig()->getBackendUrl(true));
                 exit;
             }
         }
         $contact_model = new waContactModel();
         $contact_info = $contact_model->getById($this->id);
         $auth = waSystem::getInstance()->getAuth();
         if (!$auth->checkAuth($contact_info)) {
             header("Location: " . wa()->getConfig()->getRequestUrl(false));
             exit;
         }
         if (!$contact_info || waSystem::getInstance()->getEnv() == 'backend' && !$contact_info['is_user']) {
             waSystem::getInstance()->getAuth()->clearAuth();
             header("Location: " . wa()->getConfig()->getBackendUrl(true));
             exit;
         } else {
             $this->setCache($contact_info);
         }
         if (!$last_activity) {
             $login_log_model->insert(array('contact_id' => $this->id, 'datetime_in' => date("Y-m-d H:i:s"), 'datetime_out' => null));
         } elseif ($last_datetime = strtotime($time)) {
             if (time() - $last_datetime > self::$options['activity_timeout']) {
                 $login_log_model->updateById($last_activity['id'], array('datetime_out' => $time));
                 $login_log_model->insert(array('contact_id' => $this->id, 'datetime_in' => date("Y-m-d H:i:s"), 'datetime_out' => null));
             }
         }
         $t = date("Y-m-d H:i:s");
         $contact_model->updateById($this->id, array('last_datetime' => $t));
         $this->storage->write('user_last_datetime', $t);
     }
 }
 /** Using $this->id get waContact and save it in $this->contact;
  * Load vars into $this->view specific to waContact. */
 protected function getContactInfo()
 {
     $system = wa();
     if ($this->id == $system->getUser()->getId()) {
         $this->contact = $system->getUser();
         $this->view->assign('own_profile', true);
     } else {
         $this->contact = new waContact($this->id);
         $this->view->assign('own_profile', false);
     }
     $exists = $this->contact->exists();
     if ($exists) {
         $this->view->assign('contact', $this->contact);
         // who created this contact and when
         $this->view->assign('contact_create_time', waDateTime::format('datetime', $this->contact['create_datetime'], $system->getUser()->getTimezone()));
         if ($this->contact['create_contact_id']) {
             try {
                 $author = new waContact($this->contact['create_contact_id']);
                 if ($author['name']) {
                     $this->view->assign('author', $author);
                 }
             } catch (Exception $e) {
                 // Contact not found. Ignore silently.
             }
         }
         $this->view->assign('top', $this->contact->getTopFields());
         // Main contact editor data
         $fieldValues = $this->contact->load('js', true);
         $m = new waContactModel();
         if (isset($fieldValues['company_contact_id'])) {
             if (!$m->getById($fieldValues['company_contact_id'])) {
                 $fieldValues['company_contact_id'] = 0;
                 $this->contact->save(array('company_contact_id' => 0));
             }
         }
         $contactFields = waContactFields::getInfo($this->contact['is_company'] ? 'company' : 'person', true);
         // Only show fields that are allowed in own profile
         if (!empty($this->params['limited_own_profile'])) {
             $allowed = array();
             foreach (waContactFields::getAll('person') as $f) {
                 if ($f->getParameter('allow_self_edit')) {
                     $allowed[$f->getId()] = true;
                 }
             }
             $fieldValues = array_intersect_key($fieldValues, $allowed);
             $contactFields = array_intersect_key($contactFields, $allowed);
         }
         contactsHelper::normalzieContactFieldValues($fieldValues, $contactFields);
         $this->view->assign('contactFields', $contactFields);
         $this->view->assign('contactFieldsOrder', array_keys($contactFields));
         $this->view->assign('fieldValues', $fieldValues);
         // Contact categories
         $cm = new waContactCategoriesModel();
         $this->view->assign('contact_categories', array_values($cm->getContactCategories($this->id)));
     } else {
         $this->view->assign('contact', array('id' => $this->id));
     }
     return $exists;
 }
 /**
  * @param string $hash
  * @return bool|waContact
  */
 protected function checkHash($hash)
 {
     $contact_id = substr($hash, 16, -16);
     $contact_model = new waContactModel();
     $contact = $contact_model->getById($contact_id);
     if ($contact && $hash === $this->getHash($contact_id)) {
         return new waContact($contact_id);
     }
     return false;
 }
 /**
  * @return array|bool
  * @throws waException
  */
 protected function _authByCookie()
 {
     if ($this->getOption('remember_enabled') && ($token = waRequest::cookie('auth_token'))) {
         $model = new waContactModel();
         $response = waSystem::getInstance()->getResponse();
         $id = substr($token, 15, -15);
         $user_info = $model->getById($id);
         $this->checkBan($user_info);
         $cookie_domain = ifset($this->options['cookie_domain'], '');
         if ($user_info && ($user_info['is_user'] > 0 || !$this->options['is_user']) && $token === $this->getToken($user_info)) {
             $response->setCookie('auth_token', $token, time() + 2592000, null, $cookie_domain, false, true);
             return $this->getAuthData($user_info);
         } else {
             $response->setCookie('auth_token', null, -1, null, $cookie_domain);
         }
     }
     return false;
 }
 public function contact($id)
 {
     if (!is_numeric($id)) {
         $collection = new waContactsCollection('/search/' . $id . '/', array('check_rights' => false));
         $result = $collection->getContacts('id', 0, 1);
         if ($result) {
             $c = current($result);
             return new waContact($c['id']);
         } else {
             return new waContact();
         }
     }
     $contact_model = new waContactModel();
     if ($contact = $contact_model->getById($id)) {
         return new waContact($contact);
     }
     return new waContact();
 }
Exemple #9
0
 /**
  * Returns contact's locale id.
  *
  * @return string
  */
 public function getLocale()
 {
     if (!$this->id) {
         $locale = isset($this->data['locale']) ? $this->data['locale'] : null;
         if (!$locale) {
             $locale = waRequest::get('lang');
         }
     } else {
         if (isset(self::$cache[$this->id]['locale'])) {
             $locale = self::$cache[$this->id]['locale'];
         } else {
             $contact_model = new waContactModel();
             $contact_info = $contact_model->getById($this->id);
             $this->setCache($contact_info);
             $locale = isset($contact_info['locale']) ? $contact_info['locale'] : '';
         }
     }
     if (wa()->getEnv() == 'frontend' && waRequest::param('locale')) {
         return waRequest::param('locale');
     }
     // try get locale by header Accept-Language (only for current user)
     if (!$locale && $this instanceof waAuthUser) {
         $locale = waRequest::getLocale();
     }
     if (!$locale) {
         $locale = self::$options['default']['locale'];
     }
     return $locale;
 }
 /**
  * Returns array of orders included in collection.
  * 
  * @param string $fields List of order properties, comma-separated, to be included in returned array:
  *     '*' — values from shop_order table
  *     '*,params,items,contact' (different combinations are acceptable) — values from tables shop_order, shop_order_items, shop_order_params, wa_contact
  * @param int $offset Initial position in returned order array, 0 means first order in collection
  * @param int|bool $limit Maximum order limit. 
  *     If a Boolean value is specified, then $escape = $limit and $limit = null
  *     If no value is specified, then $limit = 0.
  *     If no value is specified and $offset is non-zero, then $limit = $offset and $offset = 50   
  * @param bool $escape Whether order parameters and contact names must be escaped using htmlspecialchars() function, defaults to true
  * 
  * @return array Array of collection orders' sub-arrays
  */
 public function getOrders($fields = "*", $offset = 0, $limit = null, $escape = true)
 {
     if (is_bool($limit)) {
         $escape = $limit;
         $limit = null;
     }
     if ($limit === null) {
         if ($offset) {
             $limit = $offset;
             $offset = 0;
         } else {
             $limit = 50;
         }
     }
     $sql = $this->getSQL();
     $sql = "SELECT " . $this->getFields($fields) . " " . $sql;
     $sql .= " LIMIT " . ($offset ? $offset . ',' : '') . (int) $limit;
     $data = $this->getModel()->query($sql)->fetchAll('id');
     if (!$data) {
         return array();
     }
     $ids = array_keys($data);
     // add other fields
     foreach ($this->other_fields as $field) {
         switch ($field) {
             case 'items':
             case 'params':
                 $rows = $this->getModel($field)->getByField('order_id', $ids, true);
                 foreach ($rows as $row) {
                     if ($field == 'params') {
                         $data[$row['order_id']][$field][$row['name']] = $row['value'];
                     } else {
                         if ($escape) {
                             $row['name'] = htmlspecialchars($row['name']);
                         }
                         $data[$row['order_id']][$field][] = $row;
                     }
                 }
                 break;
             case 'contact':
                 $contact_ids = array();
                 foreach ($data as $o) {
                     $contact_ids[] = $o['contact_id'];
                 }
                 $contact_model = new waContactModel();
                 $contacts = $contact_model->getById(array_unique($contact_ids));
                 foreach ($data as &$o) {
                     if (isset($contacts[$o['contact_id']])) {
                         $c = $contacts[$o['contact_id']];
                         $o['contact'] = array('id' => $c['id'], 'name' => waContactNameField::formatName($c), 'photo' => $c['photo']);
                         if ($escape) {
                             $o['contact']['name'] = htmlspecialchars($o['contact']['name']);
                         }
                     }
                 }
                 unset($o);
                 break;
         }
     }
     unset($t);
     return $data;
 }