Guest User

Untitled

a guest
Oct 16th, 2011
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 41.60 KB | None | 0 0
  1. <?php
  2. /**
  3. * TEXT HTML's TAGS EMULATOR
  4. * Compatibility: PHP >= 4.1
  5. * Page Example: http://www.3site.it/varie/codeLighter/index.php
  6. * ---------------------------------------------------------------------
  7. * LAST FIXED:   multiple URL inline, increase speed convertion, adjusted code, color and list tags, unclosed /* comment.
  8. *       Totally rewrote and fixed wrap_convert() function with url/img/mail preservation.
  9. * LAST ADDS:    [AS] tag with different highlight FlashMX like
  10. *       you can add one or more personal tags
  11. * LAST CHANGES: IMG now use [IMG="{url}"]{comment}[/IMG] as quote and url.
  12. *       Now [PHP] & [AS] use monospace font on.
  13. *       Now are right both [URL="{URL}"] , [URL={URL}]
  14. *
  15. * ---------------------------------------------------------------------
  16. * WHAT IS THIS:
  17. * HTML 4.01 W3C Strict Standard VAlidated tags emulator with:
  18. *   personalizable style sheet
  19. *   great [tag] engine
  20. *   JavaScript crossbrowser for checks and helps
  21. *   automatic form / textarea / buttons generation
  22. *
  23. * ---------------------------------------------------------------------
  24. * HOW TO USE:
  25. * $codeLighter = new codeLighter();
  26. * echo $codeLighter->convert($text_var);
  27. * WARNING: the better way to use this script is give an htmlentities($text_var);
  28. * so if you haven't converted $text_var you need to do this.
  29. * Example:
  30. *   echo $codeLighter->convert( htmlentities($text_var) );
  31. *
  32. * ---------------------------------------------------------------------
  33. * WHAT YOU CAN DO WITH THIS:
  34. * Accepted tags: B, U, I, CODE, PHP, AS, QUOTE, LIST, MAIL, URL, IMG, FONT, SIZE, COLOR, CENTER, RIGHT
  35. * B => [b]{TEXT}[/b]
  36. * U => [u]{TEXT}[/u]
  37. * I => [i]{TEXT}[/i]
  38. * CENTER => [center]{center text}[/center]
  39. * RIGHT => [right]{right text}[/right]
  40. * CODE => [code]{code text}[/code]
  41. * PHP => [php]{php text}[/php]
  42. * ACTIONSCRIPT => [as]{action script text}[/as]
  43. * QUOTE => [quote]{quoted text}[/quote] | [quote="posted from"]{quoted text}[/quote]
  44. * LIST => [list][*]{TEXT}[/list] | [list=1][*]{TEXT}[/list]
  45. *   type:   NULL => default, unordered list
  46. *       1 => numeric ordered list
  47. *       [ for alphabetic list write to W3.org ... they wrote "Safe for kids" with DL sintax ... ]
  48. *
  49. *   Example: "[list]
  50. *         [*]something
  51. *         [*]something else
  52. *         [/list]"
  53. *   Example: "[list=1]
  54. *         [*]something
  55. *         [*]something else
  56. *         [/list]"
  57. * MAIL => {valid email addres} | [mail]{valid email addres}[/mail] | [mail={valid email addres}]{TEXT}[/mail]
  58. *   Example: "[mail][email protected][/mail]"
  59. *   Example: "[[email protected]]my email[/mail]"
  60. * URL => {valid url addres} | [url]{valid url addres}[/url] | [url="{valid url addres}"]{TEXT}[/url] | [url={valid url addres}]{TEXT}[/url]
  61. *   Example: "[url]http://www.3site.it/[/url]"
  62. *   Example: "[url="http://www.3site.it/"]hello world![/url]"
  63. *   Example: "[url=http://www.3site.it/]hello world![/url]"
  64. * IMG => [img]{valid url addres with img}[/img] | [img="{valid url addres with img}"]{ALT/TITLE}[/img] | [img={WIDTHxHEIGHT}]{valid url addres with img}[/img]
  65. *   Example: "[img]http://www.3site.it/grafica/3site1.gif[/img]"
  66. *   Example: "[img="http://www.3site.it/grafica/3site1.gif"]this is my site's logo[/img]"
  67. *
  68. *   Example: "[IMG=100x38]http://www.3site.it/grafica/3site1.gif[/IMG]"
  69. *   NOTE: you need to auth this last type of parsing with $codeLighter->imgSize(maxWidth, maxHeight); function.
  70. * FONT => [font={valid font type}]{TEXT}[/font]
  71. *   Example: "[font=courier]hello[/font] [font=arial]world[/font] !"
  72. *
  73. * SIZE => [size={number between 1 and 9}]{TEXT}[/size]
  74. *   Example: "[size=1]hello[/size] [size=9]world[/size] !"
  75. * COLOR => [color={valid hex value}]{TEXT}[/color] | [color=#{valid hex value}]{TEXT}[/color] | [color={valid color name}]{TEXT}[/color]
  76. *   Example: "[color=#002255]hello[/color] [color=red]world[/color] !"
  77. *
  78. * ---------------------------------------------------------------------
  79. * INTERNAL VARIABLES
  80. * $codeLighter->maxUrlLenght;
  81. *   This variable set max url length for validated link.
  82. *   DEFAULT: 100
  83. *   NOTE: you need to set this before ->convert() function.
  84. *
  85. * TEXTAREA SETTINGS [ optional ]
  86. *
  87. * $codeLighter->textareaButtons;
  88. *   This variable will return a buttons list with text's shortcuts dedicated for a textArea with id="codeLighterInternalTextarea"
  89. *   NOTE: you need to print $codeLighter->jsLighter; to enable JavaScript buttons functions
  90. *       SHORTCUTS: [B] [U] [I] [IMG] [CODE] [PHP] [URL] [MAIL]
  91. *   NOTE: You can specify different buttons with $codeLighter->area_buttons;
  92. *
  93. *   Example:
  94. *       echo $codeLighter->jsLighter; // dedicated JavaScript functions ofr these buttons
  95. *       echo $codeLighter->textareaButtons; // buttons
  96. *
  97. * $codeLighter->area_buttons;
  98. *   This variable modify buttons at the top of TextArea.
  99. *   WARNING: You need to inizialize the textarea with $codeLighter->createTextareaLighter(); if you 'll not use automatic form's creator.
  100.  
  101. *   DEFAULT: Array('B', 'U', 'I', 'IMG', 'CENTER', 'CODE', 'PHP', 'AS');
  102. *   MAX USAGE: Array('MAIL', 'URL', 'PHP', 'CODE', 'QUOTE', 'IMG', 'LIST', 'B', 'I', 'U', 'CENTER', 'RIGHT', 'AS');
  103. *   NOTE: LIST need [*] simbol
  104. *
  105. *   Example:
  106. *   $codeLighter->area_buttons = Array('PHP', 'CODE', 'B', 'I', 'U'); // will create only these ordered buttons
  107. *   NOTE: You must specify this variable befor textarea print.
  108. *
  109. *   Example with no automatic form generator:
  110. *   $codeLighter->area_buttons = Array('PHP', 'CODE', 'B', 'I', 'U'); // will create only these ordered buttons
  111. *   $codeLighter->createTextareaLighter();
  112. *   echo $codeLighter->textareaButtons;
  113. *   echo $codeLighter->textarea;
  114. *
  115. * $codeLighter->textarea;
  116. *   This variable will return a dedicated CSS textarea's class style with:
  117. *       class="codeLighterTextarea"
  118. *       id="codeLighterInternalTextarea"
  119. *       name="codeLighterText"
  120. *       clos="74"
  121. *       rows="10"
  122. *
  123. * @Author       Andrea Giammarchi
  124. * @Alias        andr3a
  125. * @Site         http://www.3site.it
  126. * @Mail         [email protected]
  127. * @Version      0.2c
  128. * @Begin        31/10/2003
  129. * @lastModify       08/02/2004 18:48
  130. * @Demo         http://www.3site.it/varie/codeLighter/index.php
  131. */
  132. Class codeLighter {
  133.     var $style, $output, $jsLighter, $textarea, $textareaButtons, $fake, $restriction, $newRestriction, $textarea_check, $area_buttons, $codeConverter;
  134.     var $css_code, $css_a_link, $css_a_visited, $css_a_hover, $css_quote, $css_php, $css_img, $css_button, $css_textarea, $css_textarea_span;
  135.     var $maxUrlLenght, $maxImgWidth, $maxImgHeight;
  136.     var $preg_a, $preg_b, $preg_c, $preg_d, $preg_e, $preg_f;
  137.     /**
  138.     * Public constructor.
  139.     * @Example      $codeLighter = new codeLighter();
  140.     */
  141.     function codeLighter() {
  142.         $this->newRestriction = Array();
  143.         $this->codeConverter = Array();
  144.         $this->maxImgWidth = false;
  145.         $this->maxImgHeight = false;
  146.         $this->maxUrlLenght = 100;
  147.         $this->jsAddLighter();
  148.         $this->createTextareaLighter();
  149.         $this->fake = 0;
  150.         $def_rest = Array("mail", "url", "php", "as", "code", "quote", "font", "size", "color", "img", "list", "b", "i", "u", "center", "right");
  151.         $this->restrict($def_rest);
  152.         $this->css_code = "
  153.             border: 1px solid silver;
  154.             padding: 8pt;
  155.             font-family: courier, monospace, sans-serif;
  156.             font-size: 8pt;
  157.             color: #000033;
  158.             background-color: #FAFAFA;
  159.         ";
  160.         $this->css_a_link = "
  161.             text-decoration: none;
  162.             color: #000080;
  163.             font-family: Verdana, Helvetica, sans-serif;
  164.         ";
  165.         $this->css_a_visited = "
  166.             text-decoration: none;
  167.             color: #002255;
  168.             font-family: Verdana, Helvetica, sans-serif;
  169.         ";
  170.         $this->css_a_hover = "
  171.             text-decoration: underline;
  172.             color: #0000AA;
  173.             font-family: Verdana, Helvetica, sans-serif;
  174.         ";
  175.         $this->css_quote = "
  176.             width: 100%;
  177.             height: 1%;
  178.             border: 1px solid silver;
  179.             padding: 8pt;
  180.             font-family: Verdana, Helvetica, sans-serif;
  181.             font-size: 8pt;
  182.             color: #000000;
  183.             background-color: #F1F1F1;
  184.         ";
  185.         $this->css_php = "
  186.             border: 1px solid silver;
  187.             padding: 8px;
  188.             font-family: courier, monospace, sans-serif;
  189.             font-size: 8pt;
  190.             color: #000000;
  191.             background-color: #FDFDFD;
  192.         ";
  193.         $this->css_img = "
  194.             border: 0px;
  195.         ";
  196.         $this->css_button = "
  197.             border: 1px solid silver;
  198.             font-family: Verdena, Helvetica, sans-serif;
  199.             font-weight: bold;
  200.             color: #787878;
  201.             background-color: #EFEFEF;
  202.             font-size: 7pt;
  203.         ";
  204.         $this->css_textarea = "
  205.             border: 1px solid silver;
  206.             font-family: Verdena, Helvetica, sans-serif;
  207.             font-size: 9pt;
  208.         ";
  209.         $this->css_textarea_span = "
  210.             font-family: Verdana, Helvetica, sans-serif;
  211.             font-size: 7pt;
  212.             font-weight: normal;
  213.         ";
  214.         $this->setCSS("code", $this->css_code);
  215.         $this->setCSS("a:link", $this->css_a_link);
  216.         $this->setCSS("a:visited", $this->css_a_visited);
  217.         $this->setCSS("a:hover", $this->css_a_hover);
  218.         $this->setCSS("php", $this->css_php);
  219.         $this->setCSS("img", $this->css_img);
  220.         $this->setCSS("button", $this->css_button);
  221.         $this->setCSS("textarea", $this->css_textarea);
  222.         $this->setCSS("textarea_check", $this->css_textarea_span);
  223.         $this->textarea_check = "Opened Tags";
  224.         $this->area_buttons = Array('B', 'U', 'I', 'IMG', 'CENTER', 'CODE', 'PHP', 'AS');
  225.         $this->preg_a = "/(?i)(^|\ |\n|\r|\t)(http|https|ftp|irc|ed2k|gopher|telnet|gopher|telnet)(\:\/\/)([^\[\<\>[:space:]]+)($|\ |\n|\r|\t)/";
  226.         $this->preg_b = "/(?i)(^|\ |\n|\r|\t)(w){3}(.)([^\[\<\>[:space:]]+)($|\ |\n|\r|\t)/";
  227.         $this->preg_c = "/(?i)\[url\=(\&quot;|\")(http|https|ftp|irc|ed2k|gopher|telnet|gopher|telnet)(\:\/\/)([^\<\>[:space:]]+?)(\&quot;|\")\]([^\<\>]+?)(\[\/url\])/i";
  228.         $this->preg_d = "/(?i)(\[url\=)(w){3}(\.)([^\[\<\>[:space:]]+?)(\])([^\"\<\>]+?)(\[\/url\])/i";
  229.         $this->preg_e = "/(?i)(\[url\])(http|https|ftp|irc|ed2k|gopher|telnet)(\:\/\/)([^\<\>[:space:]]+?)(\[\/url\])/i";
  230.         $this->preg_f = "/(?i)\[url\]www.([^\<\>[:space:]]+?)\[\/url\]/i";
  231.         $this->preg_g = "/(?i)\[url\=(http|https|ftp|irc|ed2k|gopher|telnet|gopher|telnet)(\:\/\/)([^\<\>[:space:]]+?)\]([^\<\>]+?)(\[\/url\])/i";
  232.         $this->createTextareaLighter();
  233.     }
  234.     /**
  235.     * Public, use to change defaults CSS.
  236.     *
  237.     * $codeLighter->setCSS($type, $value);
  238.     *       With this function you can modify CSS Style
  239.     *       Print in your output (in <head> </head> tags to respect W3C HTML 4.01 standard)
  240.     *       the variable $codeLighter->style;
  241.     *       Example:
  242.     *           echo $codeLighter->style;
  243.     *
  244.     *       $type => code - a:link - a:visited - a:hover - quote - php - img - button - textarea - textarea_check
  245.     *       NOTE:   these styles have the same CSS information for:
  246.     *               code => table
  247.     *               quote => table
  248.     *               php => table
  249.     *               img => img
  250.     *               textarea => textarea
  251.     *               textarea_check => span
  252.     *               button => input
  253.     *               a:link => a:link
  254.     *               a:visited => a:visited
  255.     *               a:hover => a:hover
  256.     *
  257.     *       $value => valid CSS information
  258.     *
  259.     *       Example:
  260.     *       $CSS_code = "
  261.     *           width: 100%;
  262.     *           height: 1%;
  263.     *           border: 1px solid silver;
  264.     *           padding: 8pt;
  265.     *           font-family: Verdana, Helvetica, sans-serif;
  266.     *           font-size: 8pt;
  267.     *           color: #000033;
  268.     *           background-color: #FAFAFA;
  269.     *       ";
  270.     *       $codeLighter->setCSS("code", $CSS_code);
  271.     *       NOTE: if you want change default CSS class values you must use setCSS , then print $codeLighter->style;
  272.     *
  273.     * @Param    String      what type of CSS you want change ( code, a:link, a:visited, a:hover, quote, php, as, img, button, textarea, textarea_check )
  274.     * @Param    String      valid CSS information.
  275.     */
  276.     function setCSS($type, $def) {
  277.         if( strtolower($type) == "code" ) {
  278.             $this->css_code = $def;
  279.         }
  280.         elseif( strtolower($type) == "a:link" ) {
  281.             $this->css_a_link = $def;
  282.         }
  283.         elseif( strtolower($type) == "a:visited" ) {
  284.             $this->css_a_visited = $def;
  285.         }
  286.         elseif( strtolower($type) == "a:hover" ) {
  287.             $this->css_a_hover = $def;
  288.         }
  289.         elseif( strtolower($type) == "quote" ) {
  290.             $this->css_quote = $def;
  291.         }
  292.         elseif( strtolower($type) == "php" || strtolower($type) == "as" ) {
  293.             $this->css_php = $def;
  294.         }
  295.         elseif( strtolower($type) == "img" ) {
  296.             $this->css_img = $def;
  297.         }
  298.         elseif( strtolower($type) == "button" ) {
  299.             $this->css_button = $def;
  300.         }
  301.         elseif( strtolower($type) == "textarea" ) {
  302.             $this->css_textarea = $def;
  303.         }
  304.         elseif( strtolower($type) == "textarea_check" ) {
  305.             $this->css_textarea_span = $def;
  306.         }
  307.         else {
  308.             die("<br /><b>WARNING:</b> \$type given ( <b>{$type}</b> ) doesn't exist.<br />Accepted types: code, a:link, a:visited, a:hover, quote, php, img, button, textarea .<br />");
  309.         }
  310.         $this->style = "
  311.         <style type=\"text/css\">
  312.         pre.codeLighterCode {".$this->css_code."}
  313.         table.codeLighterQuote {".$this->css_quote."}
  314.         pre.codeLighterPHP {".$this->css_php."}
  315.         img.codeLighterImg {".$this->css_img."}
  316.         a.codeLighterLink:link {".$this->css_a_link."}
  317.         a.codeLighterLink:visited {".$this->css_a_visited."}
  318.         a.codeLighterLink:hover {".$this->css_a_hover."}
  319.         textarea.codeLighterTextarea {".$this->css_textarea."}
  320.         input.codeLighterButton {".$this->css_button."}
  321.         span.codeLighterSpan {".$this->css_textarea_span."}
  322.         </style>
  323.         ";
  324.         return true;
  325.     }
  326.     /**
  327.     * Public, use to add one or more tags to converter.
  328.     * 2 ways to add new tags
  329.     *   $codeLighter->addTag( "myTag", Array("#notes", "#variables", "#strings", "#generic") );
  330.     *   $codeLighter->addTag( "myTag", "font-style: myStyle; border: myBorder;" );
  331.     * If you need another language to highlight use first method.
  332.     * If you need a table with a style ( not indented, not highlighted ) use second method BEFORE echo your
  333.     * $codeLighter->style;
  334.     * @Param    String      internal tag name. Example "python" ( to convert [python]{code}[/python] )
  335.     * @Param    Array/String    Array with 4 colors ( "#notes", "#variables", "#strings", "#generic" ) for code
  336.     *               highlighted, String with regular CSS style for generic persola table
  337.     */
  338.     function addTag( $tagName, $tagStyle ) {
  339.         $tagName = strtolower( $tagName );
  340.         $this->newRestriction[] = Array( $tagName , $tagStyle );
  341.         if( !is_Array( $tagStyle ) ) {
  342.             $newStyle = explode( "</style>", $this->style );
  343.             $newStyle = $newStyle[0];
  344.             $newset = "table.codeLighter{$tagName} { {$tagStyle} }";
  345.             $this->style = $newStyle.$newset."
  346.             </style>
  347.             ";
  348.         }
  349.     }
  350.     /**
  351.     * Public, use to allow [IMG=width_X_height]{IMAGE URL}[/IMG] tag
  352.     *
  353.     * $codeLighter->imgSize($maxWidth, $maxHeight);
  354.     *   This function allow parser to manipolate size of imgages.
  355.     *   Converter will accept a tag like this:
  356.     *   [IMG=100x38]http://www.3site.it/grafica/3site1.gif[/IMG]
  357.     *   $maxWidth -> integer that will be max apceptable width value for an img
  358.     *   $maxHeigh -> integer that will be max apceptable height value for an img
  359.     *   NOTE: if parameters are above these value, the image will be converted with these for width or height or width & height.
  360.     * @Param    int     Max width
  361.     * @Param    int     Max height
  362.     */
  363.     function imgSize($w, $h) {
  364.         $this->maxImgWidth = $w;
  365.         $this->maxImgHeight = $h;
  366.         $this->restriction[] = "PrivateCheckImgSize";
  367.     }
  368.     function globalRegCheck( $t ) {
  369.         $preg_modeMail = "(([a-z0-9]+[\._\-]?){1,3}([a-z0-9])*\@([a-z0-9]+[\._\-]?){1,3}([a-z0-9])*\.[a-z]{2,6})";
  370.             $preg_mail1 = "/(?i)(\[mail\])?{$preg_modeMail}(\[\/mail\])?/";
  371.         $preg_mail2 = "/(?i)\[mail\={$preg_modeMail}\]/";
  372.         $preg_imgModel = "(http|https|ftp)(\:\/\/)([^\<\>[:space:]]+?)";
  373.             $preg_img1 = "/(?i)\[img\]{$preg_imgModel}\[\/img\]/i";
  374.             $preg_img2 = "/(?i)\[img\=(\&quot;|\"){$preg_imgModel}(\&quot;|\")\]/i";
  375.             $preg_img3 = "/(?i)\[img\=([0-9]{1,}x[0-9]{1,})\]{$preg_imgModel}\[\/img\]/i";
  376.         $preg_urlModel = "(http|https|ftp|irc|ed2k|gopher|telnet)(\:\/\/)([^\[\<\>[:space:]]+?)";
  377.         $preg_url1 = "/(?i)^{$preg_urlModel}/i";
  378.         $preg_url2 = "/(?i)^((w){3}(.)([^\[\<\>[:space:]]+))/i";
  379.         $preg_url3 = "/(?i)(\[url\=)(w){3}(\.)([^\[\<\>[:space:]]+?)(\])/i";
  380.         $preg_url4 = "/(?i)\[url\=(\&quot;|\")?{$preg_urlModel}(\&quot;|\")?\]/i";
  381.         $preg_url5 = "/(?i)(\[url\]){$preg_urlModel}(\[\/url\])/i";
  382.         $preg_url6 = "/(?i)\[url\]www.([^\<\>[:space:]]+?)\[\/url\]/i";
  383.         if( preg_match($preg_url6, $t) || preg_match($preg_url5, $t) || preg_match($preg_url4, $t) || preg_match($preg_url3, $t) || preg_match($preg_url2, $t) || preg_match($preg_url1, $t) || preg_match($preg_modeMail, $t) || preg_match($preg_mail1, $t) || preg_match($preg_mail2, $t) || preg_match($preg_img1, $t) || preg_match($preg_img2, $t) || preg_match($preg_img3, $t) ) {
  384.             return true;
  385.         }
  386.         else {
  387.             return false;
  388.         }
  389.     }
  390.     function wrapConvert( $text, $size, $force = true, $tag = " " ) {
  391.         $withN = explode("\n\r", trim($text));
  392.         $globalWrap = "";
  393.         for( $a=0; $za=count($withN), $a < $za; $a++ ) {
  394.             $ex = explode(" ", trim($withN[$a]));
  395.             for( $b=0; $zb = count($ex), $b < $zb; $b++ ) {
  396.                 if( $this->globalRegCheck($ex[$b]) ) {
  397.                     $globalWrap .= $ex[$b];
  398.                 }
  399.                 else {
  400.                     $provvisory = "";
  401.                     $where = 0;
  402.                     $ex[$b] = $this->unhtmlentities( $ex[$b] );
  403.                     if($force == false) {
  404.                         if( strlen($ex[$b]) > $size && !is_numeric($ex[$b]) ) {
  405.                             for($c=0; $zc = (ceil(strlen($ex[$b])/$size)), $c < $zc; $c++) {
  406.                                 $provvisory .= substr($ex[$b],$where,$size).$tag;
  407.                                 $where += $size;
  408.                             }
  409.                         }
  410.                         else {
  411.                             $provvisory = $ex[$b];
  412.                         }
  413.                     }
  414.                     else {
  415.                         if( strlen($ex[$b])>$size ) {
  416.                             for($c=0; $zd = (ceil(strlen($ex[$b])/$size)), $c < $zd; $c++) {
  417.                                 $provvisory .= substr($ex[$b],$where,$size).$tag;
  418.                                 $where += $size;
  419.                             }
  420.                         }
  421.                         else {
  422.                             $provvisory = $ex[$b];
  423.                         }
  424.                     }
  425.                     $globalWrap .= htmlentities( $provvisory );
  426.                 }
  427.                 $globalWrap .= " ";
  428.             }
  429.             $globalWrap .= "\n\r";
  430.         }
  431.         return $globalWrap;
  432.     }
  433.     /**
  434.     * Public, use to modify text without delete valid urls , img or emails tags
  435.     *
  436.     * $codeLighter->wrap_convert($text_var, $max_length [, $convert_numerics [, $separator]]);
  437.     *   This function will parse the text as ->convert(); but you can specify max words lenght.
  438.     *   Such PHP's wordwrap function, this method allow you to chose max lenght of a word and separator type.
  439.     *
  440.     *   $text_var -> text with htmlentities(); // as needed to ->convert();
  441.     *   $max_length -> max words length // will truncate with chosed separator
  442.     *   $convert_numerics -> DEFAULT: true // will truncate numbers too
  443.     *                with false value will not truncate numbers
  444.     *   $separator -> DEFAULT: " " // only trybcated words will have this kind of separator [ not as wordwrap, separate everything ... ]
  445.     *   NOTE: this kind of converter doesn't truncate links/emails, use ->maxUrlLenght to manage links length
  446.     * @Param    String      your not yet converted string
  447.     * @Param    int     max word length
  448.     * @Param    boolean     force numeric convertion ( default: true )
  449.     * @Param    String      separator tag ( default: " " )
  450.     */
  451.     function wrap_convert( $text, $size, $force = true, $tag = " " ) {
  452.         return $this->convert( $this->wrapConvert($text, $size, $force, $tag) );
  453.     }
  454.     function ereg_engine($st, $a_name, $pre, $suf) {
  455.         while( preg_match( "/(?i)\[{$a_name}\]([^\a]+?)\[\/{$a_name}\]/i",  $st ) ) {
  456.             $st = preg_replace("/(?i)\[{$a_name}\]([^\a]+?)\[\/{$a_name}\]/i", $pre."\\1".$suf, $st);
  457.         }
  458.         return $st;
  459.     }
  460.     function case_insensitive_check($st, $tag) {
  461.         $st = preg_replace( "/(?i)\[{$tag}\]([^\a]+?)\[\/{$tag}\]/i", "[{$tag}]\\1[/{$tag}]", $st );
  462.         return $st;
  463.     }
  464.     function unhtmlentities($mystring) {
  465.         $phpv = explode(".", phpversion());
  466.         if($phpv[0]==4 && $phpv[1]<3) {
  467.             // FROM: www.php.net
  468.             $trans_tbl = get_html_translation_table(HTML_ENTITIES);
  469.             $trans_tbl = array_flip ($trans_tbl);
  470.             return strtr($mystring, $trans_tbl);
  471.         }
  472.         else {
  473.             return html_entity_decode($mystring);
  474.         }
  475.     }
  476.     function codeModifier( $mod, &$st, &$pre_table_desc, $c1="#FF8000", $c2="#0000BB", $c3="#DD0000", $c4="#007700", $btw = false ) {
  477.         if( in_Array( $mod, $this->restriction ) || $btw != false ) {
  478.             preg_match_all("/(?i)\[{$mod}\]([^\a]+?)\[\/{$mod}\]/i", $st, $php);
  479.             $countPhp = count( $php[1] );
  480.             for( $a = 0; $a < $countPhp; $a++ ) {
  481.                 $elaborate = trim($php[1][$a]);
  482.                 $check_add_pre = false;
  483.                 $check_add_suf = false;
  484.                 if((strpos($elaborate, "&lt;?php") === false) && (strpos($elaborate, "&lt;?PHP") === false) && (strpos($elaborate,"&lt;?") === false)) {
  485.                     $elaborate = "<?php\n".trim($elaborate);
  486.                     $check_add_pre = true;
  487.                 }
  488.                 if((strpos($elaborate, "?&gt;") === false)) {
  489.                     $elaborate = $elaborate."?>";
  490.                     $check_add_suf = true;
  491.                 }
  492.                 $preserve = md5(" \n\r");
  493.                 $elaborate = preg_replace("/( )([\\n\\r]+){1}( )/", $preserve, $elaborate);
  494.                 $elaborate = preg_replace("/( )([\\n\\r^ ]+){1}/", $preserve, $elaborate);
  495.                 $elaborate .= "*/";
  496.                 $elaborate = highlight_string( $this->unhtmlentities( $elaborate ), true );
  497.                 if( $check_add_pre == true ) {
  498.                     $elaborate = str_replace("<font color=\"#0000BB\">&lt;?php<br /></font>", "", $elaborate);
  499.                     $elaborate = str_replace("&lt;?php<br />", "", $elaborate);
  500.                 }
  501.                 if( $check_add_suf == true ) {
  502.                     $elaborate = str_replace("<font color=\"#0000BB\">?&gt;</font>", "", $elaborate);
  503.                     $elaborate = str_replace("?&gt;", "", $elaborate);
  504.                 }
  505.                 $elaborate = preg_replace("/(\\n|\\r)/", "", $elaborate );
  506.                 $elaborate = str_replace($preserve, "\r", $elaborate );
  507.                 $elaborate = str_replace("<font color=\"", "<span style=\"color: ", $elaborate);
  508.                 $elaborate = str_replace("#FF8000", $c1, $elaborate);
  509.                 $elaborate = str_replace("#0000BB", $c2, $elaborate);
  510.                 $elaborate = str_replace("#DD0000", $c3, $elaborate);
  511.                 $elaborate = str_replace("#007700", $c4, $elaborate);
  512.                 $elaborate = str_replace("</font>", "</span>", $elaborate);
  513.                 $elaborate = str_replace("<code>", "", $elaborate);
  514.                 $elaborate = str_replace("</code>", "", $elaborate);
  515.                 if( substr($elaborate, ( strlen($elaborate) - 9 ), 9) == "*/</span>" ) {
  516.                     $elaborate = substr($elaborate, 0, ( strlen($elaborate) - 9 ) );
  517.                 }
  518.                 $st = $this->case_insensitive_check($st, "php");
  519.                 $pre = "{$pre_table_desc}".strtoupper($mod)."</span><pre class=\"codeLighterPHP\">";
  520.                 $toChange = md5("[{$mod}]{$php[1][$a]}[/{$mod}]");
  521.                 $this->codeConverter[] = Array( $toChange, $pre.$elaborate."</pre>" );
  522.                 $st = str_replace( "[{$mod}]{$php[1][$a]}[/{$mod}]", $toChange, $st);
  523.             }
  524.         }
  525.     }
  526.     /**
  527.     * Global function.
  528.     * Converter engine.
  529.     *
  530.     * @Params   no-params   don't need params.
  531.     */
  532.     function convert($st) {
  533.         if(get_magic_quotes_gpc()) {
  534.             $st = stripslashes($st);
  535.         }
  536.         $pre_table_desc = "<span style=\"font-family: Verdana, Helvetica, sans-serif; font-size: 7pt; font-style: normal; text-decoration: none; font-weight: bold;\">";
  537.         $this->codeModifier( "php", $st, $pre_table_desc );
  538.         $this->codeModifier( "as", $st, $pre_table_desc, "#898989", "#000080", "#0000EE", "#000000" );
  539.         if(in_Array("url", $this->restriction)) {
  540.             $preg_searc = Array(
  541.             "/(?i)(^|\ |\n|\r|\t)(ed2k\:\/\/\|file\|)([^\|\<\>[:space:]]+)(\|[^\[\<\>[:space:]]+)($|\ |\n|\r|\t)/",
  542.             $this->preg_b,
  543.             $this->preg_d,
  544.             $this->preg_a,
  545.             $this->preg_e,
  546.             $this->preg_f
  547.             );
  548.             $preg_replace = Array(
  549.             "\\1<a class=\"codeLighterLink\" href=\"\\2\\3\\4\" onClick=\"window.open(this.href, '_blank'); return false;\">\\3</a>\\5",
  550.             "\\1<a class=\"codeLighterLink\" href=\"http://www\\3\\4\" onClick=\"window.open(this.href, '_blank'); return false;\">www\\3\\4</a>\\5",
  551.             "<a class=\"codeLighterLink\" href=\"http://www\\3\\4\" onClick=\"window.open(this.href, '_blank'); return false;\">\\6</a>",
  552.             "\\1[url]\\2\\3\\4[/url]\\5",
  553.             "<a class=\"codeLighterLink\" href=\"\\2\\3\\4\" onClick=\"window.open(this.href, '_blank'); return false;\">\\2\\3\\4</a>",
  554.             "<a class=\"codeLighterLink\" href=\"http://www.\\1\" onClick=\"window.open(this.href, '_blank'); return false;\">www.\\1</a>"
  555.             );
  556.             $st = preg_replace($preg_searc, $preg_replace, $st);
  557.         }
  558.         $array_lighter = Array(
  559.             Array(
  560.             "name" => "i",
  561.             "pre" => "<i>",
  562.             "suf" => "</i>",
  563.             ),
  564.             Array(
  565.             "name" => "u",
  566.             "pre" => "<span style=\"text-decoration: underline;\">",
  567.             "suf" => "</span>",
  568.             ),
  569.             Array(
  570.             "name" => "b",
  571.             "pre" => "<b>",
  572.             "suf" => "</b>",
  573.             ),
  574.             Array(
  575.             "name" => "code",
  576.             "pre" => "{$pre_table_desc}code</span><pre class=\"codeLighterCode\">",
  577.             "suf" => "</pre>",
  578.             ),
  579.             Array(
  580.             "name" => "quote",
  581.             "pre" => "{$pre_table_desc}quote</span><table class=\"codeLighterQuote\"><tr valign=\"top\"><td align=\"left\">",
  582.             "suf" => "</td></tr></table>",
  583.             )
  584.         );
  585.         for($a=0; $a<count($array_lighter); $a++) {
  586.             $a_name = $array_lighter[$a]["name"];
  587.             if( in_Array($a_name, $this->restriction) ) {
  588.                 $st = $this->ereg_engine($st, $a_name, $array_lighter[$a]["pre"], $array_lighter[$a]["suf"]);
  589.             }
  590.         }
  591.         $r = "([a-z0-9]+[\._\-]?){1,3}([a-z0-9])*";
  592.         $reg_mail2 = "({$r}\@{$r}\.[a-z]{2,6})";
  593.         $reg_mail = "(^|\ |\n|\r|\t){$reg_mail2}(\ |\n|\r|\t|$)";
  594.         if(in_Array("mail", $this->restriction)) {
  595.             $st = preg_replace("/(?i)".$reg_mail."/", "\\1[mail]\\2[/mail]\\7", $st);
  596.         }
  597.         $elements = Array(
  598.             Array("url", $this->preg_c, "<a class=\"codeLighterLink\" href=\"\\2\\3\\4\" onClick=\"window.open(this.href, '_blank'); return false;\">\\6</a>"),
  599.             Array("url", $this->preg_g, "<a class=\"codeLighterLink\" href=\"\\1\\2\\3\" onClick=\"window.open(this.href, '_blank'); return false;\">\\4</a>"),
  600.             Array("mail", "/(?i)\[mail\={$reg_mail2}\](.+?)\[\/mail\]/i", "<a class=\"codeLighterLink\" href=\"mailto:\\1\">\\6</a>"),
  601.             Array("mail", "/(?i)\[mail\]{$reg_mail2}\[\/mail\]/i", "<a class=\"codeLighterLink\" href=\"mailto:\\1\">\\1</a>"),
  602.             Array("center", "/(?i)\[center\]([^\a]+?)\[\/center\]/i", "<div style=\"text-align: center;\">\\1</div>"),
  603.             Array("quote", "/(?i)\[quote\=(&quot;|\")([^\n\r\t\<\>]+?)(&quot;|\")\]([^\a]+?)\[\/quote\]/i", "{$pre_table_desc}quote</span><br /> &nbsp;<table class=\"codeLighterQuote\"><tr valign=\"top\"><td align=\"left\"><b>\\2</b><br />\\4</td></tr></table>"),
  604.             Array("right", "/(?i)\[right\]([^\a]+?)\[\/right\]/i", "<div style=\"text-align: right;\">\\1</div>"),
  605.             Array("size", "/(?i)\[size\=([1-9])\]([^\a]+?)\[\/size\]/i", "<span style=\"font-size: \\1pt;\">\\2</span>"),
  606.             Array("color", "/(?i)\[color\=([\#]?[0-9a-f]{3}|[\#]?[0-9a-f]{6}|[a-z]{3,})\]([^\a]+?)\[\/color\]/i", "<span style=\"color: \\1;\">\\2</span>"),
  607.             Array("font", "/(?i)\[font\=([a-z]+)\]([^\a]+?)\[\/font\]/i", "<span style=\"font-family: \\1, Helvetica, sans-serif;\">\\2</span>"),
  608.             Array("img", "/(?i)\[img\=(\&quot;|\")(http|https|ftp|irc|ed2k|gopher|telnet)?(\:\/\/)?([^\<\>[:space:]]+)(\&quot;|\")\](.+?)\[\/img\]/i", "<img class=\"codeLighterImg\" src=\"\\2\\3\\4\" alt=\"\\6\" title=\"\\6\" />")
  609.         );
  610.         for( $elm = 0; $elm < count( $elements ); $elm++ ) {
  611.             $element = $elements[$elm][0];
  612.             if( in_Array($element, $this->restriction ) ) {
  613.                 while( preg_match($elements[$elm][1], $st ) ) {
  614.                     $st = preg_replace( $elements[$elm][1], $elements[$elm][2], $st );
  615.                 }
  616.                 if( $element == "size" ) {
  617.                     $codeLighter_private_replace_callback = create_function(
  618.                     '$replacement',
  619.                     'return $replacement[2] == 1 ? $replacement[1]."7".$replacement[3] : $replacement[1].( 7 + ceil($replacement[2]/2) + $replacement[2] ).$replacement[3];'
  620.                     );
  621.                     $st = preg_replace_callback("/(\<span style\=\"font-size: )([1-9])(pt;\"\>)/", "$codeLighter_private_replace_callback", $st);
  622.  
  623.                 }
  624.             }
  625.         }
  626.         if( in_Array("code", $this->restriction) ) {
  627.             $st = preg_replace( "/(\<pre class\=\"codeLighterCode\"\>)([[:space:]]*)([^\a]+?)([[:space:]]*)(\<\/pre\>)/i", "\\1\\3\\5", $st );
  628.         }
  629.         if( in_Array("img", $this->restriction) ) {
  630.             $st = preg_replace("/(?i)\[img\](http|https|ftp)(\:\/\/)([^\<\>[:space:]]+?)\[\/img\]/i", "<img class=\"codeLighterImg\" src=\"\\1\\2\\3\" alt=\"\" title=\"\" />", $st);
  631.         }
  632.         if( in_Array("PrivateCheckImgSize", $this->restriction) ) {
  633.             $codeLighter_private_replace_callback = create_function(
  634.             '$replacement',
  635.             '$sizes = str_replace("X", "x", $replacement[1]);
  636.             $sizes = explode("x", $sizes);
  637.             if($sizes[0]>0 && $sizes[0]<='.$this->maxImgWidth.' && $sizes[1]>0 && $sizes[1]<='.$this->maxImgHeight.') {
  638.                 return "<img class=\"codeLighterImg\" src=\"".$replacement[2].$replacement[3].$replacement[4]."\" alt=\"\" title=\"\" style=\"width: ".$sizes[0]."px; height: ".$sizes[1]."px;\" />";
  639.             }
  640.             elseif($sizes[0]>'.$this->maxImgWidth.' && $sizes[1]<='.$this->maxImgHeight.') {
  641.                 return "<img class=\"codeLighterImg\" src=\"".$replacement[2].$replacement[3].$replacement[4]."\" alt=\"\" title=\"\" style=\"width: '.$this->maxImgWidth.'px; height: ".$sizes[1]."px;\" />";
  642.             }
  643.             elseif($sizes[0]<='.$this->maxImgWidth.' && $sizes[1]>'.$this->maxImgHeight.') {
  644.                 return "<img class=\"codeLighterImg\" src=\"".$replacement[2].$replacement[3].$replacement[4]."\" alt=\"\" title=\"\" style=\"width: ".$sizes[0]."px; height: '.$this->maxImgHeight.'px;\" />";
  645.             }
  646.             else {
  647.                 return "[img=".$sizes[0]."x".$sizes[1]."]".$replacement[2].$replacement[3].$replacement[4]."[/img]";
  648.             }');
  649.             $st = preg_replace_callback("/(?i)\[img\=([0-9]{1,}x[0-9]{1,})\](http|https|ftp)(\:\/\/)([^\<\>[:space:]]+?)\[\/img\]/i", "$codeLighter_private_replace_callback", $st);
  650.         }
  651.         if( in_Array("list", $this->restriction) ) {
  652.             $codeLighter_private_replace_callback = create_function(
  653.             '$replacement',
  654.             'return "<ol>".str_replace("[*]", "<li>", $replacement[2])."</ol>";'
  655.             );
  656.             $st = preg_replace_callback("/(?i)(\[list\=1\])([^\a]+?)(\[\/list\])/i", "$codeLighter_private_replace_callback", $st);
  657.             $codeLighter_private_replace_callback = create_function(
  658.             '$replacement',
  659.             'return "<ul>".str_replace("[*]", "<li>", $replacement[2])."</ul>";'
  660.             );
  661.             $st = preg_replace_callback("/(?i)(\[list\])([^\a]+?)(\[\/list\])/i", "$codeLighter_private_replace_callback", $st);
  662.         }
  663.         if(in_Array("mail", $this->restriction)) {
  664.             $preg_searc = "/(\<a class\=\"codeLighterLink\" href\=\"mailto\:)([^\a]+?)(\"\>)([^\a]+?)(\<\/a\>)/i";
  665.             $st = $this->replaceCallbackMailUrl( $preg_searc, $st );
  666.         }
  667.         if(in_Array("url", $this->restriction)) {
  668.             $preg_searc = "/(\<a class\=\"codeLighterLink\" href\=\")([^\a]+?)(\" onClick\=\"window.open\(this.href, '_blank'\); return false;\"\>)([^\a]+?)(\<\/a\>)/i";
  669.             $st = $this->replaceCallbackMailUrl( $preg_searc, $st );
  670.         }
  671.         $a = count( $this->newRestriction );
  672.         if( $a > 0 ) {
  673.             $pre_table_desc = "<span style=\"font-family: Verdana, Helvetica, sans-serif; font-size: 7pt; font-style: normal; text-decoration: none; font-weight: bold;\">";
  674.             for( $b = 0; $b < $a; $b++ ) {
  675.                     if( is_Array( $this->newRestriction[$b][1] ) ) {
  676.                     $col1 = $this->newRestriction[$b][1][0];
  677.                     $col2 = $this->newRestriction[$b][1][1];
  678.                     $col3 = $this->newRestriction[$b][1][2];
  679.                     $col4 = $this->newRestriction[$b][1][3];
  680.                     $this->codeModifier( $this->newRestriction[$b][0], $st, $pre_table_desc , $col1, $col2, $col3, $col4, true );
  681.                 }
  682.                 else {
  683.                     $pre = "{$pre_table_desc}{$this->newRestriction[$b][0]}</span><table class=\"codeLighter{$this->newRestriction[$b][0]}\"><tr valign=\"top\"><td align=\"left\">";
  684.                     $st = $this->ereg_engine($st, $this->newRestriction[$b][0], $pre, "</td></tr></table>");
  685.                 }
  686.             }
  687.         }
  688.         for( $a=0; $a<count($this->codeConverter); $a++ ) {
  689.             $st = str_replace( $this->codeConverter[$a][0], $this->codeConverter[$a][1], $st );
  690.         }
  691.         $this->output = preg_replace( "/(\<br\ \/\>\<br\ \/\>|\<br\ \/\>\\n|\<br\ \/\>\\n\\r)/", " <br /> <br />", str_replace("<ul><br />", "<ul>", nl2br($st) ) );
  692.         unset($st);
  693.         return($this->output);
  694.     }
  695.     function replaceCallbackMailUrl( $preg_searc, $st ) {
  696.         $codeLighter_private_replace_callback = create_function(
  697.         '$replacement',
  698.         'if( $replacement[2] == $replacement[4] && strlen($replacement[4]) > '.$this->maxUrlLenght.' ) {
  699.             $modded = strlen($replacement[2]) > '.$this->maxUrlLenght.' ? substr($replacement[2], 0, 20)."...".substr($replacement[2], strlen($replacement[2])-20, 20) : $replacement[2];
  700.             return $replacement[1].$replacement[2].$replacement[3].$modded.$replacement[5];
  701.         }
  702.         else {
  703.             return $replacement[1].$replacement[2].$replacement[3].$replacement[4].$replacement[5];
  704.         }'
  705.         );
  706.         return preg_replace_callback($preg_searc, "$codeLighter_private_replace_callback", $st);
  707.     }
  708.     function jsAddLighter() {
  709.         $this->jsLighter = "
  710.         <script type=\"text/javascript\">
  711.             var codeLighter = function() {
  712.                 // andr3a [ www.3site.it ] [ [email protected] ]
  713.                 var used = Array();
  714.                 function codeLighter_add(str) {
  715.                     var codeLighterTextarea_add = '';
  716.                     var to_span = 'novalue';
  717.                     if(!document.getElementById('codeLighterInternalTextarea')) {
  718.                         alert('You need a TEXTAREA with id=\"codeLighterInternalTextarea\" to use these buttons!');
  719.                     }
  720.                     if(document.getElementById('codeLighterTextareaSpanChecker')!=null) {
  721.                         to_span = document.getElementById('codeLighterTextareaSpanChecker').innerHTML;
  722.                         to_span = parseInt(to_span);
  723.                     }
  724.                     if(document.getElementById('codeLighter' + str).value == '['+str+']') {
  725.                         codeLighterTextarea_add = '['+str+']';
  726.                         document.getElementById('codeLighter' + str).value = '[/'+str+']';
  727.                         this.total.push('[/'+str+']');
  728.                         if(to_span!='novalue') {
  729.                             document.getElementById('codeLighterTextareaSpanChecker').innerHTML = (to_span+1);
  730.                         }
  731.                     }
  732.                     else {
  733.                         for(var a=(this.total.length-1); a>=0; a--) {
  734.                             codeLighterTextarea_add += this.total[a];
  735.                             var my_str = this.total[a].substr(2, (this.total[a].length-3));
  736.                             document.getElementById('codeLighter' + my_str).value = '['+my_str+']';
  737.                             if(to_span!='novalue') {
  738.                                 to_span--;
  739.                             }
  740.                             if(my_str==str) {
  741.                                 break;
  742.                             }
  743.                         }
  744.                         if(this.total.length==0) {
  745.                             codeLighterTextarea_add += '[/'+str+']';
  746.                             document.getElementById('codeLighter' + str).value = '['+str+']';
  747.                             if(to_span!='novalue') {
  748.                                 to_span--;
  749.                             }
  750.                         }
  751.                         this.total = Array();
  752.                         if(to_span!='novalue') {
  753.                             document.getElementById('codeLighterTextareaSpanChecker').innerHTML = to_span;
  754.                         }
  755.                     }
  756.                     document.getElementById('codeLighterInternalTextarea').value += codeLighterTextarea_add;
  757.                     document.getElementById('codeLighterInternalTextarea').focus();
  758.                     return true;
  759.                 }
  760.                 function codeLighter_closeTags(ar) {
  761.                     var to_span = 'novalue';
  762.                     for(var a=(ar.length-1); a>=0; a--) {
  763.                         document.getElementById('codeLighterInternalTextarea').value += ar[a];
  764.                         var my_str = ar[a].substr(2, (ar[a].length-3));
  765.                         document.getElementById('codeLighter' + my_str).value = '['+my_str+']';
  766.                     }
  767.                     this.total = Array();
  768.                     if(document.getElementById('codeLighterTextareaSpanChecker')!=null) {
  769.                         to_span = document.getElementById('codeLighterTextareaSpanChecker').innerHTML;
  770.                         to_span = parseInt(to_span);
  771.                     }
  772.                     if(to_span!='novalue') {
  773.                         document.getElementById('codeLighterTextareaSpanChecker').innerHTML = '0';
  774.                     }
  775.                     document.getElementById('codeLighterInternalTextarea').focus();
  776.                 }
  777.                 function codeLighter_maxChars() {
  778.                     if(this.counter!=false) {
  779.                         var check_len = document.getElementById('codeLighterInternalTextarea').value.length;
  780.                         var rest_len = 0;
  781.                         if ( check_len >= this.counter ) {
  782.                             document.getElementById('codeLighterInternalTextarea').value = document.getElementById('codeLighterInternalTextarea').value.substring(0, this.counter);
  783.                         }
  784.                         else {
  785.                             rest_len = this.counter - check_len;
  786.                         }
  787.                         document.getElementById('codeLighterTextareaSpanCounter').innerHTML = rest_len;
  788.                     }
  789.                     else {
  790.                         document.getElementById('codeLighterTextareaSpanCounter').innerHTML = document.getElementById('codeLighterInternalTextarea').value.length;
  791.                     }
  792.                 }
  793.                 this.total = used;
  794.                 this.add = codeLighter_add;
  795.                 this.close = codeLighter_closeTags;
  796.                 this.maxChars = codeLighter_maxChars;
  797.                 this.counter = false;
  798.             }
  799.             var codeLighter = new codeLighter();
  800.         </script>
  801.         ";
  802.         return true;
  803.     }
  804.     /**
  805.     * Public, textarea javascript check for text's max length
  806.     * $codeLighter->setCounter($maxChars);
  807.     *   This setting allow textarea counter for text used on textarea.
  808.     *   If this isn't setted / printed , the counter will check only total chars used on TextArea.
  809.     *   NOTE: it works only with this class JavaScript and this class' textarea with this class' JavaScript.
  810.     *   WARNING: it works only after printed JavaScript with codeLighter object, so you can print this value only after the textarea creation.
  811.     * @Param    int     Max chars in textarea
  812.     */
  813.     function setCounter($max) {
  814.         return ("
  815.         <script type=\"text/javascript\">
  816.             codeLighter.counter = {$max};
  817.         </script>
  818.         ");
  819.     }
  820.     /**
  821.     * Public, to modify default textarea.
  822.     * $codeLighter->createTextareaLighter($to_area, $mouse_overBG, $mouse_outBG, $mouse_overCL, $mouse_outCL);
  823.     *   This function allow you to modify textarea value [ only $codeLighter->textarea ] and buttons output [ only $codeLighter->textareaButtons ]
  824.     *       $to_area => DEFAULT: ""; // text into textarea
  825.     *       $mouse_overBG => DEFAULT: "#FAFAFA"; // button MouseOVER background color
  826.     *       $mouse_outBG => DEFAULT: "#EFEFEF"; // button MouseOUT background color
  827.     *       $mouse_overCL => DEFAULT: "#565656"; // button MouseOVER text color
  828.     *       $mouse_outCL => DEFAULT: "#787878"; // button MouseOUT text color
  829.     *   Example:
  830.     *       $codeLighter->createTextareaLighter( htmlentities( $_POST["codeLighterText"] ) );
  831.     *       echo $codeLighter->textarea;
  832.     *   NOTE: If you want use this not "full optional" textArea but you want use the JavaScript
  833.     *         tag checker with number of opened tags, you can create by yourself
  834.     *         a span tag with this id name: "codeLighterTextareaSpanChecker"
  835.     *         This element will be numeric with total tags opened and not closed on textarea.
  836.     *         NOTE: you need to print $codeLighter->jsLighter; to use checker function
  837.     *   WARNING: this is the class's inizilizator, you must youse to apply changes on buttons or dedicated CSS
  838.     * @Param    String      Text to show inside textarea ( default: "" )
  839.     * @Param    String      Valid HEX info for button's mouse over ( background HTML color ) ( default: #FAFAFA )
  840.     * @Param    String      Valid HEX info for button's mouse out ( background HTML color ) ( default: #EFEFEF )
  841.     * @Param    String      Valid HEX info for button's mouse over ( text HTML color ) ( default: #565656 )
  842.     * @Param    String      Valid HEX info for button's mouse out ( text HTML color ) ( default: #787878 )
  843.     */
  844.     function createTextareaLighter( $to_area = "", $mouse_overBG = "#FAFAFA", $mouse_outBG = "#EFEFEF", $mouse_overCL = "#565656", $mouse_outCL = "#787878", $cols = "74", $rows = "10" ) {
  845.         if( preg_match( "/^[0-9]{1,3}$/", $mouse_overBG) ) {
  846.             $cols = $mouse_overBG;
  847.             $mouse_overBG = "#FAFAFA";
  848.         }
  849.         if( preg_match( "/^[0-9]{1,3}$/", $mouse_outBG) ) {
  850.             $rows = $mouse_outBG;
  851.             $mouse_outBG = "#EFEFEF";
  852.         }
  853.         $this->textareaButtons = "";
  854.         for($a=0; $a<count($this->area_buttons); $a++) {
  855.             $this->textareaButtons .= "<input id=\"codeLighter".$this->area_buttons[$a]."\" class=\"codeLighterButton\" type=\"button\" value=\"[".$this->area_buttons[$a]."]\" onClick=\"codeLighter.add('".$this->area_buttons[$a]."');\" onMouseOver=\"this.style.background='{$mouse_overBG}'; this.style.color='{$mouse_overCL}';\" onMouseOut=\"this.style.background='{$mouse_outBG}'; this.style.color='{$mouse_outCL}';\" />
  856.             ";
  857.         }
  858.         $this->textareaButtons .= "<input class=\"codeLighterButton\" type=\"button\" value=\"[ * ]\" onClick=\"codeLighter.close(codeLighter.total);\" onMouseOver=\"this.style.background='{$mouse_overBG}'; this.style.color='{$mouse_overCL}';\" onMouseOut=\"this.style.background='{$mouse_outBG}'; this.style.color='{$mouse_outCL}';\" />
  859.             ";
  860.         if(get_magic_quotes_gpc()) {
  861.             $to_area = stripslashes($to_area);
  862.         }
  863.         $this->textarea = "
  864.         <br /><textarea class=\"codeLighterTextarea\" id=\"codeLighterInternalTextarea\" name=\"codeLighterText\" onKeyUp=\"codeLighter.maxChars();\" cols=\"{$cols}\" rows=\"{$rows}\">".$to_area."</textarea>";
  865.         return true;
  866.     }
  867.     /**
  868.     * Public, to modify default form.
  869.     * $codeLighter->form($action, $value, $method);
  870.     *   This function return entire FORM with textarea and JavaScript functions.
  871.     *       $action => DEFAULT: $_SERVER["PHP_SELF"]; // form action
  872.     *       $value => DEFAULT: "SEND"; // submit button value
  873.     *       $method => DEFAULT: "POST"; // form method
  874.     *   NOTE: if you need to modify form style, use this class name for your CSS styleSheet: codeLighterAutoForm
  875.     *   NOTE: with this function you have $codeLighter->textarea_check; variable to set tags checker text value [ DEFAULT: "OPENED TAGS" ]
  876.     *
  877.     *   Example:
  878.     *       $codeLighter->textarea_check = "For this post you've opened these tags:";
  879.     * @Param    String      Where will be posted inserted text ( default: $_SERVER["PHP_SELF"] )
  880.     * @Param    String      Submit text value ( default: SEND )
  881.     * @Param    String      Method to send form ( default: POST )
  882.     */
  883.     function form($action = "", $value = "SEND", $method = "POST") {
  884.         $action = $action == "" ? $_SERVER["PHP_SELF"] : $action;
  885.         $form_auto = $this->jsLighter."
  886.         <FORM CLASS=\"codeLighterAutoForm\" name=\"codeLighterFormName\" METHOD=\"{$method}\" ACTION=\"{$action}\" ENCTYPE=\"multipart/form-data\">
  887.         <DIV STYLE=\"padding: 0px;\">".$this->textareaButtons.$this->textarea."<br />
  888.         <input class=\"codeLighterButton\" type=\"submit\" value=\"{$value}\" />
  889.         <span class=\"codeLighterSpan\">[ ".$this->textarea_check."</span>
  890.         <span id=\"codeLighterTextareaSpanChecker\" class=\"codeLighterSpan\">0</span>
  891.         <span class=\"codeLighterSpan\"> ]</span>
  892.         <span class=\"codeLighterSpan\"> [ </span>
  893.         <span id=\"codeLighterTextareaSpanCounter\" class=\"codeLighterSpan\">0</span>
  894.         <span class=\"codeLighterSpan\"> ]</span></DIV>
  895.         </FORM>
  896.         ";
  897.         return $form_auto;
  898.     }
  899.     /**
  900.     * Public, choose what tag you want enable / parse
  901.     *
  902.     * $codeLighter->restrict($restrict_array);
  903.     *       With this function you can specify what kind of tags you want to parse
  904.     *
  905.     *       Example:
  906.     *           $restrict_array = Array("code", "b");
  907.     *           $codeLighter->restrict($restrict_array);
  908.     *           // will parse only [code] or [b]
  909.     * @Param    Array       Parse tags list
  910.     */
  911.     function restrict($restrict_array) {
  912.         if(!is_Array($restrict_array)) {
  913.             $restrict_array = Array( strtolower($restrict_array) );
  914.         }
  915.         $this->restriction = $restrict_array;
  916.         for($a=0; $a<count($this->restriction); $a++) {
  917.             $this->restriction[$a] = strtolower($this->restriction[$a]);
  918.         }
  919.         return true;
  920.     }
  921. }
  922. ?>
  923.  
Advertisement
Add Comment
Please, Sign In to add comment