This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 289
/
class.ExifMetaManager.php
280 lines (257 loc) · 10.5 KB
/
class.ExifMetaManager.php
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
<?php
/*
* Copyright 2007-2013 Charles du Jeu - Abstrium SAS <team (at) pyd.io>
* This file is part of Pydio.
*
* Pydio is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Pydio 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Pydio. If not, see <http://www.gnu.org/licenses/>.
*
* The latest code can be found at <http://pyd.io/>.
*/
use Pydio\Access\Core\AJXP_MetaStreamWrapper;
use Pydio\Access\Core\Model\AJXP_Node;
use Pydio\Access\Core\Model\UserSelection;
use Pydio\Core\Utils\Utils;
use Pydio\Core\Controller\XMLWriter;
use Pydio\Core\Controller\HTMLWriter;
use Pydio\Core\Utils\TextEncoder;
use Pydio\Meta\Core\AJXP_AbstractMetaSource;
defined('AJXP_EXEC') or die( 'Access not allowed');
/**
* Extract Exif data from JPEG IMAGES
* @package AjaXplorer_Plugins
* @subpackage Meta
*/
class ExifMetaManager extends AJXP_AbstractMetaSource
{
protected $metaDefinitions;
public function init($options)
{
$this->options = $options;
}
public function performChecks()
{
if (!function_exists("exif_imagetype")) {
throw new Exception("Exif PHP extension does not seem to be installed!");
}
}
public function initMeta($accessDriver)
{
parent::initMeta($accessDriver);
if(!function_exists("exif_read_data")) return ;
//$messages = ConfService::getMessages();
$def = $this->getMetaDefinition();
if (!count($def)) {
return ;
}
$cdataHead = '<div>
<div class="panelHeader infoPanelGroup" colspan="2"><span class="user_meta_change" title="AJXP_MESSAGE[meta.exif.1]">AJXP_MESSAGE[meta.exif.2]</span>AJXP_MESSAGE[meta.exif.3]</div>
<div class="infoPanelTable exifPanelTable" cellspacing="0" border="0" cellpadding="0">';
$cdataFoot = '</div></div>';
$cdataParts = "";
$even = false;
foreach ($def as $key=>$label) {
$trClass = ($even?" class=\"even\"":"");
$even = !$even;
$cdataParts .= '<div'.$trClass.'><div class="infoPanelLabel">'.$label.'</div><div class="infoPanelValue" id="ip_'.$key.'">#{'.$key.'}</div></div>';
}
$selection = $this->getXPath()->query('registry_contributions/client_configs/component_config[@className="InfoPanel"]/infoPanelExtension');
$contrib = $selection->item(0);
$contrib->setAttribute("attributes", implode(",", array_keys($def)));
$contrib->setAttribute("modifier", "ExifCellRenderer.prototype.infoPanelModifier");
$htmlSel = $this->getXPath()->query('html', $contrib);
$html = $htmlSel->item(0);
$cdata = $this->manifestDoc->createCDATASection($cdataHead . $cdataParts . $cdataFoot);
$html->appendChild($cdata);
parent::init($this->options);
}
protected function getMetaDefinition()
{
if (isSet($this->metaDefinitions)) {
return $this->metaDefinitions;
}
$fields = $this->options["meta_fields"];
$arrF = explode(",", $fields);
$labels = $this->options["meta_labels"];
$arrL = explode(",", $labels);
$result = array();
foreach ($arrF as $index => $value) {
$value = str_replace(".", "-", $value);
if (isSet($arrL[$index])) {
$result[$value] = $arrL[$index];
} else {
$result[$value] = $value;
}
}
$this->metaDefinitions = $result;
return $result;
}
public function extractExif(\Psr\Http\Message\ServerRequestInterface $requestInterface, \Psr\Http\Message\ResponseInterface &$responseInterface)
{
$httpVars = $requestInterface->getParsedBody();
$repo = $this->accessDriver->repository;
$userSelection = new UserSelection($this->accessDriver->repository, $httpVars);
$repo->detectStreamWrapper(true);
$selectedNode = $userSelection->getUniqueNode();
$realFile = AJXP_MetaStreamWrapper::getRealFSReference($selectedNode->getUrl());
Utils::safeIniSet('exif.encode_unicode', 'UTF-8');
$exifData = @exif_read_data($realFile, 0, TRUE);
if($exifData === false || !is_array($exifData)) return;
if ($exifData !== false && isSet($exifData["GPS"])) {
$exifData["COMPUTED_GPS"] = $this->convertGPSData($exifData);
}
$iptc = $this->extractIPTC($realFile);
if(count($iptc)){
$exifData["IPTC"] = $iptc;
}
$excludeTags = array();// array("componentsconfiguration", "filesource", "scenetype", "makernote", "datadump");
$format = "xml";
if(isSet($httpVars["format"]) && $httpVars["format"] == "json"){
$format = "json";
}
$filteredData = array();
foreach ($exifData as $section => $data) {
$filteredData[$section] = array();
foreach ($data as $key => $value) {
if (is_array($value)) {
$value = implode(",", $value);
}
if(in_array(strtolower($key), $excludeTags)) continue;
if(strpos($key, "UndefinedTag:") === 0) continue;
$value = preg_replace( '/[^[:print:]]/', '',$value);
$filteredData[$section][$key] = TextEncoder::toUTF8($value);
}
}
require_once "ExifXmlMessage.php";
$x = new \Pydio\Core\Http\Response\SerializableResponseStream();
$x->addChunk(new ExifXmlMessage($filteredData));
$responseInterface = $responseInterface->withBody($x);
}
public function string_format($str)
{
$tmpStr = "";
for ($i=0;$i<strlen($str);$i++) {
if (ord($str[$i]) !=0) {
$tmpStr .= $str[$i];
}
}
return $tmpStr;
}
/**
* @param \Pydio\Access\Core\Model\AJXP_Node $ajxpNode
*/
public function extractMeta(&$ajxpNode)
{
$currentFile = $ajxpNode->getUrl();
if(!$ajxpNode->isLeaf() || preg_match("/\.zip\//",$currentFile)) return ;
if(!preg_match("/\.jpg$|\.jpeg$|\.tif$|\.tiff$/i",$currentFile)) return ;
$definitions = $this->getMetaDefinition();
if(!count($definitions)) return ;
if(!function_exists("exif_read_data")) return ;
//if(!exif_imagetype($currentFile)) return ;
$realFile = $ajxpNode->getRealFile();
$exif = @exif_read_data($realFile, 0, TRUE);
$iptc = $this->extractIPTC($realFile);
if($exif === false) return ;
$additionalMeta = array();
foreach ($definitions as $def => $label) {
list($exifSection, $exifName) = explode("-", $def);
if ($exifSection == "COMPUTED_GPS" && !isSet($exif["COMPUTED_GPS"])) {
$exif['COMPUTED_GPS'] = $this->convertGPSData($exif);
}
if (isSet($exif[$exifSection]) && isSet($exif[$exifSection][$exifName])) {
$additionalMeta[$def] = $exif[$exifSection][$exifName];
}
if ($exifSection == "IPTC" && isSet($iptc[$exifName])){
$additionalMeta[$def] = $iptc[$exifName];
}
}
$ajxpNode->mergeMetadata($additionalMeta);
}
private function extractIPTC($realFile){
$output = array();
if(!function_exists("iptcparse")) {
return $output;
}
getimagesize($realFile,$info);
if(!isset($info['APP13'])) {
return $output;
}
$iptcHeaderArray = array
(
'2#005'=>'DocumentTitle',
'2#010'=>'Urgency',
'2#015'=>'Category',
'2#020'=>'Subcategories',
'2#025'=>'Keywords',
'2#040'=>'SpecialInstructions',
'2#055'=>'CreationDate',
'2#080'=>'AuthorByline',
'2#085'=>'AuthorTitle',
'2#090'=>'City',
'2#095'=>'State',
'2#101'=>'Country',
'2#103'=>'OTR',
'2#105'=>'Headline',
'2#110'=>'Source',
'2#115'=>'PhotoSource',
'2#116'=>'Copyright',
'2#120'=>'Caption',
'2#122'=>'CaptionWriter'
);
$iptc =iptcparse($info['APP13']);
if (!is_array($iptc)) {
return $output;
}
foreach (array_keys($iptc) as $key) {
if (isSet($iptcHeaderArray[$key])) {
$cnt = count($iptc[$key]);
$val = "";
for ($i=0; $i < $cnt; $i++) $val .= $iptc[$key][$i] . " ";
$output[$iptcHeaderArray[$key]] = preg_replace( '/[^[:print:]]/', '',$val);
}
}
return $output;
}
private function convertGPSData($exif)
{
if(!isSet($exif["GPS"])) return array();
require_once(AJXP_INSTALL_PATH."/plugins/meta.exif/class.GeoConversion.php");
$converter = new GeoConversion();
$latDeg=@$this->parseGPSValue($exif["GPS"]["GPSLatitude"][0]);
$latMin=@$this->parseGPSValue($exif["GPS"]["GPSLatitude"][1]);
$latSec=@$this->parseGPSValue($exif["GPS"]["GPSLatitude"][2]);
$latHem=$exif["GPS"]["GPSLatitudeRef"];
$longDeg=@$this->parseGPSValue($exif["GPS"]["GPSLongitude"][0]);
$longMin=@$this->parseGPSValue($exif["GPS"]["GPSLongitude"][1]);
$longSec=@$this->parseGPSValue($exif["GPS"]["GPSLongitude"][2]);
$longRef=$exif["GPS"]["GPSLongitudeRef"];
$latSign = ($latHem == "S" ? "-":"");
$longSign = ($longRef == "W" ? "-":"");
$gpsData = array(
"GPS_Latitude"=>"$latDeg deg $latMin' $latSec $latHem--".$converter->DMS2Dd($latSign.$latDeg."o$latMin'$latSec"),
"GPS_Longitude"=>"$longDeg deg $longMin' $longSec $longRef--".$converter->DMS2Dd($longSign.$longDeg."o$longMin'$longSec"),
"GPS_Altitude"=> $exif["GPS"]["GPSAltitude"][0]
);
return $gpsData;
}
private function parseGPSValue($value)
{
if (strstr($value, "/") === false) {
return floatval($value);
} else {
$exp = explode("/", $value);
return round(intval($exp[0])/intval($exp[1]), 4);
}
}
}