~chroot64bit/zivios/gentoo-experimental

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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<?php
/**
 * Copyright (c) 2008 Zivios, LLC.
 *
 * This file is part of Zivios.
 *
 * Zivios is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Zivios is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Zivios.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @package     Zivios
 * @copyright   Copyright (c) 2008 Zivios, LLC. (http://www.zivios.org)
 * @license     http://www.zivios.org/legal/license
 **/

abstract class Zivios_Controller extends Zend_Controller_Action
{
    protected $_controller, $_module, $_action, $_session, $_request;

    public $_callbacks,$_divdata,$_sysnotify;

    final public function init()
    {
        /**
         * Log request information
         */
        $this->_request = $this->getRequest();
        Zivios_Log::debug("Request Information: Module => " . $this->_request->getModuleName());
        Zivios_Log::debug("Request Information: Controller => " . $this->_request->getControllerName());
        Zivios_Log::debug("Request Information: Action => " . $this->_request->getActionName());

        $this->_controller = $this->_request->getControllerName();
        $this->_module     = $this->_request->getModuleName();
        $this->_action     = $this->_request->getActionName();

        $this->_session = Zend_Registry::get('userSession');

        $this->_session->transinprocess = 0;
        unset($this->_session->transinprocess);

        $this->_callbacks = array();
        $this->_divdata = array();
        $this->_sysnotify = array();
        $this->_init();
    }

    /**
     * The preDisptach override ensures that all controllers which extend the
     * Zivios_Controller automatically go through Zivios authentication. The
     * preDisptach action will hence forward the request to the Authentication
     * module if a valid session is not found.
     */
    public function preDispatch()
    {
        if (!isset($this->_session->auth) || $this->_session->auth != 1) {
            Zivios_Log::debug('Expired / unauthenticated session. Redirecting.');

            // Establish web root.
            if ('' == WEB_ROOT) {
                $webroot = '/';
            } else {
                $webroot = WEB_ROOT;
            }

            if($this->_request->isXmlHttpRequest()) {
                Zivios_Log::debug('XHR request detected.');
                // Check if a xhr response type header was sent.
                if (null === ($errRequestHandler = $this->_request->getHeader('XHR_ERROR_HANDLEAS'))) {
                    $errRequestHandler = 'text';
                }

                switch ($errRequestHandler) {
                    case "json" :
                        Zivios_Log::debug('Getting set to flush response.');
                        $this->addCallback('zivios.redirectUrl', array($webroot));
                        $this->addNotify('Your session has expired, redirecting...');
                        $this->flushResponse();
                        break;
                    default:
                        Zivios_Log::debug('Forwarding xhr request to auth controller redirect.');
                        $this->_forward('redirecttologin','auth','default');
                        break;
                }

            } else {
                // A header redirect will do.
                Zivios_Log::debug('Forwarding request to auth controller login');
                $this->_forward('index','auth','default');
            }

            return;
        } else {
            // refresh session data.
            // @todo: additional security checks.
            $securityConfig = Zend_Registry::get('securityConfig');
            $this->_session->setExpirationSeconds($securityConfig->inactivity_timeout, 'auth');
        }
    }

    /**
     * As we have taken over the init call and declared it as a final
     * function, we hook an _init() call for classes extending the
     * controller
     */
    abstract protected function _init();

    protected function addCallback($function,$args)
    {
        $this->_callbacks[] = array ('func' => $function, 'args' => $args);
    }

    protected function refreshTreeNode($dn)
    {
        $this->addCallBack('zivios.refreshTreeNode',array($dn));
    }

    protected function refreshPane($paneid,$href=null)
    {
        if ($href != null)
            $this->addCallback('zivios.cpaneRefresh', array($paneid,$href));
        else
            $this->addCallback('zivios.cpaneRefresh', array($paneid));
    }

    protected function closeTab($tabhandlerid,$tabid)
    {
        $this->addCallBack('zivios.closeTab',array($tabhandlerid,$tabid));
    }

    protected function addNotify($message)
    {
        $this->_sysnotify = $message;
    }

