UCHome中关于公共函数(function_common.php)页面的代码分析(二)

 

代码
  1  /*
  2      [UCenter Home] (C) 2007-2008 Comsenz Inc.
  3      $Id: function_common.php 2009-10-20 21:12:00
  4      @author ymaozi
  5      @copyright http://www.codedesign.cn
  6      @uchome源码交流QQ群:83400263
  7  */
  8  /* *
  9   * 获取表名
 10   * @param string 表名
 11   * @return string 加了表前缀的表名
 12    */
 13  function  tname( $name ) {
 14       global   $_SC ;
 15       return   $_SC [ ' tablepre ' ] . $name ;
 16  }
 17 
 18  /* *
 19   * 对话框
 20   * @param string 转入的提示信息
 21   * @param string 跳转的url
 22   * @param int 跳转的时间
 23   *
 24    */
 25  function  showmessage( $msgkey ,   $url_forward = '' ,   $second = 1 ,   $values = array ()) {
 26       global   $_SGLOBAL ,   $_SC ,   $_SCONFIG ,   $_TPL ,   $space ,   $_SN ;
 27 
 28      obclean();  // 清除缓存
 29 
 30      //去掉广告
 31       $_SGLOBAL [ ' ad ' =   array ();
 32 
 33       // 语言
 34       include_once (S_ROOT . ' ./language/lang_showmessage.php ' );  // 引入语言文件
 35       if ( isset ( $_SGLOBAL [ ' msglang ' ][ $msgkey ])) {  // $_SGLOBAL['msglang']数组中是否存在$msgkey
 36           $message   =  lang_replace( $_SGLOBAL [ ' msglang ' ][ $msgkey ] ,   $values );
 37      }  else  {
 38           $message   =   $msgkey ;
 39      }
 40       // 手机
 41       if ( $_SGLOBAL [ ' mobile ' ]) {
 42           include  template( ' showmessage ' );
 43           exit ();
 44      }
 45       // 显示
 46       if ( empty ( $_SGLOBAL [ ' inajax ' ])  &&   $url_forward   &&   empty ( $second )) {
 47           header ( " HTTP/1.1 301 Moved Permanently " );
 48           header ( " Location:  $url_forward " );
 49      }  else  {
 50           if ( $_SGLOBAL [ ' inajax ' ]) {
 51               if ( $url_forward ) {
 52                   $message   =   " <a href=\ " $url_forward \ " > $message </a><ajaxok> " ;
 53              }
 54               // $message = "<h1>".$_SGLOBAL['msglang']['box_title']."</h1><a href=\"javascript:;\" onclick=\"hideMenu();\" class=\"float_del\">X</a><div class=\"popupmenu_inner\">$message</div>";
 55               echo   $message ;
 56              ob_out();
 57          }  else  {
 58               if ( $url_forward ) {
 59                   $message   =   " <a href=\ " $url_forward \ " > $message </a><script>setTimeout(\ " window . location . href  = ' $url_forward ' ;\ " " . ( $second * 1000 ) . " );</script> " ;
 60              }
 61               include  template( ' showmessage ' );
 62          }
 63      }
 64       exit ();
 65  }
 66 
 67  /* *
 68   * 判断提交是否正确
 69   * @param string 提交的按钮名
 70   * @return bool
 71    */
 72  function  submitcheck( $var ) {
 73           // 如果存在$var的值并且提交方法为post
 74       if ( ! empty ( $_POST [ $var ])  &&   $_SERVER [ ' REQUEST_METHOD ' ==   ' POST ' ) {
 75           if (( empty ( $_SERVER [ ' HTTP_REFERER ' ])  ||   preg_replace ( " /https?:\/\/([^\:\/]+).*/i " ,   " \\1 " ,   $_SERVER [ ' HTTP_REFERER ' ])  ==   preg_replace ( " /([^\:]+).*/ " ,   " \\1 " ,   $_SERVER [ ' HTTP_HOST ' ]))  &&   $_POST [ ' formhash ' ==  formhash()) {
 76               return   true ;
 77          }  else  {
 78              showmessage( ' submit_invalid ' );
 79          }
 80      }  else  {
 81           return   false ;
 82      }
 83  }
 84 
 85  /* *
 86   * 添加数据
 87   * @global array $_SGLOBAL
 88   * @param string $tablename 表名
 89   * @param array $insertsqlarr 要插入的数组
 90   * @param int $returnid
 91   * @param bool $replace
 92   * @param int $silent
 93   * @return string
 94    */
 95  function  inserttable( $tablename ,   $insertsqlarr ,   $returnid = 0 ,   $replace   =   false ,   $silent = 0 ) {
 96       global   $_SGLOBAL ;
 97 
 98       $insertkeysql   =   $insertvaluesql   =   $comma   =   '' ;
 99       foreach  ( $insertsqlarr   as   $insert_key   =>   $insert_value ) {
100           $insertkeysql   .=   $comma . ' ` ' . $insert_key . ' ` ' // 插入的键值
101           $insertvaluesql   .=   $comma . ' \ '' .$insert_value. ' \ '' // 插入的值
102           $comma   =   ' ' ;
103      }
104       $method   =   $replace ? ' REPLACE ' : ' INSERT ' ;
105       $_SGLOBAL [ ' db ' ] -> query( $method . '  INTO  ' . tname( $tablename ) . '  ( ' . $insertkeysql . ' ) VALUES ( ' . $insertvaluesql . ' ) ' ,   $silent ? ' SILENT ' : '' );
106       if ( $returnid   &&   ! $replace ) {  // 如果$returnid为真,则返回插入的uid.
107           return   $_SGLOBAL [ ' db ' ] -> insert_id();
108      }
109  }
110 
111  /* *
112   * 编辑信息
113   * @global array $_SGLOBAL
114   * @param string $tablename 更新的表名
115   * @param array $setsqlarr 更新的字段
116   * @param array $wheresqlarr where
117   * @param int $silent
118    */
119  function  updatetable( $tablename ,   $setsqlarr ,   $wheresqlarr ,   $silent = 0 ) {
120       global   $_SGLOBAL ;
121 
122       $setsql   =   $comma   =   '' ;
123       foreach  ( $setsqlarr   as   $set_key   =>   $set_value ) {
124           if ( is_array ( $set_value )) {
125               $setsql   .=   $comma . ' ` ' . $set_key . ' ` ' . ' = ' . $set_value [ 0 ];
126          }  else  {
127               $setsql   .=   $comma . ' ` ' . $set_key . ' ` ' . ' =\ '' .$set_value. ' \ '' ;
128          }
129           $comma   =   ' ' ;
130      }
131       $where   =   $comma   =   '' ;
132       if ( empty ( $wheresqlarr )) {
133           $where   =   ' 1 ' ;
134      }  elseif ( is_array ( $wheresqlarr )) {
135           foreach  ( $wheresqlarr   as   $key   =>   $value ) {
136               $where   .=   $comma . ' ` ' . $key . ' ` ' . ' =\ '' .$value. ' \ '' ;
137               $comma   =   '  AND  ' ;
138          }
139      }  else  {
140           $where   =   $wheresqlarr ;
141      }
142       $_SGLOBAL [ ' db ' ] -> query( ' UPDATE  ' . tname( $tablename ) . '  SET  ' . $setsql . '  WHERE  ' . $where ,   $silent ? ' SILENT ' : '' );
143  }
144 
145  /* *
146   * 获取用户空间信息
147   * @global array $_SGLOBAL
148   * @global array $_SCONFIG
149   * @global array $_SN
150   * @param int or string $key uid或是用户名
151   * @param string $indextype 通过uid还是用户名开通用户名
152   * @param int $auto_open 是否自动创建空间
153   * @return array
154    */
155  function  getspace( $key ,   $indextype = ' uid ' ,   $auto_open = 0 ) {
156       global   $_SGLOBAL ,   $_SCONFIG ,   $_SN ;
157 
158       $var   =   " space_{ $key }_{ $indextype } " ;
159       if ( empty ( $_SGLOBAL [ $var ])) {
160           $space   =   array ();
161           $query   =   $_SGLOBAL [ ' db ' ] -> query( " SELECT sf.*, s.* FROM  " . tname( ' space ' ) . "  s LEFT JOIN  " . tname( ' spacefield ' ) . "  sf ON sf.uid=s.uid WHERE s.{ $indextype }=' $key ' " );
162           if ( ! $space   =   $_SGLOBAL [ ' db ' ] -> fetch_array( $query )) {  // 如果数据库中不存在传入uid的空间信息
163               $space   =   array ();
164               if ( $indextype == ' uid '   &&   $auto_open ) {  // 如果传入的是uid,并开启自动开通空间功能
165                  //自动开通空间
166                   include_once (S_ROOT . ' ./uc_client/client.php ' );
167                   if ( $user   =  uc_get_user( $key ,   1 )) { // 获取用户的信息
168                       include_once (S_ROOT . ' ./source/function_space.php ' );
169                       $space   =  space_open( $user [ 0 ] ,   addslashes ( $user [ 1 ]) ,   0 ,   addslashes ( $user [ 2 ])); // 开通空间
170                  }
171              }
172          }
173           if ( $space ) {     // 如果存在空间
174               $_SN [ $space [ ' uid ' ]]  =  ( $_SCONFIG [ ' realname ' &&   $space [ ' name ' &&   $space [ ' namestatus ' ]) ? $space [ ' name ' ] : $space [ ' username ' ];  // 获取实名或是用户名
175               $space [ ' self ' =  ( $space [ ' uid ' ] == $_SGLOBAL [ ' supe_uid ' ]) ? 1 : 0 // 是否是自己的空间
176 
177              //好友缓存
178               $space [ ' friends ' =   array ();
179               if ( empty ( $space [ ' friend ' ])) {  // 如果好友为空
180                   if ( $space [ ' friendnum ' ] > 0 ) { // 如果好友数大于0
181                       $fstr   =   $fmod   =   '' ;
182                                           // 则在好友表中查找uid的好友
183                       $query   =   $_SGLOBAL [ ' db ' ] -> query( " SELECT fuid FROM  " . tname( ' friend ' ) . "  WHERE uid=' $space [uid]' AND status='1' " );
184                       while  ( $value   =   $_SGLOBAL [ ' db ' ] -> fetch_array( $query )) {
185                           $space [ ' friends ' ][]  =   $value [ ' fuid ' ];
186                           $fstr   .=   $fmod . $value [ ' fuid ' ];
187                           $fmod   =   ' , ' ;
188                      }
189                       $space [ ' friend ' =   $fstr ;
190                  }
191              }  else  {
192                   $space [ ' friends ' =   explode ( ' , ' ,   $space [ ' friend ' ]);
193              }
194 
195               $space [ ' username ' =   addslashes ( $space [ ' username ' ]);
196               $space [ ' name ' =   addslashes ( $space [ ' name ' ]);
197               $space [ ' privacy ' =   empty ( $space [ ' privacy ' ]) ? ( empty ( $_SCONFIG [ ' privacy ' ]) ? array () : $_SCONFIG [ ' privacy ' ]) : unserialize ( $space [ ' privacy ' ]);
198 
199               // 通知数
200               $space [ ' allnotenum ' =   0 ;
201               foreach  ( array ( ' notenum ' , ' pokenum ' , ' addfriendnum ' , ' mtaginvitenum ' , ' eventinvitenum ' , ' myinvitenum ' as   $value ) {
202                   $space [ ' allnotenum ' =   $space [ ' allnotenum ' +   $space [ $value ];
203              }
204               if ( $space [ ' self ' ]) {
205                   $_SGLOBAL [ ' member ' =   $space ;
206              }
207          }
208           $_SGLOBAL [ $var =   $space ;
209      }
210       return   $_SGLOBAL [ $var ];
211  }
212 
213  /* *
214   * 通过用户名或真实姓名获取用户的uid
215   * @param string $name
216   * @return int
217    */
218  function  getuid( $name ) {
219       global   $_SGLOBAL ,   $_SCONFIG ;
220 
221       $wherearr []  =   " (username=' $name ') " ;
222       if ( $_SCONFIG [ ' realname ' ]) { // 如果设置为实名,则能过实名来获取uid或通过用户名
223           $wherearr []  =   " (name=' $name ' AND namestatus = 1) " ;
224      }
225       $uid   =   0 ;
226       $query   =   $_SGLOBAL [ ' db ' ] -> query( " SELECT uid,username,name,namestatus FROM  " . tname( ' space ' ) . "  WHERE  " . implode ( '  OR  ' ,   $wherearr ) . "  LIMIT 1 " );
227       if ( $space   =   $_SGLOBAL [ ' db ' ] -> fetch_array( $query )) {
228           $uid   =   $space [ ' uid ' ];
229      }
230       return   $uid ;
231  }
232 
233  /* *
234   * 获取当前用户信息
235    */
236  function  getmember() {
237       global   $_SGLOBAL ,   $space ;
238 
239       if ( empty ( $_SGLOBAL [ ' member ' ])  &&   $_SGLOBAL [ ' supe_uid ' ]) {
240           if ( $space [ ' uid ' ==   $_SGLOBAL [ ' supe_uid ' ]) {
241               $_SGLOBAL [ ' member ' =   $space ;
242          }  else  {
243               $_SGLOBAL [ ' member ' =  getspace( $_SGLOBAL [ ' supe_uid ' ]);
244          }
245      }
246  }
247 

 

 

你可能感兴趣的:(function)