ari100krat.local /modules/admin/classes/controller/admin.php

Language PHP Lines 439
MD5 Hash 15a243e04663d85bbc880f54bac88c90 Estimated Cost $7,819 (why?)
Repository https://bitbucket.org/seyar/ari100krat.local View Raw File View Project SPDX
  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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
<?php defined('SYSPATH') OR die('No direct access allowed.');

abstract class Controller_Admin extends Controller_Template implements AACL_Resource
{

    public $auto_render = TRUE;
    public $auto_wrapper = TRUE;
    // admin param
    public $admin_path = '/admin/';
    public $admin_controller = 'main';
    public $lang = 1;  // Current Language
    public $interface_lang = 1; // admin interface Language
    public $session;  // Session info
    public $admin_model; // AdminUser model will be here
    public $model;  // Module model will be here
    // Using TRUE means requests will require authentication by default
    public $auth_required = TRUE;
    // Public variable for the user model
    public $user;
    public $module_title = 'Main';
    public $module_name = 'main';
    public $module_desc_short;
    public $module_desc_full;
    public $template;
    /**
     *
     * @var int     allow to hide breadcrums in template
     */
    public $breadcrumbs = TRUE;
    /**
     *
     * @var int     allow to hide float block width btns in template
     */
    public $floatingblock = FALSE;

    public $output_source;
    public $request;
    public $cache;
    static public $intermediate_vars = array();

    public function __construct(Request $request)
    {
        ini_set('session.gc_maxlifetime', 12*60*60);
        
        $this->admin_path = ADMIN_PATH;

        parent::__construct($request);

        $this->session = Session::instance();

        // Fix to restore session for "ajax" session via Flash. Require $_POST['user'] and $_POST['session_id']
        if (isset($_POST['session_id']) AND isset($_POST['user']))
        {
            Cookie::set('user', $_POST['user']);
            $this->session = Session::instance('native', $_POST['session_id']);
            $this->session->set('user_id', $_POST['user']);
        }


        $this->admin_model = new Model_AdminUser();

        // If a user id cookie is found attempt to load user
        if ($id = $this->session->get('user_id'))
        {
            $user = Sprig::factory('user')
                            ->values(array('id' => $id), array('status' => 1))
                            ->load();
            if ($user->loaded())
            {
                // User is logged in
                $this->user = $user;
            }
        }

        // If user is not logged in and login is required
        if ($this->auth_required AND (!$this->user
                OR
                ($this->session->get('user_ip') AND $this->session->get('user_ip') != $_SERVER['REMOTE_ADDR']) )
        )
        {
            // Redirect to the login page
            $request->redirect(url::site('/admin/login/'));
            die();
        }

        try
        {
            $defaultAccessModules = array('login', 'main', 'modules', 'photogallery', 'goods', 'publications');
            if (!in_array($this->request->controller, $defaultAccessModules))
                AACL::check($this);
        }
        catch (Exception $e)
        {
            $request->redirect(url::site('/admin/login/'));
            die();
        }

    }

    public function before()
    {
        parent::before();

        i18n::$lang = (isset($this->user->language)) ? $this->user->language : ''; //Kohana::config('admin.lang');
        // content languages
        $default_lang = $this->get_default_lang();
        $this->lang = Cookie::get('admin_lang') ? Cookie::get('admin_lang') : $default_lang;

//        $this->view->template_dir = Kohana::config('admin')->path;
        if ($this->auth_required)
        {
            if ($id = Cookie::get('user'))
            {
                $this->user->values(array('id' => $id));
            }
        }
        
        if (!defined("MODULE_SUBCONTROLLER"))
        {
            $this->template->module_name = @MODULE_NAME;
            define('MODULE_SUBCONTROLLER', $this->template->module_name);
        }
        else
        {
            $this->template->module_group = MODULE_NAME;
            $this->template->module_name = MODULE_NAME . ':' . MODULE_SUBCONTROLLER;
            $this->template->module_subname = MODULE_SUBCONTROLLER;
        }

        $this->template->module_id = @MODULE_ID;
    }

