[493] | 1 | <?php |
---|
| 2 | // +-----------------------------------------------------------------------+ |
---|
[8728] | 3 | // | Piwigo - a PHP based photo gallery | |
---|
[2297] | 4 | // +-----------------------------------------------------------------------+ |
---|
[19703] | 5 | // | Copyright(C) 2008-2013 Piwigo Team http://piwigo.org | |
---|
[2297] | 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 | // +-----------------------------------------------------------------------+ |
---|
[493] | 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 | */ |
---|
| 31 | function get_iptc_data($filename, $map) |
---|
| 32 | { |
---|
[22661] | 33 | global $conf; |
---|
| 34 | |
---|
[493] | 35 | $result = array(); |
---|
[1113] | 36 | |
---|
[493] | 37 | $imginfo = array(); |
---|
[1711] | 38 | if (false == @getimagesize($filename, $imginfo) ) |
---|
| 39 | { |
---|
| 40 | return $result; |
---|
| 41 | } |
---|
[1113] | 42 | |
---|
[493] | 43 | if (isset($imginfo['APP13'])) |
---|
| 44 | { |
---|
| 45 | $iptc = iptcparse($imginfo['APP13']); |
---|
| 46 | if (is_array($iptc)) |
---|
| 47 | { |
---|
| 48 | $rmap = array_flip($map); |
---|
| 49 | foreach (array_keys($rmap) as $iptc_key) |
---|
| 50 | { |
---|
[611] | 51 | if (isset($iptc[$iptc_key][0])) |
---|
[493] | 52 | { |
---|
[611] | 53 | if ($iptc_key == '2#025') |
---|
[493] | 54 | { |
---|
[611] | 55 | $value = implode(',', |
---|
| 56 | array_map('clean_iptc_value',$iptc[$iptc_key])); |
---|
[493] | 57 | } |
---|
[611] | 58 | else |
---|
| 59 | { |
---|
| 60 | $value = clean_iptc_value($iptc[$iptc_key][0]); |
---|
| 61 | } |
---|
| 62 | |
---|
[493] | 63 | foreach (array_keys($map, $iptc_key) as $pwg_key) |
---|
| 64 | { |
---|
[22661] | 65 | $result[$pwg_key] = $value; |
---|
| 66 | |
---|
| 67 | if (!$conf['allow_html_in_metadata']) |
---|
| 68 | { |
---|
| 69 | // in case the origin of the photo is unsecure (user upload), we |
---|
| 70 | // remove HTML tags to avoid XSS (malicious execution of |
---|
| 71 | // javascript) |
---|
| 72 | $result[$pwg_key] = strip_tags($result[$pwg_key]); |
---|
| 73 | } |
---|
[493] | 74 | } |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | return $result; |
---|
| 80 | } |
---|
[611] | 81 | |
---|
| 82 | /** |
---|
| 83 | * return a cleaned IPTC value |
---|
| 84 | * |
---|
| 85 | * @param string value |
---|
| 86 | * @return string |
---|
| 87 | */ |
---|
| 88 | function clean_iptc_value($value) |
---|
| 89 | { |
---|
| 90 | // strip leading zeros (weird Kodak Scanner software) |
---|
[1113] | 91 | while ( isset($value[0]) and $value[0] == chr(0)) |
---|
[611] | 92 | { |
---|
| 93 | $value = substr($value, 1); |
---|
| 94 | } |
---|
| 95 | // remove binary nulls |
---|
| 96 | $value = str_replace(chr(0x00), ' ', $value); |
---|
[1113] | 97 | |
---|
[2132] | 98 | if ( preg_match('/[\x80-\xff]/', $value) ) |
---|
| 99 | { |
---|
| 100 | // apparently mac uses some MacRoman crap encoding. I don't know |
---|
| 101 | // how to detect it so a plugin should do the trick. |
---|
| 102 | $value = trigger_event('clean_iptc_value', $value); |
---|
[17748] | 103 | if ( ($qual = qualify_utf8($value)) != 0) |
---|
| 104 | {// has non ascii chars |
---|
| 105 | $value = convert_charset( $value, |
---|
| 106 | $qual>0 ? 'utf-8' : 'iso-8859-1', |
---|
| 107 | get_pwg_charset() ); |
---|
| 108 | } |
---|
[2132] | 109 | } |
---|
[611] | 110 | return $value; |
---|
| 111 | } |
---|
[858] | 112 | |
---|
| 113 | /** |
---|
| 114 | * returns informations from EXIF metadata, mapping is done at the beginning |
---|
| 115 | * of the function |
---|
| 116 | * |
---|
| 117 | * @param string $filename |
---|
| 118 | * @return array |
---|
| 119 | */ |
---|
| 120 | function get_exif_data($filename, $map) |
---|
| 121 | { |
---|
[22661] | 122 | global $conf; |
---|
| 123 | |
---|
[858] | 124 | $result = array(); |
---|
| 125 | |
---|
| 126 | if (!function_exists('read_exif_data')) |
---|
| 127 | { |
---|
| 128 | die('Exif extension not available, admin should disable exif use'); |
---|
| 129 | } |
---|
[1113] | 130 | |
---|
[858] | 131 | // Read EXIF data |
---|
| 132 | if ($exif = @read_exif_data($filename)) |
---|
| 133 | { |
---|
[12254] | 134 | $exif = trigger_event('format_exif_data', $exif, $filename, $map ); |
---|
[6484] | 135 | foreach ($map as $key => $field) |
---|
| 136 | { |
---|
| 137 | if (strpos($field, ';') === false) |
---|
| 138 | { |
---|
| 139 | if (isset($exif[$field])) |
---|
| 140 | { |
---|
| 141 | $result[$key] = $exif[$field]; |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | else |
---|
| 145 | { |
---|
| 146 | $tokens = explode(';', $field); |
---|
| 147 | if (isset($exif[$tokens[0]][$tokens[1]])) |
---|
| 148 | { |
---|
| 149 | $result[$key] = $exif[$tokens[0]][$tokens[1]]; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
[858] | 153 | } |
---|
| 154 | |
---|
[22661] | 155 | if (!$conf['allow_html_in_metadata']) |
---|
[19418] | 156 | { |
---|
[22661] | 157 | foreach ($result as $key => $value) |
---|
| 158 | { |
---|
| 159 | // in case the origin of the photo is unsecure (user upload), we remove |
---|
| 160 | // HTML tags to avoid XSS (malicious execution of javascript) |
---|
| 161 | $result[$key] = strip_tags($value); |
---|
| 162 | } |
---|
[19418] | 163 | } |
---|
| 164 | |
---|
[858] | 165 | return $result; |
---|
| 166 | } |
---|
[493] | 167 | ?> |
---|