liquid_feedback_frontend
annotate fastpath/getpic.c @ 52:88ac7798b562
Several bugfixes (getpic.c, accepted but canceled issues, ...); Listing of available policies
- Bugfixes in fastpath/getpic.c (related to crashes since alpha5)
- Respect Content-Types of images in database
(needs database update, as Content-Type was incorrectly stored by previous versions)
- Typo fixed in help messages
- RSS-Feed (currently only after manual authentication while session is valid)
- Listing of available policies
- German translation fixed: "gebe" -> "gib" (Imperativ)
- Bugfixes related to issues which had been accepted but canceled afterwards
- Prohibit creation of initiatives in disabled areas or with disabled policies
- Bugfixes in fastpath/getpic.c (related to crashes since alpha5)
- Respect Content-Types of images in database
(needs database update, as Content-Type was incorrectly stored by previous versions)
- Typo fixed in help messages
- RSS-Feed (currently only after manual authentication while session is valid)
- Listing of available policies
- German translation fixed: "gebe" -> "gib" (Imperativ)
- Bugfixes related to issues which had been accepted but canceled afterwards
- Prohibit creation of initiatives in disabled areas or with disabled policies
author | bsw/jbe |
---|---|
date | Thu Apr 15 19:58:25 2010 +0200 (2010-04-15) |
parents | afd9f769c7ae |
children | 0f29051a49f6 |
rev | line source |
---|---|
bsw/jbe@4 | 1 #include <stdlib.h> |
bsw/jbe@4 | 2 #include <stdio.h> |
bsw/jbe@4 | 3 #include <libpq-fe.h> |
bsw/jbe@4 | 4 #include <string.h> |
bsw/jbe@4 | 5 #include <regex.h> |
bsw/jbe@4 | 6 #include <sys/types.h> |
bsw/jbe@4 | 7 #include <sys/stat.h> |
bsw/jbe@4 | 8 #include <unistd.h> |
bsw/jbe@4 | 9 |
bsw/jbe@4 | 10 #ifndef GETPIC_CONNINFO |
bsw/jbe@4 | 11 #define GETPIC_CONNINFO "dbname=liquid_feedback" |
bsw/jbe@4 | 12 #endif |
bsw/jbe@4 | 13 |
bsw/jbe@4 | 14 #ifndef GETPIC_DEFAULT_AVATAR |
bsw/jbe@4 | 15 #define GETPIC_DEFAULT_AVATAR "/opt/liquid_feedback_testing/app/static/avatar.jpg" |
bsw/jbe@4 | 16 #endif |
bsw/jbe@4 | 17 |
bsw/jbe@4 | 18 int main(int argc, const char * const *argv) { |
bsw/jbe@4 | 19 |
bsw/jbe@4 | 20 char *args_string; |
bsw/jbe@4 | 21 char *member_id; |
bsw/jbe@4 | 22 char *image_type; |
bsw/jbe@52 | 23 const char *sql_member_image_params[2]; |
bsw/jbe@4 | 24 |
bsw/jbe@5 | 25 char *cookies; |
bsw/jbe@5 | 26 regex_t session_ident_regex; |
bsw/jbe@4 | 27 ssize_t start, length; |
bsw/jbe@5 | 28 regmatch_t session_ident_regmatch[3]; |
bsw/jbe@4 | 29 char *session_ident; |
bsw/jbe@52 | 30 const char *sql_session_params[1]; |
bsw/jbe@4 | 31 |
bsw/jbe@5 | 32 PGconn *conn; |
bsw/jbe@5 | 33 PGresult *dbr; |
bsw/jbe@4 | 34 |
bsw/jbe@4 | 35 args_string = getenv("QUERY_STRING"); |
bsw/jbe@5 | 36 cookies = getenv("HTTP_COOKIE"); |
bsw/jbe@5 | 37 if (!args_string || !cookies) { |
bsw/jbe@4 | 38 fputs("Status: 403 Access Denied\n\n", stdout); |
bsw/jbe@4 | 39 return 0; |
bsw/jbe@4 | 40 } |
bsw/jbe@4 | 41 |
bsw/jbe@4 | 42 member_id = strtok(args_string, "+"); |
bsw/jbe@4 | 43 image_type = strtok(NULL, "+"); |
bsw/jbe@4 | 44 sql_member_image_params[0] = member_id; |
bsw/jbe@4 | 45 sql_member_image_params[1] = image_type; |
bsw/jbe@4 | 46 |
bsw/jbe@5 | 47 if (regcomp(&session_ident_regex, "(^|[; \t])liquid_feedback_session=([0-9A-Za-z]+)", REG_EXTENDED) != 0) { |
bsw/jbe@4 | 48 // shouldn't happen |
bsw/jbe@4 | 49 abort(); |
bsw/jbe@4 | 50 } |
bsw/jbe@52 | 51 if (regexec(&session_ident_regex, cookies, 3, session_ident_regmatch, 0) != 0) { |
bsw/jbe@4 | 52 fputs("Status: 403 Access Denied\n\n", stdout); |
bsw/jbe@4 | 53 return 0; |
bsw/jbe@4 | 54 } |
bsw/jbe@5 | 55 start = session_ident_regmatch[2].rm_so; |
bsw/jbe@5 | 56 length = session_ident_regmatch[2].rm_eo - session_ident_regmatch[2].rm_so; |
bsw/jbe@4 | 57 session_ident = malloc(length + 1); |
bsw/jbe@5 | 58 if (!session_ident) abort(); // shouldn't happen |
bsw/jbe@4 | 59 strncpy(session_ident, cookies + start, length); |
bsw/jbe@4 | 60 session_ident[length] = 0; |
bsw/jbe@4 | 61 sql_session_params[0] = session_ident; |
bsw/jbe@4 | 62 |
bsw/jbe@4 | 63 conn = PQconnectdb(GETPIC_CONNINFO); |
bsw/jbe@4 | 64 if (!conn) { |
bsw/jbe@4 | 65 fputs("Could not create PGconn structure.\n", stderr); |
bsw/jbe@4 | 66 return 1; |
bsw/jbe@4 | 67 } |
bsw/jbe@4 | 68 if (PQstatus(conn) != CONNECTION_OK) { |
bsw/jbe@4 | 69 fputs(PQerrorMessage(conn), stderr); |
bsw/jbe@5 | 70 PQfinish(conn); |
bsw/jbe@4 | 71 return 1; |
bsw/jbe@4 | 72 } |
bsw/jbe@4 | 73 |
bsw/jbe@4 | 74 dbr = PQexecParams(conn, |
bsw/jbe@4 | 75 "SELECT NULL FROM session JOIN member ON member.id = session.member_id WHERE session.ident = $1 AND member.active", |
bsw/jbe@4 | 76 1, NULL, sql_session_params, NULL, NULL, 0 |
bsw/jbe@4 | 77 ); |
bsw/jbe@4 | 78 if (PQresultStatus(dbr) != PGRES_TUPLES_OK) { |
bsw/jbe@4 | 79 fputs(PQresultErrorMessage(dbr), stderr); |
bsw/jbe@5 | 80 PQfinish(conn); |
bsw/jbe@4 | 81 return 1; |
bsw/jbe@4 | 82 } |
bsw/jbe@4 | 83 if (PQntuples(dbr) != 1) { |
bsw/jbe@4 | 84 fputs("Status: 403 Access Denied\n\n", stdout); |
bsw/jbe@5 | 85 PQfinish(conn); |
bsw/jbe@4 | 86 return 0; |
bsw/jbe@4 | 87 } |
bsw/jbe@4 | 88 |
bsw/jbe@4 | 89 dbr = PQexecParams(conn, |
bsw/jbe@4 | 90 "SELECT content_type, data " |
bsw/jbe@4 | 91 "FROM member_image " |
bsw/jbe@4 | 92 "WHERE member_id = $1 " |
bsw/jbe@4 | 93 "AND image_type = $2 " |
bsw/jbe@4 | 94 "AND scaled " |
bsw/jbe@4 | 95 "LIMIT 1;", |
bsw/jbe@4 | 96 2, NULL, sql_member_image_params, NULL, NULL, 1 |
bsw/jbe@4 | 97 ); |
bsw/jbe@4 | 98 if (PQresultStatus(dbr) != PGRES_TUPLES_OK) { |
bsw/jbe@4 | 99 fputs(PQresultErrorMessage(dbr), stderr); |
bsw/jbe@5 | 100 PQfinish(conn); |
bsw/jbe@4 | 101 return 1; |
bsw/jbe@4 | 102 } |
bsw/jbe@4 | 103 if (PQntuples(dbr) == 0) { |
bsw/jbe@4 | 104 struct stat sb; |
bsw/jbe@4 | 105 PQclear(dbr); |
bsw/jbe@4 | 106 PQfinish(conn); |
bsw/jbe@4 | 107 fputs("Content-Type: image/jpeg\n\n", stdout); |
bsw/jbe@4 | 108 if (stat(GETPIC_DEFAULT_AVATAR, &sb)) return 1; |
bsw/jbe@5 | 109 fprintf(stdout, "Content-Length: %i\n", (int)sb.st_size); |
bsw/jbe@4 | 110 execl("/bin/cat", "cat", GETPIC_DEFAULT_AVATAR, NULL); |
bsw/jbe@4 | 111 return 1; |
bsw/jbe@4 | 112 } else { |
bsw/jbe@4 | 113 if (PQnfields(dbr) < 0) { |
bsw/jbe@4 | 114 fputs("Too few columns returned by database.\n", stderr); |
bsw/jbe@5 | 115 PQfinish(conn); |
bsw/jbe@4 | 116 return 1; |
bsw/jbe@4 | 117 } |
bsw/jbe@4 | 118 if (PQfformat(dbr, 0) != 1 || PQfformat(dbr, 1) != 1) { |
bsw/jbe@4 | 119 fputs("Database did not return data in binary format.\n", stderr); |
bsw/jbe@5 | 120 PQfinish(conn); |
bsw/jbe@4 | 121 return 1; |
bsw/jbe@4 | 122 } |
bsw/jbe@4 | 123 if (PQgetisnull(dbr, 0, 0) || PQgetisnull(dbr, 0, 1)) { |
bsw/jbe@4 | 124 fputs("Unexpected NULL in database result.\n", stderr); |
bsw/jbe@5 | 125 PQfinish(conn); |
bsw/jbe@4 | 126 return 1; |
bsw/jbe@4 | 127 } |
bsw/jbe@52 | 128 fprintf(stdout, "Content-Type: %s\n\n", PQgetvalue(dbr, 0, 0)); |
bsw/jbe@4 | 129 fwrite(PQgetvalue(dbr, 0, 1), PQgetlength(dbr, 0, 1), 1, stdout); |
bsw/jbe@4 | 130 } |
bsw/jbe@4 | 131 PQfinish(conn); |
bsw/jbe@4 | 132 return 0; |
bsw/jbe@5 | 133 |
bsw/jbe@4 | 134 } |