PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 1ms

/i18n/i18n.plugin.disabled.php

https://github.com/ldleman/Leed-market
PHP | 393 lines | 325 code | 40 blank | 28 comment | 61 complexity | 6ca4a0409ebd25c67c57bc3830294312 MD5 | raw file
  1. <?php
  2. /*
  3. @name i18n
  4. @author Cobalt74 <http://www.cobestran.com>
  5. @link http://www.cobestran.com
  6. @licence CC by nc sa http://creativecommons.org/licenses/by-nc-sa/2.0/fr/
  7. @version 2.6.0
  8. @description Le plugin i18n permet d'effectuer une traduction de Leed et des plugins en générant les fichiers Json souhaités
  9. */
  10. // affichage d'un lien dans le menu "Gestion"
  11. function i18n_plugin_AddLink(){
  12. echo '<li><a class="toggle" href="#i18n">'._t('P_I18N_PREF_TITLE').'</a></li>';
  13. }
  14. // affichage des options de recherche et du formulaire
  15. function i18n_plugin_AddForm(){
  16. $test = array();
  17. $configuration = new Configuration();
  18. $configuration->getAll();
  19. echo '<section id="i18n" name="i18n" class="i18n">
  20. <h2>'._t('P_I18N_PREF_TITLE').'</h2>';
  21. /* -------------------------------------------------------- */
  22. // Gestion des retours des formulaires
  23. /* -------------------------------------------------------- */
  24. // cas de changement de la langue de Leed
  25. if(isset($_POST['plugin_i18n_changeLngLeed'])){
  26. $langue = substr(basename($_POST['plugin_i18n_changeLngLeed']),0,2);
  27. $configuration->put('language',$langue);
  28. $test['info'][]=_t('P_I18N_MSG_CHG_LNG_LEED');
  29. }
  30. // Cas validation de la création d'une langue sur Leed.
  31. $newLanguage = '';
  32. if(isset($_POST['plugin_i18n_newLanguage'])){
  33. $newLanguage = $_POST['plugin_i18n_newLanguage'];
  34. if (is_file($newLanguage)){
  35. $test['Erreur'][]=_t('P_I18N_NEW_LNG_FILE_EXIST').$newLanguage;
  36. } else {
  37. file_put_contents($newLanguage, '');
  38. $test['Info'][]=_t('P_I18N_NEW_LNG_FILE_OK', array($newLanguage));
  39. $_POST['plugin_i18n_selectLanguage']=$newLanguage;
  40. }
  41. }
  42. // Cas d'une copy de fichier
  43. if(isset($_POST['plugin_i18n_copyLanguage'])){
  44. $copyLanguage = $_POST['plugin_i18n_copyLanguage'];
  45. $fileDest = $_POST['plugin_i18n_copyFileDest'];
  46. if (is_file($fileDest)){
  47. $test['Erreur'][]=_t('P_I18N_NEW_LNG_FILE_EXIST').$fileDest;
  48. } else {
  49. if (is_writable(dirname($fileDest))){
  50. copy($copyLanguage, $fileDest);
  51. $test['Info'][]=_t('P_I18N_NEW_LNG_FILE_OK', array($fileDest));
  52. $_POST['plugin_i18n_selectLanguage']=$fileDest;
  53. } else {
  54. $test['Erreur'][]=_t('P_I18N_VERIF_ERR1').' '.$fileDest;
  55. }
  56. }
  57. }
  58. // Cas validation d'une MAJ d'un fichier de langue
  59. if(isset($_POST['0123456789MAJLanguage'])){
  60. $_ = array_map('addslashes',array_merge($_GET, $_POST));
  61. ksort($_);
  62. $ModifLanguage = $_['0123456789MAJLanguage'];
  63. unset($_['0123456789MAJLanguage']);
  64. if(is_writable($ModifLanguage)){
  65. file_put_contents($ModifLanguage, plugin_i18n_json_encode($_));
  66. $test['Info'][]=_t('P_I18N_UPD_LNG_FILE_OK', array($_POST['0123456789MAJLanguage']));
  67. $_POST['plugin_i18n_selectLanguage']=$ModifLanguage;
  68. } else {
  69. $test['Erreur'][]=_t('P_I18N_UPD_LNG_FILE_ERR', array($_POST['0123456789MAJLanguage']));
  70. }
  71. }
  72. // Gestion des erreurs PHP possible permettant l'écriture de fichier dans les répertoires de Leed
  73. if(!is_writable('./locale/')){
  74. $test['Erreur'][]=_t('P_I18N_VERIF_ERR1');
  75. }
  76. if (!@function_exists('file_get_contents')){
  77. $test['Erreur'][] = _t('P_I18N_VERIF_ERR2');
  78. }
  79. if (!@function_exists('file_put_contents')){
  80. $test['Erreur'][] = _t('P_I18N_VERIF_ERR2');
  81. }
  82. if (@version_compare(PHP_VERSION, '5.1.0') <= 0){
  83. $test['Erreur'][] = _t('P_I18N_VERIF_ERR3', array(PHP_VERSION));
  84. }
  85. if(ini_get('safe_mode') && ini_get('max_execution_time')!=0){
  86. $test['Erreur'][] = _t('P_I18N_VERIF_ERR4');
  87. }
  88. if (count($test)!=0){
  89. echo '<div id="result_i18n" class="result_i18n">
  90. <table>
  91. <th class="i18n_border i18n_th">'._t('P_I18N_MESSAGES').'</th>';
  92. foreach($test as $type=>$messages){
  93. echo '<tr>';
  94. foreach($messages as $message){
  95. echo '<td class="i18n_border '.($type=='Erreur'?'i18n_warn':'i18n_info').'">'.$message.'</td>';
  96. }
  97. echo '</tr>';
  98. }
  99. echo ' </table>
  100. </div>';
  101. }
  102. // Sélectionner la langue ou saisir une nouvelle langue
  103. echo '<h3>'._t('P_I18N_MANAGE_LNG_TITLE').'</h3>';
  104. echo '<form action="settings.php#i18n" method="POST">
  105. <select name="plugin_i18n_changeLngLeed">';
  106. $filesLeed = glob('./templates/'.$configuration->get('theme').'/locale/*.json');
  107. foreach($filesLeed as $file){
  108. if ($file=='./templates/'.$configuration->get('theme').'/locale/'.$configuration->get('language').'.json')
  109. {
  110. echo '<option selected=selected value="'.$file.'">'.$file.'</option>';
  111. } else {
  112. echo '<option value="'.$file.'">'.$file.'</option>';
  113. }
  114. }
  115. echo' </select>
  116. <input type="submit" name="plugin_i18n_chgLngLeed" value="'._t('P_I18N_BTN_CHG_LNG_LEED').'" class="button">
  117. </form>
  118. <h3>'._t('P_I18N_CREA_FIC_LNG').'</h3>
  119. <form action="settings.php#i18n" method="POST">
  120. <input type="text" value="" placeholder="./templates/theme/locale/xx.json" name="plugin_i18n_newLanguage">
  121. <input type="submit" name="plugin_i18n_saveButton" value="'._t('P_I18N_BTN_CREATE_FILE').'" class="button">
  122. </form>
  123. <h3>'._t('P_I18N_MANAGE_TEMPLATE_LNG').'</h3>
  124. <form action="settings.php#i18n" method="POST">
  125. <select name="plugin_i18n_copyLanguage">';
  126. $filesLeed = glob('./templates/*/locale/*.json');
  127. foreach($filesLeed as $file){
  128. if (isset($_POST['plugin_i18n_selectLanguage']) && $_POST['plugin_i18n_selectLanguage']==$file)
  129. {
  130. echo '<option selected=selected value="'.$file.'">'.$file.'</option>';
  131. } else {
  132. echo '<option value="'.$file.'">'.$file.'</option>';
  133. }
  134. }
  135. echo ' </select> '._t('P_I18N_COPY_TO').'
  136. <input type="text" value="" placeholder="./templates/theme/locale/xx.json" name="plugin_i18n_copyFileDest">
  137. <input type="submit" value="'._t('P_I18N_BTN_COPY_FILE').'" class="button">
  138. </form>
  139. <form action="settings.php#i18n" method="POST">
  140. <select name="plugin_i18n_selectLanguage">';
  141. foreach($filesLeed as $file){
  142. if (isset($_POST['plugin_i18n_selectLanguage']) && $_POST['plugin_i18n_selectLanguage']==$file)
  143. {
  144. echo '<option selected=selected value="'.$file.'">'.$file.'</option>';
  145. } else {
  146. echo '<option value="'.$file.'">'.$file.'</option>';
  147. }
  148. }
  149. echo ' </select>
  150. <input type="submit" value="'._t('P_I18N_BTN_LOAD_FILE').'" class="button">
  151. </form>';
  152. echo '<h3>'._t('P_I18N_MANAGE_PLUGIN_LNG').'</h3>
  153. <form action="settings.php#i18n" method="POST">
  154. <select name="plugin_i18n_copyLanguage">';
  155. $filesLeed = glob('./plugins/*/locale/*.json');
  156. foreach($filesLeed as $file){
  157. if (isset($_POST['plugin_i18n_selectLanguage']) && $_POST['plugin_i18n_selectLanguage']==$file)
  158. {
  159. echo '<option selected=selected value="'.$file.'">'.$file.'</option>';
  160. } else {
  161. echo '<option value="'.$file.'">'.$file.'</option>';
  162. }
  163. }
  164. echo ' </select> '._t('P_I18N_COPY_TO').'
  165. <input type="text" value="" placeholder="./plugins/plug/locale/xx.json" name="plugin_i18n_copyFileDest">
  166. <input type="submit" value="'._t('P_I18N_BTN_COPY_FILE').'" class="button">
  167. </form>
  168. <form action="settings.php#i18n" method="POST">
  169. <select name="plugin_i18n_selectLanguage">';
  170. foreach($filesLeed as $file){
  171. if (isset($_POST['plugin_i18n_selectLanguage']) && $_POST['plugin_i18n_selectLanguage']==$file)
  172. {
  173. echo '<option selected=selected value="'.$file.'">'.$file.'</option>';
  174. } else {
  175. echo '<option value="'.$file.'">'.$file.'</option>';
  176. }
  177. }
  178. echo ' </select>
  179. <input type="submit" value="'._t('P_I18N_BTN_LOAD_FILE').'" class="button">
  180. </form>';
  181. echo '<h3>Gestion des fichiers de langues de Leed</h3>
  182. <form action="settings.php#i18n" method="POST">
  183. <select name="plugin_i18n_copyLanguage">';
  184. $filesLeed = glob('./locale/*.json');
  185. foreach($filesLeed as $file){
  186. if (isset($_POST['plugin_i18n_selectLanguage']) && $_POST['plugin_i18n_selectLanguage']==$file)
  187. {
  188. echo '<option selected=selected value="'.$file.'">'.$file.'</option>';
  189. } else {
  190. echo '<option value="'.$file.'">'.$file.'</option>';
  191. }
  192. }
  193. echo ' </select> '._t('P_I18N_COPY_TO').'
  194. <input type="text" value="" placeholder="ex: ./locale/xx.json" name="plugin_i18n_copyFileDest">
  195. <input type="submit" value="'._t('P_I18N_BTN_COPY_FILE').'" class="button">
  196. </form>
  197. <form action="settings.php#i18n" method="POST">
  198. <select name="plugin_i18n_selectLanguage">';
  199. foreach($filesLeed as $file){
  200. if (isset($_POST['plugin_i18n_selectLanguage']) && $_POST['plugin_i18n_selectLanguage']==$file)
  201. {
  202. echo '<option selected=selected value="'.$file.'">'.$file.'</option>';
  203. } else {
  204. echo '<option value="'.$file.'">'.$file.'</option>';
  205. }
  206. }
  207. echo ' </select>
  208. <input type="submit" value="'._t('P_I18N_BTN_LOAD_FILE').'" class="button">
  209. </form>';
  210. // sélection d'un langage à charger
  211. if (isset($_POST['plugin_i18n_selectLanguage'])){
  212. $selectLanguage = $_POST['plugin_i18n_selectLanguage'];
  213. echo '<hr><h3>'._t('P_I18N_UPD_FILE_TITLE', array($selectLanguage)).'</h3>
  214. <span>'._t('P_I18N_MSG_ALERT').'</span>';
  215. // On scan tous les tags de Leed
  216. $foundTags = array();
  217. if (stripos(dirname($selectLanguage),'./') === false){
  218. $foundTags = plugin_i18n_scanTags(dirname($selectLanguage).'/../', 'plugins');
  219. } else {
  220. // on est dans le répertoire ./locale/fr.json
  221. $foundTags = plugin_i18n_scanTags('./', 'plugins', 'templates');
  222. }
  223. // On charge le fichier de langue existant
  224. $currentLanguage = json_decode(file_get_contents($selectLanguage),true);
  225. ksort($currentLanguage);
  226. echo '<hr><h4>'._t('P_I18N_KEY_INFILE_TITLE').'</h4>
  227. <form action="settings.php#i18n" method="POST">
  228. <input type="hidden" name="0123456789MAJLanguage" value="'.$selectLanguage.'">
  229. <table class="diffTab">
  230. <tr>
  231. <th class="i18n_border i18n_th">'._t('P_I18N_KEY_FILE_NB_KEY',array(count($currentLanguage))).'</th>
  232. <th class="i18n_border i18n_th">'._t('P_I18N_KEY_CODE_NB_KEY',array(count($foundTags))).'</th>
  233. </tr>';
  234. $language = substr(basename($_POST['plugin_i18n_selectLanguage']),0,2);
  235. foreach($currentLanguage as $key=>$value){
  236. if(in_array($key, $foundTags, true)){
  237. echo ' <tr>
  238. <td class="i18n_border i18n_textcenter">'.$key.'</td>
  239. <td class="i18n_border i18n_textcenter">';
  240. $value = htmlentities($value,ENT_COMPAT,'UTF-8');
  241. if(strlen($value)>100){
  242. echo '<textarea name="'.$key.'">'.$value.'</textarea>';
  243. }else{
  244. echo '<input type="text" name="'.$key.'" value="'.$value.'">';
  245. }
  246. echo ' </td>
  247. <td class="i18n_a">&nbsp;<a style="color:#fff" target="_blank" href="http://translate.google.fr/#auto/'.$language.'/'.$value.'" title="'._t('P_I18N_TRANSLATE').'">T</a></td>
  248. </tr>';
  249. }
  250. }
  251. echo '</table>';
  252. echo '<hr><h4>'._t('P_I18N_KEY_INFILE_NOTFND_TITLE').'</h4>
  253. <table class="diffTab">
  254. <tr>
  255. <th class="i18n_border i18n_th">'._t('P_I18N_KEY_FILE_NB_KEY',array(count($currentLanguage))).'</th>
  256. <th class="i18n_border i18n_th">'._t('P_I18N_KEY_CODE_NB_KEY',array(count($foundTags))).'</th>
  257. </tr>';
  258. // recherche des tags existant mais non trouvé dans la recherche du code
  259. foreach ($currentLanguage as $key => $value) {
  260. if(!in_array($key, $foundTags, true)){
  261. echo '<tr><td class="i18n_border i18n_textcenter">'.$key.'</td>
  262. <td class="i18n_border i18n_textcenter">'.$value.'<br />'._t('P_I18N_MSG_NOT_FND_CODE').'</td>
  263. </tr>';
  264. }
  265. }
  266. // Recherche des tags existants dans le code mais non trouvé dans la traduction
  267. foreach ($foundTags as $key => $value) {
  268. if(!isset($currentLanguage[$value])){
  269. echo '<tr><td class="i18n_border i18n_textcenter">'.$value.'</td>
  270. <td class="i18n_border i18n_textcenter"><input type="text" name="'.$value.'" value="">+</td></tr>';
  271. }
  272. }
  273. echo '</table>
  274. <input type="submit" value="'._t('P_I18N_BTN_UPD_FILE').'" class="button">
  275. </form>';
  276. }
  277. echo '</section>';
  278. }
  279. // scanner les tags de traduction dans Leed
  280. function plugin_i18n_scanTags($dir, $exclude='', $exclude2=''){
  281. $return = array();
  282. $extensions = array('html','php','js');
  283. $leedFiles = scandir($dir);
  284. //var_dump($leedFiles);
  285. foreach($leedFiles as $file){
  286. if($file!='.' && $file!='..' && $file!='.git' && $file!=$exclude && $file!=$exclude2){
  287. if(is_dir($dir.$file)){
  288. $return = array_merge($return,plugin_i18n_scanTags($dir.$file.'/', $exclude, $exclude2));
  289. }else{
  290. $ext = str_replace('.rtpl.php','.wrongphp',$file);
  291. $ext = strtolower(substr($ext,strrpos($ext,'.')+1));
  292. if(in_array($ext, $extensions)){
  293. $content = file_get_contents($dir.$file);
  294. if(preg_match_all("#_t\(([\'\\\"])([a-zA-Z0-9\_ \?\-]+)([\'\\\"])(?:,?.?)\)?#", $content, $match)){
  295. //var_dump($dir.$file.'-->',$match[2]);
  296. $return = array_merge($return,$match[2]);
  297. }
  298. }
  299. }
  300. }
  301. }
  302. $return = array_unique($return);
  303. return $return;
  304. }
  305. function plugin_i18n_json_encode($json) {
  306. array_map('html_entity_decode',$json);
  307. ksort($json);
  308. array_walk_recursive($json,
  309. function (&$item, $key) {
  310. if (is_string($item)) $item = mb_encode_numericentity($item, array (0x80, 0xffff, 0, 0xffff), 'UTF-8');
  311. });
  312. $json = mb_decode_numericentity(json_encode($json), array (0x80, 0xffff, 0, 0xffff), 'UTF-8');
  313. $json = stripslashes(stripslashes($json));
  314. $result = '';
  315. $pos = 0;
  316. $strLen = strlen($json);
  317. $indentStr = ' ';
  318. $newLine = "\n";
  319. $prevChar = '';
  320. $outOfQuotes = true;
  321. for ($i=0; $i<=$strLen; $i++) {
  322. $char = substr($json, $i, 1);
  323. if ($char == '"' && $prevChar != '\\') {
  324. $outOfQuotes = !$outOfQuotes;
  325. } else if (($char == '}' || $char == ']') && $outOfQuotes) {
  326. $result .= $newLine;
  327. $pos --;
  328. for ($j=0; $j<$pos; $j++) {
  329. $result .= $indentStr;
  330. }
  331. }
  332. $result .= $char;
  333. if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
  334. $result .= $newLine;
  335. if ($char == '{' || $char == '[') {
  336. $pos ++;
  337. }
  338. for ($j = 0; $j < $pos; $j++) {
  339. $result .= $indentStr;
  340. }
  341. }
  342. $prevChar = $char;
  343. }
  344. return $result;
  345. }
  346. // Ajout de la fonction au Hook situé avant l'affichage des évenements
  347. $myUser = (isset($_SESSION['currentUser'])?unserialize($_SESSION['currentUser']):false);
  348. if($myUser!=false) {
  349. Plugin::addHook("setting_post_link", "i18n_plugin_AddLink");
  350. Plugin::addHook("setting_post_section", "i18n_plugin_AddForm");
  351. }
  352. ?>