| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | <?php | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ownCloud - Documents App | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @author Victor Dubiniuk | 
					
						
							|  |  |  |  * @copyright 2013 Victor Dubiniuk victor.dubiniuk@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
					
						
							|  |  |  |  * later. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace OCA\Documents; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class SessionController extends Controller{ | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-09-25 16:34:35 +03:00
										 |  |  | 	public static function joinAsGuest($args){ | 
					
						
							|  |  |  | 		$uid = self::preDispatchGuest(); | 
					
						
							| 
									
										
										
										
											2013-09-28 00:11:24 +03:00
										 |  |  | 		$uid = substr(@$_POST['name'], 0, 16) .' '. $uid; | 
					
						
							| 
									
										
										
										
											2013-09-25 16:34:35 +03:00
										 |  |  | 		$token = @$args['token']; | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 		try { | 
					
						
							|  |  |  | 			$file = File::getByShareToken($token); | 
					
						
							|  |  |  | 			self::join($uid, $file); | 
					
						
							|  |  |  | 		} catch (\Exception $e){ | 
					
						
							|  |  |  | 			Helper::warnLog('Starting a session failed. Reason: ' . $e->getMessage()); | 
					
						
							|  |  |  | 			\OCP\JSON::error(); | 
					
						
							|  |  |  | 			exit(); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-09-25 16:34:35 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public static function joinAsUser($args){ | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 		$uid = self::preDispatch(); | 
					
						
							| 
									
										
										
										
											2013-09-18 23:41:43 +03:00
										 |  |  | 		$fileId = intval(@$args['file_id']); | 
					
						
							| 
									
										
										
										
											2013-12-05 01:02:08 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 		try { | 
					
						
							|  |  |  | 			$file = new File($fileId); | 
					
						
							| 
									
										
										
										
											2013-12-05 01:02:08 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 			if ($file->getPermissions() & \OCP\PERMISSION_UPDATE) { | 
					
						
							|  |  |  | 				self::join($uid, $file);	 | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				\OCP\JSON::success(array( | 
					
						
							|  |  |  | 					'permissions' => $file->getPermissions(), | 
					
						
							|  |  |  | 					'id' => $fileId | 
					
						
							|  |  |  | 				)); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 			exit(); | 
					
						
							|  |  |  | 		} catch (\Exception $e){ | 
					
						
							|  |  |  | 			Helper::warnLog('Starting a session failed. Reason: ' . $e->getMessage()); | 
					
						
							|  |  |  | 			\OCP\JSON::error(); | 
					
						
							|  |  |  | 			exit(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 	 | 
					
						
							|  |  |  | 	protected static function join($uid, $file){ | 
					
						
							|  |  |  | 			$session = Db_Session::start($uid, $file); | 
					
						
							|  |  |  | 			\OCP\JSON::success($session); | 
					
						
							|  |  |  | 			exit(); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-09-25 16:34:35 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-11-09 17:29:12 +03:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 	 * Store the document content to its origin | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public static function save(){ | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 			$esId = @$_SERVER['HTTP_WEBODF_SESSION_ID']; | 
					
						
							|  |  |  | 			if (!$esId){ | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 				throw new \Exception('Session id can not be empty'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			$memberId = @$_SERVER['HTTP_WEBODF_MEMBER_ID']; | 
					
						
							|  |  |  | 			$sessionRevision = @$_SERVER['HTTP_WEBODF_SESSION_REVISION']; | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-11-09 18:24:03 +03:00
										 |  |  | 			$stream = fopen('php://input','r'); | 
					
						
							|  |  |  | 			if (!$stream){ | 
					
						
							|  |  |  | 				throw new \Exception('New content missing'); | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-11-09 18:24:03 +03:00
										 |  |  | 			$content = stream_get_contents($stream); | 
					
						
							| 
									
										
										
										
											2013-11-09 17:29:12 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 			$session = new Db_Session(); | 
					
						
							|  |  |  | 			$session->load($esId); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			if (!$session->hasData()){ | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 				throw new \Exception('Session does not exist'); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 			$sessionData = $session->getData(); | 
					
						
							|  |  |  | 			$file = new File($sessionData['file_id']); | 
					
						
							| 
									
										
										
										
											2013-09-26 21:01:41 +03:00
										 |  |  | 			if (!$file->isPublicShare()){ | 
					
						
							|  |  |  | 				self::preDispatch(); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				self::preDispatchGuest(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-09-24 03:39:05 +03:00
										 |  |  | 			list($view, $path) = $file->getOwnerViewAndPath(); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 			$isWritable = ($view->file_exists($path) && $view->isUpdatable($path)) || $view->isCreatable($path); | 
					
						
							|  |  |  | 			if (!$isWritable){ | 
					
						
							| 
									
										
										
										
											2013-09-20 12:41:37 +03:00
										 |  |  | 				throw new \Exception($path . ' does not exist or is not writable for user ' . $uid); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-10-31 20:24:55 +03:00
										 |  |  | 			$member = new Db_Member(); | 
					
						
							|  |  |  | 			$members = $member->getActiveCollection($esId); | 
					
						
							|  |  |  | 			$memberIds = array_map( | 
					
						
							|  |  |  | 				function($x){ | 
					
						
							|  |  |  | 					return ($x['member_id']); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				$members | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 			//check if member belongs to the session
 | 
					
						
							|  |  |  | 			if (!in_array($memberId, $memberIds)){ | 
					
						
							|  |  |  | 				throw new \Exception($memberId . ' does not belong to session ' . $esId); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			// Active users except current user
 | 
					
						
							|  |  |  | 			$memberCount = count($memberIds) - 1; | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-12-26 15:42:28 +00:00
										 |  |  | 			if ($view->file_exists($path)){		 | 
					
						
							| 
									
										
										
										
											2013-12-02 22:13:34 +03:00
										 |  |  | 				 | 
					
						
							|  |  |  | 				$proxyStatus = \OC_FileProxy::$enabled; | 
					
						
							|  |  |  | 				\OC_FileProxy::$enabled = false;	 | 
					
						
							| 
									
										
										
										
											2013-09-20 09:57:10 +03:00
										 |  |  | 				$currentHash = sha1($view->file_get_contents($path)); | 
					
						
							| 
									
										
										
										
											2013-12-02 22:13:34 +03:00
										 |  |  | 				\OC_FileProxy::$enabled = $proxyStatus; | 
					
						
							|  |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-11-21 23:38:39 +03:00
										 |  |  | 				if (!Helper::isVersionsEnabled() && $currentHash !== $sessionData['genesis_hash']){ | 
					
						
							| 
									
										
										
										
											2013-09-20 09:57:10 +03:00
										 |  |  | 					// Original file was modified externally. Save to a new one
 | 
					
						
							| 
									
										
										
										
											2013-09-21 00:01:11 +03:00
										 |  |  | 					$path = Helper::getNewFileName($view, $path, '-conflict'); | 
					
						
							| 
									
										
										
										
											2013-09-20 09:57:10 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-12-26 15:42:28 +00:00
										 |  |  | 				 | 
					
						
							|  |  |  | 				$mimetype = $view->getMimeType($path); | 
					
						
							|  |  |  | 			} else { | 
					
						
							|  |  |  | 				$mimetype = Storage::MIMETYPE_LIBREOFFICE_WORDPROCESSOR; | 
					
						
							| 
									
										
										
										
											2013-09-20 09:57:10 +03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2013-12-26 15:42:28 +00:00
										 |  |  | 			$data = Filter::write($content, $mimetype); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			if ($view->file_put_contents($path, $data['content'])){ | 
					
						
							| 
									
										
										
										
											2013-10-31 20:24:55 +03:00
										 |  |  | 				// Not a last user
 | 
					
						
							|  |  |  | 				if ($memberCount>0){ | 
					
						
							|  |  |  | 					// Update genesis hash to prevent conflicts
 | 
					
						
							| 
									
										
										
										
											2013-12-02 22:13:34 +03:00
										 |  |  | 					Helper::debugLog('Update hash'); | 
					
						
							| 
									
										
										
										
											2013-12-26 15:42:28 +00:00
										 |  |  | 					 | 
					
						
							|  |  |  | 					$session->updateGenesisHash($esId, sha1($data['content'])); | 
					
						
							| 
									
										
										
										
											2013-10-31 20:24:55 +03:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					// Last user. Kill session data
 | 
					
						
							|  |  |  | 					Db_Session::cleanUp($esId); | 
					
						
							|  |  |  | 				} | 
					
						
							|  |  |  | 				 | 
					
						
							| 
									
										
										
										
											2013-10-01 18:23:37 +03:00
										 |  |  | 				$view->touch($path); | 
					
						
							| 
									
										
										
										
											2013-09-11 16:58:01 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 			\OCP\JSON::success(); | 
					
						
							|  |  |  | 			exit(); | 
					
						
							|  |  |  | 		} catch (\Exception $e){ | 
					
						
							|  |  |  | 			Helper::warnLog('Saving failed. Reason:' . $e->getMessage()); | 
					
						
							|  |  |  | 			\OCP\JSON::error(array('message'=>$e->getMessage())); | 
					
						
							|  |  |  | 			exit(); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	public static function info(){ | 
					
						
							|  |  |  | 		self::preDispatch(); | 
					
						
							|  |  |  | 		$items = @$_POST['items']; | 
					
						
							|  |  |  | 		$info = array(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if (is_array($items)){ | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 			$session = new Db_Session(); | 
					
						
							|  |  |  | 			$info = $session->getInfoByFileId($items); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		\OCP\JSON::success(array( | 
					
						
							|  |  |  | 			"info" => $info | 
					
						
							|  |  |  | 		)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	public static function listAll(){ | 
					
						
							|  |  |  | 		self::preDispatch(); | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 		$session = new Db_Session(); | 
					
						
							|  |  |  | 		$sessions = $session->getCollection(); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$preparedSessions = array_map( | 
					
						
							|  |  |  | 				function($x){ | 
					
						
							|  |  |  | 					return ($x['es_id']); | 
					
						
							|  |  |  | 				}, $sessions | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 		\OCP\JSON::success(array( | 
					
						
							|  |  |  | 			"session_list" => $preparedSessions | 
					
						
							|  |  |  | 		)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public static function listAllHtml(){ | 
					
						
							|  |  |  | 		self::preDispatch(); | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 		$session = new Db_Session(); | 
					
						
							|  |  |  | 		$sessions = $session->getCollection(); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		$preparedSessions = array_map( | 
					
						
							|  |  |  | 				function($x){ | 
					
						
							|  |  |  | 					return ($x['es_id']); | 
					
						
							|  |  |  | 				}, $sessions | 
					
						
							|  |  |  | 		); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$invites = Invite::getAllInvites(); | 
					
						
							|  |  |  | 		if (!is_array($invites)){ | 
					
						
							|  |  |  | 			$invites = array(); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		$tmpl = new \OCP\Template('documents', 'part.sessions', ''); | 
					
						
							|  |  |  | 		$tmpl->assign('invites', $invites); | 
					
						
							|  |  |  | 		$tmpl->assign('sessions', $sessions); | 
					
						
							|  |  |  | 		echo $tmpl->fetchPage(); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |