0

I'm writing some php code in order to validate bbcode, I have no error but the bbcode won't validate my input... why? Something missing?

//============================================================//
// Check if all BBCode are validated before posting something //
//============================================================//
/* Tag Color */
$openColorTagCounter  = preg_match_all('#\[color=([a-zA-Z]+|\#[a-fA-F0-9]{3}[a-fA-F0-9]{0,3})\]#i', $body);
$closeColorTagCounter = preg_match_all('#\[/color\]#i', $body);
if ($openColorTagCounter > $closeColorTagCounter) {
  $error = "Tag color opened and not closed!";
  include("form.php");
  exit();
} elseif ($openColorTagCounter < $closeColorTagCounter) {
  $error = "Tag color closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Bold */
$openBoldTagCounter  = preg_match_all('#\[bold\]#i', $body);
$closeBoldTagCounter = preg_match_all('#\[/bold\]#i', $body);
if ($openBoldTagCounter > $closeBoldTagCounter) {
  $error = "Tag bold opened and not closed!";
  include("form.php");
  exit();
} elseif ($openBoldTagCounter < $closeBoldTagCounter) {
  $error = "Tag bold closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Italic */
$openItalicTagCounter  = preg_match_all('#\[italic\]#i', $body);
$closeItalicTagCounter = preg_match_all('#\[/italic\]#i', $body);
if ($openItalicTagCounter > $closeItalicTagCounter) {
  $error = "Tag italic opened and not closed!";
  include("form.php");
  exit();
} elseif ($openItalicTagCounter < $closeItalicTagCounter) {
  $error = "Tag italic closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Underline */
$openUnderlineTagCounter  = preg_match_all('#\[underline\]#i', $body);
$closeUnderlineTagCounter = preg_match_all('#\[/underline\]#i', $body);
if ($openUnderlineTagCounter > $closeUnderlineTagCounter) {
  $error = "Tag underline opened and not closed!";
  include("form.php");
  exit();
} elseif ($openUnderlineTagCounter < $closeUnderlineTagCounter) {
  $error = "Tag underline closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Code */
$openCodeTagCounter  = preg_match_all('#\[code\]#i', $body);
$closeCodeTagCounter = preg_match_all('#\[/code\]#i', $body);
if ($openCodeTagCounter > $closeCodeTagCounter) {
  $error = "Tag code opened and not closed!";
  include("form.php");
  exit();
} elseif ($openCodeTagCounter < $closeCodeTagCounter) {
  $error = "Tag code closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Line Numbers */
$openNumberTagCounter  = preg_match_all('#\[number\]#i', $body);
$closeNumberTagCounter = preg_match_all('#\[/number\]#i', $body);
if ($openNumberTagCounter > $closeNumberTagCounter) {
  $error = "Tag number opened and not closed!";
  include("form.php");
  exit();
} elseif ($openNumberTagCounter < $closeNumberTagCounter) {
  $error = "Tag number closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Resized Image */
$openResizedImageTagCounter  = preg_match_all('#\[img=([0-9]*)x([0-9]*)\]\[align=([A-Za-z]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\](.+?)#i', $body);
$closeResizedImageTagCounter = preg_match_all('#\[/img\]#i', $body);
if ($openResizedImageTagCounter > $closeResizedImageTagCounter) {
  $error = "Tag img resized opened and not closed!";
  include("form.php");
  exit();
} elseif ($openResizedImageTagCounter < $closeResizedImageTagCounter) {
  $error = "Tag img resized closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Linked Image */
$openLinkedImageTagCounter  = preg_match_all('#\[url=(.+?)\]\[img=([0-9]*)x([0-9]*)\]\[align=([A-Za-z]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\](.+?)#i', $body);
$closeLinkedImageTagCounter = preg_match_all('#\[/img\]\[/url\]#i', $body);
if ($openLinkedImageTagCounter > $closeLinkedImageTagCounter) {
  $error = "Tag img linked opened and not closed!";
  include("form.php");
  exit();
} elseif ($openLinkedImageTagCounter < $closeLinkedImageTagCounter) {
  $error = "Tag img linked closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Normal Image */
$openImageTagCounter  = preg_match_all('#\[img\]\[align=([A-Za-z]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\](.+?)#i', $body);
$closeImageTagCounter = preg_match_all('#\[/img\]#i', $body);
if ($openImageTagCounter > $closeImageTagCounter) {
  $error = "Tag img opened and not closed!";
  include("form.php");
  exit();
} elseif ($openImageTagCounter < $closeImageTagCounter) {
  $error = "Tag img closed and not opened!";
  include("form.php");
  exit();
}
/* Tag UL */
$openUlTagCounter  = preg_match_all('#\[ul\]#i', $body);
$closeUlTagCounter = preg_match_all('#\[/ul\]#i', $body);
if ($openUlTagCounter > $closeUlTagCounter) {
  $error = "Tag ul opened and not closed!";
  include("form.php");
  exit();
} elseif ($openUlTagCounter < $closeUlTagCounter) {
  $error = "Tag ul closed and not opened!";
  include("form.php");
  exit();
}
/* Tag LI */
$openLiTagCounter  = preg_match_all('#\[li\]#i', $body);
$closeLiTagCounter = preg_match_all('#\[/li\]#i', $body);
if ($openLiTagCounter > $closeLiTagCounter) {
  $error = "Tag li opened and not closed!";
  include("form.php");
  exit();
} elseif ($openLiTagCounter < $closeLiTagCounter) {
  $error = "Tag li closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Video */
$openVideoTagCounter  = preg_match_all('#\[video=([0-9]*)x([0-9]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\]\[player_id=([0-9]*)\](.+?)#i', $body);
$closeVideoTagCounter = preg_match_all('#\[/video\]#i', $body);
if ($openVideoTagCounter > $closeVideoTagCounter) {
  $error = "Tag video opened and not closed!";
  include("form.php");
  exit();
} elseif ($openVideoTagCounter < $closeVideoTagCounter) {
  $error = "Tag video closed and not opened!";
  include("form.php");
  exit();
}
/* Tag SilverLight */
$openSilverlightTagCounter  = preg_match_all('#\[silverlight=([0-9]*)x([0-9]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\]\[player_id=([0-9]*)\](.+?)#i', $body);
$closeSilverlightTagCounter = preg_match_all('#\[/silverlight\]#i', $body);
if ($openSilverlightTagCounter > $closeSilverlightTagCounter) {
  $error = "Tag silverlight opened and not closed!";
  include("form.php");
  exit();
} elseif ($openSilverlightTagCounter < $closeSilverlightTagCounter) {
  $error = "Tag silverlight closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Center */
$openCenterTagCounter  = preg_match_all('#\[center\]#i', $body);
$closeCenterTagCounter = preg_match_all('#\[/center\]#i', $body);
if ($openCenterTagCounter > $closeCenterTagCounter) {
  $error = "Tag center opend and not closed!";
  include("form.php");
  exit();
} elseif ($openCenterTagCounter < $closeCenterTagCounter) {
  $error = "Tag center closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Web */
$openUrlTagCounter  = preg_match_all('#\[url=(.+?)\]#i', $body);
$closeUrlTagCounter = preg_match_all('#\[/url\]#i', $body);
if ($openUrlTagCounter > $closeUrlTagCounter) {
  $error = "Tag url opened and not closed!";
  include("form.php");
  exit();
} elseif ($openUrlTagCounter < $closeUrlTagCounter) {
  $error = "Tag url closed and not opened!";
  include("form.php");
  exit();
}

Seems a stupid problem but at the moment I cannot see a right way! Somebody can tell me where I'm wrong? In the meanwhile Merry Christmas!!!

Alecos
  • 57
  • 2
  • 8
  • Are you calling this code somewhere ? – Calimero Dec 22 '13 at 09:46
  • I have two php file.. post.php where I put the code that you can see and form.php that shows all output... when I click on submit if no errors found then another php file validates all inputs (spam, hacking, injection and so go) and finally the post is published. – Alecos Dec 22 '13 at 10:24
  • You may start by echo-ing the $*Counter variables values you want to fail in your input, to assert whether the numbers are what they should be (If the regexpes you're testing are wrong, you may end up with both counters set to zero and still succeed). – Calimero Dec 22 '13 at 10:27

1 Answers1

0

Thanks, I solved my problem, post here the solution. Thanks very much for helping me :) The problem was that the regex called duplicate items and so cannot see properly the structure... now I have renamed some items and of course work great :) I hope this helps someone in similar situation. Merry Christmas to all people :) Have a nice week-end :)

//============================================================//
// Check if all BBCode are validated before posting something //
//============================================================//
/* Tag Color */
$openColorTagCounter  = preg_match_all('#\[color=([a-zA-Z]+|\#[a-fA-F0-9]{3}[a-fA-F0-9]{0,3})\]#i', $body);
$closeColorTagCounter = preg_match_all('#\[/color\]#i', $body);
if ($openColorTagCounter > $closeColorTagCounter) {
  $error = "Tag color opened and not closed!";
  include("form.php");
  exit();
} elseif ($openColorTagCounter < $closeColorTagCounter) {
  $error = "Tag color closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Bold */
$openBoldTagCounter  = preg_match_all('#\[bold\]#i', $body);
$closeBoldTagCounter = preg_match_all('#\[/bold\]#i', $body);
if ($openBoldTagCounter > $closeBoldTagCounter) {
  $error = "Tag bold opened and not closed!";
  include("form.php");
  exit();
} elseif ($openBoldTagCounter < $closeBoldTagCounter) {
  $error = "Tag bold closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Italic */
$openItalicTagCounter  = preg_match_all('#\[italic\]#i', $body);
$closeItalicTagCounter = preg_match_all('#\[/italic\]#i', $body);
if ($openItalicTagCounter > $closeItalicTagCounter) {
  $error = "Tag italic opened and not closed!";
  include("form.php");
  exit();
} elseif ($openItalicTagCounter < $closeItalicTagCounter) {
  $error = "Tag italic closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Underline */
$openUnderlineTagCounter  = preg_match_all('#\[underline\]#i', $body);
$closeUnderlineTagCounter = preg_match_all('#\[/underline\]#i', $body);
if ($openUnderlineTagCounter > $closeUnderlineTagCounter) {
  $error = "Tag underline opened and not closed!";
  include("form.php");
  exit();
} elseif ($openUnderlineTagCounter < $closeUnderlineTagCounter) {
  $error = "Tag underline closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Code */
$openCodeTagCounter  = preg_match_all('#\[code\]#i', $body);
$closeCodeTagCounter = preg_match_all('#\[/code\]#i', $body);
if ($openCodeTagCounter > $closeCodeTagCounter) {
  $error = "Tag code opened and not closed!";
  include("form.php");
  exit();
} elseif ($openCodeTagCounter < $closeCodeTagCounter) {
  $error = "Tag code closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Line Numbers */
$openNumberTagCounter  = preg_match_all('#\[number\]#i', $body);
$closeNumberTagCounter = preg_match_all('#\[/number\]#i', $body);
if ($openNumberTagCounter > $closeNumberTagCounter) {
  $error = "Tag number opened and not closed!";
  include("form.php");
  exit();
} elseif ($openNumberTagCounter < $closeNumberTagCounter) {
  $error = "Tag number closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Resized Image */
$openResizedImageTagCounter  = preg_match_all('#\[img-resized=([0-9]*)x([0-9]*)\]\[align=([A-Za-z]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\]#i', $body);
$closeResizedImageTagCounter = preg_match_all('#\[/img-resized\]#i', $body);
if ($openResizedImageTagCounter > $closeResizedImageTagCounter) {
  $error = "Tag img resized opened and not closed!";
  include("form.php");
  exit();
} elseif ($openResizedImageTagCounter < $closeResizedImageTagCounter) {
  $error = "Tag img resized closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Linked Image */
$openLinkedImageTagCounter  = preg_match_all('#\[url-linked=(.+?)\]\[img-linked=([0-9]*)x([0-9]*)\]\[align=([A-Za-z]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\]#i', $body);
$closeLinkedImageTagCounter = preg_match_all('#\[/img-linked\]\[/url-linked\]#i', $body);
if ($openLinkedImageTagCounter > $closeLinkedImageTagCounter) {
  $error = "Tag img linked opened and not closed!";
  include("form.php");
  exit();
} elseif ($openLinkedImageTagCounter < $closeLinkedImageTagCounter) {
  $error = "Tag img linked closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Normal Image */
$openImageTagCounter  = preg_match_all('#\[img\]\[align=([A-Za-z]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\]#i', $body);
$closeImageTagCounter = preg_match_all('#\[/img\]#i', $body);
if ($openImageTagCounter > $closeImageTagCounter) {
  $error = "Tag img opened and not closed!";
  include("form.php");
  exit();
} elseif ($openImageTagCounter < $closeImageTagCounter) {
  $error = "Tag img closed and not opened!";
  include("form.php");
  exit();
}
/* Tag UL */
$openUlTagCounter  = preg_match_all('#\[ul\]#i', $body);
$closeUlTagCounter = preg_match_all('#\[/ul\]#i', $body);
if ($openUlTagCounter > $closeUlTagCounter) {
  $error = "Tag ul opened and not closed!";
  include("form.php");
  exit();
} elseif ($openUlTagCounter < $closeUlTagCounter) {
  $error = "Tag ul closed and not opened!";
  include("form.php");
  exit();
}
/* Tag LI */
$openLiTagCounter  = preg_match_all('#\[li\]#i', $body);
$closeLiTagCounter = preg_match_all('#\[/li\]#i', $body);
if ($openLiTagCounter > $closeLiTagCounter) {
  $error = "Tag li opened and not closed!";
  include("form.php");
  exit();
} elseif ($openLiTagCounter < $closeLiTagCounter) {
  $error = "Tag li closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Video */
$openVideoTagCounter  = preg_match_all('#\[video=([0-9]*)x([0-9]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\]\[player_id=([0-9]*)\]#i', $body);
$closeVideoTagCounter = preg_match_all('#\[/video\]#i', $body);
if ($openVideoTagCounter > $closeVideoTagCounter) {
  $error = "Tag video opened and not closed!";
  include("form.php");
  exit();
} elseif ($openVideoTagCounter < $closeVideoTagCounter) {
  $error = "Tag video closed and not opened!";
  include("form.php");
  exit();
}
/* Tag SilverLight */
$openSilverlightTagCounter  = preg_match_all('#\[silverlight=([0-9]*)x([0-9]*)\]\[hspace=([0-9]*)\]\[vspace=([0-9]*)\]\[player_id=([0-9]*)\]#i', $body);
$closeSilverlightTagCounter = preg_match_all('#\[/silverlight\]#i', $body);
if ($openSilverlightTagCounter > $closeSilverlightTagCounter) {
  $error = "Tag silverlight opened and not closed!";
  include("form.php");
  exit();
} elseif ($openSilverlightTagCounter < $closeSilverlightTagCounter) {
  $error = "Tag silverlight closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Center */
$openCenterTagCounter  = preg_match_all('#\[center\]#i', $body);
$closeCenterTagCounter = preg_match_all('#\[/center\]#i', $body);
if ($openCenterTagCounter > $closeCenterTagCounter) {
  $error = "Tag center opend and not closed!";
  include("form.php");
  exit();
} elseif ($openCenterTagCounter < $closeCenterTagCounter) {
  $error = "Tag center closed and not opened!";
  include("form.php");
  exit();
}
/* Tag Web */
$openUrlTagCounter  = preg_match_all('#\[url=(.+?)\]#i', $body);
$closeUrlTagCounter = preg_match_all('#\[/url\]#i', $body);
if ($openUrlTagCounter > $closeUrlTagCounter) {
  $error = "Tag url opened and not closed!";
  include("form.php");
  exit();
} elseif ($openUrlTagCounter < $closeUrlTagCounter) {
  $error = "Tag url closed and not opened!";
  include("form.php");
  exit();
}
Alecos
  • 57
  • 2
  • 8