Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#59 - Validate current working directory permissions #68

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 28 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,20 +99,37 @@ docker-compose -f compose.selective.yaml up

#### Using the PHP script direcly on host machine

For Direct Method, remember to edit the config.php file and modify :
For Direct Method, remember to edit the .env file and modify :

```php
$clientdate = "PASTE CLIENT DATE HERE";
$cookiedata = "PASTE COOKIE DATA HERE";
// Set the video download quality. Default is 720p.
// Available Options: "Original File", "1080p", "720p", "540p", "360p", "224p"
$video_download_quality = "720p";
```bash
COURSE_LINK=""

// Set the following flag to true if you want to merge audio and video files of presentations
$FFMPEG_PRESENTATION_MERGE_FLAG = true;
# If using selective download, add the following line and add the path of course data file downloaded from Thinki-Parser
COURSE_DATA_FILE=""

# Watch YouTube video to know how to get the client date and cookie data
CLIENT_DATE=""
COOKIE_DATA=""

# Set the video download quality. Default is 720p.
# Available Options: "Original File", "1080p", "720p", "540p", "360p", "224p"
VIDEO_DOWNLOAD_QUALITY="720p"
```

Now simply run:

NOTE: Priority of COURSE_DATA_FILE is higher than COURSE_LINK. If COURSE_DATA_FILE is set, COURSE_LINK will be ignored.
Arguments passed to script in terminal override the values in .env file.

The priority order is (Highest to Lowest):
1. COURSE_DATA_FILE (if set) Terminal > .env
2. COURSE_LINK (if set) Terminal > .env

Now simply run:
```bash
php thinkidownloader3.php
```

You can override the course link or course data file by providing it as an argument:

```bash
php thinkidownloader3.php LINK_HERE
Expand All @@ -125,6 +142,7 @@ php thinkidownloader3.php --json COURSE_DATA_FILE_PATH
```



> [!CAUTION]
> This script only downloads enrolled courses from thinkific based website. Owner of this repository is not responsible for any misuse if you share your credentials with strangers.

Expand Down
36 changes: 35 additions & 1 deletion config.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
<?php

load_env(__DIR__ . '/.env');

$clientdate = $_ENV['CLIENT_DATE'];
$cookiedata = $_ENV['COOKIE_DATA'];
$video_download_quality = $_ENV['VIDEO_DOWNLOAD_QUALITY'];
$FFMPEG_PRESENTATION_MERGE_FLAG = false;
$current_directory_path = getcwd();
$msg = '';

if($cookiedata == '' || $clientdate == '')
Expand All @@ -14,5 +17,36 @@
$msg .= 'Mbstring extension not enabled. Remove ; from php.ini config in the line ;extension=mbstring\n';
if(!extension_loaded('openssl'))
$msg .= 'Openssl not enabled in php.ini\n';
if(!$current_directory_path)
$msg .= 'Unable to get current working directory. Check if PHP or current terminal has permission to access the directory\n';
if(!is_writable($current_directory_path))
$msg .= 'Current directory is not writable. Check if PHP or current terminal has permission to write in the directory\n';
if($msg != '')
die($msg);
die($msg);


function load_env($filePath){
if (!file_exists($filePath)) {
throw new Exception("The .env file does not exist.");
}

$lines = file($filePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

foreach ($lines as $line) {
// Skip comments
if (strpos(trim($line), '#') === 0) {
continue;
}

// Split by '=' to separate key and value
[$name, $value] = array_map('trim', explode('=', $line, 2));

// Remove surrounding quotes from the value
$value = trim($value, "'\"");

// Set the environment variable
$_ENV[$name] = $value;
$_SERVER[$name] = $value;
putenv("$name=$value");
}
}
40 changes: 31 additions & 9 deletions thinkidownloader3.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,41 @@
require("include/wistia.downloader.php");

// Run.
// If --json, then read from json file.
// Else, download from url.
if(in_array("--json", $argv) && isset($argv[2])) {
$json = file_get_contents($argv[2]);
// If --json or COURSE_DATA_FILE in env, then read from json file.
// Else, download from url or use COURSE_URL from env.
if( (in_array("--json", $argv) && isset($argv[2])) || !in_array(getenv("COURSE_DATA_FILE"), ["", null, false]) ) {

// --json is higher priority than env.
if (in_array("--json", $argv)) {
echo "Using Custom Metadata File for course data.".PHP_EOL;
if (!file_exists($argv[2])) {
die("File not found: ".$argv[2].PHP_EOL);
}

$json = file_get_contents($argv[2]);
} else {
echo "Loading Custom Metadata File from .env for course data.".PHP_EOL;
$json = file_get_contents(getenv("COURSE_DATA_FILE"));
}

$data = json_decode($json, true);
$contentsdata = $data["contents"];
init_course($data);
} else if(isset($argv[1])) {
$url = query($argv[1]);
$p = parse_url($argv[1]);
// Use course url from command line. It is higher priority than env.
$courseUrl = $argv[1];
handler($courseUrl);
} else if(!in_array(getenv("COURSE_URL"), ["", null, false])) {
$courseUrl = getenv("COURSE_URL");
handler($courseUrl);
} else {
echo "Usage for using course url: php thinkidownloader3.php <course_url>".PHP_EOL;
echo "Usage for selective download: php thinkidownloader3.php --json <course.json>".PHP_EOL;
}

function handler($courseUrl) {
$url = query($courseUrl);
$p = parse_url($courseUrl);
$path = $p;
$path = explode("/", $path["path"]);
file_put_contents(end($path).".json",$url); //save coursename.json
Expand All @@ -33,8 +58,5 @@
else
echo "Fetching Course Contents... Please Wait...".PHP_EOL;
init_course($data);
} else {
echo "Usage for using course url: php thinkidownloader3.php <course_url>".PHP_EOL;
echo "Usage for selective download: php thinkidownloader3.php --json <course.json>".PHP_EOL;
}
?>