Exemple #1
0
def process(request):
    """Process a given text and redirect to viewer"""
    if 'text' in request.REQUEST and request.REQUEST['text']:
        text = request.REQUEST['text']
    elif 'uploadfile' in request.FILES:
        text = request.FILES['uploadfile'].read()
    else:
        return render_to_response('error.html',{'errormessage': "Er is geen geldige tekst ingevoerd!"} )

    #Verify checkum and other measures to counter spammers
    d = datetime.datetime.now()
    try:
        if int(request.REQUEST['checksum']) != d.year + d.month + d.day:
            return render_to_response('error.html',{'errormessage': "Invalid checksum, are you sure you are human? If not, begone!"} )
    except ValueError:
        return render_to_response('error.html',{'errormessage': "Invalid checksum, are you sure you are human? If not, begone!"} )
    if text.find("href=") != -1 or text.find("<iframe") != -1 or text.find("<img") != -1:
        return render_to_response('error.html',{'errormessage': "Je invoer moet bestaan uit platte tekst, er zijn HTML elementen gedetecteerd en deze kunnen niet verwerkt worden"} )
    elif text.find("[url=") != -1 or text.find("[img]") != -1:
        return render_to_response('error.html',{'errormessage': "Je invoer moet bestaan uit platte tekst, er zijn BBCode elementen gedetecteerd en deze kunnen niet verwerkt worden"} )
    elif text.count("http") > 10:
        return render_to_response('error.html',{'errormessage': "Je invoer bevat teveel webaddressen in de tekst, dit is om spam tegen te gaan niet toegestaan"} )


    #generate a random ID for this project
    id = 'D' + hex(random.getrandbits(128))[2:-1]

    #create CLAM client
    if 'CLAMUSER' in dir(settings):
        client = clam.common.client.CLAMClient(settings.CLAMSERVICE,settings.CLAMUSER, settings.CLAMPASS)
    else:
        client = clam.common.client.CLAMClient(settings.CLAMSERVICE)

    #creat project
    client.create(id)

    #get specification
    clamdata = client.get(id)

    #add input file
    try:
        client.addinput(id, clamdata.inputtemplate('textinput'), text, filename=id +'.txt',encoding='utf-8')
    except Exception, e:
        try:
            client.delete(id)
        except:
            pass
        return render_to_response('error.html',{'errormessage': "Kon het gekozen bestand niet toevoegen. Dit kan meerdere oorzaken hebben. Valkuil accepteert momenteel alleen platte UTF-8 gecodeerde tekst-bestanden. Met name Microsoft Word bestanden zijn nog niet ondersteund in dit stadium!",'debugmessage':str(e)} )
Exemple #2
0
def process(request):
    """Process a given text and redirect to viewer"""
    if 'text' in request.REQUEST and request.REQUEST['text']:
        text = request.REQUEST['text']
    elif 'uploadfile' in request.FILES:
        text = request.FILES['uploadfile'].read()
    else:
        return render_to_response('error.html',{'errormessage': "Did not receive any valid text!"} )


    #Verify checkum and other measures to counter spammers
    d = datetime.datetime.now()
    try:
        if int(request.REQUEST['checksum']) != d.year + d.month + d.day:
            return render_to_response('error.html',{'errormessage': "Invalid checksum, are you sure you are human? If not, begone!"} )
    except:
        return render_to_response('error.html',{'errormessage': "Invalid checksum, are you sure you are human? If not, begone!"} )
    if text.find("href=") != -1 or text.find("<iframe") != -1 or text.find("<img") != -1:
        return render_to_response('error.html',{'errormessage': "Your input must consist of plain text, HTML elements were detected but can not be processed"} )
    elif text.find("[url=") != -1 or text.find("[img]") != -1:
        return render_to_response('error.html',{'errormessage': "Your input must consist of plain text, BBCode elements were detected but can not be processed"} )
    elif text.count("http") > 10:
        return render_to_response('error.html',{'errormessage': "Your input contains too many URLs, to counter spam, this is not allowed"} )

    #generate a random ID for this project
    id = 'D' + hex(random.getrandbits(128))[2:-1]

    #create CLAM client
    if 'CLAMUSER' in dir(settings):
        client = clam.common.client.CLAMClient(settings.CLAMSERVICE,settings.CLAMUSER, settings.CLAMPASS)
    else:
        client = clam.common.client.CLAMClient(settings.CLAMSERVICE)
    #creat project
    client.create(id)

    #get specification
    clamdata = client.get(id)

    #add input file
    try:
        client.addinput(id, clamdata.inputtemplate('textinput'), text, filename=id +'.txt',encoding='utf-8')
    except Exception, e:
        try:
            client.delete(id)
        except:
            pass
        return render_to_response('error.html',{'errormessage': "Could not add the file. This can have multiple causes. At the moment, Fowlt only accepts only plain UTF-8 textfiles. Microsoft Word files are not supported at this stage!",'debugmessage':str(e)} )
