[Eis-commits] SF.net SVN: eis: [224] trunk
Status: Pre-Alpha
Brought to you by:
baslijnse
|
From: <bas...@us...> - 2007-12-13 18:50:13
|
Revision: 224
http://eis.svn.sourceforge.net/eis/?rev=224&view=rev
Author: baslijnse
Date: 2007-12-13 10:50:02 -0800 (Thu, 13 Dec 2007)
Log Message:
-----------
Added first rough version of the eis_file module
Modified Paths:
--------------
trunk/cfg/applications.xml
trunk/lib/engine/application.lib.php
trunk/lib/engine/engine.lib.php
trunk/lib/engine/request.lib.php
trunk/lib/engine/user.lib.php
Added Paths:
-----------
trunk/lib/base/fileservice.lib.php
trunk/mod/eis_file/
trunk/mod/eis_file/module.xml
trunk/mod/eis_file/services/
trunk/mod/eis_file/services/FileStoreService.php
Removed Paths:
-------------
trunk/mod/file/
Modified: trunk/cfg/applications.xml
===================================================================
--- trunk/cfg/applications.xml 2007-11-30 18:22:03 UTC (rev 223)
+++ trunk/cfg/applications.xml 2007-12-13 18:50:02 UTC (rev 224)
@@ -18,10 +18,4 @@
<application>examples</application>
<environment>minimal</environment>
</application>
- <application>
- <name>dsaasd</name>
- <module>eis_engine</module>
- <application>servermanager</application>
- <environment>minimal</environment>
- </application>
</applications>
Added: trunk/lib/base/fileservice.lib.php
===================================================================
--- trunk/lib/base/fileservice.lib.php (rev 0)
+++ trunk/lib/base/fileservice.lib.php 2007-12-13 18:50:02 UTC (rev 224)
@@ -0,0 +1,97 @@
+<?php
+ /**
+ * Base interface class for file storage services.
+ * If a module implements this interface, EIS can use it to store and retrieve files
+ */
+ class FileService extends Service {
+
+ /**
+ * Creates an empty file and returns an identifier
+ *
+ * @param path string the location of the file content
+ * @param name string the filename
+ * @param type string the mime type of the file
+ * @return The file identifier
+ */
+ function create($path, $name, $type) {
+ }
+ /**
+ * Deletes a file.
+ *
+ * @param id int The file identifier
+ */
+ function delete($id) {
+ }
+ /**
+ * Returns a path to the file.
+ *
+ * @param id int The file identifier
+ * @return A associative array with the file's attributes
+ */
+ function getPath($id) {
+ }
+ /**
+ * Returns an attribute record with metadata about the file.
+ * The record includes filename, filesize and mimetype
+ *
+ * @param id int The file identifier
+ * @return A associative array with the file's attributes
+ */
+ function getAttributes($id) {
+ }
+ /**
+ * Sets the full attribute record with metadata about the file.
+ *
+ * @param id int The file identifier
+ * @param attributes struct The attribute record
+ */
+ function setAttributes($id, $attributes) {
+ }
+ /**
+ * Returns the entire file content.
+ *
+ * @param id int The file identifier
+ * @return The file content
+ */
+ function getContent($id) {
+ }
+ /**
+ * Sets the entire file content.
+ *
+ * @param id int The file identifier
+ * @param content The file content
+ */
+ function setContent($id, $content) {
+ }
+ /**
+ * Returns the size of the file content counted in chunks.
+ *
+ * @param id int The file identifier
+ * @param chunksize int The size of a chunk in bytes
+ * @return The amount of chunks
+ */
+ function getContentChunkCount($id, $chunksize = null) {
+ }
+ /**
+ * Returns the i'th chunk of content from a file.
+ * Where i ranges from 0 to chunkcount -1.
+ *
+ * @param id int The file identifier
+ * @param i int The chunk index
+ * @param chunksize int The size of a chunk in bytes
+ * @return The chunk content
+ */
+ function getContentChunk($id, $i, $chunksize = null) {
+ }
+ /**
+ * Sets the i'th chunk of content in a file.
+ *
+ * @param id int The file identifier
+ * @param i int The chunk index
+ * @param content string The content to be put in the file
+ * @param chunksize int The size of a chunk in bytes
+ */
+ function setContentChunk($id, $i, $content, $chunksize = null) {
+ }
+ }
+?>
Modified: trunk/lib/engine/application.lib.php
===================================================================
--- trunk/lib/engine/application.lib.php 2007-11-30 18:22:03 UTC (rev 223)
+++ trunk/lib/engine/application.lib.php 2007-12-13 18:50:02 UTC (rev 224)
@@ -15,9 +15,8 @@
var $title = null;
- var $session_backend = null;
- var $authentication_backend = null;
- var $log_backend = null;
+ var $backends = array(); //Session, User, File, Image and Log
+ var $backend_defaults = array();
var $elements = array();
@@ -27,6 +26,13 @@
function Application(&$engine) {
$this->e =& $engine;
+
+ //Default backend services
+ $this->backend_defaults['session'] = array('module'=>'eis_session', 'service'=>'FileStoreService');
+ $this->backend_defaults['user'] = array('module'=>'eis_engine', 'service'=>'AuthService');
+ $this->backend_defaults['file'] = array('module'=>'eis_file', 'service'=>'FileStoreService');
+ $this->backend_defaults['image'] = array('module'=>'eis_image', 'service'=>'FileStoreService');
+ $this->backend_defaults['log'] = array('module'=>'eis_log', 'service'=>'FileLogService');
}
function load($application) {
@@ -66,9 +72,12 @@
$this->title = $struct->getValue($app,'title');
//Backends
- $this->session_backend = $struct->getValue($app,'session_backend');
- $this->authentication_backend = $struct->getValue($app,'authentication_backend');
- $this->log_backend = $struct->getValue($app,'log_backend');
+ foreach( array('session','user','file','image','log') as $backend) {
+ $service = $struct->getValue($app, $backend.'_backend');
+ if($service != null) {
+ $this->backends[$backend] = $service;
+ }
+ }
//Module configuration
$modules = $struct->getArray($app,'module');
@@ -238,6 +247,7 @@
} else {
$elem['depth'] = $this->elements[$id]['inherit_default'];
}
+
$subpage_pipelines[$subpage][] = $elem;
$catchall_pipeline[] = $elem;
}
@@ -307,9 +317,7 @@
$cache = "<?php\n";
$cache .= "\$this->modules =".var_export($this->modules, true).";\n";
$cache .= "\$this->title =".var_export($this->title, true).";\n";
- $cache .= "\$this->session_backend =".var_export($this->session_backend, true).";\n";
- $cache .= "\$this->authentication_backend =".var_export($this->authentication_backend, true).";\n";
- $cache .= "\$this->log_backend =".var_export($this->log_backend, true).";\n";
+ $cache .= "\$this->backends =".var_export($this->backends, true).";\n";
$cache .= "\$this->elements =".var_export($this->elements, true).";\n";
$cache .= "\$this->page_root =".var_export($this->page_root, true).";\n";
$cache .= "\$this->page_notfound =".var_export($this->page_notfound, true).";\n";
@@ -327,14 +335,27 @@
return $this->environment;
}
function getSessionBackend() {
- return $this->session_backend;
+ return $this->_getBackend('session');
}
- function getAuthenticationBackend() {
- return $this->authentication_backend;
+ function getUserBackend() {
+ return $this->_getBackend('user');
}
+ function getFileBackend() {
+ return $this->_getBackend('file');
+ }
+ function getImageBackend() {
+ return $this->_getBackend('image');
+ }
function getLogBackend() {
- return $this->log_backend;
+ return $this->_getBackend('log');
}
+ function _getBackend($type) {
+ if(isset($this->backends[$type])) {
+ return $this->backends[$type];
+ } else {
+ return $this->backend_defaults[$type];
+ }
+ }
function getRequiredModules() {
return array_keys($this->modules);
}
Modified: trunk/lib/engine/engine.lib.php
===================================================================
--- trunk/lib/engine/engine.lib.php 2007-11-30 18:22:03 UTC (rev 223)
+++ trunk/lib/engine/engine.lib.php 2007-12-13 18:50:02 UTC (rev 224)
@@ -249,8 +249,6 @@
fclose($f);
}
-
-
/**
*Load elementconfig
*@param module of the config to be loaded
@@ -290,8 +288,6 @@
return $instance;
}
-
-
/**
* Creates an element instance
*
@@ -407,7 +403,6 @@
$this->error(5000,$message);
}
}
-
/**
*Errorlist and the setting of errors
*@param code the code of the error
@@ -741,15 +736,14 @@
case 'static':
$this->serveStatic($args);
break;
+ case 'file':
+ $this->serveFile($args);
+ break;
case 'image':
$this->serveImage($args);
break;
- case 'file':
- $this->serveFile($args);
- break;
}
}
-
ob_end_flush();
}
@@ -904,35 +898,46 @@
$this->error(404);
}
}
+
/**
- * Serves an image stored by the image module
+ * Serves a file managed by EIS
*/
- function serveImage() {
+ function serveFile($args) {
- //Load and stream image data
- $sImage =& $this->getService('image');
+ $id = intval($args['file']);
- $request = explode('_',$this->request['image']);
- $image = $sImage->getImageById($request[0]);
+ //Load file service for the current application
+ $backend = $this->application->getFileBackend();
+ $service =& $this->getService($backend['module'], $backend['service']);
- $file = $sImage->getCachedByFilename($this->request['image']);
+ //Load file attributes
+ $attributes = $service->getAttributes($id);
- if(!$image || !$file ) {
+ if(!is_array($attributes)) {
$this->error(404);
+ return;
}
-
- //TODO: Check image clearance
- //Send headers
- header('Content-Type: '.$image['mime_type']);
- header('Content-Length: '.filesize($file));
- header('Last-Modified: '.date("r",filemtime($file)));
+ //Send headers
+ header('Content-Type: '.$attributes['type']);
+ header('Content-Length: '.$attributes['size']);
+ if($args['inline']) {
+ header('Content-Disposition: inline; filename='.$attributes['name']);
+ } else {
+ header('Content-Disposition: attachment; filename='.$attributes['name']);
+ }
- $this->_engineHeaders();
-
- //Stream data
- readfile($file);
+ //Stream content
+ $num = $service->getContentChunkCount($id);
+ for($i = 0; $i < $num; $i++) {
+ print $service->getContentChunk($id,$i);
+ }
}
+ /**
+ * Serves an image managed by EIS
+ */
+ function serveImage($args) {
+ }
/**
* Serves a web service request
@@ -1044,6 +1049,7 @@
}
/**< API METHODS */
+
function getBaseHost() {
return $this->baseHost;
}
@@ -1108,28 +1114,117 @@
function getStrings($module) {
return $this->locale->getStrings($module);
}
+ /**
+ * Get a reference to a service object.
+ *
+ * @param module string Module to which the service belongs
+ * @param service string The name of the module
+ */
+ function &getService($module, $service = 'MainService') {
+ if(!isset($this->services[$module][$service])) {
+ $this->services[$module][$service] = $this->_loadService($module,$service);
+ }
+
+ return $this->services[$module][$service];
+ }
+
+
+ //EIS Managed resource methods
+
+ /**
+ * Return the envionment's database connection
+ * @param shared boolean If shared is set to false a new connection is made.
+ */
function getDatabase($shared = true) {
return $this->environment->getDatabase($shared);
}
/**
- * Get the Datadir
+ * Return the path to the environments datadir
*
* @return returns the path to the dir on succes and null on failure
*/
function getDataDir() {
return $this->environment->getDataDir();
}
+ /**
+ * Create a new file in the EIS file storage
+ *
+ * @param path the path to the file on the filesystem
+ * @param name the filename which should be stored with the file
+ * @param the mimetype which should be stored with the file
+ * @return an integer, the id of the file
+ */
+ function createFileFromPath($path, $name, $type) {
+ $backend = $this->application->getFileBackend();
+ if(!is_array($backend)) {
+ return 0;
+ }
+ $service =& $this->getService($backend['module'],$backend['service']);
+
+ return $service->create($path, $name, $type);
+ }
/**
- * Get the page history
+ * Create a new file in the EIS file storage
+ * directly from an upload.
+ *
+ * @param name the name of the upload in the $_FILES array
+ * @return an integer, the id of the file
*/
- function getPageHistory() {
- return $this->session->getHistory();
+ function createFileFromUpload($name) {
+ if(is_array($_FILES[$name]) && is_uploaded_file($_FILES[$name]['tmp_name'])) {
+ return $this->createFileFromPath(
+ $_FILES[$name]['tmp_name'],
+ $_FILES[$name]['name'],
+ $_FILES[$name]['type'] );
+ } else {
+ return 0;
+ }
}
-
/**
- *makes an href for the user in a html link to a view of the current element
+ * Deletes a file stored in the EIS storage
+ *
+ * @param id the id of the file in the storage
+ */
+ function deleteFile($id) {
+ $backend = $this->application->getFileBackend();
+ if(!is_array($backend)) {
+ return;
+ }
+ $service =& $this->getService($backend['module'],$backend['service']);
+ $service->delete($id);
+ }
+ /**
+ * Returns the path to the file
+ *
+ * @param id the id of the file in the storage
+ * @return the path to the file
+ */
+ function getFile($id) {
+ $backend = $this->application->getFileBackend();
+ if(!is_array($backend)) {
+ return;
+ }
+ $service =& $this->getService($backend['module'],$backend['service']);
+ return $service->getPath($id);
+ }
+ /**
+ * Returns the attributes of file
+ *
+ * @param id the id of the file in the storage
+ * @return an attributes record containing filename, type and size
+ */
+ function getFileAttributes($id) {
+ $backend = $this->application->getFileBackend();
+ if(!is_array($backend)) {
+ return;
+ }
+ $service =& $this->getService($backend['module'],$backend['service']);
+ return $service->getAttributes($id);
+ }
+ /**
+ *Makes an href for the user in a html link to a view of the current element
*@param view the name of a view of the currentelement
*@param parameter an array of parameters for the href
*@see hrefPage hrefAction hrefDetach hrefImage hrefPreviousPage hrefParentPage
@@ -1234,7 +1329,7 @@
*/
function hrefPreviousPage($steps = 1) {
- $history = $this->getPageHistory();
+ $history = $this->session->getHistory();
if(!is_array($history) || ! count($history)) {
return $this->hrefParentPage();
}
@@ -1262,33 +1357,31 @@
return $link;
}
- /**
- *Make image link
- *@param id the imgae id
- *@param attributes attributes of the filename
- *@see hrefPage hrefAction hrefView hrefDetach hrefParentPage hrefPreviousPage
- *@result a string containing the link
+ /**
+ * Creates a link to an EIS managed file
+ * @param id int The id of the file
+ * @param inline bool sent the file content as inline content. Default is attachment.
*/
- function hrefImage($id, $attributes) {
- $sI =& $this->getService('image');
- $hash = $sI->genFilename($id, $attributes);
+ function hrefFile($id, $inline = false) {
+ $link = $this->getApplicationPath().'F/';
+ $link .= intval($id);
+ if ($inline) {
+ $link .= '/inline';
+ }
- $str = '?i='.$hash;
-
- return $str;
+ return $link;
}
-
- function hrefFile($id, $attachment = true) {
- }
/**
- *Set header to redirect to a specified url
- *@param path the path for the header to redirect to
+ * Creates a link to an EIS managed image
+ *@param id the imgae id
+ *@param options dictionary with image options
*/
- function redirect($path) {
- header('Location: '.$this->baseHost.$path);
- exit;
+ function hrefImage($id, $attributes = null) {
+ //TODO
}
+ // Content generation
+
function showMessage($string, $replacements = null) {
$msg['type'] = 'message';
$msg['string'] = $string;
@@ -1379,18 +1472,13 @@
function setContentType($mimetype) {
$this->contentType = $mimetype;
}
-
/**
- * Get a reference to a service object.
- *
+ *Set header to redirect to a specified url
+ *@param path the path for the header to redirect to
*/
- function &getService($module, $service = 'MainService') {
-
- if(!isset($this->services[$module][$service])) {
- $this->services[$module][$service] = $this->_loadService($module,$service);
- }
-
- return $this->services[$module][$service];
+ function redirect($path) {
+ header('Location: '.$this->baseHost.$path);
+ exit;
}
}
?>
Modified: trunk/lib/engine/request.lib.php
===================================================================
--- trunk/lib/engine/request.lib.php 2007-11-30 18:22:03 UTC (rev 223)
+++ trunk/lib/engine/request.lib.php 2007-12-13 18:50:02 UTC (rev 224)
@@ -93,9 +93,17 @@
}
break;
case 'F':
- if(count($this->path) == 2) {
+ $c = count($this->path);
+ if($c == 2 || $c == 3) {
$this->subtype = 'file';
$this->args['file'] = intval($this->path[1]);
+ $this->args['inline'] = false;
+
+ if($c == 3) {
+ if($this->path[2] == 'inline') {
+ $this->args['inline'] = true;
+ }
+ }
} else {
print "Invalid file resource request";
}
Modified: trunk/lib/engine/user.lib.php
===================================================================
--- trunk/lib/engine/user.lib.php 2007-11-30 18:22:03 UTC (rev 223)
+++ trunk/lib/engine/user.lib.php 2007-12-13 18:50:02 UTC (rev 224)
@@ -99,7 +99,7 @@
* @return the uid of the user (integer) or 0 when the login failed.
*/
function loginByCredentials($username, $password) {
- $backend = $this->e->application->getAuthenticationBackend();
+ $backend = $this->e->application->getUserBackend();
if(!is_array($backend)) {
return 0;
}
@@ -120,7 +120,7 @@
* @return the uid of the user (integer) or 0 when the login failed.
*/
function loginByMD5($username, $seed, $hash) {
- $backend = $this->e->application->getAuthenticationBackend();
+ $backend = $this->e->application->getUserBackend();
if(!is_array($backend)) {
return 0;
}
Added: trunk/mod/eis_file/module.xml
===================================================================
--- trunk/mod/eis_file/module.xml (rev 0)
+++ trunk/mod/eis_file/module.xml 2007-12-13 18:50:02 UTC (rev 224)
@@ -0,0 +1,9 @@
+<?xml version="1.0" encoding="utf-8" ?>
+<module>
+ <name>eis_file</name>
+ <version>0.1.0</version>
+ <description>File management of an EIS server.</description>
+ <author>EIS Team</author>
+ <homepage>http://eis.sourceforge.net/</homepage>
+ <license>BSD License</license>
+</module>
Added: trunk/mod/eis_file/services/FileStoreService.php
===================================================================
--- trunk/mod/eis_file/services/FileStoreService.php (rev 0)
+++ trunk/mod/eis_file/services/FileStoreService.php 2007-12-13 18:50:02 UTC (rev 224)
@@ -0,0 +1,187 @@
+<?php
+ require_once("lib/base/fileservice.lib.php");
+
+ /**
+ * Base interface class for file storage services.
+ * If a module implements this interface, EIS can use it to store and retrieve files
+ */
+ class eis_file_FileStoreService extends FileService {
+
+ var $basedir = '/eis_file/';
+ var $chunksize = 1024;
+
+ var $handles = array();
+
+ /**
+ * Creates an empty file and returns an identifier
+ * @return The file identifier
+ */
+ function create($path, $name, $type) {
+ $id = $this->_nextId();
+
+ //Create the file dir
+ mkdir($this->_getFileDir($id));
+
+ //Create attributes file
+ $attr = array('name' => $name, 'type' => $type);
+ $attr = '<?php $attributes = '.var_export($attr,true).'; ?>';
+ file_put_contents($this->_getFileDir($id).'/attributes',$attr);
+
+ //Copy content
+ copy($path, $this->_getFileDir($id).'/content');
+
+ return $id;
+ }
+ /**
+ * Deletes a file.
+ *
+ * @param id int The file identifier
+ */
+ function delete($id) {
+ $dir = $this->_getFileDir($id);
+
+ unlink($dir.'/content');
+ unlink($dir.'/attributes');
+ rmdir($dir);
+ }
+
+ /**
+ * Returns a path to the file.
+ *
+ * @param id int The file identifier
+ * @return A associative array with the file's attributes
+ */
+ function getPath($id) {
+ return $this->_getFileDir($id).'/content';
+ }
+
+ /**
+ * Returns an attribute record with metadata about the file.
+ * The record includes filename, filesize and mimetype
+ *
+ * @param id int The file identifier
+ * @return A associative array with the file's attributes
+ */
+ function getAttributes($id) {
+ $attribute_file = $this->_getFileDir($id).'/attributes';
+
+ if(is_readable($attribute_file)) {
+ include($attribute_file);
+ if(isset($attributes)) {
+ //Add filesize
+ $attributes['size'] = $this->_getFileSize($id); return $attributes;
+ }
+ }
+ return null;
+ }
+ /**
+ * Sets the full attribute record with metadata about the file.
+ *
+ * @param id int The file identifier
+ * @param attributes struct The attribute record
+ */
+ function setAttributes($id, $attributes) {
+ }
+ /**
+ * Returns the entire file content.
+ *
+ * @param id int The file identifier
+ * @return The file content
+ */
+ function getContent($id) {
+ return file_get_contents($this->_getFileContentPath($id));
+ }
+ /**
+ * Sets the entire file content.
+ *
+ * @param id int The file identifier
+ * @param content The file content
+ */
+ function setContent($id, $content) {
+ file_put_contents($this->_getFileContentPath($id),$content);
+ }
+ /**
+ * Returns the size of the file content counted in chunks.
+ *
+ * @param id int The file identifier
+ * @param chunksize int The size of a chunk in bytes
+ * @return The amount of chunks
+ */
+ function getContentChunkCount($id, $chunksize = null) {
+ return ceil(floatval($this->_getFileSize($id)) / (($chunksize == null) ? $this->chunksize : $chunksize));
+ }
+ /**
+ * Returns the i'th chunk of content from a file.
+ * Where i ranges from 0 to chunkcount -1.
+ *
+ * @param id int The file identifier
+ * @param i int The chunk index
+ * @param chunksize int The size of a chunk in bytes
+ * @return The chunk content
+ */
+ function getContentChunk($id, $i, $chunksize = null) {
+ $size = ($chunksize == null) ? $this->chunksize : $chunksize;
+ $offset = $i * $size;
+
+ $f = $this->_openFile($id);
+ fseek($f, $offset, SEEK_SET);
+
+ return fread($f, $size);
+ }
+ /**
+ * Sets the i'th chunk of content in a file.
+ *
+ * @param id int The file identifier
+ * @param i int The chunk index
+ * @param content string The content to be put in the file
+ * @param chunksize int The size of a chunk in bytes
+ */
+ function setContentChunk($id, $i, $content, $chunksize = null) {
+ //TODO
+ }
+ function _nextId() {
+
+ $dir = $this->e->getDatadir().$this->basedir;
+
+ if(is_dir($dir)) {
+ $next = 1;
+ $d = opendir($dir);
+ while($entry = readdir($d)) {
+ if(intval($entry) >= $next) {
+ $next = intval($entry) + 1;
+ }
+ }
+ closedir($d);
+
+ return $next;
+ } else {
+ mkdir($dir);
+ return 1;
+ }
+ }
+
+ function _getFileDir($id) {
+ return $this->e->getDatadir().$this->basedir.intval($id);
+ }
+ function _getFileAttributePath($id) {
+ return $this->_getFileDir($id).'/attributes';
+ }
+ function _getFileContentPath($id) {
+ return $this->_getFileDir($id).'/content';
+ }
+ function _getFileSize($id) {
+ $path = $this->_getFileContentPath($id);
+ if(file_exists($path)) {
+ return filesize($path);
+ } else {
+ return 0;
+ }
+ }
+ function _openFile($id) {
+ if(!isset($this->handles[$id])) {
+ $this->handles[$id] = fopen($this->_getFileContentPath($id),'r');
+ }
+ return $this->handles[$id];
+ }
+ }
+?>
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|