<?php
if( $a == 'list' ){
	if( empty( $sDateFrom ) || ( !empty( $sDateFrom ) && !is_date( $sDateFrom ) ) ) $sDateFrom = date( 'Y-m-d', time() - 7 * 24 * 3600 );
	if( empty( $sDateTo ) || ( !empty( $sDateTo ) && !is_date( $sDateTo ) ) ) $sDateTo = date( 'Y-m-d' );
	$oTpl->assign( 'aLogInfo', Array( 'sDateFrom' => $sDateFrom, 'sDateTo' => $sDateTo ) );
	if( $_SESSION['iAdminId'] == -1 && isset( $_GET['sPhrase'] ) ){
		$iTimeStart = dateToTime( $sDateFrom, 'ymd' );
		$iTimeStop = dateToTime( $sDateTo . ' 23:59:59', 'ymd' );
		$sql = 'SELECT * FROM `' . $oDB->throwTableName( DB_ADMIN_ACTIONS ) . '` WHERE `iTime` BETWEEN ' . $iTimeStart . ' AND ' . $iTimeStop;
		if( !empty( $_GET['iAdmin'] ) ) $sql .= ' AND `iAdmin` = ' . intval( $_GET['iAdmin'] );

		if( isset( $_GET['sPhrase'] ) && !empty( $_GET['sPhrase'] ) ){
			preg_match_all( '/(-?)((\\"([^\\"]+)\\")|([^\\"\\s][^\\s]*))/', htmlspecialchars_decode( $_GET['sPhrase'] ), $aWords, PREG_SET_ORDER );
			$aSearchP = Array( );
			foreach( $aWords as $aWord ){
				$sWord = $aWord[4];
				if( empty( $sWord ) ) $sWord = $aWord[5];
				$sWord = $oDB->escape( $sWord );
				if( $aWord[1] == '-' ){
					$aSearchP[] = '( `sGet` NOT LIKE \'%' . $sWord . '%\' AND `sPost` NOT LIKE \'%' . $sWord . '%\' )';
				} else{
					$aSearchP[] = '( `sGet` LIKE \'%' . $sWord . '%\' OR `sPost` LIKE \'%' . $sWord . '%\' )';
				}
			}
			if( !empty( $aSearchP ) ){
				$sql.= ' AND (' . implode( ' AND ', $aSearchP ) . ')';
			}
		}
		$aAdminActions = $oDB->executeSQL( $sql, true );
		$oTpl->assign( 'aAdminActions', $aAdminActions );
	}
	if( isset( $sOption ) ) $content.= $oTpl->parse( 'message_done.tpl' );
	$content.= listAdmins( );
} elseif( $a == 'delete' && isset( $iAdmin ) && is_numeric( $iAdmin ) ){
	deleteAdmin( $iAdmin );
	header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $aActions['f'] . '-list&sOption=del' );
	exit;
} elseif( $a == 'form' ){
	if( isset( $_POST['sOption'] ) ){
		$iAdmin = saveAdmin( $_POST );
		if( isset( $_POST['sPass'] ) && isset( $_POST['iAdmin'] ) ){
			sendPasswordEmail( $_POST['sEmail'], $_POST['sLogin'], $_POST['sPass'], 1 );
		}
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $p . '&sOption=save&iAdmin=' . $iAdmin );
		exit;
	}
	if( isset( $iAdmin ) && is_numeric( $iAdmin ) ){
		$aAdmin = throwAdmin( $iAdmin );
	} else{
		$aAdmin = Array( 'iAdmin' => null );
	}
	if( isset( $sOption ) ) $content.= $oTpl->parse( 'message_done.tpl' );
	$aPrivilagesAdmin = throwAdminsPrivilages( $aAdmin['iAdmin'] );
	$oTpl->assign( 'aPrivilagesAdmin', $aPrivilagesAdmin );
	$oTpl->assign( 'aPrivilages', $aPrivilages );
	$oTpl->assign( 'aAdmin', $aAdmin );
	$content.= $oTpl->parse( 'admins_form.tpl' );
} elseif( $a == 'self' ){
	if( isset( $_POST['sOption'] ) ){
		$iAdmin = saveAdminSelf( $_POST );
		if( isset( $_POST['sPass'] ) ){
			sendPasswordEmail( $_POST['sEmail'], $_POST['sLogin'], $_POST['sPass'], 1 );
		}
		header( 'Location: ' . $_SERVER['PHP_SELF'] . '?p=' . $p . '&sOption=save&iAdmin=' . $iAdmin );
		exit;
	}
	if( isset( $_SESSION['iAdminId'] ) && $_SESSION['iAdminId'] > 0 ){
		$aAdmin = throwAdmin( $_SESSION['iAdminId'] );
		if( isset( $sOption ) ) $content.= $oTpl->parse( 'message_done.tpl' );
		$oTpl->assign( 'aAdmin', $aAdmin );
		$oTpl->assign( 'self', true );
		$content.= $oTpl->parse( 'admins_form.tpl' );
	}
}
?>