-
Notifications
You must be signed in to change notification settings - Fork 1
/
Tool.php
176 lines (137 loc) · 5.61 KB
/
Tool.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
<?php
declare(strict_types=1);
namespace Gingdev\Facebook;
use Goutte\Client;
use Symfony\Component\BrowserKit\CookieJar;
use Symfony\Component\BrowserKit\Response;
class Tool
{
public const TOKEN_URL = 'https://mbasic.facebook.com/dialog/oauth';
/** @var string[] */
protected $queryData = [
'client_id' => '124024574287414',
'redirect_uri' => 'fbconnect://success',
'scope' => 'email,read_insights,read_audience_network_insights,rsvp_event,offline_access,publish_video,openid,catalog_management,user_managed_groups,groups_show_list,pages_manage_cta,pages_manage_instant_articles,pages_show_list,pages_messaging,pages_messaging_phone_number,pages_messaging_subscriptions,read_page_mailboxes,ads_management,ads_read,business_management,instagram_basic,instagram_manage_comments,instagram_manage_insights,instagram_content_publish,publish_to_groups,groups_access_member_info,leads_retrieval,whatsapp_business_management,attribution_read,pages_read_engagement,pages_manage_metadata,pages_read_user_content,pages_manage_ads,pages_manage_posts,pages_manage_engagement,audience_network_placement_management,public_profile',
'response_type' => 'token',
];
protected string $accessToken = '';
protected Client $browser;
public function __construct(string $filename)
{
$json = \file_get_contents($filename);
if (false === $json) {
throw new \RuntimeException("Unable to load file {$filename}");
}
$cookies = \json_decode($json, true);
if (\JSON_ERROR_NONE !== \json_last_error()) {
throw new \InvalidArgumentException('json_decode error: '.\json_last_error_msg());
}
if (!\is_array($cookies)) {
throw new \RuntimeException("Invalid cookie file: {$filename}");
}
/** @var string[] $cookies */
$cookieJar = new CookieJar();
$cookieJar->updateFromSetCookie($cookies);
$this->browser = new Client(cookieJar: $cookieJar);
}
public function requestAccessToken(): void
{
$this->browser->request('GET', self::TOKEN_URL.'?'.http_build_query($this->queryData));
$this->browser->followRedirects(false);
try {
$form = $this->browser
->getCrawler()
->filter('form')
->form();
} catch (\InvalidArgumentException) {
throw new \LogicException('Cookies have expired or are not valid.');
}
$this->browser->submit($form);
/** @var Response */
$resp = $this->browser->getResponse();
/** @var string */
$location = $resp->getHeader('location');
parse_str(
parse_url($location, PHP_URL_FRAGMENT),
$data
);
$this->accessToken = (string) $data['access_token'];
}
public function getAccessToken(): string
{
return $this->accessToken;
}
public function getBrowser(): Client
{
return $this->browser;
}
public function likePost(string $id, int $mode = 0): bool
{
$crawler = $this->browser->request('GET', 'https://mbasic.facebook.com/reactions/picker/?ft_id='.$id);
try {
$link = $crawler->filter('a[style="display:block"]')->eq($mode)->link();
$this->browser->click($link);
} catch (\InvalidArgumentException) {
return false;
}
return true;
}
public function likePage(string $id): bool
{
$crawler = $this->browser->request('GET', 'https://mbasic.facebook.com/'.$id.'/about');
try {
$link = $crawler->filterXPath('//a[contains(@href, "/a/profile.php")]')->link();
$this->browser->click($link);
} catch (\InvalidArgumentException) {
return false;
}
return true;
}
public function followPage(string $id): bool
{
$crawler = $this->browser->request('GET', 'https://mbasic.facebook.com/'.$id.'/about');
try {
$link = $crawler->filter('a[id="pages_follow_action_id"]')->link();
$this->browser->click($link);
} catch (\InvalidArgumentException) {
return false;
}
return true;
}
public function followUser(string $id): bool
{
$crawler = $this->browser->request('GET', 'https://mbasic.facebook.com/'.$id.'/about');
try {
$link = $crawler->filterXPath('//a[contains(@href, "/a/subscribe.php")]')->link();
$this->browser->click($link);
} catch (\InvalidArgumentException) {
return false;
}
return true;
}
public function commentPost(string $id, string $message): bool
{
$crawler = $this->browser->request('GET', 'https://mbasic.facebook.com/mbasic/comment/advanced/?target_id='.$id.'&at=compose');
try {
$form = $crawler->filter('form')->form();
$form->remove('photo'); // watch out for this damn thing
$this->browser->submit($form, ['comment_text' => $message]);
} catch (\InvalidArgumentException) {
return false;
}
return true;
}
public function sharePost(string $id): bool
{
// Nah, I hope there will be a quicker solution
$crawler = $this->browser->request('GET', 'https://mbasic.facebook.com/'.$id);
try {
$link = $crawler->filterXPath('//a[contains(@href, "/composer/mbasic")]')->link();
$this->browser->click($link);
$this->browser->submitForm('view_post');
} catch (\InvalidArgumentException) {
return false;
}
return true;
}
}