1 31 32 package org.opencms.editors.fckeditor; 33 34 import org.opencms.db.CmsDbSqlException; 35 import org.opencms.file.CmsFile; 36 import org.opencms.file.CmsProperty; 37 import org.opencms.file.CmsPropertyDefinition; 38 import org.opencms.file.CmsResource; 39 import org.opencms.file.CmsResourceFilter; 40 import org.opencms.file.CmsVfsResourceAlreadyExistsException; 41 import org.opencms.file.types.CmsResourceTypeFolder; 42 import org.opencms.file.types.CmsResourceTypeImage; 43 import org.opencms.flex.CmsFlexController; 44 import org.opencms.i18n.CmsEncoder; 45 import org.opencms.jsp.CmsJspActionElement; 46 import org.opencms.main.CmsException; 47 import org.opencms.main.CmsIllegalArgumentException; 48 import org.opencms.main.OpenCms; 49 import org.opencms.security.CmsPermissionViolationException; 50 import org.opencms.util.CmsRequestUtil; 51 import org.opencms.util.CmsStringUtil; 52 import org.opencms.workplace.CmsDialog; 53 import org.opencms.workplace.CmsWorkplaceSettings; 54 import org.opencms.xml.CmsXmlUtils; 55 56 import java.util.ArrayList ; 57 import java.util.Iterator ; 58 import java.util.List ; 59 60 import javax.servlet.http.HttpServletRequest ; 61 import javax.servlet.http.HttpServletResponse ; 62 import javax.servlet.jsp.PageContext ; 63 64 import org.apache.commons.fileupload.FileItem; 65 66 import org.dom4j.Document; 67 import org.dom4j.DocumentHelper; 68 import org.dom4j.Element; 69 70 83 public class CmsFCKEditorFileBrowser extends CmsDialog { 84 85 86 public static final int ACTION_CREATEFOLDER = 502; 87 88 89 public static final int ACTION_FILEUPLOAD = 503; 90 91 92 public static final int ACTION_GETFOLDERS = 500; 93 94 95 public static final int ACTION_GETFOLDERS_FILES = 501; 96 97 98 public static final String ATTR_COMMAND = "command"; 99 100 101 public static final String ATTR_NAME = "name"; 102 103 104 public static final String ATTR_NUMBER = "number"; 105 106 107 public static final String ATTR_PATH = "path"; 108 109 110 public static final String ATTR_RESOURCETYPE = "resourceType"; 111 112 113 public static final String ATTR_SIZE = "size"; 114 115 116 public static final String ATTR_URL = "url"; 117 118 119 public static final String COMMAND_CREATEFOLDER = "CreateFolder"; 120 121 122 public static final String COMMAND_FILEUPLOAD = "FileUpload"; 123 124 125 public static final String COMMAND_GETFOLDERS = "GetFolders"; 126 127 128 public static final String COMMAND_GETFOLDERS_FILES = "GetFoldersAndFiles"; 129 130 131 public static final String CONTENTTYPE_HTML = "text/html"; 132 133 134 public static final String CONTENTTYPE_XML = "text/xml"; 135 136 137 public static final String DIALOG_TYPE = "FCKeditor_file_browser"; 138 139 140 public static final String ERROR_CREATEFOLDER_EXISTS = "101"; 141 142 143 public static final String ERROR_CREATEFOLDER_INVALIDNAME = "102"; 144 145 146 public static final String ERROR_CREATEFOLDER_NOPERMISSIONS = "103"; 147 148 149 public static final String ERROR_CREATEFOLDER_OK = "0"; 150 151 152 public static final String ERROR_CREATEFOLDER_UNKNOWNERROR = "110"; 153 154 155 public static final String ERROR_UPLOAD_INVALID = "202"; 156 157 158 public static final String ERROR_UPLOAD_OK = "0"; 159 160 161 public static final String NODE_CONNECTOR = "Connector"; 162 163 164 public static final String NODE_CURRENTFOLDER = "CurrentFolder"; 165 166 167 public static final String NODE_ERROR = "Error"; 168 169 170 public static final String NODE_FILE = "File"; 171 172 173 public static final String NODE_FILES = "Files"; 174 175 176 public static final String NODE_FOLDER = "Folder"; 177 178 179 public static final String NODE_FOLDERS = "Folders"; 180 181 182 public static final String PARAM_COMMAND = "Command"; 183 184 185 public static final String PARAM_CURRENTFOLDER = "CurrentFolder"; 186 187 188 public static final String PARAM_NEWFOLDERNAME = "NewFolderName"; 189 190 191 public static final String PARAM_SERVERPATH = "ServerPath"; 192 193 194 public static final String PARAM_TYPE = "Type"; 195 196 197 public static final String TYPE_FILE = "File"; 198 199 200 public static final String TYPE_FLASH = "Flash"; 201 202 203 public static final String TYPE_IMAGE = "Image"; 204 205 206 public static final String TYPE_MEDIA = "Media"; 207 208 209 private Document m_document; 210 211 212 private List m_multiPartFileItems; 213 214 215 private String m_paramCommand; 216 217 218 private String m_paramCurrentFolder; 219 220 221 private String m_paramNewFolderName; 222 223 224 private String m_paramServerPath; 225 226 227 private String m_paramType; 228 229 234 public CmsFCKEditorFileBrowser(CmsJspActionElement jsp) { 235 236 super(jsp); 237 } 238 239 246 public CmsFCKEditorFileBrowser(PageContext context, HttpServletRequest req, HttpServletResponse res) { 247 248 this(new CmsJspActionElement(context, req, res)); 249 } 250 251 256 public String displayDialog() { 257 258 switch (getAction()) { 259 case ACTION_CREATEFOLDER: 260 return createFolder(); 261 case ACTION_FILEUPLOAD: 262 return uploadFile(); 263 case ACTION_GETFOLDERS: 264 return getFolders(false); 265 case ACTION_GETFOLDERS_FILES: 266 default: 267 return getFolders(true); 268 } 269 } 270 271 279 public void fillParamValues(HttpServletRequest request) { 280 281 if (null == request.getAttribute(REQUEST_ATTRIBUTE_MULTIPART)) { 283 m_multiPartFileItems = CmsRequestUtil.readMultipartFileItems(request); 285 if (m_multiPartFileItems != null) { 286 CmsRequestUtil.readParameterMapFromMultiPart( 288 getCms().getRequestContext().getEncoding(), 289 m_multiPartFileItems); 290 request.setAttribute(REQUEST_ATTRIBUTE_MULTIPART, Boolean.TRUE); 291 } 292 } 293 294 setParamCommand(decodeParamValue(PARAM_COMMAND, request.getParameter(PARAM_COMMAND))); 296 setParamCurrentFolder(decodeParamValue(PARAM_CURRENTFOLDER, request.getParameter(PARAM_CURRENTFOLDER))); 297 setParamNewFolderName(decodeParamValue(PARAM_NEWFOLDERNAME, request.getParameter(PARAM_NEWFOLDERNAME))); 298 setParamServerPath(decodeParamValue(PARAM_SERVERPATH, request.getParameter(PARAM_SERVERPATH))); 299 setParamType(decodeParamValue(PARAM_TYPE, request.getParameter(PARAM_TYPE))); 300 } 301 302 307 public String getParamCommand() { 308 309 return m_paramCommand; 310 } 311 312 317 public String getParamCurrentFolder() { 318 319 return m_paramCurrentFolder; 320 } 321 322 327 public String getParamNewFolderName() { 328 329 return m_paramNewFolderName; 330 } 331 332 337 public String getParamServerPath() { 338 339 return m_paramServerPath; 340 } 341 342 347 public String getParamType() { 348 349 return m_paramType; 350 } 351 352 357 public void setParamCommand(String paramCommand) { 358 359 m_paramCommand = paramCommand; 360 } 361 362 367 public void setParamCurrentFolder(String paramCurrentFolder) { 368 369 if (CmsStringUtil.isEmpty(paramCurrentFolder)) { 370 m_paramCurrentFolder = "/"; 371 } else { 372 m_paramCurrentFolder = paramCurrentFolder; 373 } 374 } 375 376 381 public void setParamNewFolderName(String paramNewFolderName) { 382 383 m_paramNewFolderName = paramNewFolderName; 384 } 385 386 391 public void setParamServerPath(String paramServerPath) { 392 393 if (CmsStringUtil.isEmpty(paramServerPath)) { 394 m_paramServerPath = OpenCms.getSystemInfo().getOpenCmsContext() + getParamCurrentFolder(); 395 } else { 396 m_paramServerPath = OpenCms.getSystemInfo().getOpenCmsContext() + paramServerPath; 397 } 398 } 399 400 405 public void setParamType(String paramType) { 406 407 if (CmsStringUtil.isEmpty(paramType)) { 408 m_paramType = ""; 409 } else { 410 m_paramType = paramType; 411 } 412 } 413 414 419 protected String createFolder() { 420 421 createXMLHead(); 422 Element error = getDocument().getRootElement().addElement(NODE_ERROR); 423 try { 424 getCms().createResource( 425 getParamCurrentFolder() + getParamNewFolderName(), 426 CmsResourceTypeFolder.RESOURCE_TYPE_ID); 427 error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_OK); 429 } catch (Exception e) { 430 if (e instanceof CmsVfsResourceAlreadyExistsException) { 432 error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_EXISTS); 434 } else if (e instanceof CmsIllegalArgumentException) { 435 error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_INVALIDNAME); 437 } else if (e instanceof CmsPermissionViolationException) { 438 error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_NOPERMISSIONS); 440 } else { 441 error.addAttribute(ATTR_NUMBER, ERROR_CREATEFOLDER_UNKNOWNERROR); 443 } 444 445 } 446 447 try { 448 return CmsXmlUtils.marshal(getDocument(), CmsEncoder.ENCODING_UTF_8); 449 } catch (CmsException e) { 450 return ""; 452 } 453 } 454 455 458 protected void createXMLHead() { 459 460 Element connector = getDocument().addElement(NODE_CONNECTOR); 462 connector.addAttribute(ATTR_COMMAND, getParamCommand()); 463 connector.addAttribute(ATTR_RESOURCETYPE, getParamType()); 464 Element currFolder = connector.addElement(NODE_CURRENTFOLDER); 465 currFolder.addAttribute(ATTR_PATH, getParamCurrentFolder()); 466 currFolder.addAttribute(ATTR_URL, getParamServerPath()); 467 468 } 469 470 475 protected Document getDocument() { 476 477 if (m_document == null) { 478 m_document = DocumentHelper.createDocument(); 479 } 480 return m_document; 481 } 482 483 489 protected String getFolders(boolean includeFiles) { 490 491 createXMLHead(); 492 Element folders = getDocument().getRootElement().addElement(NODE_FOLDERS); 493 Element files = null; 494 495 CmsResourceFilter filter; 497 if (includeFiles) { 498 filter = CmsResourceFilter.DEFAULT.addRequireVisible(); 500 files = getDocument().getRootElement().addElement(NODE_FILES); 501 } else { 502 filter = CmsResourceFilter.DEFAULT_FOLDERS.addRequireVisible(); 504 } 505 506 try { 507 List resources = getCms().readResources(getParamCurrentFolder(), filter, false); 508 Iterator i = resources.iterator(); 509 while (i.hasNext()) { 510 CmsResource res = (CmsResource)i.next(); 511 if (res.isFolder()) { 512 Element folder = folders.addElement(NODE_FOLDER); 514 String folderName = CmsResource.getName(res.getRootPath()); 515 folderName = CmsStringUtil.substitute(folderName, "/", ""); 516 folder.addAttribute(ATTR_NAME, folderName); 517 } else { 518 boolean showFile = true; 520 if (TYPE_IMAGE.equals(getParamType())) { 522 showFile = (res.getTypeId() == CmsResourceTypeImage.getStaticTypeId()); 523 } 524 if ((showFile) && (files != null)) { 525 Element file = files.addElement(NODE_FILE); 527 file.addAttribute(ATTR_NAME, CmsResource.getName(res.getRootPath())); 528 file.addAttribute(ATTR_SIZE, "" + (res.getLength() / 1024)); 529 } 530 } 531 532 } 533 return CmsXmlUtils.marshal(getDocument(), CmsEncoder.ENCODING_UTF_8); 534 } catch (CmsException e) { 535 return ""; 537 } 538 } 539 540 543 protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest request) { 544 545 fillParamValues(request); 547 548 setParamDialogtype(DIALOG_TYPE); 550 if (COMMAND_FILEUPLOAD.equals(getParamCommand())) { 552 setAction(ACTION_FILEUPLOAD); 554 } else if (COMMAND_CREATEFOLDER.equals(getParamCommand())) { 555 setAction(ACTION_CREATEFOLDER); 557 } else if (COMMAND_GETFOLDERS.equals(getParamCommand())) { 558 setAction(ACTION_GETFOLDERS); 560 } else { 561 setAction(ACTION_GETFOLDERS_FILES); 563 } 564 565 CmsFlexController controller = CmsFlexController.getController(getJsp().getRequest()); 567 HttpServletResponse res = controller.getTopResponse(); 568 CmsRequestUtil.setNoCacheHeaders(res); 570 String contentType = CONTENTTYPE_XML; 571 if (getAction() == ACTION_FILEUPLOAD) { 572 contentType = CONTENTTYPE_HTML; 573 } 574 res.setContentType(contentType); 575 } 576 577 582 protected String uploadFile() { 583 584 String errorCode = ERROR_UPLOAD_OK; 585 try { 586 Iterator i = m_multiPartFileItems.iterator(); 588 FileItem fi = null; 589 while (i.hasNext()) { 590 fi = (FileItem)i.next(); 591 if (fi.getName() != null) { 592 break; 594 } else { 595 continue; 597 } 598 } 599 600 if (fi != null) { 601 String fileName = fi.getName(); 602 long size = fi.getSize(); 603 long maxFileSizeBytes = OpenCms.getWorkplaceManager().getFileBytesMaxUploadSize(getCms()); 604 if (maxFileSizeBytes > 0 && size > maxFileSizeBytes) { 606 throw new Exception (); 608 } 609 byte[] content = fi.get(); 610 fi.delete(); 611 612 String newResname = CmsResource.getName(fileName.replace('\\', '/')); 614 String title = newResname; 616 if (title.lastIndexOf('.') != -1) { 617 title = title.substring(0, title.lastIndexOf('.')); 618 } 619 List properties = new ArrayList (1); 620 CmsProperty titleProp = new CmsProperty(); 621 titleProp.setName(CmsPropertyDefinition.PROPERTY_TITLE); 622 if (OpenCms.getWorkplaceManager().isDefaultPropertiesOnStructure()) { 623 titleProp.setStructureValue(title); 624 } else { 625 titleProp.setResourceValue(title); 626 } 627 properties.add(titleProp); 628 629 int resTypeId = OpenCms.getResourceManager().getDefaultTypeForName(newResname).getTypeId(); 631 632 newResname = getParamCurrentFolder() + newResname; 634 635 if (!getCms().existsResource(newResname, CmsResourceFilter.IGNORE_EXPIRATION)) { 636 try { 637 getCms().createResource(newResname, resTypeId, content, properties); 639 } catch (CmsDbSqlException sqlExc) { 640 getCms().lockResource(newResname); 642 getCms().deleteResource(newResname, CmsResource.DELETE_PRESERVE_SIBLINGS); 643 throw sqlExc; 644 } 645 } else { 646 checkLock(newResname); 648 CmsFile file = getCms().readFile(newResname, CmsResourceFilter.IGNORE_EXPIRATION); 649 byte[] contents = file.getContents(); 650 try { 651 getCms().replaceResource(newResname, resTypeId, content, null); 652 } catch (CmsDbSqlException sqlExc) { 653 file.setContents(contents); 655 getCms().writeFile(file); 656 throw sqlExc; 657 } 658 } 659 } else { 660 throw new Exception (); 662 } 663 } catch (Throwable e) { 664 errorCode = ERROR_UPLOAD_INVALID; 666 } 667 668 StringBuffer result = new StringBuffer (256); 670 result.append("<html><head><script type=\"text/javascript\">\n"); 671 result.append("window.parent.frames[\"frmUpload\"].OnUploadCompleted("); 672 result.append(errorCode); 673 result.append(");\n"); 674 result.append("</script></head></html>"); 675 return result.toString(); 676 } 677 678 } | Popular Tags |