Location: PHPKode > projects > Geeklog Plugins, Static Pages and Blocks > multifaq/admin/install.php
<?php
//
// +---------------------------------------------------------------------------+
// | Multilingual FAQ Plugin           |
// +---------------------------------------------------------------------------+
// | install.php                                                               |
// +---------------------------------------------------------------------------+
// | Copyright (C) 2009 by the following authors:                              |
// |                                                                           |
// | Author:  http://lior.weissbrod.com                                               |
//
// | Copyright (C) 2006 by the following authors:                              |
// |                                                                           |
// | Author:  Oliver Spiesshofer                                               |
// | Constructed with the Universal Plugin                                     |
// | Copyright (C) 2002 by the following authors:                              |
// | Tom Willett                 -    hide@address.com           |
// | Blaine Lang                 -    hide@address.com                    |
// | The Universal Plugin is based on prior work by:                           |
// | Tony Bibbs                  -    hide@address.com                       |
// +---------------------------------------------------------------------------+
// |                                                                           |
// | This program 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.                    |
// |                                                                           |
// | This program 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 this program; if not, write to the Free Software Foundation,   |
// | Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.           |
// |                                                                           |
// +---------------------------------------------------------------------------+
//

require_once('../../../lib-common.php');

$pi_name = 'multifaq'; // It has to be manual because perhaps the plugin's functions.inc is not loaded

$_PLG_MULTIFAQ['base_path'] = $_CONF['path'] . 'plugins/' . $pi_name . '/';

//require_once($_PLG_MULTIFAQ['base_path'] . 'config.php');
require_once($_PLG_MULTIFAQ['base_path'] . 'functions.inc');

$pi_version = $_PLG_MULTIFAQ["pi_version"];
$gl_version = $_PLG_MULTIFAQ["gl_version"];
$pi_url = $_PLG_MULTIFAQ["pi_url"];

$plugin_shortcut = $_PLG_MULTIFAQ['plugin_shortcut'];

// Table name(s) and sql to create it(them)

$_SQL[$plugin_shortcut . '_lang']="CREATE TABLE {$_TABLES[$plugin_shortcut . '_lang']} (
  `l_id` tinyint(3) NOT NULL auto_increment,
  `language` text NOT NULL default '',
  `direction` ENUM('ltr', 'rtl') NOT NULL default 'ltr',
  `is_default` tinyint(1) NOT NULL default '0',
  `is_open` tinyint(1) NOT NULL default '1',
  `commentcode` tinyint(1) NOT NULL default '0',
  `owner_id` mediumint(8) unsigned NOT NULL default '1',
  `group_id` mediumint(8) unsigned NOT NULL default '1',
  `perm_owner` tinyint(1) unsigned NOT NULL default '3',
  `perm_group` tinyint(1) unsigned NOT NULL default '2',
  `perm_members` tinyint(1) unsigned NOT NULL default '2',
  `perm_anon` tinyint(1) unsigned NOT NULL default '2',
  PRIMARY KEY  (`l_id`)
);";

$_SQL[$plugin_shortcut . '_questions']="CREATE TABLE {$_TABLES[$plugin_shortcut . '_questions']} (
  `q_id` varchar(40) NOT NULL default '',
  `t_id` int(11) NOT NULL default '1',
  `q_order` int(1) NOT NULL default '10',
  PRIMARY KEY  (`q_id`)
);";

$_SQL[$plugin_shortcut . '_topics']="CREATE TABLE {$_TABLES[$plugin_shortcut . '_topics']} (
  `topic_id` int(11) NOT NULL auto_increment,
  `level` text NOT NULL default '',
   PRIMARY KEY  (`topic_id`)
);";

