|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Http\Controllers\Github; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Http\Request; |
|
6
|
|
|
use App\Http\Requests; |
|
7
|
|
|
use App\Http\Controllers\Controller; |
|
8
|
|
|
use App\Http\Controllers\Github\GithubApiController; |
|
9
|
|
|
use Exception; |
|
10
|
|
|
use App\Model\Github\Github; |
|
11
|
|
|
|
|
12
|
|
|
class GithubController extends Controller { |
|
13
|
|
|
|
|
14
|
|
|
public $github_api; |
|
15
|
|
|
public $client_id; |
|
16
|
|
|
public $client_secret; |
|
17
|
|
|
public $github; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct() { |
|
20
|
|
|
|
|
21
|
|
|
$this->middleware('auth'); |
|
22
|
|
|
|
|
23
|
|
|
$github_controller = new GithubApiController(); |
|
24
|
|
|
$this->github_api = $github_controller; |
|
25
|
|
|
|
|
26
|
|
|
$model = new Github(); |
|
27
|
|
|
$this->github = $model->firstOrFail(); |
|
28
|
|
|
|
|
29
|
|
|
$this->client_id = $this->github->client_id; |
|
30
|
|
|
$this->client_secret = $this->github->client_secret; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Authenticate a user entirly |
|
35
|
|
|
* @return type |
|
36
|
|
|
*/ |
|
37
|
|
|
public function authenticate() { |
|
38
|
|
|
try { |
|
39
|
|
|
$url = "https://api.github.com/user"; |
|
40
|
|
|
$data = array("bio" => "This is my bio"); |
|
41
|
|
|
$data_string = json_encode($data); |
|
42
|
|
|
$auth = $this->github_api->postCurl($url, $data_string); |
|
43
|
|
|
return $auth; |
|
44
|
|
|
// if($auth!='true'){ |
|
|
|
|
|
|
45
|
|
|
// throw new Exception('can not authenticate with github', 401); |
|
46
|
|
|
// } |
|
47
|
|
|
//$authenticated = json_decode($auth); |
|
|
|
|
|
|
48
|
|
|
//dd($authenticated); |
|
49
|
|
|
} catch (Exception $ex) { |
|
50
|
|
|
return redirect('/')->with('fails', $ex->getMessage()); |
|
|
|
|
|
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Authenticate a user for a perticular application |
|
56
|
|
|
* @return type |
|
57
|
|
|
*/ |
|
58
|
|
|
public function authForSpecificApp() { |
|
59
|
|
|
try { |
|
60
|
|
|
$url = "https://api.github.com/authorizations/clients/$this->client_id"; |
|
61
|
|
|
$data = array("client_secret" => "$this->client_secret"); |
|
62
|
|
|
$data_string = json_encode($data); |
|
63
|
|
|
$method = "PUT"; |
|
64
|
|
|
$auth = $this->github_api->postCurl($url, $data_string, $method); |
|
65
|
|
|
return $auth; |
|
66
|
|
|
//dd($auth); |
|
67
|
|
|
} catch (Exception $ex) { |
|
68
|
|
|
return redirect('/')->with('fails', $ex->getMessage()); |
|
|
|
|
|
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* List all release |
|
74
|
|
|
* @return type |
|
75
|
|
|
*/ |
|
76
|
|
View Code Duplication |
public function listRepositories($owner,$repo) { |
|
77
|
|
|
try { |
|
78
|
|
|
$url = "https://api.github.com/repos/$owner/$repo/releases"; |
|
79
|
|
|
$releases = $this->github_api->getCurl($url); |
|
80
|
|
|
//dd($releases); |
|
81
|
|
|
return $releases; |
|
82
|
|
|
} catch (Exception $ex) { |
|
83
|
|
|
return redirect('/')->with('fails', $ex->getMessage()); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* List only one release by tag |
|
89
|
|
|
* @param Request $request |
|
|
|
|
|
|
90
|
|
|
* @return type |
|
91
|
|
|
*/ |
|
92
|
|
|
public function getReleaseByTag($owner,$repo) { |
|
93
|
|
|
try { |
|
94
|
|
|
$tag = \Input::get('tag'); |
|
95
|
|
|
$all_releases = $this->listRepositories($owner,$repo); |
|
96
|
|
|
//dd($all_releases); |
|
97
|
|
|
if ($tag) { |
|
98
|
|
|
foreach ($all_releases as $key => $release) { |
|
99
|
|
|
//dd($release); |
|
100
|
|
|
if (in_array($tag, $release)) { |
|
101
|
|
|
|
|
102
|
|
|
$version[$tag] = $this->getReleaseById($release["id"]); |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} else { |
|
106
|
|
|
$version[0] = $all_releases[0]; |
|
|
|
|
|
|
107
|
|
|
} |
|
108
|
|
|
//dd($version); |
|
109
|
|
|
//execute download |
|
110
|
|
|
|
|
111
|
|
|
if($this->download($version)=="success"){ |
|
|
|
|
|
|
112
|
|
|
return "success"; |
|
113
|
|
|
} |
|
114
|
|
|
//return redirect()->back()->with('success', \Lang::get('message.downloaded-successfully')); |
|
|
|
|
|
|
115
|
|
|
} catch (Exception $ex) { |
|
116
|
|
|
return redirect('/')->with('fails', $ex->getMessage()); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* List only one release by id |
|
122
|
|
|
* @param type $id |
|
123
|
|
|
* @return type |
|
124
|
|
|
*/ |
|
125
|
|
View Code Duplication |
public function getReleaseById($id) { |
|
126
|
|
|
try { |
|
127
|
|
|
$url = "https://api.github.com/repos/ladybirdweb/faveo-helpdesk/releases/$id"; |
|
128
|
|
|
$releaseid = $this->github_api->getCurl($url); |
|
129
|
|
|
return $releaseid; |
|
130
|
|
|
} catch (Exception $ex) { |
|
131
|
|
|
return redirect('/')->with('fails', $ex->getMessage()); |
|
|
|
|
|
|
132
|
|
|
} |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Get the count of download of the release |
|
137
|
|
|
* @return array||redirect |
|
|
|
|
|
|
138
|
|
|
*/ |
|
139
|
|
View Code Duplication |
public function getDownloadCount() { |
|
140
|
|
|
try { |
|
141
|
|
|
$url = "https://api.github.com/repos/ladybirdweb/faveo-helpdesk/downloads"; |
|
142
|
|
|
$downloads = $this->github_api->getCurl($url); |
|
143
|
|
|
return $downloads; |
|
144
|
|
|
} catch (Exception $ex) { |
|
145
|
|
|
return redirect('/')->with('fails', $ex->getMessage()); |
|
146
|
|
|
} |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* |
|
151
|
|
|
* @param type $release |
|
152
|
|
|
* @return type .zip file |
|
153
|
|
|
*/ |
|
154
|
|
|
public function download($release) { |
|
155
|
|
|
try { |
|
156
|
|
|
foreach ($release as $url) { |
|
157
|
|
|
$download_link = $url["zipball_url"]; |
|
158
|
|
|
} |
|
159
|
|
|
echo "<form action=$download_link method=get name=download>"; |
|
|
|
|
|
|
160
|
|
|
echo '</form>'; |
|
161
|
|
|
echo"<script language='javascript'>document.download.submit();</script>"; |
|
162
|
|
|
|
|
163
|
|
|
//return "success"; |
|
164
|
|
|
} catch (Exception $ex) { |
|
165
|
|
|
return redirect('/')->with('fails', $ex->getMessage()); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* get the settings page for github |
|
171
|
|
|
* @return view |
|
172
|
|
|
*/ |
|
173
|
|
|
public function getSettings() { |
|
174
|
|
|
try { |
|
175
|
|
|
$model = $this->github; |
|
176
|
|
|
return view('themes.default1.github.settings', compact('model')); |
|
177
|
|
|
} catch (Exception $ex) { |
|
178
|
|
|
return redirect('/')->with('fails', $ex->getMessage()); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
public function postSettings(Request $request){ |
|
183
|
|
|
$this->validate($request, [ |
|
184
|
|
|
"username"=>"required", |
|
185
|
|
|
"password"=>"required" |
|
186
|
|
|
]); |
|
187
|
|
|
try{ |
|
188
|
|
|
$this->github->fill($request->input())->save(); |
|
189
|
|
|
return redirect()->back()->with('success',\Lang::get('message.updated-successfully')); |
|
190
|
|
|
} catch (Exception $ex) { |
|
191
|
|
|
return redirect()->back()->with('fails', $ex->getMessage()); |
|
192
|
|
|
} |
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
} |
|
196
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.