d104a8c1f3eb17254dac43c325bb8f7beef44669
[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
24         if (!$this->repo) {
25             $this->repo = $this->createRepo();
26         }
27
28         $vc = $this->repo->getVc();
29
30
31         $bChanged = false;
32         $bCommit  = false;
33         if ($postData['description'] != $this->repo->getDescription()) {
34             $this->repo->setDescription($postData['description']);
35             $bChanged = true;
36         }
37
38         foreach ($postData['files'] as $num => $arFile) {
39             $bUpload = false;
40             if ($_FILES['files']['error'][$num]['upload'] == 0) {
41                 //valid file upload
42                 $bUpload = true;
43             } else if ($arFile['content'] == '' && $arFile['name'] == '') {
44                 //empty (new) file
45                 continue;
46             }
47
48             $orignalName = Tools::sanitizeFilename($arFile['original_name']);
49             $name        = Tools::sanitizeFilename($arFile['name']);
50
51             if ($arFile['type'] == '_auto_') {
52                 //FIXME: upload
53                 $arFile['type'] = $this->getType($arFile['content']);
54             }
55
56             if ($name == '') {
57                 if ($bUpload) {
58                     $name = Tools::sanitizeFilename($_FILES['files']['name'][$num]['upload']);
59                 } else {
60                     $name = $this->getNextNumberedFile('phork')
61                         . '.' . $arFile['type'];
62                 }
63             }
64
65             $bNew = false;
66             $bDelete = false;
67             if (!isset($orignalName) || $orignalName == '') {
68                 //new file
69                 $bNew = true;
70                 if (strpos($name, '.') === false) {
71                     //automatically append file extension if none is there
72                     $name .= '.' . $arFile['type'];
73                 }
74             } else if (!$this->repo->hasFile($orignalName)) {
75                 //unknown file
76                 //FIXME: Show error message
77                 continue;
78             } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
79                 $bDelete = true;
80             } else if ($orignalName != $name) {
81                 if (strpos($name, '/') === false) {
82                     //ignore names with a slash in it, would be new directory
83                     //FIXME: what to do with overwrites?
84                     $vc->getCommand('mv')
85                         ->addArgument($orignalName)
86                         ->addArgument($name)
87                         ->execute();
88                     $bCommit = true;
89                 } else {
90                     $name = $orignalName;
91                 }
92             }
93
94             $file = $this->repo->getFileByName($name, false);
95             if ($bDelete) {
96                 $command = $vc->getCommand('rm')
97                     ->addArgument($file->getFilename())
98                     ->execute();
99                 $bCommit = true;
100             } else if ($bUpload) {
101                 move_uploaded_file(
102                     $_FILES['files']['tmp_name'][$num]['upload'], $file->getFullPath()
103                 );
104                 $command = $vc->getCommand('add')
105                     ->addArgument($file->getFilename())
106                     ->execute();
107                 $bCommit = true;
108             } else if ($bNew || (isset($arFile['content']) && $file->getContent() != $arFile['content'])) {
109                 file_put_contents($file->getFullPath(), $arFile['content']);
110                 $command = $vc->getCommand('add')
111                     ->addArgument($file->getFilename())
112                     ->execute();
113                 $bCommit = true;
114             }
115         }
116
117         if ($bCommit) {
118             $vc->getCommand('commit')
119                 ->setOption('message', '')
120                 ->setOption('allow-empty-message')
121                 ->setOption('author', 'Anonymous <anonymous@phorkie>')
122                 ->execute();
123             $bChanged = true;
124         }
125
126         if ($bChanged) {
127             //FIXME: index changed files only
128             //also handle file deletions
129             $db = new Database();
130             if ($bNew) {
131                 $db->getIndexer()->addRepo($this->repo);
132             } else {
133                 $db->getIndexer()->updateRepo($this->repo);
134             }
135         }
136
137         return true;
138     }
139
140     public function createRepo()
141     {
142         $rs = new Repositories();
143         $repo = $rs->createNew();
144         $vc = $repo->getVc();
145         $vc->getCommand('init')
146             //this should be setOption, but it fails with a = between name and value
147             ->addArgument('--separate-git-dir')
148             ->addArgument($GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git')
149             ->addArgument($repo->workDir)
150             ->execute();
151
152         foreach (glob($repo->gitDir . '/hooks/*') as $hookfile) {
153             unlink($hookfile);
154         }
155
156         touch($repo->gitDir . '/git-daemon-export-ok');
157
158         return $repo;
159     }
160
161     public function getNextNumberedFile($prefix)
162     {
163         $num = -1;
164         do {
165             ++$num;
166             $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
167         } while (count($files));
168
169         return $prefix . $num;
170     }
171
172     protected function getType($content)
173     {
174         $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
175         file_put_contents($tmp, $content);
176         $type = \MIME_Type_PlainDetect::autoDetect($tmp);
177         unlink($tmp);
178
179         return $this->findExtForType($type);
180     }
181
182     protected function findExtForType($type)
183     {
184         $ext = 'text/plain';
185         foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
186             if ($arLang['mime'] == $type) {
187                 $ext = $lext;
188                 break;
189             }
190         }
191         return $ext;
192     }
193 }
194
195 ?>