/**
 * Build the modify-column of the table
 * @param   int     The user id
 * @param   string  URL params to add to table links
 * @param   array   Row of elements to alter
 * @return string Some HTML-code with modify-buttons
 */
function modify_filter($user_id, $url_params, $row)
{
    global $charset;
    $_admins_list = array_column(UserManager::get_all_administrators(), 'user_id');
    $is_admin = in_array($user_id, $_admins_list);
    $statusname = api_get_status_langvars();
    $user_is_anonymous = false;
    $current_user_status_label = $row['7'];
    if ($current_user_status_label == $statusname[ANONYMOUS]) {
        $user_is_anonymous = true;
    }
    $result = '';
    if (!$user_is_anonymous) {
        $icon = Display::return_icon('course.png', get_lang('Courses'), array('onmouseout' => 'clear_course_list (\'div_' . $user_id . '\')'));
        $result .= '<a href="javascript:void(0)" onclick="load_course_list(\'div_' . $user_id . '\',' . $user_id . ')" >
			        ' . $icon . '
					<div class="blackboard_hide" id="div_' . $user_id . '">&nbsp;&nbsp;</div>
					</a>';
        $icon = Display::return_icon('session.png', get_lang('Sessions'), array('onmouseout' => 'clear_session_list (\'div_s_' . $user_id . '\')'));
        $result .= '<a href="javascript:void(0)" onclick="load_session_list(\'div_s_' . $user_id . '\',' . $user_id . ')" >
					' . $icon . '
					<div class="blackboard_hide" id="div_s_' . $user_id . '">&nbsp;&nbsp;</div>
					</a>';
    } else {
        $result .= Display::return_icon('course_na.png', get_lang('Courses')) . '&nbsp;&nbsp;';
        $result .= Display::return_icon('course_na.png', get_lang('Sessions')) . '&nbsp;&nbsp;';
    }
    if (api_is_platform_admin()) {
        if (!$user_is_anonymous) {
            $result .= '<a href="user_information.php?user_id=' . $user_id . '">' . Display::return_icon('synthese_view.gif', get_lang('Info')) . '</a>&nbsp;&nbsp;';
        } else {
            $result .= Display::return_icon('synthese_view_na.gif', get_lang('Info')) . '&nbsp;&nbsp;';
        }
    }
    //only allow platform admins to login_as, or session admins only for students (not teachers nor other admins)
    $url = Container::getUrlGenerator()->generate('home');
    if (api_is_platform_admin() || api_is_session_admin() && $current_user_status_label == $statusname[STUDENT]) {
        if (!$user_is_anonymous) {
            if (api_global_admin_can_edit_admin($user_id)) {
                $result .= '<a href="' . $url . '?_switch_user='******'">' . Display::return_icon('login_as.png', get_lang('LoginAs')) . '</a>&nbsp;&nbsp;';
            } else {
                $result .= Display::return_icon('login_as_na.png', get_lang('LoginAs')) . '&nbsp;&nbsp;';
            }
        } else {
            $result .= Display::return_icon('login_as_na.png', get_lang('LoginAs')) . '&nbsp;&nbsp;';
        }
    } else {
        $result .= Display::return_icon('login_as_na.png', get_lang('LoginAs')) . '&nbsp;&nbsp;';
    }
    if ($current_user_status_label != $statusname[STUDENT]) {
        $result .= Display::return_icon('statistics_na.gif', get_lang('Reporting')) . '&nbsp;&nbsp;';
    } else {
        $result .= '<a href="../mySpace/myStudents.php?student=' . $user_id . '">' . Display::return_icon('statistics.gif', get_lang('Reporting')) . '</a>&nbsp;&nbsp;';
    }
    if (api_is_platform_admin(true)) {
        $editProfileUrl = Display::getProfileEditionLink($user_id, true);
        if (!$user_is_anonymous && api_global_admin_can_edit_admin($user_id, null, true)) {
            $result .= '<a href="' . $editProfileUrl . '">' . Display::return_icon('edit.png', get_lang('Edit'), array(), ICON_SIZE_SMALL) . '</a>&nbsp;';
        } else {
            $result .= Display::return_icon('edit_na.png', get_lang('Edit'), array(), ICON_SIZE_SMALL) . '</a>&nbsp;';
        }
    }
    $alloAssignSkill = api_is_platform_admin(false, true);
    if ($alloAssignSkill) {
        $result .= Display::url(Display::return_icon('skill-badges.png', get_lang('AssignSkill'), null, ICON_SIZE_SMALL), api_get_path(WEB_CODE_PATH) . 'badge/assign.php?' . http_build_query(['user' => $user_id]));
    }
    if ($is_admin) {
        $result .= Display::return_icon('admin_star.png', get_lang('IsAdministrator'), array('width' => ICON_SIZE_SMALL, 'heigth' => ICON_SIZE_SMALL));
    } else {
        $result .= Display::return_icon('admin_star_na.png', get_lang('IsNotAdministrator'));
    }
    // actions for assigning sessions, courses or users
    if (api_is_session_admin()) {
        /*if ($row[0] == api_get_user_id()) {
        			$result .= '<a href="dashboard_add_sessions_to_user.php?user='******'">'.Display::return_icon('view_more_stats.gif', get_lang('AssignSessions')).'</a>&nbsp;&nbsp;';
        		}*/
    } else {
        if ($current_user_status_label == $statusname[SESSIONADMIN]) {
            $result .= Display::url(Display::return_icon('view_more_stats.gif', get_lang('AssignSessions')), "dashboard_add_sessions_to_user.php?user={$user_id}");
        } else {
            if ($current_user_status_label == $statusname[DRH] || UserManager::is_admin($user_id) || $current_user_status_label == $statusname[STUDENT_BOSS]) {
                $result .= Display::url(Display::return_icon('user_subscribe_course.png', get_lang('AssignUsers'), '', ICON_SIZE_SMALL), "dashboard_add_users_to_user.php?user={$user_id}");
            }
            if ($current_user_status_label == $statusname[DRH] || UserManager::is_admin($user_id)) {
                $result .= Display::url(Display::return_icon('course_add.gif', get_lang('AssignCourses')), "dashboard_add_courses_to_user.php?user={$user_id}");
                $result .= Display::url(Display::return_icon('view_more_stats.gif', get_lang('AssignSessions')), "dashboard_add_sessions_to_user.php?user={$user_id}");
            }
        }
    }
    if (api_is_platform_admin()) {
        $result .= ' <a href="' . api_get_path(WEB_AJAX_PATH) . 'agenda.ajax.php?a=get_user_agenda&user_id=' . $user_id . '&modal_size=lg" class="agenda_opener ajax">' . Display::return_icon('calendar.png', get_lang('FreeBusyCalendar'), array(), ICON_SIZE_SMALL) . '</a>';
        $deleteAllowed = !api_get_configuration_value('deny_delete_users');
        if ($deleteAllowed) {
            if ($user_id != api_get_user_id() && !$user_is_anonymous && api_global_admin_can_edit_admin($user_id)) {
                // you cannot lock yourself out otherwise you could disable all the accounts including your own => everybody is locked out and nobody can change it anymore.
                $result .= ' <a href="user_list.php?action=delete_user&user_id=' . $user_id . '&' . $url_params . '&sec_token=' . $_SESSION['sec_token'] . '"  onclick="javascript:if(!confirm(' . "'" . addslashes(api_htmlentities(get_lang("ConfirmYourChoice"), ENT_QUOTES, $charset)) . "'" . ')) return false;">' . Display::return_icon('delete.png', get_lang('Delete'), array(), ICON_SIZE_SMALL) . '</a>';
            } else {
                $result .= Display::return_icon('delete_na.png', get_lang('Delete'), array(), ICON_SIZE_SMALL);
            }
        }
    }
    return $result;
}
Exemple #2
0
/**
 * Returns a path to a certain resource within the Chamilo area, specifyed through a parameter.
 * Also, this function provides conversion between path types, in this case the input path points inside the Chamilo area too.
 *
 * See $_configuration['course_folder'] in the configuration.php to alter the WEB_COURSE_PATH and SYS_COURSE_PATH parameters.
 * @param string $type              The requested path type (a defined constant), see the examples.
 * @param string $path (optional)   A path which type is to be converted. Also, it may be a defined constant for a path.
 * This parameter has meaning when $type parameter has one of the following values: TO_WEB, TO_SYS, TO_REL. Otherwise it is ignored.
 * @return string                   The requested path or the converted path.
 *
 * A terminology note:
 * The defined constants used by this function contain the abbreviations WEB, REL, SYS with the following meaning for types:
 * WEB - an absolute URL (we often call it web-path),
 * example: http://www.mychamilo.org/chamilo/courses/COURSE01/document/lesson01.html;
 *
 * REL - represents a semi-absolute URL - a web-path, which is relative to the root web-path of the server, without server's base,
 * example: /chamilo/courses/COURSE01/document/lesson01.html;
 *
 * SYS - represents an absolute path inside the scope of server's file system,
 * /var/www/chamilo/courses/COURSE01/document/lesson01.html or
 * C:/Inetpub/wwwroot/chamilo/courses/COURSE01/document/lesson01.html.
 *
 * In some abstract sense we can consider these three path types as absolute.
 *
 * Notes about the current behaviour model:
 * 1. Windows back-slashes are converted to slashes in the result.
 * 2. A semi-absolute web-path is detected by its leading slash. On Linux systems, absolute system paths start with
 * a slash too, so an additional check about presence of leading system server base is implemented. For example, the function is
 * able to distinguish type difference between /var/www/chamilo/courses/ (SYS) and /chamilo/courses/ (REL).
 * 3. The function api_get_path() returns only these three types of paths, which in some sense are absolute. The function has
 * no a mechanism for processing relative web/system paths, such as: lesson01.html, ./lesson01.html, ../css/my_styles.css.
 * It has not been identified as needed yet.
 * 4. Also, resolving the meta-symbols "." and ".." within paths has not been implemented, it is to be identified as needed.
 *
 * Example:
 * Assume that your server root is /var/www/ ,
 * Chamilo is installed in a sub folder chamilo/ and the URL of your campus is http://www.mychamilo.org
 * The other configuration parameters have not been changed.
 *
 * This is how we can get most used paths, for common purpose:
 * api_get_path(REL_PATH)                       /chamilo/
 * api_get_path(REL_COURSE_PATH)                /chamilo/courses/
 * api_get_path(REL_CODE_PATH)                  /chamilo/main/
 * api_get_path(REL_UPLOAD_PATH)                /chamilo/app/upload/
 * api_get_path(SYS_SERVER_ROOT_PATH)           /var/www/ - This is the physical folder where the system Chamilo has been placed. It is not always equal to $_SERVER['DOCUMENT_ROOT'].
 * api_get_path(SYS_PATH)                       /var/www/chamilo/
 * api_get_path(SYS_APP_PATH)                   /var/www/chamilo/app/
 * api_get_path(SYS_UPLOAD_PATH)                /var/www/chamilo/app/upload/
 * api_get_path(SYS_ARCHIVE_PATH)               /var/www/chamilo/app/cache
 * api_get_path(SYS_COURSE_PATH)                /var/www/chamilo/app/courses/
 * api_get_path(SYS_CSS_PATH)                   /var/www/chamilo/app/Resources/public/css
 * api_get_path(SYS_CODE_PATH)                  /var/www/chamilo/main/
 * api_get_path(INCLUDE_PATH)                   /var/www/chamilo/main/inc/
 * api_get_path(LIBRARY_PATH)                   /var/www/chamilo/main/inc/lib/
 * api_get_path(CONFIGURATION_PATH)             /var/www/chamilo/main/inc/conf/
 * api_get_path(SYS_LANG_PATH)                  /var/www/chamilo/main/lang/
 * api_get_path(SYS_PLUGIN_PATH)                /var/www/chamilo/plugin/
 * api_get_path(SYS_TEST_PATH)                  /var/www/chamilo/tests/
 * api_get_path(SYS_TEMPLATE_PATH)              /var/www/chamilo/main/template/
 * api_get_path(SYS_PUBLIC_PATH)                /var/www/chamilo/web/
 *
 * api_get_path(WEB_SERVER_ROOT_PATH)           http://www.mychamilo.org/
 * api_get_path(WEB_PATH)                       http://www.mychamilo.org/chamilo/
 * api_get_path(WEB_COURSE_PATH)                http://www.mychamilo.org/chamilo/courses/
 * api_get_path(WEB_CODE_PATH)                  http://www.mychamilo.org/chamilo/main/
 * api_get_path(WEB_PLUGIN_PATH)                http://www.mychamilo.org/chamilo/plugin/
 * api_get_path(WEB_ARCHIVE_PATH)               http://www.mychamilo.org/chamilo/app/cache/
 * api_get_path(WEB_IMG_PATH)                   http://www.mychamilo.org/chamilo/web/bundles/chamilocore/img/
 * api_get_path(SYS_IMG_PATH)                   /var/www/chamilo/web/bundles/chamilocore/img/
 * api_get_path(WEB_CSS_PATH)                   http://www.mychamilo.org/chamilo/web/css/
 * api_get_path(WEB_LIBRARY_PATH)               http://www.mychamilo.org/chamilo/main/inc/lib/
 * api_get_path(WEB_LIBRARY_JS_PATH)            http://www.mychamilo.org/chamilo/web/bundles/chamilocore/js
 * api_get_path(WEB_TEMPLATE_PATH)              http://www.mychamilo.org/chamilo/main/template/
 * api_get_path(WEB_UPLOAD_PATH)                http://www.mychamilo.org/chamilo/app/upload/
 * api_get_path(WEB_PUBLIC_PATH)                http://www.mychamilo.org/chamilo/web/
 *
 * This is how we retrieve paths of "registered" resource files (scripts, players, etc.):
 * api_get_path(TO_WEB, FLASH_PLAYER_AUDIO)     http://www.mychamilo.org/chamilo/main/inc/lib/mediaplayer/player.swf
 * api_get_path(TO_WEB, FLASH_PLAYER_VIDEO)     http://www.mychamilo.org/chamilo/main/inc/lib/mediaplayer/player.swf
 * api_get_path(TO_SYS, SCRIPT_SWFOBJECT)       /var/www/chamilo/main/inc/lib/swfobject/swfobject.js
 * api_get_path(TO_REL, SCRIPT_ASCIIMATHML)     /chamilo/main/inc/lib/asciimath/ASCIIMathML.js
 * ...
 *
 * We can convert arbitrary paths, that are not registered (no defined constant).
 * For guaranteed result, these paths should point inside the system Chamilo.
 * Some random examples:
 * api_get_path(TO_WEB, $_SERVER['REQUEST_URI'])
 * api_get_path(TO_SYS, $_SERVER['PHP_SELF'])
 * api_get_path(TO_REL, __FILE__)
 * ...
 */
function api_get_path($path_type, $path = null)
{
    static $paths = array(WEB_PATH => '', SYS_PATH => '', REL_PATH => '', WEB_SERVER_ROOT_PATH => '', SYS_SERVER_ROOT_PATH => '', WEB_COURSE_PATH => '', SYS_COURSE_PATH => '', REL_COURSE_PATH => '', REL_CODE_PATH => '', WEB_CODE_PATH => '', SYS_CODE_PATH => '', SYS_LANG_PATH => 'lang/', WEB_IMG_PATH => 'web/bundles/chamilocore/img/', SYS_IMG_PATH => 'web/bundles/chamilocore/img/', WEB_CSS_PATH => 'web/css/', SYS_CSS_PATH => 'app/Resources/public/css/', SYS_PLUGIN_PATH => 'plugin/', WEB_PLUGIN_PATH => 'plugin/', SYS_ARCHIVE_PATH => 'app/cache/', WEB_ARCHIVE_PATH => 'app/cache/', SYS_APP_PATH => 'app/', WEB_APP_PATH => 'app/', SYS_UPLOAD_PATH => 'app/upload/', REL_UPLOAD_PATH => 'app/upload/', INCLUDE_PATH => 'inc/', LIBRARY_PATH => 'inc/lib/', CONFIGURATION_PATH => 'app/config/', WEB_LIBRARY_PATH => 'inc/lib/', WEB_LIBRARY_JS_PATH => 'web/bundles/chamilocore/js/', WEB_AJAX_PATH => 'inc/ajax/', SYS_TEST_PATH => 'tests/', WEB_TEMPLATE_PATH => 'template/', WEB_UPLOAD_PATH => 'app/upload/', WEB_PUBLIC_PATH => 'web/', SYS_TEMPLATE_PATH => 'template/', SYS_PUBLIC_PATH => 'web/', WEB_FONTS_PATH => 'fonts/', SYS_FONTS_PATH => 'fonts/');
    static $resource_paths = array(FLASH_PLAYER_AUDIO => 'inc/lib/mediaplayer/player.swf', FLASH_PLAYER_VIDEO => 'inc/lib/mediaplayer/player.swf', SCRIPT_SWFOBJECT => 'inc/lib/swfobject/swfobject.js', SCRIPT_ASCIIMATHML => 'inc/lib/javascript/asciimath/ASCIIMathML.js', DRAWING_ASCIISVG => 'inc/lib/javascript/asciimath/d.svg');
    static $is_this_function_initialized;
    static $server_base_web;
    // No trailing slash.
    static $server_base_sys;
    // No trailing slash.
    static $root_rel;
    $root_web = Container::getUrlGenerator()->generate('home', [], \Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL);
    $rootDir = Container::getRootDir();
    // Configuration data for already installed system.
    $root_sys = $rootDir;
    $code_folder = 'main/';
    $course_folder = 'courses/';
    // Configuration data for already installed system.
    $load_new_config = false;
    // To avoid that the api_get_access_url() function fails since global.inc.php also calls the main_api.lib.php
    if ($path_type == WEB_PATH) {
        if (isset($_configuration['access_url']) && $_configuration['access_url'] != 1) {
            //we look into the DB the function api_get_access_url
            $url_info = api_get_access_url($_configuration['access_url']);
            $root_web = $url_info['active'] == 1 ? $url_info['url'] : $_configuration['root_web'];
            $load_new_config = true;
        }
    }
    if (!$is_this_function_initialized) {
        $root_rel = Container::getUrlAppend();
        // Dealing with trailing slashes.
        $root_web = api_add_trailing_slash($root_web);
        $root_sys = api_add_trailing_slash($root_sys);
        $root_rel = api_add_trailing_slash($root_rel);
        $code_folder = api_add_trailing_slash($code_folder);
        $course_folder = api_add_trailing_slash($course_folder);
        // Web server base and system server base.
        $server_base_web = preg_replace('@' . $root_rel . '$@', '', $root_web);
        // No trailing slash.
        $server_base_sys = preg_replace('@' . $root_rel . '$@', '', $root_sys);
        // No trailing slash.
        // Initialization of a table that contains common-purpose paths.
        $paths[WEB_PATH] = $root_web;
        $paths[SYS_PATH] = $root_sys;
        $paths[REL_PATH] = $root_rel;
        $paths[WEB_SERVER_ROOT_PATH] = $server_base_web . '/';
        $paths[SYS_SERVER_ROOT_PATH] = $server_base_sys . '/';
        $paths[WEB_COURSE_PATH] = $root_web . $course_folder;
        $paths[REL_COURSE_PATH] = $root_rel . $course_folder;
        $paths[REL_CODE_PATH] = $root_rel . $code_folder;
        $paths[WEB_CODE_PATH] = $root_web . $code_folder;
        $paths[SYS_CODE_PATH] = $root_sys . $code_folder;
        $paths[REL_UPLOAD_PATH] = $root_rel . $paths[SYS_UPLOAD_PATH];
        $paths[WEB_DEFAULT_COURSE_DOCUMENT_PATH] = $paths[WEB_CODE_PATH] . 'default_course_document/';
        $paths[REL_DEFAULT_COURSE_DOCUMENT_PATH] = $paths[REL_PATH] . 'main/default_course_document/';
        // Now we can switch into api_get_path() "terminology".
        $paths[SYS_LANG_PATH] = $paths[SYS_CODE_PATH] . $paths[SYS_LANG_PATH];
        $paths[SYS_APP_PATH] = $paths[SYS_PATH] . $paths[SYS_APP_PATH];
        $paths[WEB_APP_PATH] = $paths[WEB_PATH] . $paths[WEB_APP_PATH];
        $paths[SYS_UPLOAD_PATH] = $paths[SYS_PATH] . $paths[SYS_UPLOAD_PATH];
        $paths[SYS_PLUGIN_PATH] = $paths[SYS_PATH] . $paths[SYS_PLUGIN_PATH];
        $paths[SYS_ARCHIVE_PATH] = Container::getTempDir();
        $paths[SYS_TEST_PATH] = $paths[SYS_PATH] . $paths[SYS_TEST_PATH];
        $paths[SYS_TEMPLATE_PATH] = $paths[SYS_CODE_PATH] . $paths[SYS_TEMPLATE_PATH];
        $paths[SYS_PUBLIC_PATH] = $paths[SYS_PATH] . $paths[SYS_PUBLIC_PATH];
        $paths[SYS_CSS_PATH] = $paths[SYS_PATH] . $paths[SYS_CSS_PATH];
        $paths[SYS_FONTS_PATH] = $paths[SYS_CODE_PATH] . $paths[SYS_FONTS_PATH];
        $paths[WEB_CSS_PATH] = $paths[WEB_PATH] . $paths[WEB_CSS_PATH];
        $bundleWebPath = Container::getAsset()->getUrl('bundles/chamilocore/');
        $paths[WEB_IMG_PATH] = $bundleWebPath . 'img/';
        $paths[SYS_IMG_PATH] = $paths[SYS_PATH] . $paths[SYS_IMG_PATH];
        $paths[WEB_LIBRARY_PATH] = $paths[WEB_CODE_PATH] . $paths[WEB_LIBRARY_PATH];
        $paths[WEB_LIBRARY_JS_PATH] = $bundleWebPath . 'js/';
        $paths[WEB_AJAX_PATH] = $paths[WEB_CODE_PATH] . $paths[WEB_AJAX_PATH];
        $paths[WEB_FONTS_PATH] = $paths[WEB_CODE_PATH] . $paths[WEB_FONTS_PATH];
        $paths[WEB_PLUGIN_PATH] = $paths[WEB_PATH] . $paths[WEB_PLUGIN_PATH];
        $paths[WEB_ARCHIVE_PATH] = $paths[WEB_PATH] . $paths[WEB_ARCHIVE_PATH];
        $paths[WEB_TEMPLATE_PATH] = $paths[WEB_CODE_PATH] . $paths[WEB_TEMPLATE_PATH];
        $paths[WEB_UPLOAD_PATH] = $paths[WEB_PATH] . $paths[WEB_UPLOAD_PATH];
        $paths[WEB_PUBLIC_PATH] = $paths[WEB_PATH] . $paths[WEB_PUBLIC_PATH];
        $paths[INCLUDE_PATH] = $paths[SYS_CODE_PATH] . $paths[INCLUDE_PATH];
        $paths[LIBRARY_PATH] = $paths[SYS_CODE_PATH] . $paths[LIBRARY_PATH];
        $paths[CONFIGURATION_PATH] = $paths[SYS_PATH] . $paths[CONFIGURATION_PATH];
        $paths[SYS_COURSE_PATH] = Container::getCourseDir();
        $is_this_function_initialized = true;
    } else {
        if ($load_new_config) {
            //  Redefining variables to work well with the "multiple url" feature
            // All web paths need to be here
            $web_paths = array(WEB_PATH => '', WEB_SERVER_ROOT_PATH => '', WEB_COURSE_PATH => '', WEB_CODE_PATH => '', WEB_IMG_PATH => 'img/', WEB_CSS_PATH => 'web/css/', WEB_PLUGIN_PATH => 'plugin/', WEB_ARCHIVE_PATH => 'archive/', WEB_LIBRARY_PATH => 'inc/lib/', WEB_AJAX_PATH => 'inc/ajax/');
            $root_web = api_add_trailing_slash($root_web);
            // Web server base and system server base.
            $server_base_web = preg_replace('@' . $root_rel . '$@', '', $root_web);
            // No trailing slash.
            // Redefine root webs
            $paths[WEB_PATH] = $root_web;
            $paths[WEB_SERVER_ROOT_PATH] = $server_base_web . '/';
            $paths[WEB_COURSE_PATH] = $root_web . $course_folder;
            $paths[WEB_CODE_PATH] = $root_web . $code_folder;
            $paths[WEB_IMG_PATH] = $paths[WEB_CODE_PATH] . $web_paths[WEB_IMG_PATH];
            $paths[WEB_CSS_PATH] = $paths[WEB_PATH] . $web_paths[WEB_CSS_PATH];
            $paths[WEB_PLUGIN_PATH] = $paths[WEB_PATH] . $web_paths[WEB_PLUGIN_PATH];
            $paths[WEB_ARCHIVE_PATH] = $paths[WEB_PATH] . $web_paths[WEB_ARCHIVE_PATH];
            $paths[WEB_LIBRARY_PATH] = $paths[WEB_CODE_PATH] . $web_paths[WEB_LIBRARY_PATH];
            $paths[WEB_AJAX_PATH] = $paths[WEB_CODE_PATH] . $web_paths[WEB_AJAX_PATH];
            $paths[WEB_FONTS_PATH] = $paths[WEB_CODE_PATH] . $paths[WEB_FONTS_PATH];
        }
    }
    // Shallow purification and validation of input parameters.
    $path_type = trim($path_type);
    $path = trim($path);
    if (empty($path_type)) {
        return null;
    }
    // Retrieving a common-purpose path.
    if (isset($paths[$path_type])) {
        return $paths[$path_type];
    }
    // Retrieving a specific resource path.
    if (isset($resource_paths[$path])) {
        switch ($path_type) {
            case TO_WEB:
                return $paths[WEB_CODE_PATH] . $resource_paths[$path];
            case TO_SYS:
                return $paths[SYS_CODE_PATH] . $resource_paths[$path];
            case TO_REL:
                return $paths[REL_CODE_PATH] . $resource_paths[$path];
            default:
                return null;
        }
    }
    // Common-purpose paths as a second parameter - recognition.
    if (isset($paths[$path])) {
        $path = $paths[$path];
    }
    // Second purification.
    // Replacing Windows back slashes.
    $path = str_replace('\\', '/', $path);
    // Query strings sometimes mighth wrongly appear in non-URLs.
    // Let us check remove them from all types of paths.
    if (($pos = strpos($path, '?')) !== false) {
        $path = substr($path, 0, $pos);
    }
    // Detection of the input path type. Conversion to semi-absolute type ( /chamilo/main/inc/.... ).
    $courseCode = api_get_course_id();
    if (preg_match(VALID_WEB_PATH, $path)) {
        // A special case: When a URL points to the document download script directly, without
        // mod-rewrite translation, we have to translate it into an "ordinary" web path.
        // For example:
        // http://localhost/chamilo/main/document/download.php?doc_url=/image.png&cDir=/
        // becomes
        // http://localhost/chamilo/courses/TEST/document/image.png
        // TEST is a course directory name, so called "system course code".
        if (strpos($path, 'download.php') !== false) {
            // Fast detection first.
            $path = urldecode($path);
            if (preg_match('/(.*)main\\/document\\/download.php\\?doc_url=\\/(.*)&cDir=\\/(.*)?/', $path, $matches)) {
                $sys_course_code = isset($courseCode) ? $courseCode : '{SYS_COURSE_CODE}';
                // No, then use a fake code, it may be processed later.
                $path = $matches[1] . 'courses/' . $sys_course_code . '/document/' . str_replace('//', '/', $matches[3] . '/' . $matches[2]);
            }
        }
        // Replacement of the present web server base with a slash '/'.
        $path = preg_replace(VALID_WEB_SERVER_BASE, '/', $path);
    } elseif (strpos($path, $server_base_sys) === 0) {
        $path = preg_replace('@^' . $server_base_sys . '@', '', $path);
    } elseif (strpos($path, '/') === 0) {
        // Leading slash - we assume that this path is semi-absolute (REL),
        // then path is left without furthes modifications.
    } else {
        return null;
        // Probably implementation of this case won't be needed.
    }
    // Path now is semi-absolute. It is convenient at this moment repeated slashes to be removed.
    $path = preg_replace(REPEATED_SLASHES_PURIFIER, '/', $path);
    // Path conversion to the requested type.
    switch ($path_type) {
        case TO_WEB:
            return $server_base_web . $path;
        case TO_SYS:
            return $server_base_sys . $path;
        case TO_REL:
            return $path;
    }
    return null;
}
Exemple #3
0
/**
 * Returns a path to a certain resource within the Chamilo area, specifyed through a parameter.
 * Also, this function provides conversion between path types, in this case the input path points inside the Chamilo area too.
 *
 * See $_configuration['course_folder'] in the configuration.php to alter the WEB_COURSE_PATH and SYS_COURSE_PATH parameters.
 * @param string $type              The requested path type (a defined constant), see the examples.
 * @param string $path (optional)   A path which type is to be converted. Also, it may be a defined constant for a path.
 * This parameter has meaning when $type parameter has one of the following values: TO_WEB, TO_SYS, TO_REL. Otherwise it is ignored.
 * @return string                   The requested path or the converted path.
 *
 * A terminology note:
 * The defined constants used by this function contain the abbreviations WEB, REL, SYS with the following meaning for types:
 * WEB - an absolute URL (we often call it web-path),
 * example: http://www.mychamilo.org/chamilo/courses/COURSE01/document/lesson01.html;
 *
 * REL - represents a semi-absolute URL - a web-path, which is relative to the root web-path of the server, without server's base,
 * example: /chamilo/courses/COURSE01/document/lesson01.html;
 *
 * SYS - represents an absolute path inside the scope of server's file system,
 * /var/www/chamilo/courses/COURSE01/document/lesson01.html or
 * C:/Inetpub/wwwroot/chamilo/courses/COURSE01/document/lesson01.html.
 *
 * In some abstract sense we can consider these three path types as absolute.
 *
 * Notes about the current behaviour model:
 * 1. Windows back-slashes are converted to slashes in the result.
 * 2. A semi-absolute web-path is detected by its leading slash. On Linux systems, absolute system paths start with
 * a slash too, so an additional check about presense of leading system server base is implemented. For example, the function is
 * able to distinguish type difference between /var/www/chamilo/courses/ (SYS) and /chamilo/courses/ (REL).
 * 3. The function api_get_path() returns only these three types of paths, which in some sense are absolute. The function has
 * no a mechanism for processing relative web/system paths, such as: lesson01.html, ./lesson01.html, ../css/my_styles.css.
 * It has not been identified as needed yet.
 * 4. Also, resolving the meta-symbols "." and ".." withiin paths has not been implemented, it is to be identified as needed.
 *
 * Example:
 * Assume that your server root is /var/www/ , Chamilo is installed in a subfolder chamilo/ and the URL of your campus is http://www.mychamilo.org
 * The other configuration paramaters have not been changed.
 *
 * This is how we can retireve mosth used paths, for common purpose:
 * api_get_path(REL_PATH)                       /chamilo/
 * api_get_path(REL_COURSE_PATH)                /chamilo/courses/
 * api_get_path(REL_CODE_PATH)                  /chamilo/main/
 * api_get_path(SYS_SERVER_ROOT_PATH)           /var/www/ - This is the physical folder where the system Chamilo has been placed. It is not always equal to $_SERVER['DOCUMENT_ROOT'].
 * api_get_path(SYS_PATH)                       /var/www/chamilo/
 *
 * api_get_path(SYS_ARCHIVE_PATH)               /var/www/chamilo/temp/
 * api_get_path(SYS_LOG_PATH)                   /var/www/chamilo/logs/
 * api_get_path(SYS_DATA_PATH)                  /var/www/chamilo/data/
 * api_get_path(SYS_CONFIG_PATH)                /var/www/chamilo/config/
 * api_get_path(SYS_WEB_PATH)                   /var/www/chamilo/web/
 *
 * api_get_path(SYS_COURSE_PATH)                /var/www/chamilo/data/courses/
 * api_get_path(SYS_CODE_PATH)                  /var/www/chamilo/main/
 * api_get_path(SYS_CSS_PATH)                   /var/www/chamilo/main/css
 * api_get_path(INCLUDE_PATH)                   /var/www/chamilo/main/inc/
 * api_get_path(LIBRARY_PATH)                   /var/www/chamilo/main/inc/lib/
 * api_get_path(SYS_LIBRARY_JS_PATH)            /var/www/chamilo/web/Chamilo/js
 * api_get_path(CONFIGURATION_PATH)             /var/www/chamilo/main/inc/conf/
 * api_get_path(SYS_LANG_PATH)                  /var/www/chamilo/main/lang/
 * api_get_path(SYS_PLUGIN_PATH)                /var/www/chamilo/plugin/
 * api_get_path(SYS_TEST_PATH)                  /var/www/chamilo/tests/
 * api_get_path(SYS_TEMPLATE_PATH)              /var/www/chamilo/main/template/
 *
 * api_get_path(WEB_SERVER_ROOT_PATH)           http://www.mychamilo.org/
 *
 * api_get_path(WEB_PUBLIC_PATH)                http://www.mychamilo.org/chamilo/web/
 * api_get_path(WEB_PATH)                       http://www.mychamilo.org/chamilo/
 * api_get_path(WEB_COURSE_PATH)                http://www.mychamilo.org/chamilo/courses/
 * api_get_path(WEB_CODE_PATH)                  http://www.mychamilo.org/chamilo/main/
 * api_get_path(WEB_PLUGIN_PATH)                http://www.mychamilo.org/chamilo/plugin/
 * api_get_path(WEB_ARCHIVE_PATH)               http://www.mychamilo.org/chamilo/archive/
 * api_get_path(WEB_IMG_PATH)                   http://www.mychamilo.org/chamilo/web/chamilo/img/
 * api_get_path(SYS_IMG_PATH)                   /var/www/chamilo/web/bundles/chamilocore/img/
 *
 * api_get_path(WEB_CSS_PATH)                   http://www.mychamilo.org/chamilo/main/css/
 * api_get_path(WEB_LIBRARY_PATH)               http://www.mychamilo.org/chamilo/main/inc/lib/
 * api_get_path(WEB_LIBRARY_JS_PATH)            http://www.mychamilo.org/chamilo/web/Chamilo/javascript
 * api_get_path(WEB_TEMPLATE_PATH)              http://www.mychamilo.org/chamilo/main/template/
 *
 *
 * We can convert arbitrary paths, that are not registered (no defined constant).
 * For guaranteed result, these paths should point inside the system Chamilo.
 * Some random examples:
 * api_get_path(TO_WEB, $_SERVER['REQUEST_URI'])
 * api_get_path(TO_SYS, $_SERVER['PHP_SELF'])
 * api_get_path(TO_REL, __FILE__)
 * ...
 */
function api_get_path($path_type, $path = null)
{
    $paths = array(SYS_DATA_PATH => 'data/', SYS_WEB_PATH => 'web/', SYS_CONFIG_PATH => 'config/', SYS_LOG_PATH => 'logs/', WEB_DATA_COURSE_PATH => 'courses/', WEB_DATA_PATH => '/', SYS_COURSE_PATH => 'data/', SYS_CSS_PATH => 'bundles/chamilocore/css/', SYS_LANG_PATH => 'lang/', WEB_IMG_PATH => 'bundles/chamilocore/img/', SYS_IMG_PATH => 'web/bundles/chamilocore/img/', WEB_CSS_PATH => 'bundles/chamilocore/css/', SYS_PLUGIN_PATH => 'plugin/', WEB_PLUGIN_PATH => 'plugin/', WEB_ARCHIVE_PATH => 'temp/', INCLUDE_PATH => 'inc/', LIBRARY_PATH => 'inc/lib/', SYS_LIBRARY_JS_PATH => 'bundles/chamilocore/js/', CONFIGURATION_PATH => 'inc/conf/', WEB_LIBRARY_PATH => 'inc/lib/', WEB_LIBRARY_JS_PATH => 'bundles/chamilocore/js/', WEB_AJAX_PATH => 'inc/ajax/', SYS_TEST_PATH => 'tests/', WEB_TEMPLATE_PATH => 'template/', SYS_TEMPLATE_PATH => 'template/');
    $root_web = Container::getUrlGenerator()->generate('home');
    $rootDir = Container::getRootDir();
    // Configuration data for already installed system.
    $root_sys = $rootDir;
    $code_folder = 'main/';
    //$course_folder  = isset($_configuration['course_folder']) ? $_configuration['course_folder'] : null;
    $course_folder = "courses/";
    // Dealing with trailing slashes.
    $root_web = api_add_trailing_slash($root_web);
    $root_sys = api_add_trailing_slash($root_sys);
    //$root_rel       = api_add_trailing_slash($root_rel);
    $code_folder = api_add_trailing_slash($code_folder);
    $course_folder = api_add_trailing_slash($course_folder);
    $root_rel = null;
    // Initialization of a table that contains common-purpose paths.
    $paths[WEB_PATH] = $root_web;
    $webPathNoDev = str_replace('app_dev.php/', '', $paths[WEB_PATH]);
    $paths[WEB_PUBLIC_PATH] = $root_web;
    $paths[SYS_PATH] = $root_sys;
    // Update data path to get it from config file if defined
    $paths[SYS_DATA_PATH] = Container::getDataDir();
    $paths[SYS_LOG_PATH] = Container::getLogDir();
    $paths[SYS_CONFIG_PATH] = Container::getConfigDir();
    $paths[SYS_COURSE_PATH] = Container::getCourseDir();
    $paths[SYS_DEFAULT_COURSE_DOCUMENT_PATH] = $paths[SYS_DATA_PATH] . 'default_course_document/';
    $paths[SYS_WEB_PATH] = $root_sys . 'web/';
    $paths[REL_PATH] = $root_rel;
    $paths[WEB_DATA_PATH] = $paths[WEB_PUBLIC_PATH] . 'data/';
    $paths[WEB_COURSE_PATH] = $root_web . $course_folder;
    $paths[WEB_DATA_COURSE_PATH] = $paths[WEB_DATA_PATH] . $course_folder;
    $paths[REL_COURSE_PATH] = $root_rel . $course_folder;
    $paths[REL_CODE_PATH] = $root_rel . $code_folder;
    $paths[WEB_CODE_PATH] = $root_web . $code_folder;
    $paths[REL_DATA_PATH] = $root_rel . 'data/';
    $paths[WEB_DEFAULT_COURSE_DOCUMENT_PATH] = $paths[WEB_DATA_PATH] . 'default_course_document/';
    $paths[REL_DEFAULT_COURSE_DOCUMENT_PATH] = $paths[REL_DATA_PATH] . 'default_course_document/';
    $paths[SYS_CODE_PATH] = $root_sys . $code_folder;
    // Now we can switch into api_get_path() "terminology".
    $paths[SYS_LANG_PATH] = $paths[SYS_CODE_PATH] . $paths[SYS_LANG_PATH];
    $paths[SYS_PLUGIN_PATH] = $paths[SYS_PATH] . $paths[SYS_PLUGIN_PATH];
    $paths[SYS_ARCHIVE_PATH] = Container::getTempDir();
    $paths[SYS_TEST_PATH] = $paths[SYS_PATH] . $paths[SYS_TEST_PATH];
    $paths[SYS_TEMPLATE_PATH] = $paths[SYS_CODE_PATH] . $paths[SYS_TEMPLATE_PATH];
    $paths[SYS_CSS_PATH] = $paths[SYS_PATH] . $paths[SYS_CSS_PATH];
    $paths[WEB_CSS_PATH] = $paths[WEB_PATH] . $paths[WEB_CSS_PATH];
    $paths[WEB_IMG_PATH] = $paths[WEB_PATH] . $paths[WEB_IMG_PATH];
    $paths[SYS_IMG_PATH] = $paths[SYS_PATH] . $paths[SYS_IMG_PATH];
    $paths[WEB_LIBRARY_PATH] = $paths[WEB_CODE_PATH] . $paths[WEB_LIBRARY_PATH];
    $paths[WEB_LIBRARY_JS_PATH] = $webPathNoDev . $paths[WEB_LIBRARY_JS_PATH];
    $paths[WEB_AJAX_PATH] = $paths[WEB_PUBLIC_PATH] . 'main/' . $paths[WEB_AJAX_PATH];
    $paths[WEB_PLUGIN_PATH] = $paths[WEB_PATH] . $paths[WEB_PLUGIN_PATH];
    $paths[WEB_ARCHIVE_PATH] = $paths[WEB_PATH] . $paths[WEB_ARCHIVE_PATH];
    $paths[WEB_TEMPLATE_PATH] = $paths[WEB_CODE_PATH] . $paths[WEB_TEMPLATE_PATH];
    $paths[INCLUDE_PATH] = $paths[SYS_CODE_PATH] . $paths[INCLUDE_PATH];
    $paths[LIBRARY_PATH] = $paths[SYS_CODE_PATH] . $paths[LIBRARY_PATH];
    $paths[SYS_LIBRARY_JS_PATH] = $paths[SYS_PATH] . $paths[SYS_LIBRARY_JS_PATH];
    $paths[CONFIGURATION_PATH] = $paths[SYS_CODE_PATH] . $paths[CONFIGURATION_PATH];
    // Path conversion to the requested type.
    if (empty($path_type)) {
        return null;
    }
    if (isset($paths[$path_type])) {
        return $paths[$path_type];
    }
    return null;
}