<commit_before><?php

\OCP\User::checkLoggedIn();
\OCP\App::checkAppEnabled('ownnote');


function startsWith($haystack, $needle) {
	return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

function endswith($string, $test) {
	$strlen = strlen($string);
	$testlen = strlen($test);
	if ($testlen > $strlen) return false;
	return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}

function getAnnouncement() {
	$ret = "";
	$url = 'https://raw.githubusercontent.com/Fmstrat/announcements/master/ownnote/announcement.html';
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);
	//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)");
	$result = curl_exec($ch);
	if ($result === FALSE) {
		die('Curl failed: ' . curl_error($ch));
	} else {
		$ret = $result;
	}
	curl_close($ch);
	return $ret;
}

function checkEvernote($folder, $file) {
	$html = "";
	if ($html = \OC\Files\Filesystem::file_get_contents($folder."/".$file)) {
		$DOM = new DOMDocument;
		$DOM->loadHTML($html);
		$items = $DOM->getElementsByTagName('meta');
		$isEvernote = false;
		for ($i = 0; $i < $items->length; $i++) {
			$item = $items->item($i);
			if ($item->hasAttributes()) {
				$attrs = $item->attributes;
				foreach ($attrs as $a => $attr) {
					if ($attr->name == "name") {
						if ($attr->value == "exporter-version") {
							$isEvernote = true;
							continue;
						}
					}
				}
			}
		}
		if ($isEvernote) {
			$items = $DOM->getElementsByTagName('img');
			$isEvernote = false;
			for ($i = 0; $i < $items->length; $i++) {
				$item = $items->item($i);
				if ($item->hasAttributes()) {
					$attrs = $item->attributes;
					foreach ($attrs as $a => $attr) {
						if ($attr->name == "src") {
							$url = $attr->value;
							if (!startsWith($url, "http") && !startsWith($url, "/") && !startsWith($url,"data")) {
								if ($data = \OC\Files\Filesystem::file_get_contents($folder."/".$url)) {
									$type = pathinfo($url, PATHINFO_EXTENSION);
									$base64 = "data:image/".$type.";base64,".base64_encode($data);
									$html = str_replace($url, $base64, $html);
								}
							}
						}
					}
				}
			}
			\OC\Files\Filesystem::file_put_contents($folder."/".$file, $html);
		}
	}
}

function getTimeString($filetime, $now) {
	$difftime = $filetime->diff($now);
	$years = $difftime->y;
	$months = $difftime->m;
	$days = $difftime->d;
	$hours = $difftime->h;
	$minutes = $difftime->i;
	$seconds = $difftime->s;
	$timestring = "";
	if ($timestring == "" && $years == 1) $timestring = "$years year";
	if ($timestring == "" && $years > 0) $timestring = "$years years";
	if ($timestring == "" && $months == 1) $timestring = "$months month";
	if ($timestring == "" && $months > 0) $timestring = "$months months";
	if ($timestring == "" && $days == 1) $timestring = "$days day";
	if ($timestring == "" && $days > 0) $timestring = "$days days";
	if ($timestring == "" && $hours == 1) $timestring = "$hours hour";
	if ($timestring == "" && $hours > 0) $timestring = "$hours hours";
	if ($timestring == "" && $minutes == 1) $timestring = "$minutes minute";
	if ($timestring == "" && $minutes > 0) $timestring = "$minutes minutes";
	if ($timestring == "" && $seconds == 1) $timestring = "$seconds second";
	if ($timestring == "" && $seconds > 0) $timestring = "$seconds seconds";
	return $timestring;
}

function splitContent($str) {
	$maxlength = 2621440; // 5 Megs (2 bytes per character)
	$count = 0;
	$strarray = array();
	while (true) { 
		if (strlen($str) <= $maxlength) { 
			$strarray[$count++] = $str;
			return $strarray;
		} else {
			$strarray[$count++] = substr($str, 0, $maxlength);
			$str = substr($str, $maxlength);
		}
	}
}

