Issues (57)

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.

code/forms/LanguageDropdownField.php (6 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
2
/**
3
 * An extension to dropdown field, pre-configured to list languages.
4
 * The languages already used in the site will be on top.
5
 * 
6
 * @package translatable
7
 */
8
class LanguageDropdownField extends GroupedDropdownField
9
{
10
    private static $allowed_actions = array(
11
        'getLocaleForObject'
12
    );
13
    
14
    /**
15
     * Create a new LanguageDropdownField
16
     * @param string $name
17
     * @param string $title
18
     * @param array $excludeLocales List of locales that won't be included
19
     * @param string $translatingClass Name of the class with translated instances 
20
     *               where to look for used languages
21
     * @param string $list Indicates the source language list. 
22
     *               Can be either Common-English, Common-Native, Locale-English, Locale-Native
23
     */
24
    public function __construct($name, $title, $excludeLocales = array(),
25
        $translatingClass = 'SiteTree', $list = 'Common-English', $instance = null
26
    ) {
27
        $usedLocalesWithTitle = Translatable::get_existing_content_languages($translatingClass);
28
        $usedLocalesWithTitle = array_diff_key($usedLocalesWithTitle, $excludeLocales);
29
30
        if ('Common-English' == $list) {
31
            $allLocalesWithTitle = i18n::get_common_languages();
32
        } elseif ('Common-Native' == $list) {
33
            $allLocalesWithTitle = i18n::get_common_languages(true);
34
        } elseif ('Locale-English' == $list) {
35
            $allLocalesWithTitle = i18n::get_common_locales();
36
        } elseif ('Locale-Native' == $list) {
37
            $allLocalesWithTitle = i18n::get_common_locales(true);
38
        } else {
39
            $allLocalesWithTitle = i18n::config()->all_locales;
40
        }
41
42
        if (isset($allLocales[Translatable::default_locale()])) {
0 ignored issues
show
The variable $allLocales does not exist. Did you mean $allLocalesWithTitle?

This check looks for variables that are accessed but have not been defined. It raises an issue if it finds another variable that has a similar name.

The variable may have been renamed without also renaming all references.

Loading history...
43
            unset($allLocales[Translatable::default_locale()]);
44
        }
45
        
46
        // Limit to allowed locales if defined
47
        // Check for canTranslate() if an $instance is given
48
        $allowedLocales = Translatable::get_allowed_locales();
49
        foreach ($allLocalesWithTitle as $locale => $localeTitle) {
50
            if (
51
                ($allowedLocales && !in_array($locale, $allowedLocales))
0 ignored issues
show
Bug Best Practice introduced by
The expression $allowedLocales of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
52
                || ($excludeLocales && in_array($locale, $excludeLocales))
0 ignored issues
show
Bug Best Practice introduced by
The expression $excludeLocales of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
53
                || ($usedLocalesWithTitle && array_key_exists($locale, $usedLocalesWithTitle))
0 ignored issues
show
Bug Best Practice introduced by
The expression $usedLocalesWithTitle of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
54
            ) {
55
                unset($allLocalesWithTitle[$locale]);
56
            }
57
        }
58
        // instance specific permissions
59 View Code Duplication
        foreach ($allLocalesWithTitle as $locale => $localeTitle) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
            if ($instance && !$instance->canTranslate(null, $locale)) {
61
                unset($allLocalesWithTitle[$locale]);
62
            }
63
        }
64 View Code Duplication
        foreach ($usedLocalesWithTitle as $locale => $localeTitle) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
65
            if ($instance && !$instance->canTranslate(null, $locale)) {
66
                unset($usedLocalesWithTitle[$locale]);
67
            }
68
        }
69
70
        // Sort by title (array value)
71
        asort($allLocalesWithTitle);
72
        
73
        if (count($usedLocalesWithTitle)) {
74
            asort($usedLocalesWithTitle);
75
            $source = array(
76
                _t('Form.LANGAVAIL', "Available languages") => $usedLocalesWithTitle,
77
                _t('Form.LANGAOTHER', "Other languages") => $allLocalesWithTitle
78
            );
79
        } else {
80
            $source = $allLocalesWithTitle;
81
        }
82
83
        parent::__construct($name, $title, $source);
84
    }
85
86
    public function Type()
87
    {
88
        return 'languagedropdown dropdown';
89
    }
90
    
91
    public function getAttributes()
92
    {
93
        return array_merge(
94
            parent::getAttributes(),
95
            array('data-locale-url' => $this->Link('getLocaleForObject'))
96
        );
97
    }
98
    
99
    /**
100
     * Get the locale for an object that has the Translatable extension.
101
     * 
102
     * @return locale
103
     */
104
    public function getLocaleForObject()
105
    {
106
        $id = (int)$this->getRequest()->requestVar('id');
107
        $class = Convert::raw2sql($this->getRequest()->requestVar('class'));
108
        $locale = Translatable::get_current_locale();
109
        if ($id && $class && class_exists($class) && $class::has_extension('Translatable')) {
110
            // temporarily disable locale filter so that we won't filter out the object 
111
            Translatable::disable_locale_filter();
112
            $object = DataObject::get_by_id($class, $id);
113
            Translatable::enable_locale_filter();
114
            if ($object) {
115
                $locale = $object->Locale;
116
            }
117
        }
118
        return $locale;
119
    }
120
}
121