/** ConvertToPreHTML
  * 
  * @access public
  * @param string Text
  * @return string to HTML convertet Code
  */
 function ConvertToPreHTML($Text)
 {
     $Text = stripslashes($Text) . " ";
     // make all EOL-sings equal
     $Text = preg_replace("!(\r\n)|(\r)!", "\n", $Text);
     // convert htmlentities to
     $Text = str_replace('ä', 'ä', $Text);
     $Text = str_replace('Ä', 'Ä', $Text);
     $Text = str_replace('ü', 'ü', $Text);
     $Text = str_replace('Ü', 'Ü', $Text);
     $Text = str_replace('ö', 'ö', $Text);
     $Text = str_replace('Ö', 'O', $Text);
     $Text = str_replace('ß', 'ß', $Text);
     $Text = str_replace('>', '>', $Text);
     $Text = str_replace('&lt;', '<', $Text);
     // remove comments
     $Text = preg_replace("/<!--(.+?)-->/s", '', $Text);
     // extract all text we won't convert <plain>...TEXT...</plain>
     preg_match_all("/<plain>(.+?)<\\/plain>/s", $Text, $matches);
     $plains = array();
     foreach ($matches[1] as $key => $match) {
         $plains[$key] = $matches[1][$key];
         $Text = str_replace($matches[0][$key], '[plain]%' . $key . '%[/plain]', $Text);
     }
     // catch all parts, which should used as blocks <block>...TEXT...</block>
     preg_match_all("/<block>(.+?)<\\/block>/s", $Text, $matches);
     $blocks = array();
     foreach ($matches[1] as $key => $match) {
         $blocks[$key] = $matches[1][$key];
         $Text = str_replace($matches[0][$key], '[block]%' . $key . '%[/block]', $Text);
     }
     // 'repair' all urls (with no http:// but a www or ftp)
     $Text = preg_replace("/(\\ |\\r|\\n|\\[)(www|ftp)\\.(.+?)\\.([a-zA-Z.]{2,6}(|\\/.+?))/s", '$1' . "http://\$2.\$3.\$4", $Text);
     // remove all html characters
     $Text = htmlspecialchars($Text);
     // fixes for some security bugs
     $Text = str_replace("\\r", "\r", $Text);
     $Text = str_replace("\\n", "\n", $Text);
     $Text = preg_replace("!(\r\n)|(\r)!", "\n", $Text);
     $Text = preg_replace("#\\\\(\\ |\\r|\\n)#s", "\n<br />\n", $Text);
     // catch all email-adresses which should be convertet to links (<*****@*****.**>)
     preg_match_all("#\\&lt\\;([a-z0-9\\._-]+?)\\@([\\w\\-]+\\.[a-z0-9\\-\\.]+\\.*[\\w]+)\\&gt\\;#s", $Text, $emails);
     // catch all images
     preg_match_all("#\\{\\{(.+?)\\}\\}#s", $Text, $images);
     $imagesData = array();
     foreach ($images[1] as $key => $image) {
         $imagesData[$key] = TextActions::MakeImage($image);
         $Text = str_replace('{{' . $image . '}}', '[img]%' . $key . '%[/img]', $Text);
     }
     // allowed auto-link protocols
     $protos = "http|ftp|https";
     // convert urls to links http://www.domain.com to [[http://www.domain.com|www.domain.com]]
     $Text = preg_replace("#(?<!\\[\\[)({$protos}):\\/\\/(.+?)(\\ |\\n)#s", '[[$1://$2|$2]]$3', $Text);
     $Text = preg_replace("#\\[\\[({$protos}):\\/\\/([a-z0-9\\-\\.]+)\\]\\]#s", '[[$1://$2|$2]]', $Text);
     // convert catched emails into the link format [[email@example.com]]
     foreach ($emails[0] as $key => $email) {
         $Text = str_replace('&lt;' . $emails[1][$key] . '@' . $emails[2][$key] . '&gt;', '[[' . $emails[1][$key] . '@' . $emails[2][$key] . '|' . $emails[1][$key] . '@' . $emails[2][$key] . ']]', $Text);
     }
     // catch all links
     preg_match_all("#\\[\\[(.+?)\\]\\]#s", $Text, $links);
     $link_list = array();
     //			$link_sources_list = array();
     //			$sourceNr = 1;
     $linkNr = 1;
     // replace all links with a short uniqe id to replace them later back
     foreach ($links[1] as $link) {
         /*				if(substr($link, 0, 7) == 'source:') {
         					$link_sources_list[$sourceNr] = substr($link, 7);
         					$Text = str_replace("[[$link]]", "[[s$sourceNr]]", $Text);
         					$sourceNr++;
         				}
         				else {*/
         $link_list[$linkNr] = $link;
         $Text = str_replace("[[{$link}]]", "[[%{$linkNr}%]]", $Text);
         $linkNr++;
         //				}
     }
     // convert all **text** to <strong>text</strong> => Bold
     $Text = preg_replace("/\\*\\*(.+?)\\*\\*/s", "<strong>\$1</strong>", $Text);
     // convert all //text// to <em>text</em> => Italic
     $Text = preg_replace("/\\/\\/(.+?)\\/\\//s", "<em>\$1</em>", $Text);
     // convert all __text__ to <u>text</u> => Underline
     $Text = preg_replace("/__(.+?)__/s", "<u>\$1</u>", $Text);
     // convert == text == to a header <h4>
     $Text = preg_replace("#====\\ (.+?)\\ ====#s", "\n\n<h4>\$1</h4>\n", $Text);
     // convert === text === to a header <h3>
     $Text = preg_replace("#===\\ (.+?)\\ ===#s", "\n\n<h3>\$1</h3>\n", $Text);
     // convert ==== text ==== to a header <h2>
     $Text = preg_replace("#==\\ (.+?)\\ ==#s", "\n\n<h2>\$1</h2>\n", $Text);
     // convert <center>text</center> to <div class="center">text</div>
     $Text = preg_replace("#&lt;center&gt;(.+?)&lt;/center&gt;#s", "\n\n<div class=\"center\">\$1</div>\n", $Text);
     // convert ({text}{text}{text}) to tree colums
     $Text = preg_replace("#\\(\\{(.+?)[\r\n ]*\\}\\{(.+?)[\r\n ]*\\}\\{(.+?)[\r\n ]*\\}\\)#s", "\n<div class=\"column ctree\">\n\$1\n</p></div>\n<div class=\"column ctree\"><p>\n\$2\n</p></div><div class=\"column ctree\"><p>\n\$3\n</p></div>\n<p class=\"after_column\">\n", $Text);
     // convert ({text}{text}) to two colums
     $Text = preg_replace("#\\(\\{(.+?)[\r\n ]*\\}\\{(.+?)[\r\n ]*\\}\\)#s", "\n<div class=\"column ctwo\">\n\$1\n</div>\n<div class=\"column ctwo\">\n\$2\n</div>\n<p class=\"after_column\"/>\n", $Text);
     // paste links into the text
     foreach ($link_list as $linkNr => $link) {
         if (preg_match("#^(.+?)\\|(.+?)\$#i", $link, $link2)) {
             $Text = str_replace("[[%{$linkNr}%]]", "<a href=\"" . TextActions::MakeLink($link2[1]) . "\">" . $link2[2] . "</a>", $Text);
         } else {
             $Text = str_replace("[[%{$linkNr}%]]", "<a href=\"" . TextActions::MakeLink($link) . "\">" . $link . "</a>", $Text);
         }
     }
     /*	if(count($link_sources_list) > 0) {
     				$Text .= "<hr />\n<ol>"; 
     				foreach($link_sources_list as $sourceNr => $link) {
     					$Text = str_replace("[[s{$sourceNr}]]", "<a href=\"#source_{$sourceNr}\">[{$sourceNr}]</a>", $Text);
     					if(preg_match("#^(.+?)\|(.+?)$#i", $link, $link2))
     						$Text .= "\n\t<li id=\"source_{$sourceNr}\"><a href=\"" . TextActions::MakeLink($link2[1]) . "\" >{$link2[1]}</a></li>";
     					else
     						$Text .= "\n\t<li id=\"source_{$sourceNr}\"><a href=\"" . TextActions::MakeLink($link) . "\" >$link</a></li>";
     				}
     				$Text .= "\n</ol>";
     			}*/
     $lines = explode("\n", $Text);
     $lines[] = "\n";
     $tempText = '';
     $outputText = '';
     $state = LINE_STATE_NONE;
     $lastState = LINE_STATE_NONE;
     foreach ($lines as $line) {
         $lastState = $state;
         // Unsorted lists: *
         if (TextActions::StartsWith('* ', $line)) {
             $state = LINE_STATE_ULIST;
         } else {
             if (TextActions::StartsWith('# ', $line)) {
                 $state = LINE_STATE_OLIST;
             } else {
                 if (TextActions::StartsWith('^', $line) || TextActions::StartsWith('|', $line)) {
                     $state = LINE_STATE_TABLE;
                 } else {
                     if (TextActions::StartsWith('<h', $line)) {
                         $state = LINE_STATE_HEADER;
                     } else {
                         if ($line == "\n" || $line == "") {
                             $state = LINE_STATE_NONE;
                         } else {
                             $state = LINE_STATE_TEXT;
                         }
                     }
                 }
             }
         }
         if ($lastState == $state) {
             $tempText .= "\t" . $line . "\n";
         } else {
             // convert the specific parts
             if ($lastState == LINE_STATE_TEXT) {
                 $outputText .= TextActions::ConvertText($tempText);
             } else {
                 if ($lastState == LINE_STATE_ULIST) {
                     $outputText .= TextActions::ConvertUList($tempText);
                 } else {
                     if ($lastState == LINE_STATE_OLIST) {
                         $outputText .= TextActions::ConvertOList($tempText);
                     } else {
                         if ($lastState == LINE_STATE_TABLE) {
                             $outputText .= TextActions::Converttable($tempText);
                         } else {
                             if ($lastState == LINE_STATE_HEADER) {
                                 $outputText .= $tempText;
                             }
                         }
                     }
                 }
             }
             $tempText = "\t" . $line . "\n";
         }
     }
     foreach ($blocks as $key => $match) {
         $outputText = str_replace('[block]%' . $key . '%[/block]', TextActions::ConvertToPreHTML($match), $outputText);
     }
     // paste plain-parts back
     foreach ($plains as $key => $match) {
         $outputText = str_replace('[plain]%' . $key . '%[/plain]', $match, $outputText);
     }
     foreach ($imagesData as $key => $imgHtml) {
         $outputText = str_replace('[img]%' . $key . '%[/img]', $imgHtml, $outputText);
     }
     // remove the spaces which are not necessary
     $outputText = preg_replace('/\\ \\ +/', ' ', $outputText);
     $outputText = str_replace(' -- ', ' &ndash; ', $outputText);
     $outputText = str_replace(' --- ', ' &mdash; ', $outputText);
     $outputText = str_replace('(c)', '&copy;', $outputText);
     $outputText = str_replace('(r)', '&reg;', $outputText);
     $outputText = str_replace('ä', '&auml;', $outputText);
     $outputText = str_replace('Ä', '&Auml;', $outputText);
     $outputText = str_replace('ü', '&uuml;', $outputText);
     $outputText = str_replace('Ü', '&Uuml;', $outputText);
     $outputText = str_replace('ö', '&ouml;', $outputText);
     $outputText = str_replace('Ö', '&Ouml;', $outputText);
     $outputText = str_replace('ß', '&szlig;', $outputText);
     return $outputText;
 }
 /**
  * @access public
  * @return string
  */
 function _saveArticle()
 {
     $id = GetPostOrGet('article_id');
     $title = GetPostOrGet('article_title');
     $description = GetPostOrGet('article_description');
     $text = GetPostOrGet('article_text');
     if (strlen($description) > 200) {
         $description = substr($description, 0, 200);
     }
     if ($title !== null && $description !== null && $text !== null && is_numeric($id)) {
         $sql = "UPDATE " . DB_PREFIX . "articles SET \n\t\t\t\t\tarticle_title= '{$title}', \n\t\t\t\t\tarticle_description= '{$description}', \n\t\t\t\t\tarticle_text= '{$text}',\n\t\t\t\t\tarticle_html= '" . TextActions::ConvertToPreHTML($text) . "'\n\t\t\t\t\tWHERE article_id={$id}";
         db_result($sql);
     }
 }
 function Edit($page_id, $title = '', $text = '', $edit_comment = '')
 {
     global $_SERVER, $admin_lang;
     $change = GetPostOrGet('change');
     $count = 1;
     $out = '';
     $page_data = null;
     $got_mysql = false;
     if ($text == '' && $title == '') {
         if (is_numeric($change) && $text == '' && $title == '') {
             $out .= "<strong>Achtung:</strong> Sie bearbeiten nicht die aktuelle Version, wenn Sie speichern wird ihr Text den aktuellen Text &uuml;berschreiben!";
             $sql = "SELECT *\r\n\t\t\t\t\t\tFROM (" . DB_PREFIX . "pages_history page\r\n\t\t\t\t\t\tLEFT JOIN " . DB_PREFIX . "pages_text_history text ON text.page_id = page.id ) \r\n\t\t\t\t\t\tWHERE page.page_id={$page_id}\r\n\t\t\t\t\t\tORDER BY  page.page_date ASC\r\n\t\t\t\t\t\tLIMIT " . ($change - 1) . ",1";
         } else {
             if ($text == '' && $title == '') {
                 $sql = "SELECT *\r\n\t\t\t\t\t\tFROM " . DB_PREFIX . "pages_history\r\n\t\t\t\t\t\tWHERE page_id = {$page_id}\r\n\t\t\t\t\t\tLIMIT 0,1";
                 $count_result = db_result($sql);
                 $count = mysql_num_rows($count_result);
                 $sql = "SELECT struct.page_id, struct.page_title, text.text_page_text, struct.page_edit_comment\r\n\t\t\t\t\t\tFROM ( " . DB_PREFIX . "pages struct\r\n\t\t\t\t\t\tLEFT JOIN " . DB_PREFIX . "pages_text text ON text.page_id = struct.page_id )\r\n\t\t\t\t\t\tWHERE struct.page_id='{$page_id}' AND struct.page_type='text'";
             }
         }
         $page_result = db_result($sql);
         if ($page_data = mysql_fetch_object($page_result)) {
             $got_mysql = true;
         }
     }
     if ($got_mysql || ($text != '' || $title != '')) {
         if ($text != '' || $title != '') {
             $page_title = stripslashes($title);
             $page_text = stripslashes($text);
             $page_edit_comment = stripslashes($edit_comment);
             $show_preview = true;
         } else {
             $page_title = $page_data->page_title;
             $page_text = $page_data->text_page_text;
             $page_edit_comment = $admin_lang['edited'] . '...';
             $show_preview = false;
         }
         $page_text = str_replace('&', '&amp;', $page_text);
         // FIXME: doesn't solve the problem with umlauts
         /*$page_text = str_replace('�', '&auml;', $page_text);
         		$page_text = str_replace('�', '&Auml;', $page_text);
         		$page_text = str_replace('�', '&uuml;', $page_text);
         		$page_text = str_replace('�', '&Uuml;', $page_text);
         		$page_text = str_replace('�', '&ouml;', $page_text);
         		$page_text = str_replace('�', '&Ouml;', $page_text);
         		$page_text = str_replace('�', '&szlig;', $page_text);
         		**/
         $page_text = str_replace('<', '&lt;', $page_text);
         $page_text = str_replace('>', '&gt;', $page_text);
         $out .= "\t\t\t<fieldset><legend>Seite Bearbeiten</legend><form action=\"admin.php\" method=\"post\">\r\n\t\t\t\t<input type=\"hidden\" name=\"page\" value=\"pagestructure\" />\r\n\t\t\t\t<input type=\"hidden\" name=\"action\" value=\"savePage\" />\r\n\t\t\t\t<input type=\"hidden\" name=\"pageID\" value=\"{$page_id}\" />\r\n\t\t\t\t<input type=\"text\" name=\"pageTitle\" value=\"{$page_title}\" /><br />\r\n\t\t\t\t<script type=\"text/javascript\" language=\"JavaScript\" src=\"system/functions.js\"></script>\r\n\t\t\t\t<script type=\"text/javascript\" language=\"javascript\">\r\n\t\t\t\t\twriteButton(\"img/button_fett.png\",\"Formatiert Text fett\",\"**\",\"**\",\"Fetter Text\",\"f\");\r\n\t\t\t\t\twriteButton(\"img/button_kursiv.png\",\"Formatiert Text kursiv\",\"//\",\"//\",\"Kursiver Text\",\"k\");\r\n\t\t\t\t\twriteButton(\"img/button_unterstrichen.png\",\"Unterstreicht den Text\",\"__\",\"__\",\"Unterstrichener Text\",\"u\");\r\n\t\t\t\t\twriteButton(\"img/button_ueberschrift.png\",\"Markiert den Text als &Uuml;berschrift\",\"==== \",\" ====\",\"&Uuml;berschrift\",\"h\");\r\n\t\t\t\t</script><br />\r\n\t\t\t\t<textarea id=\"editor\" class=\"edit\" name=\"pageText\">{$page_text}</textarea>\r\n\t\t\t\t<script type=\"text/javascript\" language=\"javascript\">\r\n\t\t\t\t\tdocument.write('<div style=\"float:right;\">');\r\n\t\t\t\t\tdocument.write('<img onclick=\"resizeBox(-5)\" title=\"Eingabefeld verkleinern\" alt=\"Eingabefeld verkleinern\" class=\"resize\" src=\"img/up.png\" /> ');\r\n\t\t\t\t\tdocument.write('<img onclick=\"resizeBox(5)\" title=\"Eingabefeld vergr&ouml;&szlig;ern\" alt=\"Eingabefeld vergr&ouml;&szlig;ern\" class=\"resize\" src=\"img/down.png\" /><br />');\r\n\t\t\t\t\tdocument.write('</div>');\t\r\n\t\t\t\t</script>\r\n\t\t\t\t{$admin_lang['comment_on_change']}: <input name=\"pageEditComment\" style=\"width:20em;\" value=\"" . ($count == 0 ? $page_data->page_edit_comment : (is_numeric($change) ? sprintf($admin_lang['edited_from_version'], $change) : $page_edit_comment)) . "\" maxlength=\"100\" type=\"text\"/><br />\r\n\t\t\t\t<input type=\"submit\" value=\"Speichern\" class=\"button\" />\r\n\t\t\t\t<input type=\"submit\" value=\"Vorschau\" name=\"pagePreview\" class=\"button\" />\r\n\t\t\t\t<input type=\"submit\" value=\"Abbrechen\" name=\"pageAbort\" class=\"button\"/>\r\n\t\t\t</form></fieldset>\r\n";
         if ($show_preview) {
             $page_text = TextActions::ConvertToPreHTML($page_text);
             $out .= "<fieldset>\r\n\t\t\t\t\t\t<legend>Vorschau</legend>\r\n\t\t\t\t\t\t<iframe class=\"pagepreview\" src=\"index.php?content=" . urlencode($page_text) . "\"></iframe>\r\n\t\t\t\t\t</fieldset>";
         }
     }
     return $out;
 }
 function GetSavePage($PageID)
 {
     if (!is_numeric($PageID)) {
         return false;
     }
     $preview = GetPostOrGet('pagePreview');
     // oh.. somebody called the previewfunction without javascript
     if ($preview != '') {
         return $this->GetEditPage($PageID);
     }
     $pageTitle = stripslashes(GetPostOrGet('pageTitle'));
     $pageText = stripslashes(GetPostOrGet('pageText'));
     $pageComment = stripslashes(GetPostOrGet('pageEditComment'));
     $pageHtml = TextActions::ConvertToPreHTML($pageText);
     $html = addslashes($pageHtml);
     $this->LogPage($PageID, $pageComment);
     $this->UpdatePage($PageID, addslashes($pageText), $html);
     $this->UpdateTitle($PageID, addslashes($pageTitle));
 }