PageRenderTime 50ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/ajaxplorer/server/classes/class.AJXP_XMLWriter.php

https://github.com/umbecr/camilaframework
PHP | 334 lines | 265 code | 30 blank | 39 comment | 39 complexity | 89217b5a90ee8ad498be771cbd17a35c MD5 | raw file
  1. <?php
  2. /**
  3. * @package info.ajaxplorer
  4. *
  5. * Copyright 2007-2009 Charles du Jeu
  6. * This file is part of AjaXplorer.
  7. * The latest code can be found at http://www.ajaxplorer.info/
  8. *
  9. * This program is published under the LGPL Gnu Lesser General Public License.
  10. * You should have received a copy of the license along with AjaXplorer.
  11. *
  12. * The main conditions are as follow :
  13. * You must conspicuously and appropriately publish on each copy distributed
  14. * an appropriate copyright notice and disclaimer of warranty and keep intact
  15. * all the notices that refer to this License and to the absence of any warranty;
  16. * and give any other recipients of the Program a copy of the GNU Lesser General
  17. * Public License along with the Program.
  18. *
  19. * If you modify your copy or copies of the library or any portion of it, you may
  20. * distribute the resulting library provided you do so under the GNU Lesser
  21. * General Public License. However, programs that link to the library may be
  22. * licensed under terms of your choice, so long as the library itself can be changed.
  23. * Any translation of the GNU Lesser General Public License must be accompanied by the
  24. * GNU Lesser General Public License.
  25. *
  26. * If you copy or distribute the program, you must accompany it with the complete
  27. * corresponding machine-readable source code or with a written offer, valid for at
  28. * least three years, to furnish the complete corresponding machine-readable source code.
  29. *
  30. * Any of the above conditions can be waived if you get permission from the copyright holder.
  31. * AjaXplorer is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  32. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  33. *
  34. * Description : Util methods for generating XML outputs.
  35. */
  36. defined('AJXP_EXEC') or die( 'Access not allowed');
  37. class AJXP_XMLWriter
  38. {
  39. function header($docNode="tree", $attributes=array())
  40. {
  41. header('Content-Type: text/xml; charset=UTF-8');
  42. header('Cache-Control: no-cache');
  43. print('<?xml version="1.0" encoding="UTF-8"?>');
  44. $attString = "";
  45. if(count($attributes)){
  46. foreach ($attributes as $name=>$value){
  47. $attString.="$name=\"$value\" ";
  48. }
  49. }
  50. print("<$docNode $attString>");
  51. }
  52. function close($docNode="tree")
  53. {
  54. print("</$docNode>");
  55. }
  56. function write($data, $print){
  57. if($print) {
  58. print($data);
  59. return "";
  60. }else{
  61. return $data;
  62. }
  63. }
  64. function renderPaginationData($count, $currentPage, $totalPages){
  65. $string = '<pagination count="'.$count.'" total="'.$totalPages.'" current="'.$currentPage.'" overflowMessage="306" icon="folder.png" openicon="folder_open.png"/>';
  66. AJXP_XMLWriter::write($string, true);
  67. }
  68. function renderHeaderNode($nodeName, $nodeLabel, $isLeaf, $metaData = array()){
  69. header('Content-Type: text/xml; charset=UTF-8');
  70. header('Cache-Control: no-cache');
  71. print('<?xml version="1.0" encoding="UTF-8"?>');
  72. AJXP_XMLWriter::renderNode($nodeName, $nodeLabel, $isLeaf, $metaData, false);
  73. }
  74. function renderNode($nodeName, $nodeLabel, $isLeaf, $metaData = array(), $close=true){
  75. $string = "<tree";
  76. $metaData["filename"] = $nodeName;
  77. $metaData["text"] = $nodeLabel;
  78. $metaData["is_file"] = ($isLeaf?"true":"false");
  79. foreach ($metaData as $key => $value){
  80. $string .= " $key=\"$value\"";
  81. }
  82. if($close){
  83. $string .= "/>";
  84. }else{
  85. $string .= ">";
  86. }
  87. AJXP_XMLWriter::write($string, true);
  88. }
  89. function renderNodeArray($array){
  90. self::renderNode($array[0],$array[1],$array[2],$array[3]);
  91. }
  92. function catchError($code, $message, $fichier, $ligne, $context){
  93. if(error_reporting() == 0) return ;
  94. $message = "$message in $fichier (l.$ligne)";
  95. AJXP_Logger::logAction("error", array("message" => $message));
  96. AJXP_XMLWriter::header();
  97. AJXP_XMLWriter::sendMessage(null, $message, true);
  98. AJXP_XMLWriter::close();
  99. exit(1);
  100. }
  101. /**
  102. * Catch exceptions
  103. *
  104. * @param Exception $exception
  105. */
  106. function catchException($exception){
  107. AJXP_XMLWriter::catchError($exception->getCode(), $exception->getMessage(), $exception->getFile(), $exception->getLine(), null);
  108. }
  109. static function replaceAjxpXmlKeywords($xml, $stripSpaces = false){
  110. $messages = ConfService::getMessages();
  111. $matches = array();
  112. $xml = str_replace("AJXP_CLIENT_RESOURCES_FOLDER", CLIENT_RESOURCES_FOLDER, $xml);
  113. if(isSet($_SESSION["AJXP_SERVER_PREFIX_URI"])){
  114. $xml = str_replace("AJXP_THEME_FOLDER", $_SESSION["AJXP_SERVER_PREFIX_URI"].AJXP_THEME_FOLDER, $xml);
  115. $xml = str_replace("AJXP_SERVER_ACCESS", $_SESSION["AJXP_SERVER_PREFIX_URI"].SERVER_ACCESS, $xml);
  116. }else{
  117. $xml = str_replace("AJXP_THEME_FOLDER", AJXP_THEME_FOLDER, $xml);
  118. $xml = str_replace("AJXP_SERVER_ACCESS", SERVER_ACCESS, $xml);
  119. }
  120. $xml = str_replace("AJXP_MIMES_EDITABLE", AJXP_Utils::getAjxpMimes("editable"), $xml);
  121. $xml = str_replace("AJXP_MIMES_IMAGE", AJXP_Utils::getAjxpMimes("image"), $xml);
  122. $xml = str_replace("AJXP_MIMES_AUDIO", AJXP_Utils::getAjxpMimes("audio"), $xml);
  123. $xml = str_replace("AJXP_MIMES_ZIP", AJXP_Utils::getAjxpMimes("zip"), $xml);
  124. $loginRedirect = ConfService::getAuthDriverImpl()->getLoginRedirect();
  125. $xml = str_replace("AJXP_LOGIN_REDIRECT", ($loginRedirect!==false?"'".$loginRedirect."'":"false"), $xml);
  126. $xml = str_replace("AJXP_REMOTE_AUTH", "false", $xml);
  127. $xml = str_replace("AJXP_NOT_REMOTE_AUTH", "true", $xml);
  128. $xml = str_replace("AJXP_ALL_MESSAGES", "MessageHash=".json_encode(ConfService::getMessages()).";", $xml);
  129. if(preg_match_all("/AJXP_MESSAGE(\[.*?\])/", $xml, $matches, PREG_SET_ORDER)){
  130. foreach($matches as $match){
  131. $messId = str_replace("]", "", str_replace("[", "", $match[1]));
  132. $xml = str_replace("AJXP_MESSAGE[$messId]", $messages[$messId], $xml);
  133. }
  134. }
  135. if($stripSpaces){
  136. $xml = preg_replace("/[\n\r]?/", "", $xml);
  137. $xml = preg_replace("/\t/", " ", $xml);
  138. }
  139. return $xml;
  140. }
  141. function reloadCurrentNode($print = true)
  142. {
  143. return AJXP_XMLWriter::write("<reload_instruction object=\"tree\"/>", $print);
  144. }
  145. function reloadNode($nodeName, $print = true)
  146. {
  147. return AJXP_XMLWriter::write("<reload_instruction object=\"tree\" node=\"$nodeName\"/>", $print);
  148. }
  149. function reloadFileList($fileOrBool, $print = true)
  150. {
  151. if(is_string($fileOrBool)) return AJXP_XMLWriter::write("<reload_instruction object=\"list\" file=\"".AJXP_Utils::xmlEntities(SystemTextEncoding::toUTF8($fileOrBool))."\"/>", $print);
  152. else return AJXP_XMLWriter::write("<reload_instruction object=\"list\"/>", $print);
  153. }
  154. function reloadDataNode($nodePath="", $pendingSelection="", $print = true){
  155. $nodePath = AJXP_Utils::xmlEntities($nodePath, true);
  156. $pendingSelection = AJXP_Utils::xmlEntities($pendingSelection, true);
  157. return AJXP_XMLWriter::write("<reload_instruction object=\"data\" node=\"$nodePath\" file=\"$pendingSelection\"/>", $print);
  158. }
  159. function reloadRepositoryList($print = true){
  160. return AJXP_XMLWriter::write("<reload_instruction object=\"repository_list\"/>", $print);
  161. }
  162. function requireAuth($print = true)
  163. {
  164. return AJXP_XMLWriter::write("<require_auth/>", $print);
  165. }
  166. function triggerBgAction($actionName, $parameters, $messageId, $print=true){
  167. $data = AJXP_XMLWriter::write("<trigger_bg_action name=\"$actionName\" messageId=\"$messageId\">", $print);
  168. foreach ($parameters as $paramName=>$paramValue){
  169. $data .= AJXP_XMLWriter::write("<param name=\"$paramName\" value=\"$paramValue\"/>", $print);
  170. }
  171. $data .= AJXP_XMLWriter::write("</trigger_bg_action>", $print);
  172. return $data;
  173. }
  174. function writeBookmarks($allBookmarks, $print = true)
  175. {
  176. $buffer = "";
  177. foreach ($allBookmarks as $bookmark)
  178. {
  179. $path = ""; $title = "";
  180. if(is_array($bookmark)){
  181. $path = $bookmark["PATH"];
  182. $title = $bookmark["TITLE"];
  183. }else if(is_string($bookmark)){
  184. $path = $bookmark;
  185. $title = basename($bookmark);
  186. }
  187. $buffer .= "<bookmark path=\"".$path."\" title=\"".$title."\"/>";
  188. }
  189. if($print) print $buffer;
  190. else return $buffer;
  191. }
  192. function sendFilesListComponentConfig($config){
  193. if(is_string($config)){
  194. print("<client_configs><component_config className=\"FilesList\">$config</component_config></client_configs>");
  195. }
  196. }
  197. function sendMessage($logMessage, $errorMessage, $print = true)
  198. {
  199. $messageType = "";
  200. $message = "";
  201. if($errorMessage == null)
  202. {
  203. $messageType = "SUCCESS";
  204. $message = AJXP_Utils::xmlEntities($logMessage);
  205. }
  206. else
  207. {
  208. $messageType = "ERROR";
  209. $message = AJXP_Utils::xmlEntities($errorMessage);
  210. }
  211. return AJXP_XMLWriter::write("<message type=\"$messageType\">".$message."</message>", $print);
  212. }
  213. function sendUserData($userObject = null, $details=false){
  214. print(AJXP_XMLWriter::getUserXML($userObject, $details));
  215. }
  216. function getUserXML($userObject = null, $details=false)
  217. {
  218. $buffer = "";
  219. $loggedUser = AuthService::getLoggedUser();
  220. if($userObject != null) $loggedUser = $userObject;
  221. if(!AuthService::usersEnabled()){
  222. $buffer.="<user id=\"shared\">";
  223. if(!$details){
  224. $buffer.="<active_repo id=\"".ConfService::getCurrentRootDirIndex()."\" write=\"1\" read=\"1\"/>";
  225. }
  226. $buffer.= AJXP_XMLWriter::writeRepositoriesData(null, $details);
  227. $buffer.="</user>";
  228. }else if($loggedUser != null){
  229. $buffer.="<user id=\"".$loggedUser->id."\">";
  230. if(!$details){
  231. $buffer.="<active_repo id=\"".ConfService::getCurrentRootDirIndex()."\" write=\"".($loggedUser->canWrite(ConfService::getCurrentRootDirIndex())?"1":"0")."\" read=\"".($loggedUser->canRead(ConfService::getCurrentRootDirIndex())?"1":"0")."\"/>";
  232. }
  233. $buffer.= AJXP_XMLWriter::writeRepositoriesData($loggedUser, $details);
  234. $buffer.="<preferences>";
  235. $buffer.="<pref name=\"display\" value=\"".$loggedUser->getPref("display")."\"/>";
  236. $buffer.="<pref name=\"lang\" value=\"".$loggedUser->getPref("lang")."\"/>";
  237. $buffer.="<pref name=\"diapo_autofit\" value=\"".$loggedUser->getPref("diapo_autofit")."\"/>";
  238. $buffer.="<pref name=\"sidebar_splitter_size\" value=\"".$loggedUser->getPref("sidebar_splitter_size")."\"/>";
  239. $buffer.="<pref name=\"vertical_splitter_size\" value=\"".$loggedUser->getPref("vertical_splitter_size")."\"/>";
  240. $buffer.="<pref name=\"history_last_repository\" value=\"".$loggedUser->getArrayPref("history", "last_repository")."\"/>";
  241. $buffer.="<pref name=\"history_last_listing\" value=\"".AJXP_Utils::xmlEntities(stripslashes($loggedUser->getArrayPref("history", ConfService::getCurrentRootDirIndex())))."\"/>";
  242. $buffer.="<pref name=\"thumb_size\" value=\"".$loggedUser->getPref("thumb_size")."\"/>";
  243. $buffer.="<pref name=\"columns_size\" value=\"".stripslashes(str_replace("\"", "'", $loggedUser->getPref("columns_size")))."\"/>";
  244. $buffer.="<pref name=\"columns_visibility\" value=\"".stripslashes(str_replace("\"", "'", $loggedUser->getPref("columns_visibility")))."\"/>";
  245. $buffer.="<pref name=\"ls_history\" value=\"".stripslashes(str_replace("\"", "'", $loggedUser->getPref("ls_history")))."\"/>";
  246. $buffer.="<pref name=\"upload_auto_send\" value=\"".$loggedUser->getPref("upload_auto_send")."\"/>";
  247. $buffer.="<pref name=\"upload_auto_close\" value=\"".$loggedUser->getPref("upload_auto_close")."\"/>";
  248. $buffer.="<pref name=\"upload_existing\" value=\"".$loggedUser->getPref("upload_existing")."\"/>";
  249. $buffer.="</preferences>";
  250. $buffer.="<special_rights is_admin=\"".($loggedUser->isAdmin()?"1":"0")."\"/>";
  251. $bMarks = $loggedUser->getBookmarks();
  252. if(count($bMarks)){
  253. $buffer.= "<bookmarks>".AJXP_XMLWriter::writeBookmarks($bMarks, false)."</bookmarks>";
  254. }
  255. $buffer.="</user>";
  256. }
  257. return $buffer;
  258. }
  259. function writeRepositoriesData($loggedUser, $details=false){
  260. $st = "";
  261. $st .= "<repositories>";
  262. $streams = ConfService::detectRepositoryStreams(false);
  263. foreach (ConfService::getRepositoriesList() as $rootDirIndex => $rootDirObject)
  264. {
  265. $toLast = false;
  266. if($rootDirObject->getAccessType()=="ajxp_conf"){
  267. if(AuthService::usersEnabled() && !$loggedUser->isAdmin()){
  268. continue;
  269. }else{
  270. $toLast = true;
  271. }
  272. }
  273. if($loggedUser == null || $loggedUser->canRead($rootDirIndex) || $details) {
  274. $rightString = "";
  275. if($details){
  276. $rightString = " r=\"".($loggedUser->canRead($rootDirIndex)?"1":"0")."\" w=\"".($loggedUser->canWrite($rootDirIndex)?"1":"0")."\"";
  277. }
  278. $streamString = "";
  279. if(in_array($rootDirObject->accessType, $streams)){
  280. $streamString = "allowCrossRepositoryCopy=\"true\"";
  281. }
  282. if($toLast){
  283. $lastString = "<repo access_type=\"".$rootDirObject->accessType."\" id=\"".$rootDirIndex."\"$rightString $streamString><label>".SystemTextEncoding::toUTF8(AJXP_Utils::xmlEntities($rootDirObject->getDisplay()))."</label>".$rootDirObject->getClientSettings()."</repo>";
  284. }else{
  285. $st .= "<repo access_type=\"".$rootDirObject->accessType."\" id=\"".$rootDirIndex."\"$rightString $streamString><label>".SystemTextEncoding::toUTF8(AJXP_Utils::xmlEntities($rootDirObject->getDisplay()))."</label>".$rootDirObject->getClientSettings()."</repo>";
  286. }
  287. }
  288. }
  289. if(isSet($lastString)){
  290. $st.= $lastString;
  291. }
  292. $st .= "</repositories>";
  293. return $st;
  294. }
  295. function loggingResult($result, $rememberLogin="", $rememberPass = "")
  296. {
  297. $remString = "";
  298. if($rememberPass != "" && $rememberLogin!= ""){
  299. $remString = " remember_login=\"$rememberLogin\" remember_pass=\"$rememberPass\"";
  300. }
  301. print("<logging_result value=\"$result\"$remString/>");
  302. }
  303. }
  304. ?>