Skip to content

armgit / Vvveb

Public Imported from GitHub
0

Powerful and easy to use cms to build websites, blogs or ecommerce stores.

GD, Imagick image override on save option

givanz5370c4701780
diff --git a/admin/controller/localization/language.php b/admin/controller/localization/language.php
index ebd067a..44ed37e 100755
--- a/admin/controller/localization/language.php
+++ b/admin/controller/localization/language.php
@@ -34,7 +34,9 @@ class Language extends Crud {
protected $module = 'localization';
- protected $installUrl = 'https://raw.githubusercontent.com/Vvveb/{code}/master/LC_MESSAGES/vvveb.po';
+ protected $installUrl = 'https://raw.githubusercontent.com/Vvveb/{code}/master/LC_MESSAGES/';
+
+ protected $files = ['vvveb.po', 'landing-theme.po'];
protected $listUrl = 'https://www.vvveb.com/page/contribute#language';
@@ -47,27 +49,34 @@ class Language extends Crud {
function install() {
$code = filter('/[-\w]+/', $this->request->post['code']);
$url = str_replace('{code}', $code, $this->installUrl);
- $translations = download($url);
-
- if ($translations) {
- $folder = DIR_ROOT . 'locale' . DS . $code . DS . 'LC_MESSAGES';
- $poFile = $folder . DS . 'vvveb.po';
- @mkdir($folder, 0755 & ~umask(), true);
-
- if (file_put_contents($poFile, $translations)) {
- require DIR_SYSTEM . 'functions' . DS . 'php-mo.php';
-
- if (phpmo_convert($poFile)) {
- $this->view->success[] = __('Language pack installed!');
+
+ require DIR_SYSTEM . 'functions' . DS . 'php-mo.php';
+
+ foreach ($this->files as $file) {
+ $translations = download($url . $file);
+
+ if ($translations) {
+ $folder = DIR_ROOT . 'locale' . DS . $code . DS . 'LC_MESSAGES';
+ $poFile = $folder . DS . $file;
+ @mkdir($folder, 0755 & ~umask(), true);
+
+ if (file_put_contents($poFile, $translations)) {
+
+ if (phpmo_convert($poFile)) {
+ $this->view->success['language'] = __('Language pack installed!');
+ } else {
+ $this->view->errors['language'] = __('Language compilation failed!');
+ break;
+ }
} else {
- $this->view->errors[] = __('Language compilation failed!');
+ $this->view->errors[] = __('Error writing language files!');
+ break;
}
} else {
- $this->view->errors[] = __('Error writing language files!');
+ $this->view->errors[] = __('Language pack not available!');
+ $this->view->info[] = sprintf(__('Check available translations at %s'), '<a href="' . $this->listUrl . '" target="_blank">' . $this->listUrl . '</a>');
+ break;
}
- } else {
- $this->view->errors[] = __('Language pack not available!');
- $this->view->info[] = sprintf(__('Check available translations at %s'), '<a href="' . $this->listUrl . '" target="_blank">' . $this->listUrl . '</a>');
}
return $this->index();
diff --git a/admin/controller/localization/translations.php b/admin/controller/localization/translations.php
index 5d6aa47..a7a4832 100755
--- a/admin/controller/localization/translations.php
+++ b/admin/controller/localization/translations.php
@@ -22,18 +22,108 @@
namespace Vvveb\Controller\Localization;
+use function Vvveb\__;
+use function Vvveb\url;
use Vvveb\Controller\Base;
use Vvveb\Sql\LanguageSQL;
+require_once DIR_SYSTEM . 'functions' . DS . 'php-mo.php';
+
class Translations extends Base {
- function add() {
+
+ function domains() {
+ $lang = $this->request->get['lang'] ?? false;
+ if ($lang) {
+ $folder = DIR_ROOT . 'locale' . DS . $lang . DS . 'LC_MESSAGES' . DS;
+ $files = glob($folder . '*.po');
+
+ $url = ['module' => 'localization/translations', 'action' => 'domain', 'lang' => $lang];
+ //add user if does not exist yet
+ $domains['user'] = url($url + ['domain' => 'user']);
+
+ foreach ($files as $file) {
+ $domain = str_replace('.po', '', basename($file));
+ $domains[$domain] = url($url + ['domain' => $domain]);
+ }
+
+ $this->view->domains = $domains;
+ $this->view->count = count($domains);
+ } else {
+ $this->notFound(false, __('Invalid request!'));
+ }
}
+
+ function save() {
+ $view = $this->view;
+ $translations = $this->request->post['translations'] ?? [];
+ $this->response->setType('json');
+
+ $message = __('No data!');
+
+ if ($translations) {
+ //use common check po file and parse
+ $this->domain();
+
+ //var_dump($this->view->translations);
+ $folder = DIR_ROOT . 'locale' . DS . $view->lang . DS . 'LC_MESSAGES' . DS;
+ foreach ($translations as $text => $translation) {
+ $view->translations[$text] = ['msgid' => $text, 'msgstr' => [$translation]];
+ }
+
+ if (phpmo_write_po_file($view->translations, $folder . $view->domain . '.po')) {
+ $message = __('Saved!');
+
+ //append user translations
+ if ($view->domain != 'user') {
+ $user = $folder . 'user.po';
+ if (file_exists($user) && ($translations = phpmo_parse_po_file($user) ?: []) && is_array($translations)) {
+ $view->translations = array_merge($view->translations, $translations);
+ }
+ }
+ //compile
+ if (phpmo_write_mo_file($view->translations, $folder . $view->domain . '.mo')) {
+ $message .= "\n" . __('Compiled!');
+ } else {
+ $message .= "\n" . __('Error compiling!');
+ }
+ } else {
+ $message = __('Error saving!');
+ }
+
+ }
- function index() {
- $languagesList = include DIR_SYSTEM . 'data/languages-list.php';
- $this->view->languagesList = $languagesList;
+ $result = ['message' => $message];
+ $this->response->output($result);
+ }
+
+ function domain() {
+ $view = $this->view;
+ $view->lang = $lang = $this->request->get['lang'] ?? false;
+ $view->domain = $domain = $this->request->get['domain'] ?? false;
+ $url = ['module' => 'localization/translations', 'lang' => $lang, 'domain' => $domain];
+ $view->domainsUrl = url($url + ['action' => 'domains']);
+ $view->saveUrl = url($url + ['action' => 'save']);
- $languages = new LanguageSQL();
- $this->view->languages = $languages->getAll();
+ if ($lang && $domain) {
+ $poFile = DIR_ROOT . 'locale' . DS . $lang . DS . 'LC_MESSAGES' . DS . $domain . '.po';
+ $view->poFile = $poFile;
+ //$poFile = '/home/www/vvveb/vvveb/locale/ro_RO/LC_MESSAGES/vvveb.po';
+
+ if (file_exists($poFile)) {
+ $view->translations = phpmo_parse_po_file($poFile);
+ if (!is_writeable($poFile)) {
+ $view->info = [__('Po file not writable, saving will not work!')];
+ }
+ } else {
+ if ($domain == 'user') {
+ //if domain is user then create it
+ $view->translations = [];
+ } else {
+ $this->notFound(false, __('Po file does not exist!'));
+ }
+ }
+ } else {
+ $this->notFound(false, __('Invalid request!'));
+ }
}
}
diff --git a/admin/template/localization/translations/domain.tpl b/admin/template/localization/translations/domain.tpl
new file mode 100755
index 0000000..b538839
--- /dev/null
+++ b/admin/template/localization/translations/domain.tpl
@@ -0,0 +1,23 @@
+import(common.tpl)
+import(pagination.tpl)
+
+[data-v-save-url]|href = $this->saveUrl
+[data-v-domains-url]|href = $this->domainsUrl
+
+@translation = [data-v-translations] tbody tr
+@translation|deleteAllButFirstChild
+
+@translation|before = <?php
+$count = 0;
+if(isset($this->translations) && is_array($this->translations)) {
+ foreach ($this->translations as $key => $translation) {
+ if (isset($translation['msgstr'])) {?>
+
+ @translation td:nth(1)|innerText = $key
+ @translation td:nth(2)|innerText = $translation['msgstr'][0]
+
+ @translation|after = <?php
+ $count++;
+ }
+ }
+}?>
diff --git a/admin/template/localization/translations/domains.tpl b/admin/template/localization/translations/domains.tpl
new file mode 100755
index 0000000..fd8fd40
--- /dev/null
+++ b/admin/template/localization/translations/domains.tpl
@@ -0,0 +1,19 @@
+import(common.tpl)
+
+
+@domain = [data-v-domains] [data-v-domain]
+@domain|deleteAllButFirst
+
+@domain|before = <?php
+ foreach ($this->domains as $domain => $url) {
+?>
+
+ @domain img|src = $domain['img']
+
+ @domain [data-v-domain-name]|innerText = $domain
+ @domain a[data-v-domain-url]|href = $url
+
+ @domain [data-v-domain-default]|if_exists = $domain['default']
+
+@domain|after = <?php
+} ?>
diff --git a/system/functions.php b/system/functions.php
index 72d31ec..32a7ea9 100755
--- a/system/functions.php
+++ b/system/functions.php
@@ -43,7 +43,7 @@ function url($parameters, $mergeParameters = false, $useCurrentUrl = true) {
static $urlParams = [];
if ($useCurrentUrl) {
- $url=parse_url($_SERVER['REQUEST_URI'] ?? '');
+ $url = parse_url($_SERVER['REQUEST_URI'] ?? '');
if (isset($url['query'])) {
parse_str($url['query'], $urlParams);
@@ -199,7 +199,12 @@ function friendlyDate($date) {
];
$time_direction = __(' ago');
- $diff = time() - strtotime($date) + 10;
+
+ if (is_string($date)) {
+ $date = strtotime($date);
+ }
+
+ $diff = time() - $date + 10;
if ($diff < 0) {
$time_direction = __(' from now');
@@ -379,7 +384,7 @@ function humanReadable($text) {
function cleanUrl($text, $divider = '-') {
// replace non letter or digits by divider
$text = preg_replace('/[^\pL\d]+/u', $divider, $text);
-
+
// remove unwanted characters
$text = preg_replace('/[^\\-\w]+/', '', $text);
@@ -412,13 +417,33 @@ function slugify($text, $divider = '-') {
return $text;
}
+$vvvebTranslationDomains = ['vvveb', 'landing-theme'];
+
+function addTranslationDomain($domain) {
+ global $vvvebTranslationDomains;
+
+ if (!in_array($domain, $vvvebTranslationDomains)) {
+ array_push($vvvebTranslationDomains, $domain);
+ }
+}
+
if (function_exists('_')) {
function __($text, $plural = false, $count = false) {
+ global $vvvebTranslationDomains;
+
if ($plural) {
- return ngettext($text, $plural, $count);
+ foreach ($vvvebTranslationDomains as $domain) {
+ $translation = dngettext($domain, $text, $plural, $count);
+ if ($translation != $text) break;
+ }
} else {
- return gettext($text);
+ foreach ($vvvebTranslationDomains as $domain) {
+ $translation = dgettext($domain, $text);
+ if ($translation != $text) break;
+ }
}
+
+ return $translation;
}
} else {
function __($text, $plural = false, $count = false) {
@@ -1084,6 +1109,7 @@ function userPreferedLanguage() {
* @return mixed
*/
function setLanguage($langCode = 'en_US', $domain = 'vvveb') {
+ global $vvvebTranslationDomains;
//setlocale(LC_TIME, "");
//\putenv('LOCPATH=' . DIR_ROOT. "locale");
@@ -1093,6 +1119,9 @@ function setLanguage($langCode = 'en_US', $domain = 'vvveb') {
}
if (function_exists('bindtextdomain')) {
+ foreach ($vvvebTranslationDomains as $tdomain) {
+ bindtextdomain($tdomain, DIR_ROOT . 'locale');
+ }
bindtextdomain($domain, DIR_ROOT . 'locale');
textdomain($domain);
bind_textdomain_codeset($domain, 'utf8');
@@ -1130,10 +1159,10 @@ function clearLanguageCache($langCode = 'en_US', $domain = 'vvveb') {
}
if (function_exists('symlink') && symlink($locale, $nocache)) {
- bindtextdomain($domain, $nocache);
- textdomain($domain);
- bind_textdomain_codeset($domain, 'different_codeset');
- bindtextdomain($domain, $locale);
+ bindtextdomain($domain, $nocache);
+ textdomain($domain);
+ bind_textdomain_codeset($domain, 'different_codeset');
+ bindtextdomain($domain, $locale);
}
@unlink($nocache);
@@ -1179,10 +1208,10 @@ function checkPhpSyntax($source) {
$tokens = false;
if (function_exists('token_get_all')) {
- try {
- $tokens = token_get_all($source, TOKEN_PARSE);
- } catch (\ParseError $e) {
- }
+ try {
+ $tokens = token_get_all($source, TOKEN_PARSE);
+ } catch (\ParseError $e) {
+ }
}
return $tokens ? true : false;
@@ -1203,6 +1232,7 @@ function truncateWords($text, $limit) {
* @return bool
*/
function email($to, $subject, $template, $data = [], $config = []) {
+ return true;
$email = System\Email::getInstance();
if (is_array($template)) {
@@ -1265,11 +1295,12 @@ function rrmdir($src, $skip = []) {
}
}
}
-
+
closedir($dir);
+
return rmdir($src);
}
-
+
return false;
}
diff --git a/system/functions/php-mo.php b/system/functions/php-mo.php
index 6550626..bc51540 100755
--- a/system/functions/php-mo.php
+++ b/system/functions/php-mo.php
@@ -1,4 +1,5 @@
<?php
+
/**
* php.mo 0.1 by Joss Crowcroft (http://www.josscrowcroft.com).
*
@@ -25,6 +26,7 @@
/**
* The main .po to .mo function.
*/
+
function phpmo_convert($input, $output = false) {
if (! $output) {
$output = str_replace('.po', '.mo', $input);
@@ -47,11 +49,13 @@ function phpmo_clean_helper($x) {
$x[$k] = phpmo_clean_helper($v);
}
} else {
- if ($x[0] == '"') {
+ if ($x && $x[0] == '"') {
$x = substr($x, 1, -1);
}
$x = str_replace("\"\n\"", '', $x);
$x = str_replace('$', '\\$', $x);
+ $x = str_replace('\\\\', '\\', $x);
+ $x = preg_replace('/\+(["\$n])/', '\$1', $x);
}
return $x;
@@ -83,7 +87,14 @@ function phpmo_parse_po_file($in) {
if ($line === '') {
continue;
}
- list($key, $data) = preg_split('/\s/', $line, 2);
+
+ $pair = preg_split('/\s/', $line, 2);
+ $key = $pair[0];
+ $data = $pair[1] ?? null;
+
+ if ($data === null) {
+ $continue;
+ }
switch ($key) {
case '#,': // flag...
@@ -109,6 +120,10 @@ function phpmo_parse_po_file($in) {
// context
case 'msgid':
// untranslated-string
+ if (sizeof($temp)) {
+ $hash[] = $temp;
+ $temp = [];
+ }
case 'msgid_plural':
// untranslated-string-plural
$state = $key;
@@ -180,6 +195,26 @@ function phpmo_parse_po_file($in) {
return $hash;
}
+
+function phpmo_po_text($hash) {
+ $text = '';
+ foreach ($hash as $id => $value) {
+ $msgid = phpmo_clean_helper(addcslashes($value['msgid'] ?? '', '"'));
+ $msgstr = phpmo_clean_helper(addcslashes($value['msgstr'][0] ?? '', '"'));
+ $text .= "msgid \"$msgid\"\nmsgstr \"$msgstr\"\n\n";
+ }
+
+ return $text;
+}
+
+/* Write a gettext .po file. */
+/* @link http://www.gnu.org/software/gettext/manual/gettext.html#MO-Files */
+function phpmo_write_po_file($hash, $out) {
+ $text = phpmo_po_text($hash);
+ return file_put_contents($out, $text);
+}
+
+
/* Write a GNU gettext style machine object. */
/* @link http://www.gnu.org/software/gettext/manual/gettext.html#MO-Files */
function phpmo_write_mo_file($hash, $out) {
@@ -192,7 +227,13 @@ function phpmo_write_mo_file($hash, $out) {
$ids = '';
$strings = '';
- foreach ($hash as $entry) {
+ foreach ($hash as $index => &$entry) {
+ //don't add empty translations
+ if (!$entry['msgstr'] || !$entry['msgstr'][0]) {
+ unset($hash[$index]);
+ continue;
+ }
+
$id = $entry['msgid'];
if (isset($entry['msgid_plural'])) {
@@ -202,8 +243,11 @@ function phpmo_write_mo_file($hash, $out) {
if (array_key_exists('msgctxt', $entry)) {
$id = $entry['msgctxt'] . "\x04" . $id;
}
+
// plural msgstrs are NUL-separated
$str = implode("\x00", $entry['msgstr']);
+ $str = str_replace('\n',"\n", $str);//restore newlines
+
// keep track of offsets
$offsets[] = [
strlen($ids
@@ -248,5 +292,5 @@ function phpmo_write_mo_file($hash, $out) {
// strings
$mo .= $strings;
- file_put_contents($out, $mo);
+ return file_put_contents($out, $mo);
}
diff --git a/system/media/gd.php b/system/media/gd.php
index d556dd0..f437dd9 100755
--- a/system/media/gd.php
+++ b/system/media/gd.php
@@ -79,27 +79,29 @@ class Image {
return $this->mime;
}
- public function save($file, $quality = 80) {
+ public function save($file, $quality = 80, $extension = false) {
if (! $this->image) {
return;
}
$info = pathinfo($file);
- $extension = strtolower($info['extension']);
+ $extension = $extension ?: strtolower($info['extension']);
if (is_object($this->image) || is_resource($this->image)) {
if ($extension == 'jpeg' || $extension == 'jpg') {
- imagejpeg($this->image, $file, $quality);
+ $result = imagejpeg($this->image, $file, $quality);
} elseif ($extension == 'png') {
- imagepng($this->image, $file, 8, PNG_ALL_FILTERS);
+ $result = imagepng($this->image, $file, 8, PNG_ALL_FILTERS);
} elseif ($extension == 'gif') {
- imagegif($this->image, $file);
+ $result = imagegif($this->image, $file);
} elseif ($extension == 'webp') {
- imagewebp($this->image, $file, $quality);
+ $result = imagewebp($this->image, $file, $quality);
}
- return imagedestroy($this->image);
+ imagedestroy($this->image);
+
+ return $result;
}
}
diff --git a/system/media/imagick.php b/system/media/imagick.php
index e8730d8..293c8b4 100755
--- a/system/media/imagick.php
+++ b/system/media/imagick.php
@@ -67,11 +67,12 @@ class Image {
return $this->mime;
}
- public function save($file, $quality = 80) {
+ public function save($file, $quality = 80, $extension = false) {
if (! $this->image) {
return;
}
- $this->image->setImageFormat($this->mime);
+
+ $this->image->setImageFormat($extension ?: $this->mime);
$this->image->setCompressionQuality($quality);
//$this->image->setImageCompressionQuality($quality);