Initial commit

This commit is contained in:
Joey Kimsey
2024-08-04 21:15:59 -04:00
parent c9d1fb983f
commit 0d469501ee
695 changed files with 70184 additions and 71 deletions

View File

@ -0,0 +1,121 @@
<?php
/**
* app/plugins/fileshare/controllers/fileshare.php
*
* This is the home controller for the fileshare plugin.
*
* @package TP FileShare
* @version 3.0
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
*/
namespace TheTempusProject\Controllers;
use TheTempusProject\Houdini\Classes\Views;
use TheTempusProject\Houdini\Classes\Issues;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Bedrock\Functions\Upload;
use TheTempusProject\Classes\Controller;
use TheTempusProject\Models\Upload as UploadModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Classes\Forms;
use TheTempusProject\Bedrock\Functions\Session;
use TheTempusProject\Hermes\Functions\Redirect;
class Fileshare extends Controller {
protected static $uploads;
public function __construct() {
parent::__construct();
self::$uploads = new UploadModel;
self::$title = 'FileShare - {SITENAME}';
self::$pageDescription = 'Your saved Files for the site.';
}
public function index() {
$uploads = self::$uploads->getByUser();
Views::view( 'fileshare.list', $uploads );
}
public function delete( $id = null ) {
$upload = self::$uploads->findById( $id );
if ( $upload == false ) {
Issues::add( 'error', 'Upload not found.' );
return $this->index();
}
if ( $upload->createdBy != App::$activeUser->ID ) {
Issues::add( 'error', 'You do not have permission to modify this upload.' );
return $this->index();
}
$result = self::$uploads->delete( $id );
if ( $result == true ) {
Issues::add( 'success', 'Upload deleted.' );
} else {
Issues::add( 'notice', 'There was an problem deleting your upload.' );
}
return $this->index();
}
public function edit( $id = null ) {
$upload = self::$uploads->findById( $id );
if ( $upload == false ) {
Issues::add( 'error', 'Upload not found.' );
return $this->index();
}
if ( ! Input::exists( 'submit' ) ) {
return Views::view( 'calendar.calendar.edit', $upload );
}
if ( ! Forms::check( 'editUpload' ) ) {
Issues::add( 'error', [ 'There was an error editing your upload.' => Check::userErrors() ] );
return Views::view( 'fileshare.edit', $upload );
}
if ( Input::exists( 'file' ) ) {
$folder = UPLOAD_DIRECTORY . App::$activeUser->username . DIRECTORY_SEPARATOR;
if ( ! Upload::image( 'file', $folder ) ) {
Issues::add( 'error', [ 'There was an error with your upload.' => Check::systemErrors() ] );
return Views::view( 'fileshare.edit', $upload );
} else {
$route = str_replace( APP_ROOT_DIRECTORY, '', $folder );
$location = $route . Upload::last();
}
} else {
$location = '';
}
$result = self::$uploads->update( $id, Input::post('name'), $location );
if ( ! $result ) {
Issues::add( 'error', [ 'There was an error updating your upload.' => Check::userErrors() ] );
return Views::view( 'fileshare.edit', $upload );
}
Session::flash( 'success', 'Your Upload has been updated.' );
Redirect::to( 'fileshare/index/');
}
public function upload( $id = null ) {
if ( ! Input::exists() ) {
return Views::view( 'fileshare.create' );
}
if ( ! Forms::check( 'newUpload' ) ) {
Issues::add( 'error', [ 'There was an error creating your upload.' => Check::userErrors() ] );
return Views::view( 'fileshare.create' );
}
$folder = UPLOAD_DIRECTORY . App::$activeUser->username . DIRECTORY_SEPARATOR;
if ( ! Upload::image( 'file', $folder ) ) {
Issues::add( 'error', [ 'There was an error with your upload.' => Check::systemErrors() ] );
return Views::view( 'fileshare.create' );
} else {
$route = str_replace( APP_ROOT_DIRECTORY, '', $folder );
$location = $route . Upload::last();
}
$upload = self::$uploads->create( Input::post('name'), $location );
if ( ! $upload ) {
return Views::view( 'fileshare.create' );
}
Session::flash( 'success', 'Your Upload has been saved.' );
Redirect::to( 'fileshare/index/');
}
}

View File

