#if defined(ENABLE_WWW) #include #include #include #include #include "bbs.h" #include "../deps/hashids/hashids.h" extern struct bbs_config conf; extern struct user_record *gUser; extern char * aha(char *input); static char *www_decode(char *clean_url) { char *url = (char *)malloc(strlen(clean_url) + 1); int i; int j = 0; unsigned char c; if (clean_url == NULL) { free(url); return NULL; } for (i=0;i= conf.file_directory_count || sub >= conf.file_directories[dir]->file_sub_count) { return NULL; } // get filename from database snprintf(buffer, PATH_MAX, "%s/%s.sq3", conf.bbs_path, conf.file_directories[dir]->file_subs[sub]->database); rc = sqlite3_open(buffer, &db); if (rc != SQLITE_OK) { return NULL; } sqlite3_busy_timeout(db, 5000); rc = sqlite3_prepare_v2(db, sql, -1, &res, 0); if (rc != SQLITE_OK) { sqlite3_close(db); return NULL; } sqlite3_bind_int(res, 1, fid); if (sqlite3_step(res) == SQLITE_ROW) { ret = strdup(sqlite3_column_text(res, 0)); sqlite3_finalize(res); sqlite3_close(db); return ret; } sqlite3_finalize(res); sqlite3_close(db); return NULL; } char *www_create_link(int dir, int sub, int fid) { char url[PATH_MAX]; char *ret; char *hashid; int sizereq; time_t expiry; hashids_t *hashids = hashids_init(conf.bbs_name); sizereq = hashids_estimate_encoded_size_v(hashids, 4, (unsigned long long)gUser->id, (unsigned long long)dir, (unsigned long long)sub, (unsigned long long)fid); hashid = (char *)malloc(sizereq + 1); memset(hashid, 0, sizereq + 1); if (hashids_encode_v(hashids, hashid, 4, (unsigned long long)gUser->id, (unsigned long long)dir, (unsigned long long)sub, (unsigned long long)fid) == 0) { hashids_free(hashids); free(hashid); return NULL; } hashids_free(hashids); snprintf(url, PATH_MAX, "%sfiles/%s", conf.www_url, hashid); // add link into hash database expiry = time(NULL) + 86400; www_add_hash_to_db(hashid, expiry); free(hashid); ret = strdup(url); return ret; } char *www_files_display_listing(int dir, int sub) { char *page; int max_len; int len; char buffer[4096]; char *sql = "select id, filename, description, size, dlcount, uploaddate from files where approved=1 ORDER BY filename"; char *filename; char c; int size; char *aha_out; char *description; sqlite3 *db; sqlite3_stmt *res; int rc; int i; char *clean_url; page = (char *)malloc(4096); max_len = 4096; len = 0; memset(page, 0, 4096); snprintf(buffer, 4096, "

Files: %s - %s

\n", conf.file_directories[dir]->name, conf.file_directories[dir]->file_subs[sub]->name); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); snprintf(buffer, 4096, "%s/%s.sq3", conf.bbs_path, conf.file_directories[dir]->file_subs[sub]->database); rc = sqlite3_open(buffer, &db); if (rc != SQLITE_OK) { free(page); return NULL; } sqlite3_busy_timeout(db, 5000); rc = sqlite3_prepare_v2(db, sql, -1, &res, 0); if (rc != SQLITE_OK) { sqlite3_close(db); free(page); return NULL; } snprintf(buffer, 4096, "\n"); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); while (sqlite3_step(res) == SQLITE_ROW) { filename = strdup(sqlite3_column_text(res, 1)); clean_url = www_encode(basename(filename)); snprintf(buffer, 4096, "", conf.www_url, dir, sub, basename(clean_url), basename(filename)); free(clean_url); free(filename); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); size = sqlite3_column_int(res, 3); if (size > 1024 * 1024 * 1024) { size = size / 1024 / 1024 / 1024; c = 'G'; } else if (size > 1024 * 1024) { size = size / 1024 / 1024; c = 'M'; } else if (size > 1024) { size = size / 1024; c = 'K'; } else { c = 'b'; } snprintf(buffer, 4096, "", size, c); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); description = strdup((char *)sqlite3_column_text(res, 2)); for (i=0;i"); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); aha_out = aha(description); while (len + strlen(aha_out) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, aha_out); len += strlen(aha_out); free(aha_out); free(description); snprintf(buffer, 4096, "\n"); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); } snprintf(buffer, 4096, "
FilenameSizeDescription
%s%d%c
\n"); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); sqlite3_finalize(res); sqlite3_close(db); return page; } char *www_files_areas() { char *page; int max_len; int len; char buffer[4096]; int i; int j; page = (char *)malloc(4096); max_len = 4096; len = 0; memset(page, 0, 4096); sprintf(buffer, "

File Directories

\n"); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); for (i=0;idisplay_on_web) { sprintf(buffer, "
%s
\n", conf.file_directories[i]->name); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); for (j=0;jfile_sub_count;j++) { sprintf(buffer, "\n", conf.www_url, i, j, conf.file_directories[i]->file_subs[j]->name); if (len + strlen(buffer) > max_len - 1) { max_len += 4096; page = (char *)realloc(page, max_len); } strcat(page, buffer); len += strlen(buffer); } } } return page; } char *www_files_get_from_area(int dir, int sub, char *clean_file) { char *sql = "SELECT filename FROM files WHERE approved=1 AND filename LIKE ? ESCAPE \"^\""; char *filenamelike; sqlite3 *db; sqlite3_stmt *res; int rc; char buffer[PATH_MAX]; char *ret = NULL; int i; int extra = 0; int j; char *file; file = www_decode(clean_file); for (i=0;ifile_subs[sub]->database); rc = sqlite3_open(buffer, &db); if (rc != SQLITE_OK) { return NULL; } sqlite3_busy_timeout(db, 5000); rc = sqlite3_prepare_v2(db, sql, -1, &res, 0); if (rc != SQLITE_OK) { sqlite3_close(db); return NULL; } sqlite3_bind_text(res, 1, filenamelike, -1, 0); rc = sqlite3_step(res); if (rc == SQLITE_ROW) { ret = strdup(sqlite3_column_text(res, 0)); } free(filenamelike); sqlite3_finalize(res); sqlite3_close(db); return ret; } #endif