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; }
/** * 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; }