Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   1  <?php
   2  /**
   3   * Class for generating GUIDs. Usage:
   4   *
   5   * <code>
   6   * $uid = (string)new Horde_Support_Guid([$opts = array()]);
   7   * </code>
   8   *
   9   * Copyright 2009-2017 Horde LLC (http://www.horde.org/)
  10   *
  11   * @category   Horde
  12   * @package    Support
  13   * @license    http://www.horde.org/licenses/bsd
  14   */
  15  class Horde_Support_Guid
  16  {
  17      /**
  18       * Generated GUID.
  19       *
  20       * @var string
  21       */
  22      private $_guid;
  23  
  24      /**
  25       * New GUID.
  26       *
  27       * @param array $opts  Additional options:
  28       * <pre>
  29       * 'prefix' - (string) A prefix to add between the date string and the
  30       *            random string.
  31       *            DEFAULT: NONE
  32       * 'server' - (string) The server name.
  33       *            DEFAULT: $_SERVER['SERVER_NAME'] (or 'localhost')
  34       * </pre>
  35       */
  36      public function __construct(array $opts = array())
  37      {
  38          $this->generate($opts);
  39      }
  40  
  41      /**
  42       * Generates a GUID.
  43       *
  44       * @param array $opts  Additional options:
  45       * <pre>
  46       * 'prefix' - (string) A prefix to add between the date string and the
  47       *            random string.
  48       *            DEFAULT: NONE
  49       * 'server' - (string) The server name.
  50       *            DEFAULT: $_SERVER['SERVER_NAME'] (or 'localhost')
  51       * </pre>
  52       */
  53      public function generate(array $opts = array())
  54      {
  55          $this->_guid = date('YmdHis')
  56              . '.'
  57              . (isset($opts['prefix']) ? $opts['prefix'] . '.' : '')
  58              . strval(new Horde_Support_Randomid())
  59              . '@'
  60              . (isset($opts['server']) ? $opts['server'] : (!empty($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost'));
  61      }
  62  
  63      /**
  64       * Cooerce to string.
  65       *
  66       * @return string
  67       */
  68      public function __toString()
  69      {
  70          return $this->_guid;
  71      }
  72  
  73  }