Skip to content

Commit

Permalink
Added New Redirect System
Browse files Browse the repository at this point in the history
  • Loading branch information
varunsridharan committed Jun 28, 2020
1 parent 77fd9d1 commit 393075b
Show file tree
Hide file tree
Showing 26 changed files with 522 additions and 0 deletions.
92 changes: 92 additions & 0 deletions .github/generator-app/app.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php
require_once __DIR__ . '/common.php';
global $app_config, $table_list;
$app_config = array();

function get_absolute_path( $path ) {
return trim( trim( str_replace( URLS_FILES, '', $path ), '/' ) );
}

try {
if ( ! file_exists( APP_PATH . 'config.json' ) ) {
throw new Error( '⚠️ App Config File Not Found !' );
}
$app_config = json_decode( file_get_contents( APP_PATH . 'config.json' ), true );
$path1 = glob( URLS_FILES . '*.json' );
$path2 = glob( URLS_FILES . '*/*.json' );
$path3 = glob( URLS_FILES . '*/*/*.json' );
$path4 = glob( URLS_FILES . '*/*/*/*.json' );
$url_files = array_filter( array_unique( array_merge( $path1, $path2, $path3, $path4 ) ) );
$table_list = array();
if ( ! file_exists( APP_OUTPUT ) ) {
mkdir( APP_OUTPUT, 0777, true );
}

if ( ! empty( $url_files ) ) {
global $redirect_url, $title;
foreach ( $url_files as $json_file ) {
$json = json_decode( file_get_contents( $json_file ), true );
if ( isset( $json[0] ) ) {
$master_url = get_absolute_path( $json_file );
echo '###[group] Generating For ' . $master_url . PHP_EOL;
$title = $json[0];
$redirect_url = false;
$alternates = false;

if ( false !== filter_var( $json[0], FILTER_VALIDATE_URL ) ) {
$redirect_url = $json[0];
$alternates = isset( $json[1] ) ? $json[1] : array();
} else {
$redirect_url = $json[1];
$alternates = isset( $json[2] ) ? $json[2] : array();
}

if ( ! is_array( $alternates ) ) {
$alternates = array( $alternates );
}

$print = implode( PHP_EOL, $alternates );

echo <<<TEXT
Title : ${title}
Redirect URL : ${redirect_url}
Master URL : ${master_url}
Alternates : ${print}
TEXT;

$alternates[] = $master_url;
if ( ! isset( $table_list[ $redirect_url ] ) ) {
$table_list[ $redirect_url ] = array(
'name' => $title,
'urls' => array(),
);
}
foreach ( $alternates as $aurl ) {
$aurl = str_replace( '.json', '', $aurl );
$table_list[ $redirect_url ]['urls'][] = $aurl;
if ( ! file_exists( APP_OUTPUT . $aurl ) ) {
mkdir( APP_OUTPUT . $aurl, 0777, true );
}

ob_start();
include APP_PATH . 'html-template.php';
$html = ob_get_clean();
file_put_contents( APP_OUTPUT . $aurl . '/index.html', $html );
}
echo '###[endgroup]' . PHP_EOL;
} else {
echo "⚠️ Excluding File @ ${json_file}" . PHP_EOL;
}
}

ob_start();
include APP_PATH . 'main-index.php';
$html = ob_get_clean();
file_put_contents( APP_OUTPUT . '/index.html', $html );
}
} catch ( Exception $exception ) {
$msg = '🛑 Unknown Error !!' . PHP_EOL . PHP_EOL;
$msg .= print_r( $exception->getMessage(), true );
die( $msg );
}
4 changes: 4 additions & 0 deletions .github/generator-app/common.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
<?php
defined( 'APP_PATH' ) || define( 'APP_PATH', __DIR__ . '/' );
defined( 'APP_OUTPUT' ) || define( 'APP_OUTPUT', dirname( dirname( __DIR__ ) ) . '/output_html/' );
defined( 'URLS_FILES' ) || define( 'URLS_FILES', dirname( dirname( __DIR__ ) ) . '/' );
4 changes: 4 additions & 0 deletions .github/generator-app/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"default_title" : "Redirecting...",
"default_heading" : "Redirecting To"
}
51 changes: 51 additions & 0 deletions .github/generator-app/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh
set -eu

