<?php
/*
 * Copyright (C) 2008-2012 Alcatel-Lucent
 *
 * This file is part of PhpWiki.
 *
 * PhpWiki is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * PhpWiki is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

/*
 * Standard Alcatel-Lucent disclaimer for contributing to open source
 *
 * "The Configuration File ("Contribution") has not been tested and/or
 * validated for release as or in products, combinations with products or
 * other commercial use. Any use of the Contribution is entirely made at
 * the user's own responsibility and the user can not rely on any features,
 * functionalities or performances Alcatel-Lucent has attributed to the
 * Contribution.
 *
 * THE CONTRIBUTION BY ALCATEL-LUCENT IS PROVIDED AS IS, WITHOUT WARRANTY
 * OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, COMPLIANCE,
 * NON-INTERFERENCE AND/OR INTERWORKING WITH THE SOFTWARE TO WHICH THE
 * CONTRIBUTION HAS BEEN MADE, TITLE AND NON-INFRINGEMENT. IN NO EVENT SHALL
 * ALCATEL-LUCENT BE LIABLE FOR ANY DAMAGES OR OTHER LIABLITY, WHETHER IN
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * CONTRIBUTION OR THE USE OR OTHER DEALINGS IN THE CONTRIBUTION, WHETHER
 * TOGETHER WITH THE SOFTWARE TO WHICH THE CONTRIBUTION RELATES OR ON A STAND
 * ALONE BASIS."
 */

ini_set("memory_limit", "256M");
ini_set('pcre.backtrack_limit', 100000000);

// Disable compression, seems needed to get all the messages.
$no_gz_buffer=true;

require_once('../../../common/include/env.inc.php');
require_once $gfcommon.'include/pre.php';
require_once $gfplugins.'wiki/common/wikiconfig.class.php';

html_use_jquery();
if (forge_get_config('use_jquery_form_navigate')) {
    use_javascript('/scripts/jquery-formnavigate/jquery.FormNavigate-min.js');
    define('ENABLE_EDIT_TOOLBAR', true);
} else {
    // Disable Toolbar for tests
    define('ENABLE_EDIT_TOOLBAR', false);
}

if (isset($group_id) && $group_id) {
    if (! isset($project) || ! $project) {
        $project = group_get_object($group_id);
    }
} elseif(isset($project) && is_object($project)) {
    $group_id = $project->getID();
}

