BxDolWiki.php 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. /**
  10. * WIKI object.
  11. *
  12. * It's possble to create different WIKI object which will different URLs, Menus and permissions.
  13. * For example it's possible to create
  14. * http://example.com/wiki/somepageshere and http://example.com/docs/anotherpageshere
  15. *
  16. * @section wiki_create Creating the WIKI object:
  17. *
  18. *
  19. * Add record to 'sys_objects_wiki' table:
  20. *
  21. * - object: name of the WIKI object, this name will be user in URL as well,
  22. * for example, 'wiki' will have URLs like this: http://example.com/wiki/somepageshere
  23. * - uri: wiki module URI
  24. * - title: title of the WIKI, for example, documentation, help, tutorial
  25. * - module: module name WIKI object belongs to
  26. * - override_class_name: user defined class name
  27. * - override_class_file: the location of the user defined class, leave it empty if class is located in system folders.
  28. *
  29. * Add record to 'sys_rewrite_rules' table:
  30. * - preg - regular expression which matches some URL
  31. * - service - service method to call if abbe regular expression matches
  32. * - active - active flag
  33. *
  34. * Add record to 'sys_permalinks' table:
  35. * - standard - how link should look like when permalinks are off
  36. * - permalink - how link should look like when permalinks are on,
  37. * some special server configuration may be required to make permalink to work,
  38. * such as 'mod_rewrite' and .htaccess file for Apache
  39. * - check - option name which enables/disables permalinks
  40. * - compare_by_prefix - compare by prefix
  41. */
  42. class BxDolWiki extends BxDolFactory implements iBxDolFactoryObject
  43. {
  44. protected $_sObject;
  45. protected $_aObject;
  46. protected $_sLangCateg = 'Wiki';
  47. /**
  48. * Constructor
  49. * @param $aObject array of WIKI options
  50. */
  51. protected function __construct($aObject)
  52. {
  53. parent::__construct();
  54. $this->_sObject = $aObject['object'];
  55. $this->_aObject = $aObject;
  56. $this->_oQuery = new BxDolWikiQuery($aObject);
  57. }
  58. /**
  59. * Get WIKI object instance by object URI
  60. * @param $sObject object name
  61. * @return object instance or false on error
  62. */
  63. static public function getObjectInstanceByUri($sUri, $oTemplate = false)
  64. {
  65. $aObject = BxDolWikiQuery::getWikiObjectByUri($sUri);
  66. if (!$aObject || !is_array($aObject))
  67. return false;
  68. return self::getObjectInstance($aObject['object'], $oTemplate);
  69. }
  70. /**
  71. * Get WIKI object instance by object name
  72. * @param $sObject object name
  73. * @return object instance or false on error
  74. */
  75. static public function getObjectInstance($sObject, $oTemplate = false)
  76. {
  77. if (isset($GLOBALS['bxDolClasses']['BxDolWiki!'.$sObject]))
  78. return $GLOBALS['bxDolClasses']['BxDolWiki!'.$sObject];
  79. $aObject = BxDolWikiQuery::getWikiObject($sObject);
  80. if (!$aObject || !is_array($aObject))
  81. $aObject = BxDolWikiQuery::getWikiObject('system');
  82. $sClass = empty($aObject['override_class_name']) ? 'BxDolWiki' : $aObject['override_class_name'];
  83. if (!empty($aObject['override_class_file']))
  84. require_once(BX_DIRECTORY_PATH_ROOT . $aObject['override_class_file']);
  85. $o = new $sClass($aObject, $oTemplate);
  86. return ($GLOBALS['bxDolClasses']['BxDolWiki!'.$sObject] = $o);
  87. }
  88. static public function onModuleUninstall($sModule)
  89. {
  90. $aBlockIds = BxDolWikiQuery::getWikiBlocks ($sModule);
  91. if ($aBlockIds && is_array($aBlockIds))
  92. self::onBlockDelete($aBlockIds);
  93. }
  94. static public function onBlockDelete($mixedBlockIds)
  95. {
  96. BxDolWikiQuery::deleteAllRevisions($mixedBlockIds);
  97. $aBlockIds = is_array($mixedBlockIds) ? $mixedBlockIds : [$mixedBlockIds];
  98. $oStorage = BxDolStorage::getObjectInstance('sys_wiki_files');
  99. if ($oStorage) {
  100. foreach ($aBlockIds as $iBlockId) {
  101. $aFiles = $oStorage->getGhosts(0, $iBlockId, false, true);
  102. $oStorage->queueFiles($aFiles);
  103. }
  104. }
  105. }
  106. /**
  107. * Get object name
  108. */
  109. public function getObjectName ()
  110. {
  111. return $this->_sObject;
  112. }
  113. /**
  114. * Get wiki URi
  115. */
  116. public function getWikiUri ()
  117. {
  118. return $this->_aObject['uri'];
  119. }
  120. /**
  121. * Get WIKI block content
  122. * @param $iBlockId block ID
  123. * @param $sLang optional language name
  124. * @return block content
  125. */
  126. public function getBlockContent ($iBlockId, $sLang = false, $iRevision = false)
  127. {
  128. if (!$sLang)
  129. $sLang = bx_lang_name();
  130. $s = '';
  131. $sControls = '';
  132. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang, $iRevision);
  133. $aWikiLatest = $this->_oQuery->getBlockContent ($iBlockId, $sLang);
  134. if ($aWikiVer) {
  135. $oParsedown = new BxDolParsedown();
  136. $oParsedown->setSafeMode($aWikiVer['unsafe'] ? false : true);
  137. $s = $oParsedown->text($aWikiVer['content']);
  138. }
  139. if (!$aWikiVer && $aWikiLatest) {
  140. return _t('_sys_wiki_error_no_rev', $iRevision ? $iRevision : 0, $sLang);
  141. }
  142. if (!$aWikiVer && !$aWikiLatest && $this->isAllowed('edit')) {
  143. $s = _t('_sys_wiki_error_no_revs');
  144. }
  145. if ($aWikiVer || (!$aWikiVer && $this->isAllowed('edit'))) {
  146. $sControls = BxDolService::call('system', 'wiki_controls', array($this, $aWikiVer, $aWikiLatest, $iBlockId), 'TemplServiceWiki');
  147. }
  148. return $s . $sControls;
  149. }
  150. /**
  151. * Generate wiki contents - list of all wiki pages
  152. */
  153. public function getContents ($sAllExceptSpecified = '', $sOnlySpecified = '')
  154. {
  155. $aAllExceptSpecified = $sAllExceptSpecified ? explode(',', $sAllExceptSpecified) : array();
  156. $aOnlySpecified = $sOnlySpecified ? explode(',', $sOnlySpecified) : array();
  157. if (!($a = $this->_oQuery->getPages($aAllExceptSpecified, $aOnlySpecified)))
  158. return '';
  159. $aVars = array('bx_repeat:pages' => array());
  160. foreach ($a as $r) {
  161. $oPage = BxDolPage::getObjectInstance($r['object']);
  162. if (!$oPage->isVisiblePage())
  163. continue;
  164. $sUrl = $this->getPageUrl($r['uri']);
  165. $sTitle = _t($r['title']);
  166. $aVars['bx_repeat:pages'][] = array(
  167. 'url' => $sUrl,
  168. 'title' => $sTitle ? $sTitle : _t('_Empty'),
  169. );
  170. }
  171. usort($aVars['bx_repeat:pages'], function ($r1, $r2) {
  172. return strcmp($r1['title'], $r2['title']);
  173. });
  174. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_contents.html', $aVars);
  175. }
  176. /**
  177. * Generate list of wiki blocks with missing translations for the given language
  178. */
  179. public function getMissingTranslations ($sLang = false)
  180. {
  181. if (!$sLang)
  182. $sLang = bx_lang_name();
  183. if (!$this->isAllowed('edit') && !$this->isAllowed('translate') || !preg_match("/^[a-z]+$/", $sLang))
  184. return _t('_sys_txt_access_denied');
  185. if (!BxDolLanguages::getInstance()->getLangId($sLang))
  186. return _t('_sys_wiki_error_no_such_lang');
  187. $a = $this->_oQuery->getBlocksWithMissingTranslations($sLang);
  188. if (!$a)
  189. return _t('_sys_wiki_no_missing_translations', $sLang);
  190. $aVars = array(
  191. 'title_title' => _t('_sys_wiki_title_title'),
  192. 'title_lang' => _t('_sys_wiki_title_lang_missing'),
  193. 'title_author' => _t('_sys_wiki_title_author', _t('_sys_wiki_orig')),
  194. 'title_content' => _t('_sys_wiki_title_content', _t('_sys_wiki_orig')),
  195. 'title_time' => _t('_sys_wiki_title_time', _t('_sys_wiki_orig')),
  196. 'bx_repeat:blocks' => array()
  197. );
  198. foreach ($a as $iBlockId) {
  199. $aWikiVerMain = $this->_oQuery->getBlockContent ($iBlockId, false);
  200. $aPage = $this->_oQuery->getPageByBlockId($iBlockId);
  201. $sUrl = $this->getPageUrl($aPage['uri']) . '#bx-page-wiki-container-' . $iBlockId;
  202. $oProfile = BxDolProfile::getInstanceMagic($aWikiVerMain['profile_id']);
  203. $aVars['bx_repeat:blocks'][] = array(
  204. 'author_url' => $oProfile->getUrl(),
  205. 'author_name' => $oProfile->getDisplayName(),
  206. 'url' => $sUrl,
  207. 'lang' => $sLang,
  208. 'title' => _t($aPage['title']),
  209. 'content' => strmaxtextlen($aWikiVerMain['content']),
  210. 'timejs' => bx_time_js($aWikiVerMain['added']),
  211. );
  212. }
  213. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_translations_missing.html', $aVars);
  214. }
  215. /**
  216. * Generate list of wiki blocks with outdated translations for the given language
  217. */
  218. public function getOutdatedTranslations ($sLang = false)
  219. {
  220. if (!$sLang)
  221. $sLang = bx_lang_name();
  222. if (!$this->isAllowed('edit') && !$this->isAllowed('translate') || !preg_match("/^[a-z]+$/", $sLang))
  223. return _t('_sys_txt_access_denied');
  224. if (!BxDolLanguages::getInstance()->getLangId($sLang))
  225. return _t('_sys_wiki_error_no_such_lang');
  226. $aVars = array(
  227. 'title_title' => _t('_sys_wiki_title_title'),
  228. 'title_lang' => _t('_sys_wiki_title_lang_outated'),
  229. 'title_author' => _t('_sys_wiki_title_author', _t('_sys_wiki_orig')),
  230. 'title_content' => _t('_sys_wiki_title_content', _t('_sys_wiki_orig')),
  231. 'title_time_main' => _t('_sys_wiki_title_time', _t('_sys_wiki_orig')),
  232. 'title_time' => _t('_sys_wiki_title_time', '"'.$sLang.'"'),
  233. 'bx_repeat:blocks' => array()
  234. );
  235. $a = $this->_oQuery->getBlocksWithOutdatedTranslations($sLang);
  236. if (!$a)
  237. return _t('_sys_wiki_all_translations_up_to_date', $sLang);
  238. foreach ($a as $iBlockId) {
  239. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang, false, false);
  240. $aWikiVerMain = $this->_oQuery->getBlockContent ($iBlockId, false);
  241. $aPage = $this->_oQuery->getPageByBlockId($iBlockId);
  242. $sUrl = $this->getPageUrl($aPage['uri']) . '#bx-page-wiki-container-' . $iBlockId;
  243. $oProfile = BxDolProfile::getInstanceMagic($aWikiVerMain['profile_id']);
  244. $aVars['bx_repeat:blocks'][] = array(
  245. 'author_url' => $oProfile->getUrl(),
  246. 'author_name' => $oProfile->getDisplayName(),
  247. 'url' => $sUrl,
  248. 'lang' => $sLang,
  249. 'title' => _t($aPage['title']),
  250. 'content' => strmaxtextlen($aWikiVerMain['content']),
  251. 'timejs_main' => bx_time_js($aWikiVerMain['added']),
  252. 'timejs' => bx_time_js($aWikiVer['added']),
  253. );
  254. }
  255. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_translations_outdated.html', $aVars);
  256. }
  257. /**
  258. * Check if partucular action is allowed
  259. * @param $sType action type: add, edit, delete, translate
  260. * @param $sProfileId profile to check, if not provided then current profile is used
  261. * @return true if action is allowed, false otherwise
  262. */
  263. public function isAllowed ($sType, $iProfileId = false)
  264. {
  265. if ('convert-links' == $sType) {
  266. return isLogged();
  267. }
  268. // translate isn't allowed when only one language on the site
  269. if ('translate' == $sType) {
  270. $aLangs = BxDolLanguages::getInstance()->getLanguages(false, true);
  271. if (count($aLangs) < 2)
  272. return false;
  273. }
  274. // add page isn't implemented for the system
  275. if ('add-page' == $sType && 'system' == $this->_aObject['module'])
  276. return false;
  277. // any action is allowed for admin(operator)
  278. if (isAdmin())
  279. return true;
  280. // map 'action' to 'acl action'
  281. $aTypes = array(
  282. 'add-page' => 'add page',
  283. 'add' => 'add block',
  284. 'edit' => 'edit block',
  285. 'translate' => 'translate block',
  286. 'delete-version' => 'delete version',
  287. 'delete-block' => 'delete block',
  288. 'get-traaslation' => 'translate block',
  289. 'history' => 'history',
  290. 'unsafe' => 'unsafe',
  291. );
  292. // not listed actions aren't allowed
  293. if (!isset($aTypes[$sType]))
  294. return false;
  295. // hardcoded actions (not implemented for now)
  296. if (true === $aTypes[$sType] || false === $aTypes[$sType])
  297. return $aTypes[$sType];
  298. // all system actions starts with 'wiki '
  299. $sAction = ('system' == $this->_aObject['module'] ? 'wiki ' : '') . $aTypes[$sType];
  300. if (!$iProfileId)
  301. $iProfileId = bx_get_logged_profile_id();
  302. $aCheck = checkActionModule($iProfileId, $sAction, $this->_aObject['module']);
  303. return $aCheck[CHECK_ACTION_RESULT] === CHECK_ACTION_RESULT_ALLOWED;
  304. }
  305. public function addPage ($sPageUri, $sTitleLangKey)
  306. {
  307. $sUrl = $this->getPageUrl($sPageUri, false, false);
  308. return $this->_oQuery->insertPage ($sPageUri, $sUrl, $sTitleLangKey);
  309. }
  310. public function actionGetTranslation ()
  311. {
  312. $aWikiVer = $this->_oQuery->getBlockContent ((int)bx_get('block_id'), bx_get('language'), false, false);
  313. if (!$aWikiVer)
  314. return array('code' => 1, 'actions' => 'ShowMsg', 'msg' => 'no translation was found');
  315. else
  316. return array('code' => 0, 'language' => $aWikiVer['language'], 'content' => $aWikiVer['content'], 'block_id' => $aWikiVer['block_id']);
  317. }
  318. public function actionDeleteVersion ()
  319. {
  320. $iBlockId = (int)bx_get('block_id');
  321. $sLang = bx_lang_name();
  322. $oLang = BxDolLanguages::getInstance();
  323. $aVars = $this->getVarsForHistory($iBlockId, $sLang);
  324. $aVars['select_all'] = _t('_Select_all');
  325. if (!$aVars['bx_repeat:revisions'])
  326. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_msg.html', array(
  327. 'close' => _t('_sys_close'),
  328. 'msg' => _t('_sys_wiki_error_no_revs'),
  329. ));
  330. $aForm = array(
  331. 'form_attrs' => array(
  332. 'name' => 'bx-wiki-del-rev',
  333. 'method' => 'post',
  334. ),
  335. 'params' => array (
  336. 'db' => array(
  337. 'submit_name' => 'do_submit',
  338. ),
  339. ),
  340. 'inputs' => array(
  341. 'block_id' => array(
  342. 'type' => 'hidden',
  343. 'name' => 'block_id',
  344. 'value' => $iBlockId,
  345. ),
  346. 'language' => array(
  347. 'type' => 'hidden',
  348. 'name' => 'language',
  349. 'value' => $sLang,
  350. ),
  351. 'revision' => array(
  352. 'type' => 'custom',
  353. 'name' => 'revision',
  354. 'caption' => '',
  355. 'content' => BxDolTemplate::getInstance()->parseHtmlByName('wiki_delete_version.html', $aVars),
  356. ),
  357. 'buttons' => array(
  358. 'type' => 'input_set',
  359. array(
  360. 'type' => 'submit',
  361. 'name' => 'do_submit',
  362. 'value' => _t('_Submit'),
  363. ),
  364. array(
  365. 'type' => 'reset',
  366. 'name' => 'close',
  367. 'value' => _t('_sys_close'),
  368. 'attrs' => array(
  369. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide();",
  370. 'class' => "bx-def-margin-sec-left",
  371. ),
  372. ),
  373. ),
  374. ),
  375. );
  376. $oForm = new BxTemplFormView ($aForm);
  377. $oForm->initChecker();
  378. if ($oForm->isSubmittedAndValid ()) {
  379. $i = $this->_oQuery->deleteRevisions ($iBlockId, $sLang, bx_get('revision'));
  380. return array('code' => 0, 'actions' => array('Reload', 'ClosePopup', 'ShowMsg'), 'block_id' => $iBlockId, 'msg' => _t('_sys_wiki_revisions_deleted', $i));
  381. }
  382. else {
  383. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_form.html', array(
  384. 'form' => $oForm->getCode(),
  385. 'block_id' => $iBlockId,
  386. 'wiki_action_uri' => $this->getWikiUri(),
  387. 'action' => 'delete-version',
  388. 'txt_open_editor' => bx_js_string(_t('_sys_wiki_open_in_editor')),
  389. ));
  390. }
  391. }
  392. public function actionAddPage ()
  393. {
  394. // validate page URI
  395. $sPageUri = strtolower(bx_get('page'));
  396. if (!preg_match("/^[a-z0-9_-]+$/", $sPageUri))
  397. return _t('_sys_wiki_error_incorrect_page_uri', bx_process_output($sPageUri));
  398. // check if such page exist
  399. $oPage1 = BxDolPage::getObjectInstanceByUri($sPageUri);
  400. $oPage2 = BxDolPage::getObjectInstance($this->_aObject['module'] . '_' . str_replace('-', '_', $sPageUri));
  401. if ($oPage1 || $oPage2)
  402. return _t('_sys_wiki_error_page_exists', bx_process_output($sPageUri));
  403. // get lang category where translations will be added
  404. $oLang = BxDolStudioLanguagesUtils::getInstance();
  405. $iLangCat = $oLang->getLanguageCategory($this->_sLangCateg);
  406. if (!$iLangCat)
  407. return _t('_sys_wiki_error_occured', 9);
  408. // set default values for the form
  409. $aValues = array();
  410. $aLangs = $oLang->getLanguages(false, true);
  411. foreach ($aLangs as $sKey => $sLang)
  412. $aValues[$sKey] = ucfirst($sPageUri);
  413. // init form
  414. $aForm = array(
  415. 'form_attrs' => array(
  416. 'name' => 'bx-wiki-create-page',
  417. 'method' => 'post',
  418. ),
  419. 'params' => array (
  420. 'db' => array(
  421. 'submit_name' => 'do_submit',
  422. ),
  423. ),
  424. 'inputs' => array(
  425. 'page' => array(
  426. 'type' => 'hidden',
  427. 'name' => 'page',
  428. 'value' => $sPageUri,
  429. ),
  430. 'title' => array(
  431. 'type' => 'text_translatable',
  432. 'name' => 'title',
  433. 'caption' => _t('_adm_bp_txt_page_title'),
  434. 'info' => _t('_adm_bp_dsc_page_title'),
  435. 'value' => '',
  436. 'values' => $aValues,
  437. 'required' => '1',
  438. 'db' => array (
  439. 'pass' => 'Xss',
  440. ),
  441. 'checker' => array (
  442. 'func' => 'LengthTranslatable',
  443. 'params' => array(3,100, 'title'),
  444. 'error' => _t('_adm_bp_err_page_title'),
  445. ),
  446. ),
  447. 'buttons' => array(
  448. 'type' => 'input_set',
  449. array(
  450. 'type' => 'submit',
  451. 'name' => 'do_submit',
  452. 'value' => _t('_Submit'),
  453. ),
  454. array(
  455. 'type' => 'reset',
  456. 'name' => 'close',
  457. 'value' => _t('_sys_close'),
  458. 'attrs' => array(
  459. 'onclick' => "$('.bx-popup-applied:visible').dolPopupHide();",
  460. 'class' => "bx-def-margin-sec-left",
  461. ),
  462. ),
  463. ),
  464. ),
  465. );
  466. $oForm = new BxTemplStudioFormView ($aForm);
  467. $oForm->initChecker();
  468. if ($oForm->isSubmittedAndValid ()) {
  469. // insert page
  470. $sLangKey = '_' . $this->_aObject['module'] . '_' . str_replace('-', '_', $sPageUri) . '_' . time();
  471. $iPageId = $this->addPage ($sPageUri, $sLangKey);
  472. if (!$iPageId)
  473. return array('code' => 10, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_wiki_error_occured', 10));
  474. // insert translations
  475. $aLangs = $oLang->getLanguages(true, true);
  476. foreach ($aLangs as $iLangId => $sLangTitle) {
  477. $sLang = $oLang->getLangName($iLangId);
  478. if (!($sVal = $oForm->getCleanValue('title-' . $sLang)))
  479. continue;
  480. $oLang->addLanguageString($sLangKey, $sVal, $iLangId, $iLangCat);
  481. }
  482. return array('code' => 0, 'url' => BxDolPermalinks::getInstance()->permalink($sUrl));
  483. }
  484. else {
  485. // display form
  486. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_create_page_form.html', array(
  487. 'form' => $oForm->getCode(),
  488. 'wiki_action_uri' => $this->getWikiUri(),
  489. 'action' => 'add-page',
  490. ));
  491. }
  492. }
  493. public function actionAdd ()
  494. {
  495. $iCellId = (int)bx_get('cell_id');
  496. $sPage = bx_get('page');
  497. $oPage = BxDolPage::getObjectInstance($sPage);
  498. if (!$oPage)
  499. return array('code' => 4, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_wiki_error_occured', 4));
  500. if ($this->_aObject['module'] != $oPage->getModule())
  501. return array('code' => 5, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_txt_access_denied'));
  502. $iDesignBox = getParam($this->_aObject['module'] . '_design_box');
  503. $aBlock = array(
  504. 'object' => $sPage,
  505. 'cell_id' => $iCellId,
  506. 'module' => $this->_aObject['module'],
  507. 'title_system' => '',
  508. 'title' => '',
  509. 'designbox_id' => false === $iDesignBox ? 0 : $iDesignBox,
  510. 'visible_for_levels' => 2147483647,
  511. 'type' => 'wiki',
  512. 'deletable' => 1,
  513. 'copyable' => 0,
  514. 'active' => 1,
  515. );
  516. $oQueryPageBuilder = new BxDolStudioBuilderPageQuery();
  517. if (!($iBlockId = $oQueryPageBuilder->insertBlock($aBlock)))
  518. return array('code' => 6, 'actions' => array('ShowMsg'), 'msg' => _t('_sys_wiki_error_occured', 6));
  519. return array('code' => 0, 'block_id' => $iBlockId, 'cell_id' => $iCellId);
  520. }
  521. public function actionDeleteBlock ()
  522. {
  523. $iBlockId = (int)bx_get('block_id');
  524. $oQueryPageBuilder = new BxDolStudioBuilderPageQuery();
  525. if (!$oQueryPageBuilder->deleteBlocks(array('type' => 'by_id', 'value' => $iBlockId)))
  526. return array('code' => 3, 'actions' => array('ShowMsg'), 'block_id' => $iBlockId, 'msg' => _t('_sys_wiki_error_occured', 3));
  527. self::onBlockDelete($iBlockId);
  528. return array('code' => 0, 'actions' => array('DeleteBlock', 'ShowMsg'), 'block_id' => $iBlockId, 'msg' => _t('_sys_wiki_block_deleted'));
  529. }
  530. public function actionHistory ()
  531. {
  532. $iBlockId = (int)bx_get('block_id');
  533. $sLang = bx_lang_name();
  534. $oLang = BxDolLanguages::getInstance();
  535. $aVars = $this->getVarsForHistory($iBlockId, $sLang);
  536. $aVars['language'] = $oLang->getLangTitle($oLang->getLangId($sLang));
  537. $aVars['close'] = _t('_sys_close');
  538. $aVars['msg'] = $aVars['bx_repeat:revisions'] ? '' : _t('_sys_wiki_error_no_revs');
  539. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_history.html', $aVars);
  540. }
  541. public function actionTranslate ()
  542. {
  543. return $this->actionEdit (true);
  544. }
  545. public function actionEdit ($bTranslate = false)
  546. {
  547. $iBlockId = (int)bx_get('block_id');
  548. $sMainLangLabel = '';
  549. $aWikiVerMain = array();
  550. $sLangForTranslate = '';
  551. $aLangsForInput = $this->getLangsForInput ($iBlockId, $bTranslate, $sMainLangLabel, $aWikiVerMain, $sLangForTranslate);
  552. // don't allow to translate empty blocks
  553. if (!$aWikiVerMain && $bTranslate)
  554. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_msg.html', array(
  555. 'close' => _t('_sys_close'),
  556. 'msg' => _t('_sys_wiki_error_no_revs'),
  557. ));
  558. // get latest revision for block with current lang
  559. if ($bTranslate) {
  560. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLangForTranslate, false, false);
  561. }
  562. else {
  563. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, bx_lang_name());
  564. }
  565. unset($aWikiVer['notes']); // unset notes since we need this field empty in the form
  566. if (!$aWikiVer) { // check for new block, so initialize with default values
  567. $aWikiVer = array('block_id' => $iBlockId);
  568. if ($bTranslate)
  569. $aWikiVer['language'] = $sLangForTranslate;
  570. else
  571. $aWikiVer['language'] = bx_lang_name();
  572. }
  573. // init form object
  574. $oForm = BxDolForm::getObjectInstance('sys_wiki', $bTranslate ? 'sys_wiki_translate' : 'sys_wiki_edit');
  575. if (!$oForm)
  576. return _t('_sys_wiki_error_occured', 12);
  577. if (isset($oForm->aInputs['language']))
  578. $oForm->aInputs['language']['values'] = $aLangsForInput;
  579. if (isset($oForm->aInputs['content_main']) && $sMainLangLabel) {
  580. $oForm->aInputs['content_main']['caption'] = $sMainLangLabel;
  581. $oForm->aInputs['content_main']['content'] = $aWikiVerMain ? BxDolTemplate::getInstance()->parseHtmlByName('wiki_content.html', array('content' => $aWikiVerMain['content'])) : '';
  582. }
  583. $oForm->initChecker($aWikiVer);
  584. if (!$oForm->isSubmittedAndValid()) {
  585. // display form
  586. return BxDolTemplate::getInstance()->parseHtmlByName('wiki_form.html', array(
  587. 'form' => $oForm->getCode(),
  588. 'block_id' => $iBlockId,
  589. 'wiki_action_uri' => $this->getWikiUri(),
  590. 'action' => $bTranslate ? 'translate' : 'edit',
  591. 'txt_open_editor' => bx_js_string(_t('_sys_wiki_open_in_editor')),
  592. ));
  593. }
  594. else {
  595. $sLang = $oForm->getCleanValue('language');
  596. // get previous revision with priority for current language
  597. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang);
  598. $bMainLang = $this->getFieldMainLangFlag ($oForm, $sLang, $aWikiVer);
  599. $sRev = $this->getFieldRev ($oForm, $sLang, $aWikiVer);
  600. $bUnsafe = $this->getFieldUnsafeFlag ($oForm, $sLang, $aWikiVer);
  601. // insert new revision (main lang is NOT allowed for translations)
  602. $iTime = time();
  603. if ((!$bMainLang || ($bMainLang && $this->isAllowed('edit'))) && $this->isContentChanged($iBlockId, $sLang, $oForm->getCleanValue('content'))) {
  604. $sId = $oForm->insert(array(
  605. 'added' => $iTime,
  606. 'revision' => $sRev,
  607. 'main_lang' => $bMainLang,
  608. 'profile_id' => bx_get_logged_profile_id(),
  609. 'unsafe' => $bUnsafe,
  610. ));
  611. }
  612. // process translations if available
  613. if (($sTranslations = bx_get('translations')) && ($aTranslations = json_decode($sTranslations, true))) {
  614. foreach ($aTranslations as $sLang => $sContent) {
  615. if ($sLang == $oForm->getCleanValue('language'))
  616. continue;
  617. // get previous revision with priority for current language
  618. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang);
  619. $bMainLang = $this->getFieldMainLangFlag ($oForm, $sLang, $aWikiVer);
  620. $sRev = $this->getFieldRev ($oForm, $sLang, $aWikiVer);
  621. $bUnsafe = $this->getFieldUnsafeFlag ($oForm, $sLang, $aWikiVer);
  622. // insert new revision (main lang is NOT allowed for translations)
  623. if ((!$bMainLang || ($bMainLang && $this->isAllowed('edit'))) && $this->isContentChanged($iBlockId, $sLang, $sContent)) {
  624. $sId = $oForm->insert(array(
  625. 'added' => $iTime,
  626. 'revision' => $sRev,
  627. 'main_lang' => $bMainLang,
  628. 'profile_id' => bx_get_logged_profile_id(),
  629. 'unsafe' => $bUnsafe,
  630. 'language' => $sLang,
  631. 'content' => $sContent,
  632. ));
  633. }
  634. }
  635. }
  636. // update indexing data
  637. $this->updateBlockIndexingData($iBlockId);
  638. return array('code' => 0, 'actions' => array('Reload', 'ClosePopup'), 'block_id' => $iBlockId);
  639. }
  640. }
  641. public function actionConvertLinks ()
  642. {
  643. $s = bx_get('s');
  644. if (!$s)
  645. return ['s' => ''];
  646. // convert links to be viewable in external editor
  647. $s = preg_replace_callback('#\((([a-zA-Z0-9_]+)/([a-zA-Z0-9]+))\)#', function ($aMatches) {
  648. $oStorage = BxDolStorage::getObjectInstance($aMatches[2]);
  649. if ($oStorage) {
  650. $sUrl = $oStorage->getFileUrlByRemoteId($aMatches[3]);
  651. if ($sUrl)
  652. return '(' . $sUrl . ')';
  653. }
  654. return '(' . $aMatches[0] . ')';
  655. }, $s);
  656. // add references to existing pages, so short references will work in external editor
  657. $s .= "\n<!-- " . _t("_sys_wiki_external_editor_references_comment") . " -->\n";
  658. $this->_aPages = BxDolWikiQuery::getAllPages ();
  659. if ($this->_aPages) {
  660. foreach ($this->_aPages as $sUri => $r)
  661. $s .= "[{$sUri}]: " . BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($r['url']) . "\n";
  662. }
  663. return ['s' => $s];
  664. }
  665. public function updateBlockIndexingData($iBlockId)
  666. {
  667. if (!($aLangs = $this->_oQuery->getBlockLangs ($iBlockId)))
  668. return false;
  669. $sText = '';
  670. foreach ($aLangs as $sLang) {
  671. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang, false, false);
  672. $sText .= trim(strip_tags($aWikiVer['content'])) . ' ';
  673. }
  674. return $this->_oQuery->updateBlockIndexingData ($iBlockId, $sText);
  675. }
  676. protected function getPageUrl ($sPageUri, $bPermalink = true, $bAddRootUrl = true)
  677. {
  678. $sUrl = ($bAddRootUrl ? BX_DOL_URL_ROOT : '') . 'r.php?_q=' . $this->_aObject['uri'] . '/' . $sPageUri;
  679. return $bPermalink ? BxDolPermalinks::getInstance()->permalink($sUrl) : $sUrl;
  680. }
  681. protected function isContentChanged ($iBlockId, $sLang, $sContent)
  682. {
  683. if (!$sContent) // don't allow revisions with empty content
  684. return false;
  685. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sLang, false, false);
  686. if (!$aWikiVer) // when no previous revision available
  687. return true;
  688. return $sContent != $aWikiVer['content'];
  689. }
  690. protected function getFieldRev ($oForm, $sLang, $aWikiVer) {
  691. // increase revision for particular language or start with first revision
  692. return $aWikiVer && $sLang == $aWikiVer['language'] ? $aWikiVer['revision'] + 1 : 1;
  693. }
  694. protected function getFieldMainLangFlag ($oForm, $sLang, $aWikiVer) {
  695. // detect main language flag
  696. if ($aWikiVer && $sLang == $aWikiVer['language'])
  697. return $aWikiVer['main_lang']; // when revision is increased (language matches) - copy this flag from previous revision
  698. elseif (!$aWikiVer)
  699. return 1; // when it's first revision and no translations - start as main language
  700. else
  701. return 0;
  702. }
  703. protected function getFieldUnsafeFlag ($oForm, $sLang, $aWikiVer) {
  704. if ($aWikiVer && $sLang == $aWikiVer['language'] && !$aWikiVer['unsafe'])
  705. return 0; // copy 'unsafe' from previous revision only when unsafe = 0
  706. else
  707. return $this->isAllowed('unsafe') ? 1 : 0; // in other cases - check permissions
  708. }
  709. protected function getLangsForInput ($iBlockId, $bTranslateForm, &$sMainLangLabel, &$aWikiVerMain, &$sLangForTranslate)
  710. {
  711. // get main lang update time
  712. $aWikiVerMain = $this->_oQuery->getBlockContent ($iBlockId, 'neverhood');
  713. $iUpdatedMainLang = $aWikiVerMain ? $aWikiVerMain['added'] : 0;
  714. // generate values for radio set
  715. $aLangs = BxDolLanguages::getInstance()->getLanguages(false, true);
  716. foreach ($aLangs as $sKey => $sLang) {
  717. $aWikiVer = $this->_oQuery->getBlockContent ($iBlockId, $sKey, false, false);
  718. $sMainLang = $aWikiVer && $aWikiVer['main_lang'] ? '★' : '';
  719. $sComment = !$aWikiVer ? _t('_sys_wiki_lang_missing') : bx_time_js($aWikiVer['added']);
  720. if (!$aWikiVer || $iUpdatedMainLang > $aWikiVer['added'])
  721. $aLangs[$sKey] = _t('_sys_wiki_lang_mask_warn', $sLang, $sMainLang, $sComment);
  722. else
  723. $aLangs[$sKey] = _t('_sys_wiki_lang_mask', $sLang, $sMainLang, $sComment);
  724. if ($bTranslateForm && !$aWikiVer['main_lang'] && !$sLangForTranslate) {
  725. $sLangForTranslate = $sKey;
  726. }
  727. if ($bTranslateForm && $aWikiVer['main_lang']) {
  728. $sMainLangLabel = $aLangs[$sKey];
  729. unset($aLangs[$sKey]);
  730. continue;
  731. }
  732. }
  733. return $aLangs;
  734. }
  735. public function getVarsForHistory ($iBlockId, $sLang)
  736. {
  737. $a = $this->_oQuery->getBlockHistory($iBlockId, $sLang);
  738. $aVars = array(
  739. 'bx_repeat:revisions' => array()
  740. );
  741. foreach ($a as $r) {
  742. $oProfile = BxDolProfile::getInstanceMagic($r['profile_id']);
  743. list($sPageLink, $aPageParams) = bx_get_base_url_popup(array($r['block_id'].'rev' => $r['revision']));
  744. $r['author_url'] = $oProfile->getUrl();
  745. $r['author_name'] = $oProfile->getDisplayName();
  746. $r['timejs'] = bx_time_js($r['added']);
  747. $r['rev_url'] = BxDolPermalinks::getInstance()->permalink(bx_append_url_params($sPageLink, $aPageParams));
  748. $aVars['bx_repeat:revisions'][] = $r;
  749. }
  750. return $aVars;
  751. }
  752. }
  753. class BxDolParsedown extends Parsedown
  754. {
  755. protected $_aPages;
  756. protected function textElements($text)
  757. {
  758. $res = parent::textElements($text);
  759. // add all available pages as references
  760. $this->_aPages = BxDolWikiQuery::getAllPages ();
  761. if ($this->_aPages) {
  762. foreach ($this->_aPages as $sUri => $r) {
  763. $aData = array(
  764. // real link URL is set later when link is referenced
  765. 'url' => 'bx-internal-page://' . $sUri,
  766. 'title' => !empty($r['title']) ? _t($r['title']) : null,
  767. );
  768. $this->DefinitionData['Reference'][$sUri] = $aData;
  769. }
  770. }
  771. return $res;
  772. }
  773. protected function inlineLink($Excerpt)
  774. {
  775. $a = parent::inlineLink($Excerpt);
  776. if ($a && @isset($a['element']['attributes']['href']) && 0 === strncmp($a['element']['attributes']['href'], 'bx-internal-page://', 19)) {
  777. $sUri = strtolower(substr($a['element']['attributes']['href'], 19));
  778. if (isset($this->_aPages[$sUri])) {
  779. $sHref = BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink($this->_aPages[$sUri]['url']);
  780. $a['element']['attributes']['href'] = $sHref;
  781. }
  782. else {
  783. $a['element']['attributes']['href'] = $sUri;
  784. }
  785. }
  786. return $a;
  787. }
  788. protected function inlineImage($Excerpt)
  789. {
  790. $a = parent::inlineImage($Excerpt);
  791. if (@isset($a['element']['attributes']['src']) && preg_match('#^([a-zA-Z0-9_]+)/([a-zA-Z0-9]+)$#', $a['element']['attributes']['src'], $aMatches) && is_array($aMatches) && 3 == count($aMatches)) {
  792. $oStorage = BxDolStorage::getObjectInstance($aMatches[1]);
  793. if ($oStorage) {
  794. $sUrl = $oStorage->getFileUrlByRemoteId($aMatches[2]);
  795. if ($sUrl)
  796. $a['element']['attributes']['src'] = $sUrl;
  797. }
  798. }
  799. return $a;
  800. }
  801. }
  802. /** @} */