/*+JMJ
* common.c - tir HTML renderer common functions
* Copyright 2011 David Meyer <[email protected]>
* common.c is part of tirrender
*/

#include <stdio.h>
#include <string.h>
#include "krcompat.h"
#include "tirrender.h"

/* CONSTANTS: ******************************************************/

                                 /* Valid special characters for
                                  * object names
                                  */
#define OBJECT_SPECIALS "%+,-./:=@_"
#define DSCOUNT 2                 /* No. of records in dstab */

/* MACROS: *********************************************************/

/* GLOBALS: ********************************************************/

struct {
 char dataset[30];               /* dataset name */
 char root[30];                  /* dataset root file system abs. path */
 char group[30];                 /* htgroup authorized to render (read) dataset */
} dstab[] = { "black", "/path/to/ds/black/root", "black",
             "green", "/path/to/ds/green/root", "*" };

/* PROTOTYPES: *****************************************************/

/* FUNCTIONS: ******************************************************/

/* render() - Output HTML rendering of target file or directory.
* - standard header, footer, sidebar(s)
* - content for directories (ref. tirph):
*   + title from .title or dir name
*   + optional .description
*   + list of file titles w/ links to renderer
*   + list of .exits dir. titles
* - content for .txt files
*   + title from file first line
*   + rest of file contents in <pre> block
*   + escape <, > (others?)
* - content for .html files
*   + title from <title> block or first <h1>
*   + contents of <body> block or rest after first <h1>
*   + ignore file-specific style
* - content for image files (.bmp, .gif, .jpg, .png)
*   + title is file name
*   + embed image with <img> tag
* - content for other non-binary files (.c, .cgi, .css, .h, .pl,
*   .pm, .sh, .shtml, ...)
*   + title is file name
*   + file contents in <pre> block
* - content for other binary files
*   + title is file name
*   + "[binary file]"
*/
int render (dataset, object)
    char dataset[];
    char object[];
{
 printf("render dataset=%s|object=%s\n", dataset, object);
 return 0;
}

int baddataset (dataset)
    char dataset[];
{
 char dsroot[] = datasetroot(dataset);

 if (dsroot == NULL) return 1;
 else return 0;
}

int badobject (dataset, object)
    char dataset[];
    char object[];
{
 char rootdir[] = datasetroot(dataset);
 char objpath[];
 FILE *ofp;
 int n;

                                 /* check for invalid dataset */
 if (rootdir == NULL) return 1;

                                 /* for each char. in object */
 for (n = 0; n < strlen(object); n++) {
   char c = object[n];

                                 /* char. is neither alphanum. nor
                                  * allowed special: return error
                                  */
   if (!isalnum(c) && (strchr(OBJECT_SPECIALS, c) == NULL)) return 1;
 }

                                 /* attempt to access parent dir. */
 if (!strcmp(object, "..") || !strncmp(object, "../", 3) || strstr(object, "/../") || !strcmp(object + strlen(object) - 3, "/..")) return 1;

                                 /* assemple obj. path and check
                                  * obj. file existence
                                  */
 objpath = (char *) malloc(strlen(rootdir)+strlen(object)+1);
 strcpy(objpath, rootdir);
 strcat(objpath, "/");
 strcat(objpath, object);
 ofp = fopen(objpath, "r");
 if (ofp == NULL) return 1;
 fclose(ofp);

 return 0;
}

char *datasetroot (dataset)
    char dataset[];
{
 int n;
 for (n = 0; n < DSCOUNT; n++) {
   if (!strcmp(dstab[n].dataset, dataset)) return &dstab[n].root;
 }
 return NULL;
}