X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=blobdiff_plain;f=application%2Fmodels%2Fusers_model.php;h=30e18d70b7f6119a061fb90907e205018e2d8185;hb=8269d73157518cae557446f311e1ab013aa8915c;hp=8046ed3f50461d2bd8eb9d58cf23042ca2b31a78;hpb=480a21049384e47afe920e73972ca336ec8e6a54;p=living-lab-site.git diff --git a/application/models/users_model.php b/application/models/users_model.php index 8046ed3..30e18d7 100644 --- a/application/models/users_model.php +++ b/application/models/users_model.php @@ -4,7 +4,7 @@ * Class Users_model models user information from DB * * @category Model - * @author calinburloiu + * @author Călin-Andrei Burloiu * */ class Users_model extends CI_Model { @@ -189,65 +189,97 @@ class Users_model extends CI_Model { { $sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($op_response); $ax_resp = Auth_OpenID_AX_FetchResponse::fromSuccessResponse($op_response); + + if ($ax_resp) + { + $ax_email = $ax_resp->get('http://axschema.org/contact/email'); + $ax_first_name = $ax_resp->get( + 'http://axschema.org/namePerson/first'); + $ax_last_name = $ax_resp->get('http://axschema.org/namePerson/last'); + $ax_country = $ax_resp->get('http://axschema.org/contact/country'); + } + else + { + $ax_email = ''; + $ax_first_name = ''; + $ax_last_name = ''; + $ax_country = ''; + } + + if ($sreg_resp) + { + $sreg_email = $sreg_resp->get('email', ''); + $sreg_fullname = $sreg_resp->get('fullname', ''); + $sreg_nickname = $sreg_resp->get('nickname', ''); + $sreg_country = $sreg_resp->get('country', ''); + $sreg_dob = $sreg_resp->get('dob', NULL); + } + else + { + $sreg_email = $sreg_fullname = $sreg_nickname = $sreg_country = ''; + $sreg_dob = NULL; + } // E-mail - $email = $ax_resp->get('http://axschema.org/contact/email'); - if (empty($email) || is_a($email, 'Auth_OpenID_AX_Error')) - $data['email'] = $sreg_resp->get('email', ''); + if (empty($ax_email) || is_a($ax_email, 'Auth_OpenID_AX_Error')) + $data['email'] = $sreg_email; else - $data['email'] = $email[0]; + $data['email'] = $ax_email[0]; + $data['email'] = strtolower($data['email']); // First Name - $first_name = $ax_resp->get('http://axschema.org/namePerson/first'); - if (empty($first_name) || is_a($first_name, 'Auth_OpenID_AX_Error')) + if (empty($ax_first_name) + || is_a($ax_first_name, 'Auth_OpenID_AX_Error')) $data['first_name'] = ''; else - $data['first_name'] = $first_name[0]; + $data['first_name'] = $ax_first_name[0]; // Sur Name - $last_name = $ax_resp->get('http://axschema.org/namePerson/last'); - if (empty($last_name) || is_a($last_name, 'Auth_OpenID_AX_Error')) + if (empty($ax_last_name) || is_a($ax_last_name, 'Auth_OpenID_AX_Error')) $data['last_name'] = ''; else - $data['last_name'] = $last_name[0]; + $data['last_name'] = $ax_last_name[0]; // First Name and Last Name if (empty($data['first_name']) || empty($data['last_name'])) { - $fullname = $sreg_resp->get('fullname'); - - if ($fullname) + if ($sreg_fullname) { if (empty($data['first_name'])) $data['first_name'] = substr( - $fullname, 0, strrpos($fullname, ' ')); + $sreg_fullname, 0, strrpos($sreg_fullname, ' ')); if (empty($data['last_name'])) $data['last_name'] = substr( - $fullname, strrpos($fullname, ' ') + 1); + $sreg_fullname, strrpos($sreg_fullname, ' ') + 1); } } // Username - $data['username'] = $sreg_resp->get('nickname'); + $data['username'] = $sreg_nickname; if (!$data['username']) { + // Generate username from email if (!empty($data['email'])) { - $data['email'] = strtolower($data['email']); $data['username'] = substr($data['email'], 0, strpos($data['email'], '@')); $data['username'] = preg_replace(array('/[^a-z0-9\._]*/'), array(''), $data['username']); } + // Generate username from first name and sur name else if(!empty($data['first_name']) || !empty($data['last_name'])) { $data['username'] = $data['first_name'] . '_' . $data['last_name']; } + // Generate a random username else $data['username'] = $this->gen_username(); } + // Limit username to 24 characters because a prefix of 8 characters + // will be added: 'autogen_'. $data['username'] = substr($data['username'], 0, 24); + // Append a random character to the username each time it still exists. if ($this->get_userdata($data['username'])) { $chars = 'abcdefghijklmnopqrstuvwxyz0123456789'; @@ -258,23 +290,23 @@ class Users_model extends CI_Model { $data['username'] .= $chars[ mt_rand(0, $len_chars - 1) ]; } while($this->get_userdata($data['username'])); } + // Usernames autogenerated have 'autogen_' prefix and can be changed + // by the user from the account section. After this process it cannot + // be changed anymore. $data['username'] = 'autogen_' . $data['username']; // Country - $country = $ax_resp->get('http://axschema.org/contact/country'); - if (empty($country) || is_a($country, 'Auth_OpenID_AX_Error')) - $data['country'] = $sreg_resp->get('country', ''); + if (empty($ax_country) || is_a($ax_country, 'Auth_OpenID_AX_Error')) + $data['country'] = $sreg_country; else - $data['country'] = $country[0]; + $data['country'] = $ax_country[0]; // Birth Date - $data['birth_date'] = $sreg_resp->get('dob', NULL); + $data['birth_date'] = $sreg_dob; // OpenID $data['auth_src'] = 'openid'; -// print_r($data); - if (!$this->register($data, $op_response->getDisplayIdentifier())) return FALSE; @@ -383,7 +415,7 @@ class Users_model extends CI_Model { * Adds a new user to DB. * Do not add join_date and last_login column, they will be automatically * added. - * Provide an 'openid' with the OpenID as value in order to register users + * Provide an $openid with the OpenID as value in order to register users * logging in this way. * * @param array $data corresponds to DB columns @@ -473,10 +505,45 @@ class Users_model extends CI_Model { return $query->row()->id; } - // TODO cleanup account activation - public function cleanup_account_activation() + /** + * Removes users that didn't activated their account within $days_to_expire + * days inclusively. + * + * @param int $days_to_expire + */ + public function cleanup_unactivated_users($days_to_expire) { + // Get user_id-s with expired activation period. + $query = $this->db->query("SELECT u.id + FROM `users` u, `users_unactivated` a + WHERE u.id = a.user_id + AND DATEDIFF(CURRENT_DATE(), u.registration_date) > $days_to_expire"); + if ($query->num_rows() > 0) + { + $str_user_ids = ''; + $results = $query->result(); + foreach ($results as $result) + $str_user_ids .= "{$result->id}, "; + $str_user_ids = substr($str_user_ids, 0, -2); + } + else + return FALSE; + + // Delete from `users` table. + $ret = $this->db->query("DELETE FROM `users` + WHERE id IN ($str_user_ids)"); + if (!$ret) + return FALSE; + + // Delete from `users_unactivated table. + $ret = $this->db->query("DELETE FROM `users_unactivated` + WHERE user_id IN ($str_user_ids)"); + if (!$ret) + return FALSE; + + // Success + return TRUE; } /**