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)
20 if (!isset($postData['files'])) {
25 $this->repo = $this->createRepo();
28 $vc = $this->repo->getVc();
33 if ($postData['description'] != $this->repo->getDescription()) {
34 $this->repo->setDescription($postData['description']);
38 foreach ($postData['files'] as $num => $arFile) {
40 if ($_FILES['files']['error'][$num]['upload'] == 0) {
43 } else if ($arFile['content'] == '' && $arFile['name'] == '') {
48 $orignalName = Tools::sanitizeFilename($arFile['original_name']);
49 $name = Tools::sanitizeFilename($arFile['name']);
53 $name = Tools::sanitizeFilename($_FILES['files']['name'][$num]['upload']);
55 $name = $this->getNextNumberedFile('phork')
56 . '.' . $arFile['type'];
62 if (!isset($orignalName) || $orignalName == '') {
65 if (strpos($name, '.') === false) {
66 //automatically append file extension if none is there
67 $name .= '.' . $arFile['type'];
69 } else if (!$this->repo->hasFile($orignalName)) {
71 //FIXME: Show error message
73 } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
75 } else if ($orignalName != $name) {
76 if (strpos($name, '/') === false) {
77 //ignore names with a slash in it, would be new directory
78 //FIXME: what to do with overwrites?
80 ->addArgument($orignalName)
89 $file = $this->repo->getFileByName($name, false);
91 $command = $vc->getCommand('rm')
92 ->addArgument($file->getFilename())
95 } else if ($bUpload) {
97 $_FILES['files']['tmp_name'][$num]['upload'], $file->getFullPath()
99 $command = $vc->getCommand('add')
100 ->addArgument($file->getFilename())
103 } else if ($bNew || (isset($arFile['content']) && $file->getContent() != $arFile['content'])) {
104 file_put_contents($file->getFullPath(), $arFile['content']);
105 $command = $vc->getCommand('add')
106 ->addArgument($file->getFilename())
113 $vc->getCommand('commit')
114 ->setOption('message', '')
115 ->setOption('allow-empty-message')
116 ->setOption('author', 'Anonymous <anonymous@phorkie>')
122 //FIXME: index changed files only
123 //also handle file deletions
124 $db = new Database();
125 $db->getIndexer()->updateRepo($this->repo);
131 public function createRepo()
133 $rs = new Repositories();
134 $repo = $rs->createNew();
135 $vc = $repo->getVc();
136 $vc->getCommand('init')
137 //this should be setOption, but it fails with a = between name and value
138 ->addArgument('--separate-git-dir')
139 ->addArgument($GLOBALS['phorkie']['cfg']['gitdir'] . '/' . $repo->id . '.git')
140 ->addArgument($repo->workDir)
143 foreach (glob($repo->gitDir . '/hooks/*') as $hookfile) {
147 touch($repo->gitDir . '/git-daemon-export-ok');
152 public function getNextNumberedFile($prefix)
157 $files = glob($this->repo->workDir . '/' . $prefix . $num . '.*');
158 } while (count($files));
160 return $prefix . $num;