if (! isset($group_id) || ! isset($project)) {
    exit_no_group();
} else if (!($project->usesPlugin("wiki"))) {
    // We should test whether the wiki plugin is disabled globally
    // in the Forge or by the current project.
    // exit_disabled('home');
    exit_project_disabled('home');
} else {

    global $robots;

    $robots = "noindex,nofollow";

    $group_name = $project->getUnixName();
    $group_public_name = $project->getPublicName();

    $is_external = method_exists($project, 'getIsExternal') && $project->getIsExternal();

    $wc = new WikiConfig($group_id);

    define('VIRTUAL_PATH', '/wiki/g/'.$group_name);
    define('PAGE_PREFIX', '_g'.$group_id.'_');

    // We have to use a smaller value than Phpwiki due to page prefix
    define('MAX_PAGENAME_LENGTH', 92);

    define('THEME', 'fusionforge');

    // For FusionForge, we create some specific pages, located in the theme
    // except for given project
    if ($group_name == forge_get_config('wiki_help_project', 'wiki')) {
        define('WIKI_PGSRC', 'pgsrc');
    } else {
        define('WIKI_PGSRC', 'themes/fusionforge/pgsrc');
    }

    define('FUSIONFORGE', true);

    define('PATH_INFO_PREFIX', '/'.$group_name . '/');
    define('USE_PATH_INFO', true);

    define('WIKI_NAME', $group_name);

    define('ALLOWED_PROTOCOLS', "http|https|mailto|ftp|ftps|news|nntp|ssh|gopher");

    define('DISABLE_MARKUP_WIKIWORD', $wc->getWikiConfig('DISABLE_MARKUP_WIKIWORD'));

    define('NUM_SPAM_LINKS', 20 * ($wc->getWikiConfig('NUM_SPAM_LINKS')));

    define('ENABLE_RATEIT', $wc->getWikiConfig('ENABLE_RATEIT'));

    define('UPLOAD_FILE_PATH', forge_get_config('groupdir_prefix').'/'.WIKI_NAME.'/www/uploads/');
    // define('UPLOAD_DATA_PATH', SERVER_URL . '/www/'.WIKI_NAME.'/uploads/');
    if ($project->isPublic()) {
        define('UPLOAD_DATA_PATH', '/www/'.WIKI_NAME.'/uploads/');
    } else {
        define('UPLOAD_DATA_PATH', '/wiki/view.php/'.WIKI_NAME.'/uploads/');
    }

    // Do not use a directory per user but only one (per project)
    define('UPLOAD_USERDIR', false);

    // Use black list of extensions instead of white list
    define('DISABLE_UPLOAD_ONLY_ALLOWED_EXTENSIONS', true);

    // Get the maximum upload filesize from PHP config
    define('MAX_UPLOAD_SIZE', octets(ini_get('upload_max_filesize')));

    // Disable access log (already in Apache & FusionForge).
    define('ACCESS_LOG_SQL', 0);

    define('DEBUG', (forge_get_config('installation_environment') != 'production'));

    // Postgresql
    define('DATABASE_TYPE', 'SQL');
    // Dummy value (to avoid warning in SystemInfo plugin)
    define('DATABASE_DSN', 'pgsql://localhost/user_phpwiki');

    // Disable VACUUM (they are performed every night)
    define('DATABASE_OPTIMISE_FREQUENCY', 0);

    // TBD: the name should be taken from FusionForge
    // define('ADMIN_USER', 'ACOS Forge Administrator');
    define('ADMIN_USER', 'The PhpWiki programming team');
    // Dummy value
    define('ADMIN_PASSWD', 'xxx');

    // Allow ".svg" and ".swf" as extensions
    define('INLINE_IMAGES', 'png|jpg|jpeg|gif|svg|swf');

    // Allow <div> and <span> in wiki code
    define('ENABLE_MARKUP_DIVSPAN', true);

    // Disable ENABLE_ACDROPDOWN, it creates a <style> in the <body> (illegal)
    define('ENABLE_ACDROPDOWN', false);
    define('ENABLE_AJAX', false);

    define('TOOLBAR_PAGELINK_PULLDOWN', false);
    define('TOOLBAR_TEMPLATE_PULLDOWN', false);
    define('TOOLBAR_IMAGE_PULLDOWN', true);

    // Enable external pages
    define('ENABLE_EXTERNAL_PAGES', $is_external);

    // Let all revisions be stored. Default since 1.3.11
    define('MAJOR_MIN_KEEP', 2147483647);
    define('MINOR_MIN_KEEP', 2147483647);
    define('MAJOR_MAX_AGE', 2147483647);
    define('MAJOR_KEEP', 2147483647);
    define('MINOR_MAX_AGE', 2147483647);
    define('MINOR_KEEP', 2147483647);
    define('AUTHOR_MAX_AGE', 2147483647);
    define('AUTHOR_KEEP', 2147483647);
    define('AUTHOR_MIN_AGE', 2147483647);
    define('AUTHOR_MAX_KEEP', 2147483647);

    //
    // Define access rights for the wiki.
    //

    // Do not allow anon users to edit pages
    define('ALLOW_ANON_EDIT', false);

    // Do not allow fake user
    define('ALLOW_BOGO_LOGIN', false);

    // A dedicated auth has been created to get auth from FusionForge
    $USER_AUTH_ORDER = array("FusionForge");
    define('USER_AUTH_ORDER', 'FusionForge');
    define('USER_AUTH_POLICY', 'strict');

    define('EXTERNAL_LINK_TARGET', '_top');

    // Override the default configuration for CONSTANTS before index.php
    $LC_ALL = language_name_to_locale_code(choose_language_from_context());
    $LANG   = preg_replace('/_.*/', '', $LC_ALL);

    // We use a local interwiki map file
    define('INTERWIKI_MAP_FILE', 'themes/fusionforge/interwiki.map');

    define('DEFAULT_WIKI_PAGES', "");

    define('DBAUTH_AUTH_CHECK', "SELECT IF(passwd='\$password',1,0) as ok FROM plugin_wiki_pref WHERE userid='\$userid'");
    define('DBAUTH_AUTH_USER_EXISTS', "SELECT userid FROM plugin_wiki_pref WHERE userid='\$userid'");
    define('DBAUTH_AUTH_CREATE', "INSERT INTO plugin_wiki_pref (passwd,userid) VALUES ('\$password','\$userid')");
    define('DBAUTH_PREF_SELECT', "SELECT prefs FROM plugin_wiki_pref WHERE userid='\$userid'");
    define('DBAUTH_PREF_UPDATE', "UPDATE plugin_wiki_pref SET prefs='\$pref_blob' WHERE userid='\$userid'");
    define('DBAUTH_PREF_INSERT', "INSERT INTO plugin_wiki_pref (prefs,userid) VALUES ('\$pref_blob','\$userid')");
    define('DBAUTH_IS_MEMBER', "SELECT userid FROM plugin_wiki_pref WHERE userid='\$userid' AND groupname='\$groupname'");
    define('DBAUTH_GROUP_MEMBERS', "SELECT userid FROM plugin_wiki_pref WHERE groupname='\$groupname'");
    define('DBAUTH_USER_GROUPS', "SELECT groupname FROM plugin_wiki_pref WHERE userid='\$userid'");

    define('USE_DB_SESSION', true);

    define('USE_BYTEA', true);

    define('ENABLE_REVERSE_DNS', false);

    // Web DAV location
    define('DEFAULT_DUMP_DIR', forge_get_config('groupdir_prefix').'/'.$group_name."/wikidump/");
    define('HTML_DUMP_DIR', forge_get_config('groupdir_prefix').'/'.$group_name."/wikidumphtml/");

    define('COMPRESS_OUTPUT', false);

    define('CACHE_CONTROL', "NO_CACHE");

    $lg = language_name_to_locale_code(forge_get_config('default_language'));
    $lg = preg_replace('/_.*/', '', $lg);
    define('DEFAULT_LANGUAGE', $lg);

    define('DISABLE_GETIMAGESIZE', true);

    // If the user is logged in, let the Wiki know
    if (session_loggedin()) {
        $user = session_get_user();
        if ($user && is_object($user) && !$user->isError() && $user->isActive()) {
            $user_name = $user->getRealName();
            $_SESSION['user_id'] = $user_name;
            $_SERVER['PHP_AUTH_USER'] = $user_name;
        }
    } else {
        // clear out the globals, just in case...
    }

    // Load the default configuration.
    require_once(dirname(__FILE__).'/lib/prepend.php');
    require_once(dirname(__FILE__).'/lib/IniConfig.php');
    IniConfig(dirname(__FILE__)."/config/config-default.ini");

    // Override the default configuration for VARIABLES after index.php:
    // E.g. Use another DB:
    $DBParams['dbtype'] = 'SQL';
    $DBParams['dsn']    = 'ffpgsql://' . forge_get_config('database_user') . ':' .
        forge_get_config('database_password') . '@' . forge_get_config('database_host') .'/' . forge_get_config('database_name');

    $DBParams['prefix'] = "plugin_wiki_";

    // Start the wiki
    include dirname(__FILE__).'/lib/main.php';
}

/**
 * Return a number of octets from a string like "300M"
 */
function octets($val) {
    $val = trim($val);
    $last = strtolower($val[strlen($val)-1]);
    switch($last) {
        // The 'G' modifier is available since PHP 5.1.0
        case 'g':
            $val *= 1024;
        case 'm':
            $val *= 1024;
        case 'k':
            $val *= 1024;
    }
    return $val;
}
