aboutsummaryrefslogtreecommitdiff
path: root/hp_assets/lib/ajax_get_image.php
blob: 7910619ff5bb3a9958758edb775a3892a1c807fa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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();

?>