@ -0,0 +1,47 @@
<?php
/**
* app/plugins/fileshare/forms.php
*
* This houses all of the form checking functions for this plugin.
*
* @package TP FileShare
* @version 3.0
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
*/
namespace TheTempusProject\Plugins\Feedback;
use TheTempusProject\Bedrock\Functions\Input;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Classes\Forms;
class FileShareForms extends Forms {
/**
* Adds these functions to the form list.
*/
public function __construct() {
self::addHandler( 'newUpload', __CLASS__, 'newUpload' );
self::addHandler( 'editUpload', __CLASS__, 'editUpload' );
}
public static function newUpload() {
if ( ! Input::exists( 'name' ) ) {
Check::addUserError( 'You must provide a name.' );
return false;
}
if ( ! Input::exists( 'file' ) ) {
Check::addUserError( 'You must provide a file.' );
return false;
}
return true;
}
public static function editUpload() {
if ( ! Input::exists( 'name' ) ) {
Check::addUserError( 'You must provide a name.' );
return false;
}
return true;
}
}
new FileShareForms;

View File

@ -0,0 +1,181 @@
<?php
/**
* app/plugins/fileshare/models/upload.php
*
* This class is used for the manipulation of the uploads database table.
*
* @package TP FileShare
* @version 3.0
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
*/
namespace TheTempusProject\Models;
use TheTempusProject\Bedrock\Classes\Config;
use TheTempusProject\Bedrock\Functions\Check;
use TheTempusProject\Canary\Canary as Debug;
use TheTempusProject\Classes\DatabaseModel;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Bedrock\Classes\CustomException;
class Upload extends DatabaseModel {
public $tableName = 'uploads';
public $databaseMatrix = [
[ 'name', 'varchar', '256' ],
[ 'location', 'text', '64' ],
[ 'size', 'int', '20' ],
[ 'file_type', 'varchar', '64' ],
[ 'createdBy', 'int', '11' ],
[ 'createdAt', 'int', '11' ],
];
/**
* The model constructor.
*/
public function __construct() {
parent::__construct();
}
public function create( $name, $location ) {
$info = $this->getFileInfo( $location );
$size = $info['size'];
$file_type = $info['file_type'];
$fields = [
'name' => $name,
'location' => '/' . $location,
'size' => $size,
'file_type' => $file_type,
'createdAt' => time(),
'createdBy' => App::$activeUser->ID,
];
if ( ! self::$db->insert( $this->tableName, $fields ) ) {
Debug::info( 'Uploads::create - failed to insert to db' );
return false;
}
return self::$db->lastId();
}
public function update( $id, $name, $location = '' ) {
$fields = [
'name' => $name,
];
if ( ! empty( $location ) ) {
$info = $this->getFileInfo( $location );
$fields['location'] = $location;
$fields['size'] = $info['size'];
$fields['file_type'] = $info['file_type'];
}
if ( ! self::$db->update( $this->tableName, $id, $fields ) ) {
new CustomException( 'UploadsUpdate' );
Debug::error( "Uploads: $id not updated: $fields" );
return false;
}
return true;
}
public function simple() {
$uploads = self::$db->get( $this->tableName, '*' );
if ( !$uploads->count() ) {
Debug::warn( 'Could not find any uploads' );
return false;
}
$uploads = $uploads->results();
$out = [];
foreach ( $uploads as $upload ) {
$out[ $upload->name ] = $upload->location;
}
return $out;
}
// public function processFileUpload() {
// }
public function getByUser( $limit = 0 ) {
$whereClause = [
'createdBy', '=', App::$activeUser->ID,
];
if ( empty( $limit ) ) {
$uploads = self::$db->get( $this->tableName, $whereClause );
} else {
$uploads = self::$db->get( $this->tableName, $whereClause, 'ID', 'DESC', [0, $limit] );
}
if ( ! $uploads->count() ) {
Debug::info( 'No Uploads found.' );
return false;
}
return $this->filter( $uploads->results() );
}
public function delete( $idArray ) {
if ( !is_array( $idArray ) ) {
$idArray = [ $idArray ];
}
foreach ( $idArray as $id ) {
$upload = self::findById( $id );
if ( $upload ) {
$fileLocation = APP_ROOT_DIRECTORY . ltrim( $upload->location, '/' );
if (file_exists($fileLocation)) {
if (!unlink($fileLocation)) {
Debug::error("Failed to delete file: $fileLocation");
return false;
}
} else {
Debug::warn("File does not exist: $fileLocation");
}
}
}
return parent::delete( $idArray );
}
private function getFileInfo( $imagePath )
{
$data = [];
$fileLocation = APP_ROOT_DIRECTORY . ltrim( $imagePath, '/' );
if ( file_exists( $fileLocation ) ) {
$fileInfo = finfo_open(FILEINFO_MIME_TYPE);
$mimeType = finfo_file($fileInfo, $fileLocation);
finfo_close($fileInfo);
$fileSize = filesize($fileLocation);
$data['file_type'] = $mimeType;
$data['size'] = $fileSize;
$data['path'] = $fileLocation;
} else {
Debug::warn("File does not exist: $fileLocation");
}
return $data;
}
public function filter( $data, $params = [] ) {
foreach ( $data as $instance ) {
if ( !is_object( $instance ) ) {
$instance = $data;
$end = true;
}
if (isset($instance->size)) {
$instance->readableSize = $this->formatSize($instance->size);
}
$out[] = $instance;
if ( !empty( $end ) ) {
$out = $out[0];
break;
}
}
return $out;
}
public function formatSize($size) {
$units = ['B', 'KB', 'MB', 'GB', 'TB'];
$i = 0;
while ($size >= 1024 && $i < count($units) - 1) {
$size /= 1024;
$i++;
}
return round($size, 2) . ' ' . $units[$i];
}
}

