<commit_before><?php
/**
 * ownCloud - galleryplus
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @author Authors of \OCA\Files_Sharing\Helper
 *
 * @copyright <NAME> 2015
 * @copyright Authors of \OCA\Files_Sharing\Helper 2014-2015
 */

namespace OCA\GalleryPlus\Environment;

use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Share;
use OCP\ILogger;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\File;
use OCP\Files\NotFoundException;

/**
 * Builds the environment so that the services have access to the files and folders' owner
 *
 * @todo remove the serverContainer once OCP\IUserManager has a getUserFolder() method
 *
 * @package OCA\GalleryPlus\Environment
 */
class Environment {

	/**
	 * @var string
	 */
	private $appName;
	/**
	 * The userId of the logged-in user or the person sharing a folder publicly
	 *
	 * @var string
	 */
	private $userId;
	/**
	 * The userFolder of the logged-in user or the ORIGINAL owner of the files which are shared
	 * publicly
	 *
	 * A share needs to be tracked back to its original owner in order to be able to access the
	 * resource
	 *
	 * @var Folder|null
	 */
	private $userFolder;
	/**
	 * @var IUserManager
	 */
	private $userManager;
	/**
	 * @var IServerContainer
	 */
	private $serverContainer;
	/**
	 * @var ILogger
	 */
	private $logger;
	/**
	 * The path to the userFolder for users with accounts: /userId/files
	 *
	 * For public folders, it's the path from the shared folder to the root folder in the original
	 * owner's filesystem: /userId/files/parent_folder/shared_folder
	 *
	 * @var string
	 */
	private $fromRootToFolder;
	/**
	 * The name of the shared folder
	 *
	 * @var string
	 */
	private $folderName;

	/***
	 * Constructor
	 *
	 * @param string $appName
	 * @param string|null $userId
	 * @param Folder|null $userFolder
	 * @param IUserManager $userManager
	 * @param IServerContainer $serverContainer
	 * @param ILogger $logger
	 */
	public function __construct(
		$appName,
		$userId,
		$userFolder,
		IUserManager $userManager,
		IServerContainer $serverContainer,
		ILogger $logger
	) {
		$this->appName = $appName;
		$this->userId = $userId;
		$this->userFolder = $userFolder;
		$this->userManager = $userManager;
		$this->serverContainer = $serverContainer;
		$this->logger = $logger;
	}

	/**
	 * Creates the environment based on the linkItem the token links to
	 *
	 * @param array $linkItem
	 */
	public function setTokenBasedEnv($linkItem) {
		// Resolves reshares down to the last real share
		$rootLinkItem = Share::resolveReShare($linkItem);
		$origShareOwner = $rootLinkItem['uid_owner'];
		$this->userFolder = $this->setupFilesystem($origShareOwner);

		// This is actually the node ID
		$fileSource = $linkItem['file_source'];
		$this->fromRootToFolder = $this->buildFromRootToFolder($fileSource);

		$this->folderName = $linkItem['file_target'];
		$this->userId = $linkItem['uid_owner'];
	}

	/**
	 * Creates the environment for a logged-in user
	 *
	 * userId and userFolder are already known, we define fromRootToFolder
	 * so that the services can use one method to have access to resources
	 * without having to know whether they're private or public
	 */
	public function setStandardEnv() {
		$this->fromRootToFolder = $this->userFolder->getPath() . '/';
	}