$_SQL[$plugin_shortcut . '_trans']="CREATE TABLE {$_TABLES[$plugin_shortcut . '_trans']} (
  `t_id` int(11) NOT NULL auto_increment,
  `e_id` varchar(40) NOT NULL default '',
  `l_id` int(11) NOT NULL default '0',
  `type` enum('q','t') NOT NULL default 'q',
  `field1` text NOT NULL default '',
  `field2` text default '',
  `hits` int(11) NOT NULL default '0',
  `changedate` datetime default NULL,
  `commentcode` tinyint(1) NULL,
  `owner_id` mediumint(8) unsigned NOT NULL default '1',
  `group_id` mediumint(8) unsigned NOT NULL default '1',
  `perm_owner` tinyint(1) unsigned NOT NULL default '3',
  `perm_group` tinyint(1) unsigned NOT NULL default '2',
  `perm_members` tinyint(1) unsigned NOT NULL default '2',
  `perm_anon` tinyint(1) unsigned NOT NULL default '2',
  PRIMARY KEY  (`t_id`),
  KEY `l_id` (`l_id`,`type`)
);";

// Needed for later
$list_of_languages = array();
$perm = '';

function addlang($theid, $thelanguage, $thedefault = 0) {
   global $_TABLES, $list_of_languages, $plugin_shortcut, $LANG_DIRECTION, $perm;

   $list_of_languages[] = $thelanguage;

   $thedirection = 'ltr';
   if ($thedefault) {
      $directions = array("ltr", "rtl");
      if (isset($LANG_DIRECTION) && in_array($LANG_DIRECTION, $directions))
         $thedirection = $LANG_DIRECTION;
   }

   return "INSERT INTO {$_TABLES[$plugin_shortcut . '_lang']} VALUES ($theid, '$thelanguage', '$thedirection', $thedefault, {$perm['is_open']}, {$perm['commentcode']}, {$perm['owner_id']}, {$perm['group_id']}, {$perm['perm_owner']}, {$perm['perm_group']}, {$perm['perm_members']}, {$perm['perm_anon']});";
}

function addtran($theid, $external_id, $thelanguage_id, $type, $thelanguage) {
   global $_CONF, $LANG_DIRECTION, $_TABLES, $plugin_shortcut, $perm;

   if ($_CONF['language'] == $thelanguage && !empty($LANG_DIRECTION) && $LANG_DIRECTION == 'rtl')
      $thedirection = 'RTL';
   else
      $thedirection = 'LTR';

   if ($type == 'q') {
      $title = 'question';
      $commentcode = $perm['commentcode'];
   } elseif ($type == 't') {
      $title = 'topic';
      $commentcode = 'NULL';
   }

   return "INSERT INTO {$_TABLES[$plugin_shortcut . '_trans']} VALUES ($theid, $external_id, $thelanguage_id, '$type', '$thedirection $title in $thelanguage', '$thedirection details in $thelanguage', 0, NOW(), $commentcode, {$perm['owner_id']}, {$perm['group_id']}, {$perm['perm_owner']}, {$perm['perm_group']}, {$perm['perm_members']}, {$perm['perm_anon']});";
}

    // Load the online configuration records
    if (function_exists('plugin_load_configuration')) {
        if (!plugin_load_configuration()) {
            PLG_uninstall($pi_name);

            return false;
        } else {
            $my_config = config::get_instance();
            if ($my_config->group_exists($_PLG_MULTIFAQ['pi_name']))
               $_PLG_MULTIFAQ = $_PLG_MULTIFAQ + $my_config->get_config($_PLG_MULTIFAQ['pi_name']);
        }
    }

// Security Feature to add
// Fill in your security features here
// Note you must add these features to the uninstall routine in function.inc so that they will
// be removed when the uninstall routine runs.
// You do not have to use these particular features.  You can edit/add/delete them
// to fit your plugins security model
//

$NEWFEATURE = array();
$NEWFEATURE[$pi_name . '.admin'] = ucfirst($pi_name) . " Admin";
$NEWFEATURE[$pi_name . '.edit'] = ucfirst($pi_name) . " Editor";

// Only let Root users access this page
if (!SEC_inGroup('Root')) {
    // Someone is trying to illegally access this page
    COM_errorLog("Someone has tried to illegally access the multifaq install/uninstall page.  User id: {$_USER['uid']}, Username: {$_USER['username']}, IP: {$_SERVER['REMOTE_ADDR']}",1);
    $display = COM_siteHeader();
    $display .= COM_startBlock($LANG_PL00['access_denied']);
    $display .= $LANG_PL00['access_denied_msg'];
    $display .= COM_endBlock();
    $display .= COM_siteFooter(true);
    plg_mfaq_finalecho($display);
    exit;
}

