Issues (4122)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

maintenance/generateJsonI18n.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 35 and the first side effect is on line 28.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
/**
4
 * Convert a PHP messages file to a set of JSON messages files.
5
 *
6
 * Usage:
7
 *    php generateJsonI18n.php ExtensionName.i18n.php i18n/
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License along
20
 * with this program; if not, write to the Free Software Foundation, Inc.,
21
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22
 * http://www.gnu.org/copyleft/gpl.html
23
 *
24
 * @file
25
 * @ingroup Maintenance
26
 */
27
28
require_once __DIR__ . '/Maintenance.php';
29
30
/**
31
 * Maintenance script to generate JSON i18n files from a PHP i18n file.
32
 *
33
 * @ingroup Maintenance
34
 */
35
class GenerateJsonI18n extends Maintenance {
36 View Code Duplication
	public function __construct() {
37
		parent::__construct();
38
		$this->addDescription( 'Build JSON messages files from a PHP messages file' );
39
40
		$this->addArg( 'phpfile', 'PHP file defining a $messages array', false );
41
		$this->addArg( 'jsondir', 'Directory to write JSON files to', false );
42
		$this->addOption( 'extension', 'Perform default conversion on an extension',
43
			false, true );
44
		$this->addOption( 'supplementary', 'Find supplementary i18n files in subdirs and convert those',
45
			false, false );
46
	}
47
48
	public function execute() {
49
		global $IP;
50
51
		$phpfile = $this->getArg( 0 );
52
		$jsondir = $this->getArg( 1 );
53
		$extension = $this->getOption( 'extension' );
54
		$convertSupplementaryI18nFiles = $this->hasOption( 'supplementary' );
55
56
		if ( $extension ) {
57
			if ( $phpfile ) {
58
				$this->error( "The phpfile is already specified, conflicts with --extension.", 1 );
59
			}
60
			$phpfile = "$IP/extensions/$extension/$extension.i18n.php";
61
		}
62
63
		if ( !$phpfile ) {
64
			$this->error( "I'm here for an argument!" );
65
			$this->maybeHelp( true );
66
			// dies.
67
		}
68
69
		if ( $convertSupplementaryI18nFiles ) {
70
			if ( is_readable( $phpfile ) ) {
71
				$this->transformI18nFile( $phpfile, $jsondir );
72
			} else {
73
				// This is non-fatal because we might want to continue searching for
74
				// i18n files in subdirs even if the extension does not include a
75
				// primary i18n.php.
76
				$this->error( "Warning: no primary i18n file was found." );
77
			}
78
			$this->output( "Searching for supplementary i18n files...\n" );
79
			$dir_iterator = new RecursiveDirectoryIterator( dirname( $phpfile ) );
80
			$iterator = new RecursiveIteratorIterator(
81
				$dir_iterator, RecursiveIteratorIterator::LEAVES_ONLY );
82
			foreach ( $iterator as $path => $fileObject ) {
83
				if ( fnmatch( "*.i18n.php", $fileObject->getFilename() ) ) {
84
					$this->output( "Converting $path.\n" );
85
					$this->transformI18nFile( $path );
86
				}
87
			}
88
		} else {
89
			// Just convert the primary i18n file.
90
			$this->transformI18nFile( $phpfile, $jsondir );
91
		}
92
	}
93
94
	public function transformI18nFile( $phpfile, $jsondir = null ) {
95
		if ( !$jsondir ) {
96
			// Assume the json directory should be in the same directory as the
97
			// .i18n.php file.
98
			$jsondir = dirname( $phpfile ) . "/i18n";
99
		}
100
		if ( !is_dir( $jsondir ) ) {
101
			$this->output( "Creating directory $jsondir.\n" );
102
			$success = mkdir( $jsondir );
103
			if ( !$success ) {
104
				$this->error( "Could not create directory $jsondir", 1 );
105
			}
106
		}
107
108
		if ( !is_readable( $phpfile ) ) {
109
			$this->error( "Error reading $phpfile", 1 );
110
		}
111
		include $phpfile;
112
		$phpfileContents = file_get_contents( $phpfile );
113
114
		if ( !isset( $messages ) ) {
0 ignored issues
show
The variable $messages seems to never exist, and therefore isset should always return false. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
115
			$this->error( "PHP file $phpfile does not define \$messages array", 1 );
116
		}
117
118
		if ( !$messages ) {
0 ignored issues
show
The variable $messages does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
119
			$this->error( "PHP file $phpfile contains an empty \$messages array. " .
120
				"Maybe it was already converted?", 1 );
121
		}
122
123
		if ( !isset( $messages['en'] ) || !is_array( $messages['en'] ) ) {
124
			$this->error( "PHP file $phpfile does not set language codes", 1 );
125
		}
126
127
		foreach ( $messages as $langcode => $langmsgs ) {
128
			$authors = $this->getAuthorsFromComment( $this->findCommentBefore(
129
				"\$messages['$langcode'] =",
130
				$phpfileContents
131
			) );
132
			// Make sure the @metadata key is the first key in the output
133
			$langmsgs = array_merge(
134
				[ '@metadata' => [ 'authors' => $authors ] ],
135
				$langmsgs
136
			);
137
138
			$jsonfile = "$jsondir/$langcode.json";
139
			$success = file_put_contents(
140
				$jsonfile,
141
				FormatJson::encode( $langmsgs, "\t", FormatJson::ALL_OK ) . "\n"
142
			);
143
			if ( $success === false ) {
144
				$this->error( "FAILED to write $jsonfile", 1 );
145
			}
146
			$this->output( "$jsonfile\n" );
147
		}
148
149
		$this->output(
150
			"All done. To complete the conversion, please do the following:\n" .
151
			"* Add \$wgMessagesDirs['YourExtension'] = __DIR__ . '/i18n';\n" .
152
			"* Remove \$wgExtensionMessagesFiles['YourExtension']\n" .
153
			"* Delete the old PHP message file\n" .
154
			"This script no longer generates backward compatibility shims! If you need\n" .
155
			"compatibility with MediaWiki 1.22 and older, use the MediaWiki 1.23 version\n" .
156
			"of this script instead, or create a shim manually.\n"
157
		);
158
	}
159
160
	/**
161
	 * Find the documentation comment immediately before a given search string
162
	 * @param string $needle String to search for
163
	 * @param string $haystack String to search in
164
	 * @return string Substring of $haystack starting at '/**' ending right before $needle, or empty
165
	 */
166
	protected function findCommentBefore( $needle, $haystack ) {
167
		$needlePos = strpos( $haystack, $needle );
168
		if ( $needlePos === false ) {
169
			return '';
170
		}
171
		// Need to pass a negative offset to strrpos() so it'll search backwards from the
172
		// offset
173
		$startPos = strrpos( $haystack, '/**', $needlePos - strlen( $haystack ) );
174
		if ( $startPos === false ) {
175
			return '';
176
		}
177
178
		return substr( $haystack, $startPos, $needlePos - $startPos );
179
	}
180
181
	/**
182
	 * Get an array of author names from a documentation comment containing @author declarations.
183
	 * @param string $comment Documentation comment
184
	 * @return array Array of author names (strings)
185
	 */
186
	protected function getAuthorsFromComment( $comment ) {
187
		$matches = null;
188
		preg_match_all( '/@author (.*?)$/m', $comment, $matches );
189
190
		return $matches && $matches[1] ? $matches[1] : [];
191
	}
192
}
193
194
$maintClass = "GenerateJsonI18n";
195
require_once RUN_MAINTENANCE_IF_MAIN;
196