source: trunk/include/functions_metadata.inc.php @ 19703

Last change on this file since 19703 was 19703, checked in by plg, 11 years ago

update Piwigo headers to 2013 (the end of the world didn't occur as expected on r12922)

  • Property svn:eol-style set to LF
File size: 4.6 KB
Line 
1<?php
2// +-----------------------------------------------------------------------+
3// | Piwigo - a PHP based photo gallery                                    |
4// +-----------------------------------------------------------------------+
5// | Copyright(C) 2008-2013 Piwigo Team                  http://piwigo.org |
6// | Copyright(C) 2003-2008 PhpWebGallery Team    http://phpwebgallery.net |
7// | Copyright(C) 2002-2003 Pierrick LE GALL   http://le-gall.net/pierrick |
8// +-----------------------------------------------------------------------+
9// | This program is free software; you can redistribute it and/or modify  |
10// | it under the terms of the GNU General Public License as published by  |
11// | the Free Software Foundation                                          |
12// |                                                                       |
13// | This program is distributed in the hope that it will be useful, but   |
14// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
15// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
16// | General Public License for more details.                              |
17// |                                                                       |
18// | You should have received a copy of the GNU General Public License     |
19// | along with this program; if not, write to the Free Software           |
20// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
21// | USA.                                                                  |
22// +-----------------------------------------------------------------------+
23
24/**
25 * returns informations from IPTC metadata, mapping is done at the beginning
26 * of the function
27 *
28 * @param string $filename
29 * @return array
30 */
31function get_iptc_data($filename, $map)
32{
33  $result = array();
34
35  $imginfo = array();
36  if (false == @getimagesize($filename, $imginfo) )
37  {
38    return $result;
39  }
40
41  if (isset($imginfo['APP13']))
42  {
43    $iptc = iptcparse($imginfo['APP13']);
44    if (is_array($iptc))
45    {
46      $rmap = array_flip($map);
47      foreach (array_keys($rmap) as $iptc_key)
48      {
49        if (isset($iptc[$iptc_key][0]))
50        {
51          if ($iptc_key == '2#025')
52          {
53            $value = implode(',',
54                             array_map('clean_iptc_value',$iptc[$iptc_key]));
55          }
56          else
57          {
58            $value = clean_iptc_value($iptc[$iptc_key][0]);
59          }
60
61          foreach (array_keys($map, $iptc_key) as $pwg_key)
62          {
63            // in case the origin of the photo is unsecure (user upload), we
64            // remove HTML tags to avoid XSS (malicious execution of
65            // javascript)
66            $result[$pwg_key] = strip_tags($value);
67          }
68        }
69      }
70    }
71  }
72  return $result;
73}
74
75/**
76 * return a cleaned IPTC value
77 *
78 * @param string value
79 * @return string
80 */
81function clean_iptc_value($value)
82{
83  // strip leading zeros (weird Kodak Scanner software)
84  while ( isset($value[0]) and $value[0] == chr(0))
85  {
86    $value = substr($value, 1);
87  }
88  // remove binary nulls
89  $value = str_replace(chr(0x00), ' ', $value);
90
91  if ( preg_match('/[\x80-\xff]/', $value) )
92  {
93    // apparently mac uses some MacRoman crap encoding. I don't know
94    // how to detect it so a plugin should do the trick.
95    $value = trigger_event('clean_iptc_value', $value);
96    if ( ($qual = qualify_utf8($value)) != 0)
97    {// has non ascii chars
98      $value = convert_charset( $value,
99        $qual>0 ? 'utf-8' : 'iso-8859-1',
100        get_pwg_charset() );
101    }
102  }
103  return $value;
104}
105
106/**
107 * returns informations from EXIF metadata, mapping is done at the beginning
108 * of the function
109 *
110 * @param string $filename
111 * @return array
112 */
113function get_exif_data($filename, $map)
114{
115  $result = array();
116
117  if (!function_exists('read_exif_data'))
118  {
119    die('Exif extension not available, admin should disable exif use');
120  }
121
122  // Read EXIF data
123  if ($exif = @read_exif_data($filename))
124  {
125    $exif = trigger_event('format_exif_data', $exif, $filename, $map );
126    foreach ($map as $key => $field)
127    {
128      if (strpos($field, ';') === false)
129      {
130        if (isset($exif[$field]))
131        {
132          $result[$key] = $exif[$field];
133        }
134      }
135      else
136      {
137        $tokens = explode(';', $field);
138        if (isset($exif[$tokens[0]][$tokens[1]]))
139        {
140          $result[$key] = $exif[$tokens[0]][$tokens[1]];
141        }
142      }
143    }
144  }
145
146  foreach ($result as $key => $value)
147  {
148    // in case the origin of the photo is unsecure (user upload), we remove
149    // HTML tags to avoid XSS (malicious execution of javascript)
150    $result[$key] = strip_tags($value);
151  }
152
153  return $result;
154}
155?>
Note: See TracBrowser for help on using the repository browser.