    protected function addDivData($div,$content,$append=false)
    {
        if (!isset($append) || $append=='') {
            $this->_divdata[] = array ('div' => $div, 'content' => $content);
        } else {
            $this->_divdata[] = array ('div' => $div, 'content' => $content, 'append' => $append);
        }
    }

    /**
     * Function returns all response data.
     *
     * @return array $responseData
     */
    protected function getResponseData()
    {
        $responseData = array();

        if (!empty($this->_callbacks)) {
            $responseData['callbacks'] = $this->_callbacks;
        }

        if (!empty($this->_sysnotify)) {
            $responseData['sysNotify'] = $this->_sysnotify;
        }

        if (!empty($this->_divdata)) {
            $responseData['divdata'] = $this->_divdata;
        }

        return $responseData;
    }

    /**
     * Function appends data to response object.
     *
     * @return json
     */
    protected function sendResponse()
    {
        $responseData = $this->getResponseData();
        $content = Zend_Json::encode($responseData);
        $this->_response->appendBody($content);
    }

    /**
     * Function sends response back to client instantly and exits.
     *
     * @return json
     */
    protected function flushResponse()
    {
        $responseData = $this->getResponseData();
        $content = Zend_Json::encode($responseData);
        echo $content;
        exit();
    }

    protected function _getForm($formname,$section='default')
    {
        $formconfigdir = APPLICATION_PATH . '/modules/' . $this->_module . '/config/Forms';
        $formini = new Zend_Config_Ini($formconfigdir . '/'.$formname.'.ini',$section);
        return new Zend_Dojo_Form($formini);
    }


    protected function getParam($paramname)
    {
        return $this->_request->getParam($paramname);
    }

    protected function processTransaction(Zivios_Transaction_Handler $transaction)
    {
        $appConfig = Zend_Registry::get('appConfig');
        $transConfig = Zend_Registry::get('transactionConfig');

        $approot = APPLICATION_PATH;
        $alwaysdefer = $transConfig->deferalltransactions;

        if ($transaction->getRefreshDn() == null || $transaction->getRefreshDn() == '') {
            //$transaction->onSuccessRefreshDn($this->_requestoperate_dn);
        }

        if ($transaction->isDeferred() || $alwaysdefer) {
            $transaction->commit();
            Zivios_Log::debug("Deferring Transaction execution for id :".$transaction->getId());
            $cmd = $approot ."/scripts/runTransaction.sh ". $transaction->getId() . " >/tmp/transdebug.log &";
            Zivios_Log::info("Defer Command : ".$cmd);
            system($cmd);
            return 0;
        } else {
            try {
                $error = null;
                $transaction->process();
            } catch (Exception $e) {
                $error = $e;
                Zivios_Log::debug("Logging");
                Zivios_Log::exception($e);
            }

            $status = $transaction->getStatus();

            if ($status == Zivios_Transaction_Handler::STATUS_FAILED) {
                $sysnot = '<span id="transnotif'.$transaction->getId().'">Transaction ' . $transaction->getId() . ':::'.$transaction->getDescription().
                        ' <font color="red"> Failed </font> <a href="#"
                        onClick="zivios.callAction(\'/transaction/showtransaction\',{
                                        id: '.$transaction->getId().'},
                                        \'transnotif'.$transaction->getId().'\')">'.
                        'Click Here </a> for details</span>';

            } else if ($status == Zivios_Transaction_Handler::STATUS_COMPLETED) {
                $sysnot = '<span id="transnotif'.$transaction->getId().'">Transaction ' . $transaction->getId() . ':::'.$transaction->getDescription().
                        ' <font color="green"> Successful </font> <a href="#"
                        onClick="zivios.callAction(\'/transaction/showtransaction\',{
                                        id: '.$transaction->getId().'},
                                        \'transnotif'.$transaction->getId().'\')">'.
                        'Click Here </a> for details</span>' ;
                Zivios_Log::debug('Notification send: ' . $sysnot);
            }

            $this->addNotify($sysnot);
            return $status;
        }
    }

    public function indexAction()
    {}
}