Benutzer: Gast
Eintrag: Extbase: eID-Bootstrap

Extbase: eID-Bootstrap

von: marcel
Tags: Extbase, Typo3
Kreise: Typo3

localconf.php

Code: PHP
<?php
if (!defined('TYPO3_MODE')) {
	die ('Access denied.');
}

\TYPO3\CMS\Extbase\Utility\ExtensionUtility::configurePlugin(
	'VENDOR.' . $_EXTKEY,
	'Pi1',
	array(
	  'Example' => 'show,download',
	), // Allowed Controller-Action pairs
	array() // non-cacheable actions
);

/**
 * eID for Example-Controller
 */
$GLOBALS['TYPO3_CONF_VARS']['FE']['eID_include']['myExtkeyExample'] = 'EXT:my_extkey/Classes/EidExampleBootstrap.php';

?>

Classes/EidExampleBootstrap.php

Code: PHP
<?php
namespace VENDOR\MyExtkey;
use \TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * This class could called via eID
 *
 * @package TYPO3
 * @subpackage my_extkey
 */
class EidExampleBootstrap {

  /**
   * @var \array
   */
  protected $configuration;

  /**
   * @var \array
   */
  protected $bootstrap;

  /**
   * The main Method
   *
   * @return \string
   */
  public function run() {
    return $this->bootstrap->run('', $this->configuration);
  }

  /**
   * Initialize Extbase
   *
   * @param \array $TYPO3_CONF_VARS
   */
  public function __construct($TYPO3_CONF_VARS) {
    $this->configuration = array(
      'pluginName' => 'Pi1',
      'vendorName' => 'VENDOR',
      'extensionName' => 'MyExtkey',
      'controller' => 'Example',
      'action' => 'show',
      'mvc' => array(
        'requestHandlers' => array(
          'TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler' => 'TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler'
        )
      ),
      'settings'      => array(),
      'persistence'   => array(
        'storagePid' => GeneralUtility::_GET('storagePid')
      )
    );

    if (!$_POST['tx_lbrcodebase_pi1']['action']) { // set default action  
      $_POST['tx_lbrcodebase_pi1']['action'] = 'show'; 
    }
    $_POST['tx_myextkey_pi1']['controller'] = 'Example'; // set controller
    
    $this->bootstrap = new \TYPO3\CMS\Extbase\Core\Bootstrap();

    $feUserObj = \TYPO3\CMS\Frontend\Utility\EidUtility::initFeUser();
    $pid = (GeneralUtility::_GET('id')) ? GeneralUtility::_GET('id') : 1;

    $GLOBALS['TSFE'] = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController', $TYPO3_CONF_VARS, $pid, 0, TRUE);
    $GLOBALS['TSFE']->connectToDB();
    $GLOBALS['TSFE']->fe_user = $feUserObj;
    $GLOBALS['TSFE']->id = $pid;
    $GLOBALS['TSFE']->determineId();
    $GLOBALS['TSFE']->getCompressedTCarray();
    $GLOBALS['TSFE']->initTemplate();
    $GLOBALS['TSFE']->getConfigArray();
    $GLOBALS['TSFE']->includeTCA();
  }
}

global $TYPO3_CONF_VARS;
$eid = GeneralUtility::makeInstance('LBR\MyExtkey\EidExampleBootstrap', $TYPO3_CONF_VARS);
echo $eid->run();
?>

Classes/Controller/ExampleController.php

Code: TypoScript (TS)
<?php
namespace VENDOR\MyExtkey\Controller;

/**
 * @package TYPO3
 * @subpackage my_extkey
 */
class ExampleController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {

  /**
   * modelRepository
   *
   * @var \VENDOR\MyExtkey\Domain\Repository\ModelRepository
   * @inject
   */
  protected $modelRepository;

  /**
   * INIT
   * @return void
   */
  public function initializeAction() {
    $this->contentObject = $this->configurationManager->getContentObject();
  }
  
  /**
   * action show
   *
   * @return \string The rendered show-view.
   */
  public function showAction() {
    $args = $this->request->getArguments();
    $this->view->assign("model", $this->modelRepository->findByUid(intval($args['model'])) );
  }

  /**
   * action download
   *
   * @return void
   */
  public function downloadAction() {
    $args = $this->request->getArguments();
    $model = $this->modelRepository->findByUid(intval($args['model']));
    
    // check the model
    if (!$model) {
      header('HTTP/1.0 403 Forbidden');
      die();
    }

    // check the file to download
    if (!$model->getFilepath() || is_file($model->getFilepath()) === false) {
      header('HTTP/1.1 404 Not Found');
      die();
    }
    
    // open the file
    $file = @fopen($model->getFilepath(), 'rb');    
    if (is_resource($file) === false) {
      header('HTTP/1.1 404 Not Found');
      die();
    }

    // set some download-vars
    $filename = ($model->getFilename()) ? $model->getFilename() : "unknown.txt";
    $speed = 1024 * 1024;
    set_time_limit(0);
    ignore_user_abort(false);

    while (ob_get_level() > 0) {
      ob_end_clean();
    }
    
    // send header
    header('Expires: 0');
    header('Pragma: public');
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
    header('Content-Type: application/octet-stream');
    header('Content-Length: ' . sprintf('%u', filesize($model->getFilepath())));
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    header('Content-Transfer-Encoding: binary');
    
    // send file
    while (feof($file) !== true) {
      echo fread($file, $speed);
      while (ob_get_level() > 0) {
        ob_end_flush();
      }
      flush();
      sleep(1);
    }
    fclose($file);
    die();
  }
}
?>

