includes/functions.php
changeset 32 4d87aad3c4c0
parent 22 d0314575e2f0
child 38 ed06961e54dd
equal deleted inserted replaced
31:dc8741857bde 32:4d87aad3c4c0
  1607   
  1607   
  1608   return $html;
  1608   return $html;
  1609   
  1609   
  1610 }
  1610 }
  1611 
  1611 
       
  1612 /**
       
  1613  * Using the same parsing code as sanitize_html(), this function adds <litewiki> tags around certain block-level elements
       
  1614  * @param string $html The input HTML
       
  1615  * @return string formatted HTML
       
  1616  */
       
  1617 
       
  1618 function wikiformat_process_block($html)
       
  1619 {
       
  1620   
       
  1621   $tok1 = "<litewiki>";
       
  1622   $tok2 = "</litewiki>";
       
  1623   
       
  1624   $block_tags = array('div', 'p', 'table', 'blockquote', 'pre');
       
  1625   
       
  1626   $len = strlen($html);
       
  1627   $in_quote = false;
       
  1628   $quote_char = '';
       
  1629   $tag_start = 0;
       
  1630   $tag_name = '';
       
  1631   $in_tag = false;
       
  1632   $trk_name = false;
       
  1633   
       
  1634   $diag = 0;
       
  1635   
       
  1636   $block_tagname = '';
       
  1637   $in_blocksec = 0;
       
  1638   $block_start = 0;
       
  1639   
       
  1640   for ( $i = 0; $i < $len; $i++ )
       
  1641   {
       
  1642     $chr = $html{$i};
       
  1643     $prev = ( $i == 0 ) ? '' : $html{ $i - 1 };
       
  1644     $next = ( ( $i + 1 ) == $len ) ? '' : $html { $i + 1 };
       
  1645     
       
  1646     // Are we inside of a quoted section?
       
  1647     if ( $in_quote && $in_tag )
       
  1648     {
       
  1649       if ( $quote_char == $chr && $prev != '\\' )
       
  1650         $in_quote = false;
       
  1651     }
       
  1652     elseif ( ( $chr == '"' || $chr == "'" ) && $prev != '\\' && $in_tag )
       
  1653     {
       
  1654       $in_quote = true;
       
  1655       $quote_char = $chr;
       
  1656     }
       
  1657     
       
  1658     if ( $chr == '<' && !$in_tag && $next == '/' )
       
  1659     {
       
  1660       // Iterate through until we've got a tag name
       
  1661       $tag_name = '';
       
  1662       $i++;
       
  1663       while(true)
       
  1664       {
       
  1665         $i++;
       
  1666         // echo $i . ' ';
       
  1667         $chr = $html{$i};
       
  1668         $prev = ( $i == 0 ) ? '' : $html{ $i - 1 };
       
  1669         $next = ( ( $i + 1 ) == $len ) ? '' : $html { $i + 1 };
       
  1670         $tag_name .= $chr;
       
  1671         if ( $next == '>' )
       
  1672           break;
       
  1673       }
       
  1674       // echo '<br />';
       
  1675       if ( in_array($tag_name, $block_tags) )
       
  1676       {
       
  1677         if ( $block_tagname == $tag_name )
       
  1678         {
       
  1679           $in_blocksec -= 1;
       
  1680           if ( $in_blocksec == 0 )
       
  1681           {
       
  1682             $block_tagname = '';
       
  1683             $i += 2;
       
  1684             // echo 'Finished wiki litewiki wraparound calc at pos: ' . $i;
       
  1685             $full_litewiki = substr($html, $block_start, ( $i - $block_start ));
       
  1686             $new_text = "{$tok1}{$full_litewiki}{$tok2}";
       
  1687             $html = substr($html, 0, $block_start) . $new_text . substr($html, $i);
       
  1688             
       
  1689             $i += ( strlen($tok1) + strlen($tok2) ) - 1;
       
  1690             $len = strlen($html);
       
  1691             
       
  1692             //die('<pre>' . htmlspecialchars($html) . '</pre>');
       
  1693           }
       
  1694         }
       
  1695       }
       
  1696       
       
  1697       $in_tag = false;
       
  1698       $in_quote = false;
       
  1699       $tag_name = '';
       
  1700       
       
  1701       continue;
       
  1702     }
       
  1703     else if ( $chr == '<' && !$in_tag && $next != '/' )
       
  1704     {
       
  1705       // start of a tag
       
  1706       $tag_start = $i;
       
  1707       $in_tag = true;
       
  1708       $trk_name = true;
       
  1709     }
       
  1710     else if ( !$in_quote && $in_tag && $chr == '>' )
       
  1711     {
       
  1712       if ( !in_array($tag_name, $block_tags) )
       
  1713       {
       
  1714         // Inline tag - reset and go to the next one
       
  1715         // echo '&lt;inline ' . $tag_name . '&gt; ';
       
  1716         
       
  1717         $in_tag = false;
       
  1718         $tag_name = '';
       
  1719         continue;
       
  1720       }
       
  1721       else
       
  1722       {
       
  1723         // echo '&lt;block: ' . $tag_name . ' @ ' . $i . '&gt;<br/>';
       
  1724         if ( $in_blocksec == 0 )
       
  1725         {
       
  1726           //die('Found a starting tag for a block element: ' . $tag_name . ' at pos ' . $tag_start);
       
  1727           $block_tagname = $tag_name;
       
  1728           $block_start = $tag_start;
       
  1729           $in_blocksec++;
       
  1730         }
       
  1731         else if ( $block_tagname == $tag_name )
       
  1732         {
       
  1733           $in_blocksec++;
       
  1734         }
       
  1735         
       
  1736         $in_tag = false;
       
  1737         $tag_name = '';
       
  1738         continue;
       
  1739       }
       
  1740     }
       
  1741     elseif ( $in_tag && $trk_name )
       
  1742     {
       
  1743       $is_alphabetical = ( strtolower($chr) != strtoupper($chr) || in_array($chr, array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9')) || $chr == '?' || $chr == '!' || $chr == '-' );
       
  1744       if ( $is_alphabetical )
       
  1745         $tag_name .= $chr;
       
  1746       else
       
  1747       {
       
  1748         $trk_name = false;
       
  1749       }
       
  1750     }
       
  1751     
       
  1752     // Tokenization complete
       
  1753     
       
  1754   }
       
  1755   
       
  1756   $regex = '/' . str_replace('/', '\\/', preg_quote($tok2)) . '([\s]*)' . preg_quote($tok1) . '/is';
       
  1757   // die(htmlspecialchars($regex));
       
  1758   $html = preg_replace($regex, '\\1', $html);
       
  1759   
       
  1760   return $html;
       
  1761   
       
  1762 }
       
  1763 
  1612 function htmlalternatives($string)
  1764 function htmlalternatives($string)
  1613 {
  1765 {
  1614   $ret = '';
  1766   $ret = '';
  1615   for ( $i = 0; $i < strlen($string); $i++ )
  1767   for ( $i = 0; $i < strlen($string); $i++ )
  1616   {
  1768   {
  2100   {
  2252   {
  2101     return $num;
  2253     return $num;
  2102   }
  2254   }
  2103 }
  2255 }
  2104 
  2256 
       
  2257 /**
       
  2258  * Injects a string into another string at the specified position.
       
  2259  * @param string The haystack
       
  2260  * @param string The needle
       
  2261  * @param int    Position at which to insert the needle
       
  2262  */
       
  2263 
       
  2264 function inject_substr($haystack, $needle, $pos)
       
  2265 {
       
  2266   $str1 = substr($haystack, 0, $pos);
       
  2267   $pos++;
       
  2268   $str2 = substr($haystack, $pos);
       
  2269   return "{$str1}{$needle}{$str2}";
       
  2270 }
       
  2271 
  2105 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  2272 //die('<pre>Original:  01010101010100101010100101010101011010'."\nProcessed: ".uncompress_bitfield(compress_bitfield('01010101010100101010100101010101011010')).'</pre>');
  2106 
  2273 
  2107 ?>
  2274 ?>