deblan.tv/src/Deblan/Bundle/BlogBundle/Twig/Extension/BlogExtension.php
2015-03-02 21:57:49 +01:00

166 lines
5.9 KiB
PHP

<?php
namespace Deblan\Bundle\BlogBundle\Twig\Extension;
class BlogExtension extends \Twig_Extension
{
public function getName()
{
return 'blog_extension';
}
public function getFilters()
{
return array(
'comment' => new \Twig_Filter_Method($this, 'comment', array('is_safe' => array('html'))),
'post' => new \Twig_Filter_Method($this, 'post', array('is_safe' => array('html'))),
);
}
public function comment($text)
{
$text = htmlspecialchars(trim($text)).' ';
$text = preg_replace_callback(
'`(s?(http|ftp|irc)+s?://[^\s\)]+)(\s|\))+`iU',
function($data) {
return sprintf('<a target="_blank" href="%s">%s</a>%s', htmlspecialchars($data[1]), htmlspecialchars($data[1]), $data[3]);
},
$text
);
return nl2br($text);
}
public function post($text)
{
$text = preg_replace('`<clear>(.*)</clear>`isU', '', $text);
$text = preg_replace_callback(
'`<p([^>]*)>(.*)</p([^>]*)>`isU',
function($data) {
if(preg_match('`^[ ]+`', $data[1])) {
return '<p'.$data[1].'>'.nl2br(trim($data[2])).'</p>';
}
elseif(isset($data[1]) && $data[1] === '') {
return '<p>'.nl2br(trim($data[2])).'</p>';
}
else {
return $data[0];
}
},
$text
);
$text = preg_replace_callback('`<centre>(.*)</centre>`isU', function($data) { return '<div class="text-center">'.$data[1].'</div>'; }, $text);
$text = preg_replace_callback('`<droite>(.*)</droite>`isU', function($data) { return '<div class="text-right">'.$data[1].'</div>'; }, $text);
$text = preg_replace_callback('`<flgauche>(.*)</flgauche>`isU', function($data) { return '<div class="pull-left">'.$data[1].'</div>'; }, $text);
$text = preg_replace_callback('`<fldroite>(.*)</fldroite>`isU', function($data) { return '<div class="pull-right">'.$data[1].'</div>'; }, $text);
$text = preg_replace_callback('`<titre>(.*)</titre>`isU', function($data) { return '<h2>'.trim($data[1]).'</h2>'; }, $text);
$text = preg_replace_callback('`<sstitre>(.*)</sstitre>`isU', function($data) { return '<h3>'.trim($data[1]).'</h3>'; }, $text);
$text = preg_replace_callback('`<li>(.*)</li>`isU', function($data) { return '<li>'.trim($data[1]).'</li>'; }, $text);
$text = preg_replace_callback(
'`<quote auteur="([^"]+)">(.*)</quote>`isU',
function($data) {
return sprintf('<blockquote><p>%s</p><p><strong>%s</strong></p></blockquote>', nl2br(trim($data[2])), $data[1]);
},
$text
);
$text = preg_replace_callback(
'`<quote auteur=\'([^\']+)\'>(.*)</quote>`isU',
function($data) {
return sprintf('<blockquote><p>%s</p><p><strong>%s</strong></p></blockquote>', nl2br(trim($data[2])), $data[1]);
},
$text
);
$text = preg_replace_callback(
'`<quote>(.*)</quote>`isU',
function($data) {
return sprintf('<blockquote><p>%s</p></blockquote>', nl2br(trim($data[1])));
},
$text
);
$text = preg_replace_callback(
'`<img(.*)/?>`isU',
function($data) {
return sprintf('<img%s/>', ' data-dimension="destroy" '.(trim($data[1])));
},
$text
);
$text = preg_replace_callback(
'`<video(.*)>`isU',
function($data) {
return sprintf('<video%s>', ' data-dimension="destroy" '.(trim($data[1])));
},
$text
);
$text = preg_replace_callback(
'`<iframe(.*)>`isU',
function($data) {
return sprintf('<iframe%s>', ' data-dimension="destroy" '.(trim($data[1])));
},
$text
);
$text = preg_replace_callback('`<html>(.*)</html>`isU', function($data) { return html_entity_decode($data[1]); }, $text);
$text = preg_replace_callback('`<encode>(.*)</encode>`isU', function($data) { return htmlspecialchars($data[1]); }, $text);
$text = preg_replace('`<sondage>([0-9]+)</sondage>`isU', '', $text);
$text = preg_replace_callback('`<nonl2br>(.*)</nonl2br>`isU', function() { return str_replace('<br />', '', $data[1]); }, $text);
$text = preg_replace('`<object(.*)/object>`isU', '', $text);
$text = preg_replace('`smiley:([a-zA-Z]*)`is', '', $text);
$text = preg_replace_callback(
array(
'`<code langage="([^"]*)">(.*)</code>`isU',
'`<code langage=\'([^\']*)\'>(.*)</code>`isU',
),
function($data) {
$lang = strtolower(str_replace(
array('console', 'texte', 'apache'),
array('shell', 'text', 'html'),
$data[1])
);
$class = 'brush: '.$lang.'; gutter: false';
$html = sprintf('<div class="show-code">Afficher/masquer le code</div><div class="code-container"><pre class="%s">%s</pre></div>', $class, trim(htmlentities($data[2])));
return $html;
},
$text
);
$text = preg_replace_callback('`<code>(.*)</code>`isU', function($data) { return '<span class="code">'.$data[1].'</span>'; }, $text);
$text = preg_replace_callback('`<url>([^<]+)</url>`isU', function ($data) { return '<a href="'.$data[1].'">'.$data[1].'</a>'; }, $text);
$text = preg_replace_callback('`<galerie>(.*)</galerie>`isU', function($data) { '<div>'.$data[1].'</div>'; }, $text);
$text = preg_replace_callback('`"http://www.youtube.com/[^"]+"`i', function($data) { return str_replace('&', '&amp;', $data[0]); }, $text);
$text = preg_replace_callback('`"http://www.dailymotion.com/[^"]+"`i', function($data) { return str_replace('&', '&amp;', $data[0]); }, $text);
$text = str_replace('<timestamp>', time(), $text);
$text = str_replace('&lt;timestamp&gt;', time(), $text);
$text = str_replace('<couper>', '', $text);
$text = str_replace('http://upload.deblan.fr', 'https://upload.deblan.org', $text);
$text = str_replace('http://dedi.geneweb.fr', 'http://kim.deblan.fr', $text);
$text = str_replace("onclick='window.open(this.href); return false'", '', $text);
return $text;
}
}