public function DownloadUserReport()
 {
     $vs_download_format = $this->request->getParameter("download_format", pString);
     if (!$vs_download_format) {
         $vs_download_format = "tab";
     }
     $this->view->setVar("download_format", $vs_download_format);
     switch ($vs_download_format) {
         default:
         case "tab":
             $this->view->setVar("file_extension", "txt");
             $this->view->setVar("mimetype", "text/plain");
             $vs_delimiter_col = "\t";
             $vs_delimiter_row = "\n";
             break;
             # -----------------------------------
         # -----------------------------------
         case "csv":
             $this->view->setVar("file_extension", "txt");
             $this->view->setVar("mimetype", "text/plain");
             $vs_delimiter_col = ",";
             $vs_delimiter_row = "\n";
             break;
             # -----------------------------------
     }
     $o_db = new Db();
     $t_user = new ca_users();
     $va_fields = array("lname", "fname", "email", "user_name", "userclass", "active", "last_login");
     $va_profile_prefs = $t_user->getValidPreferences('profile');
     $va_profile_prefs_labels = array();
     foreach ($va_profile_prefs as $vs_pref) {
         $va_pref_info = $t_user->getPreferenceInfo($vs_pref);
         $va_profile_prefs_labels[$vs_pref] = $va_pref_info["label"];
     }
     $qr_users = $o_db->query("SELECT * FROM ca_users ORDER BY user_id DESC");
     if ($qr_users->numRows()) {
         $va_rows = array();
         # --- headings
         $va_row = array();
         # --- headings for field values
         foreach ($va_fields as $vs_field) {
             switch ($vs_field) {
                 case "last_login":
                     $va_row[] = _t("Last login");
                     break;
                     # --------------------
                 # --------------------
                 default:
                     $va_row[] = $t_user->getDisplayLabel("ca_users." . $vs_field);
                     break;
                     # --------------------
             }
         }
         # --- headings for profile prefs
         foreach ($va_profile_prefs_labels as $vs_pref => $vs_pref_label) {
             $va_row[] = $vs_pref_label;
         }
         $va_rows[] = join($vs_delimiter_col, $va_row);
         reset($va_fields);
         reset($va_profile_prefs_labels);
         $o_tep = new TimeExpressionParser();
         while ($qr_users->nextRow()) {
             $va_row = array();
             # --- fields
             foreach ($va_fields as $vs_field) {
                 switch ($vs_field) {
                     case "userclass":
                         $va_row[] = $t_user->getChoiceListValue($vs_field, $qr_users->get("ca_users." . $vs_field));
                         break;
                         # -----------------------
                     # -----------------------
                     case "active":
                         $va_row[] = $qr_users->get("ca_users." . $vs_field) == 1 ? _t("active") : _t("not active");
                         break;
                         # -----------------------
                     # -----------------------
                     case "last_login":
                         //if (!is_array($va_vars = $qr_users->getVars('vars'))) { $va_vars = array(); }
                         if (!is_array($va_vars = $qr_users->getVars('volatile_vars'))) {
                             $va_vars = array();
                         }
                         if ($va_vars['last_login'] > 0) {
                             $o_tep->setUnixTimestamps($va_vars['last_login'], $va_vars['last_login']);
                             $va_row[] = $o_tep->getText();
                         } else {
                             $va_row[] = "-";
                         }
                         break;
                         # -----------------------
                     # -----------------------
                     default:
                         if ($vs_download_format == "csv") {
                             $va_row[] = str_replace(",", "-", $qr_users->get("ca_users." . $vs_field));
                         } else {
                             $va_row[] = $qr_users->get("ca_users." . $vs_field);
                         }
                         break;
                         # -----------------------
                 }
             }
             # --- profile prefs
             foreach ($va_profile_prefs_labels as $vs_pref => $vs_pref_label) {
                 $t_user->load($qr_users->get("ca_users.user_id"));
                 $va_row[] = $t_user->getPreference($vs_pref);
             }
             $va_rows[] = join($vs_delimiter_col, $va_row);
         }
         $vs_file_contents = join($vs_delimiter_row, $va_rows);
         $this->view->setVar("file_contents", $vs_file_contents);
         return $this->render('user_report.php');
     } else {
         $this->notification->addNotification(_t("There are no users"), __NOTIFICATION_TYPE_INFO__);
         $this->ListUsers();
         return;
     }
 }
 function register()
 {
     if ($this->request->config->get('dont_allow_registration_and_login')) {
         $this->notification->addNotification(_t("Registration is not enabled"), __NOTIFICATION_TYPE_ERROR__);
         $this->redirect(caNavUrl($this->request, '', 'Front', 'Index'));
         return;
     }
     MetaTagManager::setWindowTitle($this->request->config->get("app_display_name") . ": " . _t("Register"));
     # logout user in case is already logged in
     $this->request->deauthenticate();
     $t_user = new ca_users();
     $t_user->purify(true);
     # --- process incoming registration attempt
     $ps_email = $this->request->getParameter("email", pString);
     $ps_fname = $this->request->getParameter("fname", pString);
     $ps_lname = $this->request->getParameter("lname", pString);
     $ps_password = $this->request->getParameter("password", pString);
     $ps_password2 = $this->request->getParameter("password2", pString);
     $ps_security = $this->request->getParameter("security", pString);
     $va_errors = array();
     if (!caCheckEmailAddress($ps_email)) {
         $va_errors["email"] = _t("E-mail address is not valid.");
     } else {
         $t_user->set("email", $ps_email);
     }
     if (!$ps_fname) {
         $va_errors["fname"] = _t("Please enter your first name");
     } else {
         $t_user->set("fname", $ps_fname);
     }
     if (!$ps_lname) {
         $va_errors["lname"] = _t("Please enter your last name");
     } else {
         $t_user->set("lname", $ps_lname);
     }
     if (!$ps_password || !$ps_password2) {
         $va_errors["password"] = _t("Please enter and re-type your password.");
     } else {
         if ($ps_password != $ps_password2) {
             $va_errors["password"] = _t("Passwords do not match");
         } else {
             $t_user->set("password", $ps_password);
         }
     }
     if (!$ps_security) {
         $va_errors["security"] = _t("Please answer the security question.");
     } else {
         if ($ps_security != $_REQUEST["sum"]) {
             $va_errors["security"] = _t("Your answer was incorrect, please try again");
         }
     }
     // Check user profile responses
     $va_profile_prefs = $t_user->getValidPreferences('profile');
     if (is_array($va_profile_prefs) && sizeof($va_profile_prefs)) {
         foreach ($va_profile_prefs as $vs_pref) {
             $vs_pref_value = $this->request->getParameter('pref_' . $vs_pref, pString);
             if (!$t_user->isValidPreferenceValue($vs_pref, $vs_pref_value)) {
                 $va_errors[$vs_pref] = join("; ", $t_user->getErrors());
                 $t_user->clearErrors();
             }
         }
     }
     # --- does deleted user login record for this user already exist?
     # --- (look for active records only; inactive records will effectively block reregistration)
     $vb_user_exists_but_is_deleted = false;
     if ($t_user->load(array('user_name' => $ps_email))) {
         if ((int) $t_user->get('userclass') == 255) {
             if ($t_user->get('active') == 1) {
                 // yeah... so allow registration
                 $vb_user_exists_but_is_deleted = true;
             } else {
                 // existing inactive user record blocks registration
                 $va_errors["email"] = _t("User cannot register");
             }
         } else {
             // already valid login with this user name
             $va_errors["email"] = _t("A user has already registered with this email address");
         }
     }
     # get names of form fields
     $va_fields = $t_user->getFormFields();
     # loop through fields
     foreach ($va_fields as $vs_f => $va_attr) {
         switch ($vs_f) {
             case "user_name":
                 if (!$vb_user_exists_but_is_deleted && !sizeof($va_errors)) {
                     # set field value
                     $t_user->set("user_name", $ps_email);
                     if ($t_user->numErrors() > 0) {
                         $va_errors[$vs_f] = join("; ", $t_user->getErrors());
                     }
                 }
                 break;
                 # -------------
             # -------------
             case "active":
                 if ($this->request->config->get('dont_approve_logins_on_registration')) {
                     $t_user->set("active", 0);
                 } else {
                     $t_user->set("active", 1);
                 }
                 break;
                 # -------------
             # -------------
             case "userclass":
                 $t_user->set("userclass", 1);
                 // 1=public-only
                 break;
                 # -------------
             # -------------
             default:
                 if (!$va_errors[$vs_f]) {
                     $t_user->set($vs_f, $_REQUEST[$vs_f]);
                     # set field values
                     if ($t_user->numErrors() > 0) {
                         $va_errors[$vs_f] = join("; ", $t_user->getErrors());
                     }
                 }
                 break;
                 # -------------
         }
     }
     // Save user profile responses
     if (is_array($va_profile_prefs) && sizeof($va_profile_prefs)) {
         foreach ($va_profile_prefs as $vs_pref) {
             $t_user->setPreference($vs_pref, $this->request->getParameter('pref_' . $vs_pref, pString));
         }
     }
     if (sizeof($va_errors) == 0) {
         # --- there are no errors so make new user record
         $t_user->setMode(ACCESS_WRITE);
         if ($vb_user_exists_but_is_deleted) {
             $t_user->update();
         } else {
             $t_user->insert();
         }
         $pn_user_id = $t_user->get("user_id");
         if ($t_user->numErrors()) {
             $va_errors["register"] = join("; ", $t_user->getErrors());
         } else {
             # --- add default roles
             if (($va_default_roles = $this->request->config->getList('registration_default_roles')) && sizeof($va_default_roles)) {
                 $t_user->addRoles($va_default_roles);
             }
             # --- user is joining a user group from a supplied link
             if ($this->request->session->getVar("join_user_group_id")) {
                 if (!$t_user->inGroup($this->request->session->getVar("join_user_group_id"))) {
                     $t_user->addToGroups($this->request->session->getVar("join_user_group_id"));
                     $this->request->session->setVar("join_user_group_id", "");
                     $vs_group_message = _t(" You were added to the group");
                 } else {
                     $this->request->session->setVar("join_user_group_id", "");
                     $vs_group_message = _t(" You are already a member of the group");
                 }
             }
             # --- send email confirmation
             $o_view = new View($this->request, array($this->request->getViewsDirectoryPath()));
             # -- generate email subject line from template
             $vs_subject_line = $o_view->render("mailTemplates/reg_conf_subject.tpl");
             # -- generate mail text from template - get both the text and the html versions
             $vs_mail_message_text = $o_view->render("mailTemplates/reg_conf.tpl");
             $vs_mail_message_html = $o_view->render("mailTemplates/reg_conf_html.tpl");
             caSendmail($t_user->get('email'), $this->request->config->get("ca_admin_email"), $vs_subject_line, $vs_mail_message_text, $vs_mail_message_html);
             if ($this->request->config->get("email_notification_for_new_registrations")) {
                 # --- send email to admin
                 $o_view = new View($this->request, array($this->request->getViewsDirectoryPath()));
                 $o_view->setVar("t_user", $t_user);
                 # -- generate email subject line from template
                 $vs_subject_line = $o_view->render("mailTemplates/reg_admin_notification_subject.tpl");
                 # -- generate mail text from template - get both the text and the html versions
                 $vs_mail_message_text = $o_view->render("mailTemplates/reg_admin_notification.tpl");
                 $vs_mail_message_html = $o_view->render("mailTemplates/reg_admin_notification_html.tpl");
                 caSendmail($this->request->config->get("ca_admin_email"), $this->request->config->get("ca_admin_email"), $vs_subject_line, $vs_mail_message_text, $vs_mail_message_html);
             }
             $t_user = new ca_users();
             $vs_action = $vs_controller = $vs_module_path = '';
             if ($vs_default_action = $this->request->config->get('default_action')) {
                 $va_tmp = explode('/', $vs_default_action);
                 $vs_action = array_pop($va_tmp);
                 if (sizeof($va_tmp)) {
                     $vs_controller = array_pop($va_tmp);
                 }
                 if (sizeof($va_tmp)) {
                     $vs_module_path = join('/', $va_tmp);
                 }
             } else {
                 $vs_controller = 'Splash';
                 $vs_action = 'Index';
             }
             $vs_url = caNavUrl($this->request, $vs_module_path, $vs_controller, $vs_action);
             if ($t_user->get("active")) {
                 # log in the new user
                 $this->request->doAuthentication(array('dont_redirect' => true, 'user_name' => $ps_email, 'password' => $ps_password));
                 if ($this->request->isLoggedIn()) {
                     if ($this->request->isAjax()) {
                         $this->view->setVar("message", _t('Thank you for registering!  You are now logged in.') . $vs_group_message);
                         $this->render("Form/reload_html.php");
                         return;
                     } else {
                         $this->notification->addNotification(_t('Thank you for registering!  You are now logged in.') . $vs_group_message, __NOTIFICATION_TYPE_INFO__);
                         $this->response->setRedirect($vs_url);
                     }
                 } else {
                     $va_errors["register"] = _t("Login failed.");
                 }
             } else {
                 # --- registration needs approval
                 $this->notification->addNotification(_t('Thank you for registering!  Your account will be activated after review.') . $vs_group_message, __NOTIFICATION_TYPE_INFO__);
                 $this->response->setRedirect($vs_url);
             }
         }
     }
     if (sizeof($va_errors) > 0) {
         $this->view->setVar('errors', $va_errors);
         $this->registerForm($t_user);
     }
 }
 /**
  * Return user profile values for specified user
  */
 public function GetUserProfileInfo()
 {
     if (!$this->request->user->canDoAction('can_manage_clients')) {
         return null;
     }
     $pn_user_id = $this->request->getParameter('user_id', pInteger);
     $t_user = new ca_users($pn_user_id);
     $va_profile_prefs = $t_user->getValidPreferences('profile');
     if (is_array($va_profile_prefs) && sizeof($va_profile_prefs)) {
         $va_elements = array();
         foreach ($va_profile_prefs as $vs_pref) {
             $va_pref_info = $t_user->getPreferenceInfo($vs_pref);
             $va_elements[str_replace('user_profile_', '', $vs_pref)] = array($t_user->getPreference($vs_pref));
         }
         $this->view->setVar("profile_values", $va_elements);
     }
     $this->view->setVar("user_id", $pn_user_id);
     $this->render('ajax_user_profile_info_json.php');
 }
 function register()
 {
     # logout user in case is already logged in
     $this->request->deauthenticate();
     $t_user = new ca_users();
     # --- process incoming registration attempt
     $ps_email = strip_tags($this->request->getParameter("email", pString));
     $ps_fname = strip_tags($this->request->getParameter("fname", pString));
     $ps_lname = strip_tags($this->request->getParameter("lname", pString));
     $ps_password = $this->request->getParameter("password", pString);
     $ps_password2 = $this->request->getParameter("password2", pString);
     $ps_security = $this->request->getParameter("security", pString);
     $va_errors = array();
     if (!caCheckEmailAddress($ps_email)) {
         $va_errors["email"] = _t("E-mail address is not valid.");
     } else {
         $t_user->set("email", $ps_email);
     }
     if (!$ps_fname) {
         $va_errors["fname"] = _t("Please enter your first name");
     } else {
         $t_user->set("fname", $ps_fname);
     }
     if (!$ps_lname) {
         $va_errors["lname"] = _t("Please enter your last name");
     } else {
         $t_user->set("lname", $ps_lname);
     }
     if (!$ps_password || !$ps_password2) {
         $va_errors["password"] = _t("Please enter and re-type your password.");
     } else {
         if ($ps_password != $ps_password2) {
             $va_errors["password"] = _t("Passwords do not match");
         } else {
             $t_user->set("password", $ps_password);
         }
     }
     if (!$ps_security) {
         $va_errors["security"] = _t("Please answer the security question.");
     } else {
         if ($ps_security != $_REQUEST["sum"]) {
             $va_errors["security"] = _t("Your answer was incorrect, please try again");
         }
     }
     // Check user profile responses
     $va_profile_prefs = $t_user->getValidPreferences('profile');
     if (is_array($va_profile_prefs) && sizeof($va_profile_prefs)) {
         foreach ($va_profile_prefs as $vs_pref) {
             $vs_pref_value = $this->request->getParameter('pref_' . $vs_pref, pString);
             if (!$t_user->isValidPreferenceValue($vs_pref, $vs_pref_value)) {
                 $va_errors[$vs_pref] = join("; ", $t_user->getErrors());
                 $t_user->clearErrors();
             }
         }
     }
     # --- does deleted user login record for this user already exist?
     # --- (look for active records only; inactive records will effectively block reregistration)
     $vb_user_exists_but_is_deleted = false;
     if ($t_user->load(array('user_name' => $ps_email))) {
         if ((int) $t_user->get('userclass') == 255) {
             if ($t_user->get('active') == 1) {
                 // yeah... so allow registration
                 $vb_user_exists_but_is_deleted = true;
             } else {
                 // existing inactive user record blocks registration
                 $va_errors["email"] = _t("User cannot register");
             }
         } else {
             // already valid login with this user name
             $va_errors["email"] = _t("A user has already registered with this email address");
         }
     }
     # get names of form fields
     $va_fields = $t_user->getFormFields();
     # loop through fields
     foreach ($va_fields as $vs_f => $va_attr) {
         switch ($vs_f) {
             case "user_name":
                 if (!$vb_user_exists_but_is_deleted && !sizeof($va_errors)) {
                     # set field value
                     $t_user->set("user_name", $ps_email);
                     if ($t_user->numErrors() > 0) {
                         $va_errors[$vs_f] = join("; ", $t_user->getErrors());
                     }
                 }
                 break;
                 # -------------
             # -------------
             case "active":
                 $t_user->set("active", 1);
                 break;
                 # -------------
             # -------------
             case "userclass":
                 $t_user->set("userclass", 1);
                 // 1=public-only
                 break;
                 # -------------
             # -------------
             default:
                 if (!$va_errors[$vs_f]) {
                     $t_user->set($vs_f, $_REQUEST[$vs_f]);
                     # set field values
                     if ($t_user->numErrors() > 0) {
                         $va_errors[$vs_f] = join("; ", $t_user->getErrors());
                     }
                 }
                 break;
                 # -------------
         }
     }
     // Save user profile responses
     if (is_array($va_profile_prefs) && sizeof($va_profile_prefs)) {
         foreach ($va_profile_prefs as $vs_pref) {
             $t_user->setPreference($vs_pref, $this->request->getParameter('pref_' . $vs_pref, pString));
         }
     }
     if (sizeof($va_errors) == 0) {
         # --- there are no errors so make new user record
         $t_user->setMode(ACCESS_WRITE);
         if ($vb_user_exists_but_is_deleted) {
             $t_user->update();
         } else {
             $t_user->insert();
         }
         $pn_user_id = $t_user->get("user_id");
         if ($t_user->numErrors()) {
             $va_errors["register"] = join("; ", $t_user->getErrors());
         } else {
             # --- add default roles
             if (($va_default_roles = $this->request->config->getList('registration_default_roles')) && sizeof($va_default_roles)) {
                 $t_user->addRoles($va_default_roles);
             }
             # --- send email confirmation
             # -- generate email subject line from template
             ob_start();
             require $this->request->getViewsDirectoryPath() . "/mailTemplates/reg_conf_subject.tpl";
             $vs_subject_line = ob_get_contents();
             ob_end_clean();
             # -- generate mail text from template - get both the text and the html versions
             ob_start();
             require $this->request->getViewsDirectoryPath() . "/mailTemplates/reg_conf.tpl";
             $vs_mail_message_text = ob_get_contents();
             ob_end_clean();
             ob_start();
             require $this->request->getViewsDirectoryPath() . "/mailTemplates/reg_conf_html.tpl";
             $vs_mail_message_html = ob_get_contents();
             ob_end_clean();
             caSendmail($t_user->get('email'), $this->request->config->get("ca_admin_email"), $vs_subject_line, $vs_mail_message_text, $vs_mail_message_html);
             $t_user = new ca_users();
             # log in the new user
             $this->request->doAuthentication(array('dont_redirect' => true, 'user_name' => $ps_email, 'password' => $ps_password));
             if ($this->request->isLoggedIn()) {
                 # --- login successful so redirect to search page
                 $this->notification->addNotification(_t('Thank you for registering!  You are now logged in.'), __NOTIFICATION_TYPE_INFO__);
                 $vo_session = $this->request->getSession();
                 $vs_last_page = $vo_session->getVar('site_last_page');
                 $vo_session->setVar('site_last_page', "");
                 switch ($vs_last_page) {
                     case "Sets":
                         $this->response->setRedirect(caNavUrl($this->request, "", "Sets", "addItem", array("object_id" => $vo_session->getVar('site_last_page_object_id'))));
                         break;
                         # --------------------
                     # --------------------
                     case "ObjectDetail":
                         $this->response->setRedirect(caNavUrl($this->request, "Detail", "Object", "Show", array("object_id" => $vo_session->getVar('site_last_page_object_id'))));
                         break;
                         # --------------------
                     # --------------------
                     default:
                         $this->response->setRedirect(caNavUrl($this->request, "", "", ""));
                         break;
                         # --------------------
                 }
             } else {
                 $va_errors["register"] = _t("Login failed.");
             }
         }
     } else {
         $this->view->setVar('reg_errors', $va_errors);
     }
     $this->form($t_user);
 }