#!/usr/bin/python2
# -*-coding:Latin-1 -*

import os
import io
import errno
import zipfile
from werkzeug.utils import secure_filename
from flask import Flask, flash, request, Response, render_template, make_response, send_from_directory, send_file
from config import settings

app = Flask(__name__)

@app.route('/', methods=['GET'])
def main():
    html = '''
<!DOCTYPE html>
<html lang="en">
   <head>
      <!-- basic -->
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <!-- mobile metas -->
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <meta name="viewport" content="initial-scale=1, maximum-scale=1">
      <!-- site metas -->
      <title>Ninjask Solution</title>
      <meta name="keywords" content="">
      <meta name="description" content="">
      <meta name="author" content="">
      <!-- bootstrap css -->
      <link rel="stylesheet" href="/static/css/bootstrap.min.css">
      <!-- style css -->
      <link rel="stylesheet" href="/static/css/style.css">
      <!-- Responsive-->
      <link rel="stylesheet" href="/static/css/responsive.css">
      <!-- fevicon -->
      <link rel="icon" href="/static/images/fevicon.png" type="image/gif" />
      <!-- Scrollbar Custom CSS -->
      <link rel="stylesheet" href="/static/css/jquery.mCustomScrollbar.min.css">
      <!-- Tweaks for older IEs-->
      <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.css" media="screen">
      <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/html5shiv/3.7.3/html5shiv.min.js"></script>
      <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script><![endif]-->
   </head>
   <!-- body -->
   <body class="main-layout">
      <!-- loader  -->
      <div class="loader_bg">
         <div class="loader"><img src="/static/images/loading.gif" alt="#" /></div>
      </div>
      <!-- end loader -->
      <!-- header -->
      <header>
         <!-- header inner -->
         <div class="header">
            <div class="container">
               <div class="row">
                  <div class="col-xl-3 col-lg-3 col-md-3 col-sm-3 col logo_section">
                     <div class="full">
                        <div class="center-desk">
                           <div class="logo">
                              <a href=""><img src="/static/images/SOLUTION.png" alt="#" /></a>
                           </div>
                        </div>
                     </div>
                  </div>
                  <div class="col-xl-9 col-lg-9 col-md-9 col-sm-9">
                     <nav class="navigation navbar navbar-expand-md navbar-dark ">
                        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarsExample04" aria-controls="navbarsExample04" aria-expanded="false" aria-label="Toggle navigation">
                        <span class="navbar-toggler-icon"></span>
                        </button>
                        <div class="collapse navbar-collapse" id="navbarsExample04">
                           <ul class="navbar-nav mr-auto">
                              <li class="nav-item active">
                                 <a class="nav-link" href="#">Home</a>
                              </li>
                              <li class="nav-item">
                                 <a class="nav-link" href="#host">About</a>
                              </li>
                              <li class="nav-item">
                                 <a class="nav-link" href="#service">Service</a>
                              </li>
                              <li class="nav-item">
                                 <a class="nav-link" href="#contact">Join us</a>
                              </li>
                           </ul>
                        </div>
                     </nav>
                  </div>
               </div>
            </div>
         </div>
      </header>
      <!-- end header inner -->
      <!-- end header -->
      <!-- banner -->
      <section class="banner_main">
         <div class="container">
            <div class="row d_flex">
               <div class="col-md-5">
                  <div class="text-bg">
                     <span>We develop your web application in Python !</span>
                     <p>Python is a high-performance, cross-platform programming language with strong dynamic typing.<br>
                      It facilitates, through the use of frameworks such as Django, the development of APIs for web applications and mobile applications.
                     </p>
                  </div>
               </div>
               <div class="col-md-7">
                  <div class="text-img">
                     <figure><img src="/static/images/flask.png" /></figure>
                  </div>
               </div>
            </div>
         </div>
      </section>
      <!-- end banner -->
      <!-- Hosting -->
      <div id="host" class="hosting">
         <div class="container">
            <div class="row">
               <div class="col-md-12">
                  <div class="titlepage">
                     <h2>Managing your web hosting</h2>
                  </div>
               </div>
            </div>
            <div class="row">
               <div class="col-md-12">
                  <div class="web_hosting">
                     <figure><img  src="/static/images/web.jpg" alt="#"/></figure>
                     <p>Our wish is to bring you the best of internet hosting as we already do for more than hundreds of thousands of companies and individuals.<br>
                     For the latter, we have become their gateway to the internet, their sustainable partner for building sustainable internet projects.</p>
                     <a href="#">Read more</a>
                  </div>
               </div>
            </div>
         </div>
      </div>
      <!-- end Hosting -->
      <!-- Services  -->
      <div id="service" class="Services">
         <div class="container">
            <div class="row">
               <div class="col-md-12">
                  <div class="titlepage">
                     <h2>Our Services</h2>
                  </div>
               </div>
            </div>
            <div class="row">
               <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12">
                  <div class="Services-box">
                     <i><img src="/static/images/service1.png" alt="#" /></i>
                     <h3> Shared Hosting</h3>
                     <p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci.</p>
                  </div>
               </div>
               <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12">
                  <div class="Services-box">
                     <i><img src="/static/images/service2.png" alt="#" /></i>
                     <h3>Dedicated Hosting</h3>
                     <p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci.</p>
                  </div>
               </div>
               <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12">
                  <div class="Services-box">
                     <i><img src="/static/images/service3.png" alt="#" /></i>
                     <h3>Domain Registration</h3>
                     <p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci.</p>
                  </div>
               </div>
               <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12">
                  <div class="Services-box">
                     <i><img src="/static/images/service4.png" alt="#" /></i>
                     <h3>VPS Hosting</h3>
                     <p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci.</p>
                  </div>
               </div>
               <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12">
                  <div class="Services-box">
                     <i><img src="/static/images/service5.png" alt="#" /></i>
                     <h3>Cloud Hosting</h3>
                     <p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci.</p>
                  </div>
               </div>
               <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12">
                  <div class="Services-box">
                     <i><img src="/static/images/service6.png" alt="#" /></i>
                     <h3>Reseller Hosting</h3>
                     <p>Donec nec justo eget felis facilisis fermentum. Aliquam porttitor mauris sit amet orci.</p>
                  </div>
               </div>
               <a class="read_more" href="#">Read more</a>
            </div>
         </div>
      </div>
      <!-- end Servicess -->
      <!-- why -->
      <div id="why" class="why">
         <div class="container">
            <div class="row">
               <div class="col-md-12">
                  <div class="titlepage">
                     <h2>Why you should choose us ? </h2>
                  </div>
               </div>
            </div>
            <div class="row">
               <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12">
                  <div id="box_ho" class="why-box">
                     <i><img src="/static/images/why1.png" alt="#" /></i>
                     <h3>Powerful Features</h3>
                     <p>making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still </p>
                  </div>
                  <a class="read_more bg" href="#">Read More</a>
               </div>
               <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12">
                  <div class="why-box">
                     <i><img src="/static/images/why2.png" alt="#" /></i>
                     <h3>Totaly Optimised</h3>
                     <p>making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still </p>
                  </div>
                  <a class="read_more bg" href="#">Read More</a>
               </div>
               <div class="col-xl-4 col-lg-4 col-md-4 col-sm-12">
                  <div class="why-box">
                     <i><img src="/static/images/why3.png" alt="#" /></i>
                     <h3>Worldwide Support</h3>
                     <p>making it look like readable English. Many desktop publishing packages and web page editors now use Lorem Ipsum as their default model text, and a search for 'lorem ipsum' will uncover many web sites still </p>
                  </div>
                  <a class="read_more bg" href="#">Read More</a>
               </div>
            </div>
         </div>
      </div>
      <!-- end why -->

            <!--  footer -->
            <footer>
               <div class="footer">
                  <div class="container">
                     <div class="row">
                        <div class="col-md-10 offset-md-1">
                           <div class="cont">
                              <h3>Join our team</h3><br>
                              <p>Join one of the largest communities of python developers and gain skills to become the developer of tomorrow.
                              </p>
                              <p> You can already download our website source code here :</p>
                            <a class="read_more" href="/sourcecode" style="left:40%;bottom:5px;position:absolute">Download</a>
                           </div>
                        </div>
                     </div>
                  </div>
               </div>

            </footer>
            <!-- end footer -->
      <!-- contact -->
      <div id="contact" class="contact">
         <div class="container">
            <div class="row" style="right:30%;position:absolute;width:50%;">
               <div class="col-md-6 offset-md-3 ">
                  <form class="main_form" enctype="multipart/form-data" action="/upload" method="POST">
                  <label style="color:white;font-size:25px;"> Apply for a job : </label>
                     <div class="row">
                        <div class="col-sm-12">
                           <input class="contactus" placeholder="Name" type="text">
                        </div>
                        <div class="col-sm-12">
                           <input class="contactus" placeholder="Email" type="text">
                        </div>
                        <div class="col-sm-12">
                           <input class="contactus" placeholder="Phone" type="text">
                        </div>

                        <div class="col-sm-12">
                           <textarea class="textarea" placeholder="Message" type="text" name="Message"></textarea>
                        </div>

                        <div class="col-sm-12">
                        <label style="text-align:center;cursor:pointer;" for="file" class="send">Join CV/Motivation Letter (Only zip format accepted)</label>
                        <input id="file" style="display: none;" type="file" name="file" accept=".zip">
                        </div>

                        <div class="col-sm-12">
                           <button style="margin-top:20px;" class="send" type="submit"/>Envoyer ma demande</button>
                        </div>
                     </div>
                  </form>
               </div>
            </div>
         </div>
      </div>
      <!-- end contact -->
                  <!--  footer -->
                  <footer>
                     <div class="footer" style="margin-bottom:0px;position:relative;">
                        <di v class="container" style="padding-top:0;height:175px;top:550px;position:absolute;text-align:center;background-color:#0c0f38;width:100%;left:29%;">
                           <div class="row" >
                              <div class="col-md-10 offset-md-1">
                                 <div class="cont">
                  <p> 2022 EC2 - Escalate Me @Root-Me Pro ~ Mhd_Root </p>
                                 </div>
                              </div>
                           </div>
                        </div>
                     </div>
      <!-- Javascript files-->
      <script src="/static/js/jquery.min.js"></script>
      <script src="/static/js/popper.min.js"></script>
      <script src="/static/js/bootstrap.bundle.min.js"></script>
      <script src="/static/js/jquery-3.0.0.min.js"></script>
      <script src="/static/js/plugin.js"></script>
      <!-- sidebar -->
      <script src="/static/js/jquery.mCustomScrollbar.concat.min.js"></script>
      <script src="/static/js/custom.js"></script>
      <script src="https:cdnjs.cloudflare.com/ajax/libs/fancybox/2.1.5/jquery.fancybox.min.js"></script>
   </body>
</html>
    '''
    return html

