jump to correct hash after renaming a file
[phorkie.git] / src / phorkie / Repository / Post.php
1 <?php
2 namespace phorkie;
3
4 class Repository_Post
5 {
6     public $repo;
7
8     /**
9      * When a new file is created during processing, its name
10      * is stored here for later use.
11      *
12      * @var string
13      */
14     public $newfileName;
15
16     /**
17      * List of files that have been renamed.
18      *
19      * @var array
20      */
21     public $renameMap = array();
22
23
24     public function __construct(Repository $repo = null)
25     {
26         $this->repo = $repo;
27     }
28
29     /**
30      * Processes the POST data, changes description and files
31      *
32      * @return boolean True if the post was successful
33      */
34     public function process($postData, $sessionData)
35     {
36         if (!isset($postData['files'])) {
37             return false;
38         }
39         if (!$this->hasContent($postData)) {
40             return false;
41         }
42
43         if (!$this->repo) {
44             $this->repo = $this->createRepo();
45         }
46
47         $vc = $this->repo->getVc();
48
49
50         $bChanged = false;
51         $bCommit  = false;
52         if ($postData['description'] != $this->repo->getDescription()) {
53             $this->repo->setDescription($postData['description']);
54             $bChanged = true;
55         }
56
57         $this->renameMap   = array();
58         $this->newfileName = null;
59
60         foreach ($postData['files'] as $num => $arFile) {
61             $bUpload = false;
62             if ($_FILES['files']['error'][$num]['upload'] == 0) {
63                 //valid file upload
64                 $bUpload = true;
65             } else if ($arFile['content'] == '' && $arFile['name'] == '') {
66                 //empty (new) file
67                 continue;
68             }
69
70             $originalName = Tools::sanitizeFilename($arFile['original_name']);
71             $name         = Tools::sanitizeFilename($arFile['name']);
72
73             if ($arFile['type'] == '_auto_') {
74                 //FIXME: upload
75                 $arFile['type'] = $this->getType($arFile['content']);
76             }
77
78             if ($name == '') {
79                 if ($bUpload) {
80                     $name = Tools::sanitizeFilename(
81                         $_FILES['files']['name'][$num]['upload']
82                     );
83                 } else {
84                     $name = $this->getNextNumberedFile('phork')
85                         . '.' . $arFile['type'];
86                 }
87             }
88
89             $bNew = false;
90             $bDelete = false;
91             if (!isset($originalName) || $originalName == '') {
92                 //new file
93                 $bNew = true;
94                 if (strpos($name, '.') === false) {
95                     //automatically append file extension if none is there
96                     $name .= '.' . $arFile['type'];
97                 }
98                 $this->newfileName = $name;
99             } else if (!$this->repo->hasFile($originalName)) {
100                 //unknown file
101                 //FIXME: Show error message
102                 continue;
103             } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
104                 $bDelete = true;
105             } else if ($originalName != $name) {
106                 if (strpos($name, '/') === false) {
107                     //ignore names with a slash in it, would be new directory
108                     //FIXME: what to do with overwrites?
109                     $vc->getCommand('mv')
110                         ->addArgument($originalName)
111                         ->addArgument($name)
112                         ->execute();
113                     $bCommit = true;
114                     $this->renameMap[$originalName] = $name;
115                 } else {
116                     $name = $originalName;
117                 }
118             }
119
120             $file = $this->repo->getFileByName($name, false);
121             if ($originalName !== '') {
122                 $originalFile = $this->repo->getFileByName($originalName, false);
123             }
124             if ($bDelete) {
125                 $command = $vc->getCommand('rm')
126                     ->addArgument($file->getFilename())
127                     ->execute();
128                 $bCommit = true;
129             } else if ($bUpload) {
130                 move_uploaded_file(
131                     $_FILES['files']['tmp_name'][$num]['upload'],
132                     $file->getFullPath()
133                 );
134                 $command = $vc->getCommand('add')
135                     ->addArgument($file->getFilename())
136                     ->execute();
137                 $bCommit = true;
138             } else if ($bNew
139                 || (isset($arFile['content']) && isset($originalFile)
140                     && $originalFile->getContent() != $arFile['content']
141                 )
142             ) {
143                 $dir = dirname($file->getFullPath());
144                 if (!is_dir($dir)) {
145                     mkdir($dir, 0777, true);
146                 }
147                 file_put_contents($file->getFullPath(), $arFile['content']);
148                 $command = $vc->getCommand('add')
149                     ->addArgument($file->getFilename())
150                     ->execute();
151                 $bCommit = true;
152             }
153         }
154
155         if (isset($sessionData['identity'])) {
156             $notes = $sessionData['identity'];
157         } else {
158             $notes = $sessionData['ipaddr'];
159         }
160
161         if ($bCommit) {
162             $vc->getCommand('commit')
163                 ->setOption('message', '')
164                 ->setOption('allow-empty-message')
165                 ->setOption('no-edit')
166                 ->setOption(
167                     'author',
168                     $sessionData['name'] . ' <' . $sessionData['email'] . '>'
169                 )
170                 ->execute();
171             //FIXME: git needs ref BEFORE add
172             //quick hack until http://pear.php.net/bugs/bug.php?id=19605 is fixed
173             //also waiting for https://pear.php.net/bugs/bug.php?id=19623
174             $vc->getCommand('notes --ref=identity add')
175                 ->setOption('force')
176                 ->setOption('message', "$notes")
177                 ->execute();
178             //update info for dumb git HTTP transport
179             //the post-update hook should do that IMO, but does not somehow
180             $vc->getCommand('update-server-info')->execute();
181
182             //we changed the hash by committing, so reload it
183             $this->repo->reloadHash();
184
185             $bChanged = true;
186         }
187
188         if ($bChanged) {
189             //FIXME: index changed files only
190             //also handle file deletions
191             $db = new Database();
192             $not = new Notificator();
193             if ($bNew) {
194                 $db->getIndexer()->addRepo($this->repo);
195                 $not->create($this->repo);
196             } else {
197                 $commits = $this->repo->getHistory();
198                 $db->getIndexer()->updateRepo(
199                     $this->repo,
200                     $commits[count($commits)-1]->committerTime,
201                     $commits[0]->committerTime
202                 );
203                 $not->edit($this->repo);
204             }
205         }
206
207         return true;
208     }
209
210     protected function hasContent($postData)
211     {
212         foreach ($postData['files'] as $num => $arFile) {
213             if ($_FILES['files']['error'][$num]['upload'] == 0) {
214                 return true;
215             }
216             if (isset($arFile['content']) && $arFile['content'] != '') {
217                 return true;
218             }
219             if (isset($arFile['name']) && $arFile['name'] != '') {
220                 //binary files do not have content
221                 return true;
222             }
223             if (isset($arFile['delete']) && $arFile['delete'] != '') {
224                 //binary files do not have content
225                 return true;
226             }
227         }
228         return false;
229     }
230
231     public function createRepo()
232     {
233         $rs = new Repositories();
234         $repo = $rs->createNew();
235         $vc = $repo->getVc();
236         $vc->getCommand('init')
237             //this should be setOption, but it fails with a = between name and value
238             ->addArgument('--separate-git-dir')
239             ->addArgument(
240                 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git'
241             )
242             ->addArgument($repo->workDir)
243             ->execute();
244
245         $rs = new Repository_Setup($repo);
246         $rs->afterInit();
247
248         return $repo;
249     }
250
251     public function getNextNumberedFile($prefix)
252     {
253         $num = -1;
254         do {
255             ++$num;
256             $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
257         } while (count($files));
258
259         return $prefix . $num;
260     }
261
262     public function getType($content, $returnError = false)
263     {
264         if (getenv('PATH') == '') {
265             //php-fpm does not fill $PATH by default
266             // we have to work around that since System::which() uses it
267             putenv('PATH=/usr/local/bin:/usr/bin:/bin');
268         }
269
270         $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
271         file_put_contents($tmp, $content);
272         $type = Tool_MIME_Type_PlainDetect::autoDetect($tmp);
273         unlink($tmp);
274
275         if ($returnError && $type instanceof \PEAR_Error) {
276             return $type;
277         }
278
279         return $this->findExtForType($type);
280     }
281
282     protected function findExtForType($type)
283     {
284         $ext = 'txt';
285         foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
286             if ($arLang['mime'] == $type) {
287                 $ext = $lext;
288                 break;
289             }
290         }
291         return $ext;
292     }
293 }
294
295 ?>