Skip to content

Commit 38e0702

Browse files
committed
Merge pull request #4 from qiniu/develop
first commit
2 parents f013533 + e4e6f34 commit 38e0702

File tree

10 files changed

+472
-1
lines changed

10 files changed

+472
-1
lines changed

.travis.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
language: php
2+
php:
3+
- 5.2
4+
- 5.3
5+
- 5.4
6+
before_script:
7+
- export QINIU_ACCESS_KEY="iN7NgwM31j4-BZacMjPrOQBs34UG1maYCAQmhdCV"
8+
- export QINIU_SECRET_KEY="6QTOr2Jg1gcZEWDQXKOGZh5PziC2MCV5KsntT70j"
9+
script:
10+
- cd tests; phpunit .
11+

Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
all:
2+
@echo
3+
4+
install:
5+
@echo
6+
7+
test:
8+
cd tests; phpunit .
9+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Qiniu Resource (Cloud) Storage SDK for PHP
1+
# Qiniu Resource Storage SDK for PHP
22

33
[![Build Status](https://api.travis-ci.org/qiniu/php-sdk.png?branch=master)](https://travis-ci.org/qiniu/php-sdk)
44

qiniu/auth_digest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
require_once("utils.php");
4+
require_once("conf.php");
5+
6+
// ----------------------------------------------------------
7+
8+
class Qiniu_Mac {
9+
10+
public $AccessKey;
11+
public $SecretKey;
12+
13+
public function __construct($accessKey, $secretKey)
14+
{
15+
$this->AccessKey = $accessKey;
16+
$this->SecretKey = $secretKey;
17+
}
18+
19+
public function Sign($data) // => $token
20+
{
21+
$sign = hash_hmac('sha1', $data, $this->SecretKey, true);
22+
return $this->AccessKey . ':' . Qiniu_Encode($sign);
23+
}
24+
25+
public function SignWithData($data) // => $token
26+
{
27+
$data = Qiniu_Encode($data);
28+
return $this->Sign($data) . ':' . $data;
29+
}
30+
31+
public function SignRequest($req, $incbody) // => ($token, $error)
32+
{
33+
$url = $req->URL;
34+
$data = $url['path'];
35+
if (isset($url['query'])) {
36+
$data .= '?' . $url['query'];
37+
}
38+
$data .= "\n";
39+
40+
if ($incbody) {
41+
$data .= $req->Body;
42+
}
43+
44+
return $this->Sign($data);
45+
}
46+
}
47+
48+
function Qiniu_SetKeys($accessKey, $secretKey)
49+
{
50+
global $QINIU_ACCESS_KEY;
51+
global $QINIU_SECRET_KEY;
52+
53+
$QINIU_ACCESS_KEY = $accessKey;
54+
$QINIU_SECRET_KEY = $secretKey;
55+
}
56+
57+
function Qiniu_RequireMac($mac) // => $mac
58+
{
59+
if (isset($mac)) {
60+
return $mac;
61+
}
62+
63+
global $QINIU_ACCESS_KEY;
64+
global $QINIU_SECRET_KEY;
65+
66+
return new Qiniu_Mac($QINIU_ACCESS_KEY, $QINIU_SECRET_KEY);
67+
}
68+
69+
function Qiniu_Sign($mac, $data) // => $token
70+
{
71+
return Qiniu_RequireMac($mac)->Sign($data);
72+
}
73+
74+
function Qiniu_SignWithData($mac, $data) // => $token
75+
{
76+
return Qiniu_RequireMac($mac)->SignWithData($data);
77+
}
78+
79+
// ----------------------------------------------------------
80+

qiniu/conf.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
$QINIU_UP_HOST = 'http://up.qbox.me';
4+
$QINIU_RS_HOST = 'http://rs.qbox.me';
5+
$QINIU_RSP_HOST = 'http://rsp.qbox.me';
6+
7+
$QINIU_ACCESS_KEY = '<Please apply your access key>';
8+
$QINIU_SECRET_KEY = '<Dont send your secret key to anyone>';
9+

qiniu/http.php

Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
<?php
2+
3+
require_once("auth_digest.php");
4+
5+
// --------------------------------------------------------------------------------
6+
// class Qiniu_Error
7+
8+
class Qiniu_Error
9+
{
10+
public $Err; // string
11+
public $Reqid; // string
12+
public $Details; // []string
13+
public $Code; // int
14+
15+
public function __construct($code, $err)
16+
{
17+
$this->Code = $code;
18+
$this->Err = $err;
19+
}
20+
}
21+
22+
// --------------------------------------------------------------------------------
23+
// class Qiniu_Request
24+
25+
class Qiniu_Request
26+
{
27+
public $URL;
28+
public $Header;
29+
public $Body;
30+
31+
public function __construct($url, $body)
32+
{
33+
$this->URL = $url;
34+
$this->Header = array();
35+
$this->Body = $body;
36+
}
37+
}
38+
39+
// --------------------------------------------------------------------------------
40+
// class Qiniu_Response
41+
42+
class Qiniu_Response
43+
{
44+
public $StatusCode;
45+
public $Header;
46+
public $ContentLength;
47+
public $Body;
48+
49+
public function __construct($code, $body)
50+
{
51+
$this->StatusCode = code;
52+
$this->Header = array();
53+
$this->Body = $body;
54+
$this->ContentLength = strlen($body);
55+
}
56+
}
57+
58+
// --------------------------------------------------------------------------------
59+
// class Qiniu_Header
60+
61+
function Qiniu_Header_Get($header, $key) // => $val
62+
{
63+
$val = @$header[$key];
64+
if (isset($val)) {
65+
if (isarray($val)) {
66+
return $val[0];
67+
}
68+
return $val;
69+
} else {
70+
return '';
71+
}
72+
}
73+
74+
function Qiniu_ResponseError($resp) // => $error
75+
{
76+
$header = $resp->Header;
77+
$details = Qiniu_Header_Get($header, 'X-Log');
78+
$reqId = Qiniu_Header_Get($header, 'X-Reqid');
79+
$err = new Qiniu_Error($resp->StatusCode, null);
80+
81+
if ($err->Code > 299) {
82+
if ($resp->ContentLength !== 0) {
83+
if (Qiniu_Header_Get($header, 'Content-Type') === 'application/json') {
84+
$ret = json_decode($resp->Body, true);
85+
$err->Err = $ret['error'];
86+
}
87+
}
88+
}
89+
return $err;
90+
}
91+
92+
// --------------------------------------------------------------------------------
93+
// class Qiniu_Client
94+
95+
function Qiniu_Client_incBody($req) // => $incbody
96+
{
97+
$body = $req->Body;
98+
if (!isset($body)) {
99+
return false;
100+
}
101+
102+
$ct = Qiniu_Header_Get($req->Header, 'Content-Type');
103+
if ($ct === 'application/x-www-form-urlencoded') {
104+
return true;
105+
}
106+
return false;
107+
}
108+
109+
function Qiniu_Client_do($req) // => ($resp, $error)
110+
{
111+
$ch = curl_init();
112+
$url = $req->URL;
113+
$options = array(
114+
CURLOPT_RETURNTRANSFER => true,
115+
CURLOPT_SSL_VERIFYPEER => false,
116+
CURLOPT_SSL_VERIFYHOST => false,
117+
CURLOPT_CUSTOMREQUEST => 'POST',
118+
CURLOPT_URL => $url['path']
119+
);
120+
$body = $req->Body;
121+
if (!empty($body)) {
122+
$options[CURLOPT_POSTFIELDS] = $body;
123+
$options[CURLOPT_POSTFIELDSIZE] = strlen($body);
124+
}
125+
curl_setopt_array($ch, $options);
126+
$result = curl_exec($ch);
127+
$ret = curl_errno($ch);
128+
if ($ret !== 0) {
129+
$err = new Qiniu_Error(0, curl_error($ch));
130+
curl_close($ch);
131+
return array(null, $err);
132+
}
133+
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
134+
$contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
135+
curl_close($ch);
136+
$resp = new Qiniu_Response($code, $result);
137+
$resp->Header['Content-Type'] = $contentType;
138+
return array($resp, null);
139+
}
140+
141+
class Qiniu_Client
142+
{
143+
private $mac;
144+
145+
public function __construct($mac)
146+
{
147+
$this->mac = Qiniu_RequireMac($mac);
148+
}
149+
150+
public function Exec($req) // => ($resp, $error)
151+
{
152+
$incbody = Qiniu_Client_incBody($req);
153+
$token = $this->mac->SignRequest($req, $incbody);
154+
$req->Header['Authorization'] = "QBox $token";
155+
return Qiniu_Client_do($req);
156+
}
157+
}
158+
159+
// --------------------------------------------------------------------------------
160+
161+
function Qiniu_Client_ret($resp) // => ($data, $error)
162+
{
163+
$code = $resp->StatusCode;
164+
if ($code >= 200 && $code <= 299) {
165+
if ($resp->ContentLength !== 0) {
166+
$data = json_decode($resp->Body, true);
167+
if ($data === null) {
168+
$err = new Qiniu_Error(0, json_last_error_msg());
169+
return array(null, $err);
170+
}
171+
}
172+
if ($code === 200) {
173+
return array($data, null);
174+
}
175+
}
176+
return array($data, Qiniu_ResponseError($resp));
177+
}
178+
179+
function Qiniu_Client_Call($self, $url) // => ($data, $error)
180+
{
181+
$u = array('path' => $url);
182+
$req = new Qiniu_Request($u, null);
183+
list($resp, $err) = $self->Exec($req);
184+
if ($err !== null) {
185+
return array(null, $err);
186+
}
187+
return Qiniu_Client_ret($resp);
188+
}
189+
190+
function Qiniu_Client_CallNoRet($self, $url) // => $error
191+
{
192+
$u = array('path' => $url);
193+
$req = new Qiniu_Request($u, null);
194+
list($resp, $err) = $self->Exec($req);
195+
if ($err !== null) {
196+
return array(null, $err);
197+
}
198+
if ($resp->StatusCode === 200) {
199+
return null;
200+
}
201+
return Qiniu_ResponseError($resp);
202+
}
203+
204+
// --------------------------------------------------------------------------------
205+

0 commit comments

Comments
 (0)