PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/index.php

https://gitlab.com/fbi/twivatar
PHP | 225 lines | 180 code | 38 blank | 7 comment | 25 complexity | 9d4051f67a4ad94f03b3be1d8c398281 MD5 | raw file
  1. <?php
  2. function grab_url($url) {
  3. $ch = curl_init();
  4. curl_setopt($ch, CURLOPT_URL, $url);
  5. curl_setopt($ch, CURLOPT_HEADER, 0);
  6. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  7. $output = curl_exec($ch);
  8. curl_close($ch);
  9. return $output;
  10. }
  11. function grab_and_store($user, $db) {
  12. $user_profile = json_decode(grab_url('http://api.twitter.com/1/users/show.json?screen_name=' . $user));
  13. if (!$user_profile) {
  14. return "http://a0.twimg.com/sticky/default_profile_images/default_profile_1_bigger.png";
  15. } else {
  16. $image_url = $user_profile->profile_image_url;
  17. if ($db) {
  18. $sql = sprintf('replace into twitter_avatar (user, url) values ("%s", "%s")', mysql_real_escape_string($user), $image_url);
  19. mysql_query($sql, $db);
  20. }
  21. return $image_url;
  22. }
  23. }
  24. function head($image_url) {
  25. $c = curl_init();
  26. // TODO should we include a connection & read timeout here too?
  27. curl_setopt( $c, CURLOPT_RETURNTRANSFER, true );
  28. curl_setopt( $c, CURLOPT_CUSTOMREQUEST, 'HEAD' );
  29. curl_setopt( $c, CURLOPT_HEADER, 1 );
  30. curl_setopt( $c, CURLOPT_NOBODY, true );
  31. curl_setopt( $c, CURLOPT_URL, $image_url );
  32. $res = curl_exec( $c );
  33. if (preg_match('@HTTP/1.1 404 Not Found@', $res)) {
  34. return false;
  35. } else {
  36. return true;
  37. }
  38. }
  39. function size_image($image_url, $size) {
  40. if ($size == 'original') {
  41. $image_url = preg_replace('/_normal\./', '.', $image_url);
  42. } else if ($size != 'normal') {
  43. $image_url = preg_replace('/_normal\./', '_' . $size . '.', $image_url);
  44. }
  45. return $image_url;
  46. }
  47. function redirect($image_url, $size, $db) {
  48. $image_url = size_image($image_url, $size);
  49. if ($db) {
  50. mysql_close($db);
  51. }
  52. header('location: ' . $image_url);
  53. }
  54. $user = strtolower(@$_GET['user']);
  55. $size = strtolower(isset($_GET['size']) && in_array(strtolower($_GET['size']), array('mini', 'bigger', 'normal', 'original')) ? $_GET['size'] : 'normal');
  56. $db = null;
  57. $result = null;
  58. // skipping DB to save some performance from my own box, if you host this yourself, set to true
  59. $use_db = false;
  60. if ($user) {
  61. // use in case of emergencies: skips twitter entirly
  62. if (false) {
  63. redirect("http://a0.twimg.com/sticky/default_profile_images/default_profile_1_bigger.png", $size, false);
  64. exit;
  65. }
  66. // connect to DB
  67. if ($use_db) {
  68. $db = mysql_connect('localhost', 'root');
  69. mysql_select_db('twivatar', $db);
  70. $result = mysql_query(sprintf('select url from twitter_avatar where user="%s"', mysql_real_escape_string($user)), $db);
  71. }
  72. if (!$result || mysql_num_rows($result) == 0) {
  73. // grab and store - then redirect
  74. $image_url = grab_and_store($user, $db);
  75. redirect($image_url, $size, $db);
  76. } else if (mysql_num_rows($result) > 0) {
  77. // test if URL is available - then redirect
  78. $row = mysql_fetch_object($result);
  79. // if the url returned is one of Twitter's O_o static ones, then do a grab
  80. if (!preg_match('/static\.twitter\.com/', $row->url) && head($row->url)) {
  81. redirect($row->url, $size, $db);
  82. } else { // else grab and store - then redirect
  83. $image_url = grab_and_store($user, $db);
  84. redirect($image_url, $size, $db);
  85. }
  86. }
  87. exit;
  88. }
  89. ?>
  90. <!DOCTYPE html>
  91. <html>
  92. <head>
  93. <title>Twivatar - Twitter Avatar API</title>
  94. <meta charset="utf-8">
  95. <link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.4.0/bootstrap.min.css">
  96. <style>
  97. body {
  98. padding-top: 50px;
  99. }
  100. .container {
  101. width: 600px;
  102. }
  103. .content {
  104. background-color: #fff;
  105. padding: 20px;
  106. margin: 0 -20px;
  107. -webkit-border-radius: 0 0 6px 6px;
  108. -moz-border-radius: 0 0 6px 6px;
  109. border-radius: 0 0 6px 6px;
  110. -webkit-box-shadow: 0 1px 2px rgba(0,0,0,.15);
  111. -moz-box-shadow: 0 1px 2px rgba(0,0,0,.15);
  112. box-shadow: 0 1px 2px rgba(0,0,0,.15);
  113. }
  114. .modal {
  115. position: absolute;
  116. }
  117. </style>
  118. </head>
  119. <body>
  120. <a href="http://github.com/batuhanicoz/twivatar"><img style="position: absolute; top: 30px; right: 0; border: 0;" src="https://a248.e.akamai.net/assets.github.com/img/7afbc8b248c68eb468279e8c17986ad46549fb71/687474703a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f6461726b626c75655f3132313632312e706e67" alt="Fork me on GitHub"></a>
  121. <div class="topbar">
  122. <div class="fill">
  123. <div class="container">
  124. <h3><a href="http://twivatar.herokuapp.com">Twivatar</a></h3>
  125. </div>
  126. </div>
  127. </div>
  128. <div class="container">
  129. <section id="content">
  130. <div class="row">
  131. <div class="span10">
  132. <div class="page-header">
  133. <h1>Twitter Avatar API</h1>
  134. </div>
  135. <h4>What is it?</h4>
  136. <p>Twivatar is a RESTful API to a Twitter user's avatar built out of frustration of external Twitter apps breaking when the avatar url is stored, and then changed by that user later on Twitter - the result is a broken image on that app unless they constantly check for profile changes.</p>
  137. <p>All the code is available on <a href="https://github.com/batuhanicoz/twivatar/">GitHub</a>, so feel free to fork and contribute.</p>
  138. <h4>Usage</h4>
  139. <p><code>&lt;img src="http://twivatar.herokuapp.com/[<em>screen_name</em>]" /&gt;</code></p>
  140. <p>Alternatively you can specify the size image you want from:</p>
  141. <ul>
  142. <li>mini (24x24)</li>
  143. <li>normal (48x48 - default)</li>
  144. <li>bigger (73x73)</li>
  145. <li>original</li>
  146. </ul>
  147. <p><code>&lt;img src="http://twivatar.herokuapp.com/[<em>screen_name</em>]/[<em>size</em>]" /&gt;</code></p>
  148. <p>Also, if you need it, you can use HTTPS: </p>
  149. <p><code>&lt;img src="http<strong>s</strong>://twivatar.herokuapp.com/[<em>screen_name</em>]" /&gt;</code>
  150. (or <code>&lt;img src="http<strong>s</strong>://twivatar.herokuapp.com/[<em>screen_name</em>]/[<em>size</em>]" /&gt;</code>)
  151. </p>
  152. </div>
  153. </div>
  154. </section>
  155. <footer class="footer">
  156. <div class="container">
  157. <p class="pull-right">
  158. <a href="https://github.com/batuhanicoz/twivatar">Source code on Github</a></p>
  159. <p>
  160. Twivatar is a fork of <a href="https://twitter.com/rem">@rem</a>'s twivatar.org (<a href="https://github.com/remy/twivatar">on github</a>) by <a href="https://twitter.com/batuhanicoz">@batuhanicoz</a>.
  161. </p>
  162. </div>
  163. </footer>
  164. </div>
  165. <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js" type="text/javascript"></script>
  166. <script src="js/google-code-prettify/prettify.js"></script>
  167. <script>$(function () { prettyPrint() })</script>
  168. <script type="text/javascript">
  169. var _gaq = _gaq || [];
  170. _gaq.push(['_setAccount', 'UA-11142497-11']);
  171. _gaq.push(['_trackPageview']);
  172. (function() {
  173. var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
  174. ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
  175. var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
  176. })();
  177. </script>
  178. </body>
  179. </html>