PageRenderTime 28ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/plugins/instagram/instagram.php

https://gitlab.com/mina/kirbycms-extensions
PHP | 152 lines | 76 code | 23 blank | 53 comment | 15 complexity | 78bf0a44ba6d3c85954482b95fe80fa1 MD5 | raw file
  1. <?php
  2. /**
  3. * Returns an Instagram object.
  4. * @param string The access token (see readme on how to obtain one) ALWAYS NEEDED!
  5. * @param integer The number of photos that should be fetched.
  6. * @param boolean Caching enabled.
  7. * @param integer How many seconds until the cache expires.
  8. * @param string The user-id of the user of whom the photos should be loaded. 'self' is the default, which means that your own user ID will be inserted automatically by Instagram.
  9. * @return object Object of class Instagram, that holds all the images.
  10. */
  11. function instagram($token = '', $count = 10, $cache = true, $cache_expire = 3600, $user = 'self') {
  12. return new instagram($token, $count, $cache, $cache_expire, $user);
  13. }
  14. /**
  15. * An Instagram <http://instagram.com/> plugin for Kirby <http://getkirby.com/>.
  16. * In order to use this plugin, you'll need to obtain an access token for API access. See the readme for more information.
  17. * @author Simon Albrecht <http://albrecht.me/>
  18. * @version 1.0
  19. * @copyright (c) 2012 Simon Albrecht
  20. */
  21. class instagram {
  22. var $images;
  23. var $user;
  24. /**
  25. * Constructor. Loads the data from the Instagram API.
  26. * @param string The access token.
  27. * @param integer The number of shots that will be loaded.
  28. * @param boolean Chache enabled.
  29. * @param integer How many seconds until the cache expires.
  30. * @param string The user-id of the user or 'self' for your own account.
  31. */
  32. function __construct($_token = '', $_count = 10, $_cache = true, $_cache_expire = 3600, $_user = 'self') {
  33. // Init
  34. $this->images = array();
  35. $this->user = new stdClass;
  36. // Check if a token is provided
  37. if (trim($_token) != '') {
  38. // Construct the API url…
  39. // http://instagr.am/developer/endpoints/users/
  40. $url = "https://api.instagram.com/v1/users/{$_user}/media/recent/?access_token={$_token}&count={$_count}";
  41. // Create cache directory if it doesn't exist yet
  42. if ($_cache) {
  43. dir::make(c::get('root.cache') . '/instagram');
  44. }
  45. $images_cache_id = 'instagram/images.' . md5($_token) . '.' . $_count . '.php';
  46. $images_cache_data = false;
  47. // Try to fetch data from cache
  48. if ($_cache) {
  49. $images_cache_data = (cache::modified($images_cache_id) < time() - $_cache_expire) ? false : cache::get($images_cache_id);
  50. }
  51. // Load data from the API if the cache expired or the cache is empty
  52. if (empty($images_cache_data)) {
  53. $data = $this->fetch_data($url);
  54. $photos = json_decode($data);
  55. // Set new data for the cache
  56. if ($_cache) {
  57. cache::set($images_cache_id, $photos);
  58. }
  59. } else {
  60. $photos = $images_cache_data;
  61. }
  62. // Process the images
  63. for ($i = 0; $i < $_count; $i++) {
  64. if (isset($photos->data[$i]) && count($photos->data) > 0) {
  65. // Get the user's data from the first image
  66. if ($i == 0) {
  67. $this->user->username = $photos->data[$i]->user->username;
  68. $this->user->full_name = $photos->data[$i]->user->full_name;
  69. $this->user->picture = $photos->data[$i]->user->profile_picture;
  70. }
  71. // create a new object for each image
  72. $obj = new stdClass;
  73. $obj->link = $photos->data[$i]->link;
  74. $obj->comments = @$photos->data[$i]->comments->count;
  75. $obj->likes = @$photos->data[$i]->likes->count;
  76. $obj->created = $photos->data[$i]->created_time;
  77. $obj->thumb = @$photos->data[$i]->images->thumbnail->url;
  78. $obj->url = @$photos->data[$i]->images->standard_resolution->url;
  79. $obj->image_lowres = @$photos->data[$i]->images->low_resolution->url;
  80. $obj->filter = $photos->data[$i]->filter;
  81. $obj->location = @$photos->data[$i]->location->name;
  82. $obj->latitude = @$photos->data[$i]->location->latitude;
  83. $obj->longitude = @$photos->data[$i]->location->longitude;
  84. $obj->tags = array();
  85. // attach the new object to the array
  86. $this->images[$i] = $obj;
  87. // Process tags
  88. for ($j = 0; $j < count($photos->data[$i]->tags); $j++) {
  89. $this->images[$i]->tags[$j] = $photos->data[$i]->tags[$j];
  90. }
  91. }
  92. }
  93. } else {
  94. throw new Exception('$_token MUST be set!');
  95. }
  96. }
  97. /**
  98. * Returns the images that were loaded from the API.
  99. * @return array Array of objects containing all the photo's data.
  100. */
  101. function images() {
  102. return $this->images;
  103. }
  104. /**
  105. * Returns information about the user.
  106. * @return object Object with information about the user.
  107. */
  108. function user() {
  109. return $this->user;
  110. }
  111. /**
  112. * Fetches data from an url.
  113. * @param string The url from where data should be fetched.
  114. * @return object The data loaded from the url
  115. */
  116. protected function fetch_data($url = null) {
  117. if (!is_null($url)) {
  118. // Init CURL
  119. $handler = curl_init();
  120. // CURL options
  121. curl_setopt($handler, CURLOPT_URL, $url);
  122. curl_setopt($handler, CURLOPT_RETURNTRANSFER, 1);
  123. // Load data & close connection
  124. $data = curl_exec($handler);
  125. curl_close($handler);
  126. return $data;
  127. }
  128. }
  129. }