8 public function __construct(Repository $repo = null)
14 * Processes the POST data, changes description and files
16 * @return boolean True if the post was successful
18 public function process($postData)
20 if (!isset($postData['files'])) {
23 if (!$this->hasContent($postData)) {
28 $this->repo = $this->createRepo();
31 $vc = $this->repo->getVc();
36 if ($postData['description'] != $this->repo->getDescription()) {
37 $this->repo->setDescription($postData['description']);
41 foreach ($postData['files'] as $num => $arFile) {
43 if ($_FILES['files']['error'][$num]['upload'] == 0) {
46 } else if ($arFile['content'] == '' && $arFile['name'] == '') {
51 $orignalName = Tools::sanitizeFilename($arFile['original_name']);
52 $name = Tools::sanitizeFilename($arFile['name']);
54 if ($arFile['type'] == '_auto_') {
56 $arFile['type'] = $this->getType($arFile['content']);
61 $name = Tools::sanitizeFilename($_FILES['files']['name'][$num]['upload']);
63 $name = $this->getNextNumberedFile('phork')
64 . '.' . $arFile['type'];
70 if (!isset($orignalName) || $orignalName == '') {
73 if (strpos($name, '.') === false) {
74 //automatically append file extension if none is there
75 $name .= '.' . $arFile['type'];
77 } else if (!$this->repo->hasFile($orignalName)) {
79 //FIXME: Show error message
81 } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
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?
88 ->addArgument($orignalName)
97 $file = $this->repo->getFileByName($name, false);
99 $command = $vc->getCommand('rm')
100 ->addArgument($file->getFilename())
103 } else if ($bUpload) {
105 $_FILES['files']['tmp_name'][$num]['upload'], $file->getFullPath()
107 $command = $vc->getCommand('add')
108 ->addArgument($file->getFilename())
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())
121 $vc->getCommand('commit')
122 ->setOption('message', '')
123 ->setOption('allow-empty-message')
124 ->setOption('author', 'Anonymous <anonymous@phorkie>')
130 //FIXME: index changed files only
131 //also handle file deletions
132 $db = new Database();
134 $db->getIndexer()->addRepo($this->repo);
136 $db->getIndexer()->updateRepo($this->repo);
143 protected function hasContent($postData)
145 foreach ($postData['files'] as $num => $arFile) {
146 if ($_FILES['files']['error'][$num]['upload'] == 0) {
149 if ($arFile['content'] != '') {
156 public function createRepo()
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)
168 foreach (glob($repo->gitDir . '/hooks/*') as $hookfile) {
172 touch($repo->gitDir . '/git-daemon-export-ok');
177 public function getNextNumberedFile($prefix)
182 $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
183 } while (count($files));
185 return $prefix . $num;
188 protected function getType($content)
190 $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
191 file_put_contents($tmp, $content);
192 $type = \MIME_Type_PlainDetect::autoDetect($tmp);
195 return $this->findExtForType($type);
198 protected function findExtForType($type)
201 foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
202 if ($arLang['mime'] == $type) {