    public function after()
    {
        parent::after();

        if ($this->user)
        {
            $this->template->show_module_desc = $this->user->__get('show_desc');

            $this->template->installed_modules = Model_AdminModulesControl::get_installed();
//            $this->template->shortcuts = Model_AdminShortcut::shortcuts_list();
            $this->template->modules = Model_AdminModulesControl::modulesPanel();
            $this->template->admin_default_lang = $this->lang;
            $this->template->admin_lang = $this->get_languages_array();

            // module info
            $this->template->module = array(
                'title' => I18n::get($this->module_title),
                'descr_short' => I18n::get($this->module_desc_short),
                'descr_full' => I18n::get($this->module_desc_full),
            );

            // set admin vars for link building
            $this->template->admin_path = $this->admin_path;
            $this->template->admin_controller = $this->admin_controller;

            // set route vars for link building
            $this->template->controller = Request::instance()->controller.'/';
            $this->template->action = $this->request->action;
            $this->template->id = $this->request->param('id');

            
        }
        /* interface lang */
        $user_info = $this->get_user_info(1);
        $this->template->user_info = $user_info;

        $this->interface_lang = $user_info['language'];
        I18n::lang($this->interface_lang);
        /* interface lang */

//        if ($this->auto_wrapper)
//        {
            // get month name from i18n
            $this->template->title = I18n::get($this->module_title);

            $this->template->date_month = I18n::get(strtolower(date('M')));
            $old_errRep = error_reporting();
            error_reporting( E_COMPILE_ERROR|E_RECOVERABLE_ERROR|E_ERROR|E_CORE_ERROR|E_PARSE );
            // print body
            $body = $this->template ? $this->template->render() : $this->output_source;

            // print header
            $this->request->response = '';
            $this->request->response .= $this->template->render('system/header');
            //bread crumb
            $this->request->response .= ($this->breadcrumbs === TRUE ? $this->template->render('system/breadcrumbs') : '');

            if($this->auto_wrapper)
                $this->request->response .= $body;
            else
                $this->request->response = $body;

            // print footer
            $this->request->response .= ($this->auto_wrapper === TRUE ? $this->template->render('system/footer') : '' );
            error_reporting( $old_errRep );
//        }
    }

    final public function action_skip()
    {
        // Do nothing
    }

    final public function redirect_to_controller($controller='main')
    {
        $this->request->redirect(substr(URL::base(TRUE, TRUE), 0, -1)
                . $this->admin_path . ($controller ? $controller . '/' : ''));
    }

    public function get_languages_array()
    {
        $query = DB::select()
                        ->from('languages')
                        ->order_by('name')
                        ->cached(15);

        $result = $query->execute();

        if ($result->count() == 0)
            return array();
        else
        {
            $rows = $result->as_array();
            return $rows;
        }
    }

    public function get_default_lang()
    {
        $return = 0;
        $return = DB::select()->from('languages')->where('is_default', '=', 1)->execute()->get('id');
        return $return;
    }

    public function action_set_lang()
    {
        Cookie::set('admin_lang', Request::instance()->param('id'));
        Request::instance()->redirect($_SERVER['HTTP_REFERER']);
    }

    public function show_image_preview($filename, $width=120, $height=100, $lifetime = 60)
    {
        $this->cache = Cache_Sqlite::instance();

        $cached_image = $this->cache->get('test_image' . $filename);

        if (!$cached_image)
        {

            if (!is_file($filename))
            {
                throw new Kohana_Exception404('Image does not exist');
            }

            $this->image = Image::factory($filename);

            $cached_image = array
                (
                'mime' => $this->image->mime
                , 'content' => $this->image
                        ->resize($width, $height, Image::NONE)
//                                                    ->crop($width, $height)
                        ->render()
                    )
            ;

            $this->cache->set('test_image' . $filename
                    , array
                (
                'mime' => $cached_image['mime']
                , 'content' => $cached_image['content']
                    )
                    , $lifetime
            )
            ;
        }

        header("Content-type: " . $cached_image['mime']);
        echo $cached_image['content'];
        die();
    }

    static public function delete_directory($dir)
    {
        if (!file_exists($dir))
            return true;

        if (!is_dir($dir) || is_link($dir))
            return unlink($dir);
        foreach (scandir($dir) as $item)
        {
            if ($item == '.' || $item == '..')
                continue;
            if (!Controller_Admin::delete_directory($dir . "/" . $item))
            {
                chmod($dir . "/" . $item, 0777);
                if (!Controller_Admin::delete_directory($dir . "/" . $item))
                    return false;
            };
        }
        return rmdir($dir);
    }

