<commit_before><?php

/**
 * ownCloud - bookmarks
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2014
 */

namespace OCA\Bookmarks\Controller\Rest;

use \OCP\IRequest;
use \OCP\AppFramework\ApiController;
use \OCP\AppFramework\Http\JSONResponse;
use \OCP\AppFramework\Http;
use \OCP\IDb;
use \OCA\Bookmarks\Controller\Lib\Bookmarks;
use \OCA\Bookmarks\Controller\Lib\ExportResponse;

class BookmarkController extends ApiController {

	private $userId;
	private $db;

	public function __construct($appName, IRequest $request, $userId, IDb $db) {
		parent::__construct($appName, $request);
		$this->userId = $userId;
		$this->db = $db;
		$this->request = $request;
	}

	/**
	 * @NoAdminRequired
	 */
	public function legacyGetBookmarks($type = "bookmark", $tag = '', $page = 0, $sort = "bookmarks_sorting_recent") {
		return $this->getBookmarks($type, $tag, $page, $sort);
	}

	/**
	 * @NoAdminRequired
	 */
	public function getBookmarks($type = "bookmark", $tag = '', $page = 0, $sort = "bookmarks_sorting_recent") {

		if ($type == 'rel_tags') {
			$tags = Bookmarks::analyzeTagRequest($tag);
			$qtags = Bookmarks::findTags($this->userId, $this->db, $tags);
			return new JSONResponse(array('data' => $qtags, 'status' => 'success'));
		} else { // type == bookmark
			$filterTag = Bookmarks::analyzeTagRequest($tag);

			$offset = $page * 10;

			if ($sort == 'bookmarks_sorting_clicks') {
				$sqlSortColumn = 'clickcount';
			} else {
				$sqlSortColumn = 'lastmodified';
			}
			$bookmarks = Bookmarks::findBookmarks($this->userId, $this->db, $offset, $sqlSortColumn, $filterTag, true);
			return new JSONResponse(array('data' => $bookmarks, 'status' => 'success'));
		}
	}

	/**
	 * @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
	 * 
	 * @param int $id
	 * @param bool $is_public Description
	 * @return \OCP\AppFramework\Http\TemplateResponse
	 */
	//TODO id vs record_id?
	public function legacyEditBookmark($id = null, $url = "", $item = array(), $title = "", $is_public = false, $record_id = null, $description = "") {
		if ($id == null) {
			return $this->newBookmark($url, $item, false, $title, $is_public, $description);<commit_msg>Fix http / https protocol requirement<commit_after><?php

/**
 * ownCloud - bookmarks
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @copyright <NAME> 2014
 */

namespace OCA\Bookmarks\Controller\Rest;

use \OCP\IRequest;
use \OCP\AppFramework\ApiController;
use \OCP\AppFramework\Http\JSONResponse;
use \OCP\AppFramework\Http;
use \OCP\IDb;
use \OCA\Bookmarks\Controller\Lib\Bookmarks;
use \OCA\Bookmarks\Controller\Lib\ExportResponse;

class BookmarkController extends ApiController {

	private $userId;
	private $db;

	public function __construct($appName, IRequest $request, $userId, IDb $db) {
		parent::__construct($appName, $request);
		$this->userId = $userId;
		$this->db = $db;
		$this->request = $request;
	}

	/**
	 * @NoAdminRequired
	 */
	public function legacyGetBookmarks($type = "bookmark", $tag = '', $page = 0, $sort = "bookmarks_sorting_recent") {
		return $this->getBookmarks($type, $tag, $page, $sort);
	}

	/**
	 * @NoAdminRequired
	 */
	public function getBookmarks($type = "bookmark", $tag = '', $page = 0, $sort = "bookmarks_sorting_recent") {

		if ($type == 'rel_tags') {
			$tags = Bookmarks::analyzeTagRequest($tag);
			$qtags = Bookmarks::findTags($this->userId, $this->db, $tags);
			return new JSONResponse(array('data' => $qtags, 'status' => 'success'));
		} else { // type == bookmark
			$filterTag = Bookmarks::analyzeTagRequest($tag);

			$offset = $page * 10;

			if ($sort == 'bookmarks_sorting_clicks') {
				$sqlSortColumn = 'clickcount';
			} else {
				$sqlSortColumn = 'lastmodified';
			}
			$bookmarks = Bookmarks::findBookmarks($this->userId, $this->db, $offset, $sqlSortColumn, $filterTag, true);
			return new JSONResponse(array('data' => $bookmarks, '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 = '/^[hs]{0,1}[tf]{0,1}tp[s]{0,1}\:\/\//i';
			// if not (allowed) protocol is given, assume http and https (and fetch both)
			if (! preg_match($protocols, $url)) {
				// append https to url and fetch it
				$url_https = 'https://' . $url;
				$datas_https = Bookmarks::getURLMetadata($url_https);
				// append http to url and fetch it
				$url_http = 'http://' . $url;
				$datas_http = Bookmarks::getURLMetadata($url_http);
			}
			// adopt https if it works (switch to http if it doesn't)
			if (isset($datas_https['title'])) { // test if https works
				$title = $datas_https['title'];
				$url = $url_https;
			} elseif (isset($datas_http['title'])) { // otherwise test http for results
				$title = $datas_http['title'];
				$url = $url_http;
			}
		}

		// Check if it is a valid URL (after adding http(s) prefix)
		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();

		$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
	 * 
	 * @param int $id
	 * @param bool $is_public Description
	 * @return \OCP\AppFramework\Http\TemplateResponse
	 */
	//TODO id vs record_id?
	public function legacyEditBookmark($id = null, $url = "", $item = array(), $title = "", $is_public = false, $record_id = null, $description = "") {
		if ($id == null) {
			return $this->newBookmark($url, $item, false, $title, $is_public, $description);
{'fraction_non_alphanumeric': 0.18118826477959604, 'fraction_numerical': 0.0041279669762641896, 'mean_word_length': 4.151100987091875, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 51, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4654062', 'n_tokens_mistral': 2515, 'n_tokens_neox': 2293, 'n_words': 544}