6 * The rest Copyright 2002-2005 (c) GForge Team
11 * This file is part of GForge.
13 * GForge is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * GForge is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with GForge; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 require ('squal_pre.php');
29 require ('common/include/cron_utils.php');
30 require_once('common/mail/MailingList.class');
31 require_once('common/mail/MailingListFactory.class');
33 require_once('common/include/SCM.class') ;
35 setup_plugin_manager () ;
38 // Some OSs do not allow root to do a commit
39 // so having this script do a proper checkout/commit is not possible
41 $scmcvs_proper_commit_loginfo=false;
44 * Retrieve a file into a temporary directory from a CVS server
46 * @param String $repos Repository Name
47 * @param String $file File Name
49 * return String the FileName in the working repository
51 function checkout_cvs_file($repos,$file) {
52 //echo "$repos,$file\n";
53 global $scmcvs_proper_commit_loginfo;
54 if (!$scmcvs_proper_commit_loginfo) {
55 return $repos.'/'.$file;
57 $actual_dir = getcwd();
58 $tempdirname = tempnam("/tmp","cvstracker");
61 if (!unlink($tempdirname))
64 // Create the temporary directory and returns its name.
65 if (!mkdir($tempdirname))
68 if (!chdir($tempdirname))
70 system("cvs -d ".$repos." co ".$file);
73 return $tempdirname."/".$file;
77 * commit_cvs_file commit a file to the repository
79 * @param String $repos Repository
80 * @param String $file to commit
81 * @param String $message to commit
83 function commit_cvs_file($repos,$file,$message="Automatic updated by cvstracker") {
84 global $scmcvs_proper_commit_loginfo;
85 if (!$scmcvs_proper_commit_loginfo) {
88 $actual_dir = getcwd();
89 chdir(dirname($file));
90 system("cvs -d ".$repos." ci -m \"".$message."\" ".basename($file));
91 // unlink (basename($file));
96 * release_cvs_file - Remove the file that was checked out from cvs
97 * @see checkout_cvs_file
99 function release_cvs_file($file) {
100 global $scmcvs_proper_commit_loginfo;
101 if (!$scmcvs_proper_commit_loginfo) {
104 // $file is something like /tmp/(tmp_dir)/path/to/file
105 // we must delete /tmp/tmp_dir
106 if (!preg_match("/^(\\/tmp\\/[^\\/]*)\\/.* /", $file, $result)) { // Make sure the dir is under /tmp
107 echo "Trying to release a directory not in /tmp. Skipping...";
112 // this shouldn't happen... but add it as a security checke
113 if (util_is_root_dir($dir)) {
114 echo "Trying to delete root dir. Skipping...";
117 $dir = escapeshellarg($dir);
118 system("rm -rf ".$dir);
121 function cvs_write_file($filePath, $content, $append=1) {
123 $file = fopen($filePath, 'a');
125 $file = fopen($filePath, 'w');
127 flock($file, LOCK_EX);
128 if(!empty($content)) {
129 fwrite($file, $content);
131 flock($file, LOCK_UN);
136 *add_sync_mail write to /CVSROOT/loginfo unix_name-commits@lists.gforge.company.com
138 *@param $unix_group_name Name Group
142 function add_sync_mail($unix_group_name) {
144 global $sys_lists_host, $cvsdir_prefix, $cvs_binary_version,$sys_plugins_path;
145 $loginfo_file=$cvsdir_prefix.'/'.$unix_group_name.'/CVSROOT/loginfo';
147 if (!$loginfo_file) {
148 echo "Couldn't get loginfo for $unix_group_name";
152 $content = file_get_contents ($loginfo_file);
153 if ( strstr($content, "syncmail") == FALSE) {
154 // echo $unix_group_name.":Syncmail not found in loginfo.Adding\n";
155 if ( $cvs_binary_version == "1.11" ) {
156 $pathsyncmail = "DEFAULT ".
157 $sys_plugins_path."/cvssyncmail/bin/syncmail -u %{sVv} ".
158 $unix_group_name."-commits@".$sys_lists_host;
160 $pathsyncmail = "DEFAULT ".
161 $sys_plugins_path."/cvssyncmail/bin/syncmail -u %p %{sVv} ".
162 $unix_group_name."-commits@".$sys_lists_host;
164 $content = "\n#BEGIN Added by cvs.php script\n".
165 $pathsyncmail. "\n#END Added by cvs.php script\n";
166 $loginfo_file = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/loginfo');
167 if(is_file($loginfo_file)){
168 //echo $unix_group_name.":About to write the lines\n";
169 cvs_write_file($loginfo_file, $content, 1);
171 commit_cvs_file($cvsdir_prefix."/".$unix_group_name,$loginfo_file);
172 release_cvs_file($loginfo_file);
174 // echo "Syncmail Found!\n";
179 * Function to add cvstracker lines to a loginfo file
180 * @param string the unix_group_name
183 function add_cvstracker($unix_group_name) {
184 global $cvsdir_prefix, $sys_plugins_path, $cvs_binary_version;
185 $loginfo_file=$cvsdir_prefix.'/'.$unix_group_name.'/CVSROOT/loginfo';
187 if (!$loginfo_file) {
188 echo "Couldn't get loginfo for $unix_group_name";
192 $content = file_get_contents ($loginfo_file);
193 if ( strstr($content, "cvstracker") == FALSE) {
194 $content = "\n# BEGIN added by gforge-plugin-cvstracker";
195 if ( $cvs_binary_version == "1.11" ) {
196 $content .= "\nALL ( php -q -d include_path=".ini_get('include_path').
197 " ".$sys_plugins_path."/cvstracker/bin/post.php".
198 " ".$unix_group_name." %{sVv} )";
199 } else { //it's version 1.12
200 $content .= "\nALL ( php -q -d include_path=".ini_get('include_path').
201 " ".$sys_plugins_path."/cvstracker/bin/post.php".
204 $content .= "\n# END added by gforge-plugin-cvstracker";
206 $loginfo_file = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/loginfo');
207 if(is_file($loginfo_file)){
208 //echo $unix_group_name.":About to write the lines\n";
209 cvs_write_file($loginfo_file, $content, 1);
211 commit_cvs_file($cvsdir_prefix."/".$unix_group_name,$loginfo_file);
212 release_cvs_file($loginfo_file);
214 // echo "cvstracker Found!\n";
217 // now make sure that if cvs version is 1.12, "UseNewInfoFmtStrings=yes" line
218 // MUST be present in CVSROOT/config, or else cvstracker won't work
219 if ($cvs_binary_version == "1.12") {
220 $config_file = $loginfo_file=$cvsdir_prefix.'/'.$unix_group_name.'/CVSROOT/config';
221 if (!is_file($config_file)) {
222 echo "Couldn't get CVSROOT/config for $unix_group_name";
226 $content = file_get_contents($config_file);
227 if (!preg_match("/UseNewInfoFmtStrings=yes/i", $content)) {
228 // file must be modified
229 $config_file = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/config');
230 if (is_file($config_file)) {
231 $lines = file($config_file);
233 foreach ($lines as $line) {
234 if (!preg_match("/UseNewInfoFmtStrings/i", $line)) { // maybe it was set to "no"?
235 $newlines[] = trim($line);
238 $newlines[] = "UseNewInfoFmtStrings=yes"; // add the required line at the end
240 $content = implode("\n", $newlines);
241 cvs_write_file($config_file, $content, 0);
242 commit_cvs_file($cvsdir_prefix."/".$unix_group_name, $config_file);
243 release_cvs_file($config_file);
248 function add_acl_check($unix_group_name) {
249 global $cvsdir_prefix, $cvs_binary_version;
251 $commitinfofile = $cvsdir_prefix."/".$unix_group_name.'/CVSROOT/commitinfo';
253 $content = file_get_contents ($commitinfofile);
254 if ( strstr($content, "aclcheck") == FALSE) {
256 $commitinfofile = checkout_cvs_file($cvsdir_prefix.'/'.$unix_group_name,'CVSROOT/commitinfo');
257 if ( $cvs_binary_version == "1.11" ) {
258 $aclcheck = "\n#BEGIN adding cvs acl check".
259 "\nALL php -q -d include_path=".ini_get('include_path').
260 " ".$GLOBALS['sys_plugins_path']."/scmcvs/bin/aclcheck.php ".$cvsdir_prefix."/".$unix_group_name.
261 "\n#END adding cvs acl check\n";
262 } else { //it's version 1.12
263 $aclcheck = "\n#BEGIN adding cvs acl check".
264 "\nALL php -q -d include_path=".ini_get('include_path').
265 " ".$GLOBALS['sys_plugins_path']."/scmcvs/bin/aclcheck.php %r %p ".
266 "\n#END adding cvs acl check\n";
271 cvs_write_file($commitinfofile, $aclcheck, 1);
272 commit_cvs_file($cvsdir_prefix."/".$unix_group_name,$commitinfofile);
273 release_cvs_file($commitinfofile);
275 // echo "cvstracker Found!\n";
279 function update_cvs_repositories() {
280 global $cvsdir_prefix;
282 $res = db_query("select groups.group_id,groups.unix_group_name,groups.enable_anonscm,groups.enable_pserver".
283 " FROM groups, plugins, group_plugin".
284 " WHERE groups.status != 'P' ".
285 " AND groups.group_id=group_plugin.group_id ".
286 " AND group_plugin.plugin_id=plugins.plugin_id ".
287 " AND plugins.plugin_name='scmcvs'");
289 for($i = 0; $i < db_numrows($res); $i++) {
291 Simply call cvscreate.sh
294 $project = &group_get_object(db_result($res,$i,'group_id')); // get the group object for the current group
296 if ( (!$project) || (!is_object($project)) ) {
297 echo "Error Getting Group." . " Id : " . db_result($res,$i,'group_id') . " , Name : " . db_result($res,$i,'unix_group_name');
298 break; // continue to the next project
301 $repositoryPath = $cvsdir_prefix."/".$project->getUnixName();
302 if (is_dir($repositoryPath)) {
303 $writersContent = '';
304 $readersContent = '';
306 if($project->enableAnonSCM()) {
307 $repositoryMode = 02775;
308 if ($project->enablePserver()) {
309 $readersContent = 'anonymous';
310 $passwdContent = 'anonymous:8Z8wlZezt48mY';
313 $repositoryMode = 02770;
315 chmod($repositoryPath, $repositoryMode);
316 cvs_write_file($repositoryPath.'/CVSROOT/writers', $writersContent, 0);
317 cvs_write_file($repositoryPath.'/CVSROOT/readers', $readersContent, 0);
318 cvs_write_file($repositoryPath.'/CVSROOT/passwd', $passwdContent, 0);
319 if ($project->usesPlugin('cvssyncmail')) {
320 add_sync_mail($project->getUnixName());
322 if ($project->usesPlugin('cvstracker')) {
323 add_cvstracker($project->getUnixName());
325 add_acl_check($project->getUnixName());
326 } elseif (is_file($repositoryPath)) {
327 $err .= $repositoryPath.' already exists as a file';
329 // Creates the repository
331 // first, make sure the UNIX group exists
332 $data = exec('getent group '.$project->getUnixName());
334 // group doesn't exist -- cronjob that creates users and groups hasn't run yet
338 $enableAnonSCM = ($project->enableAnonSCM()) ? 1 : 0;
339 $enablePserver = ($project->enablePserver()) ? 1 : 0;
340 system(dirname(__FILE__).'/cvscreate.sh '.
341 $project->getUnixName().
342 // ' '.($project->getID()+50000).
343 ' '.$project->getUnixName().
346 if ($project->usesPlugin('cvssyncmail')) {
347 add_sync_mail($project->getUnixName());
349 if ($project->usesPlugin('cvstracker')) {
350 add_cvstracker($project->getUnixName());
352 add_acl_check($project->getUnixName());
362 Loop through and create/update each repository for every project
363 that uses SCMCVS plugin
367 if(is_dir($cvsdir_prefix)) {
368 update_cvs_repositories();
370 if(is_file($cvsdir_prefix)) {
371 $err .= "$cvsdir_prefix exists but is a file\n";
374 if (mkdir($cvsdir_prefix)) {
375 //need to update group permissions using chmod
376 update_cvs_repositories();
378 $err .= "unable to make $cvsdir_prefix directory\n";