protected function createUser($id)
 {
     $user = new waUser($id);
     if ($user['is_user']) {
         throw new waException('Already a user.');
     }
     if (waRequest::post('invite')) {
         $user['password'] = uniqid(time(), true);
     } else {
         $login = trim(waRequest::post('login'));
         if (strlen($login) <= 0) {
             $this->errors[] = _w('Login is required.');
         } else {
             if ($u = waUser::getByLogin($login)) {
                 $nameLink = '<a href="' . wa_url() . 'webasyst/contacts/#/contact/' . $u->getId() . '">' . $u->get('name') . '</a>';
                 $this->errors[] = str_replace('%NAME_LINK%', $nameLink, _w('This login is already set for user %NAME_LINK%'));
             }
         }
         $user['login'] = $login;
         if (waRequest::post('password') === waRequest::post('confirm_password')) {
             $user['password'] = waRequest::post('password');
         } else {
             $this->errors[] = _w('Passwords do not match.');
         }
     }
     if (!$this->errors) {
         $user['is_user'] = 1;
         $this->response = $user->save();
         $this->log('create_user_account', 1);
     }
 }
 /** 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;
 }
 public function execute()
 {
     $contact_model = new waContactModel();
     if ($contact_model->countAll()) {
         $this->redirect($this->getConfig()->getBackendUrl(true));
     }
     if (($locale = waRequest::get('lang')) && waLocale::getInfo($locale)) {
         // set locale
         wa()->setLocale($locale);
         // save to database default locale
         $app_settings_model = new waAppSettingsModel();
         $app_settings_model->set('webasyst', 'locale', $locale);
     }
     if (file_exists($this->getConfig()->getRootPath() . '/install.php')) {
         @unlink($this->getConfig()->getRootPath() . '/install.php');
     }
     if (waRequest::getMethod() == 'post') {
         $errors = array();
         $login = waRequest::post('login');
         $validator = new waLoginValidator();
         if (!$validator->isValid($login)) {
             $errors['login'] = implode("<br />", $validator->getErrors());
         }
         $password = waRequest::post('password');
         $password_confirm = waRequest::post('password_confirm');
         if ($password !== $password_confirm) {
             $errors['password'] = _w('Passwords do not match');
         }
         $email = waRequest::post('email');
         $validator = new waEmailValidator();
         if (!$validator->isValid($email)) {
             $errors['email'] = implode("<br />", $validator->getErrors());
         }
         if ($errors) {
             $this->view->assign('errors', $errors);
         } else {
             // save account name
             $app_settings_model = new waAppSettingsModel();
             $app_settings_model->set('webasyst', 'name', waRequest::post('account_name'));
             if ($email) {
                 $app_settings_model->set('webasyst', 'email', $email);
                 $app_settings_model->set('webasyst', 'sender', $email);
             }
             // create user
             $user = new waUser();
             $firstname = waRequest::post('firstname');
             $user['firstname'] = $firstname ? $firstname : $login;
             $user['lastname'] = waRequest::post('lastname');
             $user['is_user'] = 1;
             $user['login'] = $login;
             $user['password'] = $password;
             $user['email'] = $email;
             $user['locale'] = wa()->getLocale();
             $user['create_method'] = 'install';
             if ($errors = $user->save()) {
                 $result = array();
                 foreach ($errors as $k => $v) {
                     $result['all'][] = $k . ": " . (is_array($v) ? implode(', ', $v) : $v);
                 }
                 $result['all'] = implode("\r\n", $result['all']);
                 $this->view->assign('errors', $result);
             } else {
                 $user->setRight('webasyst', 'backend', 1);
                 waSystem::getInstance()->getAuth()->auth(array('login' => $login, 'password' => $password));
                 $path = $this->getConfig()->getPath('config');
                 // check routing.php
                 if (!file_exists($path . '/routing.php')) {
                     $apps = wa()->getApps();
                     $data = array();
                     $domain = $this->getConfig()->getDomain();
                     $site = false;
                     foreach ($apps as $app_id => $app) {
                         if ($app_id == 'site') {
                             $site = true;
                         } elseif (!empty($app['frontend'])) {
                             $routing = array('url' => $app_id . '/*', 'app' => $app_id);
                             if (!empty($app['routing_params']) && is_array($app['routing_params'])) {
                                 $routing = array_merge($routing, $app['routing_params']);
                             }
                             $data[$domain][] = $routing;
                         }
                     }
                     if ($site) {
                         $data[$domain][] = array('url' => '*', 'app' => 'site');
                     }
                     waUtils::varExportToFile($data, $path . '/routing.php');
                 }
                 // redirect to backend
                 $this->redirect($this->getConfig()->getBackendUrl(true));
             }
         }
     }
 }
 protected function createLogin($id, $just_check = false)
 {
     $user = new waUser($id);
     $login = trim(waRequest::post('login'));
     if (strlen($login) <= 0) {
         $this->errors[] = _w('Login is required.');
     } else {
         if ($u = $this->loginExists($login, $id)) {
             $nameLink = '<a href="' . wa_url() . 'webasyst/contacts/#/contact/' . $u['id'] . '">' . $u['name'] . '</a>';
             $this->errors[] = str_replace('%NAME_LINK%', $nameLink, _w('This login is already set for user %NAME_LINK%'));
         }
     }
     if (!$this->errors) {
         if ($just_check) {
             return true;
         }
         $user['login'] = $login;
         $r = $user->save();
         if ($r === 0) {
             return array('login' => $login);
         } else {
             $this->errors = $r;
             return false;
         }
     } else {
         return false;
     }
 }