aboutsummaryrefslogtreecommitdiffstats
path: root/admin/include/functions_metadata.php
blob: 60a826774feff8ceee055a79b22c730b12067ad3 (plain)
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
<?php
// +-----------------------------------------------------------------------+
// |                          functions_metadata.php                       |
// +-----------------------------------------------------------------------+
// | application   : PhpWebGallery <http://phpwebgallery.net>              |
// | branch        : BSF (Best So Far)                                     |
// +-----------------------------------------------------------------------+
// | file          : $RCSfile$
// | last update   : $Date$
// | last modifier : $Author$
// | revision      : $Revision$
// +-----------------------------------------------------------------------+
// | This program 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                                          |
// |                                                                       |
// | This program 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 this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

include_once(PHPWG_ROOT_PATH.'/include/functions_metadata.inc.php');

function get_sync_iptc_data($file)
{
  $map = array(
    'keywords'        => '2#025',
    'date_creation'   => '2#055',
    'author'          => '2#122',
    'name'            => '2#085',
    'comment'         => '2#120'
    );
  $datefields = array('date_creation', 'date_available');
  
  $iptc = get_iptc_data($file, $map);

  foreach ($iptc as $pwg_key => $value)
  {
    if (in_array($pwg_key, $datefields))
    {
      if ( preg_match('/(\d{4})(\d{2})(\d{2})/', $value, $matches))
      {
        $iptc[$pwg_key] = $matches[1].'-'.$matches[2].'-'.$matches[3];
      }
    }
  }

  if (isset($iptc['keywords']))
  {
    // keywords separator is the comma, nothing else. Allowed characters in
    // keywords : [A-Za-z0-9], "-" and "_". All other characters will be
    // considered as separators
    $iptc['keywords'] = preg_replace('/[^\w-]+/', ',', $iptc['keywords']);
    $iptc['keywords'] = preg_replace('/^,+|,+$/', '', $iptc['keywords']);
  }

  return $iptc;
}

function update_metadata($files)
{
  global $conf;

  if (!defined('CURRENT_DATE'))
  {
    define('CURRENT_DATE', "'".date('Y-m-d')."'");
  }

  $inserts = array();

  foreach ($files as $id => $file)
  {
    $insert = array();
    $insert['id'] = $id;
    $insert['filesize'] = floor(filesize($file)/1024);
  
    if ($image_size = @getimagesize($file))
    {
      $insert['width'] = $image_size[0];
      $insert['height'] = $image_size[1];
    }
  
    if ($conf['use_exif'])
    {
      if ($exif = @read_exif_data($file))
      {
        if (isset($exif['DateTime']))
        {
          preg_match('/^(\d{4}).(\d{2}).(\d{2})/'
                     ,$exif['DateTime']
                     ,$matches);
          $insert['date_creation'] =
            "'".$matches[1].'-'.$matches[2].'-'.$matches[3]."'";
        }
      }
    }

    if ($conf['use_iptc'])
    {
      $iptc = get_sync_iptc_data($file);
      if (count($iptc) > 0)
      {
        foreach (array_keys($iptc) as $key)
        {
          $insert[$key] = "'".addslashes($iptc[$key])."'";
        }
      }
    }

    $insert['date_metadata_update'] = CURRENT_DATE;

    array_push($inserts, $insert);
  }
  
  if (count($inserts) > 0)
  {
    $dbfields = array(
      'id','filesize','width','height','name','author','comment'
      ,'date_creation','keywords','date_metadata_update'
      );

    // depending on the MySQL version, we use the multi table update or N
    // update queries
    $query = 'SELECT VERSION() AS version;';
    $row = mysql_fetch_array(mysql_query($query));
    if (version_compare($row['version'],'4.0.4') < 0)
    {
      // MySQL is prior to version 4.0.4, multi table update feature is not
      // available
      echo 'MySQL is prior to version 4.0.4, multi table update feature is not available<br />';
      foreach ($inserts as $insert)
      {
        $query = '
UPDATE '.IMAGES_TABLE.'
  SET ';
        foreach (array_diff(array_keys($insert),array('id')) as $num => $key)
        {
          if ($num > 1)
          {
            $query.= ', ';
          }
          $query.= $key.' = '.$insert[$key];
        }
        $query.= '
  WHERE id = '.$insert['id'].'
;';
        // echo '<pre>'.$query.'</pre>';
        mysql_query($query);
      }
    }
    else
    {
      // creation of the temporary table
      $query = '
DESCRIBE '.IMAGES_TABLE.'
;';
      $result = mysql_query($query);
      $columns = array();
      while ($row = mysql_fetch_array($result))
      {
        if (in_array($row['Field'], $dbfields))
        {
          $column = $row['Field'];
          $column.= ' '.$row['Type'];
          if (!isset($row['Null']) or $row['Null'] == '')
          {
            $column.= ' NOT NULL';
          }
          if (isset($row['Default']))
          {
            $column.= " default '".$row['Default']."'";
          }
          array_push($columns, $column);
        }
      }
      $query = '
CREATE TEMPORARY TABLE '.IMAGE_METADATA_TABLE.'
(
'.implode(",\n", $columns).',
PRIMARY KEY (id)
)
;';
      // echo '<pre>'.$query.'</pre>';
      mysql_query($query);
      // inserts all found pictures
      $query = '
INSERT INTO '.IMAGE_METADATA_TABLE.'
  ('.implode(',', $dbfields).')
   VALUES
   ';
      foreach ($inserts as $insert_id => $insert)
      {
        $query.= '
';
        if ($insert_id > 0)
        {
          $query.= ',';
        }
        $query.= '(';
        foreach ($dbfields as $field_id => $dbfield)
        {
          if ($field_id > 0)
          {
            $query.= ',';
          }
          
          if (!isset($insert[$dbfield]) or $insert[$dbfield] == '')
          {
            $query.= 'NULL';
          }
          else
          {
            $query.= $insert[$dbfield];
          }
        }
        $query.=')';
      }
      $query.= '
;';
      // echo '<pre>'.$query.'</pre>';
      mysql_query($query);
      // update of images table by joining with temporary table
      $query = '
UPDATE '.IMAGES_TABLE.' AS images, '.IMAGE_METADATA_TABLE.' as metadata
  SET '.implode("\n    , ",
                array_map(
                  create_function('$s', 'return "images.$s = metadata.$s";')
                  , array_diff($dbfields, array('id')))).'
  WHERE images.id = metadata.id
;';
      echo '<pre>'.$query.'</pre>';
      mysql_query($query);
    }
  }
}

