Issues (994)

src/Netscape/parser.php (1 issue)

1
<?php
2
3
namespace Netscape;
4
5
use function GuzzleHttp\json_encode;
6
7
/**
8
 * Netscape proxy parser.
9
 *
10
 * @author Dimas Lanjaka <[email protected]>
11
 */
12
class parser
13
{
14
  public static function getCookies($file)
15
  {
16
    if (realpath($file)) {
17
      return file_get_contents(realpath($file));
18
    }
19
  }
20
21
  public static function netscape2guzzle(string $cookietxt)
22
  {
23
    $parsed = self::extractCookies($cookietxt);
24
    $guzzle = [];
25
    foreach ($parsed as $cook) {
26
      $ready = [];
27
      foreach ($cook as $key => $value) {
28
        $key = ucfirst($key);
29
        switch ($key) {
30
      case 'Httponly':
31
      $key = 'HttpOnly';
32
      break;
33
      case 'Expiration-epoch':
34
      $key = 'Expires';
35
      break;
36
      case 'value':
37
      // code...
38
      break;
39
    }
40
        $ready[$key] = $value;
41
      }
42
      $guzzle[] = $ready;
43
    }
44
    //var_dump($guzzle, $parsed);
45
    return json_encode($guzzle);
46
  }
47
48
  /**
49
   * Extract any cookies found from the cookie file. This function expects to get
50
   * a string containing the contents of the cookie file which it will then
51
   * attempt to extract and return any cookies found within.
52
   *
53
   * @param string $string the contents of the cookie file
54
   *
55
   * @return array the array of cookies as extracted from the string
56
   */
57
  public static function extractCookies($string)
58
  {
59
    $lines = explode(PHP_EOL, $string);
60
61
    foreach ($lines as $line) {
62
      $cookie = [];
63
64
      // detect httponly cookies and remove #HttpOnly prefix
65
      if ('#HttpOnly_' == substr($line, 0, 10)) {
66
        $line = substr($line, 10);
67
        $cookie['httponly'] = true;
68
      } else {
69
        $cookie['httponly'] = false;
70
      }
71
72
      // we only care for valid cookie def lines
73
      if (strlen($line) > 0 && '#' != $line[0] && 6 == substr_count($line, "\t")) {
74
        // get tokens in an array
75
        $tokens = explode("\t", $line);
76
77
        // trim the tokens
78
        $tokens = array_map('trim', $tokens);
79
80
        // Extract the data
81
    $cookie['domain'] = $tokens[0]; // The domain that created AND can read the variable.
82
    $cookie['flag'] = $tokens[1];   // A TRUE/FALSE value indicating if all machines within a given domain can access the variable.
83
    $cookie['path'] = $tokens[2];   // The path within the domain that the variable is valid for.
84
    $cookie['secure'] = $tokens[3]; // A TRUE/FALSE value indicating if a secure connection with the domain is needed to access the variable.
85
86
    $cookie['expiration-epoch'] = $tokens[4];  // The UNIX time that the variable will expire on.
87
    $cookie['name'] = urldecode($tokens[5]);   // The name of the variable.
88
    $cookie['value'] = urldecode($tokens[6]);  // The value of the variable.
89
90
    // Convert date to a readable format
91
        $cookie['expiration'] = date('Y-m-d h:i:s', $tokens[4]);
92
93
        // Record the cookie.
94
        $cookies[] = $cookie;
95
      }
96
    }
97
98
    return $cookies;
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $cookies does not seem to be defined for all execution paths leading up to this point.
Loading history...
99
  }
100
}
101