# HG changeset patch # User Dan # Date 1210900144 14400 # Node ID 023a21c8f47c5c32547be0e5b561dfb3ed7d4076 # Parent d9f37d2ea2cf0b6f21a7543066972b1164c744c8 Added support for embedding Snapr images as wikitext code into regular pages diff -r d9f37d2ea2cf -r 023a21c8f47c plugins/Gallery.php --- a/plugins/Gallery.php Sat Feb 23 16:02:15 2008 -0500 +++ b/plugins/Gallery.php Thu May 15 21:09:04 2008 -0400 @@ -4,7 +4,7 @@ Plugin URI: http://enanocms.org/Enano.Img_Gallery Description: Provides an intuitive image gallery system with a browser, viewer for individual images, upload interface, and comment system integration. Author: Dan Fuhry -Version: 0.1 beta 1 +Version: 0.1 beta 2 Author URI: http://enanocms.org/ */ @@ -67,5 +67,6 @@ require( ENANO_ROOT . '/plugins/gallery/fetcher.php' ); require( ENANO_ROOT . '/plugins/gallery/search.php' ); require( ENANO_ROOT . '/plugins/gallery/sidebar.php' ); +require( ENANO_ROOT . '/plugins/gallery/imagetag.php' ); ?> diff -r d9f37d2ea2cf -r 023a21c8f47c plugins/gallery/fetcher.php --- a/plugins/gallery/fetcher.php Sat Feb 23 16:02:15 2008 -0500 +++ b/plugins/gallery/fetcher.php Thu May 15 21:09:04 2008 -0400 @@ -34,7 +34,7 @@ // sleep(5); $type = $paths->getParam(0); - if ( !in_array($type, array('thumb', 'preview', 'full')) ) + if ( !in_array($type, array('thumb', 'preview', 'full', 'embed')) ) { die('Hack attempt'); } @@ -87,6 +87,32 @@ default: $mimetype = 'application/octet-stream'; } break; + case 'embed': + if ( !isset($_GET['width']) || !isset($_GET['height']) ) + { + die('Missing width or height.'); + } + $src_filename = ENANO_ROOT . '/files/' . $row['img_filename']; + $dest_filename = ENANO_ROOT . '/cache/' . $row['img_filename'] . "-embed-$width-$height.$ext"; + $filename =& $dest_filename; + $ext = get_file_extension($filename); + + $width = intval($_GET['width']); + $height = intval($_GET['height']); + if ( empty($width) || empty($height) || $width > 2048 || $height > 2048 ) + { + die('Bad width or height'); + } + + if ( !file_exists($dest_filename) ) + { + if ( !scale_image($src_filename, $dest_filename, $width, $height, false) ) + { + die('Image scaling process failed.'); + } + } + + break; default: die('PHP...insane...'); break; diff -r d9f37d2ea2cf -r 023a21c8f47c plugins/gallery/imagetag.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/gallery/imagetag.php Thu May 15 21:09:04 2008 -0400 @@ -0,0 +1,259 @@ +attachHook('render_wikiformat_pre', 'snapr_process_image_tags($text);'); + +function snapr_process_image_tags(&$text) +{ + $text = snapr_image_tags_stage1($text, $taglist); + $text = snapr_image_tags_stage2($text, $taglist); +} + +/* + * Functions copied from render.php + */ + +/** + * Changes wikitext image tags to HTML. + * @param string The wikitext to process + * @param array Will be overwritten with the list of HTML tags (the system uses tokens for TextWiki compatibility) + * @return string + */ + +function snapr_image_tags_stage1($text, &$taglist) +{ + global $db, $session, $paths, $template, $plugins; // Common objects + + static $idcache = array(); + + $s_delim = "\xFF"; + $f_delim = "\xFF"; + $taglist = array(); + + // Wicked huh? + $regex = '/\[\[:' . str_replace('/', '\\/', preg_quote($paths->nslist['Gallery'])) . '([\w\s0-9_\(\)!@%\^\+\|\.-]+?)((\|thumb)|(\|([0-9]+)x([0-9]+)))?(\|left|\|right)?(\|raw|\|(.+))?\]\]/i'; + + preg_match_all($regex, $text, $matches); + + foreach ( $matches[0] as $i => $match ) + { + $full_tag =& $matches[0][$i]; + $imagename =& $matches[1][$i]; + $scale_type =& $matches[2][$i]; + $width =& $matches[5][$i]; + $height =& $matches[6][$i]; + $clear =& $matches[7][$i]; + $caption =& $matches[8][$i]; + + // determine the image name + $imagename = sanitize_page_id($imagename); + if ( isset($idcache[$imagename]) ) + { + $found_image_id = true; + $filename =& $idcache[$imagename]; + } + else + { + $found_image_id = false; + // get the image ID + // Ech... he sent us a string... parse it and see what we get + if ( strstr($imagename, '/') ) + { + $folders = explode('/', $imagename); + } + else + { + $folders = array($imagename); + } + foreach ( $folders as $i => $_crap ) + { + $folder =& $folders[$i]; + $folder = dirtify_page_id($folder); + $folder = str_replace('_', ' ', $folder); + } + unset($folder); + + $folders = array_reverse($folders); + // 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 + $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'; + $where = "\n " . 'WHERE g0.img_title=\'' . $db->escape($folders[0]) . '\''; + foreach ( $folders as $i => $folder ) + { + if ( $i == 0 ) + continue; + $i_dec = $i - 1; + $folder = $db->escape($folder); + $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' )"; + $where .= "\n ".'AND g'.$i.'.img_id IS NOT NULL'; + } + $where .= "\n AND g{$i}.folder_parent IS NULL"; + $sql .= $where . ';'; + + if ( !$db->sql_query($sql) ) + { + $db->_die('The image metadata could not be loaded.'); + } + + if ( $db->numrows() > 0 ) + { + $found_image_id = true; + $row = $db->fetchrow(); + $db->free_result(); + $idcache[$imagename] = $row['img_id']; + $filename =& $idcache[$imagename]; + } + } + + if ( !$found_image_id ) + { + $text = str_replace($full_tag, '[[' . makeUrlNS('Gallery', $imagename) . ']]', $text); + continue; + } + + if ( $scale_type == '|thumb' ) + { + $r_width = 225; + $r_height = 225; + + $url = makeUrlNS('Special', 'GalleryFetcher/embed/' . $filename, 'width=' . $r_width . '&height=' . $r_height, true); + } + else if ( !empty($width) && !empty($height) ) + { + $r_width = $width; + $r_height = $height; + + $url = makeUrlNS('Special', 'GalleryFetcher/embed/' . $filename, 'width=' . $r_width . '&height=' . $r_height, true); + } + else + { + $url = makeUrlNS('Special', 'GalleryFetcher/' . $filename); + } + + $img_tag = 'setHook('snapr_img_tag_parse_img'); + foreach ( $code as $cmd ) + { + eval($cmd); + } + + $img_tag .= '/>'; + + $complete_tag = ''; + + if ( !empty($scale_type) && $caption != '|raw' ) + { + $complete_tag .= '
'; + $complete_tag .= $img_tag; + $complete_tag .= ''; + + $mag_button = '[ + ]'; + + if ( !empty($caption) ) + { + $cap = substr($caption, 1); + $complete_tag .= $mag_button . $cap; + } + + $complete_tag .= '
'; + } + else if ( $caption == '|raw' ) + { + $complete_tag .= "$img_tag"; + $taglist[$i] = $complete_tag; + + $repl = "{$s_delim}e_img_{$i}{$f_delim}"; + $text = str_replace($full_tag, $repl, $text); + continue; + } + else + { + $complete_tag .= 'setHook('snapr_img_tag_parse_link'); + foreach ( $code as $cmd ) + { + eval($cmd); + } + $complete_tag .= '>'; + $complete_tag .= $img_tag; + $complete_tag .= ''; + } + + $complete_tag .= "\n\n"; + $taglist[$i] = $complete_tag; + + $pos = strpos($text, $full_tag); + + while(true) + { + $check1 = substr($text, $pos, 3); + $check2 = substr($text, $pos, 1); + if ( $check1 == '

' || $pos == 0 || $check2 == "\n" ) + { + // die('found at pos '.$pos); + break; + } + $pos--; + } + + $repl = "{$s_delim}e_img_{$i}{$f_delim}"; + $text = substr($text, 0, $pos) . $repl . substr($text, $pos); + + $text = str_replace($full_tag, '', $text); + + unset($full_tag, $filename, $scale_type, $width, $height, $clear, $caption, $r_width, $r_height); + + } + + return $text; +} + +/** + * Finalizes processing of image tags. + * @param string The preprocessed text + * @param array The list of image tags created by RenderMan::process_image_tags() + */ + +function snapr_image_tags_stage2($text, $taglist) +{ + $s_delim = "\xFF"; + $f_delim = "\xFF"; + foreach ( $taglist as $i => $tag ) + { + $repl = "{$s_delim}e_img_{$i}{$f_delim}"; + $text = str_replace($repl, $tag, $text); + } + return $text; +} + +?>