This repository has been archived by the owner on Mar 7, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
misc.php
80 lines (67 loc) · 2.01 KB
/
misc.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
<?php
/**
* Miscellaneous functions
*/
/**
* Simple CURL wrapper
*
* @param string $url The URL to request
* @param array $post_fields POST fields to send
* @param boolean $json_payload Whether the payload is JSON formatted or not
* @return object The HTTP status code (status) and response (response)
*/
function http_req($url, $post_fields = null, $json_payload = false)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, 'github-phpdoc');
if (!empty($post_fields))
{
curl_setopt($curl, CURLOPT_POST, true);
$post_data = $json_payload ? json_encode($post_fields) : http_build_query($post_fields);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
}
$response = curl_exec($curl);
return (object) array(
'response' => $response,
'status' => curl_getinfo($curl, CURLINFO_HTTP_CODE)
);
curl_close($curl);
}
/**
* Simple wrapper for RESTful GitHub API v3
*
* @param string $method Method to request (REST resource, without leading /)
* @param array $post_data POST data to be sent in JSON
* @return array Array-decoded JSON response
*/
function github_api($method, $post_data = array())
{
if (!isset($_SESSION['github']['access_token']))
{
throw new Exception("No GitHub access token is present in session data");
}
$url = "https://api.github.com/$method?access_token=" . $_SESSION['github']['access_token'];
$http = http_req($url, $post_data, true);
if ($http->status < 200 || $http->status > 200)
{
return false;
}
return json_decode($http->response, true);
}
/**
* Simple wrapper for exec()
*
* @param string $command The command to be executed
* @return object An StdClass object command's return_code and output (as array)
*/
function command($command) {
$return_code = null;
$output = array();
exec($command, $output, $return_code);
return (object) array(
'return_code' => $return_code,
'output' => $output
);
}