* * Where XXXXXXXX is the serial number. * * The script will search for an existing image on our server, if not found, * it will attempt to grab the image file from the USPTO site by performing an * http request. * * The image will load asynchronously on the page as it downloads. * */ if (!$_REQUEST['serial']) $_REQUEST['serial'] = 78689154; /** * Path to save the images */ $base_save_path = dirname(dirname(__FILE__)).'/images/'; $save_path = $base_save_path; $hitfile = $base_save_path.'lastdownload.hit'; /** * Default image file in case none found on disk or USPTO site */ $image404 = dirname(__FILE__) . '/images/image_placeholder.png'; $image_types = array( 'gif' => 'image/gif', 'jpg' => 'image/jpeg', 'png' => 'image/png', 'svg' => 'image/svg+xml', 'tiff' => 'image/tiff', 'ico' => 'image/vnd.microsoft.icon' ); $exif_types = array( IMAGETYPE_GIF => 'image/gif', IMAGETYPE_JPEG => 'image/jpeg', IMAGETYPE_PNG => 'image/png', IMAGETYPE_TIFF_II => 'image/tiff', IMAGETYPE_TIFF_MM => 'image/tiff', IMAGETYPE_ICO => 'image/vnd.microsoft.icon' ); $image_extensions = array_flip($image_types); // get serial number from URL request variable 'serial' $serial = intval($_REQUEST['serial']); function output_image($filename,$expires = null) { $info = getimagesize($filename); $type = $info['mime']; if (!$type) return false; if (!$expires) $expires = time() + (86400 * 365); $len = filesize($filename); header("Content-type: ".$type); header("Expires: ".gmdate("D, d M Y H:i:s \G\M\T",$expires)); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Pragma: public"); header("Content-Length: ".$len); header("Last-Modified: ".date('r', filemtime($filename))); @readfile($filename); die(); } function output_stream($streamed,$type,$expires = null) { if (!$expires) $expires = time() + (86400 * 365); header("Content-type: ".$type); header("Expires: ".gmdate("D, d M Y H:i:s \G\M\T",$expires)); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Pragma: public"); header("Content-Length: ".strlen($streamed)); echo $streamed; die(); } function filepath_is_writeable($filepath) { if (is_writable($filepath)) { return true; } $command = 'stat -c "%U:%a:%n" '.$filepath; $result = exec($command); if (preg_match("@777@msi", $result)) { return true; } elseif (preg_match("@(www-data|dots)@msi", $result)) { chmod($filepath, 0777); return filepath_is_writeable($filepath); } return false; } if (!$serial) { output_image($image404); } // format correct local save path $parent_path = $save_path.substr($serial,0,3); $save_path .= substr($serial,0,3).'/'.substr($serial,3,2).'/'; // Format final file path and name for the image $filename = $save_path . $serial; $found = false; // send local file if exists foreach ($image_types as $ext => $type) { if (file_exists($filename.'.'.$ext)) { $filename .= '.'.$ext; $found = true; if (!output_image($filename,time() + (86400 * 365))) { unlink($filename); } } } // get remote file from USPTO and save locally if (file_exists($filename.'.404')) { output_image($image404); } if (file_exists($hitfile) && time() - filemtime($hitfile) < 30) { output_image($image404,time() + 60); } $ch = curl_init(); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_HEADER, 0); // USPTO image request URL $uspto_url = 'https://tsdr.uspto.gov/img/SERIAL_NUMBER/large'; $image_request = str_replace('SERIAL_NUMBER', $serial, $uspto_url); curl_setopt($ch, CURLOPT_URL, $image_request); $newimgfile = curl_exec($ch); touch($hitfile); $info = curl_getinfo($ch); $type = $info['content_type']; if ($newimgfile) { // no image was found on USPTO site if (!preg_match('/^Error/', $newimgfile)) { // create save path if not exists if (!file_exists($save_path)) { mkdir($save_path, 0777, true); chmod($parent_path, 0777); chmod($save_path, 0777); } if (!filepath_is_writeable($parent_path) || !filepath_is_writeable($save_path)) { output_image($image404); } file_put_contents('/tmp/image.tmp',$newimgfile); $imginfo = getimagesize('/tmp/image.tmp'); unlink('/tmp/image.tmp'); if ($imginfo['mime']) $type = $imginfo['mime']; if (array_key_exists($type,$image_extensions)) { $filename .= '.'.$image_extensions[$type]; } else { $filename .= '.jpg'; } if (file_put_contents($filename, $newimgfile)) { output_stream($newimgfile,$type,time() + (86400 * 365)); } else { // Can't Write: $filename; output_stream($newimgfile,$type,time() + 3600); } } else { touch($filename.'.404'); // Can't Fetch: $filename; output_image($image404,time() + 3600); } } output_image($image404, time() + 30);