码迷,mamicode.com
首页 > Windows程序 > 详细

onethink和phpwind共用

时间:2015-03-13 16:36:26      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:phpwind   onethink   thinkphp   

将onethink和phpwind数据库安装在一起,使用通用的表前缀。

将onethink的member表指向phpwind有user表

以下为onethink安装在根目录下,phpwind安装在bbs目录下的情况

修改onethink中的is_login函数

function is_login(){
	$site=include('./bbs/data/cache/config.php');
	C('BBS_SITE_SET',$site['data']['site']);		
	if (!($userCookie = \Org\util\Pw::getCookie('winduser'))) {
			return 0;
	} else {
			list($uid, $password) = explode("\t", \Org\util\Pw::decrypt($userCookie));
			$user_session = session('user_auth');
			if (empty($user_session)||$user_session['uid']!=$uid) {
				//$user = new User\Api\UserApi();
				//$info = $user->info($uid);

				/* 记录登录SESSION和COOKIES */
				$auth = array(
					'uid'             => $uid,
					'username'        => get_username($uid),
					'last_login_time' => NOW_TIME,
				);
				session('user_auth', $auth);
				session('user_auth_sign', data_auth_sign($auth));
			}
			return $uid;
	}
/*
        $user = session('user_auth');
        if (empty($user)) {
            return 0;
        } else {
            return session('user_auth_sign') == data_auth_sign($user) ? $user['uid'] : 0;
        }
*/
}
think库中添加Org/util/pw.class.php和Org/WindCookie.class.php

1.WindCookie.class.php

<?php
namespace Org;

/**
 * cookie操作类
 * 
 * 使用的时候全部采用静态的方式使用该类中的所有方法:
 * <code>
 * Wind::import('WIND:http.cookie.WindCookie');
 * WindCookie::set('name', 'test');
 * </code>
 * 
 * @author Qian Su <aoxue.1988.su.qian@163.com>
 * @copyright ©2003-2103 phpwind.com
 * @license http://www.windframework.com
 * @version $Id: WindCookie.php 3760 2012-10-11 08:02:25Z yishuo $
 * @package http
 * @subpackage cookie
 */

class WindCookie {



	/**
	 * 设置cookie
	 * 
	 * @param string $name cookie名称
	 * @param string $value cookie值,默认为null
	 * @param boolean $encode 是否使用 MIME base64 对数据进行编码,默认是false即不进行编码
	 * @param string|int $expires 过期时间,默认为null即会话cookie,随着会话结束将会销毁
	 * @param string $path cookie保存的路径,默认为null即采用默认
	 * @param string $domain cookie所属域,默认为null即不设置
	 * @param boolean $secure 是否安全连接,默认为false即不采用安全链接
	 * @param boolean $httponly 是否可通过客户端脚本访问,默认为false即客户端脚本可以访问cookie
	 * @return boolean 设置成功返回true,失败返回false
	 */

	public static function set($name, $value = null, $encode = false, $expires = null, $path = null, $domain = null, $secure = false, $httponly = false) {

		if (empty($name)) return false;

		$encode && $value && $value = base64_encode($value);

		$path = $path ? $path : '/';

		setcookie($name, $value, $expires, $path, $domain, $secure, $httponly);

		return true;

	}



	/**
	 * 根据cookie的名字删除cookie
	 * 
	 * @param string $name cookie名称
	 * @return boolean 删除成功返回true
	 */

	public static function delete($name) {

		if (self::exist($name)) {

			self::set($name, '');

			unset($_COOKIE[$name]);

		}

		return true;

	}



	/**
	 * 取得指定名称的cookie值
	 * 
	 * @param string $name cookie名称
	 * @param boolean $dencode 是否对cookie值进行过解码,默认为false即不用解码
	 * @return mixed 获取成功将返回保存的cookie值,获取失败将返回false
	 */

	public static function get($name, $dencode = false) {

		if (self::exist($name)) {

			$value = $_COOKIE[$name];

			$value && $dencode && $value = base64_decode($value);

			return $value ? $value : $value;

		}

		return false;

	}



	/**
	 * 移除全部cookie
	 * 
	 * @return boolean 移除成功将返回true
	 */

	public static function deleteAll() {

		$_COOKIE = array();

		return true;

	}



	/**
	 * 判断cookie是否存在
	 * 
	 * @param string $name cookie名称
	 * @return boolean 如果不存在则返回false,否则返回true
	 */

	public static function exist($name) {

		return isset($_COOKIE[$name]);

	}

}

2.pw.class.php

<?php
namespace Org\util;
use Org\WindCookie;
/**
 * 工具类库
 *
 * @author Jianmin Chen <sky_hold@163.com>
 * @copyright ©2003-2103 phpwind.com
 * @license http://www.phpwind.com
 * @version $Id: Pw.php 28776 2013-05-23 08:46:10Z jieyin $
 * @package library
 */
class Pw {
	/**
	 * 取得指定名称的cookie值
	 *
	 * @param string $name cookie名称
	 * @param string $pre cookie前缀,默认为null即没有前缀
	 * @return boolean
	 */
	public static function getCookie($name) {
		$site = C('BBS_SITE_SET');
		$pre = $site['cookie.pre'];
		$pre && $name = $pre . '_' . $name;
		return WindCookie::get($name);
	}

