/administrator/components/com_zoo/controllers/configuration.php

https://gitlab.com/vnsoftdev/amms · PHP · 471 lines · 289 code · 125 blank · 57 comment · 29 complexity · cc76e05e20fa4e43a696ab4e7510ab4d MD5 · raw file

  1. <?php
  2. /**
  3. * @package com_zoo
  4. * @author YOOtheme http://www.yootheme.com
  5. * @copyright Copyright (C) YOOtheme GmbH
  6. * @license http://www.gnu.org/licenses/gpl.html GNU/GPL
  7. */
  8. /*
  9. Class: ConfigurationController
  10. The controller class for application configuration
  11. */
  12. class ConfigurationController extends AppController {
  13. public $application;
  14. public function __construct($default = array()) {
  15. parent::__construct($default);
  16. // set table
  17. $this->table = $this->app->table->application;
  18. // get application
  19. $this->application = $this->app->zoo->getApplication();
  20. // check ACL
  21. if (!$this->application->isAdmin()) {
  22. throw new ConfigurationControllerException("Invalid Access Permissions!", 1);
  23. }
  24. // set base url
  25. $this->baseurl = $this->app->link(array('controller' => $this->controller), false);
  26. // register tasks
  27. $this->registerTask('applyassignelements', 'saveassignelements');
  28. $this->registerTask('apply', 'save');
  29. }
  30. public function display($cachable = false, $urlparams = false) {
  31. // set toolbar items
  32. $this->app->system->application->JComponentTitle = $this->application->getToolbarTitle(JText::_('Config'));
  33. $this->app->toolbar->apply();
  34. $this->app->zoo->toolbarHelp();
  35. // get params
  36. $this->params = $this->application->getParams();
  37. // template select
  38. $options = array($this->app->html->_('select.option', '', '- '.JText::_('Select Template').' -'));
  39. foreach ($this->application->getTemplates() as $template) {
  40. $options[] = $this->app->html->_('select.option', $template->name, $template->getMetaData('name'));
  41. }
  42. $this->lists['select_template'] = $this->app->html->_('select.genericlist', $options, 'template', '', 'value', 'text', $this->params->get('template'));
  43. // get permission form
  44. $xml = simplexml_load_file(JPATH_COMPONENT . '/models/forms/permissions.xml');
  45. $this->permissions = JForm::getInstance('com_zoo.new', $xml->asXML());
  46. $this->permissions->bind(array('asset_id' => $this->application->asset_id));
  47. $this->assetPermissions = array();
  48. $asset = JTable::getInstance('Asset');
  49. foreach ($this->application->getTypes() as $typeName => $type) {
  50. $xml->fieldset->field->attributes()->section = 'type';
  51. $xml->fieldset->field->attributes()->name = 'rules_' . $typeName;
  52. $this->assetPermissions[$typeName] = JForm::getInstance('com_zoo.new.' . $typeName, $xml->asXML());
  53. if ($asset->loadByName($type->getAssetName())) {
  54. $assetName = $type->getAssetName();
  55. } else {
  56. $assetName = $this->application->asset_id;
  57. }
  58. $this->assetPermissions[$typeName]->bind(array('asset_id' => $assetName));
  59. }
  60. // manipulate js in J25
  61. if ($this->app->joomla->isVersion('2.5')) {
  62. JDispatcher::getInstance()->attach(array('event' => 'onAfterDispatch', 'handler' => array($this, 'eventCallback')));
  63. }
  64. // display view
  65. $this->getView()->setLayout('application')->display();
  66. }
  67. public function eventCallback() {
  68. $script = $this->app->system->document->_script['text/javascript'];
  69. $types = array_keys($this->application->getTypes());
  70. $types[]= 'application';
  71. $i = 3;
  72. $script = preg_replace_callback('/div#permissions-sliders\.pane-sliders/', function ($match) use (&$i, $types) {
  73. return 'div .zoo-'.$types[(int) ($i++ / 3) - 1].'-permissions';
  74. }, $script);
  75. $this->app->system->document->_script['text/javascript'] = $script;
  76. }
  77. public function save() {
  78. // check for request forgeries
  79. $this->app->session->checkToken() or jexit('Invalid Token');
  80. // init vars
  81. $post = $this->app->request->get('post:', 'array');
  82. try {
  83. // bind post
  84. self::bind($this->application, $post, array('params'));
  85. // set params
  86. $params = $this->application
  87. ->getParams()
  88. ->remove('global.')
  89. ->set('template', @$post['template'])
  90. ->set('global.config.', @$post['params']['config'])
  91. ->set('global.template.', @$post['params']['template']);
  92. if (isset($post['addons']) && is_array($post['addons'])) {
  93. foreach ($post['addons'] as $addon => $value) {
  94. $params->set("global.$addon.", $value);
  95. }
  96. }
  97. // add ACL rules to aplication object
  98. $this->application->rules = $post['rules'];
  99. foreach ($post as $key => $value) {
  100. if (stripos($key, 'rules_') === 0) {
  101. $this->application->assetRules[substr($key, 6)] = $value;
  102. }
  103. }
  104. // save application
  105. $this->table->save($this->application);
  106. // set redirect message
  107. $msg = JText::_('Application Saved');
  108. } catch (AppException $e) {
  109. // raise notice on exception
  110. $this->app->error->raiseNotice(0, JText::_('Error Saving Application').' ('.$e.')');
  111. $msg = null;
  112. }
  113. $this->setRedirect($this->baseurl, $msg);
  114. }
  115. public function getApplicationParams() {
  116. // init vars
  117. $template = $this->app->request->getCmd('template');
  118. // get params
  119. $this->params = $this->application->getParams();
  120. // get permission form
  121. $xml = simplexml_load_file(JPATH_COMPONENT . '/models/forms/permissions.xml');
  122. $this->permissions = JForm::getInstance('com_zoo.new', $xml->asXML());
  123. $this->permissions->bind(array('asset_id' => $this->application->asset_id));
  124. $this->assetPermissions = array();
  125. $asset = JTable::getInstance('Asset');
  126. foreach ($this->application->getTypes() as $typeName => $type) {
  127. $xml->fieldset->field->attributes()->section = 'type';
  128. $xml->fieldset->field->attributes()->name = 'rules_' . $typeName;
  129. $this->assetPermissions[$typeName] = JForm::getInstance('com_zoo.new.' . $typeName, $xml->asXML());
  130. if ($asset->loadByName($type->getAssetName())) {
  131. $assetName = $type->getAssetName();
  132. } else {
  133. $assetName = $this->application->asset_id;
  134. }
  135. $this->assetPermissions[$typeName]->bind(array('asset_id' => $assetName));
  136. }
  137. // set template
  138. $this->params->set('template', $template);
  139. // display view
  140. $this->getView()->setLayout('_applicationparams')->display();
  141. }
  142. public function importExport() {
  143. // set toolbar items
  144. $this->app->system->application->JComponentTitle = $this->application->getToolbarTitle(JText::_('Import / Export'));
  145. $this->app->zoo->toolbarHelp();
  146. $this->exporter = $this->app->export->getExporters('Zoo v2');
  147. // display view
  148. $this->getView()->setLayout('importexport')->display();
  149. }
  150. public function importFrom() {
  151. // check for request forgeries
  152. $this->app->session->checkToken() or jexit('Invalid Token');
  153. $exporter = $this->app->request->getString('exporter');
  154. try {
  155. $xml = $this->app->export->create($exporter)->export();
  156. $file = rtrim($this->app->system->config->get('tmp_path'), '\/') . '/' . $this->app->utility->generateUUID() . '.tmp';
  157. if (JFile::exists($file)) {
  158. JFile::delete($file);
  159. }
  160. JFile::write($file, $xml);
  161. } catch (Exception $e) {
  162. // raise error on exception
  163. $this->app->error->raiseNotice(0, JText::_('Error During Export').' ('.$e.')');
  164. $this->setRedirect($this->baseurl.'&task=importexport');
  165. return;
  166. }
  167. $this->_import($file);
  168. }
  169. public function import() {
  170. // check for request forgeries
  171. $this->app->session->checkToken() or jexit('Invalid Token');
  172. $userfile = null;
  173. $jsonfile = $this->app->request->getVar('import-json', array(), 'files', 'array');
  174. try {
  175. // validate
  176. $validator = $this->app->validator->create('file', array('extensions' => array('json')));
  177. $userfile = $validator->clean($jsonfile);
  178. $type = 'json';
  179. } catch (AppValidatorException $e) {}
  180. $csvfile = $this->app->request->getVar('import-csv', array(), 'files', 'array');
  181. try {
  182. // validate
  183. $validator = $this->app->validator->create('file', array('extensions' => array('csv')));
  184. $userfile = $validator->clean($csvfile);
  185. $type = 'csv';
  186. } catch (AppValidatorException $e) {}
  187. if (!empty($userfile)) {
  188. $file = rtrim($this->app->system->config->get('tmp_path'), '\/') . '/' . basename($userfile['tmp_name']);
  189. if (JFile::upload($userfile['tmp_name'], $file)) {
  190. $this->_import($file, $type);
  191. } else {
  192. // raise error on exception
  193. $this->app->error->raiseNotice(0, JText::_('Error Importing (Unable to upload file.)'));
  194. $this->setRedirect($this->baseurl.'&task=importexport');
  195. return;
  196. }
  197. } else {
  198. // raise error on exception
  199. $this->app->error->raiseNotice(0, JText::_('Error Importing (Unable to upload file.)'));
  200. $this->setRedirect($this->baseurl.'&task=importexport');
  201. return;
  202. }
  203. }
  204. public function importCSV() {
  205. $file = $this->app->request->getCmd('file', '');
  206. $file = rtrim($this->app->system->config->get('tmp_path'), '\/') . '/' . $file;
  207. $this->_import($file, 'importcsv');
  208. }
  209. protected function _import($file, $type = 'json') {
  210. // disable menu
  211. $this->app->request->setVar('hidemainmenu', 1);
  212. // set toolbar items
  213. $this->app->system->application->JComponentTitle = $this->application->getToolbarTitle(JText::_('Import').': '.$this->application->name);
  214. $this->app->toolbar->cancel('importexport', 'Cancel');
  215. $this->app->zoo->toolbarHelp();
  216. // set_time_limit doesn't work in safe mode
  217. if (!ini_get('safe_mode')) {
  218. @set_time_limit(0);
  219. }
  220. $layout = '';
  221. switch ($type) {
  222. case 'xml':
  223. $this->app->error->raiseWarning(0, 'XML import is not supported since ZOO 2.5!');
  224. $this->importExport();
  225. break;
  226. case 'json':
  227. if (JFile::exists($file) && $data = $this->app->data->create(file_get_contents($file))) {
  228. $this->info = $this->app->import->getImportInfo($data);
  229. $this->file = basename($file);
  230. } else {
  231. // raise error on exception
  232. $this->app->error->raiseNotice(0, JText::_('Error Importing (Not a valid JSON file)'));
  233. $this->setRedirect($this->baseurl.'&task=importexport');
  234. return;
  235. }
  236. $layout = 'importjson';
  237. break;
  238. case 'csv':
  239. $this->file = basename($file);
  240. $layout = 'configcsv';
  241. break;
  242. case 'importcsv':
  243. $this->contains_headers = $this->app->request->getBool('contains-headers', false);
  244. $this->field_separator = $this->app->request->getString('field-separator', ',');
  245. $this->field_separator = empty($this->field_separator) ? ',' : substr($this->field_separator, 0, 1);
  246. $this->field_enclosure = $this->app->request->getString('field-enclosure', '"');
  247. $this->field_enclosure = empty($this->field_enclosure) ? '"' : substr($this->field_enclosure, 0, 1);
  248. $this->info = $this->app->import->getImportInfoCSV($file, $this->contains_headers, $this->field_separator, $this->field_enclosure);
  249. $this->file = basename($file);
  250. $layout = 'importcsv';
  251. break;
  252. }
  253. // display view
  254. $this->getView()->setLayout($layout)->display();
  255. }
  256. public function doImport() {
  257. // init vars
  258. $import_frontpage = $this->app->request->getBool('import-frontpage', false);
  259. $import_categories = $this->app->request->getBool('import-categories', false);
  260. $element_assignment = $this->app->request->get('element-assign', 'array', array());
  261. $types = $this->app->request->get('types', 'array', array());
  262. $file = $this->app->request->getCmd('file', '');
  263. $file = rtrim($this->app->system->config->get('tmp_path'), '\/') . '/' . $file;
  264. if (JFile::exists($file)) {
  265. $this->app->import->import($file, $import_frontpage, $import_categories, $element_assignment, $types);
  266. }
  267. $this->setRedirect($this->baseurl.'&task=importexport', JText::_('Import successfull'));
  268. }
  269. public function doImportCSV() {
  270. // init vars
  271. $contains_headers = $this->app->request->getBool('contains-headers', false);
  272. $field_separator = $this->app->request->getString('field-separator', ',');
  273. $field_enclosure = $this->app->request->getString('field-enclosure', '"');
  274. $element_assignment = $this->app->request->get('element-assign', 'array', array());
  275. $type = $this->app->request->getCmd('type', '');
  276. $file = $this->app->request->getCmd('file', '');
  277. $file = rtrim($this->app->system->config->get('tmp_path'), '\/') . '/' . $file;
  278. if (JFile::exists($file)) {
  279. $this->app->import->importCSV($file, $type, $contains_headers, $field_separator, $field_enclosure, $element_assignment);
  280. }
  281. $this->setRedirect($this->baseurl.'&task=importexport', JText::_('Import successfull'));
  282. }
  283. public function doExport() {
  284. $exporter = $this->app->request->getCmd('exporter');
  285. if ($exporter) {
  286. try {
  287. // set_time_limit doesn't work in safe mode
  288. if (!ini_get('safe_mode')) {
  289. @set_time_limit(0);
  290. }
  291. $json = $this->app->export->create($exporter)->export();
  292. header("Pragma: public");
  293. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  294. header("Expires: 0");
  295. header("Content-Transfer-Encoding: binary");
  296. header ("Content-Type: application/json");
  297. header('Content-Disposition: attachment;'
  298. .' filename="'.JFilterOutput::stringURLSafe($this->application->name).'.json";'
  299. );
  300. echo $json;
  301. } catch (AppExporterException $e) {
  302. // raise error on exception
  303. $this->app->error->raiseNotice(0, JText::_('Error Exporting').' ('.$e.')');
  304. $this->setRedirect($this->baseurl.'&task=importexport');
  305. return;
  306. }
  307. }
  308. }
  309. public function doExportCSV() {
  310. //init vars
  311. $files = array();
  312. try {
  313. foreach ($this->application->getTypes() as $type) {
  314. if ($file = $this->app->export->toCSV($type)) {
  315. $files[] = $file;
  316. }
  317. }
  318. if (empty($files)) {
  319. throw new AppException(JText::sprintf('There are no items to export'));
  320. }
  321. $filepath = $this->app->path->path("tmp:").'/'.$this->application->getGroup().'.zip';
  322. $zip = $this->app->archive->open($filepath, 'zip');
  323. $zip->create($files, PCLZIP_OPT_REMOVE_ALL_PATH);
  324. if (is_readable($filepath) && JFile::exists($filepath)) {
  325. $this->app->filesystem->output($filepath);
  326. $files[] = $filepath;
  327. foreach ($files as $file) {
  328. if (JFile::exists($file)) {
  329. JFile::delete($file);
  330. }
  331. }
  332. } else {
  333. throw new AppException(JText::sprintf('Unable to create file %s', $filepath));
  334. }
  335. } catch (AppException $e) {
  336. // raise error on exception
  337. $this->app->error->raiseNotice(0, JText::_('Error Exporting').' ('.$e.')');
  338. $this->setRedirect($this->baseurl.'&task=importexport');
  339. return;
  340. }
  341. }
  342. }
  343. /*
  344. Class: ConfigurationControllerException
  345. */
  346. class ConfigurationControllerException extends AppException {}