public function renderBody() { if (empty($this->Data['MediaID']) || !($Media = Media::getByID($this->Data['MediaID']))) { return ''; } else { return '<div class="content-item content-media" id="contentItem-' . $this->ID . '">' . static::getMediaMarkup($Media) . '</div>'; } }
public static function handlePhotoDeleteRequest() { $GLOBALS['Session']->requireAuthentication(); $User = static::_getRequestedUser(); if (empty($_REQUEST['MediaID']) || !is_numeric($_REQUEST['MediaID'])) { return static::throwInvalidRequestError(); } if (!($Media = Media::getByID($_REQUEST['MediaID']))) { return static::throwNotFoundError(); } if ($Media->ContextClass != $User->getRootClass() || $Media->ContextID != $User->ID) { return static::throwUnauthorizedError(); } if ($User->PrimaryPhotoID == $Media->ID) { $User->PrimaryPhotoID = null; $User->save(); } $Media->destroy(); return static::respond('profilePhotoDeleted', array('success' => true, 'data' => $Media)); }
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; }
protected static function onRecordSaved(CMS_Content $Content, $requestData) { $responseData = array(); // save items if (is_array($requestData['items'])) { $responseData['changedItems'] = array(); $responseData['newItems'] = array(); $responseData['deletedItems'] = array(); $responseData['invalidItems'] = array(); $responseData['phantomsMap'] = array(); // sort and save items foreach ($requestData['items'] as $itemData) { if (!empty($itemData['ID']) && is_numeric($itemData['ID'])) { // modify an existing item $Item = CMS_ContentItem::getByID($itemData['ID']); $Item->setFields($itemData); if ($Item->validate()) { $Item->save(); $responseData['changedItems'][$Item->ID] = $Item; } else { $responseData['invalidItems'][] = $Item; } } else { // create a new item if (!empty($itemData['Class']) && in_array($itemData['Class'], CMS_ContentItem::$subClasses)) { $className = $itemData['Class']; } else { $className = CMS_ContentItem::$defaultClass; } $itemData['ContentID'] = $Content->ID; $Item = $className::create($itemData); if ($Item->validate()) { $Item->save(); $responseData['newItems'][$Item->ID] = $Item; if ($itemData['ID']) { $responseData['phantomsMap'][$itemData['ID']] = $Item->ID; } } else { $responseData['invalidItems'][] = $Item; } } } // remove deleted items $currentItemIDs = array_merge(array_keys($responseData['changedItems']), array_keys($responseData['newItems'])); $responseData['deletedItems'] = array_filter($Content->Items, function ($Item) use($currentItemIDs) { return !in_array($Item->ID, $currentItemIDs); }); if ($responseData['deletedItems']) { foreach ($responseData['deletedItems'] as $Item) { $Item->Status = 'Deleted'; $Item->save(); } $Content->Items = array_filter($Content->Items, function ($Item) { return $Item->Status != 'Deleted'; }); } // update layout if there were phantoms if (is_array($requestData['LayoutConfig']['itemOrder'])) { foreach ($requestData['LayoutConfig']['itemOrder'] as &$column) { if (!is_array($column)) { continue; } foreach ($column as &$itemID) { if (array_key_exists($itemID, $responseData['phantomsMap'])) { $itemID = $responseData['phantomsMap'][$itemID]; } else { $itemID = (int) $itemID; } } } $Content->LayoutConfig = $requestData['LayoutConfig']; } } // assign context to media if (is_array($requestData['contextMedia'])) { foreach ($requestData['contextMedia'] as $mediaID) { if (!is_numeric($mediaID)) { continue; } if (!($Media = Media::getByID($mediaID))) { continue; } if ($Media->Context) { continue; } $Media->Context = $Content; $Media->save(); } } // assign tags if (is_array($requestData['tags'])) { Tag::setTags($Content, $requestData['tags']); } // save any page changes $Content->save(); }