Skip to content
Snippets Groups Projects
Commit b59a406b authored by matthiasgrimm's avatar matthiasgrimm
Browse files

media reference check

This patch implements the first step of a media file reference
checker. Every time the user wanted to delete a media file
it would be ckecked for still existing references to this media
file. File deletion is denied if this media file is still in use.

darcs-hash:20050605185038-7ef76-475e5990609587e1b8cee0e155fa6002f1c5b27c.gz
parent f0481e4f
No related branches found
No related tags found
No related merge requests found
...@@ -36,6 +36,8 @@ $conf['maxseclevel'] = 3; //Up to which level create editable se ...@@ -36,6 +36,8 @@ $conf['maxseclevel'] = 3; //Up to which level create editable se
$conf['camelcase'] = 0; //Use CamelCase for linking? (I don't like it) 0|1 $conf['camelcase'] = 0; //Use CamelCase for linking? (I don't like it) 0|1
$conf['deaccent'] = 1; //convert accented chars to unaccented ones in pagenames? $conf['deaccent'] = 1; //convert accented chars to unaccented ones in pagenames?
$conf['useheading'] = 0; //use the first heading in a page as its name $conf['useheading'] = 0; //use the first heading in a page as its name
$conf['refcheck'] = 1; //check references before deleting media files
$conf['refcount'] = 5; //search only no of references to satisfy the refcheck
/* Antispam Features */ /* Antispam Features */
......
...@@ -71,6 +71,9 @@ $lang['uploadsucc'] = 'Datei wurde erfolgreich hochgeladen'; ...@@ -71,6 +71,9 @@ $lang['uploadsucc'] = 'Datei wurde erfolgreich hochgeladen';
$lang['uploadfail'] = 'Hochladen fehlgeschlagen. Keine Berechtigung?'; $lang['uploadfail'] = 'Hochladen fehlgeschlagen. Keine Berechtigung?';
$lang['uploadwrong'] = 'Hochladen verweigert. Diese Dateiendung ist nicht erlaubt.'; $lang['uploadwrong'] = 'Hochladen verweigert. Diese Dateiendung ist nicht erlaubt.';
$lang['uploadexist'] = 'Datei existiert bereits. Keine Änderungen vorgenommen.'; $lang['uploadexist'] = 'Datei existiert bereits. Keine Änderungen vorgenommen.';
$lang['deletesucc'] = 'Die Datei "%s" wurde gelöscht.';
$lang['deletefail'] = '"%s" konnte nicht gelöscht werden - prüfen Sie die Berechtigungen.';
$lang['mediainuse'] = 'Die Datei "%s" wurde nicht gelöscht - sie wird noch verwendet.';
$lang['namespaces'] = 'Namensräume'; $lang['namespaces'] = 'Namensräume';
$lang['mediafiles'] = 'Vorhandene Dateien in'; $lang['mediafiles'] = 'Vorhandene Dateien in';
......
...@@ -69,6 +69,9 @@ $lang['uploadsucc'] = 'Upload successful'; ...@@ -69,6 +69,9 @@ $lang['uploadsucc'] = 'Upload successful';
$lang['uploadfail'] = 'Upload failed. Maybe wrong permissions?'; $lang['uploadfail'] = 'Upload failed. Maybe wrong permissions?';
$lang['uploadwrong'] = 'Upload denied. This file extension is forbidden!'; $lang['uploadwrong'] = 'Upload denied. This file extension is forbidden!';
$lang['uploadexist'] = 'File already exists. Nothing done.'; $lang['uploadexist'] = 'File already exists. Nothing done.';
$lang['deletesucc'] = 'The file "%s" has been deleted.';
$lang['deletefail'] = '"%s" couldn\'t be deleted - check perission.';
$lang['mediainuse'] = 'The file "%s" hasn\'t been deleted - it is still in use.';
$lang['namespaces'] = 'Namespaces'; $lang['namespaces'] = 'Namespaces';
$lang['mediafiles'] = 'Available files in'; $lang['mediafiles'] = 'Available files in';
......
...@@ -294,11 +294,6 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ ...@@ -294,11 +294,6 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
return false; return false;
} }
//get text
$text = io_readfile($base.'/'.$file);
//lowercase text (u modifier does not help with case)
$lctext = utf8_strtolower($text);
//create regexp from queries //create regexp from queries
$poswords = array(); $poswords = array();
$negwords = array(); $negwords = array();
...@@ -323,12 +318,77 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ ...@@ -323,12 +318,77 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
$reg = '^(?=.*?'.join(')(?=.*?',$poswords).')'; $reg = '^(?=.*?'.join(')(?=.*?',$poswords).')';
$reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$'; $reg .= count($negwords) ? '((?!'.join('|',$negwords).').)*$' : '.*$';
search_regex($data,$base,$file,$reg,$poswords);
return true;
}
/**
* Reference search
* This fuction searches for existing references to a given media file
* and returns an array with the found pages. It doesn't pay any
* attention to ACL permissions to find every reference. The caller
* must check if the user has the appropriate rights to see the found
* page and eventually have to prevent the result from displaying.
*
* @param array $data Reference to the result data structure
* @param string $base Base usually $conf['datadir']
* @param string $file current file or directory relative to $base
* @param char $type Type either 'd' for directory or 'f' for file
* @param int $lvl Current recursion depht
* @param mixed $opts option array as given to search()
*
* $opts['query'] is the demanded media file name
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function search_reference(&$data,$base,$file,$type,$lvl,$opts){
global $conf;
//we do nothing with directories
if($type == 'd') return true;
//only search txt files
if(!preg_match('#\.txt$#',$file)) return true;
//we finish after five references found. The return value
//'false' will skip subdirectories to speed search up.
if(count($data) >= $conf['refcount']) return false;
$reg = '{{ *'.$opts['query'].' *(\|.*)?}}';
search_regex($data,$base,$file,$reg,array($opts['query']));
return true;
}
/* ------------- helper functions below -------------- */
/**
* fulltext search helper
* searches a text file with a given regular expression
* no ACL checks are performed. This have to be done by
* the caller if necessary.
*
* @param array $data reference to array for results
* @param string $base base directory
* @param string $file file name to search in
* @param string $reg regular expression to search for
* @param array $words words that should be marked in the results
*
* @author Andreas Gohr <andi@splitbrain.org>
* @author Matthias Grimm <matthiasgrimm@users.sourceforge.net>
*/
function search_regex(&$data,$base,$file,$reg,$words){
//get text
$text = io_readfile($base.'/'.$file);
//lowercase text (u modifier does not help with case)
$lctext = utf8_strtolower($text);
//do the fulltext search //do the fulltext search
$matches = array(); $matches = array();
if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){ if($cnt = preg_match_all('#'.$reg.'#usi',$lctext,$matches)){
//this is not the best way for snippet generation but the fastest I could find //this is not the best way for snippet generation but the fastest I could find
$q = $poswords[0]; //use first posword for snippet $q = $words[0]; //use first word for snippet creation
$p = utf8_strpos($lctext,$q); $p = utf8_strpos($lctext,$q);
$f = $p - 100; $f = $p - 100;
$l = utf8_strlen($q) + 200; $l = utf8_strlen($q) + 200;
...@@ -336,20 +396,21 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){ ...@@ -336,20 +396,21 @@ function search_fulltext(&$data,$base,$file,$type,$lvl,$opts){
$snippet = '<span class="search_sep"> ... </span>'. $snippet = '<span class="search_sep"> ... </span>'.
htmlspecialchars(utf8_substr($text,$f,$l)). htmlspecialchars(utf8_substr($text,$f,$l)).
'<span class="search_sep"> ... </span>'; '<span class="search_sep"> ... </span>';
$mark = '('.join('|',$poswords).')'; $mark = '('.join('|', $words).')';
$snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet); $snippet = preg_replace('#'.$mark.'#si','<span class="search_hit">\\1</span>',$snippet);
$data[] = array( $data[] = array(
'id' => $id, 'id' => pathID($file),
'count' => preg_match_all('#'.$mark.'#usi',$lctext,$matches), 'count' => preg_match_all('#'.$mark.'#usi',$lctext,$matches),
'poswords' => join(' ',$poswords), 'poswords' => join(' ',$words),
'snippet' => $snippet, 'snippet' => $snippet,
); );
} }
return true; return true;
} }
/** /**
* fulltext sort * fulltext sort
* *
......
...@@ -34,11 +34,20 @@ ...@@ -34,11 +34,20 @@
} }
//handle deletion //handle deletion
if($DEL && $AUTH >= AUTH_DELETE){ $mediareferences = array();
media_delete($DEL); if($DEL && $AUTH >= AUTH_DELETE){
if($conf['refcheck']){
search($mediareferences,$conf['datadir'],'search_reference',array('query' => $DEL));
} }
if(!count($mediareferences)){
media_delete($DEL);
}else{
$text = str_replace('%s',noNS($DEL),$lang['mediainuse']);
msg($text,0);
}
}
//handle upload //handle upload
if($_FILES['upload']['tmp_name'] && $UPLOADOK){ if($_FILES['upload']['tmp_name'] && $UPLOADOK){
media_upload($NS,$AUTH); media_upload($NS,$AUTH);
} }
...@@ -58,12 +67,16 @@ ...@@ -58,12 +67,16 @@
* @author Andreas Gohr <andi@splitbrain.org> * @author Andreas Gohr <andi@splitbrain.org>
*/ */
function media_delete($delid){ function media_delete($delid){
global $lang;
$file = mediaFN($delid); $file = mediaFN($delid);
if(@unlink($file)){ if(@unlink($file)){
return true; msg(str_replace('%s',noNS($delid),$lang['deletesucc']),1);
} return true;
//something went wrong }
msg("'$file' couldn't be deleted - check permissions",-1); //something went wrong
$text = str_replace('%s',$file,$lang['deletefail']);
msg($text,-1);
return false; return false;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment