# Path: scans/models.py
# class Scan(models.Model):
# 	name=models.CharField(max_length=200,default="")
# 	hosts=models.TextField(default="")
# 	profile=models.ForeignKey("Scan_Profile", related_name="scanprofile")
# 	user = models.ForeignKey(User,blank=True, null=True, related_name="user")
# 	version =models.CharField(max_length=100, blank=True, null=True)
# 	summary=models.TextField(blank=True, null=True)
# 	finished=models.BooleanField(default=False)
# 	def __unicode__(self):
# 		return self.args
# 
# 	#only allow ip addresses and properly formatted host names to pass through. allow comma separated and split by line. 
# 	def isvalid(self, el):
# 		el = el.rstrip()
# 		fqdn = re.findall("(?=^.{4,255}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)", el)
# 		ips = re.findall("(?:[0-9]{1,3}\.){3}[0-9]{1,3}", el)
# 
# 		if len(ips) + len(fqdn) <= 0:
# 			raise ValidationError("Proper FQDN or IP not provided")
# 
# 	def clean(self):
# 		for line in self.hosts.split("\n"): #if your hosts field can have multiple lines, you can remove this
# 			elems = line.split(",")#creates an array from comma separated values
# 			if line:
# 				for el in elems:
# 					self.isvalid(el)
# 
# class Scan_Profile(models.Model):
# 	name=models.CharField(max_length=100, default="", unique=True)
# 	author=models.ForeignKey(User, related_name="profile_author")
# 	cmdline=models.TextField(default="")
# 	def __unicode__(self):
# 		return self.name
# 
# 	#dont allow any output format. We handle that :)
# 	def clean(self):
# 		if "nmap" in self.cmdline:
# 			 raise ValidationError('Do not place "nmap" in the command line arguments!')
# 		m = re.findall("-o[A-Z]", self.cmdline)
# 		if m:
# 			 raise ValidationError('No "-o" flags... We will decide the output for you!')
# 
# Path: scans/forms.py
# class ScanForm(forms.ModelForm):
# 	time=forms.DateTimeField(initial=datetime.now)
# 	class Meta:
# 		model=Scan
# 		fields = ['name', 'hosts','profile']
# 
# class ScanProfileForm(forms.ModelForm):
# 	class Meta:
# 		model = Scan_Profile
# 		fields = ['name', 'cmdline']


from django.conf import settings
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotFound
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
from django.core.servers.basehttp import FileWrapper
from django.core.files import File
from scans.models import Scan, Scan_Profile
from scans.forms import ScanForm, ScanProfileForm
from datetime import datetime
from django.utils.timezone import utc
import os, tempfile, zipfile, StringIO, sys, subprocess
def scan_delete(request, scan_id):
	scan = get_object_or_404(Scan,pk=scan_id)
	scan.delete()
	return HttpResponseRedirect('/')

#See all scan profiles
@login_required(login_url=login_page)
def scan_profiles(request):
	profile_list = Scan_Profile.objects.all()
	context={'profiles':profile_list}
	return render(request, 'scans/scan_profiles.html', context)

#delete a scan profile
@login_required(login_url=login_page)
def delete_profile(request, profile_id):
	profile = get_object_or_404(Scan_Profile,pk=profile_id)
	profile.delete()
	return HttpResponseRedirect(reverse('scan_profiles', current_app='scans'))

#Create a scan profile. Process a form
@login_required(login_url=login_page)
def new_scan_profile(request):
	if request.method == 'POST':
		form = ScanProfileForm(request.POST)
		if form.is_valid():
			cd = form.cleaned_data
			profilename = cd['name']
			profilecmd = cd['cmdline']
			r_user = request.user
			if profilename and r_user and profilecmd:

