<?php

/**************************************************************
* This file is part of Remository
* Copyright (c) 2006-17 Martin Brampton
* 
* License: GNU General Public License version 2 or later; see LICENSE.txt
* 
* For support and other information, visit http://remository.com
* To contact Martin Brampton, write to martin@remository.com
*
* Remository started life as the psx-dude script by psx-dude@psx-dude.net
* It was enhanced by Matt Smith up to version 2.10
* Since then development has been primarily by Martin Brampton,
* with contributions from other people gratefully accepted
*/

defined('_JEXEC') or die;

class remositoryControllerUnlinked extends remositoryAdminControllers {

	function __construct ($admin) {
		parent::__construct ($admin);
	    $_REQUEST['act'] = 'unlinked';
	}

	function getOrphanDownloads () {
		$manager = remositoryContainerManager::getInstance();
		$paths = $manager->getFilePathData();
		$OrphanDownloads = array();
		foreach ($paths as $dir_path=>$containers) {
			$directory = new remositoryDirectory($dir_path);
			$neworphans = $directory->getOrphans();
			if (count($neworphans)) $OrphanDownloads = array_merge($OrphanDownloads, $neworphans);
		}
		return $OrphanDownloads;
	}

	function getOrphanUploads () {
		$upload_path = $this->repository->Up_Path.'/';
		$upload_dir = new remositoryDirectory($upload_path);
		$uporphans = $upload_dir->getOrphans(true);
		return $uporphans;
	}

	function listTask () {
		$OrphanDownloads = $this->getOrphanDownloads();
		sort($OrphanDownloads);
		$OrphanUploads = $this->getOrphanUploads();
		sort($OrphanUploads);
		$interface = remositoryInterface::getInstance();
		$link = $interface->getCfg('admin_site')."/{$interface->indexFileName()}?option=com_remository&amp;repnum=$this->repnum&amp;act=files&amp;task=addfile&amp;orphanpath=";
		$view = $this->admin->newHTMLClassCheck ('listUnlinkedHTML', $this, 0, '');
		if ($view AND $this->admin->checkCallable($view, 'view')) $view->view($OrphanDownloads, $OrphanUploads, $link);
	}

	function deleteTask () {
		$this->admin->check_selection(_DOWN_SEL_FILE_DEL);
		$OrphanDownloads = $this->getOrphanDownloads();
		$OrphanUploads = $this->getOrphanUploads();
		$cfid = remositoryRepository::getParam($_REQUEST, 'cfid', array());
	    foreach ($cfid as $file64) {
			$file = base64_decode($file64);
			if (in_array($file, $OrphanDownloads) OR in_array($file, $OrphanUploads)) @unlink($file);
	    }

        $message = count($this->admin->cfid)>1?_DOWN_ORPHAN_DONE:_DOWN_ORPHAN_SINGLE_DONE;
        JFactory::getApplication()->enqueueMessage($message, 'message');

	    $this->listTask();
	}

}
