<?php
$page_title = "Newsletter | CES Admin";
include_once("../course_registration/common_functions.php"); //common functions in the course registartion system
include_once("../course_registration/config.php"); //holds global config variables
check_if_authenticated('admin');//check if user is logged in
include_once("../course_registration/admin_html_top.php"); //get the HTML heading common to all pages in the CES admin module
date_default_timezone_set('America/Edmonton');
if (isset($_POST['submit'])) {
define("UPLOAD_DIR", $_SERVER['DOCUMENT_ROOT']."/ces/newsletter/");
$allowed = array('pdf', 'ppt', 'pptx', 'doc', 'docx');
if (!empty($_FILES["file"])) {
//check for allowed file extensions
$filename = $_FILES['file']['name'];
$ext = pathinfo($filename, PATHINFO_EXTENSION);
if(in_array($ext,$allowed)){
$theFile = $_FILES["file"];
if ($theFile["error"] !== UPLOAD_ERR_OK) {
$msg= "<p class='alert alert-error'>An error uploading the file occured.</p>";
}else{
// ensure a safe filename
$name = preg_replace("/[^A-Z0-9._-]/i", "-", $theFile["name"]);
// don't overwrite an existing file
/*$i = 0;
$parts = pathinfo($name);
while (file_exists(UPLOAD_DIR . $name)) {
$i++;
$name = $parts["filename"] . "-" . $i . "." . $parts["extension"];
}*/
// preserve file from temporary directory
$success = move_uploaded_file($theFile["tmp_name"], UPLOAD_DIR . $name);
if (!$success) {
$msg = "<p class='alert alert-error'>Unable to save file.</p>";
}else{
$msg = "<p class='alert alert-success'>The file, ".$name." was successfully uploaded.</p>";
}
}
// set proper permissions on the new file
chmod(UPLOAD_DIR . $name, 0644);
} else {
$msg = "<p class='alert alert-error'>Please upload only pdf, powerpoint, or word documents.</p>";
}
}
}
?>
<article class="ces-admin">
<h2>CES Admin - Newsletter</h2>
<?php include_once("../course_registration/ces_admin_nav.php"); ?>
<section id="upload">
<form method="post" enctype="multipart/form-data" class="form-inline" role="form">
<div class="width-50 form-group" style="display:inline-block">
<label for="file">Upload File:</label>
<input type="file" class="form-control" name="file" id="file" style="width:100%;">
</div>
<button type="submit" name="submit" class="btn btn-block ces-green-btn" >Upload</button>
<?php
if (isset($msg) && !empty($msg)) {
echo $msg;
}
?>
<p class="alert alert-info">Use the following format when naming files: <strong>YYYY-MM-Newsletter.xxx</strong><br> eg. 1999-04-Newsletter.pdf</p>
<p class="alert alert-info">File names must start with the date (<strong>YYYY-MM</strong>), and contain the word <em>Newsletter</em>.</p>
<p class="alert alert-info">Accepted file formats: .pdf, .doc, .docx, .ppt, .pptx</p>
</form>
<div class="file-list">
<table class='table table-striped'>
<thead>
<tr>
<th>File Name</th>
<th>File Size</th>
<th>Date Modified</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$dir = '../newsletter/';
$files = glob($dir.'*.{pdf,doc,docx,ppt,pptx}', GLOB_BRACE);
// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
rsort($files);
foreach ($files as $file){
echo '<tr><td><a href="/ces/newsletter/'.substr($file, strlen($dir)).'" target="_blank">'.substr($file, strlen($dir)).'</a></td>
<td>'.human_filesize(filesize($file), $decimals = 2).'</td>
<td>'.date("Y-m-d H:i:s",filemtime($file)).'</td>
<td class="delete"><a class="no-icon" href=\'newsletter-delete.php?file='.substr($file, strlen($dir)).'\'><span class="fa fa-trash"></span></a>
</tr>';
}
?>
</tbody>
</table>
</div>
</section>
</article>
</section><!-- #middle-->
</div><!-- #wrapper -->
<?php require_once $_SERVER['DOCUMENT_ROOT'].'/require/footer.php';?>