	/**
	 * 设置cookie
	 *
	 * @param string $name cookie名称
	 * @param string $value cookie值,默认为null
	 * @param string|int $expires 过期时间,默认为null即会话cookie,随着会话结束将会销毁
	 * @param string $pre cookie前缀,默认为null即没有前缀
	 * @param boolean $httponly
	 * @return boolean
	 */
	public static function setCookie($name, $value = null, $expires = null, $httponly = false) {
		$path = $domain = null;
		$site = C('BBS_SITE_SET');
		$pre = $site['cookie.pre'];
		$pre && $name = $pre . '_' . $name;
		$expires && $expires += time();
		return WindCookie::set($name, $value, false, $expires, $path, $domain, false, $httponly);
	}

	/**
	 * 加密方法
	 *
	 * @param string $str
	 * @param string $key
	 * @return string
	 */
	public static function encrypt($str, $key = '') {
		$site = C('BBS_SITE_SET');
		$key || $key = $site['hash'];
		return base64_encode(self::iencrypt($str, $key));
	}
	
	/**
	 * 解密方法
	 *
	 * @param string $str
	 * @param string $key
	 * @return string
	 */
	public static function decrypt($str, $key = '') {
		$site = C('BBS_SITE_SET');
		$key || $key = $site['hash'];
		return self::idecrypt(base64_decode($str), $key);
	}

	/**
	 * 密码加密存储
	 *
	 * @param string $pwd
	 * @return string
	 */
	public static function getPwdCode($pwd) {
		$site = C('BBS_SITE_SET');
		return md5($pwd . $site['hash']);
	}

	public function iencrypt($str, $key) {
		if ($str == '') return '';
		if (!$key || !is_string($key)) {
			return '';
		}
		$v = self::str2long($str, true);
		$k = self::str2long($key, false);
		if (count($k) < 4) {
			for ($i = count($k); $i < 4; $i++) {
				$k[$i] = 0;
			}
		}
		$n = count($v) - 1;
		
		$z = $v[$n];
		$y = $v[0];
		$delta = 0x9E3779B9;
		$q = floor(6 + 52 / ($n + 1));
		$sum = 0;
		while (0 < $q--) {
			$sum = self::int32($sum + $delta);
			$e = $sum >> 2 & 3;
			for ($p = 0; $p < $n; $p++) {
				$y = $v[$p + 1];
				$mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(
					($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
				$z = $v[$p] = self::int32($v[$p] + $mx);
			}
			$y = $v[0];
			$mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(
				($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
			$z = $v[$n] = self::int32($v[$n] + $mx);
		}
		return self::long2str($v, false);
	}

	/* (non-PHPdoc)
	 * @see IWindSecurity::decrypt()
	 */
	public function idecrypt($str, $key) {
		if ($str == '') return '';
		if (!$key || !is_string($key)) {
			return '';
		}
		$v = self::str2long($str, false);
		$k = self::str2long($key, false);
		if (count($k) < 4) {
			for ($i = count($k); $i < 4; $i++) {
				$k[$i] = 0;
			}
		}
		$n = count($v) - 1;
		
		$z = $v[$n];
		$y = $v[0];
		$delta = 0x9E3779B9;
		$q = floor(6 + 52 / ($n + 1));
		$sum = self::int32($q * $delta);
		while ($sum != 0) {
			$e = $sum >> 2 & 3;
			for ($p = $n; $p > 0; $p--) {
				$z = $v[$p - 1];
				$mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(
					($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
				$y = $v[$p] = self::int32($v[$p] - $mx);
			}
			$z = $v[$n];
			$mx = self::int32((($z >> 5 & 0x07ffffff) ^ $y << 2) + (($y >> 3 & 0x1fffffff) ^ $z << 4)) ^ self::int32(
				($sum ^ $y) + ($k[$p & 3 ^ $e] ^ $z));
			$y = $v[0] = self::int32($v[0] - $mx);
			$sum = self::int32($sum - $delta);
		}
		return self::long2str($v, true);
	}

	/**
	 * 长整型转换为字符串
	 *
	 * @param long $v
	 * @param boolean $w
	 * @return string
	 */
	private function long2str($v, $w) {
		$len = count($v);
		$s = array();
		for ($i = 0; $i < $len; $i++)
			$s[$i] = pack("V", $v[$i]);
		return $w ? substr(join('', $s), 0, $v[$len - 1]) : join('', $s);
	}

	/**
	 * 字符串转化为长整型
	 *
	 * @param string $s
	 * @param boolean $w
	 * @return Ambigous <multitype:, number>
	 */
	private function str2long($s, $w) {
		$v = unpack("V*", $s . str_repeat("\0", (4 - strlen($s) % 4) & 3));
		$v = array_values($v);
		if ($w) $v[count($v)] = strlen($s);
		return $v;
	}

	/**
	 * @param int $n
	 * @return number
	 */
	private function int32($n) {
		while ($n >= 2147483648)
			$n -= 4294967296;
		while ($n <= -2147483649)
			$n += 4294967296;
		return (int) $n;
	}

}

onethink中的user模块作相应修改

onethink和phpwind共用

标签:phpwind   onethink   thinkphp   

原文地址:http://blog.csdn.net/kslib/article/details/44241869

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!