Issues (10)

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.

src/Service/Search.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
2
/**
3
 * AnimeDb package.
4
 *
5
 * @author    Peter Gribanov <[email protected]>
6
 * @copyright Copyright (c) 2011, Peter Gribanov
7
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
8
 */
9
namespace AnimeDb\Bundle\AniDbFillerBundle\Service;
10
11
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Search as SearchPlugin;
12
use AnimeDb\Bundle\CatalogBundle\Plugin\Fill\Search\Item as ItemSearch;
13
use AnimeDb\Bundle\AniDbBrowserBundle\Service\Browser;
14
use Knp\Menu\ItemInterface;
15
16
/**
17
 * Search from site AniDB.net.
18
 */
19
class Search extends SearchPlugin
20
{
21
    /**
22
     * @var string
23
     */
24
    const NAME = 'anidb';
25
26
    /**
27
     * @var string
28
     */
29
    const TITLE = 'AniDB.net';
30
31
    /**
32
     * @var string
33
     */
34
    const ITEM_LINK = '/perl-bin/animedb.pl?show=anime&aid=#ID#';
35
36
    /**
37
     * @var Browser
38
     */
39
    protected $browser;
40
41
    /**
42
     * @var string
43
     */
44
    protected $titles_db;
45
46
    /**
47
     * @var string
48
     */
49
    protected $locale;
50
51
    /**
52
     * @param Browser $browser
53
     * @param string $titles_db
54
     * @param string $cache_dir
55
     * @param string $locale
56
     */
57
    public function __construct(Browser $browser, $titles_db, $cache_dir, $locale)
58
    {
59
        $this->browser = $browser;
60
        $this->locale = $locale;
61
        $this->titles_db = $cache_dir.'/'.$titles_db;
62
    }
63
64
    /**
65
     * @return string
66
     */
67
    public function getName()
68
    {
69
        return self::NAME;
70
    }
71
72
    /**
73
     * @return string
74
     */
75
    public function getTitle()
76
    {
77
        return self::TITLE;
78
    }
79
80
    /**
81
     * Build menu for plugin.
82
     *
83
     * @param ItemInterface $item
84
     *
85
     * @return ItemInterface
86
     */
87
    public function buildMenu(ItemInterface $item)
88
    {
89
        return parent::buildMenu($item)
90
            ->setLinkAttribute('class', 'icon-label icon-label-plugin-anidb');
91
    }
92
93
    /**
94
     * Search source by name.
95
     *
96
     * @param array $data
97
     *
98
     * @return ItemSearch[]
99
     */
100
    public function search(array $data)
101
    {
102
        // if the db does not exists, send a request to download
103
        if (!file_exists($this->titles_db)) {
104
            return [];
105
        }
106
107
        $search = $this->getUnifiedTitle($data['name']);
108
109
        // search by name
110
        $aids = [];
111
        $fp = gzopen($this->titles_db, 'r');
112
        while (!gzeof($fp)) {
113
            $line = trim(gzgets($fp, 4096));
114
            list($aid, $type, $lang, $unified) = explode('|', $line);
115
            if (mb_strpos($unified, $search, 0, 'utf8') === 0) {
116
                if ($type == 1 || ($type == 4 && $lang == $this->locale) || empty($titles[$aid])) {
117
                    $aids[] = $aid;
118
                }
119
            }
120
        }
121
        gzclose($fp);
122
        $aids = array_unique($aids);
123
124
        // get all names for aid
125
        $items = [];
126
        $fp = gzopen($this->titles_db, 'r');
127
        while (!gzeof($fp)) {
128
            $line = trim(gzgets($fp, 4096));
129
            list($aid, $type, $lang, , $title) = explode('|', $line);
130
            if (in_array($aid, $aids)) {
131
                $items[$aid][$lang][$type] = $title;
132
            }
133
        }
134
        gzclose($fp);
135
136
        // build result
137
        foreach ($items as $aid => $item) {
138
            if (!empty($item[$this->locale])) {
139
                $main_name = $this->getNameForLocale($this->locale, $item);
140 View Code Duplication
            } elseif ($this->locale != 'en' && !empty($item['en'])) {
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...
141
                $main_name = $this->getNameForLocale('en', $item);
142
            } else {
143
                $main_name = $this->getNameForLocale(array_keys($item)[0], $item);
144
            }
145
            $description = [];
146
            foreach ($item as $names) {
147
                foreach ($names as $name) {
148
                    $description[] = $name;
149
                }
150
            }
151
            sort($description);
152
            $items[$aid] = new ItemSearch(
153
                $main_name,
154
                $this->getLinkForFill($this->browser->getHost().str_replace('#ID#', $aid, self::ITEM_LINK)),
155
                $this->router->generate('ani_db_media_cover', ['id' => $aid]),
156
                implode("\n", array_unique($description)),
157
                $this->browser->getHost().str_replace('#ID#', $aid, self::ITEM_LINK)
158
            );
159
        }
160
161
        return $items;
162
    }
163
164
    /**
165
     * @param string $title
166
     *
167
     * @return string
168
     */
169 View Code Duplication
    protected function getUnifiedTitle($title)
0 ignored issues
show
This method seems to be duplicated in 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...
170
    {
171
        $title = mb_strtolower($title, 'utf8');
172
        $title = preg_replace('/\W+/u', ' ', $title);
173
174
        return trim($title);
175
    }
176
177
    /**
178
     * @param string $locale
179
     * @param array $names
180
     *
181
     * @return string
182
     */
183 View Code Duplication
    protected function getNameForLocale($locale, &$names)
0 ignored issues
show
This method seems to be duplicated in 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...
184
    {
185
        if (isset($names[$locale][1])) {
186
            $name = $names[$locale][1];
187
            unset($names[$locale][1]);
188
        } elseif (isset($names[$locale][4])) {
189
            $name = $names[$locale][4];
190
            unset($names[$locale][4]);
191
        } else {
192
            $name = array_shift($names[$locale]);
193
        }
194
195
        return $name;
196
    }
197
}
198