Advertisement
kyleAI_13

core.php: bb_parse function

Jun 14th, 2016
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.65 KB | None | 0 0
  1. <?php // I get the following error when replacing any content with [img] tags:
  2. // ( ! ) Notice: Undefined offset: 1 in C:\wamp64\www\inc\core.php on line 65
  3.  
  4. $_SQL = mysqli_connect("localhost", "root", "l0ljklol", "main");
  5. session_start();
  6. if(!empty($_SESSION['uname'])) {
  7.     $grabCurrentUser = $_SQL->query("SELECT * FROM users WHERE id = '".$_SESSION['uid']."'");
  8.     $currentUser = $grabCurrentUser->fetch_assoc();
  9.     $_USER['id'] = $_SESSION['uid'];
  10.     $_USER['name'] = $_SESSION['uname'];
  11.     $_USER['group'] = $_SESSION['group'];
  12.     $_USER['email'] = $_SESSION['email'];
  13.     $_USER['password'] = $_SESSION['password'];
  14.     $_USER['img'] = $currentUser['avatar'];
  15.     $_USER['slogan'] = $currentUser['slogan'];
  16.     $_USER['alias'] = $currentUser['alias'];
  17.     $_USER['theme'] = $currentUser['theme'];
  18.     $_SESSION['theme'] = $_USER['theme'];
  19. }
  20. function isImage($url)
  21.   {
  22.      $params = array('http' => array(
  23.                   'method' => 'HEAD'
  24.                ));
  25.      $ctx = stream_context_create($params);
  26.      $fp = @fopen($url, 'rb', false, $ctx);
  27.      if (!$fp)
  28.         return false;  // Problem with url
  29.  
  30.     $meta = stream_get_meta_data($fp);
  31.     if ($meta === false)
  32.     {
  33.         fclose($fp);
  34.         return false;  // Problem reading data from url
  35.     }
  36.  
  37.     $wrapper_data = $meta["wrapper_data"];
  38.     if(is_array($wrapper_data)){
  39.       foreach(array_keys($wrapper_data) as $hh){
  40.           if (substr($wrapper_data[$hh], 0, 19) == "Content-Type: image") // strlen("Content-Type: image") == 19
  41.           {
  42.             fclose($fp);
  43.             return true;
  44.           }
  45.       }
  46.     }
  47.  
  48.     fclose($fp);
  49.     return false;
  50.  }
  51. function dateConvert($string) {
  52.             return date('m-d-Y \a\t h:i:s', strtotime($string));
  53. }
  54. function bb_parse($string) {
  55.     $tags = 'b|i|size|color|center|quote|url|img';
  56.     while (preg_match_all('`\[('.$tags.')=?(.*?)\](.+?)\[/\1\]`', $string, $matches)) foreach ($matches[0] as $key => $match) {
  57.         list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);
  58.         switch ($tag) {
  59.             case 'b': $replacement = "<strong>$innertext</strong>"; break;
  60.             case 'u': $replacement = "<u>$innertext</u>"; break;
  61.             case 'i': $replacement = "<em>$innertext</em>"; break;
  62.             case 'size': $replacement = "<span style=\"font-size: $param;\">$innertext</span>"; break;
  63.             case 'color': $replacement = "<span style=\"color: $param;\">$innertext</span>"; break;
  64.             case 'center': $replacement = "<div class=\"centered\">$innertext</div>"; break;
  65.             case 'quote': $replacement = "<blockquote>$innertext</blockquote>" . $param? "<cite>$innertext</cite>" : ''; break;
  66.             case 'url': $replacement = '<a href="' . ($param? $param : $innertext) . "\">$innertext</a>"; break;
  67.             case 'img':
  68.                 list($width, $height) = preg_split('`[Xx]`', $param);
  69.                 $replacement = "<img " .(isImage($innertext)? "src=\"$innertext\" " : '') . (is_numeric($width)? "width=\"$width\" " : '') . (is_numeric($height)? "height=\"$height\" " : '') . 'style="max-width:80%;" />';
  70.             break;
  71.             case 'video':
  72.                 $videourl = parse_url($innertext);
  73.                 parse_str($videourl['query'], $videoquery);
  74.                 if (strpos($videourl['host'], 'youtube.com') !== FALSE) $replacement = '<embed src="http://www.youtube.com/v/' . $videoquery['v'] . '" type="application/x-shockwave-flash" width="425" height="344"></embed>';
  75.                 if (strpos($videourl['host'], 'google.com') !== FALSE) $replacement = '<embed src="http://video.google.com/googleplayer.swf?docid=' . $videoquery['docid'] . '" width="400" height="326" type="application/x-shockwave-flash"></embed>';
  76.             break;
  77.         }
  78.         $string = str_replace($match, $replacement, $string);
  79.     }
  80.     return $string;
  81. }
  82. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement