diff options
author | kx <kx@radix.pro> | 2023-03-24 03:51:10 +0300 |
---|---|---|
committer | kx <kx@radix.pro> | 2023-03-24 03:51:10 +0300 |
commit | 05d292b208dfe01324826b4c87bbc4da3389a0d5 (patch) | |
tree | b10a2269e9320785f3b61189e75f6778fa167986 /cgitcgi/ui-shared.c | |
parent | 40ab18a661ff6ada40e73969be293918d346a2f5 (diff) | |
download | cgit-ui-05d292b208dfe01324826b4c87bbc4da3389a0d5.tar.xz |
Version 0.1.7
Diffstat (limited to 'cgitcgi/ui-shared.c')
-rw-r--r-- | cgitcgi/ui-shared.c | 755 |
1 files changed, 755 insertions, 0 deletions
diff --git a/cgitcgi/ui-shared.c b/cgitcgi/ui-shared.c new file mode 100644 index 0000000..8a51d0d --- /dev/null +++ b/cgitcgi/ui-shared.c @@ -0,0 +1,755 @@ + +#ifdef HAVE_CONFIG_H +#include <config.h> +#endif + +#include <stdlib.h> +#include <stdio.h> +#include <sys/sysinfo.h> +#include <sys/types.h> +#ifdef HAVE_INTTYPES_H +#include <inttypes.h> +#else +#include <stdint.h> +#endif +#include <stddef.h> /* offsetof(3) */ +#include <dirent.h> +#include <sys/stat.h> /* chmod(2) */ +#include <sys/file.h> +#include <sys/mman.h> +#include <fcntl.h> +#include <limits.h> +#include <string.h> /* strdup(3) */ +#include <libgen.h> /* basename(3) */ +#include <ctype.h> /* tolower(3) */ +#include <errno.h> +#include <time.h> +#include <sys/time.h> +#include <pwd.h> +#include <grp.h> +#include <stdarg.h> +#include <locale.h> +#include <math.h> +#include <unistd.h> + +#include <git2.h> + +#include <nls.h> + +#include <defs.h> + +#include <fatal.h> +#include <http.h> +#include <html.h> + +#include <dlist.h> +#include <strbuf.h> +#include <repolist.h> +#include <wrapper.h> +#include <system.h> +#include <date.h> + +#include <ctx.h> +#include <git-shared.h> +#include <ui-shared.h> + + +void cgit_change_location( const char *location ) +{ + if( ctx.env.no_http && !strcmp( ctx.env.no_http, "1" ) ) + return; + + htmlf( "Status: %d %s\n", 302, "Found" ); + htmlf( "Location: %s\n", location ); + html( "\n" ); + + exit( 0 ); +} + +void cgit_print_http_headers( void ) +{ + if( ctx.env.no_http && !strcmp( ctx.env.no_http, "1" ) ) + return; + + /***************************************************** + If we pass 'Status: 200 OK' header then nginx+uWsgi + will duplicate other headers randomly + */ + if( ctx.page.status != 200 ) + htmlf( "Status: %d %s\n", ctx.page.status, ctx.page.status_message ); + + if( ctx.page.mimetype && ctx.page.charset ) + htmlf( "Content-Type: %s; charset=%s\n", ctx.page.mimetype, ctx.page.charset ); + else if( ctx.page.mimetype ) + htmlf( "Content-Type: %s\n", ctx.page.mimetype ); + + if( ctx.page.size ) + htmlf( "Content-Length: %zd\n", ctx.page.size ); + + if( !ctx.env.authenticated ) + html( "Cache-Control: no-cache, no-store\n" ); + + htmlf( "Last-Modified: %s\n", http_date( ctx.page.modified ) ); + htmlf( "Expires: %s\n", http_date( ctx.page.expires ) ); + + html( "\n" ); +} + +const struct date_mode *cgit_date_mode( enum date_mode_type type ) +{ + static struct date_mode mode; + mode.type = type; + mode.local = 0; /* may be from ctx.cfg.local_time; =0/1 */ + return &mode; +} +/* OR data_mode_from_type(): + struct date_mode *mode = DATE_MODE( DATE_ISO8601 ); + but in this case mode.local is always zero! + */ + +static void print_rel_date( struct strbuf *sb, time_t t, int tz, + const char *class, const char *value ) +{ + if( !sb || !t ) return; + + strbuf_addf( sb, "<span class='%s' title='", class ); + show_date( sb, t, tz, cgit_date_mode( DATE_ISO8601 ) ); + strbuf_addf( sb, "'>%s</span>", value ); +} + +void cgit_print_age( struct strbuf *sb, time_t t, int tz, time_t max_relative ) +{ + time_t now, secs; + char buf[32] = { 0 }; + size_t val; + + if( !sb || !t ) return; + + now = time( NULL ); + secs = now - t; + if( secs < 0 ) + secs = 0; + + if( secs > max_relative && max_relative >= 0 ) + { + + strbuf_addstr( sb, "<span title='" ); + show_date( sb, t, tz, cgit_date_mode( DATE_ISO8601 ) ); + strbuf_addstr( sb, "'>" ); + show_date( sb, t, tz, cgit_date_mode( DATE_SHORT ) ); + strbuf_addstr( sb, "</span>" ); + return; + } + + if( secs < TM_HOUR * 2 ) + { + val = (size_t)round(secs * 1.0 / TM_MIN); + snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" minute", "%"PRIdMAX" minutes", val), val ); + print_rel_date( sb, t, tz, "age-mins", (const char *)&buf[0] ); + return; + } + if( secs < TM_DAY * 2 ) + { + val = (size_t)round(secs * 1.0 / TM_HOUR); + snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" hour", "%"PRIdMAX" hours", val), val ); + print_rel_date( sb, t, tz, "age-hours", (const char *)&buf[0] ); + return; + } + if( secs < TM_WEEK * 2 ) + { + val = (size_t)round(secs * 1.0 / TM_DAY); + snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" day", "%"PRIdMAX" days", val), val ); + print_rel_date( sb, t, tz, "age-days", (const char *)&buf[0] ); + return; + } + if( secs < TM_MONTH * 2 ) + { + val = (size_t)round(secs * 1.0 / TM_WEEK); + snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" week", "%"PRIdMAX" weeks", val), val ); + print_rel_date( sb, t, tz, "age-weeks", (const char *)&buf[0] ); + return; + } + if( secs < TM_YEAR * 2 ) + { + val = (size_t)round(secs * 1.0 / TM_MONTH); + snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" month", "%"PRIdMAX" months", val), val ); + print_rel_date( sb, t, tz, "age-months", (const char *)&buf[0] ); + return; + } + + val = (size_t)round(secs * 1.0 / TM_YEAR); + snprintf( (char *)&buf[0], 32, Q_("%"PRIdMAX" year", "%"PRIdMAX" years", val), val ); + print_rel_date( sb, t, tz, "age-years", (const char *)&buf[0] ); +} + +void cgit_search_repo( const char *path ) +{ + struct repo *repo = NULL; + + if( !path || !*path ) return; + + repo = lookup_repo( config, path ); + if( repo ) + { + int position = repo_position( config, repo ); + if( position != -1 ) + { + char location[1024] = { 0 }; + + sprintf( (char *)&location[0], "/?ofs=%d", position ); + cgit_change_location( (const char *)&location[0] ); + } + } +} + +void cgit_repo_info( struct cgit_info *info, const char *revision ) +{ + const char *name = NULL, *git_root = NULL, *repo_root = NULL; + + if( !info ) return; + + name = ctx.repo.name; + git_root = ctx.repo.git_root; + repo_root = ctx.repo.repo_root; + + if( name && git_root ) + { + char path[PATH_MAX] = { 0 }; + char cmd[PATH_MAX]; + struct strbuf buf = STRBUF_INIT; + pid_t p = (pid_t) -1; + int rc; + + sprintf( (char *)&path[0], "%s/", git_root ); + if( repo_root && *repo_root ) + { + strcat( (char *)&path[0], repo_root ); + strcat( (char *)&path[0], "/" ); + } + strcat( (char *)&path[0], name ); + + if( !is_bare( (char *)&path[0] ) ) + { + strcat( (char *)&path[0], "/.git" ); + } + + if( revision && *revision ) + snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s log --pretty=format:'%%H' -1 %s 2>/dev/null", (char *)&path[0], revision ); + else + snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s log --pretty=format:'%%H' -1 refs/heads/%s 2>/dev/null", (char *)&path[0], ctx.repo.trunk ); + p = sys_exec_command( &buf, cmd ); + rc = sys_wait_command( p, NULL ); + if( rc != 0 ) + { + strbuf_release( &buf ); + return; + } + + if( buf.buf[0] ) + { + strbuf_trim( &buf ); + strncpy( info->revision, buf.buf, GIT_OID_HEXSZ+1 ); + fill_commit_info( info, (const char *)&path[0], NULL ); + } + + strbuf_release( &buf ); + + } + return; +} + + +static const char *lang_info_by_path( const char *path ) +{ + char *lang = NULL, *name = NULL, *ext = NULL; + + if( !path || !*path ) return (const char *)lang; + + ext = rindex( path, '.' ); + if( ext ) + { + if( !strncasecmp( ext, ".mk", 3 ) || + !strncasecmp( ext, ".make", 5 ) || + !strncasecmp( ext, ".Makefile", 9 ) ) + { + lang = (char *)__sbrk( 9 ); + sprintf( lang, "Makefile" ); + } + else if( !strncasecmp( ext, ".md", 3 ) ) + { + lang = (char *)__sbrk( 9 ); + sprintf( lang, "Markdown" ); + } + else if( !strncasecmp( ext, ".diff", 5 ) || + !strncasecmp( ext, ".patch", 6 ) ) + { + lang = (char *)__sbrk( 9 ); + sprintf( lang, "Diff" ); + } + else if( !strncasecmp( ext, ".c", 2 ) || + !strncasecmp( ext, ".h", 2 ) ) + { + lang = (char *)__sbrk( 2 ); + sprintf( lang, "C" ); + } + else if( !strncasecmp( ext, ".cpp", 4 ) || + !strncasecmp( ext, ".cxx", 4 ) || + !strncasecmp( ext, ".hpp", 4 ) ) + { + lang = (char *)__sbrk( 4 ); + sprintf( lang, "C++" ); + } + else if( !strncasecmp( ext, ".html", 5 ) ) + { + lang = (char *)__sbrk( 5 ); + sprintf( lang, "HTML" ); + } + else if( !strncasecmp( ext, ".css", 4 ) ) + { + lang = (char *)__sbrk( 4 ); + sprintf( lang, "CSS" ); + } + else if( !strncasecmp( ext, ".js", 3 ) ) + { + lang = (char *)__sbrk( 11 ); + sprintf( lang, "javascript" ); + } + else if( !strncasecmp( ext, ".json", 5 ) ) + { + lang = (char *)__sbrk( 5 ); + sprintf( lang, "JSON" ); + } + else if( !strncasecmp( ext, ".less", 5 ) ) + { + lang = (char *)__sbrk( 5 ); + sprintf( lang, "LESS" ); + } + else if( !strncasecmp( ext, ".scss", 5 ) ) + { + lang = (char *)__sbrk( 5 ); + sprintf( lang, "SCSS" ); + } + else if( !strncasecmp( ext, ".pl", 3 ) || + !strncasecmp( ext, ".pm", 3 ) ) + { + lang = (char *)__sbrk( 5 ); + sprintf( lang, "Perl" ); + } + else if( !strncasecmp( ext, ".py", 3 ) ) + { + lang = (char *)__sbrk( 7 ); + sprintf( lang, "Python" ); + } + else if( !strncasecmp( ext, ".php", 4 ) ) + { + lang = (char *)__sbrk( 4 ); + sprintf( lang, "PHP" ); + } + else if( !strncasecmp( ext, ".txt", 4 ) ) + { + lang = (char *)__sbrk( 10 ); + sprintf( lang, "plaintext" ); + } + else if( !strncasecmp( ext, ".go", 3 ) ) + { + lang = (char *)__sbrk( 3 ); + sprintf( lang, "Go" ); + } + } + + name = rindex( path, '/' ); + if( name ) + ++name; + else + name = (char *)path; + + if( name ) + { + if( !strncasecmp( name, "Makefile", 8 ) ) + { + lang = (char *)__sbrk( 9 ); + sprintf( lang, "Makefile" ); + } + else if( !strcasecmp( name, "README" ) ) + { + lang = (char *)__sbrk( 10 ); + sprintf( lang, "Plaintext" ); + } + else if( !strcasecmp( name, "LICENSE" ) ) + { + lang = (char *)__sbrk( 10 ); + sprintf( lang, "Plaintext" ); + } + } + + /*************************** + all chances are exhausted. probably highlight.js do it correctly... + */ + /* + if( !lang ) + { + lang = (char *)__sbrk( 10 ); + sprintf( lang, "plaintext" ); + } + */ + + return (const char *)lang; +} + +static const char *lang_info( const char *mime_type, const char *path ) +{ + char *lang = NULL; + + if( !mime_type ) return (const char *)lang; + + if( !strncmp( mime_type, "text/", 5 ) ) + { + if( !strncmp( mime_type, "text/x-c", 8 ) || !strncmp( mime_type, "text/x-csrc", 11 ) ) + { + lang = (char *)__sbrk( 2 ); + sprintf( lang, "C" ); + } + else if( !strncmp( mime_type, "text/x-perl", 11 ) ) + { + lang = (char *)__sbrk( 5 ); + sprintf( lang, "Perl" ); + } + else if( !strncmp( mime_type, "text/x-diff", 11 ) ) + { + lang = (char *)__sbrk( 5 ); + sprintf( lang, "Diff" ); + } + else if( !strncmp( mime_type, "text/x-makefile", 15 ) ) + { + lang = (char *)__sbrk( 9 ); + sprintf( lang, "Makefile" ); + } + else if( !strncmp( mime_type, "text/x-shell", 12 ) ) + { + lang = (char *)__sbrk( 5 ); + sprintf( lang, "Bash" ); + } + + /* + The 'text/plain;' mime type does not mean that the file contains plain text. + But we have to identify the language: + */ + if( !lang && path ) + { + /* Here we can make lang identification by filename or extension */ + lang = (char *)lang_info_by_path( path ); + } + } + + return (const char *)lang; +} + + +void cgit_rpath_info( struct cgit_info *info, const char *relative_path, const char *revision ) +{ + const char *name = NULL, *git_root = NULL, *repo_root = NULL; + + if( !info || !relative_path ) return; + + name = ctx.repo.name; + git_root = ctx.repo.git_root; + repo_root = ctx.repo.repo_root; + + if( name && git_root ) + { + char ref[PATH_MAX] = { 0 }; + char rpath[PATH_MAX] = { 0 }; + + char path[PATH_MAX] = { 0 }; + char cmd[PATH_MAX]; + struct strbuf buf = STRBUF_INIT; + pid_t p = (pid_t) -1; + int rc; + + sprintf( (char *)&path[0], "%s/", git_root ); + if( repo_root && *repo_root ) + { + strcat( (char *)&path[0], repo_root ); + strcat( (char *)&path[0], "/" ); + } + strcat( (char *)&path[0], name ); + + if( !is_bare( (char *)&path[0] ) ) + { + strcat( (char *)&path[0], "/.git" ); + } + + parse_relative_path( (char *)&ref[0], (char *)&rpath[0], relative_path ); + + if( revision && *revision ) + snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s log --pretty=format:'%%H' -1 %s -- %s 2>/dev/null", (char *)&path[0], revision, (char *)&rpath[0] ); + else + snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s log --pretty=format:'%%H' -1 %s -- %s 2>/dev/null", (char *)&path[0], (char *)&ref[0], (char *)&rpath[0] ); + p = sys_exec_command( &buf, cmd ); + rc = sys_wait_command( p, NULL ); + if( rc != 0 ) + { + strbuf_release( &buf ); + return; + } + + if( buf.buf[0] ) + { + strbuf_trim( &buf ); + strncpy( info->revision, buf.buf, GIT_OID_HEXSZ+1 ); + fill_commit_info( info, (const char *)&path[0], (const char *)&rpath[0] ); + if( info->kind == GIT_OBJECT_BLOB ) + { + fill_mime_info( info, (const char *)&path[0], (const char *)&rpath[0] ); + if( info->mode == GIT_FILEMODE_LINK ) + { + int len = 10; + char *lang = (char *)__sbrk( len ); + memcpy( (void *)lang, (const void *)"PlainText", len ); + info->lang = (const char *)lang; + } + else + info->lang = lang_info( (const char *)(info->mime), (const char *)&rpath[0] ); + } + } + + strbuf_release( &buf ); + + } + return; +} + + +void cgit_repo_branches_number( struct cgit_repository *rctx ) +{ + const char *name = NULL, *git_root = NULL, *repo_root = NULL, *trunk = NULL; + + if( !rctx ) return; + + name = rctx->name; + git_root = rctx->git_root; + repo_root = rctx->repo_root; + trunk = rctx->trunk; + + if( name && git_root ) + { + char path[PATH_MAX] = { 0 }; + sprintf( (char *)&path[0], "%s/", git_root ); + if( repo_root && *repo_root ) + { + strcat( (char *)&path[0], repo_root ); + strcat( (char *)&path[0], "/" ); + } + strcat( (char *)&path[0], name ); + + if( trunk && *trunk ) + rctx->nbranches = (int)branches_number( (const char *)&path[0], trunk ); + else + rctx->nbranches = (int)branches_number( (const char *)&path[0], NULL ); + } + return; +} + +void cgit_repo_tags_number( struct cgit_repository *rctx ) +{ + const char *name = NULL, *git_root = NULL, *repo_root = NULL; + + if( !rctx ) return; + + name = rctx->name; + git_root = rctx->git_root; + repo_root = rctx->repo_root; + + if( name && git_root ) + { + char path[PATH_MAX] = { 0 }; + sprintf( (char *)&path[0], "%s/", git_root ); + if( repo_root && *repo_root ) + { + strcat( (char *)&path[0], repo_root ); + strcat( (char *)&path[0], "/" ); + } + strcat( (char *)&path[0], name ); + + rctx->ntags = (int)tags_number( (const char *)&path[0] ); + } + return; +} + +void cgit_repo_commits_number( struct cgit_repository *rctx ) +{ + const char *name = NULL, *git_root = NULL, *repo_root = NULL; + + if( !rctx ) return; + + name = rctx->name; + git_root = rctx->git_root; + repo_root = rctx->repo_root; + + if( name && git_root ) + { + char path[PATH_MAX] = { 0 }; + char cmd[PATH_MAX]; + struct strbuf buf = STRBUF_INIT; + pid_t p = (pid_t) -1; + int rc; + + sprintf( (char *)&path[0], "%s/", git_root ); + if( repo_root && *repo_root ) + { + strcat( (char *)&path[0], repo_root ); + strcat( (char *)&path[0], "/" ); + } + strcat( (char *)&path[0], name ); + + if( !is_bare( (char *)&path[0] ) ) + { + strcat( (char *)&path[0], "/.git" ); + } + + /* all commits in repo: + snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s rev-list -9999 --all --count 2>/dev/null", (char *)&path[0] ); + */ + snprintf( (char *)&cmd[0], 1024, "git --git-dir=%s rev-list -9999 HEAD^{commit} --count 2>/dev/null", (char *)&path[0] ); + p = sys_exec_command( &buf, cmd ); + rc = sys_wait_command( p, NULL ); + if( rc != 0 ) + { + strbuf_release( &buf ); + return; + } + + if( buf.buf[0] ) + { + strbuf_trim( &buf ); + rctx->ncommits = atoi( buf.buf ); + } + + strbuf_release( &buf ); + } + return; +} + +void cgit_git_version( struct cgit_versions *vctx ) +{ + char cmd[1024]; + struct strbuf buf = STRBUF_INIT; + pid_t p = (pid_t) -1; + int rc; + + char *version = NULL; + int len = 0; + + if( !vctx ) return; + + snprintf( (char *)&cmd[0], 1024, "git --version 2>/dev/null | cut -f3 -d' '" ); + p = sys_exec_command( &buf, cmd ); + rc = sys_wait_command( p, NULL ); + if( rc != 0 ) + { + strbuf_release( &buf ); + return; + } + + if( buf.buf[0] ) + { + strbuf_trim( &buf ); + + len = (int)strlen( buf.buf ) + 1; + version = (char *)__sbrk( len ); + memcpy( (void *)version, (const void *)buf.buf, (size_t)len ); + vctx->git = (const char *)version; + } + + strbuf_release( &buf ); + + return; +} + +void cgit_nginx_version( struct cgit_versions *vctx ) +{ + char cmd[1024]; + struct strbuf buf = STRBUF_INIT; + pid_t p = (pid_t) -1; + int rc; + + char *version = NULL; + int len = 0; + + if( !vctx ) return; + + snprintf( (char *)&cmd[0], 1024, "nginx -v 2>&1 | cut -f2 -d'/'" ); + p = sys_exec_command( &buf, cmd ); + rc = sys_wait_command( p, NULL ); + if( rc != 0 ) + { + strbuf_release( &buf ); + return; + } + + if( buf.buf[0] ) + { + strbuf_trim( &buf ); + + len = (int)strlen( buf.buf ) + 1; + version = (char *)__sbrk( len ); + memcpy( (void *)version, (const void *)buf.buf, (size_t)len ); + vctx->nginx = (const char *)version; + } + + strbuf_release( &buf ); + + return; +} + + +void cgit_print_404_page( void ) +{ + FILE *fp; + struct strbuf buf = STRBUF_INIT; + + fp = xfopen( ctx.page.header, "r" ); + (void)strbuf_env_fread( &buf, fp ); + fclose( fp ); + + strbuf_addf( &buf, " <div class=\"content segment\">\n" ); + strbuf_addf( &buf, " <div class=\"container\">\n" ); + strbuf_addf( &buf, " <div class=\"cgit-main-content\">\n" ); + strbuf_addf( &buf, " <h1>Requested resource not found</h1>\n" ); + strbuf_addf( &buf, " <p class='leading'>Please check the requested URL or try again later.</p>\n" ); + strbuf_addf( &buf, " </div> <!-- End of cgit-main-content -->\n" ); + strbuf_addf( &buf, " </div> <!-- End of container -->\n" ); + strbuf_addf( &buf, " </div> <!-- End of content segment -->\n" ); + + fp = xfopen( ctx.page.footer, "r" ); + (void)strbuf_env_fread( &buf, fp ); + fclose( fp ); + + ctx.page.size = buf.len; + cgit_print_http_headers(); + strbuf_write( &buf, STDOUT_FILENO ); + strbuf_release( &buf ); +} + + +void cgit_print_raw_file( struct strbuf *sb, const char *mime ) +{ + struct strbuf buf = STRBUF_INIT; + char *p = (char *)mime; + + char *http_format = "Date: %s\n" + "Content-Type: %s\n" + "Content-Length: %ld\n\n"; + + if( !sb || !mime ) return; + + while( *p && *p != ';' ) ++p; + if( *p ) *p = '\0'; + + strbuf_addf( &buf, http_format, http_date( time(NULL) ), mime, sb->len ); + strbuf_addbuf( &buf, (const struct strbuf *)sb ); + strbuf_write( &buf, STDOUT_FILENO ); + strbuf_release( &buf ); + + exit( 0 ); +} |