Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 310 and 401] [Versions 311 and 401] [Versions 39 and 401] [Versions 400 and 401]

   1  <?php
   2  /**
   3   * Copyright 2009-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * See the enclosed file LICENSE for license information (LGPL). If you
   6   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
   7   *
   8   * @category  Horde
   9   * @copyright 2009-2017 Horde LLC
  10   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  11   * @package   Stream_Filter
  12   */
  13  
  14  /**
  15   * Stream filter class to convert EOL characters.
  16   *
  17   * Usage:
  18   *   stream_filter_register('horde_eol', 'Horde_Stream_Filter_Eol');
  19   *   stream_filter_[app|pre]pend($stream, 'horde_eol',
  20   *                               [ STREAM_FILTER_[READ|WRITE|ALL] ],
  21   *                               [ $params ]);
  22   *
  23   * $params is an array that can contain the following:
  24   *   - eol: (string) The EOL string to use.
  25   *          DEFAULT: <CR><LF> ("\r\n")
  26   *
  27   * @author    Michael Slusarz <slusarz@horde.org>
  28   * @category  Horde
  29   * @copyright 2009-2017 Horde LLC
  30   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  31   * @package   Stream_Filter
  32   */
  33  class Horde_Stream_Filter_Eol extends php_user_filter
  34  {
  35      /**
  36       * Replacement data
  37       *
  38       * @var mixed
  39       */
  40      protected $_replace;
  41  
  42      /**
  43       * Search array.
  44       *
  45       * @var mixed
  46       */
  47      protected $_search;
  48  
  49      /**
  50       * First character of a multi-character EOL.
  51       *
  52       * @var string
  53       */
  54      protected $_split = null;
  55  
  56      /**
  57       * @see stream_filter_register()
  58       */
  59      #[ReturnTypeWillChange]
  60      public function onCreate()
  61      {
  62          $eol = isset($this->params['eol'])
  63              ? $this->params['eol']
  64              : "\r\n";
  65  
  66          if (!strlen($eol)) {
  67              $this->_search = array("\r", "\n");
  68              $this->_replace = '';
  69          } elseif (in_array($eol, array("\r", "\n"))) {
  70              $this->_search = array("\r\n", ($eol == "\r") ? "\n" : "\r");
  71              $this->_replace = $eol;
  72          } else {
  73              $this->_search = array("\r\n", "\r", "\n");
  74              $this->_replace = array("\n", "\n", $eol);
  75              if (strlen($eol) > 1) {
  76                  $this->_split = $eol[0];
  77              }
  78          }
  79  
  80          return true;
  81      }
  82  
  83      /**
  84       * @see stream_filter_register()
  85       */
  86      #[ReturnTypeWillChange]
  87      public function filter($in, $out, &$consumed, $closing)
  88      {
  89          while ($bucket = stream_bucket_make_writeable($in)) {
  90              if (!is_null($this->_split) &&
  91                  ($bucket->data[$bucket->datalen - 1] == $this->_split)) {
  92                  $bucket->data = substr($bucket->data, 0, -1);
  93              }
  94  
  95              $bucket->data = str_replace($this->_search, $this->_replace, $bucket->data);
  96              $consumed += $bucket->datalen;
  97              stream_bucket_append($out, $bucket);
  98          }
  99  
 100          return PSFS_PASS_ON;
 101      }
 102  
 103  }