function getListing($FOLDER, $showdel) {
	// Get the listing from the database
	$requery = false;
	$uid = \OCP\User::getUser();
	$query = OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name");
	$results = $query->execute(Array($uid))->fetchAll();
	$results2 = $results;
	if ($results)
		foreach($results as $result)
			foreach($results2 as $result2)
				if ($result['id'] != $result2['id'] && $result['name'] == $result2['name'] && $result['grouping'] == $result2['grouping'] && $result['mtime'] == $result2['mtime']) {
					// We have a duplicate that should not exist. Need to remove the offending record first
					$delid = $result['id'];
					if ($result['id'] > $result2['id'])
						$delid = $result2['id'];
					$delquery = OCP\DB::prepare("DELETE FROM *PREFIX*ownnote WHERE id=?");
					$delquery->execute(Array($delid));
					$requery = true;
				}
	if ($requery) {
		$query = OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name");
		$results = $query->execute(Array($uid))->fetchAll();
		$requery = false;
	}
	$farray = array();
	if ($FOLDER != '') {
		// Create the folder if it doesn't exist
		if (!\OC\Files\Filesystem::is_dir($FOLDER)) {
			if (!\OC\Files\Filesystem::mkdir($FOLDER)) {
				echo "ERROR: Could not create ownNote directory.";
				exit;
			}
		}
		// Synchronize files to the database
		$filearr = array();
		if ($listing = \OC\Files\Filesystem::opendir($FOLDER)) {
			if (!$listing) {
				echo "ERROR: Error listing directory.";
				exit;
			}
			while (($file = readdir($listing)) !== false) {
				$tmpfile = $file;
				if ($tmpfile == "." || $tmpfile == "..") continue;
				if (!endswith($tmpfile, ".htm") && !endswith($tmpfile, ".html")) continue;
				if ($info = \OC\Files\Filesystem::getFileInfo($FOLDER."/".$tmpfile)) {
					// Check for EVERNOTE imports and rename them
					if (endswith($tmpfile, ".html")) {
						checkEvernote($FOLDER, $tmpfile);
						$tmpfile = substr($tmpfile,0,-1);
						if (!\OC\Files\Filesystem::file_exists($FOLDER."/".$tmpfile))<commit_msg>Added some test code<commit_after><?php

\OCP\User::checkLoggedIn();
\OCP\App::checkAppEnabled('ownnote');


function startsWith($haystack, $needle) {
	return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== FALSE;
}

function endswith($string, $test) {
	$strlen = strlen($string);
	$testlen = strlen($test);
	if ($testlen > $strlen) return false;
	return substr_compare($string, $test, $strlen - $testlen, $testlen) === 0;
}

function getAnnouncement() {
	$ret = "";
	$url = 'https://raw.githubusercontent.com/Fmstrat/announcements/master/ownnote/announcement.html';
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($curl1, CURLOPT_FRESH_CONNECT, TRUE);
	//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
	//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.0; S60/3.0 NokiaN73-1/2.0(2.0617.0.0.7) Profile/MIDP-2.0 Configuration/CLDC-1.1)");
	$result = curl_exec($ch);
	if ($result === FALSE) {
		die('Curl failed: ' . curl_error($ch));
	} else {
		$ret = $result;
	}
	curl_close($ch);
	return $ret;
}

function checkEvernote($folder, $file) {
	$html = "";
	if ($html = \OC\Files\Filesystem::file_get_contents($folder."/".$file)) {
		$DOM = new DOMDocument;
		$DOM->loadHTML($html);
		$items = $DOM->getElementsByTagName('meta');
		$isEvernote = false;
		for ($i = 0; $i < $items->length; $i++) {
			$item = $items->item($i);
			if ($item->hasAttributes()) {
				$attrs = $item->attributes;
				foreach ($attrs as $a => $attr) {
					if ($attr->name == "name") {
						if ($attr->value == "exporter-version") {
							$isEvernote = true;
							continue;
						}
					}
				}
			}
		}
		if ($isEvernote) {
			$items = $DOM->getElementsByTagName('img');
			$isEvernote = false;
			for ($i = 0; $i < $items->length; $i++) {
				$item = $items->item($i);
				if ($item->hasAttributes()) {
					$attrs = $item->attributes;
					foreach ($attrs as $a => $attr) {
						if ($attr->name == "src") {
							$url = $attr->value;
							if (!startsWith($url, "http") && !startsWith($url, "/") && !startsWith($url,"data")) {
								if ($data = \OC\Files\Filesystem::file_get_contents($folder."/".$url)) {
									$type = pathinfo($url, PATHINFO_EXTENSION);
									$base64 = "data:image/".$type.";base64,".base64_encode($data);
									$html = str_replace($url, $base64, $html);
								}
							}
						}
					}
				}
			}
			\OC\Files\Filesystem::file_put_contents($folder."/".$file, $html);
		}
	}
}

function getTimeString($filetime, $now) {
	$difftime = $filetime->diff($now);
	$years = $difftime->y;
	$months = $difftime->m;
	$days = $difftime->d;
	$hours = $difftime->h;
	$minutes = $difftime->i;
	$seconds = $difftime->s;
	$timestring = "";
	if ($timestring == "" && $years == 1) $timestring = "$years year";
	if ($timestring == "" && $years > 0) $timestring = "$years years";
	if ($timestring == "" && $months == 1) $timestring = "$months month";
	if ($timestring == "" && $months > 0) $timestring = "$months months";
	if ($timestring == "" && $days == 1) $timestring = "$days day";
	if ($timestring == "" && $days > 0) $timestring = "$days days";
	if ($timestring == "" && $hours == 1) $timestring = "$hours hour";
	if ($timestring == "" && $hours > 0) $timestring = "$hours hours";
	if ($timestring == "" && $minutes == 1) $timestring = "$minutes minute";
	if ($timestring == "" && $minutes > 0) $timestring = "$minutes minutes";
	if ($timestring == "" && $seconds == 1) $timestring = "$seconds second";
	if ($timestring == "" && $seconds > 0) $timestring = "$seconds seconds";
	return $timestring;
}

function splitContent($str) {
	$maxlength = 2621440; // 5 Megs (2 bytes per character)
	$count = 0;
	$strarray = array();
	while (true) { 
		if (strlen($str) <= $maxlength) { 
			$strarray[$count++] = $str;
			return $strarray;
		} else {
			$strarray[$count++] = substr($str, 0, $maxlength);
			$str = substr($str, $maxlength);
		}
	}
}

function getListing($FOLDER, $showdel) {
	// Get the listing from the database
	$requery = false;
	$uid = \OCP\User::getUser();
	$query = OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name");
	$results = $query->execute(Array($uid))->fetchAll();
	$results2 = $results;
	if ($results)
		foreach($results as $result)
			foreach($results2 as $result2)
				if ($result['id'] != $result2['id'] && $result['name'] == $result2['name'] && $result['grouping'] == $result2['grouping'] && $result['mtime'] == $result2['mtime']) {
					// We have a duplicate that should not exist. Need to remove the offending record first
					$delid = $result['id'];
					if ($result['id'] > $result2['id'])
						$delid = $result2['id'];
					$delquery = OCP\DB::prepare("DELETE FROM *PREFIX*ownnote WHERE id=?");
					$delquery->execute(Array($delid));
					$requery = true;
				}
	if ($requery) {
		$query = OCP\DB::prepare("SELECT id, name, grouping, mtime, deleted FROM *PREFIX*ownnote WHERE uid=? ORDER BY name");
		$results = $query->execute(Array($uid))->fetchAll();
		$requery = false;
	}
	// Tests to add a bunch of notes
	//$now = new DateTime();
	//for ($x = 0; $x < 199; $x++) {
		//saveNote('', "Test ".$x, '', '', $now->getTimestamp());
	//}
	$farray = array();
	if ($FOLDER != '') {
		// Create the folder if it doesn't exist
		if (!\OC\Files\Filesystem::is_dir($FOLDER)) {
			if (!\OC\Files\Filesystem::mkdir($FOLDER)) {
				echo "ERROR: Could not create ownNote directory.";
				exit;
			}
		}
		// Synchronize files to the database
		$filearr = array();
		if ($listing = \OC\Files\Filesystem::opendir($FOLDER)) {
			if (!$listing) {
				echo "ERROR: Error listing directory.";
				exit;
			}
			while (($file = readdir($listing)) !== false) {
				$tmpfile = $file;
				if ($tmpfile == "." || $tmpfile == "..") continue;
				if (!endswith($tmpfile, ".htm") && !endswith($tmpfile, ".html")) continue;
				if ($info = \OC\Files\Filesystem::getFileInfo($FOLDER."/".$tmpfile)) {
					// Check for EVERNOTE imports and rename them
					if (endswith($tmpfile, ".html")) {
						checkEvernote($FOLDER, $tmpfile);
						$tmpfile = substr($tmpfile,0,-1);
						if (!\OC\Files\Filesystem::file_exists($FOLDER."/".$tmpfile))
{'fraction_non_alphanumeric': 0.21469017958259182, 'fraction_numerical': 0.011972172787574826, 'mean_word_length': 3.780742459396752, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 78, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '1016551', 'n_tokens_mistral': 5257, 'n_tokens_neox': 4514, 'n_words': 972}