@@ -27,23 +27,23 @@ discard block |
||
27 | 27 | */ |
28 | 28 | class Google_Http_REST |
29 | 29 | { |
30 | - /** |
|
31 | - * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries |
|
32 | - * when errors occur. |
|
33 | - * |
|
34 | - * @param Google_Client $client |
|
35 | - * @param Psr\Http\Message\RequestInterface $req |
|
36 | - * @return array decoded result |
|
37 | - * @throws Google_Service_Exception on server side error (ie: not authenticated, |
|
38 | - * invalid or malformed post body, invalid url) |
|
39 | - */ |
|
40 | - public static function execute( |
|
41 | - ClientInterface $client, |
|
42 | - RequestInterface $request, |
|
43 | - $expectedClass = null, |
|
44 | - $config = array(), |
|
45 | - $retryMap = null |
|
46 | - ) { |
|
30 | + /** |
|
31 | + * Executes a Psr\Http\Message\RequestInterface and (if applicable) automatically retries |
|
32 | + * when errors occur. |
|
33 | + * |
|
34 | + * @param Google_Client $client |
|
35 | + * @param Psr\Http\Message\RequestInterface $req |
|
36 | + * @return array decoded result |
|
37 | + * @throws Google_Service_Exception on server side error (ie: not authenticated, |
|
38 | + * invalid or malformed post body, invalid url) |
|
39 | + */ |
|
40 | + public static function execute( |
|
41 | + ClientInterface $client, |
|
42 | + RequestInterface $request, |
|
43 | + $expectedClass = null, |
|
44 | + $config = array(), |
|
45 | + $retryMap = null |
|
46 | + ) { |
|
47 | 47 | $runner = new Google_Task_Runner( |
48 | 48 | $config, |
49 | 49 | sprintf('%s %s', $request->getMethod(), (string) $request->getUri()), |
@@ -52,35 +52,35 @@ discard block |
||
52 | 52 | ); |
53 | 53 | |
54 | 54 | if (null !== $retryMap) { |
55 | - $runner->setRetryMap($retryMap); |
|
55 | + $runner->setRetryMap($retryMap); |
|
56 | 56 | } |
57 | 57 | |
58 | 58 | return $runner->run(); |
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * Executes a Psr\Http\Message\RequestInterface |
|
63 | - * |
|
64 | - * @param Google_Client $client |
|
65 | - * @param Psr\Http\Message\RequestInterface $request |
|
66 | - * @return array decoded result |
|
67 | - * @throws Google_Service_Exception on server side error (ie: not authenticated, |
|
68 | - * invalid or malformed post body, invalid url) |
|
69 | - */ |
|
70 | - public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) |
|
71 | - { |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * Executes a Psr\Http\Message\RequestInterface |
|
63 | + * |
|
64 | + * @param Google_Client $client |
|
65 | + * @param Psr\Http\Message\RequestInterface $request |
|
66 | + * @return array decoded result |
|
67 | + * @throws Google_Service_Exception on server side error (ie: not authenticated, |
|
68 | + * invalid or malformed post body, invalid url) |
|
69 | + */ |
|
70 | + public static function doExecute(ClientInterface $client, RequestInterface $request, $expectedClass = null) |
|
71 | + { |
|
72 | 72 | try { |
73 | - $httpHandler = HttpHandlerFactory::build($client); |
|
74 | - $response = $httpHandler($request); |
|
73 | + $httpHandler = HttpHandlerFactory::build($client); |
|
74 | + $response = $httpHandler($request); |
|
75 | 75 | } catch (RequestException $e) { |
76 | - // if Guzzle throws an exception, catch it and handle the response |
|
77 | - if (!$e->hasResponse()) { |
|
76 | + // if Guzzle throws an exception, catch it and handle the response |
|
77 | + if (!$e->hasResponse()) { |
|
78 | 78 | throw $e; |
79 | - } |
|
79 | + } |
|
80 | 80 | |
81 | - $response = $e->getResponse(); |
|
82 | - // specific checking for Guzzle 5: convert to PSR7 response |
|
83 | - if ($response instanceof \GuzzleHttp\Message\ResponseInterface) { |
|
81 | + $response = $e->getResponse(); |
|
82 | + // specific checking for Guzzle 5: convert to PSR7 response |
|
83 | + if ($response instanceof \GuzzleHttp\Message\ResponseInterface) { |
|
84 | 84 | $response = new Response( |
85 | 85 | $response->getStatusCode(), |
86 | 86 | $response->getHeaders() ?: [], |
@@ -88,34 +88,34 @@ discard block |
||
88 | 88 | $response->getProtocolVersion(), |
89 | 89 | $response->getReasonPhrase() |
90 | 90 | ); |
91 | - } |
|
91 | + } |
|
92 | 92 | } |
93 | 93 | |
94 | 94 | return self::decodeHttpResponse($response, $request, $expectedClass); |
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Decode an HTTP Response. |
|
99 | - * @static |
|
100 | - * @throws Google_Service_Exception |
|
101 | - * @param Psr\Http\Message\RequestInterface $response The http response to be decoded. |
|
102 | - * @param Psr\Http\Message\ResponseInterface $response |
|
103 | - * @return mixed|null |
|
104 | - */ |
|
105 | - public static function decodeHttpResponse( |
|
106 | - ResponseInterface $response, |
|
107 | - RequestInterface $request = null, |
|
108 | - $expectedClass = null |
|
109 | - ) { |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Decode an HTTP Response. |
|
99 | + * @static |
|
100 | + * @throws Google_Service_Exception |
|
101 | + * @param Psr\Http\Message\RequestInterface $response The http response to be decoded. |
|
102 | + * @param Psr\Http\Message\ResponseInterface $response |
|
103 | + * @return mixed|null |
|
104 | + */ |
|
105 | + public static function decodeHttpResponse( |
|
106 | + ResponseInterface $response, |
|
107 | + RequestInterface $request = null, |
|
108 | + $expectedClass = null |
|
109 | + ) { |
|
110 | 110 | $code = $response->getStatusCode(); |
111 | 111 | |
112 | 112 | // retry strategy |
113 | 113 | if (intVal($code) >= 400) { |
114 | - // if we errored out, it should be safe to grab the response body |
|
115 | - $body = (string) $response->getBody(); |
|
114 | + // if we errored out, it should be safe to grab the response body |
|
115 | + $body = (string) $response->getBody(); |
|
116 | 116 | |
117 | - // Check if we received errors, and add those to the Exception for convenience |
|
118 | - throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body)); |
|
117 | + // Check if we received errors, and add those to the Exception for convenience |
|
118 | + throw new Google_Service_Exception($body, $code, null, self::getResponseErrors($body)); |
|
119 | 119 | } |
120 | 120 | |
121 | 121 | // Ensure we only pull the entire body into memory if the request is not |
@@ -123,60 +123,60 @@ discard block |
||
123 | 123 | $body = self::decodeBody($response, $request); |
124 | 124 | |
125 | 125 | if ($expectedClass = self::determineExpectedClass($expectedClass, $request)) { |
126 | - $json = json_decode($body, true); |
|
126 | + $json = json_decode($body, true); |
|
127 | 127 | |
128 | - return new $expectedClass($json); |
|
128 | + return new $expectedClass($json); |
|
129 | 129 | } |
130 | 130 | |
131 | 131 | return $response; |
132 | - } |
|
132 | + } |
|
133 | 133 | |
134 | - private static function decodeBody(ResponseInterface $response, RequestInterface $request = null) |
|
135 | - { |
|
134 | + private static function decodeBody(ResponseInterface $response, RequestInterface $request = null) |
|
135 | + { |
|
136 | 136 | if (self::isAltMedia($request)) { |
137 | - // don't decode the body, it's probably a really long string |
|
138 | - return ''; |
|
137 | + // don't decode the body, it's probably a really long string |
|
138 | + return ''; |
|
139 | 139 | } |
140 | 140 | |
141 | 141 | return (string) $response->getBody(); |
142 | - } |
|
142 | + } |
|
143 | 143 | |
144 | - private static function determineExpectedClass($expectedClass, RequestInterface $request = null) |
|
145 | - { |
|
144 | + private static function determineExpectedClass($expectedClass, RequestInterface $request = null) |
|
145 | + { |
|
146 | 146 | // "false" is used to explicitly prevent an expected class from being returned |
147 | 147 | if (false === $expectedClass) { |
148 | - return null; |
|
148 | + return null; |
|
149 | 149 | } |
150 | 150 | |
151 | 151 | // if we don't have a request, we just use what's passed in |
152 | 152 | if (null === $request) { |
153 | - return $expectedClass; |
|
153 | + return $expectedClass; |
|
154 | 154 | } |
155 | 155 | |
156 | 156 | // return what we have in the request header if one was not supplied |
157 | 157 | return $expectedClass ?: $request->getHeaderLine('X-Php-Expected-Class'); |
158 | - } |
|
158 | + } |
|
159 | 159 | |
160 | - private static function getResponseErrors($body) |
|
161 | - { |
|
160 | + private static function getResponseErrors($body) |
|
161 | + { |
|
162 | 162 | $json = json_decode($body, true); |
163 | 163 | |
164 | 164 | if (isset($json['error']['errors'])) { |
165 | - return $json['error']['errors']; |
|
165 | + return $json['error']['errors']; |
|
166 | 166 | } |
167 | 167 | |
168 | 168 | return null; |
169 | - } |
|
169 | + } |
|
170 | 170 | |
171 | - private static function isAltMedia(RequestInterface $request = null) |
|
172 | - { |
|
171 | + private static function isAltMedia(RequestInterface $request = null) |
|
172 | + { |
|
173 | 173 | if ($request && $qs = $request->getUri()->getQuery()) { |
174 | - parse_str($qs, $query); |
|
175 | - if (isset($query['alt']) && $query['alt'] == 'media') { |
|
174 | + parse_str($qs, $query); |
|
175 | + if (isset($query['alt']) && $query['alt'] == 'media') { |
|
176 | 176 | return true; |
177 | - } |
|
177 | + } |
|
178 | 178 | } |
179 | 179 | |
180 | 180 | return false; |
181 | - } |
|
181 | + } |
|
182 | 182 | } |
@@ -26,61 +26,61 @@ discard block |
||
26 | 26 | */ |
27 | 27 | class Google_Http_MediaFileUpload |
28 | 28 | { |
29 | - const UPLOAD_MEDIA_TYPE = 'media'; |
|
30 | - const UPLOAD_MULTIPART_TYPE = 'multipart'; |
|
31 | - const UPLOAD_RESUMABLE_TYPE = 'resumable'; |
|
32 | - |
|
33 | - /** @var string $mimeType */ |
|
34 | - private $mimeType; |
|
35 | - |
|
36 | - /** @var string $data */ |
|
37 | - private $data; |
|
38 | - |
|
39 | - /** @var bool $resumable */ |
|
40 | - private $resumable; |
|
41 | - |
|
42 | - /** @var int $chunkSize */ |
|
43 | - private $chunkSize; |
|
44 | - |
|
45 | - /** @var int $size */ |
|
46 | - private $size; |
|
47 | - |
|
48 | - /** @var string $resumeUri */ |
|
49 | - private $resumeUri; |
|
50 | - |
|
51 | - /** @var int $progress */ |
|
52 | - private $progress; |
|
53 | - |
|
54 | - /** @var Google_Client */ |
|
55 | - private $client; |
|
56 | - |
|
57 | - /** @var Psr\Http\Message\RequestInterface */ |
|
58 | - private $request; |
|
59 | - |
|
60 | - /** @var string */ |
|
61 | - private $boundary; |
|
62 | - |
|
63 | - /** |
|
64 | - * Result code from last HTTP call |
|
65 | - * @var int |
|
66 | - */ |
|
67 | - private $httpResultCode; |
|
68 | - |
|
69 | - /** |
|
70 | - * @param $mimeType string |
|
71 | - * @param $data string The bytes you want to upload. |
|
72 | - * @param $resumable bool |
|
73 | - * @param bool $chunkSize File will be uploaded in chunks of this many bytes. |
|
74 | - * only used if resumable=True |
|
75 | - */ |
|
76 | - public function __construct( |
|
77 | - Google_Client $client, |
|
78 | - RequestInterface $request, |
|
79 | - $mimeType, |
|
80 | - $data, |
|
81 | - $resumable = false, |
|
82 | - $chunkSize = false |
|
83 | - ) { |
|
29 | + const UPLOAD_MEDIA_TYPE = 'media'; |
|
30 | + const UPLOAD_MULTIPART_TYPE = 'multipart'; |
|
31 | + const UPLOAD_RESUMABLE_TYPE = 'resumable'; |
|
32 | + |
|
33 | + /** @var string $mimeType */ |
|
34 | + private $mimeType; |
|
35 | + |
|
36 | + /** @var string $data */ |
|
37 | + private $data; |
|
38 | + |
|
39 | + /** @var bool $resumable */ |
|
40 | + private $resumable; |
|
41 | + |
|
42 | + /** @var int $chunkSize */ |
|
43 | + private $chunkSize; |
|
44 | + |
|
45 | + /** @var int $size */ |
|
46 | + private $size; |
|
47 | + |
|
48 | + /** @var string $resumeUri */ |
|
49 | + private $resumeUri; |
|
50 | + |
|
51 | + /** @var int $progress */ |
|
52 | + private $progress; |
|
53 | + |
|
54 | + /** @var Google_Client */ |
|
55 | + private $client; |
|
56 | + |
|
57 | + /** @var Psr\Http\Message\RequestInterface */ |
|
58 | + private $request; |
|
59 | + |
|
60 | + /** @var string */ |
|
61 | + private $boundary; |
|
62 | + |
|
63 | + /** |
|
64 | + * Result code from last HTTP call |
|
65 | + * @var int |
|
66 | + */ |
|
67 | + private $httpResultCode; |
|
68 | + |
|
69 | + /** |
|
70 | + * @param $mimeType string |
|
71 | + * @param $data string The bytes you want to upload. |
|
72 | + * @param $resumable bool |
|
73 | + * @param bool $chunkSize File will be uploaded in chunks of this many bytes. |
|
74 | + * only used if resumable=True |
|
75 | + */ |
|
76 | + public function __construct( |
|
77 | + Google_Client $client, |
|
78 | + RequestInterface $request, |
|
79 | + $mimeType, |
|
80 | + $data, |
|
81 | + $resumable = false, |
|
82 | + $chunkSize = false |
|
83 | + ) { |
|
84 | 84 | $this->client = $client; |
85 | 85 | $this->request = $request; |
86 | 86 | $this->mimeType = $mimeType; |
@@ -90,44 +90,44 @@ discard block |
||
90 | 90 | $this->progress = 0; |
91 | 91 | |
92 | 92 | $this->process(); |
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Set the size of the file that is being uploaded. |
|
97 | - * @param $size - int file size in bytes |
|
98 | - */ |
|
99 | - public function setFileSize($size) |
|
100 | - { |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Set the size of the file that is being uploaded. |
|
97 | + * @param $size - int file size in bytes |
|
98 | + */ |
|
99 | + public function setFileSize($size) |
|
100 | + { |
|
101 | 101 | $this->size = $size; |
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Return the progress on the upload |
|
106 | - * @return int progress in bytes uploaded. |
|
107 | - */ |
|
108 | - public function getProgress() |
|
109 | - { |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Return the progress on the upload |
|
106 | + * @return int progress in bytes uploaded. |
|
107 | + */ |
|
108 | + public function getProgress() |
|
109 | + { |
|
110 | 110 | return $this->progress; |
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Send the next part of the file to upload. |
|
115 | - * @param [$chunk] the next set of bytes to send. If false will used $data passed |
|
116 | - * at construct time. |
|
117 | - */ |
|
118 | - public function nextChunk($chunk = false) |
|
119 | - { |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Send the next part of the file to upload. |
|
115 | + * @param [$chunk] the next set of bytes to send. If false will used $data passed |
|
116 | + * at construct time. |
|
117 | + */ |
|
118 | + public function nextChunk($chunk = false) |
|
119 | + { |
|
120 | 120 | $resumeUri = $this->getResumeUri(); |
121 | 121 | |
122 | 122 | if (false == $chunk) { |
123 | - $chunk = substr($this->data, $this->progress, $this->chunkSize); |
|
123 | + $chunk = substr($this->data, $this->progress, $this->chunkSize); |
|
124 | 124 | } |
125 | 125 | |
126 | 126 | $lastBytePos = $this->progress + strlen($chunk) - 1; |
127 | 127 | $headers = array( |
128 | - 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", |
|
129 | - 'content-length' => strlen($chunk), |
|
130 | - 'expect' => '', |
|
128 | + 'content-range' => "bytes $this->progress-$lastBytePos/$this->size", |
|
129 | + 'content-length' => strlen($chunk), |
|
130 | + 'expect' => '', |
|
131 | 131 | ); |
132 | 132 | |
133 | 133 | $request = new Request( |
@@ -138,78 +138,78 @@ discard block |
||
138 | 138 | ); |
139 | 139 | |
140 | 140 | return $this->makePutRequest($request); |
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Return the HTTP result code from the last call made. |
|
145 | - * @return int code |
|
146 | - */ |
|
147 | - public function getHttpResultCode() |
|
148 | - { |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Return the HTTP result code from the last call made. |
|
145 | + * @return int code |
|
146 | + */ |
|
147 | + public function getHttpResultCode() |
|
148 | + { |
|
149 | 149 | return $this->httpResultCode; |
150 | - } |
|
151 | - |
|
152 | - /** |
|
153 | - * Sends a PUT-Request to google drive and parses the response, |
|
154 | - * setting the appropiate variables from the response() |
|
155 | - * |
|
156 | - * @param Google_Http_Request $httpRequest the Reuqest which will be send |
|
157 | - * |
|
158 | - * @return false|mixed false when the upload is unfinished or the decoded http response |
|
159 | - * |
|
160 | - */ |
|
161 | - private function makePutRequest(RequestInterface $request) |
|
162 | - { |
|
150 | + } |
|
151 | + |
|
152 | + /** |
|
153 | + * Sends a PUT-Request to google drive and parses the response, |
|
154 | + * setting the appropiate variables from the response() |
|
155 | + * |
|
156 | + * @param Google_Http_Request $httpRequest the Reuqest which will be send |
|
157 | + * |
|
158 | + * @return false|mixed false when the upload is unfinished or the decoded http response |
|
159 | + * |
|
160 | + */ |
|
161 | + private function makePutRequest(RequestInterface $request) |
|
162 | + { |
|
163 | 163 | $response = $this->client->execute($request); |
164 | 164 | $this->httpResultCode = $response->getStatusCode(); |
165 | 165 | |
166 | 166 | if (308 == $this->httpResultCode) { |
167 | - // Track the amount uploaded. |
|
168 | - $range = $response->getHeaderLine('range'); |
|
169 | - if ($range) { |
|
167 | + // Track the amount uploaded. |
|
168 | + $range = $response->getHeaderLine('range'); |
|
169 | + if ($range) { |
|
170 | 170 | $range_array = explode('-', $range); |
171 | 171 | $this->progress = $range_array[1] + 1; |
172 | - } |
|
172 | + } |
|
173 | 173 | |
174 | - // Allow for changing upload URLs. |
|
175 | - $location = $response->getHeaderLine('location'); |
|
176 | - if ($location) { |
|
174 | + // Allow for changing upload URLs. |
|
175 | + $location = $response->getHeaderLine('location'); |
|
176 | + if ($location) { |
|
177 | 177 | $this->resumeUri = $location; |
178 | - } |
|
178 | + } |
|
179 | 179 | |
180 | - // No problems, but upload not complete. |
|
181 | - return false; |
|
180 | + // No problems, but upload not complete. |
|
181 | + return false; |
|
182 | 182 | } |
183 | 183 | |
184 | 184 | return Google_Http_REST::decodeHttpResponse($response, $this->request); |
185 | - } |
|
186 | - |
|
187 | - /** |
|
188 | - * Resume a previously unfinished upload |
|
189 | - * @param $resumeUri the resume-URI of the unfinished, resumable upload. |
|
190 | - */ |
|
191 | - public function resume($resumeUri) |
|
192 | - { |
|
193 | - $this->resumeUri = $resumeUri; |
|
194 | - $headers = array( |
|
195 | - 'content-range' => "bytes */$this->size", |
|
196 | - 'content-length' => 0, |
|
197 | - ); |
|
198 | - $httpRequest = new Request( |
|
199 | - 'PUT', |
|
200 | - $this->resumeUri, |
|
201 | - $headers |
|
202 | - ); |
|
203 | - |
|
204 | - return $this->makePutRequest($httpRequest); |
|
205 | - } |
|
206 | - |
|
207 | - /** |
|
208 | - * @return Psr\Http\Message\RequestInterface $request |
|
209 | - * @visible for testing |
|
210 | - */ |
|
211 | - private function process() |
|
212 | - { |
|
185 | + } |
|
186 | + |
|
187 | + /** |
|
188 | + * Resume a previously unfinished upload |
|
189 | + * @param $resumeUri the resume-URI of the unfinished, resumable upload. |
|
190 | + */ |
|
191 | + public function resume($resumeUri) |
|
192 | + { |
|
193 | + $this->resumeUri = $resumeUri; |
|
194 | + $headers = array( |
|
195 | + 'content-range' => "bytes */$this->size", |
|
196 | + 'content-length' => 0, |
|
197 | + ); |
|
198 | + $httpRequest = new Request( |
|
199 | + 'PUT', |
|
200 | + $this->resumeUri, |
|
201 | + $headers |
|
202 | + ); |
|
203 | + |
|
204 | + return $this->makePutRequest($httpRequest); |
|
205 | + } |
|
206 | + |
|
207 | + /** |
|
208 | + * @return Psr\Http\Message\RequestInterface $request |
|
209 | + * @visible for testing |
|
210 | + */ |
|
211 | + private function process() |
|
212 | + { |
|
213 | 213 | $this->transformToUploadUrl(); |
214 | 214 | $request = $this->request; |
215 | 215 | |
@@ -227,81 +227,81 @@ discard block |
||
227 | 227 | $mimeType = $this->mimeType ?: $request->getHeaderLine('content-type'); |
228 | 228 | |
229 | 229 | if (self::UPLOAD_RESUMABLE_TYPE == $uploadType) { |
230 | - $contentType = $mimeType; |
|
231 | - $postBody = is_string($meta) ? $meta : json_encode($meta); |
|
230 | + $contentType = $mimeType; |
|
231 | + $postBody = is_string($meta) ? $meta : json_encode($meta); |
|
232 | 232 | } else if (self::UPLOAD_MEDIA_TYPE == $uploadType) { |
233 | - $contentType = $mimeType; |
|
234 | - $postBody = $this->data; |
|
233 | + $contentType = $mimeType; |
|
234 | + $postBody = $this->data; |
|
235 | 235 | } else if (self::UPLOAD_MULTIPART_TYPE == $uploadType) { |
236 | - // This is a multipart/related upload. |
|
237 | - $boundary = $this->boundary ?: mt_rand(); |
|
238 | - $boundary = str_replace('"', '', $boundary); |
|
239 | - $contentType = 'multipart/related; boundary=' . $boundary; |
|
240 | - $related = "--$boundary\r\n"; |
|
241 | - $related .= "Content-Type: application/json; charset=UTF-8\r\n"; |
|
242 | - $related .= "\r\n" . json_encode($meta) . "\r\n"; |
|
243 | - $related .= "--$boundary\r\n"; |
|
244 | - $related .= "Content-Type: $mimeType\r\n"; |
|
245 | - $related .= "Content-Transfer-Encoding: base64\r\n"; |
|
246 | - $related .= "\r\n" . base64_encode($this->data) . "\r\n"; |
|
247 | - $related .= "--$boundary--"; |
|
248 | - $postBody = $related; |
|
236 | + // This is a multipart/related upload. |
|
237 | + $boundary = $this->boundary ?: mt_rand(); |
|
238 | + $boundary = str_replace('"', '', $boundary); |
|
239 | + $contentType = 'multipart/related; boundary=' . $boundary; |
|
240 | + $related = "--$boundary\r\n"; |
|
241 | + $related .= "Content-Type: application/json; charset=UTF-8\r\n"; |
|
242 | + $related .= "\r\n" . json_encode($meta) . "\r\n"; |
|
243 | + $related .= "--$boundary\r\n"; |
|
244 | + $related .= "Content-Type: $mimeType\r\n"; |
|
245 | + $related .= "Content-Transfer-Encoding: base64\r\n"; |
|
246 | + $related .= "\r\n" . base64_encode($this->data) . "\r\n"; |
|
247 | + $related .= "--$boundary--"; |
|
248 | + $postBody = $related; |
|
249 | 249 | } |
250 | 250 | |
251 | 251 | $request = $request->withBody(Psr7\stream_for($postBody)); |
252 | 252 | |
253 | 253 | if (isset($contentType) && $contentType) { |
254 | - $request = $request->withHeader('content-type', $contentType); |
|
254 | + $request = $request->withHeader('content-type', $contentType); |
|
255 | 255 | } |
256 | 256 | |
257 | 257 | return $this->request = $request; |
258 | - } |
|
259 | - |
|
260 | - /** |
|
261 | - * Valid upload types: |
|
262 | - * - resumable (UPLOAD_RESUMABLE_TYPE) |
|
263 | - * - media (UPLOAD_MEDIA_TYPE) |
|
264 | - * - multipart (UPLOAD_MULTIPART_TYPE) |
|
265 | - * @param $meta |
|
266 | - * @return string |
|
267 | - * @visible for testing |
|
268 | - */ |
|
269 | - public function getUploadType($meta) |
|
270 | - { |
|
258 | + } |
|
259 | + |
|
260 | + /** |
|
261 | + * Valid upload types: |
|
262 | + * - resumable (UPLOAD_RESUMABLE_TYPE) |
|
263 | + * - media (UPLOAD_MEDIA_TYPE) |
|
264 | + * - multipart (UPLOAD_MULTIPART_TYPE) |
|
265 | + * @param $meta |
|
266 | + * @return string |
|
267 | + * @visible for testing |
|
268 | + */ |
|
269 | + public function getUploadType($meta) |
|
270 | + { |
|
271 | 271 | if ($this->resumable) { |
272 | - return self::UPLOAD_RESUMABLE_TYPE; |
|
272 | + return self::UPLOAD_RESUMABLE_TYPE; |
|
273 | 273 | } |
274 | 274 | |
275 | 275 | if (false == $meta && $this->data) { |
276 | - return self::UPLOAD_MEDIA_TYPE; |
|
276 | + return self::UPLOAD_MEDIA_TYPE; |
|
277 | 277 | } |
278 | 278 | |
279 | 279 | return self::UPLOAD_MULTIPART_TYPE; |
280 | - } |
|
280 | + } |
|
281 | 281 | |
282 | - public function getResumeUri() |
|
283 | - { |
|
282 | + public function getResumeUri() |
|
283 | + { |
|
284 | 284 | if (null === $this->resumeUri) { |
285 | - $this->resumeUri = $this->fetchResumeUri(); |
|
285 | + $this->resumeUri = $this->fetchResumeUri(); |
|
286 | 286 | } |
287 | 287 | |
288 | 288 | return $this->resumeUri; |
289 | - } |
|
289 | + } |
|
290 | 290 | |
291 | - private function fetchResumeUri() |
|
292 | - { |
|
291 | + private function fetchResumeUri() |
|
292 | + { |
|
293 | 293 | $body = $this->request->getBody(); |
294 | 294 | if ($body) { |
295 | - $headers = array( |
|
295 | + $headers = array( |
|
296 | 296 | 'content-type' => 'application/json; charset=UTF-8', |
297 | 297 | 'content-length' => $body->getSize(), |
298 | 298 | 'x-upload-content-type' => $this->mimeType, |
299 | 299 | 'x-upload-content-length' => $this->size, |
300 | 300 | 'expect' => '', |
301 | - ); |
|
302 | - foreach ($headers as $key => $value) { |
|
301 | + ); |
|
302 | + foreach ($headers as $key => $value) { |
|
303 | 303 | $this->request = $this->request->withHeader($key, $value); |
304 | - } |
|
304 | + } |
|
305 | 305 | } |
306 | 306 | |
307 | 307 | $response = $this->client->execute($this->request, false); |
@@ -309,43 +309,43 @@ discard block |
||
309 | 309 | $code = $response->getStatusCode(); |
310 | 310 | |
311 | 311 | if (200 == $code && true == $location) { |
312 | - return $location; |
|
312 | + return $location; |
|
313 | 313 | } |
314 | 314 | |
315 | 315 | $message = $code; |
316 | 316 | $body = json_decode((string) $this->request->getBody(), true); |
317 | 317 | if (isset($body['error']['errors'])) { |
318 | - $message .= ': '; |
|
319 | - foreach ($body['error']['errors'] as $error) { |
|
318 | + $message .= ': '; |
|
319 | + foreach ($body['error']['errors'] as $error) { |
|
320 | 320 | $message .= "{$error['domain']}, {$error['message']};"; |
321 | - } |
|
322 | - $message = rtrim($message, ';'); |
|
321 | + } |
|
322 | + $message = rtrim($message, ';'); |
|
323 | 323 | } |
324 | 324 | |
325 | 325 | $error = "Failed to start the resumable upload (HTTP {$message})"; |
326 | 326 | $this->client->getLogger()->error($error); |
327 | 327 | |
328 | 328 | throw new Google_Exception($error); |
329 | - } |
|
329 | + } |
|
330 | 330 | |
331 | - private function transformToUploadUrl() |
|
332 | - { |
|
331 | + private function transformToUploadUrl() |
|
332 | + { |
|
333 | 333 | $parts = parse_url((string) $this->request->getUri()); |
334 | 334 | if (!isset($parts['path'])) { |
335 | - $parts['path'] = ''; |
|
335 | + $parts['path'] = ''; |
|
336 | 336 | } |
337 | 337 | $parts['path'] = '/upload' . $parts['path']; |
338 | 338 | $uri = Uri::fromParts($parts); |
339 | 339 | $this->request = $this->request->withUri($uri); |
340 | - } |
|
340 | + } |
|
341 | 341 | |
342 | - public function setChunkSize($chunkSize) |
|
343 | - { |
|
342 | + public function setChunkSize($chunkSize) |
|
343 | + { |
|
344 | 344 | $this->chunkSize = $chunkSize; |
345 | - } |
|
345 | + } |
|
346 | 346 | |
347 | - public function getRequest() |
|
348 | - { |
|
347 | + public function getRequest() |
|
348 | + { |
|
349 | 349 | return $this->request; |
350 | - } |
|
350 | + } |
|
351 | 351 | } |
@@ -22,52 +22,52 @@ discard block |
||
22 | 22 | */ |
23 | 23 | class Google_Task_Runner |
24 | 24 | { |
25 | - const TASK_RETRY_NEVER = 0; |
|
26 | - const TASK_RETRY_ONCE = 1; |
|
27 | - const TASK_RETRY_ALWAYS = -1; |
|
28 | - |
|
29 | - /** |
|
30 | - * @var integer $maxDelay The max time (in seconds) to wait before a retry. |
|
31 | - */ |
|
32 | - private $maxDelay = 60; |
|
33 | - /** |
|
34 | - * @var integer $delay The previous delay from which the next is calculated. |
|
35 | - */ |
|
36 | - private $delay = 1; |
|
37 | - |
|
38 | - /** |
|
39 | - * @var integer $factor The base number for the exponential back off. |
|
40 | - */ |
|
41 | - private $factor = 2; |
|
42 | - /** |
|
43 | - * @var float $jitter A random number between -$jitter and $jitter will be |
|
44 | - * added to $factor on each iteration to allow for a better distribution of |
|
45 | - * retries. |
|
46 | - */ |
|
47 | - private $jitter = 0.5; |
|
48 | - |
|
49 | - /** |
|
50 | - * @var integer $attempts The number of attempts that have been tried so far. |
|
51 | - */ |
|
52 | - private $attempts = 0; |
|
53 | - /** |
|
54 | - * @var integer $maxAttempts The max number of attempts allowed. |
|
55 | - */ |
|
56 | - private $maxAttempts = 1; |
|
57 | - |
|
58 | - /** |
|
59 | - * @var callable $action The task to run and possibly retry. |
|
60 | - */ |
|
61 | - private $action; |
|
62 | - /** |
|
63 | - * @var array $arguments The task arguments. |
|
64 | - */ |
|
65 | - private $arguments; |
|
66 | - |
|
67 | - /** |
|
68 | - * @var array $retryMap Map of errors with retry counts. |
|
69 | - */ |
|
70 | - protected $retryMap = [ |
|
25 | + const TASK_RETRY_NEVER = 0; |
|
26 | + const TASK_RETRY_ONCE = 1; |
|
27 | + const TASK_RETRY_ALWAYS = -1; |
|
28 | + |
|
29 | + /** |
|
30 | + * @var integer $maxDelay The max time (in seconds) to wait before a retry. |
|
31 | + */ |
|
32 | + private $maxDelay = 60; |
|
33 | + /** |
|
34 | + * @var integer $delay The previous delay from which the next is calculated. |
|
35 | + */ |
|
36 | + private $delay = 1; |
|
37 | + |
|
38 | + /** |
|
39 | + * @var integer $factor The base number for the exponential back off. |
|
40 | + */ |
|
41 | + private $factor = 2; |
|
42 | + /** |
|
43 | + * @var float $jitter A random number between -$jitter and $jitter will be |
|
44 | + * added to $factor on each iteration to allow for a better distribution of |
|
45 | + * retries. |
|
46 | + */ |
|
47 | + private $jitter = 0.5; |
|
48 | + |
|
49 | + /** |
|
50 | + * @var integer $attempts The number of attempts that have been tried so far. |
|
51 | + */ |
|
52 | + private $attempts = 0; |
|
53 | + /** |
|
54 | + * @var integer $maxAttempts The max number of attempts allowed. |
|
55 | + */ |
|
56 | + private $maxAttempts = 1; |
|
57 | + |
|
58 | + /** |
|
59 | + * @var callable $action The task to run and possibly retry. |
|
60 | + */ |
|
61 | + private $action; |
|
62 | + /** |
|
63 | + * @var array $arguments The task arguments. |
|
64 | + */ |
|
65 | + private $arguments; |
|
66 | + |
|
67 | + /** |
|
68 | + * @var array $retryMap Map of errors with retry counts. |
|
69 | + */ |
|
70 | + protected $retryMap = [ |
|
71 | 71 | '500' => self::TASK_RETRY_ALWAYS, |
72 | 72 | '503' => self::TASK_RETRY_ALWAYS, |
73 | 73 | 'rateLimitExceeded' => self::TASK_RETRY_ALWAYS, |
@@ -77,70 +77,70 @@ discard block |
||
77 | 77 | 28 => self::TASK_RETRY_ALWAYS, // CURLE_OPERATION_TIMEOUTED |
78 | 78 | 35 => self::TASK_RETRY_ALWAYS, // CURLE_SSL_CONNECT_ERROR |
79 | 79 | 52 => self::TASK_RETRY_ALWAYS // CURLE_GOT_NOTHING |
80 | - ]; |
|
81 | - |
|
82 | - /** |
|
83 | - * Creates a new task runner with exponential backoff support. |
|
84 | - * |
|
85 | - * @param array $config The task runner config |
|
86 | - * @param string $name The name of the current task (used for logging) |
|
87 | - * @param callable $action The task to run and possibly retry |
|
88 | - * @param array $arguments The task arguments |
|
89 | - * @throws Google_Task_Exception when misconfigured |
|
90 | - */ |
|
91 | - public function __construct( |
|
92 | - $config, |
|
93 | - $name, |
|
94 | - $action, |
|
95 | - array $arguments = array() |
|
96 | - ) { |
|
80 | + ]; |
|
81 | + |
|
82 | + /** |
|
83 | + * Creates a new task runner with exponential backoff support. |
|
84 | + * |
|
85 | + * @param array $config The task runner config |
|
86 | + * @param string $name The name of the current task (used for logging) |
|
87 | + * @param callable $action The task to run and possibly retry |
|
88 | + * @param array $arguments The task arguments |
|
89 | + * @throws Google_Task_Exception when misconfigured |
|
90 | + */ |
|
91 | + public function __construct( |
|
92 | + $config, |
|
93 | + $name, |
|
94 | + $action, |
|
95 | + array $arguments = array() |
|
96 | + ) { |
|
97 | 97 | if (isset($config['initial_delay'])) { |
98 | - if ($config['initial_delay'] < 0) { |
|
98 | + if ($config['initial_delay'] < 0) { |
|
99 | 99 | throw new Google_Task_Exception( |
100 | 100 | 'Task configuration `initial_delay` must not be negative.' |
101 | 101 | ); |
102 | - } |
|
102 | + } |
|
103 | 103 | |
104 | - $this->delay = $config['initial_delay']; |
|
104 | + $this->delay = $config['initial_delay']; |
|
105 | 105 | } |
106 | 106 | |
107 | 107 | if (isset($config['max_delay'])) { |
108 | - if ($config['max_delay'] <= 0) { |
|
108 | + if ($config['max_delay'] <= 0) { |
|
109 | 109 | throw new Google_Task_Exception( |
110 | 110 | 'Task configuration `max_delay` must be greater than 0.' |
111 | 111 | ); |
112 | - } |
|
112 | + } |
|
113 | 113 | |
114 | - $this->maxDelay = $config['max_delay']; |
|
114 | + $this->maxDelay = $config['max_delay']; |
|
115 | 115 | } |
116 | 116 | |
117 | 117 | if (isset($config['factor'])) { |
118 | - if ($config['factor'] <= 0) { |
|
118 | + if ($config['factor'] <= 0) { |
|
119 | 119 | throw new Google_Task_Exception( |
120 | 120 | 'Task configuration `factor` must be greater than 0.' |
121 | 121 | ); |
122 | - } |
|
122 | + } |
|
123 | 123 | |
124 | - $this->factor = $config['factor']; |
|
124 | + $this->factor = $config['factor']; |
|
125 | 125 | } |
126 | 126 | |
127 | 127 | if (isset($config['jitter'])) { |
128 | - if ($config['jitter'] <= 0) { |
|
128 | + if ($config['jitter'] <= 0) { |
|
129 | 129 | throw new Google_Task_Exception( |
130 | 130 | 'Task configuration `jitter` must be greater than 0.' |
131 | 131 | ); |
132 | - } |
|
132 | + } |
|
133 | 133 | |
134 | - $this->jitter = $config['jitter']; |
|
134 | + $this->jitter = $config['jitter']; |
|
135 | 135 | } |
136 | 136 | |
137 | 137 | if (isset($config['retries'])) { |
138 | - if ($config['retries'] < 0) { |
|
138 | + if ($config['retries'] < 0) { |
|
139 | 139 | throw new Google_Task_Exception( |
140 | 140 | 'Task configuration `retries` must not be negative.' |
141 | 141 | ); |
142 | - } |
|
143 | - $this->maxAttempts += $config['retries']; |
|
142 | + } |
|
143 | + $this->maxAttempts += $config['retries']; |
|
144 | 144 | } |
145 | 145 | |
146 | 146 | if (!is_callable($action)) { |
@@ -151,131 +151,131 @@ discard block |
||
151 | 151 | |
152 | 152 | $this->action = $action; |
153 | 153 | $this->arguments = $arguments; |
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Checks if a retry can be attempted. |
|
158 | - * |
|
159 | - * @return boolean |
|
160 | - */ |
|
161 | - public function canAttempt() |
|
162 | - { |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Checks if a retry can be attempted. |
|
158 | + * |
|
159 | + * @return boolean |
|
160 | + */ |
|
161 | + public function canAttempt() |
|
162 | + { |
|
163 | 163 | return $this->attempts < $this->maxAttempts; |
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Runs the task and (if applicable) automatically retries when errors occur. |
|
168 | - * |
|
169 | - * @return mixed |
|
170 | - * @throws Google_Task_Retryable on failure when no retries are available. |
|
171 | - */ |
|
172 | - public function run() |
|
173 | - { |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Runs the task and (if applicable) automatically retries when errors occur. |
|
168 | + * |
|
169 | + * @return mixed |
|
170 | + * @throws Google_Task_Retryable on failure when no retries are available. |
|
171 | + */ |
|
172 | + public function run() |
|
173 | + { |
|
174 | 174 | while ($this->attempt()) { |
175 | - try { |
|
175 | + try { |
|
176 | 176 | return call_user_func_array($this->action, $this->arguments); |
177 | - } catch (Google_Service_Exception $exception) { |
|
177 | + } catch (Google_Service_Exception $exception) { |
|
178 | 178 | $allowedRetries = $this->allowedRetries( |
179 | 179 | $exception->getCode(), |
180 | 180 | $exception->getErrors() |
181 | 181 | ); |
182 | 182 | |
183 | 183 | if (!$this->canAttempt() || !$allowedRetries) { |
184 | - throw $exception; |
|
184 | + throw $exception; |
|
185 | 185 | } |
186 | 186 | |
187 | 187 | if ($allowedRetries > 0) { |
188 | - $this->maxAttempts = min( |
|
189 | - $this->maxAttempts, |
|
190 | - $this->attempts + $allowedRetries |
|
191 | - ); |
|
188 | + $this->maxAttempts = min( |
|
189 | + $this->maxAttempts, |
|
190 | + $this->attempts + $allowedRetries |
|
191 | + ); |
|
192 | + } |
|
192 | 193 | } |
193 | - } |
|
194 | 194 | } |
195 | - } |
|
196 | - |
|
197 | - /** |
|
198 | - * Runs a task once, if possible. This is useful for bypassing the `run()` |
|
199 | - * loop. |
|
200 | - * |
|
201 | - * NOTE: If this is not the first attempt, this function will sleep in |
|
202 | - * accordance to the backoff configurations before running the task. |
|
203 | - * |
|
204 | - * @return boolean |
|
205 | - */ |
|
206 | - public function attempt() |
|
207 | - { |
|
195 | + } |
|
196 | + |
|
197 | + /** |
|
198 | + * Runs a task once, if possible. This is useful for bypassing the `run()` |
|
199 | + * loop. |
|
200 | + * |
|
201 | + * NOTE: If this is not the first attempt, this function will sleep in |
|
202 | + * accordance to the backoff configurations before running the task. |
|
203 | + * |
|
204 | + * @return boolean |
|
205 | + */ |
|
206 | + public function attempt() |
|
207 | + { |
|
208 | 208 | if (!$this->canAttempt()) { |
209 | - return false; |
|
209 | + return false; |
|
210 | 210 | } |
211 | 211 | |
212 | 212 | if ($this->attempts > 0) { |
213 | - $this->backOff(); |
|
213 | + $this->backOff(); |
|
214 | 214 | } |
215 | 215 | |
216 | 216 | $this->attempts++; |
217 | 217 | return true; |
218 | - } |
|
218 | + } |
|
219 | 219 | |
220 | - /** |
|
221 | - * Sleeps in accordance to the backoff configurations. |
|
222 | - */ |
|
223 | - private function backOff() |
|
224 | - { |
|
220 | + /** |
|
221 | + * Sleeps in accordance to the backoff configurations. |
|
222 | + */ |
|
223 | + private function backOff() |
|
224 | + { |
|
225 | 225 | $delay = $this->getDelay(); |
226 | 226 | |
227 | 227 | usleep($delay * 1000000); |
228 | - } |
|
229 | - |
|
230 | - /** |
|
231 | - * Gets the delay (in seconds) for the current backoff period. |
|
232 | - * |
|
233 | - * @return float |
|
234 | - */ |
|
235 | - private function getDelay() |
|
236 | - { |
|
228 | + } |
|
229 | + |
|
230 | + /** |
|
231 | + * Gets the delay (in seconds) for the current backoff period. |
|
232 | + * |
|
233 | + * @return float |
|
234 | + */ |
|
235 | + private function getDelay() |
|
236 | + { |
|
237 | 237 | $jitter = $this->getJitter(); |
238 | 238 | $factor = $this->attempts > 1 ? $this->factor + $jitter : 1 + abs($jitter); |
239 | 239 | |
240 | 240 | return $this->delay = min($this->maxDelay, $this->delay * $factor); |
241 | - } |
|
242 | - |
|
243 | - /** |
|
244 | - * Gets the current jitter (random number between -$this->jitter and |
|
245 | - * $this->jitter). |
|
246 | - * |
|
247 | - * @return float |
|
248 | - */ |
|
249 | - private function getJitter() |
|
250 | - { |
|
241 | + } |
|
242 | + |
|
243 | + /** |
|
244 | + * Gets the current jitter (random number between -$this->jitter and |
|
245 | + * $this->jitter). |
|
246 | + * |
|
247 | + * @return float |
|
248 | + */ |
|
249 | + private function getJitter() |
|
250 | + { |
|
251 | 251 | return $this->jitter * 2 * mt_rand() / mt_getrandmax() - $this->jitter; |
252 | - } |
|
253 | - |
|
254 | - /** |
|
255 | - * Gets the number of times the associated task can be retried. |
|
256 | - * |
|
257 | - * NOTE: -1 is returned if the task can be retried indefinitely |
|
258 | - * |
|
259 | - * @return integer |
|
260 | - */ |
|
261 | - public function allowedRetries($code, $errors = array()) |
|
262 | - { |
|
252 | + } |
|
253 | + |
|
254 | + /** |
|
255 | + * Gets the number of times the associated task can be retried. |
|
256 | + * |
|
257 | + * NOTE: -1 is returned if the task can be retried indefinitely |
|
258 | + * |
|
259 | + * @return integer |
|
260 | + */ |
|
261 | + public function allowedRetries($code, $errors = array()) |
|
262 | + { |
|
263 | 263 | if (isset($this->retryMap[$code])) { |
264 | - return $this->retryMap[$code]; |
|
264 | + return $this->retryMap[$code]; |
|
265 | 265 | } |
266 | 266 | |
267 | 267 | if ( |
268 | 268 | !empty($errors) && |
269 | 269 | isset($errors[0]['reason'], $this->retryMap[$errors[0]['reason']]) |
270 | 270 | ) { |
271 | - return $this->retryMap[$errors[0]['reason']]; |
|
271 | + return $this->retryMap[$errors[0]['reason']]; |
|
272 | 272 | } |
273 | 273 | |
274 | 274 | return 0; |
275 | - } |
|
275 | + } |
|
276 | 276 | |
277 | - public function setRetryMap($retryMap) |
|
278 | - { |
|
277 | + public function setRetryMap($retryMap) |
|
278 | + { |
|
279 | 279 | $this->retryMap = $retryMap; |
280 | - } |
|
280 | + } |
|
281 | 281 | } |
@@ -5,10 +5,10 @@ discard block |
||
5 | 5 | //============ Google Move Code ======================// |
6 | 6 | |
7 | 7 | if ( isset( $_GET['album_download_directory'] ) ) { |
8 | - $album_download_directory = $_GET['album_download_directory']; |
|
9 | - $album_download_directory = '../'.$album_download_directory; |
|
8 | + $album_download_directory = $_GET['album_download_directory']; |
|
9 | + $album_download_directory = '../'.$album_download_directory; |
|
10 | 10 | } else { |
11 | - header('location:../index.php'); |
|
11 | + header('location:../index.php'); |
|
12 | 12 | } |
13 | 13 | |
14 | 14 | require_once 'Zend/autooad.php'; |
@@ -18,10 +18,10 @@ discard block |
||
18 | 18 | |
19 | 19 | |
20 | 20 | function getAuthSubHttpClient() { |
21 | - if ( isset( $_SESSION['google_session_token'] ) ) { |
|
22 | - $client = Zend_Gdata_AuthSub::getHttpClient( $_SESSION['google_session_token'] ); |
|
23 | - return $client; |
|
24 | - } |
|
21 | + if ( isset( $_SESSION['google_session_token'] ) ) { |
|
22 | + $client = Zend_Gdata_AuthSub::getHttpClient( $_SESSION['google_session_token'] ); |
|
23 | + return $client; |
|
24 | + } |
|
25 | 25 | } |
26 | 26 | |
27 | 27 | $gp = new Zend_Gdata_Photos(getAuthSubHttpClient(), "Google-DevelopersGuide-1.0"); |
@@ -29,87 +29,87 @@ discard block |
||
29 | 29 | |
30 | 30 | |
31 | 31 | function add_new_album( $entry, $gp, $album_download_directory, $album_name ) { |
32 | - $new_album_name = str_replace( " ", "_", $album_name ); |
|
33 | - $new_album_name = $new_album_name.'_'.uniqid(); |
|
34 | - |
|
35 | - $entry->setTitle( $gp->newTitle( $new_album_name ) ); |
|
36 | - $entry->setSummary( $gp->newSummary("Album added by Facebook Album Challenge") ); |
|
37 | - $gp->insertAlbumEntry( $entry ); |
|
38 | - |
|
39 | - $path = $album_download_directory.$album_name; |
|
40 | - if ( file_exists( $path ) ) { |
|
41 | - $photos = scandir( $path ); |
|
42 | - |
|
43 | - foreach ( $photos as $photo ) { |
|
44 | - if ( $photo != "." && $photo != ".." ) { |
|
45 | - $photo_path = $path.'/'.$photo; |
|
46 | - add_new_photo_to_album( $gp, $photo_path, $new_album_name ); |
|
47 | - } |
|
48 | - } |
|
49 | - } |
|
32 | + $new_album_name = str_replace( " ", "_", $album_name ); |
|
33 | + $new_album_name = $new_album_name.'_'.uniqid(); |
|
34 | + |
|
35 | + $entry->setTitle( $gp->newTitle( $new_album_name ) ); |
|
36 | + $entry->setSummary( $gp->newSummary("Album added by Facebook Album Challenge") ); |
|
37 | + $gp->insertAlbumEntry( $entry ); |
|
38 | + |
|
39 | + $path = $album_download_directory.$album_name; |
|
40 | + if ( file_exists( $path ) ) { |
|
41 | + $photos = scandir( $path ); |
|
42 | + |
|
43 | + foreach ( $photos as $photo ) { |
|
44 | + if ( $photo != "." && $photo != ".." ) { |
|
45 | + $photo_path = $path.'/'.$photo; |
|
46 | + add_new_photo_to_album( $gp, $photo_path, $new_album_name ); |
|
47 | + } |
|
48 | + } |
|
49 | + } |
|
50 | 50 | } |
51 | 51 | |
52 | 52 | function add_new_photo_to_album( $gp, $path, $new_album_name ) { |
53 | - $user_name = "default"; |
|
54 | - $file_name = $path; |
|
55 | - $photo_name = "Photo added by Facebook Album Challenge"; |
|
56 | - $photo_caption = "Photo added by Facebook Album Challenge"; |
|
57 | - $photo_tags = "Photo, Facebook-Album-Challenge"; |
|
58 | - |
|
59 | - $fd = $gp->newMediaFileSource( $file_name ); |
|
60 | - $fd->setContentType("image/jpeg"); |
|
61 | - |
|
62 | - // Create a PhotoEntry |
|
63 | - $photo_entry = $gp->newPhotoEntry(); |
|
64 | - |
|
65 | - $photo_entry->setMediaSource( $fd ); |
|
66 | - $photo_entry->setTitle( $gp->newTitle( $photo_name ) ); |
|
67 | - $photo_entry->setSummary( $gp->newSummary( $photo_caption ) ); |
|
68 | - |
|
69 | - // add some tags |
|
70 | - $photo_media = new Zend_Gdata_Media_Extension_MediaKeywords(); |
|
71 | - $photo_media->setText( $photo_tags ); |
|
72 | - $photo_entry->mediaGroup = new Zend_Gdata_Media_Extension_MediaGroup(); |
|
73 | - $photo_entry->mediaGroup->keywords = $photo_media; |
|
74 | - |
|
75 | - // We use the AlbumQuery class to generate the URL for the album |
|
76 | - $album_query = $gp->newAlbumQuery(); |
|
77 | - |
|
78 | - $album_query->setUser( $user_name ); |
|
79 | - //$albumQuery->setAlbumId($albumId); |
|
80 | - $album_query->setAlbumName( $new_album_name ); |
|
81 | - |
|
82 | - // We insert the photo, and the server returns the entry representing |
|
83 | - // that photo after it is uploaded |
|
84 | - //$insertedEntry = $gp->insertPhotoEntry( $photoEntry, $albumQuery->getQueryUrl() ); |
|
85 | - $gp->insertPhotoEntry( $photo_entry, $album_query->getQueryUrl() ); |
|
53 | + $user_name = "default"; |
|
54 | + $file_name = $path; |
|
55 | + $photo_name = "Photo added by Facebook Album Challenge"; |
|
56 | + $photo_caption = "Photo added by Facebook Album Challenge"; |
|
57 | + $photo_tags = "Photo, Facebook-Album-Challenge"; |
|
58 | + |
|
59 | + $fd = $gp->newMediaFileSource( $file_name ); |
|
60 | + $fd->setContentType("image/jpeg"); |
|
61 | + |
|
62 | + // Create a PhotoEntry |
|
63 | + $photo_entry = $gp->newPhotoEntry(); |
|
64 | + |
|
65 | + $photo_entry->setMediaSource( $fd ); |
|
66 | + $photo_entry->setTitle( $gp->newTitle( $photo_name ) ); |
|
67 | + $photo_entry->setSummary( $gp->newSummary( $photo_caption ) ); |
|
68 | + |
|
69 | + // add some tags |
|
70 | + $photo_media = new Zend_Gdata_Media_Extension_MediaKeywords(); |
|
71 | + $photo_media->setText( $photo_tags ); |
|
72 | + $photo_entry->mediaGroup = new Zend_Gdata_Media_Extension_MediaGroup(); |
|
73 | + $photo_entry->mediaGroup->keywords = $photo_media; |
|
74 | + |
|
75 | + // We use the AlbumQuery class to generate the URL for the album |
|
76 | + $album_query = $gp->newAlbumQuery(); |
|
77 | + |
|
78 | + $album_query->setUser( $user_name ); |
|
79 | + //$albumQuery->setAlbumId($albumId); |
|
80 | + $album_query->setAlbumName( $new_album_name ); |
|
81 | + |
|
82 | + // We insert the photo, and the server returns the entry representing |
|
83 | + // that photo after it is uploaded |
|
84 | + //$insertedEntry = $gp->insertPhotoEntry( $photoEntry, $albumQuery->getQueryUrl() ); |
|
85 | + $gp->insertPhotoEntry( $photo_entry, $album_query->getQueryUrl() ); |
|
86 | 86 | } |
87 | 87 | |
88 | 88 | if ( isset( $album_download_directory ) ) { |
89 | - if ( file_exists( $album_download_directory ) ) { |
|
90 | - $album_names = scandir( $album_download_directory ); |
|
91 | - |
|
92 | - foreach ( $album_names as $album_name ) { |
|
93 | - if ( $album_name != "." && $album_name != "..") { |
|
94 | - add_new_album( $entry, $gp, $album_download_directory, $album_name ); |
|
95 | - } |
|
96 | - } |
|
97 | - |
|
98 | - $unlink_folder = rtrim( $album_download_directory, "/" ); |
|
99 | - require_once('../unlink_directory.php'); |
|
100 | - $unlink_directory = new unlink_directory(); |
|
101 | - $unlink_directory->remove_directory( $unlink_folder ); |
|
102 | - } |
|
103 | - $response = 1; |
|
89 | + if ( file_exists( $album_download_directory ) ) { |
|
90 | + $album_names = scandir( $album_download_directory ); |
|
91 | + |
|
92 | + foreach ( $album_names as $album_name ) { |
|
93 | + if ( $album_name != "." && $album_name != "..") { |
|
94 | + add_new_album( $entry, $gp, $album_download_directory, $album_name ); |
|
95 | + } |
|
96 | + } |
|
97 | + |
|
98 | + $unlink_folder = rtrim( $album_download_directory, "/" ); |
|
99 | + require_once('../unlink_directory.php'); |
|
100 | + $unlink_directory = new unlink_directory(); |
|
101 | + $unlink_directory->remove_directory( $unlink_folder ); |
|
102 | + } |
|
103 | + $response = 1; |
|
104 | 104 | } else { |
105 | - $response = 0; |
|
105 | + $response = 0; |
|
106 | 106 | } |
107 | 107 | |
108 | 108 | |
109 | 109 | if ( isset( $_GET['ajax'] ) ) |
110 | - echo $response; |
|
110 | + echo $response; |
|
111 | 111 | else |
112 | - header('location:../index.php?response='.$response); |
|
112 | + header('location:../index.php?response='.$response); |
|
113 | 113 | |
114 | 114 | |
115 | 115 | ?> |
@@ -141,20 +141,20 @@ |
||
141 | 141 | $cover_photo_id = isset($data['cover_photo']['id']) ? $data['cover_photo']['id'] : ''; |
142 | 142 | $count = isset($data['count']) ? $data['count'] : ''; |
143 | 143 | |
144 | - $pictureLink = "slideshow.php?album_id={$id}&album_name={$name}"; |
|
145 | - echo "<div class='carding'>"; |
|
146 | - echo "<a target='_blank' href='{$pictureLink}'>"; |
|
147 | - $cover_photo_id = (!empty($cover_photo_id))?$cover_photo_id : 123456; |
|
148 | - echo "<img width=200px height=200px src='https://graph.facebook.com/v3.3/{$cover_photo_id}/picture?access_token={$accessToken}' alt=''>"; |
|
149 | - echo "<p>{$name}</p>"; |
|
150 | - echo "</a>"; |
|
151 | - //echo "$images"; |
|
144 | + $pictureLink = "slideshow.php?album_id={$id}&album_name={$name}"; |
|
145 | + echo "<div class='carding'>"; |
|
146 | + echo "<a target='_blank' href='{$pictureLink}'>"; |
|
147 | + $cover_photo_id = (!empty($cover_photo_id))?$cover_photo_id : 123456; |
|
148 | + echo "<img width=200px height=200px src='https://graph.facebook.com/v3.3/{$cover_photo_id}/picture?access_token={$accessToken}' alt=''>"; |
|
149 | + echo "<p>{$name}</p>"; |
|
150 | + echo "</a>"; |
|
151 | + //echo "$images"; |
|
152 | 152 | |
153 | 153 | $photoCount = ($count > 1) ? $count . 'Photos' : $count . 'Photo'; |
154 | 154 | |
155 | - echo "<p><span style='color:#888;'>{$photoCount} / <a href='{$link}' target='_blank'>View on Facebook</a></span></p>"; |
|
156 | - echo "<p>{$description}</p>"; |
|
157 | - ?> |
|
155 | + echo "<p><span style='color:#888;'>{$photoCount} / <a href='{$link}' target='_blank'>View on Facebook</a></span></p>"; |
|
156 | + echo "<p>{$description}</p>"; |
|
157 | + ?> |
|
158 | 158 | <div class="caption"> |
159 | 159 | |
160 | 160 | <button rel="<?php echo $id.','.$name;?>" class="single-download btn btn-primary pull-left" title="Download Album"> |
@@ -1,20 +1,20 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | session_start(); |
3 | 3 | ini_set('max_execution_time', 300); |
4 | - require_once 'appconfig.php'; |
|
4 | + require_once 'appconfig.php'; |
|
5 | 5 | |
6 | - use Facebook\GraphNodes\GraphObject; |
|
7 | - use Facebook\GraphNodes\GraphSessionInfo; |
|
8 | - use Facebook\Authentication\AccessToken; |
|
9 | - use Facebook\HttpClients\FacebookHttpable; |
|
10 | - use Facebook\HttpClients\FacebookCurl; |
|
11 | - use Facebook\HttpClients\FacebookCurlHttpClient; |
|
12 | - use Facebook\FacebookSession; |
|
13 | - use Facebook\Helpers\FacebookRedirectLoginHelper; |
|
14 | - use Facebook\FacebookRequest; |
|
15 | - use Facebook\FacebookResponse; |
|
16 | - use Facebook\Exceptions\FacebookSDKException; |
|
17 | - use Facebook\Exceptions\FacebookAuthorizationException; |
|
6 | + use Facebook\GraphNodes\GraphObject; |
|
7 | + use Facebook\GraphNodes\GraphSessionInfo; |
|
8 | + use Facebook\Authentication\AccessToken; |
|
9 | + use Facebook\HttpClients\FacebookHttpable; |
|
10 | + use Facebook\HttpClients\FacebookCurl; |
|
11 | + use Facebook\HttpClients\FacebookCurlHttpClient; |
|
12 | + use Facebook\FacebookSession; |
|
13 | + use Facebook\Helpers\FacebookRedirectLoginHelper; |
|
14 | + use Facebook\FacebookRequest; |
|
15 | + use Facebook\FacebookResponse; |
|
16 | + use Facebook\Exceptions\FacebookSDKException; |
|
17 | + use Facebook\Exceptions\FacebookAuthorizationException; |
|
18 | 18 | |
19 | 19 | $session = $_SESSION['fb_access_token']; |
20 | 20 | |
@@ -24,25 +24,25 @@ discard block |
||
24 | 24 | |
25 | 25 | function download_album($session, $album_download_directory, $album_id, $album_name) { |
26 | 26 | |
27 | - $request_album_photos = "https://graph.facebook.com/v3.3/{$album_id}/photos?fields=source,images,name&limit=500&access_token={$session}"; |
|
28 | - $response_album_photos = file_get_contents($request_album_photos); |
|
29 | - $album_photos = json_decode($response_album_photos, true, 512, JSON_BIGINT_AS_STRING); |
|
27 | + $request_album_photos = "https://graph.facebook.com/v3.3/{$album_id}/photos?fields=source,images,name&limit=500&access_token={$session}"; |
|
28 | + $response_album_photos = file_get_contents($request_album_photos); |
|
29 | + $album_photos = json_decode($response_album_photos, true, 512, JSON_BIGINT_AS_STRING); |
|
30 | 30 | |
31 | - $album_directory = $album_download_directory.$album_name; |
|
32 | - if ( !file_exists( $album_directory ) ) { |
|
33 | - mkdir($album_directory, 0777); |
|
34 | - } |
|
31 | + $album_directory = $album_download_directory.$album_name; |
|
32 | + if ( !file_exists( $album_directory ) ) { |
|
33 | + mkdir($album_directory, 0777); |
|
34 | + } |
|
35 | 35 | |
36 | - $i = 1; |
|
37 | - foreach ( $album_photos['data'] as $album_photo ) { |
|
38 | - $album_photo = (array) $album_photo; |
|
39 | - file_put_contents( $album_directory.'/'.$i.".jpg", fopen( $album_photo['source'], 'r') ); |
|
40 | - $i++; |
|
41 | - } |
|
42 | - } |
|
36 | + $i = 1; |
|
37 | + foreach ( $album_photos['data'] as $album_photo ) { |
|
38 | + $album_photo = (array) $album_photo; |
|
39 | + file_put_contents( $album_directory.'/'.$i.".jpg", fopen( $album_photo['source'], 'r') ); |
|
40 | + $i++; |
|
41 | + } |
|
42 | + } |
|
43 | 43 | |
44 | - //---------- For 1 album download -------------------------------------------------// |
|
45 | - if ( isset( $_GET['single_album'] ) && !empty ( $_GET['single_album'] ) ) { |
|
44 | + //---------- For 1 album download -------------------------------------------------// |
|
45 | + if ( isset( $_GET['single_album'] ) && !empty ( $_GET['single_album'] ) ) { |
|
46 | 46 | |
47 | 47 | $single_album = explode(",", $_GET['single_album']); |
48 | 48 | download_album($session, $album_download_directory, $single_album[0], $single_album[1]); |
@@ -64,8 +64,8 @@ discard block |
||
64 | 64 | |
65 | 65 | // graph api request for user data |
66 | 66 | |
67 | - $request_albums = "https://graph.facebook.com/v3.3/me/albums?fields=id,name&access_token={$session}"; |
|
68 | - $response_albums = file_get_contents($request_albums); |
|
67 | + $request_albums = "https://graph.facebook.com/v3.3/me/albums?fields=id,name&access_token={$session}"; |
|
68 | + $response_albums = file_get_contents($request_albums); |
|
69 | 69 | |
70 | 70 | // get response |
71 | 71 | $albums = json_decode($response_albums, true, 512, JSON_BIGINT_AS_STRING); |