BxDolWiki.php 37 KB

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