PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

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

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