Merge branch 'w29_MDL-40423_m25_localsubdir' of https://github.com/skodak/moodle...
[moodle.git] / badges / lib / bakerlib.php
blob8a0c45e611ef533ef72af9a0b9a906fb09ac4ec8
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 /**
18 * Baking badges library.
20 * @package core
21 * @subpackage badges
22 * @copyright 2012 onwards Totara Learning Solutions Ltd {@link http://www.totaralms.com/}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 * @author Yuliya Bozhko <yuliya.bozhko@totaralms.com>
27 defined('MOODLE_INTERNAL') || die();
29 /**
30 * Information on PNG file chunks can be found at http://www.w3.org/TR/PNG/#11Chunks
31 * Some other info on PNG that I used http://garethrees.org/2007/11/14/pngcrush/
33 * Example of use:
34 * $png = new PNG_MetaDataHandler('file.png');
36 * if ($png->check_chunks("tEXt", "openbadge")) {
37 * $newcontents = $png->add_chunks("tEXt", "openbadge", 'http://some.public.url/to.your.assertion.file');
38 * }
40 * file_put_contents('file.png', $newcontents);
43 class PNG_MetaDataHandler
45 /** @var string File content as a string */
46 private $_contents;
47 /** @var int Length of the image file */
48 private $_size;
49 /** @var array Variable for storing parsed chunks */
50 private $_chunks;
52 /**
53 * Prepares file for handling metadata.
54 * Verifies that this file is a valid PNG file.
55 * Unpacks file chunks and reads them into an array.
57 * @param string $contents File content as a string
59 public function __construct($contents) {
60 $this->_contents = $contents;
61 $png_signature = pack("C8", 137, 80, 78, 71, 13, 10, 26, 10);
63 // Read 8 bytes of PNG header and verify.
64 $header = substr($this->_contents, 0, 8);
66 if ($header != $png_signature) {
67 debugging('This is not a valid PNG image');
70 $this->_size = strlen($this->_contents);
72 $this->_chunks = array();
74 // Skip 8 bytes of IHDR image header.
75 $position = 8;
76 do {
77 $chunk = @unpack('Nsize/a4type', substr($this->_contents, $position, 8));
78 $this->_chunks[$chunk['type']][] = substr($this->_contents, $position + 8, $chunk['size']);
80 // Skip 12 bytes chunk overhead.
81 $position += $chunk['size'] + 12;
82 } while ($position < $this->_size);
85 /**
86 * Checks if a key already exists in the chunk of said type.
87 * We need to avoid writing same keyword into file chunks.
89 * @param string $type Chunk type, like iTXt, tEXt, etc.
90 * @param string $check Keyword that needs to be checked.
92 * @return boolean (true|false) True if file is safe to write this keyword, false otherwise.
94 public function check_chunks($type, $check) {
95 if (array_key_exists($type, $this->_chunks)) {
96 foreach (array_keys($this->_chunks[$type]) as $typekey) {
97 list($key, $data) = explode("\0", $this->_chunks[$type][$typekey]);
99 if (strcmp($key, $check) == 0) {
100 debugging('Key "' . $check . '" already exists in "' . $type . '" chunk.');
101 return false;
105 return true;
109 * Adds a chunk with keyword and data to the file content.
110 * Chunk is added to the end of the file, before IEND image trailer.
112 * @param string $type Chunk type, like iTXt, tEXt, etc.
113 * @param string $key Keyword that needs to be added.
114 * @param string $value Currently an assertion URL that is added to an image metadata.
116 * @return string $result File content with a new chunk as a string. Can be used in file_put_contents() to write to a file.
118 public function add_chunks($type, $key, $value) {
119 if (strlen($key) > 79) {
120 debugging('Key is too big');
123 if ($type == 'iTXt') {
124 // iTXt International textual data.
125 // Keyword: 1-79 bytes (character string)
126 // Null separator: 1 byte
127 // Compression flag: 1 byte
128 // Compression method: 1 byte
129 // Language tag: 0 or more bytes (character string)
130 // Null separator: 1 byte
131 // Translated keyword: 0 or more bytes
132 // Null separator: 1 byte
133 // Text: 0 or more bytes
134 $data = $key . "\000'json'\0''\0\"{'method': 'hosted', 'assertionUrl': '" . $value . "'}\"";
135 } else {
136 // tEXt Textual data.
137 // Keyword: 1-79 bytes (character string)
138 // Null separator: 1 byte
139 // Text: n bytes (character string)
140 $data = $key . "\0" . $value;
142 $crc = pack("N", crc32($type . $data));
143 $len = pack("N", strlen($data));
145 // Chunk format: length + type + data + CRC.
146 // CRC is a CRC-32 computed over the chunk type and chunk data.
147 $newchunk = $len . $type . $data . $crc;
149 $result = substr($this->_contents, 0, $this->_size - 12)
150 . $newchunk
151 . substr($this->_contents, $this->_size - 12, 12);
153 return $result;