/*** This is a custom program for Western New England College
**** Other schools will not need this
***/


#include <stdio.h>
#include "global.h"
#include "custom.h"		/* for date FORMAT strings */



#include <string.h>
#include <dirent.h>		/* Directory information. */

#include <sys/stat.h>		/* for stat()            */
#include <unistd.h>		/* for unlink, mktmp ... */





#include "manhat-lib/shared_util.h"
#include "manhat-lib/shared_potato_util.h"  /* for kill_hotpotato_directory() */
#include "manhat-lib/shared_central_user.h" /* for get_central_user_info() */
#include "manhat-lib/shared_authenticate.h"
#include "manhat-lib/shared_person_list.h"  /* for get_user_info() */
#include "manhat-lib/shared_lock.h"
#include "manhat-lib/shared_login.h"
#include "manhat-lib/shared_server_locked.h"
#include "manhat-lib/shared_server_string.h"
#include "manhat-lib/shared_strtrm.h"
#include "manhat-lib/shared_cookie.h"
#include "manhat-lib/shared_encrypt.h"
#include "manhat-lib/shared_access.h"
#include "manhat-lib/shared_central_log.h"
#include "manhat-lib/shared_user_index.h"
#include "manhat-lib/shared_cs_util.h"	/* for get_string_value() */
#include "manhat-lib/shared_news_util.h"	/* so we can set_discussion_names() */

#define MAX_NUM_PARAM_ID 3


/*** AUTHENTICATION_SERVER_IP is a WNEC customization which may be 
**** found in custom.h.
***  If it's not #defined there, #define it now so this program
*** can compile
**/

#define AUTHENTICATION_SERVER_IP "10.5.19.240"

void
send_report (const char *string )
{
  send_content_type ();
  puts ("<html><head>");
  printf ("<title>%s</title></head>", "send url");
  printf ("<body>");
  puts (string);
  puts ("\r\n</body>\n</html>");
  exit (0);
}

static void
read_parameters (char *id[MAX_NUM_PARAM_ID], char *expire)
{
  char tmp[MAX_ID + 1];
  char *add;

  add = hdf_get_value(global_cgi->hdf, "CGI.RemoteAddress", 0);

  if(add)
  {
    if(strcmp(add, AUTHENTICATION_SERVER_IP))
       send_report("Page can not be displayed");
  }
  else
     send_report("Page can not be displayed");
 

  cs_get_optional_parameter ("fac_id", id[0], MAX_ID);
  cs_get_optional_parameter ("std_id", id[1], MAX_ID);
  cs_get_optional_parameter ("emp_id", tmp, MAX_ID);
  
  if(*tmp)
    snprintf(id[2],MAX_ID + 1, "e%s", tmp);

  cs_get_optional_parameter ("expire", expire, MAX_PATH);
}


static void
central_login(const char *username, char *key)
{
 SESSION user;
 char base_dir[MAX_PATH + 1];
  
 get_central_user_info ( username, &user, 0);       /*shared_central_user.c , don't set capture flag*/

 snprintf(base_dir, MAX_PATH + 1, "../%s", USERS_DIR);
 create_new_key (&user, base_dir, key, MAX_KEY_EXPIRE);      /*shared_login.c */
 

 /* cs_set_event_msg () sets up the log_event.* strings used to write the central log */ 
 if(!cs_set_event_msg())     				/* shared_cs_util.c */
    cs_critical_error(ERR_CANT_GET_EVENT_STRING, "in central_login()");   /* shared_cs_util.c */

// write_central_log(username, hdf_get_value(global_cgi->hdf, "log_event.successful_login",""), 
 //                            hdf_get_value(global_cgi->hdf, "log_event.from_auth_server",""),0);     /* shared_central_log.c */
 
 
 delete_central_user_keys(username);   /* shared_authenticate.c */

}






