PageRenderTime 63ms CodeModel.GetById 34ms RepoModel.GetById 0ms app.codeStats 0ms

/src/frapi/admin/application/modules/console/controllers/ActionController.php

https://github.com/Martin1982/IBMessagingWorkshopServer
PHP | 353 lines | 242 code | 51 blank | 60 comment | 33 complexity | 84b403468f1b06e9cca48df8bef2f583 MD5 | raw file
  1. <?php
  2. /*
  3. * LICENSE
  4. *
  5. * This source file is subject to the new BSD license that is bundled
  6. * with this package in the file LICENSE.txt.
  7. * It is also available through the world-wide-web at this URL:
  8. * http://getfrapi.com/license/new-bsd
  9. * If you did not receive a copy of the license and are unable to
  10. * obtain it through the world-wide-web, please send an email
  11. * to license@getfrapi.com so we can send you a copy immediately.
  12. *
  13. * @license New BSD
  14. * @package frapi-admin
  15. */
  16. class Console_ActionController extends Zend_Controller_Action
  17. {
  18. private $tr;
  19. /**
  20. * Main Initializer
  21. *
  22. * This is the public method that will be used by the controller base
  23. */
  24. public function init()
  25. {
  26. $this->tr = Zend_Registry::get('tr');
  27. $this->view->tr = Zend_Registry::get('tr');
  28. }
  29. /**
  30. * List action
  31. *
  32. * This is the list action. It will list all available actions.
  33. *
  34. * @return void
  35. */
  36. public function listAction()
  37. {
  38. $this->_helper->viewRenderer->setViewSuffix('txt');
  39. $model = new Default_Model_Action();
  40. $this->view->actions = $model->getAll();
  41. }
  42. /**
  43. * Add an action
  44. *
  45. * This is the add action method. It literally does what it say.
  46. * It adds an action.
  47. *
  48. *
  49. * @return void
  50. */
  51. public function addAction()
  52. {
  53. $this->_helper->viewRenderer->setViewSuffix('txt');
  54. // The options we are accepting for adding
  55. $options = new Zend_Console_Getopt(
  56. array(
  57. 'name|n=s' => $this->tr->_('NAME'),
  58. 'enabled|e' => $this->tr->_('IS_ACTION_ENABLED'),
  59. 'public|p' => $this->tr->_('IS_ACTION_PUBLIC'),
  60. 'route|r=s' => $this->tr->_('CUSTOM_ROUTE'),
  61. 'description|d=s' => $this->tr->_('DESCRIPTION'),
  62. 'parameters|pa=s' => $this->tr->_('CLI_PARAMETERS'),
  63. 'required-parameters|rp=s' => $this->tr->_('CLI_REQUIRED_PARAMETERS'),
  64. )
  65. );
  66. try {
  67. $options->parse();
  68. } catch (Zend_Console_Getopt_Exception $e) {
  69. $this->view->message = $e->getUsageMessage();
  70. return;
  71. }
  72. if ($options->name == '') {
  73. $this->view->message = $options->getUsageMessage();
  74. return;
  75. } else if ($options->route == '') {
  76. $this->view->message = $options->getUsageMessage();
  77. return;
  78. }
  79. $action_name = $options->name;
  80. $action_enabled = $options->enabled === true ? '1' : '0';
  81. $action_public = $options->public === true ? '1' : '0';
  82. $action_route = $options->route;
  83. $action_description = $options->description;
  84. $submit_data = array (
  85. 'name' => $action_name,
  86. 'enabled' => $action_enabled,
  87. 'public' => $action_public,
  88. 'route' => $action_route,
  89. 'description' => $action_description
  90. );
  91. // Handle parameters passed
  92. $action_optional_parameters = explode(',', $options->parameters);
  93. $i = 0;
  94. foreach ($action_optional_parameters as $parameter) {
  95. if ($parameter != '') {
  96. $submit_data['param'][$i] = $parameter;
  97. $i++;
  98. }
  99. }
  100. $action_required_parameters = explode(',', $options->getOption('required-parameters'));
  101. foreach ($action_required_parameters as $parameter) {
  102. if ($parameter != '') {
  103. $submit_data['param'][$i] = $parameter;
  104. $submit_data['required'][$i] = '1';
  105. $i++;
  106. }
  107. }
  108. $model = new Default_Model_Action();
  109. try {
  110. $model->add($submit_data);
  111. $this->view->message = $this->tr->_('ADDED_ACTION') . ': ' . $action_name . PHP_EOL;
  112. } catch (RuntimeException $e) {
  113. $this->view->message = $this->tr->_('ERROR_ADDING_ACTION') . ': ' . $action_name . '. ' . $e->getMessage() . PHP_EOL;
  114. }
  115. }
  116. /**
  117. * Delete an action
  118. *
  119. * This is the delete action method. It allows you to delete an action.
  120. *
  121. * @return void
  122. */
  123. public function deleteAction()
  124. {
  125. $this->_helper->viewRenderer->setViewSuffix('txt');
  126. // The options we are accepting for deleting
  127. $options = new Zend_Console_Getopt(
  128. array(
  129. 'name|n=s' => $this->tr->_('NAME'),
  130. )
  131. );
  132. try {
  133. $options->parse();
  134. } catch (Zend_Console_Getopt_Exception $e) {
  135. $this->view->message = $e->getUsageMessage();
  136. return;
  137. }
  138. if ($options->name == '') {
  139. echo $options->getUsageMessage();
  140. exit();
  141. }
  142. $action_name = ucfirst(strtolower($options->name));
  143. $model = new Default_Model_Action();
  144. $tempActions = $model->getList();
  145. $action_id = null;
  146. foreach ($tempActions as $hash => $tempName) {
  147. if ($action_name == $tempName) {
  148. $action_id = $hash;
  149. break;
  150. }
  151. }
  152. if (!$action_id) {
  153. $this->view->message = $this->tr->_('COULD_NOT_DELETE_ACTION') . ': ' . $action_name . '. ' . $this->tr->_('COULD_NOT_FIND_MATCH') . PHP_EOL;
  154. return;
  155. }
  156. try {
  157. $model->delete($action_id);
  158. $this->view->message = $this->tr->_('SUCCESS_DELETE_ACTION') . ': ' . $action_name . PHP_EOL;
  159. } catch (RuntimeException $e) {
  160. $this->view->message = $this->tr->_('ERROR_DELETING_ACTION') . ': ' . $action_name . '. ' . $e->getMessage() . PHP_EOL;
  161. }
  162. }
  163. /**
  164. * Sync the actions
  165. *
  166. * This is the sync actions method. It syncs actions to the files.
  167. *
  168. * @return void
  169. */
  170. public function syncAction()
  171. {
  172. $this->_helper->viewRenderer->setViewSuffix('txt');
  173. $dir = ROOT_PATH . DIRECTORY_SEPARATOR . 'custom' . DIRECTORY_SEPARATOR . 'Action';
  174. if (!is_writable($dir)) {
  175. $this->view->message = $this->tr->_('ACTION_WRITE_ERROR', $dir) . PHP_EOL;
  176. return;
  177. }
  178. $model = new Default_Model_Action();
  179. try {
  180. $model->sync();
  181. $this->view->message = $this->tr->_('ACTION_DEV_SYNC_SUCCESS') . PHP_EOL;
  182. } catch (RuntimeException $e) {
  183. $this->view->message = $this->tr->_('ACTION_ERROR_SYNCHRO') . ' ' . $e->getMessage();
  184. }
  185. }
  186. /**
  187. * Test an action
  188. *
  189. * This is the test action method. It test a specific action.
  190. *
  191. * @return void
  192. */
  193. public function testAction()
  194. {
  195. $this->_helper->viewRenderer->setViewSuffix('txt');
  196. // The options we are accepting for adding
  197. $options = new Zend_Console_Getopt(
  198. array(
  199. 'name|n=s' => 'Name of the action to call.',
  200. 'parameters|p=s' => 'Paramters to use. For example var1=val1&var2=val2',
  201. 'format|f=s' => 'Format to return. Defaults to XML.',
  202. 'method|m=s' => 'Method to use. Defaults to GET.',
  203. 'email|e=s' => 'Email or username to use.',
  204. 'secretkey|sk=s' => 'Secret key associated with email passed.',
  205. 'domain|d=s' => 'Domain to use, if not included will use default',
  206. 'query-uri|u=s' => 'Query uri to use. For example /testing/1',
  207. 'https|h' => 'Use https.',
  208. )
  209. );
  210. try {
  211. $options->parse();
  212. } catch (Zend_Console_Getopt_Exception $e) {
  213. $this->view->message = $e->getUsageMessage();
  214. return;
  215. }
  216. if ($options->name == '') {
  217. $this->view->message = $options->getUsageMessage();
  218. return;
  219. }
  220. $confModel = new Default_Model_Configuration();
  221. if (!$confModel->getKey('api_url')) {
  222. $this->view->message = 'Remember you can set the default API domain name in your admin configuration.' . PHP_EOL;
  223. }
  224. if (!class_exists('HttpRequest')) {
  225. $this->view->message = 'HttpRequest class was not found the pecl_http (http://pecl.php.net/package/pecl_http) package is required to use the tester.' . PHP_EOL;
  226. return;
  227. }
  228. $action_name = $options->name;
  229. $params = $options->parameters;
  230. $format = $options->format;
  231. $method = $options->method;
  232. $email = $options->email;
  233. $password = $options->secretkey;
  234. $url = $options->domain;
  235. $ssl = $options->https;
  236. $query_uri = $options->getOption('query-uri');
  237. if ($url == '') {
  238. $url = $confModel->getKey('api_url');
  239. }
  240. if ($query_uri == '') {
  241. $actionModel = new Default_Model_Action();
  242. $actions = $actionModel->getAll();
  243. foreach( $actions as $action_details) {
  244. if ($action_details['name'] == $action_name) {
  245. $query_uri = $action_details['route'];
  246. }
  247. }
  248. }
  249. $newMethod = HTTP_METH_GET;
  250. switch (strtolower($method)) {
  251. case 'get':
  252. $newMethod = HTTP_METH_GET;
  253. break;
  254. case 'post':
  255. $newMethod = HTTP_METH_POST;
  256. break;
  257. case 'put':
  258. $newMethod = HTTP_METH_PUT;
  259. break;
  260. case 'delete':
  261. $newMethod = HTTP_METH_DELETE;
  262. break;
  263. case 'head':
  264. $newMethod = HTTP_METH_HEAD;
  265. break;
  266. }
  267. $request_url = 'http' . ($ssl !== null ? 's' : '') . '://' . $url . '/' . $query_uri . '.' . strtolower($format);
  268. $httpOptions = array();
  269. if ($email && $password) {
  270. $httpOptions = array(
  271. 'headers' => array('Accept' => '*/*'),
  272. 'httpauth' => $email . ':' . $password,
  273. 'httpauthtype' => HTTP_AUTH_DIGEST,
  274. );
  275. }
  276. $request = new HttpRequest($request_url, $newMethod, $httpOptions);
  277. if ("POST" == strtoupper($method)) {
  278. $request->setBody($params);
  279. } else {
  280. $request->setQueryData($params);
  281. }
  282. $res = $request->send();
  283. $responseInfo = $request->getResponseInfo();
  284. $this->view->request_url = $responseInfo['effective_url'];
  285. $this->view->response_header = $this->collapseHeaders($res->getHeaders());
  286. $this->view->content = $res->getBody();
  287. $this->view->status = $res->getResponseCode();
  288. $this->view->method = isset($method) ? strtoupper($method) : 'GET';
  289. $this->view->request_post_fields = ($newMethod == HTTP_METH_POST) ? $params : '';
  290. }
  291. protected function collapseHeaders($headers)
  292. {
  293. $header_string = "";
  294. foreach ($headers as $name => $value) {
  295. if (is_array($value)) {
  296. $value = implode("\n\t", $value);
  297. }
  298. $header_string .= $name . ": " . wordwrap($value, 45, "\n\t") . "\n";
  299. }
  300. return $header_string;
  301. }
  302. }