Completed
Push — development ( c87275...b28f77 )
by Ashutosh
11:48
created

GithubController::getDownloadCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 9
rs 10
c 0
b 0
f 0
cc 2
nc 2
nop 0
1
<?php
2
3
namespace App\Http\Controllers\Github;
4
5
use App\Http\Controllers\Controller;
6
use App\Model\Github\Github;
7
use App\Model\Product\Subscription;
8
use Auth;
9
use Bugsnag;
10
use Exception;
11
use Illuminate\Http\Request;
12
13
class GithubController extends Controller
14
{
15
    public $github_api;
16
    public $client_id;
17
    public $client_secret;
18
    public $github;
19
20
    public function __construct()
21
    {
22
        $this->middleware('auth', ['except' => 'getlatestReleaseForUpdate']);
23
24
        $github_controller = new GithubApiController();
25
        $this->github_api = $github_controller;
26
27
        $model = new Github();
28
        $this->github = $model->firstOrFail();
29
30
        $this->client_id = $this->github->client_id;
31
        $this->client_secret = $this->github->client_secret;
32
    }
33
34
    /**
35
     * Authenticate a user entirly.
36
     *
37
     * @return type
38
     */
39
    public function authenticate()
40
    {
41
        try {
42
            $url = 'https://api.github.com/user';
43
            $data = ['bio' => 'This is my bio'];
44
            $data_string = json_encode($data);
45
            $auth = $this->github_api->postCurl($url, $data_string);
46
            dd($auth);
47
48
            return $auth;
49
        } catch (Exception $ex) {
50
            return redirect('/')->with('fails', $ex->getMessage());
51
        }
52
    }
53
54
    public function createNewAuth($note)
55
    {
56
        try {
57
            $url = 'https://api.github.com/authorizations';
58
            $data = ['note' => $note];
59
            $data_string = json_encode($data);
60
            //dd($data_string);
61
            $auth = $this->github_api->postCurl($url, $data_string);
62
63
            return $auth;
64
        } catch (Exception $ex) {
65
            return redirect('/')->with('fails', $ex->getMessage());
66
        }
67
    }
68
69
    public function getAllAuth()
70
    {
71
        try {
72
            $url = 'https://api.github.com/authorizations';
73
            $all = $this->github_api->getCurl($url);
74
75
            return $all;
76
        } catch (Exception $ex) {
77
            return redirect('/')->with('fails', $ex->getMessage());
78
        }
79
    }
80
81
    public function getAuthById($id)
82
    {
83
        try {
84
            $url = "https://api.github.com/authorizations/$id";
85
            $auth = $this->github_api->getCurl($url);
86
87
            return $auth;
88
        } catch (Exception $ex) {
89
            return redirect('/')->with('fails', $ex->getMessage());
90
        }
91
    }
92
93
    /**
94
     * Authenticate a user for a particular application.
95
     *
96
     * @return type
97
     */
98
    public function authForSpecificApp()
99
    {
100
        try {
101
            $url = "https://api.github.com/authorizations/clients/$this->client_id";
102
            $data = ['client_secret' => "$this->client_secret"];
103
            $data_string = json_encode($data);
104
            $method = 'PUT';
105
            $auth = $this->github_api->postCurl($url, $data_string, $method);
106
            //dd($auth['hashed_token']);
107
            return $auth['hashed_token'];
108
            //dd($auth);
109
        } catch (Exception $ex) {
110
            return redirect('/')->with('fails', $ex->getMessage());
111
        }
112
    }
113
114
    /**
115
     * List all release.
116
     *
117
     * @return type
118
     */
119
    public function listRepositories($owner, $repo, $order_id)
120
    {
121
        try {
122
            $releases = $this->downloadLink($owner, $repo, $order_id);
123
            if (array_key_exists('Location', $releases)) {
124
                $release = $releases['Location'];
125
            } else {
126
                $release = $this->latestRelese($owner, $repo);
127
            }
128
129
            return $release;
130
            //echo "Your download will begin in a moment. If it doesn't, <a href=$release>Click here to download</a>";
131
        } catch (Exception $ex) {
132
            dd($ex);
133
134
            return redirect('/')->with('fails', $ex->getMessage());
135
        }
136
    }
137
138
    public function listRepositoriesAdmin($owner, $repo)
139
    {
140
        try {
141
            $releases = $this->downloadLinkAdmin($owner, $repo);
142
            if (array_key_exists('Location', $releases)) {
143
                $release = $releases['Location'];
144
            } else {
145
                $release = $this->latestRelese($owner, $repo);
146
                //dd($release);
147
            }
148
            //            dd($release);
149
            return $release;
150
151
            //echo "Your download will begin in a moment. If it doesn't, <a href=$release>Click here to download</a>";
152
        } catch (Exception $ex) {
153
            dd($ex);
154
155
            return redirect('/')->with('fails', $ex->getMessage());
156
        }
157
    }
158
159
    public function latestRelese($owner, $repo)
160
    {
161
        try {
162
            $url = "https://api.github.com/repos/$owner/$repo/releases/latest";
163
            $release = $this->github_api->getCurl($url);
164
165
            return $release;
166
        } catch (Exception $ex) {
167
            //dd($ex);
168
            return redirect('/')->with('fails', $ex->getMessage());
169
        }
170
    }
171
172
    /**
173
     * List only one release by tag.
174
     *
175
     * @param Request $request
176
     *
177
     * @return type
178
     */
179
    // public function getReleaseByTag($owner, $repo)
180
    // {
181
    //     try {
182
    //         $tag = \Input::get('tag');
183
    //         $all_releases = $this->listRepositories($owner, $repo);
184
185
    //         $this->download($result['header']['Location']);
186
    //         if ($tag) {
187
    //             foreach ($all_releases as $key => $release) {
188
    //                 //dd($release);
189
    //                 if (in_array($tag, $release)) {
190
    //                     $version[$tag] = $this->getReleaseById($release['id']);
191
    //                 }
192
    //             }
193
    //         } else {
194
    //             $version[0] = $all_releases[0];
195
    //         }
196
    //         //            dd($version);
197
    //         //execute download
198
199
    //         if ($this->download($version) == 'success') {
200
    //             return 'success';
201
    //         }
202
    //         //return redirect()->back()->with('success', \Lang::get('message.downloaded-successfully'));
203
    //     } catch (Exception $ex) {
204
    //         //dd($ex);
205
    //         return redirect('/')->with('fails', $ex->getMessage());
206
    //     }
207
    // }
208
209
    /**
210
     * List only one release by id.
211
     *
212
     * @param type $id
213
     *
214
     * @return type
215
     */
216
    public function getReleaseById($id)
217
    {
218
        try {
219
            $url = "https://api.github.com/repos/ladybirdweb/faveo-helpdesk/releases/$id";
220
            $releaseid = $this->github_api->getCurl($url);
221
222
            return $releaseid;
223
        } catch (Exception $ex) {
224
            return redirect('/')->with('fails', $ex->getMessage());
225
        }
226
    }
227
228
    /**
229
     * Get the count of download of the release.
230
     *
231
     * @return array
232
     */
233
    public function getDownloadCount()
234
    {
235
        try {
236
            $url = 'https://api.github.com/repos/ladybirdweb/faveo-helpdesk/downloads';
237
            $downloads = $this->github_api->getCurl($url);
238
239
            return $downloads;
240
        } catch (Exception $ex) {
241
            return redirect('/')->with('fails', $ex->getMessage());
242
        }
243
    }
244
245
    /**
246
     * @param type $release
247
     *
248
     * @return type .zip file
249
     */
250
    public function download($release)
251
    {
252
        echo "<form action=$release method=get name=download>";
253
        echo '</form>';
254
        echo"<script language='javascript'>document.download.submit();</script>";
255
256
        //return "success";
257
    }
258
259
    /**
260
     * get the settings page for github.
261
     *
262
     * @return \view
263
     */
264
    public function getSettings()
265
    {
266
        try {
267
            $model = $this->github;
268
269
            return view('themes.default1.github.settings', compact('model'));
270
        } catch (Exception $ex) {
271
            return redirect('/')->with('fails', $ex->getMessage());
272
        }
273
    }
274
275
    public function postSettings(Request $request)
276
    {
277
        $this->validate($request, [
278
279
            'username'     => 'required',
280
            'password'     => 'required',
281
            'client_id'    => 'required',
282
            'client_secret'=> 'required',
283
284
        ]);
285
286
        try {
287
            $this->github->fill($request->input())->save();
288
289
            return redirect()->back()->with('success', \Lang::get('message.updated-successfully'));
290
        } catch (Exception $ex) {
291
            return redirect()->back()->with('fails', $ex->getMessage());
292
        }
293
    }
294
295
    /**
296
     * Github Downoload for Clients.
297
     *
298
     * @param type $owner
299
     * @param type $repo
300
     * @param type $order_id
301
     *
302
     * @return type
303
     */
304
    public function downloadLink($owner, $repo, $order_id)
305
    {
306
        try {
307
            // $url = "https://api.github.com/repos/$owner/$repo/releases";
308
            $url = "https://api.github.com/repos/$owner/$repo/zipball/master";
309
            //For helpdesk-community
310
            if ($repo == 'faveo-helpdesk') {
311
                return $array = ['Location' => $url];
312
            }
313
            //For servicedesk-community
314
            if ($repo == 'faveo-servicedesk-community') {
315
                return $array = ['Location' => $url];
316
            }
317
318
            $order_end_date = Subscription::where('order_id', '=', $order_id)->select('ends_at')->first();
319
            $url = "https://api.github.com/repos/$owner/$repo/releases";
320
321
            $link = $this->github_api->getCurl1($url);
322
            foreach ($link['body'] as $key => $value) {
323
                if (strtotime($value['created_at']) < strtotime($order_end_date->ends_at)) {
324
                    $ver[] = $value['tag_name'];
325
                }
326
            }
327
            $url = $this->getUrl($repo,$ver);
328
            $link = $this->github_api->getCurl1($url);
329
            return $link['header'];
330
        } catch (Exception $ex) {
331
             Bugsnag::notifyException($ex);
332
333
            return redirect()->back()->with('fails', $ex->getMessage());
334
        }
335
    }
336
337
    public function getUrl($repo)
338
    {
339
        //For Satellite Helpdesk
340
         if ($repo == 'faveo-satellite-helpdesk-advance') {
341
                $url = 'https://api.github.com/repos/ladybirdweb/faveo-satellite-helpdesk-advance/zipball/'.$ver[0];
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $ver seems to be never defined.
Loading history...
342
            }
343
344
            //For Helpdesk Advanced
345
            if ($repo == 'Faveo-Helpdesk-Pro') {
346
                $url = 'https://api.github.com/repos/ladybirdweb/Faveo-Helpdesk-Pro/zipball/'.$ver[0];
347
            }
348
            //For Service Desk Advance
349
            if ($repo == 'faveo-service-desk-pro') {
350
                $url = 'https://api.github.com/repos/ladybirdweb/faveo-service-desk-pro/zipball/'.$ver[0];
351
            }
352
       return $url;
353
    }
354
355
    //Github Download for Admin
356
    public function downloadLinkAdmin($owner, $repo)
357
    {
358
        try {
359
            $url = "https://api.github.com/repos/$owner/$repo/zipball/master";
360
            if ($repo == 'faveo-helpdesk') {
361
                return $array = ['Location' => $url];
362
            }
363
            $link = $this->github_api->getCurl1($url);
364
365
            return $link['header'];
366
        } catch (Exception $ex) {
367
            dd($ex);
368
369
            return redirect()->back()->with('fails', $ex->getMessage());
370
        }
371
    }
372
373
    public function findVersion($owner, $repo)
374
    {
375
        try {
376
            $release = $this->latestRelese($owner, $repo);
377
            if (array_key_exists('tag_name', $release)) {
378
                return $release['tag_name'];
379
            }
380
        } catch (Exception $ex) {
381
            // dd($ex);
382
383
            return redirect()->back()->with('fails', $ex->getMessage());
384
        }
385
    }
386
387
    public function getlatestReleaseForUpdate()
388
    {
389
        $name = \Input::get('name');
390
        $product = \App\Model\Product\Product::where('name', $name)->first();
391
        $owner = $product->github_owner;
392
        $repo = $product->github_repository;
393
        $release = $this->latestRelese($owner, $repo);
394
395
        return json_encode($release);
396
    }
397
}
398