1:  2:  3:  4:  5:  6:  7:  8:  9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 
<?php
namespace Omeka\View\Helper;

use Zend\View\Helper\AbstractHelper;

/**
 * View helper for rendering the upload size limit.
 */
class UploadLimit extends AbstractHelper
{
    const MEGABYTE_BYTES = 1048576;

    /**
     * Show the upload size limit.
     *
     * @return string
     */
    public function __invoke()
    {
        $uploadMaxFilesize = $this->parseSize(ini_get('upload_max_filesize'));
        $postMaxSize = $this->parseSize(ini_get('post_max_size'));

        $uploadLimit = min($uploadMaxFilesize, $postMaxSize);
        $uploadLimit = round($uploadLimit / self::MEGABYTE_BYTES);

        $format = $this->getView()->translate('The maximum upload size is %s MB.');
        return sprintf($format, $uploadLimit);
    }

    /**
     * Get the size in bytes represented by the given php ini config string
     *
     * @param string $sizeString
     * @return int Size in bytes
     */
    protected function parseSize($sizeString)
    {
        $value = intval($sizeString);

        $lastChar = substr($sizeString, -1);
        // Note: these cases fall through purposely
        switch ($lastChar) {
            case 'g':
            case 'G':
                $value *= 1024;
            case 'm':
            case 'M':
                $value *= 1024;
            case 'k':
            case 'K':
                $value *= 1024;
        }

        return $value;
    }
}