1<?php
2
3/**
4 * get user name an groups
5 * Works around bug in DokuWiki <= 2009-09-22 and also appends
6 * 'ALL' to list of groups to facilitate group access checking
7 *
8 * @author Chris Arndt <chris@chrisarndt.de>
9 */
10function sxs_get_user_info() {
11    global $INFO;
12    $user = array();
13    if ($_REQUEST['do'] == 'logout' or !isset($INFO['userinfo'])) {
14        $user['groups'] = array();
15    } else {
16        $user['groups'] = $INFO['userinfo']['grps'];
17        $user['name'] = $_SERVER['REMOTE_USER'];
18    }
19    array_push($user['groups'], 'ALL');
20    return $user;
21}
22
23/**
24 * check if current user should have access to given action
25 * Relies on certain configuration parameters to be present
26 *
27 * @author Chris Arndt <chris@chrisarndt.de>
28 */
29function sxs_check_action_perms($action, $perms, $user) {
30    global $conf;
31    $ac_lvl = $conf['sxs_act_ac_lvl'][$action];
32    if (!isset($ac_lvl) or $ac_lvl == '') { $ac_lvl = 255; }
33    if ($perms >= $ac_lvl) {
34        return true;
35    } elseif (!empty($conf['sxs_act_users'][$action])) {
36        $speclist = explode(',', $conf['sxs_act_users'][$action]);
37        $allowed_users = array();
38        $allowed_groups = array();
39        foreach ($speclist as $spec) {
40            $spec = trim($spec);
41            if (substr($spec, 0, 1) == '@') {
42                array_push($allowed_groups, substr($spec, 1));
43            } else {
44                array_push($allowed_users, $spec);
45            }
46        }
47        if (!empty($info['user']) and
48          in_array($user['name'], $allowed_users)) {
49            return true;
50        }
51        foreach ($user['groups'] as $group) {
52            if (in_array($group, $allowed_groups)) {
53                return true;
54            }
55        }
56    }
57    return false;
58}
59
60/**
61 * print button of link for given action depending on visibility settings
62 *
63 * @author Chris Arndt <chris@chrisarndt.de>
64 */
65function sxs_button($action, $perms, $user) {
66    global $conf;
67    if (sxs_check_action_perms($action, $perms, $user)) {
68        if ($conf['sxs_uselinks']) {
69            ptln('<li class="level1">');
70            tpl_actionlink($action);
71            ptln("</li>");
72        } else {
73            tpl_button($action);
74            ptln('<br>');
75        }
76    }
77}
78
79/**
80 * prints the contents of the command box in the sidebar
81 *
82 * @author Chris Arndt <chris@chrisarndt.de>
83 */
84function sxs_sitecmds($perms, $user) {
85    global $conf;
86    if ($conf['sxs_uselinks']) {
87        ptln('<ul>');
88    } else {
89        ptln('<div class="buttonbox">');
90    }
91    foreach ($conf['sxs_actions'] as $command) {
92        if ($command == '-') {
93            if ($conf['sxs_uselinks']) {
94                ptln('</ul>');
95                ptln('<ul>');
96            } else {
97                ptln('<br>');
98            }
99        } else {
100            sxs_button($command, $perms, $user);
101        }
102    }
103    if ($conf['sxs_uselinks']) {
104        ptln('</ul>');
105    } else {
106        ptln('</div>');
107    }
108}
109
110/**
111 * replacement for tpl_youarehere()
112 * Links namespaces to <namespace>:index
113 *
114 * taken from:
115 * <http://wiki.splitbrain.org/wiki:tips:breadcrumb_namespace_index_links>
116 */
117function sxs_youarehere() {
118    global $conf;
119    global $ID;
120    global $lang;
121
122    $parts = explode(':', $ID);
123    print hsc($lang['breadcrumb']).': ';
124
125    // Always show start page
126    if ($a_part[0] != $conf['start']) {
127        tpl_link(wl($conf['start']), $conf['start'],
128          'title="'.$conf['start'].'"');
129    }
130
131    $page = '';
132    $last = count($parts);
133    $count = 1;
134
135    foreach ($parts as $part) {
136        if ($count != $last || $part != $conf['sxs_index']) {
137            // Skip start page if already done
138            if ($part == $conf['start']) continue;
139            print $conf['sxs_crumbsep'];
140            $page .= $part;
141            // Remove underscores
142            if ($conf['sxs_removeunderscores'] == 1) {
143                $part = str_replace('_', ' ', $part);
144            }
145            // Set link variable
146            if ($count == $last) $link = $page;
147            else $link = "$page:" . $conf['sxs_index'];
148            // Check if page exists
149            if (file_exists(wikiFN($link))) {
150                tpl_link(wl($link), $part, 'title="'.$link.'"');
151            }
152            else {
153                // Print link, but mark as not-existing
154                tpl_link(wl($link), $part,
155                  'title="'.$link.'" class="wikilink2"');
156            }
157            $page .= ':';
158        }
159        $count++;
160    }
161}
162
163/**
164 * Show meta information for images
165 *
166 * @author Chris Arndt <chris@chrisarndt.de>
167 */
168function sxs_img_meta($debug=false) {
169    global $conf;
170    global $lang;
171    global $IMG;
172
173    /* change the order of fields in this array to change the order in which
174     * the image information tags are listed
175     */
176    $tags = array(
177      'img_caption' => array('IPTC.Caption', 'EXIF.UserComment',
178           'EXIF.TIFFImageDescription', 'EXIF.TIFFUserComment'),
179      'img_artist' => array('Iptc.Byline', 'Exif.TIFFArtist', 'Exif.Artist',
180        'Iptc.Credit'),
181      'img_keywords' => array('IPTC.Keywords','IPTC.Category'),
182      'img_copyr' => array('Iptc.CopyrightNotice', 'Exif.TIFFCopyright',
183        'Exif.Copyright'),
184      'img_camera' => 'Simple.Camera',
185      'img_fname' => 'File.Name',
186      'img_format' => 'File.Format',
187      'img_dimen'=> null, // built from File.Width and File.Height
188      'img_date' => 'Date.EarliestTime',
189      'img_fsize'=> 'File.NiceSize',
190    );
191
192    ptln('<table class="img_tags">');
193    ptln('<caption>', 2);
194    ptln($lang['img_metaheading'], 4);
195    if (tpl_img_getTag('File.Mime') == 'image/jpeg') {
196        sxs_btn_img_meta_edit($IMG, true);
197    }
198    ptln('</caption>', 2);
199    /* Comment out the following six lines if you don't want column headers */
200    ptln('<thead>', 2);
201    ptln('<tr>', 4);
202    ptln('<th class="label">' . $lang['colfield'] . '</th>', 6);
203    ptln('<th class="value">' . $lang['colvalue'] . '</th>', 6);
204    ptln('</tr>', 4);
205    ptln('</thead>', 2);
206    /* end column headers */
207    ptln('<tbody>', 2);
208    foreach ($tags as $tagname => $meta) {
209        if ($tagname == 'img_dimen') {
210            $info = tpl_img_getTag('File.Width') . '&#215;' .
211              tpl_img_getTag('File.Height');
212        } else {
213            $info = tpl_img_getTag($meta);
214            if ($info && $tagname == 'img_date') {
215                $info = date($conf['dformat'], $info);
216            } else {
217                $info = nl2br(hsc($info));
218            }
219        }
220        if ($info) {
221            ptln('<tr>', 4);
222            ptln('<td class="label">' . hsc($lang[$tagname]) . '</td>', 6);
223            ptln('<td class="value">' . $info . '</td>', 6);
224            ptln('</tr>', 4);
225        }
226    }
227    ptln('</tbody>', 2);
228    ptln('</table>');
229    if ($debug) {
230        dbg(tpl_img_getTag('Simple.Raw'));
231    }
232}
233
234/**
235 * Print link to mediaedit page
236 *
237 * @author Chris Arndt <chris@chrisarndt.de>
238 */
239function sxs_btn_img_meta_edit($id, $newwin=false) {
240    global $AUTH;
241    global $lang;
242    if ($AUTH >= AUTH_UPLOAD) {
243        print '<a href="' . DOKU_BASE . 'lib/exe/media.php?edit=' .
244          urlencode($id) . '"';
245        if ($newwin) {
246            print ' onclick="return metaedit(\'' . urlencode($id) .
247              '\');" target="mediaselect"';
248        }
249        print '>';
250        print '<img src="' . DOKU_BASE . 'lib/images/edit.gif" alt="' .
251          $lang['metaedit'] . '" title="' . $lang['metaedit'] . '" />';
252        ptln('</a>');
253    }
254}
255
256/**
257 * Helper function for sorting media files with images first
258 *
259 * @author Chris Arndt <chris@chrisarndt.de>
260 */
261function mediacmp($a, $b) {
262    if ($a['isimg'] && $b['isimg']) {
263        return strcmp($a['file'], $b['file']);
264    } elseif ($a['isimg']) {
265        return -1;
266    } elseif ($b['isimg']) {
267        return 1;
268    } else {
269        return strcmp($a['file'], $b['file']);
270    }
271}
272
273/**
274 * Print a list of mediafiles in the current namespace
275 *
276 * @author Chris Arndt <chris@chrisarndt.de> (based on tpl_mediafilelist)
277 */
278function sxs_mediafilelist($FileType='',$columns=2){
279    global $conf;
280    global $lang;
281    global $NS;
282    global $AUTH;
283    $dir = utf8_encodeFN(str_replace(':', '/', $NS));
284
285    $data = array();
286    search($data, $conf['mediadir'], 'search_media', array(), $dir);
287
288    if(!count($data)){
289        ptln('<div class="nothing">'.$lang['nothingfound'].'</div>');
290        return;
291    }
292
293    usort($data, "mediacmp");
294    ptln('<table>');
295    ptln('<tbody>');
296    $i = 0;
297    foreach($data as $item) {
298        if ($i == 0) { ptln('<tr>', 2); }
299        ptln('<td>', 4);
300
301        // prepare deletion button
302        if ($AUTH >= AUTH_DELETE) {
303            $ask = $lang['del_confirm'].'\\n';
304            $ask .= $item['id'];
305
306            $del = '<a href="' . DOKU_BASE . 'lib/exe/media.php?delete='.
307              urlencode($item['id']) . '" ' . 'onclick="return confirm(\'' .
308              $ask . '\')" onkeypress="return confirm(\'' . $ask . '\')">' .
309              '<img src="' . DOKU_BASE . 'lib/images/del.png" alt="' .
310            $lang['btn_delete'] . '" ' . 'align="bottom" title="' .
311            $lang['btn_delete'] . '" /></a>';
312        } else {
313            $del = '';
314        }
315
316        // show thumbnail for images
317        if ($item['isimg']) {
318            $w = $item['meta']->getField('File.Width');
319            $h = $item['meta']->getField('File.Height');
320
321            // prepare EXIF/IPTC data
322            $caption = $item['meta']->getField('IPTC.Headline');
323
324            $meta = '';
325            $t = $item['meta']->getField(array('IPTC.Caption',
326              'EXIF.UserComment', 'EXIF.TIFFImageDescription',
327              'EXIF.TIFFUserComment'));
328            if ($t) { $meta .= $t . '<br />'; }
329
330            $t = $item['meta']->getField(array('IPTC.Keywords',
331              'IPTC.Category'));
332            if($t) { $meta .= '<i>' . $t . '</i><br />'; }
333
334            // image thumbnail
335	    if ($FileType!='') {
336	      print '<a href="javascript:mediaSelect(\'' . $item['id'] . '\')"';
337	    }else{
338	      print '<a href="javascript:mediaSelect(\'' . $item['id'] . '\')"';
339	    }
340            print '  onmouseover="domTT_activate(this, event, \'content\', ';
341            print "'" . $meta . "', 'trail', true, 'styleClass', 'imagemeta'";
342            if ($caption) {
343                print ", 'caption', '" . $caption . "'";
344            }
345            print ');">';
346
347            if ($w > 120 || $h > 120) {
348                $ratio = $item['meta']->getResizeRatio(120);
349                $tw = floor($w * $ratio);
350                $th = floor($h * $ratio);
351            }
352
353            $src = ml($item['id'],array('w'=>$w,'h'=>$h));
354
355            $p = array();
356            $p['width'] = $tw;
357            $p['height'] = $th;
358            $p['alt'] = $item['id'];
359            $p['class'] = 'thumb';
360            $att = buildAttributes($p);
361
362            print '<img src="' . $src . '" ' . $att . ' />';
363            ptln('</a>');
364            ptln('<br />', 6);
365        }
366
367        // link to select media file
368	if ($FileType!='') {
369	  ptln('<a href="javascript:mediaSelect_mine(\'' . $item['id'] . '\',\'' . $FileType . '\')">' .
370	       utf8_decodeFN($item['file']) . '</a>', 6);
371	}else{
372	  if ($w>700) {
373	     ptln('<a href="javascript:mediaSelect_width(\'' . $item['id'] . '\',700)">' .
374	     	  utf8_decodeFN($item['file']) . '</a>', 6);
375	  } else {
376	  ptln('<a href="javascript:mediaSelect(\'' . $item['id'] . '\')">' .
377	       utf8_decodeFN($item['file']) . '</a>', 6);
378	       }
379	}
380
381        if ($item['isimg']) {
382            ptln('<br />', 6);
383            ptln('(' .$w. '&#215;' .$h. ' ' .filesize_h($item['size']). ')', 6);
384        } else {
385            ptln('<br />', 6);
386            ptln('(' . filesize_h($item['size']) . ')', 6);
387        }
388        // delete button
389        ptln('<br />', 6);
390        // edit button
391        if ($item['isimg']
392          && $item['meta']->getField('File.Mime') == 'image/jpeg') {
393            sxs_btn_img_meta_edit($item['id']);
394        }
395        ptln($del, 6);
396        // end of cell
397        ptln('</td>', 4);
398        $i++;
399        if ($i == $columns) {
400            ptln('</tr>', 2);
401            $i = 0;
402        }
403    }
404    if ($i != 0) {
405        for (; $i < $columns; $i++) {
406            ptln('<td>&nbsp;</td>', 4);
407        }
408        ptln('</tr>', 2);
409    }
410    ptln('</tbody>');
411    ptln('</table>');
412}
413
414/**
415 * Print the media upload form if permissions are correct
416 *
417 * @author Andreas Gohr <andi@splitbrain.org>
418 */
419function sxs_mediauploadform($FileType=''){
420  global $NS;
421  global $UPLOADOK;
422  global $AUTH;
423  global $lang;
424
425  if(!$UPLOADOK) return;
426
427  //if ($FileType!='') {
428    //ptln('<form action="'.DOKU_BASE.'lib/exe/media.php?filetype='.$FileType.'" name="upload"'.
429	 //' method="post" enctype="multipart/form-data">',2);
430  //}else{
431    ptln('<form action="'.DOKU_BASE.'lib/exe/media.php" name="upload"'.
432	 ' method="post" enctype="multipart/form-data">',2);
433  //}
434  ptln($lang['txt_upload'].':<br />',4);
435  ptln('<input type="file" name="upload" class="edit" onchange="suggestWikiname();" />',4);
436  ptln('<input type="hidden" name="ns" value="'.hsc($NS).'" /><br />',4);
437  if ($FileType!='') {
438    ptln('<input type="hidden" name="filetype" value="'.$FileType.'" /><br />',4);
439  }
440  ptln($lang['txt_filename'].'<br />',4);
441  ptln('<input type="text" name="id" class="edit" />',4);
442  ptln('<input type="submit" class="button" value="'.$lang['btn_upload'].'" accesskey="s" />',4);
443  if($AUTH >= AUTH_DELETE){
444    ptln('<label for="ow" class="simple"><input type="checkbox" name="ow" value="1" id="ow">'.$lang['txt_overwrt'].'</label>',4);
445  }
446  ptln('</form>',2);
447}
448?>
449