<?php

namespace FOPRouter;

class Push {
  
  

  
  

    // --- Statische Methoden ---
  
  public static function registerSubscriptionURL($endpoint, $publicKey, $authToken, $contentEncoding, $params = [] ) { 
    
    
    // --- BLOCK: main ---
    
		$params['endpoint'] = $endpoint;$params['publicKey'] = $publicKey;$params['authToken'] = $authToken;$params['contentEncoding'] = $contentEncoding;
		return self::registerSubscriptionURL2($params);
	
    
  }

  public static function registerSubscriptionURL2($params) { 
    
    
    // --- BLOCK: main ---
    
		// Default-URL - kann durch rewrite-Regeln abgeändert werden
		
		if (!isset($url)) {
			$url = 'FOP/Push/registerSubscription';
			$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 == 'registerSubscription') && isset($_GET['endpoint'])   && isset($_GET['publicKey'])   && isset($_GET['authToken'])   && isset($_GET['contentEncoding'])  ) {
		try {
			$result = \FOP\Push::registerSubscriptionAction($_GET['endpoint'], $_GET['publicKey'], $_GET['authToken'], $_GET['contentEncoding']);
		} 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 registerSubscriptionAccess($endpoint, $publicKey, $authToken, $contentEncoding) { 
    
    
    // --- BLOCK: access ---
    
    // --- BLOCK: main ---
    return true;
    
  }


  
  

  
  
}