Frontend:

Code: XML XHTML XSLT HTML
<!-- as link -->
<f:link.page additionalParams="{eID:'myExtkeyExample', tx_myextkey_pi1:{action:'download', model:model}}" target="_blank">Download the file from model.</f:link.page>

<!-- AJAX with jQuery -->
<div id="myModelContainer"></div>
<script>
$(document).ready(function () {
  var modelToShow = 1;
  $.post("index.php", {
    eID: 'myExtkeyExample',
    tx_myextkey_pi1: {
      model: modelToShow
    }
  }, function (html) {
    $("myModelContainer").html(html);
  }, "html");
});
</script>

Hier noch eine ähnliche Variante der EidBootstrap-Klasse

Code: PHP
<?php
namespace VENDOR\MyExt\Utility; 
use \TYPO3\CMS\Core\Utility\GeneralUtility;

/**
 * This class could called with AJAX via eID
 *
 * @package TYPO3
 * @subpackage my_ext
 */
class EidBootstrap {
  /**
   * configuration
   *
   * @var \array
   */
  protected $configuration;

  /**
   * bootstrap
   *
   * @var \array
   */
  protected $bootstrap;

  /**
   * Generates the output
   *
   * @return \string from action
   */
  public function run() {
    return $this->bootstrap->run('', $this->configuration);
  }

  /**
   * Initialize Extbase
   *
   * @param \array $TYPO3_CONF_VARS
   */
  public function __construct($TYPO3_CONF_VARS) {
    // try to set locale p.e. for using strftime()
    switch (intval($_GET['L'])) {
      case 0:
        setlocale (LC_ALL, 'de_DE@euro', 'de_DE', 'de');
        break;
      default:
      case 1:
        setlocale (LC_ALL, 'en_US', 'en', 'gb', 'uk');
        break;
    }

    // set controller and action
    if (!$_POST['tx_myext_pi1']['action'] && $_GET['tx_myext_pi1']['action']) {
      $_POST['tx_myext_pi1']['action'] = $_GET['tx_myext_pi1']['action'];
    }
    
    if (!$_POST['tx_myext_pi1']['controller'] && $_GET['tx_myext_pi1']['controller']) {
      $_POST['tx_myext_pi1']['controller'] = $_GET['tx_myext_pi1']['controller'];
    }
    
    // pre-check syntax of controller and action
    if (!preg_match('/^[a-z0-9]*$/i', $_POST['tx_myext_pi1']['action'])) {
      throw new \Exception ("Don't give me such an action-name", 1380749031);
    }
    
    if (!$_POST['tx_myext_pi1']['controller']) {
      $_POST['tx_myext_pi1']['controller'] = "Ajax"; // default controller (default action is set in plugin-configuration -> ext_localconf.php)
    } else if (!preg_match('/^[a-z0-9]*$/i', $_POST['tx_myext_pi1']['controller'])) {
      throw new \Exception ("Don't give me such a controller-name", 1380749033);
    }
    
    // set bootstrap-configuration
    $this->configuration = array(
      'pluginName'    => 'Pi1',
      'vendorName'    => 'VENDOR',
      'extensionName' => 'MyExt',
      'controller'    => $_POST['tx_myext_pi1']['controller'],
      'action'        => $_POST['tx_myext_pi1']['action'],
      'mvc'           => array(
        'requestHandlers' => array(
          'TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler' => 'TYPO3\CMS\Extbase\Mvc\Web\FrontendRequestHandler'
        )
      ),
      'settings'      => array(),
      'persistence'   => array(
        //'storagePid' => GeneralUtility::_GET('storagePid') // don't override - set by plugin.tx_myext.persistence.storagePid
      )
    );
    
    $this->bootstrap = new \TYPO3\CMS\Extbase\Core\Bootstrap();

    $feUserObj = \TYPO3\CMS\Frontend\Utility\EidUtility::initFeUser();
    $pid = (GeneralUtility::_GET('id') ? GeneralUtility::_GET('id') : 1);
    $GLOBALS['TSFE'] = GeneralUtility::makeInstance('TYPO3\CMS\Frontend\Controller\TypoScriptFrontendController', $TYPO3_CONF_VARS, $pid, 0, TRUE);
    $GLOBALS['TSFE']->connectToDB();
    $GLOBALS['TSFE']->fe_user = $feUserObj;
    $GLOBALS['TSFE']->id = $pid;
    $GLOBALS['TSFE']->determineId();
    $GLOBALS['TSFE']->getCompressedTCarray();
    $GLOBALS['TSFE']->initTemplate();
    $GLOBALS['TSFE']->getConfigArray();
    $GLOBALS['TSFE']->includeTCA();
  }
}

global $TYPO3_CONF_VARS;
$eid = GeneralUtility::makeInstance('VENDOR\MyExt\Utility\EidBootstrap', $TYPO3_CONF_VARS);
echo $eid->run(); // print content
?>

Beispiel-Extension zum Download

Größe: 7233 Bytes
Typ: application/zip

In dieser Extension sind die eID- und Page-Object-Methode aufs wesentliche reduziert vorhanden.

Eine Installation des Plugins ist hier zu finden:
https://lbrmedia.net/ajaxexample/

Es gibt noch keine Kommentare.