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