summaryrefslogtreecommitdiffstats
path: root/static/volunteer/submit/index.php
blob: 5b7aa639f6fdc4744587b256064994531590f3e1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
<?php

/**
 *
 * SPDX-License-Identifier: ISC
 *
 * Copyright © 2019 Free Software Foundation of India.
 *
 */

function to() {
    return 's@gnu.org.in';
}

function email_valid($e) {
    if (preg_match('/([\w.-]+)@([\w.-]+)/', $e)) {
        return true;
    }
    return false;
}

function conf() {
    return json_decode(file_get_contents(
        $_SERVER['DOCUMENT_ROOT'] . '/../conf.json'
    ));
}

function gl_new_issue($title, $desc) {
    $conf = conf();

    /**
     * Unable to install php curl module on the current server. So
     * commandline curl for now.
     */
    $cmd = sprintf("%s  -v -4  --request POST"
                 . " --header 'Private-Token: %s'"
                 . " %s/1047/issues"
                 . " -d title='%s'"
                 . " -d description='%s'",
                   $conf->curl,
                   $conf->token,
                   $conf->api,
                   urlencode($title),
                   urlencode($desc));
    $o = NULL;
    exec($cmd, $o);
    return true;
}

function send($n, $e, $m) {
    $p = $n . ' <' . $e . '>';
    $s = $p . ' wants to volunteer for FSF India';
    $msg = 'Per says:' . PHP_EOL . PHP_EOL . $m
       . PHP_EOL;

    $mo = mail(to(), $s, $msg);
    $go = gl_new_issue($s, $msg);

    return $mo && $go;
}

function em_fw($t) {
    return preg_replace('/^(\w+)( .+)/', '<strong>$1</strong>$2', $t);
}

function post() {
    $n = $_POST['name'];
    $e = $_POST['email'];
    $m = $_POST['msg'];
    $c = $_POST['c'];

    $errors = [];
    if (empty($n)) {
        $errors[] = em_fw('Name is required');
    }
    if (!email_valid($e)) {
        $errors[] = em_fw('Email is invalid');
    }
    if (empty($m)) {
        $errors[] = em_fw('Message is required');
    }
    if ($c !== '42') {
        $errors[] = '41 + 1 is not \'' . $c . '\'';
    }

    $sok = true;
    if (empty($errors)) {
        $sok = send($n, $e, $m);
    }
    if (!$sok) {
        $errors[] = 'Unable to process your submission.';
    }

    return [
        'ok' => empty($errors) && $sok,
        'errors' => $errors
    ];
}

$result = post();
include(__DIR__ . '/submit.html');