Completed
Push — actionrefactor ( 549a9e...0f9e19 )
by Andreas
04:26
created

actions.php ➔ act_edit()   C

Complexity

Conditions 10
Paths 96

Size

Total Lines 49
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 30
nc 96
nop 1
dl 0
loc 49
rs 5.5471
c 0
b 0
f 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * DokuWiki Actions
4
 *
5
 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6
 * @author     Andreas Gohr <[email protected]>
7
 */
8
9
if(!defined('DOKU_INC')) die('meh.');
10
11
12
function act_dispatch(){
13
    $router = \dokuwiki\ActionRouter::getInstance(); // is this needed here or could we delegate it to tpl_content() later?
14
15
    $headers = array('Content-Type: text/html; charset=utf-8');
16
    trigger_event('ACTION_HEADERS_SEND',$headers,'act_sendheaders');
17
18
    // clear internal variables
19
    unset($router);
20
    unset($headers);
21
    // make all globals available to the template
22
    extract($GLOBALS);
23
24
    include(template('main.php'));
25
    // output for the commands is now handled in inc/templates.php
26
    // in function tpl_content()
27
}
28
29
/**
30
 * Send the given headers using header()
31
 *
32
 * @param array $headers The headers that shall be sent
33
 */
34
function act_sendheaders($headers) {
35
    foreach ($headers as $hdr) header($hdr);
36
}
37
38
/**
39
 * Sanitize the action command
40
 *
41
 * @author Andreas Gohr <[email protected]>
42
 *
43
 * @param array|string $act
44
 * @return string
45
 */
46
function act_clean($act){
47
    // check if the action was given as array key
48
    if(is_array($act)){
49
        list($act) = array_keys($act);
50
    }
51
52
    //remove all bad chars
53
    $act = strtolower($act);
54
    $act = preg_replace('/[^1-9a-z_]+/','',$act);
55
56
    if($act == 'export_html') $act = 'export_xhtml';
57
    if($act == 'export_htmlbody') $act = 'export_xhtmlbody';
58
59
    if($act === '') $act = 'show';
60
    return $act;
61
}
62
63
64
/**
65
 * Handle 'draftdel'
66
 *
67
 * Deletes the draft for the current page and user
68
 *
69
 * @param string $act action command
70
 * @return string action command
71
 */
72
function act_draftdel($act){
0 ignored issues
show
Unused Code introduced by
The parameter $act is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
    global $INFO;
74
    @unlink($INFO['draft']);
1 ignored issue
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
75
    $INFO['draft'] = null;
76
    return 'show';
77
}
78
79
/**
80
 * Do a redirect after receiving post data
81
 *
82
 * Tries to add the section id as hash mark after section editing
83
 *
84
 * @param string $id page id
85
 * @param string $preact action command before redirect
86
 */
87
function act_redirect($id,$preact){
88
    global $PRE;
89
    global $TEXT;
90
91
    $opts = array(
92
            'id'       => $id,
93
            'preact'   => $preact
94
            );
95
    //get section name when coming from section edit
96
    if($PRE && preg_match('/^\s*==+([^=\n]+)/',$TEXT,$match)){
97
        $check = false; //Byref
98
        $opts['fragment'] = sectionID($match[0], $check);
99
    }
100
101
    trigger_event('ACTION_SHOW_REDIRECT',$opts,'act_redirect_execute');
102
}
103
104
/**
105
 * Execute the redirect
106
 *
107
 * @param array $opts id and fragment for the redirect and the preact
108
 */
109
function act_redirect_execute($opts){
110
    $go = wl($opts['id'],'',true);
111
    if(isset($opts['fragment'])) $go .= '#'.$opts['fragment'];
112
113
    //show it
114
    send_redirect($go);
115
}
116
117
/**
118
 * Validate POST data
119
 *
120
 * Validates POST data for a subscribe or unsubscribe request. This is the
121
 * default action for the event ACTION_HANDLE_SUBSCRIBE.
122
 *
123
 * @author Adrian Lang <[email protected]>
124
 *
125
 * @param array &$params the parameters: target, style and action
126
 * @throws Exception
127
 */
128
function subscription_handle_post(&$params) {
129
    global $INFO;
130
    global $lang;
131
    /* @var Input $INPUT */
132
    global $INPUT;
133
134
    // Get and validate parameters.
135
    if (!isset($params['target'])) {
136
        throw new Exception('no subscription target given');
137
    }
138
    $target = $params['target'];
139
    $valid_styles = array('every', 'digest');
140
    if (substr($target, -1, 1) === ':') {
141
        // Allow “list” subscribe style since the target is a namespace.
142
        $valid_styles[] = 'list';
143
    }
144
    $style  = valid_input_set('style', $valid_styles, $params,
145
                              'invalid subscription style given');
146
    $action = valid_input_set('action', array('subscribe', 'unsubscribe'),
147
                              $params, 'invalid subscription action given');
148
149
    // Check other conditions.
150
    if ($action === 'subscribe') {
151
        if ($INFO['userinfo']['mail'] === '') {
152
            throw new Exception($lang['subscr_subscribe_noaddress']);
153
        }
154
    } elseif ($action === 'unsubscribe') {
155
        $is = false;
156
        foreach($INFO['subscribed'] as $subscr) {
157
            if ($subscr['target'] === $target) {
158
                $is = true;
159
            }
160
        }
161
        if ($is === false) {
162
            throw new Exception(sprintf($lang['subscr_not_subscribed'],
163
                                        $INPUT->server->str('REMOTE_USER'),
164
                                        prettyprint_id($target)));
165
        }
166
        // subscription_set deletes a subscription if style = null.
167
        $style = null;
168
    }
169
170
    $params = compact('target', 'style', 'action');
171
}
172
173
//Setup VIM: ex: et ts=2 :
174