1<?php
2
3if(!defined('DOKU_INC')) die();
4require_once(DOKU_PLUGIN.'action.php');
5
6class action_plugin_mediatooltip extends DokuWiki_Action_Plugin {
7   private $fields;
8   private $toolTipOptions;
9 function register(Doku_Event_Handler $controller) {
10       $controller->register_hook('MEDIA_UPLOAD_FINISH', 'BEFORE', $this, '_media_finish');
11       $controller->register_hook('TPL_CONTENT_DISPLAY', 'BEFORE', $this, '_insert_exif');
12 }
13
14   function __construct() {
15      $this->init_fields();
16      $this->toolTipOptions = $this->getConf('fields');
17      if(!empty($this->toolTipOptions)) $this->toolTipOptions = explode(',',$this->toolTipOptions);
18   }
19
20 /*
21 Array
22(
23    [FileModified] => 1541022351
24    [Time] => 1541022351
25    [TimeSource] =>.
26    [TimeStr] => 2018-10-31 16:45:51
27    [EarliestTime] => 1541022351
28    [EarliestTimeSource] =>.
29    [EarliestTimeStr] => 2018-10-31 16:45:51
30    [LatestTime] => 1541022351
31    [LatestTimeSource] => FileModified
32    [LatestTimeStr] => 2018-10-31 16:45:51
33)
34 */
35
36 function _media_finish(Doku_Event $event, $param) {
37     global   $USERINFO, $INPUT;
38     $this->setupLocale();
39
40     $meta = new JpegMeta($event->data[0]);
41      $camera = $meta->getCamera();
42     if(!$camera) return;
43
44     $dt = $this->date_time($event->data[0]);
45     $user =$this->_user_name();
46     if(!$dt  && !$user)  return;
47     if($user) {
48        $user = '_' . $user . '.';
49     }
50     else $user = '.';
51     if($dt) {
52         $dt .= '_';
53     }
54    $parts = pathinfo($event->data[1]);
55    $parts['filename'] = trim($parts['filename'],'_');
56    $file_name = $dt . $parts['filename'] . $user . $parts['extension'];
57
58    $path =  $parts['dirname'] . "/$file_name";
59
60    $ow = $INPUT->str('ow');
61    if(!empty($camera) &&  file_exists($path) && $ow == 'false') {
62         $event->preventDefault();
63    }
64     $event->data[1] = $path;
65
66   }
67
68   /*set up user and client (user's real name)  */
69  function _user_name()  {
70      global   $USERINFO;
71
72     $user = "";
73     $dw_session = array_keys($_SESSION);
74     $auth =$_SESSION[$dw_session[0]];
75
76     $use_real = $this->getConf('enable_real');
77     $photogroups = $this->getConf('groups');
78
79
80     if(!empty($photogroups) ) {
81         if(isset($auth)) {
82            $user_grps =  $auth['auth']['info']['grps'];
83         }
84         else if(isset($USERINFO) ) {
85            $user_grps =  $auth['auth']['info']['grps'];
86         }
87
88         if(!empty($user_grps)) {
89             foreach($user_grps as $grp) {
90               if(strpos($photogroups,$grp) !== false) {
91                   return $grp;
92               }
93             }
94         }
95      }
96     if(isset($auth)) {
97        if($this->getConf('enable_userid')) {
98            $user =  $auth['auth']['user'];
99        }
100        else if($use_real) {
101            $user =  $auth['auth']['info']['name'];
102            $user=preg_replace('/\s+/', '_',$user);
103        }
104     }
105     if($use_real && empty($user) && isset($USERINFO)) {
106            $client_name = $USERINFO['name'];
107            if(!empty($client_name))
108                $client_name = preg_replace('/\s+/', '_',$client_name);
109                if(empty($user)) {
110                    $user = $client_name;
111                }
112           }
113
114     if(!empty($user)) {
115         if(function_exists ( 'utf8_strtolower' )) {
116             $user = utf8_strtolower($user);
117         }
118         else $user = strtolower($user);
119     }
120     return $user;
121  }
122
123 function date_time($upload_path) {
124     $date_style = $this->getConf('date_style');
125     if($date_style == 'none') return "";
126
127     $meta = new JpegMeta($upload_path);
128     $dates = $meta->getDates();
129     $time_str = $dates['TimeStr'];
130     list($dt,$tm) = preg_split("/\s/",$time_str);
131     if($date_style=='date') return $dt;
132     if(strpos($date_style,'s')== false) {
133        $tm = preg_replace("/:\d+$/","",$tm,1);
134     }
135     $time_format = $this->getConf('time_format');
136     $tmsep = ($time_format == 'hour.min.sec') ? '.': ($time_format == 'hour-min-sec' ? '-' : "_");
137     $tm = str_replace(':',$tmsep,$tm);
138     $dt=$dt . '_' .$tm;
139     return $dt;
140 }
141
142function _insert_exif(Doku_Event $event) {
143   if(empty($this->toolTipOptions)) {
144       return;
145  }
146
147    $event->data = preg_replace_callback(
148         "/title=\"([^\"]+\.(jpg|jpeg|tiff))/i",
149        function ($matches) {
150             list($_pre,$_img) = explode('=',$matches[0]); // $matchs[0] has complete path to image
151             $_img = trim($_img,'"');
152
153             $meta = new JpegMeta(mediaFN($_img));
154                        /* if filename is omitted, then an artist and/or title is required */
155              $useFileName = in_array('File',$this->toolTipOptions);
156
157              if(!$useFileName && $this->getFieldValue('Artist',$meta))
158               {
159                 $matches[0] = 'title="';
160                 $BR = "";
161              }
162            else $BR = '<br>';
163
164            if(in_array('Camera', $this->toolTipOptions)) {
165             $camera = $meta->getCamera();
166             $camera = trim($camera);
167            }
168            else $camera = "";
169
170            if(in_array('Date', $this->toolTipOptions)) {
171             $dates = $meta->getDates();
172             $time_str = $dates['TimeStr'];
173            }
174            else $time_str = "";
175
176            if(in_array('ImgSize', $this->toolTipOptions)) {
177                $w = $this->getFieldValue('Width',$meta);
178                $h = $this->getFieldValue('Height',$meta);
179                if(!empty($w) && !empty($h)) {
180                $matches[0] .= '" data-size ="' . $this->format_attribute("$w X $h pixels");
181            }
182            }
183
184            if(in_array('FileSize', $this->toolTipOptions)) {
185                $matches[0] .= ', " data-fsize ="' . $this->format_attribute($this->getFieldValue('FileSize',$meta));
186            }
187
188            if($camera) {
189                                      //remove sometimes duplicate camera name
190             $camera = preg_replace("/\b(\w+)(?:\s+\\1\b)/i", "$1", $camera);
191             $speed =  $meta->_info['exif']['ExposureTime']['val'];//getShutterSpeed();
192			 $fstop =  $meta->_info['exif']['FNumber']['val'];
193             if($fstop) {
194			     $camera .= "&nbsp;&nbsp;F:$fstop @ $speed sec.";
195             }
196            }
197            if($time_str) {
198             $camera .= "&nbsp;&nbsp;&nbsp;tm=$time_str";
199            }
200
201            if(in_array('Artist', $this->toolTipOptions)) {
202              $artist = $this->getFieldValue('Artist',$meta);
203            }
204            if($_img)  {
205                $_title = $_img;
206            }
207            else $_title = $meta->getTitle();
208
209             if(!empty($artist) && !empty($_title)) {
210                 $matches[0] .=  "&nbsp;$BR" . 'Artist: ' . trim($artist);
211                 if(!empty($_title) && strpos($matches[0],$_title) === false)
212                      $matches[0] .= ",&nbsp;" . $_title;
213             }
214             else if(!empty($artist)) {
215                $matches[0] .=  $this->getLang('creator') . $artist;
216             }
217             elseif(!$useFileName && !empty($_title)) {
218                 $matches[0] .=  trim($_title);
219             }
220
221            $captionpos = $this->getConf('captionpos');
222            if(in_array('Caption', $this->toolTipOptions)) {
223             $caption =  $this->getFieldValue('Caption',$meta);
224             if(!empty($caption) && $caption != $_title) {
225                 $matches[0] .= '" data-caption ="' .  "$captionpos::" . $this->format_attribute($caption);
226             }
227            }
228           if($camera) {
229             $matches[0] .= '"  data-rel ="' .  $this->format_attribute($camera);
230           }
231
232           if(in_array('Copyright', $this->toolTipOptions)) {
233             $copy = $meta->_info['exif']['Copyright'];
234             if(empty($copy)) $copy = $this->getFieldValue('Copyright',$meta);
235           }
236
237             if(!empty($copy)) {
238                 $copypos = trim($this->getConf('copypos'));
239                 if($captionpos == 'screen') {
240                     $copypos = 'tooltip';
241                 }
242                 $matches[0] .= '" license="' .  "$copypos::" .$this->format_attribute($copy);
243             }
244             $matches[0] = preg_replace("/data-/","\n    data-",$matches[0]);
245          // msg(htmlentities($matches[0]),2);
246             return $matches [0];
247        },
248        $event->data
249    );
250       $event->data = str_replace('><img',">\n    <img",$event->data);
251}
252
253function format_attribute($value) {
254    $value = trim($value);
255    $value = htmlentities($value,ENT_DISALLOWED|ENT_QUOTES);
256    $value = preg_replace('/\s+/','&nbsp;',$value);
257    return $value;
258}
259
260function getFieldValue($field,$meta) {
261    $ar = $this->fields[$field];
262    foreach($ar AS $el)
263    {
264       if(is_array($el)) {
265         while(!empty($el)) {
266             $inner = array_shift($el);
267             $value = $meta->getField($inner);
268             if($value) return $value;
269         }
270       }
271       $value =  $meta->getField($el);
272
273       if($value) {
274         return $value;
275      }
276     }
277
278    return false;
279}
280
281function init_fields() {
282    $this->fields = array(
283    'Title' => array('Iptc.Headline',
284                'img_title',
285                'text'),
286
287    'Date' => array('',
288                'img_date',
289                'date',
290                array('Date.EarliestTime')),
291
292    'File' => array('',
293                'img_fname',
294                'text',
295                array('File.Name')),
296
297    'Caption' => array('Iptc.Caption',
298                'img_caption',
299                'textarea',
300                array('Exif.UserComment',
301                      'Exif.TIFFImageDescription',
302                      'Exif.TIFFUserComment')),
303
304   'Artist' => array('Iptc.Byline',
305                'img_artist',
306                'text',
307                array('Exif.TIFFArtist',
308                      'Exif.Artist',
309                      'Iptc.Credit')),
310
311    'Copyright' => array('Iptc.CopyrightNotice',
312                'img_copyr',
313                'text',
314                array('Exif.TIFFCopyright',
315                      'Exif.Copyright')),
316
317    'FileSize'  => array('',
318                'img_fsize',
319                'text',
320                 array('File.NiceSize')),
321
322    'Width'=> array('',
323                'img_width',
324                'text',
325                array('File.Width')),
326
327    'Height' => array('',
328                'img_height',
329                'text',
330                array('File.Height')),
331
332    'Camera' => array('',
333                'img_camera',
334                'text',
335                array('Simple.Camera')),
336
337     );
338/*
339Exif.ApertureValue
340Exif.MaxApertureValue
341Exif.ShutterSpeedValue
342Exif.XResolution
343Exif.YResolution
344*/
345}
346
347
348function write_debug($data) {
349   return;
350  if (!$handle = fopen(DOKU_INC .'uploadnename.txt', 'a')) {
351    return;
352    }
353  if(is_array($data) || is_object($data)) {
354     $data = print_r($data,true);
355  }
356    // Write $somecontent to our opened file.
357    fwrite($handle, "$data\n");
358    fclose($handle);
359
360}
361
362}
363
364
365
366
367
368
369
370
371
372
373
374
375