	/**
	 * Returns the resource located at the given path
	 *
	 * The path starts from the user's files folder because we'll query that folder to get the
	 * information we need. The resource is either a File or a Folder
	 *
	 * @param string $subPath
	 *
	 * @return File|Folder
	 */
	public function getResourceFromPath($subPath) {<commit_msg>Use the real original share owner Fixes #163<commit_after><?php
/**
 * ownCloud - galleryplus
 *
 * This file is licensed under the Affero General Public License version 3 or
 * later. See the COPYING file.
 *
 * @author <NAME> <<EMAIL>>
 * @author Authors of \OCA\Files_Sharing\Helper
 *
 * @copyright <NAME> 2015
 * @copyright Authors of \OCA\Files_Sharing\Helper 2014-2015
 */

namespace OCA\GalleryPlus\Environment;

use OCP\IServerContainer;
use OCP\IUserManager;
use OCP\Share;
use OCP\ILogger;
use OCP\Files\Folder;
use OCP\Files\Node;
use OCP\Files\File;
use OCP\Files\NotFoundException;

/**
 * Builds the environment so that the services have access to the files and folders' owner
 *
 * @todo remove the serverContainer once OCP\IUserManager has a getUserFolder() method
 *
 * @package OCA\GalleryPlus\Environment
 */
class Environment {

	/**
	 * @var string
	 */
	private $appName;
	/**
	 * The userId of the logged-in user or the person sharing a folder publicly
	 *
	 * @var string
	 */
	private $userId;
	/**
	 * The userFolder of the logged-in user or the ORIGINAL owner of the files which are shared
	 * publicly
	 *
	 * A share needs to be tracked back to its original owner in order to be able to access the
	 * resource
	 *
	 * @var Folder|null
	 */
	private $userFolder;
	/**
	 * @var IUserManager
	 */
	private $userManager;
	/**
	 * @var IServerContainer
	 */
	private $serverContainer;
	/**
	 * @var ILogger
	 */
	private $logger;
	/**
	 * The path to the userFolder for users with accounts: /userId/files
	 *
	 * For public folders, it's the path from the shared folder to the root folder in the original
	 * owner's filesystem: /userId/files/parent_folder/shared_folder
	 *
	 * @var string
	 */
	private $fromRootToFolder;
	/**
	 * The name of the shared folder
	 *
	 * @var string
	 */
	private $folderName;

	/***
	 * Constructor
	 *
	 * @param string $appName
	 * @param string|null $userId
	 * @param Folder|null $userFolder
	 * @param IUserManager $userManager
	 * @param IServerContainer $serverContainer
	 * @param ILogger $logger
	 */
	public function __construct(
		$appName,
		$userId,
		$userFolder,
		IUserManager $userManager,
		IServerContainer $serverContainer,
		ILogger $logger
	) {
		$this->appName = $appName;
		$this->userId = $userId;
		$this->userFolder = $userFolder;
		$this->userManager = $userManager;
		$this->serverContainer = $serverContainer;
		$this->logger = $logger;
	}

	/**
	 * Creates the environment based on the linkItem the token links to
	 *
	 * @param array $linkItem
	 */
	public function setTokenBasedEnv($linkItem) {
		// Resolves reshares down to the last real share
		$rootLinkItem = Share::resolveReShare($linkItem);
		$origShareOwner = $rootLinkItem['uid_owner'];
		$this->userFolder = $this->setupFilesystem($origShareOwner);

		// This is actually the node ID
		$fileSource = $linkItem['file_source'];
		$this->fromRootToFolder = $this->buildFromRootToFolder($fileSource);

		$this->folderName = $linkItem['file_target'];
		$this->userId = $rootLinkItem['uid_owner'];
	}

	/**
	 * Creates the environment for a logged-in user
	 *
	 * userId and userFolder are already known, we define fromRootToFolder
	 * so that the services can use one method to have access to resources
	 * without having to know whether they're private or public
	 */
	public function setStandardEnv() {
		$this->fromRootToFolder = $this->userFolder->getPath() . '/';
	}

	/**
	 * Returns the resource located at the given path
	 *
	 * The path starts from the user's files folder because we'll query that folder to get the
	 * information we need. The resource is either a File or a Folder
	 *
	 * @param string $subPath
	 *
	 * @return File|Folder
	 */
	public function getResourceFromPath($subPath) {
{'fraction_non_alphanumeric': 0.10884991297362431, 'fraction_numerical': 0.0038827152229214085, 'mean_word_length': 3.66875, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 41, 'https://': 0, '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': '2983210', 'n_tokens_mistral': 2441, 'n_tokens_neox': 2242, 'n_words': 759}