From 443090030722c27f33914332cb23252f78297e2e Mon Sep 17 00:00:00 2001 From: Antoni Sawicki Date: Mon, 20 May 2019 00:49:26 -0700 Subject: initial support for url shortcuts --- dialogs.c | 8 ++++ dir.c | 121 +++++++++++++++++++++++++++++++++++--------------------- fileio.c | 132 +++++++++++++++++++++++++++++++++++++++++++++++--------------- wfm.c | 7 +++- wfm.h | 4 +- 5 files changed, 194 insertions(+), 78 deletions(-) diff --git a/dialogs.c b/dialogs.c index 2a1f733..f0fc25a 100644 --- a/dialogs.c +++ b/dialogs.c @@ -150,6 +150,14 @@ void singleprompt_ui(char *m_action) { "  
Enter name of the new directory:

\n" " \n"); + else if(strcmp(m_action, "mkurl")==0) + fprintf(cgiOut, + "  
Name of the shortcut file:

\n" + " \n" + "

\n" + "  
The shortcut URL:

\n" + " \n"); + fprintf(cgiOut, " \n" " \n" diff --git a/dir.c b/dir.c index afe64c2..6477255 100644 --- a/dir.c +++ b/dir.c @@ -3,8 +3,8 @@ //char ICO_FAV[256]; char ICO_DIR[256], ICO_AUP[256], ICO_ADN[256], ICO_GEN[256], ICO_NEW[256], ICO_ZIP[256]; char ICO_IMG[256], ICO_OFF[256], ICO_PDF[256]; -char ICO_TXT[256], ICO_EXE[256], ICO_MED[257], ICO_ISO[256], ICO_LNK[256]; -regex_t reg_zip, reg_img, reg_pdf, reg_exe, reg_txt, reg_off, reg_med, reg_iso; +char ICO_TXT[256], ICO_EXE[256], ICO_MED[257], ICO_ISO[256], ICO_LNK[256], ICO_DSK[256], ICO_EDT[256]; +regex_t reg_zip, reg_img, reg_pdf, reg_exe, reg_txt, reg_off, reg_med, reg_iso, reg_lnk; char M_HR[]="(Last Hour)"; char M_DAY[]="(Last Day)"; @@ -26,7 +26,7 @@ static const char *access_string[]={ "none", "readonly", "readwrite" }; void dir_icoinita(void) { //snprintf(ICO_FAV, sizeof(ICO_FAV), "\"Favicon\"", rt.iconsurl, rt.favicon); snprintf(ICO_DIR, sizeof(ICO_DIR), "\"Dir\"", rt.iconsurl); - snprintf(ICO_LNK, sizeof(ICO_LNK), "\"Symlink\"", rt.iconsurl); + snprintf(ICO_LNK, sizeof(ICO_LNK), "\"Link/Shortcut\"", rt.iconsurl); snprintf(ICO_AUP, sizeof(ICO_AUP), "\"Up\"", rt.iconsurl); snprintf(ICO_ADN, sizeof(ICO_ADN), "\"Down\"", rt.iconsurl); snprintf(ICO_GEN, sizeof(ICO_GEN), "\"Unknown\"", rt.iconsurl); @@ -39,6 +39,8 @@ void dir_icoinita(void) { snprintf(ICO_EXE, sizeof(ICO_EXE), "\"Exec\"", rt.iconsurl); snprintf(ICO_MED, sizeof(ICO_MED), "\"Multimedia\"", rt.iconsurl); snprintf(ICO_ISO, sizeof(ICO_ISO), "\"Disk", rt.iconsurl); + snprintf(ICO_DSK, sizeof(ICO_DSK), "\"Save\"", rt.iconsurl); + snprintf(ICO_EDT, sizeof(ICO_EDT), "\"Edit\"", rt.iconsurl); if( regcomp(®_zip, "\\.(zip|rar|tar|gz|tgz|z|arj|bz|tbz|7z|xz)$", REG_EXTENDED | REG_ICASE)!=0 || @@ -48,7 +50,8 @@ void dir_icoinita(void) { regcomp(®_med, "\\.(mp3|mp4|vaw|mov|avi|ivr|mkv)$", REG_EXTENDED | REG_ICASE)!=0 || regcomp(®_pdf, "\\.(pdf|ps|eps|ai)$", REG_EXTENDED | REG_ICASE)!=0 || regcomp(®_exe, "\\.(exe|com|pif)$", REG_EXTENDED | REG_ICASE)!=0 || - regcomp(®_txt, "\\.(txt|asc|nfo|me|md|log|htm|html|shtml|js|jsp|php|xml|dtd|css|bas|c|h|cpp|cmd|bat|sh|ksh|awk|reg|log|bak|cfg|conf|py|json|yaml|url|lnk|desktop)$", REG_EXTENDED | REG_ICASE)!=0 + regcomp(®_lnk, "\\.(url|lnk|desktop|shortcut|webloc)$", REG_EXTENDED | REG_ICASE)!=0 || + regcomp(®_txt, "\\.(txt|asc|nfo|me|md|log|htm|html|shtml|js|jsp|php|xml|dtd|css|bas|c|h|cpp|cmd|bat|sh|ksh|awk|reg|log|bak|cfg|conf|py|json|yaml)$", REG_EXTENDED | REG_ICASE)!=0 ) error("Unable to compile regex."); } @@ -65,9 +68,9 @@ void dirlist(void) { char rtime[64], mtime[64], atime[64]; char *stime; char sortby[64]={0}; - char *name, *name_urlencoded, *icon, *linecolor; - int nentr=0, e=0, n=1; - int editable; + char *name, *name_urlencoded, *icon, *linecolor, *action, *raction, *ricon, *laction, *licon; + int nentr=0, e=0, n=1; + int editable, is_link; int upload_id=0; time_t now; @@ -205,9 +208,9 @@ void dirlist(void) { "" " \"Access\" %s\n", cgiScriptName, wp.virt_dirname_urlencoded, rt.iconsurl, access_string[rt.access_level], access_string[rt.access_level]); - else if(rt.auth_method==3) - fprintf(cgiOut, - "" + else if(rt.auth_method==3) + fprintf(cgiOut, + "" "\"Access\"" " %s \"User\" %s \n", cgiScriptName, rt.iconsurl, access_string[rt.access_level], access_string[rt.access_level], rt.iconsurl, rt.loggedinuser); @@ -297,6 +300,14 @@ void dirlist(void) { "\n", cgiScriptName, wp.virt_dirname_urlencoded, rt.token, rt.iconsurl); + fprintf(cgiOut, + "\n" + "\n" + "" + "%s New URL" + "\n" + "\n", + cgiScriptName, wp.virt_dirname_urlencoded, rt.token, ICO_LNK); fprintf(cgiOut, @@ -509,15 +520,16 @@ void dirlist(void) { else if(now-direntry[e].mtime < 365*24*3600) stime=M_YR; else stime=M_OLD; - if(regexec(®_zip, name, 0, 0, 0)==0) { icon=ICO_ZIP; editable=0; } - else if(regexec(®_img, name, 0, 0, 0)==0) { icon=ICO_IMG; editable=0; } - else if(regexec(®_off, name, 0, 0, 0)==0) { icon=ICO_OFF; editable=0; } - else if(regexec(®_pdf, name, 0, 0, 0)==0) { icon=ICO_PDF; editable=0; } - else if(regexec(®_txt, name, 0, 0, 0)==0) { icon=ICO_TXT; editable=1; } - else if(regexec(®_exe, name, 0, 0, 0)==0) { icon=ICO_EXE; editable=0; } - else if(regexec(®_med, name, 0, 0, 0)==0) { icon=ICO_MED; editable=0; } - else if(regexec(®_iso, name, 0, 0, 0)==0) { icon=ICO_ISO; editable=0; } - else { icon=ICO_GEN; editable=0; } + if(regexec(®_zip, name, 0, 0, 0)==0) { icon=ICO_ZIP; editable=0; is_link=0; } + else if(regexec(®_img, name, 0, 0, 0)==0) { icon=ICO_IMG; editable=0; is_link=0; } + else if(regexec(®_off, name, 0, 0, 0)==0) { icon=ICO_OFF; editable=0; is_link=0; } + else if(regexec(®_pdf, name, 0, 0, 0)==0) { icon=ICO_PDF; editable=0; is_link=0; } + else if(regexec(®_txt, name, 0, 0, 0)==0) { icon=ICO_TXT; editable=1; is_link=0; } + else if(regexec(®_exe, name, 0, 0, 0)==0) { icon=ICO_EXE; editable=0; is_link=0; } + else if(regexec(®_med, name, 0, 0, 0)==0) { icon=ICO_MED; editable=0; is_link=0; } + else if(regexec(®_iso, name, 0, 0, 0)==0) { icon=ICO_ISO; editable=0; is_link=0; } + else if(regexec(®_lnk, name, 0, 0, 0)==0) { icon=ICO_LNK; editable=1; is_link=1; } + else { icon=ICO_GEN; editable=0; is_link=0; } if(cfg.edit_any_file) { editable=1; } @@ -537,6 +549,27 @@ void dirlist(void) { } } + // default action + if(cfg.edit_by_default && editable) { + action="edit"; + raction="save"; + ricon=ICO_DSK; + laction="goto_url"; + licon=ICO_LNK; + } else if(is_link) { + action="goto_url"; + raction="save"; + ricon=ICO_DSK; + laction="edit"; + licon=ICO_EDT; + } else { + action="save"; + raction="edit"; + ricon=ICO_EDT; + laction=""; + licon=""; + } + // filename fprintf(cgiOut, "\n"); @@ -548,10 +581,11 @@ void dirlist(void) { fprintf(cgiOut, "onMouseOver=\"this.bgColor='#%s';\" onMouseOut=\"this.bgColor='#%s';\"", tHL_COLOR, linecolor); + // default action fprintf(cgiOut, ">\n" - "%s %s\n", - name, cgiScriptName, (cfg.edit_by_default && editable) ? "edit" : "sendfile", wp.virt_dirname_urlencoded, name_urlencoded, rt.token, name, icon, name); + "%s %s\n", + name, cgiScriptName, action, wp.virt_dirname_urlencoded, name_urlencoded, rt.token, action, name, icon, name); // size / date @@ -576,7 +610,7 @@ void dirlist(void) { // move fprintf(cgiOut, "\n" - "" + "" "\"Move\n" "\n", cgiScriptName, wp.virt_dirname_urlencoded, name_urlencoded, rt.token, name, rt.iconsurl, name); @@ -590,34 +624,33 @@ void dirlist(void) { "\n", cgiScriptName, wp.virt_dirname_urlencoded, name_urlencoded, rt.token, name, rt.iconsurl); + // edit for text files.. + if(editable) + fprintf(cgiOut, + "\n" + "\n" + "%s\n" + "\n", + cgiScriptName, raction, wp.virt_dirname_urlencoded, name_urlencoded, rt.token, raction, name, ricon); + + // links + if(is_link) + fprintf(cgiOut, + "\n" + "\n" + "%s\n" + "\n", + cgiScriptName, laction, wp.virt_dirname_urlencoded, name_urlencoded, rt.token, laction, name, licon); - // view - if(strlen(cfg.homeurl)>4) + // view via external link + if(strlen(cfg.homeurl)>4 && !is_link) fprintf(cgiOut, "\n" "\n" - "\"Preview\n" + "%s\n" "\n", - cfg.homeurl, (wp.virt_dirname[0]!='/') ? "/" : "", (strcmp(wp.virt_dirname, "/")==0) ? "" : wp.virt_dirname, name, name, rt.iconsurl, name); + cfg.homeurl, (wp.virt_dirname[0]!='/') ? "/" : "", (strcmp(wp.virt_dirname, "/")==0) ? "" : wp.virt_dirname, name, name, ICO_LNK); - - // edit for text files.. - if(editable) { - if(cfg.edit_by_default) - fprintf(cgiOut, - "\n" - "\n" - "\"Download\n" - "\n", - cgiScriptName, wp.virt_dirname_urlencoded, name_urlencoded, rt.token, name, rt.iconsurl); - else - fprintf(cgiOut, - "\n" - "\n" - "\"Edit\n" - "\n", - cgiScriptName, wp.virt_dirname_urlencoded, name_urlencoded, rt.token, name, rt.iconsurl); - } fprintf(cgiOut, "\n" diff --git a/fileio.c b/fileio.c index 66c726c..3ff3d31 100644 --- a/fileio.c +++ b/fileio.c @@ -2,40 +2,11 @@ #include "wfm.h" -/* -// Debug dump vars -//void debugdumpvars(void) { - - cgiHeaderContentType("text/plain"); - - printf( - "virt_dirname=%s\n" - "wp.phys_dirname=%s\n" - "wp.virt_filename=%s\n" - "wp.phys_filename=%s\n" - "wp.virt_destination=%s\n" - "wp.phys_destination=%s\n" - // "wp.final_destination=%s\n" - "virt_parent=%s\n", - virt_dirname, - wp.phys_dirname, - wp.virt_filename, - wp.phys_filename, - wp.virt_destination, - wp.phys_destination, - // wp.final_destination, - virt_parent - ); - - exit(1); -} -*/ - // // Send file to client browser -// Called by cgiMain action=sendfile +// Called by cgiMain action=save // -void sendfile(void) { +void save(void) { char buff[1024]={0}; FILE *in; int rd=0, tot=0, size=0, pos=0, blk=0; @@ -248,6 +219,105 @@ void edit_save(void) { redirect("%s?highlight=%s&directory=%s&token=%s", cgiScriptName, wp.virt_filename_urlencoded, wp.virt_dirname_urlencoded, rt.token); } + +// +// Go To URL called by action=goto_url +// +// TODO: Support for webloc and other formats +// +void goto_url(void) { + FILE *in; + const char urlstr[] = "[InternetShortcut]"; + const char dskstr[] = "[Desktop Entry]"; + const char xmlstr[] = "%s", strerror(errno)); + + //rd=fread(chkbuf, sizeof(chkbuf), 1, in); + + if(!fgets(chkbuf, sizeof(chkbuf), in)) + error("Unable to read or not a link file
%s
", wp.virt_filename); + + if( + strncmp(chkbuf, urlstr, strlen(urlstr))!=0 && + strncmp(chkbuf, dskstr, strlen(dskstr))!=0 && + strncmp(chkbuf, xmlstr, strlen(xmlstr))!=0 + ) + error("Not a URL file"); + + // process link by looking for url + buff=calloc(1024, 1); + + while(fgets(buff, 1024, in)) { + if(strncasecmp(buff, "URL=", 4)==0) { + buff+=4; + found=1; + break; + } + else if(strncasecmp(buff, "RL=", 3)==0) { + buff+=3; + found=1; + break; + } + } + + fclose(in); + + if(!found) + error("No URL spec found in file
", wp.virt_filename); + + redirect("%s", buff); +} + + +// +// Create a new new URL shortcut file +// Called by cgiMain action=mkurl +// TODO: Support webloc, desktop and other file format +// +void mkurl(void) { + FILE *output; + struct stat sb; + regex_t reg_url; + + checkfilename(NULL); + + regcomp(®_url, "\\.url$", REG_EXTENDED | REG_ICASE); + + if(regexec(®_url, wp.phys_filename, 0, 0, 0)!=0) + snprintf(wp.final_destination, sizeof(wp.final_destination), "%s.url", wp.phys_filename); + else + snprintf(wp.final_destination, sizeof(wp.final_destination), "%s", wp.phys_filename); + + if(stat(wp.final_destination, &sb)==0) + error("File %s already exists.
[%s]", wp.virt_filename, strerror(errno)); + + output=fopen(wp.final_destination, "w"); + + if(!output) + error("Unable to create file %s.
%s", wp.virt_filename, strerror(errno)); + + cgiFormStringNoNewlines("destination", wp.virt_destination, sizeof(wp.virt_destination)); + + fprintf(output, "[InternetShortcut]\r\nURL=%s\r\n", wp.virt_destination); + + fclose(output); + + wfm_commit(CHANGE, NULL); + + redirect("%s?highlight=%s&directory=%s&token=%s", cgiScriptName, wp.virt_filename_urlencoded, wp.virt_dirname_urlencoded, rt.token); + +} + + // // Recursively Delete Folders - Internal Routine // Called by fileio_delete when directory is encountered diff --git a/wfm.c b/wfm.c index 53130a8..eb8afa1 100644 --- a/wfm.c +++ b/wfm.c @@ -231,7 +231,7 @@ void checkfilename(char *inp_filename) { void checkdestination(void) { int absolute_destination; - cgiFormStringNoNewlines("destination", wp.virt_destination, sizeof(wp.virt_filename)); + cgiFormStringNoNewlines("destination", wp.virt_destination, sizeof(wp.virt_destination)); strip(wp.virt_destination, sizeof(wp.virt_filename), VALIDCHRS_DIR); cgiFormInteger("absdst", &absolute_destination, 0); // move operation relies on absolute paths, rename does not @@ -596,7 +596,7 @@ int cgiMain(void) { else if(cgiFormSubmitClicked("multi_move_prompt")==cgiFormSuccess && rt.access_level >= PERM_RO) multiprompt_ui("move"); else if(cgiFormSubmitClicked("multi_move_prompt.x")==cgiFormSuccess && rt.access_level >= PERM_RO) multiprompt_ui("move"); else if(cgiFormSubmitClicked("upload")==cgiFormSuccess && rt.access_level >= PERM_RW) receivefile(); - else if(strcmp(action, "sendfile")==0 && rt.access_level >= PERM_RO) sendfile(); + else if(strcmp(action, "save")==0 && rt.access_level >= PERM_RO) save(); else if(strcmp(action, "delete")==0 && rt.access_level >= PERM_RW) delete(); else if(strcmp(action, "delete_prompt")==0 && rt.access_level >= PERM_RW) multiprompt_ui("delete"); else if(strcmp(action, "move_prompt")==0 && rt.access_level >= PERM_RW) multiprompt_ui("move"); @@ -608,6 +608,9 @@ int cgiMain(void) { else if(strcmp(action, "mkfile_prompt")==0 && rt.access_level >= PERM_RW) singleprompt_ui("mkfile"); else if(strcmp(action, "mkdir")==0 && rt.access_level >= PERM_RW) newdir(); else if(strcmp(action, "mkdir_prompt")==0 && rt.access_level >= PERM_RW) singleprompt_ui("mkdir"); + else if(strcmp(action, "mkurl")==0 && rt.access_level >= PERM_RW) mkurl(); + else if(strcmp(action, "mkurl_prompt")==0 && rt.access_level >= PERM_RW) singleprompt_ui("mkurl"); + else if(strcmp(action, "goto_url")==0 && rt.access_level >= PERM_RO) goto_url(); else if(strcmp(action, "about")==0 && rt.access_level >= PERM_RO) about(); else if(strcmp(action, "login")==0 ) login(); else if( rt.access_level >= PERM_RO) dirlist(); diff --git a/wfm.h b/wfm.h index 0baee23..cb180e8 100644 --- a/wfm.h +++ b/wfm.h @@ -149,8 +149,10 @@ void mkdir_ui(void); void mkfile_ui(void); void multiprompt_ui(char *); void about(void); -void sendfile(void); +void save(void); void receivefile(void); +void mkurl(void); +void goto_url(void); off_t du(char *); void re_dir_ui(char *, int); int re_dir_up(char *); -- cgit v1.2.3