You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
223 lines
7.4 KiB
223 lines
7.4 KiB
<?php
|
|
|
|
namespace tts;
|
|
|
|
final class assets {
|
|
|
|
public static function get_ver(string $filename): string {
|
|
$safe_file = \tts\security::filter_uri($filename);
|
|
if (! file_exists($safe_file)) {
|
|
return "?ver=false";
|
|
}
|
|
return "?ver=" . date('Y.m.d_H.i.s', filemtime($safe_file));
|
|
}
|
|
|
|
public static function get_ajax_files(string $ajax_folder): string {
|
|
$safe_folder = \tts\security::filter_uri($ajax_folder);
|
|
|
|
$ret = "var assets_files = [];" . PHP_EOL;
|
|
$js = glob($safe_folder . "*.{js,css}", GLOB_BRACE);
|
|
foreach($js as $file) {
|
|
$ret .= 'assets_files.push({ filename: "' . basename($file) . '", ts: "' . self::get_ver($file) . '" });' . PHP_EOL;
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public static function loadall_css(array $files, string $scope='project'): string {
|
|
$ret = '';
|
|
foreach($files as $file=>$a_media) {
|
|
$ret .= self::wrap_css($file, $scope, $a_media);
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public static function loadall_js(array $files, string $scope='project'): string {
|
|
$ret = '';
|
|
foreach($files as $file=>$a) {
|
|
$ret .= self::wrap_js($file, $scope, $a);
|
|
}
|
|
return $ret;
|
|
}
|
|
|
|
public static function image(string $file, string $scope = '', array $a = array()): string {
|
|
$more = '';
|
|
if (count($a)) {
|
|
foreach ($a as $k => $v) {
|
|
$more .= " {$k}=\"{$v}\"";
|
|
}
|
|
}
|
|
$file = self::wrap_asset($file, $scope);
|
|
if ($file === false) {
|
|
return '';
|
|
}
|
|
return "<img src=\"{$file}\" {$more}/>\r\n";
|
|
}
|
|
|
|
public static function alert($msg) {
|
|
return self::inline_js('alert("'.$msg.'");');
|
|
}
|
|
|
|
/**
|
|
* Wrapper for JS/CSS assets for the page.
|
|
* @param string $file or CDN
|
|
* @param string $scope (project/framework/cdn)
|
|
* @retval boolean|string false is not found, else Asset REF
|
|
*/
|
|
public static function wrap_asset(string $file, string $scope = 'project') {
|
|
$scope = \tts\common::string_to_lowercase($scope);
|
|
if ($scope === 'cdn') {
|
|
$proto = \tts\safer_io::get_clean_server_var('SERVER_PROTOCOL');
|
|
if ($proto === null) {
|
|
$protocol = '://';
|
|
} else {
|
|
$protocol = strtolower(substr($proto, 0, strpos($proto, '/'))) . '://';
|
|
}
|
|
return (\tts\common::is_string_found($file, '://') === true) ? $file : $protocol . $file;
|
|
}
|
|
if ($scope === 'project' || $scope === 'app') {
|
|
$path = PROJECT_ASSETS_DIR . "/";
|
|
$tts = self::is_minified($path, $file);
|
|
return ($tts !== false) ? PROJECT_ASSETS_BASE_REF . "/" . $tts : false;
|
|
}
|
|
if ($scope === 'assets') {
|
|
$path = dirname(\tts\site_helper::get_root(), 1) . "/public/assets/";
|
|
$tts = self::is_minified($path, $file);
|
|
return ($tts !== false) ? "/assets/" . $tts : false;
|
|
}
|
|
return $file;
|
|
}
|
|
/**
|
|
* Fetch Version of file if exists
|
|
* @param string $file
|
|
* @param string $scope
|
|
* @return string|bool
|
|
*/
|
|
private static function project_paths(string $file, string $scope = 'project'): string | bool {
|
|
$scope = \tts\common::string_to_lowercase($scope);
|
|
if ($scope === 'cdn') {
|
|
return "";
|
|
} else if ($scope === 'project' || $scope === 'app') {
|
|
$path = PROJECT_ASSETS_DIR . "/";
|
|
} else if ($scope === 'assets') {
|
|
$path = dirname(\tts\site_helper::get_root(), 1) . "/public/assets/";
|
|
} else {
|
|
return "";
|
|
}
|
|
$check = self::is_minified($path, $file);
|
|
return ($check !==false) ? self::get_ver($path . $check) : false;
|
|
}
|
|
|
|
/**
|
|
* Wrapper to return the CSS href=file.. stylesheet code for use in page.
|
|
* @param string $file - CSS file
|
|
* @param string $media - default of all media
|
|
* @retval string
|
|
*/
|
|
public static function wrap_css(string $file, string $scope = 'project', array $a_media = array()): string {
|
|
$more = '';
|
|
if (count($a_media)) {
|
|
foreach ($a_media as $k => $v) {
|
|
$more .= " {$k}=\"{$v}\"";
|
|
}
|
|
} else {
|
|
$more .= " media=\"all\"";
|
|
}
|
|
|
|
$ver = self::project_paths($file, $scope);
|
|
$wrap_file = self::wrap_asset($file, $scope);
|
|
if ($ver === false || $wrap_file === false) {
|
|
return "<!-- CSS: {$file} not Found -->";
|
|
}
|
|
return "<link rel=\"stylesheet\" href=\"{$wrap_file}{$ver}\" type=\"text/css\"{$more}/>\r\n";
|
|
}
|
|
|
|
/**
|
|
* Wrapper to return the JavaScript src=file... code for use with page.
|
|
* @param type $file - external JS file.
|
|
* @retval string of script src=file
|
|
*/
|
|
public static function wrap_js(string $file, string $scope = 'project', array $a = array()): string {
|
|
$more = '';
|
|
if (count($a)) {
|
|
foreach ($a as $k => $v) {
|
|
$more .= (isset($k) && $k !=0 ) ? " {$k}=\"{$v}\"" : " {$v}";
|
|
}
|
|
}
|
|
|
|
$ver = self::project_paths($file, $scope);
|
|
$wrap_file = self::wrap_asset($file, $scope);
|
|
if ($ver === false || $wrap_file === false) {
|
|
return "<!-- Script: {$file} not Found -->";
|
|
}
|
|
return "<script src=\"{$wrap_file}{$ver}\" type=\"text/javascript\"{$more}></script>\r\n";
|
|
//return "<script type=\"text/javascript\">js_loader(\"{$file}\");</script>";
|
|
}
|
|
|
|
/**
|
|
* Purpose: To do inline JavaScript.
|
|
* @param type $code string of code to inline into page.
|
|
* @retval type
|
|
*/
|
|
public static function inline_js(string $code): string {
|
|
return "<script type=\"text/javascript\">\r\n//<![CDATA[\r\n {$code}\r\n //]]> \r\n </script>\r\n";
|
|
}
|
|
|
|
/**
|
|
* Purpose: To execute this JavaScript code once JQuery is Ready().
|
|
* @param string $code to do once JQuery is Ready
|
|
* @retval string wrapped in ready code...
|
|
*/
|
|
public static function jquery_load(string $code): string {
|
|
return "\r\n$(function() { \r\n \t {$code} \r\n }); \r\n";
|
|
}
|
|
|
|
public static function is_minified(string $path, string $file) {
|
|
$safe_path = \tts\security::filter_uri($path);
|
|
$safe_file = \tts\security::filter_uri($file);
|
|
|
|
if (\tts\common::is_string_found($safe_file, '.auto.js')) {
|
|
$production = (\tts\main\configure::get('tts', 'live') === true) ? true : false;
|
|
$safe_file = str_replace('.auto.js', '', $safe_file);
|
|
if ( $production && file_exists($safe_path . $safe_file . '.min.js') ) {
|
|
return $safe_file . '.min.js';
|
|
}
|
|
return (file_exists($safe_path . $safe_file . '.js')) ? $safe_file . '.js' : false;
|
|
}
|
|
|
|
if (\tts\common::is_string_found($safe_file, '.auto.css')) {
|
|
$production = (\tts\main\configure::get('tts', 'live') === true) ? true : false;
|
|
$safe_file = str_replace('.auto.css', '', $safe_file);
|
|
if ( $production && file_exists($safe_path . $safe_file . '.min.css') ) {
|
|
return $safe_file . '.min.css';
|
|
}
|
|
return (file_exists($safe_path . $safe_file . '.css')) ? $safe_file . '.css' : false;
|
|
}
|
|
return ( file_exists($safe_path . $safe_file) ) ? $safe_file : false;
|
|
}
|
|
|
|
/**
|
|
* meta redirect when headers are already sent...
|
|
* @param string url - site to do redirect on
|
|
* @reval none
|
|
*/
|
|
public static function goto_url(string $url): void {
|
|
echo '<META http-equiv="refresh" content="0;URL=' . $url . '">';
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Rediect to url and attempt to send via header.
|
|
* @param string $url - site to do redirect for
|
|
* @retval none - exits once done
|
|
*/
|
|
public static function redirect_url(string $url): void {
|
|
$url = str_replace(array('&', "\n", "\r"), array('&', '', ''), $url);
|
|
if (!headers_sent()) {
|
|
header('Location: ' . $url);
|
|
} else {
|
|
self::goto_url($url);
|
|
}
|
|
exit;
|
|
}
|
|
|
|
}
|
|
|