forked from zmarz/whatcd-php
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwhatcd.class.php
More file actions
executable file
·158 lines (121 loc) · 3.84 KB
/
whatcd.class.php
File metadata and controls
executable file
·158 lines (121 loc) · 3.84 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<?php
/**
* PHP class for accessing What.CD's API
*
* @author GLaDOSDan
* @license http://www.opensource.org/licenses/bsd-license.php BSD License
* @link https://github.com/GLaDOSDan/whatcd-php
*/
class WhatCD {
private $username;
private $password;
public $URI = 'https://what.cd/ajax.php';
/**
* Constructor to load username and password into variables for other functions in the class to access
* @param str $username
* @param str $password
* @return null
*/
public function __construct($username, $password){
$this->username = $username;
$this->password = $password;
$this->cookie_jar = dirname(__FILE__).'./tmp/what.cd.cookies';
if (empty($this->password) || empty($this->username)){
throw new Exception('[What.CD API] Invalid username or password passed to constructor');
}
return null;
}
/**
* Logs into what.cd
* @return boolean
*/
public function login(){
$POST = array(
'username' => $this->username,
'password' => $this->password,
'keeplogged' => '1',
'login' => 'Log in'
);
$ch = curl_init('https://what.cd/login.php');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'thejester/whatcd-php');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_jar);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $POST);
$return = curl_exec($ch);
curl_close($ch);
if (false !== strpos($return, 'Your username or password was incorrect')){
// Username or password incorrect, throw exception
throw new Exception('[What.CD API] Username or password rejected by What.CD');
}
if (false !== strpos($return, 'Manage sessions')){
// Logged in
return true;
}
return true;
}
/**
* Interface for making request to What.CD API
* @param str $action
* @param array $arguments
* @return array - parsed JSON response
*/
public function request($action, $arguments = null){
return $this->parse_response($this->curl_request($action, $arguments));
}
/**
* Parse response from What.CD and return the results
* @param str $data - JSON response from API
* @return array - parsed JSON response
*/
public function parse_response($data){
$json = json_decode($data, true);
if ($json['status'] == 'success'){
return $json['response'];
}
if ($json['status'] == 'failure'){
if (!isset($json['error'])){
throw new Exception('[What.CD API] Generic API failure');
} else {
throw new Exception('[What.CD API] Error: ' . $json['error']);
}
}
}
/**
* Make cURL request to API
* @param str $action
* @param array $arguments
* @return str $response
*/
public function curl_request($action, $arguments = null){
$URI = $this->URI;
$args[] = '?action=' . $action;
if (!empty($arguments)){
foreach ($arguments as $attribute => $value){
$args[] = urlencode($attribute) . '=' . urlencode($value);
}
}
$URI .= implode('&', $args);
$ch = curl_init($URI);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'thejester/whatcd-php');
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookie_jar);
curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookie_jar);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
$HTTP_CODE = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
if ($HTTP_CODE == 302){
if ($this->login()){
return $this->request($action, $arguments, true);
}
return false;
}
return $response;
}
}