summaryrefslogtreecommitdiff
path: root/lib/exe/media.php
blob: 5ca3bd360d3b33efb023a06d2a14efb35d436e3f (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
<?php
  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__)).'/');
  require_once(DOKU_INC.'inc/init.php');
  require_once(DOKU_INC.'inc/common.php');
  require_once(DOKU_INC.'lang/en/lang.php');
  require_once(DOKU_INC.'lang/'.$conf['lang'].'/lang.php');
  require_once(DOKU_INC.'inc/html.php');
  require_once(DOKU_INC.'inc/search.php');
  require_once(DOKU_INC.'inc/template.php');
  require_once(DOKU_INC.'inc/auth.php');

  header('Content-Type: text/html; charset='.$lang['encoding']);

  //get namespace to display (either direct or from deletion order)
  if($_REQUEST['delete']){
		$DEL = cleanID($_REQUEST['delete']);
    $NS  = getNS($DEL);
  }else{
    $NS = $_REQUEST['ns'];
    $NS = cleanID($NS);
  }

  //check upload permissions
  $AUTH = auth_quickaclcheck("$NS:*");
  if($AUTH >= AUTH_UPLOAD){
    $UPLOADOK = true;
    //create the given namespace (just for beautification)
    $mdir = $conf['mediadir'].'/'.utf8_encodeFN(str_replace(':','/',$NS));
    io_makeFileDir("$mdir/xxx");
  }else{
    $UPLOADOK = false;
  }

  //handle deletion
	if($DEL && $AUTH >= AUTH_DELETE){
		media_delete($DEL);
	}

	//handle upload
  if($_FILES['upload']['tmp_name'] && $UPLOADOK){
    media_upload($NS,$AUTH);
  }

  //start output and load template
  header('Content-Type: text/html; charset=utf-8');
  include(DOKU_INC.'lib/tpl/'.$conf['template'].'/lib/exe/media.php');

  //restore old umask
  umask($conf['oldumask']);

/**********************************************/

/**
 * Deletes mediafiles - Auth is not handled here!
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function media_delete($delid){
  $file = mediaFN($delid);
	if(@unlink($file)){
		return true;
	}
	//something went wrong
  msg("'$file' couldn't be deleted - check permissions",-1);
  return false;
}

/**
 * Handles Mediafile uploads
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function media_upload($NS,$AUTH){
	require_once(DOKU_INC.'inc/confutils.php');
  global $lang;
  global $conf;

  // get file
  $id   = $_POST['id'];
  $file = $_FILES['upload'];
  // get id
  if(empty($id)) $id = $file['name'];
  $id   = cleanID($NS.':'.$id);
  // get filename
  $fn   = mediaFN($id);

  // get filetype regexp
	$types = array_keys(getMimeTypes());
	$types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
  $regex = join('|',$types);

  // we set the umask here but this doesn't really help
  // because a temp file was created already
  umask($conf['umask']);
  if(preg_match('/\.('.$regex.')$/i',$fn)){
		//check for overwrite
		if(@file_exists($fn) && (!$_POST['ow'] || $AUTH < AUTH_DELETE)){
			msg($lang['uploadexist'],0);
			return false;
		}
  	// prepare directory
  	io_makeFileDir($fn);
    if(move_uploaded_file($file['tmp_name'], $fn)) {
			// set the correct permission here
			chmod($fn, 0777 - $conf['umask']);
      msg($lang['uploadsucc'],1);
      return true;
    }else{
      msg($lang['uploadfail'],-1);
    }
  }else{
    msg($lang['uploadwrong'],-1);
  }
  return false;
}

/**
 * Userfunction for html_buildlist
 *
 * Prints available media namespaces
 *
 * @author Andreas Gohr <andi@splitbrain.org>
 */
function media_html_list_namespaces($item){
  $ret  = '';
  $ret .= '<a href="'.DOKU_BASE.'lib/exe/media.php?ns='.idfilter($item['id']).'" class="idx_dir">';
  $pos = strrpos($item['id'], ':');
  $ret .= substr($item['id'], $pos > 0 ? $pos + 1 : 0);
  $ret .= '</a>';
  return $ret;
}

?>