Forum Moderators: coopster
db.php
<?php
class db {
private static $_instance =null;
private $_pdo,
$_query,
$_error= false,
$_results,
$_count = 0;
private function __construct() {
try{
$this->_pdo = new PDO('mysql:host=' . Config::get('mysql/host') . ';dbname=' .Config::get('mysql/db') ,Config::get('mysql/username'), Config::get('mysql/password'));
} catch(PDOException $e){
die($e->getMessage());
}
}
public static function getInstance(){
if(!isset (self::$_instance)){
self::$_instance =new db();
}
return self::$_instance;
}
public function query($sql, $params =array()){
$this->_error = false;
if ($this->_query = $this->_pdo->prepare($sql)){
$x=1;
if(count($params)){
foreach ($params as $param) {
$this->_query->bindValue($x, $param);
$x++;
}
}
if($this->_query->execute()){
$this->_results = $this->_query->fetchAll(PDO::FETCH_OBJ);
$this->_count = $this ->_query -> rowCount();
} else {
$this->_error = true;
}
return $this ;
}
}
public function action($action, $table, $where = array()){
if (count($where === 3)){
$operators =array('=','>','<','>=','<=');
$field = $where [0];
$operator = $where [1];
$value = $where [2];
if (in_array($operator ,$operators)){
$sql = "{$action} FROM {$table} WHERE {$field} {$operator} ?";
if(!$this->query($sql,array($value))->error()){
return $this;
}
}
}
return false;
}
public function get($table,$where){
return $this ->action('SELECT *',$table , $where);
}
public function delete($table,$where){
return $this ->action('DELETE *',$table , $where);
}
public function insert_reg($table, $fields = array() ){
if (count ($fields)){
$keys = array_keys($fields);
$values = '';
$x =1;
foreach ($fields as $field) {
$values .= '?' ;
if ($x < count($fields)){
$values .= ' , ' ;
}
$x++;
}
$sql = "INSERT INTO register (`".implode('`, `',$keys)."`) VALUES ({$values} )";
if(!$this->query($sql, $fields)->error()){
return true;
}
}
return false;
}
public function insert($table, $fields = array()){
if (count ($fields)){
$keys = array_keys($fields);
$values = '';
$x =1;
foreach ($fields as $field) {
$values .= '?' ;
if ($x < count($fields)){
$values .= ' , ' ;
}
$x++;
}
$sql = "INSERT INTO users (`".implode('`, `',$keys)."`) VALUES ({$values})";
if (!$this ->query($sql,$fields)->error()){
return true;
}
}
return false;
}
public function update($table ,$id ,$fields){
$set= '';
$x= 1;
foreach ($fields as $name => $value) {
$set .= "{$name} = ?";
if ($x < count($fields)){
$set .=' , ';
}
$x++;
}
$sql = "UPDATE {$table} SET {$set} WHERE id = {$id} ";
if (!$this ->query($sql,$fields)->error()){
return true;
}
return false;
}
public function update_reg($table ,$id ,$fields){
$set= '';
$x= 1;
foreach ($fields as $name => $value) {
$set .= "{$name} = ?";
if ($x < count($fields)){
$set .=' , ';
}
$x++;
}
$sql = " UPDATE {$table} SET {$set} WHERE user_id = {$id} ";
if (!$this ->query($sql,$fields)->error()){
return true;
}
return false;
}
public function results(){
return $this ->_results ;
}
public function first(){
return $this->results()[0];
}
public function error(){
return $this->_error;
}
public function count (){
return $this ->_count ;
}
}
<?php
class User {
private $_db,
$_data,
$_sessionName,
$_LoggedIn;
public function __construct($user = null){
$this->_db = db::getInstance();
$this ->_sessionName = config::get('session/session_name');
if(!$user){
if(Session::exists($this->_sessionName)){
$user =Session::get($this->_sessionName);
if ($this->find($user)){
$this->_LoggedIn = true;
} else {
$this ->find($user);
}
}
}
}
public function update($fields = array(), $id=null ){
if (!$id && $this->isLoggedIn()){
$id = $this->data()->id;
}
if(!$this->_db->update('users', $id, $fields)){
throw new Exception('There was a problem updating');
}
}
public function create($fields = array()){
if(!$this->_db->insert('users', $fields)){
throw new Exception('There was a problem creating this account.');
}
}
public function register($fields = array()){
if(!$this->_db->insert_reg('register', $fields)){
throw new Exception('There was a problem creating this account.');
}
}
public function update_reg($fields = array(), $id=null ){
if (!$id && $this->isLoggedIn()){
$id = $this->data()->id;
}
if(!$this->_db->update_reg('register', $id, $fields)){
throw new Exception('There was a problem updating');
}
}
public function checkUpload(){
//check for an uploaded file:
if (isset($_FILES['upload'])){
//validate the type of file
$allowed = array('image/pjpeg', 'image/jpeg', 'image/gif', 'image/pdf', 'image/png');
if (in_array($_FILES['upload']['type'], $allowed)){
print "uploading file";
if(move_uploaded_file($_FILES['upload']['tmp_name'], "images/{$_FILES['upload']['name']} ")){
echo "<p><em> The file has been uploaded! </em></p>";
$image = "{$_FILES['upload']['name']}";
print "$image";
}//end of move...if
}else {
echo "<p>invalid file type! Please upload a JPEG GIF OR PDF FILE</p>";
if($_FILES['upload']['error'] > 0){
echo '<p>The File could not be uploaded because: <strong>';
switch ($_FILES['upload']['error']) {
case 1:
print 'The file exceeds the upload_max_filesize setting in php.ini.';
break;
case 2:
print 'The file exceeds the MAX_FILE_SIZE setting size in the HTML form.';
break;
default:
print 'A system error occured.';
break;
}
}
print '</strong></p>';
}
if (file_exists($_FILES['upload']['tmp_name']) && is_file($_FILES['upload']['tmp_name'])){
print "file exists";
unlink($_FILES['upload']['tmp_name']);
}
return $image;
}
}
public function find($user = null){
if($user){
$field = (is_numeric($user))? 'id' : 'username';
$data = $this->_db->get('users', array($field,'=',$user));
if($data->count()) {
$this->_data = $data->first();
return true;
}
}
}
public function login($username = null, $password = null){
$user=$this->find($username);
if($user){
if($this->data()->password === Hash::make(Input::get('password'), $this->data()->salt)){
Session::put($this->_sessionName, $this->data()->id);
}
return true;
}
// echo $this->data()->password ;
// echo '<br>';
// echo Hash::make($password, $this->data()->salt);
return false;
}
public function hasPermission($key){
$group = $this->_db ->get('groups', array('id','=', $this->data()->group));
if($group->count()){
$permissions = json_decode($group ->first() ->permissions, true);
if($permissions[$key] == true){
return true;
}
}
return false;
}
public function exists() {
return (!empty($this->_data))? true : false;
}
public function logout(){
Session::delete($this->_sessionName);
Redirect::to('login.php');
}
public function data(){
return $this->_data;
}
public function isLoggedIn(){
return $this->_LoggedIn;
}
}
<?php
require_once '../core/init.php';
$user = new User();
$matchid = (escape($user->data()->id));
/*echo Session::get(config::get('session/session_'))
echo $user->data()->username;
die();*/
if($user->isLoggedIn()){
if(Session::exists('home')){
echo '<p>' .Session::flash('home'). '</p>';
}
?>
<!--Hello, This part of the code is to save the data in the database
and stay on the same page -->
<?php
if (isset($_POST['continue'])){
if(Token::check(Input::get('token'))){
$Validate = new Validate();
$Validation = $Validate-> check($_POST , array(
'fullname' => array(
'required' => true,
'min' => 2,
'max' => 100,
),
'birth' => array(
'required' => true,
'min' => 6,
),
'nationality' => array(
'required' => true,
)
));
if ($Validation -> passed()){
try{
$user ->update_reg(array(
'user_id'=> $matchid,
'fullname'=>Input::get('fullname'),
'gender'=>Input::get('gender'),
'birth'=>Input::get('birth'),
'nationality'=>Input::get('nationality'),
'email'=>Input::get('email'),
'city'=>Input::get('city'),
'region'=>Input::get('region'),
'address'=>Input::get('address'),
'maritalstatus'=>Input::get('maritalstatus'),
'telephone'=>Input::get('telephone'),
'country'=>Input::get('country'),
'language'=>Input::get('language'),
'fathername'=>Input::get('fathername'),
'ftelephone'=>Input::get('ftelephone'),
'coursename'=>Input::get('coursename'),
'subject1'=>Input::get('subject1'),
'subject2'=>Input::get('subject2'),
'subject3'=>Input::get('subject3'),
'subject4'=>Input::get('subject4'),
'subject5'=>Input::get('subject5'),
'subject6'=>Input::get('subject6'),
'grade1'=>Input::get('grade1'),
'grade2'=>Input::get('grade2'),
'grade3'=>Input::get('grade3'),
'grade4'=>Input::get('grade4'),
'grade5'=>Input::get('grade5'),
'grade6'=>Input::get('grade6'),
'index1'=>Input::get('index1'),
'index2'=>Input::get('index2'),
'index3'=>Input::get('index3'),
'index4'=>Input::get('index4'),
'index5'=>Input::get('index5'),
'index6'=>Input::get('index6'),
'totalprice'=>Input::get('totalprice'),
'institution1'=>Input::get('institution1'),
'from1'=>Input::get('from1'),
'to1'=>Input::get('to1'),
'job1'=>Input::get('job1'),
'traininginstitution'=>Input::get('traininginstitution'),
'progtype'=>Input::get('progtype'),
'progname'=>Input::get('progname'),
'disability'=>Input::get('disability'),
'declaration'=>Input::get('declaration')
));
Session::flash('update','Form been updated Successfully');
Redirect::to('view.php');
} catch(Execption $e){
die($e ->getMessage());
}
}else{
foreach ($Validation -> errors() as $error){
echo $error, '<br>';
}
}
}
}
?>
<!--This is the end of the update Code -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<link rel="stylesheet" href="../css/pure-min.css">
<style>
body {background:#f1f1f1; width:870px; margin:0 auto; padding-left:50px; padding-top:20px;}
.form fieldset{border:0px; padding:0px; margin:0px;}
form label {
float: left;
width: 150px;
margin-bottom: 5px;
margin-top: 5px;
}
form legend {
}
.clear {
display: block;
clear: both;
width: 100%;
}
</style>
</head>
<body>
<form class="pure-form" id="form1" name="form1" method="POST" >
<p>
<legend><strong class="legend">Demographic Information</strong></legend>
<fieldset>
<label for="fullname">Fullname</label>
<input name="fullname" type="text" id="fullname" maxlength="100" value="<?php if(isset($_POST['fullname'])){echo $_POST['fullname'];}?>" size="50" onChange="javascript:this.value=this.value.toUpperCase();" />
Enter Your Name as per your result slip<br class="clear" />
<label for="gender">Gender</label>
<input type="radio" name="gender" checked="checked" value="Male" id="gender_0" />
Male
<input type="radio" name="gender" value="Female" id="gender_1" />
Female
<br class="clear" />
<br class="clear" />
<label for="birth">Birth Date</label>
<input type="text" name="birth" id="birth" maxlength="25" value="<?php if(isset($_POST['birth'])){echo $_POST['birth'];}?>" />
As per Birth Certificate
<br class="clear" />
<label for="nationality">Nationality</label>
<input type="text" name="nationality" id="nationality" maxlength="50" value="<?php if(isset($_POST['nationality'])){echo $_POST['nationality'];}?>" onChange="javascript:this.value=this.value.toUpperCase();" />
Enter Your Nationality
<br class="clear" />
<label for="email">Email</label>
<input type="text" name="email" id="email" maxlength="100" value="<?php if(isset($_POST['email'])){echo $_POST['email'];}?>" />
Enter your Email (Eg: example@yahoo.com)
<br class="clear" />
<label for="marital">Marital Status</label>
<select name="marital" id="select">
<option value="<?php if(isset($_POST['marital'])){echo $_POST['marital'];}?>"><?php if(isset($_POST['marital'])){echo $_POST['marital'];}?> </option>
<option value="Single">Single</option>
<option value="Married">Married</option>
<option value="Divored">Divorced</option>
</select> Select Your Martial Status
<br class="clear" />
<label for="address">Address</label>
<textarea name="address" id="address" cols="45" rows="5" maxlength="100" value="<?php if(isset($_POST['address'])){echo $_POST['address'];}?>" onChange="javascript:this.value=this.value.toUpperCase();" ></textarea>
Please Enter Your Postal Address
<br class="clear" />
<label for="city">City</label>
<input type="text" name="city" id="city" maxlength="25" value="<?php if(isset($_POST['city'])){echo $_POST['city'];}?>" onChange="javascript:this.value=this.value.toUpperCase();" /> Enter Your City
<br class="clear" />
......
<input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
<input type="submit" name="cancel" value="Cancel" onclick="javascript: form.action='view.php';" >
<input type="submit" name="continue" id="submit" value="Update and Continue" />