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();
29 $this->repo->setDescription($postData['description']);
32 foreach ($postData['files'] as $arFile) {
33 if ($arFile['content'] == '' && $arFile['name'] == '') {
38 $orignalName = $this->sanitizeFilename($arFile['original_name']);
39 $name = $this->sanitizeFilename($arFile['name']);
42 $name = $this->getNextNumberedFile('phork')
43 . '.' . $arFile['type'];
48 if (!isset($orignalName) || $orignalName == '') {
51 } else if (!$this->repo->hasFile($orignalName)) {
53 //FIXME: Show error message
55 } else if (isset($arFile['delete']) && $arFile['delete'] == 1) {
57 } else if ($orignalName != $name) {
58 //FIXME: what to do with overwrites?
60 ->addArgument($orignalName)
66 $file = $this->repo->getFileByName($name, false);
68 $command = $vc->getCommand('rm')
69 ->addArgument($file->getFilename())
72 } else if ($bNew || $file->getContent() != $arFile['content']) {
73 file_put_contents($file->getPath(), $arFile['content']);
74 $command = $vc->getCommand('add')
75 ->addArgument($file->getFilename())
82 $vc->getCommand('commit')
83 ->setOption('message', '')
84 ->setOption('allow-empty-message')
85 ->setOption('author', 'Anonymous <anonymous@phorkie>')
92 public function createRepo()
94 $rs = new Repositories();
95 $repo = $rs->createNew();
97 $vc->initRepository();
99 foreach (glob($repo->repoDir . '/.git/hooks/*') as $hookfile) {
103 touch($repo->repoDir . '/.git/git-daemon-export-ok');
108 public function getNextNumberedFile($prefix)
113 $files = glob($this->repo->repoDir . '/' . $prefix . $num . '.*');
114 } while (count($files));
116 return $prefix . $num;
120 * Removes malicious parts from a file name
122 * @param string $file File name from the user
124 * @return string Fixed and probably secure filename
126 public function sanitizeFilename($file)
129 $file = str_replace(array('\\', '//'), '/', $file);
130 $file = str_replace('/../', '/', $file);
131 if (substr($file, 0, 3) == '../') {
132 $file = substr($file, 3);
134 if (substr($file, 0, 1) == '../') {
135 $file = substr($file, 1);