posaune
[timetracker.git] / password_reset.php
1 <?php
2 // +----------------------------------------------------------------------+
3 // | Anuko Time Tracker
4 // +----------------------------------------------------------------------+
5 // | Copyright (c) Anuko International Ltd. (https://www.anuko.com)
6 // +----------------------------------------------------------------------+
7 // | LIBERAL FREEWARE LICENSE: This source code document may be used
8 // | by anyone for any purpose, and freely redistributed alone or in
9 // | combination with other software, provided that the license is obeyed.
10 // |
11 // | There are only two ways to violate the license:
12 // |
13 // | 1. To redistribute this code in source form, with the copyright
14 // |    notice or license removed or altered. (Distributing in compiled
15 // |    forms without embedded copyright notices is permitted).
16 // |
17 // | 2. To redistribute modified versions of this code in *any* form
18 // |    that bears insufficient indications that the modifications are
19 // |    not the work of the original author(s).
20 // |
21 // | This license applies to this document only, not any other software
22 // | that it may be combined with.
23 // |
24 // +----------------------------------------------------------------------+
25 // | Contributors:
26 // | https://www.anuko.com/time_tracker/credits.htm
27 // +----------------------------------------------------------------------+
28
29 require_once('initialize.php');
30 import('form.Form');
31 import('ttUser');
32 import('ttUserHelper');
33
34 if ($auth->isPasswordExternal()) {
35   header('Location: login.php');
36   exit();
37 }
38
39 $cl_login = $request->getParameter('login');
40
41 $form = new Form('resetPasswordForm');
42 $form->addInput(array('type'=>'text','maxlength'=>'100','name'=>'login','style'=>'width: 300px;','value'=>$cl_login));
43 $form->addInput(array('type'=>'submit','name'=>'btn_submit','value'=>$i18n->get('button.reset_password')));
44
45 if ($request->isPost()) {
46   // Validate user input.
47   if (!ttValidString($cl_login)) $err->add($i18n->get('error.field'), $i18n->get('label.login'));
48
49   if ($err->no()) {
50     if (!ttUserHelper::getUserByLogin($cl_login)) {
51       // User with a specified login was not found.
52       // In this case, if login looks like email, try finding user by email.
53       if (ttValidEmail($cl_login)) {
54         $login = ttUserHelper::getUserByEmail($cl_login);
55         if ($login)
56           $cl_login = $login;
57         else
58           $err->add($i18n->get('error.no_login'));
59       } else
60         $err->add($i18n->get('error.no_login'));
61     }
62   }
63
64   if ($err->no()) {
65     $user = new ttUser($cl_login); // Note: reusing $user from initialize.php here.
66
67     // Prepare and save a temporary reference for user.
68     $temp_ref = md5(uniqid());
69     ttUserHelper::saveTmpRef($temp_ref, $user->id);
70
71     $user_i18n = null;
72     if ($user->lang != $i18n->lang) {
73       $user_i18n = new I18n();
74       $user_i18n->load($user->lang);
75     } else
76       $user_i18n = &$i18n;
77
78     // Where do we email to?
79     $receiver = null;
80     if ($user->email)
81       $receiver = $user->email;
82     else {
83       if (ttValidEmail($cl_login))
84         $receiver = $cl_login;
85       else
86         $err->add($i18n->get('error.no_email'));
87     }
88
89     if ($receiver) {
90       import('mail.Mailer');
91       $sender = new Mailer();
92       $sender->setCharSet(CHARSET);
93       $sender->setSender(SENDER);
94       $sender->setReceiver("$receiver");
95       if ((!empty($_SERVER['HTTPS']) && ($_SERVER['HTTPS'] !== 'off')) || ($_SERVER['SERVER_PORT'] == 443))
96         $secure_connection = true;
97       if($secure_connection)
98         $http = 'https';
99       else
100         $http = 'http';
101
102       $cl_subject = $user_i18n->get('form.reset_password.email_subject');
103       if (APP_NAME)
104         $pass_edit_url = $http.'://'.$_SERVER['HTTP_HOST'].'/'.APP_NAME.'/password_change.php?ref='.$temp_ref;
105       else
106         $pass_edit_url = $http.'://'.$_SERVER['HTTP_HOST'].'/password_change.php?ref='.$temp_ref;
107
108       $sender->setMailMode(MAIL_MODE);
109       $res = $sender->send($cl_subject, sprintf($user_i18n->get('form.reset_password.email_body'), $_SERVER['REMOTE_ADDR'], $pass_edit_url));
110       $smarty->assign('result_message', $res ? $i18n->get('form.reset_password.message') : $i18n->get('error.mail_send'));
111     }
112   }
113 } // isPost
114
115 $smarty->assign('forms', array($form->getName()=>$form->toArray()));
116 $smarty->assign('onload', 'onLoad="document.resetPasswordForm.login.focus()"');
117 $smarty->assign('title', $i18n->get('title.reset_password'));
118 $smarty->assign('content_page_name', 'password_reset.tpl');
119 $smarty->display('index.tpl');