Skip to content

Commit

Permalink
1.34.0
Browse files Browse the repository at this point in the history
* [-] removed rewrite_hrefs_to_blank(), convert_UTF16BE_to_UTF8() functions
* [*] updated GDWrapper methods, added image quality
* [*] added quality parameter to rotate(), rotate2(), addWaterMark()
* [*] minor fixes
  • Loading branch information
KarelWintersky committed Jun 4, 2021
1 parent 78e0446 commit 70c51a0
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 143 deletions.
87 changes: 13 additions & 74 deletions functions/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,9 @@ function getResourcePath($type = "photos", $cdate = null): string
. date("Y/m", $cdate);

if (!is_dir($path)) {
mkdir($path, 0777, true);
if (!mkdir( $path, 0777, true ) && !is_dir( $path )) {
throw new \RuntimeException( sprintf( 'Directory "%s" was not created', $path ) );
}
}

return $path;
Expand Down Expand Up @@ -71,7 +73,9 @@ function getimagepath($type = "photos", $cdate = null):string
. DIRECTORY_SEPARATOR;

if (!is_dir($path)) {
mkdir($path, 0777, true);
if (!mkdir( $path, 0777, true ) && !is_dir( $path )) {
throw new \RuntimeException( sprintf( 'Directory "%s" was not created', $path ) );
}
}

return $path;
Expand All @@ -82,7 +86,7 @@ function getimagepath($type = "photos", $cdate = null):string
/**
*
* @param $number
* @param array $forms (array or string with glues, x|y|z or [x,y,z]
* @param mixed $forms (array or string with glues, x|y|z or [x,y,z]
* @param string $glue
* @return string
*/
Expand Down Expand Up @@ -240,7 +244,7 @@ function smarty_modifier_html_substr($string, $length, $addstring = "")
$noTagLength = strlen(strip_tags($string));

// Parser loop
for ($j = 0; $j < strlen($string); $j++) {
for ($j = 0, $jMax = strlen( $string ); $j < $jMax; $j++) {

$currentChar = substr($string, $j, 1);
$ret .= $currentChar;
Expand Down Expand Up @@ -280,7 +284,7 @@ function smarty_modifier_html_substr($string, $length, $addstring = "")
$currentTag = substr($currentTag, 1, -1);
}

array_push($tagsArray, $currentTag);
$tagsArray[] = $currentTag;

} else if (strpos($currentTag, "</") !== FALSE) {
array_pop($tagsArray);
Expand All @@ -304,7 +308,7 @@ function smarty_modifier_html_substr($string, $length, $addstring = "")
}

// Close broken XHTML elements
while (sizeof($tagsArray) != 0) {
while (count($tagsArray) != 0) {
$aTag = array_pop($tagsArray);
// if a <p> or <li> tag needs to be closed, put the add-string in first
if (($aTag == "p" || $aTag == "li") && strlen($string) > $length) {
Expand All @@ -321,80 +325,15 @@ function smarty_modifier_html_substr($string, $length, $addstring = "")
// if we have not added the add-string already
if (strlen($string) > $length && $addstringAdded == false) {
return ($ret . $addstring);
} else {
return ($ret);
}
} else {
return ($string);

return ($ret);
}
}

}

/**
* Doctor Piter
*/
if (!function_exists('convert_UTF16BE_to_UTF8')){
function convert_UTF16BE_to_UTF8($t)
{
//return preg_replace( '#%u([0-9A-F]{1,4})#ie', "'& #'.hexdec('\\1').';'", $t );
// return preg_replace('#%u([0-9A-F]{4})#se', 'iconv("UTF-16BE","UTF-8",pack("H4","$1"))', $t);
return preg_replace_callback('#%u([0-9A-F]{4})#s', function (){
iconv("UTF-16BE","UTF-8", pack("H4","$1"));
}, $t);
return ($string);
}
}

