8000 dokuwiki/inc/media.php at travistest · alexlehm/dokuwiki · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
2137 lines (1864 loc) · 63.5 KB

media.php

File metadata and controls

2137 lines (1864 loc) · 63.5 KB
if ($REV){
addMediaLogEntry($new, $id, DOKU_CHANGE_TYPE_REVERT, sprintf($lang['restored'], dformat($REV)), $REV);
} elseif ($overwrite) {
addMediaLogEntry($new, $id, DOKU_CHANGE_TYPE_EDIT);
} else {
addMediaLogEntry($new, $id, DOKU_CHANGE_TYPE_CREATE, $lang['created']);
}
return $id;
}else{
return array($lang['uploadfail'],-1);
}
}
/**
* Moves the current version of media file to the media_attic
* directory
*
* @author Kate Arzamastseva <pshns@ukr.net>
* @param string $id
* @return int - revision date
*/
function media_saveOldRevision($id){
global $conf, $lang;
$oldf = mediaFN($id);
if(!@file_exists($oldf)) return '';
$date = filemtime($oldf);
if (!$conf['mediarevisions']) return $date;
if (!getRevisionInfo($id, $date, 8192, true)) {
// there was an external edit,
// there is no log entry for current version of file
if (!@file_exists(mediaMetaFN($id,'.changes'))) {
addMediaLogEntry($date, $id, DOKU_CHANGE_TYPE_CREATE, $lang['created']);
} else {
addMediaLogEntry($date, $id, DOKU_CHANGE_TYPE_EDIT);
}
}
$newf = mediaFN($id,$date);
io_makeFileDir($newf);
if(copy($oldf, $newf)) {
// Set the correct permission here.
// Always chmod media because they may be saved with different permissions than expected from the php umask.
// (Should normally chmod to $conf['fperm'] only if $conf['fperm'] is set.)
chmod($newf, $conf['fmode']);
}
return $date;
}
/**
* This function checks if the uploaded content is really what the
* mimetype says it is. We also do spam checking for text types here.
*
* We need to do this stuff because we can not rely on the browser
* to do this check correctly. Yes, IE is broken as usual.
*
* @author Andreas Gohr <andi@splitbrain.org>
* @link http://www.splitbrain.org/blog/2007-02/12-internet_explorer_facilitates_cross_site_scripting
* @fixme check all 26 magic IE filetypes here?
*/
function media_contentcheck($file,$mime){
global $conf;
if($conf['iexssprotect']){
$fh = @fopen($file, 'rb');
if($fh){
$bytes = fread($fh, 256);
fclose($fh);
if(preg_match('/<(script|a|img|html|body|iframe)[\s>]/i',$bytes)){
return -3;
}
}
}
if(substr($mime,0,6) == 'image/'){
$info = @getimagesize($file);
if($mime == 'image/gif' && $info[2] != 1){
return -1;
}elseif($mime == 'image/jpeg' && $info[2] != 2){
return -1;
}elseif($mime == 'image/png' && $info[2] != 3){
return -1;
}
# fixme maybe check other images types as well
}elseif(substr($mime,0,5) == 'text/'){
global $TEXT;
$TEXT = io_readFile($file);
if(checkwordblock()){
return -2;
}
}
return 0;
}
/**
* Send a notify mail on uploads
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function media_notify($id,$file,$mime,$old_rev=false){
global $conf;
if(empty($conf['notify'])) return false; //notify enabled?
$subscription = new Subscription();
return $subscription->send_media_diff($conf['notify'], 'uploadmail', $id, $old_rev);
}
/**
* List all files in a given Media namespace
*/
function media_filelist($ns,$auth=null,$jump='',$fullscreenview=false,$sort=false){
global $conf;
global $lang;
$ns = cleanID($ns);
// check auth our self if not given (needed for ajax calls)
if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
if (!$fullscreenview) echo '<h1 id="media__ns">:'.hsc($ns).'</h1>'.NL;
if($auth < AUTH_READ){
// FIXME: print permission warning here instead?
echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
}else{
if (!$fullscreenview) {
media_uploadform($ns, $auth);
media_searchform($ns);
}
$dir = utf8_encodeFN(str_replace(':','/',$ns));
$data = array();
search($data,$conf['mediadir'],'search_media',
array('showmsg'=>true,'depth'=>1),$dir,1,$sort);
if(!count($data)){
echo '<div class="nothing">'.$lang['nothingfound'].'</div>'.NL;
}else {
if ($fullscreenview) {
echo '<ul class="' . _media_get_list_type() . '">';
}
foreach($data as $item){
if (!$fullscreenview) {
media_printfile($item,$auth,$jump);
} else {
media_printfile_thumbs($item,$auth,$jump);
}
}
if ($fullscreenview) echo '</ul>'.NL;
}
}
}
/**
* Prints tabs for files list actions
*
* @author Kate Arzamastseva <pshns@ukr.net>
* @author Adrian Lang <mail@adrianlang.de>
*
* @param string $selected_tab - opened tab
*/
function media_tabs_files($selected_tab = ''){
global $lang;
$tabs = array();
foreach(array('files' => 'mediaselect',
'upload' => 'media_uploadtab',
'search' => 'media_searchtab') as $tab => $caption) {
$tabs[$tab] = array('href' => media_managerURL(array('tab_files' => $tab), '&'),
'caption' => $lang[$caption]);
}
html_tabs($tabs, $selected_tab);
}
/**
* Prints tabs for files details actions
*
* @author Kate Arzamastseva <pshns@ukr.net>
* @param string $image filename of the current image
* @param string $selected_tab opened tab
*/
function media_tabs_details($image, $selected_tab = ''){
global $lang, $conf;
$tabs = array();
$tabs['view'] = array('href' => media_managerURL(array('tab_details' => 'view'), '&'),
'caption' => $lang['media_viewtab']);
list(, $mime) = mimetype($image);
if ($mime == 'image/jpeg' && @file_exists(mediaFN($image))) {
$tabs['edit'] = array('href' => media_managerURL(array('tab_details' => 'edit'), '&'),
'caption' => $lang['media_edittab']);
}
if ($conf['mediarevisions']) {
$tabs['history'] = array('href' => media_managerURL(array('tab_details' => 'history'), '&'),
'caption' => $lang['media_historytab']);
}
html_tabs($tabs, $selected_tab);
}
/**
* Prints options for the tab that displays a list of all files
*
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_tab_files_options(){
global $lang;
global $INPUT;
global $ID;
$form = new Doku_Form(array('class' => 'options', 'method' => 'get',
'action' => wl($ID)));
$media_manager_params = media_managerURL(array(), '', false, true);
foreach($media_manager_params as $pKey => $pVal){
$form->addHidden($pKey, $pVal);
}
$form->addHidden('sectok', null);
if ($INPUT->has('q')) {
$form->addHidden('q', $INPUT->str('q'));
}
$form->addElement('<ul>'.NL);
foreach(array('list' => array('listType', array('thumbs', 'rows')),
'sort' => array('sortBy', array('name', 'date')))
as $group => $content) {
$checked = "_media_get_${group}_type";
$checked = $checked();
$form->addElement('<li class="' . $content[0] . '">');
foreach($content[1] as $option) {
$attrs = array();
if ($checked == $option) {
$attrs['checked'] = 'checked';
}
$form->addElement(form_makeRadioField($group, $option,
$lang['media_' . $group . '_' . $option],
$content[0] . '__' . $option,
$option, $attrs));
}
$form->addElement('</li>'.NL);
}
$form->addElement('<li>');
$form->addElement(form_makeButton('submit', '', $lang['btn_apply']));
$form->addElement('</li>'.NL);
$form->addElement('</ul>'.NL);
$form->printForm();
}
/**
* Returns type of sorting for the list of files in media manager
*
* @author Kate Arzamastseva <pshns@ukr.net>
* @return string - sort type
*/
function _media_get_sort_type() {
return _media_get_display_param('sort', array('default' => 'name', 'date'));
}
function _media_get_list_type() {
return _media_get_display_param('list', array('default' => 'thumbs', 'rows'));
}
function _media_get_display_param($param, $values) {
global $INPUT;
if (in_array($INPUT->str($param), $values)) {
// FIXME: Set cookie
return $INPUT->str($param);
} else {
$val = get_doku_pref($param, $values['default']);
if (!in_array($val, $values)) {
$val = $values['default'];
}
return $val;
}
}
/**
* Prints tab that displays a list of all files
*
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_tab_files($ns,$auth=null,$jump='') {
global $lang;
if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
if($auth < AUTH_READ){
echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
}else{
media_filelist($ns,$auth,$jump,true,_media_get_sort_type());
}
}
/**
* Prints tab that displays uploading form
*
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_tab_upload($ns,$auth=null,$jump='') {
global $lang;
if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
echo '<div class="upload">'.NL;
if ($auth >= AUTH_UPLOAD) {
echo '<p>' . $lang['mediaupload'] . '</p>';
}
media_uploadform($ns, $auth, true);
echo '</div>'.NL;
}
/**
* Prints tab that displays search form
*
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_tab_search($ns,$auth=null) {
global $INPUT;
$do = $INPUT->str('mediado');
$query = $INPUT->str('q');
echo '<div class="search">'.NL;
media_searchform($ns, $query, true);
if ($do == 'searchlist' || $query) {
media_searchlist($query,$ns,$auth,true,_media_get_sort_type());
}
echo '</div>'.NL;
}
/**
* Prints tab that displays mediafile details
*
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_tab_view($image, $ns, $auth=null, $rev=false) {
global $lang;
if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
if ($image && $auth >= AUTH_READ) {
$meta = new JpegMeta(mediaFN($image, $rev));
media_preview($image, $auth, $rev, $meta);
media_preview_buttons($image, $auth, $rev);
media_details($image, $auth, $rev, $meta);
} else {
echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
}
}
/**
* Prints tab that displays form for editing mediafile metadata
*
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_tab_edit($image, $ns, $auth=null) {
if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
if ($image) {
list(, $mime) = mimetype($image);
if ($mime == 'image/jpeg') media_metaform($image,$auth);
}
}
/**
* Prints tab that displays mediafile revisions
*
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_tab_history($image, $ns, $auth=null) {
global $lang;
global $INPUT;
if(is_null($auth)) $auth = auth_quickaclcheck("$ns:*");
$do = $INPUT->str('mediado');
if ($auth >= AUTH_READ && $image) {
if ($do == 'diff'){
media_diff($image, $ns, $auth);
} else {
$first = $INPUT->int('first');
html_revisions($first, $image);
}
} else {
echo '<div class="nothing">'.$lang['media_perm_read'].'</div>'.NL;
}
}
/**
* Prints mediafile details
*
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_preview($image, $auth, $rev=false, $meta=false) {
$size = media_image_preview_size($image, $rev, $meta);
if ($size) {
global $lang;
echo '<div class="image">';
$more = array();
if ($rev) {
$more['rev'] = $rev;
} else {
$t = @filemtime(mediaFN($image));
$more['t'] = $t;
}
$more['w'] = $size[0];
$more['h'] = $size[1];
$src = ml($image, $more);
echo '<a href="'.$src.'" target="_blank" title="'.$lang['mediaview'].'">';
echo '<img src="'.$src.'" alt="" style="max-width: '.$size[0].'px;" />';
echo '</a>';
echo '</div>'.NL;
}
}
/**
* Prints mediafile action buttons
*
* @author Kate Arzamastseva <pshns@ukr.net>
*/
function media_preview_buttons($image, $auth, $rev=false) {
global $lang, $conf;
echo '<ul class="actions">'.NL;
if($auth >= AUTH_DELETE && !$rev && @file_exists(mediaFN($image))){
// delete button
$form = new Doku_Form(array('id' => 'mediamanager__btn_delete',
'action'=>media_managerURL(array('delete' => $image), '&')));
$form->addElement(form_makeButton('submit','',$lang['btn_delete']));
echo '<li>';
$form->printForm();
echo '</li>'.NL;
}
$auth_ow = (($conf['mediarevisions']) ? AUTH_UPLOAD : AUTH_DELETE);
if($auth >= $auth_ow && !$rev){
// upload new version button
$form = new Doku_Form(array('id' => 'mediamanager__btn_update',
'action'=>media_managerURL(array('image' => $image, 'mediado' => 'update'), '&')));
$form->addElement(form_makeButton('submit','',$lang['media_update']));
echo '<li>';
$form->printForm();
echo '</li>'.NL;
}
if($auth >= AUTH_UPLOAD && $rev && $conf['mediarevisions'] && @file_exists(mediaFN($image, $rev))){
// restore button
$form = new Doku_Form(array('id' => 'mediamanager__btn_restore',
'action'=>media_managerURL(array('image' => $image), '&')));
$form->addHidden('mediado','restore');
$form->addHidden('rev',$rev);
$form->addElement(form_makeButton('submit','',$lang['media_restore']));
echo '<li>';
$form->printForm();
echo '</li>'.NL;
}
echo '</ul>'.NL;
}
/**
* Returns image width and height for mediamanager preview panel
*
* @author Kate Arzamastseva <pshns@ukr.net>
* @param string $image
* @param int $rev
* @param JpegMeta $meta
* @param int $size
* @return array
*/
function media_image_preview_size($image, $rev, $meta, $size = 500) {
if (!preg_match("/\.(jpe?g|gif|png)$/", $image) || !file_exists(mediaFN($image, $rev))) return false;
$info = getimagesize(mediaFN($image, $rev));
$w = (int) $info[0];
$h = (int) $info[1];
if($meta && ($w > $size || $h > $size)){
$ratio = $meta->getResizeRatio($size, $size);
$w = floor($w * $ratio);
$h = floor($h * $ratio);
}
return array($w, $h);
}
/**
* Returns the requested EXIF/IPTC tag from the image meta
*
* @author Kate Arzamastseva <pshns@ukr.net>
* @param array $tags
* @param JpegMeta $meta
* @param string $alt
* @return string
*/
function media_getTag($tags,$meta,$alt=''){
if($meta === false) return $alt;
$info = $meta->getField($tags);
if($info == false) return $alt;
return $info;
}
/**
* Returns mediafile tags
*
* @author Kate Arzamastseva <pshns@ukr.net>
* @param JpegMeta $meta
* @return array
*/
function media_file_tags($meta) {
// load the field descriptions
static $fields = null;
if(is_null($fields)){
$config_files = getConfigFiles('mediameta');
foreach ($config_files as $config_file) {
if(@file_exists($config_file)) include($config_file);
}
}
$tags = array();
0