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'] . '>'
170 ->setEnvVar('GIT_AUTHOR_NAME', $sessionData['name'])
171 ->setEnvVar('GIT_AUTHOR_EMAIL', $sessionData['email'])
172 ->setEnvVar('GIT_COMMITTER_NAME', $sessionData['name'])
173 ->setEnvVar('GIT_COMMITTER_EMAIL', $sessionData['email'])
175 //FIXME: git needs ref BEFORE add
176 //quick hack until http://pear.php.net/bugs/bug.php?id=19605 is fixed
177 $vc->getCommand('notes --ref=identity add')
179 ->setOption('message', "$notes")
180 ->setEnvVar('GIT_AUTHOR_NAME', $sessionData['name'])
181 ->setEnvVar('GIT_AUTHOR_EMAIL', $sessionData['email'])
182 ->setEnvVar('GIT_COMMITTER_NAME', $sessionData['name'])
183 ->setEnvVar('GIT_COMMITTER_EMAIL', $sessionData['email'])
185 //update info for dumb git HTTP transport
186 //the post-update hook should do that IMO, but does not somehow
187 $vc->getCommand('update-server-info')->execute();
189 //we changed the hash by committing, so reload it
190 $this->repo->reloadHash();
196 //FIXME: index changed files only
197 //also handle file deletions
198 $db = new Database();
199 $not = new Notificator();
201 $db->getIndexer()->addRepo($this->repo);
202 $not->create($this->repo);
204 $commits = $this->repo->getHistory();
205 $db->getIndexer()->updateRepo(
207 $commits[count($commits)-1]->committerTime,
208 $commits[0]->committerTime
210 $not->edit($this->repo);
217 protected function hasContent($postData)
219 foreach ($postData['files'] as $num => $arFile) {
220 if ($_FILES['files']['error'][$num]['upload'] == 0) {
223 if (isset($arFile['content']) && $arFile['content'] != '') {
226 if (isset($arFile['name']) && $arFile['name'] != '') {
227 //binary files do not have content
230 if (isset($arFile['delete']) && $arFile['delete'] != '') {
231 //binary files do not have content
238 public function createRepo()
240 $rs = new Repositories();
241 $repo = $rs->createNew();
242 $vc = $repo->getVc();
243 $vc->getCommand('init')
244 //this should be setOption, but it fails with a = between name and value
245 ->addArgument('--separate-git-dir')
247 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git'
249 ->addArgument($repo->workDir)
252 $rs = new Repository_Setup($repo);
258 public function getNextNumberedFile($prefix)
263 $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
264 } while (count($files));
266 return $prefix . $num;
269 public function getType($content, $returnError = false)
271 if (getenv('PATH') == '') {
272 //php-fpm does not fill $PATH by default
273 // we have to work around that since System::which() uses it
274 putenv('PATH=/usr/local/bin:/usr/bin:/bin');
277 $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
278 file_put_contents($tmp, $content);
279 $type = Tool_MIME_Type_PlainDetect::autoDetect($tmp);
282 if ($returnError && $type instanceof \PEAR_Error) {
286 return $this->findExtForType($type);
289 protected function findExtForType($type)
292 foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
293 if ($arLang['mime'] == $type) {