executeQuery($query);
if ($result->rowCount()) {
$data = $result->fetch();
$excludes = array('id', 'name');
$query = "SELECT * FROM settings_current WHERE subkey = 'vchamilo'";
$virtualSettings = $connection->executeQuery($query);
$virtualSettings = $virtualSettings->fetchAll();
$homePath = '';
$coursePath = '';
$archivePath = '';
foreach ($virtualSettings as $setting) {
switch ($setting['variable']) {
case 'vchamilo_home_real_root':
$homePath = $setting['selected_value'];
break;
case 'vchamilo_course_real_root':
$coursePath = $setting['selected_value'];
break;
case 'vchamilo_archive_real_root':
$archivePath = $setting['selected_value'];
break;
}
}
if (empty($homePath) || empty($coursePath) || empty($archivePath)) {
echo 'Configure correctly the vchamilo plugin';
exit;
}
// Only load if is visible
if ($data && $data['visible'] === '1') {
foreach ($data as $key => $value) {
if (!in_array($key, $excludes)) {
$_configuration[$key] = $value;
}
$_configuration['virtual'] = $data['root_web'].'/';
}
$data['SYS_ARCHIVE_PATH'] = $archivePath.'/'.$data['slug'];
$data['SYS_HOME_PATH'] = $homePath.'/'.$data['slug'];
$data['SYS_COURSE_PATH'] = $coursePath.'/'.$data['slug'];
$virtualChamilo = $data;
} else {
exit("This portal is disabled. Please contact your administrator");
}
} else {
// Platform was not configured yet
//die("VChamilo : Could not fetch virtual chamilo configuration");
}
}
/**
* @param array $_configuration
*/
function vchamilo_get_hostname(&$_configuration)
{
if (!empty($_SERVER['HTTP_X_FORWARDED_PROTO']) &&
$_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https' || !empty($_configuration['force_https_forwarded_proto'])
) {
$protocol = 'https';
} else {
$protocol = 'http';
}
if (defined('CLI_VCHAMILO_OVERRIDE')) {
$_configuration['vchamilo_web_root'] = CLI_VCHAMILO_OVERRIDE;
$_configuration['vchamilo_name'] = preg_replace('#https?://#', '', CLI_VCHAMILO_OVERRIDE);
// remove radical from override for name
// fake the server signature
global $_SERVER;
$_SERVER['SERVER_NAME'] = $_configuration['vchamilo_name'];
$_SERVER['HTTP_HOST'] = $_configuration['vchamilo_name'];
$_SERVER['QUERY_STRING'] = '';
$_SERVER['REQUEST_URI'] = CLI_VCHAMILO_OVERRIDE;
return;
}
$_configuration['vchamilo_web_root'] = "{$protocol}://".@$_SERVER['HTTP_HOST'];
$_configuration['vchamilo_name'] = @$_SERVER['HTTP_HOST'];
if (empty($_configuration['vchamilo_name'])) { // try again with another source if has failed
$_configuration['vchamilo_name'] = "{$protocol}://".$_SERVER['SERVER_NAME'];
if ($_SERVER['SERVER_PORT'] != 80) {
$_configuration['vchamilo_name'] .= ':'.$_SERVER['SERVER_PORT'];
}
$_configuration['vchamilo_name'] = $_SERVER['SERVER_NAME'];
}
}
/**
* provides a side connection to a vchamilo database
* @param array $vchamilo
* @return a connection
*/
function vchamilo_boot_connection(&$_configuration)
{
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => $_configuration['db_host'],
'user' => $_configuration['db_user'],
'password' => $_configuration['db_password'],
'dbname' => isset($_configuration['main_database']) ? $_configuration['main_database'] : '',
// Only relevant for pdo_sqlite, specifies the path to the SQLite database.
'path' => isset($_configuration['db_path']) ? $_configuration['db_path'] : '',
// Only relevant for pdo_mysql, pdo_pgsql, and pdo_oci/oci8,
'port' => isset($_configuration['db_port']) ? $_configuration['db_port'] : '',
);
try {
$database = new \Database();
$connection = $database->connect(
$dbParams,
$_configuration['root_sys'],
$_configuration['root_sys'],
true
);
} catch (Exception $e) {
echo('Side connection failure with '.$_configuration['db_host'].', '.$_configuration['db_user'].', ******** ');
die();
}
return $connection;
}
function vchamilo_redirect($url) {
if (preg_match('#https?://#', $url)) {
header('location: '.$url);
} else {
header('location: ' . api_get_path(WEB_PATH).$url);
}
exit;
}
/**
* @param string $course_folder
* @return string
*/
function vchamilo_get_htaccess_fragment($course_folder)
{
$str = "
# Change this file to fit your configuration and save it as .htaccess in the courses folder #
# Chamilo mod rewrite
# Comment lines start with # and are not processed
";
Display::addFlash(Display::return_message($str));
if (!defined('CLI_SCRIPT')) echo "";
}
/**
* Sets a platform configuration setting to a given value, creating it if necessary
* @param string The value we want to record
* @param string The variable name we want to insert
* @param string The subkey for the variable we want to insert
* @param string The type for the variable we want to insert
* @param string The category for the variable we want to insert
* @param string The title
* @param string The comment
* @param string The scope
* @param string The subkey text
* @param int The access_url for which this parameter is valid
* @param int The changeability of this setting for non-master urls
* @return boolean true on success, false on failure
*/
function api_update_setting(
$val,
$var,
$sk = null,
$type = 'textfield',
$c = null,
$title = '',
$com = '',
$sc = null,
$skt = null,
$a = 1,
$v = 0
) {
global $_setting;
if (empty($var) || !isset($val)) {
return false;
}
$t_settings = Database::get_main_table(TABLE_MAIN_SETTINGS_CURRENT);
$var = Database::escape_string($var);
$val = Database::escape_string($val);
$a = (int) $a;
if (empty($a)) { $a = 1; }
// Check if this variable doesn't exist already
$select = "SELECT id FROM $t_settings WHERE variable = '$var' ";
if (!empty($sk)) {
$sk = Database::escape_string($sk);
$select .= " AND subkey = '$sk'";
}
if ($a > 1) {
$select .= " AND access_url = $a";
} else {
$select .= " AND access_url = 1 ";
}
$res = Database::query($select);
if (Database::num_rows($res) > 0) { // Found item for this access_url.
$row = Database::fetch_array($res);
// update value
$update['selected_value'] = $val;
Database::update($t_settings, $update, array('id = ?' => $row['id']));
return $row['id'];
// update in memory setting value
$_setting[$var][$sk] = $val;
}
// Item not found for this access_url, we have to check if the whole thing is missing
// (in which case we ignore the insert) or if there *is* a record but just for access_url = 1
$insert = "INSERT INTO $t_settings " .
"(variable,selected_value," .
"type,category," .
"subkey,title," .
"comment,scope," .
"subkeytext,access_url,access_url_changeable)" .
" VALUES ('$var','$val',";
if (isset($type)) {
$type = Database::escape_string($type);
$insert .= "'$type',";
} else {
$insert .= "NULL,";
}
if (isset($c)) { // Category
$c = Database::escape_string($c);
$insert .= "'$c',";
} else {
$insert .= "NULL,";
}
if (isset($sk)) { // Subkey
$sk = Database::escape_string($sk);
$insert .= "'$sk',";
} else {
$insert .= "NULL,";
}
if (isset($title)) { // Title
$title = Database::escape_string($title);
$insert .= "'$title',";
} else {
$insert .= "NULL,";
}
if (isset($com)) { // Comment
$com = Database::escape_string($com);
$insert .= "'$com',";
} else {
$insert .= "NULL,";
}
if (isset($sc)) { // Scope
$sc = Database::escape_string($sc);
$insert .= "'$sc',";
} else {
$insert .= "NULL,";
}
if (isset($skt)) { // Subkey text
$skt = Database::escape_string($skt);
$insert .= "'$skt',";
} else {
$insert .= "NULL,";
}
$insert .= "$a,$v)";
$res = Database::query($insert);
// update in memory setting value
$_setting[$var][$sk] = $value;
return $res;
}
/**
* converts a timestamp to sql tms
* @param lint $time a unix timestamp
*/
function make_tms($time) {
$tms = date('Y-m-d H:i:s', $time);
return $tms;
}
/**
* Makes sure the data is using valid utf8, invalid characters are discarded.
*
* Note: this function is not intended for full objects with methods and private properties.
*
* @param mixed $value
* @return mixed with proper utf-8 encoding
*/
function fix_utf8($value) {
if (is_null($value) or $value === '') {
return $value;
} else if (is_string($value)) {
if ((string)(int)$value === $value) {
// shortcut
return $value;
}
// Lower error reporting because glibc throws bogus notices.
$olderror = error_reporting();
if ($olderror & E_NOTICE) {
error_reporting($olderror ^ E_NOTICE);
}
// Note: this duplicates min_fix_utf8() intentionally.
static $buggyiconv = null;
if ($buggyiconv === null) {
$buggyiconv = (!function_exists('iconv') or iconv('UTF-8', 'UTF-8//IGNORE', '100'.chr(130).'\80') !== '100\80');
}
if ($buggyiconv) {
if (function_exists('mb_convert_encoding')) {
$subst = mb_substitute_character();
mb_substitute_character('');
$result = mb_convert_encoding($value, 'utf-8', 'utf-8');
mb_substitute_character($subst);
} else {
// Warn admins on admin/index.php page.
$result = $value;
}
} else {
$result = iconv('UTF-8', 'UTF-8//IGNORE', $value);
}
if ($olderror & E_NOTICE) {
error_reporting($olderror);
}
return $result;
} else if (is_array($value)) {
foreach ($value as $k=>$v) {
$value[$k] = fix_utf8($v);
}
return $value;
} else if (is_object($value)) {
$value = clone($value); // do not modify original
foreach ($value as $k=>$v) {
$value->$k = fix_utf8($v);
}
return $value;
} else {
// this is some other type, no utf-8 here
return $value;
}
}
function print_object($obj) {
echo '';
print_r($obj);
echo '';
}
function require_js($file, $component, $return = false)
{
global $_configuration, $htmlHeadXtra;
if (preg_match('/^local_/', $component)) {
$component = str_replace('local_', '', $component);
$path = 'local/';
} else {
$path = 'plugin/';
}
// Secure the postslashing of the roots.
$root_web = $_configuration['root_web'].'/';
$root_web = preg_replace('#//$#', '/', $root_web);
$str = ''."\n";
if ($return === 'head') {
$htmlHeadXtra[] = $str;
}
if ($return) {
return $str;
}
echo $str;
}
function require_css($file, $component, $return = false)
{
global $_configuration, $htmlHeadXtra;
if (preg_match('/^local_/', $component)) {
$component = str_replace('local_', '', $component);
$path = 'local/';
} else {
$path = 'plugin/';
}
// Secure the postslashing of the roots.
$root_web = $_configuration['root_web'].'/';
$root_web = preg_replace('#//$#', '/', $root_web);
$str = ''."\n";
if ($return === 'head') {
$htmlHeadXtra[] = $str;
}
if ($return) {
return $str;
}
echo $str;
}
/**
*
*/
function required_param($key, $type = 0)
{
if (array_key_exists($key, $_REQUEST)) {
$value = $_REQUEST[$key];
$value = param_filter_type($value, $type);
return $value;
}
die("Missing expected param $key in request input");
}
function optional_param($key, $default, $type = 0)
{
if (array_key_exists($key, $_REQUEST)) {
$value = $_REQUEST[$key];
$value = param_filter_type($value, $type);
return $value;
}
return $default;
}
function param_filter_type($value, $type)
{
switch($type) {
case 0:
return $value; // no filtering
case PARAM_BOOL:
return $value == 0; // forces outputing boolean
case PARAM_INT:
if (preg_match('/^([1-90]+)/', $value, $matches)) {
return $matches[1];
}
return 0;
case PARAM_TEXT:
// TODO more filtering here
return $value;
}
}
function redirect($url) {
header("Location: $url\n\n");
}
function vchamilo_get_slug_from_url($url)
{
$slugify = new Slugify();
$urlInfo = parse_url($url);
return $slugify->slugify($urlInfo['host']);
}
/**
* Check if all settings are complete
*/
function vchamilo_check_settings()
{
$enabled = vchamilo_get_config('vchamilo', 'enable_virtualisation');
if (empty($enabled)) {
api_not_allowed(true, 'Plugin is not enabled');
}
global $virtualChamilo;
if (!isset($virtualChamilo)) {
api_not_allowed(true, 'You have to edit the configuration.php. Please check the readme file.');
}
$coursePath = vchamilo_get_config('vchamilo', 'course_real_root');
$homePath = vchamilo_get_config('vchamilo', 'home_real_root');
$archivePath = vchamilo_get_config('vchamilo', 'archive_real_root');
$cmdSql = vchamilo_get_config('vchamilo', 'cmd_mysql');
$cmdMySql = vchamilo_get_config('vchamilo', 'cmd_mysqldump');
if (empty($coursePath) || empty($homePath) || empty($archivePath) || empty($cmdSql)|| empty($cmdMySql)) {
api_not_allowed(true, 'You have to complete all plugin settings.');
}
$separator = DIRECTORY_SEPARATOR;
$templatePath = api_get_path(SYS_PATH).'plugin'.$separator.'vchamilo'.$separator.'templates';
$paths = [
$coursePath,
$homePath,
$archivePath,
$templatePath
];
foreach ($paths as $path) {
if (!is_writable($path)) {
Display::addFlash(
Display::return_message('Directory must have writable permissions: '.$path, 'warning')
);
}
}
}
/**
* @param object $instance
* @return \Doctrine\DBAL\Connection
*/
function vchamilo_get_connection_from_instance($instance)
{
$dbParams = array(
'driver' => 'pdo_mysql',
'host' => $instance->db_host,
'user' => $instance->db_user,
'password' => $instance->db_password,
'dbname' => $instance->main_database,
// Only relevant for pdo_sqlite, specifies the path to the SQLite database.
//'path' => isset($_configuration['db_path']) ? $_configuration['db_path'] : '',
// Only relevant for pdo_mysql, pdo_pgsql, and pdo_oci/oci8,
//'port' => isset($_configuration['db_port']) ? $_configuration['db_port'] : '',
);
try {
$database = new \Database();
$connection = $database->connect(
$dbParams,
api_get_configuration_value('root_sys'),
api_get_configuration_value('root_sys'),
true
);
return $connection;
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
}