9 * When a new file is created during processing, its name
10 * is stored here for later use.
18 public function __construct(Repository $repo = null)
24 * Processes the POST data, changes description and files
26 * @return boolean True if the post was successful
28 public function process($postData, $sessionData)
30 if (!isset($postData['files'])) {
33 if (!$this->hasContent($postData)) {
38 $this->repo = $this->createRepo();
41 $vc = $this->repo->getVc();
46 if ($postData['description'] != $this->repo->getDescription()) {
47 $this->repo->setDescription($postData['description']);
51 foreach ($postData['files'] as $num => $arFile) {
53 if ($_FILES['files']['error'][$num]['upload'] == 0) {
56 } else if ($arFile['content'] == '' && $arFile['name'] == '') {
61 $originalName = Tools::sanitizeFilename($arFile['original_name']);
62 $name = Tools::sanitizeFilename($arFile['name']);
64 if ($arFile['type'] == '_auto_') {
66 $arFile['type'] = $this->getType($arFile['content']);
71 $name = Tools::sanitizeFilename(
72 $_FILES['files']['name'][$num]['upload']
75 $name = $this->getNextNumberedFile('phork')
76 . '.' . $arFile['type'];
82 if (!isset($originalName) || $originalName == '') {
85 if (strpos($name, '.') === false) {
86 //automatically append file extension if none is there
87 $name .= '.' . $arFile['type'];
89 $this->newfileName = $name;
90 } else if (!$this->repo->hasFile($originalName)) {
92 //FIXME: Show error message
94 } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
96 } else if ($originalName != $name) {
97 if (strpos($name, '/') === false) {
98 //ignore names with a slash in it, would be new directory
99 //FIXME: what to do with overwrites?
100 $vc->getCommand('mv')
101 ->addArgument($originalName)
106 $name = $originalName;
110 $file = $this->repo->getFileByName($name, false);
111 if ($originalName !== '') {
112 $originalFile = $this->repo->getFileByName($originalName, false);
115 $command = $vc->getCommand('rm')
116 ->addArgument($file->getFilename())
119 } else if ($bUpload) {
121 $_FILES['files']['tmp_name'][$num]['upload'],
124 $command = $vc->getCommand('add')
125 ->addArgument($file->getFilename())
129 || (isset($arFile['content']) && isset($originalFile)
130 && $originalFile->getContent() != $arFile['content']
133 $dir = dirname($file->getFullPath());
135 mkdir($dir, 0777, true);
137 file_put_contents($file->getFullPath(), $arFile['content']);
138 $command = $vc->getCommand('add')
139 ->addArgument($file->getFilename())
145 if (isset($sessionData['identity'])) {
146 $notes = $sessionData['identity'];
148 $notes = $sessionData['ipaddr'];
152 $vc->getCommand('commit')
153 ->setOption('message', '')
154 ->setOption('allow-empty-message')
155 ->setOption('no-edit')
158 $sessionData['name'] . ' <' . $sessionData['email'] . '>'
161 //FIXME: git needs ref BEFORE add
162 //quick hack until http://pear.php.net/bugs/bug.php?id=19605 is fixed
163 //also waiting for https://pear.php.net/bugs/bug.php?id=19623
164 $vc->getCommand('notes --ref=identity add')
166 ->setOption('message', "$notes")
168 //update info for dumb git HTTP transport
169 //the post-update hook should do that IMO, but does not somehow
170 $vc->getCommand('update-server-info')->execute();
172 //we changed the hash by committing, so reload it
173 $this->repo->reloadHash();
179 //FIXME: index changed files only
180 //also handle file deletions
181 $db = new Database();
182 $not = new Notificator();
184 $db->getIndexer()->addRepo($this->repo);
185 $not->create($this->repo);
187 $commits = $this->repo->getHistory();
188 $db->getIndexer()->updateRepo(
190 $commits[count($commits)-1]->committerTime,
191 $commits[0]->committerTime
193 $not->edit($this->repo);
200 protected function hasContent($postData)
202 foreach ($postData['files'] as $num => $arFile) {
203 if ($_FILES['files']['error'][$num]['upload'] == 0) {
206 if (isset($arFile['content']) && $arFile['content'] != '') {
209 if (isset($arFile['name']) && $arFile['name'] != '') {
210 //binary files do not have content
213 if (isset($arFile['delete']) && $arFile['delete'] != '') {
214 //binary files do not have content
221 public function createRepo()
223 $rs = new Repositories();
224 $repo = $rs->createNew();
225 $vc = $repo->getVc();
226 $vc->getCommand('init')
227 //this should be setOption, but it fails with a = between name and value
228 ->addArgument('--separate-git-dir')
230 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git'
232 ->addArgument($repo->workDir)
235 $rs = new Repository_Setup($repo);
241 public function getNextNumberedFile($prefix)
246 $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
247 } while (count($files));
249 return $prefix . $num;
252 public function getType($content, $returnError = false)
254 if (getenv('PATH') == '') {
255 //php-fpm does not fill $PATH by default
256 // we have to work around that since System::which() uses it
257 putenv('PATH=/usr/local/bin:/usr/bin:/bin');
260 $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
261 file_put_contents($tmp, $content);
262 $type = Tool_MIME_Type_PlainDetect::autoDetect($tmp);
265 if ($returnError && $type instanceof \PEAR_Error) {
269 return $this->findExtForType($type);
272 protected function findExtForType($type)
275 foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
276 if ($arLang['mime'] == $type) {