Ejemplo n.º 1
0
/**
 * Smarty {fetch} plugin
 *
 * Type:     function<br>
 * Name:     fetch<br>
 * Purpose:  fetch file, web or ftp data and display results
 * @link http://smarty.php.net/manual/en/language.function.fetch.php {fetch}
 *       (Smarty online manual)
 * @author Monte Ohrt <monte at ohrt dot com>
 * @param array
 * @param Smarty
 * @return string|null if the assign parameter is passed, Smarty assigns the
 *                     result to a template variable
 */
function smarty_function_fetch($params, &$smarty)
{
    if (empty($params['file'])) {
        $smarty->_trigger_fatal_error("[plugin] parameter 'file' cannot be empty");
        return;
    }
    $content = '';
    if ($smarty->security && !preg_match('!^(http|ftp)://!i', $params['file'])) {
        $_params = array('resource_type' => 'file', 'resource_name' => $params['file']);
        require_once SMARTY_CORE_DIR . 'core.is_secure.php';
        if (!smarty_core_is_secure($_params, $smarty)) {
            $smarty->_trigger_fatal_error('[plugin] (secure mode) fetch \'' . $params['file'] . '\' is not allowed');
            return;
        }
        // fetch the file
        if ($fp = @fopen($params['file'], 'r')) {
            while (!feof($fp)) {
                $content .= fgets($fp, 4096);
            }
            fclose($fp);
        } else {
            $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
            return;
        }
    } else {
        // not a local file
        if (preg_match('!^http://!i', $params['file'])) {
            // http fetch
            if ($uri_parts = parse_url($params['file'])) {
                // set defaults
                $host = $server_name = $uri_parts['host'];
                $timeout = 30;
                $accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*";
                $agent = "Smarty Template Engine " . $smarty->_version;
                $referer = "";
                $uri = !empty($uri_parts['path']) ? $uri_parts['path'] : '/';
                $uri .= !empty($uri_parts['query']) ? '?' . $uri_parts['query'] : '';
                $_is_proxy = false;
                if (empty($uri_parts['port'])) {
                    $port = 80;
                } else {
                    $port = $uri_parts['port'];
                }
                if (!empty($uri_parts['user'])) {
                    $user = $uri_parts['user'];
                }
                if (!empty($uri_parts['pass'])) {
                    $pass = $uri_parts['pass'];
                }
                // loop through parameters, setup headers
                foreach ($params as $param_key => $param_value) {
                    switch ($param_key) {
                        case "file":
                        case "assign":
                        case "assign_headers":
                            break;
                        case "user":
                            if (!empty($param_value)) {
                                $user = $param_value;
                            }
                            break;
                        case "pass":
                            if (!empty($param_value)) {
                                $pass = $param_value;
                            }
                            break;
                        case "accept":
                            if (!empty($param_value)) {
                                $accept = $param_value;
                            }
                            break;
                        case "header":
                            if (!empty($param_value)) {
                                if (!preg_match('![\\w-]+: .+!', $param_value)) {
                                    $smarty->_trigger_fatal_error("[plugin] invalid header format '" . $param_value . "'");
                                    return;
                                } else {
                                    $extra_headers[] = $param_value;
                                }
                            }
                            break;
                        case "proxy_host":
                            if (!empty($param_value)) {
                                $proxy_host = $param_value;
                            }
                            break;
                        case "proxy_port":
                            if (!preg_match('!\\D!', $param_value)) {
                                $proxy_port = (int) $param_value;
                            } else {
                                $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '" . $param_key . "'");
                                return;
                            }
                            break;
                        case "agent":
                            if (!empty($param_value)) {
                                $agent = $param_value;
                            }
                            break;
                        case "referer":
                            if (!empty($param_value)) {
                                $referer = $param_value;
                            }
                            break;
                        case "timeout":
                            if (!preg_match('!\\D!', $param_value)) {
                                $timeout = (int) $param_value;
                            } else {
                                $smarty->_trigger_fatal_error("[plugin] invalid value for attribute '" . $param_key . "'");
                                return;
                            }
                            break;
                        default:
                            $smarty->_trigger_fatal_error("[plugin] unrecognized attribute '" . $param_key . "'");
                            return;
                    }
                }
                if (!empty($proxy_host) && !empty($proxy_port)) {
                    $_is_proxy = true;
                    $fp = fsockopen($proxy_host, $proxy_port, $errno, $errstr, $timeout);
                } else {
                    $fp = fsockopen($server_name, $port, $errno, $errstr, $timeout);
                }
                if (!$fp) {
                    $smarty->_trigger_fatal_error("[plugin] unable to fetch: {$errstr} ({$errno})");
                    return;
                } else {
                    if ($_is_proxy) {
                        fputs($fp, 'GET ' . $params['file'] . " HTTP/1.0\r\n");
                    } else {
                        fputs($fp, "GET {$uri} HTTP/1.0\r\n");
                    }
                    if (!empty($host)) {
                        fputs($fp, "Host: {$host}\r\n");
                    }
                    if (!empty($accept)) {
                        fputs($fp, "Accept: {$accept}\r\n");
                    }
                    if (!empty($agent)) {
                        fputs($fp, "User-Agent: {$agent}\r\n");
                    }
                    if (!empty($referer)) {
                        fputs($fp, "Referer: {$referer}\r\n");
                    }
                    if (isset($extra_headers) && is_array($extra_headers)) {
                        foreach ($extra_headers as $curr_header) {
                            fputs($fp, $curr_header . "\r\n");
                        }
                    }
                    if (!empty($user) && !empty($pass)) {
                        fputs($fp, "Authorization: BASIC " . base64_encode("{$user}:{$pass}") . "\r\n");
                    }
                    fputs($fp, "\r\n");
                    while (!feof($fp)) {
                        $content .= fgets($fp, 4096);
                    }
                    fclose($fp);
                    $csplit = explode("\r\n\r\n", $content, 2);
                    $content = $csplit[1];
                    if (!empty($params['assign_headers'])) {
                        $smarty->assign($params['assign_headers'], split("\r\n", $csplit[0]));
                    }
                }
            } else {
                $smarty->_trigger_fatal_error("[plugin] unable to parse URL, check syntax");
                return;
            }
        } else {
            // ftp fetch
            if ($fp = @fopen($params['file'], 'r')) {
                while (!feof($fp)) {
                    $content .= fgets($fp, 4096);
                }
                fclose($fp);
            } else {
                $smarty->_trigger_fatal_error('[plugin] fetch cannot read file \'' . $params['file'] . '\'');
                return;
            }
        }
    }
    if (!empty($params['assign'])) {
        $smarty->assign($params['assign'], $content);
    } else {
        return $content;
    }
}
Ejemplo n.º 2
0
 /**
  * fetch the template info. Gets timestamp, and source
  * if get_source is true
  *
  * sets $source_content to the source of the template, and
  * $resource_timestamp to its time stamp
  * @param string $resource_name
  * @param string $source_content
  * @param integer $resource_timestamp
  * @param boolean $get_source
  * @param boolean $quiet
  * @return boolean
  */
 function _fetch_resource_info(&$params)
 {
     if (!isset($params['get_source'])) {
         $params['get_source'] = true;
     }
     if (!isset($params['quiet'])) {
         $params['quiet'] = false;
     }
     $_return = false;
     $_params = array('resource_name' => $params['resource_name']);
     if (isset($params['resource_base_path'])) {
         $_params['resource_base_path'] = $params['resource_base_path'];
     } else {
         $_params['resource_base_path'] = $this->template_dir;
     }
     if ($this->_parse_resource_name($_params)) {
         $_resource_type = $_params['resource_type'];
         $_resource_name = $_params['resource_name'];
         switch ($_resource_type) {
             case 'file':
                 if ($params['get_source']) {
                     $params['source_content'] = $this->_read_file($_resource_name);
                 }
                 $params['resource_timestamp'] = filemtime($_resource_name);
                 $_return = is_file($_resource_name);
                 break;
             default:
                 // call resource functions to fetch the template source and timestamp
                 if ($params['get_source']) {
                     $_source_return = isset($this->_plugins['resource'][$_resource_type]) && call_user_func_array($this->_plugins['resource'][$_resource_type][0][0], array($_resource_name, &$params['source_content'], &$this));
                 } else {
                     $_source_return = true;
                 }
                 $_timestamp_return = isset($this->_plugins['resource'][$_resource_type]) && call_user_func_array($this->_plugins['resource'][$_resource_type][0][1], array($_resource_name, &$params['resource_timestamp'], &$this));
                 $_return = $_source_return && $_timestamp_return;
                 break;
         }
     }
     if (!$_return) {
         // see if we can get a template with the default template handler
         if (!empty($this->default_template_handler_func)) {
             if (!is_callable($this->default_template_handler_func)) {
                 $this->trigger_error("default template handler function \"{$this->default_template_handler_func}\" doesn't exist.");
             } else {
                 $_return = call_user_func_array($this->default_template_handler_func, array($_params['resource_type'], $_params['resource_name'], &$params['source_content'], &$params['resource_timestamp'], &$this));
             }
         }
     }
     if (!$_return) {
         if (!$params['quiet']) {
             $this->trigger_error('unable to read resource: "' . $params['resource_name'] . '"');
         }
     } else {
         if ($_return && $this->security) {
             require_once SMARTY_CORE_DIR . 'core.is_secure.php';
             if (!smarty_core_is_secure($_params, $this)) {
                 if (!$params['quiet']) {
                     $this->trigger_error('(secure mode) accessing "' . $params['resource_name'] . '" is not allowed');
                 }
                 $params['source_content'] = null;
                 $params['resource_timestamp'] = null;
                 return false;
             }
         }
     }
     return $_return;
 }
