liquid_feedback_frontend

annotate fastpath/getpic.c @ 1012:6c087fa4ba35

Fixed typo in admin cancel issue
author bsw
date Sun Aug 11 22:07:13 2013 +0200 (2013-08-11)
parents 2dfa6f5b7670
children 7f7142e949ee
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@871 25 #ifndef PUBLIC_ACCESS
bsw/jbe@5 26 char *cookies;
bsw/jbe@5 27 regex_t session_ident_regex;
bsw/jbe@4 28 ssize_t start, length;
bsw/jbe@5 29 regmatch_t session_ident_regmatch[3];
bsw/jbe@4 30 char *session_ident;
bsw/jbe@52 31 const char *sql_session_params[1];
bsw@871 32 #endif
bsw/jbe@4 33
bsw/jbe@5 34 PGconn *conn;
bsw/jbe@5 35 PGresult *dbr;
bsw/jbe@4 36
bsw/jbe@4 37 args_string = getenv("QUERY_STRING");
bsw@873 38 #ifdef PUBLIC_ACCESS
bsw@873 39 if (!args_string) {
bsw@873 40 fputs("Status: 403 Access Denied\n\n", stdout);
bsw@873 41 return 0;
bsw@873 42 }
bsw@873 43 #else
bsw/jbe@5 44 cookies = getenv("HTTP_COOKIE");
bsw/jbe@5 45 if (!args_string || !cookies) {
bsw/jbe@4 46 fputs("Status: 403 Access Denied\n\n", stdout);
bsw/jbe@4 47 return 0;
bsw/jbe@4 48 }
bsw@871 49 #endif
bsw/jbe@4 50
bsw/jbe@4 51 member_id = strtok(args_string, "+");
bsw/jbe@4 52 image_type = strtok(NULL, "+");
bsw/jbe@4 53 sql_member_image_params[0] = member_id;
bsw/jbe@4 54 sql_member_image_params[1] = image_type;
bsw/jbe@4 55
bsw@871 56 #ifndef PUBLIC_ACCESS
bsw/jbe@5 57 if (regcomp(&session_ident_regex, "(^|[; \t])liquid_feedback_session=([0-9A-Za-z]+)", REG_EXTENDED) != 0) {
bsw/jbe@4 58 // shouldn't happen
bsw/jbe@4 59 abort();
bsw/jbe@4 60 }
bsw/jbe@52 61 if (regexec(&session_ident_regex, cookies, 3, session_ident_regmatch, 0) != 0) {
bsw/jbe@4 62 fputs("Status: 403 Access Denied\n\n", stdout);
bsw/jbe@4 63 return 0;
bsw/jbe@4 64 }
bsw/jbe@5 65 start = session_ident_regmatch[2].rm_so;
bsw/jbe@5 66 length = session_ident_regmatch[2].rm_eo - session_ident_regmatch[2].rm_so;
bsw/jbe@4 67 session_ident = malloc(length + 1);
bsw/jbe@5 68 if (!session_ident) abort(); // shouldn't happen
bsw/jbe@4 69 strncpy(session_ident, cookies + start, length);
bsw/jbe@4 70 session_ident[length] = 0;
bsw/jbe@4 71 sql_session_params[0] = session_ident;
bsw@874 72 #endif
bsw/jbe@4 73
bsw/jbe@4 74 conn = PQconnectdb(GETPIC_CONNINFO);
bsw/jbe@4 75 if (!conn) {
bsw/jbe@4 76 fputs("Could not create PGconn structure.\n", stderr);
bsw/jbe@4 77 return 1;
bsw/jbe@4 78 }
bsw/jbe@4 79 if (PQstatus(conn) != CONNECTION_OK) {
bsw/jbe@4 80 fputs(PQerrorMessage(conn), stderr);
bsw/jbe@5 81 PQfinish(conn);
bsw/jbe@4 82 return 1;
bsw/jbe@4 83 }
bsw/jbe@4 84
bsw@874 85 #ifndef PUBLIC_ACCESS
bsw/jbe@4 86 dbr = PQexecParams(conn,
bsw/jbe@4 87 "SELECT NULL FROM session JOIN member ON member.id = session.member_id WHERE session.ident = $1 AND member.active",
bsw/jbe@4 88 1, NULL, sql_session_params, NULL, NULL, 0
bsw/jbe@4 89 );
bsw/jbe@4 90 if (PQresultStatus(dbr) != PGRES_TUPLES_OK) {
bsw/jbe@4 91 fputs(PQresultErrorMessage(dbr), stderr);
bsw/jbe@5 92 PQfinish(conn);
bsw/jbe@4 93 return 1;
bsw/jbe@4 94 }
bsw/jbe@4 95 if (PQntuples(dbr) != 1) {
bsw/jbe@4 96 fputs("Status: 403 Access Denied\n\n", stdout);
bsw/jbe@5 97 PQfinish(conn);
bsw/jbe@4 98 return 0;
bsw/jbe@4 99 }
bsw@871 100 #endif
bsw/jbe@4 101
bsw/jbe@4 102 dbr = PQexecParams(conn,
bsw/jbe@4 103 "SELECT content_type, data "
bsw/jbe@4 104 "FROM member_image "
bsw/jbe@4 105 "WHERE member_id = $1 "
bsw/jbe@4 106 "AND image_type = $2 "
bsw/jbe@4 107 "AND scaled "
bsw/jbe@4 108 "LIMIT 1;",
bsw/jbe@4 109 2, NULL, sql_member_image_params, NULL, NULL, 1
bsw/jbe@4 110 );
bsw/jbe@4 111 if (PQresultStatus(dbr) != PGRES_TUPLES_OK) {
bsw/jbe@4 112 fputs(PQresultErrorMessage(dbr), stderr);
bsw/jbe@5 113 PQfinish(conn);
bsw/jbe@4 114 return 1;
bsw/jbe@4 115 }
bsw/jbe@4 116 if (PQntuples(dbr) == 0) {
bsw/jbe@4 117 struct stat sb;
bsw/jbe@4 118 PQclear(dbr);
bsw/jbe@4 119 PQfinish(conn);
bsw/jbe@4 120 fputs("Content-Type: image/jpeg\n\n", stdout);
bsw/jbe@4 121 if (stat(GETPIC_DEFAULT_AVATAR, &sb)) return 1;
bsw/jbe@5 122 fprintf(stdout, "Content-Length: %i\n", (int)sb.st_size);
bsw/jbe@4 123 execl("/bin/cat", "cat", GETPIC_DEFAULT_AVATAR, NULL);
bsw/jbe@4 124 return 1;
bsw/jbe@4 125 } else {
bsw/jbe@4 126 if (PQnfields(dbr) < 0) {
bsw/jbe@4 127 fputs("Too few columns returned by database.\n", stderr);
bsw/jbe@5 128 PQfinish(conn);
bsw/jbe@4 129 return 1;
bsw/jbe@4 130 }
bsw/jbe@4 131 if (PQfformat(dbr, 0) != 1 || PQfformat(dbr, 1) != 1) {
bsw/jbe@4 132 fputs("Database did not return data in binary format.\n", stderr);
bsw/jbe@5 133 PQfinish(conn);
bsw/jbe@4 134 return 1;
bsw/jbe@4 135 }
bsw/jbe@4 136 if (PQgetisnull(dbr, 0, 0) || PQgetisnull(dbr, 0, 1)) {
bsw/jbe@4 137 fputs("Unexpected NULL in database result.\n", stderr);
bsw/jbe@5 138 PQfinish(conn);
bsw/jbe@4 139 return 1;
bsw/jbe@4 140 }
bsw/jbe@52 141 fprintf(stdout, "Content-Type: %s\n\n", PQgetvalue(dbr, 0, 0));
bsw/jbe@4 142 fwrite(PQgetvalue(dbr, 0, 1), PQgetlength(dbr, 0, 1), 1, stdout);
bsw/jbe@4 143 }
bsw/jbe@4 144 PQfinish(conn);
bsw/jbe@4 145 return 0;
bsw/jbe@5 146
bsw/jbe@4 147 }

Impressum / About Us