Open-source social media scheduler built for AI agents and the teams behind them. Create, schedule and publish posts to every network through MCP or the REST API, or write them yourself in a composer your team will love.
PHP ai-agentsblueskyfacebookinstagramlaravellinkedinmastodonmcpmcp-serveropen-sourcerest-apiself-hostedsocial-mediasocial-media-managementsocial-media-schedulerthreadstiktokvuejsxyoutube
fix: file handle leaks, return types, Facebook reel/story binary upload, null checks, cleanup
Paulo Castellano45dd3f4bdead
diff --git a/app/Services/Social/BlueskyPublisher.php b/app/Services/Social/BlueskyPublisher.phpindex 418954973..ef8e7f3f3 100644--- a/app/Services/Social/BlueskyPublisher.php+++ b/app/Services/Social/BlueskyPublisher.php@@ -312,7 +312,7 @@ class BlueskyPublisherthrow new TokenExpiredException('Bluesky session expired');}- private function handleApiError(Response $response): void+ private function handleApiError(Response $response): never{throw BlueskyPublishException::fromApiResponse($response);}diff --git a/app/Services/Social/FacebookPublisher.php b/app/Services/Social/FacebookPublisher.phpindex 4f476c013..f651e8681 100644--- a/app/Services/Social/FacebookPublisher.php+++ b/app/Services/Social/FacebookPublisher.php@@ -219,17 +219,23 @@ class FacebookPublisher$videoId = data_get($data, 'video_id');// Upload the video file- $uploadResponse = Http::post("{$this->baseUrl}/{$videoId}", [- 'upload_phase' => 'transfer',- 'video_file_chunk' => $media->url,- 'access_token' => $accessToken,- ]);+ $tempFile = tempnam(sys_get_temp_dir(), 'fb_reel_');+ try {+ Http::withOptions(['sink' => $tempFile])->timeout(600)->get($media->url);++ $uploadResponse = Http::withToken($accessToken)+ ->attach('video_file_chunk', fopen($tempFile, 'r'), 'video.mp4')+ ->post("{$this->baseUrl}/{$videoId}", [+ 'upload_phase' => 'transfer',+ 'access_token' => $accessToken,+ ]);- if ($uploadResponse->failed()) {- Log::error('Facebook reel upload transfer failed', [- 'body' => $uploadResponse->body(),- ]);- $this->handleApiError($uploadResponse);+ if ($uploadResponse->failed()) {+ Log::error('Facebook reel upload transfer failed', ['body' => $uploadResponse->body()]);+ $this->handleApiError($uploadResponse);+ }+ } finally {+ @unlink($tempFile);}// Finish and publish the reel@@ -275,11 +281,24 @@ class FacebookPublisher$videoId = $response->json()['video_id'];// Transfer the video- Http::post("{$this->baseUrl}/{$videoId}", [- 'upload_phase' => 'transfer',- 'video_file_chunk' => $media->url,- 'access_token' => $accessToken,- ]);+ $tempFile = tempnam(sys_get_temp_dir(), 'fb_story_');+ try {+ Http::withOptions(['sink' => $tempFile])->timeout(600)->get($media->url);++ $transferResponse = Http::withToken($accessToken)+ ->attach('video_file_chunk', fopen($tempFile, 'r'), 'video.mp4')+ ->post("{$this->baseUrl}/{$videoId}", [+ 'upload_phase' => 'transfer',+ 'access_token' => $accessToken,+ ]);++ if ($transferResponse->failed()) {+ Log::error('Facebook video story transfer failed', ['body' => $transferResponse->body()]);+ $this->handleApiError($transferResponse);+ }+ } finally {+ @unlink($tempFile);+ }// Finish the story$finishResponse = Http::post("{$this->baseUrl}/{$pageId}/video_stories", [diff --git a/app/Services/Social/InstagramPublisher.php b/app/Services/Social/InstagramPublisher.phpindex 96c375f4c..0585a596c 100644--- a/app/Services/Social/InstagramPublisher.php+++ b/app/Services/Social/InstagramPublisher.php@@ -191,7 +191,13 @@ class InstagramPublishercontinue;}- $childId = $containerResponse->json()['id'];+ $childId = $containerResponse->json()['id'] ?? null;++ if (! $childId) {+ Log::error('Instagram carousel item creation returned no ID', ['body' => $containerResponse->body()]);++ continue;+ }// Wait for video processing if neededif ($isVideo) {diff --git a/app/Services/Social/LinkedInPagePublisher.php b/app/Services/Social/LinkedInPagePublisher.phpindex f2fc484b5..08b73ae2b 100644--- a/app/Services/Social/LinkedInPagePublisher.php+++ b/app/Services/Social/LinkedInPagePublisher.php@@ -344,40 +344,42 @@ class LinkedInPagePublisher$uploadedPartIds = [];$handle = fopen($tempFile, 'r');- foreach ($uploadInstructions as $index => $instruction) {- $uploadUrl = data_get($instruction, 'uploadUrl');- $firstByte = data_get($instruction, 'firstByte');- $lastByte = data_get($instruction, 'lastByte');-- $chunkLength = $lastByte - $firstByte + 1;- fseek($handle, $firstByte);- $chunkData = fread($handle, $chunkLength);-- $chunkResponse = Http::withToken($this->accessToken)- ->withHeaders([- 'Content-Type' => 'application/octet-stream',- ])- ->timeout(600)- ->withBody($chunkData, 'application/octet-stream')- ->put($uploadUrl);-- if ($chunkResponse->failed()) {- Log::error('LinkedIn Page video chunk upload failed', [- 'index' => $index,- 'body' => $chunkResponse->body(),- ]);- $this->handleApiError($chunkResponse);- }+ try {+ foreach ($uploadInstructions as $index => $instruction) {+ $uploadUrl = data_get($instruction, 'uploadUrl');+ $firstByte = data_get($instruction, 'firstByte');+ $lastByte = data_get($instruction, 'lastByte');++ $chunkLength = $lastByte - $firstByte + 1;+ fseek($handle, $firstByte);+ $chunkData = fread($handle, $chunkLength);++ $chunkResponse = Http::withToken($this->accessToken)+ ->withHeaders([+ 'Content-Type' => 'application/octet-stream',+ ])+ ->timeout(600)+ ->withBody($chunkData, 'application/octet-stream')+ ->put($uploadUrl);++ if ($chunkResponse->failed()) {+ Log::error('LinkedIn Page video chunk upload failed', [+ 'index' => $index,+ 'body' => $chunkResponse->body(),+ ]);+ $this->handleApiError($chunkResponse);+ }++ $etag = $chunkResponse->header('etag');+ if ($etag) {+ $uploadedPartIds[] = $etag;+ }- $etag = $chunkResponse->header('etag');- if ($etag) {- $uploadedPartIds[] = $etag;}-+ } finally {+ fclose($handle);}- fclose($handle);-// Step 3: Finalize upload$finalizeResponse = $this->getHttpClient()->post("{$this->baseUrl}/rest/videos?action=finalizeUpload", [diff --git a/app/Services/Social/LinkedInPublisher.php b/app/Services/Social/LinkedInPublisher.phpindex 5d9605e3f..1a9a37911 100644--- a/app/Services/Social/LinkedInPublisher.php+++ b/app/Services/Social/LinkedInPublisher.php@@ -325,40 +325,42 @@ class LinkedInPublisher$uploadedPartIds = [];$handle = fopen($tempFile, 'r');- foreach ($uploadInstructions as $index => $instruction) {- $uploadUrl = data_get($instruction, 'uploadUrl');- $firstByte = data_get($instruction, 'firstByte');- $lastByte = data_get($instruction, 'lastByte');-- $chunkLength = $lastByte - $firstByte + 1;- fseek($handle, $firstByte);- $chunkData = fread($handle, $chunkLength);-- $chunkResponse = Http::withToken($this->accessToken)- ->withHeaders([- 'Content-Type' => 'application/octet-stream',- ])- ->timeout(600)- ->withBody($chunkData, 'application/octet-stream')- ->put($uploadUrl);-- if ($chunkResponse->failed()) {- Log::error('LinkedIn video chunk upload failed', [- 'index' => $index,- 'body' => $chunkResponse->body(),- ]);- $this->handleApiError($chunkResponse);- }+ try {+ foreach ($uploadInstructions as $index => $instruction) {+ $uploadUrl = data_get($instruction, 'uploadUrl');+ $firstByte = data_get($instruction, 'firstByte');+ $lastByte = data_get($instruction, 'lastByte');++ $chunkLength = $lastByte - $firstByte + 1;+ fseek($handle, $firstByte);+ $chunkData = fread($handle, $chunkLength);++ $chunkResponse = Http::withToken($this->accessToken)+ ->withHeaders([+ 'Content-Type' => 'application/octet-stream',+ ])+ ->timeout(600)+ ->withBody($chunkData, 'application/octet-stream')+ ->put($uploadUrl);++ if ($chunkResponse->failed()) {+ Log::error('LinkedIn video chunk upload failed', [+ 'index' => $index,+ 'body' => $chunkResponse->body(),+ ]);+ $this->handleApiError($chunkResponse);+ }++ $etag = $chunkResponse->header('etag');+ if ($etag) {+ $uploadedPartIds[] = $etag;+ }- $etag = $chunkResponse->header('etag');- if ($etag) {- $uploadedPartIds[] = $etag;}-+ } finally {+ fclose($handle);}- fclose($handle);-// Step 3: Finalize upload$finalizeResponse = $this->getHttpClient()->post("{$this->baseUrl}/rest/videos?action=finalizeUpload", [diff --git a/app/Services/Social/MastodonPublisher.php b/app/Services/Social/MastodonPublisher.phpindex fa962cd1d..bc78fd3fb 100644--- a/app/Services/Social/MastodonPublisher.php+++ b/app/Services/Social/MastodonPublisher.php@@ -121,7 +121,7 @@ class MastodonPublisher}}- private function handleApiError(Response $response): void+ private function handleApiError(Response $response): never{throw MastodonPublishException::fromApiResponse($response);}diff --git a/app/Services/Social/PinterestPublisher.php b/app/Services/Social/PinterestPublisher.phpindex b67f2fcdd..c7cd9d29a 100644--- a/app/Services/Social/PinterestPublisher.php+++ b/app/Services/Social/PinterestPublisher.php@@ -332,7 +332,7 @@ class PinterestPublisherthrow new \Exception("Pinterest media processing timeout after {$maxAttempts} attempts");}- public function refreshToken(SocialAccount $account): void+ private function refreshToken(SocialAccount $account): void{$response = Http::asForm()->withBasicAuth(diff --git a/app/Services/Social/TikTokPublisher.php b/app/Services/Social/TikTokPublisher.phpindex 0243eab58..db45766c9 100644--- a/app/Services/Social/TikTokPublisher.php+++ b/app/Services/Social/TikTokPublisher.php@@ -127,7 +127,7 @@ class TikTokPublisher}// Wait for processing and get final status- $finalStatus = $this->waitForPublishStatus($publishId);+ $this->waitForPublishStatus($publishId);return ['id' => $publishId,@@ -182,7 +182,7 @@ class TikTokPublisher}// Wait for processing and get final status- $finalStatus = $this->waitForPublishStatus($publishId);+ $this->waitForPublishStatus($publishId);return ['id' => $publishId,diff --git a/app/Services/Social/XPublisher.php b/app/Services/Social/XPublisher.phpindex 6fef7b57a..947f9e186 100644--- a/app/Services/Social/XPublisher.php+++ b/app/Services/Social/XPublisher.php@@ -195,34 +195,36 @@ class XPublisher$handle = fopen($tempFile, 'r');$index = 0;- while (! feof($handle)) {- $chunk = fread($handle, $chunkSize);+ try {+ while (! feof($handle)) {+ $chunk = fread($handle, $chunkSize);- if ($chunk === '' || $chunk === false) {- break;- }+ if ($chunk === '' || $chunk === false) {+ break;+ }- $appendResponse = Http::withToken($this->accessToken)- ->timeout(300)- ->attach('media', $chunk, 'chunk'.$index)- ->post("{$this->baseUrl}/2/media/upload/{$mediaId}/append", [- 'segment_index' => $index,- ]);+ $appendResponse = Http::withToken($this->accessToken)+ ->timeout(300)+ ->attach('media', $chunk, 'chunk'.$index)+ ->post("{$this->baseUrl}/2/media/upload/{$mediaId}/append", [+ 'segment_index' => $index,+ ]);++ if ($appendResponse->failed()) {+ Log::error('X chunked upload APPEND error', [+ 'status' => $appendResponse->status(),+ 'body' => $appendResponse->body(),+ 'segment' => $index,+ ]);+ $this->handleApiError($appendResponse);+ }- if ($appendResponse->failed()) {- Log::error('X chunked upload APPEND error', [- 'status' => $appendResponse->status(),- 'body' => $appendResponse->body(),- 'segment' => $index,- ]);- $this->handleApiError($appendResponse);+ $index++;}-- $index++;+ } finally {+ fclose($handle);}- fclose($handle);-// FINALIZE - Use the new v2 endpoint$finalizeResponse = Http::withToken($this->accessToken)->timeout(60)diff --git a/app/Services/Social/YouTubePublisher.php b/app/Services/Social/YouTubePublisher.phpindex 5d35230a9..c373bfd3c 100644--- a/app/Services/Social/YouTubePublisher.php+++ b/app/Services/Social/YouTubePublisher.php@@ -183,8 +183,8 @@ class YouTubePublisher$shortsTag = ' #Shorts';$availableLength = $maxLength - strlen($shortsTag);- $title = strtok($content, "\n");- $title = strtok($title, '.');+ $firstLine = explode("\n", $content)[0];+ $title = explode('.', $firstLine)[0];if (strlen($title) > $availableLength) {$title = substr($title, 0, $availableLength - 3).'...';diff --git a/tests/Unit/Services/Social/FacebookPublisherTest.php b/tests/Unit/Services/Social/FacebookPublisherTest.phpindex 90dd1a307..0ac0291eb 100644--- a/tests/Unit/Services/Social/FacebookPublisherTest.php+++ b/tests/Unit/Services/Social/FacebookPublisherTest.php@@ -174,6 +174,7 @@ test('facebook publisher can publish reel', function () {->push(['video_id' => 'reel_video_123'], 200)->push(['id' => 'reel_123', 'success' => true], 200),'*/reel_video_123' => Http::response(['success' => true], 200),+ '*' => Http::response('', 200),]);$result = $this->publisher->publish($this->postPlatform);@@ -230,6 +231,7 @@ test('facebook publisher can publish video story', function () {->push(['video_id' => 'story_video_123'], 200)->push(['post_id' => 'video_story_post_123'], 200),'*/story_video_123' => Http::response(['success' => true], 200),+ '*' => Http::response('', 200),]);$result = $this->publisher->publish($this->postPlatform);