function plugin_compatible_with_this_geeklog_version()
{
/*
    if (!function_exists('SEC_createToken')) {
        return false;
    }
*/

    return true;
}

function plugin_install_now() {
    global $_TABLES, $_CONF, $pi_name, $pi_version, $gl_version, $pi_url, $_SQL, $NEWFEATURE, $plugin_shortcut, $list_of_languages, $perm;

    COM_errorLog("Attempting to install the $pi_name Plugin",1);

    // Create the plugin admin security group
    COM_errorLog("Attempting to create $pi_name admin group", 1);
    DB_query("INSERT INTO {$_TABLES['groups']} (grp_name, grp_descr) "
        . "VALUES ('{$NEWFEATURE[$pi_name . '.admin']}', 'Users in this group can administer the $pi_name plugin')",1);
    if (DB_error()) {
        PLG_uninstall($pi_name);
        return false;
        exit;
    }
    COM_errorLog('...success',1);
    $group_id[] = DB_insertId();

    // Create the plugin editor security group
    COM_errorLog("Attempting to create $pi_name editor group", 1);
    DB_query("INSERT INTO {$_TABLES['groups']} (grp_name, grp_descr) VALUES ('{$NEWFEATURE[$pi_name . '.edit']}', 'Users in this group can edit $pi_name items')",1);
    if (DB_error()) {
        PLG_uninstall($pi_name);
        return false;
        exit;
    }
    COM_errorLog('...success',1);
    $group_id[] = DB_insertId();

    // Add plugin Features
    $i = -1;
    foreach ($NEWFEATURE as $feature => $desc) {
        $i += 1;
        COM_errorLog("Adding $feature feature",1);
        DB_query("INSERT INTO {$_TABLES['features']} (ft_name, ft_descr) "
            . "VALUES ('$feature','$desc')",1);
        if (DB_error()) {
            COM_errorLog("Failure adding $feature feature",1);
            PLG_uninstall($pi_name);
            return false;
            exit;
        }
        $feat_id = DB_insertId();
        COM_errorLog("Success",1);
        COM_errorLog("Adding $feature feature to admin group",1);
        DB_query("INSERT INTO {$_TABLES['access']} (acc_ft_id, acc_grp_id) VALUES ($feat_id, {$group_id[$i]})");
        if (DB_error()) {
            COM_errorLog("Failure adding $feature feature to admin group",1);
            PLG_uninstall($pi_name);
            return false;
            exit;
        }
        COM_errorLog("Success",1);
    }

    // OK, now give Root users access to this plugin now! NOTE: Root group should always be 1
    COM_errorLog("Attempting to give all users in Root group access to $pi_name admin group",1);

  for ($i = 0; $i < count($group_id); $i++) {
    DB_query("INSERT INTO {$_TABLES['group_assignments']} VALUES ({$group_id[$i]}, NULL, 1)");
    if (DB_error()) {
        PLG_uninstall($pi_name);
        return false;
        exit;
    }
  }

    // Create the Plugins Tables

    foreach ($_SQL as $table => $sql) {
        COM_errorLog("Creating $table table",1);
        DB_query($sql,1);
        if (DB_error()) {
            COM_errorLog("Error Creating $table table",1);
            PLG_uninstall($pi_name);
            return false;
            exit;
        }
        COM_errorLog("Success - Created $table table",1);
    }

    // Prepare Default Data

$perm = plg_mfaq_setDefaultPermissions($group_id[1]);

$_SQL_DEF[$plugin_shortcut . '_lang - ' . $_CONF['language']] = addlang(1, $_CONF['language'], 1);

if (isset($_CONF['language_files'])) {
    $i = 1;
    foreach ($_CONF['language_files'] as $key => $value)
        if ($value <> $_CONF['language']) {
            $i += 1;
            $_SQL_DEF[$plugin_shortcut . '_lang - ' . $value] = addlang($i, $value);
        }
} elseif ($_CONF['default_charset'] <> 'utf-8' && $_CONF['language'] <>  'english')
    $_SQL_DEF[$plugin_shortcut . '_lang - english'] = addlang(2, 'english');
elseif ($_CONF['default_charset'] == 'utf-8' && $_CONF['language'] <>  'english_utf-8')
    $_SQL_DEF[$plugin_shortcut . '_lang - english_utf-8'] = addlang(2, 'english_utf-8');

$q_id = COM_makesid();
$_SQL_DEF[$plugin_shortcut . '_questions']="INSERT INTO {$_TABLES[$plugin_shortcut . '_questions']} VALUES ('$q_id', 1, 10);";

$_SQL_DEF[$plugin_shortcut . '_topics']="INSERT INTO {$_TABLES[$plugin_shortcut . '_topics']} VALUES (1, '001');";

for ($i = 0; $i < count($list_of_languages); $i++) {
        $j = $i + 1;
        $_SQL_DEF['trans_topic - ' . $list_of_languages[$i]] = addtran($j, 1, $j, 't', $list_of_languages[$i]);
        $_SQL_DEF['trans_question - ' . $list_of_languages[$i]] = addtran($j * 3, $q_id, $j, 'q', $list_of_languages[$i]);
}

    // Insert Default Data

    foreach ($_SQL_DEF as $table => $sql) {
        COM_errorLog("Inserting default data into $table table",1);
        DB_query($sql,1);
        if (DB_error()) {
            COM_errorLog("Error inserting default data into $table table ($sql)",1);
            PLG_uninstall($pi_name);
            return false;
            exit;
        }
        COM_errorLog("Success - inserting data into $table table",1);
    }

    // Finally, register the plugin with Geeklog
    COM_errorLog("Registering $pi_name plugin with Geeklog", 1);
    DB_delete($_TABLES['plugins'],'pi_name',$pi_name);
    DB_query("INSERT INTO {$_TABLES['plugins']} (pi_name, pi_version, pi_gl_version, pi_homepage, pi_enabled) "
        . "VALUES ('$pi_name', '$pi_version', '$gl_version', '$pi_url', 1)");

    if (DB_error()) {
        PLG_uninstall($pi_name);
        return false;
        exit;
    }

    plg_mfaq_sitemap();

    COM_errorLog("Succesfully installed the $pi_name Plugin!",1);
    return true;
}

