<?php
/* --------------------------------------------------------------------
Chevereto
http://chevereto.com/
@version 3.2.6
@author Rodolfo Berrios A. <http://rodolfoberrios.com/>
<inbox@rodolfoberrios.com>
Copyright (C) 2014 Rodolfo Berrios A. All rights reserved.
BY USING THIS SOFTWARE YOU DECLARE TO ACCEPT THE CHEVERETO EULA
http://chevereto.com/license
--------------------------------------------------------------------- */
namespace CHV\Render;
use G, CHV;
if(!defined("access") or !access) die("This file cannot be directly accessed.");
function get_email_body_str($file) {
ob_start();
G\Render\include_theme_file($file);
$mail_body = ob_get_contents();
ob_end_clean();
return $mail_body;
}
/* ---------------------------------------------------------------------------------------------------------------------------------------- */
/*** THEME DATA FUNCTIONS ***/
/* ---------------------------------------------------------------------------------------------------------------------------------------- */
/*** LANGUAGE TAGS ***/
function get_lang_html_tags($lang_code=NULL) {
$lang = CHV\get_locales()[!is_null($lang_code) ? $lang_code : CHV_LANGUAGE_CODE];
return 'xml:lang="'.$lang['base'].'" lang="'.$lang['base'].'" dir="'.$lang['dir'].'"';
}
function get_select_options_html($arr, $selected) {
$html = '';
foreach($arr as $k => $v) {
$selected = is_bool($selected) ? ($selected ? 1 : 0) : $selected;
$html .= '<option value="'.$k.'"'.($selected == $k ? ' selected' : '').'>'.$v.'</option>'."\n";
}
return $html;
}
function get_recaptcha_html($theme="red", $key=NULL) {
$public_key = CHV\get_chv_setting('recaptcha_public_key');
if($key) {
$public_key = $key;
}
require_once(CHV_APP_PATH_LIB_VENDOR . "recaptchalib.php");
return '<script type="text/javascript">
var RecaptchaOptions = {
theme : "'.$theme.'"
};
</script>' . recaptcha_get_html($public_key, $theme);
}
/**
* ----------------------------------------------------------------------------------------------------------------------------------------
* ----------------------------------------------------------------------------------------------------------------------------------------
* ----------------------------------------------------------------------------------------------------------------------------------------
*/
function get_share_links($share_element) {
if(function_exists("get_share_links")) {
return \get_share_links($share_element);
}
if(!$share_element["twitter"]) {
$share_element["twitter"] = CHV\get_chv_setting('twitter_account');
}
$share_element["urlencoded"] = array();
foreach($share_element as $key => $value) {
if($key == "urlencoded") continue;
$share_element["urlencoded"][$key] = rawurlencode($value);
}
global $share_links_networks;
G\Render\include_theme_file('custom_hooks/share_links');
$return = array();
foreach($share_links_networks as $key => $value) {
$search = array("%URL%", "%TITLE%", "%DESCRIPTION%", "%HTML%", "%PHOTO_URL%", "%TWITTER%");
$replace= array("url", "title", "description", "HTML", "image", "twitter");
for($i=0; $i<count($replace); $i++) {
if(array_key_exists($replace[$i], $share_element["urlencoded"])) {
$replace[$i] = $share_element["urlencoded"][$replace[$i]];
}
}
$value["url"] = str_replace($search, $replace, $value["url"]);
$return[] = '<a data-href="'.$value["url"].'" class="popup-link btn-32 btn-social btn-'.$key.'" rel="tooltip" data-tiptip="top" title="'.$value["label"].'"><span class="btn-icon icon-'.$key.'"></span></a>';
}
return $return;
}
/**
* PEAFOWL FRAMEWORK
* ----------------------------------------------------------------------------------------------------------------------------------------
*/
function include_peafowl_head(){
echo '<link rel="stylesheet" href="' . versionize_src(CHV_URL_PEAFOWL . 'peafowl.css') . '">' . "\n" .
'<script>document.documentElement.className += " js";(function(w,d,u){w.readyQ=[];w.bindReadyQ=[];function p(x,y){if(x=="ready"){w.bindReadyQ.push(y);}else{w.readyQ.push(x);}};var a={ready:p,bind:p};w.$=w.jQuery=function(f){if(f===d||f===u){return a}else{p(f)}}})(window,document);' . "\n" .
'var devices=["phone","phablet","tablet","laptop","desktop"];window_to_device=function(){var e=[480,768,992,1024,1680],t=[],n="",r=window.innerWidth>0?window.innerWidth:screen.width;for(var i=0;i<devices.length;++i){if(r>=e[i]){t.push(devices[i])}}if(t.length==0)t.push(devices[0]);n=t[t.length-1];for(var i=0;i<devices.length;++i){document.documentElement.className=document.documentElement.className.replace(devices[i],"");if(i==devices.length-1){document.documentElement.className+=" "+n}document.documentElement.className=document.documentElement.className.replace(/\s+/g," ")}if(n=="laptop"||n=="desktop"){var s=document.getElementById("pop-box-mask");if(s!==null){s.parentNode.removeChild(s)}}};window_to_device();$(document).ready(function(){PF.obj.devices=window["devices"];PF.fn.window_to_device=window["window_to_device"]});</script>' . "\n";
}
function include_peafowl_foot(){
echo '<!--[if lte IE 7]><script src="' . versionize_src(CHV_URL_PEAFOWL . 'js/lte-ie7.js').'"></script><![endif]-->' . "\n" .
'<script src="' . versionize_src(CHV_URL_PEAFOWL . 'js/jquery.min.js').'"></script>' . "\n" .
'<script src="' . versionize_src(CHV_URL_PEAFOWL . 'js/scripts.js').'"></script>' . "\n" .
'<script>(function($,d){$.each(readyQ,function(i,f){$(f)});$.each(bindReadyQ,function(i,f){$(d).bind("ready",f)})})(jQuery,document)</script>' . "\n" .
'<script src="' . versionize_src(CHV_URL_PEAFOWL . 'peafowl.js').'"></script>';
}
function get_peafowl_item_list($tpl="image", $item, $template, $requester=NULL) {
if(empty($requester)) {
$requester = CHV\Login::getUser();
} else if(!is_array($requester) and !is_null($requester)) {
$requester = CHV\User::getSingle($requester, 'id');
}
// Default
$stock_tpl = "IMAGE";
if($tpl == "album" or $tpl == "user/album") {
$stock_tpl = "ALBUM";
}
if($tpl == "user") {
$stock_tpl = "USER";
} else {
if(array_key_exists('user', $item)) {
CHV\User::fill($item["user"]);
}
}
$filled_template = $template["tpl_list_item/$tpl"]; // Stock the unfilled template
$tpl_replacements = $template;
$conditional_replaces[$item["user"]["id"] == NULL ? "tpl_list_item/image_description_user" : "tpl_list_item/image_description_guest"] = NULL;
$conditional_replaces[$item["user"]["avatar"] == NULL ? "tpl_list_item/image_description_user_avatar" : "tpl_list_item/image_description_user_no_avatar"] = NULL;
if($stock_tpl == "IMAGE") {
$conditional_replaces['tpl_list_item/' . (!$item['file_resource']['image'] ? 'image_cover_image' : 'image_cover_empty')] = NULL;
}
if($stock_tpl == "ALBUM") {
$conditional_replaces[$item["image_count"] == 0 ? "tpl_list_item/album_cover_image" : "tpl_list_item/album_cover_empty"] = NULL;
for($i=1; $i<count($item["images_slice"]); $i++) {
$template["tpl_list_item/album_thumbs"] = str_replace("%$i", "", $template["tpl_list_item/album_thumbs"]);
}
$template["tpl_list_item/album_thumbs"] = preg_replace("/%[0-9]+(.*)%[0-9]+/", "", $template["tpl_list_item/album_thumbs"]);
}
if($stock_tpl == "USER") {
$conditional_replaces[$item["avatar"] ? "tpl_list_item/user_no_avatar" : "tpl_list_item/user_avatar"] = NULL;
foreach(array("twitter", "facebook", "website") as $social) {
if(!$item[$social]) {
$conditional_replaces["tpl_list_item/user_" . $social] = NULL;
}
}
$conditional_replaces[empty($item["avatar"]['url']) ? "tpl_list_item/user_cover_image" : "tpl_list_item/user_cover_empty"] = NULL;
$conditional_replaces[empty($item["background"]['url']) ? "tpl_list_item/user_background_image" : "tpl_list_item/user_background_empty"] = NULL;
}
if(!$requester['is_admin'] and (is_null($requester) or $item["user"]["id"] !== $requester['id']) ) {
$template["tpl_list_item/item_image_edit_tools"] = NULL;
}
if(!$requester['is_admin']) {
$template["tpl_list_item/item_image_admin_tools"] = NULL;
}
foreach($conditional_replaces as $k => $v) {
$template[$k] = $v;
}
preg_match_all("#%(tpl_list_item/.*)%#", $filled_template, $matches);
if(is_array($matches[1])) {
foreach($matches[1] as $k => $v) {
$filled_template = replace_tpl_string($v, $template[$v], $filled_template);
}
}
foreach($template as $k => $v) {
$filled_template = replace_tpl_string($k, $v, $filled_template);
}
// Now stock the item values
$replacements = array_change_key_case(flatten_array($item, $stock_tpl."_"), CASE_UPPER);
if($stock_tpl == "IMAGE" or $stock_tpl == "ALBUM") {
$replacements["ITEM_URL_EDIT"] = ($stock_tpl == "IMAGE" ? $item["url_viewer"] : $item["url"]) . "#edit";
}
// Public for the guest
if(!array_key_exists('user', $item)) {
$replacements['IMAGE_ALBUM_PRIVACY'] = 'public';
}
if($stock_tpl == 'IMAGE') {
$replacements['IMAGE_FLAG'] = $item['nsfw'] ? 'unsafe' : 'safe';
}
foreach($replacements as $k => $v) {
$filled_template = replace_tpl_string($k, $v, $filled_template);
}
$column_sizes = array(
"image" => 8,
"album" => 8,
"user" => 8
);
foreach($column_sizes as $k => $v) {
$filled_template = replace_tpl_string("COLUMN_SIZE_".strtoupper($k), $v, $filled_template);
}
return $filled_template;
}
function replace_tpl_string($search, $replace, $subject) {
return str_replace("%".$search."%", is_null($replace) ? "" : $replace, $subject);
}
// http://stackoverflow.com/a/9546215
function flatten_array($array, $prefix = '') {
$result = array();
foreach($array as $key => $value) {
if(is_array($value)) {
$result = $result + flatten_array($value, $prefix . $key . '_');
} else {
$result[$prefix . $key] = $value;
}
}
return $result;
}
// This function is sort of an alias of php die() but with html error display
function chevereto_die($error_msg, $paragraph=NULL, $title=NULL) {
if(!is_array($error_msg) && G\check_value($error_msg)) $error_msg = array($error_msg);
if(is_null($paragraph)) {
$paragraph = "The system has encountered errors or missettings that must be fixed to allow proper Chevereto functionality. Chevereto won't run until the following issues are solved:";
}
$solution = "Need help or questions about this? Go to <a href='http://chevereto.com/support' target='_blank'>Chevereto support<a/>.";
$title = (!is_null($title)) ? $title : 'System error';
$doctitle = $title . " - Chevereto";
$handled_request = G_ROOT_PATH == '/' ? sanitize_path_slashes($_SERVER["REQUEST_URI"]) : str_ireplace(G_ROOT_PATH_RELATIVE, "", G\add_trailing_slashes($_SERVER["REQUEST_URI"]));
$base_request = explode('/', rtrim(str_replace("//", "/", str_replace("?", "/", $handled_request)), '/'))[0];
if($base_request == 'json' || $base_request == 'api'){
$output = array(
'status_code' => 500,
'status_txt' => G\get_set_status_header_desc(500),
'error' => $title,
'errors' => $error_msg
);
G\set_status_header(500);
G\json_prepare();
die(G\Render\json_output($output));
}
$html = [
'<h1>'.$title.'</h1>',
'<p>'.$paragraph.'</p>'
];
if(is_array($error_msg)) {
$html[] = '<ul class="errors">';
foreach($error_msg as $error) {
$html[] = '<li>'.$error.'</li>';
}
$html[] = '</ul>';
}
$html[] = '<p>'.$solution.'</p>';
$html = join("", $html);
$template = CHV_APP_PATH_SYSTEM . 'template.php';
if(!require_once($template)) {
die("Can't find " . G\absolute_to_relative($system_template));
}
die();
}
function getFriendlyExif($Exif) {
if(gettype($Exif) == 'string') {
$Exif = json_decode($Exif);
}
if($Exif->Make) {
$exif_show_data = [];
$exif_show_data[] = $Exif->ExposureTime;
if($Exif->FNumber or $Exif->COMPUTED->ApertureFNumber) {
$exif_show_data[] = 'ƒ/' . ($Exif->FNumber ? G\fraction_to_decimal($Exif->FNumber) : explode('/', $Exif->COMPUTED->ApertureFNumber)[1]);
}
if($Exif->ISOSpeedRatings) {
$exif_show_data[] = 'ISO' . $Exif->ISOSpeedRatings;
}
if($Exif->FocalLength) {
$exif_show_data[] = G\fraction_to_decimal($Exif->FocalLength) . 'mm';
}
return (object) [
'camera' => $Exif->Make . " " . $Exif->Model,
'capture' => implode(" ", $exif_show_data)
];
}
return null;
}
function arr_printer($arr, $tpl='', $wrap=[]) {
ksort($arr);
$rtn = '';
$rtn .= $wrap[0];
foreach($arr as $k => $v) {
if(is_array($v)) {
$rtn .= strtr($tpl, ['%K' => $k, '%V' => arr_printer($v, $tpl, $wrap)]);
} else {
$rtn .= strtr($tpl, ['%K' => $k, '%V' => $v]);
}
}
$rtn .= $wrap[1];
return $rtn;
}
function versionize_src($src) {
//return $src.'?'.G\random_string(128);
return $src.'?'.md5(CHV\get_chevereto_version());
}
function show_banner($banner) {
if(strpos($banner, 'banner_') !== 0) {
$banner = 'banner_' . $banner;
};
$banner_code = CHV\get_chv_setting($banner);
if($banner_code) {
echo '<div id="'.$banner.'" class="ad-banner">'.$banner_code.'</div>';
}
}