Exemple #3
0
def process(request):
    """Process a given text and redirect to viewer"""
    if "text" in request.REQUEST and request.REQUEST["text"]:
        text = request.REQUEST["text"]
    elif "uploadfile" in request.FILES:
        text = request.FILES["uploadfile"].read()
    else:
        return render_to_response("error.html", {"errormessage": "Er is geen geldige tekst ingevoerd!"})

    # generate a random ID for this project
    id = "D" + hex(random.getrandbits(128))[2:-1]

    # create CLAM client
    if "CLAMUSER" in dir(settings):
        client = clam.common.client.CLAMClient(settings.CLAMSERVICE, settings.CLAMUSER, settings.CLAMPASS)
    else:
        client = clam.common.client.CLAMClient(settings.CLAMSERVICE)
    # creat project
    client.create(id)

    # get specification
    clamdata = client.get(id)

    # add input file
    try:
        client.addinput(id, clamdata.inputtemplate("textinput"), text, filename=id + ".txt", encoding="utf-8")
    except Exception, e:
        try:
            client.delete(id)
        except:
            pass
        return render_to_response(
            "error.html",
            {
                "errormessage": "Kon het gekozen bestand niet toevoegen. Dit kan meerdere oorzaken hebben. Valkuil accepteert momenteel alleen platte UTF-8 gecodeerde tekst-bestanden. Met name Microsoft Word bestanden zijn nog niet ondersteund in dit stadium!",
                "debugmessage": str(e),
            },
        )
Exemple #4
0
def process():
    """Process a given text and redirect to viewer"""
    if 'text' in request.form and request.form['text']:
        text = request.form['text']
    elif 'uploadfile' in request.files:
        text = request.files['uploadfile'].read()
    else:
        return render_template(
            'error.html',
            **statics().update(
                {'errormessage': "Er is geen geldige tekst ingevoerd!"}))

    #Verify checkum and other measures to counter spammers
    d = datetime.datetime.utcnow()
    try:
        if int(request.form['checksum']) != d.year + d.month + d.day:
            return render_template(
                'error.html',
                **statics().update({
                    'errormessage':
                    "Invalid checksum, are you sure you are human? If not, begone!"
                }))
    except:
        return render_template(
            'error.html',
            **statics().update({
                'errormessage':
                "Invalid checksum, are you sure you are human? If not, begone!"
            }))
    if text.find("href=") != -1 or text.find("<iframe") != -1 or text.find(
            "<img") != -1:
        return render_template(
            'error.html',
            **statics().update({
                'errormessage':
                "Je invoer moet bestaan uit platte tekst, er zijn HTML elementen gedetecteerd en deze kunnen niet verwerkt worden"
            }))
    elif text.find("[url=") != -1 or text.find("[img]") != -1:
        return render_template(
            'error.html',
            **statics().update({
                'errormessage':
                "Je invoer moet bestaan uit platte tekst, er zijn BBCode elementen gedetecteerd en deze kunnen niet verwerkt worden"
            }))
    elif text.count("http") > 10:
        return render_template(
            'error.html',
            **statics().update({
                'errormessage':
                "Je invoer bevat teveel webaddressen in de tekst, dit is om spam tegen te gaan niet toegestaan"
            }))

    #generate a random ID for this project
    doc_id = 'D' + hex(random.getrandbits(128))[2:-1]

    #create CLAM client
    if 'username' in settings:
        client = clam.common.client.CLAMClient(settings['url'],
                                               settings['username'],
                                               settings['password'],
                                               basicauth=True)
    else:
        client = clam.common.client.CLAMClient(settings['url'])

    #creat project
    client.create(doc_id)

    #get specification
    clamdata = client.get(doc_id)

    #add input file
    try:
        client.addinput(id,
                        clamdata.inputtemplate('textinput'),
                        text,
                        filename=doc_id + '.txt',
                        encoding='utf-8')
    except Exception as e:
        try:
            client.delete(doc_id)
        except:
            pass
        return render_template(
            'error.html',
            **statics().update({
                'errormessage':
                "Kon het gekozen bestand niet toevoegen. Dit kan meerdere oorzaken hebben. Valkuil accepteert momenteel alleen platte UTF-8 gecodeerde tekst-bestanden. Met name Microsoft Word bestanden zijn nog niet ondersteund in dit stadium!",
                'debugmessage': str(e)
            }))

    donate = 'donate' in request.form and request.form['donate'] == 'yes'

    #start CLAM
    client.start(doc_id,
                 sensitivity=request.form['sensitivity'],
                 donate=donate)

    while clamdata.status != clam.common.status.DONE:
        clamdata = client.get(doc_id)
        if clamdata == clam.common.status.DONE:
            break
        else:
            time.sleep(1)  #wait 1 second before polling status again

    #retrieve output file
    found = False
    redirect_error = None
    url = None
    for outputfile in clamdata.output:
        if str(outputfile)[-4:] == '.xml':
            #there should be only one FoLiA output file
            try:
                outputfile.loadmetadata()
            except:
                continue
            if outputfile.metadata.provenance.outputtemplate_id == 'foliaoutput' and not found:
                outputfile.copy(os.path.join(tmpdir, doc_id + '.xml'))
                found = True
                response = requests.get(str(outputfile) + "/flatviewer",
                                        auth=(settings['username'],
                                              settings['password']))
                if response.status_code == 302:
                    url = r.headers['Location']
                else:
                    redirect_error = response.status_code
        elif outputfile == 'error.log':
            outputfile.copy(os.path.join(tmpdir, doc_id + '.log'))

    #remove project
    client.delete(doc_id)

    if redirect_error:
        return render_template(
            'error.html',
            **statics().update({
                'errormessage':
                "Internal connection problem; Unable to visualise output (connection to FLAT failed with HTTP {redirect_error}"
                .format(redirect_error=redirect_error),
                'debugmessage':
                str(e)
            }))
    elif found:
        return redirect(url)
    else:
        return render_template(
            'error.html',
            **statics().update({
                'errormessage':
                "Unable to retrieve file from CLAM service",
                'debugmessage':
                " ".join([str(x) for x in clamdata.output])
            }))
