includes/namespaces/file.php
changeset 800 9cdfe82c56cd
child 801 eb8b23f11744
equal deleted inserted replaced
799:4629ad98ee88 800:9cdfe82c56cd
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Enano - an open-source CMS capable of wiki functions, Drupal-like sidebar blocks, and everything in between
       
     5  * Version 1.1.5 (Caoineag alpha 5)
       
     6  * Copyright (C) 2006-2008 Dan Fuhry
       
     7  *
       
     8  * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License
       
     9  * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
       
    10  *
       
    11  * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
       
    12  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details.
       
    13  */
       
    14 
       
    15 class Namespace_File extends Namespace_Default
       
    16 {
       
    17   function send()
       
    18   {
       
    19     global $output;
       
    20     
       
    21     $output->add_before_footer($this->show_info());
       
    22     $output->add_before_footer($this->display_categories());
       
    23     
       
    24     if ( $this->exists )
       
    25     {
       
    26       $this->send_from_db();
       
    27     }
       
    28     else
       
    29     {
       
    30       $output->header();
       
    31       $this->error_404();
       
    32       $output->footer();
       
    33     }
       
    34   }
       
    35   
       
    36   function show_info()
       
    37   {
       
    38     global $db, $session, $paths, $template, $plugins; // Common objects
       
    39     global $lang;
       
    40     
       
    41     $local_page_id = $this->page_id;
       
    42     $local_namespace = $this->namespace;
       
    43     $html = '';
       
    44     
       
    45     // Prevent unnecessary work
       
    46     if ( $local_namespace != 'File' )
       
    47       return null;
       
    48     
       
    49     $selfn = $local_page_id;
       
    50     if ( substr($paths->cpage['name'], 0, strlen($paths->nslist['File'])) == $paths->nslist['File'])
       
    51     {
       
    52       $selfn = substr($local_page_id, strlen($paths->nslist['File']), strlen($local_page_id));
       
    53     }
       
    54     $selfn = $db->escape($selfn);
       
    55     $q = $db->sql_query('SELECT f.mimetype,f.time_id,f.size,l.log_id FROM ' . table_prefix . "files AS f\n"
       
    56                       . "  LEFT JOIN " . table_prefix . "logs AS l\n"
       
    57                       . "    ON ( l.time_id = f.time_id AND ( l.action = 'reupload' OR l.action IS NULL ) )\n"
       
    58                       . "  WHERE f.page_id = '$selfn'\n"
       
    59                       . "    ORDER BY f.time_id DESC;");
       
    60     if ( !$q )
       
    61     {
       
    62       $db->_die('The file type could not be fetched.');
       
    63     }
       
    64     
       
    65     if ( $db->numrows() < 1 )
       
    66     {
       
    67       $html .= '<div class="mdg-comment" style="margin-left: 0;">
       
    68               <h3>' . $lang->get('onpage_filebox_heading') . '</h3>
       
    69               <p>' . $lang->get('onpage_filebox_msg_not_found', array('upload_link' => makeUrlNS('Special', 'UploadFile/'.$local_page_id))) . '</p>
       
    70             </div>
       
    71             <br />';
       
    72       return $html;
       
    73     }
       
    74     $r = $db->fetchrow();
       
    75     $mimetype = $r['mimetype'];
       
    76     $datestring = enano_date('F d, Y h:i a', (int)$r['time_id']);
       
    77     $html .= '<div class="mdg-comment" style="margin-left: 0;">
       
    78             <h3>' . $lang->get('onpage_filebox_heading') . '</h3>
       
    79             <p>' . $lang->get('onpage_filebox_lbl_type') . ' '.$r['mimetype'].'<br />';
       
    80     
       
    81     $size = $r['size'] . ' ' . $lang->get('etc_unit_bytes');
       
    82     if ( $r['size'] >= 1048576 )
       
    83     {
       
    84       $size .= ' (' . ( round($r['size'] / 1048576, 1) ) . ' ' . $lang->get('etc_unit_megabytes_short') . ')';
       
    85     }
       
    86     else if ( $r['size'] >= 1024 )
       
    87     {
       
    88       $size .= ' (' . ( round($r['size'] / 1024, 1) ) . ' ' . $lang->get('etc_unit_kilobytes_short') . ')';
       
    89     }
       
    90     
       
    91     $html .= $lang->get('onpage_filebox_lbl_size', array('size' => $size));
       
    92     
       
    93     $html .= '<br />' . $lang->get('onpage_filebox_lbl_uploaded') . ' ' . $datestring . '</p>';
       
    94     if ( substr($mimetype, 0, 6) != 'image/' && ( substr($mimetype, 0, 5) != 'text/' || $mimetype == 'text/html' || $mimetype == 'text/javascript' ) )
       
    95     {
       
    96       $html .= '<div class="warning-box">
       
    97               ' . $lang->get('onpage_filebox_msg_virus_warning') . '
       
    98             </div>';
       
    99     }
       
   100     if ( substr($mimetype, 0, 6) == 'image/' )
       
   101     {
       
   102       $html .= '<p>
       
   103               <a href="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn).'">
       
   104                 <img style="border: 0;" alt="'.$paths->page.'" src="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn.htmlspecialchars(urlSeparator).'preview').'" />
       
   105               </a>
       
   106             </p>';
       
   107     }
       
   108     $html .= '<p>
       
   109             <a href="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn.'/'.$r['time_id'].htmlspecialchars(urlSeparator).'download').'">
       
   110               ' . $lang->get('onpage_filebox_btn_download') . '
       
   111             </a>';
       
   112     if(!$paths->page_protected && ( $paths->wiki_mode || $session->get_permissions('upload_new_version') ))
       
   113     {
       
   114       $html .= '  |  <a href="'.makeUrlNS('Special', 'UploadFile'.'/'.$selfn).'">
       
   115               ' . $lang->get('onpage_filebox_btn_upload_new') . '
       
   116             </a>';
       
   117     }
       
   118     $html .= '</p>';
       
   119     if ( $db->numrows() > 1 )
       
   120     {
       
   121       // requery, sql_result_seek() doesn't work on postgres
       
   122       $db->free_result();
       
   123       $q = $db->sql_query('SELECT f.mimetype,f.time_id,f.size,l.log_id FROM ' . table_prefix . "files AS f\n"
       
   124                       . "  LEFT JOIN " . table_prefix . "logs AS l\n"
       
   125                       . "    ON ( l.time_id = f.time_id AND ( l.action = 'reupload' OR l.action IS NULL ) )\n"
       
   126                       . "  WHERE f.page_id = '$selfn'\n"
       
   127                       . "    ORDER BY f.time_id DESC;");
       
   128       if ( !$q )
       
   129         $db->_die();
       
   130       
       
   131       $html .= '<h3>' . $lang->get('onpage_filebox_heading_history') . '</h3><p>';
       
   132       $last_rollback_id = false;
       
   133       while ( $r = $db->fetchrow() )
       
   134       {
       
   135         $html .= '(<a href="'.makeUrlNS('Special', 'DownloadFile'.'/'.$selfn.'/'.$r['time_id'].htmlspecialchars(urlSeparator).'download').'">' . $lang->get('onpage_filebox_btn_this_version') . '</a>) ';
       
   136         if ( $session->get_permissions('history_rollback') && $last_rollback_id )
       
   137           $html .= ' (<a href="#rollback:' . $last_rollback_id . '" onclick="ajaxRollback(\''.$last_rollback_id.'\'); return false;">' . $lang->get('onpage_filebox_btn_revert') . '</a>) ';
       
   138         else if ( $session->get_permissions('history_rollback') && !$last_rollback_id )
       
   139           $html .= ' (' . $lang->get('onpage_filebox_btn_current') . ') ';
       
   140         $last_rollback_id = $r['log_id'];
       
   141         $mimetype = $r['mimetype'];
       
   142         $datestring = enano_date('F d, Y h:i a', (int)$r['time_id']);
       
   143         
       
   144         $html .= $datestring.': '.$r['mimetype'].', ';
       
   145         
       
   146         $fs = $r['size'];
       
   147         $fs = (int)$fs;
       
   148         
       
   149         if($fs >= 1048576)
       
   150         {
       
   151           $fs = round($fs / 1048576, 1);
       
   152           $size = $fs . ' ' . $lang->get('etc_unit_megabytes_short');
       
   153         }
       
   154         else
       
   155         if ( $fs >= 1024 )
       
   156         {
       
   157           $fs = round($fs / 1024, 1);
       
   158           $size = $fs . ' ' . $lang->get('etc_unit_kilobytes_short');
       
   159         }
       
   160         else
       
   161         {
       
   162           $size = $fs . ' ' . $lang->get('etc_unit_bytes');
       
   163         }
       
   164         
       
   165         $html .= $size;
       
   166         
       
   167         $html .= '<br />';
       
   168       }
       
   169       $html .= '</p>';
       
   170     }
       
   171     $db->free_result();
       
   172     $html .= '</div><br />';
       
   173     return $html;
       
   174   }
       
   175 }
       
   176