· 9 years ago · Oct 01, 2016, 12:40 PM
1<?php
2
3namespace App\Library\Adfly;
4
5use Exception;
6
7class AdflyController {
8 private $protocol;
9 private $host;
10 private $user_id;
11 private $hmac_algo;
12 private $secret_key;
13 private $public_key;
14 private $connection;
15
16 public function __construct() {
17 $this->protocol = \Config::get('adfly.protocol');
18 $this->host = \Config::get('adfly.host');
19 $this->user_id = \Config::get('adfly.user_id');
20 $this->hmac_algo = \Config::get('adfly.hmac_algo');
21 $this->secret_key = \Config::get('adfly.secret_key');
22 $this->public_key = \Config::get('adfly.public_key');
23 $this->connection = new \GuzzleHttp\Client(['base_uri' => $this->protocol . "://" . $this->host]);
24 }
25
26 public function get($command, $params = []) {
27 $res = $this->connection->request('GET', "/v1/" . $command, ['query' => $this->getParams($params)]);
28 return json_decode($res->getBody()->getContents());
29 }
30
31 public function post($command, $params = []) {
32 $res = $this->connection->request('POST', "/v1/" . $command, ['query' => $this->getParams($params)]);
33 return json_decode($res->getBody()->getContents());
34 }
35
36 public function put($command, $params = []) {
37 $res = $this->connection->request('PUT', "/v1/" . $command, ['query' => $this->getParams($params)]);
38 return json_decode($res->getBody()->getContents());
39 }
40
41 public function delete($command, $params = []) {
42 $res = $this->connection->request('DELETE', "/v1/" . $command, ['query' => $this->getParams($params)]);
43 return json_decode($res->getBody()->getContents());
44 }
45
46 public function auth($username, $password) {
47 return $this->post('auth', ['username' => $username, 'password' => $password]);
48 }
49
50 public function getGroups($page=1) {
51 return $this->get('urlGroups', ['_page' => $page]);
52 }
53
54 public function createGroup($name) {
55 return $this->post('urlGroups', ['name' => $name]);
56 }
57
58 public function updateAccountDetails(array $params=[]) {
59 return $this->put('account', $params);
60 }
61
62 public function expand(array $urls, array $hashes=array()) {
63 $params = array();
64 $i = 0;
65 foreach ($urls as $url) $params[sprintf('url[%d]', $i++)] = $url;
66 $i = 0;
67 foreach ($hashes as $hash) $params[sprintf('hash[%d]', $i++)] = $hash;
68 return $this->get('expand', $params);
69 }
70
71 public function shorten(array $urls, $domain=false, $advertType=false, $groupId=false) {
72 $params = array();
73 $i = 0;
74 if ($domain !== false) $params['domain'] = $domain;
75 if ($advertType !== false) $params['advert_type'] = $advertType;
76 if ($groupId !== false) $params['group_id'] = $groupId;
77 foreach ($urls as $url) $params[sprintf('url[%d]', $i++)] = $url;
78 return $this->post('shorten', $params);
79 }
80
81 public function getUrls($page=1, $q=null) {
82 $params = array('_page' => $page);
83 if ($q) $params['q'] = $q;
84 return $this->get('urls', $params);
85 }
86
87 public function getReferrers($urlId=null) {
88 return $this->get('referrers', $urlId ? ['url_id' => $urlId] : []);
89 }
90
91 public function getDomains() {
92 return $this->get('domains', []);
93 }
94
95 public function getAccountPubReferrals($fromDate='', $toDate='', $page=1, $includeBanned=0) {
96 return $this->get('accountPubReferrals', [
97 'fromDate' => $fromDate,
98 'toDate' => $toDate,
99 '_page' => $page,
100 'includeBanned' => $includeBanned
101 ]);
102 }
103
104 public function getAccountAdvReferrals($fromDate='', $toDate='', $page=1, $includeBanned=0) {
105 return $this->get('accountAdvReferrals', [
106 'fromDate' => $fromDate,
107 'toDate' => $toDate,
108 '_page' => $page,
109 'includeBanned' => $includeBanned
110 ]);
111 }
112
113 public function getAccountPopReferrals($fromDate='', $toDate='', $page=1, $includeBanned=0) {
114 return $this->get('accountPopReferrals', [
115 'fromDate' => $fromDate,
116 'toDate' => $toDate,
117 '_page' => $page,
118 'includeBanned' => $includeBanned
119 ]);
120 }
121
122 public function getCountries($urlId=null) {
123 return $this->get('countries', $urlId ? ['url_id' => $urlId] : []);
124 }
125
126 public function getAnnouncements($type=null) {
127 $params = array();
128 if (!empty($type) && in_array($type,array(1,2))) $params['type'] = $type;
129 return $this->get('announcements', $params);
130 }
131
132 public function getPublisherReferrals() {
133 return $this->get('publisherReferralStats');
134 }
135
136 public function getAdvertiserReferrals() {
137 return $this->get('advertiserReferralStats');
138 }
139
140 public function getWithdraw() {
141 return $this->get('withdraw');
142 }
143
144 public function getWithdrawalTransactions() {
145 return $this->get('withdrawalTransactions');
146 }
147
148 public function getPublisherStats($date = null, $urlId = 0){
149 $params = array();
150 if(!empty($date)) $params['date'] = $date;
151 if(!empty($urlId)) $params['urlId'] = $urlId;
152 return $this->get('publisherStats', $params);
153 }
154
155 public function getProfile() {
156 return $this->get('profile')->data;
157 }
158
159 public function getAdvertiserCampaigns( $fromDate = null, $toDate = null,$adType = null, $adFilter = null){
160 if(!empty($fromDate)) $params['fromDate'] = $fromDate;
161 if(!empty($toDate)) $params['toDate'] = $toDate;
162 if(!empty($adType)) $params['adType'] = $adType;
163 if(!empty($adFilter)) $params['adFilter'] = $adFilter;
164 return $this->get('advertiserCampaigns', $params);
165 }
166
167 public function getAdvertiserGraph($date = null, $websiteId = 0,$adType = null, $adFilter = null){
168 $params = array();
169 if(!empty($date)) $params['date'] = $date;
170 if(!empty($websiteId)) $params['websiteId'] = $websiteId;
171 if(!empty($adType)) $params['adType'] = $adType;
172 if(!empty($adFilter)) $params['adFilter'] = $adFilter;
173 return $this->get('advertiserGraph', $params);
174 }
175
176 public function getAdvertiserCampaignParts($campaignId, $fromDate = null, $toDate = null, $adType = null, $adFilter = null){
177 $params = array('campaignId' => $campaignId);
178 if(!empty($fromDate)) $params['fromDate'] = $fromDate;
179 if(!empty($toDate)) $params['toDate'] = $toDate;
180 if(!empty($adType)) $params['adType'] = $adType;
181 if(!empty($adFilter)) $params['adFilter'] = $adFilter;
182 return $this->get('advertiserCampaignParts', $params);
183 }
184
185 public function updateUrl($id, $url=false, $advertType=false, $title=false, $groupId=false, $fbDescription=false, $fbImage=false) {
186 $params = array();
187 if ($url !== false) $params['url'] = $url;
188 if ($advertType !== false) $params['advert_type'] = $advertType;
189 if ($title !== false) $params['title'] = $title;
190 if ($groupId !== false) $params['group_id'] = $groupId;
191 if ($fbDescription !== false) $params['fb_description'] = $fbDescription;
192 if ($fbImage !== false) $params['fb_image'] = $fbImage;
193 return $this->put("urls/{$id}", $params);
194 }
195
196 public function deleteUrl($id) {
197 return $this->delete("urls/{$id}");
198 }
199
200 private function getParams(array $params=array()) {
201 $params['_user_id'] = $this->user_id;
202 $params['_api_key'] = $this->public_key;
203 $params['_timestamp'] = time();
204 $params['_hash'] = $this->doHmac($params);
205 return $params;
206 }
207
208 private function doHmac(array $params) {
209 $params = array_map(function($x) { return is_null($x) ? '' : $x; }, $params);
210 if (ksort($params)) return hash_hmac($this->hmac_algo, http_build_query($params), $this->secret_key);
211 throw new RuntimeException('Could not ksort data array');
212 }
213}