<?vsp 
--  
--  $Id$
--  
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--  
--  Copyright (C) 1998-2018 OpenLink Software
--  
--  This project is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; only version 2 of the License, dated June 1991.
--  
--  This program is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details.
--  
--  You should have received a copy of the GNU General Public License along
--  with this program; if not, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--  
--  
 http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
   declare _user_name, _login_ok varchar;
  declare _sid varchar;
  declare _err long varchar;
  declare _passold, _passnew1, _passnew2, _to_do varchar;
  declare _password_ok, _passwords_match varchar;
  declare _err_msg varchar;

  _to_do := get_keyword('TO_DO', params);
  _sid := '';
  _sid := get_keyword('SESS', params);

   _login_ok := 'NO';

 if (_sid   <> '')
 {
  for select _username, _userpass from wapsession where sid = _sid  do
  {
   _login_ok := 'YES';
   _user_name := _username;
   update wapsession set _time_last_active = datestring ( now() ) where  sid = _sid;
  }
  }else{
    _login_ok := 'NO';
  }

-- 1 presents the users with the change password form to fill in
if (_login_ok = 'YES' and (is_empty_or_null(_to_do)))
{ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
<card id="update" title="OpenLink">
 <do type="options" label="Cancel">
  <go href="setting.vsp?SESS=<?=_sid?>"/>
 </do>
    <p>Old Password: <input name="passold" title="Password" value="" type="password" maxlength="30" /></p>
    <p>New Password :<input name="passnew1" title="New Password" value="" type="password" maxlength="30" /></p>
    <p>Re-enter New Password :<input name="passnew2" title="Re-enter Password" value="" type="password" maxlength="30" /></p>
    <p>Update Now?</p>


   <p><a href="password.vsp?SESS=<?=_sid?>&amp;TO_DO=UPDATE&amp;PASSOLD=$(passold:escape)&amp;PASSNEW1=$(passnew1:escape)&amp;PASSNEW2=$(passnew2:escape)">Yes</a></p>
   <p><a href="setting.vsp?SESS=<?=_sid?>">No</a></p>
</card>
</wml>
<?vsp
-- close 1
}

-- 2 when the SID is not valid
if(_login_ok = 'NO')
{  http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
  <card id="P1" title="Wrong" >
    <p>
   Wrong Login
    </p>
  </card>
</wml>
<?vsp
-- close 2
}

-- 3 when the user has filled in the change password form, we need to validate it
if(_login_ok = 'YES' and _to_do = 'UPDATE')
{
  _passold := get_keyword('PASSOLD', params);
     _passnew1 := get_keyword('PASSNEW1', params);
  _passnew2 := get_keyword('PASSNEW2', params);
     _err_msg := '';

  if (_passnew1 = _passnew2)
  {
   _passwords_match := 'YES';
  }else{
   _passwords_match := 'NO';
  _err_msg := 'Your passwords did not match';
  }

  if (exists (select 1 from mailpwd where _username = _user_name and _userpass = _passold))
  {
   _password_ok := 'YES';
 }else{
  _password_ok := 'NO';
  _err_msg := concat(_err_msg, '<br/>', 'Current password incorrect<br/>');
 }
}
-- close 3

-- 4 when everything is validated OK, we will update the user password
if(_passwords_match = 'YES' and _password_ok = 'YES')
{
 update mailpwd set _userpass = _passnew1 where _username = _user_name;
 update wapsession set _userpass = _passnew1 where sid = _sid and _username = _user_name;
 http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
  <card id="updated" title="Password Updated" >
  <onevent type="onenterforward">
   <refresh>
 <setvar name="passold" value=""/>
 <setvar name="passnew1" value=""/>
 <setvar name="passnew2" value=""/>
 </refresh>
  </onevent>
 <do type="accept" label="Mail Menu">
     <go href="checkmail.vsp?SESS=<?=_sid?>&amp;LIST=1&amp;FOLDER=Inbox">
    </go>
 </do>
    <p>   Password Updated</p>

  <p><a href="checkmail.vsp?SESS=<?=_sid?>&amp;LIST=1&amp;FOLDER=Inbox">Mail Menu</a></p>
  <p><a href="setting.vsp?SESS=<?=_sid?>">Setting</a></p>
  </card>
</wml>
<?vsp
}
-- close 4

-- 5 when there is something wrong with the user input
if(_passwords_match = 'NO' or _password_ok = 'NO')
{ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
  <card id="Error" title="Error Found" >
 <do type="accept" label="Mail Menu">
     <go href="checkmail.vsp?SESS=<?=_sid?>&amp;LIST=1&amp;FOLDER=Inbox">
    </go>
 </do>
    <p>
 Error, Cannot Proceed
 <?vsp http(sprintf('%s',_err_msg));?>     </p>

  <p><a href="checkmail.vsp?SESS=<?=_sid?>&amp;LIST=1&amp;FOLDER=Inbox">Mail Menu</a></p>
  <p><a href="password.vsp?SESS=<?=_sid?>">Back</a></p>
  </card>
</wml>
<?vsp
}
-- close 4

if(_login_ok = 'NO'){ http_rewrite(); http_header('Content-type: text/vnd.wap.wml\r\n');
?><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">


<wml>
  <card id="P1" title="Wrong" >
    <p mode="nowrap">
   Wrong Login
    </p>
  </card>
</wml>
<?vsp
}
?>


