Show
Ignore:
Timestamp:
05/17/07 16:25:43 (5 years ago)
Author:
fproulx
Message:

2007-05-17 François Proulx <francois.proulx@…>

  • WARNING: This has not been fully tested. Full testing to be completed on 2006-05-18 but I'm pretty confident that it works pretty well.
  • Added automatic resize on Avatars
  • Added FavoriteHotspots? content (mostly used for Profiles)
  • Added new features to Content and File
  • Fixed URL regexp bug in HyperlinkUtils?
  • Updated French translations
  • Updates profiles template
  • TODO: Add getNewUI et processNewUI to FavoriteHotspots? to simplify input
Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/wifidog-auth/wifidog/classes/Content/File/File.php

    r1206 r1226  
    11<?php 
     2 
    23 
    34/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ 
     
    6364{ 
    6465 
    65     /** 
    66      * File size unit: Byte 
    67  
    68      */ 
    69     const UNIT_BYTES = 1; 
    70  
    71     /** 
    72      * File size unit: KB 
    73  
    74      */ 
    75     const UNIT_KILOBYTES = 1024; 
    76  
    77     /** 
    78      * File size unit: MB 
    79  
    80      */ 
    81     const UNIT_MEGABYTES = 1048576; 
    82  
    83     /** 
    84      * File size unit: GB 
    85  
    86      */ 
    87     const UNIT_GIGABYTES = 1073741824; 
    88  
    89         /** Can the user edit the filename */ 
    90                 private $configEnableEditFilename = true; 
    91                  
    92                         /** Can the user edit the mime type */ 
    93                 private $configEnableEditMimeType = true; 
    94     /** 
    95      * Constructor 
    96      * 
    97      * @param string $content_id Content id 
    98      */ 
    99     protected function __construct($content_id) 
    100     { 
    101          
    102         $db = AbstractDb::getObject(); 
    103  
    104         // Init values 
    105         $row = null; 
    106         parent :: __construct($content_id); 
    107  
    108         $content_id = $db->escapeString($content_id); 
    109         $sql = "SELECT * FROM content_file WHERE files_id='$content_id'"; 
    110         $db->execSqlUniqueRes($sql, $row, false); 
    111  
    112         if ($row == null) { 
    113             /* 
    114              * Since the parent Content exists, the necessary data in 
    115              * content_group had not yet been created 
    116              */ 
    117             $sql = "INSERT INTO content_file (files_id) VALUES ('$content_id')"; 
    118             $db->execSqlUpdate($sql, false); 
    119  
    120             $sql = "SELECT * FROM content_file WHERE files_id='$content_id'"; 
    121             $db->execSqlUniqueRes($sql, $row, false); 
    122  
    123             if ($row == null) { 
    124                 throw new Exception(_("The content with the following id could not be found in the database: ").$content_id); 
    125             } 
    126         } 
    127  
    128         $this->mBd = &$db; 
    129         $this->files_row = $row; 
    130     } 
    131  
    132     /** 
    133      * Set Binary data from a POST form data field 
    134      * 
    135      * @param string $upload_field The form field that contains the data 
    136      * 
    137      * @return bool True if successful 
    138  
    139      */ 
    140     private function setBinaryDataFromPostVar($upload_field) 
    141     { 
    142         // Init values 
    143         $_retval = false; 
    144  
    145         if (!empty ($_FILES[$upload_field]) && $_FILES[$upload_field]['error'] == UPLOAD_ERR_OK) { 
    146             // Unlink BLOB if any exists 
    147             $blob_oid = $this->getBinaryDataOid(); 
    148  
    149             if ($blob_oid) { 
    150                 $this->mBd->unlinkLargeObject($blob_oid); 
    151             } 
    152  
    153             // Updating database 
    154             // Create a new BLOB 
    155             $new_oid = $this->mBd->importLargeObject($_FILES[$upload_field]['tmp_name']); 
    156             // Switch to new OID and touch file 
    157             $this->setBinaryDataOid($new_oid); 
    158             $this->setLocalFileSize($_FILES[$upload_field]['size']); 
    159             $this->setMimeType($_FILES[$upload_field]['type']); 
    160             $this->setFilename($_FILES[$upload_field]['name']); 
    161  
    162             $_retval = true; 
    163         } else { 
    164             switch ($_FILES[$upload_field]['error']) 
    165             { 
    166             case 'UPLOAD_ERR_INI_SIZE': 
    167                 echo _("File size exceeds limit specified in PHP.ini"); 
    168                 break; 
    169  
    170             case 'UPLOAD_ERR_FORM_SIZE': 
    171                 echo _("File size exceeds limit specified HTML form"); 
    172                 break; 
    173  
    174             case 'UPLOAD_ERR_PARTIAL': 
    175                 echo _("File upload was interrupted"); 
    176                 break; 
    177  
    178             case 'UPLOAD_ERR_NO_TMP_DIR': 
    179                 echo _("Missing temp folder"); 
    180                 break; 
    181  
    182             } 
    183         } 
    184  
    185         return $_retval; 
    186     } 
    187  
    188     /** 
    189      * Returns the binary data from the database 
    190      * 
    191      * @return string Binary data from the database 
    192  
    193      */ 
    194     private function getBinaryDataOid() 
    195     { 
    196         return $this->mBd->unescapeBinaryString($this->files_row['data_blob']); 
    197     } 
    198  
    199     /** 
    200      * Saves the binary data to the database 
    201      * 
    202      * @param string $oid Binary data 
    203      * 
    204      * @return void 
    205  
    206      */ 
    207     private function setBinaryDataOid($oid) 
    208     { 
    209         if (is_null($oid)) { 
    210             $oid_sql = "NULL"; 
    211         } 
    212         else 
    213         { 
    214                 $oid_sql = $this->mBd->escapeString($oid); 
    215         } 
    216         if($this->files_row['data_blob']!=$oid){ 
    217         $this->mBd->execSqlUpdate("UPDATE content_file SET data_blob = $oid WHERE files_id='".$this->getId()."'", false); 
    218         // Touch and refresh this object 
    219         $this->touch(); 
    220         } 
    221     } 
    222  
    223     /** 
    224      * Is the MimeType editable 
    225      * @param $enabled Default is true 
    226      */ 
    227     protected function configEnableEditMimeType($enabled=true) 
    228     { 
    229         return $this->configEnableEditMimeType=$enabled; 
    230     } 
    231      
    232     /** 
    233      * Returns the MIME type of the file 
    234      * 
    235      * @return string MIME type of file 
    236      */ 
    237     public function getMimeType() 
    238     { 
    239         return $this->files_row['mime_type']; 
    240     } 
    241  
    242     /** 
    243      * Saves the MIME type of the file 
    244      * 
    245      * @param string $mime_type 
    246      * 
    247      * @return void 
    248      */ 
    249     private function setMimeType($mime_type) 
    250     { 
    251         $mime_type = $this->mBd->escapeString($mime_type); 
    252         $this->mBd->execSqlUpdate("UPDATE content_file SET mime_type ='".$mime_type."' WHERE files_id='".$this->getId()."'", false); 
    253         // Touch and refresh this object 
    254         $this->touch(); 
    255     } 
    256  
    257     /** 
    258      * Is the filename editable 
    259      * @param $enabled Default is true 
    260      */ 
    261     protected function configEnableEditFilename($enabled=true) 
    262     { 
    263         return $this->configEnableEditFilename=$enabled; 
    264     } 
    265      
    266     /** 
    267      * Returns the filename of the file 
    268      * 
    269      * @return string Filename of file 
    270  
    271      */ 
    272     protected function getFilename() 
    273     { 
    274         return $this->files_row['filename']; 
    275     } 
    276  
    277     /** 
    278      * Stores the filename of the file 
    279      * 
    280      * @param string $file_name Filename of the file 
    281      * 
    282      * @return void 
    283      */ 
    284     private function setFilename($file_name) 
    285     { 
    286         $file_name = $this->mBd->escapeString($file_name); 
    287         $this->mBd->execSqlUpdate("UPDATE content_file SET filename ='".$file_name."' WHERE files_id='".$this->getId()."'", false); 
    288         // Touch and refresh this object 
    289         $this->touch(); 
    290     } 
    291  
    292     /** 
    293      * Returns the size of the file 
    294      * 
    295      * @param string $unit Name of constant of which kind of filesize unit 
    296      *                     to use 
    297      *                     Possibilities: 
    298      *                       + self::UNIT_BYTES 
    299      *                       + self::UNIT_KILOBYTES 
    300      *                       + self::MEGABYTES 
    301      *                       + self::GIGABYTES 
    302      * 
    303      * @return float Size of file 
    304      */ 
    305     protected function getFileSize($unit = self::UNIT_BYTES) 
    306     { 
    307         if ($this->isLocalFile()) { 
    308             $size = $this->files_row['local_binary_size']; 
    309         } else { 
    310             $size = $this->files_row['remote_size']; 
    311         } 
    312  
    313         switch ($unit) { 
    314         case self::UNIT_KILOBYTES: 
    315         case self::UNIT_MEGABYTES: 
    316         case self::UNIT_GIGABYTES: 
    317         case self::UNIT_BYTES: 
    318             $size = round($size / $unit, 2); 
    319             break; 
    320  
    321         } 
    322  
    323         return $size; 
    324     } 
    325  
    326     /** 
    327      * Sets the size of a local file 
    328      * 
    329      * @param int    $size Size of file 
    330      * @param string $unit Name of constant of which kind of filesize unit 
    331      *                     to use 
    332      *                     Possibilities: 
    333      *                       + self::UNIT_BYTES 
    334      *                       + self::UNIT_KILOBYTES 
    335      *                       + self::MEGABYTES 
    336      *                       + self::GIGABYTES 
    337      * 
    338      * @return void 
    339      */ 
    340     private function setLocalFileSize($size, $unit = self::UNIT_BYTES) 
    341     { 
    342         if (is_numeric($size)) { 
    343             $octet_size = $size * $unit; 
    344  
    345             $this->mBd->execSqlUpdate("UPDATE content_file SET local_binary_size = $octet_size WHERE files_id='" . $this->getId() . "'", false); 
    346             $this->refresh(); 
    347         } 
    348     } 
    349  
    350     /** 
    351      * Sets the size of a remote file 
    352      * 
    353      * @param int    $size Size of file 
    354      * @param string $unit Name of constant of which kind of filesize unit 
    355      *                     to use 
    356      *                     Possibilities: 
    357      *                       + self::UNIT_BYTES 
    358      *                       + self::UNIT_KILOBYTES 
    359      *                       + self::MEGABYTES 
    360      *                       + self::GIGABYTES 
    361      * 
    362      * @return void 
    363  
    364      */ 
    365     private function setRemoteFileSize($size, $unit = self::UNIT_KILOBYTES) 
    366     { 
    367         if (is_numeric($size)) { 
    368             $octet_size = $size * $unit; 
    369  
    370             $this->mBd->execSqlUpdate("UPDATE content_file SET remote_size = $octet_size WHERE files_id='".$this->getId()."'", false); 
    371             $this->refresh(); 
    372         } 
    373     } 
    374  
    375     /** 
    376      * Get URL of file 
    377      * 
    378      * @return string URL of file 
    379      */ 
    380     public function getFileUrl() 
    381     { 
    382         // Init values 
    383         $_retval = null; 
    384  
    385         if (!$this->isLocalFile()) { 
    386             $_retval = $this->files_row['url']; 
    387         } else { 
    388             $_retval = BASE_SSL_PATH . "file_download.php?file_id=" . $this->getId(); 
    389         } 
    390  
    391         return $_retval; 
    392     } 
    393  
    394     /** 
    395      * Sets URL of a file 
    396      * 
    397      * @param string $url 
    398      * 
    399      * @return void 
    400  
    401      */ 
    402     private function setURL($url) 
    403     { 
    404         if ($url == null) { 
    405             $url_sql = "NULL"; 
    406         } else { 
    407             $url_sql = "'".$this->mBd->escapeString($url)."'"; 
    408         } 
    409                 if($this->files_row['url']!=$url){ 
    410          
    411         $this->mBd->execSqlUpdate("UPDATE content_file SET url = $url_sql WHERE files_id='".$this->getId()."'", false); 
    412         $this->refresh(); 
    413                 } 
    414     } 
    415  
    416     /** 
    417      * Returns if file is a local file 
    418      * 
    419      * @return bool True if file is local 
    420      */ 
    421     protected function isLocalFile() 
    422     { 
    423         return is_null($this->files_row['url']); 
    424     } 
    425  
    426     /** 
    427      * Shows the administration interface for RssAggregator. 
    428      * 
    429      * @param string $subclass_admin_interface HTML code to be added after the 
    430      *                                         administration interface 
    431      * 
    432      * @return string HTML code for the administration interface 
    433      */ 
    434     public function getAdminUI($subclass_admin_interface = null, $title=null) 
    435     { 
    436         // Init values 
    437         $html = ''; 
    438  
    439         $html .= "<li class='admin_element_item_container'>\n"; 
    440         $html .= "<div class='admin_element_data'>\n"; 
    441         $html .= "<input type='radio' name='file_mode".$this->getId()."' value='by_upload' ". ($this->isLocalFile() ? "CHECKED" : "")." />"; 
    442         $html .= _("Upload a new file (Uploading a new one will replace any existing file)")." : \n"; 
    443         $html .= '<input type="hidden" name="MAX_FILE_SIZE" value="1073741824" />'; 
    444         $html .= '<input name="file_file_upload'.$this->getId().'" type="file" />'; 
    445         $html .= "</div>\n"; 
    446         $html .= "</li>\n"; 
    447  
    448         $html .= "<li class='admin_element_item_container'>\n"; 
    449         $html .= "<div class='admin_element_data'>\n"; 
    450         $html .= "<input type='radio' name='file_mode".$this->getId()."' value='remote' ". (!$this->isLocalFile() ? "CHECKED" : "").">"; 
    451         $html .= _("Remote file via URL")." : \n"; 
    452  
    453  
    454         if ($this->isLocalFile()) { 
    455             $html .= "<input name='file_url".$this->getId()."' type='text' size='50'/>"; 
    456         } else { 
    457             $html .= "<input name='file_url".$this->getId()."' type='text' size='50' value='".$this->getFileUrl()."'/>"; 
    458         } 
    459  
    460         $html .= "</div>\n"; 
    461         $html .= "</li>\n"; 
    462  
    463         if (!$this->isLocalFile()) { 
    464             $html .= "<div class='admin_element_item_container'>\n"; 
    465             $html .= "<div class='admin_element_label'>"._("File URL")." : </div>\n"; 
    466             $html .= "<div class='admin_element_data'>\n"; 
    467             $html .= $this->getFileUrl(); 
    468             $html .= "</div>\n"; 
    469             $html .= "</li>\n"; 
    470         } 
    471          
    472 if($this->configEnableEditFilename) { 
    473         $html .= "<li class='admin_element_item_container'>\n"; 
    474         $html .= "<div class='admin_element_label'>"._("Filename to display")." : </div>\n"; 
    475         $html .= "<div class='admin_element_data'>\n"; 
    476         $html .= '<input type="text" name="file_file_name'.$this->getId().'" value="'.$this->getFilename().'" />'; 
    477         $html .= "</div>\n"; 
    478         $html .= "</li>\n"; 
    479 } 
    480  
    481         if ($this->isLocalFile()) { 
    482                 if($this->configEnableEditMimeType){ 
    483             $html .= "<li class='admin_element_item_container'>\n"; 
    484             $html .= "<div class='admin_element_label'>"._("MIME type")." : </div>\n"; 
    485             $html .= "<div class='admin_element_data'>\n"; 
    486             $html .= '<input type="text" name="file_mime_type'.$this->getId().'" value="'.$this->getMimeType().'" />'; 
    487             $html .= "</div>\n"; 
    488             $html .= "</li>\n"; 
    489                 } 
    490  
    491             $html .= "<li class='admin_element_item_container'>\n"; 
    492             $html .= "<div class='admin_element_data'>"._("Locally stored file size")." : \n"; 
    493             $html .= $this->getFileSize(self :: UNIT_KILOBYTES)." "._("KB"); 
    494         } else { 
    495             $html .= "<div class='admin_element_item_container'>\n"; 
    496             $html .= "<div class='admin_element_label'>"._("Remote file size (Automatically converted from KB to Bytes)")." : </div>\n"; 
    497             $html .= "<div class='admin_element_data'>\n"; 
    498             // The hidden field contains old value to determine if we have to update ( this prevents unwanted successive floating point evaluation ) 
    499             $html .= '<input type="hidden" name="file_old_remote_size'.$this->getId().'" value="'.$this->getFileSize().'" />'; 
    500             $html .= '<input type="text" name="file_remote_size'.$this->getId().'" value="'.$this->getFileSize().'" />'; 
    501  
    502         } 
    503                 $html .= " <a href='".$this->getFileUrl()."'>"._("Download")."</a>\n"; 
    504         $html .= " "._("Last update")." : \n"; 
    505                     $html .= $this->getLastUpdateTimestamp(); 
    506                 $html .= "</div>\n"; 
    507             $html .= "</li>\n"; 
    508  
    509         $html .= $subclass_admin_interface; 
    510  
    511         return parent::getAdminUI($html, $title); 
    512     } 
    513  
    514     /** 
    515      * Processes the input of the administration interface for RssAggregator 
    516      * 
    517      * @return void 
    518      */ 
    519     public function processAdminUI() 
    520     { 
    521         //echo "File::processAdminUI()<br/>\n"; 
    522         if ($this->isOwner(User :: getCurrentUser()) || User :: getCurrentUser()->isSuperAdmin()) { 
    523             parent :: processAdminUI(); 
    524  
    525             // If no file was uploaded, update filename and mime type 
    526             if (!empty ($_REQUEST["file_mode".$this->getId()])) { 
    527                 if ($this->configEnableEditFilename && !empty($_REQUEST["file_file_name".$this->getId()])) { 
    528                     $this->setFilename($_REQUEST["file_file_name".$this->getId()]); 
    529                 } 
    530  
    531                 $file_mode = $_REQUEST["file_mode".$this->getId()]; 
    532  
    533                 if ($file_mode == "by_upload") { 
    534                     if($this->configEnableEditMimeType &&isset($_REQUEST["file_mime_type".$this->getId()])) { 
    535                         $this->setMimeType($_REQUEST["file_mime_type".$this->getId()]); 
    536                     } 
    537  
    538                     $this->setBinaryDataFromPostVar("file_file_upload".$this->getId()); 
    539                     $this->setURL(null); 
    540  
    541                     // Reset the remote file size ( not used ) 
    542                     $this->setRemoteFileSize(0); 
    543                 } else { 
    544                     if ($file_mode == "remote") { 
    545                         $this->setURL($_REQUEST["file_url".$this->getId()]); 
    546                         $this->setBinaryDataOid(null); 
    547  
    548                         // When switching from local to remote, this field does not exist yet 
    549                         if (isset($_REQUEST["file_old_remote_size".$this->getId()])) { 
    550                             if ($_REQUEST["file_remote_size".$this->getId()] != $_REQUEST["file_old_remote_size".$this->getId()]) { 
    551                                 $this->setRemoteFileSize($_REQUEST["file_remote_size".$this->getId()]); 
    552                             } 
    553                         } else { 
    554                             $this->setRemoteFileSize(0); 
    555                         } 
    556                     } 
    557                 } 
    558             } 
    559         } 
    560     } 
    561  
    562     /** 
    563      * Retreives the user interface of this object. 
    564      * 
    565      * @return string The HTML fragment for this interface 
    566      */ 
    567     public function getUserUI() 
    568     { 
    569         // Init values 
    570         $html = ''; 
    571  
    572         if($this->getFileSize() > 0) { 
    573             $append_size = " (".$this->getFileSize(self :: UNIT_KILOBYTES)." "._("KB").")"; 
    574         } else { 
    575             $append_size = ""; 
    576         } 
    577  
    578         $html .= "<div class='download_button'><a href='".htmlentities($this->getFileUrl())."'>"._("Download")." ".$this->getFilename()."$append_size</a></div>"; 
    579         $html = $this->replaceHyperLinks($html); 
    580         $this->setUserUIMainDisplayContent($html); 
    581         return parent::getUserUI(); 
    582     } 
    583  
    584     /** 
    585      * Reloads the object from the database. Should normally be called after 
    586      * a set operation. This function is private because calling it from a 
    587      * subclass will call the constructor from the wrong scope. 
    588      * 
    589      * @return void 
    590      */ 
    591     private function refresh() 
    592     { 
    593         $this->__construct($this->id); 
    594     } 
    595  
    596     /** 
    597      * Deletes a File object 
    598      * 
    599      * @param string $errmsg Reference to error message 
    600      * 
    601      * @return bool True if deletion was successful 
    602      * @internal Persistent content will not be deleted 
    603      */ 
    604     public function delete(&$errmsg) 
    605     { 
    606         if ($this->isPersistent() == false) { 
    607             // Unlink BLOB if any exists 
    608             $blob_oid = $this->getBinaryDataOid(); 
    609  
    610             if($blob_oid) { 
    611                 $errmsg = "Deleting BLOB OID : $blob_oid"; 
    612  
    613                 if($this->mBd->UnlinkLargeObject($blob_oid) == false) { 
    614                     $errmsg = _("Unable to successfully unlink BLOB OID : $blob_oid !"); 
    615                     return false; 
    616                 } 
    617             } 
    618  
    619             $this->mBd->execSqlUpdate("DELETE FROM content_file WHERE files_id = '".$this->getId()."'", false); 
    620         } else { 
    621             $errmsg = _("Could not delete this file, since it is persistent"); 
    622         } 
    623  
    624         return parent::delete($errmsg); 
    625     } 
     66        /** 
     67         * File size unit: Byte 
     68 
     69         */ 
     70        const UNIT_BYTES = 1; 
     71 
     72        /** 
     73         * File size unit: KB 
     74 
     75         */ 
     76        const UNIT_KILOBYTES = 1024; 
     77 
     78        /** 
     79         * File size unit: MB 
     80 
     81         */ 
     82        const UNIT_MEGABYTES = 1048576; 
     83 
     84        /** 
     85         * File size unit: GB 
     86 
     87         */ 
     88        const UNIT_GIGABYTES = 1073741824; 
     89 
     90        /** Can the user edit the filename */ 
     91        private $configEnableEditFilename = true; 
     92 
     93        /** Can the user edit the mime type */ 
     94        private $configEnableEditMimeType = true; 
     95 
     96        // Should the admin UI display metadata about the file 
     97        private $fileMetadataVerboseFlag = true; 
     98 
     99        /** 
     100         * Constructor 
     101         * 
     102         * @param string $content_id Content id 
     103         */ 
     104        protected function __construct($content_id) 
     105        { 
     106 
     107                $db = AbstractDb :: getObject(); 
     108 
     109                // Init values 
     110                $row = null; 
     111                parent :: __construct($content_id); 
     112 
     113                $content_id = $db->escapeString($content_id); 
     114                $sql = "SELECT * FROM content_file WHERE files_id='$content_id'"; 
     115                $db->execSqlUniqueRes($sql, $row, false); 
     116 
     117                if ($row == null) 
     118                { 
     119                        /* 
     120                         * Since the parent Content exists, the necessary data in 
     121                         * content_group had not yet been created 
     122                         */ 
     123                        $sql = "INSERT INTO content_file (files_id) VALUES ('$content_id')"; 
     124                        $db->execSqlUpdate($sql, false); 
     125 
     126                        $sql = "SELECT * FROM content_file WHERE files_id='$content_id'"; 
     127                        $db->execSqlUniqueRes($sql, $row, false); 
     128 
     129                        if ($row == null) 
     130                        { 
     131                                throw new Exception(_("The content with the following id could not be found in the database: ") . $content_id); 
     132                        } 
     133                } 
     134 
     135                $this->mBd = & $db; 
     136                $this->files_row = $row; 
     137        } 
     138 
     139        public function setFileMetadataVerbose($flag) { 
     140                if(is_bool($flag)) 
     141                        $this->metadataVerboseFlag = $flag; 
     142        } 
     143 
     144        /** 
     145         * Validates the uploaded file and return a boolean to tell if valid 
     146         * This method should be overridden when you need to write special validation scripts 
     147         * 
     148         * @param string path to input file 
     149         * @param string path to output file (by ref.), making sure you create a struct that matches the $_FILES[][] format 
     150         * 
     151         * @return boolean 
     152         */ 
     153        protected function validateUploadedFile($input, &$output) { 
     154                return true; 
     155        } 
     156 
     157        /** 
     158         * Set Binary data from a POST form data field 
     159         * 
     160         * @param string $upload_field The form field that contains the data 
     161         * 
     162         * @return bool True if successful 
     163 
     164         */ 
     165        private function setBinaryDataFromPostVar($upload_field) 
     166        { 
     167                // Init values 
     168                $_retval = false; 
     169                $output = null; 
     170 
     171                if (!empty ($_FILES[$upload_field]) && $_FILES[$upload_field]['error'] == UPLOAD_ERR_OK && $this->validateUploadedFile($_FILES[$upload_field], $output)) 
     172                { 
     173                        // Unlink BLOB if any exists 
     174                        $blob_oid = $this->getBinaryDataOid(); 
     175 
     176                        if ($blob_oid) 
     177                        { 
     178                                $this->mBd->unlinkLargeObject($blob_oid); 
     179                        } 
     180 
     181                        if(empty($output)) { 
     182                                // input hasn't changed during validation 
     183                                $output = $_FILES[$upload_field]; 
     184                        } 
     185 
     186                        // Updating database 
     187                        // Create a new BLOB 
     188                        $new_oid = $this->mBd->importLargeObject($output['tmp_name']); 
     189                        // Switch to new OID and touch file 
     190                        $this->setBinaryDataOid($new_oid); 
     191                        $this->setLocalFileSize($output['size']); 
     192                        $this->setMimeType($output['type']); 
     193                        $this->setFilename($output['name']); 
     194 
     195                        $_retval = true; 
     196                } 
     197                else 
     198                { 
     199                        switch ($_FILES[$upload_field]['error']) 
     200                        { 
     201                                case 'UPLOAD_ERR_INI_SIZE' : 
     202                                        echo _("File size exceeds limit specified in PHP.ini"); 
     203                                        break; 
     204 
     205                                case 'UPLOAD_ERR_FORM_SIZE' : 
     206                                        echo _("File size exceeds limit specified HTML form"); 
     207                                        break; 
     208 
     209                                case 'UPLOAD_ERR_PARTIAL' : 
     210                                        echo _("File upload was interrupted"); 
     211                                        break; 
     212 
     213                                case 'UPLOAD_ERR_NO_TMP_DIR' : 
     214                                        echo _("Missing temp folder"); 
     215                                        break; 
     216 
     217                        } 
     218                } 
     219 
     220                return $_retval; 
     221        } 
     222 
     223        /** 
     224         * Returns the binary data from the database 
     225         * 
     226         * @return string Binary data from the database 
     227 
     228         */ 
     229        private function getBinaryDataOid() 
     230        { 
     231                return $this->mBd->unescapeBinaryString($this->files_row['data_blob']); 
     232        } 
     233 
     234        /** 
     235         * Saves the binary data to the database 
     236         * 
     237         * @param string $oid Binary data 
     238         * 
     239         * @return void 
     240 
     241         */ 
     242        private function setBinaryDataOid($oid) 
     243        { 
     244                if (is_null($oid)) 
     245                { 
     246                        $oid_sql = "NULL"; 
     247                } 
     248                else 
     249                { 
     250                        $oid_sql = $this->mBd->escapeString($oid); 
     251                } 
     252                if ($this->files_row['data_blob'] != $oid) 
     253                { 
     254                        $this->mBd->execSqlUpdate("UPDATE content_file SET data_blob = $oid WHERE files_id='" . $this->getId() . "'", false); 
     255                        // Touch and refresh this object 
     256                        $this->touch(); 
     257                } 
     258        } 
     259 
     260        /** 
     261         * Is the MimeType editable 
     262         * @param $enabled Default is true 
     263         */ 
     264        protected function configEnableEditMimeType($enabled = true) 
     265        { 
     266                return $this->configEnableEditMimeType = $enabled; 
     267        } 
     268 
     269        /** 
     270         * Returns the MIME type of the file 
     271         * 
     272         * @return string MIME type of file 
     273         */ 
     274        public function getMimeType() 
     275        { 
     276                return $this->files_row['mime_type']; 
     277        } 
     278 
     279        /** 
     280         * Saves the MIME type of the file 
     281         * 
     282         * @param string $mime_type 
     283         * 
     284         * @return void 
     285         */ 
     286        private function setMimeType($mime_type) 
     287        { 
     288                $mime_type = $this->mBd->escapeString($mime_type); 
     289                $this->mBd->execSqlUpdate("UPDATE content_file SET mime_type ='" . $mime_type . "' WHERE files_id='" . $this->getId() . "'", false); 
     290                // Touch and refresh this object 
     291                $this->touch(); 
     292        } 
     293 
     294        /** 
     295         * Is the filename editable 
     296         * @param $enabled Default is true 
     297         */ 
     298        protected function configEnableEditFilename($enabled = true) 
     299        { 
     300                return $this->configEnableEditFilename = $enabled; 
     301        } 
     302 
     303        /** 
     304         * Returns the filename of the file 
     305         * 
     306         * @return string Filename of file 
     307 
     308         */ 
     309        protected function getFilename() 
     310        { 
     311                return $this->files_row['filename']; 
     312        } 
     313 
     314        /** 
     315         * Stores the filename of the file 
     316         * 
     317         * @param string $file_name Filename of the file 
     318         * 
     319         * @return void 
     320         */ 
     321        private function setFilename($file_name) 
     322        { 
     323                $file_name = $this->mBd->escapeString($file_name); 
     324                $this->mBd->execSqlUpdate("UPDATE content_file SET filename ='" . $file_name . "' WHERE files_id='" . $this->getId() . "'", false); 
     325                // Touch and refresh this object 
     326                $this->touch(); 
     327        } 
     328 
     329        /** 
     330         * Returns the size of the file 
     331         * 
     332         * @param string $unit Name of constant of which kind of filesize unit 
     333         *                     to use 
     334         *                     Possibilities: 
     335         *                       + self::UNIT_BYTES 
     336         *                       + self::UNIT_KILOBYTES 
     337         *                       + self::MEGABYTES 
     338         *                       + self::GIGABYTES 
     339         * 
     340         * @return float Size of file 
     341         */ 
     342        protected function getFileSize($unit = self :: UNIT_BYTES) 
     343        { 
     344                if ($this->isLocalFile()) 
     345                { 
     346                        $size = $this->files_row['local_binary_size']; 
     347                } 
     348                else 
     349                { 
     350                        $size = $this->files_row['remote_size']; 
     351                } 
     352 
     353                switch ($unit) 
     354                { 
     355                        case self :: UNIT_KILOBYTES : 
     356                        case self :: UNIT_MEGABYTES : 
     357                        case self :: UNIT_GIGABYTES : 
     358                        case self :: UNIT_BYTES : 
     359                                $size = round($size / $unit, 2); 
     360                                break; 
     361 
     362                } 
     363 
     364                return $size; 
     365        } 
     366 
     367        /** 
     368         * Sets the size of a local file 
     369         * 
     370         * @param int    $size Size of file 
     371         * @param string $unit Name of constant of which kind of filesize unit 
     372         *                     to use 
     373         *                     Possibilities: 
     374         *                       + self::UNIT_BYTES 
     375         *                       + self::UNIT_KILOBYTES 
     376         *                       + self::MEGABYTES 
     377         *                       + self::GIGABYTES 
     378         * 
     379         * @return void 
     380         */ 
     381        private function setLocalFileSize($size, $unit = self :: UNIT_BYTES) 
     382        { 
     383                if (is_numeric($size)) 
     384                { 
     385                        $octet_size = $size * $unit; 
     386 
     387                        $this->mBd->execSqlUpdate("UPDATE content_file SET local_binary_size = $octet_size WHERE files_id='" . $this->getId() . "'", false); 
     388                        $this->refresh(); 
     389                } 
     390        } 
     391 
     392        /** 
     393         * Sets the size of a remote file 
     394         * 
     395         * @param int    $size Size of file 
     396         * @param string $unit Name of constant of which kind of filesize unit 
     397         *                     to use 
     398         *                     Possibilities: 
     399         *                       + self::UNIT_BYTES 
     400         *                       + self::UNIT_KILOBYTES 
     401         *                       + self::MEGABYTES 
     402         *                       + self::GIGABYTES 
     403         * 
     404         * @return void 
     405 
     406         */ 
     407        private function setRemoteFileSize($size, $unit = self :: UNIT_KILOBYTES) 
     408        { 
     409                if (is_numeric($size)) 
     410                { 
     411                        $octet_size = $size * $unit; 
     412 
     413                        $this->mBd->execSqlUpdate("UPDATE content_file SET remote_size = $octet_size WHERE files_id='" . $this->getId() . "'", false); 
     414                        $this->refresh(); 
     415                } 
     416        } 
     417 
     418        /** 
     419         * Get URL of file 
     420         * 
     421         * @return string URL of file 
     422         */ 
     423        public function getFileUrl() 
     424        { 
     425                // Init values 
     426                $_retval = null; 
     427 
     428                if (!$this->isLocalFile()) 
     429                { 
     430                        $_retval = $this->files_row['url']; 
     431                } 
     432                else 
     433                { 
     434                        $_retval = BASE_SSL_PATH . "file_download.php?file_id=" . $this->getId(); 
     435                } 
     436 
     437                return $_retval; 
     438        } 
     439 
     440        /** 
     441         * Sets URL of a file 
     442         * 
     443         * @param string $url 
     444         * 
     445         * @return void 
     446 
     447         */ 
     448        private function setURL($url) 
     449        { 
     450                if ($url == null) 
     451                { 
     452                        $url_sql = "NULL"; 
     453                } 
     454                else 
     455                { 
     456                        $url_sql = "'" . $this->mBd->escapeString($url) . "'"; 
     457                } 
     458                if ($this->files_row['url'] != $url) 
     459                { 
     460 
     461                        $this->mBd->execSqlUpdate("UPDATE content_file SET url = $url_sql WHERE files_id='" . $this->getId() . "'", false); 
     462                        $this->refresh(); 
     463                } 
     464        } 
     465 
     466        /** 
     467         * Returns if file is a local file 
     468         * 
     469         * @return bool True if file is local 
     470         */ 
     471        protected function isLocalFile() 
     472        { 
     473                return is_null($this->files_row['url']); 
     474        } 
     475 
     476        /** 
     477         * Shows the administration interface for RssAggregator. 
     478         * 
     479         * @param string $subclass_admin_interface HTML code to be added after the 
     480         *                                         administration interface 
     481         * 
     482         * @return string HTML code for the administration interface 
     483         */ 
     484        public function getAdminUI($subclass_admin_interface = null, $title = null) 
     485        { 
     486                // Init values 
     487                $html = ''; 
     488 
     489                $html .= "<li class='admin_element_item_container'>\n"; 
     490                $html .= "<div class='admin_element_data'>\n"; 
     491                $html .= "<input type='radio' name='file_mode" . $this->getId() . "' value='by_upload' " . ($this->isLocalFile() ? "CHECKED" : "") . " />"; 
     492                $html .= _("Upload a new file (Uploading a new one will replace any existing file)") . " : \n"; 
     493                $html .= '<input type="hidden" name="MAX_FILE_SIZE" value="1073741824" />'; 
     494                $html .= '<input name="file_file_upload' . $this->getId() . '" type="file" />'; 
     495                $html .= "</div>\n"; 
     496                $html .= "</li>\n"; 
     497 
     498                $html .= "<li class='admin_element_item_container'>\n"; 
     499                $html .= "<div class='admin_element_data'>\n"; 
     500                $html .= "<input type='radio' name='file_mode" . $this->getId() . "' value='remote' " . (!$this->isLocalFile() ? "CHECKED" : "") . ">"; 
     501                $html .= _("Remote file via URL") . " : \n"; 
     502 
     503                if ($this->isLocalFile()) 
     504                { 
     505                        $html .= "<input name='file_url" . $this->getId() . "' type='text' size='50'/>"; 
     506                } 
     507                else 
     508                { 
     509                        $html .= "<input name='file_url" . $this->getId() . "' type='text' size='50' value='" . $this->getFileUrl() . "'/>"; 
     510                } 
     511 
     512                $html .= "</div>\n"; 
     513                $html .= "</li>\n"; 
     514 
     515                if (!$this->isLocalFile()) 
     516                { 
     517                        $html .= "<div class='admin_element_item_container'>\n"; 
     518                        $html .= "<div class='admin_element_label'>" . _("File URL") . " : </div>\n"; 
     519                        $html .= "<div class='admin_element_data'>\n"; 
     520                        $html .= $this->getFileUrl(); 
     521                        $html .= "</div>\n"; 
     522                        $html .= "</li>\n"; 
     523                } 
     524 
     525                if ($this->configEnableEditFilename) 
     526                { 
     527                        $html .= "<li class='admin_element_item_container'>\n"; 
     528                        $html .= "<div class='admin_element_label'>" . _("Filename to display") . " : </div>\n"; 
     529                        $html .= "<div class='admin_element_data'>\n"; 
     530                        $html .= '<input type="text" name="file_file_name' . $this->getId() . '" value="' . $this->getFilename() . '" />'; 
     531                        $html .= "</div>\n"; 
     532                        $html .= "</li>\n"; 
     533                } 
     534 
     535                if ($this->isLocalFile()) 
     536                { 
     537                        if ($this->configEnableEditMimeType) 
     538                        { 
     539                                $html .= "<li class='admin_element_item_container'>\n"; 
     540                                $html .= "<div class='admin_element_label'>" . _("MIME type") . " : </div>\n"; 
     541                                $html .= "<div class='admin_element_data'>\n"; 
     542                                $html .= '<input type="text" name="file_mime_type' . $this->getId() . '" value="' . $this->getMimeType() . '" />'; 
     543                                $html .= "</div>\n"; 
     544                                $html .= "</li>\n"; 
     545                        } 
     546 
     547                        //$html .= "<li class='admin_element_item_container'>\n"; 
     548                        if($this->metadataVerboseFlag == true) { 
     549                                $html .= "<div class='admin_element_data'>" . _("Locally stored file size") . " : \n"; 
     550                                $html .= $this->getFileSize(self :: UNIT_KILOBYTES) . " " . _("KB"); 
     551                        } 
     552                } 
     553                else 
     554                { 
     555                        $html .= "<div class='admin_element_item_container'>\n"; 
     556                        $html .= "<div class='admin_element_label'>" . _("Remote file size (Automatically converted from KB to Bytes)") . " : </div>\n"; 
     557                        $html .= "<div class='admin_element_data'>\n"; 
     558                        // The hidden field contains old value to determine if we have to update ( this prevents unwanted successive floating point evaluation ) 
     559                        $html .= '<input type="hidden" name="file_old_remote_size' . $this->getId() . '" value="' . $this->getFileSize() . '" />'; 
     560                        $html .= '<input type="text" name="file_remote_size' . $this->getId() . '" value="' . $this->getFileSize() . '" />'; 
     561 
     562                } 
     563 
     564                if($this->metadataVerboseFlag == true) { 
     565                        $html .= " <a href='" . $this->getFileUrl() . "'>" . _("Download") . "</a>\n"; 
     566                        $html .= " " . _("Last update") . " : \n"; 
     567                        $html .= $this->getLastUpdateTimestamp(); 
     568                        $html .= "</div>\n"; 
     569                } 
     570                $html .= "</li>\n"; 
     571 
     572                $html .= $subclass_admin_interface; 
     573 
     574                return parent :: getAdminUI($html, $title); 
     575        } 
     576 
     577        /** 
     578         * Processes the input of the administration interface for RssAggregator 
     579         * 
     580         * @return void 
     581         */ 
     582        public function processAdminUI() 
     583        { 
     584                //echo "File::processAdminUI()<br/>\n"; 
     585                if ($this->isOwner(User :: getCurrentUser()) || User :: getCurrentUser()->isSuperAdmin()) 
     586                { 
     587                        parent :: processAdminUI(); 
     588 
     589                        // If no file was uploaded, update filename and mime type 
     590                        if (!empty ($_REQUEST["file_mode" . $this->getId()])) 
     591                        { 
     592                                if ($this->configEnableEditFilename && !empty ($_REQUEST["file_file_name" . $this->getId()])) 
     593                                { 
     594                                        $this->setFilename($_REQUEST["file_file_name" . $this->getId()]); 
     595                                } 
     596 
     597                                $file_mode = $_REQUEST["file_mode" . $this->getId()]; 
     598 
     599                                if ($file_mode == "by_upload") 
     600                                { 
     601                                        if ($this->configEnableEditMimeType && isset ($_REQUEST["file_mime_type" . $this->getId()])) 
     602                                        { 
     603                                                $this->setMimeType($_REQUEST["file_mime_type" . $this->getId()]); 
     604                                        } 
     605 
     606                                        $this->setBinaryDataFromPostVar("file_file_upload" . $this->getId()); 
     607                                        $this->setURL(null); 
     608 
     609                                        // Reset the remote file size ( not used ) 
     610                                        $this->setRemoteFileSize(0); 
     611                                } 
     612                                else 
     613                                { 
     614                                        if ($file_mode == "remote") 
     615                                        { 
     616                                                $this->setURL($_REQUEST["file_url" . $this->getId()]); 
     617                                                $this->setBinaryDataOid(null); 
     618 
     619                                                // When switching from local to remote, this field does not exist yet 
     620                                                if (isset ($_REQUEST["file_old_remote_size" . $this->getId()])) 
     621                                                { 
     622                                                        if ($_REQUEST["file_remote_size" . $this->getId()] != $_REQUEST["file_old_remote_size" . $this->getId()]) 
     623                                                        { 
     624                                                                $this->setRemoteFileSize($_REQUEST["file_remote_size" . $this->getId()]); 
     625                                                        } 
     626                                                } 
     627                                                else 
     628                                                { 
     629                                                        $this->setRemoteFileSize(0); 
     630                                                } 
     631                                        } 
     632                                } 
     633                        } 
     634                } 
     635        } 
     636 
     637        /** 
     638         * Retreives the user interface of this object. 
     639         * 
     640         * @return string The HTML fragment for this interface 
     641         */ 
     642        public function getUserUI() 
     643        { 
     644                // Init values 
     645                $html = ''; 
     646 
     647                if ($this->getFileSize() > 0) 
     648                { 
     649                        $append_size = " (" . $this->getFileSize(self :: UNIT_KILOBYTES) . " " . _("KB") . ")"; 
     650                } 
     651                else 
     652                { 
     653                        $append_size = ""; 
     654                } 
     655 
     656                $html .= "<div class='download_button'><a href='" . htmlentities($this->getFileUrl()) . "'>" . _("Download") . " " . $this->getFilename() . "$append_size</a></div>"; 
     657                $html = $this->replaceHyperLinks($html); 
     658                $this->setUserUIMainDisplayContent($html); 
     659                return parent :: getUserUI(); 
     660        } 
     661 
     662        /** 
     663         * Reloads the object from the database. Should normally be called after 
     664         * a set operation. This function is private because calling it from a 
     665         * subclass will call the constructor from the wrong scope. 
     666         * 
     667         * @return void 
     668         */ 
     669        private function refresh() 
     670        { 
     671                $this->__construct($this->id); 
     672        } 
     673 
     674        /** 
     675         * Deletes a File object 
     676         * 
     677         * @param string $errmsg Reference to error message 
     678         * 
     679         * @return bool True if deletion was successful 
     680         * @internal Persistent content will not be deleted 
     681         */ 
     682        public function delete(& $errmsg) 
     683        { 
     684                if ($this->isPersistent() == false) 
     685                { 
     686                        // Unlink BLOB if any exists 
     687                        $blob_oid = $this->getBinaryDataOid(); 
     688 
     689                        if ($blob_oid) 
     690                        { 
     691                                $errmsg = "Deleting BLOB OID : $blob_oid"; 
     692 
     693                                if ($this->mBd->UnlinkLargeObject($blob_oid) == false) 
     694                                { 
     695                                        $errmsg = _("Unable to successfully unlink BLOB OID : $blob_oid !"); 
     696                                        return false; 
     697                                } 
     698                        } 
     699 
     700                        $this->mBd->execSqlUpdate("DELETE FROM content_file WHERE files_id = '" . $this->getId() . "'", false); 
     701                } 
     702                else 
     703                { 
     704                        $errmsg = _("Could not delete this file, since it is persistent"); 
     705                } 
     706 
     707                return parent :: delete($errmsg); 
     708        } 
    626709 
    627710} 
     
    634717 * End: 
    635718 */ 
    636  
    637