Exemple #1
0
 /**
  * Getting instance
  *
  * @return \Core\Loader
  */
 public static function getInstance()
 {
     if (is_null(self::$_instance)) {
         self::$_instance = new self();
     }
     return self::$_instance;
 }
Exemple #2
0
 protected final function display($cacheId = null, $callback = null)
 {
     $className = strtolower(get_class($this));
     $this->old_template_dir = $this->view->template_dir;
     $this->view->template_dir = CoreLoader::$system['widget_folder'] . '/' . $this->name . '/views/';
     if (!self::$debug && $cacheId) {
         $cacheId = 'Widget|' . $cacheId;
         if (!$this->view->caching) {
             $this->setCache(TRUE);
         }
     }
     if ($this->view->caching) {
         if ($cacheId && $callback && $this->view->isCached($this->view_file, $cacheId) == false) {
             if (is_array($callback)) {
                 $params = array();
                 if (isset($callback[2])) {
                     $params = $callback[2];
                     unset($callback[2]);
                 }
                 $data = call_user_func_array(array($callback[0], $callback[1]), $params);
             } else {
                 $data = $callback();
             }
             $this->view->assign($data);
             CoreLoader::log('[' . $className . '][caching:on] updating data from $callback.');
         }
         $this->view->display($this->view_file, $cacheId);
     } else {
         if ($callback) {
             if (is_array($callback)) {
                 $params = array();
                 if (isset($callback[2])) {
                     $params = $callback[2];
                     unset($callback[2]);
                 }
                 $data = call_user_func_array(array($callback[0], $callback[1]), $params);
             } else {
                 $data = $callback();
             }
             $this->view->assign($data);
             CoreLoader::log('[' . $className . '][caching:off] reading data from $callback.');
         }
         $this->view->display($this->view_file);
     }
     $this->view->template_dir = $this->old_template_dir;
     if (!is_null($this->old_caching)) {
         $this->view->caching = $this->old_caching;
     }
     if (!is_null($this->old_cache_lifetime)) {
         $this->view->cache_lifetime = $this->old_cache_lifetime;
     }
 }
 public function __construct($width = '120', $height = '40', $characters = '6')
 {
     $code = $this->generateCode($characters);
     // font size will be 75% of the image height
     $font_size = $height * 0.75;
     $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
     // set the colours
     $background_color = imagecolorallocate($image, 255, 255, 255);
     $text_color = imagecolorallocate($image, 228, 130, 64);
     $noise_color = imagecolorallocate($image, 247, 141, 70);
     // generate random dots in background
     for ($i = 0; $i < $width * $height / 3; $i++) {
         imagefilledellipse($image, mt_rand(0, $width), mt_rand(0, $height), 1, 1, $noise_color);
     }
     // generate random lines in background
     for ($i = 0; $i < $width * $height / 150; $i++) {
         imageline($image, mt_rand(0, $width), mt_rand(0, $height), mt_rand(0, $width), mt_rand(0, $height), $noise_color);
     }
     // create textbox and add text
     $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
     $x = ($width - $textbox[4]) / 2;
     $y = ($height - $textbox[5]) / 2;
     imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font, $code) or die('Error in imagettftext function');
     // output captcha image to browser
     header('Content-Type: image/jpeg');
     header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
     header("Cache-Control: no-cache");
     header("Pragma: no-cache");
     imagejpeg($image);
     imagedestroy($image);
     CoreLoader::load('Captcha');
     $session_id = Session_manager::getSession()->id;
     $table = new Captcha();
     $rows = $table->loadRowsWhere(array('ip' => $_SERVER['REMOTE_ADDR'], 'session_id' => $session_id));
     if (isset($rows[0])) {
         $row = $rows[0];
     } else {
         $row = $table->loadRow();
     }
     $row->created = time();
     $row->ip = $_SERVER['REMOTE_ADDR'];
     $row->session_id = $session_id;
     $row->code = $code;
     $res = $row->save();
     if (is_array($res)) {
         trigger_error(var_export($res, true));
     }
 }
