1 | <?php |
---|
2 | if (!defined('PHPWG_ROOT_PATH')) die('Hacking attempt!'); |
---|
3 | |
---|
4 | function parse_fract( $f ) |
---|
5 | { |
---|
6 | $nd = explode( '/', $f ); |
---|
7 | return $nd[0]/$nd[1]; |
---|
8 | } |
---|
9 | |
---|
10 | function parse_lat_lon( $arr ) |
---|
11 | { |
---|
12 | $v=0; |
---|
13 | $v += parse_fract( $arr[0] ); |
---|
14 | $v += parse_fract( $arr[1] )/60; |
---|
15 | $v += parse_fract( $arr[2] )/3600; |
---|
16 | return $v; |
---|
17 | } |
---|
18 | |
---|
19 | if ( isset($_POST['submit']) ) |
---|
20 | $sync_options = array( |
---|
21 | 'simulate' => isset($_POST['simulate']), |
---|
22 | 'use_high' => isset($_POST['use_high']), |
---|
23 | 'cat_id' => isset($_POST['cat_id']) ? (int)$_POST['cat_id'] : 0, |
---|
24 | 'subcats_included' => isset($_POST['subcats_included']), |
---|
25 | ); |
---|
26 | else |
---|
27 | $sync_options = array( |
---|
28 | 'simulate' => true, |
---|
29 | 'use_high' => true, |
---|
30 | 'cat_id' => 0, |
---|
31 | 'subcats_included' => true, |
---|
32 | ); |
---|
33 | |
---|
34 | |
---|
35 | if ( isset($_POST['submit']) ) |
---|
36 | { |
---|
37 | include_once(PHPWG_ROOT_PATH.'include/functions_picture.inc.php'); |
---|
38 | |
---|
39 | $where_clauses = array(); |
---|
40 | if ( $sync_options['cat_id']!=0 ) |
---|
41 | { |
---|
42 | $query=' |
---|
43 | SELECT id FROM '.CATEGORIES_TABLE.' |
---|
44 | WHERE '; |
---|
45 | if ( $sync_options['subcats_included']) |
---|
46 | $query .= 'uppercats REGEXP \'(^|,)'.$sync_options['cat_id'].'(,|$)\''; |
---|
47 | else |
---|
48 | $query .= 'id='.$sync_options['cat_id']; |
---|
49 | $cat_ids = array_from_query($query, 'id'); |
---|
50 | |
---|
51 | $query=' |
---|
52 | SELECT id,path,lat,lon,has_high |
---|
53 | FROM '.IMAGES_TABLE.' INNER JOIN '.IMAGE_CATEGORY_TABLE.' ON id=image_id |
---|
54 | WHERE category_id IN ('.implode(',', $cat_ids).') |
---|
55 | GROUP BY id'; |
---|
56 | } |
---|
57 | else |
---|
58 | { |
---|
59 | $query=' |
---|
60 | SELECT id,path,lat,lon,has_high |
---|
61 | FROM '.IMAGES_TABLE; |
---|
62 | } |
---|
63 | |
---|
64 | $images = hash_from_query( $query, 'id'); |
---|
65 | $datas = array(); |
---|
66 | $errors = array(); |
---|
67 | foreach ($images as $image) |
---|
68 | { |
---|
69 | $filename = $image['path']; |
---|
70 | if ($sync_options['use_high'] and $image['has_high']=='true') |
---|
71 | $filename = get_high_path($image); |
---|
72 | $exif = @read_exif_data( $filename ); |
---|
73 | if ( empty($exif) ) |
---|
74 | continue; |
---|
75 | $exif = array_intersect_key( $exif, array_flip( array('GPSLatitudeRef', 'GPSLatitude', 'GPSLongitudeRef', 'GPSLongitude') ) ); |
---|
76 | if ( count($exif)!=4 ) |
---|
77 | continue; |
---|
78 | if ( !in_array($exif['GPSLatitudeRef'], array('S', 'N') ) ) |
---|
79 | { |
---|
80 | $errors[] = $filename. ': GPSLatitudeRef not S or N'; |
---|
81 | continue; |
---|
82 | } |
---|
83 | if ( !in_array($exif['GPSLongitudeRef'], array('W', 'E') ) ) |
---|
84 | { |
---|
85 | $errors[] = $filename. ': GPSLongitudeRef not W or E'; |
---|
86 | continue; |
---|
87 | } |
---|
88 | if (!is_array($exif['GPSLatitude']) or !is_array($exif['GPSLongitude']) ) |
---|
89 | { |
---|
90 | $errors[] = $filename. ': GPSLatitude and GPSLongitude are not arrays'; |
---|
91 | continue; |
---|
92 | } |
---|
93 | $lat = parse_lat_lon( $exif['GPSLatitude'] ); |
---|
94 | if ( $exif['GPSLatitudeRef']=='S' ) |
---|
95 | $lat = -$lat; |
---|
96 | $lon = parse_lat_lon( $exif['GPSLongitude'] ); |
---|
97 | if ( $exif['GPSLongitudeRef']=='W' ) |
---|
98 | $lon = -$lon; |
---|
99 | $datas[] = array ( |
---|
100 | 'id' => $image['id'], |
---|
101 | 'lat' => $lat, |
---|
102 | 'lon' => $lon, |
---|
103 | ); |
---|
104 | } |
---|
105 | $template->assign( 'sync_errors', $errors ); |
---|
106 | |
---|
107 | if ( count($datas)>0 and !$sync_options['simulate'] ) |
---|
108 | { |
---|
109 | mass_updates( |
---|
110 | IMAGES_TABLE, |
---|
111 | array( |
---|
112 | 'primary' => array('id'), |
---|
113 | 'update' => array('lat', 'lon') |
---|
114 | ), |
---|
115 | $datas |
---|
116 | ); |
---|
117 | rvm_invalidate_cache(); |
---|
118 | } |
---|
119 | |
---|
120 | $template->assign( |
---|
121 | 'metadata_result', |
---|
122 | array( |
---|
123 | 'NB_ELEMENTS_DONE' => count($datas), |
---|
124 | 'NB_ELEMENTS_CANDIDATES' => count($images), |
---|
125 | 'NB_ERRORS' => count($errors), |
---|
126 | )); |
---|
127 | |
---|
128 | } |
---|
129 | |
---|
130 | |
---|
131 | $query = ' |
---|
132 | SELECT id, |
---|
133 | CONCAT(name, IF(dir IS NULL, " (V)", "") ) AS name, |
---|
134 | uppercats, global_rank |
---|
135 | FROM '.CATEGORIES_TABLE; |
---|
136 | display_select_cat_wrapper($query, |
---|
137 | array( $sync_options['cat_id'] ), |
---|
138 | 'categories', |
---|
139 | false); |
---|
140 | $template->assign( |
---|
141 | array( |
---|
142 | 'SUBCATS_INCLUDED_CHECKED' => $sync_options['subcats_included'] ? 'checked="checked"' : '', |
---|
143 | 'USE_HIGH_CHECKED' => $sync_options['use_high'] ? 'checked="checked"' : '' |
---|
144 | ) |
---|
145 | ); |
---|
146 | |
---|
147 | ?> |
---|