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 $orignalName = 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($orignalName) || $orignalName == '') {
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($orignalName)) {
92 //FIXME: Show error message
94 } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
96 } else if ($orignalName != $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($orignalName)
106 $name = $orignalName;
110 $file = $this->repo->getFileByName($name, false);
112 $command = $vc->getCommand('rm')
113 ->addArgument($file->getFilename())
116 } else if ($bUpload) {
118 $_FILES['files']['tmp_name'][$num]['upload'],
121 $command = $vc->getCommand('add')
122 ->addArgument($file->getFilename())
126 || (isset($arFile['content'])
127 && $file->getContent() != $arFile['content']
130 $dir = dirname($file->getFullPath());
132 mkdir($dir, 0777, true);
134 file_put_contents($file->getFullPath(), $arFile['content']);
135 $command = $vc->getCommand('add')
136 ->addArgument($file->getFilename())
142 if (isset($sessionData['identity'])) {
143 $notes = $sessionData['identity'];
145 $notes = $sessionData['ipaddr'];
149 $vc->getCommand('commit')
150 ->setOption('message', '')
151 ->setOption('allow-empty-message')
152 ->setOption('no-edit')
155 $sessionData['name'] . ' <' . $sessionData['email'] . '>'
158 //FIXME: git needs ref BEFORE add
159 //quick hack until http://pear.php.net/bugs/bug.php?id=19605 is fixed
160 //also waiting for https://pear.php.net/bugs/bug.php?id=19623
161 $vc->getCommand('notes --ref=identity add')
163 ->setOption('message', "$notes")
165 //update info for dumb git HTTP transport
166 //the post-update hook should do that IMO, but does not somehow
167 $vc->getCommand('update-server-info')->execute();
173 //FIXME: index changed files only
174 //also handle file deletions
175 $db = new Database();
176 $not = new Notificator();
178 $db->getIndexer()->addRepo($this->repo);
179 $not->create($this->repo);
181 $commits = $this->repo->getHistory();
182 $db->getIndexer()->updateRepo(
184 $commits[count($commits)-1]->committerTime,
185 $commits[0]->committerTime
187 $not->edit($this->repo);
194 protected function hasContent($postData)
196 foreach ($postData['files'] as $num => $arFile) {
197 if ($_FILES['files']['error'][$num]['upload'] == 0) {
200 if (isset($arFile['content']) && $arFile['content'] != '') {
203 if (isset($arFile['name']) && $arFile['name'] != '') {
204 //binary files do not have content
207 if (isset($arFile['delete']) && $arFile['delete'] != '') {
208 //binary files do not have content
215 public function createRepo()
217 $rs = new Repositories();
218 $repo = $rs->createNew();
219 $vc = $repo->getVc();
220 $vc->getCommand('init')
221 //this should be setOption, but it fails with a = between name and value
222 ->addArgument('--separate-git-dir')
224 $GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git'
226 ->addArgument($repo->workDir)
229 $rs = new Repository_Setup($repo);
235 public function getNextNumberedFile($prefix)
240 $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
241 } while (count($files));
243 return $prefix . $num;
246 public function getType($content, $returnError = false)
248 if (getenv('PATH') == '') {
249 //php-fpm does not fill $PATH by default
250 // we have to work around that since System::which() uses it
251 putenv('PATH=/usr/local/bin:/usr/bin:/bin');
254 $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
255 file_put_contents($tmp, $content);
256 $type = Tool_MIME_Type_PlainDetect::autoDetect($tmp);
259 if ($returnError && $type instanceof \PEAR_Error) {
263 return $this->findExtForType($type);
266 protected function findExtForType($type)
269 foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
270 if ($arLang['mime'] == $type) {