[ Index ]
 

Code source de DokuWiki 2006-11-06

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/inc/ -> media.php (source)

   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  
   9  if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../').'/');
  10  if(!defined('NL')) define('NL',"\n");
  11  
  12  require_once (DOKU_INC.'inc/html.php');
  13  require_once (DOKU_INC.'inc/search.php');
  14  require_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   */
  24  function 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   */
  51  function 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   */
  82  function 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      foreach($fields as $key => $field){
 101          // get current value
 102          $tags = array($field[0]);
 103          if(is_array($field[3])) $tags = array_merge($tags,$field[3]);
 104          $value = tpl_img_getTag($tags,'',$src);
 105  
 106          // prepare attributes
 107          $p = array();
 108          $p['class'] = 'edit';
 109          $p['id']    = 'meta__'.$key;
 110          $p['name']  = 'meta['.$field[0].']';
 111  
 112          // put label
 113          echo '<div class="metafield">';
 114          echo '<label for="meta__'.$key.'">';
 115          echo ($lang[$field[1]]) ? $lang[$field[1]] : $field[1];
 116          echo ':</label>';
 117  
 118          // put input field
 119          if($field[2] == 'text'){
 120              $p['value'] = $value;
 121              $p['type']  = 'text';
 122              $att = buildAttributes($p);
 123              echo "<input $att/>".NL;
 124          }else{
 125              $att = buildAttributes($p);
 126              echo "<textarea $att rows=\"6\" cols=\"50\">".formText($value).'</textarea>'.NL;
 127          }
 128          echo '</div>'.NL;
 129      }
 130      echo '<div class="buttons">'.NL;
 131      echo '<input type="hidden" name="img" value="'.hsc($id).'" />'.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 '</div>'.NL;
 137      echo '</form>'.NL;
 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   */
 148  function 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   */
 182  function 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 and id
 189      $id   = $_POST['id'];
 190      $file = $_FILES['upload'];
 191      if(empty($id)) $id = $file['name'];
 192  
 193      // check extensions
 194      list($fext) = mimetype($file['name']);
 195      list($iext) = mimetype($id);
 196      if($fext && !$iext){
 197          // no extension specified in id - readd original one
 198          $id .= '.'.$fext;
 199      }elseif($fext && $fext != $iext){
 200          // extension was changed, print warning
 201          msg(sprintf($lang['mediaextchange'],$fext,$iext));
 202      }
 203  
 204      // get filename
 205      $id   = cleanID($ns.':'.$id);
 206      $fn   = mediaFN($id);
 207  
 208      // get filetype regexp
 209      $types = array_keys(getMimeTypes());
 210      $types = array_map(create_function('$q','return preg_quote($q,"/");'),$types);
 211      $regex = join('|',$types);
 212  
 213      // because a temp file was created already
 214      if(preg_match('/\.('.$regex.')$/i',$fn)){
 215          //check for overwrite
 216          if(@file_exists($fn) && (!$_POST['ow'] || $auth < AUTH_DELETE)){
 217              msg($lang['uploadexist'],0);
 218              return false;
 219          }
 220          // prepare directory
 221          io_createNamespace($id, 'media');
 222          if(move_uploaded_file($file['tmp_name'], $fn)) {
 223              // Set the correct permission here.
 224              // Always chmod media because they may be saved with different permissions than expected from the php umask.
 225              // (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
 226              chmod($fn, $conf['fmode']);
 227              msg($lang['uploadsucc'],1);
 228              return $id;
 229          }else{
 230              msg($lang['uploadfail'],-1);
 231          }
 232      }else{
 233          msg($lang['uploadwrong'],-1);
 234      }
 235      return false;
 236  }
 237  
 238  
 239  
 240  /**
 241   * List all files in a given Media namespace
 242   */
 243  function media_filelist($ns,$auth=null,$jump=''){
 244      global $conf;
 245      global $lang;
 246      $ns = cleanID($ns);
 247  
 248      // check auth our self if not given (needed for ajax calls)
 249      if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
 250  
 251      echo '<h1 id="media__ns">:'.hsc($ns).'</h1>'.NL;
 252  
 253      if($auth < AUTH_READ){
 254          // FIXME: print permission warning here instead?
 255          echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
 256          return;
 257      }
 258  
 259      media_uploadform($ns, $auth);
 260  
 261      $dir = utf8_encodeFN(str_replace(':','/',$ns));
 262      $data = array();
 263      search($data,$conf['mediadir'],'search_media',array(),$dir);
 264  
 265      if(!count($data)){
 266          echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
 267          return;
 268      }
 269  
 270      foreach($data as $item){
 271          media_printfile($item,$auth,$jump);
 272      }
 273  }
 274  
 275  /**
 276   * Print action links for a file depending on filetype
 277   * and available permissions
 278   *
 279   * @todo contains inline javascript
 280   */
 281  function media_fileactions($item,$auth){
 282      global $lang;
 283  
 284      // view button
 285      $link = ml($item['id'],'',true);
 286      echo ' <a href="'.$link.'" target="_blank"><img src="'.DOKU_BASE.'lib/images/magnifier.png" '.
 287           'alt="'.$lang['mediaview'].'" title="'.$lang['mediaview'].'" class="btn" /></a>';
 288  
 289  
 290      // no further actions if not writable
 291      if(!$item['writable']) return;
 292  
 293      // delete button
 294      if($auth >= AUTH_DELETE){
 295          $ask  = addslashes($lang['del_confirm']).'\\n';
 296          $ask .= addslashes($item['id']);
 297  
 298          echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?delete='.rawurlencode($item['id']).'" '.
 299               'onclick="return confirm(\''.$ask.'\')" onkeypress="return confirm(\''.$ask.'\')">'.
 300               '<img src="'.DOKU_BASE.'lib/images/trash.png" alt="'.$lang['btn_delete'].'" '.
 301               'title="'.$lang['btn_delete'].'" class="btn" /></a>';
 302      }
 303  
 304      // edit button
 305      if($auth >= AUTH_UPLOAD && $item['isimg'] && $item['meta']->getField('File.Mime') == 'image/jpeg'){
 306          echo ' <a href="'.DOKU_BASE.'lib/exe/mediamanager.php?edit='.rawurlencode($item['id']).'">'.
 307               '<img src="'.DOKU_BASE.'lib/images/pencil.png" alt="'.$lang['metaedit'].'" '.
 308               'title="'.$lang['metaedit'].'" class="btn" /></a>';
 309      }
 310  
 311  }
 312  
 313  /**
 314   * Formats and prints one file in the list
 315   */
 316  function media_printfile($item,$auth,$jump){
 317      global $lang;
 318  
 319      // Prepare zebra coloring
 320      // I always wanted to use this variable name :-D
 321      static $twibble = 1;
 322      $twibble *= -1;
 323      $zebra = ($twibble == -1) ? 'odd' : 'even';
 324  
 325      // Automatically jump to recent action
 326      if($jump == $item['id']) {
 327          $jump = ' id="scroll__here" ';
 328      }else{
 329          $jump = '';
 330      }
 331  
 332      // Prepare fileicons
 333      list($ext,$mime) = mimetype($item['file']);
 334      $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
 335      $class = 'select mediafile mf_'.$class;
 336  
 337      // Prepare filename
 338      $file = utf8_decodeFN($item['file']);
 339  
 340      // Prepare info
 341      $info = '';
 342      if($item['isimg']){
 343          $info .= (int) $item['meta']->getField('File.Width');
 344          $info .= '&#215;';
 345          $info .= (int) $item['meta']->getField('File.Height');
 346          $info .= ' ';
 347      }
 348      $info .= filesize_h($item['size']);
 349  
 350      // ouput
 351      echo '<div class="'.$zebra.'"'.$jump.'>'.NL;
 352      echo '<a name="h_'.$item['id'].'" class="'.$class.'">'.$file.'</a> ';
 353      echo '<span class="info">('.$info.')</span>'.NL;
 354      media_fileactions($item,$auth);
 355      echo '<div class="example" id="ex_'.$item['id'].'">';
 356      echo $lang['mediausage'].' <code>{{:'.$item['id'].'}}</code>';
 357      echo '</div>';
 358      if($item['isimg']) media_printimgdetail($item);
 359      echo '<div class="clearer"></div>'.NL;
 360      echo '</div>'.NL;
 361  }
 362  
 363  /**
 364   * Prints a thumbnail and metainfos
 365   */
 366  function media_printimgdetail($item){
 367      // prepare thumbnail
 368      $w = (int) $item['meta']->getField('File.Width');
 369      $h = (int) $item['meta']->getField('File.Height');
 370      if($w>120 || $h>120){
 371          $ratio = $item['meta']->getResizeRatio(120);
 372          $w = floor($w * $ratio);
 373          $h = floor($h * $ratio);
 374      }
 375      $src = ml($item['id'],array('w'=>$w,'h'=>$h));
 376      $p = array();
 377      $p['width']  = $w;
 378      $p['height'] = $h;
 379      $p['alt']    = $item['id'];
 380      $p['class']  = 'thumb';
 381      $att = buildAttributes($p);
 382  
 383      // output
 384      echo '<div class="detail">';
 385      echo '<div class="thumb">';
 386      echo '<a name="d_'.$item['id'].'" class="select">';
 387      echo '<img src="'.$src.'" '.$att.' />';
 388      echo '</a>';
 389      echo '</div>';
 390  
 391      // read EXIF/IPTC data
 392      $t = $item['meta']->getField('IPTC.Headline');
 393      $d = $item['meta']->getField(array('IPTC.Caption','EXIF.UserComment',
 394                                         'EXIF.TIFFImageDescription',
 395                                         'EXIF.TIFFUserComment'));
 396      if(utf8_strlen($d) > 250) $d = utf8_substr($d,0,250).'...';
 397      $k = $item['meta']->getField(array('IPTC.Keywords','IPTC.Category'));
 398  
 399      // print EXIF/IPTC data
 400      if($t || $d || $k ){
 401          echo '<p>';
 402          if($t) echo '<strong>'.htmlspecialchars($t).'</strong><br />';
 403          if($d) echo htmlspecialchars($d).'<br />';
 404          if($t) echo '<em>'.htmlspecialchars($k).'</em>';
 405          echo '</p>';
 406      }
 407      echo '</div>';
 408  }
 409  
 410  /**
 411   * Print the media upload form if permissions are correct
 412   *
 413   * @author Andreas Gohr <andi@splitbrain.org>
 414   */
 415  function media_uploadform($ns, $auth){
 416      global $lang;
 417  
 418      if($auth < AUTH_UPLOAD) return; //fixme print info on missing permissions?
 419  
 420      ?>
 421      <div class="upload"><?php echo $lang['mediaupload']?></div>
 422      <form action="<?php echo DOKU_BASE?>lib/exe/mediamanager.php"
 423            method="post" enctype="multipart/form-data" class="upload">
 424        <fieldset>
 425          <input type="hidden" name="ns" value="<?php echo hsc($ns)?>" />
 426  
 427          <p>
 428            <label for="upload__file"><?php echo $lang['txt_upload']?>:</label>
 429            <input type="file" name="upload" class="edit" id="upload__file" />
 430          </p>
 431  
 432          <p>
 433            <label for="upload__name"><?php echo $lang['txt_filename']?>:</label>
 434            <span class="nowrap">
 435            <input type="text" name="id" class="edit" id="upload__name" /><input
 436                   type="submit" class="button" value="<?php echo $lang['btn_upload']?>"
 437                   accesskey="s" />
 438            </span>
 439          </p>
 440  
 441          <?php if($auth >= AUTH_DELETE){?>
 442              <p>
 443                <input type="checkbox" name="ow" value="1" id="dw__ow" class="check" />
 444                <label for="dw__ow" class="check"><?php echo $lang['txt_overwrt']?></label>
 445              </p>
 446          <?php }?>
 447        </fieldset>
 448      </form>
 449      <?php
 450  }
 451  
 452  
 453  
 454  /**
 455   * Build a tree outline of available media namespaces
 456   *
 457   * @author Andreas Gohr <andi@splitbrain.org>
 458   */
 459  function media_nstree($ns){
 460      global $conf;
 461      global $lang;
 462  
 463      // currently selected namespace
 464      $ns  = cleanID($ns);
 465      if(empty($ns)){
 466          $ns = dirname(str_replace(':','/',$ID));
 467          if($ns == '.') $ns ='';
 468      }
 469      $ns  = utf8_encodeFN(str_replace(':','/',$ns));
 470  
 471      $data = array();
 472      search($data,$conf['mediadir'],'search_index',array('ns' => $ns, 'nofiles' => true));
 473  
 474      // wrap a list with the root level around the other namespaces
 475      $item = array( 'level' => 0, 'id' => '',
 476                     'open' =>'true', 'label' => '['.$lang['mediaroot'].']');
 477  
 478      echo '<ul class="idx">';
 479      echo media_nstree_li($item);
 480      echo media_nstree_item($item);
 481      echo html_buildlist($data,'idx','media_nstree_item','media_nstree_li');
 482      echo '</li>';
 483      echo '</ul>';
 484  }
 485  
 486  /**
 487   * Userfunction for html_buildlist
 488   *
 489   * Prints a media namespace tree item
 490   *
 491   * @author Andreas Gohr <andi@splitbrain.org>
 492   */
 493  function media_nstree_item($item){
 494      $pos   = strrpos($item['id'], ':');
 495      $label = substr($item['id'], $pos > 0 ? $pos + 1 : 0);
 496      if(!$item['label']) $item['label'] = $label;
 497  
 498      $ret  = '';
 499      $ret .= '<a href="'.DOKU_BASE.'lib/exe/mediamanager.php?ns='.idfilter($item['id']).'" class="idx_dir">';
 500      $ret .= $item['label'];
 501      $ret .= '</a>';
 502      return $ret;
 503  }
 504  
 505  /**
 506   * Userfunction for html_buildlist
 507   *
 508   * Prints a media namespace tree item opener
 509   *
 510   * @author Andreas Gohr <andi@splitbrain.org>
 511   */
 512  function media_nstree_li($item){
 513      $class='media level'.$item['level'];
 514      if($item['open']){
 515          $class .= ' open';
 516          $img   = DOKU_BASE.'lib/images/minus.gif';
 517          $alt   = '&minus;';
 518      }else{
 519          $class .= ' closed';
 520          $img   = DOKU_BASE.'lib/images/plus.gif';
 521          $alt   = '+';
 522      }
 523      return '<li class="'.$class.'">'.
 524             '<img src="'.$img.'" alt="'.$alt.'" />';
 525  }


Généré le : Tue Apr 3 20:47:31 2007 par Balluche grâce à PHPXref 0.7