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($_FILES['files']['name'][$num]['upload']);
63 $name = $this->getNextNumberedFile('phork')
64 . '.' . $arFile['type'];
70 if (!isset($orignalName) || $orignalName == '') {
73 if (strpos($name, '.') === false) {
74 //automatically append file extension if none is there
75 $name .= '.' . $arFile['type'];
77 } else if (!$this->repo->hasFile($orignalName)) {
79 //FIXME: Show error message
81 } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
83 } else if ($orignalName != $name) {
84 if (strpos($name, '/') === false) {
85 //ignore names with a slash in it, would be new directory
86 //FIXME: what to do with overwrites?
88 ->addArgument($orignalName)
97 $file = $this->repo->getFileByName($name, false);
99 $command = $vc->getCommand('rm')
100 ->addArgument($file->getFilename())
103 } else if ($bUpload) {
105 $_FILES['files']['tmp_name'][$num]['upload'], $file->getFullPath()
107 $command = $vc->getCommand('add')
108 ->addArgument($file->getFilename())
111 } else if ($bNew || (isset($arFile['content']) && $file->getContent() != $arFile['content'])) {
112 file_put_contents($file->getFullPath(), $arFile['content']);
113 $command = $vc->getCommand('add')
114 ->addArgument($file->getFilename())
120 $commitmsg = "phorkie commit";
121 if (isset($sessionData['identity'])) {
122 $commitmsg .= " from ".$sessionData['identity'];
124 $commitmsg .= " by ".$sessionData['ipaddr'];
128 $vc->getCommand('commit')
129 ->setOption('message', $commitmsg)
130 ->setOption('author', $sessionData['name'].' <'.$sessionData['email'].'>')
136 //FIXME: index changed files only
137 //also handle file deletions
138 $db = new Database();
140 $db->getIndexer()->addRepo($this->repo);
142 $db->getIndexer()->updateRepo($this->repo);
149 protected function hasContent($postData)
151 foreach ($postData['files'] as $num => $arFile) {
152 if ($_FILES['files']['error'][$num]['upload'] == 0) {
155 if ($arFile['content'] != '') {
162 public function createRepo()
164 $rs = new Repositories();
165 $repo = $rs->createNew();
166 $vc = $repo->getVc();
167 $vc->getCommand('init')
168 //this should be setOption, but it fails with a = between name and value
169 ->addArgument('--separate-git-dir')
170 ->addArgument($GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git')
171 ->addArgument($repo->workDir)
174 foreach (glob($repo->gitDir . '/hooks/*') as $hookfile) {
178 touch($repo->gitDir . '/git-daemon-export-ok');
183 public function getNextNumberedFile($prefix)
188 $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
189 } while (count($files));
191 return $prefix . $num;
194 protected function getType($content)
196 $tmp = tempnam(sys_get_temp_dir(), 'phorkie-autodetect-');
197 file_put_contents($tmp, $content);
198 $type = \MIME_Type_PlainDetect::autoDetect($tmp);
201 return $this->findExtForType($type);
204 protected function findExtForType($type)
207 foreach ($GLOBALS['phorkie']['languages'] as $lext => $arLang) {
208 if ($arLang['mime'] == $type) {