-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathextendRemoteControl.php
175 lines (166 loc) · 7.28 KB
/
extendRemoteControl.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
<?php
/**
* Demo plugin to show how to extendRemoteControl Plugin for LimeSurvey.
*
* @author Denis Chenu <denis@sondages.pro>
* @copyright 2015-2019 Denis Chenu <http://sondages.pro>
* @license GPL v3
* @version 1.2.0
*
* 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 3 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.
*/
class extendRemoteControl extends PluginBase {
protected $storage = 'DbStorage';
static protected $description = 'Allow to add function to remoteControl';
static protected $name = 'extendRemoteControl';
protected $settings = array(
'information' => array(
'type' => 'info',
'content' => '',
'default'=> false
),
);
public function init()
{
$this->subscribe('newDirectRequest');
$this->subscribe('newUnsecureRequest','newDirectRequest');
}
/**
* The access is done here for remoteControl access with plugin
* @see remotecontrol::run()
*/
public function newDirectRequest()
{
$oEvent = $this->getEvent();
if ($oEvent->get('target') != $this->getName())
return;
$action = $oEvent->get('function');
$oAdminController = new \AdminController('admin/remotecontrol');
Yii::import('application.helpers.remotecontrol.*');
Yii::setPathOfAlias('extendRemoteControl', dirname(__FILE__));
Yii::import("extendRemoteControl.RemoteControlHandler");
$oHandler=new \RemoteControlHandler($oAdminController);
$RPCType=Yii::app()->getConfig("RPCInterface");
if($RPCType!='json')
{
header("Content-type: application/json");
echo json_encode(array(
'status'=>'error',
'message'=>'Only for json RPCInterface',
));
Yii::app()->end();
}
if (Yii::app()->request->getIsPostRequest())
{
Yii::app()->loadLibrary('LSjsonRPCServer');
if (!isset($_SERVER['CONTENT_TYPE']))
{
$serverContentType = explode(';', $_SERVER['HTTP_CONTENT_TYPE']);
$_SERVER['CONTENT_TYPE'] = reset($serverContentType);
}
LSjsonRPCServer::handle($oHandler);
}
elseif(Yii::app()->getConfig("rpc_publish_api") == true) // Show like near Core LS do it
{
$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;
$version=floatval(Yii::app()->getConfig("versionnumber"));
if($version<2.5)
{
$content=$oAdminController->renderPartial('application.views.admin.remotecontrol.index_view',$aData,true);
$oEvent->setContent($this, $content);
}
else // Show something for 2.5, but 2.5 have a better system
{
return $oAdminController->render('application.views.admin.remotecontrol.index_view',$aData);
}
}
}
/**
* Update the information content to show the good link
* @params getValues
*/
public function getPluginSettings($getValues=true)
{
if(!Permission::model()->hasGlobalPermission('settings','read')) {
throw new CHttpException(403);
}
$this->settings['information']['content']="";
/* test if plugins/unsecure is in noCsrfValidationRoutes : in internal for compatible LimeSurvey version */
if(in_array('plugins/unsecure',App()->request->noCsrfValidationRoutes))
{
$url=Yii::app()->getController()->createAbsoluteUrl('plugins/unsecure', array('plugin' => $this->getName(), 'function' => 'action'));
}
else
{
$this->settings['information']['content'].="<p class='alert alert-warning'>You need to add 'plugins/direct' to noCsrfValidationRoutes in your config file</p>";
$url=Yii::app()->getController()->createAbsoluteUrl('plugins/direct', array('plugin' => $this->getName(), 'function' => 'action'));
}
if(Yii::app()->getConfig("RPCInterface")=='json')
{
$this->settings['information']['content'].="<p class='alert alert-info'>".sprintf(gT("The remote url was <code>%s</code>",'unescaped'),$url)."</p>";
if(Yii::app()->getConfig("rpc_publish_api") == true)
{
if(floatval(Yii::app()->getConfig("versionnumber"))>=2.5)
{
$url=Yii::app()->getController()->createAbsoluteUrl('admin/pluginhelper', array('plugin' => $this->getName(), 'sa'=>'sidebody','method'=>'actionIndex','surveyId'=>0));
}
$this->settings['information']['content'].="<p class='alert alert-warning'>".sprintf(gT("The API was published on <a href='%s'>%s</a>",'unescaped'),$url,$url)."</p>";
}
}
else
{
$this->settings['information']['content']="<p class='alert alert-danger'>".gT("JSON RPC is not active.")."</p>";
}
return parent::getPluginSettings($getValues);
}
/**
* Show remote control function list in 2.50
* Used by PluginHelper->getContent
* @see remotecontrol::run()
*/
public function actionIndex()
{
if(Yii::app()->getConfig("RPCInterface")=='json' && Yii::app()->getConfig("rpc_publish_api"))
{
$oAdminController = new \AdminController('admin/remotecontrol');
Yii::import('application.helpers.remotecontrol.*');
Yii::setPathOfAlias('extendRemoteControl', dirname(__FILE__));
Yii::import("extendRemoteControl.RemoteControlHandler");
$oHandler=new \RemoteControlHandler($oAdminController);
$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' => "<pre>".$method->getDocComment()."</pre>",
'parameters' => $method->getParameters()
);
}
}
ksort($list);
$aData['method'] = 'json';
$aData['list'] = $list;
return Yii::app()->controller->renderPartial('application.views.admin.remotecontrol.index_view', $aData, true);
}
}
}