From ef2fed182ff854786381be71c603ee2fea290e08 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Sun, 26 Apr 2015 08:13:38 +0200 Subject: some refactoring: removed base64 attempts, captcha in separate class --- CMakeLists.txt | 1 + src/base64.cpp | 0 src/base64.hpp | 4 ---- src/captcha.cpp | 44 ++++++++++++++++++++++++++++++++++++++++++++ src/captcha.hpp | 13 +++++++++++++ src/user.cpp | 37 +++++-------------------------------- 6 files changed, 63 insertions(+), 36 deletions(-) delete mode 100644 src/base64.cpp delete mode 100644 src/base64.hpp create mode 100644 src/captcha.cpp create mode 100644 src/captcha.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index e88ea9a..8a87524 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,7 @@ set(SRC src/strusCms.cpp src/master.cpp src/intro.cpp + src/captcha.cpp src/user.cpp ) diff --git a/src/base64.cpp b/src/base64.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/src/base64.hpp b/src/base64.hpp deleted file mode 100644 index 1c55655..0000000 --- a/src/base64.hpp +++ /dev/null @@ -1,4 +0,0 @@ -#ifndef BASE64_HPP -#define BASE64_HPP - -#endif diff --git a/src/captcha.cpp b/src/captcha.cpp new file mode 100644 index 0000000..2d44ebc --- /dev/null +++ b/src/captcha.cpp @@ -0,0 +1,44 @@ +#include "captcha.hpp" + +#include +#include + +#define GIFSIZE 17646 +extern "C" { + const int gifsize = GIFSIZE; + void captcha( unsigned char im[70*200], unsigned char l[6]); + void makegif(unsigned char im[70*200], unsigned char gif[gifsize]); +} + +struct captcha generateCaptcha( ) +{ + struct captcha c; + unsigned char l[6]; + unsigned char *im = new unsigned char[70*200]; + unsigned char *gif = new unsigned char[gifsize]; + + captcha( im, l ); + + c.text = std::string( (char *)l ); + + std::ostringstream raws( std::stringstream::out | std::stringstream::binary ); + makegif( im, gif ); + raws.write( (char *)gif, gifsize ); + std::string raw = raws.str( ); + + std::istringstream in( std::ios_base::in | std::ios_base::binary ); + in.str( raw ); + + base64::encoder E; + std::string base_s; + base_s.reserve( 3 * gifsize ); + std::ostringstream base( base_s, std::stringstream::out | std::stringstream::binary ); + E.encode( in, base ); + c.base64 = base.str( ); + + delete gif; + delete im; + + return c; +} + diff --git a/src/captcha.hpp b/src/captcha.hpp new file mode 100644 index 0000000..486522e --- /dev/null +++ b/src/captcha.hpp @@ -0,0 +1,13 @@ +#ifndef CAPTCHA_HPP +#define CAPTCHA_HPP + +#include + +struct captcha { + std::string text; // the text the user has to guess + std::string base64; // the GIF in BASE64 encoding +}; + +captcha generateCaptcha( ); + +#endif diff --git a/src/user.cpp b/src/user.cpp index 65ca2d4..ebc3b06 100644 --- a/src/user.cpp +++ b/src/user.cpp @@ -1,14 +1,13 @@ #include "content.hpp" #include "user.hpp" #include "strusCms.hpp" +#include "captcha.hpp" #include #include #include #include #include -#include -#include namespace apps { @@ -83,41 +82,15 @@ bool user::check_login( std::string user, std::string password ) return true; } -#define GIFSIZE 17646 -extern "C" { - const int gifsize = GIFSIZE; - void captcha( unsigned char im[70*200], unsigned char l[6]); - void makegif(unsigned char im[70*200], unsigned char gif[gifsize]); -} - void user::ini( content::user &c ) { master::ini( c ); - unsigned char l[6]; - unsigned char *im = new unsigned char[70*200]; - unsigned char *gif = new unsigned char[gifsize]; - - captcha( im, l ); + struct captcha ca = generateCaptcha( ); + last_captcha = new_captcha; - new_captcha = std::string( (char *)l ); + new_captcha = ca.text; - std::ostringstream raws( std::stringstream::out | std::stringstream::binary ); - makegif( im, gif ); - raws.write( (char *)gif, gifsize ); - std::string raw = raws.str( ); - - std::istringstream in( std::ios_base::in | std::ios_base::binary ); - in.str( raw ); - - base64::encoder E; - std::string base_s; - base_s.reserve( 3 * gifsize ); - std::ostringstream base( base_s, std::stringstream::out | std::stringstream::binary ); - E.encode( in, base ); - c.captcha_base64 = base.str( ); - - delete gif; - delete im; + c.captcha_base64 = ca.base64; } } // namespace apps -- cgit v1.2.3-54-g00ecf