int main() { { // Using archive directly std::string buffer; { person john; init_person(john); cppcms::archive a; john.save(a); buffer = a.str(); } { person john; cppcms::archive a; a.str(buffer); john.load(a); std::cout << john.name << " " << john.children.front().name << " " << john.friends.front() << std::endl; } } { // Generic way std::string buffer; { person john; init_person(john); cppcms::serialization_traits<person>::save(john,buffer); } { person john; cppcms::serialization_traits<person>::load(buffer,john); std::cout << john.name << " " << john.children.front().name << " " << john.friends.front() << std::endl; } } return 0; }
void main(std::string /*url*/) { { person me; if(session().is_set("me")) { session().fetch_data("me",me); me.salary += 1; session().store_data("me",me); } } { person john; if(!cache().fetch_data("john",john)) { init_person(john); cache().store_data("john",john); } } }
/** Benutzer anmelden (Passwort Überprüfen) */ int main(int argc, char ** argv) { cgi datCGI; init_CGI(&datCGI); person login_person; init_person(&login_person); //fprintf(stderr, "Hallo vor Post\n"); get_CGI_data(&datCGI); if(datCGI.request_method != POST) { print_exit_failure("Use POST!"); } //fprintf(stderr, "POST_DATA: %s", datCGI.POST_data); //Aus POST_data den String zwischen <AttributName>= und '&' ausschneiden extract_POST_data(&datCGI, "email", &login_person.email); remove_newline(login_person.email); extract_POST_data(&datCGI, "pass", &login_person.password); remove_newline(login_person.password); if(login_person.email == NULL) { httpSetCookie("EMAIL", "NULL"); httpSetCookie("SID", "0"); httpCacheControl("no-cache"); char * redirectString=NULL; asprintf(&redirectString, "https://%s/incorrect_password.html", datCGI.http_host); httpRedirect(redirectString); } //fprintf(stderr, "POST_DATA: %s", datCGI.POST_data); //TODO: Verhindern, dass sich ein anderer Nutzer vom selben Rechner aus einloggt wenn der erste noch nicht abgemeldet ist //(zweimaliges Anmelden verhindern) //Das ist sehr unwahrscheinlich /* if(datCGI.http_cookies != NULL){ person already_logged_in_person; init_person(&already_logged_in_person); char * cook_sid=NULL; if(extract_COOKIE_data(&datCGI, "EMAIL", &already_logged_in_person.email) == 0 && extract_COOKIE_data(&datCGI, "SID", &cook_sid) == 0){ //print_exit_failure("Hier ist schon jemand eingeloggt"); already_logged_in_person.sid=atoi(cook_sid); if(get_person_by_sid(&already_logged_in_person)){ print_exit_failure("Hier ist schon jemand eingeloggt"); } } }*/ UserState user_state=verify_user(&login_person); //Zwei cookies setzen if(user_state == PW_CORRECT || user_state == PW_CORRECT_ALREADY_LOGGED_IN) { httpSetCookie("EMAIL", login_person.email); char * sid_string; asprintf(&sid_string, "%d", login_person.sid); httpSetCookie("SID", sid_string); httpCacheControl("no-store, no-cache, must-revalidate, max-age=0"); char * redirectString=NULL; asprintf(&redirectString, "https://%s/cgi-bin/all_messages.cgi", datCGI.http_host); httpRedirect(redirectString); } if(user_state == PW_INCORRECT) { httpSetCookie("EMAIL", "NULL"); httpSetCookie("SID", "0"); httpCacheControl("no-store, no-cache, must-revalidate, max-age=0"); char * redirectString=NULL; asprintf(&redirectString, "https://%s/incorrect_password.html", datCGI.http_host); httpRedirect(redirectString); } /* httpHeader(HTML); printf("<!DOCTYPE html><head>\ <title>InfoWall -- Anmeldung</title>\ <meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\" />\ <meta name=\"viewport\" content=\"width=device-width\">\ </head>\ <body>"); printf("%s\n", datCGI.POST_data); puts("<h1>Erhaltene Daten:</h1>\n"); printf("<br>CONTENT_LENGTH: %d -- REQUEST_METHOD: %s\n", datCGI.content_length, datCGI.request_method); printf("<br>Name: %s\nPassword: %s\n", login_person.email, login_person.password); printf("<br>Post Data: %s\n", datCGI.POST_data); puts("<br>\n\n\n\n"); if(login_person.auth && user_state==0){ puts("<h2>Personendaten:</h2>\n"); printf("<br>User ID: %d\n", login_person.id); printf("<br>Vorname: %s\n", login_person.first_name); printf("<br>Nachname: %s\n", login_person.name); printf("<br>Email: %s\n", login_person.email); printf("<br>Passwort: %s (richtig)\n", login_person.password); printf("<br>Faecher: %s\n", login_person.courses); if(login_person.isTeacher)printf("<br>Kuerzel: %s\n", login_person.acronym); printf("<br>SID: %d\n", login_person.sid); puts("<a href=\"/cgi-bin/logout.cgi\" style=\"color: green;\">LOGOUT</a>\ <br><a href=\"/cgi-bin/all_messages.cgi\">Alle Nachrichten</a>"); puts("<iframe src=\"/cgi-bin/all_messages.cgi\" style=\"width: 100%; height: 500px;\""); }else{ puts("<br>YOU FAIL!!\n"); if(user_state == 1){ puts("Bereits angemeldet!"); printf("<a href=\"/cgi-bin/logout.cgi\">LOGOUT</a>\ <br><a href=\"/cgi-bin/all_messages.cgi\">Alle Nachrichten</a>"); } } printf("</body>\ </html>");*/ exit(0); }
int main(int argc, char ** argv){ cgi datCGI; char * teach=NULL; char * acceptTOS=NULL; person reg_person; bool pw_short=false; init_person(®_person); init_CGI(&datCGI); get_CGI_data(&datCGI); if(datCGI.request_method == GET){ print_exit_failure("Use POST!"); } //Für die Namen: siehe HTML-Dokument mit entsprechenden <input>-Elementen extract_POST_data(&datCGI, "name_vor", ®_person.first_name); remove_newline(reg_person.first_name); clean_string(reg_person.first_name); extract_POST_data(&datCGI, "name", ®_person.name); remove_newline(reg_person.name); clean_string(reg_person.name); extract_POST_data(&datCGI, "email", ®_person.email); remove_newline(reg_person.email); clean_string(reg_person.email); extract_POST_data(&datCGI, "pass", ®_person.password); remove_newline(reg_person.password); extract_POST_data(&datCGI, "acronym", ®_person.acronym); remove_newline(reg_person.acronym); clean_string(reg_person.acronym); extract_POST_data(&datCGI, "teach", &teach); remove_newline(teach); extract_POST_data(&datCGI, "acceptTOS", &acceptTOS); remove_newline(acceptTOS); //TODO: fehlerhaften Aufruf abfangen if(strcmp(teach, "true") == 0){ reg_person.isTeacher=true; if(strlen(reg_person.acronym) != 3){ print_html_error("Das Kürzel muss genau 3 Zeichen lang sein", "/registrierung.html"); exit(EXIT_FAILURE); } }else{ reg_person.isTeacher=false; } //Die E-Mail-Adresse muss genau EIN '@' beinhalten if((strchr(reg_person.email, '@') == strrchr(reg_person.email, '@')) && strchr(reg_person.email, '@')) { #ifdef DEBUG fprintf(stderr, "es scheint alles zu passen (EMAIL)\n"); #endif // DEBUG if(strlen(reg_person.password)<8){ pw_short=true; } insert_user(®_person); } //fprintf(stderr, "\nnow comes da htmlz\n"); httpCacheControl("no-store, no-cache, must-revalidate, max-age=0"); httpHeader(HTML); //printf("%s\n", datCGI.POST_data); print_html_head("Passwort erneut eingeben", "Verifikation"); puts("<body>\n\ <div id='login-form'>\n"); printf("<p><span>Herzlich willkommen <span style='font-weight: bold;'>%s %s.</span><br>Bitte %s zum Anmelden %s Passwort ein</p>\n", reg_person.first_name, reg_person.name, reg_person.isTeacher ? "geben Sie" : "gib", reg_person.isTeacher ? "Ihr" : "dein" ); printf("<form method='post' action='/cgi-bin/login.cgi' style='border-radius: 1em; padding: 1em;' autocomplete='off'>\n\ <input type='hidden' name='email' value='%s' />\n\ <input class='textIn' placeholder='Passwort' type='password' id='pass' name='pass' required>\n\ <button class='submitButton' type='submit'>Anmelden*</button>\n\ </form>\n",reg_person.email); puts("<small>* Cookies müssen aktiviert sein!</small>\n"); if(pw_short){ puts("<br><small style='color: yellow; background-color: red;'>Sie sollten wirklich ein längeres Passwort verwenden!!</small>\n"); } puts("</div>\n</body>\n</html>\n"); /*puts("Erhaltene Daten:\n"); printf("CONTENT_LENGTH: %d\n", datCGI.content_length); printf("Name: %s\nPassword: %s\n", reg_person.name, reg_person.password); printf("Kuerzel: %s\nTeach: %s\n", reg_person.acronym, teach); printf("accepted TOS: %s\n\n", acceptTOS); printf("Post Data: %s\n", datCGI.POST_data);*/ exit(0); }