]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/Irc/extlib/phergie/Phergie/Plugin/Ideone.php
Merge branch '0.9.x' into 1.0.x
[quix0rs-gnu-social.git] / plugins / Irc / extlib / phergie / Phergie / Plugin / Ideone.php
1 <?php
2 /**
3  * Phergie
4  *
5  * PHP version 5
6  *
7  * LICENSE
8  *
9  * This source file is subject to the new BSD license that is bundled
10  * with this package in the file LICENSE.
11  * It is also available through the world-wide-web at this URL:
12  * http://phergie.org/license
13  *
14  * @category  Phergie
15  * @package   Phergie_Plugin_Ideone
16  * @author    Phergie Development Team <team@phergie.org>
17  * @copyright 2008-2010 Phergie Development Team (http://phergie.org)
18  * @license   http://phergie.org/license New BSD License
19  * @link      http://pear.phergie.org/package/Phergie_Plugin_Ideone
20  */
21
22 /**
23  * Interfaces with ideone.com to execute code and return the result.
24  *
25  * @category Phergie
26  * @package  Phergie_Plugin_Ideone
27  * @author   Phergie Development Team <team@phergie.org>
28  * @license  http://phergie.org/license New BSD License
29  * @link     http://pear.phergie.org/package/Phergie_Plugin_Ideone
30  * @uses     Phergie_Plugin_Command pear.phergie.org
31  */
32 class Phergie_Plugin_Ideone extends Phergie_Plugin_Abstract
33 {
34     /**
35      * Checks for dependencies.
36      *
37      * @return void
38      */
39     public function onLoad()
40     {
41         $this->plugins->getPlugin('Command');
42     }
43
44     /**
45      * Checks a service response for an error, sends a notice to the event
46      * source if an error has occurred, and returns whether an error was found.
47      *
48      * @param array $result Associative array representing the service response
49      *
50      * @return boolean TRUE if an error is found, FALSE otherwise
51      */
52     protected function isError($result)
53     {
54         if ($result['error'] != 'OK') {
55             $this->doNotice($this->event->getNick(), 'ideone error: ' . $result['error']);
56             return true;
57         }
58         return false;
59     }
60
61     /**
62      * Executes a source code sequence in a specified language and returns
63      * the result.
64      *
65      * @param string $language Programming language the source code is in
66      * @param string $code     Source code to execute
67      *
68      * @return void
69      */
70     public function onCommandIdeone($language, $code)
71     {
72         $source = $this->event->getSource();
73         $nick = $this->event->getNick();
74
75         // Get authentication credentials
76         $user = $this->getConfig('ideone.user', 'test');
77         $pass = $this->getConfig('ideone.pass', 'test');
78
79         // Normalize the command parameters
80         $language = strtolower($language);
81
82         // Massage PHP code to allow for convenient shorthand
83         if ($language == 'php') {
84             if (!preg_match('/^<\?(?:php)?/', $code)) {
85                 $code = '<?php ' . $code;
86             }
87             switch (substr($code, -1)) {
88                 case '}':
89                 case ';':
90                     break;
91                 default:
92                     $code .= ';';
93                     break;
94             }
95         }
96
97         // Identify the language to use
98         $client = new SoapClient('http://ideone.com/api/1/service.wsdl');
99         $response = $client->getLanguages($user, $pass);
100         if ($this->isError($response)) {
101             return;
102         }
103         $languageLength = strlen($language);
104         foreach ($response['languages'] as $languageId => $languageName) {
105             if (strncasecmp($language, $languageName, $languageLength) == 0) {
106                 break;
107             }
108         }
109
110         // Send the paste data
111         $response = $client->createSubmission(
112             $user,
113             $pass,
114             $code,
115             $languageId,
116             null, // string input - data from stdin
117             true, // boolean run - TRUE to execute the code
118             false // boolean private - FALSE to make the paste public
119         );
120         if ($this->isError($response)) {
121             return;
122         }
123         $link = $response['link'];
124
125         // Wait until the paste data is processed or the service fails
126         $attempts = $this->getConfig('ideone.attempts', 10);
127         foreach (range(1, $attempts) as $attempt) {
128             $response = $client->getSubmissionStatus($user, $pass, $link);
129             if ($this->isError($response)) {
130                 return;
131             }
132             if ($response['status'] == 0) {
133                 $result = $response['result'];
134                 break;
135             } else {
136                 $result = null;
137                 sleep(1);
138             }
139         }
140         if ($result == null) {
141             $this->doNotice($nick, 'ideone error: Timed out');
142             return;
143         }
144         if ($result != 15) {
145             $this->doNotice($nick, 'ideone error: Status code ' . $result);
146             return;
147         }
148
149         // Get details for the created paste
150         $response = $client->getSubmissionDetails(
151             $user,
152             $pass,
153             $link,
154             false, // boolean withSource - FALSE to not return the source code
155             false, // boolean withInput - FALSE to not return stdin data
156             true,  // boolean withOutput - TRUE to include output
157             true,  // boolean withStderr - TRUE to return stderr data
158             false  // boolean withCmpinfo - TRUE to return compilation info
159         );
160         if ($this->isError($response)) {
161             return;
162         }
163
164         // Replace the output if it exceeds a specified maximum length
165         $outputLimit = $this->getConfig('ideone.output_limit', 100);
166         var_dump($response);
167         if ($outputLimit && strlen($response['output']) > $outputLimit) {
168             $response['output'] = 'Output is too long to post';
169         }
170
171         // Format the message
172         $msg = $this->getConfig('ideone.format', '%nick%: [ %link% ] %output%');
173         $response['nick'] = $nick;
174         $response['link'] = 'http://ideone.com/' . $link;
175         foreach ($response as $key => $value) {
176             $msg = str_replace('%' . $key . '%', $value, $msg);
177         }
178         $this->doPrivmsg($source, $msg);
179     }
180 }