Home >Backend Development >PHP Tutorial >uploadinstallincludesauto_init.php源码分析
1 <?php 2 3 /** 4 * ECSHOP 首页文件 5 * ============================================================================ 6 * * 版权所有 2005-2012 上海商派网络科技有限公司,并保留所有权利。 7 * 网站地址: http://www.ecshop.com; 8 * ---------------------------------------------------------------------------- 9 * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和 10 * 使用;不允许对程序代码以任何形式任何目的的再发布。 11 * ============================================================================ 12 * $Author: liubo $ 13 * $Id: index.php 17217 2011-01-19 06:29:08Z liubo $ 14 */ 15 16 define('IN_ECS', true); 17 18 require(dirname(__FILE__) . '/includes/init.php'); 19 20 if ((DEBUG_MODE & 2) != 2) 21 { 22 $smarty->caching = true; 23 } 24 $ua = strtolower($_SERVER['HTTP_USER_AGENT']); 25 26 $uachar = "/(nokia|sony|ericsson|mot|samsung|sgh|lg|philips|panasonic|alcatel|lenovo|cldc|midp|mobile)/i"; 27 28 if(($ua == '' || preg_match($uachar, $ua))&& !strpos(strtolower($_SERVER['REQUEST_URI']),'wap')) 29 { 30 $Loaction = 'mobile/'; 31 32 if (!empty($Loaction)) 33 { 34 ecs_header("Location: $Loaction\n"); 35 36 exit; 37 } 38 39 } 40 /*------------------------------------------------------ */ 41 //-- Shopex系统地址转换 42 /*------------------------------------------------------ */ 43 if (!empty($_GET['gOo'])) 44 { 45 if (!empty($_GET['gcat'])) 46 { 47 /* 商品分类。*/ 48 $Loaction = 'category.php?id=' . $_GET['gcat']; 49 } 50 elseif (!empty($_GET['acat'])) 51 { 52 /* 文章分类。*/ 53 $Loaction = 'article_cat.php?id=' . $_GET['acat']; 54 } 55 elseif (!empty($_GET['goodsid'])) 56 { 57 /* 商品详情。*/ 58 $Loaction = 'goods.php?id=' . $_GET['goodsid']; 59 } 60 elseif (!empty($_GET['articleid'])) 61 { 62 /* 文章详情。*/ 63 $Loaction = 'article.php?id=' . $_GET['articleid']; 64 } 65 66 if (!empty($Loaction)) 67 { 68 ecs_header("Location: $Loaction\n"); 69 70 exit; 71 } 72 } 73 74 //判断是否有ajax请求 75 $act = !empty($_GET['act']) ? $_GET['act'] : ''; 76 if ($act == 'cat_rec') 77 { 78 $rec_array = array(1 => 'best', 2 => 'new', 3 => 'hot'); 79 $rec_type = !empty($_REQUEST['rec_type']) ? intval($_REQUEST['rec_type']) : '1'; 80 $cat_id = !empty($_REQUEST['cid']) ? intval($_REQUEST['cid']) : '0'; 81 include_once('includes/cls_json.php'); 82 $json = new JSON; 83 $result = array('error' => 0, 'content' => '', 'type' => $rec_type, 'cat_id' => $cat_id); 84 85 $children = get_children($cat_id); 86 $smarty->assign($rec_array[$rec_type] . '_goods', get_category_recommend_goods($rec_array[$rec_type], $children)); // 推荐商品 87 $smarty->assign('cat_rec_sign', 1); 88 $result['content'] = $smarty->fetch('library/recommend_' . $rec_array[$rec_type] . '.lbi'); 89 die($json->encode($result)); 90 } 91 92 /*------------------------------------------------------ */ 93 //-- 判断是否存在缓存,如果存在则调用缓存,反之读取相应内容 94 /*------------------------------------------------------ */ 95 /* 缓存编号 */ 96 $cache_id = sprintf('%X', crc32($_SESSION['user_rank'] . '-' . $_CFG['lang'])); 97 98 if (!$smarty->is_cached('index.dwt', $cache_id)) 99 {100 assign_template();101 102 $position = assign_ur_here();103 $smarty->assign('page_title', $position['title']); // 页面标题104 $smarty->assign('ur_here', $position['ur_here']); // 当前位置105 106 /* meta information */107 $smarty->assign('keywords', htmlspecialchars($_CFG['shop_keywords']));108 $smarty->assign('description', htmlspecialchars($_CFG['shop_desc']));109 $smarty->assign('flash_theme', $_CFG['flash_theme']); // Flash轮播图片模板110 111 $smarty->assign('feed_url', ($_CFG['rewrite'] == 1) ? 'feed.xml' : 'feed.php'); // RSS URL112 113 $smarty->assign('categories', get_categories_tree()); // 分类树114 $smarty->assign('helps', get_shop_help()); // 网店帮助115 $smarty->assign('top_goods', get_top10()); // 销售排行116 117 $smarty->assign('best_goods', get_recommend_goods('best')); // 推荐商品118 $smarty->assign('new_goods', get_recommend_goods('new')); // 最新商品119 $smarty->assign('hot_goods', get_recommend_goods('hot')); // 热点文章120 $smarty->assign('promotion_goods', get_promote_goods()); // 特价商品121 $smarty->assign('brand_list', get_brands());122 $smarty->assign('promotion_info', get_promotion_info()); // 增加一个动态显示所有促销信息的标签栏123 124 $smarty->assign('invoice_list', index_get_invoice_query()); // 发货查询125 $smarty->assign('new_articles', index_get_new_articles()); // 最新文章126 $smarty->assign('group_buy_goods', index_get_group_buy()); // 团购商品127 $smarty->assign('auction_list', index_get_auction()); // 拍卖活动128 $smarty->assign('shop_notice', $_CFG['shop_notice']); // 商店公告129 130 /* 首页主广告设置 */131 $smarty->assign('index_ad', $_CFG['index_ad']);132 if ($_CFG['index_ad'] == 'cus')133 {134 $sql = 'SELECT ad_type, content, url FROM ' . $ecs->table("ad_custom") . ' WHERE ad_status = 1';135 $ad = $db->getRow($sql, true);136 $smarty->assign('ad', $ad);137 }138 139 /* links */140 $links = index_get_links();141 $smarty->assign('img_links', $links['img']);142 $smarty->assign('txt_links', $links['txt']);143 $smarty->assign('data_dir', DATA_DIR); // 数据目录144 145 /* 首页推荐分类 */146 $cat_recommend_res = $db->getAll("SELECT c.cat_id, c.cat_name, cr.recommend_type FROM " . $ecs->table("cat_recommend") . " AS cr INNER JOIN " . $ecs->table("category") . " AS c ON cr.cat_id=c.cat_id");147 if (!empty($cat_recommend_res))148 {149 $cat_rec_array = array();150 foreach($cat_recommend_res as $cat_recommend_data)151 {152 $cat_rec[$cat_recommend_data['recommend_type']][] = array('cat_id' => $cat_recommend_data['cat_id'], 'cat_name' => $cat_recommend_data['cat_name']);153 }154 $smarty->assign('cat_rec', $cat_rec);155 }156 157 /* 页面中的动态内容 */158 assign_dynamic('index');159 }160 161 $smarty->display('index.dwt', $cache_id);162 163 /*------------------------------------------------------ */164 //-- PRIVATE FUNCTIONS165 /*------------------------------------------------------ */166 167 /**168 * 调用发货单查询169 *170 * @access private171 * @return array172 */173 function index_get_invoice_query()174 {175 $sql = 'SELECT o.order_sn, o.invoice_no, s.shipping_code FROM ' . $GLOBALS['ecs']->table('order_info') . ' AS o' .176 ' LEFT JOIN ' . $GLOBALS['ecs']->table('shipping') . ' AS s ON s.shipping_id = o.shipping_id' .177 " WHERE invoice_no > '' AND shipping_status = " . SS_SHIPPED .178 ' ORDER BY shipping_time DESC LIMIT 10';179 $all = $GLOBALS['db']->getAll($sql);180 181 foreach ($all AS $key => $row)182 {183 $plugin = ROOT_PATH . 'includes/modules/shipping/' . $row['shipping_code'] . '.php';184 185 if (file_exists($plugin))186 {187 include_once($plugin);188 189 $shipping = new $row['shipping_code'];190 $all[$key]['invoice_no'] = $shipping->query((string)$row['invoice_no']);191 }192 }193 194 clearstatcache();195 196 return $all;197 }198 199 /**200 * 获得最新的文章列表。201 *202 * @access private203 * @return array204 */205 function index_get_new_articles()206 {207 $sql = 'SELECT a.article_id, a.title, ac.cat_name, a.add_time, a.file_url, a.open_type, ac.cat_id, ac.cat_name ' .208 ' FROM ' . $GLOBALS['ecs']->table('article') . ' AS a, ' .209 $GLOBALS['ecs']->table('article_cat') . ' AS ac' .210 ' WHERE a.is_open = 1 AND a.cat_id = ac.cat_id AND ac.cat_type = 1' .211 ' ORDER BY a.article_type DESC, a.add_time DESC LIMIT ' . $GLOBALS['_CFG']['article_number'];212 $res = $GLOBALS['db']->getAll($sql);213 214 $arr = array();215 foreach ($res AS $idx => $row)216 {217 $arr[$idx]['id'] = $row['article_id'];218 $arr[$idx]['title'] = $row['title'];219 $arr[$idx]['short_title'] = $GLOBALS['_CFG']['article_title_length'] > 0 ?220 sub_str($row['title'], $GLOBALS['_CFG']['article_title_length']) : $row['title'];221 $arr[$idx]['cat_name'] = $row['cat_name'];222 $arr[$idx]['add_time'] = local_date($GLOBALS['_CFG']['date_format'], $row['add_time']);223 $arr[$idx]['url'] = $row['open_type'] != 1 ?224 build_uri('article', array('aid' => $row['article_id']), $row['title']) : trim($row['file_url']);225 $arr[$idx]['cat_url'] = build_uri('article_cat', array('acid' => $row['cat_id']), $row['cat_name']);226 }227 228 return $arr;229 }230 231 /**232 * 获得最新的团购活动233 *234 * @access private235 * @return array236 */237 function index_get_group_buy()238 {239 $time = gmtime();240 $limit = get_library_number('group_buy', 'index');241 242 $group_buy_list = array();243 if ($limit > 0)244 {245 $sql = 'SELECT gb.act_id AS group_buy_id, gb.goods_id, gb.ext_info, gb.goods_name, g.goods_thumb, g.goods_img ' .246 'FROM ' . $GLOBALS['ecs']->table('goods_activity') . ' AS gb, ' .247 $GLOBALS['ecs']->table('goods') . ' AS g ' .248 "WHERE gb.act_type = '" . GAT_GROUP_BUY . "' " .249 "AND g.goods_id = gb.goods_id " .250 "AND gb.start_time <= '" . $time . "' " .251 "AND gb.end_time >= '" . $time . "' " .252 "AND g.is_delete = 0 " .253 "ORDER BY gb.act_id DESC " .254 "LIMIT $limit" ;255 $res = $GLOBALS['db']->query($sql);256 257 while ($row = $GLOBALS['db']->fetchRow($res))258 {259 /* 如果缩略图为空,使用默认图片 */260 $row['goods_img'] = get_image_path($row['goods_id'], $row['goods_img']);261 $row['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);262 263 /* 根据价格阶梯,计算最低价 */264 $ext_info = unserialize($row['ext_info']);265 $price_ladder = $ext_info['price_ladder'];266 if (!is_array($price_ladder) || empty($price_ladder))267 {268 $row['last_price'] = price_format(0);269 }270 else271 {272 foreach ($price_ladder AS $amount_price)273 {274 $price_ladder[$amount_price['amount']] = $amount_price['price'];275 }276 }277 ksort($price_ladder);278 $row['last_price'] = price_format(end($price_ladder));279 $row['url'] = build_uri('group_buy', array('gbid' => $row['group_buy_id']));280 $row['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?281 sub_str($row['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $row['goods_name'];282 $row['short_style_name'] = add_style($row['short_name'],'');283 $group_buy_list[] = $row;284 }285 }286 287 return $group_buy_list;288 }289 290 /**291 * 取得拍卖活动列表292 * @return array293 */294 function index_get_auction()295 {296 $now = gmtime();297 $limit = get_library_number('auction', 'index');298 $sql = "SELECT a.act_id, a.goods_id, a.goods_name, a.ext_info, g.goods_thumb ".299 "FROM " . $GLOBALS['ecs']->table('goods_activity') . " AS a," .300 $GLOBALS['ecs']->table('goods') . " AS g" .301 " WHERE a.goods_id = g.goods_id" .302 " AND a.act_type = '" . GAT_AUCTION . "'" .303 " AND a.is_finished = 0" .304 " AND a.start_time <= '$now'" .305 " AND a.end_time >= '$now'" .306 " AND g.is_delete = 0" .307 " ORDER BY a.start_time DESC" .308 " LIMIT $limit";309 $res = $GLOBALS['db']->query($sql);310 311 $list = array();312 while ($row = $GLOBALS['db']->fetchRow($res))313 {314 $ext_info = unserialize($row['ext_info']);315 $arr = array_merge($row, $ext_info);316 $arr['formated_start_price'] = price_format($arr['start_price']);317 $arr['formated_end_price'] = price_format($arr['end_price']);318 $arr['thumb'] = get_image_path($row['goods_id'], $row['goods_thumb'], true);319 $arr['url'] = build_uri('auction', array('auid' => $arr['act_id']));320 $arr['short_name'] = $GLOBALS['_CFG']['goods_name_length'] > 0 ?321 sub_str($arr['goods_name'], $GLOBALS['_CFG']['goods_name_length']) : $arr['goods_name'];322 $arr['short_style_name'] = add_style($arr['short_name'],'');323 $list[] = $arr;324 }325 326 return $list;327 }328 329 /**330 * 获得所有的友情链接331 *332 * @access private333 * @return array334 */335 function index_get_links()336 {337 $sql = 'SELECT link_logo, link_name, link_url FROM ' . $GLOBALS['ecs']->table('friend_link') . ' ORDER BY show_order';338 $res = $GLOBALS['db']->getAll($sql);339 340 $links['img'] = $links['txt'] = array();341 342 foreach ($res AS $row)343 {344 if (!empty($row['link_logo']))345 {346 $links['img'][] = array('name' => $row['link_name'],347 'url' => $row['link_url'],348 'logo' => $row['link_logo']);349 }350 else351 {352 $links['txt'][] = array('name' => $row['link_name'],353 'url' => $row['link_url']);354 }355 }356 357 return $links;358 }359 360 ?>
调用结构层级
@ini_set('display_errors', 1); //设置显示错误
error_reporting(E_ALL ^ E_NOTICE); //报告所有的错误、通知,其中通知是脚本发现的可能是错误的地方
clearstatcache(); //清除缓存
define('ROOT_PATH', str_replace('install/includes/auto_init.php', '', str_replace('\\', '/', __FILE__))); //定义常量'ROOT_PATH'为网站根目录upload/
if (isset($_SERVER['PHP_SELF'])) //定义常量'PHP_SELF'为,$_SERVER['PHP_SELF'](当前执行脚本的文件名)或$_SERVER['SCRIPT_NAME'](当前执行脚本所在文件夹的路径)
{
define('PHP_SELF', $_SERVER['PHP_SELF']);
}
else
{
define('PHP_SELF', $_SERVER['SCRIPT_NAME']);
}
define('EC_CHARSET','utf-8'); //定义常量'EC_CHARSET'和'EC_DB_CHARSET'为UTF8
define('EC_DB_CHARSET','utf8');
require(ROOT_PATH . 'includes/lib_base.php'); //引用基础函数库,公用函数库,时间函数库,方便之后调用其中的函数
require(ROOT_PATH . 'includes/lib_common.php');
require(ROOT_PATH . 'includes/lib_time.php');
require(ROOT_PATH . 'includes/cls_error.php'); //生成一个错误类对象,模板为message.dwt,此处并未导入dwt文件,只是给类成员对象$_template赋值
$err = new ecs_error('message.dwt');
require(ROOT_PATH . 'install/includes/cls_template.php'); //生成一个模板类对象,并给类成员对象路径$path赋值
$smarty = new template(ROOT_PATH . 'install/templates/');
require(ROOT_PATH . 'install/includes/lib_auto_installer.php'); //调用lib_auto_installer.php实际上什么也没做,lib_auto_installer.php提供了一系列库函数以供调用
header('Content-type: text/html; charset='.EC_CHARSET); //告诉浏览器文本类型,编码格式,其中EC_CHARSET在前面定义过,值为'utf-8'。
@set_time_limit(360); //设定脚本的最长执行时间为360秒,如果超过了时间限制,程序结束
总结一下:
这个php为自动安装程序做了初始化工作,包括生成一个错误类ecs_error对象(执行构造函数将公有成员变量$_template值设为‘message.dwt’),生成一个模板类template对象(执行构造函数将公有成员变量$path值设为'/upload/install/templates/';告诉浏览器文本类型为text/html,编码格式utf8;设定脚本最长执行时间为360秒。
对于最后一条语句@set_time_limit(360)我有一些疑问想和大家分享一下。
set_time_limit函数实际上和修改php.ini中的max_execution_time可以起到一样的效果。这里说设定脚本最长执行时间为360秒,最初看到这条语句我陷入了一个误区:由于从顶层的index.php到这个php是一路require过来,并且在执行时require语句会被它所包含的文件直接替换,那么这个360秒到底是限制哪一个php的的执行时间。就在我百思不得其解时,看到了脚本这个词,然后忽然想到这个函数不是用来限制php文件的执行时间,php文件也不是脚本文件,这个函数是用来限制ecshop中用到的js之类脚本的执行时间,原谅我没文化,在此罗嗦了一大堆。