Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]

   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      public function onCreate()
  60      {
  61          $eol = isset($this->params['eol'])
  62              ? $this->params['eol']
  63              : "\r\n";
  64  
  65          if (!strlen($eol)) {
  66              $this->_search = array("\r", "\n");
  67              $this->_replace = '';
  68          } elseif (in_array($eol, array("\r", "\n"))) {
  69              $this->_search = array("\r\n", ($eol == "\r") ? "\n" : "\r");
  70              $this->_replace = $eol;
  71          } else {
  72              $this->_search = array("\r\n", "\r", "\n");
  73              $this->_replace = array("\n", "\n", $eol);
  74              if (strlen($eol) > 1) {
  75                  $this->_split = $eol[0];
  76              }
  77          }
  78  
  79          return true;
  80      }
  81  
  82      /**
  83       * @see stream_filter_register()
  84       */
  85      public function filter($in, $out, &$consumed, $closing)
  86      {
  87          while ($bucket = stream_bucket_make_writeable($in)) {
  88              if (!is_null($this->_split) &&
  89                  ($bucket->data[$bucket->datalen - 1] == $this->_split)) {
  90                  $bucket->data = substr($bucket->data, 0, -1);
  91              }
  92  
  93              $bucket->data = str_replace($this->_search, $this->_replace, $bucket->data);
  94              $consumed += $bucket->datalen;
  95              stream_bucket_append($out, $bucket);
  96          }
  97  
  98          return PSFS_PASS_ON;
  99      }
 100  
 101  }