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, $sessionData)
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(
62 $_FILES['files']['name'][$num]['upload']
65 $name = $this->getNextNumberedFile('phork')
66 . '.' . $arFile['type'];
72 if (!isset($orignalName) || $orignalName == '') {
75 if (strpos($name, '.') === false) {
76 //automatically append file extension if none is there
77 $name .= '.' . $arFile['type'];
79 } else if (!$this->repo->hasFile($orignalName)) {
81 //FIXME: Show error message
83 } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
85 } else if ($orignalName != $name) {
86 if (strpos($name, '/') === false) {
87 //ignore names with a slash in it, would be new directory
88 //FIXME: what to do with overwrites?
90 ->addArgument($orignalName)
99 $file = $this->repo->getFileByName($name, false);
101 $command = $vc->getCommand('rm')
102 ->addArgument($file->getFilename())
105 } else if ($bUpload) {
107 $_FILES['files']['tmp_name'][$num]['upload'],
110 $command = $vc->getCommand('add')
111 ->addArgument($file->getFilename())
115 || (isset($arFile['content'])
116 && $file->getContent() != $arFile['content']
119 $dir = dirname($file->getFullPath());
121 mkdir($dir, 0777, true);
123 file_put_contents($file->getFullPath(), $arFile['content']);
124 $command = $vc->getCommand('add')
125 ->addArgument($file->getFilename())
131 if (isset($sessionData['identity'])) {
132 $notes = $sessionData['identity'];
134 $notes = $sessionData['ipaddr'];
138 $vc->getCommand('commit')
139 ->setOption('message', '')
140 ->setOption('allow-empty-message')
141 ->setOption('no-edit')
144 $sessionData['name'] . ' <' . $sessionData['email'] . '>'
147 //FIXME: git needs ref BEFORE add
148 //quick hack until http://pear.php.net/bugs/bug.php?id=19605 is fixed
149 //also waiting for https://pear.php.net/bugs/bug.php?id=19623
150 $vc->getCommand('notes --ref=identity add')
152 ->setOption('message', "$notes")
158 //FIXME: index changed files only
159 //also handle file deletions
160 $db = new Database();
161 $not = new Notificator();
163 $db->getIndexer()->addRepo($this->repo);
164 $not->create($this->repo);
166 $commits = $this->repo->getHistory();
167 $db->getIndexer()->updateRepo(
169 $commits[count($commits)-1]->committerTime,
170 $commits[0]->committerTime
172 $not->edit($this->repo);
179 protected function hasContent($postData)
181 foreach ($postData['files'] as $num => $arFile) {
182 if ($_FILES['files']['error'][$num]['upload'] == 0) {
185 if (isset($arFile['content']) && $arFile['content'] != '') {
188 if (isset($arFile['name']) && $arFile['name'] != '') {
189 //binary files do not have content
192 if (isset($arFile['delete']) && $arFile['delete'] != '') {
193 //binary files do not have content
200 public function createRepo()
202 $rs = new Repositories();
203 $repo = $rs->createNew();
204 $vc = $repo->getVc();
205 $vc->getCommand('init')
206 //this should be setOption, but it fails with a = between name and value
207 ->addArgument('--separate-git-dir')
209 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git'
211 ->addArgument($repo->workDir)
214 $rs = new Repository_Setup($repo);
220 public function getNextNumberedFile($prefix)
225 $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
226 } while (count($files));
228 return $prefix . $num;
231 public function getType($content)
233 $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
234 file_put_contents($tmp, $content);
235 $type = \MIME_Type_PlainDetect::autoDetect($tmp);
238 return $this->findExtForType($type);
241 protected function findExtForType($type)
244 foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
245 if ($arLang['mime'] == $type) {