/**
 * Smarty {html_js_icon} function plugin
 *
 * Type:     function<br>
 * Name:     html_js_icon<br>
 * Date:     October 06, 2005
 * Input:<br>
 *         - button = button (and path) of image (required)
 *         - border = border width (optional, default 0)
 *         - height = image height (optional, default actual height)
 *         - basedir = base directory
 *
 * Examples: {html_js_icon image="email.gif"}
 * Output:   <img src="images/masthead.gif" border=0 width=400 height=23>
 * @author r23 <*****@*****.**>
 * @author credits to Monte Ohrt <*****@*****.**>
 * @author credits to Duda <*****@*****.**> - wrote first image function
 *           in repository, helped with lots of functionality
 * @version  1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_html_js_icon($params, &$smarty)
{
    MyOOS_CoreApi::requireOnce('lib/smarty/libs/plugins/shared.escape_special_chars.php');
    $image = '';
    $alt = '';
    $align = 'middle';
    $border = 0;
    $height = '';
    $width = '';
    $extra = '';
    $sTheme = oos_var_prep_for_os($_SESSION['theme']);
    $basedir = 'themes/' . $sTheme . '/images/icons/';
    foreach ($params as $_key => $_val) {
        switch ($_key) {
            case 'image':
            case 'basedir':
            case 'align':
                ${$_key} = $_val;
                break;
            case 'alt':
                if (!is_array($_val)) {
                    ${$_key} = smarty_function_escape_special_chars($_val);
                } else {
                    $smarty->trigger_error("html_js_icon: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
            default:
                if (!is_array($_val)) {
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
                } else {
                    $smarty->trigger_error("html_js_icon: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    if (empty($image)) {
        $smarty->trigger_error("html_js_icon: missing 'button' parameter", E_USER_NOTICE);
        return;
    }
    $_image_path = $basedir . $image;
    if (!isset($params['width']) || !isset($params['height'])) {
        if ($smarty->security && ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) && (require_once SMARTY_CORE_DIR . 'core.is_secure.php') && !smarty_core_is_secure($_params, $smarty)) {
            $smarty->trigger_error("html_js_icon:: (secure) '{$_image_path}' not in secure directory", E_USER_NOTICE);
        } elseif (!($_image_data = @getimagesize($_image_path))) {
            if (!file_exists($_image_path)) {
                $smarty->trigger_error("html_js_icon: unable to find '{$_image_path}'", E_USER_NOTICE);
                return;
            } elseif (!is_readable($_image_path)) {
                $smarty->trigger_error("html_js_icon: unable to read '{$_image_path}'", E_USER_NOTICE);
                return;
            } else {
                $smarty->trigger_error("html_js_icon: '{$_image_path}' is not a valid image button", E_USER_NOTICE);
                return;
            }
        }
        if (!isset($params['width'])) {
            $width = $_image_data[0];
        }
        if (!isset($params['height'])) {
            $height = $_image_data[1];
        }
    }
    return '<img src="' . $basedir . $image . '" alt="' . $alt . '" align="' . $align . '" border="' . $border . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />';
}
Ejemplo n.º 4
0
/**
 * Smarty {html_image} function plugin
 *
 * Type:     function<br>
 * Name:     html_image<br>
 * Date:     Feb 24, 2003<br>
 * Purpose:  format HTML tags for the image<br>
 * Input:<br>
 *         - file = file (and path) of image (required)
 *         - height = image height (optional, default actual height)
 *         - width = image width (optional, default actual width)
 *         - basedir = base directory for absolute paths, default
 *                     is environment variable DOCUMENT_ROOT
 *         - path_prefix = prefix for path output (optional, default empty)
 *
 * Examples: {html_image file="/images/masthead.gif"}
 * Output:   <img src="/images/masthead.gif" width=400 height=23>
 * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
 *      (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @author credits to Duda <*****@*****.**> - wrote first image function
 *           in repository, helped with lots of functionality
 * @version  1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_html_image($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared', 'escape_special_chars');
    $alt = '';
    $file = '';
    $height = '';
    $width = '';
    $extra = '';
    $prefix = '';
    $suffix = '';
    $path_prefix = '';
    $server_vars = $smarty->request_use_auto_globals ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
    $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
    foreach ($params as $_key => $_val) {
        switch ($_key) {
            case 'file':
            case 'height':
            case 'width':
            case 'dpi':
            case 'path_prefix':
            case 'basedir':
                ${$_key} = $_val;
                break;
            case 'alt':
                if (!is_array($_val)) {
                    ${$_key} = smarty_function_escape_special_chars($_val);
                } else {
                    $smarty->trigger_error("html_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
            case 'link':
            case 'href':
                $prefix = '<a href="' . $_val . '">';
                $suffix = '</a>';
                break;
            default:
                if (!is_array($_val)) {
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
                } else {
                    $smarty->trigger_error("html_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    if (empty($file)) {
        $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
        return;
    }
    if (substr($file, 0, 1) == '/') {
        $_image_path = $basedir . $file;
    } else {
        $_image_path = $file;
    }
    if (!isset($params['width']) || !isset($params['height'])) {
        if (!($_image_data = @getimagesize($_image_path))) {
            if (!file_exists($_image_path)) {
                $smarty->trigger_error("html_image: unable to find '{$_image_path}'", E_USER_NOTICE);
                return;
            } else {
                if (!is_readable($_image_path)) {
                    $smarty->trigger_error("html_image: unable to read '{$_image_path}'", E_USER_NOTICE);
                    return;
                } else {
                    $smarty->trigger_error("html_image: '{$_image_path}' is not a valid image file", E_USER_NOTICE);
                    return;
                }
            }
        }
        if ($smarty->security && ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) && (require_once SMARTY_CORE_DIR . 'core.is_secure.php') && !smarty_core_is_secure($_params, $smarty)) {
            $smarty->trigger_error("html_image: (secure) '{$_image_path}' not in secure directory", E_USER_NOTICE);
        }
        if (!isset($params['width'])) {
            $width = $_image_data[0];
        }
        if (!isset($params['height'])) {
            $height = $_image_data[1];
        }
    }
    if (isset($params['dpi'])) {
        if (strstr($server_vars['HTTP_USER_AGENT'], 'Mac')) {
            $dpi_default = 72;
        } else {
            $dpi_default = 96;
        }
        $_resize = $dpi_default / $params['dpi'];
        $width = round($width * $_resize);
        $height = round($height * $_resize);
    }
    return $prefix . '<img src="' . $path_prefix . $file . '" alt="' . $alt . '" width="' . $width . '" height="' . $height . '"' . $extra . ' />' . $suffix;
}
Ejemplo n.º 5
0
 function test_core_is_secure_function_is_secure_false()
 {
     $security = $this->smarty->security;
     $this->smarty->security = true;
     /* check if test_cases.php is secure (should be false) */
     $params = array('resource_type' => 'file', 'resource_base_path' => dirname(__FILE__) . '/templates', 'resource_name' => __FILE__);
     $this->assertFalse(smarty_core_is_secure($params, $this->smarty));
     $this->smarty->security = $security;
 }
