<?php
/**
 * @copyright	Copyright 2006-2013, Miles Johnson - http://milesj.me
 * @license		http://opensource.org/licenses/mit-license.php - Licensed under the MIT License
 * @link		http://milesj.me/code/php/decoda
 */

//namespace Decoda\Filter;
namespace Neag\MainBundle\DecodaFilter;

use Decoda\Filter\ImageFilter;
use Decoda\Decoda;

/**
 * Provides tags for images.
 */
class ImageDataFilter extends ImageFilter {
  
    public $kernel;
    
    public $DB;
    
    public $doctrine;
    
    public $globals;
    
    /**
     * Regex pattern.
     */
     /*
    const IMAGE_PATTERN = '/^(?:https?:\/)?(?:.){0,2}\/(.*?)\.(?:jpg|jpeg|png|gif|bmp)$/is';
    const WIDTH_HEIGHT = '/^([0-9%]{1,4}+)x([0-9%]{1,4}+)$/';
    const DIMENSION = '/^[0-9%]{1,4}+$/';    
    */

    /**
     * Supported tags.
     *
     * @var array
     */
    protected $_tags = array(
        'img' => array(
            'htmlTag' => 'img',
            'displayType' => Decoda::TYPE_INLINE,
            'allowedTypes' => Decoda::TYPE_NONE,
            'contentPattern' => self::IMAGE_PATTERN,
            'autoClose' => true,
            'attributes' => array(
                'default' => self::WIDTH_HEIGHT,
                'width' => self::DIMENSION,
                'height' => self::DIMENSION,
                'alt' => self::WILDCARD
            )
        ),
        'imgl' => array(
            'htmlTag' => 'img',
            'displayType' => Decoda::TYPE_INLINE,
            'allowedTypes' => Decoda::TYPE_NONE,
            'contentPattern' => self::IMAGE_PATTERN,
            'autoClose' => true,
            'attributes' => array(
                'default' => self::WIDTH_HEIGHT,
                'width' => self::DIMENSION,
                'height' => self::DIMENSION,
                'alt' => self::WILDCARD
            )
        ),
        'imgr' => array(
            'htmlTag' => 'img',
            'displayType' => Decoda::TYPE_INLINE,
            'allowedTypes' => Decoda::TYPE_NONE,
            'contentPattern' => self::IMAGE_PATTERN,
            'autoClose' => true,
            'attributes' => array(
                'default' => self::WIDTH_HEIGHT,
                'width' => self::DIMENSION,
                'height' => self::DIMENSION,
                'alt' => self::WILDCARD
            )
        ),
        'image' => array(
            'htmlTag' => 'img',
            'displayType' => Decoda::TYPE_INLINE,
            'allowedTypes' => Decoda::TYPE_NONE,
            'contentPattern' => self::IMAGE_PATTERN,
            'autoClose' => true,
            'attributes' => array(
                'default' => self::WIDTH_HEIGHT,
                'width' => self::DIMENSION,
                'height' => self::DIMENSION,
                'alt' => self::WILDCARD
            )
        )
    );    
    
    /**
     * ???????????
     */
    public function __construct($DB = null, $doctrine = null, $kernel = null, $globals = null)
    {
        $this->DB = $DB;
        $this->doctrine = $doctrine;
        $this->kernel = $kernel;
        $this->globals = $globals;
    } 
    
	/**
     * Use the content as the image source.
     *
     * @param array $tag
     * @param string $content
     * @return string
     */
    public function parse(array $tag, $content) {
        
        if (is_numeric($content)) {
            $id = $content;
            $Image = $this->doctrine
            ->getRepository('NeagMainBundle:Image')
            ->find($id);
            $html = '';
            if (!empty($Image)) {
                $comment = $Image->getComment();
                $info = $this->globals->getImageInfo($Image);
                //$this->globals->trace($info);                
                if (!empty($info['src'])) {
                    $html = '<img src="'.$info['src'].'" alt="'.htmlspecialchars($comment).'" border="0" width="'.$info['width'].'" height="'.$info['height'].'" />';
                }
                if (!empty($info['full_src'])) {
                    $html = '<a class="lightbox" data-fancybox-group="button" href="'.htmlspecialchars($info['full_src']).'">'.$html.'</a>';
                }                
                if (!empty($comment)) {
                    $html = '<table border="0" cellspacing="0" cellpadding="0" width="'.$info['width'].'"><tr><td align="left" valign="top">'.$html.'</td></tr>';
                    $html .= '<tr><td align="left" valign="top" class="bbcode-img-comment">'.htmlspecialchars($comment).'</td></tr>';
                    $html .= '</table>';
                }
            }
            //$this->globals->trace($tag);            
            if ($tag['tag'] == 'imgl') {
                $html = '<div class="bbcode-imgl">'.$html.'</div>';
            }
            if ($tag['tag'] == 'imgr') {
                $html = '<div class="bbcode-imgr">'.$html.'</div>';
            }
            //$this->globals->trace($html);                                
            return $html;
        }

        // If more than 1 http:// is found in the string, possible XSS attack
        if ((mb_substr_count($content, 'http://') + mb_substr_count($content, 'https://')) > 1) {
            return null;
        }

        $tag['attributes']['src'] = $content;

        if (!empty($tag['attributes']['default'])) {
            list($width, $height) = explode('x', $tag['attributes']['default']);

            $tag['attributes']['width'] = $width;
            $tag['attributes']['height'] = $height;
        }

        if (empty($tag['attributes']['alt'])) {
            $tag['attributes']['alt'] = '';
        }

        return parent::parse($tag, $content);
    }
}