FubFubFub

mastobookmark.php

Aug 3rd, 2023
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.97 KB | Source Code | 0 0
  1. <?php
  2.  
  3. $MASTODON_SERVER = "[MASTO_SERVER]" // For instance: "mastodon.social";
  4. $SHAARLI_SECRET = "[SHAARLI_SECRET]";
  5. $MASTODON_TOKEN = "[MASTO_TOKEN]";
  6. $SHAARLI_ENDPOINT = "[SHAARLI_ENDPOINT]" // For instance: "https://example.com/shaarli/api/v1/";
  7. $CHUNK_SIZE = 5;
  8.  
  9. $MASTODON_ENDPOINT = "https://".$MASTODON_SERVER."/api/v1/";
  10. $SHAARLI_TOKEN = generateJwtToken($SHAARLI_SECRET);
  11. $mastodon_headers = array(
  12.   'Authorization: Bearer ' . $MASTODON_TOKEN
  13. );
  14.  
  15. $ADD_TO_SHAARLI = true;
  16. $DELETE_FROM_MASTODON = true;
  17. $ITERATE = true;
  18. for ($i=0;$i<$argc;$i++) {
  19.   if ($argv[$i] == "DONT_DELETE")  { $DELETE_FROM_MASTODON = false; }
  20.   if ($argv[$i] == "DONT_ADD")     { $ADD_TO_SHAARLI = false; $DELETE_FROM_MASTODON = false; }
  21.   if ($argv[$i] == "DONT_ITERATE") { $ITERATE = false; }
  22. }
  23. $shaarli_headers = array(
  24.   'Authorization: Bearer ' . $SHAARLI_TOKEN
  25. );
  26.  
  27. $ch = curl_init();
  28. $nextStep = true;
  29. $nextUrl = $MASTODON_ENDPOINT."bookmarks?limit=".$CHUNK_SIZE;
  30. while ($nextStep && $nextUrl != "") {
  31.   curl_reset($ch);
  32.   curl_setopt($ch, CURLOPT_HTTPHEADER, $mastodon_headers);
  33.   curl_setopt($ch, CURLOPT_URL, $nextUrl);
  34.   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  35.   curl_setopt($ch, CURLOPT_HEADER, true);
  36.   $server_output = curl_exec($ch);
  37.  
  38.   $nextUrl = "";
  39.  
  40.   $headerSize = curl_getinfo( $ch , CURLINFO_HEADER_SIZE );
  41.   $headerStr = substr( $server_output , 0 , $headerSize );
  42.   $bodyStr = substr( $server_output , $headerSize );
  43.   $headers = headersToArray( $headerStr );
  44.   $linkHeader = $headers['link'];
  45.   $nextLinkEnd = stripos($linkHeader, ">; rel=\"next\"");
  46.   $nextUrl = substr($linkHeader, 0, $nextLinkEnd);
  47.   $nextUrl = substr($nextUrl, strripos($nextUrl, "<")+1);
  48.  
  49.   $mastoBMs = json_decode($bodyStr, true);
  50.   foreach ($mastoBMs as $bookmark) {
  51.     $createdAt = date_create($bookmark["created_at"]);
  52.     echo "Bookmark " . $bookmark["id"] . " from Mastodon was created at " . date_format($createdAt, DATE_ISO8601) . "\n";
  53.     $tags = array("source:mastodon");
  54.     foreach ($bookmark["tags"] as $mastoTag) {
  55.       array_push($tags, $mastoTag["name"]);
  56.     }
  57.     $linkOnMastodonServer = "https://".$MASTODON_SERVER."/@".$bookmark["account"]["acct"]."/".$bookmark["id"];
  58.     $linkText = "Original link: ".$bookmark["url"]."\n";
  59.     $newLink = array("url" => $linkOnMastodonServer, "tags" => $tags, "private" => true, "description" => $linkText.strip_tags($bookmark["content"]));
  60.     $newLinkJson = json_encode($newLink);
  61.     curl_reset($ch);
  62.     $shaarli_headers = array(
  63.       'Authorization: Bearer ' . $SHAARLI_TOKEN,
  64.       'Content-Type:application/json'
  65.     );
  66.     curl_setopt($ch, CURLOPT_HTTPHEADER, $shaarli_headers);
  67.     curl_setopt($ch, CURLOPT_URL, $SHAARLI_ENDPOINT."links");
  68.     curl_setopt($ch, CURLOPT_POSTFIELDS, $newLinkJson );
  69.     curl_setopt($ch, CURLOPT_POST, true);
  70.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  71.  
  72.     //echo "new link JSON: [".$newLinkJson."]\n";
  73.     if ($ADD_TO_SHAARLI) {
  74.       $result = curl_exec($ch);
  75.       echo "Added link to Shaarli.\n";
  76.     }
  77.     else {
  78.       echo "Would have added link to Shaarli.\n";
  79.     }
  80.     if ($DELETE_FROM_MASTODON) {
  81.       $unbookmarkUrl = $MASTODON_ENDPOINT."statuses/".$bookmark["id"]."/unbookmark";
  82.       curl_reset($ch);
  83.       curl_setopt($ch, CURLOPT_HTTPHEADER, $mastodon_headers);
  84.       curl_setopt($ch, CURLOPT_URL, $unbookmarkUrl);
  85.      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  86.       curl_setopt($ch, CURLOPT_POST, true);
  87.       $server_output = curl_exec($ch);
  88.       echo "Removed status ".$bookmark["id"]." from bookmarks\n";
  89.     }
  90.     else {
  91.       echo "Would have unbookmarked status on Mastodon.\n";
  92.     }
  93.   }
  94.   if (!$ITERATE) {
  95.     echo "Not iterating further.\n";
  96.     $nextStep = false;
  97.   }
  98. }
  99.  
  100. curl_close($ch);
  101.  
  102. function base64url_encode($data) {
  103.   return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
  104. }
  105.  
  106. function generateJwtToken($secret) {
  107.   $header = base64_encode('{
  108.    "typ": "JWT",
  109.    "alg": "HS512"
  110.  }');
  111.   $payload = base64_encode('{
  112.    "iat": '. time() .'
  113.  }');
  114.   $signature = base64url_encode(hash_hmac('sha512', $header .'.'. $payload , $secret, true));
  115.   return $header .'.'. $payload .'.'. $signature;
  116. }
  117.  
  118. function headersToArray( $str )
  119. {
  120.     $headers = array();
  121.     $headersTmpArray = explode( "\r\n" , $str );
  122.     for ( $i = 0 ; $i < count( $headersTmpArray ) ; ++$i )
  123.     {
  124.         // we dont care about the two \r\n lines at the end of the headers
  125.         if ( strlen( $headersTmpArray[$i] ) > 0 )
  126.         {
  127.             // the headers start with HTTP status codes, which do not contain a colon so we can filter them out too
  128.             if ( strpos( $headersTmpArray[$i] , ":" ) )
  129.             {
  130.                 $headerName = substr( $headersTmpArray[$i] , 0 , strpos( $headersTmpArray[$i] , ":" ) );
  131.                 $headerValue = substr( $headersTmpArray[$i] , strpos( $headersTmpArray[$i] , ":" )+1 );
  132.                 $headers[$headerName] = $headerValue;
  133.             }
  134.         }
  135.     }
  136.     return $headers;
  137. }
  138.  
  139. ?>
Add Comment
Please, Sign In to add comment