5 * Gforge cvsweb php wrapper
7 * Copyright 2003-2004 (c) Gforge
14 require_once('pre.php'); // Initial db and session library, opens session
15 require_once('www/scm/include/scm_utils.php');
21 // content types with html output
22 $supportedContentTypes = array('text/html', 'text/x-cvsweb-markup');
24 $plainTextDiffTypes = array('c', 's', 'u', '');
26 $contentType = 'text/html';
28 // we analyze the query to find the needed information
29 // this will allow us to determine the project name and the content type
30 if(getStringFromGet('cvsroot') && strpos(getStringFromGet('cvsroot'), ';') === false) {
31 $projectName = getStringFromGet('cvsroot');
32 if(getStringFromGet('r1') && getStringFromGet('r2') && in_array(getStringFromGet('f'), $plainTextDiffTypes)) {
33 $contentType = 'text/plain';
36 $queryString = getStringFromServer('QUERY_STRING');
37 if(preg_match_all('/[;]?([^\?;=]+)=([^;]+)/', $queryString, $matches, PREG_SET_ORDER)) {
38 for($i = 0, $size = sizeof($matches); $i < $size; $i++) {
39 $query[$matches[$i][1]] = urldecode($matches[$i][2]);
41 $projectName = $query['cvsroot'];
42 if(isset($query['content-type'])) {
43 $contentType = $query['content-type'];
45 if(isset($query['r1']) && isset($query['r2']) && (!isset($query['f']) || in_array($query['f'], $plainTextDiffTypes))) {
46 $contentType = 'text/plain';
50 // remove eventual leading /cvsroot/ or cvsroot/
51 $projectName = ereg_replace('^..[^/]*/','', $projectName);
53 // we found a project name in the query
55 $Group =& group_get_object_by_name($projectName);
56 if (!$Group || !is_object($Group) || $Group->isError()) {
59 if (!$Group->usesSCM()) {
60 exit_error('Error',$Language->getText('scm_index','error_this_project_has_turned_off'));
63 // check if the scm_box is located in another server
64 $scm_box = $Group->getSCMBox();
65 //$external_scm = (gethostbyname($sys_default_domain) != gethostbyname($scm_box));
66 $external_scm = !$sys_scm_single_host;
68 if (session_loggedin()) {
69 if (user_ismember($Group->getID())) {
70 $perm = & $Group->getPermission(session_get_user());
72 if (!($perm && is_object($perm) && $perm->isCVSReader()) && !$Group->enableAnonSCM()) {
73 exit_permission_denied();
75 } else if (!$Group->enableAnonSCM()) {
76 exit_permission_denied();
79 } else if (!$Group->enableAnonSCM()) { // user is not logged in... check if group accepts anonymous CVS
80 exit_permission_denied();
83 // User has access to the CVS... check for valid script
84 // (only if we're working on a local CVS server, if the CVS server is external the variable
85 // $sys_path_to_scmweb points to the path of the cvsweb script on the remote server)
86 if (!isset($GLOBALS['sys_path_to_scmweb']) || (!$external_scm && !is_file($GLOBALS['sys_path_to_scmweb'].'/cvsweb'))) {
87 exit_error('Error',"cvsweb script doesn't exist");
90 // should we output html ?
91 $isHtml = in_array($contentType, $supportedContentTypes);
93 // If we are accessing an external SCM box, execute the cvsweb script remotely and
96 //$server_script = "/cgi-bin/cvsweb";
98 $scmweb = $GLOBALS["sys_path_to_scmweb"];
99 // remove trailing slash
100 $scmweb = preg_replace("/\\/\$/", "", $scmweb);
102 $server_script = $scmweb."/cvsweb";
103 // remove leading / (if any)
104 $server_script = preg_replace("/^\\//", "", $server_script);
106 // pass the parameters passed to this script to the remote script in the same fashion
107 $script_url = "http://".$scm_box."/".$server_script.$_SERVER["PATH_INFO"]."?".$_SERVER["QUERY_STRING"];
108 $fh = @fopen($script_url, "r");
110 exit_error('Error', 'Could not open script <b>'.$script_url.'</b>.');
113 // start reading the output of the script (in 8k chunks)
116 $content .= fread($fh, 8192);
120 // Now, we must replace the occurencies of $server_script with this script
121 // (do this only of outputting HTML)
122 // We must do this because we can't pass the environment variable SCRIPT_NAME
123 // to the cvsweb script (maybe this can be fixed in the future?)
124 $content = str_replace("/".$server_script, $_SERVER["SCRIPT_NAME"], $content);
128 // SCM Box is the same server as this... execute the cvsweb script locally
130 // call to CVSWeb cgi. We use environment variables to pass parameters to the cgi.
131 passthru('PHP_WRAPPER="1" SCRIPT_NAME="'.getStringFromServer('SCRIPT_NAME').'" PATH_INFO="'.getStringFromServer('PATH_INFO').'" QUERY_STRING="'.getStringFromServer('QUERY_STRING').'" '.$GLOBALS['sys_path_to_scmweb'].'/cvsweb 2>&1');
132 $content = ob_get_contents();
138 scm_header(array('title'=>$Language->getText('scm_index','cvs_repository'),'group'=>$Group->getID()));
139 echo '<div id="cvsweb">';
141 header("Content-type: $contentType" );
144 // if we output html and we found the mbstring extension, we should try to encode the output of CVSWeb in UTF-8
145 if($isHtml && extension_loaded('mbstring')) {
146 $encoding = mb_detect_encoding($content);
147 if($encoding != 'UTF-8') {
148 $content = mb_convert_encoding($content, 'UTF-8', $encoding);