1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
|
#region License GNU GPL
// VoteModelView.cs
//
// Copyright (C) 2013 - BehaviorIsManaged
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free Software Foundation;
// either version 2 of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
// See the GNU General Public License for more details.
// You should have received a copy of the GNU General Public License along with this program;
// if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#endregion
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Web;
using System.Drawing;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Windows.Controls;
using System.Windows.Forms;
using System.Windows.Forms.Integration;
using System.Windows.Media.Imaging;
using Uplauncher.Properties;
using WebBrowser = System.Windows.Forms.WebBrowser;
namespace Uplauncher
{
public class VoteModelView : INotifyPropertyChanged
{
private int m_reloadTries;
private WebBrowser m_webBrowser;
private DelegateCommand m_reloadCommand;
private WebBrowserDocumentCompletedEventHandler m_onLoaded;
private DelegateCommand m_voteCommand;
public VoteModelView(string url, int siteID)
{
RawURL = url;
SiteID = siteID;
AskingCredentials = true;
}
public string RawURL
{
get;
set;
}
public string CompleteURL
{
get { return string.Format(RawURL, SiteID); }
}
public int SiteID
{
get;
set;
}
public BitmapImage Captcha
{
get;
set;
}
public string CaptchaString
{
get;
set;
}
public string ChallengeField
{
get;
set;
}
public string SessionId
{
get;
set;
}
public string Username
{
get;
set;
}
public string Password
{
get;
set;
}
public bool AskingCredentials
{
get;
set;
}
public bool NotAskingCredentials
{
get { return !AskingCredentials; }
}
public bool AreCredentialsValid
{
get;
private set;
}
public bool WrongCredentials
{
get;
private set;
}
public bool IsVoting
{
get;
set;
}
public bool? DialogResult
{
get;
set;
}
public DelegateCommand ReloadCommand
{
get
{
return m_reloadCommand ?? ( m_reloadCommand = new DelegateCommand(Reload, () => !IsVoting) );
}
}
public DelegateCommand VoteCommand
{
get
{
return m_voteCommand ?? ( m_voteCommand = new DelegateCommand(Vote, () => !IsVoting) );
}
}
#region LoginCommand
private DelegateCommand m_loginCommand;
public DelegateCommand LoginCommand
{
get { return m_loginCommand ?? (m_loginCommand = new DelegateCommand(OnLogin, CanLogin)); }
}
private bool CanLogin(object parameter)
{
return AskingCredentials && parameter is PasswordBox &&
!string.IsNullOrEmpty((parameter as PasswordBox).Password) && !string.IsNullOrEmpty(Username);
}
private void OnLogin(object parameter)
{
if (parameter == null || !CanLogin(parameter))
return;
Password = ( parameter as PasswordBox ).Password;
try
{
var client = new WebClient();
var output = client.DownloadString(string.Format(Constants.APILoginURL, Username, Password, 0));
var result = bool.Parse(output);
if (!result)
{
WrongCredentials = true;
}
else
{
AskingCredentials = false;
AreCredentialsValid = true;
}
}
catch (WebException)
{
MessageBox.Show(Resources.Unreached_Server, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
DialogResult = false;
}
}
#endregion
public void Vote()
{
if (string.IsNullOrEmpty(CaptchaString))
{
MessageBox.Show(Resources.Empty_Captcha, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
IsVoting = true;
ReloadCommand.RaiseCanExecuteChanged();
VoteCommand.RaiseCanExecuteChanged();
HtmlElement input = null;
HtmlElement button = null;
foreach (HtmlElement element in m_webBrowser.Document.GetElementsByTagName("input"))
{
if (element.GetAttribute("value") == "Voter")
{
button = element;
}
if (element.GetAttribute("name") == "recaptcha_response_field")
{
input = element;
}
}
// errors
m_webBrowser.Navigating += OnNavigating;
m_webBrowser.DocumentCompleted += OnVoteCompleted;
input.InnerText = CaptchaString;
button.InvokeMember("click");
}
private void OnNavigating(object sender, WebBrowserNavigatingEventArgs e)
{
}
private void OnVoteCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
m_webBrowser.DocumentCompleted -= OnVoteCompleted;
if (m_webBrowser.Document == null)
{
MessageBox.Show(Resources.Unknown_Vote_Error, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
else
{
if (!m_webBrowser.Document.Cookie.Contains(" parasubmitvote2"))
{
MessageBox.Show(Resources.Vote_Refused_Error, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
DialogResult = false;
}
else
{
var client = new WebClient();
var output = client.DownloadString(string.Format(Constants.APILoginURL, Username, Password, 1));
if (string.IsNullOrEmpty(output))
{
MessageBox.Show(Resources.Unreached_Server, Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
DialogResult = false;
}
int minutesUntilNextVote;
if (int.TryParse(output, out minutesUntilNextVote))
{
MessageBox.Show(string.Format(Resources.Already_Voted, minutesUntilNextVote), Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
DialogResult = false;
}
else
{
var result = bool.Parse(output);
DialogResult = result;
}
}
}
}
public void Reload()
{
m_webBrowser = new WebBrowser();
// c'est ici que j'aimerai suprimmer les cookies.
m_webBrowser.Navigate(CompleteURL);
m_webBrowser.DocumentCompleted += m_onLoaded = (e, s) => OnReloaded();
m_webBrowser.ScriptErrorsSuppressed = true;
m_webBrowser.AllowWebBrowserDrop = false;
}
private void OnReloaded()
{
try
{
m_webBrowser.DocumentCompleted -= m_onLoaded;
if (m_webBrowser.Document == null)
throw new Exception(string.Format("Cannot reach {0}", CompleteURL));
var element = m_webBrowser.Document.GetElementById("recaptcha_challenge_field");
if (element == null)
throw new Exception("Element with id 'recaptcha_challenge_field' not found in source. Check URL");
ChallengeField = element.GetAttribute("value");
if (string.IsNullOrEmpty(ChallengeField))
throw new Exception("'recaptcha_challenge_field' has no value attribute or is empty");
var imageElement = m_webBrowser.Document.GetElementById("recaptcha_image");
if (imageElement == null)
throw new Exception("Element with id 'recaptcha_image' not found in source. Check URL");
if (imageElement.FirstChild == null || !imageElement.FirstChild.TagName.Equals("img", StringComparison.OrdinalIgnoreCase))
throw new Exception("Element 'recaptcha_image' first child is not an <img>");
var captchaURL = imageElement.FirstChild.GetAttribute("src");
if (string.IsNullOrEmpty(captchaURL))
throw new Exception("Captcha image url is null or empty in element 'recaptcha_image'");
Captcha = new BitmapImage(new Uri(captchaURL));
if (Captcha == null)
throw new Exception(string.Format("Cannot retrieve captcha image URL={0}", captchaURL));
foreach (string cookie in m_webBrowser.Document.Cookie.Trim().Split(';'))
{
if (string.IsNullOrEmpty(cookie))
continue;
string name = cookie.Split('=')[0];
string value = cookie.Substring(name.Length + 1).Trim();
if (name.Trim() == "PHPSESSID")
SessionId = value;
}
}
catch (Exception ex)
{
m_reloadTries++;
if (m_reloadTries > 5)
{
MessageBox.Show(string.Format(Resources.Error_OnLoading, ex), Resources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error);
DialogResult = false;
}
else
{
Reload();
}
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
} |
Partager