echo "🔄 Fetching Envato Links"
php .github/generator-app/envato.php

echo "🔄 Fetching WordPress Links"
php .github/generator-app/wordpress.php

echo "🔄 Running Shortlink Generator
"
php .github/generator-app/app.php

git config --global user.email "[email protected]" && git config --global user.name "Github Action Bot"
cd ../
PUSH_TO_BRANCH="gh-pages"

if [ -z "$(git ls-remote --heads https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git ${PUSH_TO_BRANCH})" ]; then
echo " "
echo "##[group] $PUSH_TO_BRANCH Create Log"
git clone --quiet https://x-access-token:$GITHUB_TOKEN@github.com/${GITHUB_REPOSITORY}.git $PUSH_TO_BRANCH >/dev/null
cd $PUSH_TO_BRANCH
git checkout --orphan $PUSH_TO_BRANCH >/dev/null
git rm -rf . >/dev/null
echo "$GITHUB_REPOSITORY" >README.md
git add README.md
git commit -a -m "➕ Create $PUSH_TO_BRANCH Branch"
git push origin $PUSH_TO_BRANCH
cd ..
echo "##[endgroup]"
echo "🗃 $PUSH_TO_BRANCH Created"
echo " "
else
echo "##[group] 👌 $PUSH_TO_BRANCH Cloned"
git clone --quiet --branch=$PUSH_TO_BRANCH https://x-access-token:$GITHUB_TOKEN@github.com/${GITHUB_REPOSITORY}.git $PUSH_TO_BRANCH
echo "##[endgroup]"
fi

rm -rf $PUSH_TO_BRANCH/*
cp -r $GITHUB_WORKSPACE/output_html/* $PUSH_TO_BRANCH/
cd $PUSH_TO_BRANCH/

if [ "$(git status --porcelain)" != "" ]; then
echo "##[group] 👌 Website Published"
git add .
git commit -m " :book: #$GITHUB_RUN_NUMBER - Website Regenerated / :zap: Triggered By $GITHUB_SHA"
git push origin $PUSH_TO_BRANCH
echo "##[endgroup]"
else
echo "✅ Nothing To Push"
fi
38 changes: 38 additions & 0 deletions .github/generator-app/envato.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
require_once __DIR__ . '/common.php';

define( 'SAVE_PATH', URLS_FILES . 'envato/' );
define( 'ITEMS_JSON_LINK', 'https://cdn.svarun.dev/envato/items.json' );
try {
$json = json_decode( file_get_contents( ITEMS_JSON_LINK ), true );
if ( isset( $json['plugins'] ) ) {
foreach ( $json as $group => $items ) {
foreach ( $items as $item ) {
$id = $item['id'];
$url = ( isset( $item['ref_url'] ) ) ? $item['ref_url'] : false;
$url = ( empty( $url ) && isset( $item['url'] ) ) ? $item['url'] : $url;
$slug = $item['slug'];
$mini_slug = $item['mini_slug'];
$redirect_from = array(
"/${slug}/",
"/envato/${slug}/",
"/${mini_slug}/",
"/envato/${mini_slug}/",
);

@mkdir( SAVE_PATH, 0777, true );
if ( ! file_exists( SAVE_PATH . $id . '.json' ) ) {
file_put_contents( SAVE_PATH . $id . '.json', json_encode( array(
$item['name'],
$url,
$redirect_from,
) ) );
}
}
}
}
} catch ( Exception $exception ) {
$msg = '🛑 Unknown Error !!' . PHP_EOL . PHP_EOL;
$msg .= print_r( $exception->getMessage(), true );
die( $msg );
}
34 changes: 34 additions & 0 deletions .github/generator-app/html-template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en-US">

<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-63845590-5"></script>

<script>
window.dataLayer = window.dataLayer || [];

function gtag() {
dataLayer.push( arguments );
}

gtag( 'js', new Date() );
gtag( 'config', 'UA-63845590-5' );
</script>

<meta charset="utf-8">

<title><?php echo $title; ?> | <?php echo $app_config['default_title']; ?></title>

<link rel="canonical" href="<?php echo $redirect_url; ?>">

<script>location = "<?php echo $redirect_url; ?>"</script>

<meta http-equiv="refresh" content="0; url=<?php echo $redirect_url; ?>">

<meta name="robots" content="noindex">

<h1><?php echo $app_config['default_heading']; ?> | <?php echo $title; ?> </h1>

<a href="<?php echo $redirect_url; ?>>">Click here if you are not redirected.</a>

</html>
161 changes: 161 additions & 0 deletions .github/generator-app/main-index.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<!-- CSS only -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css"
integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">

<link href="https:////cdn.datatables.net/1.10.21/css/jquery.dataTables.min.css"/>

<style>
/*! normalize.css v3.0.2 | MIT License | git.io/normalize */
@import url("https://fonts.googleapis.com/css?family=Open+Sans:400,700");

