public static function populateFromAsset(AssetFileModel $asset)
 {
     if ($asset->kind === 'json' && strpos($asset->filename, EmbeddedAssetsPlugin::getFileNamePrefix(), 0) === 0) {
         try {
             $rawData = craft()->embeddedAssets->readAssetFile($asset);
             if ($rawData) {
                 $data = JsonHelper::decode($rawData);
                 if ($data['__embeddedasset__']) {
                     unset($data['__embeddedasset__']);
                     $embed = new EmbeddedAssetsModel();
                     $embed->id = $asset->id;
                     foreach ($embed->attributeNames() as $key) {
                         if (isset($data[$key])) {
                             $embed->{$key} = $data[$key];
                         }
                     }
                     // For embedded assets saved with version 0.2.1 or below, this will provide a usable fallback
                     if (empty($embed->requestUrl)) {
                         $embed->requestUrl = $embed->url;
                     }
                     return $embed;
                 }
             }
         } catch (\Exception $e) {
             EmbeddedAssetsPlugin::log("Error reading embedded asset data on asset {$asset->id} (\"{$e->getMessage()}\")", LogLevel::Error);
             return null;
         }
     }
     return null;
 }
 public static function populateFromAsset(AssetFileModel $asset)
 {
     if ($asset->kind === 'json' && strpos($asset->filename, EmbeddedAssetsPlugin::getFileNamePrefix(), 0) === 0) {
         try {
             $url = $asset->getUrl();
             if (!UrlHelper::isAbsoluteUrl($url)) {
                 $protocol = craft()->request->isSecureConnection() ? 'https' : 'http';
                 $url = UrlHelper::getUrlWithProtocol($url, $protocol);
             }
             // See http://stackoverflow.com/questions/272361/how-can-i-handle-the-warning-of-file-get-contents-function-in-php
             $rawData = @file_get_contents($url);
             if ($rawData) {
                 $data = JsonHelper::decode($rawData);
                 if ($data['__embeddedasset__']) {
                     unset($data['__embeddedasset__']);
                     $embed = new EmbeddedAssetsModel();
                     $embed->id = $asset->id;
                     foreach ($data as $key => $value) {
                         $embed->{$key} = $value;
                     }
                     return $embed;
                 }
             }
         } catch (\Exception $e) {
             return null;
         }
     }
     return null;
 }
 /**
  * Reads in data from an external link.
  *
  * @param $url
  * @return bool|mixed|string
  */
 private function _readExternalFile($url)
 {
     if (function_exists('curl_init')) {
         EmbeddedAssetsPlugin::log("Reading file with `curl`");
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_AUTOREFERER, true);
         curl_setopt($ch, CURLOPT_HEADER, 0);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_URL, $url);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
         $data = curl_exec($ch);
         $error = curl_error($ch);
         if (!empty($error)) {
             EmbeddedAssetsPlugin::log("Error reading file (\"{$error}\")", LogLevel::Error);
             $data = false;
         }
         curl_close($ch);
         return $data;
     }
     $allowUrlFopen = preg_match('/1|yes|on|true/i', ini_get('allow_url_fopen'));
     if ($allowUrlFopen) {
         EmbeddedAssetsPlugin::log("Reading file with `file_get_contents`");
         return @file_get_contents($url);
     }
     return false;
 }
 private function _storeFile(EmbeddedAssetsModel $media, $folderId)
 {
     $fileName = EmbeddedAssetsPlugin::getFileNamePrefix() . StringHelper::UUID() . '.json';
     $fileData = $media->getAttributes(null, true);
     $fileData['__embeddedasset__'] = true;
     unset($fileData['id']);
     unset($fileData['settings']);
     $this->_addToFiles('assets-upload', $fileName, JsonHelper::encode($fileData));
     $response = craft()->assets->uploadFile($folderId);
     if ($response->isSuccess()) {
         $fileId = $response->getDataItem('fileId');
         $file = craft()->assets->getFileById($fileId);
         return $file;
     } else {
         throw new \Exception($response->errorMessage);
     }
 }