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