PageRenderTime 50ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/source/application/controller/Home/LoginController.php

http://sharebooks.googlecode.com/
PHP | 411 lines | 299 code | 72 blank | 40 comment | 42 complexity | 8235c735318e623c94839e039f26d5d9 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. // Business class to include
  3. class Home_LoginController extends TvcController
  4. {
  5. const MENU_ITEM = Parameter::MENU_REGISTER;
  6. /*
  7. * Log in
  8. */
  9. function loginAction()
  10. {
  11. $smarty = TVC::load('smarty');
  12. $return_link = TVC_Request::get('return_link');
  13. $smarty->assign('register_link', APPLICATION_URL . '/login/register?affCd=AAAAA');
  14. $login_time = TVC_Request::getIntNumber('login_time');
  15. if(Parameter::$params['ALLOW_PUBLIC'])
  16. {
  17. setcookie("from_public_site", "1", time() + 3600*24*30, "/", DOMAIN);
  18. }
  19. // get id of requested user
  20. $rid = TVC_Request::getIntNumber('rid');
  21. // get salt value
  22. $salt_value = TVC_Request::get('sno');
  23. // get id of user
  24. $id = TVC_Request::getIntNumber('id');
  25. $error_code = null;
  26. $user = new User();
  27. // if 3 parameters exist, do OneClick Login action
  28. if ($rid && $salt_value && $id)
  29. {
  30. $user->get($id);
  31. if (User::validateLoginByOneClick($rid, $salt_value, $id))
  32. {
  33. //check user login without encoding password
  34. $error_code = User::performLogin($user->email, $user->password, User::APPLICATION_PUBLIC , false);
  35. }
  36. }
  37. else
  38. {
  39. if (TVC_Request::isParamSet("login"))
  40. {
  41. if(($login_time-1) >= Parameter::$params['NUMBER_OF_PASSWORD_ATTEMT'])
  42. {
  43. $error_captcha = Utils::validateCaptcha(TVC_Request::get('captcha'));
  44. if($error_captcha)
  45. {
  46. $error_code[] = $error_captcha;
  47. }
  48. }
  49. if(!$error_code)
  50. {
  51. //check user login.
  52. $error_code = User::performLogin($_POST["login"], $_POST["pwd"], User::APPLICATION_PUBLIC);
  53. }
  54. }
  55. }
  56. if (!TVC_Session::exist('user_serialize'))
  57. {
  58. $smarty->assign("login_time", $login_time+1);
  59. $smarty->assign("turn_on_captcha", ($login_time >= Parameter::$params['NUMBER_OF_PASSWORD_ATTEMT'])?1:0);
  60. //login fail
  61. $smarty->assign("return_link", $return_link);
  62. $smarty->assign("message_error", $error_code);
  63. $smarty->assign("login", (TVC_Request::isParamSet('login') ? TVC_Request::get('login') : ""));
  64. $smarty->display('home/login.html');
  65. }
  66. else
  67. {
  68. if (!$return_link)
  69. {
  70. TVC::redirect(APPLICATION_URL."/home/home");
  71. }
  72. else
  73. {
  74. TVC::redirect(APPLICATION_URL . $return_link);
  75. }
  76. }
  77. }
  78. /*
  79. * Log in
  80. */
  81. function loginDomainAction()
  82. {
  83. $available_lang = TVC_Config::get('I18N','AUTHORIZED_LANGUAGES');
  84. if (TVC_Request::isParamSet("SID"))
  85. {
  86. session_destroy();
  87. $SID = TVC_Request::getString('SID');
  88. session_id($SID);
  89. session_start();
  90. setcookie("from_other_site", "1", time() + 3600*24*30, "/", DOMAIN);
  91. // set language
  92. if((TVC_Request::isParamSet("lang"))
  93. && (in_array(TVC_Request::getString("lang"), $available_lang)))
  94. {
  95. $language = TVC_Request::getString("lang");
  96. }
  97. else
  98. {
  99. $language = TVC_Config::get('I18N','DEFAULT_LANGUAGE');
  100. }
  101. TVC::redirect(APPLICATION_URL."?language=".$language);
  102. }
  103. $reponse = array();
  104. if((TVC_Request::isParamSet("lang"))
  105. && (in_array(TVC_Request::getString("lang"), $available_lang)))
  106. {
  107. $language = TVC_Request::getString("lang");
  108. }
  109. else
  110. {
  111. $language = TVC_Config::get('I18N','DEFAULT_LANGUAGE');
  112. }
  113. Utils::setIniLang($language);
  114. if (TVC_Request::isParamSet("username"))
  115. {
  116. $error_code = User::performLogin(TVC_Request::getString('username'), TVC_Request::getString("password"), User::APPLICATION_PUBLIC);
  117. if (TVC_Session::exist('user_serialize'))
  118. {
  119. $reponse['status'] = 1;
  120. $reponse['result'] = APPLICATION_URL.'/login/loginDomain/SID/'.session_id().'?lang='.$language;
  121. }
  122. else
  123. {
  124. $reponse['status'] = 0;
  125. $error_label = array('_LB_USER_IS_NOT_ACTIVE' => _LB_USER_IS_NOT_ACTIVE,
  126. '_LB_NOT_GRANTED' => _LB_NOT_GRANTED,
  127. '_LB_INVALID_LOGIN_OR_PASSWORD' => _LB_INVALID_LOGIN_OR_PASSWORD
  128. );
  129. $reponse['result'] = $error_label[$error_code[0]];
  130. }
  131. }
  132. else
  133. {
  134. $reponse['status'] = 0;
  135. $reponse['result'] = _LB_NOT_GRANTED;
  136. }
  137. echo json_encode ($reponse);
  138. }
  139. /*
  140. * Logout
  141. */
  142. function logoutAction()
  143. {
  144. TVC_Session::remove();
  145. Utils::resetAccessAuthentical();
  146. TVC::redirect(URL_MASTER_BO."/login/logout");
  147. }
  148. public function logoutFromEDTAction()
  149. {
  150. TVC_Session::remove();
  151. Utils::resetAccessAuthentical();
  152. TVC::redirect(URL_MASTER_BO."/login/logoutFromPublic");
  153. }
  154. public function logoutFromADVAction()
  155. {
  156. TVC_Session::remove();
  157. Utils::resetAccessAuthentical();
  158. TVC::redirect(URL_EDITOR_BO."/login/logoutFromPublic");
  159. }
  160. /**
  161. * Register a new member
  162. */
  163. public function registerAction()
  164. {
  165. $smarty = TVC::load('smarty');
  166. $user = new User();
  167. $act = TVC_Request::getIntNumber('act');
  168. if($act)
  169. {
  170. if($user->validateInputRegister())
  171. {
  172. $user->password=sha1($user->password);
  173. $user->activation_code = sha1($user->password);
  174. $user->insert();
  175. //send activate email
  176. $user->sendActivateEmail($user->id, $user->first_name." ".$user->last_name,$user->email,TVC_Request::get('user_pass'),$user->activation_code,
  177. 'home/mails/subscribe-mail-'.TVC_MultiLanguage::getLanguage().'.tpl');
  178. TVC::redirect(APPLICATION_URL . '/login/notify');
  179. }
  180. }
  181. $language_code = TVC_Multilanguage::getLanguage();
  182. $sex = array(Parameter::MALE_SEX=>'_LB_MALE_SEX', Parameter::FEMALE_SEX=>'_LB_FEMALE_SEX', Parameter::NONE_SEX=>'_LB_NONE_SEX');
  183. $smarty->assign('backlink', APPLICATION_URL .'/login/login');
  184. $smarty->assign('sex', $sex);
  185. $smarty->assign('user', $user);
  186. $smarty->assign('lang', $language_code);
  187. $smarty->display('home/user_register.html');
  188. }
  189. public function activateAction()
  190. {
  191. $user = new User();
  192. $user->activateFromMail(TVC_Request::get('c'));
  193. TVC::redirect(APPLICATION_URL . '/login/login');
  194. }
  195. public function notifyAction()
  196. {
  197. $smarty = TVC::load('smarty');
  198. $smarty->display('home/registration_notice.html');
  199. }
  200. public function generateCaptchaAction()
  201. {
  202. return Utils::generateCAPTCHA();
  203. }
  204. public function forgotPasswordAction()
  205. {
  206. $smarty = TVC::load('smarty');
  207. $return_link = TVC_Request::get('return_link');
  208. $act = TVC_Request::get('act');
  209. $email = TVC_Request::get('email');
  210. $captcha = TVC_Request::get('captcha');
  211. $error_code = array();
  212. if($act)
  213. {
  214. //validate email
  215. if($email == "")
  216. {
  217. $error_code[] = '_LB_EMAIL_IS_EMPTY';
  218. }
  219. elseif(strlen($email) > Parameter::MAX_LENGTH_NAME || !Utils::checkEmailValid($email) || !User::checkUserExist($email, true))
  220. {
  221. $error_code[]='_LB_INVALID_EMAIL';
  222. }
  223. $error_captcha = Utils::validateCaptcha($captcha);
  224. if($error_captcha)
  225. {
  226. $error_code[] = $error_captcha;
  227. }
  228. if(count($error_code)==0)
  229. {
  230. $user = new User();
  231. $user->selectAdd();
  232. $user->selectAdd("user.id user_id, concat(first_name, ' ', last_name) name, email, password");
  233. $user->whereAdd("user.email='$email'");
  234. $user->find(true);
  235. //- Disable the user & update activation code
  236. $user->query("UPDATE user SET activation_code='".sha1($user->password)."' WHERE email='$email'");
  237. //- Send an Email with a private link (key)
  238. $user->sendActivateEmail($user->id_contact, $user->user_id, $user->name,$user->email,$user->password,$user->account_type,sha1($user->password),
  239. 'home/mails/forgot-password-mail-'.TVC_MultiLanguage::getLanguage().'.tpl', 1);
  240. $smarty->assign('sendMailSuccess', 1);
  241. }
  242. }
  243. $smarty->assign('return_link', $return_link);
  244. $smarty->assign('email', $email);
  245. $smarty->assign('capstr', $captcha);
  246. $smarty->assign("message_error", $error_code);
  247. $smarty->display('home/forgot_password.html');
  248. }
  249. /**
  250. * Check the user is exist
  251. * This function is called by AJAX
  252. *
  253. * @param String $email
  254. * @return boolean true is returned if the user is exist
  255. */
  256. public function checkEMailExist($email)
  257. {
  258. return User::checkEMailExist($email);
  259. }
  260. /**
  261. * Check the contact is exist
  262. * This function is called by AJAX
  263. *
  264. * @param String $name
  265. * @return boolean true is returned if the user is exist
  266. */
  267. public function checkNameExist($first_name, $last_name)
  268. {
  269. return User::checkNameExist($first_name, $last_name);
  270. }
  271. public function renewPasswordAction()
  272. {
  273. $smarty = TVC::load('smarty');
  274. $act=TVC_Request::get('act');
  275. $info_str=TVC_Request::get('c');
  276. $error_code = array();
  277. $user = new User();
  278. if($info_str)
  279. {
  280. $info = explode(",",base64_decode($info_str));
  281. $u_id = $info[1];
  282. $u_email = $info[2];
  283. $activation_code = $info[3];
  284. $user->id = $u_id;
  285. $user->id_contact = $cid;
  286. $user->email = trim($u_email);
  287. $user->activation_code = $activation_code;
  288. }
  289. if($act == 1)
  290. {
  291. $new_pwd = TVC_Request::get('new_pwd');
  292. $confirm_pwd = TVC_Request::get('confirm_pwd');
  293. //validate user password
  294. if($new_pwd == "")
  295. {
  296. $error_code[] = '_LB_PASSWORD_IS_EMPTY';
  297. }
  298. else if(strlen($new_pwd) > Parameter::MAX_LENGTH_PASSWORD)
  299. {
  300. $error_code[] = '_MSG_PASSWORD_IS_TOO_LONG';
  301. }
  302. else if(strlen($new_pwd) < Parameter::MIN_LENGTH_PASSWORD)
  303. {
  304. $error_code[] = '_MSG_PASSWORD_IS_TOO_SHORT';
  305. }
  306. else if($confirm_pwd != $new_pwd)
  307. {
  308. $error_code[] = '_LB_PASSWORD_DOESNT_MATCH';
  309. }
  310. if(count($error_code) == 0)
  311. {
  312. $user->password = sha1($new_pwd);
  313. $user->is_disabled = 0;
  314. $user->activation_code = '';
  315. $user->update();
  316. $smarty->assign('renewSuccess',1);
  317. }
  318. else
  319. {
  320. $smarty->assign('renewPassword',1);
  321. }
  322. $smarty->assign("message_error", $error_code);
  323. $smarty->display('home/renew_password.html');
  324. }
  325. else
  326. {
  327. if($user->find(true))
  328. {
  329. $smarty->assign("message_error", $error_code);
  330. $smarty->assign('renewPassword',1);
  331. $smarty->display('home/renew_password.html');
  332. }
  333. else
  334. {
  335. TVC::redirect(APPLICATION_URL . '/login/login');
  336. }
  337. }
  338. }
  339. }