\n"); exit(0); } $login_token = $argv[1]; // Initial request, fetch data and user credentials $serviceContent = getServiceContent($login_token); if ($serviceContent == null || !isset($serviceContent->user)) { fwrite(STDOUT, ($serviceContent ? $serviceContent->message : 'Unable to log in.') . "\n"); exit(0); } // Fetch supported types $serviceContent = getSupportedTypes($serviceContent->user->id, $serviceContent->user->token); if ($serviceContent == null || !isset($serviceContent->user)) { fwrite(STDOUT, ($serviceContent ? $serviceContent->message : 'Unable to get supported types.') . "\n"); exit(0); } // Extract supported file formats from the service response $supportedFormat = getSupportedFormats($serviceContent); $userId = $serviceContent->user->id; $accessToken = $serviceContent->user->token; // Fetch the file list from the service $syncContent = getSyncContent($userId, $accessToken); $alreadyUploaded = isset($syncContent->md5) && is_array($syncContent->md5) ? $syncContent->md5 : array(); $files = array(); // Bootstrap the script, recursive method in order to traverse sub-folders readFiles(getcwd(), $files); // Display a nice welcome text with instructions fwrite(STDOUT, sprintf("Found %s file(s). Press \"L\" to list or \"U\" to start the upload. [Q to Quit]\n", count($files))); do { do { // Wait for user input $cmd = strtoupper(fgetc(STDIN)); } while (trim($cmd) == ''); if ($cmd == 'L') { // Display a list of files that were found in the dir and all child-dirs cmdList($files); } else if ($cmd == 'U') { // Trigger the upload action cmdUpload($files, $alreadyUploaded, $userId, $accessToken); exit(0); } // Allow user to abort using "Q" } while ($cmd !== 'Q'); function getDefaultPostData($mode) { return array( 'mode' => $mode, 'client' => 'php uploader', 'version' => '1.0', 'device_name' => 'php uploader', 'user_agent' => 'php uploader 1.0' ); } function cmdList($files) { fwrite(STDOUT, "Listing found, supported files\n"); // Display all files that are available for uploading foreach ($files as $file) { fwrite(STDOUT, $file . "\n"); } fwrite(STDOUT, "Press 'U' to start the upload if this looks reasonable\n"); } function cmdUpload($files, $alreadyUploaded, $userId, $accessToken) { fwrite(STDOUT, "Uploading..\n"); foreach ($files as $file) { fwrite(STDOUT, "Uploading: $file\n"); $checksum = md5_file($file); // Check if the file already exists on the service, if yes, then skipp if (in_array($checksum, $alreadyUploaded)) { fwrite(STDOUT, " skipping, already uploaded.\n"); continue; } // File is allowed to be uploaded if (uploadFile($file, $userId, $accessToken) === true) { fwrite(STDOUT, " Done!\n"); } else { fwrite(STDOUT, "Failed!\n"); } } } /** * Returns user credentials and account info * * @param string $login_token * * @return stdClass */ function getServiceContent($login_token) { $postData = getDefaultPostData('login_token'); $postData['login_token'] = $login_token; $postData['app_id'] = 1007; $postData['type'] = 'account'; $encodedData = json_encode($postData); $result = file_get_contents( 'https://api.ibroadcast.com/s/JSON/', null, stream_context_create( array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/json' . "\r\n" . 'Content-Length: ' . strlen($encodedData) . "\r\n" . 'User-Agent: ' . $postData['user_agent'] . "\r\n", 'content' => $encodedData, ), ) ) ); return json_decode($result); } /** * Returns supported types * * @param string $login_token * * @return stdClass */ function getSupportedTypes($user_id, $token) { $postData = getDefaultPostData('status'); $postData['user_id'] = $user_id; $postData['token'] = $token; $postData['supported_types'] = 1; $encodedData = json_encode($postData); $result = file_get_contents( 'https://api.ibroadcast.com/s/JSON/', null, stream_context_create( array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/json' . "\r\n" . 'Content-Length: ' . strlen($encodedData) . "\r\n" . 'User-Agent: ' . $postData['user_agent'] . "\r\n", 'content' => $encodedData, ), ) ) ); return json_decode($result); } /** * Returns a hash map of uploaded files. * * @param string $userId * @param string $accessToken * * @return stdClass */ function getSyncContent($userId, $accessToken) { $postData = http_build_query( array( 'user_id' => $userId, 'token' => $accessToken, ) ); $result = file_get_contents( 'https://upload.ibroadcast.com', null, stream_context_create( array( 'http' => array( 'method' => 'POST', 'header' => 'Content-Type: application/x-www-form-urlencoded' . "\r\n" . 'Content-Length: ' . strlen($postData) . "\r\n" . 'User-Agent: ' . 'php uploader 1.0' . "\r\n", 'content' => $postData, ) ) ) ); return json_decode($result); } /** * Uploads the given file to the remote service. * * @param string $file * @param string $userId * @param string $accessToken * * @return bool True, if the file upload was successful */ function uploadFile($file, $userId, $accessToken) { $multipartBoundary = '-----------'.microtime(true); $header = 'Content-Type: multipart/form-data; boundary=' . $multipartBoundary . "\r\n" . 'User-Agent: ' . 'php uploader 1.0' . "\r\n"; $file_contents = file_get_contents($file); $contentType = mime_content_type($file); $content = "--".$multipartBoundary."\r\n". "Content-Disposition: form-data; name=\"user_id\"\r\n\r\n". "$userId\r\n"; $content .= "--".$multipartBoundary."\r\n". "Content-Disposition: form-data; name=\"token\"\r\n\r\n". "$accessToken\r\n"; $content .= "--".$multipartBoundary."\r\n". "Content-Disposition: form-data; name=\"file_path\"\r\n\r\n". "$file\r\n"; $content .= "--".$multipartBoundary."\r\n". "Content-Disposition: form-data; name=\"method\"\r\n\r\n". "php uploader\r\n"; $content .= "--".$multipartBoundary."\r\n". "Content-Disposition: form-data; name=\"file\"; filename=\"".basename($file)."\"\r\n". "Content-Type: $contentType\r\n\r\n". $file_contents."\r\n"; $content .= "--".$multipartBoundary."--\r\n"; file_get_contents( 'https://upload.ibroadcast.com', null, stream_context_create( array( 'http' => array( 'method' => 'POST', 'header' => $header, 'content' => $content, ) ) ) ); return true; } /** * Returns an array of allowed file extensions. * * @param stdClass $serviceContent * * @return array */ function getSupportedFormats($serviceContent) { $supportedFormat = array(); foreach ($serviceContent->supported as $key => $format) { $supportedFormat[] = str_replace('.', '', $format->extension); } return $supportedFormat; } /** * Recursive function for traversing directories. * * @param string $dirPath * @param array $files */ function readFiles($dirPath, &$files) { global $supportedFormat, $files; $dirContent = scandir($dirPath); foreach ($dirContent as $key => $item) { // Skipp hidden files and parent folder references if (in_array($item, array('.','..')) || $item[0] === '.') { continue; } $itemPath = $dirPath . '/' . $item; if (is_dir($itemPath)) { readFiles($itemPath, $files); } else if (is_file($itemPath)) { $fileInfo = explode('.', $item); if (in_array($fileInfo[1], $supportedFormat)) { $files[] = $itemPath; } } } } exit(0);