Avatar billede horizon Nybegynder
14. april 2005 - 21:23 Der er 1 løsning

BBC kode problemer

Jeg har prøvet at eksperimentere med at skrive noget fra et SMF forum til at være kompatibelt med mit eget...

men jeg er løbet ind i et problem

Her er min Opret debat kode:

<?php

session_start();

include('../access.php');

if(!isset($_POST[Klaeber]));
$_POST[Klaeber] = 0;

require('Subs-Post.php');

function Post2() {

// Add special html entities to the subject, message, name, and email.
$_POST['message'] = htmlspecialchars($_POST['message']);
$_POST['Navn'] = htmlspecialchars($_POST['Navn']);

preparsecode($_POST['message']);

addslashes($_POST[message]);

// Cheat and fix entities in the subject line.
$_POST['Navn'] = preg_replace('~&amp;#(\d{4,5}|[3-9]\d{2,4}|2[6-9]\d);~', '&#$1;', $_POST['Navn']);

// At this point, we want to make sure the subject isn't too long.  Stripslashes first to avoid a trailing slash.
if (isset($_POST['Navn']) && strlen(stripslashes($_POST['Navn'])) > 100)
  $_POST['Navn'] = addslashes(substr(stripslashes($_POST['Navn']), 0, 100));

// Hack to make it so &#324324... can't happen.
$_POST['Navn'] = preg_replace('~&#\d+$~', '', $_POST['Navn']);

// Fix the message icon.
$_POST['icon'] = preg_replace('~[./\\*:]~', '', $_POST['icon']);

// This is a new topic. Save it.
if (isset($_POST[OpretDebat]))
{
  // Insert the post.
  mysql_query("
  INSERT INTO
    Test_Forum_Debatter
    (Klaeber
    ,ForaID
    ,Navn
    ,Ikon
    ,Forfatter
    ,AntalSvar)
  VALUES (
  $_POST[Klaeber]
  ,$_POST[ForaID]
  ,'$_POST[Navn]'
  ,'$_POST[icon]'
  ,$_SESSION[brugernr]
  ,1)")or die("MySql Opret Debat fejl: " . mysql_error());
 
  $HentDID = mysql_query("
  SELECT
    DebatID
  FROM
    Test_Forum_Debatter
  WHERE
    ForaID = $_POST[ForaID]
    AND Navn = '$_POST[Navn]'
    AND Klaeber = $_POST[Klaeber]
    AND Ikon = '$_POST[icon]'")or die("MySql hent DebatID fejl: " . mysql_error());
  $DID = mysql_fetch_array($HentDID);
 
  $DebatID = $DID[DebatID];
   
  mysql_query("
  INSERT INTO Test_Forum_Indlaeg
    (DebatID,ForaID,OprettetTid,BrugerID,Navn,Ikon,Krop)
  VALUES($DebatID,$_POST[ForaID],NOW(),$_SESSION[brugernr],'$_POST[Navn]','$_POST[icon]','$_POST[message]')")or die("MySql Opret Indlæg fejl: " . mysql_error());
}
// Already existing topic, new post.
elseif(isset($_POST[OpretIndlaeg]))
{

$DebatID = $_POST[DebatID];

  mysql_query("
  INSERT INTO Test_Forum_Indlaeg
    ($DID[DebatID],ForaID,OprettetTid,BrugerID,Navn,Ikon,Krop)
  VALUES ($DebatID,$_POST[ForaID],NOW(),$_SESSION[brugernr],'$_POST[Navn]','$_POST[icon]', '$_POST[message]', )")or die("MySql Opret Indlæg fejl: " . mysql_error());
 
  mysql_query("UPDATE Test_Forum_Debatter SET AntalSvar = AntalSvar + 1 WHERE DebatID = $DebatID")or die("MySql Opdater Indlæg Tæller fejl: " . mysql_error());
 
  mysql_query("DELETE FROM Test_Forum_Laest WHERE DebatID = $DebatID");

  $newTopic = false;
}

mysql_close();

header("Location: ../../Test_Forum/Debatter.php?KID=$_POST[KategoriID]&FID=$_POST[ForaID]&DID=$DebatID");

}
Post2();
?>

og her er subs-post siden

<?php

$message = $_POST[message];

function preparsecode($message, $breaks = true)
{

// This line makes all languages *theoretically* work even with the wrong charset ;).
$message = preg_replace('~&amp;#(\d{4,5}|[3-9]\d{2,4}|2[6-9]\d);~', '&#$1;', $message);

// Remove \r's, replace tabs with spaces, two spaces with hard spaces, and \n's with breaks. (last is optional...)
$message = strtr($message, array("\r" => '', '  ' => '&nbsp; '));
if ($breaks)
  $message = strtr($message, array("\n" => '<br />'));

// Check if all quotes are closed...
$parts = preg_split('~(\[/quote\])~i', $message, -1, PREG_SPLIT_DELIM_CAPTURE);

$level = 0;
for ($i = 0, $n = count($parts); $i < $n; $i++)
{
  if (preg_match('~\[/quote\]~i',$parts[$i]) != 0)
  $level--;
  preg_match_all('~(\[quote=.+?\])|(\[quote author=.+?\])|(\[quote author=(.+?) link=(.+?) date=(.+?)\])|(\[quote\])~i', $parts[$i], $regs);
  $level += count($regs[0]);

  // Add on extra [quote]s...
  if ($level < 0)
  {
  $parts[$i] = str_repeat('[quote]', 0 - $level) . $parts[$i];
  $level = 0;
  }
}
$message = implode('', $parts);

// Add additional [/quote]s to the end.
if ($level > 0)
  $message .= str_repeat('[/quote]', $level);

// Check if all code tags are closed.
$codeopen = preg_match_all('~(?!\[)(\[code\])~i', $message, $dummy);
$codeclose = preg_match_all('~(?!\[)(\[/code\])~i', $message, $dummy);

// Close/open all code tags...
if ($codeopen > $codeclose)
  $message .= str_repeat('[/code]', $codeopen - $codeclose);
elseif ($codeclose > $codeopen)
  $message = str_repeat('[code]', $codeclose - $codeopen) . $message;

// Now that we've fixed all the code tags, let's fix the img and url tags...
$parts = preg_split('~\[/?code\]~i', $message);

// Only mess with stuff outside [code] tags.
for ($i = 0, $n = count($parts); $i < $n; $i++)
{
  // 1 (odd) means a code section, not post text.
  if ($i % 2 == 1)
  $parts[$i] = '[code]' . $parts[$i] . '[/code]';
  // Mess with the tags outside [code].
  else
  fixTags($parts[$i]);
}

// Put it back together and remove that first space.
$message = implode('', $parts);
}

// Fix any URLs posted - ie. remove 'java script:'.
function fixTags($message)
{
global $modSettings;

$fixArray = array
(
  // http://... or [img width=1]http://...[/img]
  array('tag' => 'img', 'protocol' => 'http', 'embeddedUrl' => false, 'hasEqualSign' => false, 'hasExtra' => true),
  // [url]http://...[/url]
  array('tag' => 'url', 'protocol' => 'http', 'embeddedUrl' => true, 'hasEqualSign' => false),
  // name
  array('tag' => 'url', 'protocol' => 'http', 'embeddedUrl' => true, 'hasEqualSign' => true),
  // [iurl]http://...[/iurl]
  array('tag' => 'iurl', 'protocol' => 'http', 'embeddedUrl' => true, 'hasEqualSign' => false),
  // [iurl=http://...]name[/iurl]
  array('tag' => 'iurl', 'protocol' => 'http', 'embeddedUrl' => true, 'hasEqualSign' => true),
  // [ftp]ftp://...[/ftp]
  array('tag' => 'ftp', 'protocol' => 'ftp', 'embeddedUrl' => true, 'hasEqualSign' => false),
  // [ftp=ftp://...]name[/ftp]
  array('tag' => 'ftp', 'protocol' => 'ftp', 'embeddedUrl' => true, 'hasEqualSign' => true),
  // [flash]http://...[/flash]
  array('tag' => 'flash', 'protocol' => 'http', 'embeddedUrl' => false, 'hasEqualSign' => true, 'hasExtra' => true)
);

// Fix each type of tag.
foreach ($fixArray as $param)
  fixTag($message, $param['tag'], $param['protocol'], $param['embeddedUrl'], $param['hasEqualSign'], isset($param['hasExtra']));

// Now fix possible security problems with images loading links automatically...
$message = preg_replace('~(\[img.*?\])(.+?)\[/img\]~eis', '\'$1\' . preg_replace(\'~action(=|%3d)(?!dlattach)~i\', \'action-\', \'$2\') . \'[/img]\'', $message);

// Limit the size of images posted?
if (!empty($modSettings['maxwidth']) || !empty($modSettings['maxheight']))
{
  // Find all the img tags - with or without width and height.
  preg_match_all('~\[img(\s+width=\d+)?(\s+height=\d+)?(\s+width=\d+)?\](.+?)\[/img\]~is', $message, $matches, PREG_PATTERN_ORDER);

  $replaces = array();
  foreach ($matches[0] as $match => $dummy)
  {
  // If the width was after the height, handle it.
  $matches[1][$match] = !empty($matches[3][$match]) ? $matches[3][$match] : $matches[1][$match];

  // Now figure out if they had a desired height or width...
  $desired_width = !empty($matches[1][$match]) ? (int) substr(trim($matches[1][$match]), 6) : 0;
  $desired_height = !empty($matches[2][$match]) ? (int) substr(trim($matches[2][$match]), 7) : 0;

  // One was omitted, or both.  We'll have to find its real size...
  if (empty($desired_width) || empty($desired_height))
  {
    list ($width, $height) = url_image_size($matches[4][$match]);

    // They don't have any desired width or height!
    if (empty($desired_width) && empty($desired_height))
    {
    $desired_width = $width;
    $desired_height = $height;
    }
    // Scale it to the width...
    elseif (empty($desired_width))
    $desired_width = (int) (($desired_height * $width) / $height);
    // Scale if to the height.
    else
    $desired_height = (int) (($desired_width * $height) / $width);
  }

  // If the width and height are fine, just continue along...
  if ($desired_width <= $modSettings['maxwidth'] && $desired_height <= $modSettings['maxheight'])
    continue;

  // Too bad, it's too wide.  Make it as wide as the maximum.
  if ($desired_width > $modSettings['maxwidth'] && !empty($modSettings['maxwidth']))
  {
    $desired_height = (int) (($modSettings['maxwidth'] * $desired_height) / $desired_width);
    $desired_width = $modSettings['maxwidth'];
  }

  // Now check the height, as well.  Might have to scale twice, even...
  if ($desired_height > $modSettings['maxheight'] && !empty($modSettings['maxheight']))
  {
    $desired_width = (int) (($modSettings['maxheight'] * $desired_width) / $desired_height);
    $desired_height = $modSettings['maxheight'];
  }

  $replaces[$matches[0][$match]] = '[img width=' . $desired_width . ' height=' . $desired_height . ']' . $matches[4][$match] . '[/img]';
  }

  // If any img tags were actually changed...
  if (!empty($replaces))
  $message = strtr($message, $replaces);
}
}

// Fix a specific class of tag - ie. url with =.
function fixTag($message, $myTag, $protocol, $embeddedUrl = false, $hasEqualSign = false, $hasExtra = false)
{
while (preg_match('/\[(' . $myTag . ($hasExtra ? '(?:[^\]]*?)' : '') . ')' . ($hasEqualSign ? '(=(.+?))' : '(())') . '\](.+?)\[\/(' . $myTag . ')\]/is', $message, $matches))
{
  // All the different information from the match.
  $leftTag = $matches[1];
  $equalTo = $matches[3];
  $searchfor = $matches[4];
  $rightTag = $matches[5];
  $replace = $hasEqualSign && $embeddedUrl ? $equalTo : $searchfor;

  // Remove all leading and trailing whitespace.
  $replace = trim($replace);
  if (!stristr($replace, $protocol . '://'))
  {
  if ($protocol != 'http' || !stristr($replace, 'https://'))
    $replace = $protocol . '://' . $replace;
  else
    $replace = stristr($replace, 'https://');
  }
  else
  $replace = stristr($replace, $protocol . '://');

  // Put the tag back together.
  if ($hasEqualSign && $embeddedUrl)
  $message = str_replace(
    '[' . $leftTag . '=' . $equalTo . ']' . $searchfor . '[/' . $rightTag . ']',
    '<' . $myTag . '=' . $replace . ']' . $searchfor . '</' . $myTag . '>', $message);
  elseif ($hasEqualSign && !$embeddedUrl)
  $message = str_replace(
    '[' . $leftTag . '=' . $equalTo . ']' . $searchfor . '[/' . $rightTag . ']',
    '<' . $myTag . '=' . $equalTo . ']' . $replace . '</' . $myTag . '>', $message);
  elseif ($embeddedUrl && $replace != $searchfor)
  $message = str_replace(
    '[' . $leftTag . ']' . $searchfor . '[/' . $rightTag . ']',
    '<' . $myTag . '=' . $replace . ']' . $searchfor . '</' . $myTag . '>', $message);
  else
  $message = str_replace(
    '[' . $leftTag . ']' . $searchfor . '[/' . $rightTag . ']',
    '<' . preg_replace('~' . preg_quote($myTag) . '~i', $myTag, $leftTag) . '>' . $replace . '</' . $myTag . '>', $message);
}

// Replace the braces with brackets.
$message = str_replace(
  array('<' . $myTag . '>', '<' . $myTag . '=', '</' . $myTag . '>'),
  array('[' . $myTag . ']', '[' . $myTag . '=', '[/' . $myTag . ']'), $message);

// If there is extra stuff we also need to do this. (flash, img.)
if ($hasExtra)
  $message = preg_replace('~<(' . $myTag . '.+?)>~i', '[$1]', $message);
}

$_POST[message] = $message;

?>

Men! det vil altså ikke virke! den erstatter ikke evt. specielle tags som f.eks. eller nogle af de andre.

Er der nogle som har forstand på dette da jeg ikke selv kan finde det...

Forresten skal lige siges... ALT virker, den smider det hele ind i databaserne, men det er lige som om den springer let og elegant forbi "preparsecode()" funktionen..
Avatar billede horizon Nybegynder
17. april 2005 - 20:20 #1
satans til hjælpsomhed!
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Vi tilbyder markedets bedste kurser inden for webudvikling

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester