예제 #1
0
 protected static function apiPost($data)
 {
     $ch = curl_init('https://api.postmarkapp.com/email');
     curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Accept: application/json', 'X-Postmark-Server-Token: ' . static::$apiKey));
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
     curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
     if ($data) {
         curl_setopt($ch, CURLOPT_POST, true);
         curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
     }
     $result = curl_exec($ch);
     $httpStatus = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     #		die("<hr>result: $result, status: $httpStatus<hr>");
     if ($httpStatus == 200) {
         return json_decode($result, true);
     } else {
         \Emergence\Logger::general_error('PostmarkMailer Delivery Error', ['exceptionClass' => \PostmarkMailer::class, 'exceptionMessage' => $result, 'exceptionCode' => $httpStatus]);
         return false;
     }
 }
 public static function handleThumbnailRequest()
 {
     // send caching headers
     $expires = 60 * 60 * 24 * 365;
     header("Cache-Control: public, max-age={$expires}");
     header('Expires: ' . gmdate('D, d M Y H:i:s \\G\\M\\T', time() + $expires));
     header('Pragma: public');
     // thumbnails are immutable for a given URL, so no need to actually check anything if the browser wants to revalidate its cache
     if (!empty($_SERVER['HTTP_IF_NONE_MATCH']) || !empty($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
         header('HTTP/1.0 304 Not Modified');
         exit;
     }
     // get context
     if (!is_numeric(static::peekPath())) {
         $contextClass = static::shiftPath();
         $contextID = is_numeric(static::peekPath()) ? static::shiftPath() : false;
         $mediaID = false;
     } else {
         $contextClass = false;
         $contextID = false;
         $mediaID = static::shiftPath();
     }
     // get format
     if (preg_match('/^(\\d+)x(\\d+)(x([0-9A-F]{6})?)?$/i', static::peekPath(), $matches)) {
         static::shiftPath();
         $maxWidth = $matches[1];
         $maxHeight = $matches[2];
         $fillColor = !empty($matches[4]) ? $matches[4] : false;
     } else {
         $maxWidth = static::$defaultThumbnailWidth;
         $maxHeight = static::$defaultThumbnailHeight;
         $fillColor = false;
     }
     if (static::peekPath() == 'cropped') {
         static::shiftPath();
         $cropped = true;
     } else {
         $cropped = false;
     }
     // load media
     try {
         if ($mediaID) {
             if (!($Media = Media::getByID($mediaID))) {
                 return static::throwNotFoundError('Media not found');
             }
         } elseif ($contextClass && $contextID) {
             if (!($Media = Media::getByContext($contextClass, $contextID))) {
                 $Media = Media::getBlank($contextClass);
             }
         } elseif ($contextClass) {
             if (!($Media = Media::getBlank($contextClass))) {
                 return static::throwNotFoundError('Media not found');
             }
         } else {
             return static::throwError('Invalid request');
         }
         // get thumbnail
         $thumbPath = $Media->getThumbnail($maxWidth, $maxHeight, $fillColor, $cropped);
     } catch (Exception $e) {
         \Emergence\Logger::general_warning('Caught exception while creating thumbnail for media, returning server error', array('exceptionClass' => get_class($e), 'exceptionMessage' => $e->getMessage(), 'exceptionCode' => $e->getCode(), 'recordData' => $Media->getData(), 'thumbFormat' => array('maxWidth' => $maxWidth, 'maxHeight' => $maxHeight, 'fillColor' => $fillColor, 'cropped' => $cropped)));
         return static::throwServerError('Thumbnail unavailable');
     }
     // dump it out
     header("ETag: media-{$Media->ID}-{$maxWidth}-{$maxHeight}-{$fillColor}-{$cropped}");
     header("Content-Type: {$Media->ThumbnailMIMEType}");
     header('Content-Length: ' . filesize($thumbPath));
     readfile($thumbPath);
     exit;
 }
예제 #3
0
 public static function createFromFile($file, $fieldValues = array())
 {
     try {
         // handle url input
         if (filter_var($file, FILTER_VALIDATE_URL)) {
             $tempName = tempnam('/tmp', 'remote_media');
             copy($file, $tempName);
             $file = $tempName;
         }
         // analyze file
         $mediaInfo = static::analyzeFile($file);
         // create media object
         $Media = $mediaInfo['className']::create($fieldValues);
         // init media
         $Media->initializeFromAnalysis($mediaInfo);
         // save media
         $Media->save();
         // write file
         $Media->writeFile($file);
         return $Media;
     } catch (Exception $e) {
         \Emergence\Logger::general_warning('Caught exception while processing media upload, aborting upload and returning null', array('exceptionClass' => get_class($e), 'exceptionMessage' => $e->getMessage(), 'exceptionCode' => $e->getCode(), 'recordData' => $Media ? $Media->getData() : null, 'mediaInfo' => $mediaInfo));
         // fall through to cleanup below
     }
     // remove photo record
     if ($Media) {
         $Media->destroy();
     }
     return null;
 }