PageRenderTime 75ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/modules/admin/classes/controller/admin.php

https://bitbucket.org/seyar/startech.local
PHP | 422 lines | 329 code | 54 blank | 39 comment | 25 complexity | 78ca00ef31532ecd3bb0dc91480e9b20 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php
  2. defined('SYSPATH') OR die('No direct access allowed.');
  3. abstract class Controller_Admin extends Controller_Quicky //implements AACL_Resource
  4. {
  5. // public $auto_render = TRUE;
  6. // public $auto_wrapper = TRUE;
  7. // admin param
  8. public $admin_path = '/admin/';
  9. public $admin_controller = 'main';
  10. public $lang = 1; // Current Language
  11. public $interface_lang = 1; // admin interface Language
  12. public $session; // Session info
  13. public $admin_model; // AdminUser model will be here
  14. public $model; // Module model will be here
  15. // Using TRUE means requests will require authentication by default
  16. public $auth_required = TRUE;
  17. // Public variable for the user model
  18. public $user;
  19. public $module_title;
  20. public $module_desc_short;
  21. public $module_desc_full;
  22. public $template = 'index.tpl';
  23. public $output_source;
  24. public $request;
  25. public $cache;
  26. public function __construct(Request $request, Response $response)
  27. {
  28. $this->admin_path = ADMIN_PATH;
  29. parent::__construct($request,$response);
  30. $this->session = & Session::instance();
  31. // Fix to restore session for "ajax" session via Flash. Require $_POST['user'] and $_POST['session_id']
  32. if ( (isset($_POST['session_id']) || isset($_POST['session_name'])) AND isset($_POST['user']))
  33. {
  34. $_POST['session_id'] = isset($_POST['session_name']) && !empty($_POST['session_name']) ? $_POST['session_name'] : $_POST['session_id'];
  35. Cookie::set('user', $_POST['user']);
  36. // $this->session = Session::instance('native', $_POST['session_id']);
  37. $this->session->set('user_id', $_POST['user']);
  38. }
  39. $this->admin_model = new Model_AdminUser();
  40. // If a user id cookie is found attempt to load user
  41. if ($id = $this->session->get('user_id'))
  42. {
  43. $user = Sprig::factory('user')
  44. ->values(array('id' => $id), array('status' => 1))
  45. ->load();
  46. if ($user->loaded())
  47. {
  48. // User is logged in
  49. $this->user = $user;
  50. }
  51. }
  52. // If user is not logged in and login is required
  53. if ($this->auth_required AND (!$this->user
  54. OR
  55. ($this->session->get('user_ip') AND $this->session->get('user_ip') != $_SERVER['REMOTE_ADDR']) )
  56. )
  57. {
  58. // Redirect to the login page
  59. $request->redirect(url::site('/admin/login/'));
  60. die();
  61. }
  62. /*
  63. try
  64. {
  65. $defaultAccessModules = array('login', 'main', 'modules', 'photogallery','publications', 'clientside');
  66. if (!in_array($this->request->controller(), $defaultAccessModules))
  67. AACL::check($this);
  68. }
  69. catch (Exception $e)
  70. {
  71. Kohana_Log::instance()->add(Log::INFO, 'Not Access module '.__METHOD__);
  72. // $request->redirect(url::site('/admin/login/'));
  73. // die('mmn');
  74. }
  75. */
  76. }
  77. public function before()
  78. {
  79. parent::before();
  80. //echo Kohana::debug($this->user->language);die;
  81. i18n::$lang = $this->user->language; //Kohana::$config->load('admin.lang');
  82. // content languages
  83. $default_lang = $this->get_default_lang();
  84. $this->lang = Cookie::get('admin_lang',$default_lang);
  85. $this->view->template_dir = Kohana::$config->load('admin')->path;
  86. if ($this->auth_required)
  87. {
  88. if ($id = Cookie::get('user'))
  89. {
  90. $this->user->values(array('id' => $id));
  91. }
  92. }
  93. if ($this->user){
  94. $this->view->assign('show_module_desc', $this->user->__get('show_desc'));
  95. $this->view->assign('installed_modules', Model_AdminModulesControl::get_installed());
  96. $this->view->assign('shortcuts', Model_AdminShortcut::shortcuts_list());
  97. $this->view->assign('modules', Model_AdminModulesControl::modulesPanel());
  98. $this->view->assign('admin_default_lang', $this->lang);
  99. $this->view->assign('admin_lang', $this->get_languages_array());
  100. }
  101. // set admin vars for link building
  102. $this->view->assign('admin_path', $this->admin_path);
  103. $this->view->assign('admin_controller', $this->admin_controller);
  104. $this->header = $this->view->fetch('system/header.tpl');
  105. $this->footer = $this->view->fetch('system/footer.tpl');
  106. }
  107. public function after()
  108. {
  109. // parent::after();
  110. // module info
  111. $this->view->assign('module'
  112. , array(
  113. 'title' => I18n::get($this->module_title),
  114. 'descr_short' => I18n::get($this->module_desc_short),
  115. 'descr_full' => I18n::get($this->module_desc_full),
  116. )
  117. );
  118. // set route vars for link building
  119. $this->view->assign('controller', Request::$current->controller());
  120. $this->view->assign('action', $this->request->action());
  121. $this->view->assign('id', $this->request->param('id'));
  122. if (!defined("MODULE_SUBCONTROLLER"))
  123. {
  124. $this->view->assign('module_name', MODULE_NAME);
  125. }
  126. else
  127. {
  128. $this->view->assign('module_group', MODULE_NAME);
  129. $this->view->assign('module_name', MODULE_NAME . ':' . MODULE_SUBCONTROLLER);
  130. $this->view->assign('module_subname', MODULE_SUBCONTROLLER);
  131. }
  132. $this->view->assign('module_id', MODULE_ID);
  133. /* interface lang */
  134. $user_info = $this->get_user_info(1);
  135. $this->interface_lang = $user_info['language'];
  136. I18n::lang($this->interface_lang);
  137. /* interface lang */
  138. // get month name from i18n
  139. $this->view->assign('title', I18n::get($this->module_title));
  140. $this->view->assign('date_month', I18n::get(strtolower(date('M'))));
  141. if ($this->auto_render)
  142. {
  143. $response = $this->view->fetch($this->template);
  144. // print header
  145. // print body
  146. // $this->request->response .= $this->template ? $this->view->fetch($this->template) : $this->output_source;
  147. }
  148. $this->response->body($this->header.$response.$this->footer);
  149. }
  150. final public function action_skip()
  151. {
  152. // Do nothing
  153. }
  154. final public function redirect_to_controller($controller='main')
  155. {
  156. $this->request->redirect(substr(URL::base(TRUE, TRUE), 0, -1)
  157. . $this->admin_path . ($controller ? $controller . '/' : ''));
  158. }
  159. static public function get_languages_array()
  160. {
  161. $query = DB::select()
  162. ->from('languages')
  163. ->order_by('name')
  164. ->cached(15);
  165. $result = $query->execute();
  166. if ($result->count() == 0)
  167. return array();
  168. else
  169. {
  170. $rows = $result->as_array('prefix');
  171. return $rows;
  172. }
  173. }
  174. public function get_default_lang()
  175. {
  176. $return = 0;
  177. $return = DB::select()->from('languages')->where('is_default', '=', 1)->execute()->get('id');
  178. return $return;
  179. }
  180. public function action_set_lang()
  181. {
  182. Cookie::set('admin_lang', Request::$current->param('id'));
  183. $this->request->redirect($_SERVER['HTTP_REFERER']);
  184. }
  185. public function show_image_preview($filename, $width=120, $height=100, $lifetime = 60)
  186. {
  187. $this->cache = Cache_Sqlite::instance();
  188. $cached_image = $this->cache->get('test_image' . $filename);
  189. if (!$cached_image)
  190. {
  191. if (!is_file($filename))
  192. {
  193. throw new Kohana_Exception404('Image does not exist');
  194. }
  195. $this->image = Image::factory($filename);
  196. $cached_image = array
  197. (
  198. 'mime' => $this->image->mime
  199. , 'content' => $this->image
  200. ->resize($width, $height, Image::NONE)
  201. // ->crop($width, $height)
  202. ->render()
  203. )
  204. ;
  205. $this->cache->set('test_image' . $filename
  206. , array
  207. (
  208. 'mime' => $cached_image['mime']
  209. , 'content' => $cached_image['content']
  210. )
  211. , $lifetime
  212. )
  213. ;
  214. }
  215. header("Content-type: " . $cached_image['mime']);
  216. echo $cached_image['content'];
  217. die();
  218. }
  219. public function delete_directory($dir)
  220. {
  221. if (!file_exists($dir))
  222. return true;
  223. if (!is_dir($dir) || is_link($dir))
  224. return unlink($dir);
  225. foreach (scandir($dir) as $item)
  226. {
  227. if ($item == '.' || $item == '..')
  228. continue;
  229. if (!Controller_Admin::delete_directory($dir . "/" . $item))
  230. {
  231. chmod($dir . "/" . $item, 0777);
  232. if (!Controller_Admin::delete_directory($dir . "/" . $item))
  233. return false;
  234. };
  235. }
  236. return rmdir($dir);
  237. }
  238. public static function transliterate($str)
  239. {
  240. $tr = array(
  241. "Ґ" => "G", "Ё" => "YO", "Є" => "E", "Ї" => "YI", "І" => "I",
  242. "і" => "i", "ґ" => "g", "ё" => "yo", "№" => "#", "є" => "e",
  243. "ї" => "yi", "А" => "A", "Б" => "B", "В" => "V", "Г" => "G",
  244. "Д" => "D", "Е" => "E", "Ж" => "ZH", "З" => "Z", "И" => "I",
  245. "Й" => "Y", "К" => "K", "Л" => "L", "М" => "M", "Н" => "N",
  246. "О" => "O", "П" => "P", "Р" => "R", "С" => "S", "Т" => "T",
  247. "У" => "U", "Ф" => "F", "Х" => "H", "Ц" => "TS", "Ч" => "CH",
  248. "Ш" => "SH", "Щ" => "SCH", "Ъ" => "'", "Ы" => "YI", "Ь" => "",
  249. "Э" => "E", "Ю" => "YU", "Я" => "YA", "а" => "a", "б" => "b",
  250. "в" => "v", "г" => "g", "д" => "d", "е" => "e", "ж" => "zh",
  251. "з" => "z", "и" => "i", "й" => "y", "к" => "k", "л" => "l",
  252. "м" => "m", "н" => "n", "о" => "o", "п" => "p", "р" => "r",
  253. "с" => "s", "т" => "t", "у" => "u", "ф" => "f", "х" => "h",
  254. "ц" => "ts", "ч" => "ch", "ш" => "sh", "щ" => "sch", "ъ" => "'",
  255. "ы" => "yi", "ь" => "", "э" => "e", "ю" => "yu", "я" => "ya"
  256. );
  257. return strtr($str, $tr);
  258. }
  259. public static function to_url($str)
  260. {
  261. return htmlspecialchars(strtr(
  262. UTF8::str_ireplace(' ', '_', Controller_Admin::transliterate(UTF8::transliterate_to_ascii($str)))
  263. , array("\"" => '', "'" => '')
  264. )
  265. );
  266. }
  267. static public function get_sitemap()
  268. {
  269. return array();
  270. }
  271. static public function sitemapXML()
  272. {
  273. return array();
  274. }
  275. static public function get_user_info($id = 1)
  276. {
  277. $user = Sprig::factory('user');
  278. $user->values(array('id' => $id))->load(NULL, 1);
  279. return $user->as_array();
  280. }
  281. public function acl_id()
  282. {
  283. // Controller namespace, controller name
  284. return 'c:' . strtolower($this->request->controller());
  285. }
  286. /**
  287. * AACL_Resource::acl_actions() implementation
  288. *
  289. * @param bool $return_current [optional]
  290. * @return mixed
  291. */
  292. public function acl_actions($return_current = FALSE)
  293. {
  294. if ($return_current)
  295. {
  296. return $this->request->action;
  297. }
  298. // Find all actions in this class
  299. $reflection = new ReflectionClass($this);
  300. $actions = array();
  301. // Add all public methods that start with 'action_'
  302. foreach ($reflection->getMethods(ReflectionMethod::IS_PUBLIC) as $method)
  303. {
  304. if (substr($method->name, 0, 7) === 'action_')
  305. {
  306. $actions[] = substr($method->name, 7);
  307. }
  308. }
  309. return $actions;
  310. }
  311. /**
  312. * AACL_Resource::acl_conditions() implementation
  313. *
  314. * @param Model_User $user [optional] logged in user model
  315. * @param object $condition [optional] condition to test
  316. * @return mixed
  317. */
  318. public function acl_conditions(Model_User $user = NULL, $condition = NULL)
  319. {
  320. if (is_null($user) AND is_null($condition))
  321. {
  322. // We have no conditions
  323. return array();
  324. }
  325. else
  326. {
  327. // We have no conditions so this test should fail!
  328. return FALSE;
  329. }
  330. }
  331. /**
  332. * AACL_Resource::acl_instance() implementation
  333. *
  334. * Note that the object instance returned should not be used for anything except querying the acl_* methods
  335. *
  336. * @param string Class name of object required
  337. * @return Object
  338. */
  339. public static function acl_instance($class_name)
  340. {
  341. // Return controller instance populated with manipulated request details
  342. $instance = new $class_name(Request::factory());
  343. $controller_name = strtolower(substr($class_name, 11));
  344. if ($controller_name !== Request::factory()->controller)
  345. {
  346. // Manually override controller name and action
  347. $instance->request->controller = strtolower(substr(get_class($this), 11));
  348. $instance->request->action = NULL;
  349. }
  350. return $instance;
  351. }
  352. }