Commit 7e87cc73281edd256a984e73f290efea30cc57f9
1 parent
ff70d88d
Exists in
master
#41 Implementando cadastro de foto para pessoa/aluno
Showing
7 changed files
with
1187 additions
and
2 deletions
Show diff stats
ieducar/configuration/ieducar.ini
| ... | ... | @@ -61,6 +61,11 @@ app.database.port = 5433 |
| 61 | 61 | ; Define uma url para redirecionar todas as requisições; |
| 62 | 62 | ; app.routes.redirect_to = /intranet/manutencao.php |
| 63 | 63 | |
| 64 | +; Configurações do armazenamento de imagem amazon s3 | |
| 65 | +;app.aws.bucketname = nome do bucket | |
| 66 | +;app.aws.awsacesskey = chave | |
| 67 | +;app.aws.awssecretkey = chave secreta | |
| 68 | + | |
| 64 | 69 | ; Configurações de template |
| 65 | 70 | ; Os caminhos de diretórios devem sempre ser relativos a intranet/ |
| 66 | 71 | app.template.vars.instituicao = i-Educar | ... | ... |
| ... | ... | @@ -0,0 +1,911 @@ |
| 1 | +<?php | |
| 2 | +/** | |
| 3 | +* $Id$ | |
| 4 | +* | |
| 5 | +* Copyright (c) 2007, Donovan Schonknecht. All rights reserved. | |
| 6 | +* | |
| 7 | +* Redistribution and use in source and binary forms, with or without | |
| 8 | +* modification, are permitted provided that the following conditions are met: | |
| 9 | +* | |
| 10 | +* - Redistributions of source code must retain the above copyright notice, | |
| 11 | +* this list of conditions and the following disclaimer. | |
| 12 | +* - Redistributions in binary form must reproduce the above copyright | |
| 13 | +* notice, this list of conditions and the following disclaimer in the | |
| 14 | +* documentation and/or other materials provided with the distribution. | |
| 15 | +* | |
| 16 | +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 17 | +* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
| 18 | +* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
| 19 | +* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | |
| 20 | +* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
| 21 | +* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
| 22 | +* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
| 23 | +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
| 24 | +* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | +* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | +* POSSIBILITY OF SUCH DAMAGE. | |
| 27 | +*/ | |
| 28 | + | |
| 29 | +/** | |
| 30 | +* Amazon S3 PHP class | |
| 31 | +* | |
| 32 | +* @link http://undesigned.org.za/2007/10/22/amazon-s3-php-class | |
| 33 | +* @version 0.2.3 | |
| 34 | +*/ | |
| 35 | +class S3 { | |
| 36 | + // ACL flags | |
| 37 | + const ACL_PRIVATE = 'private'; | |
| 38 | + const ACL_PUBLIC_READ = 'public-read'; | |
| 39 | + const ACL_PUBLIC_READ_WRITE = 'public-read-write'; | |
| 40 | + | |
| 41 | + private static $__accessKey; // AWS Access key | |
| 42 | + private static $__secretKey; // AWS Secret key | |
| 43 | + | |
| 44 | + | |
| 45 | + /** | |
| 46 | + * Constructor, used if you're not calling the class statically | |
| 47 | + * | |
| 48 | + * @param string $accessKey Access key | |
| 49 | + * @param string $secretKey Secret key | |
| 50 | + * @return void | |
| 51 | + */ | |
| 52 | + public function __construct($accessKey = null, $secretKey = null) { | |
| 53 | + if ($accessKey !== null && $secretKey !== null) | |
| 54 | + self::setAuth($accessKey, $secretKey); | |
| 55 | + } | |
| 56 | + | |
| 57 | + | |
| 58 | + /** | |
| 59 | + * Set access information | |
| 60 | + * | |
| 61 | + * @param string $accessKey Access key | |
| 62 | + * @param string $secretKey Secret key | |
| 63 | + * @return void | |
| 64 | + */ | |
| 65 | + public static function setAuth($accessKey, $secretKey) { | |
| 66 | + self::$__accessKey = $accessKey; | |
| 67 | + self::$__secretKey = $secretKey; | |
| 68 | + } | |
| 69 | + | |
| 70 | + | |
| 71 | + /** | |
| 72 | + * Get a list of buckets | |
| 73 | + * | |
| 74 | + * @param boolean $detailed Returns detailed bucket list when true | |
| 75 | + * @return array | false | |
| 76 | + */ | |
| 77 | + public static function listBuckets($detailed = false) { | |
| 78 | + $rest = new S3Request('GET', '', ''); | |
| 79 | + $rest = $rest->getResponse(); | |
| 80 | + if ($rest->error === false && $rest->code !== 200) | |
| 81 | + $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |
| 82 | + if ($rest->error !== false) { | |
| 83 | + trigger_error(sprintf("S3::listBuckets(): [%s] %s", $rest->error['code'], $rest->error['message']), E_USER_WARNING); | |
| 84 | + return false; | |
| 85 | + } | |
| 86 | + $results = array(); //var_dump($rest->body); | |
| 87 | + if (!isset($rest->body->Buckets)) return $results; | |
| 88 | + | |
| 89 | + if ($detailed) { | |
| 90 | + if (isset($rest->body->Owner, $rest->body->Owner->ID, $rest->body->Owner->DisplayName)) | |
| 91 | + $results['owner'] = array( | |
| 92 | + 'id' => (string)$rest->body->Owner->ID, 'name' => (string)$rest->body->Owner->ID | |
| 93 | + ); | |
| 94 | + $results['buckets'] = array(); | |
| 95 | + foreach ($rest->body->Buckets->Bucket as $b) | |
| 96 | + $results['buckets'][] = array( | |
| 97 | + 'name' => (string)$b->Name, 'time' => strtotime((string)$b->CreationDate) | |
| 98 | + ); | |
| 99 | + } else | |
| 100 | + foreach ($rest->body->Buckets->Bucket as $b) $results[] = (string)$b->Name; | |
| 101 | + | |
| 102 | + return $results; | |
| 103 | + } | |
| 104 | + | |
| 105 | + | |
| 106 | + /* | |
| 107 | + * Get contents for a bucket | |
| 108 | + * | |
| 109 | + * If maxKeys is null this method will loop through truncated result sets | |
| 110 | + * | |
| 111 | + * @param string $bucket Bucket name | |
| 112 | + * @param string $prefix Prefix | |
| 113 | + * @param string $marker Marker (last file listed) | |
| 114 | + * @param string $maxKeys Max keys (maximum number of keys to return) | |
| 115 | + * @return array | false | |
| 116 | + */ | |
| 117 | + public static function getBucket($bucket, $prefix = null, $marker = null, $maxKeys = null) { | |
| 118 | + $rest = new S3Request('GET', $bucket, ''); | |
| 119 | + if ($prefix !== null && $prefix !== '') $rest->setParameter('prefix', $prefix); | |
| 120 | + if ($marker !== null && $prefix !== '') $rest->setParameter('marker', $marker); | |
| 121 | + if ($maxKeys !== null && $prefix !== '') $rest->setParameter('max-keys', $maxKeys); | |
| 122 | + $response = $rest->getResponse(); | |
| 123 | + if ($response->error === false && $response->code !== 200) | |
| 124 | + $response->error = array('code' => $response->code, 'message' => 'Unexpected HTTP status'); | |
| 125 | + if ($response->error !== false) { | |
| 126 | + trigger_error(sprintf("S3::getBucket(): [%s] %s", $response->error['code'], $response->error['message']), E_USER_WARNING); | |
| 127 | + return false; | |
| 128 | + } | |
| 129 | + | |
| 130 | + $results = array(); | |
| 131 | + | |
| 132 | + $lastMarker = null; | |
| 133 | + if (isset($response->body, $response->body->Contents)) | |
| 134 | + foreach ($response->body->Contents as $c) { | |
| 135 | + $results[(string)$c->Key] = array( | |
| 136 | + 'name' => (string)$c->Key, | |
| 137 | + 'time' => strToTime((string)$c->LastModified), | |
| 138 | + 'size' => (int)$c->Size, | |
| 139 | + 'hash' => substr((string)$c->ETag, 1, -1) | |
| 140 | + ); | |
| 141 | + $lastMarker = (string)$c->Key; | |
| 142 | + //$response->body->IsTruncated = 'true'; break; | |
| 143 | + } | |
| 144 | + | |
| 145 | + | |
| 146 | + if (isset($response->body->IsTruncated) && | |
| 147 | + (string)$response->body->IsTruncated == 'false') return $results; | |
| 148 | + | |
| 149 | + // Loop through truncated results if maxKeys isn't specified | |
| 150 | + if ($maxKeys == null && $lastMarker !== null && (string)$response->body->IsTruncated == 'true') | |
| 151 | + do { | |
| 152 | + $rest = new S3Request('GET', $bucket, ''); | |
| 153 | + if ($prefix !== null) $rest->setParameter('prefix', $prefix); | |
| 154 | + $rest->setParameter('marker', $lastMarker); | |
| 155 | + | |
| 156 | + if (($response = $rest->getResponse(true)) == false || $response->code !== 200) break; | |
| 157 | + if (isset($response->body, $response->body->Contents)) | |
| 158 | + foreach ($response->body->Contents as $c) { | |
| 159 | + $results[(string)$c->Key] = array( | |
| 160 | + 'name' => (string)$c->Key, | |
| 161 | + 'time' => strToTime((string)$c->LastModified), | |
| 162 | + 'size' => (int)$c->Size, | |
| 163 | + 'hash' => substr((string)$c->ETag, 1, -1) | |
| 164 | + ); | |
| 165 | + $lastMarker = (string)$c->Key; | |
| 166 | + } | |
| 167 | + } while ($response !== false && (string)$response->body->IsTruncated == 'true'); | |
| 168 | + | |
| 169 | + return $results; | |
| 170 | + } | |
| 171 | + | |
| 172 | + | |
| 173 | + /** | |
| 174 | + * Put a bucket | |
| 175 | + * | |
| 176 | + * @param string $bucket Bucket name | |
| 177 | + * @param constant $acl ACL flag | |
| 178 | + * @return boolean | |
| 179 | + */ | |
| 180 | + public function putBucket($bucket, $acl = self::ACL_PRIVATE) { | |
| 181 | + $rest = new S3Request('PUT', $bucket, ''); | |
| 182 | + $rest->setAmzHeader('x-amz-acl', $acl); | |
| 183 | + $rest = $rest->getResponse(); | |
| 184 | + if ($rest->error === false && $rest->code !== 200) | |
| 185 | + $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |
| 186 | + if ($rest->error !== false) { | |
| 187 | + trigger_error(sprintf("S3::putBucket({$bucket}): [%s] %s", | |
| 188 | + $rest->error['code'], $rest->error['message']), E_USER_WARNING); | |
| 189 | + return false; | |
| 190 | + } | |
| 191 | + return true; | |
| 192 | + } | |
| 193 | + | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * Delete an empty bucket | |
| 197 | + * | |
| 198 | + * @param string $bucket Bucket name | |
| 199 | + * @return boolean | |
| 200 | + */ | |
| 201 | + public function deleteBucket($bucket = '') { | |
| 202 | + $rest = new S3Request('DELETE', $bucket); | |
| 203 | + $rest = $rest->getResponse(); | |
| 204 | + if ($rest->error === false && $rest->code !== 204) | |
| 205 | + $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |
| 206 | + if ($rest->error !== false) { | |
| 207 | + trigger_error(sprintf("S3::deleteBucket({$bucket}): [%s] %s", | |
| 208 | + $rest->error['code'], $rest->error['message']), E_USER_WARNING); | |
| 209 | + return false; | |
| 210 | + } | |
| 211 | + return true; | |
| 212 | + } | |
| 213 | + | |
| 214 | + | |
| 215 | + /** | |
| 216 | + * Create input info array for putObject() | |
| 217 | + * | |
| 218 | + * @param string $file Input file | |
| 219 | + * @param mixed $md5sum Use MD5 hash (supply a string if you want to use your own) | |
| 220 | + * @return array | false | |
| 221 | + */ | |
| 222 | + public static function inputFile($file, $md5sum = true) { | |
| 223 | + if (!file_exists($file) || !is_file($file) || !is_readable($file)) { | |
| 224 | + trigger_error('S3::inputFile(): Unable to open input file: '.$file, E_USER_WARNING); | |
| 225 | + return false; | |
| 226 | + } | |
| 227 | + return array('file' => $file, 'size' => filesize($file), | |
| 228 | + 'md5sum' => $md5sum !== false ? (is_string($md5sum) ? $md5sum : | |
| 229 | + base64_encode(md5_file($file, true))) : ''); | |
| 230 | + } | |
| 231 | + | |
| 232 | + | |
| 233 | + /** | |
| 234 | + * Use a resource for input | |
| 235 | + * | |
| 236 | + * @param string $file Input file | |
| 237 | + * @param integer $bufferSize Input byte size | |
| 238 | + * @param string $md5sum MD5 hash to send (optional) | |
| 239 | + * @return array | false | |
| 240 | + */ | |
| 241 | + public static function inputResource(&$resource, $bufferSize, $md5sum = '') { | |
| 242 | + if (!is_resource($resource) || $bufferSize <= 0) { | |
| 243 | + trigger_error('S3::inputResource(): Invalid resource or buffer size', E_USER_WARNING); | |
| 244 | + return false; | |
| 245 | + } | |
| 246 | + $input = array('size' => $bufferSize, 'md5sum' => $md5sum); | |
| 247 | + $input['fp'] =& $resource; | |
| 248 | + return $input; | |
| 249 | + } | |
| 250 | + | |
| 251 | + | |
| 252 | + /** | |
| 253 | + * Put an object | |
| 254 | + * | |
| 255 | + * @param mixed $input Input data | |
| 256 | + * @param string $bucket Bucket name | |
| 257 | + * @param string $uri Object URI | |
| 258 | + * @param constant $acl ACL constant | |
| 259 | + * @param array $metaHeaders Array of x-amz-meta-* headers | |
| 260 | + * @param string $contentType Content type | |
| 261 | + * @return boolean | |
| 262 | + */ | |
| 263 | + public static function putObject($input, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $contentType = null) { | |
| 264 | + if ($input == false) return false; | |
| 265 | + $rest = new S3Request('PUT', $bucket, $uri); | |
| 266 | + | |
| 267 | + if (is_string($input)) $input = array( | |
| 268 | + 'data' => $input, 'size' => strlen($input), | |
| 269 | + 'md5sum' => base64_encode(md5($input, true)) | |
| 270 | + ); | |
| 271 | + | |
| 272 | + // Data | |
| 273 | + if (isset($input['fp'])) | |
| 274 | + $rest->fp =& $input['fp']; | |
| 275 | + elseif (isset($input['file'])) | |
| 276 | + $rest->fp = @fopen($input['file'], 'rb'); | |
| 277 | + elseif (isset($input['data'])) | |
| 278 | + $rest->data = $input['data']; | |
| 279 | + | |
| 280 | + // Content-Length (required) | |
| 281 | + if (isset($input['size']) && $input['size'] > 0) | |
| 282 | + $rest->size = $input['size']; | |
| 283 | + else { | |
| 284 | + if (isset($input['file'])) | |
| 285 | + $rest->size = filesize($input['file']); | |
| 286 | + elseif (isset($input['data'])) | |
| 287 | + $rest->size = strlen($input['data']); | |
| 288 | + } | |
| 289 | + | |
| 290 | + // Content-Type | |
| 291 | + if ($contentType !== null) | |
| 292 | + $input['type'] = $contentType; | |
| 293 | + elseif (!isset($input['type']) && isset($input['file'])) | |
| 294 | + $input['type'] = self::__getMimeType($input['file']); | |
| 295 | + else | |
| 296 | + $input['type'] = 'application/octet-stream'; | |
| 297 | + | |
| 298 | + // We need to post with the content-length and content-type, MD5 is optional | |
| 299 | + if ($rest->size > 0 && ($rest->fp !== false || $rest->data !== false)) { | |
| 300 | + $rest->setHeader('Content-Type', $input['type']); | |
| 301 | + if (isset($input['md5sum'])) $rest->setHeader('Content-MD5', $input['md5sum']); | |
| 302 | + | |
| 303 | + $rest->setAmzHeader('x-amz-acl', $acl); | |
| 304 | + foreach ($metaHeaders as $h => $v) $rest->setAmzHeader('x-amz-meta-'.$h, $v); | |
| 305 | + $rest->getResponse(); | |
| 306 | + } else | |
| 307 | + $rest->response->error = array('code' => 0, 'message' => 'Missing input parameters'); | |
| 308 | + | |
| 309 | + if ($rest->response->error === false && $rest->response->code !== 200) | |
| 310 | + $rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status'); | |
| 311 | + if ($rest->response->error !== false) { | |
| 312 | + trigger_error(sprintf("S3::putObject(): [%s] %s", $rest->response->error['code'], $rest->response->error['message']), E_USER_WARNING); | |
| 313 | + return false; | |
| 314 | + } | |
| 315 | + return true; | |
| 316 | + } | |
| 317 | + | |
| 318 | + | |
| 319 | + /** | |
| 320 | + * Puts an object from a file (legacy function) | |
| 321 | + * | |
| 322 | + * @param string $file Input file path | |
| 323 | + * @param string $bucket Bucket name | |
| 324 | + * @param string $uri Object URI | |
| 325 | + * @param constant $acl ACL constant | |
| 326 | + * @param array $metaHeaders Array of x-amz-meta-* headers | |
| 327 | + * @param string $contentType Content type | |
| 328 | + * @return boolean | |
| 329 | + */ | |
| 330 | + public static function putObjectFile($file, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $contentType = null) { | |
| 331 | + return self::putObject(S3::inputFile($file), $bucket, $uri, $acl, $metaHeaders, $contentType); | |
| 332 | + } | |
| 333 | + | |
| 334 | + | |
| 335 | + /** | |
| 336 | + * Put an object from a string (legacy function) | |
| 337 | + * | |
| 338 | + * @param string $string Input data | |
| 339 | + * @param string $bucket Bucket name | |
| 340 | + * @param string $uri Object URI | |
| 341 | + * @param constant $acl ACL constant | |
| 342 | + * @param array $metaHeaders Array of x-amz-meta-* headers | |
| 343 | + * @param string $contentType Content type | |
| 344 | + * @return boolean | |
| 345 | + */ | |
| 346 | + public function putObjectString($string, $bucket, $uri, $acl = self::ACL_PRIVATE, $metaHeaders = array(), $contentType = 'text/plain') { | |
| 347 | + return self::putObject($string, $bucket, $uri, $acl, $metaHeaders, $contentType); | |
| 348 | + } | |
| 349 | + | |
| 350 | + | |
| 351 | + /** | |
| 352 | + * Get an object | |
| 353 | + * | |
| 354 | + * @param string $bucket Bucket name | |
| 355 | + * @param string $uri Object URI | |
| 356 | + * @param mixed &$saveTo Filename or resource to write to | |
| 357 | + * @return mixed | |
| 358 | + */ | |
| 359 | + public static function getObject($bucket = '', $uri = '', $saveTo = false) { | |
| 360 | + $rest = new S3Request('GET', $bucket, $uri); | |
| 361 | + if ($saveTo !== false) { | |
| 362 | + if (is_resource($saveTo)) | |
| 363 | + $rest->fp =& $saveTo; | |
| 364 | + else | |
| 365 | + if (($rest->fp = @fopen($saveTo, 'wb')) == false) | |
| 366 | + $rest->response->error = array('code' => 0, 'message' => 'Unable to open save file for writing: '.$saveTo); | |
| 367 | + } | |
| 368 | + if ($rest->response->error === false) $rest->getResponse(); | |
| 369 | + | |
| 370 | + if ($rest->response->error === false && $rest->response->code !== 200) | |
| 371 | + $rest->response->error = array('code' => $rest->response->code, 'message' => 'Unexpected HTTP status'); | |
| 372 | + if ($rest->response->error !== false) { | |
| 373 | + trigger_error(sprintf("S3::getObject({$bucket}, {$uri}): [%s] %s", | |
| 374 | + $rest->response->error['code'], $rest->response->error['message']), E_USER_WARNING); | |
| 375 | + return false; | |
| 376 | + } | |
| 377 | + $rest->file = realpath($saveTo); | |
| 378 | + return $rest->response; | |
| 379 | + } | |
| 380 | + | |
| 381 | + | |
| 382 | + /** | |
| 383 | + * Get object information | |
| 384 | + * | |
| 385 | + * @param string $bucket Bucket name | |
| 386 | + * @param string $uri Object URI | |
| 387 | + * @param boolean $returnInfo Return response information | |
| 388 | + * @return mixed | false | |
| 389 | + */ | |
| 390 | + public static function getObjectInfo($bucket = '', $uri = '', $returnInfo = true) { | |
| 391 | + $rest = new S3Request('HEAD', $bucket, $uri); | |
| 392 | + $rest = $rest->getResponse(); | |
| 393 | + if ($rest->error === false && ($rest->code !== 200 && $rest->code !== 404)) | |
| 394 | + $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |
| 395 | + if ($rest->error !== false) { | |
| 396 | + trigger_error(sprintf("S3::getObjectInfo({$bucket}, {$uri}): [%s] %s", | |
| 397 | + $rest->error['code'], $rest->error['message']), E_USER_WARNING); | |
| 398 | + return false; | |
| 399 | + } | |
| 400 | + return $rest->code == 200 ? $returnInfo ? $rest->headers : true : false; | |
| 401 | + } | |
| 402 | + | |
| 403 | + | |
| 404 | + /** | |
| 405 | + * Set logging for a bucket | |
| 406 | + * | |
| 407 | + * @param string $bucket Bucket name | |
| 408 | + * @param string $targetBucket Target bucket (where logs are stored) | |
| 409 | + * @param string $targetPrefix Log prefix (e,g; domain.com-) | |
| 410 | + * @return boolean | |
| 411 | + */ | |
| 412 | + public static function setBucketLogging($bucket, $targetBucket, $targetPrefix) { | |
| 413 | + $dom = new DOMDocument; | |
| 414 | + $bucketLoggingStatus = $dom->createElement('BucketLoggingStatus'); | |
| 415 | + $bucketLoggingStatus->setAttribute('xmlns', 'http://s3.amazonaws.com/doc/2006-03-01/'); | |
| 416 | + | |
| 417 | + $loggingEnabled = $dom->createElement('LoggingEnabled'); | |
| 418 | + | |
| 419 | + $loggingEnabled->appendChild($dom->createElement('TargetBucket', $targetBucket)); | |
| 420 | + $loggingEnabled->appendChild($dom->createElement('TargetPrefix', $targetPrefix)); | |
| 421 | + | |
| 422 | + // TODO: Add TargetGrants | |
| 423 | + | |
| 424 | + $bucketLoggingStatus->appendChild($loggingEnabled); | |
| 425 | + $dom->appendChild($bucketLoggingStatus); | |
| 426 | + | |
| 427 | + $rest = new S3Request('PUT', $bucket, ''); | |
| 428 | + $rest->setParameter('logging', null); | |
| 429 | + $rest->data = $dom->saveXML(); | |
| 430 | + $rest->size = strlen($rest->data); | |
| 431 | + $rest->setHeader('Content-Type', 'application/xml'); | |
| 432 | + $rest = $rest->getResponse(); | |
| 433 | + if ($rest->error === false && $rest->code !== 200) | |
| 434 | + $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |
| 435 | + if ($rest->error !== false) { | |
| 436 | + trigger_error(sprintf("S3::setBucketLogging({$bucket}, {$uri}): [%s] %s", | |
| 437 | + $rest->error['code'], $rest->error['message']), E_USER_WARNING); | |
| 438 | + return false; | |
| 439 | + } | |
| 440 | + return true; | |
| 441 | + } | |
| 442 | + | |
| 443 | + | |
| 444 | + /** | |
| 445 | + * Get logging status for a bucket | |
| 446 | + * | |
| 447 | + * This will return false if logging is not enabled. | |
| 448 | + * Note: To enable logging, you also need to grant write access to the log group | |
| 449 | + * | |
| 450 | + * @param string $bucket Bucket name | |
| 451 | + * @return array | false | |
| 452 | + */ | |
| 453 | + public static function getBucketLogging($bucket = '') { | |
| 454 | + $rest = new S3Request('GET', $bucket, ''); | |
| 455 | + $rest->setParameter('logging', null); | |
| 456 | + $rest = $rest->getResponse(); | |
| 457 | + if ($rest->error === false && $rest->code !== 200) | |
| 458 | + $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |
| 459 | + if ($rest->error !== false) { | |
| 460 | + trigger_error(sprintf("S3::getBucketLogging({$bucket}): [%s] %s", | |
| 461 | + $rest->error['code'], $rest->error['message']), E_USER_WARNING); | |
| 462 | + return false; | |
| 463 | + } | |
| 464 | + if (!isset($rest->body->LoggingEnabled)) return false; // No logging | |
| 465 | + return array( | |
| 466 | + 'targetBucket' => (string)$rest->body->LoggingEnabled->TargetBucket, | |
| 467 | + 'targetPrefix' => (string)$rest->body->LoggingEnabled->TargetPrefix, | |
| 468 | + ); | |
| 469 | + } | |
| 470 | + | |
| 471 | + | |
| 472 | + /** | |
| 473 | + * Set object or bucket Access Control Policy | |
| 474 | + * | |
| 475 | + * @param string $bucket Bucket name | |
| 476 | + * @param string $uri Object URI | |
| 477 | + * @param array $acp Access Control Policy Data (same as the data returned from getAccessControlPolicy) | |
| 478 | + * @return boolean | |
| 479 | + */ | |
| 480 | + public static function setAccessControlPolicy($bucket, $uri = '', $acp = array()) { | |
| 481 | + $dom = new DOMDocument; | |
| 482 | + $dom->formatOutput = true; | |
| 483 | + $accessControlPolicy = $dom->createElement('AccessControlPolicy'); | |
| 484 | + $accessControlList = $dom->createElement('AccessControlList'); | |
| 485 | + | |
| 486 | + // It seems the owner has to be passed along too | |
| 487 | + $owner = $dom->createElement('Owner'); | |
| 488 | + $owner->appendChild($dom->createElement('ID', $acp['owner']['id'])); | |
| 489 | + $owner->appendChild($dom->createElement('DisplayName', $acp['owner']['name'])); | |
| 490 | + $accessControlPolicy->appendChild($owner); | |
| 491 | + | |
| 492 | + foreach ($acp['acl'] as $g) { | |
| 493 | + $grant = $dom->createElement('Grant'); | |
| 494 | + $grantee = $dom->createElement('Grantee'); | |
| 495 | + $grantee->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); | |
| 496 | + if (isset($g['id'])) { // CanonicalUser (DisplayName is omitted) | |
| 497 | + $grantee->setAttribute('xsi:type', 'CanonicalUser'); | |
| 498 | + $grantee->appendChild($dom->createElement('ID', $g['id'])); | |
| 499 | + } elseif (isset($g['email'])) { // AmazonCustomerByEmail | |
| 500 | + $grantee->setAttribute('xsi:type', 'AmazonCustomerByEmail'); | |
| 501 | + $grantee->appendChild($dom->createElement('EmailAddress', $g['email'])); | |
| 502 | + } elseif ($g['type'] == 'Group') { // Group | |
| 503 | + $grantee->setAttribute('xsi:type', 'Group'); | |
| 504 | + $grantee->appendChild($dom->createElement('URI', $g['uri'])); | |
| 505 | + } | |
| 506 | + $grant->appendChild($grantee); | |
| 507 | + $grant->appendChild($dom->createElement('Permission', $g['permission'])); | |
| 508 | + $accessControlList->appendChild($grant); | |
| 509 | + } | |
| 510 | + | |
| 511 | + $accessControlPolicy->appendChild($accessControlList); | |
| 512 | + $dom->appendChild($accessControlPolicy); | |
| 513 | + | |
| 514 | + $rest = new S3Request('PUT', $bucket, ''); | |
| 515 | + $rest->setParameter('acl', null); | |
| 516 | + $rest->data = $dom->saveXML(); | |
| 517 | + $rest->size = strlen($rest->data); | |
| 518 | + $rest->setHeader('Content-Type', 'application/xml'); | |
| 519 | + $rest = $rest->getResponse(); | |
| 520 | + if ($rest->error === false && $rest->code !== 200) | |
| 521 | + $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |
| 522 | + if ($rest->error !== false) { | |
| 523 | + trigger_error(sprintf("S3::setAccessControlPolicy({$bucket}, {$uri}): [%s] %s", | |
| 524 | + $rest->error['code'], $rest->error['message']), E_USER_WARNING); | |
| 525 | + return false; | |
| 526 | + } | |
| 527 | + return true; | |
| 528 | + } | |
| 529 | + | |
| 530 | + | |
| 531 | + /** | |
| 532 | + * Get object or bucket Access Control Policy | |
| 533 | + * | |
| 534 | + * Currently this will trigger an error if there is no ACL on an object (will fix soon) | |
| 535 | + * | |
| 536 | + * @param string $bucket Bucket name | |
| 537 | + * @param string $uri Object URI | |
| 538 | + * @return mixed | false | |
| 539 | + */ | |
| 540 | + public static function getAccessControlPolicy($bucket, $uri = '') { | |
| 541 | + $rest = new S3Request('GET', $bucket, $uri); | |
| 542 | + $rest->setParameter('acl', null); | |
| 543 | + $rest = $rest->getResponse(); | |
| 544 | + if ($rest->error === false && $rest->code !== 200) | |
| 545 | + $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |
| 546 | + if ($rest->error !== false) { | |
| 547 | + trigger_error(sprintf("S3::getAccessControlPolicy({$bucket}, {$uri}): [%s] %s", | |
| 548 | + $rest->error['code'], $rest->error['message']), E_USER_WARNING); | |
| 549 | + return false; | |
| 550 | + } | |
| 551 | + | |
| 552 | + $acp = array(); | |
| 553 | + if (isset($rest->body->Owner, $rest->body->Owner->ID, $rest->body->Owner->DisplayName)) { | |
| 554 | + $acp['owner'] = array( | |
| 555 | + 'id' => (string)$rest->body->Owner->ID, 'name' => (string)$rest->body->Owner->DisplayName | |
| 556 | + ); | |
| 557 | + } | |
| 558 | + if (isset($rest->body->AccessControlList)) { | |
| 559 | + $acp['acl'] = array(); | |
| 560 | + foreach ($rest->body->AccessControlList->Grant as $grant) { | |
| 561 | + foreach ($grant->Grantee as $grantee) { | |
| 562 | + if (isset($grantee->ID, $grantee->DisplayName)) // CanonicalUser | |
| 563 | + $acp['acl'][] = array( | |
| 564 | + 'type' => 'CanonicalUser', | |
| 565 | + 'id' => (string)$grantee->ID, | |
| 566 | + 'name' => (string)$grantee->DisplayName, | |
| 567 | + 'permission' => (string)$grant->Permission | |
| 568 | + ); | |
| 569 | + elseif (isset($grantee->EmailAddress)) // AmazonCustomerByEmail | |
| 570 | + $acp['acl'][] = array( | |
| 571 | + 'type' => 'AmazonCustomerByEmail', | |
| 572 | + 'email' => (string)$grantee->EmailAddress, | |
| 573 | + 'permission' => (string)$grant->Permission | |
| 574 | + ); | |
| 575 | + elseif (isset($grantee->URI)) // Group | |
| 576 | + $acp['acl'][] = array( | |
| 577 | + 'type' => 'Group', | |
| 578 | + 'uri' => (string)$grantee->URI, | |
| 579 | + 'permission' => (string)$grant->Permission | |
| 580 | + ); | |
| 581 | + else continue; | |
| 582 | + } | |
| 583 | + } | |
| 584 | + } | |
| 585 | + return $acp; | |
| 586 | + } | |
| 587 | + | |
| 588 | + | |
| 589 | + /** | |
| 590 | + * Delete an object | |
| 591 | + * | |
| 592 | + * @param string $bucket Bucket name | |
| 593 | + * @param string $uri Object URI | |
| 594 | + * @return mixed | |
| 595 | + */ | |
| 596 | + public static function deleteObject($bucket = '', $uri = '') { | |
| 597 | + $rest = new S3Request('DELETE', $bucket, $uri); | |
| 598 | + $rest = $rest->getResponse(); | |
| 599 | + if ($rest->error === false && $rest->code !== 204) | |
| 600 | + $rest->error = array('code' => $rest->code, 'message' => 'Unexpected HTTP status'); | |
| 601 | + if ($rest->error !== false) { | |
| 602 | + trigger_error(sprintf("S3::deleteObject(): [%s] %s", $rest->error['code'], $rest->error['message']), E_USER_WARNING); | |
| 603 | + return false; | |
| 604 | + } | |
| 605 | + return true; | |
| 606 | + } | |
| 607 | + | |
| 608 | + | |
| 609 | + /** | |
| 610 | + * Get MIME type for file | |
| 611 | + * | |
| 612 | + * @internal Used to get mime types | |
| 613 | + * @param string &$file File path | |
| 614 | + * @return string | |
| 615 | + */ | |
| 616 | + public static function __getMimeType(&$file) { | |
| 617 | + $type = false; | |
| 618 | + // Fileinfo documentation says fileinfo_open() will use the | |
| 619 | + // MAGIC env var for the magic file | |
| 620 | + if (extension_loaded('fileinfo') && isset($_ENV['MAGIC']) && | |
| 621 | + ($finfo = finfo_open(FILEINFO_MIME, $_ENV['MAGIC'])) !== false) { | |
| 622 | + if (($type = finfo_file($finfo, $file)) !== false) { | |
| 623 | + // Remove the charset and grab the last content-type | |
| 624 | + $type = explode(' ', str_replace('; charset=', ';charset=', $type)); | |
| 625 | + $type = array_pop($type); | |
| 626 | + $type = explode(';', $type); | |
| 627 | + $type = array_shift($type); | |
| 628 | + } | |
| 629 | + finfo_close($finfo); | |
| 630 | + | |
| 631 | + // If anyone is still using mime_content_type() | |
| 632 | + } elseif (function_exists('mime_content_type')) | |
| 633 | + $type = mime_content_type($file); | |
| 634 | + | |
| 635 | + if ($type !== false && strlen($type) > 0) return $type; | |
| 636 | + | |
| 637 | + // Otherwise do it the old fashioned way | |
| 638 | + static $exts = array( | |
| 639 | + 'jpg' => 'image/jpeg', 'gif' => 'image/gif', 'png' => 'image/png', | |
| 640 | + 'tif' => 'image/tiff', 'tiff' => 'image/tiff', 'ico' => 'image/x-icon', | |
| 641 | + 'swf' => 'application/x-shockwave-flash', 'pdf' => 'application/pdf', | |
| 642 | + 'zip' => 'application/zip', 'gz' => 'application/x-gzip', | |
| 643 | + 'tar' => 'application/x-tar', 'bz' => 'application/x-bzip', | |
| 644 | + 'bz2' => 'application/x-bzip2', 'txt' => 'text/plain', | |
| 645 | + 'asc' => 'text/plain', 'htm' => 'text/html', 'html' => 'text/html', | |
| 646 | + 'xml' => 'text/xml', 'xsl' => 'application/xsl+xml', | |
| 647 | + 'ogg' => 'application/ogg', 'mp3' => 'audio/mpeg', 'wav' => 'audio/x-wav', | |
| 648 | + 'avi' => 'video/x-msvideo', 'mpg' => 'video/mpeg', 'mpeg' => 'video/mpeg', | |
| 649 | + 'mov' => 'video/quicktime', 'flv' => 'video/x-flv', 'php' => 'text/x-php' | |
| 650 | + ); | |
| 651 | + $ext = strToLower(pathInfo($file, PATHINFO_EXTENSION)); | |
| 652 | + return isset($exts[$ext]) ? $exts[$ext] : 'application/octet-stream'; | |
| 653 | + } | |
| 654 | + | |
| 655 | + | |
| 656 | + /** | |
| 657 | + * Generate the auth string: "AWS AccessKey:Signature" | |
| 658 | + * | |
| 659 | + * This uses the hash extension if loaded | |
| 660 | + * | |
| 661 | + * @internal Signs the request | |
| 662 | + * @param string $string String to sign | |
| 663 | + * @return string | |
| 664 | + */ | |
| 665 | + public static function __getSignature($string) { | |
| 666 | + return 'AWS '.self::$__accessKey.':'.base64_encode(extension_loaded('hash') ? | |
| 667 | + hash_hmac('sha1', $string, self::$__secretKey, true) : pack('H*', sha1( | |
| 668 | + (str_pad(self::$__secretKey, 64, chr(0x00)) ^ (str_repeat(chr(0x5c), 64))) . | |
| 669 | + pack('H*', sha1((str_pad(self::$__secretKey, 64, chr(0x00)) ^ | |
| 670 | + (str_repeat(chr(0x36), 64))) . $string))))); | |
| 671 | + } | |
| 672 | + | |
| 673 | + | |
| 674 | +} | |
| 675 | + | |
| 676 | +final class S3Request { | |
| 677 | + private $verb, $bucket, $uri, $resource = '', $parameters = array(), | |
| 678 | + $amzHeaders = array(), $headers = array( | |
| 679 | + 'Host' => '', 'Date' => '', 'Content-MD5' => '', 'Content-Type' => '' | |
| 680 | + ); | |
| 681 | + public $fp = false, $size = 0, $data = false, $response; | |
| 682 | + | |
| 683 | + | |
| 684 | + /** | |
| 685 | + * Constructor | |
| 686 | + * | |
| 687 | + * @param string $verb Verb | |
| 688 | + * @param string $bucket Bucket name | |
| 689 | + * @param string $uri Object URI | |
| 690 | + * @return mixed | |
| 691 | + */ | |
| 692 | + function __construct($verb, $bucket = '', $uri = '') { | |
| 693 | + $this->verb = $verb; | |
| 694 | + $this->bucket = strtolower($bucket); | |
| 695 | + $this->uri = $uri !== '' ? '/'.$uri : '/'; | |
| 696 | + | |
| 697 | + if ($this->bucket !== '') { | |
| 698 | + $this->bucket = explode('/', $this->bucket); | |
| 699 | + $this->resource = '/'.$this->bucket[0].$this->uri; | |
| 700 | + $this->headers['Host'] = $this->bucket[0].'.s3.amazonaws.com'; | |
| 701 | + $this->bucket = implode('/', $this->bucket); | |
| 702 | + } else { | |
| 703 | + $this->headers['Host'] = 's3.amazonaws.com'; | |
| 704 | + if (strlen($this->uri) > 1) | |
| 705 | + $this->resource = '/'.$this->bucket.$this->uri; | |
| 706 | + else $this->resource = $this->uri; | |
| 707 | + } | |
| 708 | + $this->headers['Date'] = gmdate('D, d M Y H:i:s T'); | |
| 709 | + | |
| 710 | + $this->response = new STDClass; | |
| 711 | + $this->response->error = false; | |
| 712 | + } | |
| 713 | + | |
| 714 | + | |
| 715 | + /** | |
| 716 | + * Set request parameter | |
| 717 | + * | |
| 718 | + * @param string $key Key | |
| 719 | + * @param string $value Value | |
| 720 | + * @return void | |
| 721 | + */ | |
| 722 | + public function setParameter($key, $value) { | |
| 723 | + $this->parameters[$key] = $value; | |
| 724 | + } | |
| 725 | + | |
| 726 | + | |
| 727 | + /** | |
| 728 | + * Set request header | |
| 729 | + * | |
| 730 | + * @param string $key Key | |
| 731 | + * @param string $value Value | |
| 732 | + * @return void | |
| 733 | + */ | |
| 734 | + public function setHeader($key, $value) { | |
| 735 | + $this->headers[$key] = $value; | |
| 736 | + } | |
| 737 | + | |
| 738 | + | |
| 739 | + /** | |
| 740 | + * Set x-amz-meta-* header | |
| 741 | + * | |
| 742 | + * @param string $key Key | |
| 743 | + * @param string $value Value | |
| 744 | + * @return void | |
| 745 | + */ | |
| 746 | + public function setAmzHeader($key, $value) { | |
| 747 | + $this->amzHeaders[$key] = $value; | |
| 748 | + } | |
| 749 | + | |
| 750 | + | |
| 751 | + /** | |
| 752 | + * Get the S3 response | |
| 753 | + * | |
| 754 | + * @return object | false | |
| 755 | + */ | |
| 756 | + public function getResponse() { | |
| 757 | + $query = ''; | |
| 758 | + if (sizeof($this->parameters) > 0) { | |
| 759 | + $query = substr($this->uri, -1) !== '?' ? '?' : '&'; | |
| 760 | + foreach ($this->parameters as $var => $value) | |
| 761 | + if ($value == null || $value == '') $query .= $var.'&'; | |
| 762 | + else $query .= $var.'='.$value.'&'; | |
| 763 | + $query = substr($query, 0, -1); | |
| 764 | + $this->uri .= $query; | |
| 765 | + if (isset($this->parameters['acl']) || !isset($this->parameters['logging'])) | |
| 766 | + $this->resource .= $query; | |
| 767 | + } | |
| 768 | + $url = (extension_loaded('openssl')?'https://':'http://').$this->headers['Host'].$this->uri; | |
| 769 | + //var_dump($this->bucket, $this->uri, $this->resource, $url); | |
| 770 | + | |
| 771 | + | |
| 772 | + // Basic setup | |
| 773 | + $curl = curl_init(); | |
| 774 | + curl_setopt($curl, CURLOPT_USERAGENT, 'S3/php'); | |
| 775 | + curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); | |
| 776 | + curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); | |
| 777 | + curl_setopt($curl, CURLOPT_URL, $url); | |
| 778 | + | |
| 779 | + // Headers | |
| 780 | + $headers = array(); $amz = array(); | |
| 781 | + foreach ($this->amzHeaders as $header => $value) | |
| 782 | + if (strlen($value) > 0) $headers[] = $header.': '.$value; | |
| 783 | + foreach ($this->headers as $header => $value) | |
| 784 | + if (strlen($value) > 0) $headers[] = $header.': '.$value; | |
| 785 | + foreach ($this->amzHeaders as $header => $value) | |
| 786 | + if (strlen($value) > 0) $amz[] = strToLower($header).':'.$value; | |
| 787 | + $amz = (sizeof($amz) > 0) ? "\n".implode("\n", $amz) : ''; | |
| 788 | + | |
| 789 | + // Authorization string | |
| 790 | + $headers[] = 'Authorization: ' . S3::__getSignature( | |
| 791 | + $this->verb."\n". | |
| 792 | + $this->headers['Content-MD5']."\n". | |
| 793 | + $this->headers['Content-Type']."\n". | |
| 794 | + $this->headers['Date'].$amz."\n".$this->resource | |
| 795 | + ); | |
| 796 | + | |
| 797 | + curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); | |
| 798 | + curl_setopt($curl, CURLOPT_HEADER, false); | |
| 799 | + curl_setopt($curl, CURLOPT_RETURNTRANSFER, false); | |
| 800 | + curl_setopt($curl, CURLOPT_WRITEFUNCTION, array(&$this, '__responseWriteCallback')); | |
| 801 | + curl_setopt($curl, CURLOPT_HEADERFUNCTION, array(&$this, '__responseHeaderCallback')); | |
| 802 | + | |
| 803 | + // Request types | |
| 804 | + switch ($this->verb) { | |
| 805 | + case 'GET': break; | |
| 806 | + case 'PUT': | |
| 807 | + if ($this->fp !== false) { | |
| 808 | + curl_setopt($curl, CURLOPT_PUT, true); | |
| 809 | + curl_setopt($curl, CURLOPT_INFILE, $this->fp); | |
| 810 | + if ($this->size > 0) | |
| 811 | + curl_setopt($curl, CURLOPT_INFILESIZE, $this->size); | |
| 812 | + } elseif ($this->data !== false) { | |
| 813 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
| 814 | + curl_setopt($curl, CURLOPT_POSTFIELDS, $this->data); | |
| 815 | + if ($this->size > 0) | |
| 816 | + curl_setopt($curl, CURLOPT_BUFFERSIZE, $this->size); | |
| 817 | + } else | |
| 818 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT'); | |
| 819 | + break; | |
| 820 | + case 'HEAD': | |
| 821 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD'); | |
| 822 | + curl_setopt($curl, CURLOPT_NOBODY, true); | |
| 823 | + break; | |
| 824 | + case 'DELETE': | |
| 825 | + curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE'); | |
| 826 | + break; | |
| 827 | + default: break; | |
| 828 | + } | |
| 829 | + | |
| 830 | + // Execute, grab errors | |
| 831 | + if (curl_exec($curl)) | |
| 832 | + $this->response->code = curl_getinfo($curl, CURLINFO_HTTP_CODE); | |
| 833 | + else | |
| 834 | + $this->response->error = array( | |
| 835 | + 'code' => curl_errno($curl), | |
| 836 | + 'message' => curl_error($curl), | |
| 837 | + 'resource' => $this->resource | |
| 838 | + ); | |
| 839 | + | |
| 840 | + @curl_close($curl); | |
| 841 | + | |
| 842 | + // Parse body into XML | |
| 843 | + if ($this->response->error === false && isset($this->response->headers['type']) && | |
| 844 | + $this->response->headers['type'] == 'application/xml' && isset($this->response->body)) { | |
| 845 | + $this->response->body = simplexml_load_string($this->response->body); | |
| 846 | + | |
| 847 | + // Grab S3 errors | |
| 848 | + if (!in_array($this->response->code, array(200, 204)) && | |
| 849 | + isset($this->response->body->Code, $this->response->body->Message)) { | |
| 850 | + $this->response->error = array( | |
| 851 | + 'code' => (string)$this->response->body->Code, | |
| 852 | + 'message' => (string)$this->response->body->Message | |
| 853 | + ); | |
| 854 | + if (isset($this->response->body->Resource)) | |
| 855 | + $this->response->error['resource'] = (string)$this->response->body->Resource; | |
| 856 | + unset($this->response->body); | |
| 857 | + } | |
| 858 | + } | |
| 859 | + | |
| 860 | + // Clean up file resources | |
| 861 | + if ($this->fp !== false && is_resource($this->fp)) fclose($this->fp); | |
| 862 | + | |
| 863 | + return $this->response; | |
| 864 | + } | |
| 865 | + | |
| 866 | + | |
| 867 | + /** | |
| 868 | + * CURL write callback | |
| 869 | + * | |
| 870 | + * @param resource &$curl CURL resource | |
| 871 | + * @param string &$data Data | |
| 872 | + * @return integer | |
| 873 | + */ | |
| 874 | + private function __responseWriteCallback(&$curl, &$data) { | |
| 875 | + if ($this->response->code == 200 && $this->fp !== false) | |
| 876 | + return fwrite($this->fp, $data); | |
| 877 | + else | |
| 878 | + $this->response->body .= $data; | |
| 879 | + return strlen($data); | |
| 880 | + } | |
| 881 | + | |
| 882 | + | |
| 883 | + /** | |
| 884 | + * CURL header callback | |
| 885 | + * | |
| 886 | + * @param resource &$curl CURL resource | |
| 887 | + * @param string &$data Data | |
| 888 | + * @return integer | |
| 889 | + */ | |
| 890 | + private function __responseHeaderCallback(&$curl, &$data) { | |
| 891 | + if (($strlen = strlen($data)) <= 2) return $strlen; | |
| 892 | + if (substr($data, 0, 4) == 'HTTP') | |
| 893 | + $this->response->code = (int)substr($data, 9, 3); | |
| 894 | + else { | |
| 895 | + list($header, $value) = explode(': ', trim($data)); | |
| 896 | + if ($header == 'Last-Modified') | |
| 897 | + $this->response->headers['time'] = strtotime($value); | |
| 898 | + elseif ($header == 'Content-Length') | |
| 899 | + $this->response->headers['size'] = (int)$value; | |
| 900 | + elseif ($header == 'Content-Type') | |
| 901 | + $this->response->headers['type'] = $value; | |
| 902 | + elseif ($header == 'ETag') | |
| 903 | + $this->response->headers['hash'] = substr($value, 1, -1); | |
| 904 | + elseif (preg_match('/^x-amz-meta-.*$/', $header)) | |
| 905 | + $this->response->headers[$header] = is_numeric($value) ? (int)$value : $value; | |
| 906 | + } | |
| 907 | + return $strlen; | |
| 908 | + } | |
| 909 | + | |
| 910 | +} | |
| 911 | +?> | |
| 0 | 912 | \ No newline at end of file | ... | ... |
ieducar/intranet/atendidos_cad.php
| ... | ... | @@ -37,6 +37,7 @@ require_once 'include/clsCadastro.inc.php'; |
| 37 | 37 | require_once 'include/pessoa/clsCadastroRaca.inc.php'; |
| 38 | 38 | require_once 'include/pessoa/clsCadastroFisicaRaca.inc.php'; |
| 39 | 39 | require_once 'include/pmieducar/clsPmieducarAluno.inc.php'; |
| 40 | +require_once 'include/pessoa/clsCadastroFisicaFoto.inc.php'; | |
| 40 | 41 | |
| 41 | 42 | require_once 'App/Model/ZonaLocalizacao.php'; |
| 42 | 43 | |
| ... | ... | @@ -44,6 +45,7 @@ require_once 'Portabilis/String/Utils.php'; |
| 44 | 45 | require_once 'Portabilis/View/Helper/Application.php'; |
| 45 | 46 | require_once 'Portabilis/Utils/Validation.php'; |
| 46 | 47 | require_once 'Portabilis/Date/Utils.php'; |
| 48 | +require_once 'image_check.php'; | |
| 47 | 49 | |
| 48 | 50 | /** |
| 49 | 51 | * clsIndex class. |
| ... | ... | @@ -109,6 +111,10 @@ class indice extends clsCadastro |
| 109 | 111 | var $caminho_det; |
| 110 | 112 | var $caminho_lst; |
| 111 | 113 | |
| 114 | + // Variáveis para controle da foto | |
| 115 | + var $objPhoto; | |
| 116 | + var $arquivoFoto; | |
| 117 | + | |
| 112 | 118 | function Inicializar() |
| 113 | 119 | { |
| 114 | 120 | $this->cod_pessoa_fj = @$_GET['cod_pessoa_fj']; |
| ... | ... | @@ -167,6 +173,21 @@ class indice extends clsCadastro |
| 167 | 173 | $this->campoOculto('cod_pessoa_fj', $this->cod_pessoa_fj); |
| 168 | 174 | $this->campoTexto('nm_pessoa', 'Nome', $this->nm_pessoa, '50', '255', TRUE); |
| 169 | 175 | |
| 176 | + $foto = false; | |
| 177 | + if (is_numeric($this->cod_pessoa_fj)){ | |
| 178 | + $objFoto = new ClsCadastroFisicaFoto($this->cod_pessoa_fj); | |
| 179 | + $detalheFoto = $objFoto->detalhe(); | |
| 180 | + if(count($detalheFoto)) | |
| 181 | + $foto = $detalheFoto['caminho']; | |
| 182 | + } else | |
| 183 | + $foto=false; | |
| 184 | + | |
| 185 | + if ($foto!=false){ | |
| 186 | + $this->campoRotulo('fotoAtual_','Foto atual','<img height="117" src="'.$foto.'"/>'); | |
| 187 | + $this->campoArquivo('file','Trocar foto',$this->arquivoFoto,40,'<br/> <span style="font-style: italic; font-size= 10px;">* Recomenda-se imagens nos formatos jpeg, jpg, png e gif. Tamanho máximo: 150KB</span>'); | |
| 188 | + }else | |
| 189 | + $this->campoArquivo('file','Foto',$this->arquivoFoto,40,'<br/> <span style="font-style: italic; font-size= 10px;">* Recomenda-se imagens nos formatos jpeg, jpg, png e gif. Tamanho máximo: 150KB</span>'); | |
| 190 | + | |
| 170 | 191 | |
| 171 | 192 | // ao cadastrar pessoa do pai ou mãe apartir do cadastro de outra pessoa, |
| 172 | 193 | // é enviado o tipo de cadastro (pai ou mae). |
| ... | ... | @@ -925,8 +946,12 @@ class indice extends clsCadastro |
| 925 | 946 | if (! $this->validatesCpf($this->id_federal)) |
| 926 | 947 | return false; |
| 927 | 948 | |
| 949 | + if (!$this->validatePhoto()) | |
| 950 | + return false; | |
| 951 | + | |
| 928 | 952 | $pessoaId = $this->createOrUpdatePessoa($pessoaIdOrNull); |
| 929 | 953 | |
| 954 | + $this->savePhoto($pessoaId); | |
| 930 | 955 | $this->createOrUpdatePessoaFisica($pessoaId); |
| 931 | 956 | $this->createOrUpdateDocumentos($pessoaId); |
| 932 | 957 | $this->createOrUpdateTelefones($pessoaId); |
| ... | ... | @@ -936,6 +961,52 @@ class indice extends clsCadastro |
| 936 | 961 | return true; |
| 937 | 962 | } |
| 938 | 963 | |
| 964 | + | |
| 965 | + //envia foto e salva caminha no banco | |
| 966 | + protected function savePhoto($id){ | |
| 967 | + | |
| 968 | + if ($this->objPhoto!=null){ | |
| 969 | + | |
| 970 | + $caminhoFoto = $this->objPhoto->sendPicture($id); | |
| 971 | + if ($caminhoFoto!=''){ | |
| 972 | + //new clsCadastroFisicaFoto($id)->exclui(); | |
| 973 | + $obj = new clsCadastroFisicaFoto($id,$caminhoFoto); | |
| 974 | + $detalheFoto = $obj->detalhe(); | |
| 975 | + if (is_array($detalheFoto) && count($detalheFoto)>0) | |
| 976 | + $obj->edita(); | |
| 977 | + else | |
| 978 | + $obj->cadastra(); | |
| 979 | + | |
| 980 | + return true; | |
| 981 | + } else{ | |
| 982 | + echo '<script>alert(\'Foto não salva.\')</script>'; | |
| 983 | + return false; | |
| 984 | + } | |
| 985 | + } | |
| 986 | + } | |
| 987 | + | |
| 988 | + // Retorna true caso a foto seja válida | |
| 989 | + protected function validatePhoto(){ | |
| 990 | + | |
| 991 | + $this->arquivoFoto = $_FILES["file"]; | |
| 992 | + if (!empty($this->arquivoFoto["name"])){ | |
| 993 | + $this->objPhoto = new PictureController($this->arquivoFoto); | |
| 994 | + if ($this->objPhoto->validatePicture()){ | |
| 995 | + return TRUE; | |
| 996 | + } else { | |
| 997 | + $this->mensagem = $this->objPhoto->getErrorMessage(); | |
| 998 | + return false; | |
| 999 | + } | |
| 1000 | + return false; | |
| 1001 | + }else{ | |
| 1002 | + $this->objPhoto = null; | |
| 1003 | + return true; | |
| 1004 | + } | |
| 1005 | + | |
| 1006 | + } | |
| 1007 | + | |
| 1008 | + | |
| 1009 | + | |
| 939 | 1010 | protected function createOrUpdatePessoa($pessoaId = null) { |
| 940 | 1011 | $pessoa = new clsPessoa_(); |
| 941 | 1012 | $pessoa->idpes = $pessoaId; | ... | ... |
ieducar/intranet/atendidos_det.php
| ... | ... | @@ -32,6 +32,7 @@ require_once 'include/clsBase.inc.php'; |
| 32 | 32 | require_once 'include/clsDetalhe.inc.php'; |
| 33 | 33 | require_once 'include/clsBanco.inc.php'; |
| 34 | 34 | require_once 'include/pessoa/clsCadastroRaca.inc.php'; |
| 35 | +require_once 'include/pessoa/clsCadastroFisicaFoto.inc.php'; | |
| 35 | 36 | require_once 'include/pessoa/clsCadastroFisicaRaca.inc.php'; |
| 36 | 37 | |
| 37 | 38 | require_once 'App/Model/ZonaLocalizacao.php'; |
| ... | ... | @@ -86,7 +87,15 @@ class indice extends clsDetalhe |
| 86 | 87 | 'ddd_fax', 'fone_fax', 'email', 'url', 'tipo', 'sexo', 'zona_localizacao' |
| 87 | 88 | ); |
| 88 | 89 | |
| 89 | - $this->addDetalhe(array('Nome', $detalhe['nome'])); | |
| 90 | + | |
| 91 | + $objFoto = new clsCadastroFisicaFoto($cod_pessoa); | |
| 92 | + $caminhoFoto = $objFoto->detalhe(); | |
| 93 | + if ($caminhoFoto!=false) | |
| 94 | + $this->addDetalhe(array('Nome', $detalhe['nome'].' | |
| 95 | + <p><img height="117" src="'.$caminhoFoto['caminho'].'"/></p>')); | |
| 96 | + else | |
| 97 | + $this->addDetalhe(array('Nome', $detalhe['nome'])); | |
| 98 | + | |
| 90 | 99 | $this->addDetalhe(array('CPF', int2cpf($detalhe['cpf']))); |
| 91 | 100 | |
| 92 | 101 | if ($detalhe['data_nasc']) { | ... | ... |
ieducar/intranet/educar_aluno_det.php
| ... | ... | @@ -37,6 +37,8 @@ require_once 'App/Model/ZonaLocalizacao.php'; |
| 37 | 37 | require_once 'Educacenso/Model/AlunoDataMapper.php'; |
| 38 | 38 | require_once 'Transporte/Model/AlunoDataMapper.php'; |
| 39 | 39 | |
| 40 | +require_once 'include/pessoa/clsCadastroFisicaFoto.inc.php'; | |
| 41 | + | |
| 40 | 42 | require_once 'Portabilis/View/Helper/Application.php'; |
| 41 | 43 | |
| 42 | 44 | /** |
| ... | ... | @@ -135,6 +137,11 @@ class indice extends clsDetalhe |
| 135 | 137 | $det_raca = $obj_raca->detalhe(); |
| 136 | 138 | } |
| 137 | 139 | |
| 140 | + $objFoto = new clsCadastroFisicaFoto($this->ref_idpes); | |
| 141 | + $detalheFoto = $objFoto->detalhe(); | |
| 142 | + if ($detalheFoto) | |
| 143 | + $caminhoFoto = $detalheFoto['caminho']; | |
| 144 | + | |
| 138 | 145 | $registro['nome_aluno'] = strtoupper($det_pessoa_fj['nome']); |
| 139 | 146 | $registro['cpf'] = int2IdFederal($det_fisica['cpf']); |
| 140 | 147 | $registro['data_nasc'] = dataToBrasil($det_fisica['data_nasc']); |
| ... | ... | @@ -376,7 +383,10 @@ class indice extends clsDetalhe |
| 376 | 383 | } |
| 377 | 384 | |
| 378 | 385 | if ($registro['nome_aluno']) { |
| 379 | - $this->addDetalhe(array('Nome Aluno', $registro['nome_aluno'])); | |
| 386 | + if ($caminhoFoto!=null and $caminhoFoto!='') | |
| 387 | + $this->addDetalhe(array('Nome Aluno', $registro['nome_aluno'].'<p><img height="117" src="'.$caminhoFoto.'"/></p>')); | |
| 388 | + else | |
| 389 | + $this->addDetalhe(array('Nome Aluno', $registro['nome_aluno'])); | |
| 380 | 390 | } |
| 381 | 391 | |
| 382 | 392 | if (idFederal2int($registro['cpf'])) { | ... | ... |
| ... | ... | @@ -0,0 +1,162 @@ |
| 1 | +<?php | |
| 2 | + | |
| 3 | +/** | |
| 4 | + * i-Educar - Sistema de gestão escolar | |
| 5 | + * | |
| 6 | + * Copyright (C) 2006 Prefeitura Municipal de Itajaí | |
| 7 | + * <ctima@itajai.sc.gov.br> | |
| 8 | + * | |
| 9 | + * Este programa é software livre; você pode redistribuí-lo e/ou modificá-lo | |
| 10 | + * sob os termos da Licença Pública Geral GNU conforme publicada pela Free | |
| 11 | + * Software Foundation; tanto a versão 2 da Licença, como (a seu critério) | |
| 12 | + * qualquer versão posterior. | |
| 13 | + * | |
| 14 | + * Este programa é distribuído na expectativa de que seja útil, porém, SEM | |
| 15 | + * NENHUMA GARANTIA; nem mesmo a garantia implícita de COMERCIABILIDADE OU | |
| 16 | + * ADEQUAÇÃO A UMA FINALIDADE ESPECÍFICA. Consulte a Licença Pública Geral | |
| 17 | + * do GNU para mais detalhes. | |
| 18 | + * | |
| 19 | + * Você deve ter recebido uma cópia da Licença Pública Geral do GNU junto | |
| 20 | + * com este programa; se não, escreva para a Free Software Foundation, Inc., no | |
| 21 | + * endereço 59 Temple Street, Suite 330, Boston, MA 02111-1307 USA. | |
| 22 | + * | |
| 23 | + * @author Lucas Schmoeller da Silva <lucas@portabilis.com.br> | |
| 24 | + * @category i-Educar | |
| 25 | + * @license @@license@@ | |
| 26 | + * @package Api | |
| 27 | + * @subpackage Modules | |
| 28 | + * @since Arquivo disponível desde a versão ? | |
| 29 | + * @version $Id$ | |
| 30 | + */ | |
| 31 | + | |
| 32 | +class PictureController { | |
| 33 | + | |
| 34 | + var $imageFile; | |
| 35 | + var $errorMessage; | |
| 36 | + var $maxWidth; | |
| 37 | + var $maxHeight; | |
| 38 | + var $maxSize; | |
| 39 | + var $suportedExtensions; | |
| 40 | + var $imageName; | |
| 41 | + | |
| 42 | + function PictureController($imageFile, $maxWidth = NULL, $maxHeight = NULL, $maxSize = NULL, | |
| 43 | + $suportedExtensions = NULL){ | |
| 44 | + | |
| 45 | + | |
| 46 | + $this->imageFile = $imageFile; | |
| 47 | + | |
| 48 | + | |
| 49 | + if ($maxWidth!=null) | |
| 50 | + $this->maxWidth = $maxWidth; | |
| 51 | + else | |
| 52 | + $this->maxWidth = 500; | |
| 53 | + | |
| 54 | + if ($maxHeight!=null) | |
| 55 | + $this->maxHeight = $maxHeight; | |
| 56 | + else | |
| 57 | + $this->maxHeight = 500; | |
| 58 | + | |
| 59 | + if ($maxSize!=null) | |
| 60 | + $this->maxSize = $maxSize; | |
| 61 | + else | |
| 62 | + $this->maxSize = 150*1024; | |
| 63 | + | |
| 64 | + if ($suportedExtensions != null) | |
| 65 | + $this->suportedExtensions = $suportedExtensions; | |
| 66 | + else | |
| 67 | + $this->suportedExtensions = array('jpeg','jpg','gif','png'); | |
| 68 | + } | |
| 69 | + | |
| 70 | + /** | |
| 71 | + * Envia imagem caso seja válida e retorna caminho | |
| 72 | + * | |
| 73 | + * @author Lucas Schmoeller da Silva - lucas@portabilis.com | |
| 74 | + * @return String | |
| 75 | + */ | |
| 76 | + function sendPicture($imageName){ | |
| 77 | + | |
| 78 | + $this->imageName = $imageName; | |
| 79 | + $tmp = $this->imageFile["tmp_name"]; | |
| 80 | + include('s3_config.php'); | |
| 81 | + //Rename image name. | |
| 82 | + | |
| 83 | + $actual_image_name = $directory.$this->imageName; | |
| 84 | + if($s3->putObjectFile($tmp, $bucket , $actual_image_name, S3::ACL_PUBLIC_READ) ) | |
| 85 | + { | |
| 86 | + | |
| 87 | + $s3file='http://'.$bucket.'.s3.amazonaws.com/'.$actual_image_name; | |
| 88 | + return $s3file; | |
| 89 | + } | |
| 90 | + else{ | |
| 91 | + $this->errorMessage = "Ocorreu um erro no servidor ao enviar foto. Tente novamente."; | |
| 92 | + return ''; | |
| 93 | + } | |
| 94 | + } | |
| 95 | + | |
| 96 | + /** | |
| 97 | + * Verifica se a imagem é válida | |
| 98 | + * | |
| 99 | + * @author Lucas Schmoeller da Silva - lucas@portabilis.com | |
| 100 | + * @return boolean | |
| 101 | + */ | |
| 102 | + function validatePicture(){ | |
| 103 | + | |
| 104 | + $msg=''; | |
| 105 | + | |
| 106 | + $name = $this->imageFile["name"]; | |
| 107 | + $size = $this->imageFile["size"]; | |
| 108 | + $ext = $this->getExtension($name); | |
| 109 | + | |
| 110 | + | |
| 111 | + if(strlen($name) > 0) | |
| 112 | + { | |
| 113 | + // Verifica formato do arquivo | |
| 114 | + if(in_array($ext,$this->suportedExtensions)) | |
| 115 | + { | |
| 116 | + // Verifica tamanho do arquivo | |
| 117 | + // @TODO Validar largura e altura da imagem | |
| 118 | + if($size < $this->maxSize){ | |
| 119 | + return true; | |
| 120 | + } | |
| 121 | + else{ | |
| 122 | + $this->errorMessage = "Não é permitido fotos com mais de 150KB."; | |
| 123 | + return false; | |
| 124 | + } | |
| 125 | + } | |
| 126 | + else{ | |
| 127 | + $this->errorMessage = "Deve ser enviado uma imagem do tipo jpeg, jpg, png ou gif."; | |
| 128 | + return false; | |
| 129 | + } | |
| 130 | + } | |
| 131 | + else{ | |
| 132 | + $this->errorMessage = "Selecione uma imagem."; | |
| 133 | + return false; | |
| 134 | + } | |
| 135 | + $this->errorMessage = "Imagem inválida."; | |
| 136 | + return false; | |
| 137 | + } | |
| 138 | + | |
| 139 | + /** | |
| 140 | + * Retorna a mensagem de erro | |
| 141 | + * | |
| 142 | + * @author Lucas Schmoeller da Silva - lucas@portabilis.com | |
| 143 | + * @return String | |
| 144 | + */ | |
| 145 | + function getErrorMessage(){ | |
| 146 | + return $this->errorMessage; | |
| 147 | + } | |
| 148 | + | |
| 149 | + | |
| 150 | + function getExtension($name) | |
| 151 | + { | |
| 152 | + $i = strrpos($name,"."); | |
| 153 | + if (!$i) | |
| 154 | + return ""; | |
| 155 | + $l = strlen($name) - $i; | |
| 156 | + $ext = substr($name,$i+1,$l); | |
| 157 | + | |
| 158 | + return $ext; | |
| 159 | + } | |
| 160 | +} | |
| 161 | + | |
| 162 | +?> | |
| 0 | 163 | \ No newline at end of file | ... | ... |
| ... | ... | @@ -0,0 +1,17 @@ |
| 1 | +<?php | |
| 2 | +// Bucket Name | |
| 3 | +require_once $_SERVER['DOCUMENT_ROOT'] . '/includes/bootstrap.php'; | |
| 4 | + | |
| 5 | +$bucket= $GLOBALS['coreExt']['Config']->app->aws->bucketname; | |
| 6 | +$directory=$GLOBALS['coreExt']['Config']->app->database->dbname.'/'; | |
| 7 | +$key = $GLOBALS['coreExt']['Config']->app->aws->awsacesskey; | |
| 8 | +$secretKey = $GLOBALS['coreExt']['Config']->app->aws->awssecretkey; | |
| 9 | + | |
| 10 | +if (!class_exists('S3')) | |
| 11 | + require_once 'S3.php'; | |
| 12 | + | |
| 13 | +//instantiate the class | |
| 14 | +$s3 = new S3($key, $secretKey); | |
| 15 | + | |
| 16 | +$s3->putBucket($bucket, S3::ACL_PUBLIC_READ); | |
| 17 | +?> | |
| 0 | 18 | \ No newline at end of file | ... | ... |