PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/core/Services/Edit/Controller.php

https://gitlab.com/ElvisAns/tiki
PHP | 309 lines | 248 code | 43 blank | 18 comment | 42 complexity | dde971c06b3930ae6941cc950b774e44 MD5 | raw file
  1. <?php
  2. // (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
  3. //
  4. // All Rights Reserved. See copyright.txt for details and a complete list of authors.
  5. // Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
  6. // $Id$
  7. /**
  8. * Class Services_Edit_Controller
  9. *
  10. * Controller for various editing based services, wiki/html conversion, preview, inline editing etc
  11. *
  12. */
  13. class Services_Edit_Controller
  14. {
  15. public function setUp()
  16. {
  17. Services_Exception_Disabled::check('feature_wiki');
  18. }
  19. /**
  20. * Returns the section for use with certain features like banning
  21. * @return string
  22. */
  23. public function getSection()
  24. {
  25. return 'wiki page';
  26. }
  27. public function action_towiki($input)
  28. {
  29. $res = TikiLib::lib('edit')->parseToWiki($input->data->none());
  30. return [
  31. 'data' => $res,
  32. ];
  33. }
  34. public function action_tohtml($input)
  35. {
  36. $res = TikiLib::lib('edit')->parseToWysiwyg($input->data->none(), false, $input->allowhtml->int() ? true : false);
  37. return [
  38. 'data' => $res,
  39. ];
  40. }
  41. public function action_inlinesave($input)
  42. {
  43. global $user;
  44. Services_Exception_Disabled::check('wysiwyg_inline_editing');
  45. if ($_SERVER['REQUEST_METHOD'] === 'POST') {
  46. $pageName = $input->page->text();
  47. $info = TikiLib::lib('tiki')->get_page_info($pageName);
  48. $data = $input->data->none();
  49. // Check if HTML format is allowed
  50. if ($info['is_html']) {
  51. // Save as HTML
  52. $edit_data = TikiLib::lib('edit')->partialParseWysiwygToWiki($data);
  53. $is_html = '1';
  54. } else {
  55. // Convert HTML to wiki and save as wiki
  56. $edit_data = TikiLib::lib('edit')->parseToWiki($data);
  57. $is_html = null;
  58. }
  59. $edit_comment = tra('Inline editor update');
  60. $res = TikiLib::lib('tiki')->update_page($pageName, $edit_data, $edit_comment, $user, $_SERVER['REMOTE_ADDR']);
  61. return [
  62. 'data' => $res,
  63. ];
  64. }
  65. }
  66. public function action_preview($input)
  67. {
  68. Services_Exception_Disabled::check('feature_warn_on_edit');
  69. global $user, $prefs, $page;
  70. $tikilib = TikiLib::lib('tiki');
  71. $autoSaveIdParts = explode(':', $input->autoSaveId->text()); // user, section, object id
  72. foreach ($autoSaveIdParts as & $part) {
  73. $part = urldecode($part);
  74. }
  75. $page = $autoSaveIdParts[2]; // plugins use global $page for approval
  76. if (! Perms::get('wiki page', $page)->edit || $user != TikiLib::lib('service')->internal('semaphore', 'get_user', ['object_id' => $page, 'check' => 1])) {
  77. return '';
  78. }
  79. $info = $tikilib->get_page_info($page, false);
  80. if (empty($info)) {
  81. $info = [ // new page
  82. 'data' => '',
  83. ];
  84. }
  85. $info['is_html'] = $input->allowHtml->int();
  86. if (! isset($info['wysiwyg']) && isset($_SESSION['wysiwyg'])) {
  87. $info['wysiwyg'] = $_SESSION['wysiwyg'];
  88. }
  89. $options = [
  90. 'is_html' => $info['is_html'],
  91. 'preview_mode' => true,
  92. 'process_wiki_paragraphs' => ($prefs['wysiwyg_htmltowiki'] === 'y' || $info['wysiwyg'] == 'n'),
  93. 'page' => $page,
  94. ];
  95. if (count($autoSaveIdParts) === 3 && ! empty($user) && $user === $autoSaveIdParts[0] && $autoSaveIdParts[1] === 'wiki_page') {
  96. $editlib = TikiLib::lib('edit');
  97. $smarty = TikiLib::lib('smarty');
  98. $wikilib = TikiLib::lib('wiki');
  99. $smarty->assign('inPage', $input->inPage->int() ? true : false);
  100. $parserlib = TikiLib::lib('parser');
  101. if ($input->inPage->int()) {
  102. $diffstyle = $input->diff_style->text();
  103. if (! $diffstyle) { // use previously set diff_style
  104. $diffstyle = getCookie('preview_diff_style', 'preview', '');
  105. }
  106. $data = $editlib->partialParseWysiwygToWiki(
  107. TikiLib::lib('autosave')->get_autosave($input->editor_id->text(), $input->autoSaveId->text())
  108. );
  109. $data = $tikilib->convertAbsoluteLinksToRelative($data);
  110. TikiLib::lib('smarty')->assign('diff_style', $diffstyle);
  111. if ($diffstyle) {
  112. if (! empty($info['created'])) {
  113. $info = $tikilib->get_page_info($page); // get page with data this time
  114. }
  115. if ($input->hdr->int()) { // TODO refactor with code in editpage
  116. if ($input->hdr->int() === 0) {
  117. list($real_start, $real_len) = $tikilib->get_wiki_section($info['data'], 1);
  118. $real_len = $real_start;
  119. $real_start = 0;
  120. } else {
  121. list($real_start, $real_len) = $tikilib->get_wiki_section($info['data'], $input->hdr->int());
  122. }
  123. $info['data'] = substr($info['data'], $real_start, $real_len);
  124. }
  125. require_once('lib/diff/difflib.php');
  126. if ($info['is_html'] == 1) {
  127. $diffold = $tikilib->htmldecode($info['data']);
  128. } else {
  129. $diffold = $info['data'];
  130. }
  131. if ($info['is_html']) {
  132. $diffnew = $tikilib->htmldecode($data);
  133. } else {
  134. $diffnew = $data;
  135. }
  136. if ($diffstyle === 'htmldiff') {
  137. $diffnew = $parserlib->parse_data($diffnew, $options);
  138. $diffold = $parserlib->parse_data($diffold, $options);
  139. }
  140. $data = diff2($diffold, $diffnew, $diffstyle);
  141. $smarty->assign_by_ref('diffdata', $data);
  142. $smarty->assign('translation_mode', 'y'); // disables the headings etc
  143. $smarty->assign('show_version_info', 'n'); // disables the headings etc
  144. $data = $smarty->fetch('pagehistory.tpl');
  145. } else {
  146. $data = $parserlib->parse_data($data, $options);
  147. }
  148. $parsed = $data;
  149. } else { // popup window
  150. TikiLib::lib('header')->add_js(
  151. '
  152. function get_new_preview() {
  153. $("body").css("opacity", 0.6);
  154. location.reload(true);
  155. }
  156. $(window).on("load", function(){
  157. if (typeof opener != "undefined") {
  158. opener.ajaxPreviewWindow = this;
  159. }
  160. }).on("unload", function(){
  161. if (typeof opener.ajaxPreviewWindow != "undefined") {
  162. opener.ajaxPreviewWindow = null;
  163. }
  164. });
  165. '
  166. );
  167. $smarty->assign('headtitle', tra('Preview'));
  168. $data = '<div class="container"><div class="row row-middle"><div class="col-sm-12"><div class="wikitext">';
  169. if (TikiLib::lib('autosave')->has_autosave($input->editor_id->text(), $input->autoSaveId->text())) {
  170. $data .= $parserlib->parse_data(
  171. $editlib->partialParseWysiwygToWiki(
  172. TikiLib::lib('autosave')->get_autosave($input->editor_id->text(), $input->autoSaveId->text())
  173. ),
  174. $options
  175. );
  176. } else {
  177. if ($autoSaveIdParts[1] == 'wiki_page') {
  178. $canBeRefreshed = false;
  179. $data .= $wikilib->get_parse($autoSaveIdParts[2], $canBeRefreshed);
  180. }
  181. }
  182. $data .= '</div></div></div></div>';
  183. $smarty->assign_by_ref('mid_data', $data);
  184. $smarty->assign('mid', '');
  185. $parsed = $smarty->fetch("tiki_full.tpl");
  186. $_SERVER['HTTP_X_REQUESTED_WITH'] = 'xmlhttprequest'; // to fool Services_Broker into putputting full page
  187. }
  188. if ($prefs['feature_wiki_footnotes'] === 'y') {
  189. $footnote = $input->footnote->text();
  190. if ($footnote) {
  191. $footnote = $parserlib->parse_data($footnote);
  192. } else {
  193. $footnote = $wikilib->get_footnote($user, $page);
  194. }
  195. }
  196. return ['parsed' => $parsed, 'parsed_footnote' => $footnote];
  197. }
  198. }
  199. public function action_help($input)
  200. {
  201. global $prefs;
  202. $smarty = TikiLib::lib('smarty');
  203. $help_sections = [];
  204. if ($input->wiki->int()) {
  205. $help_sections[] = [
  206. 'id' => 'wiki-help',
  207. 'title' => tr('Syntax Help'),
  208. 'content' => $smarty->fetch('tiki-edit_help.tpl'),
  209. ];
  210. }
  211. if ($input->wysiwyg->int()) {
  212. $help_sections[] = [
  213. 'id' => 'wysiwyg-help',
  214. 'title' => tr('WYSIWYG Help'),
  215. 'content' => $smarty->fetch('tiki-edit_help_wysiwyg.tpl'),
  216. ];
  217. }
  218. if ($input->plugins->int()) {
  219. $areaId = $input->areaId->word();
  220. $wikilib = TikiLib::lib('wiki');
  221. $plugins = $wikilib->list_plugins(true, $areaId);
  222. $smarty->assign('plugins', $plugins);
  223. $help_sections[] = [
  224. 'id' => 'plugin-help',
  225. 'title' => tr('Plugin Help'),
  226. 'content' => $smarty->fetch('tiki-edit_help_plugins.tpl'),
  227. ];
  228. if ($prefs['wikiplugin_list_gui'] === 'y') {
  229. TikiLib::lib('header')
  230. ->add_jsfile('lib/jquery_tiki/pluginedit_list.js')
  231. ->add_jsfile('vendor_bundled/vendor/jquery-plugins/nestedsortable/jquery.ui.nestedSortable.js');
  232. }
  233. }
  234. if ($input->sheet->int()) {
  235. $help_sections[] = [
  236. 'id' => 'sheet-help',
  237. 'title' => tr('Spreadsheet Help'),
  238. 'content' => $smarty->fetch('tiki-edit_help_sheet.tpl'),
  239. ];
  240. }
  241. return [
  242. 'title' => tr('Edit Help'),
  243. 'help_sections' => $help_sections,
  244. ];
  245. }
  246. public function action_inline_dialog($input)
  247. {
  248. $smarty = TikiLib::lib('smarty');
  249. $smarty->loadPlugin('smarty_function_service_inline');
  250. $display = [];
  251. foreach ($input->fields as $field) {
  252. $html = smarty_function_service_inline($field->fetch->text(), $smarty->getEmptyInternalTemplate());
  253. $display[] = [
  254. 'label' => $field->label->text(),
  255. 'field' => new Tiki_Render_Editable($html, [
  256. 'layout' => 'dialog',
  257. 'object_store_url' => $field->store->text(),
  258. ]),
  259. ];
  260. }
  261. return [
  262. 'title' => tr('Edit'),
  263. 'fields' => $display,
  264. ];
  265. }
  266. }