auto-scripts/auto-php


Home Back
#!/usr/bin/env python

import os
import sys

print("Writing project structure...")
os.mkdir(sys.argv[1])
os.chdir(sys.argv[1])
os.mkdir("views")

indexphp='''<?php
    require_once "globals.php" ;
    global $router;
    include("ca.php");
    
    /*
     *  $router->get('pattern', function() { });
        $router->post('pattern', function() {  });
        $router->put('pattern', function() {  });
        $router->delete('pattern', function() {  });
        $router->options('pattern', function() {  });
        $router->patch('pattern', function() { });
        $router->all('pattern', function() { });
     * */
    
     $router->get('/', function () {
         echo '<h1>Home page</h1>';
         
     });
     
     $router->get('/fabio', function () {
         echo '<h1>Hello Fabio!</h1>';
         echo 's='.$_GET['s'];
         
     });
     
     $router->set404(function () {
         echo '<h1>Pagina non trovata </h1>';
    });

     
     
      $router->run();

?>
     
'''
f = open("index.php", "w")
f.write(indexphp)
f.close()



globalsphp='''<?php 
# https://github.com/bramus/router/tree/master
require_once __DIR__ . '/Router.php';
$router = new \Bramus\Router\Router();

function template($fileName, $arrayVal=[])
{
    $txt = file_get_contents($fileName);
    return strtr($txt, $arrayVal);
}     

?>
'''
f = open("globals.php", "w")
f.write(globalsphp)
f.close()

print("Download Bramus Router from freemedialab.org")
os.system("curl https://www.freemedialab.org/code/bramus_router/Router.txt > Router.php")

caphp='''<?php
    // Users
    // make the password with:  echo -n 'mypassword' |md5sum
    $accounts['fabio'] = 'a53bd0415947807bcb95ceec535820ee';
    $accounts['rita'] = '2794d223f90059c9f705c73a99384085';
    
    $login_error='<h2>Login error</h2><script>window.location = "/"; </script>';

    $htmlFormLogin='';
    
    
    function checkAuth($user, $pass,$accounts)
    {
        // global $accounts;
        // var_dump($accounts);
        
        if (array_key_exists($user, $accounts) && $accounts[$user]==$pass)
        {
            return true;
        }else{
            return false;
        }
    }
    
    function checkUsername($username)
    {
        if (ctype_alnum($username)) {
           return true;
        }else{
            return false;
        }
    }
    
    function checkPassword($password)
    {
        if (ctype_alnum($password)) {
           return true;
        }else{
            return false;
        }
    }
    
    // Not edit
    session_start(); 
    
    if (isset($_POST["login_btn"]))
    {
        
        if (!checkUsername($_POST["username"]) || !checkPassword($_POST["password"]))
        {
            echo "<p>Error...</p>";
            die();
        }
        
        
        $username=filter_var($_POST["username"], FILTER_SANITIZE_STRING);
        $password=filter_var(md5($_POST["password"]), FILTER_SANITIZE_STRING);
        
        if (checkAuth($username,$password,$accounts))
        {
            //logged in
            $_SESSION["username"]=$username;
            $_SESSION["password"]=$password;
            return ;            
        }else{
            //login error
            echo $login_error;    
            die();
        }
    }else{
        if (isset($_SESSION["username"]))
        {
            if (checkAuth($_SESSION["username"],$_SESSION["password"],$accounts)) return ;
        }
        if (!empty($htmlFormLogin))
        {
            echo $htmlFormLogin;
        }else{    
            echo '
            <!DOCTYPE html >
            <html >
            <head>
                <title>Login</title>
                <meta http-equiv="content-type" content="text/html;charset=utf-8" />
            </head>

            <body>
                
                <h2>Login</h2>
                <form action="'.$_SERVER['REQUEST_URI'].'" method="post">
                <div><label> Username: <input id="username" name="username" type="text" placeholder="Insert username"></label></div>
                <div><label> Password: <input id="password" name="password" type="text" placeholder="Insert password"></label></div>
                <div><button id="login_btn" name="login_btn" >Login</button></div>
                </form>
            </body>';
        }
        
        
        die();
    
    
    }
?> '''

f = open("ca.php", "w")
f.write(caphp)
f.close()

print ("Write .htaccess")
htaccess='''RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

'''
f = open(".htaccess", "w")
f.write(htaccess)
f.close()

Powered by Code, a simple repository browser by Fabio Di Matteo