setTranslator(); $this->setTemplate(); } private function setTemplate() { $this->template = new PHPTAL(); $this->template->setphpCodeDestination("tmp/phptal/"); $this->setTemplateRepository(); $this->template->setTranslator($this->getTranslator()); } private function setTemplateRepository() { $repository_folders = array("templates", "user", "pages", "administrate"); $repos_path = dirname(dirname(__FILE__)); if (is_dir(dirname(dirname(__FILE__))."/includes/".PLUGIN)) { $this->plugin_loaded = dirname(dirname(__FILE__))."/includes/".PLUGIN."/"; } foreach ($repository_folders as $repository_folder) { if ($this->plugin_loaded && is_dir($this->plugin_loaded.'/views/'.$repository_folder)) { $this->template->setTemplateRepository($this->plugin_loaded.'views/'.$repository_folder.'/'); } $this->template->setTemplateRepository(dirname(dirname(__FILE__)).'/views/'.$repository_folder.'/'); } } private function setTranslator() { $language = DEFAULT_LANGUAGE; if (isset($_SESSION['language']) && in_array($_SESSION['language'], array("et", "ru", "en"))) { $language = $_SESSION['language']; } else if ($this->is_logged_in()) { $language = $this->user->language; } $tr = new PHPTAL_GetTextTranslator(); $tr->setLanguage(constant('LOCALE_'.$language).DEFAULT_ENCODING, constant('LOCALE_'.$language).DEFAULT_ENCODING); $tr->addDomain(DEFAULT_DOMAIN, 'i18n'); $tr->useDomain(DEFAULT_DOMAIN); $this->translator = $tr; } function getTranslator() { return $this->translator; } function setNavigation() { if ($this->plugin AND method_exists($this->plugin, "setNavigation")) { $this->plugin->setNavigation($this); } if (is_admin()) { $this->navigation['administration'] = array('title'=>_('Administration'), 'url'=>"administrate/suggestions", 'current'=>$this->is_current_main('administrate'), 'level'=>0); } } function is_current_main($current) { if (!$this->handler) return false; if ($this->handler->name == $current) { return true; } return false; } /************ * Page View * ************/ private function view($view="page_not_found") { if ($this->execute_page()) { $this->template->teke = $this; $this->template->request = $_GET; $input_values = array(); if (isset($_SESSION['input_values'])) { $input_values = $_SESSION['input_values']; unset($_SESSION['input_values']); } $this->template->input_values = $input_values; $this->template->setTemplate($view.".html"); echo $this->template->execute(); } } public function view_page($page, $handler=NULL) { $this->page = $page; $template = $page[0]; if (in_array(end($page), array("add", "edit", "activate")) && $this->has_access(ACCESS_CAN_EDIT)) { $this->action = end($page); } if ($handler) { if ($handler == "pages") { $template = $page[0]; } else if (is_file(dirname(dirname(__FILE__))."/includes/".PLUGIN."/handler/handler_".$handler.".php") || in_array($handler, array("user", "administrate"))) { $hn = ucfirst($handler."Handler"); if (class_exists($hn)) { $this->handler = new $hn($page); if (isset($this->handler->template)) { $template = $this->handler->template; } } } else { $template = "page_not_found"; } } if ($template) { $this->view($template); } else { $this->view(); } } public function execute_page() { $request_uri = $_SERVER["REQUEST_URI"]; if (substr_count($request_uri, "/actions/") || substr_count($request_uri, "/includes/".PLUGIN."/actions/")) { return false; } if (substr_count($request_uri, "/images/")) { return false; } /*if (substr_count($request_uri, "/email_confirm.php")) { return false; }*/ if (substr_count($request_uri, "/views/js/")) { return false; } return true; } public function get_main_navigation_items() { $this->setNavigation(); $nav = array(); foreach ($this->navigation as $handelr => $handler_nav) { $nav [] = $handler_nav; if (isset($this->handler) && $this->handler->name == $handelr) { foreach ($this->handler->navigation_main as $item) { $nav [] = $item; } } } return $nav; } public function get_navigation_items() { if (is_object($this->handler)) { return $this->handler->navigation; } } public function add_system_message($message, $type='success') { $messages = array(); if (!in_array($type, array('success', 'error'))) { $type = "success"; } if (isset($_SESSION['system_messages'])) { $messages = $_SESSION['system_messages']; } $messages[] = array('type' => $type, 'message' => $message); $_SESSION['system_messages'] = $messages; } public function get_system_messages() { $messages = array(); if (isset($_SESSION['system_messages'])) { $messages = $_SESSION['system_messages']; unset($_SESSION['system_messages']); } return $messages; } public function is_logged_in() { if (isset($_SESSION['user'])) { return true; } return false; } public function is_admin() { return $this->has_access(9); } function has_access($level) { if ($this->user->level >= $level) return true; return false; } function get_language() { //if (isset($_SESSION['language'])) return $_SESSION['language']; //return DEFAULT_LANGUAGE; if (isset($_SESSION['language']) && in_array($_SESSION['language'], array("et", "ru", "en", "fi", "sv"))) { return $_SESSION['language']; } else if ($this->is_logged_in()) { return $this->user->language; } return DEFAULT_LANGUAGE; } function send_mail($user, $subject, $message) { $to_name = $user->getFullName(); $to_email = $user->email; $to = "{$to_name} <{$to_email}>"; $headers = "MIME-Version: 1.0". "\r\n"; $headers .= "Content-type: text/plain; charset=UTF-8" . "\r\n"; $headers .= "From:" . SITE_NAME . " " . "\r\n"; return mail($to, $subject, $message, $headers); } function generate_random_string($len, $chars = 'abcdefghijklmnopqrstuwxyz0123456789') { $string = ''; for ($i = 0; $i < $len; $i++) { $pos = mt_rand(0, strlen($chars)-1); $string .= $chars{$pos}; } return $string; } function formatDate($date, $format="d.m.Y") { $time = strtotime($date); return date($format, $time); } function getSiteLanguages() { return array( "en" => "english", "et" => "eesti", "ru" => "русский", "fi" => "suomi", "sv" => "svenska" ); } } ?>