Commit 8a20a0f9 authored by jan.koester's avatar jan.koester
Browse files

added contact form

parent 37946522
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -7,3 +7,4 @@ add_subdirectory(navbar)
add_subdirectory(media)
add_subdirectory(nginxfiler)
add_subdirectory(template)
add_subdirectory(contact)
+24 −0
Original line number Diff line number Diff line
configure_file(CMakeLists.txt.in smtpclientlibrary-download/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/smtpclientlibrary-download )
if(result)
  message(FATAL_ERROR "CMake step for smtpclientlibrary failed: ${result}")
endif()
execute_process(COMMAND ${CMAKE_COMMAND} --build .
  RESULT_VARIABLE result
  WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/smtpclientlibrary-download )
if(result)
  message(FATAL_ERROR "Build step for smtpclientlibrary failed: ${result}")
endif()


add_library(contact SHARED contact.cpp)

target_include_directories(contact PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/smtpclientlibrary-src/src")

target_link_libraries(contact PRIVATE smtpclient)
target_link_libraries(contact PUBLIC dl blogidev)

install (TARGETS contact DESTINATION lib/blogi/plugins)
+15 −0
Original line number Diff line number Diff line
cmake_minimum_required(VERSION 3.10)

project(smtpclientlibrary-download NONE)

include(ExternalProject)
ExternalProject_Add(smtpclientlibrary
  GIT_REPOSITORY    https://github.com/jeremydumais/CPP-SMTPClient-library.git
  GIT_TAG           master
  SOURCE_DIR        "${CMAKE_CURRENT_BINARY_DIR}/smtpclientlibrary-src"
  BINARY_DIR        "${CMAKE_CURRENT_BINARY_DIR}/smtpclientlibrary-build"
  CONFIGURE_COMMAND ""
  BUILD_COMMAND     ""
  INSTALL_COMMAND   ""
  TEST_COMMAND      ""
)
+156 −0
Original line number Diff line number Diff line
/*******************************************************************************
 * Copyright (c) 2024, Jan Koester jan.koester@gmx.net
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 * Neither the name of the <organization> nor the
 *      names of its contributors may be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *******************************************************************************/



#include <iostream>
#include <algorithm>

#include <cpp/opportunisticsecuresmtpclient.hpp>
#include <cpp/plaintextmessage.hpp>

#include <htmlpp/html.h>
#include <htmlpp/exception.h>

#include <httppp/http.h>
#include <httppp/exception.h>

#include <database.h>
#include <auth.h>
#include <conf.h>
#include <plugin.h>
#include <session.h>

namespace blogi {
    class Contact : public PluginApi{
    public:

        void initPlugin(){
            _SHost=Args->config->getValue(Args->config->getKey("/BLOGI/CONTACT/SMTPADDR"),0);
            _SPort=Args->config->getIntValue(Args->config->getKey("/BLOGI/CONTACT/SMTPPORT"),0);
            _SUser=Args->config->getValue(Args->config->getKey("/BLOGI/CONTACT/SMTPUSER"),0);
            _SPass=Args->config->getValue(Args->config->getKey("/BLOGI/CONTACT/SMTPPASS"),0);
            _Sender=Args->config->getValue(Args->config->getKey("/BLOGI/CONTACT/SENDER"),0);
            _Receiver=Args->config->getValue(Args->config->getKey("/BLOGI/CONTACT/RECEIVER"),0);
            std::cout << "contact plugin loaded" <<std::endl;
        }

        bool Controller(const int tid,libhttppp::HttpRequest *req,libhtmlpp::HtmlElement *page,const char *sessionid){
            libhttppp::HttpForm curform;
            curform.parse(req);

            char buf[512];
            std::string name,mail;
            std::vector<char> message;

            for (libhttppp::HttpForm::UrlcodedForm::Data* cururlform = curform.UrlFormData.getFormData(); cururlform;
                cururlform = cururlform->nextData()) {
                    if(strcmp(cururlform->getKey(),"name")==0 && cururlform->getValue()){
                        name=cururlform->getValue();
                    }else if(strcmp(cururlform->getKey(),"mail")==0 && cururlform->getValue()){
                        mail=cururlform->getValue();
                    }else if(strcmp(cururlform->getKey(),"message")==0 && cururlform->getValue()){
                        std::copy(cururlform->getValue(),cururlform->getValue()+strlen(cururlform->getValue()),std::back_inserter(message));
                    }

            }

            message.push_back('\0');

            if(!mail.empty() && !name.empty() && !message.empty()){

                std::string subject="Message from Blogi(";

                subject+=name;
                subject+=')';

                jed_utils::OpportunisticSecureSMTPClient client(_SHost.c_str(),_SPort);
                client.setCredentials(jed_utils::Credential(_SUser.c_str(),_SPass.c_str()));
                try	{
                    jed_utils::PlaintextMessage msg(jed_utils::MessageAddress(_Sender.c_str(), "Blogi Contact Form"),
                                                    { jed_utils::MessageAddress(_Receiver.c_str()) },
                                                    subject.c_str(),
                                                    message.data());

                    int err_no = client.sendMail(msg);
                    if (err_no != 0) {
                        std::cerr << client.getCommunicationLog() << '\n';
                        std::string errorMessage = client.getErrorMessage(err_no);
                        std::cerr << "An error occurred: " << errorMessage
                        << " (error no: " << err_no << ")" << '\n';
                        return 1;
                    }
                }
                catch (std::invalid_argument &err) {
                    std::cerr << err.what() << std::endl;
                }
            }
            libhtmlpp::HtmlString cform,out;

            cform << "<div><form method=\"post\" action=\"" << Args->config->buildurl("contact",buf,512)
                  <<"\" ><p><label>Name: <input type=\"text\" id=\"name\" name=\"name\" ></label><label>Email:"
                  << " <input id=\"mail\" name=\"mail\" type=\"text\"></label></p><p><label>Text: <textarea id=\"message\""
                  << " name=\"message\" ></textarea></label></p><p><input type=\"submit\" value=\"Senden\"></p></form></div>";

            libhtmlpp::HtmlElement *hmain;
            if( (hmain=page->getElementbyID("main")) )
                hmain->appendChild(cform.parse());

            Args->theme->printSite(tid,out,page,req->getRequestURL(),Args->auth->isLoggedIn(tid,req,sessionid));

            libhttppp::HttpResponse curres;
            curres.setVersion(HTTPVERSION(1.1));
            curres.setContentType("text/html");
            curres.setState(HTTP200);
            curres.send(req, out.c_str(),out.size());
            return true;
        };

        const char *getName(){
            return "contact";
        }

        const char *getVersion(){
            return "0.2alpha";
        }

        const char *getAuthor(){
            return "Jan Koester";
        }
    private:
        std::string _SHost,_SUser,_SPass,_Sender,_Receiver;
        int _SPort;
    };
};

extern "C" blogi::PluginApi* create() {
    return new blogi::Contact();
}

extern "C" void destroy(blogi::PluginApi* p) {
    delete p;
}
+4 −0
Original line number Diff line number Diff line
@@ -633,6 +633,10 @@ namespace blogi {
            return "Jan Koester";
        }

        bool haveSettings(){
            return false;
        }

        void initPlugin(){
            blogi::SQL sql;
            blogi::DBResult res;
Loading