Exemple #5
0
def process(request):
    """Process a given text and redirect to viewer"""
    if 'text' in request.REQUEST and request.REQUEST['text']:
        text = request.REQUEST['text']
    elif 'uploadfile' in request.FILES:
        text = request.FILES['uploadfile'].read()
    else:
        return render_to_response('error.html',{'errormessage': "Er is geen geldige tekst ingevoerd!"} )

    #Verify checkum and other measures to counter spammers
    d = datetime.datetime.utcnow()
    try:
        if int(request.REQUEST['checksum']) != d.year + d.month + d.day:
            return render_to_response('error.html',{'errormessage': "Invalid checksum, are you sure you are human? If not, begone!"} )
    except:
        return render_to_response('error.html',{'errormessage': "Invalid checksum, are you sure you are human? If not, begone!"} )
    if text.find("href=") != -1 or text.find("<iframe") != -1 or text.find("<img") != -1:
        return render_to_response('error.html',{'errormessage': "Je invoer moet bestaan uit platte tekst, er zijn HTML elementen gedetecteerd en deze kunnen niet verwerkt worden"} )
    elif text.find("[url=") != -1 or text.find("[img]") != -1:
        return render_to_response('error.html',{'errormessage': "Je invoer moet bestaan uit platte tekst, er zijn BBCode elementen gedetecteerd en deze kunnen niet verwerkt worden"} )
    elif text.count("http") > 10:
        return render_to_response('error.html',{'errormessage': "Je invoer bevat teveel webaddressen in de tekst, dit is om spam tegen te gaan niet toegestaan"} )


    #generate a random ID for this project
    id = 'D' + hex(random.getrandbits(128))[2:-1]

    #create CLAM client
    if 'CLAMUSER' in dir(settings):
        client = clam.common.client.CLAMClient(settings.CLAMSERVICE,settings.CLAMUSER, settings.CLAMPASS)
    else:
        client = clam.common.client.CLAMClient(settings.CLAMSERVICE)

    #creat project
    client.create(id)

    #get specification
    clamdata = client.get(id)

    #add input file
    try:
        client.addinput(id, clamdata.inputtemplate('textinput'), text, filename=id +'.txt',encoding='utf-8')
    except Exception as e:
        try:
            client.delete(id)
        except:
            pass
        return render_to_response('error.html',{'errormessage': "Kon het gekozen bestand niet toevoegen. Dit kan meerdere oorzaken hebben. Valkuil accepteert momenteel alleen platte UTF-8 gecodeerde tekst-bestanden. Met name Microsoft Word bestanden zijn nog niet ondersteund in dit stadium!",'debugmessage':str(e)} )

    if 'donate' in request.REQUEST and request.REQUEST['donate'] == 'yes':
        donate = True
    else:
        donate = False

    #start CLAM
    client.start(id, sensitivity=request.REQUEST['sensitivity'], donate=donate)

    while clamdata.status != clam.common.status.DONE:
        clamdata = client.get(id)
        if clamdata == clam.common.status.DONE:
            break
        else:
            time.sleep(1) #wait 1 second before polling status again

    #retrieve output file
    found = False
    for outputfile in clamdata.output:
        if str(outputfile)[-4:] == '.xml':
            try:
                outputfile.loadmetadata()
            except:
                continue
            if outputfile.metadata.provenance.outputtemplate_id == 'foliaoutput' and not found:
                outputfile.copy(settings.DOCDIR + id + '.xml')
                found = True
        elif outputfile == 'error.log':
                outputfile.copy(settings.DOCDIR + id + '.log')



    if not found:
        return render_to_response('error.html', {'errormessage': "Unable to retrieve file from CLAM service", 'debugmessage': " ".join([ str(x) for x in clamdata.output ])  })
        return HttpResponseForbidden("Unable to retrieve file from CLAM Service")

    #remove project
    client.delete(id)

    return redirect('/' + id + '/', permanent=False)