html {
font-family: sans-serif;
-ms-text-size-adjust: 100%;
-webkit-text-size-adjust: 100%
}

body {
padding: 0;
margin: 0;
font-family: "Open Sans", "Helvetica Neue", Helvetica, Arial, sans-serif;
font-size: 16px;
line-height: 1.5;
color: #606c71
}

a {
color: #1e6bb8;
text-decoration: none
}

a:hover {
text-decoration: underline
}


.page-header {
color: #fff;
padding: 2rem 6rem;
text-align: center;
background-color: #159957;
background-image: linear-gradient(120deg, #155799, #159957)
}

@media screen and (min-width: 64em) {
.page-header {
padding: 5rem 6rem
}
}

@media screen and (min-width: 42em) and (max-width: 64em) {
.page-header {
padding: 3rem 4rem
}
}

@media screen and (max-width: 42em) {
.page-header {
padding: 2rem 1rem
}
}

.project-name {
margin-top: 0;
margin-bottom: 0.1rem
}

@media screen and (min-width: 64em) {
.project-name {
font-size: 3.25rem
}
}

@media screen and (min-width: 42em) and (max-width: 64em) {
.project-name {
font-size: 2.25rem
}
}

@media screen and (max-width: 42em) {
.project-name {
font-size: 1.75rem
}
}

.project-tagline {
margin-bottom: 2rem;
font-weight: normal;
opacity: 0.7
}

@media screen and (min-width: 64em) {
.project-tagline {
font-size: 1.25rem
}
}

@media screen and (min-width: 42em) and (max-width: 64em) {
.project-tagline {
font-size: 1.15rem
}
}

@media screen and (max-width: 42em) {
.project-tagline {
font-size: 1rem
}
}

.page-header > h2 {
margin-bottom: 0;
margin-top: 10px
}

</style>
<section class="page-header">
<h1 class="project-name">Personal URL Shortener / Redirector</h1>
</section>
<div class="container my-5">
<div class="row">
<div class="col-12">
<table class="table table-bordered table-striped">
<thead class="thead-dark">
<tr>
<th style="width:45%;">To URL</th>
<th>Redirect Options</th>
</tr>
</thead>
<tbody>
<?php
foreach ( $table_list as $url => $data ) {

echo '<tr>';
echo "<td><a href=\"${url}\" class='mb-2 d-block'>{$data['name']}</a> <code>${url}</code></td>";
echo '<td><ul style="padding: 0;margin: 0;list-style: inside;padding-left: 10px;">';
foreach ( $data['urls'] as $url ) {
echo "<li><a href=\"${url}\">${url}</a></li>";
}
echo '</ul></td>';
echo '</tr>';
}
?>
</tbody>
</table>
</div>
</div>
</div>

<script
src="https://code.jquery.com/jquery-3.5.1.min.js"
integrity="sha256-9/aliU8dGd2tb6OSsuzixeV4y/faTqgFtohetphbbj0="
crossorigin="anonymous"></script>
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs4-4.1.1/dt-1.10.21/datatables.min.css"/>

<script type="text/javascript" src="https://cdn.datatables.net/v/bs4-4.1.1/dt-1.10.21/datatables.min.js"></script>
<script>
$( document ).ready( function() {
$( 'table' ).DataTable( {
paging: false
} );
} );
</script>
Loading

0 comments on commit 393075b

Please sign in to comment.