Exemple #4
0
 /**
  * 实例化一个模型
  * @param type $classname_path
  * @param type $hmvc_module_floder
  * @return type CoreModel
  */
 public static function instance($classname_path = null, $hmvc_module_floder = NULL)
 {
     if (!empty($hmvc_module_floder)) {
         CoreRouter::switchHmvcConfig($hmvc_module_floder);
     }
     //这里调用控制器instance是为了触发自动加载,从而避免了插件模式下,直接instance模型,自动加载失效的问题
     CoreController::instance();
     if (empty($classname_path)) {
         $renew = is_bool($classname_path) && $classname_path === true;
         CoreLoader::classAutoloadRegister();
         return empty(self::$instance) || $renew ? self::$instance = new self() : self::$instance;
     }
     $system = CoreLoader::$system;
     $classname_path = str_replace('.', DIRECTORY_SEPARATOR, $classname_path);
     $classname = basename($classname_path);
     $model_folders = $system['model_folder'];
     if (!is_array($model_folders)) {
         $model_folders = array($model_folders);
     }
     $count = count($model_folders);
     CoreLoader::classAutoloadRegister();
     foreach ($model_folders as $key => $model_folder) {
         $filepath = $model_folder . DIRECTORY_SEPARATOR . $classname_path . $system['model_file_suffix'];
         $alias_name = $classname;
         if (isset(CoreModelLoader::$model_files[$alias_name])) {
             return CoreModelLoader::$model_files[$alias_name];
         }
         if (file_exists($filepath)) {
             //在plugin模式下,路由器不再使用,那么自动注册不会被执行,自动加载功能会失效,所以在这里再尝试加载一次,
             //如此一来就能满足两种模式
             //CoreLoader::classAutoloadRegister();
             if (!class_exists($classname, FALSE)) {
                 CoreLoader::includeOnce($filepath);
             }
             if (class_exists($classname, FALSE)) {
                 return CoreModelLoader::$model_files[$alias_name] = new $classname();
             } else {
                 if ($key == $count - 1) {
                     Fn::trigger404('Model Class:' . $classname . ' not found.');
                 }
             }
         } else {
             if ($key == $count - 1) {
                 Fn::trigger404($filepath . ' not  found.');
             }
         }
     }
 }
Exemple #5
0
function smarty_function_widget($params, &$template)
{
    if (empty($params['name'])) {
        user_error('Widget is missing name.', E_USER_ERROR);
    }
    $widget = CoreLoader::widget($params['name'], $template->smarty);
    $method = 'execute';
    $widget->_setTpl('view.tpl');
    if (!empty($params['func'])) {
        $method = $params['func'];
        $widget->_setTpl($method . '.tpl');
    }
    if ($method[0] != '_' && method_exists($widget, $method)) {
        $widget->init($params)->{$method}($params);
    }
}
Exemple #6
0
 function set_cookie_raw($key, $value, $life = null, $path = "/", $domian = null, $http_only = false)
 {
     return CoreLoader::setCookieRaw($key, $value, $life, $path, $domian, $http_only);
 }
Exemple #7
0
 function __get($classname)
 {
     if (isset(self::$lib_files[$classname])) {
         return self::$lib_files[$classname];
     } else {
         return CoreLoader::lib($classname);
     }
 }
Exemple #8
0
 private static function get_x_type($rule, $method, $key)
 {
     $val = null;
     switch ($method) {
         case 'get':
             $val = self::get($key);
             break;
         case 'post':
             $val = self::post($key);
             break;
         case 'get_post':
             $val = self::get_post($key);
             break;
         case 'post_get':
             $val = self::post_get($key);
             break;
     }
     if (is_null(CoreLoader::checkData($rule, array('check' => $val)))) {
         return $val;
     } else {
         return null;
     }
 }
Exemple #9
0
 public static function setConfig($system)
 {
     CoreLoader::$system = $system;
 }