# Path: scans/models.py
# class Scan(models.Model):
# 	name=models.CharField(max_length=200,default="")
# 	hosts=models.TextField(default="")
# 	profile=models.ForeignKey("Scan_Profile", related_name="scanprofile")
# 	user = models.ForeignKey(User,blank=True, null=True, related_name="user")
# 	version =models.CharField(max_length=100, blank=True, null=True)
# 	summary=models.TextField(blank=True, null=True)
# 	finished=models.BooleanField(default=False)
# 	def __unicode__(self):
# 		return self.args
# 
# 	#only allow ip addresses and properly formatted host names to pass through. allow comma separated and split by line. 
# 	def isvalid(self, el):
# 		el = el.rstrip()
# 		fqdn = re.findall("(?=^.{4,255}$)(^((?!-)[a-zA-Z0-9-]{0,62}[a-zA-Z0-9]\.)+[a-zA-Z]{2,63}$)", el)
# 		ips = re.findall("(?:[0-9]{1,3}\.){3}[0-9]{1,3}", el)
# 
# 		if len(ips) + len(fqdn) <= 0:
# 			raise ValidationError("Proper FQDN or IP not provided")
# 
# 	def clean(self):
# 		for line in self.hosts.split("\n"): #if your hosts field can have multiple lines, you can remove this
# 			elems = line.split(",")#creates an array from comma separated values
# 			if line:
# 				for el in elems:
# 					self.isvalid(el)
# 
# class Scan_Profile(models.Model):
# 	name=models.CharField(max_length=100, default="", unique=True)
# 	author=models.ForeignKey(User, related_name="profile_author")
# 	cmdline=models.TextField(default="")
# 	def __unicode__(self):
# 		return self.name
# 
# 	#dont allow any output format. We handle that :)
# 	def clean(self):
# 		if "nmap" in self.cmdline:
# 			 raise ValidationError('Do not place "nmap" in the command line arguments!')
# 		m = re.findall("-o[A-Z]", self.cmdline)
# 		if m:
# 			 raise ValidationError('No "-o" flags... We will decide the output for you!')
# 
# Path: scans/forms.py
# class ScanForm(forms.ModelForm):
# 	time=forms.DateTimeField(initial=datetime.now)
# 	class Meta:
# 		model=Scan
# 		fields = ['name', 'hosts','profile']
# 
# class ScanProfileForm(forms.ModelForm):
# 	class Meta:
# 		model = Scan_Profile
# 		fields = ['name', 'cmdline']


# Path: scans/views.py
from django.conf import settings
from django.shortcuts import render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotFound
from django.core.urlresolvers import reverse
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
from django.core.servers.basehttp import FileWrapper
from django.core.files import File
from scans.models import Scan, Scan_Profile
from scans.forms import ScanForm, ScanProfileForm
from datetime import datetime
from django.utils.timezone import utc
import os, tempfile, zipfile, StringIO, sys, subprocess

def scan_delete(request, scan_id):
	scan = get_object_or_404(Scan,pk=scan_id)
	scan.delete()
	return HttpResponseRedirect('/')

#See all scan profiles
@login_required(login_url=login_page)
def scan_profiles(request):
	profile_list = Scan_Profile.objects.all()
	context={'profiles':profile_list}
	return render(request, 'scans/scan_profiles.html', context)

#delete a scan profile
@login_required(login_url=login_page)
def delete_profile(request, profile_id):
	profile = get_object_or_404(Scan_Profile,pk=profile_id)
	profile.delete()
	return HttpResponseRedirect(reverse('scan_profiles', current_app='scans'))

#Create a scan profile. Process a form
@login_required(login_url=login_page)
def new_scan_profile(request):
	if request.method == 'POST':
		form = ScanProfileForm(request.POST)
		if form.is_valid():
			cd = form.cleaned_data
			profilename = cd['name']
			profilecmd = cd['cmdline']
			r_user = request.user
			if profilename and r_user and profilecmd:

				s = Scan_Profile(name=profilename, author=r_user, cmdline=profilecmd)