do not use a commit message
[phorkie.git] / src / phorkie / Repository / Post.php
1 <?php
2 namespace phorkie;
3
4 class Repository_Post
5 {
6     public $repo;
7
8     public function __construct(Repository $repo = null)
9     {
10         $this->repo = $repo;
11     }
12
13     /**
14      * Processes the POST data, changes description and files
15      *
16      * @return boolean True if the post was successful
17      */
18     public function process($postData, $sessionData)
19     {
20         if (!isset($postData['files'])) {
21             return false;
22         }
23         if (!$this->hasContent($postData)) {
24             return false;
25         }
26
27         if (!$this->repo) {
28             $this->repo = $this->createRepo();
29         }
30
31         $vc = $this->repo->getVc();
32
33
34         $bChanged = false;
35         $bCommit  = false;
36         if ($postData['description'] != $this->repo->getDescription()) {
37             $this->repo->setDescription($postData['description']);
38             $bChanged = true;
39         }
40
41         foreach ($postData['files'] as $num => $arFile) {
42             $bUpload = false;
43             if ($_FILES['files']['error'][$num]['upload'] == 0) {
44                 //valid file upload
45                 $bUpload = true;
46             } else if ($arFile['content'] == '' && $arFile['name'] == '') {
47                 //empty (new) file
48                 continue;
49             }
50
51             $orignalName = Tools::sanitizeFilename($arFile['original_name']);
52             $name        = Tools::sanitizeFilename($arFile['name']);
53
54             if ($arFile['type'] == '_auto_') {
55                 //FIXME: upload
56                 $arFile['type'] = $this->getType($arFile['content']);
57             }
58
59             if ($name == '') {
60                 if ($bUpload) {
61                     $name = Tools::sanitizeFilename($_FILES['files']['name'][$num]['upload']);
62                 } else {
63                     $name = $this->getNextNumberedFile('phork')
64                         . '.' . $arFile['type'];
65                 }
66             }
67
68             $bNew = false;
69             $bDelete = false;
70             if (!isset($orignalName) || $orignalName == '') {
71                 //new file
72                 $bNew = true;
73                 if (strpos($name, '.') === false) {
74                     //automatically append file extension if none is there
75                     $name .= '.' . $arFile['type'];
76                 }
77             } else if (!$this->repo->hasFile($orignalName)) {
78                 //unknown file
79                 //FIXME: Show error message
80                 continue;
81             } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
82                 $bDelete = true;
83             } else if ($orignalName != $name) {
84                 if (strpos($name, '/') === false) {
85                     //ignore names with a slash in it, would be new directory
86                     //FIXME: what to do with overwrites?
87                     $vc->getCommand('mv')
88                         ->addArgument($orignalName)
89                         ->addArgument($name)
90                         ->execute();
91                     $bCommit = true;
92                 } else {
93                     $name = $orignalName;
94                 }
95             }
96
97             $file = $this->repo->getFileByName($name, false);
98             if ($bDelete) {
99                 $command = $vc->getCommand('rm')
100                     ->addArgument($file->getFilename())
101                     ->execute();
102                 $bCommit = true;
103             } else if ($bUpload) {
104                 move_uploaded_file(
105                     $_FILES['files']['tmp_name'][$num]['upload'], $file->getFullPath()
106                 );
107                 $command = $vc->getCommand('add')
108                     ->addArgument($file->getFilename())
109                     ->execute();
110                 $bCommit = true;
111             } else if ($bNew || (isset($arFile['content']) && $file->getContent() != $arFile['content'])) {
112                 file_put_contents($file->getFullPath(), $arFile['content']);
113                 $command = $vc->getCommand('add')
114                     ->addArgument($file->getFilename())
115                     ->execute();
116                 $bCommit = true;
117             }
118         }
119
120         if (isset($sessionData['identity'])) {
121             $notes = $sessionData['identity'];
122         } else {
123             $notes = $sessionData['ipaddr'];
124         }
125
126         if ($bCommit) {
127             $vc->getCommand('commit')
128                 ->setOption('message', '')
129                 ->setOption('allow-empty-message')
130                 ->setOption('author', $sessionData['name'].' <'.$sessionData['email'].'>')
131                 ->execute();
132             //FIXME: git needs ref BEFORE add
133             //quick hack until http://pear.php.net/bugs/bug.php?id=19605 is fixed
134             $vc->getCommand('notes --ref=identity add')
135                                 ->setOption('force')
136                 ->setOption('message', "$notes")
137                 ->execute();
138             $bChanged = true;
139         }
140
141         if ($bChanged) {
142             //FIXME: index changed files only
143             //also handle file deletions
144             $db = new Database();
145             if ($bNew) {
146                 $db->getIndexer()->addRepo($this->repo);
147             } else {
148                 $db->getIndexer()->updateRepo($this->repo);
149             }
150         }
151
152         return true;
153     }
154
155     protected function hasContent($postData)
156     {
157         foreach ($postData['files'] as $num => $arFile) {
158             if ($_FILES['files']['error'][$num]['upload'] == 0) {
159                 return true;
160             }
161             if ($arFile['content'] != '') {
162                 return true;
163             }
164         }
165         return false;
166     }
167
168     public function createRepo()
169     {
170         $rs = new Repositories();
171         $repo = $rs->createNew();
172         $vc = $repo->getVc();
173         $vc->getCommand('init')
174             //this should be setOption, but it fails with a = between name and value
175             ->addArgument('--separate-git-dir')
176             ->addArgument($GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git')
177             ->addArgument($repo->workDir)
178             ->execute();
179
180         foreach (glob($repo->gitDir . '/hooks/*') as $hookfile) {
181             unlink($hookfile);
182         }
183
184         touch($repo->gitDir . '/git-daemon-export-ok');
185
186         return $repo;
187     }
188
189     public function getNextNumberedFile($prefix)
190     {
191         $num = -1;
192         do {
193             ++$num;
194             $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
195         } while (count($files));
196
197         return $prefix . $num;
198     }
199
200     protected function getType($content)
201     {
202         $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
203         file_put_contents($tmp, $content);
204         $type = \MIME_Type_PlainDetect::autoDetect($tmp);
205         unlink($tmp);
206
207         return $this->findExtForType($type);
208     }
209
210     protected function findExtForType($type)
211     {
212         $ext = 'txt';
213         foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
214             if ($arLang['mime'] == $type) {
215                 $ext = $lext;
216                 break;
217             }
218         }
219         return $ext;
220     }
221 }
222
223 ?>