Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $MASTODON_SERVER = "[MASTO_SERVER]" // For instance: "mastodon.social";
- $SHAARLI_SECRET = "[SHAARLI_SECRET]";
- $MASTODON_TOKEN = "[MASTO_TOKEN]";
- $SHAARLI_ENDPOINT = "[SHAARLI_ENDPOINT]" // For instance: "https://example.com/shaarli/api/v1/";
- $CHUNK_SIZE = 5;
- $MASTODON_ENDPOINT = "https://".$MASTODON_SERVER."/api/v1/";
- $SHAARLI_TOKEN = generateJwtToken($SHAARLI_SECRET);
- $mastodon_headers = array(
- 'Authorization: Bearer ' . $MASTODON_TOKEN
- );
- $ADD_TO_SHAARLI = true;
- $DELETE_FROM_MASTODON = true;
- $ITERATE = true;
- for ($i=0;$i<$argc;$i++) {
- if ($argv[$i] == "DONT_DELETE") { $DELETE_FROM_MASTODON = false; }
- if ($argv[$i] == "DONT_ADD") { $ADD_TO_SHAARLI = false; $DELETE_FROM_MASTODON = false; }
- if ($argv[$i] == "DONT_ITERATE") { $ITERATE = false; }
- }
- $shaarli_headers = array(
- 'Authorization: Bearer ' . $SHAARLI_TOKEN
- );
- $ch = curl_init();
- $nextStep = true;
- $nextUrl = $MASTODON_ENDPOINT."bookmarks?limit=".$CHUNK_SIZE;
- while ($nextStep && $nextUrl != "") {
- curl_reset($ch);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $mastodon_headers);
- curl_setopt($ch, CURLOPT_URL, $nextUrl);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_HEADER, true);
- $server_output = curl_exec($ch);
- $nextUrl = "";
- $headerSize = curl_getinfo( $ch , CURLINFO_HEADER_SIZE );
- $headerStr = substr( $server_output , 0 , $headerSize );
- $bodyStr = substr( $server_output , $headerSize );
- $headers = headersToArray( $headerStr );
- $linkHeader = $headers['link'];
- $nextLinkEnd = stripos($linkHeader, ">; rel=\"next\"");
- $nextUrl = substr($linkHeader, 0, $nextLinkEnd);
- $nextUrl = substr($nextUrl, strripos($nextUrl, "<")+1);
- $mastoBMs = json_decode($bodyStr, true);
- foreach ($mastoBMs as $bookmark) {
- $createdAt = date_create($bookmark["created_at"]);
- echo "Bookmark " . $bookmark["id"] . " from Mastodon was created at " . date_format($createdAt, DATE_ISO8601) . "\n";
- $tags = array("source:mastodon");
- foreach ($bookmark["tags"] as $mastoTag) {
- array_push($tags, $mastoTag["name"]);
- }
- $linkOnMastodonServer = "https://".$MASTODON_SERVER."/@".$bookmark["account"]["acct"]."/".$bookmark["id"];
- $linkText = "Original link: ".$bookmark["url"]."\n";
- $newLink = array("url" => $linkOnMastodonServer, "tags" => $tags, "private" => true, "description" => $linkText.strip_tags($bookmark["content"]));
- $newLinkJson = json_encode($newLink);
- curl_reset($ch);
- $shaarli_headers = array(
- 'Authorization: Bearer ' . $SHAARLI_TOKEN,
- 'Content-Type:application/json'
- );
- curl_setopt($ch, CURLOPT_HTTPHEADER, $shaarli_headers);
- curl_setopt($ch, CURLOPT_URL, $SHAARLI_ENDPOINT."links");
- curl_setopt($ch, CURLOPT_POSTFIELDS, $newLinkJson );
- curl_setopt($ch, CURLOPT_POST, true);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- //echo "new link JSON: [".$newLinkJson."]\n";
- if ($ADD_TO_SHAARLI) {
- $result = curl_exec($ch);
- echo "Added link to Shaarli.\n";
- }
- else {
- echo "Would have added link to Shaarli.\n";
- }
- if ($DELETE_FROM_MASTODON) {
- $unbookmarkUrl = $MASTODON_ENDPOINT."statuses/".$bookmark["id"]."/unbookmark";
- curl_reset($ch);
- curl_setopt($ch, CURLOPT_HTTPHEADER, $mastodon_headers);
- curl_setopt($ch, CURLOPT_URL, $unbookmarkUrl);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
- curl_setopt($ch, CURLOPT_POST, true);
- $server_output = curl_exec($ch);
- echo "Removed status ".$bookmark["id"]." from bookmarks\n";
- }
- else {
- echo "Would have unbookmarked status on Mastodon.\n";
- }
- }
- if (!$ITERATE) {
- echo "Not iterating further.\n";
- $nextStep = false;
- }
- }
- curl_close($ch);
- function base64url_encode($data) {
- return rtrim(strtr(base64_encode($data), '+/', '-_'), '=');
- }
- function generateJwtToken($secret) {
- $header = base64_encode('{
- "typ": "JWT",
- "alg": "HS512"
- }');
- $payload = base64_encode('{
- "iat": '. time() .'
- }');
- $signature = base64url_encode(hash_hmac('sha512', $header .'.'. $payload , $secret, true));
- return $header .'.'. $payload .'.'. $signature;
- }
- function headersToArray( $str )
- {
- $headers = array();
- $headersTmpArray = explode( "\r\n" , $str );
- for ( $i = 0 ; $i < count( $headersTmpArray ) ; ++$i )
- {
- // we dont care about the two \r\n lines at the end of the headers
- if ( strlen( $headersTmpArray[$i] ) > 0 )
- {
- // the headers start with HTTP status codes, which do not contain a colon so we can filter them out too
- if ( strpos( $headersTmpArray[$i] , ":" ) )
- {
- $headerName = substr( $headersTmpArray[$i] , 0 , strpos( $headersTmpArray[$i] , ":" ) );
- $headerValue = substr( $headersTmpArray[$i] , strpos( $headersTmpArray[$i] , ":" )+1 );
- $headers[$headerName] = $headerValue;
- }
- }
- }
- return $headers;
- }
- ?>
Add Comment
Please, Sign In to add comment