<?php

namespace FOPRouter;

class Files {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function downloadURL($id, $w = 0 , $h = 0 , $q = 70 , $disregardPermissions = false , $params = [] ) { 
    
    
    // --- BLOCK: main ---
    
		$params['id'] = $id;$params['w'] = $w;$params['h'] = $h;$params['q'] = $q;$params['disregardPermissions'] = $disregardPermissions;
		return self::downloadURL2($params);
	
    
  }

  public static function uploadURL($x, $params = [] ) { 
    
    
    // --- BLOCK: main ---
    
		$params['x'] = $x;
		return self::uploadURL2($params);
	
    
  }

  public static function previewURL($id, $params = [] ) { 
    
    
    // --- BLOCK: main ---
    
		$params['id'] = $id;
		return self::previewURL2($params);
	
    
  }

  public static function downloadURL2($params) { 
    
    
    // --- BLOCK: a-shorturl ---
    
	if (
		(!isset($params['w']) || $params['w'] == 0) &&
		(!isset($params['h']) || $params['h'] == 0) &&
		(!isset($params['q']) || $params['q'] == 70) &&
		(!isset($params['disregardPermissions']) || $params['disregardPermissions'] == false) &&
		(\FOP\Configuration::getFOPUtilEnableRewrite() == 'true' || \FOP\Configuration::getFOPUtilEnableRewrite() == 1)
	) {
		return \FOP\Util::getCurrentBaseurl() . 'fop-files/' . $params['id'] . '/' . urlencode($params['fakename'] ?? \FOP\Database::query('SELECT filename FROM fop_files WHERE '.qi('ID').' = ' . \FOP\Database::quote($params['id']))->fetchColumn());
	}

    // --- BLOCK: main ---
    
		// Default-URL - kann durch rewrite-Regeln abgeändert werden
		
		if (!isset($url)) {
			$url = 'FOP/Files/download';
			$isFirst = true;
		}
		foreach ($params AS $k => $v) {
			if (!is_null($v)) {
				$url .= ($isFirst ? '?' : '&') . urlencode($k) . '=' . \FOP\Util::urlencode($v);
				$isFirst = false;
			}
		}
		if (\FOP\Configuration::getFOPUtilEnableRewrite() == 'true' || \FOP\Configuration::getFOPUtilEnableRewrite() == 1) {
			return \FOP\Util::getCurrentBaseurl() . $url;
		} else {
			return \FOP\Util::getCurrentBaseurl() . 'fop-router.php?q=' . urlencode($url);
		}
	
    
  }

  public static function uploadURL2($params) { 
    
    
    // --- BLOCK: main ---
    
		// Default-URL - kann durch rewrite-Regeln abgeändert werden
		
		if (!isset($url)) {
			$url = 'FOP/Files/upload';
			$isFirst = true;
		}
		foreach ($params AS $k => $v) {
			if (!is_null($v)) {
				$url .= ($isFirst ? '?' : '&') . urlencode($k) . '=' . \FOP\Util::urlencode($v);
				$isFirst = false;
			}
		}
		if (\FOP\Configuration::getFOPUtilEnableRewrite() == 'true' || \FOP\Configuration::getFOPUtilEnableRewrite() == 1) {
			return \FOP\Util::getCurrentBaseurl() . $url;
		} else {
			return \FOP\Util::getCurrentBaseurl() . 'fop-router.php?q=' . urlencode($url);
		}
	
    
  }

  public static function previewURL2($params) { 
    
    
    // --- BLOCK: main ---
    
		// Default-URL - kann durch rewrite-Regeln abgeändert werden
		
		if (!isset($url)) {
			$url = 'FOP/Files/preview';
			$isFirst = true;
		}
		foreach ($params AS $k => $v) {
			if (!is_null($v)) {
				$url .= ($isFirst ? '?' : '&') . urlencode($k) . '=' . \FOP\Util::urlencode($v);
				$isFirst = false;
			}
		}
		if (\FOP\Configuration::getFOPUtilEnableRewrite() == 'true' || \FOP\Configuration::getFOPUtilEnableRewrite() == 1) {
			return \FOP\Util::getCurrentBaseurl() . $url;
		} else {
			return \FOP\Util::getCurrentBaseurl() . 'fop-router.php?q=' . urlencode($url);
		}
	
    
  }

  public static function route($q) { 
    
    
    // --- BLOCK: main ---
    
	if (($q == 'download') && isset($_GET['id'])      ) {
		try {
			$result = \FOP\Files::downloadAction($_GET['id'], isset($_GET['w']) ? $_GET['w'] : 0, isset($_GET['h']) ? $_GET['h'] : 0, isset($_GET['q']) ? $_GET['q'] : 70, isset($_GET['disregardPermissions']) ? $_GET['disregardPermissions'] : false);
		} catch(\FOP\AccessDeniedException $e){
			\Ajax\Errors::render($e->getMessage() ?: 'accessdenied');
			die();
		}
		if (isset($result)) {
			header('Content-Type: application/json'); 
			die(json_encode($result));
		} else {
			die('');
		}
	}if (($q == 'upload') && isset($_GET['x'])  ) {
		try {
			$result = \FOP\Files::uploadAction($_GET['x']);
		} catch(\FOP\AccessDeniedException $e){
			\Ajax\Errors::render($e->getMessage() ?: 'accessdenied');
			die();
		}
		if (isset($result)) {
			header('Content-Type: application/json'); 
			die(json_encode($result));
		} else {
			die('');
		}
	}if (($q == 'preview') && isset($_GET['id'])  ) {
		try {
			$result = \FOP\Files::previewAction($_GET['id']);
		} catch(\FOP\AccessDeniedException $e){
			\Ajax\Errors::render($e->getMessage() ?: 'accessdenied');
			die();
		}
		if (isset($result)) {
			header('Content-Type: application/json'); 
			die(json_encode($result));
		} else {
			die('');
		}
	}

	header('HTTP/1.0 404 Not found');
	\Ajax\Errors::render('Die angeforderte Funktion wurde nicht gefunden.');
	die();
    
  }

  public static function downloadAccess($id, $w = 0 , $h = 0 , $q = 70 , $disregardPermissions = false ) { 
    
    
    // --- BLOCK: access ---
    
    // --- BLOCK: main ---
    return true;
    
  }

  public static function uploadAccess($x) { 
    
    
    // --- BLOCK: access ---
    if(!((!\FOP\User::getIsGuest()))) return false;
    // --- BLOCK: main ---
    return true;
    
  }

  public static function previewAccess($id) { 
    
    
    // --- BLOCK: access ---
    if(!((!\FOP\User::getIsGuest()))) return false;
    // --- BLOCK: main ---
    return true;
    
  }


  
  

  
  
}



