CI_Resource_Management
Resource Management Class for Codeigniter framework. It allows to store and
process files w/wo encryption, (*1)
What is CI Resource Management, (*2)
This is a library coded to work in CodeIgniter Framework that helps in the
process of management resources - files - and storing them in your filesystem
with or without encryption. By default it uses rijndael-256 - commonly known
as AES256 - to store files., (*3)
If your web application handles a lot of files, that you must secure in some
way and want to have a common library to do all the hard work of storing and
encrypting files in your application. This library is for you., (*4)
Installation, (*5)
1.a (If you use composer) Execute: composer require cjci/res_storage
and fill in
your desired version (dev-master for latest changes)., (*6)
1.b (Manually) Download desired version of this project to application/libraries/, (*7)
- Create your own res_storage.php config file in application/config
to configure your path and encryption key. (see Usage for more details)
Usage, (*8)
Codeigniters wrapper to loader class, (*9)
This class can be used in anywhere in your code just by its FQNS CJCI/ResStorage/ResStorage
But, if you want to use $this->load->('res_storage')
in your code, because you are familiar
with CI Loader. Just create a res_storage.php file in your application/libraries folder.
Contents as follows:, (*10)
<?php
if (!defined('BASEPATH'))
exit('No direct script access allowed');
use CJCI\ResStorage\ResStorage;
class res_storage extends ResStorage{}
Now we need to configure where to store files and a new clear secretkey.
(It will be the basis of a
new hashed secret key), (*11)
Creating a res_storage.php file in your CodeIgniter's config dir:, (*12)
$config['clearkey'] = "YourSecretKey" ;
, (*13)
$config['storage_dir'] = "/Path/To/Your/Filesystem/";
, (*14)
Or as a parameter when using CI Loader Class:, (*15)
$this->load->library('res_storage',array('clearkey' => 'MyKey','storage_dir' => '/Path'));
, (*16)
Instantiate
$this->load->library('res_storage');
, (*17)
Store a file
When storing files it will store a copy of that file.
It wont move or delete original file.
$uuid = $this->res_storage->store_file('/Path/to/File.txt');
, (*18)
$uuid is auniq a string identifier that identifies that file in the future., (*19)
Storing a file will store a copy of a document. using a file handler class
(Encrypted AES256 32IV by default)
And also will store metadata of that document into res_storage table., (*20)
Read Metadata
var_dump($this->res_storage->metadata($uuid));
, (*21)
Returns, (*22)
array(10) {
["id"]=> string(1) "8"
["uuid"]=> string(23) "52825331913af4.96762038"
["filename"]=> string(22) "logo_entry_transparent.jpg"
["path"]=> string(46) "/var/www/webpage/resources/827/c0b/b38/"
["mimetype"]=> string(10) "image/jpeg"
["hash"]=> string(32) "827c0bb38c277eb592ff122b39b67d9e"
["b64_iv"]=> string(44) "maasdaX7JsfsGJvgcMhX2jBRkm2N4SV7523sYgP6Pb7gZN89Xa62mU="
["accessed"]=> string(1) "0"
["stored"]=> string(19) "2013-08-01 20:11:29"
["lastaccess"]=> string(19) "2013-08-01 20:11:29"
}
Get File contents
$contents = $this->res_storage->file_get_contents($uuid);
, (*23)
Output contents to browser using metadata
Uses metadata to write some headers and then file contents
$this->res_storage->readfile($uuid);
, (*24)
Delete File
$this->res_storage->delete($uuid)
, (*25)
Everything will throw an Exception if something goes wrong.
So if you want to be safe and not have halfloaded pages... try using
try-catch:, (*26)
try{
return $this->res_storage->readfile($uuid);
}catch (Exception $e) {
error_log ('/* Captured: ', $e->getMessage(), "*/ \n");
http_response_code(404);
die("File not found.");
}
Creating your own FileHandle with your own encryption, (*27)
Extend and include somewhere in your code RSFile Class if you don't want to use
MCRYPT to store your files encrypted.
Otherwise extend RSFileEncrypted and modify it's properties to fit your needs., (*28)
Add to your config/res_storage.php:
$config['file_handler'] = 'My_File_Handler' ;
, (*29)
Or Extend ResStorage and override the following to use your FileHandler:, (*30)
use CJCI\ResStorage\ResStorage;
class My_Storage extends ResStorage {
/**
* Class to use to handle phisically files. Default RSFileEncrypted
* use RSFile for non encrypted handling. (or write your own!)Where files will be stored.
* FileClass
* @access public
* @var string
*/
public $FileClass = "MY_File_Handler";
}
License, (*31)
GPL V3, (*32)