PageRenderTime 26ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/administrator/components/com_jce/controller/profiles.php

https://bitbucket.org/pastor399/newcastleunifc
PHP | 468 lines | 325 code | 87 blank | 56 comment | 48 complexity | 063a981c2a32ac84b742e4f8762b36fd MD5 | raw file
  1. <?php
  2. /**
  3. * @package JCE
  4. * @copyright Copyright (c) 2009-2013 Ryan Demmer. All rights reserved.
  5. * @license GNU/GPL 2 or later - http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  6. * JCE is free software. This version may have been modified pursuant
  7. * to the GNU General Public License, and as distributed it includes or
  8. * is derivative of works licensed under the GNU General Public License or
  9. * other free or open source software licenses.
  10. */
  11. defined('_JEXEC') or die('RESTRICTED');
  12. class WFControllerProfiles extends WFController {
  13. /**
  14. * Custom Constructor
  15. */
  16. public function __construct($default = array()) {
  17. parent::__construct();
  18. $this->registerTask('apply', 'save');
  19. $this->registerTask('unpublish', 'publish');
  20. $this->registerTask('enable', 'publish');
  21. $this->registerTask('disable', 'publish');
  22. $this->registerTask('orderup', 'order');
  23. $this->registerTask('orderdown', 'order');
  24. }
  25. public function remove() {
  26. // Check for request forgeries
  27. JRequest::checkToken() or die('RESTRICTED');
  28. $db = JFactory::getDBO();
  29. $user = JFactory::getUser();
  30. $cid = JRequest::getVar('cid', array(0), 'post', 'array');
  31. JArrayHelper::toInteger($cid, array(0));
  32. if (count($cid) < 1) {
  33. JError::raiseError(500, WFText::_('WF_PROFILES_SELECT_ERROR'));
  34. }
  35. $cids = implode(',', $cid);
  36. $query = 'DELETE FROM #__wf_profiles'
  37. . ' WHERE id IN ( ' . $cids . ' )'
  38. ;
  39. $db->setQuery($query);
  40. if (!$db->query()) {
  41. JError::raiseError(500, $db->getErrorMsg());
  42. }
  43. $msg = JText::sprintf('WF_PROFILES_DELETED', count($cid));
  44. $this->setRedirect('index.php?option=com_jce&view=profiles', $msg);
  45. }
  46. public function copy() {
  47. // Check for request forgeries
  48. JRequest::checkToken() or die('RESTRICTED');
  49. $db = JFactory::getDBO();
  50. $user = JFactory::getUser();
  51. $cid = JRequest::getVar('cid', array(0), 'post', 'array');
  52. JArrayHelper::toInteger($cid, array(0));
  53. $n = count($cid);
  54. if ($n == 0) {
  55. return JError::raiseWarning(500, WFText::_('WF_PROFILES_SELECT_ERROR'));
  56. }
  57. $row = JTable::getInstance('profiles', 'WFTable');
  58. foreach ($cid as $id) {
  59. // load the row from the db table
  60. $row->load((int) $id);
  61. $row->name = JText::sprintf('WF_PROFILES_COPY_OF', $row->name);
  62. $row->id = 0;
  63. $row->published = 0;
  64. if (!$row->check()) {
  65. return JError::raiseWarning(500, $row->getError());
  66. }
  67. if (!$row->store()) {
  68. return JError::raiseWarning(500, $row->getError());
  69. }
  70. $row->checkin();
  71. $row->reorder('ordering=' . $db->Quote($row->ordering));
  72. }
  73. $msg = JText::sprintf('WF_PROFILES_COPIED', $n);
  74. $this->setRedirect('index.php?option=com_jce&view=profiles', $msg);
  75. }
  76. public function save() {
  77. // Check for request forgeries
  78. JRequest::checkToken() or die('RESTRICTED');
  79. $db = JFactory::getDBO();
  80. $filter = JFilterInput::getInstance();
  81. $row = JTable::getInstance('profiles', 'WFTable');
  82. $task = $this->getTask();
  83. $result = array('error' => false);
  84. if (!$row->bind(JRequest::get('post'))) {
  85. JError::raiseError(500, $db->getErrorMsg());
  86. }
  87. // add types from usergroups
  88. $row->types = JRequest::getVar('usergroups', array(), 'post', 'array');
  89. foreach (get_object_vars($row) as $key => $value) {
  90. switch ($key) {
  91. case 'name':
  92. case 'description':
  93. $value = $filter->clean($value);
  94. break;
  95. case 'components':
  96. case 'device':
  97. $value = implode(',', $this->cleanInput($value));
  98. break;
  99. case 'types':
  100. case 'users':
  101. $value = implode(',', $this->cleanInput($value, 'int'));
  102. break;
  103. case 'area':
  104. if (empty($value) || count($value) == 2) {
  105. $value = 0;
  106. } else {
  107. $value = $value[0];
  108. }
  109. break;
  110. case 'plugins':
  111. $value = preg_replace('#[^\w,]+#', '', $value);
  112. break;
  113. case 'rows':
  114. $value = preg_replace('#[^\w,;]+#', '', $value);
  115. break;
  116. case 'params':
  117. $json = array();
  118. // suhosin - params submitted as string
  119. if (is_string($value)) {
  120. $value = trim($value);
  121. // base64 decode
  122. //$value = base64_decode($value);
  123. parse_str(rawurldecode($value), $json);
  124. } else {
  125. if (array_key_exists('editor', $value)) {
  126. $json['editor'] = $value['editor'];
  127. }
  128. // get plugins
  129. $plugins = explode(',', $row->plugins);
  130. foreach ($plugins as $plugin) {
  131. // add plugin params to array
  132. if (array_key_exists($plugin, $value)) {
  133. $json[$plugin] = $value[$plugin];
  134. }
  135. }
  136. }
  137. // clean data
  138. $json = $this->cleanInput($json);
  139. // encode as json string
  140. $value = json_encode($json);
  141. break;
  142. case 'params-string':
  143. $value = trim($value);
  144. parse_str(rawurldecode($value), $json);
  145. $key = 'params';
  146. $value = json_encode($json);
  147. break;
  148. }
  149. $row->$key = $value;
  150. }
  151. if (!$row->check()) {
  152. JError::raiseError(500, $db->getErrorMsg());
  153. }
  154. if (!$row->store()) {
  155. JError::raiseError(500, $db->getErrorMsg());
  156. }
  157. $row->checkin();
  158. switch ($task) {
  159. case 'apply':
  160. $msg = JText::sprintf('WF_PROFILES_SAVED_CHANGES', $row->name);
  161. $this->setRedirect('index.php?option=com_jce&view=profiles&task=edit&cid[]=' . $row->id, $msg);
  162. break;
  163. case 'save':
  164. default:
  165. $msg = JText::sprintf('WF_PROFILES_SAVED', $row->name);
  166. $this->setRedirect('index.php?option=com_jce&view=profiles', $msg);
  167. break;
  168. }
  169. }
  170. /**
  171. * Generic publish method
  172. * @return
  173. */
  174. public function publish() {
  175. // Check for request forgeries
  176. JRequest::checkToken() or die('Invalid Token');
  177. $db = JFactory::getDBO();
  178. $user = JFactory::getUser();
  179. $cid = JRequest::getVar('cid', array(0), 'post', 'array');
  180. JArrayHelper::toInteger($cid, array(0));
  181. switch ($this->getTask()) {
  182. case 'publish':
  183. case 'enable':
  184. $publish = 1;
  185. break;
  186. case 'unpublish':
  187. case 'disable':
  188. $publish = 0;
  189. break;
  190. }
  191. $view = JRequest::getCmd('view');
  192. if (count($cid) < 1) {
  193. $action = $publish ? WFText::_('WF_LABEL_PUBLISH') : WFText::_('WF_LABEL_UNPUBLISH');
  194. JError::raiseError(500, JText::sprintf('WF_PROFILES_VIEW_SELECT', $view, $action));
  195. }
  196. $cids = implode(',', $cid);
  197. $query = 'UPDATE #__wf_profiles SET published = ' . (int) $publish
  198. . ' WHERE id IN ( ' . $cids . ' )'
  199. . ' AND ( checked_out = 0 OR ( checked_out = ' . (int) $user->get('id') . ' ))'
  200. ;
  201. $db->setQuery($query);
  202. if (!$db->query()) {
  203. JError::raiseError(500, $db->getErrorMsg());
  204. }
  205. if (count($cid) == 1) {
  206. $row = JTable::getInstance('profiles', 'WFTable');
  207. $row->checkin($cid[0]);
  208. }
  209. $this->setRedirect('index.php?option=com_jce&view=profiles');
  210. }
  211. public function order() {
  212. // Check for request forgeries
  213. JRequest::checkToken() or jexit('Invalid Token');
  214. $db = JFactory::getDBO();
  215. $cid = JRequest::getVar('cid', array(0), 'post', 'array');
  216. JArrayHelper::toInteger($cid, array(0));
  217. $uid = $cid[0];
  218. $inc = ( $this->getTask() == 'orderup' ? -1 : 1 );
  219. $row = JTable::getInstance('profiles', 'WFTable');
  220. $row->load($uid);
  221. $row->move($inc);
  222. $this->setRedirect('index.php?option=com_jce&view=profiles');
  223. }
  224. public function saveorder() {
  225. // Check for request forgeries
  226. JRequest::checkToken() or jexit('RESTRICTED');
  227. $cid = JRequest::getVar('cid', array(0), 'post', 'array');
  228. $order = JRequest::getVar('order', array(0), 'post', 'array');
  229. if (!empty($cid)) {
  230. $model = $this->getModel('profiles', 'WFModel');
  231. $result = $model->saveOrder($cid, $order);
  232. }
  233. // ajax request
  234. if (JRequest::getWord('tmpl') === 'component') {
  235. echo (int) $result;
  236. JFactory::getApplication()->close();
  237. }
  238. $msg = WFText::_('WF_PROFILES_ORDERING_SAVED');
  239. $this->setRedirect('index.php?option=com_jce&view=profiles', $msg);
  240. }
  241. public function cancelEdit() {
  242. // Check for request forgeries
  243. JRequest::checkToken() or die('RESTRICTED');
  244. $view = JRequest::getCmd('view');
  245. $db = JFactory::getDBO();
  246. $row = JTable::getInstance($view, 'WFTable');
  247. $row->bind(JRequest::get('post'));
  248. $row->checkin();
  249. $this->setRedirect(JRoute::_('index.php?option=com_jce&view=' . $view, false));
  250. }
  251. public function export() {
  252. $mainframe = JFactory::getApplication();
  253. $db = JFactory::getDBO();
  254. $tmp = $mainframe->getCfg('tmp_path');
  255. $buffer = '<?xml version="1.0" encoding="utf-8" standalone="yes"?>';
  256. $buffer .= "\n" . '<export type="profiles">';
  257. $buffer .= "\n\t" . '<profiles>';
  258. $cid = JRequest::getVar('cid', array(0), 'post', 'array');
  259. JArrayHelper::toInteger($cid, array(0));
  260. if (count($cid) < 1) {
  261. JError::raiseError(500, WFText::_('WF_PROFILES_SELECT_ERROR'));
  262. }
  263. $cids = implode(',', $cid);
  264. // get froup data
  265. $query = 'SELECT * FROM #__wf_profiles'
  266. . ' WHERE id IN (' . $cids . ')'
  267. ;
  268. $db->setQuery($query);
  269. $profiles = $db->loadObjectList();
  270. foreach ($profiles as $profile) {
  271. // remove some stuff
  272. unset($profile->id);
  273. unset($profile->checked_out);
  274. unset($profile->checked_out_time);
  275. // set published to 0
  276. $profile->published = 0;
  277. $buffer .= "\n\t\t";
  278. $buffer .= '<profile>';
  279. foreach ($profile as $key => $value) {
  280. if ($key == 'params') {
  281. $buffer .= "\n\t\t\t" . '<' . $key . '>';
  282. if ($value) {
  283. $params = explode("\n", $value);
  284. foreach ($params as $param) {
  285. if ($param !== '') {
  286. $buffer .= "\n\t\t\t\t" . '<param>' . $param . '</param>';
  287. }
  288. }
  289. $buffer .= "\n\t\t\t\t";
  290. }
  291. $buffer .= '</' . $key . '>';
  292. } else {
  293. $buffer .= "\n\t\t\t" . '<' . $key . '>' . $this->encodeData($value) . '</' . $key . '>';
  294. }
  295. }
  296. $buffer .= "\n\t\t</profile>";
  297. }
  298. $buffer .= "\n\t</profiles>";
  299. $buffer .= "\n</export>";
  300. // set_time_limit doesn't work in safe mode
  301. if (!ini_get('safe_mode')) {
  302. @set_time_limit(0);
  303. }
  304. $name = 'jce_profile_' . date('Y_m_d') . '.xml';
  305. header("Pragma: public");
  306. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  307. header("Expires: 0");
  308. header("Content-Transfer-Encoding: binary");
  309. header("Content-Type: text/xml");
  310. header('Content-Disposition: attachment;'
  311. . ' filename="' . $name . '";'
  312. );
  313. echo $buffer;
  314. exit();
  315. }
  316. /**
  317. * Process XML restore file
  318. * @param object $xml
  319. * @return boolean
  320. */
  321. public function import() {
  322. // Check for request forgeries
  323. JRequest::checkToken() or die('RESTRICTED');
  324. $app = JFactory::getApplication();
  325. $file = JRequest::getVar('import', '', 'files', 'array');
  326. $input = JRequest::getVar('import_input');
  327. $tmp = $app->getCfg('tmp_path');
  328. $model = $this->getModel('profiles', 'WFModel');
  329. $filter = JFilterInput::getInstance();
  330. jimport('joomla.filesystem.file');
  331. if (!is_array($file)) {
  332. $app->enqueueMessage(WFText::_('WF_PROFILES_UPLOAD_NOFILE'), 'error');
  333. } else {
  334. // check for valid uploaded file
  335. if (is_uploaded_file($file['tmp_name']) && $file['name']) {
  336. // create destination path
  337. $destination = $tmp . '/' . $file['name'];
  338. if (JFile::upload($file['tmp_name'], $destination)) {
  339. // check it exists, was uploaded properly
  340. if (JFile::exists($destination)) {
  341. // process import
  342. $model->processImport($destination);
  343. } else {
  344. $app->enqueueMessage(WFText::_('WF_PROFILES_UPLOAD_FAILED'), 'error');
  345. }
  346. } else {
  347. $app->enqueueMessage(WFText::_('WF_PROFILES_UPLOAD_FAILED'), 'error');
  348. }
  349. } else {
  350. // clean input
  351. $input = $filter->clean($input, 'path');
  352. // check for file input value instead
  353. if ($input) {
  354. // check file exists
  355. if (JFile::exists($input)) {
  356. // process import
  357. $model->processImport($input);
  358. } else {
  359. $app->enqueueMessage(WFText::_('WF_PROFILES_IMPORT_NOFILE'), 'error');
  360. }
  361. } else {
  362. $app->enqueueMessage(WFText::_('WF_PROFILES_UPLOAD_FAILED'), 'error');
  363. }
  364. }
  365. }
  366. $this->setRedirect('index.php?option=com_jce&view=profiles');
  367. }
  368. /**
  369. * CDATA encode a parameter if it contains & < > characters, eg: <![CDATA[index.php?option=com_content&view=article&id=1]]>
  370. * @param object $param
  371. * @return CDATA encoded parameter or parameter
  372. */
  373. private function encodeData($data) {
  374. if (preg_match('/[<>&]/', $data)) {
  375. $data = '<![CDATA[' . $data . ']]>';
  376. }
  377. $data = preg_replace('/"/', '\"', $data);
  378. return $data;
  379. }
  380. }
  381. ?>