/firstrend/src/admin/Lib/Action/IntegrateAction.class.php
PHP | 718 lines | 474 code | 96 blank | 148 comment | 74 complexity | e7f1496e7235c9549f8d2e73025b0d7b MD5 | raw file
Possible License(s): Apache-2.0, AGPL-1.0, GPL-2.0, GPL-3.0, LGPL-2.1
- <?php
- // +----------------------------------------------------------------------
- // | Fanwe ???????? (Build on ThinkPHP)
- // +----------------------------------------------------------------------
- // | Copyright (c) 2009 http://www.fanwe.com All rights reserved.
- // +----------------------------------------------------------------------
- // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
- // +----------------------------------------------------------------------
- // | Author:
- // +----------------------------------------------------------------------
-
- class IntegrateAction extends CommonAction{
- /**
- * ???????????????
- *
- * @access public
- * @param string $directory ???????
- * @return array
- */
- function read_modules($directory = '.')
- {
- $dir = @opendir($directory);
- $set_modules = true;
- $modules = array();
-
- while (false !== ($file = @readdir($dir)))
- {
- if (preg_match("/^.*?\.php$/", $file))
- {
- include_once($directory. '/' .$file);
- }
- }
- @closedir($dir);
- unset($set_modules);
-
- foreach ($modules AS $key => $value)
- {
- ksort($modules[$key]);
- }
- ksort($modules);
-
- return $modules;
- }
-
- /**
- * ?????????
- *
- * @access public
- * @param
- *
- * @return void
- */
- function get_charset_list()
- {
- return array(
- 'utf8' => 'UTF-8',
- 'gbk' => 'GB2312/GBK',
- 'big5' => 'BIG5',
- );
- }
-
- /**
- * ???????????
- *
- * @access public
- * @param string $file_path ????
- * @param bool $rename_prv ??????????????rename()?????
- *
- * @return int ?????????{0 <= x <= 15}??????????????????????
- * ??????????????????????
- * ???rename()????????????????????????????????
- */
- function file_mode_info($file_path)
- {
- /* ?????????????????? */
- if (!file_exists($file_path))
- {
- return false;
- }
-
- $mark = 0;
-
- if (strtoupper(substr(PHP_OS, 0, 3)) == 'WIN')
- {
- /* ???? */
- $test_file = $file_path . '/cf_test.txt';
-
- /* ????? */
- if (is_dir($file_path))
- {
- /* ???????? */
- $dir = @opendir($file_path);
- if ($dir === false)
- {
- return $mark; //???????????????????????????
- }
- if (@readdir($dir) !== false)
- {
- $mark ^= 1; //???? 001?????? 000
- }
- @closedir($dir);
-
- /* ???????? */
- $fp = @fopen($test_file, 'wb');
- if ($fp === false)
- {
- return $mark; //???????????????????
- }
- if (@fwrite($fp, 'directory access testing.') !== false)
- {
- $mark ^= 2; //??????011???????? 010
- }
- @fclose($fp);
-
- @unlink($test_file);
-
- /* ????????? */
- $fp = @fopen($test_file, 'ab+');
- if ($fp === false)
- {
- return $mark;
- }
- if (@fwrite($fp, "modify test.\r\n") !== false)
- {
- $mark ^= 4;
- }
- @fclose($fp);
-
- /* ??????????rename()????? */
- if (@rename($test_file, $test_file) !== false)
- {
- $mark ^= 8;
- }
- @unlink($test_file);
- }
- /* ????? */
- elseif (is_file($file_path))
- {
- /* ?????? */
- $fp = @fopen($file_path, 'rb');
- if ($fp)
- {
- $mark ^= 1; //?? 001
- }
- @fclose($fp);
-
- /* ?????? */
- $fp = @fopen($file_path, 'ab+');
- if ($fp && @fwrite($fp, '') !== false)
- {
- $mark ^= 6; //??????? 111?????????011...
- }
- @fclose($fp);
-
- /* ??????????rename()????? */
- if (@rename($test_file, $test_file) !== false)
- {
- $mark ^= 8;
- }
- }
- }
- else
- {
- if (@is_readable($file_path))
- {
- $mark ^= 1;
- }
-
- if (@is_writable($file_path))
- {
- $mark ^= 14;
- }
- }
-
- return $mark;
- }
-
-
- /**
- *
- *
- * @access public
- * @param
- *
- * @return void
- */
- function save_integrate_config ($code, $user_field, $cfg)
- {
- $sql = "SELECT COUNT(*) as number FROM ".C("DB_PREFIX")."sys_conf WHERE name = 'INTEGRATE_CODE'";
- $number = M()->query($sql);
-
- if (intval($number[0]['number']) == 0)
- {
- $sql = "INSERT INTO ".C("DB_PREFIX")."sys_conf(name, is_show, status, val) VALUES ('INTEGRATE_CODE', 0, 1,'$code')";
- }
- else
- {
- $sql = "SELECT val FROM ".C("DB_PREFIX")."sys_conf WHERE name = 'INTEGRATE_CODE'";
- $tmp = M()->query($sql);
- $sql = "UPDATE ".C("DB_PREFIX")."sys_conf SET val = '$code' WHERE name = 'INTEGRATE_CODE'";
- }
-
- M()->query($sql);
-
- /* ????? */
- if (isset($_SERVER['HTTP_X_FORWARDED_HOST']))
- {
- $cur_domain = $_SERVER['HTTP_X_FORWARDED_HOST'];
- }
- elseif (isset($_SERVER['HTTP_HOST']))
- {
- $cur_domain = $_SERVER['HTTP_HOST'];
- }
- else
- {
- if (isset($_SERVER['SERVER_NAME']))
- {
- $cur_domain = $_SERVER['SERVER_NAME'];
- }
- elseif (isset($_SERVER['SERVER_ADDR']))
- {
- $cur_domain = $_SERVER['SERVER_ADDR'];
- }
- }
-
- /* ??????? */
- $int_domain = str_replace(array('http://', 'https://'), array('', ''), $cfg['integrate_url']);
- if (strrpos($int_domain, '/'))
- {
- $int_domain = substr($int_domain, 0, strrpos($int_domain, '/'));
- }
-
- if ($cur_domain != $int_domain)
- {
- $same_domain = true;
- $domain = '';
-
- /* ??????????????? */
- $cur_domain_arr = explode(".", $cur_domain);
- $int_domain_arr = explode(".", $int_domain);
-
- if (count($cur_domain_arr) != count($int_domain_arr) || $cur_domain_arr[0] == '' || $int_domain_arr[0] == '')
- {
- /* ??????? */
- $same_domain = false;
- }
- else
- {
- /* ???????????????????????? */
- $count = count($cur_domain_arr);
-
- for ($i = 1; $i < $count; $i++)
- {
- if ($cur_domain_arr[$i] != $int_domain_arr[$i])
- {
- $domain = '';
- $same_domain = false;
- break;
- }
- else
- {
- $domain .= ".$cur_domain_arr[$i]";
- }
- }
- }
-
- if ($same_domain == false)
- {
- /* ???????????? */
- $cfg['cookie_domain'] = '';
- $cfg['cookie_path'] = '/';
- }
- else
- {
- $cfg['cookie_domain'] = $domain;
- $cfg['cookie_path'] = '/';
- }
- }
- else
- {
- $cfg['cookie_domain'] = '';
- $cfg['cookie_path'] = '/';
- }
-
-
- $sql = "SELECT COUNT(*) as number FROM ".C("DB_PREFIX")."sys_conf WHERE name = 'INTEGRATE_CONFIG'";
- $number = M()->query($sql);
- if (intval($number[0]['number']) == 0)
- {
- $sql = "INSERT INTO ".C("DB_PREFIX")."sys_conf (name, is_show, status, val) ".
- "VALUES ('INTEGRATE_CONFIG', 0, 1, '" . serialize($cfg) . "')";
- }
- else
- {
- $sql = "UPDATE ".C("DB_PREFIX")."sys_conf SET val='". serialize($cfg) ."' ".
- "WHERE name='INTEGRATE_CONFIG'";
- }
-
- M()->query($sql);
-
-
- $sql = "SELECT COUNT(*) as number FROM ".C("DB_PREFIX")."sys_conf WHERE name = 'INTEGRATE_FIELD_ID'";
- $number = M()->query($sql);
- if (intval($number[0]['number']) == 0)
- {
- $sql = "INSERT INTO ".C("DB_PREFIX")."sys_conf (name, is_show, status, val) ".
- "VALUES ('INTEGRATE_FIELD_ID', 0, 1, '" . $user_field . "')";
- }
- else
- {
- $sql = "UPDATE ".C("DB_PREFIX")."sys_conf SET val='". $user_field ."' ".
- "WHERE name='INTEGRATE_FIELD_ID'";
- }
-
- M()->query($sql);
- return true;
- }
-
- public function index() {
- //echo FANWE_ROOT.'core/include/integrates';
- $modules = $this->read_modules(FANWE_ROOT.'core/include/integrates');
- //dump($modules);exit;
- $code = fanweC('INTEGRATE_CODE');
- //dump($code);
-
- for ($i = 0; $i < count($modules); $i++)
- {
- $modules[$i]['installed'] = ($modules[$i]['code'] == $code) ? 1 : 0;
- }
-
- $allow_set_points = fanweC('INTEGRATE_CODE') == 'fanwe' ? 0 : 1;
-
- $allow_set_points = 0;
- $this->assign('allow_set_points', $allow_set_points);
- $this->assign('modules', $modules);
-
- //dump($modules);
-
- //assign_query_info();
- //$this->display('integrates_list.htm');
- $this->display ();
- }
-
- /*------------------------------------------------------ */
- //-- ??????????
- /*------------------------------------------------------ */
- public function install()
- {
- /* ??ucenter??????uc_client?uc_client/data???? */
- if ($_GET['code'] == 'ucenter')
- {
- $uc_client_dir = $this->file_mode_info(FANWE_ROOT. 'uc_client/data');
- if ($uc_client_dir === false)
- {
- $this->error('uc_client?????????uc_client???????????????');
- //return;
- }
- if ($uc_client_dir < 7)
- {
- $this->error ('uc_client/data?????????uc_client/data???????777');
- //return;
- }
- //eval()
-
- $a = M()->query('select user_name from '.C("DB_PREFIX").'user group by user_name having count(*) > 1');
- if (!empty($a)){
- $this->error ('???????????????.<br>'.M('User')->getLastSql());
- }
- }
-
- if ($_GET['code'] == 'fanwe')
- {
- D("SysConf")->where("status=1 and name='INTEGRATE_CODE'")->setField("val", "fanwe");
- D("SysConf")->where("status=1 and name='INTEGRATE_FIELD_ID'")->setField("val", "uid");
- D("SysConf")->where("status=1 and name='POINTS_RULE'")->setField("val", "");
- //clear_cache_files();
-
- clearCache();
-
- $this->assign ('jumpUrl', u('Integrate/index'));
- $this->success ( '???????????????');
- return;
- }
- else
- {
- //D("User")->where("sync_flag > 0")->setField(array("sync_flag","nickname"), array("0",""));
-
- $set_modules = true;
- include_once(FANWE_ROOT.'core/include/integrates/'.$_GET['code'].".php");
- $set_modules = false;
-
- $cfg = $modules[0]['default'];
- $cfg['integrate_url'] = "http://";
-
- if (empty($cfg['db_charset']))
- $cfg['db_charset'] = 'UTF-8';
-
- $this->assign('cfg', $cfg);
- $this->assign('save', 0);
- $this->assign('set_list', $this->get_charset_list());
- $this->assign('code', $_GET['code']);
- $this->assign('user_field', $modules[0]['user_field']);
-
- //dump($this->get_charset_list());
- $this->display("install");
- }
- }
-
-
- /*------------------------------------------------------ */
- //-- ??UCenter??
- /*------------------------------------------------------ */
- public function setup_ucenter()
- {
- require_once(FANWE_ROOT.'core/class/transport.class.php');
- $result = array('status' => 0, 'info' => '', 'data' => '');
-
-
- $app_type = 'OTHER';
- $app_name = 'FANWE';//fanweC('SHOP_NAME'); //$db->getOne('SELECT value FROM ' . $ecs->table('shop_config') . " WHERE code = 'shop_name'");
- $app_url = 'http://'.$_SERVER['HTTP_HOST'].__ROOT__;
- $app_charset = 'UTF-8';
- $app_dbcharset = strtolower((str_replace('-', '', 'UTF-8')));
- $ucapi = trim($_REQUEST['uc_url']);
-
- $ucfounderpw = trim($_REQUEST['uc_pass']);
- $postdata ="m=app&a=add&ucfounder=&ucfounderpw=".urlencode($ucfounderpw)."&apptype=".urlencode($app_type).
- "&appname=".urlencode($app_name)."&appurl=".urlencode($app_url)."&appip=&appcharset=".$app_charset.
- '&appdbcharset='.$app_dbcharset;
- $t = new transport;
-
- $ucconfig = $t->request($ucapi.'/index.php', $postdata);
- // dump($ucconfig);exit;
- $ucconfig = $ucconfig['body'];
- if(empty($ucconfig))
- {
- //ucenter ????
- $result['error'] = 1;
- $result['info'] = '????:'.$ucapi.'/index.php?'.$postdata;
-
- }
- elseif($ucconfig == '-1')
- {
- //???????
- $result['error'] = 1;
- $result['info'] = '???????';
- }
- else
- {
- list($appauthkey, $appid) = explode('|', $ucconfig);
- if(empty($appauthkey) || empty($appid))
- {
- //ucenter ??????
- $result['error'] = 1;
- $result['info'] = '??????:'.$ucconfig;
- }
- else
- {
- $result['error'] = 0;
- $result['data'] = $ucconfig;
- $result['info'] = '??????????';
- }
- }
- return $result;
- }
-
- /*------------------------------------------------------ */
- //-- ?????UCenter?????
- /*------------------------------------------------------ */
- public function save_uc_config_first()
- {
-
-
- $result = $this->setup_ucenter();
- if ($result['error'] == 1)
- $this->error($result['info']);
-
-
- $ucconfig = $result['data'];
- $user_field = $_REQUEST['user_field'];
- $code = $_REQUEST['code'];
- //echo $ucconfig; exit;
- list($appauthkey, $appid, $ucdbhost, $ucdbname, $ucdbuser, $ucdbpw, $ucdbcharset, $uctablepre, $uccharset, $ucapi, $ucip) = explode('|', $ucconfig);
- $uc_url = !empty($ucapi)? $ucapi : trim($_REQUEST['uc_url']);
- $cfg = array(
- 'uc_id' => $appid,
- 'uc_key' => $appauthkey,
- 'uc_url' => $uc_url,
- 'uc_ip' => '',
- 'uc_connect' => 'post',
- 'uc_charset' => $uccharset,
- 'db_host' => $ucdbhost,
- 'db_user' => $ucdbuser,
- 'db_name' => $ucdbname,
- 'db_pass' => $ucdbpw,
- 'db_pre' => $uctablepre,
- 'db_charset' => strtolower($ucdbcharset),
- );
- /* ??UC??? */
- //$cfg['uc_lang'] = $_LANG['uc_lang'];
-
- /* ?????????????? */
- $_SESSION['cfg'] = $cfg;
- $_SESSION['code'] = $code;
-
- /* ?????? */
- if ($_POST['save'] == 1)
- {
- if ($this->save_integrate_config($code, $user_field, $cfg))
- {
- $this->assign ('jumpUrl', U('Integrate/index'));
- $this->success ('????!');
- }
- else
- {
- $this->assign ('jumpUrl', U('Integrate/index'));
- $this->error('????!');
- }
- }
-
- /* ?????? */
- $this->save_integrate_config($code, $user_field, $cfg);
-
- //include_once(VENDOR_PATH."mysql.php");
- //$ucdb = new cls_mysql($cfg['db_host'], $cfg['db_user'], $cfg['db_pass'], $cfg['db_name'], $cfg['db_charset'], null, 1);
-
- $user_startid_intro = "?????????????????";
-
- $this->assign('user_startid_intro', $user_startid_intro);
- //$this->assign('user_startid_intro', "??????ID?".$user_maxid."; UC????ID?".$maxuid."?<br>?? ID ? 888 ?????? ".$maxuid."+888 ???");
- $this->display('uc_import');
- }
-
-
- /*------------------------------------------------------ */
- //-- ??UCenter?????
- /*------------------------------------------------------ */
- public function save_uc_config()
- {
- $code = $_POST['code'];
- $user_field = $_POST['user_field'];
-
- $cfg = unserialize(fanweC('INTEGRATE_CONFIG'));
- if ($_POST['cfg']['uc_connect'] == 'mysql'){
- include_once(VENDOR_PATH."integrates/".$code.".php");
- $_POST['cfg']['quiet'] = 1;
- $cls_user = new $code ($_POST['cfg']);
-
- if ($cls_user->error)
- {
- if ($cls_user->error == 1)
- {
- $this->error('??????????????');
- }
- elseif ($cls_user->error == 2)
- {
- $this->error('?????????????????????');
- }
- elseif ($cls_user->error == 1049)
- {
- $this->error('??????');
- }
- else
- {
- sys_msg($cls_user->db->error());
- }
- }
- }
-
-
- /* ????????? */
- $cfg = array_merge($cfg, $_POST['cfg']);
-
- /* ?????? */
- if ($this->save_integrate_config($code, $user_field, $cfg))
- {
- $this->assign ('jumpUrl', U('Integrate/index'));
- $this->success('????!');
- }
- else
- {
- $this->assign ('jumpUrl', U('Integrate/index'));
- $this->error('????!');
- }
- }
-
-
- public function import_user()
- {
- //???????????????????????????
- ini_set("memory_limit","100M");
-
- $cfg = unserialize(fanweC('INTEGRATE_CONFIG'));// INTEGRATE_CONFIG $_SESSION['cfg'];
-
- //echo VENDOR_PATH."mysql.php"; exit;
- //include_once(VENDOR_PATH."mysql.php");
- /*
- //include_once(__ROOT__."/app/source/class/mysql_db.php");
- include_once(FANWE_ROOT."/core/class/db.class.php");
- include_once(FANWE_ROOT."/core/class/mysql.class.php");
- $db_cfg = array(
- 'DB_HOST'=>$cfg['db_host'],
- 'DB_NAME'=>$cfg['db_name'],
- 'DB_USER'=>$cfg['db_user'],
- 'DB_PWD'=>$cfg['db_pass'],
- 'DB_PORT'=>3306,
- 'DB_PREFIX'=>$cfg['db_pre'],
- );
- $class = 'FDbMySql';
- $ucdb = &FDB::object($class);
- $ucdb->setConfig($db_cfg);
- $ucdb->connect();
- */
-
- $db_cfg = array(
- 'dbhost'=>$cfg['db_host'],
- 'dbname'=>$cfg['db_name'],
- 'dbuser'=>$cfg['db_user'],
- 'dbpwd'=>$cfg['db_pass'],
- 'dbcharset'=>$cfg['db_charset'],
- 'pconnect'=>'',
- );
-
- Vendor('mysql');
- $ucdb = new mysqldb($db_cfg);
- //dump($ucdb); exit;
- Log::record("==================uc???? begin======================");
-
-
- $item_list = M()->query("SELECT uid as id,user_name,password as user_pwd, ucenter_id, email, '' as last_ip,reg_time as create_time FROM " . C("DB_PREFIX") . "user ORDER BY `id` ASC");
- foreach ($item_list AS $data)
- {
- $salt = rand(100000, 999999);
- $password = md5($data['user_pwd'].$salt); //uc?????md5(md5(??)+???)
- if (strtolower($cfg['db_charset']) == 'gbk'){
- $data['username'] = addslashes(utf8ToGB($data['user_name']));
- }else{
- $data['username'] = addslashes($data['user_name']);
- }
-
- $uc_userinfo = $ucdb->fetchFirst("SELECT `uid`, `password`, `salt` FROM ".$cfg['db_pre']."members WHERE `username`='$data[username]'");
- //dump($uc_userinfo);
- if(!$uc_userinfo) //???uc????????????UC?
- {
- $ucdb->query("INSERT INTO ".$cfg['db_pre']."members SET username='$data[username]', password='$password', email='$data[email]', regip='$data[last_ip]', regdate='$data[create_time]', salt='$salt'", 'SILENT');
- $lastuid = $ucdb->insertId();
- $ucdb->query("INSERT INTO ".$cfg['db_pre']."memberfields SET uid='$lastuid'",'SILENT');
-
- M()->query("UPDATE " . C("DB_PREFIX") . "user SET `ucenter_id`='" . $lastuid . "' WHERE `uid`='" . $data['id'] . "'");
-
-
- //Log::record("INSERT INTO ".$cfg['db_pre']."members SET username='$data[username]', password='$password', email='$data[email]', regip='$data[last_ip]', regdate='$data[create_time]', salt='$salt'");
- //Log::record("INSERT INTO ".$cfg['db_pre']."memberfields SET uid='$lastuid'");
-
- //M()->query("UPDATE " . C("DB_PREFIX") . "user SET `id`= $lastuid "." where id = ".$data['id']);
- }
- else
- {
- M()->query("UPDATE " . C("DB_PREFIX") . "user SET `ucenter_id`='" . $uc_userinfo['uid'] . "' WHERE `uid`='" . $data['id'] . "'");
- /*
- if ($merge_method == 1)//1:??UC??????????????????
- {
- if (md5($data['user_pwd'].$uc_userinfo['salt']) == $uc_userinfo['password'])
- {
- //$merge_uid[] = $data['id'];
- $uc_uid[] = array('user_id' => $data['id'], //???ID
- 'uid' => $uc_userinfo['uid'] //???ID
- );
- continue;
- }
- }
- */
- $ucdb->query("REPLACE INTO ".$cfg['db_pre']."mergemembers SET appid='".UC_APPID."', username='$data[username]'", 'SILENT');
- //Log::record("REPLACE INTO ".$cfg['db_pre']."mergemembers SET appid='".UC_APPID."', username='$data[username]'");
- }
- }
-
-
- //M()->query("UPDATE " . C("DB_PREFIX") . "user SET `ucenter_id`= ucenter_id_tmp");
-
- //Log::record("==================uc???? end======================");
- //Log::save();
-
- clearCache();
-
- $this->assign ('jumpUrl', u('Integrate/index'));
- $this->success ('?????????? UCenter');
- }
-
- public function edit()
- {
- /*
- $set_modules = true;
- include_once(FANWE_ROOT.'core/include/integrates/'.$_GET['code'].".php");
- $set_modules = false;
- */
- $user_field = fanweC('INTEGRATE_FIELD_ID');
-
- $this->assign('user_field', $user_field);// $modules[0]['user_field']);
-
- if ($_GET['code'] == 'fanwe')
- {
- $this->assign ('jumpUrl', U('Integrate/index'));
- $this->error('????FANWE?????????????');
- }
- else
- {
- $cfg = unserialize(fanweC('INTEGRATE_CONFIG'));
- $this->assign('save', 1);
- $this->assign('set_list', $this->get_charset_list());
- $this->assign('code', $_GET['code']);
- $this->assign('cfg', $cfg);
- //dump($this->get_charset_list());
- $this->display('edit');
- }
- }
- }
- ?>