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