Show
Ignore:
Timestamp:
04/21/05 10:58:29 (8 years ago)
Author:
fproulx
Message:

2005-04-21 Fran�ois Proulx <francois.proulx@…>

  • Added explicit admin UI exceptions support for Flickr
  • Completed File and Picture objects
Files:
1 modified

Legend:

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

    r559 r560  
    2525*/ 
    2626 
    27 require_once BASEPATH.'classes/FormSelectGenerator.php'; 
    2827require_once BASEPATH.'classes/Content.php'; 
    2928 
     
    3433class File extends Content 
    3534{ 
    36     /* File size units */ 
    37     const UNIT_BYTES = 1; 
    38     const UNIT_KILOBYTES = 1024; 
    39     const UNIT_MEGABYTES = 1048576; 
    40     const UNIT_GIGABYTES = 1073741824; 
    41      
     35        /* File size units */ 
     36        const UNIT_BYTES = 1; 
     37        const UNIT_KILOBYTES = 1024; 
     38        const UNIT_MEGABYTES = 1048576; 
     39        const UNIT_GIGABYTES = 1073741824; 
     40 
    4241        /**Constructeur 
    4342        @param $content_id Content id 
     
    4645        { 
    4746                parent :: __construct($content_id); 
    48         $this->setIsPersistent(false); 
    49         $this->setIsTrivialContent(true); 
     47                $this->setIsPersistent(false); 
     48                $this->setIsTrivialContent(true); 
    5049                global $db; 
    5150 
    5251                $content_id = $db->EscapeString($content_id); 
    53                 $sql = "SELECT files_id, filename, mime_type, url, octet_length(binary_data) AS size FROM files WHERE files_id='$content_id'"; 
     52                $sql = "SELECT files_id, filename, mime_type, url, octet_length(binary_data) AS local_binary_size, remote_size FROM files WHERE files_id='$content_id'"; 
    5453                $db->ExecSqlUniqueRes($sql, $row, false); 
    5554                if ($row == null) 
     
    5958                        $db->ExecSqlUpdate($sql, false); 
    6059 
    61                         $sql = "SELECT files_id, filename, mime_type, url, octet_length(binary_data) AS size FROM files WHERE files_id='$content_id'"; 
     60                        $sql = "SELECT files_id, filename, mime_type, url, octet_length(binary_data) AS local_binary_size, remote_size FROM files WHERE files_id='$content_id'"; 
    6261                        $db->ExecSqlUniqueRes($sql, $row, false); 
    6362                        if ($row == null) 
     
    7170        } 
    7271 
    73     /** 
    74     * Set Binary data from a POST form data field 
    75     * @param string $upload_field The form field that contains the data 
    76     *  
    77     */ 
     72        /** 
     73        * Set Binary data from a POST form data field 
     74        * @param string $upload_field The form field that contains the data 
     75        *  
     76        */ 
    7877        function setBinaryDataFromPostVar($upload_field) 
    7978        { 
     
    8786                        // Updating database 
    8887                        $this->setBinaryData($buffer); 
    89             $this->setMimeType($_FILES[$upload_field]['type']); 
    90             $this->setFilename($_FILES[$upload_field]['name']); 
    91             $this->refresh(); 
    92             return true; 
     88                        $this->setMimeType($_FILES[$upload_field]['type']); 
     89                        $this->setFilename($_FILES[$upload_field]['name']); 
     90                        $this->refresh(); 
     91                        return true; 
    9392                } 
    9493                else 
     
    107106        function setBinaryData($data) 
    108107        { 
    109                 $data = $this->mBd->EscapeBinaryString($data); 
    110                 $this->mBd->ExecSqlUpdate("UPDATE files SET binary_data ='".$data."' WHERE files_id='".$this->getId()."'", false); 
     108        if($data == null) 
     109            $data = "NULL"; 
     110        else 
     111                  $data = "'".$this->mBd->EscapeBinaryString($data)."'"; 
     112                $this->mBd->ExecSqlUpdate("UPDATE files SET binary_data = $data WHERE files_id='".$this->getId()."'", false); 
    111113                $this->refresh(); 
    112114        } 
     
    135137                $this->refresh(); 
    136138        } 
     139 
     140        function getFileSize($unit = self :: UNIT_BYTES) 
     141        { 
     142        if($this->isLocalFile()) 
     143            $size = $this->files_row['local_binary_size']; 
     144        else 
     145            $size = $this->files_row['remote_size']; 
     146         
     147                switch ($unit) 
     148                { 
     149                        case self :: UNIT_KILOBYTES; 
     150                        case self :: UNIT_MEGABYTES : 
     151                        case self :: UNIT_GIGABYTES : 
     152                        case self :: UNIT_BYTES : 
     153                                return round($size / $unit, 2); 
     154                        default : 
     155                                return $size; 
     156                                break; 
     157                } 
     158        } 
    137159     
    138     function getFileSize($unit = self::UNIT_BYTES) 
     160    function setRemoteFileSize($size, $unit = self::UNIT_KILOBYTES) 
    139161    { 
    140         switch($unit) 
     162        if(is_numeric($size)) 
    141163        { 
    142             case self::UNIT_KILOBYTES; 
    143             case self::UNIT_MEGABYTES: 
    144             case self::UNIT_GIGABYTES: 
    145             case self::UNIT_BYTES: 
    146                 return round($this->files_row['size'] / $unit, 2); 
    147             default: 
    148                 return $this->files_row['size']; 
    149                 break; 
     164            $octet_size = $size * $unit; 
     165            $this->mBd->execSqlUpdate("UPDATE files SET remote_size = $octet_size WHERE files_id='".$this->getId()."'", false); 
     166            $this->refresh(); 
    150167        } 
    151168    } 
    152      
    153     function getFileUrl() 
    154     { 
    155         //TODO: build local url + file generator 
    156         if(!isLocalFile()) 
    157             return $this->files_row['url']; 
    158         else 
    159             return "http://"; 
    160     } 
    161      
    162     function setURL($url) 
    163     { 
    164         $url = $this->mBd->EscapeString($url); 
    165         $this->mBd->execSqlUpdate("UPDATE files SET url = '$url' WHERE files_id='".$this->getId()."'", false); 
    166         $this->refresh(); 
    167     } 
    168      
    169     function isLocalFile() 
    170     { 
    171         return !empty($this->files_row['url']); 
    172     } 
     169 
     170        function getFileUrl() 
     171        { 
     172                if (!$this->isLocalFile()) 
     173                        return $this->files_row['url']; 
     174                else 
     175                        return BASE_SSL_PATH."file_download.php?file_id=".$this->getId(); 
     176        } 
     177 
     178        function setURL($url) 
     179        { 
     180        if($url == null) 
     181            $url = "NULL"; 
     182                else 
     183            $url = "'".$this->mBd->EscapeString($url)."'"; 
     184                $this->mBd->execSqlUpdate("UPDATE files SET url = $url WHERE files_id='".$this->getId()."'", false); 
     185                $this->refresh(); 
     186        } 
     187 
     188        function isLocalFile() 
     189        { 
     190                return is_null($this->files_row['url']); 
     191        } 
    173192 
    174193        /**Affiche l'interface d'administration de l'objet */ 
    175         function getAdminUI() 
     194        function getAdminUI($subclass_admin_interface = null) 
    176195        { 
    177196                $html = ''; 
    178197                $html .= "<div class='admin_class'>File (".get_class($this)." instance)</div>\n"; 
     198 
     199                $html .= "<div class='admin_section_container'>\n"; 
     200                $html .= "<div class='admin_section_title'>"; 
     201                $html .= "<input type='radio' name='file_mode".$this->getId()."' value='by_upload' ". ($this->isLocalFile() ? "CHECKED" : "").">"; 
     202                $html .= _("Upload a new file (Uploading a new one will replace any existing file)")." : </div>\n"; 
     203                $html .= "<div class='admin_section_data'>\n"; 
     204                $html .= '<input type="hidden" name="MAX_FILE_SIZE" value="1073741824" />'; 
     205                $html .= '<input name="file_file_upload'.$this->getId().'" type="file" />'; 
     206                $html .= "</div>\n"; 
     207                $html .= "</div>\n"; 
     208 
     209                $html .= "<div class='admin_section_container'>\n"; 
     210                $html .= "<div class='admin_section_title'>"; 
     211                $html .= "<input type='radio' name='file_mode".$this->getId()."' value='remote' ". (!$this->isLocalFile() ? "CHECKED" : "").">"; 
     212                $html .= _("Remote file via URL")." : </div>\n"; 
     213                $html .= "<div class='admin_section_data'>\n"; 
     214        if($this->isLocalFile()) 
     215            $html .= "<input name='file_url".$this->getId()."' type='text' size='50'/>"; 
     216        else 
     217            $html .= "<input name='file_url".$this->getId()."' type='text' size='50' value='".$this->getFileUrl()."'/>"; 
     218                $html .= "</div>\n"; 
     219                $html .= "</div>\n"; 
    179220         
    180                 $html .= "<div class='admin_section_container'>\n"; 
    181         $html .= "<div class='admin_section_title'>"; 
    182         $html .= "<input type='radio' name='file_by_upload".$this->getId()."' value='true' ".(!$this->isLocalFile()?"CHECKED":"").">"; 
    183         $html .= _("Upload a new file (This will replace any existing file)")." : </div>\n"; 
    184         $html .= "<div class='admin_section_data'>\n"; 
    185         $html .= '<input type="hidden" name="MAX_FILE_SIZE" value="1073741824" />'; 
    186         $html .= '<input name="file_file_upload'.$this->getId().'" type="file" />'; 
    187                 $html .= "</div>\n"; 
    188         $html .= "</div>\n"; 
     221        if (!$this->isLocalFile()) 
     222        { 
     223            $html .= "<div class='admin_section_container'>\n"; 
     224            $html .= "<div class='admin_section_title'>"._("File URL")." : </div>\n"; 
     225            $html .= "<div class='admin_section_data'>\n"; 
     226            $html .= $this->getFileUrl(); 
     227            $html .= "</div>\n"; 
     228            $html .= "</div>\n"; 
     229        } 
     230 
     231        $html .= "<div class='admin_section_container'>\n"; 
     232                $html .= "<div class='admin_section_title'>"._("Filename to display")." : </div>\n"; 
     233                $html .= "<div class='admin_section_data'>\n"; 
     234                $html .= '<input type="text" name="file_file_name'.$this->getId().'" value="'.$this->getFilename().'" />'; 
     235                $html .= "</div>\n"; 
     236                $html .= "</div>\n"; 
     237 
     238                if ($this->isLocalFile()) 
     239        { 
     240            $html .= "<div class='admin_section_container'>\n"; 
     241                $html .= "<div class='admin_section_title'>"._("MIME type")." : </div>\n"; 
     242                $html .= "<div class='admin_section_data'>\n"; 
     243                $html .= '<input type="text" name="file_mime_type'.$this->getId().'" value="'.$this->getMimeType().'" />'; 
     244                $html .= "</div>\n"; 
     245                $html .= "</div>\n"; 
     246 
     247                        $html .= "<div class='admin_section_container'>\n"; 
     248                        $html .= "<div class='admin_section_title'>"._("Locally stored file size")." : </div>\n"; 
     249                        $html .= "<div class='admin_section_data'>\n"; 
     250                        $html .= $this->getFileSize(self :: UNIT_KILOBYTES)." "._("KB"); 
     251                        $html .= "</div>\n"; 
     252                        $html .= "</div>\n"; 
     253                } 
     254                else 
     255                { 
     256                        $html .= "<div class='admin_section_container'>\n"; 
     257            $html .= "<div class='admin_section_title'>"._("Remote file size")." : </div>\n"; 
     258            $html .= "<div class='admin_section_data'>\n"; 
     259            // The hidden field contains old value to determine if we have to update ( this prevents unwanted successive floating point evaluation ) 
     260            $html .= '<input type="hidden" name="file_old_remote_size'.$this->getId().'" value="'.$this->getFileSize().'" />'; 
     261            $html .= '<input type="text" name="file_remote_size'.$this->getId().'" value="'.$this->getFileSize().'" />'; 
     262            $html .= "</div>\n"; 
     263            $html .= "</div>\n"; 
     264                } 
    189265         
    190266        $html .= "<div class='admin_section_container'>\n"; 
    191         $html .= "<div class='admin_section_title'>"; 
    192         $html .= "<input type='radio' name='file_by_url".$this->getId()."' value='true' ".($this->isLocalFile()?"CHECKED":"").">"; 
    193         $html .= _("Remote file via URL")." : </div>\n"; 
    194267        $html .= "<div class='admin_section_data'>\n"; 
    195         $html .= "<input name='file_url".$this->getId()."' type='text' size='50'/>"; 
     268        $html .= "<a href='".$this->getFileUrl()."'>"._("Download")." ".$this->getFilename()." (".$this->getFileSize(self::UNIT_KILOBYTES)." "._("KB").")</a>"; 
    196269        $html .= "</div>\n"; 
    197270        $html .= "</div>\n"; 
    198271         
    199         $html .= "<div class='admin_section_container'>\n"; 
    200         $html .= "<div class='admin_section_title'>"._("Filename")." : </div>\n"; 
    201         $html .= "<div class='admin_section_data'>\n"; 
    202         $html .= '<input type="text" name="file_file_name'.$this->getId().'" value="'.$this->getFilename().'" />'; 
    203         $html .= "</div>\n"; 
    204         $html .= "</div>\n"; 
    205          
    206         $html .= "<div class='admin_section_container'>\n"; 
    207         $html .= "<div class='admin_section_title'>"._("MIME type")." : </div>\n"; 
    208         $html .= "<div class='admin_section_data'>\n"; 
    209         $html .= '<input type="text" name="file_mime_type'.$this->getId().'" value="'.$this->getMimeType().'" />'; 
    210         $html .= "</div>\n"; 
    211         $html .= "</div>\n"; 
    212          
    213         if($this->isLocalFile()) 
     272        $html .= $subclass_admin_interface; 
     273                return parent :: getAdminUI($html); 
     274        } 
     275 
     276        function processAdminUI() 
     277        { 
     278                parent :: processAdminUI(); 
     279                // If no file was uploaded, update filename and mime type 
     280        if(!empty($_REQUEST["file_mode".$this->getId()])) 
    214281        { 
    215             $html .= "<div class='admin_section_container'>\n"; 
    216             $html .= "<div class='admin_section_title'>"._("File size")." : </div>\n"; 
    217             $html .= "<div class='admin_section_data'>\n"; 
    218             $html .= $this->getFileSize(self::UNIT_KILOBYTES)." "._("KB"); 
    219             $html .= "</div>\n"; 
    220             $html .= "</div>\n"; 
     282            $file_mode = $_REQUEST["file_mode".$this->getId()]; 
     283                if ($file_mode == "by_upload") 
     284                { 
     285                        $this->setBinaryDataFromPostVar("file_file_upload".$this->getId()); 
     286                        $this->setURL(null); 
     287                // Reset the remote file size ( not used ) 
     288                $this->setRemoteFileSize(0); 
     289                } 
     290                else 
     291                { 
     292                        if ($file_mode == "remote") 
     293                        { 
     294                                $this->setURL($_REQUEST["file_url".$this->getId()]); 
     295                                $this->setBinaryData(null); 
     296                    // When switching from local to remote, this field does not exist yet 
     297                    if(!empty($_REQUEST["file_old_remote_size".$this->getId()])) 
     298                    { 
     299                        if($_REQUEST["file_remote_size".$this->getId()] != $_REQUEST["file_old_remote_size".$this->getId()]) 
     300                            $this->setRemoteFileSize($_REQUEST["file_remote_size".$this->getId()]); 
     301                    } 
     302                    else 
     303                        $this->setRemoteFileSize(0); 
     304                        } 
     305                        $this->setFilename($_REQUEST["file_file_name".$this->getId()]); 
     306                } 
    221307        } 
    222         else 
    223         { 
    224             //TODO: implement user defined size 
    225         } 
    226          
    227                 return parent :: getAdminUI($html); 
    228         } 
    229  
    230         function processAdminUI() 
    231         { 
    232                 parent :: processAdminUI(); 
    233          
    234         // If no file was uploaded, update filename and mime type 
    235         if(!empty($_REQUEST["file_by_upload".$this->getId()])) 
    236         { 
    237             $this->setBinaryDataFromPostVar("file_file_upload".$this->getId()); 
    238         } 
    239         else 
    240             if(!empty($_REQUEST["file_url".$this->getId()])) 
    241             { 
    242                 $this->setURL($_REQUEST["file_url".$this->getId()]); 
    243             } 
    244         $this->setMimeType($_REQUEST["file_mime_type".$this->getId()]); 
    245         $this->setFilename($_REQUEST["file_file_name".$this->getId()]); 
    246         } 
    247  
    248         /**Affiche l'interface usager de l'objet 
    249                 */ 
     308        } 
    250309 
    251310        /** Retreives the user interface of this object.  Anything that overrides this method should call the parent method with it's output at the END of processing. 
     
    256315                $html = ''; 
    257316                $html .= "<div class='user_ui_container'>\n"; 
    258                 $html .= "<div class='user_ui_object_class'>Langstring (".get_class($this)." instance)</div>\n"; 
    259                 $html .= "<a href='".$this->getFileUrl()."'>"._("Download this file")." (".$this->getFileSize(UNIT_KILOBYTES)." "._("KB").")</a>"; 
     317                $html .= "<div class='user_ui_object_class'>File (".get_class($this)." instance)</div>\n"; 
     318                $html .= "<a href='".htmlentities($this->getFileUrl())."'>"._("Download")." ".$this->getFilename()." (".$this->getFileSize(self::UNIT_KILOBYTES)." "._("KB").")</a>"; 
    260319                $html .= "</div>\n"; 
    261320                return parent :: getUserUI($html);