/
remotecontrol.php
181 lines (163 loc) · 7.43 KB
/
remotecontrol.php
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
<?php
/*
* LimeSurvey
* Copyright (C) 2007-2011 The LimeSurvey Project Team / Carsten Schmitz
* All rights reserved.
* License: GNU/GPL License v2 or later, see LICENSE.php
* LimeSurvey is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYRIGHT.php for copyright notices and details.
*
*/
class remotecontrol extends Survey_Common_Action
{
/**
* @var Zend_XmlRpc_Server
*/
protected $xmlrpc;
/**
* This is the XML-RPC server routine
*
* @access public
* @return void
*/
public function run()
{
Yii::import('application.helpers.remotecontrol.*');
$oHandler=new remotecontrol_handle($this->controller);
$RPCType=Yii::app()->getConfig("RPCInterface");
if (Yii::app()->getRequest()->isPostRequest) {
if ($RPCType=='xml')
{
$cur_path = get_include_path();
set_include_path($cur_path . PATH_SEPARATOR . APPPATH . 'helpers');
// Yii::import was causing problems for some odd reason
require_once('Zend/XmlRpc/Server.php');
require_once('Zend/XmlRpc/Server/Exception.php');
require_once('Zend/XmlRpc/Value/Exception.php');
$this->xmlrpc = new Zend_XmlRpc_Server();
$this->xmlrpc->sendArgumentsToAllMethods(false);
Yii::import('application.libraries.LSZend_XmlRpc_Response_Http');
$this->xmlrpc->setResponseClass('LSZend_XmlRpc_Response_Http');
$this->xmlrpc->setClass($oHandler);
$result = $this->xmlrpc->handle();
if ($result instanceof LSZend_XmlRpc_Response_Http)
{
$result->printXml();
}
else
{
// an Zend_XmlRpc_Server_Fault with exception message from XMLRPC
echo $result;
}
}
elseif($RPCType=='json')
{
Yii::app()->loadLibrary('LSjsonRPCServer');
if (!isset($_SERVER['CONTENT_TYPE']))
{
$serverContentType = explode(';', $_SERVER['HTTP_CONTENT_TYPE']);
$_SERVER['CONTENT_TYPE'] = reset($serverContentType);
}
LSjsonRPCServer::handle($oHandler);
}
foreach (App()->log->routes as $route)
{
$route->enabled = $route->enabled && !($route instanceOf CWebLogRoute);
}
exit;
} else {
// Disabled output of API methods for now
if (Yii::app()->getConfig("rpc_publish_api") == true && in_array($RPCType, array('xml', 'json'))) {
$reflector = new ReflectionObject($oHandler);
foreach ($reflector->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
/* @var $method ReflectionMethod */
if (substr($method->getName(),0,1) !== '_') {
$list[$method->getName()] = array(
'description' => str_replace(array("\r", "\r\n", "\n"), "<br/>", $method->getDocComment()),
'parameters' => $method->getParameters()
);
}
}
ksort($list);
$aData['method'] = $RPCType;
$aData['list'] = $list;
$aData['display']['menu_bars'] = false; // Hide normal menu bar
$this->_renderWrappedTemplate('remotecontrol', array('index_view'), $aData);
}
}
}
/**
* Simple procedure to test most RPC functions
*
*/
public function test()
{
die(); // Remove if you want to test this function
$RPCType=Yii::app()->getConfig("RPCInterface");
$serverUrl = App()->createAbsoluteUrl('/admin/remotecontrol');
$sFileToImport=dirname(Yii::app()->basePath).DIRECTORY_SEPARATOR.'docs'.DIRECTORY_SEPARATOR.'demosurveys'.DIRECTORY_SEPARATOR.'limesurvey2_sample_survey_english.lss';
if ($RPCType == 'xml') {
$cur_path = get_include_path();
set_include_path($cur_path . PATH_SEPARATOR . APPPATH . 'helpers');
require_once('Zend/XmlRpc/Client.php');
$client = new Zend_XmlRpc_Client($serverUrl);
} elseif ($RPCType == 'json') {
Yii::app()->loadLibrary('jsonRPCClient');
$client = new jsonRPCClient($serverUrl);
}
else die('RPC interface not activated in global settings');
$sSessionKey= $client->call('get_session_key', array('admin','password'));
if (is_array($sSessionKey)) {echo $sSessionKey['status']; die();}
else
{
echo 'Retrieved session key'.'<br>';
}
$sLSSData=base64_encode(file_get_contents($sFileToImport));
$iSurveyID=$client->call('import_survey', array($sSessionKey, $sLSSData, 'lss','Test import by JSON_RPC',1000));
echo 'Created new survey SID:'.$iSurveyID.'<br>';
$aResult=$client->call('activate_survey', array($sSessionKey, $iSurveyID));
if ($aResult['status']=='OK')
{
echo 'Survey '.$iSurveyID.' successfully activated.<br>';
}
$aResult=$client->call('activate_tokens', array($sSessionKey, $iSurveyID,array(1,2)));
if ($aResult['status']=='OK')
{
echo 'Tokens for Survey ID '.$iSurveyID.' successfully activated.<br>';
}
$aResult=$client->call('set_survey_properties', array($sSessionKey, $iSurveyID,array('faxto'=>'0800-LIMESURVEY')));
if (!array_key_exists('status', $aResult))
{
echo 'Modified survey settings for survey '.$iSurveyID.'<br>';
}
$aResult=$client->call('add_language', array($sSessionKey, $iSurveyID,'ar'));
if ($aResult['status']=='OK')
{
echo 'Added Arabian as additional language'.'<br>';
}
$aResult=$client->call('set_language_properties', array($sSessionKey, $iSurveyID,array('surveyls_welcometext'=>'An Arabian welcome text!'),'ar'));
if ($aResult['status']=='OK')
{
echo 'Modified survey locale setting welcometext for Arabian in survey ID '.$iSurveyID.'<br>';
}
$aResult=$client->call('delete_language', array($sSessionKey, $iSurveyID,'ar'));
if ($aResult['status']=='OK')
{
echo 'Removed Arabian as additional language'.'<br>';
}
//Very simple example to export responses as Excel file
//$aResult=$client->call('export_responses', array($sSessionKey,$iSurveyID,'xls'));
//$aResult=$client->call('export_responses', array($sSessionKey,$iSurveyID,'pdf'));
//$aResult=$client->call('export_responses', array($sSessionKey,$iSurveyID,'doc'));
$aResult=$client->call('export_responses', array($sSessionKey,$iSurveyID,'csv'));
//file_put_contents('test.xls',base64_decode(chunk_split($aResult)));
$aResult=$client->call('delete_survey', array($sSessionKey, $iSurveyID));
echo 'Deleted survey SID:'.$iSurveyID.'-'.$aResult['status'].'<br>';
// Release the session key - close the session
$Result= $client->call('release_session_key', array($sSessionKey));
echo 'Closed the session'.'<br>';
}
}