Advertisement
Guest User

Untitled

a guest
Jun 21st, 2024
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.16 KB | Source Code | 0 0
  1. <?php
  2. if(!defined('golapp'))
  3. {
  4.     die('Direct access not permitted');
  5. }
  6.  
  7. define( 'BLUESKY_API_HANDLE' , 'gamingonlinux.com' );
  8. //define( 'BLUESKY_API_PASSWORD' , '' );
  9. //define( 'BLUESKY_POST_LINK' , '' );
  10. //define( 'BLUESKY_POST_TITLE' , '' );
  11.  
  12. $text=BLUESKY_POST_TITLE;
  13. $link=BLUESKY_POST_LINK;
  14.  
  15. function bluesky_connect() {
  16.     $curl = curl_init();
  17.     curl_setopt_array($curl, array(
  18.         CURLOPT_URL => 'https://bsky.social/xrpc/com.atproto.server.createSession',
  19.         CURLOPT_RETURNTRANSFER => true,
  20.         CURLOPT_FOLLOWLOCATION => true,
  21.         CURLOPT_POST => true,
  22.         CURLOPT_POSTFIELDS =>'{
  23.             "identifier":"'.BLUESKY_API_HANDLE.'",
  24.             "password":"'.BLUESKY_API_PASSWORD.'"
  25.     }',
  26.         CURLOPT_HTTPHEADER => array(
  27.             'Content-Type: application/json'
  28.         ),
  29.     ));
  30.     $response = curl_exec($curl);
  31.     curl_close($curl);
  32.     return json_decode($response,TRUE);
  33. }
  34.  
  35. function upload_media_to_bluesky($session, $filename, $fileUploadDir = '/tmp') {
  36.     // have we been passed a file?
  37.     if (empty($filename)) return;
  38.     // get the mime type, size and basename of the file
  39.     $body = file_get_contents($filename);
  40.     // If the path is a URL, use basename to get the filename
  41.     if (filter_var($filename, FILTER_VALIDATE_URL)) {
  42.         $basename = basename(parse_url($filename, PHP_URL_PATH));
  43.     }
  44.     else {
  45.         // If the path is a local path, use basename to get the filename
  46.         $basename = basename($filename);
  47.     }
  48.     $size = strlen($body);
  49.     // does the file size need reducing?
  50.     if ($size > 1000000) {
  51.         $newImage = imagecreatefromstring($body);
  52.         // downsample the image until it is less than maxImageSize (if possible!)
  53.         for ($i = 9; $i >= 1; $i--) {
  54.             imagejpeg($newImage, $fileUploadDir.'/'.$basename,$i * 10);
  55.             $size = strlen(file_get_contents($fileUploadDir.'/'.$basename));
  56.             if ($size < 1000000) {
  57.                 break;
  58.             }
  59.             else {
  60.                 unlink($fileUploadDir.'/'.$basename);
  61.             }
  62.         }
  63.         $body = file_get_contents($fileUploadDir.'/'.$basename);
  64.         unlink($fileUploadDir.'/'.$basename);
  65.     }
  66.     // get the file mime type
  67.     if (filter_var($filename, FILTER_VALIDATE_URL)) {
  68.         $headers = get_headers($filename, 1);
  69.         if (isset($headers['Content-Type'])) {
  70.             $content_type = $headers['Content-Type'];
  71.         }
  72.     }
  73.     else {
  74.         $content_type = mime_content_type($filename);
  75.     }
  76.     $curl = curl_init();
  77.     curl_setopt_array($curl, array(
  78.         CURLOPT_URL => 'https://bsky.social/xrpc/com.atproto.repo.uploadBlob',
  79.         CURLOPT_RETURNTRANSFER => true,
  80.         CURLOPT_FOLLOWLOCATION => true,
  81.         CURLOPT_POST => true,
  82.         CURLOPT_POSTFIELDS => $body,
  83.         CURLOPT_HTTPHEADER => array(
  84.             'Content-Type: '.$content_type,
  85.             'Authorization: Bearer '.$session['accessJwt']
  86.         ),
  87.     ));
  88.     $response = curl_exec($curl);
  89.     curl_close($curl);
  90.     $response = json_decode($response, false, 512, JSON_THROW_ON_ERROR);
  91.     $image = $response->blob;
  92.     return $image;
  93. }
  94.  
  95. function fetch_link_card($session, $url) {
  96.     // The required fields for every embed card
  97.     $card = [
  98.         "uri" => $url,
  99.         "title" => "",
  100.         "description" => "",
  101.     ];
  102.     // Create a new DOMDocument
  103.     $doc = new DOMDocument();
  104.     // Suppress errors for invalid HTML, if needed
  105.     libxml_use_internal_errors(true);
  106.     // Load the HTML from the URL
  107.     $doc->loadHTMLFile($url);
  108.     // Restore error handling
  109.     libxml_use_internal_errors(false);
  110.     // Create a new DOMXPath object for querying the document
  111.     $xpath = new DOMXPath($doc);
  112.     // Query for "og:title" and "og:description" meta tags
  113.     $title_tag = $xpath->query('//meta[@property="og:title"]/@content');
  114.     if ($title_tag->length > 0) {
  115.         $card["title"] = $title_tag[0]->nodeValue;
  116.     }
  117.     $description_tag = $xpath->query('//meta[@property="og:description"]/@content');
  118.     if ($description_tag->length > 0) {
  119.         $card["description"] = $description_tag[0]->nodeValue;
  120.     }
  121.     // If there is an "og:image" meta tag, fetch and upload that image
  122.     $image_tag = $xpath->query('//meta[@property="og:image"]/@content');
  123.     if ($image_tag->length > 0) {
  124.         $img_url = $image_tag[0]->nodeValue;
  125.         // Naively turn a "relative" URL (just a path) into a full URL, if needed
  126.         if (!parse_url($img_url, PHP_URL_SCHEME)) {
  127.             $img_url = $url . $img_url;
  128.         }
  129.         $image = upload_media_to_bluesky($session, $img_url);
  130.     }
  131.     $embed = [
  132.         '$type' => 'app.bsky.embed.external',
  133.         'external' => [
  134.             'uri' => $card['uri'],
  135.             'title' => $card['title'],
  136.             'description' => $card['description'],
  137.             'thumb' => $image,
  138.         ],
  139.     ];
  140.     return $embed;
  141. }
  142.  
  143. function bluesky_post($session, $text , $link) {
  144.     $args = [
  145.         'repo' => $session['did'],
  146.         'collection' => 'app.bsky.feed.post',
  147.         'record' => [
  148.             '$type' => 'app.bsky.feed.post',
  149.             'createdAt' => date('c'),
  150.             'text' => $text,
  151.             'langs' => ['en'],
  152.             'embed' => fetch_link_card($session, $link),
  153.         ],
  154.     ];
  155.     $curl = curl_init();
  156.     curl_setopt_array($curl, array(
  157.         CURLOPT_URL => 'https://bsky.social/xrpc/com.atproto.repo.createRecord',
  158.         CURLOPT_RETURNTRANSFER => true,
  159.         CURLOPT_FOLLOWLOCATION => true,
  160.         CURLOPT_POST => true,
  161.         CURLOPT_POSTFIELDS => json_encode( $args ),
  162.         CURLOPT_HTTPHEADER => array(
  163.             'Content-Type: application/json',
  164.             'Authorization: Bearer '.$session['accessJwt']
  165.         ),
  166.     ));
  167.     $response = curl_exec($curl);
  168.     curl_close($curl);
  169. }
  170.  
  171. $session = bluesky_connect();
  172.  
  173. bluesky_post($session, $text, $link);
  174.  
  175. ?>
  176.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement