/** * @NoAdminRequired */ public function newBookmark($url = "", $item = array(), $from_own = 0, $title = "", $is_public = false, $description = "") { // Check if it is a valid URL if (filter_var($url, FILTER_VALIDATE_URL) === FALSE) { return new JSONResponse(array('status' => 'error'), Http::STATUS_BAD_REQUEST); } $tags = isset($item['tags']) ? $item['tags'] : array(); if ($from_own == 0) { $datas = Bookmarks::getURLMetadata($url); if (isset($datas['title'])) { $title = $datas['title']; } } $id = Bookmarks::addBookmark($this->userId, $this->db, $url, $title, $tags, $description, $is_public); $bm = Bookmarks::findUniqueBookmark($id, $this->userId, $this->db); return new JSONResponse(array('item' => $bm, 'status' => 'success')); }
/** * @NoAdminRequired */ public function newBookmark($url = "", $item = array(), $from_own = 0, $title = "", $is_public = false, $description = "") { if ($from_own == 0) { // allow only http(s) and (s)ftp $protocols = '/^(https?|s?ftp)\\:\\/\\//i'; if (preg_match($protocols, $url)) { $data = Bookmarks::getURLMetadata($url); // if not (allowed) protocol is given, assume http and https (and fetch both) } else { // append https to url and fetch it $url_https = 'https://' . $url; $data_https = Bookmarks::getURLMetadata($url_https); // append http to url and fetch it $url_http = 'http://' . $url; $data_http = Bookmarks::getURLMetadata($url_http); } if ($title === '' && isset($data['title'])) { // prefer original url if working $title = $data['title']; //url remains unchanged } elseif (isset($data_https['title'])) { // test if https works $title = $title === '' ? $data_https['title'] : $title; $url = $url_https; } elseif (isset($data_http['title'])) { // otherwise test http for results $title = $title === '' ? $data_http['title'] : $title; $url = $url_http; } } // Check if it is a valid URL (after adding http(s) prefix) $urlData = parse_url($url); if ($urlData === false || !isset($urlData['scheme']) || !isset($urlData['host'])) { return new JSONResponse(array('status' => 'error'), Http::STATUS_BAD_REQUEST); } $tags = isset($item['tags']) ? $item['tags'] : array(); $id = Bookmarks::addBookmark($this->userId, $this->db, $url, $title, $tags, $description, $is_public); $bm = Bookmarks::findUniqueBookmark($id, $this->userId, $this->db); return new JSONResponse(array('item' => $bm, 'status' => 'success')); }
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); }
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); }