fe6858bed2e55932e5d9d7892aa390cc76ba9bb3
[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(
62                         $_FILES['files']['name'][$num]['upload']
63                     );
64                 } else {
65                     $name = $this->getNextNumberedFile('phork')
66                         . '.' . $arFile['type'];
67                 }
68             }
69
70             $bNew = false;
71             $bDelete = false;
72             if (!isset($orignalName) || $orignalName == '') {
73                 //new file
74                 $bNew = true;
75                 if (strpos($name, '.') === false) {
76                     //automatically append file extension if none is there
77                     $name .= '.' . $arFile['type'];
78                 }
79             } else if (!$this->repo->hasFile($orignalName)) {
80                 //unknown file
81                 //FIXME: Show error message
82                 continue;
83             } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
84                 $bDelete = true;
85             } else if ($orignalName != $name) {
86                 if (strpos($name, '/') === false) {
87                     //ignore names with a slash in it, would be new directory
88                     //FIXME: what to do with overwrites?
89                     $vc->getCommand('mv')
90                         ->addArgument($orignalName)
91                         ->addArgument($name)
92                         ->execute();
93                     $bCommit = true;
94                 } else {
95                     $name = $orignalName;
96                 }
97             }
98
99             $file = $this->repo->getFileByName($name, false);
100             if ($bDelete) {
101                 $command = $vc->getCommand('rm')
102                     ->addArgument($file->getFilename())
103                     ->execute();
104                 $bCommit = true;
105             } else if ($bUpload) {
106                 move_uploaded_file(
107                     $_FILES['files']['tmp_name'][$num]['upload'],
108                     $file->getFullPath()
109                 );
110                 $command = $vc->getCommand('add')
111                     ->addArgument($file->getFilename())
112                     ->execute();
113                 $bCommit = true;
114             } else if ($bNew
115                 || (isset($arFile['content'])
116                     && $file->getContent() != $arFile['content']
117                 )
118             ) {
119                 file_put_contents($file->getFullPath(), $arFile['content']);
120                 $command = $vc->getCommand('add')
121                     ->addArgument($file->getFilename())
122                     ->execute();
123                 $bCommit = true;
124             }
125         }
126
127         if (isset($sessionData['identity'])) {
128             $notes = $sessionData['identity'];
129         } else {
130             $notes = $sessionData['ipaddr'];
131         }
132
133         if ($bCommit) {
134             $vc->getCommand('commit')
135                 ->setOption('message', '')
136                 ->setOption('allow-empty-message')
137                 ->setOption(
138                     'author',
139                     $sessionData['name'] . ' <' . $sessionData['email'] . '>'
140                 )
141                 ->execute();
142             //FIXME: git needs ref BEFORE add
143             //quick hack until http://pear.php.net/bugs/bug.php?id=19605 is fixed
144             //also waiting for https://pear.php.net/bugs/bug.php?id=19623
145             $vc->getCommand('notes --ref=identity add')
146                 ->setOption('force')
147                 ->setOption('message', "$notes")
148                 ->execute();
149             $bChanged = true;
150         }
151
152         if ($bChanged) {
153             //FIXME: index changed files only
154             //also handle file deletions
155             $db = new Database();
156             if ($bNew) {
157                 $db->getIndexer()->addRepo($this->repo);
158             } else {
159                 $db->getIndexer()->updateRepo($this->repo);
160             }
161         }
162
163         return true;
164     }
165
166     protected function hasContent($postData)
167     {
168         foreach ($postData['files'] as $num => $arFile) {
169             if ($_FILES['files']['error'][$num]['upload'] == 0) {
170                 return true;
171             }
172             if ($arFile['content'] != '') {
173                 return true;
174             }
175         }
176         return false;
177     }
178
179     public function createRepo()
180     {
181         $rs = new Repositories();
182         $repo = $rs->createNew();
183         $vc = $repo->getVc();
184         $vc->getCommand('init')
185             //this should be setOption, but it fails with a = between name and value
186             ->addArgument('--separate-git-dir')
187             ->addArgument(
188                 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git'
189             )
190             ->addArgument($repo->workDir)
191             ->execute();
192
193         $rs = new Repository_Setup($repo);
194         $rs->afterInit();
195
196         return $repo;
197     }
198
199     public function getNextNumberedFile($prefix)
200     {
201         $num = -1;
202         do {
203             ++$num;
204             $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
205         } while (count($files));
206
207         return $prefix . $num;
208     }
209
210     protected function getType($content)
211     {
212         $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
213         file_put_contents($tmp, $content);
214         $type = \MIME_Type_PlainDetect::autoDetect($tmp);
215         unlink($tmp);
216
217         return $this->findExtForType($type);
218     }
219
220     protected function findExtForType($type)
221     {
222         $ext = 'txt';
223         foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
224             if ($arLang['mime'] == $type) {
225                 $ext = $lext;
226                 break;
227             }
228         }
229         return $ext;
230     }
231 }
232
233 ?>