if (!function_exists('rewrite_hrefs_to_blank')) {

/**
*
* @param $text
* @return string|string[]|null
*/
function rewrite_hrefs_to_blank(string $text):string
{
return preg_replace_callback("/<a([^>]+)>(.*?)<\/a>/i", function ($matches) {
$matches[1] = trim($matches[1]);
$arr = [];

$matches[1] = preg_replace("/([\"']{0,})\s([a-zA-Z]+\=(\"|'))/i", "$1-!break!-$2", $matches[1]);
$matches[1] = explode("-!break!-", $matches[1]);

// предполагаем, что по-умолчанию у всех ссылок нужно ставить target=_blank
$blank = true;
foreach ($matches[1] as $v) {
$r = explode("=", $v, 2);
$r[1] = trim($r[1], "'");
$r[1] = trim($r[1], '"');
$arr[$r[0]] = $r[1];
if ($r[0] == "href") {
// условия, исключающие установку target=_blank
if (!preg_match("/([a-zA-Z]+)\:\/\/(.*)/i", $r[1])) {
// ссылка не начинается с какого-либо протокола, соот-но она внутренняя и новое окно не нужно
$blank = false;
} else {
// ссылка внешняя, и нам надо понять, не ведет ли она в наш же домен
if (stristr($r[1], getenv('DOMAIN.FQDN'))) $blank = false;
}
}
}
// если уже есть в списке target, то ничего не делаем
foreach ($arr as $k => $v) {
if ($k == "blank") $blank = false;
}
if ($blank) $arr['target'] = "_blank";
$prms = array();
foreach ($arr as $k => $v) {
$prms[] = "{$k}=\"{$v}\"";
}
$params_as_string = implode(" ", $prms);
return "<a {$params_as_string}>{$matches[2]}</a>";
}, $text);

}
}


# -eof-

9 changes: 5 additions & 4 deletions interfaces/GDWrapperInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public static function verticalimage(string $fn_source, string $fn_target, int $
* @return bool
*/
public static function getFixedPicture(string $fn_source, string $fn_target, int $maxwidth, int $maxheight, int $image_quality = null):bool;

/**
* Добавляет на изображение вотермарк (
*
Expand All @@ -87,9 +87,10 @@ public static function getFixedPicture(string $fn_source, string $fn_target, int
* @param string $fn_source
* @param array $params
* @param int $pos_index
* @param null $quality
* @return bool
*/
public static function addWaterMark(string $fn_source, array $params, int $pos_index):bool;
public static function addWaterMark(string $fn_source, array $params, int $pos_index, $quality = null):bool;

/**
* NEVER USED
Expand All @@ -100,7 +101,7 @@ public static function addWaterMark(string $fn_source, array $params, int $pos_i
* @param string $dist
* @return bool
*/
public static function rotate(string $fn_source, string $dist = ""):bool;
public static function rotate(string $fn_source, string $dist = "", $quality = null):bool;

/**
* Используется на 47news
Expand All @@ -112,5 +113,5 @@ public static function rotate(string $fn_source, string $dist = ""):bool;
* @param string $dist
* @return bool
*/
public static function rotate2(string $fn_source, string $dist = ""):bool ;
public static function rotate2(string $fn_source, string $dist = "", $quality = null):bool ;
}
10 changes: 7 additions & 3 deletions sources/DateTimeLocal.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ public static function convertDateRu(string $datetime, bool $is_show_time = fals

public static function convertDate(string $datetime, bool $is_show_time = false, string $year_suffix = 'г.'):string
{
if ($datetime == "0000-00-00 00:00:00" or $datetime == "0000-00-00") return "-";
if ($datetime === "0000-00-00 00:00:00" || $datetime === "0000-00-00") return "-";
list($y, $m, $d, $h, $i, $s) = sscanf($datetime, "%d-%d-%d %d:%d:%d");

$rusdate = $d . ' ' . self::ruMonths[$m] .
($y ? " {$y} {$year_suffix}" : '');
$rusdate
= $d
. ' '
. self::ruMonths[$m]
. ($y ? " {$y} {$year_suffix}"
: '');

if ($is_show_time) {
$rusdate .= " " . sprintf("%02d", $h) . ":" . sprintf("%02d", $i);
Expand Down
Loading

0 comments on commit 70c51a0

Please sign in to comment.