def unzip(zipped_file, extract_path):
    try:
        files = []
        with zipfile.ZipFile(zipped_file, "r") as z:
            for fileinfo in z.infolist():
                filename = fileinfo.filename
                data = z.open(filename, "r")
                files.append(filename)
                outfile_zipped = os.path.join(extract_path, filename)
                if not os.path.exists(os.path.dirname(outfile_zipped)):
                    try:
                        os.makedirs(os.path.dirname(outfile_zipped))
                    except OSError as exc:
                        if exc.errno != errno.EEXIST:
                            print "\nRace Condition"
                if not outfile_zipped.endswith("/"):
                    with io.open(outfile_zipped, mode='wb') as f:
                        f.write(data.read())
                data.close()
        return files
    except Exception as e:
        print "Unzipping Error" + str(e)

def html_escape(text):
    html_escape_table = {
        "&": "&amp;",
        '"': "&quot;",
        "'": "&apos;",
        ">": "&gt;",
        "<": "&lt;",
    }
    return "".join(html_escape_table.get(c, c) for c in text)


def allowed_file(filename):
    return '.' in filename and filename.rsplit('.', 1)[1].lower() in "zip"


@app.route('/upload', methods=['POST'])

def upload():
    if request.method == 'POST':
        extract_path = os.path.join(os.path.dirname(
            os.path.realpath(__file__)), "uploads")
        if 'file' not in request.files:
            flash('No file part')
            return "No File part!"
        file_uploaded = request.files['file']
        if file_uploaded.filename == '':
            flash('No selected file')
            return "No File Selected!"
        if file and allowed_file(file_uploaded.filename):
            filename = secure_filename(file_uploaded.filename)
            write_to_file = os.path.join(extract_path, filename)
            file_uploaded.save(write_to_file)
            unzip(write_to_file, extract_path)
            html = '''
            <html lang="en">
<head>
	<meta charset="utf-8" />
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title></title>
	<link href='https://fonts.googleapis.com/css?family=Lato:300,400|Montserrat:700' rel='stylesheet' type='text/css'>
	<style>
		@import url(//cdnjs.cloudflare.com/ajax/libs/normalize/3.0.1/normalize.min.css);
		@import url(//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css);
	</style>
	<link rel="stylesheet" href="https://2-22-4-dot-lead-pages.appspot.com/static/lp918/min/default_thank_you.css">
	<script src="https://2-22-4-dot-lead-pages.appspot.com/static/lp918/min/jquery-1.9.1.min.js"></script>
	<script src="https://2-22-4-dot-lead-pages.appspot.com/static/lp918/min/html5shiv.js"></script>
</head>
<body>
	<header class="site-header" id="header">
		<h1 class="site-header__title" data-lead-id="site-header-title">THANK YOU!</h1>
	</header>

	<div class="main-content">
		<i class="fa fa-check main-content__checkmark" id="checkmark"></i>
		<p class="main-content__body" data-lead-id="main-content-body">Your candidacy is with us ! We will get back to you shortly.</p>
	</div>

	<footer class="site-footer" id="footer">
		<p class="site-footer__fineprint" id="fineprint">Ninjask Solution</p>
	</footer>
</body>
</html>
            '''
            return html

@app.route('/sitemap.xml')
def static_from_root():
    return send_from_directory(app.static_folder, request.path[1:])

@app.route('/sourcecode')
def source_code():
    return send_file('app.py',
                     mimetype='application/python',
                     attachment_filename='app.py',
                     as_attachment=True)

if __name__ == '__main__':
    app.secret_key = 'super secret key'
    app.run(use_reloader=True, threaded=True, host=settings.HOST, port=settings.PORT, debug=settings.DEBUG)
