Utils::getBodyByVirginProxies()   B
last analyzed

Complexity

Conditions 4
Paths 8

Size

Total Lines 36
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 36
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 21
nc 8
nop 3
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: lenon
5
 * Date: 03/04/16
6
 * Time: 19:24.
7
 */
8
9
namespace Aszone\SearchHacking;
10
11
use Symfony\Component\DomCrawler\Crawler;
12
use Aszone\FakeHeaders\FakeHeaders;
13
use GuzzleHttp\Client;
14
15
class Utils
16
{
17 View Code Duplication
    public static function sanitazeLinks($links = array())
0 ignored issues
show
Duplication introduced by
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...
18
    {
19
        $hrefs = array();
20
21
        if (!empty($links)) {
22
            foreach ($links as $keyLink => $valueLink) {
23
                $url = static::clearLink($valueLink->getAttribute('href'));
24
                $validResultOfBlackList = static::checkBlacklist($url);
0 ignored issues
show
Security Bug introduced by
It seems like $url defined by static::clearLink($value...->getAttribute('href')) on line 23 can also be of type false; however, Aszone\SearchHacking\Utils::checkBlacklist() does only seem to accept string, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

Loading history...
25
26
                if (!$validResultOfBlackList and $url) {
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
27
                    $hrefs[] = $url;
28
                }
29
            }
30
31
            $hrefs = array_unique($hrefs);
32
        }
33
34
        return $hrefs;
35
    }
36
37
    public static function checkBlacklist($url = '')
38
    {
39
        if (!empty($url)) {
40
            $validXmlrpc = preg_match("/(https?\:\/\/|^)(.+?)\//", $url, $matches, PREG_OFFSET_CAPTURE);
0 ignored issues
show
Unused Code introduced by
$validXmlrpc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
41
            $url = '';
42
43
            if (isset($matches[2][0])) {
44
                $url = $matches[2][0];
45
            }
46
47
            $ini_blakclist = parse_ini_file(__DIR__.'/../resources/Blacklist.ini');
48
49
            $key = array_search($url, $ini_blakclist);
50
51
            if ($key != false) {
52
                return true;
53
            }
54
        }
55
56
        return false;
57
    }
58
59
    public static function clearLink($url = '')
60
    {
61
        if (!empty($url)) {
62
            $validXmlrpc = preg_match('/search%3Fq%3Dcache:.+?:(.+?)%252B/', $url, $matches, PREG_OFFSET_CAPTURE);
0 ignored issues
show
Unused Code introduced by
$validXmlrpc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
63
64
            if (isset($matches[1][0])) {
65
                return $matches[1][0];
66
            }
67
68
            $validXmlrpc = preg_match("/search\?q=cache:.+?:(.+?)\+/", $url, $matches, PREG_OFFSET_CAPTURE);
0 ignored issues
show
Unused Code introduced by
$validXmlrpc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
69
70
            if (isset($matches[1][0])) {
71
                return $matches[1][0];
72
            }
73
74
            $validXmlrpc = preg_match('/url=(.*?)&tld/', $url, $matches, PREG_OFFSET_CAPTURE);
0 ignored issues
show
Unused Code introduced by
$validXmlrpc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
75
76
            if (isset($matches[1][0])) {
77
                return urldecode($matches[1][0]);
78
            }
79
80
            //Msn Bing
81
            $validXmlrpc = preg_match("/^((http|https):\/\/|www).+?\/?ld=.+?\&u=(.+?)\n/", $url, $matches, PREG_OFFSET_CAPTURE);
0 ignored issues
show
Unused Code introduced by
$validXmlrpc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
82
83
            if (isset($matches[1][0])) {
84
                return urldecode($matches[1][0]);
85
            }
86
87
            $validXmlrpc = preg_match("/^((http|https):\/\/|www)(.+?)\//", $url, $matches, PREG_OFFSET_CAPTURE);
0 ignored issues
show
Unused Code introduced by
$validXmlrpc is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88
89
            if (isset($matches[0][0])) {
90
                $check[] = strpos($url, 'www.blogger.com');
0 ignored issues
show
Coding Style Comprehensibility introduced by
$check was never initialized. Although not strictly required by PHP, it is generally a good practice to add $check = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
91
                $check[] = strpos($url, 'youtube.com');
92
                $check[] = strpos($url, '.google.');
93
                $check[] = strpos($url, 'yandex.ru');
94
                $check[] = strpos($url, 'microsoft.com');
95
                $check[] = strpos($url, 'microsofttranslator.com');
96
                $check[] = strpos($url, '.yahoo.com');
97
                $check[] = strpos($url, 'yahoo.uservoice.com');
98
                $check[] = strpos($url, 'www.mozilla.org');
99
                $check[] = strpos($url, 'www.facebook.com');
100
                $check[] = strpos($url, 'go.mail.ru');
101
                $check[] = strpos($url, '/search/srpcache?p=');
102
                $check[] = strpos($url, 'flickr.com');
103
104
                $tmp = array_filter($check);
105
106
                if (empty($tmp)) {
107
                    return trim($url);
108
                }
109
            }
110
        }
111
112
        return false;
113
    }
114
115
    public static function getLinks($body)
116
    {
117
        $crawler = new Crawler($body);
118
119
        return $crawler->filter('a');
120
    }
121
122
    public static function getBody($urlOfSearch, $proxy)
123
    {
124
        $header = new FakeHeaders();
125
        $valid = true;
0 ignored issues
show
Unused Code introduced by
$valid is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
126
127
        try {
128
            $client = new Client([
129
                'defaults' => [
130
                    'headers' => ['User-Agent' => $header->getUserAgent()],
131
                    'proxy' => $proxy,
132
                    'timeout' => 60,
133
                ],
134
            ]);
135
136
            return $client->get($urlOfSearch)->getBody()->getContents();
137
        } catch (\Exception $e) {
138
            $message = 'ERROR : '.$e->getMessage()."\n";
139
140
            if ($proxy == false) {
141
                $message .= "Your ip is blocked, we are using proxy at now...\n";
142
            }
143
144
            return $message;
145
        }
146
147
        return false;
0 ignored issues
show
Unused Code introduced by
return false; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
148
    }
149
150
    public static function getBodyByVirginProxies($urlOfSearch, $urlProxie, $proxy)
151
    {
152
        $header = new FakeHeaders();
153
154
        echo 'Proxy : '.$urlProxie."\n";
155
156
        $dataToPost = ['body' => ['url' => $urlOfSearch]];
157
158
        $valid = true;
159
        while ($valid == true) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison === instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
160
            try {
161
                $client = new Client([
162
                    'defaults' => [
163
                        'headers' => ['User-Agent' => $header->getUserAgent()],
164
                        'proxy' => $proxy,
165
                        'timeout' => 60,
166
                    ],
167
                ]);
168
169
                $res = $client->post($urlProxie, $dataToPost);
170
                $body = $res->getBody()->getContents();
171
172
                //check if change new tor ip
173
                $valid = false;
174
            } catch (\Exception $e) {
175
                echo 'ERROR : '.$e->getMessage()."\n";
176
                if ($proxy == false) {
177
                    echo "This ip of virgin proxy is blocked, we are using proxy at now...\n";
178
                }
179
180
                return 'repeat';
181
            }
182
        }
183
184
        return $body;
0 ignored issues
show
Bug introduced by
The variable $body 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...
185
    }
186
}
187