9 * When a new file is created during processing, its name
10 * is stored here for later use.
17 * List of files that have been renamed.
21 public $renameMap = array();
24 public function __construct(Repository $repo = null)
30 * Processes the POST data, changes description and files
32 * @return boolean True if the post was successful
34 public function process($postData, $sessionData)
36 if (!isset($postData['files'])) {
39 if (!$this->hasContent($postData)) {
44 $this->repo = $this->createRepo();
47 $vc = $this->repo->getVc();
52 if ($postData['description'] != $this->repo->getDescription()) {
53 $this->repo->setDescription($postData['description']);
57 $this->renameMap = array();
58 $this->newfileName = null;
60 foreach ($postData['files'] as $num => $arFile) {
62 if ($_FILES['files']['error'][$num]['upload'] == 0) {
65 } else if ($arFile['content'] == '' && $arFile['name'] == '') {
70 $originalName = Tools::sanitizeFilename($arFile['original_name']);
71 $name = Tools::sanitizeFilename($arFile['name']);
73 if ($arFile['type'] == '_auto_') {
75 $arFile['type'] = $this->getType($arFile['content']);
80 $name = Tools::sanitizeFilename(
81 $_FILES['files']['name'][$num]['upload']
84 $name = $this->getNextNumberedFile('phork')
85 . '.' . $arFile['type'];
91 if (!isset($originalName) || $originalName == '') {
94 if (strpos($name, '.') === false) {
95 //automatically append file extension if none is there
96 $name .= '.' . $arFile['type'];
98 $this->newfileName = $name;
99 } else if (!$this->repo->hasFile($originalName)) {
101 //FIXME: Show error message
103 } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
105 } else if ($originalName != $name) {
106 if (strpos($name, '/') === false) {
107 //ignore names with a slash in it, would be new directory
108 //FIXME: what to do with overwrites?
109 $vc->getCommand('mv')
110 ->addArgument($originalName)
114 $this->renameMap[$originalName] = $name;
116 $name = $originalName;
120 $file = $this->repo->getFileByName($name, false);
121 if ($originalName !== '') {
122 $originalFile = $this->repo->getFileByName($originalName, false);
125 $command = $vc->getCommand('rm')
126 ->addArgument($file->getFilename())
129 } else if ($bUpload) {
131 $_FILES['files']['tmp_name'][$num]['upload'],
134 $command = $vc->getCommand('add')
135 ->addArgument($file->getFilename())
139 || (isset($arFile['content']) && isset($originalFile)
140 && $originalFile->getContent() != $arFile['content']
143 $dir = dirname($file->getFullPath());
145 mkdir($dir, 0777, true);
147 file_put_contents($file->getFullPath(), $arFile['content']);
148 $command = $vc->getCommand('add')
149 ->addArgument($file->getFilename())
155 if (isset($sessionData['identity'])) {
156 $notes = $sessionData['identity'];
158 $notes = $sessionData['ipaddr'];
162 $vc->getCommand('commit')
163 ->setOption('message', '')
164 ->setOption('allow-empty-message')
165 ->setOption('no-edit')
168 $sessionData['name'] . ' <' . $sessionData['email'] . '>'
171 //FIXME: git needs ref BEFORE add
172 //quick hack until http://pear.php.net/bugs/bug.php?id=19605 is fixed
173 //also waiting for https://pear.php.net/bugs/bug.php?id=19623
174 $vc->getCommand('notes --ref=identity add')
176 ->setOption('message', "$notes")
178 //update info for dumb git HTTP transport
179 //the post-update hook should do that IMO, but does not somehow
180 $vc->getCommand('update-server-info')->execute();
182 //we changed the hash by committing, so reload it
183 $this->repo->reloadHash();
189 //FIXME: index changed files only
190 //also handle file deletions
191 $db = new Database();
192 $not = new Notificator();
194 $db->getIndexer()->addRepo($this->repo);
195 $not->create($this->repo);
197 $commits = $this->repo->getHistory();
198 $db->getIndexer()->updateRepo(
200 $commits[count($commits)-1]->committerTime,
201 $commits[0]->committerTime
203 $not->edit($this->repo);
210 protected function hasContent($postData)
212 foreach ($postData['files'] as $num => $arFile) {
213 if ($_FILES['files']['error'][$num]['upload'] == 0) {
216 if (isset($arFile['content']) && $arFile['content'] != '') {
219 if (isset($arFile['name']) && $arFile['name'] != '') {
220 //binary files do not have content
223 if (isset($arFile['delete']) && $arFile['delete'] != '') {
224 //binary files do not have content
231 public function createRepo()
233 $rs = new Repositories();
234 $repo = $rs->createNew();
235 $vc = $repo->getVc();
236 $vc->getCommand('init')
237 //this should be setOption, but it fails with a = between name and value
238 ->addArgument('--separate-git-dir')
240 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git'
242 ->addArgument($repo->workDir)
245 $rs = new Repository_Setup($repo);
251 public function getNextNumberedFile($prefix)
256 $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
257 } while (count($files));
259 return $prefix . $num;
262 public function getType($content, $returnError = false)
264 if (getenv('PATH') == '') {
265 //php-fpm does not fill $PATH by default
266 // we have to work around that since System::which() uses it
267 putenv('PATH=/usr/local/bin:/usr/bin:/bin');
270 $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
271 file_put_contents($tmp, $content);
272 $type = Tool_MIME_Type_PlainDetect::autoDetect($tmp);
275 if ($returnError && $type instanceof \PEAR_Error) {
279 return $this->findExtForType($type);
282 protected function findExtForType($type)
285 foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
286 if ($arLang['mime'] == $type) {