website2.lib.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601
  1. <?php
  2. /* Copyright (C) 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 <https://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/core/lib/website2.lib.php
  19. * \ingroup website
  20. * \brief Library for website module (rare functions not required for execution of website)
  21. */
  22. /**
  23. * Save content of a page on disk
  24. *
  25. * @param string $filemaster Full path of filename master.inc.php for website to generate
  26. * @return boolean True if OK
  27. */
  28. function dolSaveMasterFile($filemaster)
  29. {
  30. global $conf;
  31. // Now generate the master.inc.php page
  32. dol_syslog("We regenerate the master file");
  33. dol_delete_file($filemaster);
  34. $mastercontent = '<?php'."\n";
  35. $mastercontent .= '// File generated to link to the master file - DO NOT MODIFY - It is just an include'."\n";
  36. $mastercontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) {\n";
  37. $mastercontent .= " if (! defined('USEEXTERNALSERVER')) define('USEEXTERNALSERVER', 1);\n";
  38. $mastercontent .= " require_once '".DOL_DOCUMENT_ROOT."/master.inc.php';\n";
  39. $mastercontent .= "}\n";
  40. $mastercontent .= '?>'."\n";
  41. $result = file_put_contents($filemaster, $mastercontent);
  42. if (!empty($conf->global->MAIN_UMASK)) {
  43. @chmod($filemaster, octdec($conf->global->MAIN_UMASK));
  44. }
  45. return $result;
  46. }
  47. /**
  48. * Save an alias page on disk (A page that include the reference page).
  49. * It saves file into the root directory but also into language subdirectory.
  50. *
  51. * @param string $filealias Full path of filename to generate
  52. * @param Website $object Object website
  53. * @param WebsitePage $objectpage Object websitepage
  54. * @return boolean True if OK
  55. * @see dolSavePageContent()
  56. */
  57. function dolSavePageAlias($filealias, $object, $objectpage)
  58. {
  59. global $conf;
  60. // Now create the .tpl file
  61. dol_syslog("dolSavePageAlias We regenerate the alias page filealias=".$filealias);
  62. $aliascontent = '<?php'."\n";
  63. $aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
  64. $aliascontent .= 'global $dolibarr_main_data_root;'."\n";
  65. $aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'./page'.$objectpage->id.'.tpl.php\'; ';
  66. $aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
  67. $aliascontent .= '?>'."\n";
  68. $result = file_put_contents($filealias, $aliascontent);
  69. if ($result === false) {
  70. dol_syslog("Failed to write file ".$filealias, LOG_WARNING);
  71. }
  72. if (!empty($conf->global->MAIN_UMASK)) {
  73. @chmod($filealias, octdec($conf->global->MAIN_UMASK));
  74. }
  75. // Save also alias into language subdirectory if it is not a main language
  76. if ($objectpage->lang && in_array($objectpage->lang, explode(',', $object->otherlang))) {
  77. $dirname = dirname($filealias);
  78. $filename = basename($filealias);
  79. $filealiassub = $dirname.'/'.$objectpage->lang.'/'.$filename;
  80. $aliascontent = '<?php'."\n";
  81. $aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
  82. $aliascontent .= 'global $dolibarr_main_data_root;'."\n";
  83. $aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'../page'.$objectpage->id.'.tpl.php\'; ';
  84. $aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
  85. $aliascontent .= '?>'."\n";
  86. $result = file_put_contents($filealiassub, $aliascontent);
  87. if ($result === false) {
  88. dol_syslog("Failed to write file ".$filealiassub, LOG_WARNING);
  89. }
  90. if (!empty($conf->global->MAIN_UMASK)) {
  91. @chmod($filealiassub, octdec($conf->global->MAIN_UMASK));
  92. }
  93. } elseif (empty($objectpage->lang) || !in_array($objectpage->lang, explode(',', $object->otherlang))) {
  94. // Save also alias into all language subdirectories if it is a main language
  95. if (empty($conf->global->WEBSITE_DISABLE_MAIN_LANGUAGE_INTO_LANGSUBDIR) && !empty($object->otherlang)) {
  96. $dirname = dirname($filealias);
  97. $filename = basename($filealias);
  98. foreach (explode(',', $object->otherlang) as $sublang) {
  99. // Avoid to erase main alias file if $sublang is empty string
  100. if (empty(trim($sublang))) continue;
  101. $filealiassub = $dirname.'/'.$sublang.'/'.$filename;
  102. $aliascontent = '<?php'."\n";
  103. $aliascontent .= "// File generated to wrap the alias page - DO NOT MODIFY - It is just a wrapper to real page\n";
  104. $aliascontent .= 'global $dolibarr_main_data_root;'."\n";
  105. $aliascontent .= 'if (empty($dolibarr_main_data_root)) require \'../page'.$objectpage->id.'.tpl.php\'; ';
  106. $aliascontent .= 'else require $dolibarr_main_data_root.\'/website/\'.$website->ref.\'/page'.$objectpage->id.'.tpl.php\';'."\n";
  107. $aliascontent .= '?>'."\n";
  108. $result = file_put_contents($filealiassub, $aliascontent);
  109. if ($result === false) {
  110. dol_syslog("Failed to write file ".$filealiassub, LOG_WARNING);
  111. }
  112. if (!empty($conf->global->MAIN_UMASK)) {
  113. @chmod($filealiassub, octdec($conf->global->MAIN_UMASK));
  114. }
  115. }
  116. }
  117. }
  118. return ($result ?true:false);
  119. }
  120. /**
  121. * Save content of a page on disk (page name is generally ID_of_page.php).
  122. * Page contents are always saved into "root" directory. Only aliases pages saved with dolSavePageAlias() can be in root or language subdir.
  123. *
  124. * @param string $filetpl Full path of filename to generate
  125. * @param Website $object Object website
  126. * @param WebsitePage $objectpage Object websitepage
  127. * @param int $backupold 1=Make a backup of old page
  128. * @return boolean True if OK
  129. * @see dolSavePageAlias()
  130. */
  131. function dolSavePageContent($filetpl, Website $object, WebsitePage $objectpage, $backupold = 0)
  132. {
  133. global $conf, $db;
  134. // Now create the .tpl file (duplicate code with actions updatesource or updatecontent but we need this to save new header)
  135. dol_syslog("We regenerate the tpl page filetpl=".$filetpl);
  136. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  137. if ($backupold) {
  138. dol_delete_file($filetpl.'.old');
  139. $result = dol_move($filetpl, $filetpl.'.old', 0, 1, 0, 0);
  140. if (! $result) {
  141. return false;
  142. }
  143. } else {
  144. dol_delete_file($filetpl);
  145. }
  146. $shortlangcode = '';
  147. if ($objectpage->lang) {
  148. $shortlangcode = substr($objectpage->lang, 0, 2); // en_US or en-US -> en
  149. }
  150. if (empty($shortlangcode)) {
  151. $shortlangcode = substr($object->lang, 0, 2); // en_US or en-US -> en
  152. }
  153. $tplcontent = '';
  154. $tplcontent .= "<?php // BEGIN PHP\n";
  155. $tplcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
  156. $tplcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) {\n";
  157. $tplcontent .= ' $pathdepth = count(explode(\'/\', $_SERVER[\'SCRIPT_NAME\'])) - 2;'."\n";
  158. $tplcontent .= ' require_once ($pathdepth ? str_repeat(\'../\', $pathdepth) : \'./\').\'master.inc.php\';'."\n";
  159. $tplcontent .= "} // Not already loaded\n";
  160. $tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
  161. $tplcontent .= "require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
  162. $tplcontent .= "ob_start();\n";
  163. $tplcontent .= "// END PHP ?>\n";
  164. if (!empty($conf->global->WEBSITE_FORCE_DOCTYPE_HTML5)) {
  165. $tplcontent .= "<!DOCTYPE html>\n";
  166. }
  167. $tplcontent .= '<html'.($shortlangcode ? ' lang="'.$shortlangcode.'"' : '').'>'."\n";
  168. $tplcontent .= '<head>'."\n";
  169. $tplcontent .= '<title>'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'</title>'."\n";
  170. $tplcontent .= '<meta charset="utf-8">'."\n";
  171. $tplcontent .= '<meta http-equiv="content-type" content="text/html; charset=utf-8" />'."\n";
  172. $tplcontent .= '<meta name="robots" content="index, follow" />'."\n";
  173. $tplcontent .= '<meta name="viewport" content="width=device-width, initial-scale=1.0">'."\n";
  174. $tplcontent .= '<meta name="keywords" content="'.dol_string_nohtmltag($objectpage->keywords).'" />'."\n";
  175. $tplcontent .= '<meta name="title" content="'.dol_string_nohtmltag($objectpage->title, 0, 'UTF-8').'" />'."\n";
  176. $tplcontent .= '<meta name="description" content="'.dol_string_nohtmltag($objectpage->description, 0, 'UTF-8').'" />'."\n";
  177. $tplcontent .= '<meta name="generator" content="'.DOL_APPLICATION_TITLE.' '.DOL_VERSION.' (https://www.dolibarr.org)" />'."\n";
  178. $tplcontent .= '<meta name="dolibarr:pageid" content="'.dol_string_nohtmltag($objectpage->id).'" />'."\n";
  179. // Add canonical reference
  180. if ($object->virtualhost) {
  181. $tplcontent .= '<link rel="canonical" href="'.(($objectpage->id == $object->fk_default_home) ? '/' : (($shortlangcode != substr($object->lang, 0, 2) ? '/'.$shortlangcode : '').'/'.$objectpage->pageurl.'.php')).'" />'."\n";
  182. }
  183. // Add translation reference (main language)
  184. if ($object->isMultiLang()) {
  185. // Add page "translation of"
  186. $translationof = $objectpage->fk_page;
  187. if ($translationof) {
  188. $tmppage = new WebsitePage($db);
  189. $tmppage->fetch($translationof);
  190. if ($tmppage->id > 0) {
  191. $tmpshortlangcode = '';
  192. if ($tmppage->lang) {
  193. $tmpshortlangcode = preg_replace('/[_-].*$/', '', $tmppage->lang); // en_US or en-US -> en
  194. }
  195. if (empty($tmpshortlangcode)) {
  196. $tmpshortlangcode = preg_replace('/[_-].*$/', '', $object->lang); // en_US or en-US -> en
  197. }
  198. if ($tmpshortlangcode != $shortlangcode) {
  199. $tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $tmppage->id ? '/' : (($tmpshortlangcode != substr($object->lang, 0, 2)) ? '/'.$tmpshortlangcode : '').'/'.$tmppage->pageurl.'.php').'" />'."\n";
  200. }
  201. }
  202. }
  203. // Add "has translation pages"
  204. $sql = "SELECT rowid as id, lang, pageurl from ".MAIN_DB_PREFIX.'website_page where fk_page IN ('.$db->sanitize($objectpage->id.($translationof ? ", ".$translationof : '')).")";
  205. $resql = $db->query($sql);
  206. if ($resql) {
  207. $num_rows = $db->num_rows($resql);
  208. if ($num_rows > 0) {
  209. while ($obj = $db->fetch_object($resql)) {
  210. $tmpshortlangcode = '';
  211. if ($obj->lang) {
  212. $tmpshortlangcode = preg_replace('/[_-].*$/', '', $obj->lang); // en_US or en-US -> en
  213. }
  214. if ($tmpshortlangcode != $shortlangcode) {
  215. $tplcontent .= '<link rel="alternate" hreflang="'.$tmpshortlangcode.'" href="'.($object->fk_default_home == $obj->id ? '/' : (($tmpshortlangcode != substr($object->lang, 0, 2) ? '/'.$tmpshortlangcode : '')).'/'.$obj->pageurl.'.php').'" />'."\n";
  216. }
  217. }
  218. }
  219. } else {
  220. dol_print_error($db);
  221. }
  222. // Add myself
  223. $tplcontent .= '<?php if ($_SERVER["PHP_SELF"] == "'.(($object->fk_default_home == $objectpage->id) ? '/' : (($shortlangcode != substr($object->lang, 0, 2)) ? '/'.$shortlangcode : '')).'/'.$objectpage->pageurl.'.php") { ?>'."\n";
  224. $tplcontent .= '<link rel="alternate" hreflang="'.$shortlangcode.'" href="'.(($object->fk_default_home == $objectpage->id) ? '/' : (($shortlangcode != substr($object->lang, 0, 2)) ? '/'.$shortlangcode : '').'/'.$objectpage->pageurl.'.php').'" />'."\n";
  225. $tplcontent .= '<?php } ?>'."\n";
  226. }
  227. // Add manifest.json. Do we have to add it only on home page ?
  228. $tplcontent .= '<?php if ($website->use_manifest) { print \'<link rel="manifest" href="/manifest.json.php" />\'."\n"; } ?>'."\n";
  229. $tplcontent .= '<!-- Include link to CSS file -->'."\n";
  230. // Add js
  231. $tplcontent .= '<link rel="stylesheet" href="/styles.css.php?website=<?php echo $websitekey; ?>" type="text/css" />'."\n";
  232. $tplcontent .= '<!-- Include link to JS file -->'."\n";
  233. $tplcontent .= '<script async src="/javascript.js.php"></script>'."\n";
  234. // Add headers
  235. $tplcontent .= '<!-- Include HTML header from common file -->'."\n";
  236. $tplcontent .= '<?php if (file_exists(DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html")) include DOL_DATA_ROOT."/website/".$websitekey."/htmlheader.html"; ?>'."\n";
  237. $tplcontent .= '<!-- Include HTML header from page header block -->'."\n";
  238. $tplcontent .= preg_replace('/<\/?html>/ims', '', $objectpage->htmlheader)."\n";
  239. $tplcontent .= '</head>'."\n";
  240. $tplcontent .= '<!-- File generated by Dolibarr website module editor -->'."\n";
  241. $tplcontent .= '<body id="bodywebsite" class="bodywebsite bodywebpage-'.$objectpage->ref.'">'."\n";
  242. $tplcontent .= $objectpage->content."\n";
  243. $tplcontent .= '</body>'."\n";
  244. $tplcontent .= '</html>'."\n";
  245. $tplcontent .= '<?php // BEGIN PHP'."\n";
  246. $tplcontent .= '$tmp = ob_get_contents(); ob_end_clean(); dolWebsiteOutput($tmp, "html", '.$objectpage->id.');'."\n";
  247. $tplcontent .= "// END PHP ?>\n";
  248. //var_dump($filetpl);exit;
  249. $result = file_put_contents($filetpl, $tplcontent);
  250. if (!empty($conf->global->MAIN_UMASK)) {
  251. @chmod($filetpl, octdec($conf->global->MAIN_UMASK));
  252. }
  253. return $result;
  254. }
  255. /**
  256. * Save content of the index.php and/or the wrapper.php page
  257. *
  258. * @param string $pathofwebsite Path of website root
  259. * @param string $fileindex Full path of file index.php
  260. * @param string $filetpl File tpl the index.php page redirect to (used only if $fileindex is provided)
  261. * @param string $filewrapper Full path of file wrapper.php
  262. * @return boolean True if OK
  263. */
  264. function dolSaveIndexPage($pathofwebsite, $fileindex, $filetpl, $filewrapper)
  265. {
  266. global $conf;
  267. $result1 = false;
  268. $result2 = false;
  269. dol_mkdir($pathofwebsite);
  270. if ($fileindex) {
  271. dol_delete_file($fileindex);
  272. $indexcontent = '<?php'."\n";
  273. $indexcontent .= "// BEGIN PHP File generated to provide an index.php as Home Page or alias redirector - DO NOT MODIFY - It is just a generated wrapper.\n";
  274. $indexcontent .= '$websitekey=basename(__DIR__); if (empty($websitepagefile)) $websitepagefile=__FILE__;'."\n";
  275. $indexcontent .= "if (! defined('USEDOLIBARRSERVER') && ! defined('USEDOLIBARREDITOR')) { require_once './master.inc.php'; } // Load master if not already loaded\n";
  276. $indexcontent .= 'if (! empty($_GET[\'pageref\']) || ! empty($_GET[\'pagealiasalt\']) || ! empty($_GET[\'pageid\'])) {'."\n";
  277. $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/lib/website.lib.php';\n";
  278. $indexcontent .= " require_once DOL_DOCUMENT_ROOT.'/core/website.inc.php';\n";
  279. $indexcontent .= ' redirectToContainer($_GET[\'pageref\'], $_GET[\'pagealiasalt\'], $_GET[\'pageid\']);'."\n";
  280. $indexcontent .= "}\n";
  281. $indexcontent .= "include_once './".basename($filetpl)."'\n";
  282. $indexcontent .= '// END PHP ?>'."\n";
  283. $result1 = file_put_contents($fileindex, $indexcontent);
  284. if (!empty($conf->global->MAIN_UMASK)) {
  285. @chmod($fileindex, octdec($conf->global->MAIN_UMASK));
  286. }
  287. } else {
  288. $result1 = true;
  289. }
  290. if ($filewrapper) {
  291. dol_delete_file($filewrapper);
  292. $wrappercontent = file_get_contents(DOL_DOCUMENT_ROOT.'/website/samples/wrapper.php');
  293. $result2 = file_put_contents($filewrapper, $wrappercontent);
  294. if (!empty($conf->global->MAIN_UMASK)) {
  295. @chmod($filewrapper, octdec($conf->global->MAIN_UMASK));
  296. }
  297. } else {
  298. $result2 = true;
  299. }
  300. return ($result1 && $result2);
  301. }
  302. /**
  303. * Save content of a page on disk
  304. *
  305. * @param string $filehtmlheader Full path of filename to generate
  306. * @param string $htmlheadercontent Content of file
  307. * @return boolean True if OK
  308. */
  309. function dolSaveHtmlHeader($filehtmlheader, $htmlheadercontent)
  310. {
  311. global $conf, $pathofwebsite;
  312. dol_syslog("Save html header into ".$filehtmlheader);
  313. dol_mkdir($pathofwebsite);
  314. $result = file_put_contents($filehtmlheader, $htmlheadercontent);
  315. if (!empty($conf->global->MAIN_UMASK)) {
  316. @chmod($filehtmlheader, octdec($conf->global->MAIN_UMASK));
  317. }
  318. return $result;
  319. }
  320. /**
  321. * Save content of a page on disk
  322. *
  323. * @param string $filecss Full path of filename to generate
  324. * @param string $csscontent Content of file
  325. * @return boolean True if OK
  326. */
  327. function dolSaveCssFile($filecss, $csscontent)
  328. {
  329. global $conf, $pathofwebsite;
  330. dol_syslog("Save css file into ".$filecss);
  331. dol_mkdir($pathofwebsite);
  332. $result = file_put_contents($filecss, $csscontent);
  333. if (!empty($conf->global->MAIN_UMASK)) {
  334. @chmod($filecss, octdec($conf->global->MAIN_UMASK));
  335. }
  336. return $result;
  337. }
  338. /**
  339. * Save content of a page on disk. For example into documents/website/mywebsite/javascript.js.php file.
  340. *
  341. * @param string $filejs Full path of filename to generate
  342. * @param string $jscontent Content of file
  343. * @return boolean True if OK
  344. */
  345. function dolSaveJsFile($filejs, $jscontent)
  346. {
  347. global $conf, $pathofwebsite;
  348. dol_syslog("Save js file into ".$filejs);
  349. dol_mkdir($pathofwebsite);
  350. $result = file_put_contents($filejs, $jscontent);
  351. if (!empty($conf->global->MAIN_UMASK)) {
  352. @chmod($filejs, octdec($conf->global->MAIN_UMASK));
  353. }
  354. return $result;
  355. }
  356. /**
  357. * Save content of a page on disk
  358. *
  359. * @param string $filerobot Full path of filename to generate
  360. * @param string $robotcontent Content of file
  361. * @return boolean True if OK
  362. */
  363. function dolSaveRobotFile($filerobot, $robotcontent)
  364. {
  365. global $conf, $pathofwebsite;
  366. dol_syslog("Save robot file into ".$filerobot);
  367. dol_mkdir($pathofwebsite);
  368. $result = file_put_contents($filerobot, $robotcontent);
  369. if (!empty($conf->global->MAIN_UMASK)) {
  370. @chmod($filerobot, octdec($conf->global->MAIN_UMASK));
  371. }
  372. return $result;
  373. }
  374. /**
  375. * Save content of a page on disk
  376. *
  377. * @param string $filehtaccess Full path of filename to generate
  378. * @param string $htaccess Content of file
  379. * @return boolean True if OK
  380. */
  381. function dolSaveHtaccessFile($filehtaccess, $htaccess)
  382. {
  383. global $conf, $pathofwebsite;
  384. dol_syslog("Save htaccess file into ".$filehtaccess);
  385. dol_mkdir($pathofwebsite);
  386. $result = file_put_contents($filehtaccess, $htaccess);
  387. if (!empty($conf->global->MAIN_UMASK)) {
  388. @chmod($filehtaccess, octdec($conf->global->MAIN_UMASK));
  389. }
  390. return $result;
  391. }
  392. /**
  393. * Save content of a page on disk
  394. *
  395. * @param string $file Full path of filename to generate
  396. * @param string $content Content of file
  397. * @return boolean True if OK
  398. */
  399. function dolSaveManifestJson($file, $content)
  400. {
  401. global $conf, $pathofwebsite;
  402. dol_syslog("Save manifest.js.php file into ".$file);
  403. dol_mkdir($pathofwebsite);
  404. $result = file_put_contents($file, $content);
  405. if (!empty($conf->global->MAIN_UMASK)) {
  406. @chmod($file, octdec($conf->global->MAIN_UMASK));
  407. }
  408. return $result;
  409. }
  410. /**
  411. * Save content of a page on disk
  412. *
  413. * @param string $file Full path of filename to generate
  414. * @param string $content Content of file
  415. * @return boolean True if OK
  416. */
  417. function dolSaveReadme($file, $content)
  418. {
  419. global $conf, $pathofwebsite;
  420. dol_syslog("Save README.md file into ".$file);
  421. dol_mkdir($pathofwebsite);
  422. $result = file_put_contents($file, $content);
  423. if (!empty($conf->global->MAIN_UMASK)) {
  424. @chmod($file, octdec($conf->global->MAIN_UMASK));
  425. }
  426. return $result;
  427. }
  428. /**
  429. * Show list of themes. Show all thumbs of themes/skins
  430. *
  431. * @param Website $website Object website to load the template into
  432. * @return void
  433. */
  434. function showWebsiteTemplates(Website $website)
  435. {
  436. global $conf, $langs, $db, $form;
  437. $dirthemes = array('/doctemplates/websites');
  438. if (!empty($conf->modules_parts['websitetemplates'])) { // Using this feature slow down application
  439. foreach ($conf->modules_parts['websitetemplates'] as $reldir) {
  440. $dirthemes = array_merge($dirthemes, (array) ($reldir.'doctemplates/websites'));
  441. }
  442. }
  443. $dirthemes = array_unique($dirthemes);
  444. // Now dir_themes=array('/themes') or dir_themes=array('/theme','/mymodule/theme')
  445. $colspan = 2;
  446. print '<!-- For website template import -->'."\n";
  447. print '<table class="noborder centpercent">';
  448. // Title
  449. print '<tr class="liste_titre"><th class="titlefield">';
  450. print $form->textwithpicto($langs->trans("Templates"), $langs->trans("ThemeDir").' : '.join(", ", $dirthemes));
  451. print '</th>';
  452. print '<th class="right">';
  453. $url = 'https://www.dolistore.com/43-web-site-templates';
  454. print '<a href="'.$url.'" target="_blank">';
  455. print $langs->trans('DownloadMoreSkins');
  456. print '</a>';
  457. print '</th></tr>';
  458. print '<tr><td colspan="'.$colspan.'">';
  459. print '<table class="nobordernopadding" width="100%"><tr><td><div class="center">';
  460. if (count($dirthemes)) {
  461. $i = 0;
  462. foreach ($dirthemes as $dir) {
  463. //print $dirroot.$dir;exit;
  464. $dirtheme = DOL_DATA_ROOT.$dir; // This include loop on $conf->file->dol_document_root
  465. if (is_dir($dirtheme)) {
  466. $handle = opendir($dirtheme);
  467. if (is_resource($handle)) {
  468. while (($subdir = readdir($handle)) !== false) {
  469. if (is_file($dirtheme."/".$subdir) && substr($subdir, 0, 1) <> '.'
  470. && substr($subdir, 0, 3) <> 'CVS' && preg_match('/\.zip$/i', $subdir)) {
  471. $subdirwithoutzip = preg_replace('/\.zip$/i', '', $subdir);
  472. // Disable not stable themes (dir ends with _exp or _dev)
  473. if ($conf->global->MAIN_FEATURES_LEVEL < 2 && preg_match('/_dev$/i', $subdir)) {
  474. continue;
  475. }
  476. if ($conf->global->MAIN_FEATURES_LEVEL < 1 && preg_match('/_exp$/i', $subdir)) {
  477. continue;
  478. }
  479. print '<div class="inline-block" style="margin-top: 10px; margin-bottom: 10px; margin-right: 20px; margin-left: 20px;">';
  480. $file = $dirtheme."/".$subdirwithoutzip.".jpg";
  481. $url = DOL_URL_ROOT.'/viewimage.php?modulepart=doctemplateswebsite&file='.$subdirwithoutzip.".jpg";
  482. if (!file_exists($file)) {
  483. $url = DOL_URL_ROOT.'/public/theme/common/nophoto.png';
  484. }
  485. $originalfile = basename($file);
  486. $entity = $conf->entity;
  487. $modulepart = 'doctemplateswebsite';
  488. $cache = '';
  489. $title = $file;
  490. $ret = '';
  491. $urladvanced = getAdvancedPreviewUrl($modulepart, $originalfile, 1, '&entity='.$entity);
  492. if (!empty($urladvanced)) {
  493. $ret .= '<a class="'.$urladvanced['css'].'" target="'.$urladvanced['target'].'" mime="'.$urladvanced['mime'].'" href="'.$urladvanced['url'].'">';
  494. } else {
  495. $ret .= '<a href="'.DOL_URL_ROOT.'/viewimage.php?modulepart='.$modulepart.'&entity='.$entity.'&file='.urlencode($originalfile).'&cache='.$cache.'">';
  496. }
  497. print $ret;
  498. print '<img class="img-skinthumb shadow" src="'.$url.'" border="0" alt="'.$title.'" title="'.$title.'" style="margin-bottom: 5px;">';
  499. print '</a>';
  500. print '<br>';
  501. print $subdir.' ('.dol_print_size(dol_filesize($dirtheme."/".$subdir), 1, 1).')';
  502. print '<br><a href="'.$_SERVER["PHP_SELF"].'?action=importsiteconfirm&website='.$website->ref.'&templateuserfile='.$subdir.'" class="button">'.$langs->trans("Load").'</a>';
  503. print '</div>';
  504. $i++;
  505. }
  506. }
  507. }
  508. }
  509. }
  510. } else {
  511. print '<span class="opacitymedium">'.$langs->trans("None").'</span>';
  512. }
  513. print '</div></td></tr></table>';
  514. print '</td></tr>';
  515. print '</table>';
  516. }