Manual:Hooks/ImgAuthBeforeStream

From MediaWiki.org
Jump to: navigation, search
ImgAuthBeforeStream
Available from version 1.16.0
Executed before file is streamed to user, but only when using img_auth.

*Define function:
function fnMyHook( $title, $path, $name, $result ) { ... }

*Attach hook:
$wgHooks['ImgAuthBeforeStream'][] = 'MyExtensionHooks::someExample';
Called from: img_auth.php

*For more information about attaching hooks, see Manual:Hooks.
*For examples of extensions using this hook, see Category:ImgAuthBeforeStream extensions.


[edit] Details

This hook is executed before the file is streamed to the browser - ONLY if you are using Image Authorization.

If the hook returns true, it will go ahead and stream the file. If not, it will display the indexed messages returned in $result[0] and $result[1].


Parameters (all passed by reference):

  • &$title: the Title object of the file as it would appear for the upload page
  • &$path: the original file and path name when img_auth was invoked by the the web server
  • &$name: the name only component of the file
  • &$result: The location to pass back results of the hook routine (only used if failed)
    • $result[0]=The index of the header message
    • $result[1]=The index of the body text message
    • $result[2 through n]=Parameters passed to body text message. Please note the header message cannot receive/use parameters.

IMPORTANT: Parameters $result[0], and $result[1] are message indices, NOT actual messages. They will be resolved and localized using wfMsgHTML($msgIndex). Parameters are only be passed to the body text message. For help in defining and using messages, see: System Messages

[edit] Example

This example hook routined checks to see if the file was stored in Namespace mode, and if so, modifies the Title to include the namespace. If the title is now invalid, returns indexes to the header and body text of the user messages as well as the name of the file ($name) as a parameter which is passed used by the body text.

$wgHooks['ImgAuthBeforeStream'][] = 'NSFileRepoImgAuthCheck';
 
function NSFileRepoImgAuthCheck($title, $path, $name, $result) {
        global $wgContLang;
 
# See if stored in a NS path

        $subdirs = explode('/',$path);
        if (strlen($subdirs[1]) == 3 && is_numeric($subdirs[1]) && $subdirs[1] >= 100)  {
                $title = Title::makeTitleSafe( NS_FILE, $wgContLang->getNsText($subdirs[1]).":".$name );
                if( !$title instanceof Title ) {
                        $result = array('img-auth-accessdenied','img-auth-badtitle',$name);
                        return false;
                }
        }
        return true;
}
Personal tools
Namespaces
Variants
Actions
Site
Support
Download
Development
Communication
Print/export
Toolbox