<?php
require '../core/connection.php';

if(isset($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])== 'xmlhttprequest'){

if (isset($_POST['fname'])) {
	$name = $_POST['fname'];
	if (empty($name)) {
		echo "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please input a name</b></p></div>";

	}else{

		$fetch = $conn->query("SELECT * FROM faculty WHERE name = '$name'");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		echo "<div style='color:#e6102e'><i class='fa fa-ban fa-5x'></i><p><b>You have added this faculty once </b></p></div>";
		}else{
		$in = array('name' => $name, );
		create('faculty',$in);
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>You have successfully added $name</b></p></div>";

			
		}
		
	}
}

if (isset($_POST['dname'])) {
	$name = $_POST['dname'];
	$faculty = $_POST['faculty'];
	if (empty($name) || empty($faculty)) {
		echo "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please fill all empty fields</b></p></div>";

	}else{

		$fetch = $conn->query("SELECT * FROM department WHERE name = '$name'");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		echo "<div style='color:#e6102e'><i class='fa fa-ban fa-5x'></i><p><b>You have added this dapartment once </b></p></div>";
		}else{
		$in = array('name' => $name,
					'under' => $faculty, 

					);
		create('department',$in);
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>You have successfully added $name</b></p></div>";

			
		}
		
	}
}


if (isset($_POST['cname'])) {
	$name = $_POST['cname'];
	$department = $_POST['department'];
	if (empty($name) || empty($department)) {
		echo "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please fill all empty fields</b></p></div>";

	}else{

		$fetch = $conn->query("SELECT * FROM course WHERE name = '$name'");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		echo "<div style='color:#e6102e'><i class='fa fa-ban fa-5x'></i><p><b>You have added this dapartment once </b></p></div>";
		}else{
		$in = array('name' => $name,
					'under' => $department, 

					);
		create('course',$in);
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>You have successfully added $name</b></p></div>";

			
		}
		
	}
}


if (isset($_POST['lecturer'])) {
	$lecturer = $_POST['lecturer'];
	$olecturer = $_POST['olecturer'];
	$department = $_POST['department'];
	$password = sha1(md5("123456"));
	$cutlen = substr($lecturer, 0,1);
	
	$cutlen = "$olecturer.$cutlen";
	if (empty($lecturer) || empty($olecturer) || empty($department)) {
		echo "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please fill all empty fields</b></p></div>";

	}else{

		$fetch = $conn->query("SELECT * FROM lecturers WHERE sname = '$lecturer' AND oname = '$olecturer'");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();

		$fetch2 = $conn->query("SELECT * FROM users WHERE username = '$cutlen'");
		$result2 = $fetch2->fetchAll(PDO::FETCH_OBJ);
		$count2 = $fetch2->rowCount();

		if ($count > 0) {
		echo "<div style='color:#e6102e'><i class='fa fa-ban fa-5x'></i><p><b>You have added this person once </b></p></div>";
		}elseif ($count2 > 0) {
		echo "<div style='color:#e6102e'><i class='fa fa-ban fa-5x'></i><p><b>You have used this username </b></p></div>";
		}else{
		$in = array('sname' => $lecturer,
					'oname' => $olecturer, 
					'dept' => $department, 

					);
		
		create('lecturers',$in);
		$fetch = $conn->query("SELECT * FROM lecturers WHERE sname = '$lecturer' AND oname = '$olecturer'");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		foreach ($result as $key) {
			$ident = $key->id;
		}
		$in2 = array('iden' => $ident,
					'username' => $cutlen,
					'password' => $password, 
					'priviledge' => 'lecturer', 
					);
		create('users',$in2);
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>You have successfully added $lecturer $olecturer</b></p></div>";

			
		}
		
	}
}


if (isset($_POST['student'])) {
	$student = $_POST['student'];
	$ostudent = $_POST['ostudent'];
	$department = $_POST['department'];
	$matric = $_POST['matric'];
	$password = sha1(md5("123456"));
	$cutlen = substr($ostudent, 0,1);
	
	$cutlen = "$ostudent.$cutlen";
	if (empty($student) || empty($ostudent) || empty($department) || empty($matric)) {
		echo "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please fill all empty fields</b></p></div>";

	}else{

		$fetch = $conn->query("SELECT * FROM students WHERE sname = '$student' AND oname = '$ostudent'");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		echo "<div style='color:#e6102e'><i class='fa fa-ban fa-5x'></i><p><b>You have added this dapartment once </b></p></div>";
		}else{
		$in = array('sname' => $student,
					'oname' => $ostudent, 
					'matric' => $matric, 
					'dept' => $department, 

					);
		create('students',$in);
		$fetch = $conn->query("SELECT * FROM students WHERE sname = '$student' AND oname = '$ostudent'");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		foreach ($result as $key) {
			$ident = $key->id;
		}
		$in2 = array('iden' => $ident,
					'username' => $matric,
					'password' => $password, 
					'priviledge' => 'student', 
					);
		create('users',$in2);
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>You have successfully added $student $ostudent</b></p></div>";

			
		}
		
	}
}

if (isset($_POST['staffname'])) {
	$name = $_POST['staffname'];
	$password = sha1(md5('123456'));
	if (empty($name)) {
		echo "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please enter a name</b></p></div>";

	}else{

		$fetch = $conn->query("SELECT * FROM users WHERE username = '$name'");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		echo "<div style='color:#e6102e'><i class='fa fa-ban fa-5x'></i><p><b>Name Already exist, please change</b></p></div>";
		}else{
		$in = array('username' => $name,
					'password' => $password, 
					'priviledge' => 'staff', 
					);
		create('users',$in);
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>You have successfully added $name</b></p></div>";

			
		}
		
	}
}

if(isset($_POST['action']) && $_POST['action'] == 'delfaculty'){
		$id = $_POST['id'];
	
		$conn->query("DELETE FROM faculty WHERE id = $id");
		$conn->query("DELETE FROM department WHERE under = $id");
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Deleted</b></p></div>";		
	}

if(isset($_POST['action']) && $_POST['action'] == 'delstaff'){
		$id = $_POST['id'];
	
		$conn->query("DELETE FROM users WHERE id = $id");
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Deleted</b></p></div>";		
	}


if(isset($_POST['action']) && $_POST['action'] == 'deactivate'){
		$id = $_POST['id'];

		$in = array('priviledge' => 'deactivated', );

		update('users',$in,'id',$id);

		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Deactivated</b></p></div>";		
	}

if(isset($_POST['action']) && $_POST['action'] == 'activate'){
		$id = $_POST['id'];

		$in = array('priviledge' => 'staff', );

		update('users',$in,'id',$id);

		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Activated</b></p></div>";		
	}

if(isset($_POST['action']) && $_POST['action'] == 'deldepartment'){
		$id = $_POST['id'];
	
		$conn->query("DELETE FROM department WHERE id = $id");
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Deleted</b></p></div>";		
	}

if(isset($_POST['action']) && $_POST['action'] == 'refreshlect'){
		$id = $_POST['id'];
		$password = sha1(md5('123456'));

		$fetch = $conn->query("SELECT * FROM lecturers WHERE id = $id");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		foreach ($result as $key) {
		$sname = $key->sname;
		$oname = $key->oname;
		$cutlen = substr($sname, 0,1);
			}
		}
	
		$cutlen = "$oname.$cutlen";
	
		$in = array('password' => $password, );

		update('users',$in,'username',$cutlen);

		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Password Reset Done</b></p></div>";			
	}

	if(isset($_POST['uppassword'])){
		$id = $_POST['iden'];
		$password = $_POST['uppassword'];
		$password = sha1(md5($password));
	
		$in = array('password' => $password, );

		update('users',$in,'id',$id);

		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Password Reset Done</b></p></div>";			
	}

if(isset($_POST['action']) && $_POST['action'] == 'refreshstaff'){
		$id = $_POST['id'];
		$password = sha1(md5('123456'));

	
		$in = array('password' => $password, );

		update('users',$in,'id',$id);

		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Password Reset Done</b></p></div>";			
	}
