English Sentence Loading...
英语句子加载中...
用php实现一个双向队列
作者:蘑菇 日期:2010-07-23 00:54
代码实现:
这道题从难度上讲其实不是很难,它主要考察了phper以下几个方面的技能:
1. 当然是双向队列的定义,这个就不多做解释了。
2. 考察对函数是否熟悉。
3. 考察OOP编程。
4. 考察程序员的代码规范和编程习惯。
下载: deque.php
- <?php
- class deque
- {
- public $queue = array();
- public $length = 0;
- public function frontAdd($node){
- array_unshift($this->queue,$node);
- $this->countqueue();
- }
- public function frontRemove(){
- $node = array_shift($this->queue);
- $this->countqueue();
- return $node;
- }
- public function rearAdd($node){
- array_push($this->queue,$node);
- $this->countqueue();
- }
- public function rearRemove(){
- $node = array_pop($this->queue);
- $this->countqueue();
- return $node;
- }
- public function countqueue(){
- $this->length = count($this->queue);
- }
- }
- ?>
这道题从难度上讲其实不是很难,它主要考察了phper以下几个方面的技能:
1. 当然是双向队列的定义,这个就不多做解释了。
2. 考察对函数是否熟悉。
3. 考察OOP编程。
4. 考察程序员的代码规范和编程习惯。
Joomla 用户密码的生成原理
作者:蘑菇 日期:2010-07-22 01:09
在网上看了许多有关Joomla用户密码的问题,有好多人对此有些疑问,也有些人说Joomla的密码不是md5加密的。这里,我想大概说一下Joomla用户密码的生成过程。
当然,Joomla对密码的加密方式有很多种,有ssha、md5、base64等等,但是因为我们很少触及这些设置,所以大多数还是在使用默认的md5加密方式。
有些人会问:为什么同样的密码每次加密后的结果都不一样呢?原因就是因为Joomla在加密的时候添加了一个随机key。由于这个key是随机的,所以每次加密后的结果也会不一样。
举例说明:
我的Joomla的明文密码是 123456,加密后的结果是
6fa7069f6c2b4a7abc376669113acab8:cW2r7QqdGQrp2Rw9sbxCxN85nSek6tlV
我们看到,加密后的字符串不是普通的md5格式,它被“:”分成了两段,这也是致使有些人怀疑它不是md5的原因。其实后面的这段是key,在每次添加新用户或修改密码时,这一串是随机生成的,而前边的密码才是md5加密后的密码,加密方法为:
md5(明文密码.key)
如上面的密码应该为
6fa7069f6c2b4a7abc376669113acab8 == md5('123456cW2r7QqdGQrp2Rw9sbxCxN85nSek6tlV')
在验证用户密码时也通过这个key做一下md5验证就可以了。
生成这个key和加密密码的函数分别为 getSalt() 和 getCryptedPassword()
它们在 libraries/joomla/user/helper.php 文件中被定义,在 libraries/joomla/user/user.php 的bind()方法中被调用。
当然,Joomla对密码的加密方式有很多种,有ssha、md5、base64等等,但是因为我们很少触及这些设置,所以大多数还是在使用默认的md5加密方式。
有些人会问:为什么同样的密码每次加密后的结果都不一样呢?原因就是因为Joomla在加密的时候添加了一个随机key。由于这个key是随机的,所以每次加密后的结果也会不一样。
举例说明:
我的Joomla的明文密码是 123456,加密后的结果是
6fa7069f6c2b4a7abc376669113acab8:cW2r7QqdGQrp2Rw9sbxCxN85nSek6tlV
我们看到,加密后的字符串不是普通的md5格式,它被“:”分成了两段,这也是致使有些人怀疑它不是md5的原因。其实后面的这段是key,在每次添加新用户或修改密码时,这一串是随机生成的,而前边的密码才是md5加密后的密码,加密方法为:
md5(明文密码.key)
如上面的密码应该为
6fa7069f6c2b4a7abc376669113acab8 == md5('123456cW2r7QqdGQrp2Rw9sbxCxN85nSek6tlV')
在验证用户密码时也通过这个key做一下md5验证就可以了。
生成这个key和加密密码的函数分别为 getSalt() 和 getCryptedPassword()
它们在 libraries/joomla/user/helper.php 文件中被定义,在 libraries/joomla/user/user.php 的bind()方法中被调用。
标签: joomla,php php
php 分页类
作者:蘑菇 日期:2010-06-01 23:22
这个分页类融合了数据库查询的功能,用起来还比较方便。下面这个是用了ADODB类来操作数据库,关于ADODB的操作,在这里就不多说了。
使用举例:
下载: dbclass.php
- <?php
- /**
- * FileName : dbclass.php
- * Description: Simple db class
- * Author : Liubing(liubing@eduicc.com)
- */
- class dbclass
- {
- var $dblink;
- var $_sql;
- var $primary_key;
- function dbclass($primary_key='id')
- {
- $this->connectDB();
- $this->primary_key = $primary_key;
- }
- function connectDB()
- {
- global $db_config;
- if(is_object($this->dblink)) return;
- $this->dblink = &ADONewConnection('mysql');
- $this->dblink->PConnect($db_config["db_host"],$db_config["db_user"],$db_config["db_password"],$db_config["db_name"]);
- }
- function E($sql='')
- {
- $sql != '' && $this->_sql = $sql;
- $this->dblink->Execute("SET NAMES UTF8");
- return $this->dblink->Execute($this->_sql);
- }
- function Store($fields,$tb,$key='')
- {
- if(count($fields) == 0) return false;
- foreach($fields as $field => $value){
- $params[] = "`{$field}`='".$this->safestring($value)."'";
- }
- $key = intval($key);
- if($key > 0){ //modify
- if($this->primary_key == '' || $key < 1) return false;
- $sql = "UPDATE `{$tb}` SET ".implode(",",$params)." WHERE `{$this->primary_key}`='{$key}'";
- }else{ //add new record
- $sql = "INSERT INTO `{$tb}` SET ".implode(",",$params);
- }
- return $this->E($sql);
- }
- function getResults($params,$tb,$orderfield='')
- {
- $sql = "SELECT * FROM `{$tb}` ";
- if(is_array($params) && count($params) > 0){
- $sql.= " WHERE ".implode(" AND ",$params);
- }
- if($orderfield != '') $sql.= " ORDER BY `{$orderfield}`";
- $rs = $this->E($sql);
- $offset = 0;
- while(!$rs->EOF){
- $rs->fields["offset"] = ++$offset;
- $results[] = $rs->fields;
- $rs->MoveNext();
- }
- if(count($results) == 0) return false;
- elseif(count($results) == 1) return $results[0];
- else return $results;
- }
- function getRow($key,$tb)
- {
- if($this->primary_key == '' || $key < 1) return false;
- $params[] = "`{$this->primary_key}`='{$key}'";
- $results = $this->getResults($params,$tb);
- return $results;
- }
- function delRow($key,$tb)
- {
- if($this->primary_key == '' || $key < 1) return false;
- $sql = "DELETE FROM `{$tb}` WHERE `".$this->primary_key."`={$key}";
- $this->E($sql);
- }
- function getNum($tb)
- {
- $sql = "SELECT COUNT(*) AS `t` FROM `{$tb}`";
- $rs = $this->E($sql);
- return $rs->fields['t'];
- }
- function getMax($field,$table,$params=array())
- {
- $sql = "SELECT MAX(`{$field}`) AS `{$field}` FROM `{$table}` WHERE 1 ";
- foreach($params as $f => $val){
- $sql.= " AND `{$f}`='{$val}'";
- }
- $rs = $this->E($sql);
- return $rs->fields[$field];
- }
- //html,mysql
- function safestring($string,$mode="3")
- {
- switch($mode)
- {
- case "0":
- break;
- case "1":
- $string = mysql_escape_string($string);
- break;
- case "2":
- $string = strip_tags($string);
- break;
- case "3":
- $string = strip_tags(mysql_escape_string($string));
- break;
- default:
- break;
- }
- return $string;
- }
- }
下载: pageclass.php
- <?php
- /**
- * FileName : pageclass.php
- * Description: pagination class
- * Author : Liubing(liubing@eduicc.com)
- */
- require_once dirname(__FILE__).'/dbclass.php';
- class pageclass extends dbclass
- {
- var $page;
- var $pagesize;
- var $pagesql;
- var $results;
- var $offset;
- var $params;
- var $totalpage;
- var $totalnum;
- var $pagelink;
- var $pagerange;
- function pageclass($page=1,$params=array(),$pagesize=10)
- {
- dbclass::dbclass();
- $page = intval($page);
- $pagesize = intval($pagesize);
- $this->page = $page < 1 ? 1 : $page;
- $this->pagesize = $pagesize < 10 ? 10 : $pagesize;
- $this->offset = ($this->page - 1) * $this->pagesize;
- $this->params = $params;
- $this->pagerange = 10;
- }
- function loadpage($sql)
- {
- $sql = preg_replace("/select/i","select",$sql);
- $sqlitems = explode("select",$sql);
- unset($sqlitems[0]);
- $newsql = implode("SELECT",$sqlitems);
- $newsql = "SELECT SQL_CALC_FOUND_ROWS ".$newsql." LIMIT {$this->offset},{$this->pagesize}";
- $rs = $this->E($newsql);
- $pagesql = "SELECT FOUND_ROWS() AS `allnum`";
- $pagers = $this->E($pagesql);
- $pageresult = $pagers->fields;
- $this->totalnum = $pageresult["allnum"];
- $this->setpage();
- $offset = $this->offset;
- while(!$rs->EOF){
- $offset++;
- $rs->fields['offset'] = $offset;
- $this->results[] = $rs->fields;
- $rs->MoveNext();
- }
- }
- function setpage()
- {
- if(count($this->params) > 0){
- foreach($this->params as $key => $value){
- $params.= "{$key}={$value}&";
- }
- }
- $this->totalpage = ceil($this->totalnum/$this->pagesize);
- $this->pagelink = "<span class='nolink'>".$this->page."/".$this->totalpage."</span>";
- //FIRST PREV
- if($this->page > 1){
- $this->pagelink.= " <a href='?{$params}page=1'>FIRST</a> ";
- $this->pagelink.= " <a href='?{$params}page=".($this->page - 1)."'>PREV</a> ";
- }else{
- $this->pagelink.= " <span class='nolink'>FIRST</span> ";
- $this->pagelink.= " <span class='nolink'>PREV</span> ";
- }
- for($i = $this->page - $this->pagerange; $i <= $this->page + $this->pagerange; $i++){
- if($i < 1 || $i > $this->totalpage) continue;
- $this->pagelink.= $i == $this->page ? " <span class='currentpage'>[{$i}]</span> " : "<a href='?{$params}page={$i}'>[{$i}]</a> ";
- }
- //NEXT LAST
- if($this->page < $this->totalpage){
- $this->pagelink.= " <a href='?{$params}page=".($this->page + 1)."'>NEXT</a> ";
- $this->pagelink.= " <a href='?{$params}page={$this->totalpage}'>LAST</a> ";
- }else{
- $this->pagelink.= " <span class='nolink'>NEXT</span> ";
- $this->pagelink.= " <span class='nolink'>LAST</span> ";
- }
- }
- }
使用举例:
下载: list.php
- <?php
- //引入ADODB
- require_once dirname(__FILE__)."/../lib/adodb/adodb.inc.php";
- //DB configuration
- $db_config = array("db_host"=>"localhost","db_user"=>"root","db_password"=>"123456","db_name"=>"flow");
- $page = intval($_REQUEST['page']);
- $sql = "SELECT * FROM `flow_bulletins` WHERE 1=1 ";
- $keyword != "" && $sql.= " AND (`bulletin_title_en` LIKE '%{$keyword}%' OR `bulletin_title_cn` LIKE '%{$keyword}%' OR `bulletin_title_fr` LIKE '%{$keyword}%') ";
- $status != "" && $sql.= " AND `enabled`='{$status}' ";
- $sql .= " ORDER BY `ordering`";
- $pageparams = compact("keyword","status");
- $pageobj = new pageclass($page, $pageparams);
- $pageobj->loadpage($sql);
- var_dump($pageobj);
- ?>
php 生成 xml 添加 BOM
作者:蘑菇 日期:2010-05-27 21:20
php 生成缩略图
作者:蘑菇 日期:2010-05-06 11:56
下载: thumbclass.php
- <?php
- /*
- * Create Thumbnail picture
- * Author: Liubing
- * Last Modify: 2010-05-06 12:09
- *
- * How to use it:
- *
- * $source_pic = dirname(__FILE__).'/1920VectorLove008.png';
- * $thumb = new Thumbnail($source_pic,250,100);
- * $thumb->create();
- */
- class Thumbnail
- {
- /* source picture */
- var $source_pic;
- /* source picture width */
- var $source_w;
- /* source picture height */
- var $source_h;
- /* source picture mimetype */
- var $source_mime;
- /* source picture is valid or not */
- var $source_valid;
- /* thumb picture directory */
- var $dest_dir;
- /* thumb picture name */
- var $dest_pic;
- /* thumb picture width */
- var $dest_w;
- /* thumb picture height */
- var $dest_h;
- /* assign mimetype to function name */
- var $mime2func;
- function Thumbnail($source_pic,$width="400",$height="300",$dest_dir="")
- {
- $this->source_pic = $source_pic;
- $this->source_valid = false;
- $this->dest_dir = $dest_dir == "" ? dirname(__FILE__) : $dest_dir;
- $this->dest_w = intval($width);
- $this->dest_h = intval($height);
- $this->mime2func = array("image/jpeg" => "jpeg", "image/gif" => "gif", "image/png" => "png");
- $this->readSource();
- }
- /* Read Source Picture */
- function readSource()
- {
- if(file_exists($this->source_pic)){
- $picinfo = getimagesize($this->source_pic);
- if(is_array($picinfo)){
- $this->source_w = $picinfo[0];
- $this->source_h = $picinfo[1];
- $this->source_mime = $picinfo['mime'];
- $this->source_valid = true;
- $basename = basename($this->source_pic);
- $this->dest_pic = substr(md5(rand()),0,10)."_".$this->dest_w."_".$this->dest_h."_".$basename;
- $this->setRealSize();
- }
- }
- }
- //Set Thumb Size (keep proportion)
- function setRealSize()
- {
- if(!$this->source_valid) return;
- if($this->dest_w < 1 || $this->dest_w >= $this->source_w) $this->dest_w = 0;
- if($this->dest_h < 1 || $this->dest_h >= $this->source_h) $this->dest_h = 0;
- if($this->dest_w == 0 && $this->dest_h == 0) return;
- if($this->dest_w > 0 && $this->dest_h > 0){
- if(($this->source_w / $this->dest_w) > ($this->source_h / $this->dest_h)){
- $this->dest_h = ceil(($this->source_h * $this->dest_w) / $this->source_w);
- }else{
- $this->dest_w = ceil(($this->source_w * $this->dest_h) / $this->source_h);
- }
- }elseif($this->dest_w > 0){
- $this->dest_h = ceil(($this->source_h * $this->dest_w) / $this->source_w);
- }else{
- $this->dest_w = ceil(($this->source_w * $this->dest_h) / $this->source_h);
- }
- }
- function create()
- {
- if(!$this->source_valid || $this->mime2func[$this->source_mime] == '') return;
- //create folder
- if(substr($this->dest_dir,-1,1) != "/") $this->dest_dir .= "/";
- $this->dest_dir .= "thumb/".date("Y-m");
- if(!is_dir($this->dest_dir)) mkdir($this->dest_dir);
- //copy source to thumb
- if($this->dest_w == 0 || $this->dest_h == 0){
- copy($this->source_pic,$this->dest_dir."/".$this->dest_pic);
- return;
- }
- //create thumb
- $createfunc = "imagecreatefrom".$this->mime2func[$this->source_mime];
- $outputfunc = "image".$this->mime2func[$this->source_mime];
- $d_im = imagecreatetruecolor($this->dest_w,$this->dest_h);
- $s_im = $createfunc($this->source_pic);
- imagecopyresized($d_im, $s_im, 0, 0, 0, 0, $this->dest_w, $this->dest_h, $this->source_w, $this->source_h);
- //save
- $outputfunc($d_im,$this->dest_dir."/".$this->dest_pic);
- //destroy
- imagedestroy($d_im);
- imagedestroy($s_im);
- }
- }
- ?>

