PageRenderTime 59ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 1ms

/wolf/app/controllers/TranslateController.php

http://wolfcms.googlecode.com/
PHP | 203 lines | 143 code | 29 blank | 31 comment | 39 complexity | 79599907ae19735e22a9e9aa2775e5a7 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. /*
  3. * Wolf CMS - Content Management Simplified. <http://www.wolfcms.org>
  4. * Copyright (C) 2008,2009,2010 Martijn van der Kleijn <martijn.niji@gmail.com>
  5. *
  6. * This file is part of Wolf CMS. Wolf CMS is licensed under the GNU GPLv3 license.
  7. * Please see license.txt for the full license text.
  8. */
  9. /**
  10. * @package wolf
  11. * @subpackage controllers
  12. *
  13. * @author Martijn van der Kleijn <martijn.niji@gmail.com>
  14. * @copyright Martijn van der Kleijn, 2008-2010
  15. * @license http://www.gnu.org/licenses/gpl.html GPL License
  16. *
  17. * @version $Id: TranslateController.php 311 2010-11-18 21:30:29Z martijn.niji@gmail.com $
  18. */
  19. /**
  20. * Class TranslateController
  21. *
  22. * This controller allows users to generate a template for a translation file.
  23. *
  24. * @package wolf
  25. * @subpackage controllers
  26. *
  27. * @since 0.9.4
  28. */
  29. final class TranslateController extends Controller {
  30. public final function __construct() {
  31. AuthUser::load();
  32. if ( ! AuthUser::isLoggedIn()) {
  33. redirect(get_url('login'));
  34. }
  35. $this->assignToLayout('sidebar', new View('translate/sidebar'));
  36. }
  37. public final function index() {
  38. $this->setLayout('backend');
  39. $this->display('translate/index');
  40. }
  41. public final function core() {
  42. $complete = array();
  43. $basedir = CMS_ROOT.'';
  44. $dirs = $this->listdir($basedir);
  45. foreach ($dirs as $id => $path) {
  46. $tmp = array();
  47. $strings = array();
  48. $fsize = filesize($path);
  49. if ($fsize > 0) {
  50. $fh = fopen($path, 'r');
  51. $data = fread($fh, $fsize);
  52. fclose($fh);
  53. if (strpos($data, '__(\'')) {
  54. $data = substr($data, strpos($data, '__(\'')+4);
  55. $tmp = explode('__(\'', $data);
  56. foreach ($tmp as $string) {
  57. $endpos = strpos($string, '\'');
  58. while (substr($string, $endpos-1, 1) == "\\") {
  59. $endpos = $endpos + strpos(substr($string, $endpos+1, strpos($string, '\'')), '\'') + 1;
  60. }
  61. $strings[] = substr($string, 0, $endpos);
  62. }
  63. if (sizeof($strings) > 0) {
  64. $complete = array_merge($complete, $strings);
  65. }
  66. }
  67. if (strpos($data, '__("')) {
  68. $data = substr($data, strpos($data, '__("')+4);
  69. $tmp = explode('__("', $data);
  70. foreach ($tmp as $string) {
  71. $endpos = strpos($string, '"');
  72. while (substr($string, $endpos-1, 1) == "\\") {
  73. $endpos = $endpos + strpos(substr($string, $endpos+1, strpos($string, '"')), '"') + 1;
  74. }
  75. $strings[] = substr($string, 0, $endpos);
  76. }
  77. if (sizeof($strings) > 0) {
  78. $files[$path] = $strings;
  79. }
  80. }
  81. }
  82. }
  83. // These are a few generated strings which the TranslateController cannot pick out.
  84. // So we add them manually for now.
  85. $complete = array_merge($complete, array('Add Page', 'Edit Page', 'Add snippet',
  86. 'Edit snippet', 'Add layout' ,'Edit layout',
  87. 'Add user', 'Edit user'
  88. ));
  89. $this->display('translate/core', array('complete' => $complete));
  90. }
  91. public final function plugins() {
  92. $files = array();
  93. $basedir = PLUGINS_ROOT;
  94. $dirs = $this->listdir($basedir, true);
  95. foreach ($dirs as $id => $path) {
  96. $tmp = array();
  97. $strings = array();
  98. $fsize = filesize($path);
  99. if ($fsize > 0) {
  100. $fh = fopen($path, 'r');
  101. $data = fread($fh, $fsize);
  102. fclose($fh);
  103. if (strpos($data, '__(\'')) {
  104. $data = substr($data, strpos($data, '__(\'')+4);
  105. $tmp = explode('__(\'', $data);
  106. foreach ($tmp as $string) {
  107. $endpos = strpos($string, '\'');
  108. while (substr($string, $endpos-1, 1) == "\\") {
  109. $endpos = $endpos + strpos(substr($string, $endpos+1, strpos($string, '\'')), '\'') + 1;
  110. }
  111. $strings[] = substr($string, 0, $endpos);
  112. }
  113. if (sizeof($strings) > 0) {
  114. $files[$path] = $strings;
  115. }
  116. }
  117. if (strpos($data, '__("')) {
  118. $data = substr($data, strpos($data, '__("')+4);
  119. $tmp = explode('__("', $data);
  120. foreach ($tmp as $string) {
  121. $endpos = strpos($string, '"');
  122. while (substr($string, $endpos-1, 1) == "\\") {
  123. $endpos = $endpos + strpos(substr($string, $endpos+1, strpos($string, '"')), '"') + 1;
  124. }
  125. $strings[] = substr($string, 0, $endpos);
  126. }
  127. if (sizeof($strings) > 0) {
  128. $files[$path] = $strings;
  129. }
  130. }
  131. }
  132. }
  133. $this->display('translate/plugins', array('files' => $files));
  134. }
  135. private final function listdir($start_dir='.', $plugins = false) {
  136. $files = array();
  137. if (is_dir($start_dir)) {
  138. $fh = opendir($start_dir);
  139. while (($file = readdir($fh)) !== false) {
  140. # loop through the files, skipping . and .., and recursing if necessary
  141. if (strcmp($file, '.')==0 || strcmp($file, '..')==0) {
  142. continue;
  143. }
  144. $filepath = $start_dir . '/' . $file;
  145. if ($plugins) {
  146. if ( is_dir($filepath) && !strpos($filepath, 'i18n') ) {
  147. $files = array_merge($files, $this->listdir($filepath, $plugins));
  148. }
  149. else {
  150. if (!strpos($filepath, 'I18n') && strpos($filepath, '.php', strlen($filepath) - 5) || strpos($filepath, '.phtml', strlen($filepath) - 7)) {
  151. array_push($files, $filepath);
  152. }
  153. }
  154. }
  155. else {
  156. if ( is_dir($filepath) && !strpos($filepath, 'i18n') && !strpos($filepath, 'plugins') ) {
  157. $files = array_merge($files, $this->listdir($filepath, $plugins));
  158. }
  159. else {
  160. if (!strpos($filepath, 'I18n') && strpos($filepath, '.php', strlen($filepath) - 5) || strpos($filepath, '.phtml', strlen($filepath) - 7)) {
  161. array_push($files, $filepath);
  162. }
  163. }
  164. }
  165. }
  166. closedir($fh);
  167. }
  168. else {
  169. # false if the function was called with an invalid non-directory argument
  170. $files = false;
  171. }
  172. return $files;
  173. }
  174. }