/** * @CORS * @NoAdminRequired * @NoCSRFRequired * @PublicPage */ public function returnAsJson($user, $password = null, $tags = array(), $conjunction = "or", $select = null, $sortby = "") { if ($user == null || $this->userManager->userExists($user) == false) { return $this->newJsonErrorMessage("User could not be identified"); } if ($tags[0] == "") { $tags = array(); } $public = true; if ($password != null) { $public = false; } if (!$public && !$this->userManager->checkPassword($user, $password)) { $msg = 'REST API accessed with wrong password'; \OCP\Util::writeLog('bookmarks', $msg, \OCP\Util::WARN); return $this->newJsonErrorMessage("Wrong password for user " . $user); } $attributesToSelect = array('url', 'title'); if ($select != null) { $attributesToSelect = array_merge($attributesToSelect, $select); $attributesToSelect = array_unique($attributesToSelect); } $output = Bookmarks::findBookmarks($user, $this->db, 0, $sortby, $tags, true, -1, $public, $attributesToSelect, $conjunction); if (count($output) == 0) { $output["status"] = 'error'; $output["message"] = "No results from this query"; return new JSONResponse($output); } return new JSONResponse($output); }
function testPublicQuery() { Bookmarks::addBookmark($this->userid, $this->db, "http://www.golem.de", "Golem", array("four"), "PublicNoTag", true); Bookmarks::addBookmark($this->userid, $this->db, "http://www.9gag.com", "9gag", array("two", "three"), "PublicTag", true); $output = $this->publicController->returnAsJson($this->userid); $data = $output->getData(); $this->assertEquals(2, count($data)); $this->cleanDB(); }
/** * @NoAdminRequired */ public function fullTags() { header("Cache-Control: no-cache, must-revalidate"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); $qtags = Bookmarks::findTags($this->userId, $this->db, array(), 0, 400); $tags = array(); foreach ($qtags as $tag) { $tags[] = $tag['tag']; } return new JSONResponse($tags); }
/** * @NoAdminRequired * @NoCSRFRequired */ public function bookmarklet($url = "", $title = "") { $bookmarkExists = Bookmarks::bookmarkExists($url, $this->userId, $this->db); $description = ""; if ($bookmarkExists != false) { $bookmark = Bookmarks::findUniqueBookmark($bookmarkExists, $this->userId, $this->db); $description = $bookmark['description']; } $params = array('url' => $url, 'title' => $title, 'description' => $description, 'bookmarkExists' => $bookmarkExists); return new TemplateResponse('bookmarks', 'addBookmarklet', $params); // templates/main.php }
function search($query) { $results = array(); if (substr_count($query, ' ') > 0) { $search_words = explode(' ', $query); } else { $search_words = $query; } $db = \OC::$server->getDb(); $user = \OCP\User::getUser(); $bookmarks = Bookmarks::findBookmarks($user, $db, 0, 'id', $search_words, false); $l = new \OC_l10n('bookmarks'); //resulttype can't be localized, javascript relies on that type foreach ($bookmarks as $bookmark) { $results[] = new \OC_Search_Result($bookmark['title'], $bookmark['title'], $bookmark['url'], (string) $l->t('Bookm.')); } return $results; }
function testGetURLMetadata() { $config = $this->getMockBuilder('\\OCP\\IConfig')->disableOriginalConstructor()->getMock(); $certificateManager = $this->getMock('\\OCP\\ICertificateManager'); $httpHelperMock = $this->getMockBuilder('\\OC\\HTTPHelper')->setConstructorArgs(array($config, $certificateManager))->getMock(); $returnAmazonDe = file_get_contents(__DIR__ . '/res/amazonHtml.file'); $returnGolemDe = file_get_contents(__DIR__ . '/res/golemHtml.file'); $httpHelperMock->expects($this->any())->method('getUrlContent')->with($this->anything())->will($this->onConsecutiveCalls($returnAmazonDe, $returnGolemDe)); $this->registerHttpHelper($httpHelperMock); $metadataAmazon = Bookmarks::getURLMetadata('amazonHtml'); $this->assertTrue($metadataAmazon['url'] == 'amazonHtml'); $this->assertTrue(strpos($metadataAmazon['title'], 'ü') !== false); $metadataGolem = Bookmarks::getURLMetadata('golemHtml'); $this->assertTrue($metadataGolem['url'] == 'golemHtml'); $this->assertTrue(strpos($metadataGolem['title'], 'für') == false); }
/** @NoAdminRequired * * @return \OCP\AppFramework\Http\JSONResponse */ public function exportBookmark() { $file = <<<EOT <!DOCTYPE NETSCAPE-Bookmark-file-1> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"> <!-- This is an automatically generated file. It will be read and overwritten. Do Not Edit! --> <TITLE>Bookmarks</TITLE> <H1>Bookmarks</H1> <DL><p> EOT; $bookmarks = Bookmarks::findBookmarks($this->userId, $this->db, 0, 'id', array(), true, -1); foreach ($bookmarks as $bm) { $title = $bm['title']; if (trim($title) === '') { $url_parts = parse_url($bm['url']); $title = isset($url_parts['host']) ? OCA\Bookmarks\Controller\Lib\Helper::getDomainWithoutExt($url_parts['host']) : $bm['url']; } $file .= '<DT><A HREF="' . \OC_Util::sanitizeHTML($bm['url']) . '" TAGS="' . implode(',', \OC_Util::sanitizeHTML($bm['tags'])) . '">'; $file .= htmlspecialchars($title, ENT_QUOTES, 'UTF-8') . '</A>'; if ($bm['description']) { $file .= '<DD>' . htmlspecialchars($bm['description'], ENT_QUOTES, 'UTF-8'); } $file .= "\n"; } return new ExportResponse($file); }
/** * @CORS * @NoAdminRequired * @NoCSRFRequired */ public function returnDeleteAsJson($id) { $user = \OCP\User::getUser(); $output = Bookmarks::deleteUrl($user, $this->db, $id); if (!$output) { $output = array(); $output["status"] = 'error'; $output["message"] = "Cannot delete bookmark"; return new JSONResponse($output); } else { $output = array(); $output["status"] = 'success'; $output["message"] = "Bookmark deleted"; return new JSONResponse($output); } }
/** * @brief Finds all bookmarks, matching the filter * @param $userid UserId * @param IDb $db Database Interface * @param int $offset offset * @param string $sqlSortColumn result with this column * @param string|array $filters filters can be: empty -> no filter, a string -> filter this, a string array -> filter for all strings * @param bool $filterTagOnly true, filter affects only tags, else filter affects url, title and tags * @param int $limit limit of items to return (default 10) if -1 or false then all items are returned * @param bool $public check if only public bookmarks should be returned * @param array $requestedAttributes select all the attributes that should be returned. default is * + tags * @param string $tagFilterConjunction select wether the filterTagOnly should filter with an AND or an OR conjunction * @return Collection of specified bookmarks */ public static function findBookmarks($userid, IDb $db, $offset, $sqlSortColumn, $filters, $filterTagOnly, $limit = 10, $public = false, $requestedAttributes = null, $tagFilterConjunction = "and") { $CONFIG_DBTYPE = \OCP\Config::getSystemValue('dbtype', 'sqlite'); if (is_string($filters)) { $filters = array($filters); } $toSelect = '*'; $tableAttributes = array('id', 'url', 'title', 'user_id', 'description', 'public', 'added', 'lastmodified', 'clickcount'); $returnTags = true; if ($requestedAttributes != null) { $key = array_search('tags', $requestedAttributes); if ($key == false) { $returnTags = false; } else { unset($requestedAttributes[$key]); } $toSelect = implode(",", array_intersect($tableAttributes, $requestedAttributes)); } if ($CONFIG_DBTYPE == 'pgsql') { $sql = "SELECT " . $toSelect . " FROM (SELECT *, (select array_to_string(array_agg(`tag`),',')\n\t\t\t\tfrom `*PREFIX*bookmarks_tags` where `bookmark_id` = `b2`.`id`) as `tags`\n\t\t\t\tFROM `*PREFIX*bookmarks` `b2`\n\t\t\t\tWHERE `user_id` = ? ) as `b` WHERE true "; } else { $sql = "SELECT " . $toSelect . ", (SELECT GROUP_CONCAT(`tag`) from `*PREFIX*bookmarks_tags`\n\t\t\t\tWHERE `bookmark_id` = `b`.`id`) as `tags`\n\t\t\t\tFROM `*PREFIX*bookmarks` `b`\n\t\t\t\tWHERE `user_id` = ? "; } $params = array($userid); if ($public) { $sql .= ' AND public = 1 '; } if (count($filters) > 0) { Bookmarks::findBookmarksBuildFilter($sql, $params, $filters, $filterTagOnly, $tagFilterConjunction, $CONFIG_DBTYPE); } if (!in_array($sqlSortColumn, $tableAttributes)) { $sqlSortColumn = 'lastmodified'; } $sql .= " ORDER BY " . $sqlSortColumn . " DESC "; if ($limit == -1 || $limit === false) { $limit = null; $offset = null; } $query = $db->prepareQuery($sql, $limit, $offset); $results = $query->execute($params)->fetchAll(); $bookmarks = array(); foreach ($results as $result) { if ($returnTags) { $result['tags'] = explode(',', $result['tags']); } else { unset($result['tags']); } $bookmarks[] = $result; } return $bookmarks; }
function testGetURLMetadata() { $config = $this->getMockBuilder('\\OCP\\IConfig')->disableOriginalConstructor()->getMock(); $amazonResponse = $this->getMock('OCP\\Http\\Client\\IResponse'); $amazonResponse->expects($this->once())->method('getBody')->will($this->returnValue(file_get_contents(__DIR__ . '/res/amazonHtml.file'))); $amazonResponse->expects($this->once())->method('getHeader')->with('Content-Type')->will($this->returnValue('')); $golemResponse = $this->getMock('OCP\\Http\\Client\\IResponse'); $golemResponse->expects($this->once())->method('getBody')->will($this->returnValue(file_get_contents(__DIR__ . '/res/golemHtml.file'))); $golemResponse->expects($this->once())->method('getHeader')->with('Content-Type')->will($this->returnValue('text/html; charset=UTF-8')); $clientMock = $this->getMock('OCP\\Http\\Client\\IClient'); $clientMock->expects($this->exactly(2))->method('get')->will($this->returnCallback(function ($page) use($amazonResponse, $golemResponse) { if ($page === 'amazonHtml') { return $amazonResponse; } else { if ($page === 'golemHtml') { return $golemResponse; } } })); $clientServiceMock = $this->getMock('OCP\\Http\\Client\\IClientService'); $clientServiceMock->expects($this->any())->method('newClient')->will($this->returnValue($clientMock)); $this->registerHttpService($clientServiceMock); $metadataAmazon = Bookmarks::getURLMetadata('amazonHtml'); $this->assertTrue($metadataAmazon['url'] == 'amazonHtml'); $this->assertTrue(strpos($metadataAmazon['title'], 'ü') !== false); $metadataGolem = Bookmarks::getURLMetadata('golemHtml'); $this->assertTrue($metadataGolem['url'] == 'golemHtml'); $this->assertTrue(strpos($metadataGolem['title'], 'für') == false); }