<commit_before><?php

namespace OC\CernBox\Share;

use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUser;
use OCP\Share\IManager;

class MountProvider extends \OCA\Files_Sharing\MountProvider {

	private $fastUrlsEnabled;
	private $fastUrls;
	protected $logger;
	
	/**
	 * @param \OCP\IConfig $config
	 * @param IManager $shareManager
	 * @param ILogger $logger
	 */
	public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) {
		$this->logger = $logger;
		$this->fastUrlsEnabled = \OC::$server->getConfig()->getSystemValue("cbox.fasturlsenabled", false);
		$this->fastUrls = \OC::$server->getConfig()->getSystemValue("cbox.fasturls", array());
		parent::__construct($config, $shareManager, $logger);
	}

	public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) {
		if ($this->fastUrlsEnabled) {
			$shouldMount = $this->shouldMountShares();
			$this->logger->info(sprintf("should mount shares=%s uri=%s", $shouldMount === true? 'yes' : 'no', $_SERVER['REQUEST_URI']));
			if($shouldMount === true) {
				return parent::getMountsForUser($user, $storageFactory);
			} else {
				return [];
			}
		} else {
			return parent::getMountsForUser($user, $storageFactory);
		}
	}

	// black sorcery
	private function shouldMountShares() {
		// if we match the fast urls we do not mount the shares
		$url = urldecode(trim(\OC::$server->getRequest()->getRequestUri(), '/'));
		$url = trim($url, '/');
		foreach($this->fastUrls as $fastUrl) {
			if(strpos($url, $fastUrl) !== false) {
				return false;
			}
		}
		
		// from version 9 basic file ops are done through remote.php/webdav endpoint
		// Example: /remote.php/webdav/cernbox-project-data-mining%20(%239993727)
		// so we get the filename and check if it points to a shared mount.
		$remotePos = strpos($url, 'remote.php/webdav');
		if($remotePos !== false) {
			$filename = explode('/', trim(substr($url, $remotePos + strlen('remote.php/webdav')), '/'))[0];	
			$isShared = $this->isSharedPath($filename);
			return $isShared;
		} 

		if(isset($_GET['shared_with_me']) && $_GET['shared_with_me'] === 'true') {
			if(isset($_GET['path'])) {
				$isShared = $this->isSharedPath($_GET['path']);
				return $isShared;
			}
			return true;
		}

		if(isset($_GET['reshares'])) {
			if(isset($_GET['path'])) {
				return $this->isSharedPath($_GET['path']);
			}
		}

		if(strpos($url, 'apps/eosinfo/getinfo') !== false) {
			if(isset($_POST['path'])) {
				return $this->isSharedPath($_POST['path']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/wopiviewer/open') !== false) {
			if(isset($_POST['filename'])) {
				return $this->isSharedPath($_POST['filename']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/swanviewer/load') !== false) {
			if(isset($_GET['path'])) {
				return $this->isSharedPath($_GET['path']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/rootviewer/load') !== false) {
			if(isset($_GET['path'])) {
				return $this->isSharedPath($_GET['path']);
			}
			return true;
		}
		
		if(strpos($url, 'core/preview') !== false) {
			if(isset($_GET['file'])) {
				return $this->isSharedPath($_GET['file']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/gallery/preview') !== false) {
			if(isset($_GET['file'])) {
				return $this->isSharedPath($_GET['file']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/swanviewer/eosinfo') !== false) {
			if(isset($_GET['filename'])) {
				return $this->isSharedPath($_GET['filename']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/files/ajax/download.php') !== false) {
			if(isset($_GET['files'])) {
				$isShared = $this->isSharedPath($_GET['files']);
				if($isShared) {
					return true;
				}
			}
			
			if(isset($_GET['dir'])) {
				return $this->isSharedPath($_GET['dir']);
			}
			return true;
		}

		if(strpos($url, 'apps/files/ajax') !== false) {
			return true;
		}	
		
		if(strpos($url, 'apps/files_texteditor') !== false) {
			return true;
		}	

		return false;
	}
	
	private function isSharedPath($path) { // uri paths always start with leading slash (e.g. ?dir=/bla) 
		// assume that all shared items follow the same // naming convention at the top directory (and they do not clash with normal files and directories)
		// the convention for top directory is: "name (#123)"
		// examples:
		// "/aaa (#1234)/jas" => true
		// "/ d s (#77663455)/jas" => true
		// "/aaa (#1)/jas" => false
		// "/aaa (#ssss)/jas" => false
		// "aaa (#1234)/jas" => false
		// "/(#7766)/jas" => false
		// "/ (#7766)/jas" => true (this is a flaw)
		$path = trim($path, '/');
		if ($this->startsWith ( $path, '/' )) {
			$topdir = explode ( "/", $path ) [1];
		} else {
			$topdir = explode ( "/", $path ) [0];
		}

		$parts = explode ( " ", $topdir );
		if (count ( $parts ) < 2) {
			return false;
		}

		$marker = end($parts);
		// preg_match returns 1 or 0 or false
		$isShared = preg_match ( "/[(][#](\d{3,})[)]/", $marker ); // we match at least 3 digits enclosed within our marker: (#123)
		$isShared === 1? $isShared = true : $isShared = false;
		if($isShared) {
			$_SERVER['cernbox_file_target'] = $topdir;
		}
		$this->logger->info(sprintf("shared_path=%s shared=%s", $topdir, $isShared ? 'true' : 'false'));
		return $isShared;
	}
	
	private function startsWith($haystack, $needle) {
		return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
	}
}
<commit_msg>Optimize loading and saving of text files<commit_after><?php

namespace OC\CernBox\Share;

use OCP\Files\Storage\IStorageFactory;
use OCP\IConfig;
use OCP\ILogger;
use OCP\IUser;
use OCP\Share\IManager;

class MountProvider extends \OCA\Files_Sharing\MountProvider {

	private $fastUrlsEnabled;
	private $fastUrls;
	protected $logger;
	
	/**
	 * @param \OCP\IConfig $config
	 * @param IManager $shareManager
	 * @param ILogger $logger
	 */
	public function __construct(IConfig $config, IManager $shareManager, ILogger $logger) {
		$this->logger = $logger;
		$this->fastUrlsEnabled = \OC::$server->getConfig()->getSystemValue("cbox.fasturlsenabled", false);
		$this->fastUrls = \OC::$server->getConfig()->getSystemValue("cbox.fasturls", array());
		parent::__construct($config, $shareManager, $logger);
	}

	public function getMountsForUser(IUser $user, IStorageFactory $storageFactory) {
		if ($this->fastUrlsEnabled) {
			$shouldMount = $this->shouldMountShares();
			$this->logger->info(sprintf("should mount shares=%s uri=%s", $shouldMount === true? 'yes' : 'no', $_SERVER['REQUEST_URI']));
			if($shouldMount === true) {
				return parent::getMountsForUser($user, $storageFactory);
			} else {
				return [];
			}
		} else {
			return parent::getMountsForUser($user, $storageFactory);
		}
	}

	// black sorcery
	private function shouldMountShares() {
		// if we match the fast urls we do not mount the shares
		$url = urldecode(trim(\OC::$server->getRequest()->getRequestUri(), '/'));
		$url = trim($url, '/');
		foreach($this->fastUrls as $fastUrl) {
			if(strpos($url, $fastUrl) !== false) {
				return false;
			}
		}
		
		// from version 9 basic file ops are done through remote.php/webdav endpoint
		// Example: /remote.php/webdav/cernbox-project-data-mining%20(%239993727)
		// so we get the filename and check if it points to a shared mount.
		$remotePos = strpos($url, 'remote.php/webdav');
		if($remotePos !== false) {
			$filename = explode('/', trim(substr($url, $remotePos + strlen('remote.php/webdav')), '/'))[0];	
			$isShared = $this->isSharedPath($filename);
			return $isShared;
		} 

		if(isset($_GET['shared_with_me']) && $_GET['shared_with_me'] === 'true') {
			if(isset($_GET['path'])) {
				$isShared = $this->isSharedPath($_GET['path']);
				return $isShared;
			}
			return true;
		}

		if(isset($_GET['reshares'])) {
			if(isset($_GET['path'])) {
				return $this->isSharedPath($_GET['path']);
			}
		}

		if(strpos($url, 'apps/eosinfo/getinfo') !== false) {
			if(isset($_POST['path'])) {
				return $this->isSharedPath($_POST['path']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/wopiviewer/open') !== false) {
			if(isset($_POST['filename'])) {
				return $this->isSharedPath($_POST['filename']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/swanviewer/load') !== false) {
			if(isset($_GET['path'])) {
				return $this->isSharedPath($_GET['path']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/rootviewer/load') !== false) {
			if(isset($_GET['path'])) {
				return $this->isSharedPath($_GET['path']);
			}
			return true;
		}
		
		if(strpos($url, 'core/preview') !== false) {
			if(isset($_GET['file'])) {
				return $this->isSharedPath($_GET['file']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/gallery/preview') !== false) {
			if(isset($_GET['file'])) {
				return $this->isSharedPath($_GET['file']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/swanviewer/eosinfo') !== false) {
			if(isset($_GET['filename'])) {
				return $this->isSharedPath($_GET['filename']);
			}
			return true;
		}
		
		if(strpos($url, 'apps/files/ajax/download.php') !== false) {
			if(isset($_GET['files'])) {
				$isShared = $this->isSharedPath($_GET['files']);
				if($isShared) {
					return true;
				}
			}
			
			if(isset($_GET['dir'])) {
				return $this->isSharedPath($_GET['dir']);
			}
			return true;
		}

		if(strpos($url, 'apps/files/ajax') !== false) {
			return true;
		}	
		
		if(strpos($url, 'apps/files_texteditor/ajax/loadfile') !== false) {
			if(isset($_GET['files'])) {
				$isShared = $this->isSharedPath($_GET['files']);
				if($isShared) {
					return true;
				}
			}
			
			if(isset($_GET['dir'])) {
				return $this->isSharedPath($_GET['dir']);
			}
			return true;
		}	
		
		if(strpos($url, 'apps/files_texteditor/ajax/savefile') !== false) {
			if(isset($_GET['files'])) {
				$isShared = $this->isSharedPath($_GET['files']);
				if($isShared) {
					return true;
				}
			}
			
			if(isset($_GET['dir'])) {
				return $this->isSharedPath($_GET['dir']);
			}
			return true;
		}	

		return false;
	}
	
	private function isSharedPath($path) { // uri paths always start with leading slash (e.g. ?dir=/bla) 
		// assume that all shared items follow the same // naming convention at the top directory (and they do not clash with normal files and directories)
		// the convention for top directory is: "name (#123)"
		// examples:
		// "/aaa (#1234)/jas" => true
		// "/ d s (#77663455)/jas" => true
		// "/aaa (#1)/jas" => false
		// "/aaa (#ssss)/jas" => false
		// "aaa (#1234)/jas" => false
		// "/(#7766)/jas" => false
		// "/ (#7766)/jas" => true (this is a flaw)
		$path = trim($path, '/');
		if ($this->startsWith ( $path, '/' )) {
			$topdir = explode ( "/", $path ) [1];
		} else {
			$topdir = explode ( "/", $path ) [0];
		}

		$parts = explode ( " ", $topdir );
		if (count ( $parts ) < 2) {
			return false;
		}

		$marker = end($parts);
		// preg_match returns 1 or 0 or false
		$isShared = preg_match ( "/[(][#](\d{3,})[)]/", $marker ); // we match at least 3 digits enclosed within our marker: (#123)
		$isShared === 1? $isShared = true : $isShared = false;
		if($isShared) {
			$_SERVER['cernbox_file_target'] = $topdir;
		}
		$this->logger->info(sprintf("shared_path=%s shared=%s", $topdir, $isShared ? 'true' : 'false'));
		return $isShared;
	}
	
	private function startsWith($haystack, $needle) {
		return $needle === "" || strrpos($haystack, $needle, -strlen($haystack)) !== false;
	}
}

{'fraction_non_alphanumeric': 0.20250833187160147, 'fraction_numerical': 0.00912120680582354, 'mean_word_length': 3.641025641025641, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '663675', 'n_tokens_mistral': 4658, 'n_tokens_neox': 3949, 'n_words': 811}