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