| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace OCA\Bookmarks\Controller\Rest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use \OCP\AppFramework\ApiController; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use \OCP\IRequest; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use \OCP\IDb; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use \OCP\AppFramework\Http\JSONResponse; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use \OC\User\Manager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use OCA\Bookmarks\Controller\Lib\Bookmarks; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | use OCP\Util; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | class PublicController extends ApiController { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | 	private $db; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | 	private $userManager; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 17 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 18 | 3 |  | 	public function __construct($appName, IRequest $request, IDb $db, Manager $userManager) { | 
            
                                                                        
                            
            
                                    
            
            
                | 19 | 3 |  | 		parent::__construct( | 
            
                                                                        
                            
            
                                    
            
            
                | 20 | 3 |  | 				$appName, $request); | 
            
                                                                        
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 22 | 3 |  | 		$this->db = $db; | 
            
                                                                        
                            
            
                                    
            
            
                | 23 | 3 |  | 		$this->userManager = $userManager; | 
            
                                                                        
                            
            
                                    
            
            
                | 24 | 3 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | 	 * @param string $user | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | 	 * @param string $password | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | 	 * @param array $tags | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  | 	 * @param string $conjunction | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | 	 * @param array $select | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | 	 * @param string $sortby | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	 * @return JSONResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  | 	 * @CORS | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	 * @NoAdminRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	 * @NoCSRFRequired | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  | 	 * @PublicPage | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 3 |  | 	public function returnAsJson($user, $password = null, $tags = array(), $conjunction = "or", $select = null, $sortby = "") { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  | 		if ($user == null || $this->userManager->userExists($user) == false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 | 3 |  | 			return $this->newJsonErrorMessage("User could not be identified"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 		if (!is_array($tags)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  | 			if(is_string($tags) && $tags !== '') { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 				$tags = [ $tags ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 			} else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  | 				$tags = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 			} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 		$public = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  | 		if ($password != null) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  | 			$public = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 		if (!$public && !$this->userManager->checkPassword($user, $password)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  | 			$msg = 'REST API accessed with wrong password'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  | 			Util::writeLog('bookmarks', $msg, Util::WARN); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  | 			return $this->newJsonErrorMessage("Wrong password for user " . $user); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  | 		$attributesToSelect = array('url', 'title'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  | 		if ($select != null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  | 			$attributesToSelect = array_merge($attributesToSelect, $select); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  | 			$attributesToSelect = array_unique($attributesToSelect); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  | 		$output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 		if (count($output) == 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 			$output["status"] = 'error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 			$output["message"] = "No results from this query"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 			return new JSONResponse($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  | 		return new JSONResponse($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * @param string $message | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * @return JSONResponse | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 3 |  | 	public function newJsonErrorMessage($message) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 3 |  | 		$output = array(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 3 |  | 		$output["status"] = 'error'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 | 3 |  | 		$output["message"] = $message; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 | 3 |  | 		return new JSONResponse($output); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 98 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 99 |  |  |  |