PageRenderTime 31ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/4.8/administrator/components/com_languages/admin.languages.class.php

http://miacms.googlecode.com/
PHP | 333 lines | 289 code | 31 blank | 13 comment | 32 complexity | ac80a73a986fc5f2545144171588e879 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0, LGPL-2.0
  1. <?php
  2. /**
  3. * @package MiaCMS
  4. * @subpackage Languages
  5. * @author MiaCMS see README.php
  6. * @copyright see README.php
  7. * See COPYRIGHT.php for copyright notices and details.
  8. * @license GNU/GPL Version 2, see LICENSE.php
  9. * MiaCMS is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License
  11. * as published by the Free Software Foundation; version 2 of the License.
  12. */
  13. defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
  14. class Request
  15. {
  16. var $name;
  17. var $vars;
  18. function Request($name)
  19. {
  20. $this->name = $name;
  21. $this->attributes = array();
  22. }
  23. function set($var, $value)
  24. {
  25. $this->vars[$var] = $value;
  26. }
  27. function get($var=null)
  28. {
  29. return is_null($var) ? $this->vars : $this->vars[$var];
  30. }
  31. function addFromRequest($var, $global='request'){
  32. switch (strtolower($global))
  33. {
  34. case 'get':
  35. $this->vars[$var] = mosGetParam($_GET, $var);
  36. break;
  37. case 'post':
  38. $this->vars[$var] = mosGetParam($_POST, $var);
  39. break;
  40. case 'cookie':
  41. $this->vars[$var] = mosGetParam($_COOKIE, $var);
  42. break;
  43. case 'request':
  44. $this->vars[$var] = mosGetParam($_REQUEST, $var);
  45. break;
  46. default:
  47. trigger_error('Invalid Request Array', E_USER_ERROR);
  48. break;
  49. }
  50. }
  51. function setByRef($var, &$value)
  52. {
  53. $this->vars[$var] = &$value;
  54. }
  55. function &getByRef($var)
  56. {
  57. return $this->vars[$var];
  58. }
  59. function &session($reset = false)
  60. {
  61. $name = '__' . $this->name . '_session';
  62. if (!isset($_SESSION[$name]) || $reset) {
  63. $_SESSION[$name] = array();
  64. }
  65. return $_SESSION[$name];
  66. }
  67. function &getInstance($name)
  68. {
  69. static $requests;
  70. if (!isset($requests[$name])) {
  71. $requests[$name] = new Request($name);
  72. }
  73. return $requests[$name];
  74. }
  75. }
  76. class Controller
  77. {
  78. var $name;
  79. var $dir;
  80. var $request;
  81. var $action;
  82. var $renderer;
  83. function Controller($name)
  84. {
  85. $this->name = $name;
  86. $this->request =& Request::getInstance($name);
  87. $this->renderer =& Renderer::getInstance();
  88. $this->renderer->setdir(dirname(__FILE__).DIRECTORY_SEPARATOR.'views'.DIRECTORY_SEPARATOR.'templates');
  89. }
  90. function forward($actionname)
  91. {
  92. if (!$actionname) return $this->view('index');
  93. $actionfile = dirname(__FILE__)."/actions/$actionname.action.php";
  94. $actionclass = $actionname.'Action';
  95. if (file_exists($actionfile)) include($actionfile);
  96. else return trigger_error("Action file '$actionfile' not found.", E_USER_ERROR);
  97. if (class_exists($actionclass)) $action = new $actionclass();
  98. else return trigger_error("Action class '$actionclass' not found.", E_USER_ERROR);
  99. $action->execute($this, $this->request);
  100. }
  101. function view($viewname)
  102. {
  103. $viewfile = dirname(__FILE__)."/views/$viewname.view.php";
  104. $viewclass = $viewname.'View';
  105. if (file_exists($viewfile)) include($viewfile);
  106. #else return trigger_error("View file '$viewfile' not found.", E_USER_ERROR);
  107. if (class_exists($viewclass)) $view = new $viewclass($this);
  108. else return trigger_error("View class '$viewclass' not found.", E_USER_ERROR);
  109. $view->render($this->renderer, $this->request);
  110. }
  111. function redirect($task=null, $act=null)
  112. {
  113. $url = $_SERVER['PHP_SELF'].'?option='.$this->name;
  114. $url .= !is_null($task) ? '&task='.$task : '';
  115. $url .= !is_null($act) ? '&act='.$act : '';
  116. if (headers_sent()) {
  117. echo "<script>document.location.href='$url';</script>";
  118. } else {
  119. #if (ob_get_contents()) while (@ob_end_clean()); // clear output buffer if one exists
  120. header( "Location: $url" );
  121. }
  122. exit;
  123. }
  124. }
  125. class Action
  126. {
  127. var $view;
  128. function Action()
  129. {
  130. }
  131. function execute(&$controller, &$request)
  132. {
  133. return trigger_error('Action::execute() must be overridden.', E_USER_ERROR);
  134. }
  135. function setView($view)
  136. {
  137. $this->viewname = $view;
  138. }
  139. function getView()
  140. {
  141. return $this->view;
  142. }
  143. }
  144. class View
  145. {
  146. var $controller;
  147. function View(&$controller){
  148. $this->controller = $controller;
  149. }
  150. function render(&$request, &$renderer)
  151. {
  152. return trigger_error('View::render() must be overridden');
  153. }
  154. }
  155. class Renderer
  156. {
  157. var $dir;
  158. var $vars = array();
  159. var $engine = 'php';
  160. var $template = '';
  161. var $debug = 0;
  162. function Renderer(){}
  163. function &getInstance($type = 'php') {
  164. static $renderer;
  165. if (is_null($renderer[$type])) {
  166. if ($type == 'php') {
  167. $renderer[$type] = new Renderer();
  168. } else {
  169. $classname = $type . 'Renderer';
  170. if (class_exists($classname))
  171. $renderer[$type] = new $classname();
  172. }
  173. }
  174. return $renderer[$type];
  175. }
  176. function display($template, $return = false){
  177. if ($template == NULL){
  178. return trigger_error('A template has not been specified', E_USER_ERROR);
  179. }
  180. $this->template = $this->dir . $template;
  181. if ($this->debug) echo nl2br($this->template."\n");
  182. if (is_readable($this->template)) {
  183. extract($this->getvars());
  184. if ($return) {
  185. ob_start();
  186. include_once($this->template);
  187. $ret = ob_get_contents();
  188. ob_end_clean();
  189. return $ret;
  190. } else {
  191. include_once($this->template);
  192. }
  193. } else {
  194. return trigger_error("Template file $template does not exist or is not readable", E_USER_ERROR);
  195. }
  196. return false;
  197. }
  198. function fetch($template){
  199. return $this->display($template, true);
  200. }
  201. function &getengine(){
  202. return $this->engine;
  203. }
  204. function addvar($key, $value){
  205. $this->vars[$key] = $value;
  206. }
  207. function addbyref ($key, &$value) {
  208. $this->vars[$key] = $value;
  209. }
  210. function getvars($name = false){
  211. return (isset($this->vars[$name])) ? $this->vars[$name] : $this->vars;
  212. }
  213. function setdir($dir){
  214. $this->dir = (substr($dir, -1) == DIRECTORY_SEPARATOR) ? $dir : $dir.DIRECTORY_SEPARATOR;
  215. }
  216. function getdir(){
  217. return $this->dir;
  218. }
  219. function settemplate($template){
  220. $this->template = $template;
  221. }
  222. }
  223. class XMLUtils
  224. {
  225. function parse_into_array($xml) {
  226. $p = xml_parser_create();
  227. xml_parser_set_option($p, XML_OPTION_CASE_FOLDING, 0);
  228. xml_parser_set_option($p, XML_OPTION_SKIP_WHITE, 1);
  229. xml_parse_into_struct($p, $xml, $values);
  230. xml_parser_free($p);
  231. $current = $prev = array();
  232. $xmlarray =& $current;
  233. foreach($values as $key => $value) {
  234. $index = count($xmlarray);
  235. switch ($value['type'])
  236. {
  237. case 'open':
  238. $xmlarray[$index] = array();
  239. $xmlarray[$index]['tag'] = isset($value["tag"]) ? $value["tag"] : null;
  240. $xmlarray[$index]['value'] = isset($value["value"]) ? $value["value"] : null;
  241. $xmlarray[$index]['attributes'] = isset($value["attributes"]) ? $value["attributes"] : null;
  242. $xmlarray[$index]['nodes'] = array();
  243. $prev[count($prev)] = &$xmlarray;
  244. $xmlarray = &$xmlarray[$index]['nodes'];
  245. break;
  246. case 'complete':
  247. $xmlarray[$index] = array();
  248. $xmlarray[$index]['tag'] = isset($value["tag"]) ? $value["tag"] : null;
  249. $xmlarray[$index]['value'] = isset($value["value"]) ? $value["value"] : null;
  250. $xmlarray[$index]['attributes'] = isset($value["attributes"]) ? $value["attributes"] : null;
  251. break;
  252. case 'close':
  253. $xmlarray = &$prev[count($prev) - 1];
  254. unset($prev[count($prev) - 1]);
  255. break;
  256. }
  257. }
  258. return $xmlarray;
  259. }
  260. function parse_file_into_array($file) {
  261. return XMLUtils::parse_into_array(file_get_contents($file));
  262. }
  263. function array_to_xml($array, $encoding='utf-8') {
  264. $xml = "<?xml version=\"1.0\" encoding=\"$encoding\"?>\n";
  265. if ((!empty($array)) AND (is_array($array))) {
  266. foreach ($array as $key => $value) {
  267. switch ($value["type"]) {
  268. case "open":
  269. $xml .= str_repeat("\t", $value["level"] - 1);
  270. $xml .= "<" . strtolower($value["tag"]);
  271. if (isset($value["attributes"])) {
  272. foreach ($value["attributes"] as $k => $v) {
  273. $xml .= sprintf(' %s="%s"', strtolower($k), $v);
  274. }
  275. }
  276. $xml .= ">\n";
  277. break;
  278. case "complete":
  279. $xml .= str_repeat("\t", $value["level"] - 1);
  280. $xml .= "<" . strtolower($value["tag"]);
  281. if (isset($value["attributes"])) {
  282. foreach ($value["attributes"] as $k => $v) {
  283. $xml .= sprintf(' %s="%s"', strtolower($k), $v);
  284. }
  285. }
  286. $xml .= ">";
  287. $xml .= isset($value['value']) ? $value['value'] : false;
  288. $xml .= "</".strtolower($value["tag"]).">\n";
  289. break;
  290. case "close":
  291. $xml .= str_repeat("\t", $value["level"] - 1);
  292. $xml .= "</" . strtolower($value["tag"]) . ">\n";
  293. break;
  294. default:
  295. break;
  296. }
  297. }
  298. }
  299. return $xml;
  300. }
  301. }
  302. ?>