if(isset($_POST['action']) && $_POST['action'] == 'refreshstu'){
		$id = $_POST['id'];
		$password = sha1(md5('123456'));
		$fetch = $conn->query("SELECT * FROM students WHERE id = $id");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		foreach ($result as $key) {
		$matric = $key->matric;
			}
		}
	
		$in = array('password' => $password, );

		update('users',$in,'matric',$matric);

		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Password Reset Done</b></p></div>";			
	}

if(isset($_POST['action']) && $_POST['action'] == 'delcourse'){
		$id = $_POST['id'];
	
		$conn->query("DELETE FROM course WHERE id = $id");
		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Deleted</b></p></div>";		
	}

if(isset($_POST['action']) && $_POST['action'] == 'delbooks'){
		$id = $_POST['id'];
	
		$fetch = $conn->query("SELECT * FROM books WHERE id = $id");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		foreach ($result as $key) {
		$frontpage = $key->frontpage;
		$file = $key->file;
		}
			unlink("../books/image/$frontpage");
			unlink("../books/file/$file");
		 	$conn->query("DELETE FROM books WHERE id = $id");
		 	echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Deleted</b></p></div>";
		 }		
	}


if(isset($_POST['action']) && $_POST['action'] == 'dellect'){
		$id = $_POST['id'];
	
		$fetch = $conn->query("SELECT * FROM lecturers WHERE id = $id");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		foreach ($result as $key) {
		$sname = $key->sname;
		$oname = $key->oname;
		$cutlen = substr($sname, 0,1);
	
		$cutlen = "$oname.$cutlen";
		}
		 	$conn->query("DELETE FROM lecturers WHERE id = $id");
		 	$conn->query("DELETE FROM users WHERE username = '$cutlen'");
		 	echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Deleted</b></p></div>";
		 }		
	}


if(isset($_POST['action']) && $_POST['action'] == 'delstu'){
		$id = $_POST['id'];
	
		$fetch = $conn->query("SELECT * FROM students WHERE id = $id");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		$count = $fetch->rowCount();
		if ($count > 0) {
		foreach ($result as $key) {
		$matric = $key->matric;
			}
		 	$conn->query("DELETE FROM students WHERE id = $id");
		 	$conn->query("DELETE FROM users WHERE username = '$matric'");
		 	echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>Deleted</b></p></div>";
		 }		
	}


