Discuz教程网

史上第一强:Discuz 源代码分析系列(2)--./include/global.func.php(2)

[复制链接]
authicon JAZZ-NA 发表于 2013-3-5 11:32:32 | 显示全部楼层 |阅读模式
这个是第二部分。
第一部分:http://www.discuz.net/viewthread.php?tid=612193
第二部分:http://www.discuz.net/viewthread.php?tid=612195
第三部分:http://www.discuz.net/viewthread.php?tid=612197

申明下版权:
1.这里面的每个中文字都是我打的,code部分是引用的,当然我也加了一点注释在里面了。
2.如果要转载的话请注明
  1. 转自[url]www.discuz.net[/url] 作者:郭鑫
复制代码

3.由于我个人的能力有限,写这篇文章没有参考一点资料,甚至连本地环境也没有搭建(遇到了白屏问题),所以难免会有错误的地方,大家发现了的话请跟帖或者联系我吧,我会尽快更正。

  1. /**
  2. * 输出用的,这个时候可以做一点事情,比如说:把sid用正则写到form里面,rewrite规则启用后把论坛里面的forumdisplay.php?fid=1&page=2用正则换成forum-1-2.html。
  3. * 并把ftp连接关掉,写入cache
  4. */
  5. function output() {
  6. global $sid, $transsidstatus, $rewritestatus, $ftp;
  7. if(($transsidstatus = empty($GLOBALS['_DCOOKIE']['sid']) && $transsidstatus) || in_array($rewritestatus, array(2, 3))) {
  8. if($transsidstatus) {
  9. $searcharray = array
  10. (
  11. "/]+s*)href=(["|']?)([^"'s]+)/ies",
  12. "/()/is"
  13. );
  14. $replacearray = array
  15. (
  16. "transsid('3','
  17. "1n"
  18. );
  19. } else {
  20. $searcharray = array
  21. (
  22. //"//",
  23. "/]*)>/e",
  24. "/]*)>/e",
  25. "/]*)>/e",
  26. "/]*)>/e"
  27. );
  28. $replacearray = array
  29. (
  30. //"",
  31. "rewrite_forum('1', '3', '4')",
  32. "rewrite_thread('1', '5', '3', '6')",
  33. "rewrite_profile('2', '3', '4')",
  34. "rewrite_space('2', '3', '4')"
  35. );
  36. }
  37. $content = preg_replace($searcharray, $replacearray, ob_get_contents());
  38. ob_end_clean();
  39. $GLOBALS['gzipcompress'] ? ob_start('ob_gzhandler') : ob_start();
  40. echo $content;
  41. }
  42. if($ftp['connid']) {
  43. @ftp_close($ftp['connid']);
  44. }
  45. $ftp = array();
  46. if(defined('CACHE_FILE') && CACHE_FILE && !defined('CACHE_FORBIDDEN')) {
  47. global $cachethreaddir;
  48. if(diskfreespace(DISCUZ_ROOT.'./'.$cachethreaddir) > 1000000) {
  49. $fp = fopen(CACHE_FILE, 'w');
  50. if($fp) {
  51. flock($fp, LOCK_EX);
  52. fwrite($fp, empty($content) ? ob_get_contents() : $content);
  53. }
  54. @fclose($fp);
  55. }
  56. }
  57. }
复制代码
  1. /**这一段都是rewrite规则,和上一个函数相映成趣~
  2. * @para int $tid 帖子的id
  3. * @para int $page 页数
  4. * @para int $prevpage 上一页
  5. * @para string $extra 附加的东东
  6. *
  7. * @return string 返回一个链接,如:
  8. */
  9. function rewrite_thread($tid, $page = 0, $prevpage = 0, $extra = '') {
  10. return '';
  11. }
  12. function rewrite_forum($fid, $page = 0, $extra = '') {
  13. return '';
  14. }
  15. function rewrite_profile($uid, $username, $extra = '') {
  16. return '';
  17. }
  18. function rewrite_space($uid, $username, $extra = '') {
  19. return '';
  20. }
复制代码

  1. /**
  2. * 访问时段检查
  3. * @para string $periods 允许访问的时段
  4. * @para string showmessage 如果不允许访问的时段会员访问了的话出现的自定义提示
  5. *
  6. * @return TRUE or no return
  7. */
  8. function periodscheck($periods, $showmessage = 1) {
  9. global $timestamp, $disableperiodctrl, $_DCACHE, $banperiods;
  10. if(!$disableperiodctrl && $_DCACHE['settings'][$periods]) {
  11. $now = gmdate('G.i', $timestamp + $_DCACHE['settings']['timeoffset'] * 3600);
  12. foreach(explode("rn", str_replace(':', '.', $_DCACHE['settings'][$periods])) as $period) {
  13. list($periodbegin, $periodend) = explode('-', $period);
  14. if(($periodbegin > $periodend && ($now >= $periodbegin || $now < $periodend)) || ($oeriodbegin < $periodend && $now >= $periodbegin && $now < $periodend)) {
  15. $banperiods = str_replace("rn", ', ', $_DCACHE['settings'][$periods]);
  16. if($showmessage) {
  17. showmessage('period_nopermission', NULL, 'NOPERM');
  18. } else {
  19. return TRUE;
  20. }
  21. }
  22. }
  23. }
  24. return FALSE;
  25. }
