Skip to content

Commit

Permalink
Fix vchamilo plugin in order to use URL with a url_append
Browse files Browse the repository at this point in the history
Example, vchamilo with this URL should work

my.chamilo.net/chamilo_site1
my.chamilo.net/chamilo_site2
my.chamilo.net
  • Loading branch information
jmontoyaa committed Dec 6, 2016
1 parent e1f7f45 commit 23c07f9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 20 deletions.
10 changes: 6 additions & 4 deletions main/inc/lib/api.lib.php
Expand Up @@ -819,10 +819,12 @@ function api_get_path($path = '', $configuration = [])

global $virtualChamilo;
if (!empty($virtualChamilo)) {
$paths[$root_web][SYS_ARCHIVE_PATH] = $virtualChamilo[SYS_ARCHIVE_PATH].'/';
$paths[$root_web][SYS_HOME_PATH] = $virtualChamilo[SYS_HOME_PATH].'/';
$paths[$root_web][SYS_COURSE_PATH] = $virtualChamilo[SYS_COURSE_PATH].'/';
$paths[$root_web][SYS_UPLOAD_PATH] = $virtualChamilo[SYS_UPLOAD_PATH].'/';
$paths[$root_web][SYS_ARCHIVE_PATH] = api_add_trailing_slash($virtualChamilo[SYS_ARCHIVE_PATH]);
$paths[$root_web][SYS_HOME_PATH] = api_add_trailing_slash($virtualChamilo[SYS_HOME_PATH]);
$paths[$root_web][SYS_COURSE_PATH] = api_add_trailing_slash($virtualChamilo[SYS_COURSE_PATH]);
$paths[$root_web][SYS_UPLOAD_PATH] = api_add_trailing_slash($virtualChamilo[SYS_UPLOAD_PATH]);
//$paths[$root_web][REL_PATH] = $virtualChamilo[REL_PATH];
//$paths[$root_web][REL_COURSE_PATH] = $virtualChamilo[REL_COURSE_PATH];
}

$isInitialized[$root_web] = true;
Expand Down
3 changes: 2 additions & 1 deletion plugin/vchamilo/lang/english.php
Expand Up @@ -103,12 +103,13 @@

$strings['SiteNameExample'] = 'Example: Chamilo';
$strings['InstitutionExample'] = 'Example: Chamilo Association';
$strings['RootWebExample'] = 'Example: http://www.chamilo.org/';
$strings['RootWebExample'] = 'Example: http://www.chamilo.org/ (with final slash)';
$strings['DatabaseDescription'] = 'A new database will be created with that name.';
$strings['RootWebExists'] = 'An instance with the same root web exists.';
$strings['ImportInstance'] = 'Import instance';
$strings['ConfigurationPath'] = 'Chamilo configuration path';
$strings['UploadRealRoot'] = 'Upload files';
$strings['DatabaseAccessShouldBeDifferentThanMasterChamilo'] = 'Database server should be different than the Chamilo master';
$strings['UrlAppendExample'] = 'Example: /chamilo_v1 (with first slash)';


53 changes: 43 additions & 10 deletions plugin/vchamilo/lib/Virtual.php
Expand Up @@ -24,7 +24,8 @@ public static function hookConfiguration(& $_configuration)
return;
}

// provides an effective value for the virtual root_web based on domain analysis

// provides an effective value for the virtual root_web based on domain analysis
self::getHostName($_configuration);

// We are on physical chamilo. Let original config play
Expand All @@ -40,8 +41,7 @@ public static function hookConfiguration(& $_configuration)
/** @var \Doctrine\DBAL\Connection $connection */
$connection = Virtual::bootConnection($_configuration);

$table = 'vchamilo';
$query = "SELECT * FROM $table WHERE root_web = '$virtualChamiloWebRoot'";
$query = "SELECT * FROM vchamilo WHERE root_web = '$virtualChamiloWebRoot'";
$result = $connection->executeQuery($query);

if ($result->rowCount()) {
Expand Down Expand Up @@ -86,15 +86,19 @@ public static function hookConfiguration(& $_configuration)
if ($data && $data['visible'] === '1') {
foreach ($data as $key => $value) {
if (!in_array($key, $excludes)) {
// Avoid empty password_encryption
if ($key == 'password_encryption' && empty($value)) {
continue;
}
$_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'];
$data['SYS_UPLOAD_PATH'] = $uploadPath.'/'.$data['slug'];
$data['SYS_ARCHIVE_PATH'] = self::addTrailingSlash($archivePath).$data['slug'];
$data['SYS_HOME_PATH'] = self::addTrailingSlash($homePath).$data['slug'];
$data['SYS_COURSE_PATH'] = self::addTrailingSlash($coursePath).$data['slug'];
$data['SYS_UPLOAD_PATH'] = self::addTrailingSlash($uploadPath).$data['slug'];

if (!empty($passwordEncryption)) {
$_configuration['password_encryption'] = $passwordEncryption;
Expand Down Expand Up @@ -139,7 +143,24 @@ public static function getHostName(&$_configuration)
return;
}

$_configuration['vchamilo_web_root'] = "{$protocol}://".@$_SERVER['HTTP_HOST'];
$contentPrefix = '/';
if (isset($_SERVER['CONTEXT_PREFIX']) && !empty($_SERVER['CONTEXT_PREFIX'])) {
$contentPrefix = $_SERVER['CONTEXT_PREFIX'];
} else {
// Getting url_append from URL
if (isset($_SERVER['REQUEST_URI'])) {
$requestUri = $_SERVER['REQUEST_URI'];
if (strpos($requestUri, '/courses/') !== false) {
$result = substr($requestUri, 0, strpos($requestUri, '/courses/'));
if (!empty($result) && $result != '/') {
$contentPrefix = $result;
}
}
}
}

$_configuration['vchamilo_web_root'] = "{$protocol}://".@$_SERVER['HTTP_HOST'].$contentPrefix;

$_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'];
Expand All @@ -150,6 +171,15 @@ public static function getHostName(&$_configuration)
}
}

/**
* @param string $path
* @return string
*/
public static function addTrailingSlash($path)
{
return substr($path, -1) == '/' ? $path : $path.'/';
}

/**
* provides a side connection to a vchamilo database
* @param array $_configuration
Expand Down Expand Up @@ -659,7 +689,7 @@ public static function loadFilesFromTemplate($vchamilo, $template)
);

copyDirTo(
self::chopLastSlash($templateDir.'/data/upload'),
self::chopLastSlash($templateDir.'/data/upload/'),
self::chopLastSlash($uploadPath),
false
);
Expand Down Expand Up @@ -756,7 +786,8 @@ public static function getSlugFromUrl($url)
$slugify = new Slugify();
$urlInfo = parse_url($url);
if (isset($urlInfo['host'])) {
return $slugify->slugify($urlInfo['host']);
$path = $urlInfo['path'] != '/' ? '_'.$urlInfo['path'] : '';
return $slugify->slugify($urlInfo['host'].$path);
}

return false;
Expand Down Expand Up @@ -907,6 +938,8 @@ public static function addInstance($data)
return ;
}

$data->root_web = api_add_trailing_slash($data->root_web);

self::ctrace('Registering: '.$data->root_web);
$tablename = Database::get_main_table('vchamilo');
$sql = "SELECT * FROM $tablename
Expand Down
3 changes: 1 addition & 2 deletions plugin/vchamilo/views/editinstance.php
Expand Up @@ -39,8 +39,7 @@
case 'registerinstance':
Virtual::addInstance($data);
echo '<a class="btn btn-primary" href="'.api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php'.'">Continue</a>';
// vchamilo_redirect(api_get_path(WEB_PLUGIN_PATH).'vchamilo/views/manage.php');
die;
exit;
break;
case 'updateinstance':
unset($data->what);
Expand Down
35 changes: 32 additions & 3 deletions plugin/vchamilo/views/editinstance_form.php
Expand Up @@ -272,7 +272,7 @@ public function _validate_files(&$files)
$status = $this->_upload_manager->preprocess_files();

// now check that we really want each file
foreach ($_FILES as $elname=>$file) {
foreach ($_FILES as $elname => $file) {
if ($mform->elementExists($elname) and $mform->getElementType($elname)=='file') {
$required = $mform->isElementRequired($elname);
if (!empty($this->_upload_manager->files[$elname]['uploadlog']) &&
Expand All @@ -294,9 +294,7 @@ public function _validate_files(&$files)

// return errors if found
if ($status && 0 == count($errors)) {

return true;

} else {
$files = array();

Expand Down Expand Up @@ -363,6 +361,12 @@ public function definition()
);
$form->applyFilter('root_web', 'trim');

$form->addElement(
'text',
'url_append',
['url_append', $plugin->get_lang('UrlAppendExample')]
);

if ($this->_mode == 'update') {
$encryptList = Virtual::getEncryptList();
$encryptMethod = $form->addElement(
Expand Down Expand Up @@ -427,6 +431,31 @@ public function definition()
);
} else {
if ($this->instance) {
$form->addLabel(
'slug',
$this->instance['slug']
);

$form->addLabel(
'archive_real_root',
Virtual::getConfig('vchamilo', 'archive_real_root').$this->instance['slug']
);

$form->addLabel(
'course_real_root',
Virtual::getConfig('vchamilo', 'course_real_root').$this->instance['slug']
);

$form->addLabel(
'home_real_root',
Virtual::getConfig('vchamilo', 'home_real_root').$this->instance['slug']
);

$form->addLabel(
'upload_real_root',
Virtual::getConfig('vchamilo', 'upload_real_root').$this->instance['slug']
);

$form->addLabel(
$this->_plugin->get_lang('template'),
$this->instance['template']
Expand Down

0 comments on commit 23c07f9

Please sign in to comment.