/*
* Main Function
*/

$action = COM_applyFilter($_POST['action']);

$display = COM_siteHeader();
$T = new Template($_PLG_MULTIFAQ['base_path'] . 'templates/admin');
$T->set_file('install', 'install.thtml');
$T->set_var('cgiurl', $_CONF['site_admin_url'] . '/plugins/' . $pi_name . '/install.php');

if ($action == 'install') {
    if (!plugin_install_now())
       	echo COM_refresh($_CONF['site_admin_url'] . '/plugins.php?msg=72');
} elseif ($action == "uninstall")
   PLG_uninstall($pi_name);

if (DB_count($_TABLES['plugins'], 'pi_name', $pi_name) == 0) {
    $T->set_var('btnmsg', $LANG_MULTIFAQ['install']);
    $T->set_var('action','install');

    $T->set_var(array(
        'direction'       => 'ltr',
        'install_doc'       => nl2br(htmlspecialchars(implode('', file($_PLG_MULTIFAQ['base_path'] . "INSTALL"))))
    ));
} else {
    echo COM_refresh($_CONF['site_admin_url'] . '/plugins/' . $pi_name . '/index.php?plg_mfaq_action=plg_mfaq_sel&amp;what=language&amp;mode=install');
    exit;
}
$T->parse('output','install');
$display .= $T->finish($T->get_var('output'));
$display .= COM_siteFooter(true);

plg_mfaq_finalecho($display);

?>
Return current item: Geeklog Plugins, Static Pages and Blocks