/** * 参数赋值 * @param unknown_type $attr * @param unknown_type $value */ protected function init($attr, $value = '') { $this->attr = $attr; $this->templateFile = $this->getTemplateFile(); if (!empty($value)) { $this->value = $value; $this->sign = tsmd5(json_encode($this->attr) . $this->value); } else { $this->sign = tsmd5(json_encode($this->attr) . $this->templateFile); } if (is_array($this->attr['head_link']) && !empty($this->attr['head_link'])) { foreach ($this->attr['head_link'] as &$value) { $value->url = str_replace('[@]', '&', $value->url); } } $this->tmplCacheFile = C('TMPL_CACHE_PATH') . '/' . APP_NAME . '_' . $this->sign . C('TMPL_CACHFILE_SUFFIX'); }
/** * 生成邀请码 * @param int $uid 用户ID * @param string $type 邀请码类型 * @param int $num 邀请码数量,默认为5 * @param bool $isAdmin 是否为管理员邀请操作,默认为false * @return bool|string 成功返回邀请码,失败返回false */ public function createInviteCode($uid, $type, $num = 5, $isAdmin = false) { $adminVal = $isAdmin ? 1 : 0; // 验证数据 if (empty($uid) || empty($num) || empty($type)) { return false; } // 邀请码数组 $inviteCodes = array(); $insertDatas = array(); for ($i = 1; $i <= $num; $i++) { $inviteCode = tsmd5($uid . microtime(true) . rand(1111, 9999) . $i); array_push($inviteCodes, $inviteCode); array_push($insertDatas, array('inviter_uid' => $uid, 'code' => $inviteCode, 'is_used' => 0, 'is_admin' => $isAdmin ? 1 : 0, 'type' => $type, 'is_received' => 0, 'receiver_uid' => 0, 'receiver_email' => null, 'ctime' => time())); } if (count($insertDatas)) { Capsule::table('invite_code')->insert($insertDatas); return $inviteCodes; } return false; }
/** * 生成邀请码 * @param integer $uid 用户ID * @param string $type 邀请码类型 * @param integer $num 邀请码数量,默认为5 * @param boolean $isAdmin 是否为管理员邀请操作,默认为false * @return boolean|string 成功返回邀请码,失败返回false */ public function createInviteCode($uid, $type, $num = 5, $isAdmin = false) { $adminVal = $isAdmin ? 1 : 0; // 验证数据 if (empty($uid) || empty($num) || empty($type)) { return false; } // 邀请码数组 $inviteCodes = array(); // 生成邀请码清单 $codes = array(); for ($i = 1; $i <= $num; $i++) { $inviteCode = tsmd5($uid . microtime(true) . rand(1111, 9999) . $i); $inviteCodes[] = $inviteCode; $codes[] = "({$uid}, '{$inviteCode}', 0, '{$type}', {$adminVal})"; } // 插入数据库 if (!empty($codes)) { $sql = "INSERT INTO {$this->tablePrefix}{$this->tableName} (`inviter_uid`, `code`, `is_used`, `type`, `is_admin`) VALUES " . implode(',', $codes); $this->execute($sql); return $inviteCodes; } return false; }
/** * 加载主模板并缓存 * @access public * @param string $tmplTemplateFile 模板文件 * @param string $varPrefix 模板变量前缀 * @return string * @throws ThinkExecption */ public function loadTemplate($tmplTemplateFile = '') { if (empty($tmplTemplateFile)) { $tmplTemplateFile = $this->config['default_tmpl']; } if (!is_file($tmplTemplateFile)) { $tmplTemplateFile = dirname($this->config['default_tmpl']) . '/' . $tmplTemplateFile . $this->config['template_suffix']; if (!is_file($tmplTemplateFile)) { throw_exception(L('_TEMPLATE_NOT_EXIST_')); } } $this->templateFile = $tmplTemplateFile; //根据模版文件名定位缓存文件 $tmplCacheFile = $this->config['cache_path'] . '/' . APP_NAME . '_' . tsmd5($tmplTemplateFile) . $this->config['cache_suffix']; $tmplContent = ''; // 需要更新模版 读出原模板内容 $tmplContent = file_get_contents($tmplTemplateFile); //编译模板内容 $tmplContent = $this->compiler($tmplContent); //重写Cache文件 if (C('TS_CACHE_TYPE') == 'SAEMC') { $mmc = memcache_init(); if ($mmc == false) { exit("mc init failed\n"); } memcache_set($mmc, $tmplCacheFile, trim($tmplContent)); } else { // 检测分组目录 if (!is_dir($this->config['cache_path'])) { mkdir($this->config['cache_path'], 0777, true); } if (false === file_put_contents($tmplCacheFile, trim($tmplContent))) { throw_exception(L('_CACHE_WRITE_ERROR_')); } } return $tmplCacheFile; }
/** * 加载主模板并缓存 * @access public * @param string $tmplTemplateFile 模板文件 * @param string $varPrefix 模板变量前缀 * @return string * @throws ThinkExecption */ public function loadTemplate($tmplTemplateFile = '') { if (empty($tmplTemplateFile)) { $tmplTemplateFile = $this->config['default_tmpl']; } if (!is_file($tmplTemplateFile)) { $tmplTemplateFile = dirname($this->config['default_tmpl']) . '/' . $tmplTemplateFile . $this->config['template_suffix']; if (!is_file($tmplTemplateFile)) { throw_exception(L('_TEMPLATE_NOT_EXIST_')); } } $this->templateFile = $tmplTemplateFile; //根据模版文件名定位缓存文件 $tmplCacheFile = $this->config['cache_path'] . '/' . APP_NAME . '_' . tsmd5($tmplTemplateFile) . $this->config['cache_suffix']; $tmplContent = ''; // 检查Cache文件是否需要更新 // 需要更新模版 读出原模板内容 $tmplContent = file_get_contents($tmplTemplateFile); //编译模板内容 $tmplContent = $this->compiler($tmplContent); // 检测分组目录 if (!is_dir($this->config['cache_path'])) { mkdir($this->config['cache_path'], 0777, true); } //重写Cache文件 if (false === file_put_contents($tmplCacheFile, trim($tmplContent))) { throw_exception(L('_CACHE_WRITE_ERROR_')); } return $tmplCacheFile; }
function fetch($templateFile = '', $tvar = array(), $charset = 'utf-8', $contentType = 'text/html', $display = false) { //注入全局变量ts global $ts; $tvar['ts'] = $ts; unset($tvar['templateCacheFile'], $tvar['templateFile']); //$GLOBALS['_viewStartTime'] = microtime(TRUE); if (null === $templateFile) { // 使用null参数作为模版名直接返回不做任何输出 return; } if (empty($charset)) { $charset = C('DEFAULT_CHARSET'); } // 网页字符编码 header("Content-Type:" . $contentType . "; charset=" . $charset); header("Cache-control: private"); //支持页面回跳 //页面缓存 ob_start(); ob_implicit_flush(0); // 模版名为空. if ('' == $templateFile) { $templateFile = APP_TPL_PATH . '/' . MODULE_NAME . '/' . ACTION_NAME . '.html'; // 模版名为ACTION_NAME } elseif (file_exists(APP_TPL_PATH . '/' . MODULE_NAME . '/' . $templateFile . '.html')) { $templateFile = APP_TPL_PATH . '/' . MODULE_NAME . '/' . $templateFile . '.html'; } elseif (file_exists(APP_TPL_PATH . '/' . $templateFile . '.html')) { $templateFile = APP_TPL_PATH . '/' . $templateFile . '.html'; // 模版是绝对路径 } elseif (file_exists($templateFile)) { // 模版不存在 } else { throw_exception(L('_TEMPLATE_NOT_EXIST_') . '[' . $templateFile . ']'); } //模版缓存文件 $templateCacheFile = C('TMPL_CACHE_PATH') . '/' . APP_NAME . '_' . tsmd5($templateFile) . '.php'; //载入模版缓存 if (!$ts['_debug'] && file_exists($templateCacheFile)) { //if(1==2){ //TODO 开发 extract($tvar, EXTR_OVERWRITE); //载入模版缓存文件 if (C('TS_CACHE_TYPE') == 'SAEMC') { $mmc = memcache_init(); if ($mmc == false) { exit("mc init failed\n"); } $content = memcache_get($mmc, $templateCacheFile); eval(' ?> ' . $content); } else { include $templateCacheFile; } //重新编译 } else { tshook('tpl_compile', array('templateFile', $templateFile)); // 缓存无效 重新编译 tsload(CORE_LIB_PATH . '/Template.class.php'); tsload(CORE_LIB_PATH . '/TagLib.class.php'); tsload(CORE_LIB_PATH . '/TagLib/TagLibCx.class.php'); $tpl = Template::getInstance(); // 编译并加载模板文件 $tpl->load($templateFile, $tvar, $charset); } // 获取并清空缓存 $content = ob_get_clean(); // 模板内容替换 $replace = array('__ROOT__' => SITE_URL, '__UPLOAD__' => UPLOAD_URL, '__PUBLIC__' => THEME_PUBLIC_URL, '__THEME__' => THEME_PUBLIC_URL, '__APP__' => APP_PUBLIC_URL, '__URL__' => __ROOT__ . '/' . ROOT_FILE . '?app=' . APP_NAME . '&mod=' . MODULE_NAME); if (C('TOKEN_ON')) { if (strpos($content, '{__TOKEN__}')) { // 指定表单令牌隐藏域位置 $replace['{__TOKEN__}'] = $this->buildFormToken(); } elseif (strpos($content, '{__NOTOKEN__}')) { // 标记为不需要令牌验证 $replace['{__NOTOKEN__}'] = ''; } elseif (preg_match('/<\\/form(\\s*)>/is', $content, $match)) { // 智能生成表单令牌隐藏域 $replace[$match[0]] = $this->buildFormToken() . $match[0]; } } // 允许用户自定义模板的字符串替换 if (is_array(C('TMPL_PARSE_STRING'))) { $replace = array_merge($replace, C('TMPL_PARSE_STRING')); } $content = str_replace(array_keys($replace), array_values($replace), $content); // 布局模板解析 //$content = $this->layout($content,$charset,$contentType); // 输出模板文件 if ($display) { echo $content; } else { return $content; } }
function fetch($templateFile = '', $tvar = array(), $charset = 'utf-8', $contentType = 'text/html', $display = false) { //注入全局变量ts global $ts; $tvar['ts'] = $ts; unset($tvar['templateCacheFile'], $tvar['templateFile']); //$GLOBALS['_viewStartTime'] = microtime(TRUE); if (null === $templateFile) { // 使用null参数作为模版名直接返回不做任何输出 return; } if (empty($charset)) { $charset = C('DEFAULT_CHARSET'); } // 网页字符编码 header('Content-Type:' . $contentType . '; charset=' . $charset); header('Cache-control: private'); //支持页面回跳 //页面缓存 ob_start(); ob_implicit_flush(0); // 模版名为空. if ('' == $templateFile) { $templateFile = APP_TPL_PATH . '/' . ucfirst(MODULE_NAME) . '/' . ACTION_NAME . '.html'; // 模版名为ACTION_NAME } elseif (file_exists(APP_TPL_PATH . '/' . ucfirst(MODULE_NAME) . '/' . $templateFile . '.html')) { $templateFile = APP_TPL_PATH . '/' . ucfirst(MODULE_NAME) . '/' . $templateFile . '.html'; } elseif (file_exists(APP_TPL_PATH . '/' . $templateFile . '.html')) { $templateFile = APP_TPL_PATH . '/' . $templateFile . '.html'; // 模版是绝对路径 } elseif (file_exists($templateFile)) { // 模版不存在 } else { throw_exception(L('_TEMPLATE_NOT_EXIST_') . '[' . $templateFile . ']'); } //模版缓存文件 $templateCacheFile = C('TMPL_CACHE_PATH') . '/' . APP_NAME . '_' . tsmd5($templateFile) . '.php'; //载入模版缓存 if (!$ts['_debug'] && file_exists($templateCacheFile) && (!defined('TS_APP_DEV') || TS_APP_DEV == false)) { //if(1==2){ //TODO 开发 extract($tvar, EXTR_OVERWRITE); //载入模版缓存文件 if (C('TS_CACHE_TYPE') == 'SAEMC') { $mmc = memcache_init(); if ($mmc == false) { exit("mc init failed\n"); } $content = memcache_get($mmc, $templateCacheFile); eval(' ?> ' . $content); } else { include $templateCacheFile; } //重新编译 } else { tshook('tpl_compile', array('templateFile', $templateFile)); $tpl = Template::getInstance(); // 编译并加载模板文件 $tpl->load($templateFile, $tvar, $charset); } // 获取并清空缓存 $content = ob_get_clean(); // 模板内容替换 $replace = array('__ROOT__' => SITE_URL, '__UPLOAD__' => UPLOAD_URL, '__PUBLIC__' => THEME_PUBLIC_URL, '__THEME__' => THEME_PUBLIC_URL, '__APP__' => APP_PUBLIC_URL, '__URL__' => __ROOT__ . '/' . ROOT_FILE . '?app=' . APP_NAME . '&mod=' . MODULE_NAME); // 允许用户自定义模板的字符串替换 if (is_array(C('TMPL_PARSE_STRING'))) { $replace = array_merge($replace, C('TMPL_PARSE_STRING')); } $content = str_replace(array_keys($replace), array_values($replace), $content); // 输出模板文件 if ($display) { echo $content; } else { return $content; } }