复制代码
  1. /**
  2. * 加密安全提问的,MD5加密
  3. * @para int $qestionid
  4. * @para string $anser
  5. *
  6. * @return string 返回加密后的字串
  7. */
  8. function quescrypt($questionid, $answer) {
  9. return $questionid > 0 && $answer != '' ? substr(md5($answer.md5($questionid)), 16, 8) : '';
  10. }
复制代码

  1. /**
  2. * 生成随机数(这里返回$hash名字有点点不合适,hash是包含key和value的,这里没有这样的意思)
  3. * @para int $length 要生成的长度
  4. * @para int $numeric 传入一个非零的数表示要生成的全是数字
  5. *
  6. * @return string 返回生成的字串
  7. */
  8. function random($length, $numeric = 0) {
  9. PHP_VERSION < '4.2.0' && mt_srand((double)microtime() * 1000000);
  10. if($numeric) {
  11. $hash = sprintf('%0'.$length.'d', mt_rand(0, pow(10, $length) - 1));
  12. } else {
  13. $hash = '';
  14. $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
  15. $max = strlen($chars) - 1;
  16. for($i = 0; $i < $length; $i++) {
  17. $hash .= $chars[mt_rand(0, $max)];
  18. }
  19. }
  20. return $hash;
  21. }
复制代码
  1. /**
  2. * 删除目录和文件用的
  3. * @para string $dirname 要删除的目录名
  4. * @para boolean $keepdir 是否在删除文件后保留目录,也就是只清空目录
  5. *
  6. * @return boolean
  7. */
  8. function removedir($dirname, $keepdir = FALSE) {
  9. $dirname = wipespecial($dirname);
  10. if(!is_dir($dirname)) {
  11. return FALSE;
  12. }
  13. $handle = opendir($dirname);
  14. while(($file = readdir($handle)) !== FALSE) {
  15. if($file != '.' && $file != '..') {
  16. $dir = $dirname . DIRECTORY_SEPARATOR . $file;
  17. is_dir($dir) ? removedir($dir) : unlink($dir);
  18. }
  19. }
  20. closedir($handle);
  21. return !$keepdir ? (@rmdir($dirname) ? TRUE : FALSE) : TRUE;
  22. }
复制代码
  1. /**
  2. * 发信件用的,用到了include目录下的sendmail.inc.php
  3. * @para string $email_to
  4. * @para string $email_subject
  5. * @para string $email_message
  6. * @para string $email_from
  7. */
  8. function sendmail($email_to, $email_subject, $email_message, $email_from = '') {
  9. extract($GLOBALS, EXTR_SKIP);
  10. require DISCUZ_ROOT.'./include/sendmail.inc.php';
  11. }
复制代码
  1. /**
  2. * 发送PM的函数
  3. * @para int $toid 对方id
  4. * @para string $subject PM主题
  5. * @para int $fromid 发送方id
  6. * @para string $from 发送方用户名
  7. */
  8. function sendpm($toid, $subject, $message, $fromid = '', $from = '') {
  9. extract($GLOBALS, EXTR_SKIP);
  10. include language('pms');
  11. if(isset($language[$subject])) {
  12. eval("$subject = addslashes("".$language[$subject]."");");
  13. }
  14. if(isset($language[$message])) {
  15. eval("$message = addslashes("".$language[$message]."");");
  16. }
  17. if(!$fromid && !$from) {
  18. $fromid = $discuz_uid;
  19. $from = $discuz_user;
  20. }
  21. $pmids = array();
  22. foreach(explode(',', $toid) as $uid) {
  23. if(is_numeric($uid)) {
  24. $query = $db->query("INSERT INTO {$tablepre}pms (msgfrom, msgfromid, msgtoid, folder, new, subject, dateline, message)
  25. VALUES ('$from', '$fromid', '$uid', 'inbox', '1', '$subject', '$timestamp', '$message')");
  26. if($query) {
  27. $pmids[] = $uid;
  28. }
  29. }
  30. }
  31. if($toid = implodeids($pmids)) {
  32. $db->query("UPDATE {$tablepre}members SET newpm='1' WHERE uid IN ($toid)");
  33. }
  34. }
复制代码

[ 本帖最后由 郭鑫 于 2007-5-4 01:24 编辑 ]



上一篇:打开个人空间如何显示主题页面,同时让游客也能访问主题页面。
下一篇:史上第一强:Discuz 源代码分析系列(3)--./include/misc.func.php
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

1314学习网 ( 浙ICP备10214163号 )

GMT+8, 2025-5-4 14:30

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表