📄 utils.php
字号:
<?php/** * utils.php - Misc utils common to all aspects of the site * * Copyright 1999-2001 (c) VA Linux Systems * * @version $Id: utils.php.patched,v 1.1.2.1 2002/11/30 09:57:57 cbayle Exp $ * * This file is part of GForge. * * GForge is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * GForge is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with GForge; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 US *//** * util_check_fileupload() - determines if a filename is appropriate for upload * * @param string The name of the file being uploaded */function util_check_fileupload($filename) { /* Empty file is a valid file. This is because this function should be called unconditionally at the top of submit action processing and many forms have optional file upload. */ if ($filename == 'none' || $filename == '') { return true; } /* This should be enough... */ if (!is_uploaded_file($filename)) { return false; } /* ... but we'd rather be paranoic */ if (strstr($filename, '..')) { return false; } if (!is_file($filename)) { return false; } if (!file_exists($filename)) { return false; } if ((dirname($filename) != '/tmp') && (dirname($filename) != "/var/tmp")) { return false; } return true;}/** * util_send_message() - Send email * This function should be used in place of the PHP mail() function * * @param string The email recipients address * @param string The email subject * @param string The body of the email message * @param string The optional email sender address. Defaults to 'noreply@' * @param string The addresses to blind-carbon-copy this message * */function util_send_message($to,$subject,$body,$from='',$BCC='') { if (!$to) { $to='noreply@'.$GLOBALS['sys_default_domain']; } if (!$from) { $from='noreply@'.$GLOBALS['sys_default_domain']; } $body = "To: $to". "\nFrom: $from". "\nBCC: $BCC". "\nSubject: $subject". "\n\n$body"; exec ("/bin/echo \"". util_prep_string_for_sendmail($body) . "\" | /usr/sbin/sendmail -f'$from' -t -i > /dev/null 2>&1 &");}function util_send_jabber($to,$subject,$body) { if (!$GLOBALS['sys_use_jabber']) { return; } $JABBER = new Jabber(); if (!$JABBER->Connect()) { echo '<br />Unable to connect'; return false; } //$JABBER->SendAuth(); //$JABBER->AccountRegistration(); if (!$JABBER->SendAuth()) { echo '<br />Auth Failure'; $JABBER->Disconnect(); return false; //or die("Couldn't authenticate!"); } $JABBER->SendPresence(NULL, NULL, "online"); $body=htmlspecialchars($body); $to_arr=explode(',',$to); for ($i=0; $i<count($to_arr); $i++) { if ($to_arr[$i]) { //echo '<br />Sending Jabbers To: '.$to_arr[$i]; if (!$JABBER->SendMessage($to_arr[$i], "normal", NULL, array("body" => $body,"subject"=>$subject))) { echo '<br />Error Sending to '.$to_arr[$i]; } } } $JABBER->CruiseControl(2); $JABBER->Disconnect();}/** * util_prep_string_for_sendmail() - Prepares a string to be sent by email * * @param string The text to be prepared * @returns The prepared text * */function util_prep_string_for_sendmail($body) { //$body=str_replace("\\","\\\\",$body); $body=str_replace("`","\\`",$body); $body=str_replace("\"","\\\"",$body); $body=str_replace("\$","\\\$",$body); return $body;}/** * util_handle_message() - a convenience wrapper which sends messages * to either a jabber account or email account or both, depending on * user preferences * * @param array array of user_id's from the user table * @param string subject of the message * @param string the message body * @param string a comma-separated list of email address * @param string a comma-separated list of jabber address */function util_handle_message($id_arr,$subject,$body,$extra_emails='',$extra_jabbers='') { $address=array(); if (count($id_arr) < 1) { } else { $res=db_query("SELECT user_id, jabber_address,email,jabber_only FROM users WHERE user_id IN (". implode($id_arr,',') .")"); $rows=db_numrows($res); for ($i=0; $i<$rows; $i++) { if (db_result($res, $i, 'user_id') == 100) { // Do not send messages to "Nobody" continue; } // // Build arrays of the jabber address // if (db_result($res,$i,'jabber_address')) { $address['jabber_address'][]=db_result($res,$i,'jabber_address'); if (db_result($res,$i,'jabber_only') != 1) { $address['email'][]=db_result($res,$i,'email'); } } else { $address['email'][]=db_result($res,$i,'email'); } } if (count($address['email']) > 0) { $extra_email1=implode($address['email'],',').','; } if (count($address['jabber_address']) > 0) { $extra_jabber1=implode($address['jabber_address'],',').','; } } if ($extra_email1 || $extra_emails) { util_send_message('',$subject,$body,'',$extra_email1.$extra_emails); } if ($extra_jabber1 || $extra_jabbers) { util_send_jabber($extra_jabber1.$extra_jabbers,$subject,$body); }}/** * util_unconvert_htmlspecialchars() - Unconverts a string converted with htmlspecialchars() * This function requires PHP 4.0.3 or greater * * @param string The string to unconvert * @returns The unconverted string * */function util_unconvert_htmlspecialchars($string) { if (strlen($string) < 1) { return ''; } else { //$trans = get_html_translation_table(HTMLENTITIES, ENT_QUOTES); $trans = get_html_translation_table(HTML_ENTITIES); $trans = array_flip ($trans); $str = strtr ($string, $trans); return $str; }}/** * util_result_columns_to_assoc() - Takes a result set and turns the column pair into an associative array * * @param string The result set ID * @param int The column key * @param int The optional column value * @returns An associative array * */function util_result_columns_to_assoc($result, $col_key=0, $col_val=1) { $rows=db_numrows($result); if ($rows > 0) { $arr=array(); for ($i=0; $i<$rows; $i++) { $arr[db_result($result,$i,$col_key)]=db_result($result,$i,$col_val); } } else { $arr=array(); } return $arr;}/** * util_result_column_to_array() - Takes a result set and turns the optional column into an array * * @param int The result set ID * @param int The column * @resturns An array * */function &util_result_column_to_array($result, $col=0) { /* Takes a result set and turns the optional column into an array */ $rows=db_numrows($result); if ($rows > 0) { $arr=array(); for ($i=0; $i<$rows; $i++) { $arr[$i]=db_result($result,$i,$col); } } else { $arr=array(); } return $arr;}/** * util_wrap_find_space() - Find the first space in a string * * @param string The string in which to find the space * @param int The number of characters to wrap - Default is 80 * @returns The position of the first space * */function util_wrap_find_space($string,$wrap) { //echo"\n"; $start=$wrap-5; $try=1; $found=false; while (!$found) { //find the first space starting at $start $pos=@strpos($string,' ',$start); //if that space is too far over, go back and start more to the left if (($pos > ($wrap+5)) || !$pos) { $try++; $start=($wrap-($try*5)); //if we've gotten so far left , just truncate the line if ($start<=10) { return $wrap; } $found=false; } else { $found=true; } } return $pos;}/** * util_line_wrap() - Automatically linewrap text * * @param string The text to wrap * @param int The number of characters to wrap - Default is 80 * @param string The line break to use - Default is '\n' * @returns The wrapped text * */function util_line_wrap ($text, $wrap = 80, $break = "\n") { $paras = explode("\n", $text); $result = array(); $i = 0; while ($i < count($paras)) { if (strlen($paras[$i]) <= $wrap) { $result[] = $paras[$i]; $i++; } else { $pos=util_wrap_find_space($paras[$i],$wrap); $result[] = substr($paras[$i], 0, $pos); $new = trim(substr($paras[$i], $pos, strlen($paras[$i]) - $pos)); if ($new != '') { $paras[$i] = $new; $pos=util_wrap_find_space($paras[$i],$wrap); } else { $i++; } } } return implode($break, $result);}/** * util_make_links() - Turn URL's into HREF's. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -