Esempio n. 1
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;
}