plugins/gallery/viewimage.php
changeset 33 6dc190e2158b
parent 25 d9f37d2ea2cf
child 34 5e9bd8e5c6bf
equal deleted inserted replaced
32:9803dde5df5f 33:6dc190e2158b
    15 ##
    15 ##
    16 ## GALLERY NAMESPACE HANDLER
    16 ## GALLERY NAMESPACE HANDLER
    17 ##
    17 ##
    18 
    18 
    19 $plugins->attachHook('page_not_found', 'gallery_namespace_handler($this);');
    19 $plugins->attachHook('page_not_found', 'gallery_namespace_handler($this);');
       
    20 $plugins->attachHook('page_type_string_set', 'if ( $local_namespace == "Gallery" ) $this->namespace_string = \'image\';');
    20 
    21 
    21 function gallery_namespace_handler(&$page)
    22 function gallery_namespace_handler(&$page)
    22 {
    23 {
    23   global $db, $session, $paths, $template, $plugins; // Common objects
    24   global $db, $session, $paths, $template, $plugins; // Common objects
    24   
    25   
    29   {
    30   {
    30     page_Special_Gallery();
    31     page_Special_Gallery();
    31     return true;
    32     return true;
    32   }
    33   }
    33   
    34   
    34   if ( preg_match('/^[0-9]+$/', $page->page_id) )
    35   $row =& $page->image_info;
    35   {
       
    36     $img_id = intval($page->page_id);
       
    37     if ( !$img_id )
       
    38       return false;
       
    39     $q = $db->sql_query('SELECT img_id, img_title, img_desc, print_sizes, img_time_upload, img_time_mod, img_filename, folder_parent, img_tags FROM '.table_prefix.'gallery WHERE img_id=' . $img_id . ';');
       
    40     if ( !$q )
       
    41       $db->_die();
       
    42   }
       
    43   else
       
    44   {
       
    45     // Ech... he sent us a string... parse it and see what we get
       
    46     if ( strstr($page->page_id, '/') )
       
    47     {
       
    48       $folders = explode('/', $page->page_id);
       
    49     }
       
    50     else
       
    51     {
       
    52       $folders = array($page->page_id);
       
    53     }
       
    54     foreach ( $folders as $i => $_crap )
       
    55     {
       
    56       $folder =& $folders[$i];
       
    57       $folder = dirtify_page_id($folder);
       
    58       $folder = str_replace('_', ' ', $folder);
       
    59     }
       
    60     unset($folder);
       
    61     
       
    62     $folders = array_reverse($folders);
       
    63     // This is one of the best MySQL tricks on the market. We're going to reverse-travel a folder path using LEFT JOIN and the incredible power of metacoded SQL
       
    64     $sql = 'SELECT g0.img_id, g0.img_title, g0.img_desc, g0.print_sizes, g0.img_time_upload, g0.img_time_mod, g0.img_filename, g0.folder_parent, g0.img_tags FROM '.table_prefix.'gallery AS g0';
       
    65     $where = "\n  " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\'';
       
    66     foreach ( $folders as $i => $folder )
       
    67     {
       
    68       if ( $i == 0 )
       
    69         continue;
       
    70       $i_dec = $i - 1;
       
    71       $folder = $db->escape($folder);
       
    72       $sql .= "\n  LEFT JOIN ".table_prefix."gallery AS g{$i}\n    ON ( g{$i}.img_id=g{$i_dec}.folder_parent AND g{$i}.img_title='$folder' )";
       
    73       $where .= "\n    ".'AND g'.$i.'.img_id IS NOT NULL';
       
    74     }
       
    75     $where .= "\n    AND g{$i}.folder_parent IS NULL";
       
    76     $sql .= $where . ';';
       
    77     
       
    78     if ( !$db->sql_query($sql) )
       
    79     {
       
    80       $db->_die('The image metadata could not be loaded.');
       
    81     }
       
    82     
       
    83     // Now that the folder data is no longer needed, we can fool around with it a little
       
    84     $folders = $page->page_id;
       
    85     if ( !strstr($folders, '/') )
       
    86     {
       
    87       $hier = '/';
       
    88     }
       
    89     else
       
    90     {
       
    91       $hier = preg_replace('/\/([^\/]+)$/', '/', $folders);
       
    92       $hier = sanitize_page_id($hier);
       
    93     }
       
    94     
       
    95   }
       
    96   if ( $db->numrows() < 1 )
       
    97   {
       
    98     // Image not found - show custom error message
       
    99     $template->header();
       
   100     echo '<h3>There is no image in the gallery with this ID.</h3>';
       
   101     echo '<p>You have requested an image that couldn\'t be looked up. Please check the URL and try again, or visit the <a href="' . makeUrlNS('Special', 'Gallery') . '">Gallery index</a>.</p>';
       
   102     $template->footer();
       
   103     return false;
       
   104   }
       
   105   $row = $db->fetchrow();
       
   106   
    36   
   107   $db->free_result();
    37   $db->free_result();
   108   
    38   
   109   $img_id = $row['img_id'];
    39   $img_id = $row['img_id'];
   110   
    40   
   311   
   241   
   312   $template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/gallery/canvas.js"></script>');
   242   $template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/gallery/canvas.js"></script>');
   313   $template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/gallery/tagging.js"></script>');
   243   $template->add_header('<script type="text/javascript" src="' . scriptPath . '/plugins/gallery/tagging.js"></script>');
   314   
   244   
   315   $template->tpl_strings['PAGE_NAME'] = 'Gallery image: ' . htmlspecialchars($row['img_title']);
   245   $template->tpl_strings['PAGE_NAME'] = 'Gallery image: ' . htmlspecialchars($row['img_title']);
       
   246   if ( is_object(@$GLOBALS['output']) )
       
   247   {
       
   248     global $output;
       
   249     $output->set_title('Gallery image: ' . $row['img_title']);
       
   250   }
       
   251   else if ( method_exists($template, 'assign_vars') )
       
   252   {
       
   253     $template->assign_vars(array(
       
   254         'PAGE_NAME' => 'Gallery image: ' . htmlspecialchars($row['img_title'])
       
   255       ));
       
   256   }
   316   $title_spacey = strtolower(htmlspecialchars($row['img_title']));
   257   $title_spacey = strtolower(htmlspecialchars($row['img_title']));
   317   
   258   
   318   $template->header();
   259   $template->header();
   319   
   260   
   320   $img_id = intval($img_id);
   261   $img_id = intval($img_id);
   423   echo '<tr><td class="row3" colspan="2" style="text-align: center;"><a href="' . makeUrlNS('Special', 'GalleryFetcher/full/' . $img_id, 'download', 'true') . '">Download image</a></td></tr>';
   364   echo '<tr><td class="row3" colspan="2" style="text-align: center;"><a href="' . makeUrlNS('Special', 'GalleryFetcher/full/' . $img_id, 'download', 'true') . '">Download image</a></td></tr>';
   424           
   365           
   425   echo '</table></div>';
   366   echo '</table></div>';
   426   
   367   
   427   $template->footer();
   368   $template->footer();
   428     
       
   429 }
   369 }
   430 
   370 
       
   371 /**
       
   372  * This is for Enano 1.1.6 and up.
       
   373  */
       
   374 
       
   375 class Namespace_Gallery extends Namespace_Default
       
   376 {
       
   377   public $image_info;
       
   378   
       
   379   function __construct($page_id, $namespace, $revision_id = 0)
       
   380   {
       
   381     global $db, $session, $paths, $template, $plugins; // Common objects
       
   382       
       
   383     $this->page_id = sanitize_page_id($page_id);
       
   384     $this->namespace = $namespace;
       
   385     $this->revision_id = intval($revision_id);
       
   386     
       
   387     // only do this if calling from the (very heavily feature filled) abstract
       
   388     // this will still be called if you're using your own handler but not replacing the constructor
       
   389     if ( __CLASS__ == 'Namespace_Gallery' )
       
   390     {
       
   391       $this->exists = false;
       
   392       // NOTE! These should already be WELL sanitized before we reach this stage.
       
   393       
       
   394       if ( preg_match('/^[0-9]+$/', $this->page_id) )
       
   395       {
       
   396         $img_id = intval($this->page_id);
       
   397         if ( !$img_id )
       
   398         {
       
   399           $this->exists = false;
       
   400           return;
       
   401         }
       
   402         $q = $db->sql_query('SELECT img_id, img_title, img_desc, print_sizes, img_time_upload, img_time_mod, img_filename, folder_parent, img_tags FROM '.table_prefix.'gallery WHERE img_id=' . $img_id . ';');
       
   403         if ( !$q )
       
   404           $db->_die();
       
   405       }
       
   406       else
       
   407       {
       
   408         // Ech... he sent us a string... parse it and see what we get
       
   409         if ( strstr($this->page_id, '/') )
       
   410         {
       
   411           $folders = explode('/', $this->page_id);
       
   412         }
       
   413         else
       
   414         {
       
   415           $folders = array($this->page_id);
       
   416         }
       
   417         foreach ( $folders as $i => $_crap )
       
   418         {
       
   419           $folder =& $folders[$i];
       
   420           $folder = dirtify_page_id($folder);
       
   421           $folder = str_replace('_', ' ', $folder);
       
   422         }
       
   423         unset($folder);
       
   424         
       
   425         $folders = array_reverse($folders);
       
   426         // This is one of the best MySQL tricks on the market. We're going to reverse-travel a folder path using LEFT JOIN and the incredible power of metacoded SQL
       
   427         $sql = 'SELECT g0.img_id, g0.img_title, g0.img_desc, g0.print_sizes, g0.img_time_upload, g0.img_time_mod, g0.img_filename, g0.folder_parent, g0.img_tags FROM '.table_prefix.'gallery AS g0';
       
   428         $where = "\n  " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\'';
       
   429         foreach ( $folders as $i => $folder )
       
   430         {
       
   431           if ( $i == 0 )
       
   432             continue;
       
   433           $i_dec = $i - 1;
       
   434           $folder = $db->escape($folder);
       
   435           $sql .= "\n  LEFT JOIN ".table_prefix."gallery AS g{$i}\n    ON ( g{$i}.img_id=g{$i_dec}.folder_parent AND g{$i}.img_title='$folder' )";
       
   436           $where .= "\n    ".'AND g'.$i.'.img_id IS NOT NULL';
       
   437         }
       
   438         $where .= "\n    AND g{$i}.folder_parent IS NULL";
       
   439         $sql .= $where . ';';
       
   440         
       
   441         if ( !$db->sql_query($sql) )
       
   442         {
       
   443           $db->_die('The image metadata could not be loaded.');
       
   444         }
       
   445         
       
   446         // Now that the folder data is no longer needed, we can fool around with it a little
       
   447         $folders = $this->page_id;
       
   448         if ( !strstr($folders, '/') )
       
   449         {
       
   450           $hier = '/';
       
   451         }
       
   452         else
       
   453         {
       
   454           $hier = preg_replace('/\/([^\/]+)$/', '/', $folders);
       
   455           $hier = sanitize_page_id($hier);
       
   456         }
       
   457         
       
   458       }
       
   459       if ( $db->numrows() < 1 )
       
   460       {
       
   461         // Image not found
       
   462         $this->exists = false;
       
   463       }
       
   464       else
       
   465       {
       
   466         $this->image_info = $db->fetchrow();
       
   467         $this->exists = true;
       
   468       }
       
   469       
       
   470       $db->free_result();
       
   471     }
       
   472   }
       
   473   
       
   474   function send()
       
   475   {
       
   476     if ( $this->exists )
       
   477     {
       
   478       gallery_namespace_handler($this);
       
   479     }
       
   480     else
       
   481     {
       
   482       global $output;
       
   483       $output->header();
       
   484       $this->error_404();
       
   485       $output->footer();
       
   486     }
       
   487   }
       
   488   
       
   489   function error_404()
       
   490   {
       
   491     echo '<h3>There is no image in the gallery with this ID.</h3>';
       
   492     echo '<p>You have requested an image that couldn\'t be looked up. Please check the URL and try again, or visit the <a href="' . makeUrlNS('Special', 'Gallery') . '">Gallery index</a>.</p>';
       
   493   }
       
   494 }
       
   495 
   431 ?>
   496 ?>