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.
   1  <?php
   2  /**
   3   * LICENSE
   4   *
   5   * This file is part of CFPropertyList.
   6   *
   7   * The PHP implementation of Apple's PropertyList can handle XML PropertyLists
   8   * as well as binary PropertyLists. It offers functionality to easily convert
   9   * data between worlds, e.g. recalculating timestamps from unix epoch to apple
  10   * epoch and vice versa. A feature to automagically create (guess) the plist
  11   * structure from a normal PHP data structure will help you dump your data to
  12   * plist in no time.
  13   *
  14   * Copyright (c) 2018 Teclib'
  15   *
  16   * Permission is hereby granted, free of charge, to any person obtaining a copy
  17   * of this software and associated documentation files (the "Software"), to deal
  18   * in the Software without restriction, including without limitation the rights
  19   * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  20   * copies of the Software, and to permit persons to whom the Software is
  21   * furnished to do so, subject to the following conditions:
  22   *
  23   * The above copyright notice and this permission notice shall be included in all
  24   * copies or substantial portions of the Software.
  25   *
  26   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  27   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  29   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  31   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  32   * SOFTWARE.
  33   *
  34   * ------------------------------------------------------------------------------
  35   * @author    Rodney Rehm <rodney.rehm@medialize.de>
  36   * @author    Christian Kruse <cjk@wwwtech.de>
  37   * @copyright Copyright © 2018 Teclib
  38   * @package   plist
  39   * @license   MIT
  40   * @link      https://github.com/TECLIB/CFPropertyList/
  41   * @link      http://developer.apple.com/documentation/Darwin/Reference/ManPages/man5/plist.5.html Property Lists
  42   * ------------------------------------------------------------------------------
  43   */
  44  
  45  namespace CFPropertyList;
  46  
  47  use \DOMDocument;
  48  use \Iterator;
  49  use \ArrayAccess;
  50  
  51  /**
  52   * Date Type of CFPropertyList
  53   * Note: CFDate uses Unix timestamp (epoch) to store dates internally
  54   */
  55  class CFDate extends CFType
  56  {
  57      const TIMESTAMP_APPLE = 0;
  58      const TIMESTAMP_UNIX  = 1;
  59      const DATE_DIFF_APPLE_UNIX = 978307200;
  60  
  61     /**
  62      * Create new Date CFType.
  63      * @param integer $value timestamp to set
  64      * @param integer $format format the timestamp is specified in, use {@link TIMESTAMP_APPLE} or {@link TIMESTAMP_UNIX}, defaults to {@link TIMESTAMP_APPLE}
  65      * @uses setValue() to convert the timestamp
  66      */
  67      public function __construct($value, $format = CFDate::TIMESTAMP_UNIX)
  68      {
  69          $this->setValue($value, $format);
  70      }
  71  
  72     /**
  73      * Set the Date CFType's value.
  74      * @param integer $value timestamp to set
  75      * @param integer $format format the timestamp is specified in, use {@link TIMESTAMP_APPLE} or {@link TIMESTAMP_UNIX}, defaults to {@link TIMESTAMP_UNIX}
  76      * @return void
  77      * @uses TIMESTAMP_APPLE to determine timestamp type
  78      * @uses TIMESTAMP_UNIX to determine timestamp type
  79      * @uses DATE_DIFF_APPLE_UNIX to convert Apple-timestamp to Unix-timestamp
  80      */
  81      public function setValue($value, $format = CFDate::TIMESTAMP_UNIX)
  82      {
  83          if ($format == CFDate::TIMESTAMP_UNIX) {
  84              $this->value = $value;
  85          } else {
  86              $this->value = $value + CFDate::DATE_DIFF_APPLE_UNIX;
  87          }
  88      }
  89  
  90    /**
  91     * Get the Date CFType's value.
  92     * @param integer $format format the timestamp is specified in, use {@link TIMESTAMP_APPLE} or {@link TIMESTAMP_UNIX}, defaults to {@link TIMESTAMP_UNIX}
  93     * @return integer Unix timestamp
  94     * @uses TIMESTAMP_APPLE to determine timestamp type
  95     * @uses TIMESTAMP_UNIX to determine timestamp type
  96     * @uses DATE_DIFF_APPLE_UNIX to convert Unix-timestamp to Apple-timestamp
  97     */
  98      public function getValue($format = CFDate::TIMESTAMP_UNIX)
  99      {
 100          if ($format == CFDate::TIMESTAMP_UNIX) {
 101              return $this->value;
 102          } else {
 103              return $this->value - CFDate::DATE_DIFF_APPLE_UNIX;
 104          }
 105      }
 106  
 107   /**
 108    * Get XML-Node.
 109    * @param DOMDocument $doc DOMDocument to create DOMNode in
 110    * @param string $nodeName For compatibility reasons; just ignore it
 111    * @return DOMNode &lt;date&gt;-Element
 112    */
 113      public function toXML(DOMDocument $doc, $nodeName = "")
 114      {
 115          $text = $doc->createTextNode(gmdate("Y-m-d\TH:i:s\Z", $this->getValue()));
 116          $node = $doc->createElement("date");
 117          $node->appendChild($text);
 118          return $node;
 119      }
 120  
 121   /**
 122    * convert value to binary representation
 123    * @param CFBinaryPropertyList The binary property list object
 124    * @return The offset in the object table
 125    */
 126      public function toBinary(CFBinaryPropertyList &$bplist)
 127      {
 128          return $bplist->dateToBinary($this->value);
 129      }
 130  
 131   /**
 132    * Create a UNIX timestamp from a PList date string
 133    * @param string $val The date string (e.g. "2009-05-13T20:23:43Z")
 134    * @return integer The UNIX timestamp
 135    * @throws PListException when encountering an unknown date string format
 136    */
 137      public static function dateValue($val)
 138      {
 139        //2009-05-13T20:23:43Z
 140          if (!preg_match('/^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2})Z/', $val, $matches)) {
 141              throw new PListException("Unknown date format: $val");
 142          }
 143          return gmmktime($matches[4], $matches[5], $matches[6], $matches[2], $matches[3], $matches[1]);
 144      }
 145  }