Localisation updates for core and extension messages from translatewiki.net (2011...
[toast/cookiecaptcha.git] / Asirra.class.php
1 <?php
2 /**
3  * @author Bachsau
4  */
5
6 class Asirra extends SimpleCaptcha
7 {
8         // Asirra URLs
9         public $asirra_localpath = '';
10         public $asirra_clientscript = 'http://challenge.asirra.com/js/AsirraClientSide.js';
11         public $asirra_apiscript = 'http://challenge.asirra.com/cgi/Asirra';
12
13         // As we don't have to store anything but some other things to do,
14         // we're going to replace that constructor completely.
15         function __construct()
16         {
17                 global $wgScriptPath, $wgAsirraScriptPath;
18
19                 // WTF isn't this in ConfirmEdit_body.php?
20                 wfLoadExtensionMessages('ConfirmEdit');
21                 wfLoadExtensionMessages('Asirra');
22
23                 // Try to find $asirra_localpath if not set
24                 if (!$this -> asirra_localpath = $wgAsirraScriptPath)
25                 {
26                         if (strpos(__FILE__, $_SERVER['DOCUMENT_ROOT']) === 0)
27                         {
28                                 $this -> asirra_localpath = preg_replace('/^' . preg_quote($_SERVER['DOCUMENT_ROOT'], '/') . '(\\/*)/s', '/', dirname(__FILE__));
29                         }
30                         else
31                         {
32                                 $this -> asirra_localpath = $wgScriptPath . '/extensions/ConfirmEdit';
33                         }
34                 }
35         }
36
37         function getForm()
38         {
39                 global $wgAsirraEnlargedPosition, $wgAsirraCellsPerRow, $wgOut;
40
41                 return '
42                         <script type="text/javascript" src="' . $this -> asirra_clientscript . '"></script>
43                         <script type="text/javascript" src="' . $this -> asirra_localpath . '/asirra_contentloaded.js"></script>
44                         <script type="text/javascript" src="' . $this -> asirra_localpath . '/asirra_humanverify.js"></script>
45                         <script type="text/javascript">
46                                 asirraState.SetEnlargedPosition("' . $wgAsirraEnlargedPosition . '");
47                                 asirraState.SetCellsPerRow(' . $wgAsirraCellsPerRow . ');
48                                 var asirra_js_failed = "' . $this -> getMessage('createaccount-fail') . '"
49                         </script>
50                         <noscript>' . $wgOut -> parse($this -> getMessage('nojs')) . '</noscript>
51                 ';
52         }
53
54         function getMessage( $action )
55         {
56                 $name = 'asirra-' . $action;
57                 $text = wfMsg($name);
58                 // Obtain a more tailored message, if possible, otherwise, fall back to
59                 // the default for edits
60                 return wfEmptyMsg($name, $text) ? wfMsg('asirra-edit') : $text;
61         }
62
63         // This is where the party goes on...
64         function passCaptcha()
65         {
66                 global $wgRequest, $wgAsirra;
67
68                 $ticket = $wgRequest -> getVal('Asirra_Ticket');
69                 $url = $this -> asirra_apiscript . '?action=ValidateTicket&ticket=' . $ticket;
70
71                 $ch = curl_init();
72                 curl_setopt($ch, CURLOPT_URL, $url);
73                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
74                 curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
75                 $resultXml = curl_exec($ch);
76                 curl_close($ch);
77
78                 $xml_parser = xml_parser_create();
79                 xml_set_element_handler($xml_parser, 'AsirraXmlParser::startElement', 'AsirraXmlParser::endElement');
80                 xml_set_character_data_handler($xml_parser, 'AsirraXmlParser::characterData');
81                 xml_parse($xml_parser, $resultXml, 1);
82                 xml_parser_free($xml_parser);
83
84                 if ($wgAsirra['passed'])
85                 {
86                         return true;
87
88                 }
89                 return false;
90         }
91 }
92
93 class AsirraXmlParser
94 {
95         static function startElement($parser, $name, $attrs)
96         {
97                 global $wgAsirra;
98
99                 $wgAsirra['inResult'] = ($name=="RESULT");
100         }
101
102         static function endElement($name)
103         {
104                 global $wgAsirra;
105
106                 $wgAsirra['inResult'] = 0;
107         }
108
109         static function characterData($parer, $data)
110         {
111                 global $wgAsirra;
112
113                 if ($wgAsirra['inResult'] && $data == "Pass")
114                 {
115                         $wgAsirra['passed'] = 1;
116                 }
117         }
118 }