Exemple #10
0
 public function __construct()
 {
     parent::__construct();
     $this->router_info = CoreRouter::info();
     // ====================================
     // 确定语言
     // ====================================
     $cookie_lifetime = 7 * 86400;
     if (!empty($_GET['lang']) && preg_match('/^[a-z\\-]+$/', $_GET['lang'])) {
         if ($this->validLanguage($_GET['lang'])) {
             CoreLoader::setCookie('lang', $_GET['lang'], $cookie_lifetime);
             CoreLoader::$system['language'] = $_GET['lang'];
         } else {
             CoreLoader::setCookie('lang', CoreLoader::$system['default_language'], $cookie_lifetime);
             CoreLoader::$system['language'] = CoreLoader::$system['default_language'];
         }
     } else {
         $cookieLang = CoreInput::cookie('lang');
         if (empty($cookieLang) || !preg_match('/^[a-z\\-]+$/', $cookieLang) || !$this->validLanguage($cookieLang)) {
             // if user doesn't specify any language, check his/her browser language
             // check if the visitor language is supported
             // $this->language(true) to return the country code such as en-US zh-CN zh-TW
             $countryCode = true;
             $langcode = !empty($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : '';
             $langcode = !empty($langcode) ? explode(';', $langcode) : $langcode;
             $langcode = !empty($langcode[0]) ? explode(',', $langcode[0]) : $langcode;
             if (!$countryCode) {
                 $langcode = !empty($langcode[0]) ? explode('-', $langcode[0]) : $langcode;
             }
             $lang = $langcode[0];
             $lang = strtolower($lang);
             if (preg_match('/^[a-z\\-]+$/', $lang) && $this->validLanguage($lang)) {
                 CoreLoader::setCookie('lang', $lang, $cookie_lifetime);
                 CoreLoader::$system['language'] = $lang;
             } else {
                 CoreLoader::setCookie('lang', CoreLoader::$system['default_language'], $cookie_lifetime);
                 CoreLoader::$system['language'] = CoreLoader::$system['default_language'];
             }
         } else {
             CoreLoader::$system['language'] = $cookieLang;
         }
     }
     #echo CoreLoader::$system['language'];
     $locale = CoreLoader::locale(CoreLoader::$system['language']);
     $langEncoding = explode('.', $locale->getLocale());
     $localeDataFile = CoreLoader::$system['language_folder'] . '/' . $langEncoding[0] . '/locale.php';
     if (file_exists($localeDataFile)) {
         CoreLocale::$LANG = (include $localeDataFile);
     }
     // ====================================
     // 初始化Smarty模板引擎
     // ====================================
     include_once FRAMEWORK_CORE_PATH . '/Smarty/Smarty.class.php';
     $smarty = new Smarty();
     $view_dir = self::$system['view_folder'];
     $cache_dir = self::$system['table_cache_folder'] . '/smarty';
     // $smarty->force_compile = true;
     $smarty->error_reporting = 9;
     //E_ALL;
     $smarty->debugging = false;
     $smarty->caching = false;
     //$smarty->use_sub_dirs = true;
     $smarty->cache_lifetime = 120;
     $smarty->left_delimiter = '<{';
     $smarty->right_delimiter = '}>';
     $smarty->allow_php_templates = true;
     $smarty->setTemplateDir($view_dir);
     $smarty->setCompileDir($cache_dir . '/templates_c/');
     $smarty->setConfigDir($cache_dir . '/configs/');
     $smarty->setCacheDir($cache_dir . '/cache/');
     $smarty->registered_cache_resources = array('phpFastCache', 'memcache');
     //$smarty->caching_type = 'phpFastCache'; //TODO: 缓存无效
     $smarty->caching_type = 'file';
     switch (phpFastCache::$storage) {
         case 'memcache':
         case 'apc':
             $smarty->caching_type = phpFastCache::$storage;
             break;
         default:
             if (function_exists('apc_cache_info')) {
                 $smarty->caching_type = 'apc';
             }
             break;
     }
     $smarty->default_resource_type = 'file';
     //模板保存方式
     // ====================================
     // 设置全局变量
     // ====================================
     $smarty->assignGlobal('TITLE', '');
     $smarty->assignGlobal('KEYWORDS', '');
     $smarty->assignGlobal('DESCRIPTION', '');
     // ====================================
     // 注册自定义函数
     // ====================================
     //$smarty->registerPlugin('function', 'burl', 'build_url');
     //<{burl param=value}> ===> function build_url(...){...}
     //$smarty->registerPlugin('modifier', 'astatus', 'audit_status');
     //<{$var|astatus}> ===> function audit_status(...){...}
     $smarty->registerPlugin('modifier', 'url', 'Fn::url');
     $this->view =& $smarty;
 }