Skip to content

Commit

Permalink
fix: Support {{cur}} for custom URL's, keep Unsplash link back hidd…
Browse files Browse the repository at this point in the history
…en if using custom URL
  • Loading branch information
tomershvueli committed May 9, 2021
1 parent fbbcf12 commit 77fe152
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ __NOTE__: PHP cURL is required for fetching external images.
__NOTE__: There [have been reports](https://github.com/tomershvueli/homepage/issues/24#issuecomment-754450034) that applying for a Normal API upgrading from a demo API will result in Unsplash shutting down your API key entirely. I suggest sticking to the demo API key and setting the `time_to_refresh_bg` config variable to `90000`. This will ensure that you don't surpass the 50 hourly requests that Unsplash provides for demo API keys. Or feel free to use a custom background image (see below).

### Custom Background Images
- `custom_url` => Input a custom URL that will return proper JSON
- `custom_url` => Input a custom URL that will return proper JSON. Supports `{{cur}}` substitution for current URL.
- `custom_url_headers` => Add any headers that may be needed to complete a cURL request to the aforementioned URL properly
- `custom_url_selector` => Input a proper PHP array selector to be used on the JSON received above. For example, if I were to fetch from Github's user API with a 'custom_url' of 'https://api.github.com/users/octocat', the 'custom_url_selector' would simply be `['avatar_url']`. `[{random}]` can be replaced for a random index in an array.

Expand Down
6 changes: 4 additions & 2 deletions src/hp_assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ function setBgImg() {
if (bg != "" && bg != null) {
preloadimages([bg]).done(function(images) {
$("#homepage").css("background-image", `url(${bg})`).css("background-size", "cover");
$("#pic-info-wrap").removeClass("hidden");
$("#pic-info-url").attr("href", `${data['image_user_url']}${unsplashUtmPostfix}`).text(data['image_user_name']);
if (data['image_user_name']) {
$("#pic-info-wrap").removeClass("hidden");
$("#pic-info-url").attr("href", `${data['image_user_url']}${unsplashUtmPostfix}`).text(data['image_user_name']);
}
});
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/hp_assets/lib/ajax_get_image.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,19 @@ function traverse_json($json, $selector) {
return $obj;
}

function get_current_url() {
$protocol = ((!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://";
$domainName = $_SERVER['SERVER_NAME'];
return $protocol . $domainName;
}

$config = json_decode(file_get_contents(dirname(__FILE__) . "/../../config.json"), true);

if (!empty($config['protected']['custom_url'])) {
// We're fetching from a custom URL
$json = json_decode(curl_get_contents($config['protected']['custom_url'], $config['protected']['custom_url_headers']), true);
$image_url = traverse_json($json, $config['protected']['custom_url_selector']);
$custom_url = str_replace("{{cur}}", get_current_url(), $config['protected']['custom_url']);
$json = json_decode(curl_get_contents($custom_url, $config['protected']['custom_url_headers']), true);
$image_url = traverse_json($json, $config['protected']['custom_url_selector']);

echo json_encode(array('success' => 1, 'url' => $image_url));
} else if (!empty($config['protected']['unsplash_client_id'])) {
Expand Down

0 comments on commit 77fe152

Please sign in to comment.