1<?php 2/** 3 * All output and handler function needed for the media management popup 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/'); 10if(!defined('NL')) define('NL',"\n"); 11 12require_once(DOKU_INC.'inc/html.php'); 13require_once(DOKU_INC.'inc/search.php'); 14require_once(DOKU_INC.'inc/JpegMeta.php'); 15 16/** 17 * Lists pages which currently use a media file selected for deletion 18 * 19 * References uses the same visual as search results and share 20 * their CSS tags except pagenames won't be links. 21 * 22 * @author Matthias Grimm <matthiasgrimm@users.sourceforge.net> 23 */ 24function media_filesinuse($data,$id){ 25 global $lang; 26 echo '<h1>'.$lang['reference'].' <code>'.hsc(noNS($id)).'</code></h1>'; 27 echo '<p>'.hsc($lang['ref_inuse']).'</p>'; 28 29 $hidden=0; //count of hits without read permission 30 usort($data,'sort_search_fulltext'); 31 foreach($data as $row){ 32 if(auth_quickaclcheck($row['id']) >= AUTH_READ){ 33 echo '<div class="search_result">'; 34 echo '<span class="mediaref_ref">'.$row['id'].'</span>'; 35 echo ': <span class="search_cnt">'.$row['count'].' '.$lang['hits'].'</span><br />'; 36 echo '<div class="search_snippet">'.$row['snippet'].'</div>'; 37 echo '</div>'; 38 }else 39 $hidden++; 40 } 41 if ($hidden){ 42 print '<div class="mediaref_hidden">'.$lang['ref_hidden'].'</div>'; 43 } 44} 45 46/** 47 * Handles the saving of image meta data 48 * 49 * @author Andreas Gohr <andi@splitbrain.org> 50 */ 51function media_metasave($id,$auth,$data){ 52 if($auth < AUTH_UPLOAD) return false; 53 global $lang; 54 $src = mediaFN($id); 55 56 $meta = new JpegMeta($src); 57 $meta->_parseAll(); 58 59 foreach($data as $key => $val){ 60 $val=trim($val); 61 if(empty($val)){ 62 $meta->deleteField($key); 63 }else{ 64 $meta->setField($key,$val); 65 } 66 } 67 68 if($meta->save()){ 69 msg($lang['metasaveok'],1); 70 return $id; 71 }else{ 72 msg($lang['metasaveerr'],-1); 73 return false; 74 } 75} 76 77/** 78 * Display the form to edit image meta data 79 * 80 * @author Andreas Gohr <andi@splitbrain.org> 81 */ 82function media_metaform($id,$auth){ 83 if($auth < AUTH_UPLOAD) return false; 84 global $lang; 85 86 // load the field descriptions 87 static $fields = null; 88 if(is_null($fields)){ 89 include(DOKU_CONF.'mediameta.php'); 90 if(@file_exists(DOKU_CONF.'mediameta.local.php')){ 91 include(DOKU_CONF.'mediameta.local.php'); 92 } 93 } 94 95 $src = mediaFN($id); 96 97 // output 98 echo '<h1>'.hsc(noNS($id)).'</h1>'.NL; 99 echo '<form action="'.DOKU_BASE.'lib/exe/mediamanager.php" accept-charset="utf-8" method="post" class="meta">'.NL; 100 echo '<input type="hidden" name="img" value="'.hsc($id).'" />'.NL; 101 foreach($fields as $key => $field){ 102 // get current value 103 $tags = array($field[0]); 104 if(is_array($field[3])) $tags = array_merge($tags,$field[3]); 105 $value = tpl_img_getTag($tags,'',$src); 106 107 // prepare attributes 108 $p = array(); 109 $p['class'] = 'edit'; 110 $p['id'] = 'meta__'.$key; 111 $p['name'] = 'meta['.$field[0].']'; 112 113 // put label 114 echo '<div class="metafield">'; 115 echo '<label for="meta__'.$key.'">'; 116 echo ($lang[$field[1]]) ? $lang[$field[1]] : $field[1]; 117 echo '</label>'; 118 119 // put input field 120 if($field[2] == 'text'){ 121 $p['value'] = $value; 122 $p['type'] = 'text'; 123 $att = buildAttributes($p); 124 echo "<input $att/>".NL; 125 }else{ 126 $att = buildAttributes($p); 127 echo "<textarea $att>".formText($value).'</textarea>'.NL; 128 } 129 echo '</div>'.NL; 130 } 131 echo '<div class="buttons">'.NL; 132 echo '<input name="do[save]" type="submit" value="'.$lang['btn_save']. 133 '" title="ALT+S" accesskey="s" class="button" />'.NL; 134 echo '<input name="do[cancel]" type="submit" value="'.$lang['btn_cancel']. 135 '" title="ALT+C" accesskey="c" class="button" />'.NL; 136 echo '</form>'.NL; 137 echo '</div>'; 138} 139 140/** 141 * Handles media file deletions 142 * 143 * If configured, checks for media references before deletion 144 * 145 * @author Andreas Gohr <andi@splitbrain.org> 146 * @return mixed false on error, true on delete or array with refs 147 */ 148function media_delete($id,$auth){ 149 if($auth < AUTH_DELETE) return false; 150 global $conf; 151 global $lang; 152 153 $mediareferences = array(); 154 if($conf['refcheck']){ 155 search($mediareferences,$conf['datadir'],'search_reference',array('query' => $id)); 156 } 157 158 if(!count($mediareferences)){ 159 $file = mediaFN($id); 160 if(@unlink($file)){ 161 msg(str_replace('%s',noNS($id),$lang['deletesucc']),1); 162 io_sweepNS($id,'mediadir'); 163 return true; 164 } 165 //something went wrong 166 msg(str_replace('%s',$file,$lang['deletefail']),-1); 167 return false; 168 }elseif(!$conf['refshow']){ 169 msg(str_replace('%s',noNS($id),$lang['mediainuse']),0); 170 return false; 171 } 172 173 return $mediareferences; 174} 175 176/** 177 * Handles media file uploads 178 * 179 * @author Andreas Gohr <andi@splitbrain.org> 180 * @return mixed false on error, id of the new file on success 181 */ 182function media_upload($ns,$auth){ 183 if($auth < AUTH_UPLOAD) return false; 184 require_once(DOKU_INC.'inc/confutils.php'); 185 global $lang; 186 global $conf; 187 188 // get file 189 $id = $_POST['id']; 190 $file = $_FILES['upload']; 191 // get id 192 if(empty($id)) $id = $file['name']; 193 $id = cleanID($ns.':'.$id); //FIXME handle relative and absolute names here 194 // get filename 195 $fn = mediaFN($id); 196 197 // get filetype regexp 198 $types = array_keys(getMimeTypes()); 199 $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types); 200 $regex = join('|',$types); 201 202 // because a temp file was created already 203 if(preg_match('/\.('.$regex.')$/i',$fn)){ 204 //check for overwrite 205 if(@file_exists($fn) && (!$_POST['ow'] || $auth < AUTH_DELETE)){ 206 msg($lang['uploadexist'],0); 207 return false; 208 } 209 // prepare directory 210 io_makeFileDir($fn); 211 if(move_uploaded_file($file['tmp_name'], $fn)) { 212 // set the correct permission here 213 if($conf['fperm']) chmod($fn, $conf['fperm']); 214 msg($lang['uploadsucc'],1); 215 return $id; 216 }else{ 217 msg($lang['uploadfail'],-1); 218 } 219 }else{ 220 msg($lang['uploadwrong'],-1); 221 } 222 return false; 223} 224 225 226 227/** 228 * List all files in a given Media namespace 229 */ 230function media_filelist($ns,$auth=null,$jump=''){ 231 global $conf; 232 global $lang; 233 $ns = cleanID($ns); 234 235 // check auth our self if not given (needed for ajax calls) 236 if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*"); 237 238 echo '<h1>:'.hsc($ns).'</h1>'.NL; 239 240 if($auth < AUTH_READ){ 241 // FIXME: print permission warning here instead? 242 echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL; 243 return; 244 } 245 246 media_uploadform($ns, $auth); 247 248 $dir = utf8_encodeFN(str_replace(':','/',$ns)); 249 $data = array(); 250 search($data,$conf['mediadir'],'search_media',array(),$dir); 251 252 if(!count($data)){ 253 echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL; 254 return; 255 } 256 257 foreach($data as $item){ 258 media_printfile($item,$auth,$jump); 259 } 260} 261 262/** 263 * Print action links for a file depending on filetype 264 * and available permissions 265 * 266 * @todo contains inline javascript 267 */ 268function media_fileactions($item,$auth){ 269 global $lang; 270 271 // no actions if not writable 272 if(!$item['writable']) return; 273 274 // delete button 275 if($auth >= AUTH_DELETE){ 276 $ask = addslashes($lang['del_confirm']).'\\n'; 277 $ask .= addslashes($item['id']); 278 279 echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']).'" '. 280 'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'. 281 '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '. 282 'title="'.$lang['btn_delete'].'" class="btn" /></a>'; 283 } 284 285 // edit button 286 if($auth >= AUTH_UPLOAD && $item['isimg'] && $item['meta']->getField('File.Mime') == 'image/jpeg'){ 287 echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?edit='.rawurlencode($item['id']).'">'. 288 '<img src="'.DOKU_BASE.'lib/images/pencil.png" alt="'.$lang['metaedit'].'" '. 289 'title="'.$lang['metaedit'].'" class="btn" /></a>'; 290 } 291 292} 293 294/** 295 * Formats and prints one file in the list 296 */ 297function media_printfile($item,$auth,$jump){ 298 global $lang; 299 300 // Prepare zebra coloring 301 // I always wanted to use this variable name :-D 302 static $twibble = 1; 303 $twibble *= -1; 304 $zebra = ($twibble == -1) ? 'odd' : 'even'; 305 306 // Automatically jump to recent action 307 if($jump == $item['id']) { 308 $jump = ' id="scroll__here" '; 309 }else{ 310 $jump = ''; 311 } 312 313 // Prepare fileicons 314 list($ext,$mime) = mimetype($item['file']); 315 $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext); 316 $class = 'select mediafile mf_'.$class; 317 318 // Prepare filename 319 $file = utf8_decodeFN($item['file']); 320 321 // Prepare info 322 $info = ''; 323 if($item['isimg']){ 324 $info .= (int) $item['meta']->getField('File.Width'); 325 $info .= '×'; 326 $info .= (int) $item['meta']->getField('File.Height'); 327 $info .= ' '; 328 } 329 $info .= filesize_h($item['size']); 330 331 // ouput 332 echo '<div class="'.$zebra.'"'.$jump.'>'.NL; 333 echo '<a name="h_'.$item['id'].'" class="'.$class.'">'.$file.'</a> '; 334 echo '<span class="info">('.$info.')</span>'.NL; 335 media_fileactions($item,$auth); 336 echo '<div class="example" id="ex_'.$item['id'].'">'; 337 echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}<code>'; 338 echo '</div>'; 339 if($item['isimg']) media_printimgdetail($item); 340 echo '<div class="clearer"></div>'.NL; 341 echo '</div>'.NL; 342} 343 344/** 345 * Prints a thumbnail and metainfos 346 */ 347function media_printimgdetail($item){ 348 // prepare thumbnail 349 $w = (int) $item['meta']->getField('File.Width'); 350 $h = (int) $item['meta']->getField('File.Height'); 351 if($w>120 || $h>120){ 352 $ratio = $item['meta']->getResizeRatio(120); 353 $w = floor($w * $ratio); 354 $h = floor($h * $ratio); 355 } 356 $src = ml($item['id'],array('w'=>$w,'h'=>$h)); 357 $p = array(); 358 $p['width'] = $w; 359 $p['height'] = $h; 360 $p['alt'] = $item['id']; 361 $p['class'] = 'thumb'; 362 $att = buildAttributes($p); 363 364 // output 365 echo '<div class="detail">'; 366 echo '<div class="thumb">'; 367 echo '<a name="d_'.$item['id'].'" class="select">'; 368 echo '<img src="'.$src.'" '.$att.' />'; 369 echo '</a>'; 370 echo '</div>'; 371 372 //read EXIF/IPTC data 373 echo '<p>'; 374 $t = $item['meta']->getField('IPTC.Headline'); 375 if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />'; 376 377 $t = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment', 378 'EXIF.TIFFImageDescription', 379 'EXIF.TIFFUserComment')); 380 if($t) echo htmlspecialchars($t).'<br />'; 381 382 $t = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category')); 383 if($t) echo '<em>'.htmlspecialchars($t).'</em>'; 384 echo '</p>'; 385 echo '</div>'; 386} 387 388/** 389 * Print the media upload form if permissions are correct 390 * 391 * @author Andreas Gohr <andi@splitbrain.org> 392 */ 393function media_uploadform($ns, $auth){ 394 global $lang; 395 396 if($auth < AUTH_UPLOAD) return; //fixme print info on missing permissions? 397 398 ?> 399 <form action="<?php echo DOKU_BASE?>lib/exe/mediamanager.php" 400 method="post" enctype="multipart/form-data" class="upload"> 401 <input type="hidden" name="ns" value="<?php echo hsc($ns)?>" /> 402 403 <?php echo $lang['txt_upload']?>: 404 <input type="file" name="upload" class="edit" id="upload__file" /><br /> 405 406 <?php echo $lang['txt_filename']?>: 407 <input type="text" name="id" class="edit" id="upload__name" /> 408 <input type="submit" class="button" value="<?php echo $lang['btn_upload']?>" accesskey="s" /> 409 410 <?php if($auth >= AUTH_DELETE){?> 411 <br /> 412 <label for="dw__ow"> 413 <input type="checkbox" name="ow" value="1" id="dw__ow" /><?php echo $lang['txt_overwrt']?> 414 </label> 415 <?php }?> 416 </form> 417 <?php 418} 419 420 421 422/** 423 * Build a tree outline of available media namespaces 424 * 425 * @author Andreas Gohr <andi@splitbrain.org> 426 */ 427function media_nstree($ns){ 428 global $conf; 429 430 // currently selected namespace 431 $ns = cleanID($ns); 432 if(empty($ns)){ 433 $ns = dirname(str_replace(':','/',$ID)); 434 if($ns == '.') $ns =''; 435 } 436 $ns = utf8_encodeFN(str_replace(':','/',$ns)); 437 438 $data = array(); 439 search($data,$conf['mediadir'],'search_index',array('ns' => $ns)); 440 441 // wrap a list with the root level around the other namespaces 442 $item = array( 'level' => 0, 'id' => '', 'open' =>'true', 'label' => ':*'); 443 444 echo '<ul class="idx">'; 445 echo media_nstree_li($item); 446 echo media_nstree_item($item); 447 echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li'); 448 echo '</li>'; 449 echo '</ul>'; 450} 451 452/** 453 * Userfunction for html_buildlist 454 * 455 * Prints a media namespace tree item 456 * 457 * @author Andreas Gohr <andi@splitbrain.org> 458 */ 459function media_nstree_item($item){ 460 $pos = strrpos($item['id'], ':'); 461 $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0); 462 if(!$item['label']) $item['label'] = $label; 463 464 $ret = ''; 465 $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">'; 466 $ret .= $item['label']; 467 $ret .= '</a>'; 468 return $ret; 469} 470 471/** 472 * Userfunction for html_buildlist 473 * 474 * Prints a media namespace tree item opener 475 * 476 * @author Andreas Gohr <andi@splitbrain.org> 477 */ 478function media_nstree_li($item){ 479 $class='media level'.$item['level']; 480 if($item['open']){ 481 $class .= ' open'; 482 $img = DOKU_BASE.'lib/images/minus.gif'; 483 }else{ 484 $class .= ' closed'; 485 $img = DOKU_BASE.'lib/images/plus.gif'; 486 } 487 return '<li class="'.$class.'">'. 488 '<img src="'.$img.'" alt="*" />'; 489} 490