-
Notifications
You must be signed in to change notification settings - Fork 8
/
openphoto
executable file
·123 lines (103 loc) · 3.04 KB
/
openphoto
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
#!/usr/bin/php
<?php
$consumerKey = getenv('consumerKey');
$consumerSecret = getenv('consumerSecret');
$token = getenv('token');
$tokenSecret = getenv('tokenSecret');
$arguments = getopt('F:X:h:e:pvs', array('encode::'));
$host = 'localhost';
$useSSL = false;
if(isset($arguments['h']))
$host = $arguments['h'];
$method = 'get';
if(isset($arguments['X']))
$method = strtolower($arguments['X']);
$endpoint = '/photos/pageSize-3/list.json';
if(isset($arguments['e']))
$endpoint = $arguments['e'];
$pretty = false;
if(isset($arguments['p']))
$pretty = true;
$verbose = false;
if(isset($arguments['v']))
$verbose = true;
if(isset($arguments['s']))
$useSSL = true;
$encode = false;
if(isset($arguments['encode']))
$encode = true;
$fields = array();
if(isset($arguments['F']))
{
foreach((array)$arguments['F'] as $field)
{
$parts = explode('=', $field);
if($encode && $parts[0] == 'photo' && strncmp($parts[1][0], '@', 1) == 0 && is_file(substr($parts[1], 1)))
$fields[$parts[0]] = base64_encode(file_get_contents(substr($parts[1], 1)));
else
$fields[$parts[0]] = $parts[1];
}
}
include 'OpenPhotoOAuth.php';
$client = new OpenPhotoOAuth($host, $consumerKey, $consumerSecret, $token, $tokenSecret);
$client->useSSL($useSSL);
if($method == 'get')
$resp = $client->get($endpoint, $fields);
elseif($method == 'post')
$resp = $client->post($endpoint, $fields);
if($verbose)
echo sprintf("==========\nMethod: %s\nHost: %s\nEndpoint: %s\nSSL: %s\n==========\n\n", $method, $host, $endpoint, $useSSL ? 'Yes' : 'No');
if($pretty)
echo indent($resp);
else
echo $resp;
if($verbose || $pretty)
echo "\n";
// from https://gist.github.com/906036
function indent($json) {
$result = '';
$pos = 0;
$strLen = strlen($json);
$indentStr = ' ';
$newLine = "\n";
$prevChar = '';
$outOfQuotes = true;
for ($i=0; $i<=$strLen; $i++) {
// Grab the next character in the string.
$char = substr($json, $i, 1);
// Put spaces in front of :
if ($outOfQuotes && $char == ':' && $prevChar != ' ') {
$result .= ' ';
}
if ($outOfQuotes && $char != ' ' && $prevChar == ':') {
$result .= ' ';
}
// Are we inside a quoted string?
if ($char == '"' && $prevChar != '\\') {
$outOfQuotes = !$outOfQuotes;
// If this character is the end of an element,
// output a new line and indent the next line.
} else if(($char == '}' || $char == ']') && $outOfQuotes) {
$result .= $newLine;
$pos --;
for ($j=0; $j<$pos; $j++) {
$result .= $indentStr;
}
}
// Add the character to the result string.
$result .= $char;
// If the last character was the beginning of an element,
// output a new line and indent the next line.
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
$result .= $newLine;
if ($char == '{' || $char == '[') {
$pos ++;
}
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
$prevChar = $char;
}
return $result;
}