public bool Login(string user, string password) { try { _username = user; _password = password; string login_url = _serverURL + "/user/login"; easy.SetOpt(LibCurl.CURLoption.CURLOPT_URL, login_url); string loginFields = "name=" + user + "&pass="******"&form_id=user_login" + "&op=Log in"; easy.SetOpt(LibCurl.CURLoption.CURLOPT_POSTFIELDS, loginFields); LibCurl.CURLcode exec = easy.Perform(); string retUrl = ""; EasyCurl.GetInfo(LibCurl.CURLINFO.CURLINFO_EFFECTIVE_URL, ref retUrl); if ((retUrl == login_url) && (HttpConnectCode == 200)) //case already loggedin, page is returning 403, and not 200 { errorMessage("Coudn't login to your site with cURL"); _isloggedIn = false; } else if (HttpConnectCode == 403)//need to logout before { if (Logout()) { Login(user, password); } else { errorMessage("Coudn't logout from previous logged-in user"); return(false); } } else if (HttpConnectCode == 200) { _isloggedIn = true; } else { _isloggedIn = false; } } catch (Exception ex) { errorMessage(ex.Message); _isloggedIn = false; } return(_isloggedIn); }
public bool Logout() { try { easy.SetOpt(LibCurl.CURLoption.CURLOPT_URL, _serverURL + @"/logout"); LibCurl.CURLcode exec = easy.Perform(); string retUrl = ""; EasyCurl.GetInfo(LibCurl.CURLINFO.CURLINFO_EFFECTIVE_URL, ref retUrl); _isloggedIn = false; return(retUrl == _serverURL); } catch (Exception ex) { errorMessage(ex.Message); return(false); } }
/// <summary> /// Upload a file to Drupal using the form-file module and CURL /// </summary> /// <param name="localPath">Local file location</param> /// <param name="serverDirectory">Save to a specific directory in drupal</param> /// <returns> File ID or -1 if failed to upload</returns> public int FileUpload(string localPath, string serverDirectory) { // Use file form module string url = ServerURL + "file-form"; LibCurl.MultiPartForm mf = new LibCurl.MultiPartForm(); AddFormFile(mf, localPath, "files[file_upload]"); // Optional parameter - save to a different directory AddFormField(mf, serverDirectory, "file_directory"); AddFormField(mf, "form_token", getToken(url, "edit-file-form-upload-form-token")); AddFormField(mf, "form_id", "file_form_upload"); AddFormField(mf, "op", "Upload file"); EasyCurl.SetOpt(LibCurl.CURLoption.CURLOPT_HTTPPOST, mf); EasyCurl.SetOpt(LibCurl.CURLoption.CURLOPT_URL, url); _htmlDataIn = "";//clear html data in return LibCurl.CURLcode exec = DrupCurlPerform(); // Check if Json returned a file id int fileID; string ex = @"fid"":.""([^""]*)"; Regex rx = new Regex(ex); MatchCollection fields = rx.Matches(_htmlDataIn); if (fields.Count > 0) { fileID = Convert.ToInt32(fields[0].Groups[1].Captures[0].Value); } else { fileID = -1; } //JsonUtility.GenerateIndentedJsonText = false; string tempHttp = ""; EasyCurl.GetInfo(LibCurl.CURLINFO.CURLINFO_EFFECTIVE_URL, ref tempHttp); if (((fileID == -1)))// || (tempHttp == ServerURL + "file-form")) { sendLogEvent("Error uploading file, Curl error no: " + "\n", "Curl", Enums.MessageType.Error); } return(fileID); }
public bool DownloadFile(string httpPath, string savePath) { try { if (_isloggedIn) { easy.SetOpt(LibCurl.CURLoption.CURLOPT_WRITEFUNCTION, wf); fileDownload = new FileStream(savePath, FileMode.Create); ///replace space with %20 string a = " "; string b = "&20"; httpPath = httpPath.Replace(a, b); easy.SetOpt(LibCurl.CURLoption.CURLOPT_URL, httpPath); LibCurl.CURLcode exec = easy.Perform(); //CURLcode exec = DrupCurlPerform(); double d = 0; easy.GetInfo(LibCurl.CURLINFO.CURLINFO_CONTENT_LENGTH_DOWNLOAD, ref d); //easy.SetOpt(CURLoption.CURLOPT_WRITEFUNCTION,null); fileDownload.Close(); if ((d == 0) || (this.HttpConnectCode != 200) || (!DrutNETBase.FileExists(savePath))) { sendLogEvent("Can't download file with Curl: " + exec.ToString() + "\n", "Curl", Enums.MessageType.Error); return(false); } return(true); } else { sendLogEvent("CURL not logged-in", "Curl" + "\n", Enums.MessageType.Error); return(false); } } catch (Exception ex) { errorMessage(ex.Message); return(false); } }
private string getToken(string url, string formId) { _htmlDataIn = ""; //clear data in string EasyCurl.SetOpt(LibCurl.CURLoption.CURLOPT_URL, url); LibCurl.CURLcode exec = DrupCurlPerform(); if (HttpConnectCode != 200) { sendLogEvent("Error access " + HttpConnectCode.ToString() + "\n", "Curl", Enums.MessageType.Error); _htmlDataIn = ""; return(""); } Regex rx = new Regex(formId + @".*value=""([^""]*)"); MatchCollection fields = rx.Matches(_htmlDataIn); _htmlDataIn = ""; if (fields.Count > 0) { return(fields[0].Groups[1].Captures[0].Value); } else { return(""); } }