<commit_before><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright	Copyright (C) 2011 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

namespace Nooku\Component\Files;

use Nooku\Library;

/**
 * Mimetype Mixin Class
 *
 * @author  <NAME> <http://nooku.assembla.com/profile/ercanozkaya>
 * @package Nooku\Component\Files
 */
class MixinMimetype extends Library\Object
{
	/**
	 * Used as a way to continue on the chain when the method is not available.
	 */
	const NOT_AVAILABLE = -1;

	/**
	 * Adapters to use for mimetype detection
	 *
	 * @var array
	 */
	protected $_adapters = array();

	public function __construct(Library\ObjectConfig $config)
	{
		parent::__construct($config);

		if (isset($config->adapters)) {
			$this->_adapters = Library\ObjectConfig::unbox($config->adapters);
		}
	}

	protected function _initialize(Library\ObjectConfig $config)
	{
		if (empty($config->adapters)) {
			$config->adapters = array('image', 'finfo');
		}

		parent::_initialize($config);
	}

	public function getMimetype($path)
	{
		$mimetype = false;

		if (!file_exists($path)) {
			return $mimetype;
		}

		foreach ($this->_adapters as $i => $adapter)
		{
			$function = '_detect'.ucfirst($adapter);
			$return = $this->$function($path);

			if (!empty($return) && $return !== MixinMimetype::NOT_AVAILABLE)
            {
				$mimetype = $return;
				break;
			}
		}

		// strip charset from text files
		if (!empty($mimetype) && strpos($mimetype, ';')) {
			$mimetype = substr($mimetype, 0, strpos($mimetype, ';'));
		}

		// special case: empty text files
		if ($mimetype == 'application/x-empty') {
			$mimetype = 'text/plain';
		}

		
		return $mimetype;
	}

	protected function _detectImage($path)
	{
		if (in_array(strtolower(pathinfo($path, PATHINFO_EXTENSION)), DatabaseRowFile::$image_extensions)
			&& $info = getimagesize($path)) {
			return $info['mime'];
		}

		return MixinMimetype::NOT_AVAILABLE;
	}

	protected function _detectFinfo($path)
	{
		if (!class_exists('finfo')) {
			return MixinMimetype::NOT_AVAILABLE;
		}

		// PHP updated libmagic to v5 in 5.3.11 which broke the old mimetype formats
		// Use the system wide magic file for these versions
		$database = version_compare(phpversion(), '5.3.11', '>=') ? null : dirname(__FILE__).'/mimetypes/magic';
		$finfo    = new \finfo(FILEINFO_MIME, $database);
		
		if (empty($finfo)) {
		    return MixinMimetype::NOT_AVAILABLE;
		}
		
		$mimetype = $finfo->file($path);<commit_msg>re #407 : Code cleanup<commit_after><?php
/**
 * Nooku Framework - http://www.nooku.org
 *
 * @copyright	Copyright (C) 2011 - 2013 <NAME> and Timble CVBA. (http://www.timble.net)
 * @license		GNU GPLv3 <http://www.gnu.org/licenses/gpl.html>
 * @link		git://git.assembla.com/nooku-framework.git for the canonical source repository
 */

namespace Nooku\Component\Files;

use Nooku\Library;

/**
 * Mimetype Mixin Class
 *
 * @author  <NAME> <http://nooku.assembla.com/profile/ercanozkaya>
 * @package Nooku\Component\Files
 */
class MixinMimetype extends Library\Object
{
	/**
	 * Used as a way to continue on the chain when the method is not available.
	 */
	const NOT_AVAILABLE = -1;

	/**
	 * Adapters to use for mimetype detection
	 *
	 * @var array
	 */
	protected $_adapters = array();

	public function __construct(Library\ObjectConfig $config)
	{
		parent::__construct($config);

		if (isset($config->adapters)) {
			$this->_adapters = Library\ObjectConfig::unbox($config->adapters);
		}
	}

	protected function _initialize(Library\ObjectConfig $config)
	{
		if (empty($config->adapters)) {
			$config->adapters = array('image', 'finfo');
		}

		parent::_initialize($config);
	}

	public function getMimetype($path)
	{
		$mimetype = false;

		if (file_exists($path))
        {
            foreach ($this->_adapters as $i => $adapter)
            {
                $function = '_detect'.ucfirst($adapter);
                $return = $this->$function($path);

                if (!empty($return) && $return !== MixinMimetype::NOT_AVAILABLE)
                {
                    $mimetype = $return;
                    break;
                }
            }

            // strip charset from text files
            if (!empty($mimetype) && strpos($mimetype, ';')) {
                $mimetype = substr($mimetype, 0, strpos($mimetype, ';'));
            }

            // special case: empty text files
            if ($mimetype == 'application/x-empty') {
                $mimetype = 'text/plain';
            }
		}

		return $mimetype;
	}

	protected function _detectImage($path)
	{
        if (in_array(strtolower(pathinfo($path, PATHINFO_EXTENSION)), DatabaseRowFile::$image_extensions)
			&& $info = getimagesize($path)) {
			return $info['mime'];
		}

		return MixinMimetype::NOT_AVAILABLE;
	}

	protected function _detectFinfo($path)
	{
		if (!class_exists('finfo')) {
			return MixinMimetype::NOT_AVAILABLE;
		}

		// PHP updated libmagic to v5 in 5.3.11 which broke the old mimetype formats
		// Use the system wide magic file for these versions
		$database = version_compare(phpversion(), '5.3.11', '>=') ? null : dirname(__FILE__).'/mimetypes/magic';
		$finfo    = new \finfo(FILEINFO_MIME, $database);
		
		if (empty($finfo)) {
		    return MixinMimetype::NOT_AVAILABLE;
		}
		
		$mimetype = $finfo->file($path);
{'fraction_non_alphanumeric': 0.15165963689712086, 'fraction_numerical': 0.007885567577480285, 'mean_word_length': 3.131818181818182, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 31, 'https://': 0, 'lorem ipsum': 0, 'www.': 6, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '909386', 'n_tokens_mistral': 2039, 'n_tokens_neox': 1822, 'n_words': 390}