aliroFileInDB.php

Go to the documentation of this file.
00001 <?php
00002 
00003 /*******************************************************************************
00004  * Aliro - the modern, accessible content management system
00005  *
00006  * Aliro is open source software, free to use, and licensed under GPL.
00007  * You can find the full licence at http://www.gnu.org/copyleft/gpl.html GNU/GPL
00008  *
00009  * The author freely draws attention to the fact that Aliro derives from Mambo,
00010  * software that is controlled by the Mambo Foundation.  However, this section
00011  * of code is totally new.  If it should contain any fragments that are similar
00012  * to Mambo, please bear in mind (1) there are only so many ways to do things
00013  * and (2) the author of Aliro is also the author and copyright owner for large
00014  * parts of Mambo 4.6.
00015  *
00016  * Tribute should be paid to all the developers who took Mambo to the stage
00017  * it had reached at the time Aliro was created.  It is a feature rich system
00018  * that contains a good deal of innovation.
00019  *
00020  * Your attention is also drawn to the fact that Aliro relies on other items of
00021  * open source software, which is very much in the spirit of open source.  Aliro
00022  * wishes to give credit to those items of code.  Please refer to
00023  * http://aliro.org/credits for details.  The credits are not included within
00024  * the Aliro package simply to avoid providing a marker that allows hackers to
00025  * identify the system.
00026  *
00027  * Copyright in this code is strictly reserved by its author, Martin Brampton.
00028  * If it seems appropriate, the copyright will be vested in the Aliro Organisation
00029  * at a suitable time.
00030  *
00031  * Copyright (c) 2007 Martin Brampton
00032  *
00033  * http://aliro.org
00034  *
00035  * counterpoint@aliro.org
00036  *
00037  * aliroFileInDB is a file object that is stored in the database
00038  *
00039  */
00040 
00041 class aliroFileInDB {
00042     private $filename = '';
00043     private $database = null;
00044 
00045     public function __construct ($name) {
00046         $this->filename = $name;
00047         $this->database = aliroDatabase::getInstance();
00048     }
00049 
00050     public function fromFile ($source, $delete=false) {
00051         if ($f = fopen($source,'rb')) {
00052             $this->delete();
00053             $chunkid = 0;
00054             $sql = "INSERT INTO #__file_system (filename, chunkid, datachunk, bloblength) VALUES ($this->filename, ";
00055             while($f && !feof($f)) {
00056                 $chunk = fread($f, 60000);
00057                 $chunk = $this->database->getEscaped($chunk);
00058                 $this->database->doSQL($sql."$chunkid, '$chunk', LENGTH(datachunk))");
00059                 $chunkid++;
00060             }
00061             fclose($f);
00062             if ($delete) @unlink($source);
00063             return true;
00064         }
00065         else return false;
00066     }
00067 
00068     public function toFile ($destination, $delete=false) {
00069         $result = false;
00070         if (!file_exists($destination) AND $f = @fopen($destination, 'wb')) {
00071             $this->database->setQuery("SELECT chunkid FROM #__file_system WHERE filename='$this->filename' ORDER BY chunkid");
00072             $chunks = $this->database->loadResultArray();
00073             if ($chunks) foreach ($chunks as $chunkid) {
00074                 $this->database->setQuery("SELECT datachunk FROM #__file_system WHERE filename='$this->filename' AND chunkid=$chunkid");
00075                 $datachunk = $this->database->loadResult();
00076                 if (fwrite ($f, $datachunk)) $result = true;
00077                 else {
00078                     $result = false;
00079                     break;
00080                 }
00081             }
00082             fclose($f);
00083         }
00084         if ($result AND $delete) $this->delete();
00085         return $result;
00086     }
00087 
00088     public function delete () {
00089             $this->database->doSQL("DELETE FROM #__file_system WHERE filename='$this->filename'");
00090     }
00091 
00092 }
00093 
00094 ?>

Generated on Thu Apr 17 13:03:26 2008 for ALIRO by  doxygen 1.5.5