| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * ownCloud - Documents App | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * @author Victor Dubiniuk | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  |  * @copyright 2014 Victor Dubiniuk victor.dubiniuk@gmail.com | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  |  * | 
					
						
							|  |  |  |  * This file is licensed under the Affero General Public License version 3 or | 
					
						
							|  |  |  |  * later. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | namespace OCA\Documents\Controller; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | use \OCP\AppFramework\Controller; | 
					
						
							|  |  |  | use \OCP\IRequest; | 
					
						
							| 
									
										
										
										
											2014-10-28 20:21:27 +03:00
										 |  |  | use \OCP\AppFramework\Http; | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | use \OCP\AppFramework\Http\JSONResponse; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 20:21:27 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | use \OCA\Documents\Db; | 
					
						
							|  |  |  | use \OCA\Documents\File; | 
					
						
							|  |  |  | use \OCA\Documents\Helper; | 
					
						
							|  |  |  | use OCA\Documents\Filter; | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 20:21:27 +03:00
										 |  |  | class BadRequestException extends \Exception { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	protected $body = ""; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public function setBody($body){ | 
					
						
							|  |  |  | 		$this->body = $body; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	public function getBody(){ | 
					
						
							|  |  |  | 		return $this->body; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | class SessionController extends Controller{ | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 	protected $uid; | 
					
						
							| 
									
										
										
										
											2014-10-28 22:38:08 +03:00
										 |  |  | 	protected $logger; | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-28 22:38:08 +03:00
										 |  |  | 	public function __construct($appName, IRequest $request, $logger, $uid){ | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 		parent::__construct($appName, $request); | 
					
						
							|  |  |  | 		$this->uid = $uid; | 
					
						
							| 
									
										
										
										
											2014-10-28 22:38:08 +03:00
										 |  |  | 		$this->logger = $logger; | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @NoAdminRequired | 
					
						
							|  |  |  | 	 * @PublicPage | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public function joinAsGuest($token, $name){ | 
					
						
							|  |  |  | 		$uid = substr($name, 0, 16); | 
					
						
							| 
									
										
										
										
											2014-04-09 16:57:42 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											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-10-23 23:08:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 			$response = array_merge(  | 
					
						
							|  |  |  | 				Db\Session::start($uid, $file), | 
					
						
							|  |  |  | 				array('status'=>'success') | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 		} catch (\Exception $e){ | 
					
						
							| 
									
										
										
										
											2014-10-28 22:38:08 +03:00
										 |  |  | 			$this->logger->warning('Starting a session failed. Reason: ' . $e->getMessage(), array('app' => $this->appName)); | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 			$response = array ( | 
					
						
							|  |  |  | 				'status'=>'error' | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2013-12-05 01:02:08 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 		return $response; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @NoAdminRequired | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	public function joinAsUser($fileId){ | 
					
						
							| 
									
										
										
										
											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-10-23 23:08:55 +03:00
										 |  |  | 				$response = Db\Session::start($this->uid, $file); | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 			} else { | 
					
						
							| 
									
										
										
										
											2014-08-12 19:32:32 +03:00
										 |  |  | 				$info = $view->getFileInfo($path); | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 				$response = array( | 
					
						
							| 
									
										
										
										
											2014-04-09 16:57:42 +03:00
										 |  |  | 					'permissions' => $info['permissions'], | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 					'id' => $fileId | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 				); | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 			$response = array_merge(  | 
					
						
							|  |  |  | 					$response, | 
					
						
							|  |  |  | 					array('status'=>'success') | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 		} catch (\Exception $e){ | 
					
						
							| 
									
										
										
										
											2014-10-28 22:38:08 +03:00
										 |  |  | 			$this->logger->warning('Starting a session failed. Reason: ' . $e->getMessage(), array('app' => $this->appName)); | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 			$response = array ( | 
					
						
							|  |  |  | 				'status'=>'error' | 
					
						
							|  |  |  | 			); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 		 | 
					
						
							|  |  |  | 		return $response; | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-12-19 01:14:32 +03:00
										 |  |  | 	 | 
					
						
							| 
									
										
										
										
											2014-10-28 20:21:27 +03:00
										 |  |  | 	public function poll($command, $args){ | 
					
						
							|  |  |  | 		$response = new JSONResponse(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		try{ | 
					
						
							|  |  |  | 			$esId = isset($args['es_id']) ? $args['es_id'] : null; | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 			$session = new Db\Session(); | 
					
						
							|  |  |  | 			$session->load($esId); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 			$memberId = isset($args['member_id']) ? $args['member_id'] : null; | 
					
						
							|  |  |  | 			$member = new Db\Member(); | 
					
						
							|  |  |  | 			$member->load($memberId); | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 			if (!$member->getIsGuest()){ | 
					
						
							|  |  |  | 				\OCP\JSON::checkLoggedIn(); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			try { | 
					
						
							|  |  |  | 				$file = new File($session->getFileId()); | 
					
						
							|  |  |  | 			} catch (\Exception $e){ | 
					
						
							| 
									
										
										
										
											2014-10-28 22:38:08 +03:00
										 |  |  | 				$this->logger->warning('Error. Session no longer exists. ' . $e->getMessage(), array('app' => $this->appName)); | 
					
						
							| 
									
										
										
										
											2014-10-28 20:21:27 +03:00
										 |  |  | 				$ex = new BadRequestException(); | 
					
						
							|  |  |  | 				$ex->setBody( | 
					
						
							|  |  |  | 						implode(',', $this->request->getParams()) | 
					
						
							|  |  |  | 				); | 
					
						
							|  |  |  | 				throw $ex; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 			switch ($command){ | 
					
						
							|  |  |  | 				case 'sync_ops': | 
					
						
							|  |  |  | 					$seqHead = (string) isset($args['seq_head']) ? $args['seq_head'] : null; | 
					
						
							|  |  |  | 					if (!is_null($seqHead)){ | 
					
						
							|  |  |  | 						$ops = isset($args['client_ops']) ? $args['client_ops'] : null; | 
					
						
							|  |  |  | 						$hasOps = is_array($ops) && count($ops)>0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						$op = new Db\Op(); | 
					
						
							|  |  |  | 						$currentHead = $op->getHeadSeq($esId); | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 						try { | 
					
						
							|  |  |  | 							$member->updateActivity($memberId); | 
					
						
							|  |  |  | 						} catch (\Exception $e){ | 
					
						
							|  |  |  | 							//Db error. Not critical
 | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 						// TODO handle the case ($currentHead == "") && ($seqHead != "")
 | 
					
						
							|  |  |  | 						if ($seqHead == $currentHead) { | 
					
						
							|  |  |  | 							// matching heads
 | 
					
						
							|  |  |  | 							if ($hasOps) { | 
					
						
							|  |  |  | 								// incoming ops without conflict
 | 
					
						
							|  |  |  | 								// Add incoming ops, respond with a new head
 | 
					
						
							|  |  |  | 								$newHead = Db\Op::addOpsArray($esId, $memberId, $ops); | 
					
						
							|  |  |  | 								$response->setData(  | 
					
						
							|  |  |  | 										array( | 
					
						
							|  |  |  | 											'result' => 'added', | 
					
						
							|  |  |  | 											'head_seq' => $newHead ? $newHead : $currentHead | 
					
						
							|  |  |  | 										) | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} else { | 
					
						
							|  |  |  | 								// no incoming ops (just checking for new ops...)
 | 
					
						
							|  |  |  | 								$response->setData(  | 
					
						
							|  |  |  | 										array( | 
					
						
							|  |  |  | 											'result' => 'new_ops', | 
					
						
							|  |  |  | 											'ops' => array(), | 
					
						
							|  |  |  | 											'head_seq' => $currentHead | 
					
						
							|  |  |  | 										) | 
					
						
							|  |  |  | 								); | 
					
						
							|  |  |  | 							} | 
					
						
							|  |  |  | 						} else { // HEADs do not match
 | 
					
						
							|  |  |  | 								$response->setData(  | 
					
						
							|  |  |  | 										array( | 
					
						
							|  |  |  | 											'result' => $hasOps ? 'conflict' : 'new_ops', | 
					
						
							|  |  |  | 											'ops' => $op->getOpsAfterJson($esId, $seqHead), | 
					
						
							|  |  |  | 											'head_seq' => $currentHead, | 
					
						
							|  |  |  | 											) | 
					
						
							|  |  |  | 								);			 | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 				 | 
					
						
							|  |  |  | 						$inactiveMembers = $member->updateByTimeout($esId); | 
					
						
							|  |  |  | 						foreach ($inactiveMembers as $inactive){ | 
					
						
							|  |  |  | 							$op->removeCursor($esId, $inactive); | 
					
						
							|  |  |  | 							$op->removeMember($esId, $inactive); | 
					
						
							|  |  |  | 						} | 
					
						
							|  |  |  | 					} else { | 
					
						
							|  |  |  | 						// Error - no seq_head passed
 | 
					
						
							|  |  |  | 						throw new BadRequestException(); | 
					
						
							|  |  |  | 					} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 				default: | 
					
						
							|  |  |  | 					$ex = new BadRequestException(); | 
					
						
							|  |  |  | 					$ex->setBody( | 
					
						
							|  |  |  | 							implode(',', $this->request->getParams()) | 
					
						
							|  |  |  | 					); | 
					
						
							|  |  |  | 					throw $ex; | 
					
						
							|  |  |  | 					break; | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} catch (BadRequestException $e){ | 
					
						
							|  |  |  | 			$response->setStatus(Http::STATUS_BAD_REQUEST); | 
					
						
							|  |  |  | 			$response->setData( | 
					
						
							|  |  |  | 					array('err' => 'bad request:[' . $e->getBody() . ']') | 
					
						
							|  |  |  | 			); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		return $response; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	 | 
					
						
							|  |  |  | 	 | 
					
						
							| 
									
										
										
										
											2013-11-09 17:29:12 +03:00
										 |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 	 * @NoAdminRequired | 
					
						
							|  |  |  | 	 * @PublicPage | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 	 * Store the document content to its origin | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 	public function save(){ | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 		try { | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 			$esId = $this->request->server['HTTP_WEBODF_SESSION_ID']; | 
					
						
							| 
									
										
										
										
											2013-09-27 18:43:10 +03:00
										 |  |  | 			if (!$esId){ | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 				throw new \Exception('Session id can not be empty'); | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 			 | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 			$memberId = $this->request->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); | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 			// $sessionRevision = $this->request->server['HTTP_WEBODF_SESSION_REVISION'];
 | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 			 | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 			//NB ouch! New document content is passed as an input stream content
 | 
					
						
							| 
									
										
										
										
											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-10-23 23:08:55 +03:00
										 |  |  | 				if ($this->uid){ | 
					
						
							|  |  |  | 					$view = new \OC\Files\View('/' . $this->uid . '/files'); | 
					
						
							| 
									
										
										
										
											2014-03-18 00:35:09 +03:00
										 |  |  | 		 | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 					$dir = \OCP\Config::getUserValue($this->uid, 'documents', 'save_path', ''); | 
					
						
							| 
									
										
										
										
											2014-05-14 00:06:10 +03:00
										 |  |  | 					$path = Helper::getNewFileName($view, $dir . 'New Document.odt'); | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 				} else { | 
					
						
							|  |  |  | 					throw $e; | 
					
						
							| 
									
										
										
										
											2014-05-14 00:06:10 +03:00
										 |  |  | 				} | 
					
						
							| 
									
										
										
										
											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
 | 
					
						
							| 
									
										
										
										
											2014-10-28 22:38:08 +03:00
										 |  |  | 					$this->logger->debug('Update hash', array('app' => $this->appName)); | 
					
						
							| 
									
										
										
										
											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
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 			$response = array('status'=>'success'); | 
					
						
							| 
									
										
										
										
											2013-09-03 00:06:21 +03:00
										 |  |  | 		} catch (\Exception $e){ | 
					
						
							| 
									
										
										
										
											2014-10-28 22:38:08 +03:00
										 |  |  | 			$this->logger->warning('Saving failed. Reason:' . $e->getMessage(), array('app' => $this->appName)); | 
					
						
							| 
									
										
										
										
											2014-03-28 20:25:37 +03:00
										 |  |  | 			\OC_Response::setStatus(500); | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 			$response = array(); | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2014-10-23 23:08:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		return $response; | 
					
						
							| 
									
										
										
										
											2013-09-02 19:54:23 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } |