1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
|
<?php
/**
* XMP Reader - Read XMP data
*
* Zoph is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Zoph is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with Zoph; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* @author Jeroen Roos
* @package Zoph
*/
namespace xmp;
use file;
use XMLReader;
use log;
/**
* @author Jeroen Roos
* @package Zoph
*/
class reader {
/** @var array names of XMP/rdf containers */
private static $containers = array("rdf:Seq", "rdf:Bag", "rdf:Alt");
/** @var array names of XMP/rdf container elements */
private static $containerElements = array("rdf:li");
/** @var array xmp data */
private $xmpdata = array();
/** @var array file data - file contents */
private $XMPs = array();
/** @var sidecar from which data was loaded */
public ?string $sidecar = null;
/** @param array Extensions to try for XMP Sidecar files */
private $XMPext=array("xmp", "XMP", "xml", "XML");
/**
* Create new xmp/reader object
* @param file file to read
*/
public function __construct(private file $file) {
}
/**
* Read XMP from sidecar files
*/
public function getXMPfromSidecar() {
log::msg("Loading XMP from sidecar files", log::NOTIFY, log::IMPORT);
foreach ($this->XMPext as $ext) {
foreach (array($this->file->getName(), $this->file->getNameNoExt()) as $name) {
$filename = $this->file->getPath() . "/" . $name . "." . $ext;
log::msg("Trying " . $filename, log::DEBUG, log::IMPORT);
if (file_exists($filename)) {
log::msg("Found " . $filename, log::DEBUG, log::IMPORT);
$sidecar = new file($filename);
$xmp = new self($sidecar);
$this->sidecar = $sidecar->getName();
return $xmp->getXMP();
}
}
}
log::msg("No sidecar file found", log::NOTIFY, log::IMPORT);
return array();
}
/**
* Read XMP from file
*/
public function getXMP() {
$this->file->getMime();
switch ($this->file->type) {
case "image":
$this->getXMPfromJPEG();
break;
case "xmp":
$this->getXMPfromXML();
break;
default:
throw new fileUnknownFileTypeException("Unknown filetype " . $this->file->type);
break;
}
$this->readXMPs();
$return = array();
foreach ($this->xmpdata as $xmp) {
$return[] = new data($xmp);
}
return $return;
}
/**
* Read XMP-xml from an XML-file
*/
private function getXMPfromXML() {
$this->XMPs=array(file_get_contents($this->file));
}
private function readXMPs() {
foreach ($this->XMPs as $xmp) {
$this->xmpdata[]=$this->readXMP($xmp);
}
}
/**
* Decode XMP data from XML
* @param string XML
* @return array XMPdata
*/
public function readXMP($xmp) {
$xml = new XMLReader();
$xml->XML($xmp);
return $this->decodeXML($xml);
}
/**
* Read XMP-xml from a JPEG
* Read JPG in chunks of 1024 bytes to keep memory usage low.
*/
private function getXMPfromJPEG() {
$fp = fopen($this->file, "rb");
$buffer = "";
$open = false;
while (($data = fread($fp, 1024)) !== false) {
if ($data === "") {
break;
}
$buffer .= $data;
if (!$open) {
$openpos =strpos($buffer, "<x:xmpmeta");
if ($openpos !== false) {
$open = true;
} else {
// keep 12 bytes of buffer, in case the XMP is right on the edge
$buffer = substr($buffer, strlen($buffer) - 12);
}
} else {
$closepos =strpos($buffer, "</x:xmpmeta>");
if ($closepos !== false) {
$this->XMPs[] = substr($buffer, $openpos, ($closepos - $openpos) + 12);
$open = false;
$buffer = substr($buffer, $closepos + 11);
}
}
}
}
private function decodeXML(XMLReader $xml) {
$node=array();
while ($xml->read()) {
if ($xml->nodeType==XMLReader::ELEMENT) {
$nodename = $xml->name;
if (in_array($nodename, self::$containers)) {
$container = $nodename;
$containerElement = false;
} else if (in_array($nodename, self::$containerElements)) {
$container = false;
$containerElement = true;
} else {
$container = false;
$containerElement = false;
}
$empty=$xml->isEmptyElement;
if ($xml->hasAttributes) {
$attr=array();
$xml->moveToFirstAttribute();
do {
$attr[$xml->name] = $xml->value;
} while ($xml->moveToNextAttribute());
if (!$empty) {
$data=array_merge($attr, (array) $this->decodeXML($xml));
} else {
$data=$attr;
}
} else if ($xml->hasValue) {
$data = $xml->value;
} else {
$data=$this->decodeXML($xml);
}
if ($container) {
$node = $data;
} else if ($containerElement) {
$node[] =$data;
} else {
if (!is_array($node)) {
$node = array($node);
}
if (!isset($node[$nodename])) {
$node[$nodename] = $data;
} else {
$nodedata = $node[$nodename];
$node[$nodename] = array_merge(
$nodedata,
$data
);
}
}
} else if ($xml->nodeType==XMLReader::TEXT) {
$node = $xml->value;
} else if ($xml->nodeType==XMLReader::END_ELEMENT) {
break;
} else if (($xml->nodeType != XMLReader::WHITESPACE) &&
($xml->nodeType != XMLReader::SIGNIFICANT_WHITESPACE) &&
($xml->nodeType != XMLReader::PI)) {
$node[] = $xml->value;
}
}
return $node;
}
}
?>
|