| 
									
										
										
										
											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){ | 
					
						
							| 
									
										
										
										
											2014-04-09 17:48:48 +03:00
										 |  |  | 		self::preDispatchGuest(); | 
					
						
							| 
									
										
										
										
											2014-04-09 16:57:42 +03:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		$uid = Helper::getArrayValueByKey($_POST, 'name'); | 
					
						
							| 
									
										
										
										
											2014-05-14 00:06:10 +03:00
										 |  |  | 		$uid = substr($uid, 0, 16); | 
					
						
							| 
									
										
										
										
											2014-04-09 16:57:42 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:57:42 +03:00
										 |  |  | 			$token = Helper::getArrayValueByKey($args, 'token'); | 
					
						
							| 
									
										
										
										
											2014-04-10 21:23:26 +03:00
										 |  |  | 			$file = File::getByShareToken($token); | 
					
						
							| 
									
										
										
										
											2014-09-08 22:05:31 +03:00
										 |  |  | 			if ($file->isPasswordProtected() && !$file->checkPassword('')){ | 
					
						
							|  |  |  | 				throw new \Exception('Not authorized'); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-08-04 21:33:00 +03:00
										 |  |  | 			$session = Db\Session::start($uid, $file); | 
					
						
							| 
									
										
										
										
											2014-04-09 17:48:48 +03:00
										 |  |  | 			\OCP\JSON::success($session); | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 		} 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(); | 
					
						
							| 
									
										
										
										
											2014-04-10 21:23:26 +03:00
										 |  |  | 		$fileId = Helper::getArrayValueByKey($args, 'file_id'); | 
					
						
							| 
									
										
										
										
											2013-12-05 01:02:08 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											2014-04-09 16:57:42 +03:00
										 |  |  | 			$view = \OC\Files\Filesystem::getView(); | 
					
						
							|  |  |  | 			$path = $view->getPath($fileId); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			if ($view->isUpdatable($path)) { | 
					
						
							| 
									
										
										
										
											2014-04-10 21:23:26 +03:00
										 |  |  | 				$file = new File($fileId); | 
					
						
							| 
									
										
										
										
											2014-08-04 20:51:50 +03:00
										 |  |  | 				$session = Db\Session::start($uid, $file); | 
					
						
							| 
									
										
										
										
											2014-04-09 17:48:48 +03:00
										 |  |  | 				\OCP\JSON::success($session); | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2014-08-12 19:32:32 +03:00
										 |  |  | 				$info = $view->getFileInfo($path); | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 				\OCP\JSON::success(array( | 
					
						
							| 
									
										
										
										
											2014-04-09 16:57:42 +03:00
										 |  |  | 					'permissions' => $info['permissions'], | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 					'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
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											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']; | 
					
						
							| 
									
										
										
										
											2014-08-04 21:00:58 +03:00
										 |  |  | 			$currentMember = new Db\Member(); | 
					
						
							| 
									
										
										
										
											2014-04-11 23:12:23 +03:00
										 |  |  | 			$currentMember->load($memberId); | 
					
						
							|  |  |  | 			if (is_null($currentMember->getIsGuest()) || $currentMember->getIsGuest()){ | 
					
						
							| 
									
										
										
										
											2014-05-14 00:06:10 +03:00
										 |  |  | 				self::preDispatchGuest(); | 
					
						
							| 
									
										
										
										
											2014-04-10 20:14:45 +03:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2014-05-14 00:06:10 +03:00
										 |  |  | 				$uid = self::preDispatch(); | 
					
						
							| 
									
										
										
										
											2014-04-10 20:14:45 +03:00
										 |  |  | 			} | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			//check if member belongs to the session
 | 
					
						
							| 
									
										
										
										
											2014-04-11 23:12:23 +03:00
										 |  |  | 			if ($esId != $currentMember->getEsId()){ | 
					
						
							| 
									
										
										
										
											2014-04-10 20:14:45 +03:00
										 |  |  | 				throw new \Exception($memberId . ' does not belong to session ' . $esId); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2014-05-14 00:06:10 +03:00
										 |  |  | 			// Extra info for future usage
 | 
					
						
							|  |  |  | 			// $sessionRevision = Helper::getArrayValueByKey($_SERVER, 'HTTP_WEBODF_SESSION_REVISION');
 | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-08-04 20:51:50 +03:00
										 |  |  | 			$session = new Db\Session(); | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 			$session->load($esId); | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2014-04-11 23:12:23 +03:00
										 |  |  | 			if (!$session->getEsId()){ | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 				throw new \Exception('Session does not exist'); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-04-11 23:12:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-03-18 00:35:09 +03:00
										 |  |  | 			try { | 
					
						
							| 
									
										
										
										
											2014-04-11 23:12:23 +03:00
										 |  |  | 				if ($currentMember->getIsGuest()){ | 
					
						
							|  |  |  | 					$file = File::getByShareToken($currentMember->getToken()); | 
					
						
							| 
									
										
										
										
											2014-04-11 00:59:51 +03:00
										 |  |  | 				} else { | 
					
						
							| 
									
										
										
										
											2014-04-11 23:12:23 +03:00
										 |  |  | 					$file = new File($session->getFileId()); | 
					
						
							| 
									
										
										
										
											2014-04-10 21:23:26 +03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				 | 
					
						
							| 
									
										
										
										
											2014-06-26 18:21:51 +03:00
										 |  |  | 				list($view, $path) = $file->getOwnerViewAndPath(true); | 
					
						
							| 
									
										
										
										
											2014-03-18 00:35:09 +03:00
										 |  |  | 			} catch (\Exception $e){ | 
					
						
							|  |  |  | 				//File was deleted or unshared. We need to save content as new file anyway
 | 
					
						
							|  |  |  | 				//Sorry, but for guests it would be lost :(
 | 
					
						
							| 
									
										
										
										
											2014-05-14 00:06:10 +03:00
										 |  |  | 				if (isset($uid)){ | 
					
						
							|  |  |  | 					$view = new \OC\Files\View('/' . $uid . '/files'); | 
					
						
							| 
									
										
										
										
											2014-03-18 00:35:09 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2014-05-14 00:06:10 +03:00
										 |  |  | 					$dir = \OCP\Config::getUserValue(\OCP\User::getUser(), 'documents', 'save_path', ''); | 
					
						
							|  |  |  | 					$path = Helper::getNewFileName($view, $dir . 'New Document.odt'); | 
					
						
							|  |  |  | 				} | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2014-08-04 21:00:58 +03:00
										 |  |  | 			$member = new Db\Member(); | 
					
						
							| 
									
										
										
										
											2013-10-31 20:24:55 +03:00
										 |  |  | 			$members = $member->getActiveCollection($esId); | 
					
						
							|  |  |  | 			$memberIds = array_map( | 
					
						
							|  |  |  | 				function($x){ | 
					
						
							|  |  |  | 					return ($x['member_id']); | 
					
						
							|  |  |  | 				}, | 
					
						
							|  |  |  | 				$members | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 			 | 
					
						
							|  |  |  | 			// Active users except current user
 | 
					
						
							|  |  |  | 			$memberCount = count($memberIds) - 1; | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2014-04-11 00:59:51 +03: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; | 
					
						
							|  |  |  | 				 | 
					
						
							| 
									
										
										
										
											2014-04-11 23:12:23 +03:00
										 |  |  | 				if (!Helper::isVersionsEnabled() && $currentHash !== $session->getGenesisHash()){ | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2014-08-04 20:51:50 +03:00
										 |  |  | 					Db\Session::cleanUp($esId); | 
					
						
							| 
									
										
										
										
											2013-10-31 20:24:55 +03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 				 | 
					
						
							| 
									
										
										
										
											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(); | 
					
						
							|  |  |  | 		} catch (\Exception $e){ | 
					
						
							|  |  |  | 			Helper::warnLog('Saving failed. Reason:' . $e->getMessage()); | 
					
						
							| 
									
										
										
										
											2014-03-28 20:25:37 +03:00
										 |  |  | 			//\OCP\JSON::error(array('message'=>$e->getMessage()));
 | 
					
						
							|  |  |  | 			\OC_Response::setStatus(500); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-03-28 20:25:37 +03:00
										 |  |  | 		exit(); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |