aboutsummaryrefslogtreecommitdiff
path: root/hp_assets/lib/ajax_get_image.php
diff options
context:
space:
mode:
authorUnknown <philipp_kutyla@gmx.de>2017-02-20 14:57:52 +0100
committerUnknown <philipp_kutyla@gmx.de>2017-02-20 14:57:52 +0100
commit7675aad8555d1943c0d06a9fc662aa9164cfce7f (patch)
tree04384d22c690a5f5def439fd59b436ec2c81b24f /hp_assets/lib/ajax_get_image.php
parentef8940a0035d95f08f204601c8a7168ffd9058d8 (diff)
downloadsimple-dash-fork-7675aad8555d1943c0d06a9fc662aa9164cfce7f.zip
Responsiveness, scrolling in link-wrapper
Diffstat (limited to 'hp_assets/lib/ajax_get_image.php')
-rw-r--r--hp_assets/lib/ajax_get_image.php66
1 files changed, 66 insertions, 0 deletions
diff --git a/hp_assets/lib/ajax_get_image.php b/hp_assets/lib/ajax_get_image.php
new file mode 100644
index 0000000..7910619
--- /dev/null
+++ b/hp_assets/lib/ajax_get_image.php
@@ -0,0 +1,66 @@
+<?php
+
+ // AJAX call to fetch a new background image
+
+ // http://stackoverflow.com/a/24707821 => use instead of file_get_contents for external URL's
+ function curl_get_contents($url, $headers = null) {
+ $ch = curl_init();
+
+ curl_setopt($ch, CURLOPT_HEADER, 0);
+ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
+ curl_setopt($ch, CURLOPT_URL, $url);
+
+ // Include potential headers with request
+ if (!empty($headers)) {
+ curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
+ }
+
+ $data = curl_exec($ch);
+ curl_close($ch);
+
+ return $data;
+ }
+
+ // Traverse a JSON object given a string selector
+ function traverse_json($json, $selector) {
+ $regex = "\[\'?([{}a-z0-9_]+)\'?\]";
+ preg_match_all('/' . $regex . '/i', $selector, $matches);
+
+ // Go through each regex match and traverse the JSON object given the keys
+ $obj = $json;
+ foreach ($matches[1] as $i => $match) {
+ if ($match == '{random}' && is_array($obj)) {
+ // Let's fetch a random index of the array
+ $rand = rand(0, count($obj));
+ $obj = $obj[$rand];
+ } else {
+ // Keep traversing the object
+ $obj = $obj[$match];
+ }
+ }
+
+ return $obj;
+ }
+
+ $config = json_decode(file_get_contents(dirname(__FILE__) . "/../../config.json"), true);
+
+ if (!empty($config['custom_url'])) {
+ // We're fetching from a custom URL
+ $json = json_decode(curl_get_contents($config['custom_url'], $config['custom_url_headers']), true);
+ $image_url = traverse_json($json, $config['custom_url_selector']);
+
+ echo json_encode(array('success' => 1, 'url' => $image_url));
+ } else if (!empty($config['unsplash_client_id'])) {
+ // We're fetching from Unsplash's API
+ $url = "https://api.unsplash.com/photos/random?per_page=1&client_id=" . $config['unsplash_client_id'];
+ $json = json_decode(curl_get_contents($url), true);
+ $image_url = $json['urls']['regular'];
+ $image_user_name = $json['user']['name'];
+ $image_user_url = $json['user']['links']['html'];
+
+ echo json_encode(array('success' => 1, 'url' => $image_url, 'image_user_name' => $image_user_name, 'image_user_url' => $image_user_url));
+ }
+
+ die();
+
+?> \ No newline at end of file