diff --git a/includes/YesWiki.php b/includes/YesWiki.php index e918a02e9..bfacd5c76 100755 --- a/includes/YesWiki.php +++ b/includes/YesWiki.php @@ -386,14 +386,28 @@ public function GetMessage() return $message; } - public function getBaseUrl() + public function getBaseUrl($useDataPath = false) { $url = explode('wakka.php', $this->config['base_url']); $url = explode('index.php', $url[0]); $url = preg_replace(array('/\/\?$/', '/\/$/'), '', $url[0]); + if ($useDataPath && !empty($this->config['dataPath']) ) { + // we add an imaginary folder in order to retrieve yeswiki assets from yeswiki's source folder. web servers need to be configured to redirect yeswiki-assets to the main yeswiki folder + $url .= '/yeswiki-assets'; + } return $url; } + public function getLocalPath($folder = '') + { + $dataFolders = ['', 'cache', 'files', 'custom']; + if (in_array($folder, $dataFolders) && !empty($this->config['dataPath']) ) { + // we add an imaginary folder in order to retrieve yeswiki assets from yeswiki's source folder. web servers need to be configured to redirect yeswiki-assets to the main yeswiki folder + $folder = $this->config['dataPath'].'/'.$folder ; + } + return $folder; + } + public function Redirect($url) { header("Location: $url"); diff --git a/includes/YesWikiInit.php b/includes/YesWikiInit.php index 9f4862622..9113c737a 100644 --- a/includes/YesWikiInit.php +++ b/includes/YesWikiInit.php @@ -48,9 +48,12 @@ class Init public function __construct($config = array()) { $this->getRoute(); + if (!empty($_SERVER['YESWIKI_DATA_PATH']) && is_dir($_SERVER['YESWIKI_DATA_PATH'])) { + $this->configFile = $_SERVER['YESWIKI_DATA_PATH'].'/yeswiki.config.php'; + } $this->config = $this->getConfig($config); $this->setIframeHeaders(); - + /* @todo : compare versions, start installer for update if necessary */ if (!file_exists($this->configFile)) { $this->doInstall(); @@ -231,6 +234,7 @@ public function getConfig($wakkaConfig = array()) 'timezone' => 'Europe/Paris', // Only used if not set in wakka.config.php nor in php.ini 'root_page' => 'PagePrincipale', // backup root_page if deleted from wakka.config.php 'wakka_name' => '', // backup wakka_name if deleted from wakka.config.php + 'dataPath' => !empty($_SERVER['YESWIKI_DATA_PATH']) ? $_SERVER['YESWIKI_DATA_PATH'] : '', 'htmlPurifierActivated' => false, // TODO ectoplasme set to true 'favorites_activated' => true, ArchiveService::PARAMS_KEY_IN_WAKKA => [ diff --git a/includes/services/AssetsManager.php b/includes/services/AssetsManager.php index 633b94ea7..7b5fcb99c 100644 --- a/includes/services/AssetsManager.php +++ b/includes/services/AssetsManager.php @@ -75,7 +75,7 @@ public function LinkCSSFile($file, $conditionstart = '', $conditionend = '', $at $isUrl = strpos($file, "http://") === 0 || strpos($file, "https://") === 0; if ($isUrl || !empty($file) && file_exists($file)) { - $href = $isUrl ? $file : "{$this->wiki->getBaseUrl()}/{$file}"; + $href = $isUrl ? $file : "{$this->wiki->getBaseUrl(true)}/{$file}"; $revision = $this->wiki->GetConfigValue('yeswiki_release', null); return <<wiki->GetConfigValue('debug') != 'yes') { if (array_key_exists($file, self::PRODUCTION_PATH_MAPPING)) { $file = self::PRODUCTION_PATH_MAPPING[$file]; diff --git a/includes/services/CommentService.php b/includes/services/CommentService.php index ab755187a..cdd61ee80 100644 --- a/includes/services/CommentService.php +++ b/includes/services/CommentService.php @@ -22,6 +22,7 @@ class CommentService implements EventSubscriberInterface protected $eventDispatcher; protected $mailer; protected $pageManager; + protected $hashcashService; protected $params; protected $pagesWhereCommentWereRendered; protected $userManager; @@ -35,6 +36,7 @@ public function __construct( EventDispatcher $eventDispatcher, Mailer $mailer, PageManager $pageManager, + HashCashService $hashcashService, ParameterBagInterface $params, TemplateEngine $templateEngine, UserManager $userManager @@ -47,6 +49,7 @@ public function __construct( $this->pageManager = $pageManager; $this->templateEngine = $templateEngine; $this->userManager = $userManager; + $this->hashcashService = $hashcashService; $this->params = $params; $this->pagesWhereCommentWereRendered = []; $this->commentsActivated = $this->params->get('comments_activated'); @@ -71,8 +74,7 @@ public function addCommentIfAuthorized($content, $idComment = '') } else { if ($this->wiki->HasAccess("comment", $content['pagetag']) && $this->wiki->Loadpage($content['pagetag'])) { if ($this->params->get('use_hashcash')) { - require_once('tools/security/secret/wp-hashcash.lib'); - if (!isset($content["hashcash_value"]) || ($content["hashcash_value"] != hashcash_field_value())) { + if (!isset($content["hashcash_value"]) || ($content["hashcash_value"] != $this->hashcashService->hashcash_field_value())) { return [ 'code' => 400, 'error' => _t('HASHCASH_COMMENT_NOT_SAVED_MAYBE_YOU_ARE_A_ROBOT') diff --git a/includes/services/Mailer.php b/includes/services/Mailer.php index a4c954ba8..46a4a2b0b 100644 --- a/includes/services/Mailer.php +++ b/includes/services/Mailer.php @@ -220,6 +220,7 @@ public function subscribeToMailingList($email, $mailingList) // TODO when PR #967 merged, refactor this part with YesWiki::getBaseUrl public function getBaseUrl(): string { + // TODO: use getBaseUrl from core unless there is a trap ? return preg_replace('/(\\/wakka\\.php\\?wiki=|\\/\\?wiki=|\\/\\?|\\/)$/m', '', $this->params->get('base_url')) ; } diff --git a/includes/services/TemplateEngine.php b/includes/services/TemplateEngine.php index f9f84cf46..119ebdac8 100644 --- a/includes/services/TemplateEngine.php +++ b/includes/services/TemplateEngine.php @@ -81,9 +81,11 @@ public function __construct( } } + $dataPath = (!empty($this->wiki->config['dataPath'])) ? $this->wiki->config['dataPath'].'/' : ''; + // Set up twig $this->twig = new \Twig\Environment($this->twigLoader, [ - 'cache' => 'cache/templates/', + 'cache' => $dataPath.'cache/templates/', 'auto_reload' => true ]); @@ -104,6 +106,9 @@ public function __construct( $this->addTwigHelper('_t', function ($key, $params = []) { return html_entity_decode(_t($key, $params)); }); + $this->addTwigHelper('baseUrl', function ($useDataPath = null) { + return $this->wiki->getBaseUrl($useDataPath); + }); $this->addTwigHelper('url', function ($options) { $options = array_merge(['tag' => '', 'handler' => '', 'params' => []], $options); $iframe = !empty($options['handler']) ? $options['handler'] : testUrlInIframe(); diff --git a/includes/urlutils.inc.php b/includes/urlutils.inc.php index c857bed06..63a2b1042 100755 --- a/includes/urlutils.inc.php +++ b/includes/urlutils.inc.php @@ -35,9 +35,10 @@ function getAbsoluteUrl() * as it affects the resulting url. Defaults to false. * @return string The base url of the wiki */ -function computeBaseURL($rewrite_mode = false) +function computeBaseURL($rewrite_mode = false, $dataPath = null) { - $scriptlocation = str_replace(array('/index.php', '/wakka.php'), '', $_SERVER["SCRIPT_NAME"]); + $prefix = (!empty($dataPath) && is_dir($dataPath)) ? '/yeswiki-assets' : ''; + $scriptlocation = $prefix.str_replace(array('/index.php', '/wakka.php'), '', $_SERVER["SCRIPT_NAME"]); return getRootUrl() . $scriptlocation diff --git a/setup/footer.php b/setup/footer.php index 55156e31e..4fa065be1 100755 --- a/setup/footer.php +++ b/setup/footer.php @@ -6,7 +6,7 @@ ob_end_flush(); ?> - - + + diff --git a/setup/header.php b/setup/header.php index 64da2e72a..9ceff7f0f 100755 --- a/setup/header.php +++ b/setup/header.php @@ -9,6 +9,7 @@ if (!defined('YW_CHARSET')) { define('YW_CHARSET', $charset); } +$yesWikiDataPath = !empty($_SERVER['YESWIKI_DATA_PATH']) ? $_SERVER['YESWIKI_DATA_PATH'] : ''; header("Content-Type: text/html; charset=$charset"); ob_start(); ?> @@ -17,9 +18,9 @@ <?php echo _t('INSTALLATION_OF_YESWIKI'); ?> - - - + + + diff --git a/setup/install.php b/setup/install.php index ae4b95439..6e5db0012 100755 --- a/setup/install.php +++ b/setup/install.php @@ -131,7 +131,7 @@ foreach ($tablesNames as $tableName) { try { if (mysqli_num_rows(mysqli_query($dblink, "SHOW TABLES LIKE \"{$config['table_prefix']}$tableName\";")) !== 0 // existing table - && mysqli_num_rows(mysqli_query($dblink, "SELECT * FROM `{$config['table_prefix']}$tableName`;")) === 0) /* empty table */{ + && mysqli_num_rows(mysqli_query($dblink, "SELECT * FROM `{$config['table_prefix']}$tableName`;")) === 0) { /* empty table */ mysqli_query($dblink, "DROP TABLE IF EXISTS `{$config['table_prefix']}$tableName`;"); } } catch (\Throwable $th) { @@ -148,11 +148,14 @@ ); mysqli_autocommit($dblink, true); +// get path is src and data are separated +$dataPath = (!empty($_SERVER['YESWIKI_DATA_PATH']) && is_dir($_SERVER['YESWIKI_DATA_PATH'])) ? $_SERVER['YESWIKI_DATA_PATH'].'/' : ''; + // Config indexation by robots if (!isset($config['allow_robots']) || $config['allow_robots'] != '1') { // update robots.txt file - if (file_exists('robots.txt')) { - $robotFile = file_get_contents('robots.txt'); + if (file_exists("{$dataPath}robots.txt")) { + $robotFile = file_get_contents("{$dataPath}robots.txt"); // replace text if (preg_match( "/User-agent: \*(\r?\n?)(?:\s*(?:Disa|A)llow:\s*\/\s*)?/", @@ -173,7 +176,7 @@ $robotFile .= "Disallow: /\n"; } // save robots.txt file - file_put_contents('robots.txt', $robotFile); + file_put_contents($dataPath.'robots.txt', $robotFile); // set meta $config['meta'] = array_merge( @@ -181,8 +184,8 @@ ['robots' => 'noindex,nofollow,max-image-preview:none,noarchive,noimageindex'] ); } else { - if (file_exists('robots.txt')) { - $robotFile = file_get_contents('robots.txt'); + if (file_exists("{$dataPath}robots.txt")) { + $robotFile = file_get_contents("{$dataPath}robots.txt"); // replace text if (preg_match( "/User-agent: \*(\r?\n?)(?:\s*(?:Disa|A)llow:\s*\/\s*)?/", @@ -203,7 +206,7 @@ $robotFile .= "Allow: /\n"; } // save robots.txt file - file_put_contents('robots.txt', $robotFile); + file_put_contents("{$dataPath}robots.txt", $robotFile); } diff --git a/setup/writeconfig.php b/setup/writeconfig.php index fdca4098b..7a05926be 100755 --- a/setup/writeconfig.php +++ b/setup/writeconfig.php @@ -26,7 +26,7 @@ $config['db_charset'] = 'utf8mb4'; // convert config array into PHP code -$configCode = ""; @@ -48,7 +48,7 @@ // write fclose($fp); - echo "
\n
"._t('FINISHED_CONGRATULATIONS').' !
'._t('IT_IS_RECOMMANDED_TO_REMOVE_WRITE_ACCESS_TO_CONFIG_FILE').' wakka.config.php ('._t('THIS_COULD_BE_UNSECURE').').
'; + echo "
\n
"._t('FINISHED_CONGRATULATIONS').' !
'._t('IT_IS_RECOMMANDED_TO_REMOVE_WRITE_ACCESS_TO_CONFIG_FILE').' '.$wakkaConfigLocation.' ('._t('THIS_COULD_BE_UNSECURE').').
'; echo "
\n'._t('GO_TO_YOUR_NEW_YESWIKI_WEBSITE')."\n
\n"; //header('Location: '.$config['base_url'].$config['root_page']); } else { diff --git a/tools/attach/actions/player.php b/tools/attach/actions/player.php index 15427281a..30ab59ed4 100755 --- a/tools/attach/actions/player.php +++ b/tools/attach/actions/player.php @@ -178,7 +178,7 @@ '; echo $output; } elseif ($extension=="mm") { - $output = ''; + $output = ''; $output .="[mm]"; echo $output; } else { diff --git a/tools/attach/handlers/AjaxUploadHandler.php b/tools/attach/handlers/AjaxUploadHandler.php index 045c019cb..e979ab009 100644 --- a/tools/attach/handlers/AjaxUploadHandler.php +++ b/tools/attach/handlers/AjaxUploadHandler.php @@ -42,7 +42,7 @@ public function run() $sizeLimit = $att->attachConfig['max_file_size']; $uploader = new qqFileUploader($allowedExtensions, $sizeLimit, $this->hasTempTag); - $result = $uploader->handleUpload($att->attachConfig['upload_path']); + $result = $uploader->handleUpload($att->getUploadPath()); } catch (\Throwable $th) { $errorsMessage .= "{$th->getMessage()} in {$th->getFile()}, line {$th->getLine()}"; } diff --git a/tools/attach/libs/attach.lib.php b/tools/attach/libs/attach.lib.php index 732643614..717947330 100644 --- a/tools/attach/libs/attach.lib.php +++ b/tools/attach/libs/attach.lib.php @@ -130,17 +130,6 @@ public function mkdir_recursif($dir) public function GetScriptPath() { return $this->wiki->getBaseUrl().'/'; - // if (preg_match("/.(php)$/i", $_SERVER["PHP_SELF"])) { - // $a = explode('/', $_SERVER["PHP_SELF"]); - // $a[count($a) - 1] = ''; - // $path = implode('/', $a); - // } else { - // $path = $_SERVER["PHP_SELF"]; - // } - // $http = (isset($_SERVER['HTTPS']) ? 'https://' : 'http://'); - // return !empty($_SERVER["HTTP_HOST"]) ? - // $http . $_SERVER["HTTP_HOST"] . $path - // : $http . $_SERVER["SERVER_NAME"] . $path; } /** * Calcul le repertoire d'upload en fonction du safe_mode @@ -155,7 +144,11 @@ public function GetUploadPath() $this->mkdir_recursif($path); } } - return $path; + if (!empty($this->wiki->config['dataPath'])) { + return $this->wiki->config['dataPath'].'/'.$path; + } else { + return $path; + } } /** * Calcul le repertoire de cache en fonction du safe_mode @@ -170,7 +163,11 @@ public function GetCachePath() $this->mkdir_recursif($path); } } - return $path; + if (!empty($this->wiki->config['dataPath'])) { + return $this->wiki->config['dataPath'].'/'.$path; + } else { + return $path; + } } /** * Calcule le nom complet du fichier attaché en fonction du safe_mode, du nom et de la date de @@ -499,6 +496,8 @@ public function showAsImage($fullFilename) $height = $this->height; $img_name = $fullFilename; } + $imgUrl = $this->GetScriptPath() . str_replace($this->wiki->getLocalPath(), '', $img_name); + // pour l'image avec bordure on enleve la taille de la bordure! if (strstr($this->classes, 'whiteborder')) { $width = $width - 20; @@ -506,7 +505,7 @@ public function showAsImage($fullFilename) } //c'est une image : balise - $img = "GetScriptPath() . $img_name . "\" " . + $img = "desc . ($this->link ? "\nLien vers: $this->link" : "") . "\" width=\"" . $width . "\" height=\"" . $height . "\" />"; //test si c'est une image sensible $classDataForLinks = @@ -1078,7 +1077,7 @@ public function getResizedFilename($fullFilename, $width, $height, string $mode { $uploadPath = $this->GetUploadPath(); $cachePath = $this->GetCachePath(); - $newFileName = preg_replace("/^$uploadPath/", "$cachePath", $fullFilename); + $newFileName = preg_replace("~^$uploadPath~", "$cachePath", $fullFilename); $newFileName = $this->calculer_nom_fichier_vignette($newFileName, $width, $height); if ($mode == "crop") { $newFileName = preg_replace("/_vignette_/", "_cropped_", $newFileName); diff --git a/tools/attach/templates/actions/pdf.twig b/tools/attach/templates/actions/pdf.twig index 7a71ec1a9..8123a9b87 100644 --- a/tools/attach/templates/actions/pdf.twig +++ b/tools/attach/templates/actions/pdf.twig @@ -3,7 +3,7 @@ {% block main %}