View File

@ -0,0 +1,45 @@
<?php
/**
* app/plugins/fileshare/plugin.php
*
* This houses all of the main plugin info and functionality.
*
* @package TP FileShare
* @version 3.0
* @author Joey Kimsey <Joey@thetempusproject.com>
* @link https://TheTempusProject.com
* @license https://opensource.org/licenses/MIT [MIT LICENSE]
*/
namespace TheTempusProject\Plugins;
use TheTempusProject\TheTempusProject as App;
use TheTempusProject\Classes\Plugin;
use TheTempusProject\Models\Upload;
use TheTempusProject\Houdini\Classes\Components;
use TheTempusProject\Houdini\Classes\Views;
class Fileshare extends Plugin {
public $pluginName = 'TP FileShare';
public $configName = 'fileshare';
public $pluginAuthor = 'JoeyK';
public $pluginWebsite = 'https://TheTempusProject.com';
public $modelVersion = '1.0';
public $pluginVersion = '3.0';
public $pluginDescription = 'A simple plugin which adds a site wide file-sharing system.';
public $permissionMatrix = [
'uploadFiles' => [
'pretty' => 'Can upload files',
'default' => false,
],
];
private static $loaded = false;
public function __construct( $load = false ) {
parent::__construct( $load );
if ( $this->checkEnabled() && App::$isLoggedIn ) {
if ( ! self::$loaded ) {
App::$topNavRightDropdown .= '<li><a href="{ROOT_URL}fileshare/index/"><i class="glyphicon glyphicon-cloud"></i> FileShare</a></li>';
self::$loaded = true;
}
}
}
}

View File

@ -0,0 +1,22 @@
<legend>File Upload</legend>
<form action="" method="post" class="form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="token" value="{TOKEN}">
<div class="form-group">
<label for="name" class="col-lg-3 control-label">Name</label>
<div class="col-lg-3">
<input type="text" class="form-control" name="name" id="name">
</div>
</div>
<div class="form-group">
<label for="file" class="col-lg-3 control-label">File</label>
<div class="col-lg-3">
<input type="file" class="form-control" name="file" id="file">
</div>
</div>
<div class="form-group">
<label for="submit" class="col-lg-3 control-label"></label>
<div class="col-lg-3">
<button name="submit" value="submit" type="submit" class="btn btn-lg btn-primary center-block ">Upload</button>
</div>
</div>
</form>

View File

@ -0,0 +1,22 @@
<legend>Edit File</legend>
<form action="" method="post" class="form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="token" value="{TOKEN}">
<div class="form-group">
<label for="name" class="col-lg-3 control-label">Name</label>
<div class="col-lg-3">
<input type="text" class="form-control" name="name" id="name" value="{name}">
</div>
</div>
<div class="form-group">
<label for="file" class="col-lg-3 control-label">File (uploading a new file will replace the existing file)</label>
<div class="col-lg-3">
<input type="file" class="form-control" name="file" id="file">
</div>
</div>
<div class="form-group">
<label for="submit" class="col-lg-3 control-label"></label>
<div class="col-lg-3">
<button name="submit" value="submit" type="submit" class="btn btn-lg btn-primary center-block ">Save</button>
</div>
</div>
</form>

View File

@ -0,0 +1,24 @@
<div class="row">
{LOOP}
<div class="col-sm-6 col-md-4">
<div class="thumbnail">
<img src="{location}" alt="{name}">
<div class="caption">
<h3>{name}</h3>
<p>{readableSize}</p>
<p>{file_type}</p>
<p>
<a href="/fileshare/edit/{ID}" class="btn btn-warning" role="button">edit</a>
<a href="/fileshare/delete/{ID}" class="btn btn-danger" role="button">Delete</a>
</p>
</div>
</div>
</div>
{/LOOP}
{ALT}
<div class="col-sm-6 col-md-4">
No Uploads Found
</div>
{/ALT}
</div>
<a href="{ROOT_URL}fileshare/upload" class="btn btn-sm btn-primary" role="button">Upload File</a>