1<?php
2/**
3 * Dokuwiki Action Plugin Media-File-Rename
4 * Allows renaming of mediafiles to dokuwiki-conform names
5 *
6 * @author Christian Eder
7 *
8 */
9
10if(!defined('DOKU_INC')) die();
11if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
12require_once(DOKU_PLUGIN.'action.php');
13
14class action_plugin_mediarename extends DokuWiki_Action_Plugin {
15
16    /**
17     * Register its handlers with the dokuwiki's event controller
18     */
19    function register(Doku_Event_Handler $controller) {
20        // MEDIAMANAGER_CONTENT_OUTPUT Wraps the output of the (right) content pane in the Media Manager
21        // intresting but we use MEDIAMANAGER_STARTED
22        $controller->register_hook(
23            'MEDIAMANAGER_CONTENT_OUTPUT', 'BEFORE', $this, 'handle_link'
24        );
25    }
26
27
28
29    /**
30     * Handle the event
31     */
32    function handle_link(&$event, $param) {
33        global $conf;
34        global $lang;
35        global $INPUT;
36        if (isset($_REQUEST['rename'])){
37            $ns = cleanID($INPUT->str('ns'));
38            $dir = utf8_encodeFN(str_replace(':','/',$ns));
39            $data = array();
40            $recurse=($INPUT->str('rename')=='recv') ? true : false;
41
42            search($data,$conf['mediadir'],array($this,'_media_file_rename'),array('showmsg'=>true,'recurse'=>$recurse),$dir,0);
43
44        }
45
46    }
47
48    /**
49     * Callback-function for the search call. It checks permission for the given ids.
50     * If an invalid filename is encountered, rename it to a valid one generated.
51     * Does the same with directries. If recursion is selected, it renames recursively.
52     *
53     * Fix-me:
54     * Attention: in case that a directory had to be renamed, recursion breaks -
55     * possible invalid filenames inside that directory are not renamed.
56     * Simply run the function a second time !
57     * This is for the case that a directory has to be renamed exactly to the name of
58     * an already exising directory - to prevent the unintendet moving of the files from the
59     * first dir to the existing dir (that would follow from the recursive process)
60     */
61    function _media_file_rename(&$data,$base,$file,$type,$lvl,$opts){
62        $info         = array();
63        $info['id']   = pathID($file,true);
64        //quick permissioncheck
65        if(is_null($auth)){
66            $auth = auth_quickaclcheck($info['id']);
67        }
68        if($auth < AUTH_READ){
69            return;
70        }
71
72        //check for validity of the filename
73        if($info['id'] != cleanID($info['id'])){
74            $id   = cleanID($ns.':'.$info['id'],false,true);
75
76            //find a target filename which is free to use (avoid overwrite)
77            $tmp=$id;
78            $fn='';
79            $cnt=0;
80            while (!$fn){
81                if (!file_exists(mediaFN($tmp))){
82                    $fn   = mediaFN($tmp);
83                } else {
84                    $cnt++;
85                    $ext=strrchr($id,'.');
86                    //is there an extension?
87                    if ($ext) {
88                        $tmp=substr($id,0,strrpos($id,'.')).'_'.$cnt.$ext;
89                    }
90                    else {
91                        $tmp=$id.'_'.$cnt;
92                    }
93                }
94            }
95            //and now rename
96            rename($base."/".$file,$fn);
97            if($opts['showmsg']) {
98                msg(hsc($info['id']).' was not a valid file name for DokuWiki - moved to new name: '.$tmp);
99            }
100            //return true if recursion is desired
101
102        }
103
104        //return true if recursion is desired
105        if($type == 'd') {
106            if ($opts['recurse']==true) {
107                return true;
108            }
109        }
110
111        return false;
112
113    }
114
115
116}
117
118