-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.php
214 lines (191 loc) · 5.82 KB
/
functions.php
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
/**
* Returns an authorized API client.
* @return Google_Client the authorized client object
*/
function getClient()
{
$client = new Google_Client();
$client->setApplicationName('BDDTrans Parser');
$client->setScopes(Google_Service_Sheets::SPREADSHEETS);
$client->setAuthConfig(__DIR__ . '/credentials.json');
$client->setAccessType('offline');
$client->setPrompt('select_account consent');
// Load previously authorized token from a file, if it exists.
// The file token.json stores the user's access and refresh tokens, and is
// created automatically when the authorization flow completes for the first
// time.
$tokenPath = __DIR__ . '/token.json';
if (file_exists($tokenPath)) {
$accessToken = json_decode(file_get_contents($tokenPath), true);
$client->setAccessToken($accessToken);
}
// If there is no previous token or it's expired.
if ($client->isAccessTokenExpired()) {
// Refresh the token if possible, else fetch a new one.
if ($client->getRefreshToken()) {
$client->fetchAccessTokenWithRefreshToken($client->getRefreshToken());
} else {
// Request authorization from the user.
$authUrl = $client->createAuthUrl();
printf("Open the following link in your browser:\n%s\n", $authUrl);
print 'Enter verification code: ';
$authCode = trim(fgets(STDIN));
// Exchange authorization code for an access token.
$accessToken = $client->fetchAccessTokenWithAuthCode($authCode);
$client->setAccessToken($accessToken);
// Check to see if there was an error.
if (array_key_exists('error', $accessToken)) {
throw new Exception(join(', ', $accessToken));
}
}
// Save the token to a file.
if (!file_exists(dirname($tokenPath))) {
mkdir(dirname($tokenPath), 0700, true);
}
file_put_contents($tokenPath, json_encode($client->getAccessToken()));
}
return $client;
}
/**
* Return page content of a URL
* cURL: http://php.net/manual/en/book.curl.php
* cURL Options: http://www.php.net/manual/en/function.curl-setopt.php
* @param $url URL of the request
* @param $token Login token
* @return String The page content
*/
function urlopen($url, $token = null)
{
$curl_handler = curl_init();
$options = array(CURLOPT_URL => $url,
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 3,
CURLOPT_POST => true
);
curl_setopt_array($curl_handler, $options);
if ($token) {
curl_setopt($curl_handler, CURLOPT_COOKIE, "token=$token");
}
$result = curl_exec($curl_handler);
curl_close($curl_handler);
return $result;
}
/**
* Return token from Login Cookies
* cURL: http://php.net/manual/en/book.curl.php
* cURL Options: http://www.php.net/manual/en/function.curl-setopt.php
* @param $url URL of the request
* @param $login Login for connexion to site
* @param $password Password for connexion to site
* @return String The token
*/
function getLoginToken($url, $login, $password)
{
$curl_handler = curl_init();
$options = array(CURLOPT_URL => $url,
CURLOPT_FAILONERROR => true,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_TIMEOUT => 3,
CURLOPT_POST => true,
CURLOPT_HEADER => true,
CURLOPT_POSTFIELDS => "pseudoco=$login&pwdco=$password",
CURLOPT_HTTPHEADER => array('Content-Type: application/x-www-form-urlencoded')
);
curl_setopt_array($curl_handler, $options);
$result = curl_exec($curl_handler);
curl_close($curl_handler);
preg_match('/^Set-Cookie:.*token=([a-zA-Z0-9]{64});/mi', $result, $matches);
return $matches[1];
}
/**
* Check for login status with the provided token
* @param $base_url Base URL of the site
* @param $token Login token
* @return Bool, True if logged in, False otherwise
*/
function checkLoginStatus($base_url, $token)
{
$html = urlopen($base_url . '/accueil/', $token);
$xpath = getHtmlDomXPath($html);
$connexion_links = $xpath->query("//div[@class='col_right']/a[@class='connexion']");
$connexion_link = $connexion_links->item(0)->getAttribute("href");
if ($connexion_link == '../userpanel/') {
return true;
}
return false;
}
/**
* Return the comments for the specified page
* @param $url URL of the page
* @param $token Login token
* @return Array All comments
*/
function getComments($url, $token)
{
$html = urlopen($url, $token);
$xpath = getHtmlDomXPath($html);
$view_com = $xpath->query("//div[@class='view_com']");
$comments = array();
foreach ($view_com as $com) {
$tag = $xpath->query("p[@class='view_com_tag']/span", $com)->item(0)->textContent;
$body = $xpath->query("p[@class='view_com_body']", $com)->item(0)->textContent;
array_push($comments, array(
'tag' => $tag,
'body' => ($body)
));
}
return $comments;
}
/**
* @param $html
* @return DOMXpath
*/
function getHtmlDomXPath($html)
{
$doc = new DOMDocument();
libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_clear_errors();
return new DOMXpath($doc);
}
function writeCSV($data, $csvfile)
{
$fp = fopen($csvfile, 'w');
$header = false;
foreach ($data as $row)
{
if (empty($header))
{
$header = array_keys($row);
fputcsv($fp, $header);
$header = array_flip($header);
}
fputcsv($fp, array_merge($header, $row));
}
fclose($fp);
return;
}
function updateGoogleSheet($spreadsheetId, $values)
{
$client = getClient();
$service = new Google_Service_Sheets($client);
$range = "A:Z";
$body = new Google_Service_Sheets_ValueRange([
'values' => $values
]);
$params = [
'valueInputOption' => "RAW"
];
$result = $service->spreadsheets_values->update($spreadsheetId, $range, $body, $params);
printf("%d rows updated.\n", $result->getUpdatedRows());
// return array(
// 'spreadsheetId' => $result->spreadsheetId,
// 'updatedCells' => $result->updatedCells,
// 'updatedColumns' => $result->updatedColumns,
// 'updatedRange' => $result->updatedRange,
// 'updatedRows' => $result->updatedRows
// );
return;
}