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'])) {
25 $this->repo = $this->createRepo();
28 $vc = $this->repo->getVc();
33 if ($postData['description'] != $this->repo->getDescription()) {
34 $this->repo->setDescription($postData['description']);
38 foreach ($postData['files'] as $num => $arFile) {
40 if ($_FILES['files']['error'][$num]['upload'] == 0) {
43 } else if ($arFile['content'] == '' && $arFile['name'] == '') {
48 $orignalName = Tools::sanitizeFilename($arFile['original_name']);
49 $name = Tools::sanitizeFilename($arFile['name']);
51 if ($arFile['type'] == '_auto_') {
53 $arFile['type'] = $this->getType($arFile['content']);
58 $name = Tools::sanitizeFilename($_FILES['files']['name'][$num]['upload']);
60 $name = $this->getNextNumberedFile('phork')
61 . '.' . $arFile['type'];
67 if (!isset($orignalName) || $orignalName == '') {
70 if (strpos($name, '.') === false) {
71 //automatically append file extension if none is there
72 $name .= '.' . $arFile['type'];
74 } else if (!$this->repo->hasFile($orignalName)) {
76 //FIXME: Show error message
78 } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
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?
85 ->addArgument($orignalName)
94 $file = $this->repo->getFileByName($name, false);
96 $command = $vc->getCommand('rm')
97 ->addArgument($file->getFilename())
100 } else if ($bUpload) {
102 $_FILES['files']['tmp_name'][$num]['upload'], $file->getFullPath()
104 $command = $vc->getCommand('add')
105 ->addArgument($file->getFilename())
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())
118 $vc->getCommand('commit')
119 ->setOption('message', '')
120 ->setOption('allow-empty-message')
121 ->setOption('author', 'Anonymous <anonymous@phorkie>')
127 //FIXME: index changed files only
128 //also handle file deletions
129 $db = new Database();
131 $db->getIndexer()->addRepo($this->repo);
133 $db->getIndexer()->updateRepo($this->repo);
140 public function createRepo()
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)
152 foreach (glob($repo->gitDir . '/hooks/*') as $hookfile) {
156 touch($repo->gitDir . '/git-daemon-export-ok');
161 public function getNextNumberedFile($prefix)
166 $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
167 } while (count($files));
169 return $prefix . $num;
172 protected function getType($content)
174 $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
175 file_put_contents($tmp, $content);
176 $type = \MIME_Type_PlainDetect::autoDetect($tmp);
179 return $this->findExtForType($type);
182 protected function findExtForType($type)
185 foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
186 if ($arLang['mime'] == $type) {