function store_user($text) { $filename = get_cache_filename('user'); if (strlen($text) > 0) { @file_put_contents($filename, $text); } else { // delete file @unlink($filename); } }
function widget_sowidget_getsocontent($userid, $hash, $cache_time) { $url = "http://stackoverflow.com/users/flair/" . $userid . ".json"; $gravatar = '<img border="0" alt="Gravatar" src="http://www.gravatar.com/avatar/' . $hash . '?s=48&d=identicon&r=PG" height=48 width=48 />'; $cache_file = get_cache_filename('stackoverflow' . $userid); $timedif = @(time() - filemtime($cache_file)); if (file_exists($cache_file) && $timedif < $cache_time) { $raw = file_get_contents($cache_file); } else { $raw = file_get_contents($url); if ($f = @fopen($cache_file, 'w')) { fwrite($f, $raw, strlen($raw)); fclose($f); } } // Decode JSON $json_decoded = json_decode($raw, true); $username = $json_decoded['displayName']; $url = $json_decoded['profileUrl']; $badges = $json_decoded['badgeHtml']; $rep = $json_decoded['reputation']; $content = '<style>.badge1 { color: #FFCC00; } .badge2 { color: #C0C0C0; } .badge3 { color: #CC9966; } </style>'; $content .= '<div style="background-color: #dddddd; border: 1px solid #888888; display: block; float: left; padding: 2px; margin: 2px;width: auto; line-height: 1.3em !important;"'; $content .= '<div style="float: left; width: 48px; display: block; vertical-align: middle;">'; $content .= '<a href="' . $url . '" target="_blank">'; $content .= $gravatar; $content .= '</a>'; $content .= '</div>'; $content .= '<div style="float: right; width: 100px; text-align: right; display: block;">'; $content .= '<a href="' . $url . '" target="_blank">' . htmlentities($username) . '</a><br />'; $content .= '<span style="font-size: 12px;">' . $rep . '</span><span style="color: #A00004;">r</span>'; $content .= '<br/>'; $content .= $badges; $content .= '</div>'; $content .= '</div>'; $content .= '<div style="clear: both;"></div>'; return $content; }
function cache_image($image_data, $mime_type, $checksum) { $cache_file = get_cache_filename($checksum); $image_data = base64_encode($image_data); $cache_entry = json_encode(compact('image_data', 'mime_type')); return file_put_contents($cache_file, $cache_entry); }