/**
 * returns an array associating element id (images.id) with its complete
 * path in the filesystem
 *
 * @param int id_uppercat
 * @param boolean recursive ?
 * @param boolean only newly added files ?
 * @return array
 */
function get_filelist($category_id = '', $recursive = false, $only_new = false)
{
  $files = array();

  $query = '
SELECT id, dir
  FROM '.CATEGORIES_TABLE.'
  WHERE dir IS NOT NULL
;';
  $result = mysql_query($query);
  $cat_dirs = array();
  while ($row = mysql_fetch_array($result))
  {
    $cat_dirs[$row['id']] = $row['dir'];
  }

  // filling $uppercats_array : to each category id the uppercats list is
  // associated
  $uppercats_array = array();
  
  $query = '
SELECT id, uppercats
  FROM '.CATEGORIES_TABLE.'
  WHERE site_id = 1
    AND dir IS NOT NULL';
  if (is_numeric($category_id))
  {
    if ($recursive)
    {
      $query.= '
    AND uppercats REGEXP \'(^|,)'.$category_id.'(,|$)\'
';
    }
    else
    {
      $query.= '
    AND id = '.$category_id.'
';
    }
  }
  $query.= '
;';
  $result = mysql_query($query);
  while ($row = mysql_fetch_array($result))
  {
    $uppercats_array[$row['id']] =  $row['uppercats'];
  }

  $query = '
SELECT galleries_url
  FROM '.SITES_TABLE.'
  WHERE id = 1
';
  $row = mysql_fetch_array(mysql_query($query));
  $basedir = $row['galleries_url'];
  
  // filling $cat_fulldirs
  $cat_fulldirs = array();
  foreach ($uppercats_array as $cat_id => $uppercats)
  {
    $uppercats = str_replace(',', '/', $uppercats);
    $cat_fulldirs[$cat_id] = $basedir.preg_replace('/(\d+)/e',
                                                   "\$cat_dirs['$1']",
                                                   $uppercats);
  }

  $query = '
SELECT id, file, storage_category_id
  FROM '.IMAGES_TABLE.'
  WHERE storage_category_id IN ('.implode(','
                                          ,array_keys($uppercats_array)).')';
  if ($only_new)
  {
    $query.= '
    AND date_metadata_update IS NULL
';
  }
  $query.= '
;';
  $result = mysql_query($query);
  while ($row = mysql_fetch_array($result))
  {
    $files[$row['id']]
      = $cat_fulldirs[$row['storage_category_id']].'/'.$row['file'];
  }
  
  return $files;
}
?>