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 1999-2017 Horde LLC (http://www.horde.org/)
   4   *
   5   * -----
   6   *
   7   * This file contains code adapted from PEAR's PHP_Compat library (v1.6.0a3).
   8   *
   9   *   http://pear.php.net/package/PHP_Compat
  10   *
  11   * This code was released under the LGPL 2.1
  12   *
  13   * -----
  14   *
  15   * See the enclosed file LICENSE for license information (LGPL). If you
  16   * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  17   *
  18   * @category  Horde
  19   * @copyright 2009-2017 Horde LLC
  20   * @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
  21   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  22   * @package   Mime
  23   */
  24  
  25  /**
  26   * Class used to uudecode data.
  27   *
  28   * Needed because PHP's built-in uudecode() method is broken.
  29   *
  30   * @author    Chuck Hagenbuch <chuck@horde.org>
  31   * @author    Aidan Lister <aidan@php.net>
  32   * @author    Michael Slusarz <slusarz@horde.org>
  33   * @author    Michael Wallner <mike@php.net>
  34   * @category  Horde
  35   * @copyright 2009-2017 Horde LLC
  36   * @copyright 2004-2007 Aidan Lister <aidan@php.net>, Arpad Ray <arpad@php.net>
  37   * @license   http://www.horde.org/licenses/lgpl21 LGPL 2.1
  38   * @package   Mime
  39   * @since     2.5.0
  40   */
  41  class Horde_Mime_Uudecode implements Countable, IteratorAggregate
  42  {
  43      const UUENCODE_REGEX = "/begin ([0-7]{3}) (.+)\r?\n(.+)\r?\nend/Us";
  44  
  45      /**
  46       * Uudecode data.
  47       *
  48       * A list of arrays, with each array corresponding to a file in the input
  49       * and containing the following keys:
  50       *   - data: (string) Unencoded data.
  51       *   - name: (string) Filename.
  52       *   - perms: (string) Octal permissions.
  53       *
  54       * @var array
  55       */
  56      protected $_data = array();
  57  
  58      /**
  59       * Scans $input for uuencoded data and converts it to unencoded data.
  60       *
  61       * @param string $input  The input data
  62       */
  63      public function __construct($input)
  64      {
  65          /* Find all uuencoded sections. */
  66          if (preg_match_all(self::UUENCODE_REGEX, $input, $matches, PREG_SET_ORDER)) {
  67              foreach ($matches as $v) {
  68                  $this->_data[] = array(
  69                      'data' => $this->_uudecode($v[3]),
  70                      'name' => $v[2],
  71                      'perm' => $v[1]
  72                  );
  73              }
  74          }
  75      }
  76  
  77      /**
  78       * PHP 5's built-in convert_uudecode() is broken. Need this wrapper.
  79       *
  80       * @param string $input  UUencoded input.
  81       *
  82       * @return string  Decoded string.
  83       */
  84      protected function _uudecode($input)
  85      {
  86          $decoded = '';
  87  
  88          foreach (explode("\n", $input) as $line) {
  89              $c = count($bytes = unpack('c*', substr(trim($line,"\r\n\t"), 1)));
  90  
  91              while ($c % 4) {
  92                  $bytes[++$c] = 0;
  93              }
  94  
  95              foreach (array_chunk($bytes, 4) as $b) {
  96                  $b0 = ($b[0] == 0x60) ? 0 : $b[0] - 0x20;
  97                  $b1 = ($b[1] == 0x60) ? 0 : $b[1] - 0x20;
  98                  $b2 = ($b[2] == 0x60) ? 0 : $b[2] - 0x20;
  99                  $b3 = ($b[3] == 0x60) ? 0 : $b[3] - 0x20;
 100  
 101                  $b0 <<= 2;
 102                  $b0 |= ($b1 >> 4) & 0x03;
 103                  $b1 <<= 4;
 104                  $b1 |= ($b2 >> 2) & 0x0F;
 105                  $b2 <<= 6;
 106                  $b2 |= $b3 & 0x3F;
 107  
 108                  $decoded .= pack('c*', $b0, $b1, $b2);
 109              }
 110          }
 111  
 112          return rtrim($decoded, "\0");
 113      }
 114  
 115      /* Countable method. */
 116  
 117      #[ReturnTypeWillChange]
 118      public function count()
 119      {
 120          return count($this->_data);
 121      }
 122  
 123      /* IteratorAggregate method. */
 124  
 125      #[ReturnTypeWillChange]
 126      public function getIterator()
 127      {
 128          return new ArrayIterator($this->_data);
 129      }
 130  
 131  }