summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2015-04-26 08:13:38 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2015-04-26 08:13:38 +0200
commitef2fed182ff854786381be71c603ee2fea290e08 (patch)
tree752df53691b824e37f2319d59cced2b750f4fd6f
parent48b476289940862a9754f67752f0a092b0a1f0d2 (diff)
downloadaCms-ef2fed182ff854786381be71c603ee2fea290e08.tar.gz
aCms-ef2fed182ff854786381be71c603ee2fea290e08.tar.bz2
some refactoring: removed base64 attempts, captcha in separate class
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/base64.cpp0
-rw-r--r--src/base64.hpp4
-rw-r--r--src/captcha.cpp44
-rw-r--r--src/captcha.hpp13
-rw-r--r--src/user.cpp37
6 files changed, 63 insertions, 36 deletions
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
--- a/src/base64.cpp
+++ /dev/null
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 <b64/encode.h>
+#include <sstream>
+
+#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 <string>
+
+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 <cppcms/url_dispatcher.h>
#include <cppcms/url_mapper.h>
#include <cppdb/frontend.h>
#include <cppcms/session_interface.h>
#include <booster/posix_time.h>
-#include <b64/encode.h>
-#include <sstream>
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