/** * Parsuje YOUTUBE * @param array $tag wszystkie informacje o tagu * @param array $openNode tag otwierajacy * @param array $body zawertosc pomiedzy tagiem otwierajacym a zamykajacym * @param array $closeNode tak zamykajacy * @param BbCodeSettings $settings */ public function checkMovie($tag, &$openNode, &$body, &$closeNode, $settings) { require_once dirname(__FILE__) . '/../DataValidator.php'; // wyciagamy caly tekst $bodyStr = ''; foreach ($body as &$el) { $bodyStr .= $el['text']; // wersja do wyswietlenia $el['text'] = ''; } // sprawdzamy czy ciag jest urlem $str = DataValidator::checkUrl($bodyStr); // skoro nie jest urlem wiec ISTNIEJE prawdopodobienstwo, ze jest to id filmu if ($str == false) { $idMovie = htmlspecialchars($bodyStr); } elseif (preg_match('/watch\\?v=([a-zA-Z0-9_\\-]+)/i', $str, $matches)) { // szukanie id filmu $idMovie = $matches[1]; } else { // nie ma id wiec lipa $openNode = $settings->removeNode($openNode); $closeNode = $settings->removeNode($closeNode); return false; } // zamkniecie nie jest nam potrzebne $closeNode['text'] = ''; // Ustawiamy content $openNode['text'] = '<iframe title="YouTube video player" width="560" height="349" src="http://www.youtube.com/embed/' . $idMovie . '?rel=0" frameborder="0"> </iframe> '; //ustawiamy link dla zaufanego bbcode reset($body); $body[key($body)]['tagText'] = $str; }
/** * Parsuje LISTY * @param array $tag * @param array $openNode * @param array $body * @param array $closeNode * @param BbCodeSettings $settings */ public function parseList($tag, &$openNode, &$body, &$closeNode, $settings) { $good = false; foreach ($body as &$el) { if ($el['type'] == BbCode::NODE_TYPE_OPEN && $el['tagname'] == 'li') { $good = true; break; } } if (!$good) { $openNode = $settings->removeNode($openNode); $closeNode = $settings->removeNode($closeNode); } }
/** * Parsuje IMG * @param array $tag * @param array $openNode * @param array $body * @param array $cNode * @param BbCodeSettings $settings */ public function checkImg($tag, &$openNode, &$body, &$cNode, $settings) { require_once dirname(__FILE__) . '/../DataValidator.php'; $src = ''; if (isset($openNode['attributes']['tag_attributes']['img'])) { $src = DataValidator::checkUrl($openNode['attributes']['tag_attributes']['img']); } $bodyStr = ''; foreach ($body as $el) { $bodyStr .= $el['text']; } $bodyUrl = DataValidator::checkUrl($bodyStr); if ($bodyUrl) { $src = $bodyUrl; } else { if ($bodyStr) { $openNode['attributes']['tag_attributes']['alt'] = $bodyStr; } } if ($src == false) { $openNode = $settings->removeNode($openNode, $settings->removeInvalidTags); $cNode = $settings->removeNode($cNode, $settings->removeInvalidTags); return false; } $openNode['attributes']['tag_attributes']['src'] = $src; unset($el); if (isset($openNode['attributes']['tag_attributes']['img'])) { $tagSizes = explode('x', $openNode['attributes']['tag_attributes']['img']); if (is_array($tagSizes)) { $tagSizes = array_map('trim', $tagSizes); if (isset($tagSizes[0]) && is_numeric($tagSizes[0])) { $openNode['attributes']['tag_attributes']['width'] = round($tagSizes[0]); } if (isset($tagSizes[1]) && is_numeric($tagSizes[1])) { $openNode['attributes']['tag_attributes']['height'] = round($tagSizes[1]); } } } // wlaczona kontrola wielkosci zdjecia i czy zawsze mozemy taką kontrolę przeprowadzic if ($this->imageMaxWidth > 0 || $this->imageMaxHeight > 0 || $this->imageMinHeight > 0 || $this->imageMinHeight > 0) { if (ini_get('allow_url_fopen') && $this->checkRealImageSize) { // ustawienie maksymalnego czasu pobierania info o zdjeciu $oldDefaultSocketTimeout = ini_get('default_socket_timeout'); $socketTimeout = 5; if (isset($this->socketTimeout) && $this->socketTimeout >= 0) { $socketTimeout = $this->socketTimeout; } ini_set('default_socket_timeout', $socketTimeout); $size = @getimagesize($openNode['attributes']['tag_attributes']['src']); // z roznych przyczyn nie udalo sie pobrac zdjecia badz nie jest on plikiem graficznym if (!$size) { $openNode = $settings->removeNode($openNode); $cNode = $settings->removeNode($cNode); $openNode['text'] = '[Nieprawidłowe zdjęcie]'; return false; } } elseif (isset($openNode['attributes']['tag_attributes']['width']) || isset($openNode['attributes']['tag_attributes']['height'])) { $size = array(); $size[0] = isset($openNode['attributes']['tag_attributes']['width']) ? $openNode['attributes']['tag_attributes']['width'] : false; $size[1] = isset($openNode['attributes']['tag_attributes']['height']) ? $openNode['attributes']['tag_attributes']['height'] : false; if ($size[0] === false && $size[1] === false) { $size = false; } } if (isset($size) && $size) { //pomocnicze zachowanie wymiarow $mainWidth = $size[0]; $mainHeight = $size[1]; $width = isset($openNode['attributes']['tag_attributes']['width']) ? $openNode['attributes']['tag_attributes']['width'] : $size[0]; $height = isset($openNode['attributes']['tag_attributes']['height']) ? $openNode['attributes']['tag_attributes']['height'] : $size[1]; // szerokosc if ($this->imageMaxWidth > 0 && $width > $this->imageMaxWidth) { $width = $this->imageMaxWidth; $height = $this->imageMaxWidth * $height / $width; } // wysokosc if ($this->imageMaxHeight > 0 && $height > $this->imageMaxHeight) { $height = $this->imageMaxHeight; $width = $this->imageMaxHeight * $width / $height; } $width = round($width); $height = round($height); if ($this->imageMinWidth > 0 && $this->imageMinWidth > $width) { $width = $this->imageMinWidth; } if ($this->imageMinHeight > 0 && $this->imageMinHeight > $height) { $height = $this->imageMinHeight; } if ($width != $mainWidth) { $openNode['attributes']['tag_attributes']['width'] = $width; } if ($height != $mainHeight) { $openNode['attributes']['tag_attributes']['height'] = $height; } } // przywrocenie domyslnego ustawienia if (ini_get('allow_url_fopen') && $this->checkRealImageSize) { ini_set('default_socket_timeout', $oldDefaultSocketTimeout); } } // ustawianie ostatecznie pobranych argumentow $imgValue = ''; if (isset($openNode['attributes']['tag_attributes']['width'])) { $imgValue .= $openNode['attributes']['tag_attributes']['width']; } if (isset($openNode['attributes']['tag_attributes']['height'])) { $imgValue .= 'x' . $openNode['attributes']['tag_attributes']['height']; } if ($imgValue) { $openNode['attributes']['tag_attributes']['img'] = $imgValue; } // usuwamy zawartosc body foreach ($body as $key => &$el) { if ($el['type'] != 0) { $el = array('type' => 0, 'text' => '', 'tagText' => $el['text']); } else { $el = array('type' => 0, 'text' => '', 'tagText' => $el['text']); } } $cNode['text'] = ''; $openNode = BbCode::rebuildNode($tag, $openNode, $settings); $openNode['text'] = substr($openNode['text'], 0, -1) . '/>'; // domykamy img :) }
/** * Sprawdza i poprawia poprawność użycia kolejności tagów * Funkcja pracuje bezpośrednio na {@link $_nodesArray} */ private function _checkValidHtml() { $newNodeArr = array(); $openTags = array(); $openTagsWithAttr = array(); foreach ($this->_nodesArray as $key => $node) { $newNode = array(); switch ($node['type']) { // tag otwierajacy case self::NODE_TYPE_OPEN: $openTags[count($newNodeArr) - 1] = $node['tagname']; if (isset($node['attributes'])) { $openTagsWithAttr[count($newNodeArr) - 1] = $node; } $newNode = $node; break; // tag zamykajacy // tag zamykajacy case self::NODE_TYPE_CLOSE: // nie bylo tagu otwierajacego to papa if (!in_array($node['tagname'], $openTags)) { $newNode = $this->settings->removeNode($node); break; } // aktualny ostatni otwarty tag $parent = end($openTags); if ($parent != $node['tagname']) { $tmpCloseNodeArr = array(); $tmpOpenNodeArr = array(); // szukamy niedomknietych tagow $openTagsKeys = array_keys($openTags); $openTagsSearchKey = array_search($node['tagname'], array_reverse($openTags, true)); $sliceKey = array_search($openTagsSearchKey, $openTagsKeys); // nie zamkniete tagi $noClosed = array_slice($openTags, $sliceKey + 1, null, true); // rodzic dla tagow otwieranych if ($sliceKey > 0) { $validOpenTagsParent = $openTags[$openTagsKeys[$sliceKey - 1]]; } else { $validOpenTagsParent = false; } $parent = $node['tagname']; foreach ($noClosed as $tKey => $tag) { $nodeTagInfo = $this->tags[$tag]; $parentTagInfo = $this->tags[$parent]; // zamykanie if (self::_checkAllowedTagName($nodeTagInfo, self::CHECK_PARENT, $node['tagname']) && self::_checkAllowedTagName($parentTagInfo, self::CHECK_CHILD, $tag)) { $last = end($newNodeArr); if ($last['type'] == self::NODE_TYPE_OPEN && $last['tagname'] == $tag) { array_pop($newNodeArr); } else { $tmpCloseNodeArr[] = $this->_createNode($this->settings->openChar . '/' . $tag . $this->settings->closeChar); $parent = $tag; } } // otwieranie if (!self::_checkAllowedTagName($nodeTagInfo, self::CHECK_PARENT, $validOpenTagsParent)) { continue; } if ($validOpenTagsParent) { $vParentTagInfo = $this->tags[$validOpenTagsParent]; if (!self::_checkAllowedTagName($vParentTagInfo, self::CHECK_CHILD, $tag)) { continue; } } if (isset($openTagsWithAttr[$tKey])) { $tmpOpenNodeArr[] = $openTagsWithAttr[$tKey]; } else { $tmpOpenNodeArr[] = $this->_createNode($this->settings->openChar . $tag . $this->settings->closeChar); } } unset($openTags[$openTagsSearchKey]); $newNodeArr = array_merge($newNodeArr, array_reverse($tmpCloseNodeArr)); //$node=false; } else { array_pop($openTags); $parent = end($openTags); /* // niedozwolony rodzic? $nodeTagInfo=$this->tags[$node['tagname']]; if(!self::_checkAllowedTagName($nodeTagInfo, self::CHECK_PARENT, $parent)) { //usuwamy continue; } // niedozwolony rodzic? if($parent && !self::_checkAllowedTagName($this->tags[$parent], self::CHECK_CHILD, $node['tagname'])) { //usuwamy //continue; } */ $lastNode = end($newNodeArr); if ($lastNode['type'] == self::NODE_TYPE_OPEN && $lastNode['tagname'] == $node['tagname']) { array_pop($newNodeArr); $node = false; } } $newNode = $node; break; // tag samozamykajacy się lub tekst // tag samozamykajacy się lub tekst case self::NODE_TYPE_TEXT: case self::NODE_TYPE_SELF_CLOSE: $newNode = $node; break; } $lastElement = end($newNodeArr); if ($newNode) { if ($lastElement['type'] === self::NODE_TYPE_OPEN && $newNode['type'] === self::NODE_TYPE_CLOSE && $newNode['tagname'] == $lastElement['tagname']) { array_pop($newNodeArr); } else { $newNodeArr[] = $newNode; } } if (isset($tmpOpenNodeArr) && $tmpOpenNodeArr) { $newNodeArr = array_merge($newNodeArr, $tmpOpenNodeArr); $tmpOpenNodeArr = array(); } } $this->_nodesArray = $newNodeArr; }
/** * Parsuje URL * @param array $tag * @param array $openNode * @param array $body * @param array $cNode * @param BbCodeSettings $settings */ public function parseUrl($tag, &$openNode, &$body, &$cNode, $settings) { if (isset($openNode['attributes']['tag_attributes']['url'])) { return false; } require_once dirname(__FILE__) . '/../DataValidator.php'; $str = false; $inImg = false; foreach ($body as &$el) { // szukamy urla w tekscie if ($el['type'] == BbCode::NODE_TYPE_TEXT) { $str = DataValidator::checkUrl($el['text']); if ($str !== false) { if (!$inImg) { $str = $this->shortUrl($el['text'], self::URL_LENGTH); } break; } } // jezeli jest obrazek i posiada adres obrazka to adres jest przepisywany do [URL] if ($el['type'] == BbCode::NODE_TYPE_OPEN && $el['tagname'] == 'img') { if (isset($el['attributes']['tag_attributes']['img'])) { $inImg = true; $str = $el['attributes']['tag_attributes']['img']; break; } } if ($el['type'] == BbCode::NODE_TYPE_CLOSE && $el['tagname'] == 'img') { $inImg = false; } } if ($str === false) { $openNode = $settings->removeNode($openNode); $cNode = $settings->removeNode($cNode); return false; } $openNode['attributes'] = array('tag_attributes' => array('url' => $str)); $openNode = BbCode::rebuildNode($tag, $openNode, $settings); }