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