basic file upload works
[phorkie.git] / src / phorkie / Repository.php
1 <?php
2 namespace phorkie;
3
4
5 class Repository
6 {
7     /**
8      * Repository ID (number in repositories directory)
9      *
10      * @var integer
11      */
12     public $id;
13
14     /**
15      * Full path to the git repository
16      *
17      * @var string
18      */
19     public $repoDir;
20
21     /**
22      * Load Repository data from GET-Request
23      *
24      * @return void
25      *
26      * @throws Exception When something is wrong
27      */
28     public function loadFromRequest()
29     {
30         if (!isset($_GET['id'])) {
31             throw new Exception_Input('Paste ID missing');
32         }
33         if (!is_numeric($_GET['id'])) {
34             throw new Exception_Input('Paste ID not numeric');
35         }
36         $this->id = (int)$_GET['id'];
37
38         $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
39         if (!is_dir($repoDir)) {
40             throw new Exception_NotFound('Paste not found');
41         }
42         $this->repoDir = $repoDir;
43     }
44
45     public function loadById($id)
46     {
47         if (!is_numeric($id)) {
48             throw new Exception_Input('Paste ID not numeric');
49         }
50         $this->id = (int)$id;
51
52         $repoDir = $GLOBALS['phorkie']['cfg']['repos'] . '/' . $this->id;
53         if (!is_dir($repoDir)) {
54             throw new Exception_NotFound('Paste not found');
55         }
56         $this->repoDir = $repoDir;
57     }
58
59     public function getVc()
60     {
61         return new \VersionControl_Git($this->repoDir);
62     }
63
64     /**
65      * Loads the list of files in this repository
66      *
67      * @return File[] Array of files
68      */
69     public function getFiles()
70     {
71         $files = glob($this->repoDir . '/*');
72         $arFiles = array();
73         foreach ($files as $path) {
74             $arFiles[] = new File($path, $this);
75         }
76         return $arFiles;
77     }
78
79     public function getFileByName($name, $bHasToExist = true)
80     {
81         $base = basename($name);
82         if ($base != $name) {
83             throw new Exception('No directories supported for now');
84         }
85         if ($name == '') {
86             throw new Exception_Input('Empty file name given');
87         }
88         $path = $this->repoDir . '/' . $base;
89         if ($bHasToExist && !is_readable($path)) {
90             throw new Exception_Input('File does not exist');
91         }
92         return new File($path, $this);
93     }
94
95     public function hasFile($name)
96     {
97         try {
98             $this->getFileByName($name);
99         } catch (Exception $e) {
100             return false;
101         }
102         return true;
103     }
104
105     /**
106      * Permanently deletes the paste repository without any way to get
107      * it back.
108      *
109      * @return boolean True if all went well, false if not
110      */
111     public function delete()
112     {
113         return Tools::recursiveDelete($this->repoDir);
114     }
115
116     public function getDescription()
117     {
118         if (!is_readable($this->repoDir . '/.git/description')) {
119             return null;
120         }
121         return file_get_contents($this->repoDir . '/.git/description');
122     }
123
124     public function setDescription($description)
125     {
126         file_put_contents($this->repoDir . '/.git/description', $description);
127     }
128
129     /**
130      * Get a link to the repository
131      *
132      * @param string $type Link type. Supported are:
133      *                     - "edit"
134      *                     - "display"
135      *                     - "fork"
136      *
137      * @return string
138      */
139     public function getLink($type)
140     {
141         if ($type == 'edit') {
142             return '/' . $this->id . '/edit';
143         } else if ($type == 'display') {
144             return '/' . $this->id;
145         } else if ($type == 'fork') {
146             return '/' . $this->id . '/fork';
147         } else if ($type == 'delete') {
148             return '/' . $this->id . '/delete';
149         } else if ($type == 'delete-confirm') {
150             return '/' . $this->id . '/delete/confirm';
151         }
152         throw new Exception('Unknown link type');
153     }
154
155     public function getCloneURL($public = true)
156     {
157         $var = $public ? 'public' : 'private';
158         if (isset($GLOBALS['phorkie']['cfg']['git'][$var])) {
159             return $GLOBALS['phorkie']['cfg']['git'][$var] . $this->id . '/.git';
160         }
161         return null;
162     }
163 }
164
165 ?>