PageRenderTime 37ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/public/js/tiny_mce/plugins/imagemanager/classes/Utils/LanguagePack.php

https://bitbucket.org/cidious/raise.org
PHP | 305 lines | 224 code | 62 blank | 19 comment | 29 complexity | 243dd09ec78a0f57b80cbbf84ffc91f5 MD5 | raw file
  1. <?php
  2. /**
  3. * $Id: LanguagePack.php 10 2007-05-27 10:55:12Z spocke $
  4. *
  5. * @package Moxiecode.utils
  6. * @author Moxiecode
  7. * @copyright Copyright � 2007, Moxiecode Systems AB, All rights reserved.
  8. */
  9. /**
  10. * This class handles XML language packs.
  11. *
  12. * @package MCFileManager.utils
  13. */
  14. class Moxiecode_LanguagePack {
  15. var $_items;
  16. var $_parser;
  17. var $_currentTarget;
  18. var $_currentLanguage;
  19. var $_currentDir;
  20. var $_encoding;
  21. var $_header;
  22. var $_tagContent;
  23. var $_tag;
  24. var $_tagAttrs;
  25. var $_cdataArr;
  26. function Moxiecode_LanguagePack() {
  27. $this->_header = array();
  28. $this->_items = array();
  29. $this->_cdataArr = array();
  30. $this->_currentTarget = "";
  31. $this->_currentLanguage = "en";
  32. $this->_currentDir = "ltr";
  33. $this->_header["major"] = 0;
  34. $this->_header["minor"] = 1;
  35. $this->_header["releasedate"] = date("Y-m-d");
  36. }
  37. function setLanguage($language) {
  38. $this->_currentLanguage = $language;
  39. }
  40. function getLanguage() {
  41. return $this->_currentLanguage;
  42. }
  43. /* Loads XML string */
  44. function loadXML($data) {
  45. // Check encoding on data
  46. preg_match('/<?xml.*encoding=[\'"](.*?)[\'"].*?>/m', $data, $matches);
  47. $encoding = "UTF-8";
  48. // Found XML encoding
  49. if (count($matches) > 1)
  50. $encoding = strtoupper($matches[1]);
  51. $this->_encoding = $encoding;
  52. // OMG! PHP Xpath is crappy, cant detect CDATA, haxx!
  53. $data = preg_replace("/<\!\[CDATA\[/", "<![CDATA[#CDATA#", $data);
  54. $this->_parser = xml_parser_create($encoding); // Auto detect for PHP4/PHP5
  55. xml_set_object($this->_parser, $this);
  56. xml_set_element_handler($this->_parser, "_saxStartElement", "_saxEndElement");
  57. xml_set_character_data_handler($this->_parser, "_saxCharacterData");
  58. xml_parser_set_option($this->_parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
  59. if (!xml_parse($this->_parser, $data, true))
  60. trigger_error(sprintf("Language pack loading failed, XML error: %s at line %d.", xml_error_string(xml_get_error_code($this->_parser)), xml_get_current_line_number($this->_parser)), E_USER_ERROR);
  61. xml_parser_free($this->_parser);
  62. }
  63. /* Loads XML file */
  64. function load($file) {
  65. if (($fp = @fopen($file, "r"))) {
  66. $data = '';
  67. while (!feof($fp))
  68. $data .= fread($fp, 8192);
  69. fclose($fp);
  70. if (ini_get("magic_quotes_gpc"))
  71. $data = stripslashes($data);
  72. $this->loadXML($data);
  73. } else
  74. trigger_error("Could not open XML: ". $file, E_USER_ERROR);
  75. }
  76. function save($file, $enc="UTF-8") {
  77. if (($fp = @fopen($file, "w"))) {
  78. fwrite($fp, $this->toString($enc));
  79. fclose($fp);
  80. } else
  81. trigger_error("Could not open XML for writing: ". $file, E_USER_ERROR);
  82. }
  83. function getGroups() {
  84. return $this->_items[$this->_currentLanguage]["data"];
  85. }
  86. function setGroups($groups) {
  87. $this->_items[$this->_currentLanguage]["data"] = $groups;
  88. }
  89. function getGroup($name) {
  90. return $this->_items[$this->_currentLanguage]["data"][$name];
  91. }
  92. function get($target, $name) {
  93. return isset($this->_items[$this->_currentLanguage]["data"][$target][$name]) ? $this->_items[$this->_currentLanguage]["data"][$target][$name] : ("$" . $name . "$");
  94. }
  95. function set($target, $name, $value, $cdata = 3) {
  96. if ($cdata != 3) {
  97. if (!isset($this->_cdataArr[$this->_currentLanguage]))
  98. $this->_cdataArr[$this->_currentLanguage] = array();
  99. if (!isset($this->_cdataArr[$this->_currentLanguage][$target]))
  100. $this->_cdataArr[$this->_currentLanguage][$target] = array();
  101. $this->_cdataArr[$this->_currentLanguage][$target][$name] = $cdata;
  102. }
  103. if (!isset($this->_items[$this->_currentLanguage]["data"][$target]))
  104. $this->_items[$this->_currentLanguage]["data"][$target] = array();
  105. $this->_items[$this->_currentLanguage]["data"][$target][$name] = $value;
  106. }
  107. function getLanguageTitle() {
  108. return $this->_items[$this->_currentLanguage]["title"];
  109. }
  110. function setAuthor($author) {
  111. $this->_header["author"] = $author;
  112. }
  113. function getAuthor() {
  114. return $this->_header["author"];
  115. }
  116. function setVersion($major, $minor, $releasedate) {
  117. $this->_header["major"] = $major;
  118. $this->_header["minor"] = $minor;
  119. $this->_header["releasedate"] = $releasedate;
  120. }
  121. function getMinor() {
  122. return $this->_header["minor"];
  123. }
  124. function getMajor() {
  125. return $this->_header["major"];
  126. }
  127. function getReleaseDate() {
  128. return $this->_header["releasedate"];
  129. }
  130. function getDescription() {
  131. return $this->_header["description"];
  132. }
  133. function setDescription($description) {
  134. $this->_header["description"] = $description;
  135. }
  136. function createLanguage($lang, $dir, $title) {
  137. $this->_currentLanguage = $lang;
  138. $this->_items[$lang] = array("dir" => $dir, "title" => $title, "data" => array());
  139. }
  140. function updateLanguage($target, $lang, $dir, $title) {
  141. $this->_currentLanguage = $lang;
  142. $this->_items[$lang] = $this->_items[$target];
  143. unset($this->_items[$target]);
  144. $this->_items[$lang]["dir"] = $dir;
  145. $this->_items[$lang]["title"] = $title;
  146. $this->_cdataArr[$lang] = $this->_cdataArr[$target];
  147. unset($this->_cdataArr[$target]);
  148. }
  149. // * * Private methods
  150. function _saxStartElement($parser, $name, $attrs) {
  151. $this->_tag = $name;
  152. $this->_tagAttrs = $attrs;
  153. $this->_tagContent = "";
  154. switch($name) {
  155. case "LANGUAGE":
  156. $this->_currentLanguage = $this->_tagAttrs["CODE"];
  157. $this->_currentDir = $this->_tagAttrs["DIR"];
  158. $this->_items[$this->_currentLanguage] = array("dir" => $this->_currentDir, "title" => $this->_tagAttrs["TITLE"], "data" => array());
  159. break;
  160. case "GROUP":
  161. $this->_currentTarget = $this->_tagAttrs["TARGET"];
  162. $this->_items[$this->_currentLanguage]["data"][$this->_tagAttrs["TARGET"]] = array();
  163. break;
  164. }
  165. }
  166. function _saxEndElement($parser, $name) {
  167. preg_match('/^#CDATA#/', $this->_tagContent, $matches);
  168. $this->_tagContent = preg_replace("/^#CDATA#/", "", $this->_tagContent);
  169. if (count($matches) == 0)
  170. $this->_tagContent = trim($this->_tagContent);
  171. switch($name) {
  172. case "ITEM":
  173. if (count($matches) != 0) {
  174. if (!isset($this->_cdataArr[$this->_currentLanguage]))
  175. $this->_cdataArr[$this->_currentLanguage] = array();
  176. if (!isset($this->_cdataArr[$this->_currentLanguage][$this->_currentTarget]))
  177. $this->_cdataArr[$this->_currentLanguage][$this->_currentTarget] = array();
  178. $this->_cdataArr[$this->_currentLanguage][$this->_currentTarget][$this->_tagAttrs["NAME"]] = true;
  179. }
  180. $this->_items[$this->_currentLanguage]["data"][$this->_currentTarget][$this->_tagAttrs["NAME"]] = $this->_tagContent;
  181. break;
  182. case "AUTHOR":
  183. $this->_header["author"] = $this->_tagContent;
  184. break;
  185. case "VERSION":
  186. $this->_header["minor"] = trim($this->_tagAttrs["MINOR"]);
  187. $this->_header["major"] = trim($this->_tagAttrs["MAJOR"]);
  188. $this->_header["releasedate"] = trim($this->_tagAttrs["RELEASEDATE"]);
  189. break;
  190. case "DESCRIPTION":
  191. $this->_header["description"] = $this->_tagContent;
  192. break;
  193. }
  194. // Clear memory!
  195. $this->_tagContent = "";
  196. $this->_tag = "";
  197. $this->_tagAttrs = "";
  198. }
  199. function _saxCharacterData($parser, $data) {
  200. $this->_tagContent .= $data;
  201. }
  202. function xmlEncode($str) {
  203. if (strtolower($this->_encoding) == "utf-8")
  204. return utf8_encode(htmlspecialchars($str, ENT_QUOTES, $this->_encoding));
  205. return htmlspecialchars($str, ENT_QUOTES, $this->_encoding);
  206. }
  207. function toString($enc = "") {
  208. $oldenc = "";
  209. if ($enc == "")
  210. $enc = $this->_encoding;
  211. else {
  212. $oldenc = $this->_encoding;
  213. $this->_encoding = $enc;
  214. }
  215. $doc = "";
  216. $doc .= '<?xml version="1.0" encoding="'. $enc .'"?>'. "\n";
  217. $doc .= '<language-pack>'. "\n";
  218. $doc .= ' <header>'. "\n";
  219. $doc .= ' <author>'. $this->xmlEncode($this->_header["author"]) .'</author>'. "\n";
  220. $doc .= ' <version major="'. $this->xmlEncode($this->_header["major"]) .'" minor="'. $this->xmlEncode($this->_header["minor"]) .'" releasedate="'. $this->xmlEncode($this->_header["releasedate"]) .'" />'. "\n";
  221. $doc .= ' <description>'. $this->xmlEncode($this->_header["description"]) .'</description>'. "\n";
  222. $doc .= ' </header>'. "\n";
  223. foreach($this->_items as $code => $language) {
  224. $doc .= ' <language code="'. $this->xmlEncode($code) .'" dir="'. $this->xmlEncode($language["dir"]) .'" title="'. $this->xmlEncode($language["title"]) .'">'. "\n";
  225. foreach($language["data"] as $target => $group) {
  226. $doc .= ' <group target="'. $this->xmlEncode($target) .'">'. "\n";
  227. foreach($group as $name => $item) {
  228. if (isset($this->_cdataArr[$code]) && isset($this->_cdataArr[$code][$target]) && isset($this->_cdataArr[$code][$target][$name]) && $this->_cdataArr[$code][$target][$name]) {
  229. $doc .= ' <item name="'. $this->xmlEncode($name) .'"><![CDATA['. $item .']]></item>'. "\n";
  230. } else
  231. $doc .= ' <item name="'. $this->xmlEncode($name) .'">'. $this->xmlEncode($item) .'</item>'. "\n";
  232. }
  233. $doc .= ' </group>'. "\n";
  234. }
  235. $doc .= ' </language>'. "\n";
  236. }
  237. $doc .= '</language-pack>'. "\n";
  238. if ($oldenc != "")
  239. $this->_encoding = $oldenc;
  240. return $doc;
  241. }
  242. }
  243. ?>