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

/mollify/backend/plugin/Share/ShareHandler.class.php

http://mollify.googlecode.com/
PHP | 126 lines | 89 code | 28 blank | 9 comment | 14 complexity | 323c4d6181217dedba777617537afc6a MD5 | raw file
Possible License(s): LGPL-2.1, BSD-3-Clause
  1. <?php
  2. /**
  3. * Copyright (c) 2008- Samuli Järvelä
  4. *
  5. * All rights reserved. This program and the accompanying materials
  6. * are made available under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution, and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html. If redistributing this code,
  9. * this entire header must remain intact.
  10. */
  11. require_once("dao/ShareDao.class.php");
  12. class ShareHandler {
  13. private $env;
  14. private $settings;
  15. public function __construct($env, $settings) {
  16. $this->env = $env;
  17. $this->settings = $settings;
  18. }
  19. public function getItemContextData($item, $details, $key, $data) {
  20. return array(
  21. "count" => $this->dao()->getShareCount($item, $this->env->session()->userId())
  22. );
  23. }
  24. public function getShares($item) {
  25. return $this->dao()->getShares($item, $this->env->session()->userId());
  26. }
  27. public function addShare($item, $name, $expirationTs, $active) {
  28. $created = $this->env->configuration()->formatTimestampInternal(time());
  29. $this->dao()->addShare($this->GUID(), $item, $name, $this->env->session()->userId(), $expirationTs, $created, $active);
  30. }
  31. public function editShare($id, $name, $expirationTs, $active) {
  32. $this->dao()->editShare($id, $name, $expirationTs, $active);
  33. }
  34. public function deleteShare($id) {
  35. $this->dao()->deleteShare($id);
  36. }
  37. public function processShareGet($id) {
  38. $share = $this->dao()->getShare($id, $this->env->configuration()->formatTimestampInternal(time()));
  39. if (!$share) $this->showInvalidSharePage();
  40. $this->env->filesystem()->allowFilesystems = TRUE;
  41. $item = $this->env->filesystem()->item($share["item_id"]);
  42. if (!$item) throw new ServiceException("INVALID_REQUEST");
  43. if ($item->isFile()) $this->processDownload($item);
  44. else $this->processUploadPage($id, $item);
  45. }
  46. private function showInvalidSharePage() {
  47. include("pages/InvalidShare.php");
  48. die();
  49. }
  50. private function processDownload($file) {
  51. $mobile = ($this->env->request()->hasParam("m") and strcmp($this->env->request()->param("m"), "1") == 0);
  52. $this->env->filesystem()->temporaryItemPermission($file, Authentication::PERMISSION_VALUE_READONLY);
  53. $this->env->filesystem()->download($file, $mobile);
  54. }
  55. private function processUploadPage($shareId, $folder) {
  56. $uploader = $this->getUploader();
  57. $uploader->showPage($shareId, $folder);
  58. die();
  59. }
  60. public function processSharePost($id) {
  61. $share = $this->dao()->getShare($id);
  62. if (!$share) $this->showInvalidSharePage();
  63. $this->env->filesystem()->allowFilesystems = TRUE;
  64. $item = $this->env->filesystem()->item($share["item_id"]);
  65. if (!$item or $item->isFile()) throw new ServiceException("INVALID_REQUEST");
  66. $this->processUpload($id, $item);
  67. }
  68. private function getUploader() {
  69. $uploader = FALSE;
  70. if (isset($this->settings) and isset($this->settings["uploader"])) $uploader = $this->settings["uploader"];
  71. if (!$uploader) require_once("upload/http/PublicUploader.class.php");
  72. else require_once($uploader."/PublicUploader.class.php");
  73. return new PublicUploader($this->env);
  74. }
  75. public function processUpload($shareId, $folder) {
  76. $this->env->filesystem()->temporaryItemPermission($folder, Authentication::PERMISSION_VALUE_READWRITE);
  77. $uploader = $this->getUploader();
  78. $uploader->uploadTo($shareId, $folder);
  79. }
  80. public function onEvent($e) {
  81. if (strcmp(FilesystemController::EVENT_TYPE_FILE, $e->type()) != 0) return;
  82. $type = $e->subType();
  83. if ($type === FileEvent::DELETE)
  84. $this->dao()->deleteShares($e->item());
  85. }
  86. private function dao() {
  87. return new ShareDao($this->env);
  88. }
  89. private function GUID() {
  90. if (function_exists('com_create_guid') === true)
  91. return str_replace('-', '', trim(com_create_guid(), '{}'));
  92. return sprintf('%04X%04X%04X%04X%04X%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
  93. }
  94. public function __toString() {
  95. return "CommentHandler";
  96. }
  97. }
  98. ?>