/**
  * Hook executed before saving url_preview user type value. Checks and removes signature of the $value.
  * If signature is correct, checks current user's access to $value.
  *
  * @param array $userField Array containing parameters of the user field.
  * @param string $value Signed value of the user field.
  * @return int Unsigned value of the user field, or null in case of errors.
  */
 public static function onBeforeSave($userField, $value)
 {
     $imageUrl = null;
     if (strpos($value, ';') !== false) {
         list($value, $imageUrl) = explode(';', $value);
     }
     $signer = new Signer();
     try {
         $value = $signer->unsign($value, UrlPreview::SIGN_SALT);
     } catch (SystemException $e) {
         return null;
     }
     $metadata = UrlMetadataTable::getById($value)->fetch();
     if (!is_array($metadata)) {
         return null;
     }
     if ($metadata['TYPE'] === UrlMetadataTable::TYPE_STATIC) {
         if ($imageUrl && is_array($metadata['EXTRA']['IMAGES']) && in_array($imageUrl, $metadata['EXTRA']['IMAGES'])) {
             UrlPreview::setMetadataImage((int) $value, $imageUrl);
         }
         return $value;
     } else {
         if ($metadata['TYPE'] === UrlMetadataTable::TYPE_DYNAMIC && UrlPreview::checkDynamicPreviewAccess($metadata['URL'])) {
             return $value;
         }
     }
     return null;
 }
Example #2
0
 /**
  * Sets main image url for the metadata with given id.
  * @param int $id Id of the metadata to set image url.
  * @param string $imageUrl Url of the image.
  * @return bool Returns true in case of successful update, or false otherwise.
  * @throws ArgumentException
  */
 public static function setMetadataImage($id, $imageUrl)
 {
     if (!is_int($id)) {
         throw new ArgumentException("Id of the metadata must be an integer", "id");
     }
     if (!is_string($imageUrl) && !is_null($imageUrl)) {
         throw new ArgumentException("Url of the image must be a string", "imageUrl");
     }
     $metadata = UrlMetadataTable::getList(array('select' => array('IMAGE', 'IMAGE_ID', 'EXTRA'), 'filter' => array('=ID' => $id)))->fetch();
     if (isset($metadata['EXTRA']['IMAGES'])) {
         $imageIndex = array_search($imageUrl, $metadata['EXTRA']['IMAGES']);
         if ($imageIndex === false) {
             unset($metadata['EXTRA']['SELECTED_IMAGE']);
         } else {
             $metadata['EXTRA']['SELECTED_IMAGE'] = $imageIndex;
         }
     }
     if (static::getOptionSaveImages()) {
         $metadata['IMAGE_ID'] = static::saveImage($imageUrl);
         $metadata['IMAGE'] = null;
     } else {
         $metadata['IMAGE'] = $imageUrl;
         $metadata['IMAGE_ID'] = null;
     }
     return UrlMetadataTable::update($id, $metadata)->isSuccess();
 }