includes/render.php
changeset 569 6ba792bc9071
parent 536 218a627eb53e
child 592 27377179fe58
equal deleted inserted replaced
568:3700f7124c2b 569:6ba792bc9071
   866     $s_delim = "\xFF";
   866     $s_delim = "\xFF";
   867     $f_delim = "\xFF";
   867     $f_delim = "\xFF";
   868     $taglist = array();
   868     $taglist = array();
   869     
   869     
   870     // Wicked huh?
   870     // Wicked huh?
   871     $regex = '/\[\[:' . str_replace('/', '\\/', preg_quote($paths->nslist['File'])) . '([\w\s0-9_\(\)!@%\^\+\|\.-]+?)((\|thumb)|(\|([0-9]+)x([0-9]+)))?(\|left|\|right)?(\|raw|\|(.+))?\]\]/i';
   871     $ns_file = str_replace('/', '\\/', preg_quote($paths->nslist['File']));
       
   872     $regex = '/
       
   873            \[\[                                                                  # starting delimiter 
       
   874            :' . $ns_file . '([\w\s0-9_\(\)!@%\^\+\|\.-]+?\.(?:png|gif|jpg|jpeg)) # image filename
       
   875            (?:(?:\|(?:.+?))*)                                                    # parameters
       
   876            \]\]                                                                  # ending delimiter
       
   877            /ix';
   872     
   878     
   873     preg_match_all($regex, $text, $matches);
   879     preg_match_all($regex, $text, $matches);
   874     
   880     
   875     foreach ( $matches[0] as $i => $match )
   881     foreach ( $matches[0] as $i => $match )
   876     {
   882     {
   877       
   883       
   878       $full_tag   =& $matches[0][$i];
   884       $full_tag   =& $matches[0][$i];
   879       $filename   =& $matches[1][$i];
   885       $filename   =& $matches[1][$i];
   880       $scale_type =& $matches[2][$i];
   886       
   881       $width      =& $matches[5][$i];
   887       // apply recursion (hack? @todo could this be done with (?R) in PCRE?)
   882       $height     =& $matches[6][$i];
   888       $tag_pos = strpos($text, $full_tag);
   883       $clear      =& $matches[7][$i];
   889       $tag_end_pos = $tag_pos + strlen($full_tag);
   884       $caption    =& $matches[8][$i];
   890       while ( get_char_count($full_tag, ']') < get_char_count($full_tag, '[') && $tag_end_pos < strlen($text) )
       
   891       {
       
   892         $full_tag .= substr($text, $tag_end_pos, 1);
       
   893         $tag_end_pos++;
       
   894       }
       
   895       if ( $tag_end_pos > strlen($text) )
       
   896       {
       
   897         // discard tag, not closed fully
       
   898         continue;
       
   899       }
       
   900       
       
   901       // init the various image parameters
       
   902       $width = null;
       
   903       $height = null;
       
   904       $scale_type = null;
       
   905       $raw_display = false;
       
   906       $clear = null;
       
   907       $caption = null;
       
   908       
       
   909       // trim tag and parse particles
       
   910       $tag_trim = rtrim(ltrim($full_tag, '['), ']');
       
   911       // trim off the filename from the start of the tag
       
   912       $filepart_len = 1 + strlen($paths->nslist['File']) + strlen($filename) + 1;
       
   913       $tag_trim = substr($tag_trim, $filepart_len);
       
   914       // explode and we should have parameters
       
   915       $tag_parts = explode('|', $tag_trim);
       
   916       
       
   917       // for each of the parameters, see if it matches a known option. If so, apply it;
       
   918       // otherwise, see if a plugin reserved that parameter and if not treat it as the caption
       
   919       foreach ( $tag_parts as $param )
       
   920       {
       
   921         switch($param)
       
   922         {
       
   923           case 'left':
       
   924           case 'right':
       
   925             $clear = $param;
       
   926             break;
       
   927           case 'thumb':
       
   928             $scale_type = 'thumb';
       
   929             break;
       
   930           case 'raw':
       
   931             $raw_display = true;
       
   932             break;
       
   933           default:
       
   934             // height specification
       
   935             if ( preg_match('/^([0-9]+)x([0-9]+)$/', $param, $dims) )
       
   936             {
       
   937               $width = intval($dims[1]);
       
   938               $height = intval($dims[2]);
       
   939               break;
       
   940             }
       
   941             // not the height, so see if a plugin took this over
       
   942             // this hook requires plugins to return true if they modified anythin
       
   943             $code = $plugins->setHook('img_tag_parse_params');
       
   944             foreach ( $code as $cmd )
       
   945             {
       
   946               if ( eval($cmd) )
       
   947                 break 2;
       
   948             }
       
   949             // we would have broken out by now if a plugin properly handled this,
       
   950             // so just set the caption now.
       
   951             $caption = $param;
       
   952             break;
       
   953         }
       
   954       }
   885       
   955       
   886       if ( !isPage( $paths->nslist['File'] . $filename ) )
   956       if ( !isPage( $paths->nslist['File'] . $filename ) )
   887       {
   957       {
   888         $text = str_replace($full_tag, '[[' . makeUrlNS('File', $filename) . ']]', $text);
   958         $text = str_replace($full_tag, '[[' . makeUrlNS('File', $filename) . ']]', $text);
   889         continue;
   959         continue;
   890       }
   960       }
   891       
   961       
   892       if ( $scale_type == '|thumb' )
   962       if ( $scale_type == 'thumb' )
   893       {
   963       {
   894         $r_width  = 225;
   964         $r_width  = 225;
   895         $r_height = 225;
   965         $r_height = 225;
   896         
   966         
   897         $url = makeUrlNS('Special', 'DownloadFile/' . $filename, 'preview&width=' . $r_width . '&height=' . $r_height, true);
   967         $url = makeUrlNS('Special', 'DownloadFile/' . $filename, 'preview&width=' . $r_width . '&height=' . $r_height, true);
   925       
   995       
   926       $img_tag .= '/>';
   996       $img_tag .= '/>';
   927       
   997       
   928       $complete_tag = '';
   998       $complete_tag = '';
   929       
   999       
   930       if ( !empty($scale_type) && $caption != '|raw' )
  1000       if ( !empty($scale_type) && !$raw_display )
   931       {
  1001       {
   932         $complete_tag .= '<div class="thumbnail" ';
  1002         $complete_tag .= '<div class="thumbnail" ';
   933         $clear_text = '';
  1003         $clear_text = '';
   934         if ( !empty($clear) )
  1004         if ( !empty($clear) )
   935         {
  1005         {
   936           $side = ( $clear == '|left' ) ? 'left' : 'right';
  1006           $side = ( $clear == 'left' ) ? 'left' : 'right';
   937           $opposite = ( $clear == '|left' ) ? 'right' : 'left';
  1007           $opposite = ( $clear == 'left' ) ? 'right' : 'left';
   938           $clear_text .= "float: $side; margin-$opposite: 20px; width: {$r_width}px;";
  1008           $clear_text .= "float: $side; margin-$opposite: 20px; width: {$r_width}px;";
   939           $complete_tag .= 'style="' . $clear_text . '" ';
  1009           $complete_tag .= 'style="' . $clear_text . '" ';
   940         }
  1010         }
   941         $complete_tag .= '>';
  1011         $complete_tag .= '>';
   942         
  1012         
   946         
  1016         
   947         $mag_button = '<a href="' . makeUrlNS('File', $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>';
  1017         $mag_button = '<a href="' . makeUrlNS('File', $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>';
   948       
  1018       
   949         if ( !empty($caption) )
  1019         if ( !empty($caption) )
   950         {
  1020         {
   951           $cap = substr($caption, 1);
  1021           $complete_tag .= $mag_button . $caption;
   952           $complete_tag .= $mag_button . $cap;
       
   953         }
  1022         }
   954         
  1023         
   955         $complete_tag .= '</div>';
  1024         $complete_tag .= '</div>';
   956       }
  1025       }
   957       else if ( $caption == '|raw' )
  1026       else if ( $raw_display )
   958       {
  1027       {
   959         $complete_tag .= "$img_tag";
  1028         $complete_tag .= "$img_tag";
   960         $taglist[$i] = $complete_tag;
  1029         $taglist[$i] = $complete_tag;
   961         
  1030         
   962         $repl = "{$s_delim}e_img_{$i}{$f_delim}";
  1031         $repl = "{$s_delim}e_img_{$i}{$f_delim}";
   979       $complete_tag .= "\n\n";
  1048       $complete_tag .= "\n\n";
   980       $taglist[$i] = $complete_tag;
  1049       $taglist[$i] = $complete_tag;
   981       
  1050       
   982       $pos = strpos($text, $full_tag);
  1051       $pos = strpos($text, $full_tag);
   983       
  1052       
       
  1053       /*
   984       while(true)
  1054       while(true)
   985       {
  1055       {
   986         $check1 = substr($text, $pos, 3);
  1056         $check1 = substr($text, $pos, 3);
   987         $check2 = substr($text, $pos, 1);
  1057         $check2 = substr($text, $pos, 1);
   988         if ( $check1 == '<p>' || $pos == 0 || $check2 == "\n" )
  1058         if ( $check1 == '<p>' || $pos == 0 || $check2 == "\n" )
   990           // die('found at pos '.$pos);
  1060           // die('found at pos '.$pos);
   991           break;
  1061           break;
   992         }
  1062         }
   993         $pos--;
  1063         $pos--;
   994       }
  1064       }
   995       
  1065       */
       
  1066       
       
  1067       /*
   996       $repl = "{$s_delim}e_img_{$i}{$f_delim}";
  1068       $repl = "{$s_delim}e_img_{$i}{$f_delim}";
   997       $text = substr($text, 0, $pos) . $repl . substr($text, $pos);
  1069       $text = substr($text, 0, $pos) . $repl . substr($text, $pos);
   998       
  1070       
   999       $text = str_replace($full_tag, '', $text);
  1071       $text = str_replace($full_tag, '', $text);
       
  1072       */
       
  1073       $text = str_replace_once($full_tag, $complete_tag, $text);
  1000       
  1074       
  1001       unset($full_tag, $filename, $scale_type, $width, $height, $clear, $caption, $r_width, $r_height);
  1075       unset($full_tag, $filename, $scale_type, $width, $height, $clear, $caption, $r_width, $r_height);
  1002       
  1076       
  1003     }
  1077     }
       
  1078     
       
  1079     // if ( count($matches[0]) > 0 )
       
  1080     //   die('<pre>' . htmlspecialchars($text) . '</pre>');
  1004     
  1081     
  1005     return $text;
  1082     return $text;
  1006   }
  1083   }
  1007   
  1084   
  1008   /**
  1085   /**