Files
le-matelot/client/plugins/traduction/Traduction.class.php
2020-01-27 08:56:08 +01:00

325 lines
9.5 KiB
PHP

<?php
/*************************************************************************************/
/* */
/* Thelia - Plugin de tradunction */
/* */
/* Copyright (c) Franck Allimant, 2010-2012 */
/* email : franck@cqfdev.fr */
/* */
/* This program is free software; you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation; either version 2 of the License, or */
/* (at your option) any later version. */
/* */
/* This program is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program; if not, write to the Free Software */
/* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
/* */
/*************************************************************************************/
include_once(dirname(__FILE__) . "/../../../classes/PluginsClassiques.class.php");
include_once(dirname(__FILE__) . "/../../../classes/Lang.class.php");
include_once(dirname(__FILE__) . "/../../../classes/Reecriture.class.php");
class Traduction extends PluginsClassiques
{
const MARQUE_DEBUT = '::';
const MARQUE_FIN = '::';
const VERSION = '1.0.7';
const MODULE = 'traduction';
const LANGDIR = '/template/lang';
public $basedir;
public $idlangue;
public $langues;
public $base_thelia;
public function __construct()
{
parent::__construct("traduction");
$this->langues = false;
$this->base_thelia = realpath(dirname(__FILE__) . '/../../..');
}
public function init()
{
$this->ajout_desc(
"Traduction",
"Plugin de traduction de votre site",
"Ce plugin permet de traduire facilement votre site directement depuis le back-office de Thélia.",
1);
$langdir = $this->base_thelia . self::LANGDIR;
if (! is_dir($langdir))
{
if (! @mkdir($langdir, 0777, true))
{
die("Impossible de créer le répertoire $langdir. Créez le manuellement, en lui donnant les permissions nécessaires à la création de fichiers (777), puis rechargez cette page.");
}
@chmod($langdir, 0777);
}
if (! $fh = @fopen("$langdir/test.tmp", 'w'))
{
die("Impossible de créer un fichier le répertoire $langdir. Donnez lui les permissions nécessaires à la création de fichiers (777), puis rechargez cette page.");
}
else
{
fclose($fh);
@unlink("$langdir/test.tmp");
}
$this->importer();
$this->importer_15();
}
public function destroy() {
// Restaurer les anciens fichiers de langue 1.5
$langdir = realpath(dirname(__FILE__) . '/../../../template/lang');
if ($dh = @opendir($langdir))
{
while ($file = readdir($dh))
{
if (preg_match('/^([0-9])+\.php.ANCIEN/', $file, $matches))
{
$langfile = $this->base_thelia . self::LANGDIR.'/'.$matches[1].'.php';
if (@rename("$langdir/$file", $langfile) === false) {
die("Echec lors du renommage du fichier $langdir/$file. Autorisez les droits en écriture de ce fichier et du répertoire qui le contient.");
}
}
}
}
}
public function post()
{
global $res;
if (preg_match_all('!'.self::MARQUE_DEBUT.'(.*?)'.self::MARQUE_FIN.'!is', $res, $matches))
{
$this->charger_langues($source = 'session');
$transfile = $this->base_thelia . self::LANGDIR. '/traduction_'.$this->idlangue.'.php';
if (is_file($transfile))
{
@include_once($transfile);
}
foreach($matches[1] as $match)
{
$hash = hash('md5', $match);
if (isset($traduction[$hash]))
{
$res = str_replace(self::MARQUE_DEBUT.$match.self::MARQUE_FIN, $traduction[$hash], $res);
}
else
{
$res = str_replace(self::MARQUE_DEBUT.$match.self::MARQUE_FIN, $match, $res);
}
}
}
}
public function boucle($texte, $args)
{
$exclure = lireTag($args, 'exclure');
$id = lireTag($args, 'id');
$this->charger_langues($source = 'session');
$res = '';
// Trouver l'url ré-écrite, si elle existe
$reecriture = new Reecriture();
if (Variable::lire("rewrite") != 0) {
$requrl = lireParam('url', 'string');
if ($requrl != '') $reecriture->charger($requrl);
}
$url = preg_replace('/[\&\?]*lang=[0-9]+/', '', $_SERVER['REQUEST_URI']);
$url .= strstr($url, '?') == false ? '?' : '&';
$lng = new Lang();
$query = 'select id from ' . $lng->table . ' where 1 ';
if ($id != '') $query .= ' and id in ( ' . $id . ')';
if ($exclure != '') $query .= ' and id not in ( ' . $exclure . ')';
$result = mysql_query($query);
$lngredir = new Reecriture();
while ($result && $row = mysql_fetch_object($result))
{
$lng->charger($row->id);
if ($reecriture->actif && $lngredir->charger_param($reecriture->fond, $reecriture->param, $lng->id, 1)) {
$lngurl = $lngredir->url;
}
else {
$lngurl = htmlentities($url . 'lang='.$lng->id);
}
$tmp = str_replace('#ID', $lng->id, $texte);
$tmp = str_replace('#DESCRIPTION', $lng->description, $tmp);
$tmp = str_replace('#CODE', $lng->code, $tmp);
$tmp = str_replace('#URL', $lngurl, $tmp);
$res .= $tmp;
}
return $res;
}
protected function charger_langues($source = 'session')
{
if ($this->langues) return;
$this->langues = array();
$lng = new Lang();
$query = 'select id from ' . $lng->table;
$result = mysql_query($query);
while ($result && $row = mysql_fetch_object($result))
{
$lng = new Lang();
$lng->charger($row->id);
$this->langues[] = $lng;
}
if ($source == 'session')
{
$this->idlangue = isset($_SESSION["navig"]->lang) && intval($_SESSION["navig"]->lang) != 0 ? intval($_SESSION["navig"]->lang) : 1;
}
else
{
$this->idlangue = isset($_REQUEST['lang']) && intval($_REQUEST['lang']) != 0 ? intval($_REQUEST['lang']) : 1;
}
}
protected function importer()
{
// Importer les fichiers de FichierLang, si y'en a
$langdir = realpath(dirname(__FILE__)) . '/../../../lang';
if ($dh = @opendir($langdir))
{
while ($file = readdir($dh))
{
if (preg_match('/lang([0-9])\.php/', $file, $matches))
{
unset($GLOBALS['dictionnaire']);
include_once("$langdir/$file");
$langfile = $this->base_thelia . self::LANGDIR.'/traduction_'.$matches[1].'.php';
// Ne pas écraser un fichier existant
if (file_exists($langfile)) continue;
if ($fp = fopen($langfile, 'w'))
{
fwrite($fp, '<' . '?php' . "\n");
fwrite($fp, '$traduction = array('."\n");
foreach($GLOBALS['dictionnaire'] as $key => $val)
{
$hash = hash('md5', $key);
$val = str_replace("'", "\'", $val);
fwrite($fp, "\t'$hash' => '".$val."',\n");
}
fwrite($fp, ");\n");
fwrite($fp, '?' . ">\n");
fclose($fp);
}
else
{
die("Echec lors de l'écriture du fichier $langfile. Autorisez les droits en écriture de ce fichier et du répertoire qui le contient.");
}
}
}
}
}
protected function importer_15()
{
// Importer les fichiers de FichierLang, si y'en a
$langdir = realpath(dirname(__FILE__) . '/../../../template/lang');
if ($dh = @opendir($langdir))
{
while ($file = readdir($dh))
{
if (preg_match('/^([0-9])+\.php/', $file, $matches))
{
unset($GLOBALS['dicotpl']);
include_once("$langdir/$file");
$langfile = $this->base_thelia . self::LANGDIR.'/traduction_'.$matches[1].'.php';
// Ne pas écraser un fichier existant
if (file_exists($langfile)) continue;
if ($fp = fopen($langfile, 'w'))
{
fwrite($fp, '<' . '?php' . "\n");
fwrite($fp, '$traduction = array('."\n");
foreach($GLOBALS['dicotpl'] as $key => $val)
{
$hash = hash('md5', $key);
$val = str_replace("'", "\'", $val);
fwrite($fp, "\t'$hash' => '".$val."',\n");
}
fwrite($fp, ");\n");
fwrite($fp, '?' . ">\n");
fclose($fp);
if (@rename("$langdir/$file", "$langdir/$file.ANCIEN") === false) {
die("Echec lors du renommage du fichier $langdir/$file. Autorisez les droits en écriture de ce fichier et du répertoire qui le contient.");
}
}
else
{
die("Echec lors de l'écriture du fichier $langfile. Autorisez les droits en écriture de ce fichier et du répertoire qui le contient.");
}
}
}
}
}
}
?>