    public static function transliterate($str)
    {

        $tr = array(
            "Ґ" => "G", "Ё" => "YO", "Є" => "E", "Ї" => "YI", "І" => "I",
            "і" => "i", "ґ" => "g", "ё" => "yo", "№" => "#", "є" => "e",
            "ї" => "yi", "А" => "A", "Б" => "B", "В" => "V", "Г" => "G",
            "Д" => "D", "Е" => "E", "Ж" => "ZH", "З" => "Z", "И" => "I",
            "Й" => "Y", "К" => "K", "Л" => "L", "М" => "M", "Н" => "N",
            "О" => "O", "П" => "P", "Р" => "R", "С" => "S", "Т" => "T",
            "У" => "U", "Ф" => "F", "Х" => "H", "Ц" => "TS", "Ч" => "CH",
            "Ш" => "SH", "Щ" => "SCH", "Ъ" => "'", "Ы" => "Y", "Ь" => "",
            "Э" => "E", "Ю" => "YU", "Я" => "YA", "а" => "a", "б" => "b",
            "в" => "v", "г" => "g", "д" => "d", "е" => "e", "ж" => "zh",
            "з" => "z", "и" => "i", "й" => "y", "к" => "k", "л" => "l",
            "м" => "m", "н" => "n", "о" => "o", "п" => "p", "р" => "r",
            "с" => "s", "т" => "t", "у" => "u", "ф" => "f", "х" => "h",
            "ц" => "ts", "ч" => "ch", "ш" => "sh", "щ" => "sch", "ъ" => "'",
            "ы" => "y", "ь" => "", "э" => "e", "ю" => "yu", "я" => "ya"
        );
        return strtr($str, $tr);
    }

    public static function to_url($str)
    {
        return htmlspecialchars(strtr(
                        UTF8::str_ireplace(' ', '_', Controller_Admin::transliterate(UTF8::transliterate_to_ascii($str)))
                        , array("\"" => '', "'" => '')
                )
        );
    }

    static public function sitemap()
    {
        return array();
    }

    static public function sitemapXML()
    {
        return array();
    }

    static public function get_user_info($id = 1)
    {
        $user = Sprig::factory('user');
        $user->values(array('id' => $id))->load(NULL, 1);
        return $user->as_array();
    }

    public function acl_id()
    {
        // Controller namespace, controller name
        return 'c:' . strtolower($this->request->controller);
    }

    /**
     * AACL_Resource::acl_actions() implementation
     *
     * @param	bool	$return_current [optional]
     * @return	mixed
     */
    public function acl_actions($return_current = FALSE)
    {
        if ($return_current)
        {
            return $this->request->action;
        }

        // Find all actions in this class
        $reflection = new ReflectionClass($this);

        $actions = array();

        // Add all public methods that start with 'action_'
        foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method)
        {
            if (substr($method->name, 0, 7) === 'action_')
            {
                $actions[] = substr($method->name, 7);
            }
        }

        return $actions;
    }

    /**
     * AACL_Resource::acl_conditions() implementation
     *
     * @param	Model_User	$user [optional] logged in user model
     * @param	object    	$condition [optional] condition to test
     * @return	mixed
     */
    public function acl_conditions(Model_User $user = NULL, $condition = NULL)
    {
        if (is_null($user) AND is_null($condition))
        {
            // We have no conditions
            return array();
        }
        else
        {
            // We have no conditions so this test should fail!
            return FALSE;
        }
    }

    /**
     * AACL_Resource::acl_instance() implementation
     *
     * Note that the object instance returned should not be used for anything except querying the acl_* methods
     *
     * @param	string	Class name of object required
     * @return	Object
     */
    public static function acl_instance($class_name)
    {
        // Return controller instance populated with manipulated request details
        $instance = new $class_name(Request::instance());

        $controller_name = strtolower(substr($class_name, 11));

        if ($controller_name !== Request::instance()->controller)
        {
            // Manually override controller name and action
            $instance->request->controller = strtolower(substr(get_class($this), 11));

            $instance->request->action = NULL;
        }

        return $instance;
    }

}
Back to Top