use repository and file classes
[phorkie.git] / www / index.php
1 <?php
2 namespace Phorkie;
3 /**
4  * Show paste creation form
5  *
6  * Elements:
7  * - description
8  * - file name (default: default.php)
9  * - content
10  *
11  * Creates and redirects to display page
12  */
13 require_once 'www-header.php';
14
15 if (isset($_POST['file'])) {
16     //save
17     $repoDir = $GLOBALS['phorkie']['cfg']['repos'];
18     $n = count(glob($repoDir . '/*', GLOB_ONLYDIR));
19     $dir = $repoDir . '/' . $n . '/'; 
20     mkdir($dir, 0777);//FIXME
21     $vc = new VersionControl_Git($dir);
22     $vc->initRepository();
23     file_put_contents($dir . '.git/description', $_POST['description']);
24
25     foreach ($_POST['file'] as $num => $arFile) {
26         if ($arFile['name'] != '') {
27             $fname = $arFile['name'];
28         } else {
29             $fname = 'phork' . $num . '.' . $arFile['type'];
30         }
31         $fpath = $dir . $fname;
32         file_put_contents($fpath, $arFile['content']);
33         //fixme: let the class do that when it is able to
34         $command = $vc->getCommand('add')
35             ->addArgument($fname)
36             ->execute();
37     }
38     $command = $vc->getCommand('commit')
39         ->setOption('message', 'initial paste')
40         ->execute();
41     //redirect to phork
42     redirect($n);
43 }
44
45 $phork = array(
46     '1' => array(
47         'filename' => '',
48         'content' => '',
49         'type' => ''
50     )
51 );
52 render('index', array('file' => $phork, 'description' => ''));
53 ?>