/**
* Smarty {resized_image} function plugin for impressCMS
*
* Type:     function
* Name:     resized_image
* Date:     December 22, 2010
* Purpose:  resizes an image to the specified size and returns a formatted HTML tag for the image
* Input:
*         - file = file (and path) of image (required)
*         - height = image height (optional, default actual height)
*         - width = image width (optional, default actual width)
*         - basedir = base directory for absolute paths, default
*                     is environment variable DOCUMENT_ROOT
*         - path_prefix = prefix for path output (optional, default empty)
*         - return = return an IMG tag or just and URL (defaults to IMG tag)
*		     - fit = The way the image is resized. Possible values:
			� inside (the image will fit inside the new dimensions while maintaining the aspect ratio)
			� fill (the image will completely fit inside the new dimensions)
			� outside (the image will be resized to completely fill the specified rectangle, while still maintaining aspect ratio)
*
* Examples: {resized_image file="/images/image.jpg" height=70 width=120 fit="inside"}
* You can add extra parameters to the tag, such as class, alt or id.
* {resized_image file="/images/image.jpg" height=70 width=120 fit="inside" class="resized"}
* @author Ignacio Segura <nacho at pensamientosdivergentes.net>
* Based on work by Monte Ohrt <monte at ohrt dot com> and Duda <*****@*****.**>
* @version  1.11
* @param array
* @param Smarty
* @return string
* @uses smarty_function_escape_special_chars()
* @uses WideImage library
*/
function smarty_function_resized_image($params, &$smarty)
{
    require_once $smarty->_get_plugin_filepath('shared', 'escape_special_chars');
    // Preparing arrays that will store original and resized image data
    $original = array();
    $resized = array();
    // Get parameters from the tag
    $alt = '';
    $file = '';
    $height = '';
    $width = '';
    $extra = '';
    $prefix = '';
    $suffix = '';
    $fit = 'inside';
    $return = 'img';
    $path_prefix = '';
    $server_vars = $smarty->request_use_auto_globals ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
    $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';
    foreach ($params as $_key => $_val) {
        switch ($_key) {
            case 'file':
            case 'height':
            case 'width':
            case 'fit':
            case 'return':
            case 'path_prefix':
            case 'basedir':
                ${$_key} = $_val;
                break;
            case 'alt':
                if (!is_array($_val)) {
                    ${$_key} = smarty_function_escape_special_chars($_val);
                } else {
                    $smarty->trigger_error("resized_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
            case 'link':
            case 'href':
                $prefix = '<a href="' . $_val . '">';
                $suffix = '</a>';
                break;
            default:
                if (!is_array($_val)) {
                    $extra .= ' ' . $_key . '="' . smarty_function_escape_special_chars($_val) . '"';
                } else {
                    $smarty->trigger_error("resized_image: extra attribute '{$_key}' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }
    // Checking the existence of required parameters
    if (empty($file)) {
        $smarty->trigger_error("resized_image: missing 'file' parameter", E_USER_ERROR);
        return;
    }
    if (!isset($params['width']) && !isset($params['height'])) {
        $smarty->trigger_error("resized_image: New size was not specified", E_USER_ERROR);
        return;
    }
    // If image resized is 'fit', both height and width are required
    if ($fit == 'fill' && (empty($width) || empty($height))) {
        $smarty->trigger_error("resized_image:  When you choose 'fill' fit, you have to specify both width and height", E_USER_ERROR);
    }
    // Transliteration - prepare a clean name for file
    $clean_file = str_replace(' ', '_', $file);
    // remove spaces
    $from = explode(' ', 'Á á É é è ê È Ê Í í Ó ó Ú ú Ñ ñ Ç ç');
    $to = explode(' ', 'A a E e e e E E I i O o U u N n C c');
    $clean_file = str_replace($from, $to, strtolower($clean_file));
    // removing special characters, convert to lowercase, encoding in URL the remaining for safe
    $clean_file = str_replace('%2F', '/', urlencode($clean_file));
    // URLencode the remaining, but taking into consideration the / char.
    // Preparing paths
    if (substr($file, 0, 1) == '/') {
        $original['path'] = $basedir . $file;
        $resized['path'] = ICMS_ROOT_PATH . '/cache' . $clean_file;
        $resized['url'] = ICMS_URL . '/cache' . $clean_file;
    } elseif (strpos($file, ICMS_URL) === 0) {
        // In case of full URL
        $original['path'] = ICMS_ROOT_PATH . str_replace(ICMS_URL, '', $file);
        $clean_file = str_replace(ICMS_URL, '', urldecode($clean_file));
        // Clean file shouuld not have Full URL
        $resized['path'] = ICMS_ROOT_PATH . '/cache' . $clean_file;
        $resized['url'] = ICMS_URL . '/cache' . $clean_file;
    } else {
        $original['path'] = $file;
        $resized['path'] = ICMS_ROOT_PATH . '/cache/' . $clean_file;
        $resized['url'] = ICMS_URL . '/cache/' . $clean_file;
    }
    // Check if original image exists
    if (!($_image_data = @getimagesize($original['path']))) {
        if (!file_exists($original['path'])) {
            $smarty->trigger_error("resized_image: unable to find '" . $original['path'] . "'", E_USER_NOTICE);
            return;
        } else {
            if (!is_readable($original['path'])) {
                $smarty->trigger_error("resized_image: unable to read '" . $original['path'] . "'", E_USER_NOTICE);
                return;
            } else {
                $smarty->trigger_error("resized_image: '" . $original['path'] . "' is not a valid image file", E_USER_NOTICE);
                return;
            }
        }
    }
    // Smarty Security check (comes from Smarty html_image tag, being honest, I don't understand what it does).
    if ($smarty->security && ($_params = array('resource_type' => 'file', 'resource_name' => $original['path'])) && (require_once SMARTY_CORE_DIR . 'core.is_secure.php') && !smarty_core_is_secure($_params, $smarty)) {
        $smarty->trigger_error("resized_image: (secure) '" . $original['path'] . "' not in secure directory", E_USER_NOTICE);
    }
    // Original and resized dimensions
    if (!isset($params['width'])) {
        $original['width'] = $_image_data[0];
    }
    if (!isset($params['height'])) {
        $original['height'] = $_image_data[1];
    }
    $resized['width'] = $width;
    $resized['height'] = $height;
    // build resized file name
    $resized['dir'] = substr($resized['path'], 0, strrpos($resized['path'], "/"));
    // extract path
    $resized['path'] = substr($resized['path'], 0, strrpos($resized['path'], ".")) . "-" . $resized['width'] . "x" . $resized['height'] . substr($resized['path'], strrpos($resized['path'], "."));
    // build path + file name
    $resized['url'] = substr($resized['url'], 0, strrpos($resized['url'], ".")) . "-" . $resized['width'] . "x" . $resized['height'] . substr($resized['url'], strrpos($resized['url'], "."));
    // build file URL
    // If file does not exist
    // or it's outdated, create:
    if (!file_exists($resized['path']) or filemtime($original['path']) > filemtime($resized['path'])) {
        if (!is_dir($resized['dir'])) {
            // If dir does not exist, create
            mkdir($resized['dir'], 0755, true);
        }
        // Resize image using WideImage library
        include_once ICMS_LIBRARIES_PATH . '/wideimage/lib/WideImage.php';
        $resized_img = WideImage::load($original['path'], 'jpg');
        $resized_img->resize($resized['width'], $resized['height'], $fit)->saveToFile($resized['path']);
    }
    if ($return == 'url') {
        return $resized['url'];
    } else {
        return $prefix . '<img src="' . $resized['url'] . '" alt="' . $alt . '" ' . $extra . ' />' . $suffix;
    }
}
Ejemplo n.º 7
0
    /**
     * fetch the template info. Gets timestamp, and source
     * if get_source is true
     *
     * sets $source_content to the source of the template, and
     * $resource_timestamp to its time stamp
     * @param string $resource_name
     * @param string $source_content
     * @param integer $resource_timestamp
     * @param boolean $get_source
     * @param boolean $quiet
     * @return boolean
     */
    function _fetch_resource_info(&$params)
    {
        if (!isset($params['get_source'])) {
            $params['get_source'] = true;
        }
        if (!isset($params['quiet'])) {
            $params['quiet'] = false;
        }
        $_return = false;
        $_params = array('resource_name' => $params['resource_name']);
        if (isset($params['resource_base_path'])) {
            $_params['resource_base_path'] = $params['resource_base_path'];
        } else {
            $_params['resource_base_path'] = $this->template_dir;
        }
        if ($this->_parse_resource_name($_params)) {
            $_resource_type = $_params['resource_type'];
            $_resource_name = $_params['resource_name'];
            switch ($_resource_type) {
                case 'file':
                    if ($params['get_source']) {
                        $params['source_content'] = $this->_read_file($_resource_name);
                    }
                    $params['resource_timestamp'] = filemtime($_resource_name);
                    $_return = is_file($_resource_name) && is_readable($_resource_name);
                    break;
                default:
                    // call resource functions to fetch the template source and timestamp
                    if ($params['get_source']) {
                        $_source_return = isset($this->_plugins['resource'][$_resource_type]) && call_user_func_array($this->_plugins['resource'][$_resource_type][0][0], array($_resource_name, &$params['source_content'], &$this));
                    } else {
                        $_source_return = true;
                    }
                    $_timestamp_return = isset($this->_plugins['resource'][$_resource_type]) && call_user_func_array($this->_plugins['resource'][$_resource_type][0][1], array($_resource_name, &$params['resource_timestamp'], &$this));
                    $_return = $_source_return && $_timestamp_return;
                    break;
            }
        }
        if (!$_return) {
            // see if we can get a template with the default template handler
            if (!empty($this->default_template_handler_func)) {
                if (!is_callable($this->default_template_handler_func)) {
                    $this->trigger_error("default template handler function \"{$this->default_template_handler_func}\" doesn't exist.");
                } else {
                    $_return = call_user_func_array($this->default_template_handler_func, array($_params['resource_type'], $_params['resource_name'], &$params['source_content'], &$params['resource_timestamp'], &$this));
                }
            }
        }
        if (!$_return) {
            if (!$params['quiet']) {
                $file_Name = explode('/', $params['resource_name']);
                $file_Name = end($file_Name);
                $this->html_trigger_error('Невозможно загрузить шаблон

                    <span style="font-weight:bold;color:green;">' . (!empty($file_Name) ? $file_Name : $params['resource_name']) . '</span>' . (!empty($file_Name) ? ' из папки: <span style="color:blue;">' . $this->template_dir . '/' . str_replace($file_Name, '', $params['resource_name']) . '</span>' : ''));
            }
        } else {
            if ($_return && $this->security) {
                require_once SMARTY_CORE_DIR . 'core.is_secure.php';
                if (!smarty_core_is_secure($_params, $this)) {
                    if (!$params['quiet']) {
                        $this->trigger_error('(secure mode) accessing "' . $params['resource_name'] . '" is not allowed');
                    }
                    $params['source_content'] = null;
                    $params['resource_timestamp'] = null;
                    return false;
                }
            }
        }
        return $_return;
    }
/**
 * Smarty {html_image} function plugin
 *
 * Type:     function<br>
 * Name:     html_image<br>
 * Date:     Feb 24, 2003<br>
 * Purpose:  format HTML tags for the image<br>
 * Input:<br>
 *         - file = file (and path) of image (required)
 *         - height = image height (optional, default actual height)
 *         - width = image width (optional, default actual width)
 *         - basedir = base directory for absolute paths, default
 *                     is environment variable DOCUMENT_ROOT
 *
 * Examples: {html_image file="images/masthead.gif"}
 * Output:   <img src="images/masthead.gif" width=400 height=23>
 * @link http://smarty.php.net/manual/en/language.function.html.image.php {html_image}
 *      (Smarty online manual)
 * @author   Monte Ohrt <monte at ohrt dot com>
 * @author credits to Duda <*****@*****.**> - wrote first image function
 *           in repository, helped with lots of functionality
 * @version  1.0
 * @param array
 * @param Smarty
 * @return string
 * @uses smarty_function_escape_special_chars()
 */
function smarty_function_html_image($params, &$smarty)
{
    MyOOS_CoreApi::requireOnce('lib/smarty/libs/plugins/shared.escape_special_chars.php');

    $alt = '';
    $file = '';
    $height = '';
    $width = '';
    $extra = '';
    $prefix = '';
    $suffix = '';

    $server_vars = ($smarty->request_use_auto_globals) ? $_SERVER : $GLOBALS['HTTP_SERVER_VARS'];
    $basedir = isset($server_vars['DOCUMENT_ROOT']) ? $server_vars['DOCUMENT_ROOT'] : '';

    foreach($params as $_key => $_val) {
        switch($_key) {
            case 'file':
            case 'height':
            case 'width':
            case 'dpi':
            case 'basedir':
                $$_key = $_val;
                break;

            case 'alt':
                if(!is_array($_val)) {
                    $$_key = smarty_function_escape_special_chars($_val);
                } else {
                    $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;

            case 'link':
            case 'href':
                $prefix = '<a href="' . $_val . '">';
                $suffix = '</a>';
                break;

            default:
                if(!is_array($_val)) {
                    $extra .= ' '.$_key.'="'.smarty_function_escape_special_chars($_val).'"';
                } else {
                    $smarty->trigger_error("html_image: extra attribute '$_key' cannot be an array", E_USER_NOTICE);
                }
                break;
        }
    }

    if (empty($file)) {
        $smarty->trigger_error("html_image: missing 'file' parameter", E_USER_NOTICE);
        return;
    }

    if (substr($file,0,1) == '/') {
        $_image_path = $basedir . $file;
    } else {
        $_image_path = $file;
    }

    if(!isset($params['width']) || !isset($params['height'])) {
        if ($smarty->security &&
            ($_params = array('resource_type' => 'file', 'resource_name' => $_image_path)) &&
            (require_once(SMARTY_CORE_DIR . 'core.is_secure.php')) &&
            (!smarty_core_is_secure($_params, $smarty)) ) {
            $smarty->trigger_error("html_image: (secure) '$_image_path' not in secure directory", E_USER_NOTICE);

        } elseif (!$_image_data = @getimagesize($_image_path)) {
            if(!file_exists($_image_path)) {
                $smarty->trigger_error("html_image: unable to find '$_image_path'", E_USER_NOTICE);
                return;
            } elseif(!is_readable($_image_path)) {
                $smarty->trigger_error("html_image: unable to read '$_image_path'", E_USER_NOTICE);
                return;
            } else {
                $smarty->trigger_error("html_image: '$_image_path' is not a valid image file", E_USER_NOTICE);
                return;
            }
        }

        if(!isset($params['width'])) {
            $width = $_image_data[0];
        }
        if(!isset($params['height'])) {
            $height = $_image_data[1];
        }

    }

    $sSlash = (defined('OOS_XHTML') && (OOS_XHTML == '1') ? ' /' : '');

    return $prefix . '<img src="'.$file.'" alt="'.$alt.'" width="'.$width.'" height="'.$height.'"'.$extra.$sSlash.'>' . $suffix;
}