plugins/gallery/imagetag.php
changeset 26 023a21c8f47c
child 42 7c6e2e97aa08
equal deleted inserted replaced
25:d9f37d2ea2cf 26:023a21c8f47c
       
     1 <?php
       
     2 
       
     3 /*
       
     4  * Snapr
       
     5  * Version 0.1 beta 1
       
     6  * Copyright (C) 2007 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 // Add an [[:Image:foo]] tag handler to the wiki formatter
       
    16 $plugins->attachHook('render_wikiformat_pre', 'snapr_process_image_tags($text);');
       
    17 
       
    18 function snapr_process_image_tags(&$text)
       
    19 {
       
    20   $text = snapr_image_tags_stage1($text, $taglist);
       
    21   $text = snapr_image_tags_stage2($text, $taglist);
       
    22 }
       
    23 
       
    24 /*
       
    25  * Functions copied from render.php
       
    26  */
       
    27 
       
    28 /**
       
    29  * Changes wikitext image tags to HTML.
       
    30  * @param string The wikitext to process
       
    31  * @param array Will be overwritten with the list of HTML tags (the system uses tokens for TextWiki compatibility)
       
    32  * @return string
       
    33  */
       
    34 
       
    35 function snapr_image_tags_stage1($text, &$taglist)
       
    36 {
       
    37   global $db, $session, $paths, $template, $plugins; // Common objects
       
    38   
       
    39   static $idcache = array();
       
    40   
       
    41   $s_delim = "\xFF";
       
    42   $f_delim = "\xFF";
       
    43   $taglist = array();
       
    44   
       
    45   // Wicked huh?
       
    46   $regex = '/\[\[:' . str_replace('/', '\\/', preg_quote($paths->nslist['Gallery'])) . '([\w\s0-9_\(\)!@%\^\+\|\.-]+?)((\|thumb)|(\|([0-9]+)x([0-9]+)))?(\|left|\|right)?(\|raw|\|(.+))?\]\]/i';
       
    47   
       
    48   preg_match_all($regex, $text, $matches);
       
    49   
       
    50   foreach ( $matches[0] as $i => $match )
       
    51   {
       
    52     $full_tag   =& $matches[0][$i];
       
    53     $imagename  =& $matches[1][$i];
       
    54     $scale_type =& $matches[2][$i];
       
    55     $width      =& $matches[5][$i];
       
    56     $height     =& $matches[6][$i];
       
    57     $clear      =& $matches[7][$i];
       
    58     $caption    =& $matches[8][$i];
       
    59     
       
    60     // determine the image name
       
    61     $imagename = sanitize_page_id($imagename);
       
    62     if ( isset($idcache[$imagename]) )
       
    63     {
       
    64       $found_image_id = true;
       
    65       $filename =& $idcache[$imagename];
       
    66     }
       
    67     else
       
    68     {
       
    69       $found_image_id = false;
       
    70       // get the image ID
       
    71       // Ech... he sent us a string... parse it and see what we get
       
    72       if ( strstr($imagename, '/') )
       
    73       {
       
    74         $folders = explode('/', $imagename);
       
    75       }
       
    76       else
       
    77       {
       
    78         $folders = array($imagename);
       
    79       }
       
    80       foreach ( $folders as $i => $_crap )
       
    81       {
       
    82         $folder =& $folders[$i];
       
    83         $folder = dirtify_page_id($folder);
       
    84         $folder = str_replace('_', ' ', $folder);
       
    85       }
       
    86       unset($folder);
       
    87       
       
    88       $folders = array_reverse($folders);
       
    89       // 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
       
    90       $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';
       
    91       $where = "\n  " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\'';
       
    92       foreach ( $folders as $i => $folder )
       
    93       {
       
    94         if ( $i == 0 )
       
    95           continue;
       
    96         $i_dec = $i - 1;
       
    97         $folder = $db->escape($folder);
       
    98         $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' )";
       
    99         $where .= "\n    ".'AND g'.$i.'.img_id IS NOT NULL';
       
   100       }
       
   101       $where .= "\n    AND g{$i}.folder_parent IS NULL";
       
   102       $sql .= $where . ';';
       
   103       
       
   104       if ( !$db->sql_query($sql) )
       
   105       {
       
   106         $db->_die('The image metadata could not be loaded.');
       
   107       }
       
   108       
       
   109       if ( $db->numrows() > 0 )
       
   110       {
       
   111         $found_image_id = true;
       
   112         $row = $db->fetchrow();
       
   113         $db->free_result();
       
   114         $idcache[$imagename] = $row['img_id'];
       
   115         $filename =& $idcache[$imagename];
       
   116       }
       
   117     }
       
   118     
       
   119     if ( !$found_image_id )
       
   120     {
       
   121       $text = str_replace($full_tag, '[[' . makeUrlNS('Gallery', $imagename) . ']]', $text);
       
   122       continue;
       
   123     }
       
   124     
       
   125     if ( $scale_type == '|thumb' )
       
   126     {
       
   127       $r_width  = 225;
       
   128       $r_height = 225;
       
   129       
       
   130       $url = makeUrlNS('Special', 'GalleryFetcher/embed/' . $filename, 'width=' . $r_width . '&height=' . $r_height, true);
       
   131     }
       
   132     else if ( !empty($width) && !empty($height) )
       
   133     {
       
   134       $r_width = $width;
       
   135       $r_height = $height;
       
   136       
       
   137       $url = makeUrlNS('Special', 'GalleryFetcher/embed/' . $filename, 'width=' . $r_width . '&height=' . $r_height, true);
       
   138     }
       
   139     else
       
   140     {
       
   141       $url = makeUrlNS('Special', 'GalleryFetcher/' . $filename);
       
   142     }
       
   143     
       
   144     $img_tag = '<img src="' . $url . '" ';
       
   145     
       
   146     // if ( isset($r_width) && isset($r_height) && $scale_type != '|thumb' )
       
   147     // {
       
   148     //   $img_tag .= 'width="' . $r_width . '" height="' . $r_height . '" ';
       
   149     // }
       
   150     
       
   151     $img_tag .= 'style="border-width: 0px; /* background-color: white; */" ';
       
   152     
       
   153     $code = $plugins->setHook('snapr_img_tag_parse_img');
       
   154     foreach ( $code as $cmd )
       
   155     {
       
   156       eval($cmd);
       
   157     }
       
   158     
       
   159     $img_tag .= '/>';
       
   160     
       
   161     $complete_tag = '';
       
   162     
       
   163     if ( !empty($scale_type) && $caption != '|raw' )
       
   164     {
       
   165       $complete_tag .= '<div class="thumbnail" ';
       
   166       $clear_text = '';
       
   167       if ( !empty($clear) )
       
   168       {
       
   169         $side = ( $clear == '|left' ) ? 'left' : 'right';
       
   170         $opposite = ( $clear == '|left' ) ? 'right' : 'left';
       
   171         $clear_text .= "float: $side; margin-$opposite: 20px; width: {$r_width}px;";
       
   172         $complete_tag .= 'style="' . $clear_text . '" ';
       
   173       }
       
   174       $complete_tag .= '>';
       
   175       
       
   176       $complete_tag .= '<a href="' . makeUrlNS('Gallery', $filename) . '" style="display: block;">';
       
   177       $complete_tag .= $img_tag;
       
   178       $complete_tag .= '</a>';
       
   179       
       
   180       $mag_button = '<a href="' . makeUrlNS('Gallery', $filename) . '" style="display: block; float: right; clear: right; margin: 0 0 10px 10px;"><img alt="[ + ]" src="' . scriptPath . '/images/thumbnail.png" style="border-width: 0px;" /></a>';
       
   181     
       
   182       if ( !empty($caption) )
       
   183       {
       
   184         $cap = substr($caption, 1);
       
   185         $complete_tag .= $mag_button . $cap;
       
   186       }
       
   187       
       
   188       $complete_tag .= '</div>';
       
   189     }
       
   190     else if ( $caption == '|raw' )
       
   191     {
       
   192       $complete_tag .= "$img_tag";
       
   193       $taglist[$i] = $complete_tag;
       
   194       
       
   195       $repl = "{$s_delim}e_img_{$i}{$f_delim}";
       
   196       $text = str_replace($full_tag, $repl, $text);
       
   197       continue;
       
   198     }
       
   199     else
       
   200     {
       
   201       $complete_tag .= '<a href="' . makeUrlNS('Gallery', $filename) . '" style="display: block;"';
       
   202       $code = $plugins->setHook('snapr_img_tag_parse_link');
       
   203       foreach ( $code as $cmd )
       
   204       {
       
   205         eval($cmd);
       
   206       }
       
   207       $complete_tag .= '>';
       
   208       $complete_tag .= $img_tag;
       
   209       $complete_tag .= '</a>';
       
   210     }
       
   211     
       
   212     $complete_tag .= "\n\n";
       
   213     $taglist[$i] = $complete_tag;
       
   214     
       
   215     $pos = strpos($text, $full_tag);
       
   216     
       
   217     while(true)
       
   218     {
       
   219       $check1 = substr($text, $pos, 3);
       
   220       $check2 = substr($text, $pos, 1);
       
   221       if ( $check1 == '<p>' || $pos == 0 || $check2 == "\n" )
       
   222       {
       
   223         // die('found at pos '.$pos);
       
   224         break;
       
   225       }
       
   226       $pos--;
       
   227     }
       
   228     
       
   229     $repl = "{$s_delim}e_img_{$i}{$f_delim}";
       
   230     $text = substr($text, 0, $pos) . $repl . substr($text, $pos);
       
   231     
       
   232     $text = str_replace($full_tag, '', $text);
       
   233     
       
   234     unset($full_tag, $filename, $scale_type, $width, $height, $clear, $caption, $r_width, $r_height);
       
   235     
       
   236   }
       
   237   
       
   238   return $text;
       
   239 }
       
   240 
       
   241 /**
       
   242  * Finalizes processing of image tags.
       
   243  * @param string The preprocessed text
       
   244  * @param array The list of image tags created by RenderMan::process_image_tags()
       
   245  */
       
   246  
       
   247 function snapr_image_tags_stage2($text, $taglist)
       
   248 {
       
   249   $s_delim = "\xFF";
       
   250   $f_delim = "\xFF";
       
   251   foreach ( $taglist as $i => $tag )
       
   252   {
       
   253     $repl = "{$s_delim}e_img_{$i}{$f_delim}";
       
   254     $text = str_replace($repl, $tag, $text);
       
   255   }               
       
   256   return $text;
       
   257 }
       
   258 
       
   259 ?>