if (isset($_POST['pname'])) {
    $pname = sanitize($_POST['pname']);
    $autor = sanitize($_POST['autor']);
    $desc = sanitize($_POST['desc']);
    $dop = sanitize($_POST['dop']);
    $edition = sanitize($_POST['edition']);
    $publisher = sanitize($_POST['publisher']);
    $nop = sanitize($_POST['nop']);
    $pop = sanitize($_POST['pop']);
    $book = sanitize($_POST['book']);
    $course = sanitize($_POST['course']);
    $by = sanitize($_POST['by']);
    $pass = $_FILES['pass']['tmp_name'];
    $filess = $_FILES['filess']['tmp_name'];
    $date = date('l M d, Y H:i');

    if (empty($pname) || empty($autor) || empty($desc) || empty($book) || empty($pass) || empty($filess) || empty($dop) || empty($edition) || empty($publisher) || empty($nop) || empty($pop)) {
        echo "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please fill all empty fields</b></p></div>";
    }elseif ($book == "text" && empty($course)) {
    	echo "<div style='color:#e6102e'><i class='fa fa-warning fa-5x'></i><p><b>Please fill all empty fields</b></p></div>";
    }else{

    	$fetch = $conn->query("SELECT * FROM course WHERE id = $course");
		$result = $fetch->fetchAll(PDO::FETCH_OBJ);
		foreach ($result as $key) {
			$coursename = $key->name;
			$underdep = $key->under;
		}
		$fetch2 = $conn->query("SELECT * FROM department WHERE id = $underdep");
		$result2 = $fetch2->fetchAll(PDO::FETCH_OBJ);
		foreach ($result2 as $key2) {
			$departmentname = $key2->name;
		}

       
        $temporary = explode("." , $_FILES [ "pass" ][ "name" ]);
        $temporary2 = explode("." , $_FILES [ "filess" ][ "name" ]);
        $file_extension = end ($temporary );
        $file_extension2 = end ($temporary2 );
        $pic = $_FILES['pass']['name'];
        $picp = $_FILES['pass']['tmp_name'];
        $pics = $_FILES['pass']['size'];

        $pic2 = $_FILES['filess']['name'];
        $picp2 = $_FILES['filess']['tmp_name'];
        $pics2 = $_FILES['filess']['size'];

        $rando = rand(00001,99999);
        $rando1 = rand(00001,99999);
        $hash = sha1($pic);
        $hash2 = sha1($pic2);
        $hash = $hash.$rando;
        $hash2 = $hash2.$rando1;

        $ext1 = pathinfo($pic, PATHINFO_EXTENSION);
        $ext1 = strtolower($ext1);

        $ext2 = pathinfo($pic2, PATHINFO_EXTENSION);
        $ext2 = strtolower($ext2);

        $upload_folder1 = "../books/image/".$hash.".".$ext1;
        $uploadpic = $hash.".".$ext1;

        $upload_folder2 = "../books/file/".$hash2.".".$ext2;
        $uploadpic2 = $hash2.".".$ext2;

        if ($book == "journal" || $book == "paper") {
        	$course = $book;
        }

        $in = array('name' => $pname,
                    'frontpage' => $uploadpic, 
                    'author' => $autor, 
                    'description' => $desc, 
                    'dop' => $dop, 
                    'edition' => $edition, 
                    'publisher' => $publisher, 
                    'pop' => $pop, 
                    'nop' => $nop, 
                    'file' => $uploadpic2, 
                    'course' => $course, 
                    'by' => $by, 
                    'date' => $date, 
                    'underd' => $departmentname, 
                    'underc' => $coursename, 
                );
        move_uploaded_file($picp, $upload_folder1);
        move_uploaded_file($picp2, $upload_folder2);
        create('books',$in);
        echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>You have successfully added $pname</b></p></div>";

    }
    }


    if (isset($_POST['action']) && $_POST['action'] == "findtab") {
                         $select = $conn->query("SELECT * FROM users WHERE priviledge = 'lecturer' || priviledge = 'student' order by help DESC");
                            $fetch = $select->fetchAll(PDO::FETCH_OBJ);
                            $count = $select->rowCount();
                            $serial = 1;
                            $dcolor = "#c19524";
                            if ($count > 0) {
                              foreach ($fetch as $value) {
                                $iden = $value->id;
                                $username = $value->username;
                            ?>
                             <a href="?mod=desk&link=load&user=<?php echo $iden; ?>"><div class="col-md-12 col-xs-12 col-sm-12 col-lg-12" style="border: 1px solid <?php echo $dcolor; ?>; border-radius: 5px"><?php echo "$username"; ?></div></a><br><br>
                              
                            <?php
                            $serial++;
                              }
                            }
                        ?>
<?php
    }






    if (isset($_POST['2id'])) {
    $id2 = sanitize($_POST['2id']);
    $filess = $_FILES['fileadd']['tmp_name'];
    $date = date('l M d, Y H:i');

    if (empty($filess)) {
    	$checkf = "no";
    }else{
    	$checkf = "yes";
    }

     if (!isset($_POST['message'])) {
    	$checkm = "no";
    }else{
    	$checkm = "yes";
    }

    if ($checkm == "no" && $checkf == "no") {
    	# code...
    }else{
    		$inside = "conversation";
    		$inside2 = "notification2";

    	if ($checkf == "yes") {
    		
    	$allowed = array('jpg', 'jpeg', 'gif', 'png', 'pp5', 'jp2', 'bmp', 'tif', 'ppf', 'cpt', 'tga', 'psd', 'dwg');
        $temporary2 = explode("." , $_FILES [ "fileadd" ][ "name" ]);
        $file_extension2 = end ($temporary2 );

        $pic2 = $_FILES['fileadd']['name'];
        $picp2 = $_FILES['fileadd']['tmp_name'];
        $pics2 = $_FILES['fileadd']['size'];

        $rando1 = rand(00001,99999);
        $hash2 = sha1($pic2);
        $hash2 = $hash2.$rando1;

        $ext2 = pathinfo($pic2, PATHINFO_EXTENSION);
        $ext2 = strtolower($ext2);

        if (in_array($ext2, $allowed)) {
        $type2 = "image";
        }else{
        	$type2 = "others";
        }
        $upload_folder2 = "messagefiles/".$hash2.".".$ext2;
        $uploadpic2 = $hash2.".".$ext2;


        $in = array('iden' => $id2,
                    'by' => "admin", 
                    'type1' => 'doc', 
                    'type2' => $type2, 
                    'message' => $uploadpic2, 
                    'dates' => $date, 
                );
         $in2 = array('iden' => $id2,
                );
         $in3 = array('help' => $date,
                );

        move_uploaded_file($picp2, $upload_folder2);
        create($inside,$in);
        create('notification2',$in2);
        update('users',$in3,'id',$id2);
        echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>You have successfully added </b></p></div>";
    	}elseif ($checkm == "yes" && $checkf == "no") {
    		$message = sanitize($_POST['message']);
    	if (empty($message)) {
    		# code...
    	}else{
    		$in = array('iden' => $id2,
                    'by' => "user", 
                    'type1' => 'text', 
                    'type2' => 'text', 
                    'message' => $message, 
                    'dates' => $date, 
                );
    		$in2 = array('iden' => $id2,
                );

    		$in3 = array('help' => $date,
                );

    		create($inside,$in);
    		create($inside2,$in2);
    		update('users',$in3,'id',$id2);
    		echo "<div style='color:#169f85'><i class='fa fa-check-circle-o fa-5x'></i><p><b>You have successfully added </b></p></div>";
    	}
    	}


    }


    }


    if (isset($_POST['action']) && $_POST['action'] == "finding") {
    	$iden = $_POST['ident'];
    	$provi = $_POST['provi'];
    	
    	$checki = "notification2";
    	
    	$select = $conn->query("SELECT * FROM $checki WHERE iden = '$iden'");
        $fetch = $select->fetchALL(PDO::FETCH_OBJ);
        $count = $select->rowCount();
        if ($count > 0) {
        echo "1";
        }else{
        	echo "0";
        }
    	
    }



    if (isset($_POST['action']) && $_POST['action'] == "findingnow") {
    	$iden = $_POST['ident'];
    	$checki = "conversation";
    	   $select = $conn->query("SELECT * FROM $checki WHERE iden = '$iden'");
        $fetch = $select->fetchALL(PDO::FETCH_OBJ);
        $count = $select->rowCount();
        if ($count > 0) {
       foreach ($fetch as $value) {
                        $by = $value->by;
                        $iden1 = $value->iden;
                        $type1 = $value->type1;
                        $type2 = $value->type2;
                        $message = $value->message;
                        $dates = $value->dates;
                        if ($by == "user") {
                           $box = "box2";
                        }elseif ($by = "admin") {
                           $box = "box1";
                        }
                        

         ?>
					<div class="<?php echo $box; ?>" id="<?php echo $iden1; ?>">
                     <div class="btext" style="overflow: hidden;">
                     <?php
                     if ($type1 == "doc" && $type2 == "image") {
                     	?>
                     	<img src="messagefiles/<?php echo $message ?>" width="30%">
                     	<?php
                        }elseif ($type1 == "doc" && $type2 == "others") {
                        ?>
						<a href="messagefiles/<?php echo $message; ?>" download><i class="fa fa-file fa-5x"></i></a>

                        <?php
                        $message = "";
                        }else{
                        ?>
							<p><?php echo "$message"; ?></p>
                        <?php
                        }
                     ?>
                         
                         <hr>
                         <small><?php echo "$dates"; ?></small>
                     </div> 
                  </div>
                  <br>
         <?php
             }
        }

        $conn->query("DELETE FROM notification2 WHERE iden = $iden");
    	
    }

}
?>