index.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. <?php
  2. /* Copyright (C) 2016-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/collab/index.php
  19. * \ingroup collab
  20. * \brief Page to work on a shared document (PAD)
  21. */
  22. define('NOSCANPOSTFORINJECTION',1);
  23. define('NOSTYLECHECK',1);
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/admin.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  27. // Load translation files required by the page
  28. $langs->loadLangs(array("admin","other","website"));
  29. if (! $user->admin) accessforbidden();
  30. if (! ((GETPOST('testmenuhider','int') || ! empty($conf->global->MAIN_TESTMENUHIDER)) && empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)))
  31. {
  32. $conf->dol_hide_leftmenu = 1; // Force hide of left menu.
  33. }
  34. $error=0;
  35. $website=GETPOST('website', 'alpha');
  36. $page=GETPOST('page', 'alpha');
  37. $pageid=GETPOST('pageid', 'int');
  38. $action=GETPOST('action','alpha');
  39. if (GETPOST('delete')) { $action='delete'; }
  40. if (GETPOST('preview')) $action='preview';
  41. if (GETPOST('create')) { $action='create'; }
  42. if (GETPOST('editmedia')) { $action='editmedia'; }
  43. if (GETPOST('editcss')) { $action='editcss'; }
  44. if (GETPOST('editmenu')) { $action='editmenu'; }
  45. if (GETPOST('setashome')) { $action='setashome'; }
  46. if (GETPOST('editmeta')) { $action='editmeta'; }
  47. if (GETPOST('editcontent')) { $action='editcontent'; }
  48. if (empty($action)) $action='preview';
  49. /*
  50. * Actions
  51. */
  52. if (GETPOST('refreshsite')) $pageid=0; // If we change the site, we reset the pageid.
  53. if (GETPOST('refreshpage')) $action='preview';
  54. // Add a collab page
  55. if ($action == 'add')
  56. {
  57. $db->begin();
  58. $objectpage->title = GETPOST('WEBSITE_TITLE');
  59. $objectpage->pageurl = GETPOST('WEBSITE_PAGENAME');
  60. $objectpage->description = GETPOST('WEBSITE_DESCRIPTION');
  61. $objectpage->keywords = GETPOST('WEBSITE_KEYWORD');
  62. if (empty($objectpage->title))
  63. {
  64. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("WEBSITE_PAGENAME")), null, 'errors');
  65. $error++;
  66. }
  67. if (! $error)
  68. {
  69. $res = $objectpage->create($user);
  70. if ($res <= 0)
  71. {
  72. $error++;
  73. setEventMessages($objectpage->error, $objectpage->errors, 'errors');
  74. }
  75. }
  76. if (! $error)
  77. {
  78. $db->commit();
  79. setEventMessages($langs->trans("PageAdded", $objectpage->pageurl), null, 'mesgs');
  80. $action='';
  81. }
  82. else
  83. {
  84. $db->rollback();
  85. }
  86. $action = 'preview';
  87. $id = $objectpage->id;
  88. }
  89. // Update page
  90. if ($action == 'delete')
  91. {
  92. $db->begin();
  93. $res = $object->fetch(0, $website);
  94. $res = $objectpage->fetch($pageid, $object->fk_website);
  95. if ($res > 0)
  96. {
  97. $res = $objectpage->delete($user);
  98. if (! $res > 0)
  99. {
  100. $error++;
  101. setEventMessages($objectpage->error, $objectpage->errors, 'errors');
  102. }
  103. if (! $error)
  104. {
  105. $db->commit();
  106. setEventMessages($langs->trans("PageDeleted", $objectpage->pageurl, $website), null, 'mesgs');
  107. header("Location: ".$_SERVER["PHP_SELF"].'?website='.$website);
  108. exit;
  109. }
  110. else
  111. {
  112. $db->rollback();
  113. }
  114. }
  115. else
  116. {
  117. dol_print_error($db);
  118. }
  119. }
  120. /*
  121. * View
  122. */
  123. $form = new Form($db);
  124. $help_url='';
  125. llxHeader('', $langs->trans("WebsiteSetup"), $help_url, '', 0, '', '', '', '', '', '<!-- Begin div class="fiche" -->'."\n".'<div class="fichebutwithotherclass">');
  126. print "\n".'<form action="'.$_SERVER["PHP_SELF"].'" method="POST"><div>';
  127. print '<input type="hidden" name="token" value="'.$_SESSION['newtoken'].'">';
  128. if ($action == 'create')
  129. {
  130. print '<input type="hidden" name="action" value="add">';
  131. }
  132. // Add a margin under toolbar ?
  133. $style='';
  134. if ($action != 'preview' && $action != 'editcontent') $style=' margin-bottom: 5px;';
  135. //var_dump($objectpage);exit;
  136. print '<div class="centpercent websitebar">';
  137. if (count($object->records) > 0)
  138. {
  139. // ***** Part for web sites
  140. print '<div class="websiteselection hideonsmartphoneimp">';
  141. print $langs->trans("WebSite").': ';
  142. print '</div>';
  143. // List of websites
  144. print '<div class="websiteselection">';
  145. $out='';
  146. $out.='<select name="website" class="minwidth100" id="website">';
  147. if (empty($object->records)) $out.='<option value="-1">&nbsp;</option>';
  148. // Loop on each sites
  149. $i=0;
  150. foreach($object->records as $key => $valwebsite)
  151. {
  152. if (empty($website)) $website=$valwebsite->ref;
  153. $out.='<option value="'.$valwebsite->ref.'"';
  154. if ($website == $valwebsite->ref) $out.=' selected'; // To preselect a value
  155. $out.='>';
  156. $out.=$valwebsite->ref;
  157. $out.='</option>';
  158. $i++;
  159. }
  160. $out.='</select>';
  161. $out.=ajax_combobox('website');
  162. print $out;
  163. print '<input type="submit" class="button" name="refreshsite" value="'.$langs->trans("Load").'">';
  164. if ($website)
  165. {
  166. $virtualurl='';
  167. $dataroot=DOL_DATA_ROOT.'/collab/'.$website;
  168. if (! empty($object->virtualhost)) $virtualurl=$object->virtualhost;
  169. }
  170. if ($website && $action == 'preview')
  171. {
  172. $disabled='';
  173. if (empty($user->rights->websites->write)) $disabled=' disabled="disabled"';
  174. print ' &nbsp; ';
  175. //print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("MediaFiles")).'" name="editmedia">';
  176. print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("EditCss")).'" name="editcss">';
  177. print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("EditMenu")).'" name="editmenu">';
  178. print '<input type="submit"'.$disabled.' class="button" value="'.dol_escape_htmltag($langs->trans("AddPage")).'" name="create">';
  179. }
  180. print '</div>';
  181. // Button for websites
  182. print '<div class="websitetools">';
  183. if ($action == 'preview')
  184. {
  185. print '<div class="websiteinputurl">';
  186. print '<input type="text" id="previewsiteurl" class="minwidth200imp" name="previewsite" placeholder="'.$langs->trans("http://myvirtualhost").'" value="'.$virtualurl.'">';
  187. //print '<input type="submit" class="button" name="previewwebsite" target="tab'.$website.'" value="'.$langs->trans("ViewSiteInNewTab").'">';
  188. $htmltext=$langs->trans("SetHereVirtualHost", $dataroot);
  189. print $form->textwithpicto('', $htmltext);
  190. print '</div>';
  191. $urlext=$virtualurl;
  192. $urlint=$urlwithroot.'/public/collab/index.php?website='.$website;
  193. //if (! empty($object->virtualhost))
  194. //{
  195. print '<a class="websitebuttonsitepreview" id="previewsiteext" href="'.$urlext.'" target="tab'.$website.'" alt="'.dol_escape_htmltag($langs->trans("PreviewSiteServedByWebServer", $langs->transnoentitiesnoconv("Site"), $langs->transnoentitiesnoconv("Site"), $dataroot, $urlext)).'">';
  196. print $form->textwithpicto('', $langs->trans("PreviewSiteServedByWebServer", $langs->transnoentitiesnoconv("Site"), $langs->transnoentitiesnoconv("Site"), $dataroot, $urlext?$urlext:$langs->trans("VirtualHostUrlNotDefined")), 1, 'preview_ext');
  197. print '</a>';
  198. //}
  199. print '<a class="websitebuttonsitepreview" id="previewsite" href="'.$urlwithroot.'/public/collab/index.php?website='.$website.'" target="tab'.$website.'" alt="'.dol_escape_htmltag($langs->trans("PreviewSiteServedByDolibarr", $langs->transnoentitiesnoconv("Site"), $langs->transnoentitiesnoconv("Site"), $urlint)).'">';
  200. print $form->textwithpicto('', $langs->trans("PreviewSiteServedByDolibarr", $langs->transnoentitiesnoconv("Site"), $langs->transnoentitiesnoconv("Site"), $urlint, $dataroot), 1, 'preview');
  201. print '</a>';
  202. }
  203. if (in_array($action, array('editcss','editmenu','create')))
  204. {
  205. if ($action != 'preview') print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Cancel")).'" name="preview">';
  206. if (preg_match('/^create/',$action)) print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'" name="update">';
  207. if (preg_match('/^edit/',$action)) print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'" name="update">';
  208. }
  209. print '</div>';
  210. // ***** Part for pages
  211. if ($website)
  212. {
  213. print '</div>';
  214. $array=$objectpage->fetchAll($object->id);
  215. if (! is_array($array) && $array < 0) dol_print_error('', $objectpage->error, $objectpage->errors);
  216. $atleastonepage=(is_array($array) && count($array) > 0);
  217. print '<div class="centpercent websitebar"'.($style?' style="'.$style.'"':'').'">';
  218. print '<div class="websiteselection hideonsmartphoneimp">';
  219. print $langs->trans("Page").': ';
  220. print '</div>';
  221. print '<div class="websiteselection">';
  222. if ($action != 'add')
  223. {
  224. $out='';
  225. $out.='<select name="pageid" id="pageid" class="minwidth200">';
  226. if ($atleastonepage)
  227. {
  228. if (empty($pageid) && $action != 'create') // Page id is not defined, we try to take one
  229. {
  230. $firstpageid=0;$homepageid=0;
  231. foreach($array as $key => $valpage)
  232. {
  233. if (empty($firstpageid)) $firstpageid=$valpage->id;
  234. if ($object->fk_default_home && $key == $object->fk_default_home) $homepageid=$valpage->id;
  235. }
  236. $pageid=$homepageid?$homepageid:$firstpageid; // We choose home page and if not defined yet, we take first page
  237. }
  238. foreach($array as $key => $valpage)
  239. {
  240. $out.='<option value="'.$key.'"';
  241. if ($pageid > 0 && $pageid == $key) $out.=' selected'; // To preselect a value
  242. $out.='>';
  243. $out.=$valpage->title;
  244. if ($object->fk_default_home && $key == $object->fk_default_home) $out.=' ('.$langs->trans("HomePage").')';
  245. $out.='</option>';
  246. }
  247. }
  248. else $out.='<option value="-1">&nbsp;</option>';
  249. $out.='</select>';
  250. $out.=ajax_combobox('pageid');
  251. print $out;
  252. }
  253. else
  254. {
  255. print $langs->trans("New");
  256. }
  257. print '<input type="submit" class="button" name="refreshpage" value="'.$langs->trans("Load").'"'.($atleastonepage?'':' disabled="disabled"').'>';
  258. //print $form->selectarray('page', $array);
  259. if ($action == 'preview')
  260. {
  261. $disabled='';
  262. if (empty($user->rights->websites->write)) $disabled=' disabled="disabled"';
  263. if ($pageid > 0)
  264. {
  265. print ' &nbsp; ';
  266. if ($object->fk_default_home > 0 && $pageid == $object->fk_default_home) print '<input type="submit" class="button" disabled="disabled" value="'.dol_escape_htmltag($langs->trans("SetAsHomePage")).'" name="setashome">';
  267. else print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("SetAsHomePage")).'" name="setashome">';
  268. print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("EditPageMeta")).'" name="editmeta">';
  269. print '<input type="submit" class="button"'.$disabled.' value="'.dol_escape_htmltag($langs->trans("EditPageContent")).'" name="editcontent">';
  270. //print '<a href="'.$_SERVER["PHP_SELF"].'?action=editmeta&website='.urlencode($website).'&pageid='.urlencode($pageid).'" class="button">'.dol_escape_htmltag($langs->trans("EditPageMeta")).'</a>';
  271. //print '<a href="'.$_SERVER["PHP_SELF"].'?action=editcontent&website='.urlencode($website).'&pageid='.urlencode($pageid).'" class="button">'.dol_escape_htmltag($langs->trans("EditPageContent")).'</a>';
  272. print '<input type="submit" class="buttonDelete" name="delete" value="'.$langs->trans("Delete").'"'.($atleastonepage?'':' disabled="disabled"').'>';
  273. }
  274. }
  275. print '</div>';
  276. print '<div class="websiteselection">';
  277. print '</div>';
  278. print '<div class="websitetools">';
  279. if ($website && $pageid > 0 && $action == 'preview')
  280. {
  281. $websitepage = new WebSitePage($db);
  282. $websitepage->fetch($pageid);
  283. $realpage=$urlwithroot.'/public/collab/index.php?website='.$website.'&page='.$pageid;
  284. $pagealias = $websitepage->pageurl;
  285. print '<div class="websiteinputurl">';
  286. print '<input type="text" id="previewpageurl" class="minwidth200imp" name="previewsite" value="'.$pagealias.'" disabled="disabled">';
  287. //print '<input type="submit" class="button" name="previewwebsite" target="tab'.$website.'" value="'.$langs->trans("ViewSiteInNewTab").'">';
  288. $htmltext=$langs->trans("WEBSITE_PAGENAME", $pagealias);
  289. print $form->textwithpicto('', $htmltext);
  290. print '</div>';
  291. if (! empty($object->virtualhost))
  292. {
  293. $urlext=$virtualurl.'/'.$pagealias.'.php';
  294. print '<a class="websitebuttonsitepreview" id="previewpageext" href="'.$urlext.'" target="tab'.$website.'" alt="'.dol_escape_htmltag($langs->trans("PreviewSiteServedByWebServer", $langs->transnoentitiesnoconv("Page"), $langs->transnoentitiesnoconv("Page"), $dataroot, $urlext)).'">';
  295. print $form->textwithpicto('', $langs->trans("PreviewSiteServedByWebServer", $langs->transnoentitiesnoconv("Page"), $langs->transnoentitiesnoconv("Page"), $dataroot, $urlext?$urlext:$langs->trans("VirtualHostUrlNotDefined")), 1, 'preview_ext');
  296. print '</a>';
  297. }
  298. else
  299. {
  300. print '<a class="websitebuttonsitepreview" id="previewpageextnoclick" href="#">';
  301. print $form->textwithpicto('', $langs->trans("PreviewSiteServedByWebServer", $langs->transnoentitiesnoconv("Page"), $langs->transnoentitiesnoconv("Page"), $dataroot, $urlext?$urlext:$langs->trans("VirtualHostUrlNotDefined")), 1, 'preview_ext');
  302. print '</a>';
  303. }
  304. print '<a class="websitebuttonsitepreview" id="previewpage" href="'.$realpage.'&nocache='.dol_now().'" class="button" target="tab'.$website.'" alt="'.dol_escape_htmltag($langs->trans("PreviewSiteServedByDolibarr", $langs->transnoentitiesnoconv("Page"), $langs->transnoentitiesnoconv("Page"), $realpage)).'">';
  305. print $form->textwithpicto('', $langs->trans("PreviewSiteServedByDolibarr", $langs->transnoentitiesnoconv("Page"), $langs->transnoentitiesnoconv("Page"), $realpage, $dataroot), 1, 'preview');
  306. print '</a>'; // View page in new Tab
  307. //print '<input type="submit" class="button" name="previewpage" target="tab'.$website.'"value="'.$langs->trans("ViewPageInNewTab").'">';
  308. // TODO Add js to save alias like we save virtual host name and use dynamic virtual host for url of id=previewpageext
  309. }
  310. if (! in_array($action, array('editcss','editmenu','create')))
  311. {
  312. if ($action != 'preview') print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Cancel")).'" name="preview">';
  313. if (preg_match('/^create/',$action)) print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'" name="update">';
  314. if (preg_match('/^edit/',$action)) print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans("Save")).'" name="update">';
  315. }
  316. print '</div>';
  317. if ($action == 'preview')
  318. {
  319. // Adding jquery code to change on the fly url of preview ext
  320. if (! empty($conf->use_javascript_ajax))
  321. {
  322. print '<script type="text/javascript" language="javascript">
  323. jQuery(document).ready(function() {
  324. jQuery("#previewsiteext,#previewpageext").click(function() {
  325. newurl=jQuery("#previewsiteurl").val();
  326. newpage=jQuery("#previewsiteurl").val() + "/" + jQuery("#previewpageurl").val() + ".php";
  327. console.log("Open url "+newurl);
  328. /* Save url */
  329. jQuery.ajax({
  330. method: "POST",
  331. url: "'.DOL_URL_ROOT.'/core/ajax/saveinplace.php",
  332. data: {
  333. field: \'editval_virtualhost\',
  334. element: \'websites\',
  335. table_element: \'website\',
  336. fk_element: '.$object->id.',
  337. value: newurl,
  338. },
  339. context: document.body
  340. });
  341. jQuery("#previewsiteext").attr("href",newurl);
  342. jQuery("#previewpageext").attr("href",newpage);
  343. });
  344. });
  345. </script>';
  346. }
  347. }
  348. }
  349. }
  350. else
  351. {
  352. print '<div class="websiteselection">';
  353. $langs->load("errors");
  354. print $langs->trans("ErrorModuleSetupNotComplete");
  355. print '<div>';
  356. $action='';
  357. }
  358. print '</div>';
  359. $head = array();
  360. if ($action == 'editcontent')
  361. {
  362. /*
  363. * Editing global variables not related to a specific theme
  364. */
  365. $csscontent = @file_get_contents($filecss);
  366. $contentforedit = '';
  367. /*$contentforedit.='<style scoped>'."\n"; // "scoped" means "apply to parent element only". Not yet supported by browsers
  368. $contentforedit.=$csscontent;
  369. $contentforedit.='</style>'."\n";*/
  370. $contentforedit .= $objectpage->content;
  371. require_once DOL_DOCUMENT_ROOT.'/core/class/doleditor.class.php';
  372. $doleditor=new DolEditor('PAGE_CONTENT',$contentforedit,'',500,'Full','',true,true,true,ROWS_5,'90%');
  373. $doleditor->Create(0, '', false);
  374. }
  375. print "</div>\n</form>\n";
  376. // End of page
  377. llxFooter();
  378. $db->close();