Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
NSFileRepoHooks
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 5
132
0.00% covered (danger)
0.00%
0 / 1
 register
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 onBeforePageDisplay
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 onImgAuthBeforeCheckFileExists
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
12
 onImgAuthBeforeStream
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
12
 onUploadVerification
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3use MediaWiki\MediaWikiServices;
4
5class NSFileRepoHooks {
6    public static function register() {
7        require_once __DIR__ . '/DefaultSettings.php';
8        $GLOBALS['wgLocalFileRepo']['class'] = "NSLocalRepo";
9    }
10
11    /**
12     * Add JavaScript
13     * @param OutputPage &$out
14     * @param Skin &$skin
15     * @return bool true
16     */
17    public static function onBeforePageDisplay( &$out, &$skin ) {
18        if ( $out->getTitle()->isSpecial( 'Upload' ) ) {
19            $out->addModules( 'ext.nsfilerepo.special.upload' );
20        }
21
22        return true;
23    }
24
25    /**
26     * @param &$path
27     * @param &$name
28     * @param &$filename
29     * @return bool
30     */
31    public static function onImgAuthBeforeCheckFileExists( &$path, &$name, &$filename ) {
32        $nsfrhelper = new NSFileRepoHelper();
33        $title = $nsfrhelper->getTitleFromPath( $path );
34        if ( $title instanceof Title && $title->getNamespace() !== NS_MAIN ) {
35            // Not using "$title->getPrefixedDBKey()" because "$wgCapitalLinkOverrides[NS_FILE]" may be "false"
36            $name = $title->getNsText() . ':' . $name;
37        }
38
39        return true;
40    }
41
42    /**
43     * @param Title $title
44     * @param $path
45     * @param $name
46     * @param &$result
47     * @return bool
48     */
49    public static function onImgAuthBeforeStream( $title, $path, $name, &$result ) {
50        $nsfrhelper = new NSFileRepoHelper();
51        $authTitle = $nsfrhelper->getTitleFromPath( $path );
52
53        if ( $authTitle instanceof Title === false ) {
54            $result = array( 'img-auth-accessdenied', 'img-auth-badtitle', $name );
55            return false;
56        }
57
58        $context = RequestContext::getMain();
59        $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
60        if ( !$permissionManager->userCan( 'read', $context->getUser(), $authTitle ) ) {
61            $result = array( 'img-auth-accessdenied', 'img-auth-noread', $name );
62            return false;
63        }
64
65        return true;
66    }
67
68    /**
69     * Checks if the destination file name contains a valid namespace prefix
70     * @param string $destName
71     * @param string $tempPath
72     * @param string &$error
73     * @return bool
74     */
75    public static function onUploadVerification( $destName, $tempPath, &$error ) {
76        $title = Title::newFromText( $destName );
77        // There is a colon in the name, but it was not a valid namespace prefix!
78        if ( strpos( $title->getText(), ':' ) !== false ) {
79            $error = 'illegal-filename';
80            return false;
81        }
82        return true;
83    }
84}