php8
[web.mtrack] / admin / forkrepo.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3 include '../../inc/common.php';
4
5 MTrackACL::requireAnyRights('Browser', 'fork');
6
7 if ($_SERVER['REQUEST_METHOD'] == 'POST') {
8   $rid = $_POST['source'];
9   MTrackACL::requireAnyRights("repo:$rid", 'read');
10   $name = trim($_POST['name']);
11
12   if (strlen($name) == 0) {
13     throw new Exception("missing name");
14   }
15   if (preg_match("/[^a-zA-Z0-9_.-]/", $name)) {
16     throw new Exception("$name contains illegal characters");
17   }
18   $owner = mtrack_canon_username(MTrackAuth::whoami());
19   if (preg_match("/[^a-zA-Z0-9_.-]/", $owner)) {
20     throw new Exception("$owner must be a locally defined user");
21   }
22
23   $S = MTrackRepo::loadById($rid);
24   if (!$S->canFork()) {
25     throw new Exception("cannot fork this repo");
26   }
27   $P = new MTrackRepo;
28   $P->shortname = $name;
29   if (isset($_POST['repo:parent'])) {
30     // FIXME: ACL check to see if we're allowed to create under the specified
31     // parent
32     $P->parent = $_POST['repo:parent'];
33   } else {
34     $P->parent = "user:$owner";
35   }
36
37   $P->scmtype = $S->scmtype;
38   $P->description = $S->description;
39   $P->clonedfrom = $S->repoid;
40
41   $CS = MTrackChangeset::begin("repo:X",
42     "Clone repo $S->shortname as $P->shortname");
43   $P->save($CS);
44   $CS->setObject("repo:$P->repoid");
45   $CS->commit();
46   $name = $P->getBrowseRootName();
47   header("Location: ${ABSWEB}browse.php/$name");
48   exit;
49 }
50
51 header("Location: ${ABSWEB}browse.php");
52 exit;
53