<?php
/*
 *
 * sflog!
 * Copyright (C) 2005 Alberto Sfolcini <a.sfolcini@gmail.com>
 *
 * This file is a part of sflog!.
 *
 * sflog! 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; either version 2
 * of the License, or (at your option) any later version.
 *
 * sflog! 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 Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 */
  
  error_reporting(E_USER_ERROR);
  
  require_once(dirname(__FILE__)."/../../cms/Auth.class.php");
  require_once(dirname(__FILE__)."/../../common/WriteFile.class.php");
  
  
  // Check for authorizations
  $auth = new Auth();
  if (!$auth->isAuth()){
  	header("Location: login.php");
  	die();
  }
  // ------ end auth's check

  $pfile  = dirname(__FILE__).'/../../admin/passwd';
  if (file_exists($pfile))
  	$truePasswd	= trim(file_get_contents($pfile));
  else
  	$truePasswd = "";
  
  $cPasswd		= "";	
  $newPasswd1	= "";
  $newPasswd2	= "";
  	  
  if (isset($_POST['cPasswd'])){
  	if (!($_POST['cPasswd']==''))
	  	$cPasswd 	  = stripslashes($_POST['cPasswd']);
	if (isset($_POST['newPasswd1'])){
  		if (!($_POST['newPasswd1']==''))
	  		$newPasswd1 	  = $_POST['newPasswd1'];
	}
	if (isset($_POST['newPasswd2'])){
  		if (!($_POST['newPasswd2']==''))
	  		$newPasswd2 	  = $_POST['newPasswd2'];
	}
		
	if ($auth->checkPasswd($cPasswd)){
		// check if pass 1 and 2 match
		if (strcmp(trim($newPasswd1),trim($newPasswd2))==0){
			$file = new WriteFile($pfile,REWRITE);
			$file->writeStr(crypt($newPasswd1));
			$file->closeFile();
			print('Password has been changed.');
			die();
		}else{
			print('<font color="red">Passwords do not match!</font>');
		}
	}else{
		print('<font color="red">The current password is incorrect!</font>');
	}
	
	
  }   
?>	
<FORM action="<?=$_SERVER['PHP_SELF']?>" method="POST">
	<table cellspacing="0" cellpaddgin="0" border="0">
		<tr>
			<td><b>Current password</b></td>
			<td><input type="PASSWORD" name="cPasswd" size="16"></td>
		</tr>
		<tr>
			<td><b>New password</b>&nbsp;</td>
			<td><input type="PASSWORD" name="newPasswd1" size="16"></td>
		</tr>
		<tr>
			<td><b>Confirm new password</b>&nbsp;</td>
			<td><input type="PASSWORD" name="newPasswd2" size="16"></td>
		</tr>
		<tr>
			<td>&nbsp;</td>
			<td>
				<input type="SUBMIT" value="Update" size="1">
			</td>
		</tr>
	</table>
</FORM>