fix bug #10: error when nothing submitted
[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)
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 ($bCommit) {
121             $vc->getCommand('commit')
122                 ->setOption('message', '')
123                 ->setOption('allow-empty-message')
124                 ->setOption('author', 'Anonymous <anonymous@phorkie>')
125                 ->execute();
126             $bChanged = true;
127         }
128
129         if ($bChanged) {
130             //FIXME: index changed files only
131             //also handle file deletions
132             $db = new Database();
133             if ($bNew) {
134                 $db->getIndexer()->addRepo($this->repo);
135             } else {
136                 $db->getIndexer()->updateRepo($this->repo);
137             }
138         }
139
140         return true;
141     }
142
143     protected function hasContent($postData)
144     {
145         foreach ($postData['files'] as $num => $arFile) {
146             if ($_FILES['files']['error'][$num]['upload'] == 0) {
147                 return true;
148             }
149             if ($arFile['content'] != '') {
150                 return true;
151             }
152         }
153         return false;
154     }
155
156     public function createRepo()
157     {
158         $rs = new Repositories();
159         $repo = $rs->createNew();
160         $vc = $repo->getVc();
161         $vc->getCommand('init')
162             //this should be setOption, but it fails with a = between name and value
163             ->addArgument('--separate-git-dir')
164             ->addArgument($GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git')
165             ->addArgument($repo->workDir)
166             ->execute();
167
168         foreach (glob($repo->gitDir . '/hooks/*') as $hookfile) {
169             unlink($hookfile);
170         }
171
172         touch($repo->gitDir . '/git-daemon-export-ok');
173
174         return $repo;
175     }
176
177     public function getNextNumberedFile($prefix)
178     {
179         $num = -1;
180         do {
181             ++$num;
182             $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
183         } while (count($files));
184
185         return $prefix . $num;
186     }
187
188     protected function getType($content)
189     {
190         $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
191         file_put_contents($tmp, $content);
192         $type = \MIME_Type_PlainDetect::autoDetect($tmp);
193         unlink($tmp);
194
195         return $this->findExtForType($type);
196     }
197
198     protected function findExtForType($type)
199     {
200         $ext = 'text/plain';
201         foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
202             if ($arLang['mime'] == $type) {
203                 $ext = $lext;
204                 break;
205             }
206         }
207         return $ext;
208     }
209 }
210
211 ?>