PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/ser-0.9.6/serweb-0.9.4/application_layer/apu_login.php

#
PHP | 351 lines | 222 code | 51 blank | 78 comment | 26 complexity | 4b492f63697275fd829a5aab4d7b248d MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0
  1. <?php
  2. /**
  3. * Application unit login
  4. *
  5. * @author Karel Kozlik
  6. * @version $Id: apu_login.php,v 1.6.2.1 2005/06/13 11:53:31 kozlik Exp $
  7. * @package serweb
  8. */
  9. /** Application unit login
  10. *
  11. *
  12. * This application unit is used for login into application. This APU can't
  13. * be combined with others APUs on one page.
  14. *
  15. * Configuration:
  16. * --------------
  17. * 'check_admin_privilege' (bool) default: false
  18. * check if user has administrator privilege
  19. *
  20. * 'fully_qualified_name_on_login' (bool) default: $config->fully_qualified_name_on_login
  21. * trou if should be entered fully qualifide username (username@domain)
  22. *
  23. * 'check_supported_domain_on_login' (bool) default: false
  24. * Should be extra checked domain if is present in table domains
  25. * in case that 'fully_qualified_name_on_login' is true
  26. *
  27. * 'redirect_on_login' (string) default: 'my_account.php'
  28. * name of script ot which is browser redirected after succesfull login
  29. *
  30. * 'redirect_on_first_login' (string) default: null
  31. * if is set, user is redirected to this script after his first login to serweb
  32. *
  33. * 'cookie_domain' (string) default: null
  34. * The domain that the cookie in which is stored username is available
  35. *
  36. * 'msg_logout' default: $lang_str['msg_logout_s'] and $lang_str['msg_logout_l']
  37. * message which should be showed on user logout - assoc array with keys 'short' and 'long'
  38. *
  39. * 'form_name' (string) default: 'login_form'
  40. * name of html form
  41. *
  42. * 'form_submit' (assoc)
  43. * assotiative array describe submit element of form. For details see description
  44. * of method add_submit in class form_ext
  45. *
  46. * 'smarty_form' name of smarty variable - see below
  47. * 'smarty_action' name of smarty variable - see below
  48. *
  49. * Exported smarty variables:
  50. * --------------------------
  51. * opt['smarty_form'] (form)
  52. * phplib html form
  53. *
  54. * opt['smarty_action'] (action)
  55. * tells what should smarty display. Values:
  56. * 'default' -
  57. * 'was_logged_out' - when user was logged out
  58. *
  59. */
  60. class apu_login extends apu_base_class{
  61. var $smarty_action='default';
  62. var $user_uuid = null;
  63. var $username = null;
  64. var $domain = null;
  65. var $password = null;
  66. /* return required data layer methods - static class */
  67. function get_required_data_layer_methods(){
  68. return array('domain_exists', 'is_user_registered', 'get_privileges_of_user');
  69. }
  70. /* return array of strings - requred javascript files */
  71. function get_required_javascript(){
  72. return array('login_completion.js.php');
  73. }
  74. /* constructor */
  75. function apu_login(){
  76. global $lang_str, $sess_lang, $config;
  77. parent::apu_base_class();
  78. /* set default values to $this->opt */
  79. $this->opt['fully_qualified_name_on_login'] = $config->fully_qualified_name_on_login;
  80. $this->opt['check_supported_domain_on_login'] = false;
  81. $this->opt['redirect_on_login'] = 'my_account.php';
  82. $this->opt['redirect_on_first_login'] = null;
  83. $this->opt['check_admin_privilege'] = false;
  84. $this->opt['cookie_domain'] = null;
  85. $this->opt['xxl_redirect_after_login'] = false;
  86. /* message on attributes update */
  87. $this->opt['msg_logout']['short'] = &$lang_str['msg_logout_s'];
  88. $this->opt['msg_logout']['long'] = &$lang_str['msg_logout_l'];
  89. /*** names of variables assigned to smarty ***/
  90. /* form */
  91. $this->opt['smarty_form'] = 'form';
  92. /* smarty action */
  93. $this->opt['smarty_action'] = 'action';
  94. /* name of html form */
  95. $this->opt['form_name'] = 'login_form';
  96. $this->opt['form_submit']=array('type' => 'image',
  97. 'text' => $lang_str['b_login'],
  98. 'src' => get_path_to_buttons("btn_login.gif", $sess_lang));
  99. }
  100. function action_login(&$errors){
  101. global $data_auth, $lang_str, $sess, $config, $pre_uid;
  102. if ($sess->is_registered('auth')) $sess->unregister('auth');
  103. // set cookie only if not doing http redirect because
  104. // $_POST['remember_uname'] is not set during redirect
  105. if (!isset($_GET["redir_id"])){
  106. if (isset($_POST['remember_uname']) and $_POST['remember_uname'])
  107. setcookie('serwebuser', $_POST['uname'], time()+31536000, null, $this->opt['cookie_domain']); //cookie expires in one year
  108. else
  109. setcookie('serwebuser', '', time(), null, $this->opt['cookie_domain']); //delete cookie
  110. }
  111. if (isModuleLoaded('xxl') and $this->opt['xxl_redirect_after_login']){
  112. xxl_http_redirect(array("get_params"=>array(
  113. "uname" => $this->username,
  114. "domain" => $this->domain,
  115. "pass" => $this->password,
  116. "redir_id" => $this->opt['instance_id'])));
  117. }
  118. $sess->register('pre_uid');
  119. $pre_uid=$this->user_uuid;
  120. if ($this->opt['redirect_on_first_login']){
  121. //check if user exists in subscriber table
  122. if (($registered = $data_auth->is_user_registered(new Cserweb_auth($pre_uid, $this->username, $this->domain), $errors)) < 0) return false;
  123. if (!$registered){
  124. sw_log("User login: first login of this user - redirecting to page: ".$this->opt['redirect_on_first_login'], PEAR_LOG_DEBUG);
  125. $this->controler->change_url_for_reload($this->opt['redirect_on_first_login']);
  126. return true;
  127. }
  128. }
  129. sw_log("User login: redirecting to page: ".$this->opt['redirect_on_login'], PEAR_LOG_DEBUG);
  130. $this->controler->change_url_for_reload($this->opt['redirect_on_login']);
  131. return true;
  132. }
  133. /* this metod is called always at begining */
  134. function init(){
  135. parent::init();
  136. $this->controler->set_onload_js("
  137. if (document.forms['".$this->opt['form_name']."']['uname'].value != '') {
  138. document.forms['".$this->opt['form_name']."']['passw'].focus();
  139. } else {
  140. document.forms['".$this->opt['form_name']."']['uname'].focus();
  141. }
  142. ");
  143. }
  144. /* check _get and _post arrays and determine what we will do */
  145. function determine_action(){
  146. if ($this->was_form_submited() or
  147. (isset($_GET["redir_id"]) and $_GET["redir_id"] == $this->opt['instance_id'])){ // Is there data to process?
  148. $this->action=array('action'=>"login",
  149. 'validate_form'=>true,
  150. 'reload'=>true,
  151. 'alone'=>true);
  152. }
  153. else $this->action=array('action'=>"default",
  154. 'validate_form'=>false,
  155. 'reload'=>false);
  156. }
  157. /* create html form */
  158. function create_html_form(&$errors){
  159. global $lang_str;
  160. parent::create_html_form($errors);
  161. $cookie_uname="";
  162. if (isset($_COOKIE['serwebuser'])) $cookie_uname=$_COOKIE['serwebuser'];
  163. $this->f->add_element(array("type"=>"text",
  164. "name"=>"uname",
  165. "size"=>20,
  166. "maxlength"=>50,
  167. "value"=>$cookie_uname,
  168. "minlength"=>1,
  169. "length_e"=>$lang_str['fe_not_filled_username'],
  170. "extrahtml"=>"autocomplete='off' ".
  171. ($this->opt['fully_qualified_name_on_login'] ? " onBlur='login_completion(this)'" : "")));
  172. $this->f->add_element(array("type"=>"text",
  173. "name"=>"passw",
  174. "value"=>"",
  175. "size"=>20,
  176. "maxlength"=>25,
  177. "pass"=>1));
  178. $this->f->add_element(array("type"=>"checkbox",
  179. "name"=>"remember_uname",
  180. "value"=>"1",
  181. "checked"=>$cookie_uname?1:0));
  182. }
  183. function check_admin_privilege($user, &$errors){
  184. global $data_auth;
  185. //check for admin privilege
  186. if (false === $privileges = $data_auth->get_privileges_of_user(
  187. $user,
  188. array('change_privileges','is_admin'),
  189. $errors)
  190. ) return false;
  191. foreach($privileges as $row)
  192. if ($row->priv_name=='is_admin' and $row->priv_value) return true;
  193. return false;
  194. }
  195. /* validate html form */
  196. function validate_form(&$errors){
  197. global $config, $data, $data_auth, $lang_str;
  198. // don't display logout mesage in case that form was submited
  199. if (isset($_GET['logout'])) unset($_GET['logout']);
  200. if (isset($_GET["redir_id"]) and
  201. isModuleLoaded('xxl') and
  202. $this->opt['xxl_redirect_after_login']){
  203. $this->username = $_GET['uname'];
  204. $this->domain = $_GET['domain'];
  205. $this->password = $_GET['pass'];
  206. }
  207. else{
  208. if (false === parent::validate_form($errors)) return false;
  209. $this->password = $_POST['passw'];
  210. sw_log("User login: values from login form: username: ".
  211. $_POST['uname'].", password: ".$this->password, PEAR_LOG_DEBUG);
  212. //if fully quantified username is given
  213. if ($this->opt['fully_qualified_name_on_login']) {
  214. // parse username and domain from it
  215. if (ereg("^([^@]+)@(.+)", $_POST['uname'], $regs)){
  216. $this->username=$regs[1];
  217. $this->domain=$regs[2];
  218. }
  219. else {
  220. sw_log("User login: authentication failed: unsuported format of username. Can't parse username and domain part", PEAR_LOG_INFO);
  221. $errors[]=$lang_str['bad_username'];
  222. return false;
  223. }
  224. }
  225. else{
  226. $this->username=$_POST['uname'];
  227. $this->domain=$config->domain;
  228. }
  229. }
  230. sw_log("User login: checking password of user with username: ".
  231. $this->username.", domain: ".$this->domain, PEAR_LOG_DEBUG);
  232. $data_auth->set_xxl_user_id('sip:'.$this->username.'@'.$this->domain);
  233. $data_auth->expect_user_id_may_not_exists();
  234. if ($this->opt['check_supported_domain_on_login']){
  235. if (true !== $data_auth->domain_exists($this->domain, $errors)){
  236. sw_log("User login: authentication failed: domain '".$this->domain."'' is not supported. Please check table domain", PEAR_LOG_INFO);
  237. $errors[]=$lang_str['bad_username'];
  238. return false;
  239. }
  240. }
  241. if (false === $this->user_uuid = $data_auth->check_passw_of_user($this->username, $this->domain, $this->password, $errors)) {
  242. sw_log("User login: authentication failed: bad username or domain or password ", PEAR_LOG_INFO);
  243. $errors[]=$lang_str['bad_username'];
  244. return false;
  245. }
  246. if (is_null($this->user_uuid)){
  247. sw_log("User login: authentication failed: no user ID", PEAR_LOG_INFO);
  248. $errors[]=$lang_str['bad_username'];
  249. return false;
  250. }
  251. if ($this->opt['check_admin_privilege']){
  252. if (!$this->check_admin_privilege(
  253. new Cserweb_auth($this->user_uuid, $this->username, $this->domain),
  254. $errors)){
  255. $errors[]=$lang_str['bad_username'];
  256. sw_log("User login: authentication failed: user hasn't admin privileges", PEAR_LOG_INFO);
  257. return false;
  258. }
  259. }
  260. sw_log("User login: authentication succeeded, uuid: ".$this->user_uuid, PEAR_LOG_DEBUG);
  261. return true;
  262. }
  263. /* add messages to given array */
  264. function return_messages(&$msgs){
  265. global $_GET;
  266. if (isset($_GET['logout'])){
  267. $msgs[]=&$this->opt['msg_logout'];
  268. $this->smarty_action="was_logged_out";
  269. }
  270. }
  271. /* assign variables to smarty */
  272. function pass_values_to_html(){
  273. global $smarty;
  274. $smarty->assign_by_ref($this->opt['smarty_action'], $this->smarty_action);
  275. }
  276. /* return info need to assign html form to smarty */
  277. function pass_form_to_html(){
  278. return array('smarty_name' => $this->opt['smarty_form'],
  279. 'form_name' => $this->opt['form_name'],
  280. 'after' => '',
  281. 'before' => ($this->opt['fully_qualified_name_on_login'] ?
  282. 'login_completion(f.uname);':
  283. ''));
  284. }
  285. }
  286. ?>