void
init(char *data[MAX_NUM_PARAM_ID], int len, int max)
{
    int i;
    for(i =0;i<len; i++)
    {
	data[i] = (char *)malloc(sizeof(char) * max);
	data[i][0] = '\0';
    }

}






void
send_all_urls( char *username[MAX_NUM_PARAM_ID],  char *key[MAX_NUM_PARAM_ID], int count, char *expire)
{
    int i, j =1;
    char server_string[MAX_PATH +1];
    char url[MAX_PATH*2 +1];
    char sub_url[MAX_PATH +1];

    get_server_string(server_string, MAX_PATH);
    snprintf(url, MAX_PATH*2 +1, "url=%s/%s/%s?count%%3D%d%%26expire%%3D%s", server_string, ALIAS, "wnec_redirect", count, expire);
    for(i = 0; i < MAX_NUM_PARAM_ID; i++)
    {
       if(strlen(username[i]) && strlen(key[i]))
       {
	     if(i == 0)  
	     {
	       snprintf(sub_url, MAX_PATH +1, "%%26id%d%%3D%s%%26acc_type%d%%3Dfaculty", j,  key[i],j);
	       j++;
	     }
             else if(i == 1)
	     {
	       snprintf(sub_url, MAX_PATH +1, "%%26id%d%%3D%s%%26acc_type%d%%3Dstudent", j,  key[i],j);
	       j++;
	     }
	     else if(i == 2)
	     {
	       snprintf(sub_url, MAX_PATH +1, "%%26id%d%%3D%s%%26acc_type%d%%3Demploy", j,  key[i],j);
	       j++;
	     }
	     strncat(url, sub_url, 100); 
       }	       
    }
    
    send_report (url );
}





static int find_username(const char *id, char *username)
{
    char *buf;
    char realname[MAX_NAME +1];
   buffer_index ( &buf );

    if(buf)
    {
           index_get_names(buf, id, username, realname);
           if(strlen(username))
            return 1;
    }
    return 0;
}







int
get_central_usernames( char *id[MAX_NUM_PARAM_ID],
		                    char *username[MAX_NUM_PARAM_ID], int len, int *count)
{
   int i;
   int flag =0;

   for(i =0; i<len; i++)
   {
       if(find_username(id[i], username[i]))
            flag++;
   }
   if(flag)
   {
         *count = flag;
          return 1;
   }
   return 0;

}





void
free_char(char *data[MAX_NUM_PARAM_ID])
{
    int i;
    for(i =0; i<MAX_NUM_PARAM_ID; i++)
    {
	if(data[i])
	   free(data[i]);
    }
}





void
send_url(int count)
{
    char server_string[MAX_PATH +1];
    char url[MAX_PATH +1];

    get_server_string(server_string, MAX_PATH);
    snprintf(url, MAX_PATH +1, "url=%s/%s/%s?count%%3D%d", server_string, ALIAS, "wnec_redirect", count);
    send_report (url );

}







int
main ()
{
  char *id[MAX_NUM_PARAM_ID];
  char *username[MAX_NUM_PARAM_ID];
  char *key[MAX_NUM_PARAM_ID];
  char expire[MAX_PATH +1];
  int count =0;
  int i =0;


  cs_cgi_init();
 
  if(server_locked())
      send_url( -1);

  init(id, MAX_NUM_PARAM_ID, MAX_ID +1);
  init(username, MAX_NUM_PARAM_ID, MAX_USERNAME +1);
  init(key, MAX_NUM_PARAM_ID, MAX_KEY +1);

  read_parameters (id, expire);
  if(!get_central_usernames(id, username, MAX_NUM_PARAM_ID, &count)) 
      send_url( 0);

   for(i =0; i<MAX_NUM_PARAM_ID; i++)
   {
      if(strlen(username[i])) 
         central_login(username[i],  key[i]);
   }
   send_all_urls(username, key, count, expire); 
   free_char(id);
   free_char(username);
   free_char(key);
  cs_cgi_destroy();
  return 0;
}



