2012-10-11 08:00:15 +00:00
< ?
class Misc {
2015-01-28 08:00:26 +00:00
/**
* Send an email .
*
* We can do this one of two ways - either using MailGun or with PHP ' s mail function .
* Checks for EMAIL_DELIVERY_TYPE and then proceeds as directed to send e - mail .
*
* @ param string $To the email address to send it to .
* @ param string $Subject
* @ param string $Body
* @ param string $From The user part of the user @ NONSSL_SITE_URL email address .
* @ param string $ContentType text / plain or text / html
*/
public static function send_email ( $To , $Subject , $Body , $From , $ContentType ) {
switch ( EMAIL_DELIVERY_TYPE ) {
case 'local' :
// remove the next line if you want to send HTML email from some places...
$ContentType = 'text/plain' ;
$Headers = 'MIME-Version: 1.0' . " \r \n " ;
$Headers .= 'Content-type: ' . $ContentType . '; charset=iso-8859-1' . " \r \n " ;
$Headers .= 'From: ' . SITE_NAME . ' <' . $From . '@' . NONSSL_SITE_URL . '>' . " \r \n " ;
$Headers .= 'Reply-To: ' . $From . '@' . NONSSL_SITE_URL . " \r \n " ;
$Headers .= 'X-Mailer: Project Gazelle' . " \r \n " ;
$Headers .= 'Message-Id: <' . Users :: make_secret () . '@' . NONSSL_SITE_URL . " > \r \n " ;
$Headers .= 'X-Priority: 3' . " \r \n " ;
mail ( $To , $Subject , $Body , $Headers , " -f $From @ " . NONSSL_SITE_URL );
break ;
case 'mailgun' :
// set up our message first
$From .= '@' . NONSSL_SITE_URL ;
$OutgoingEmail = array (
'from' => $From ,
'to' => $To ,
'h:Reply-To' => $From ,
'subject' => $Subject ,
'text' => $Body );
// now let's POST it to mailgun
$Curl = curl_init ();
curl_setopt ( $Curl , CURLOPT_URL , MAILGUN_API_URL );
curl_setopt ( $Curl , CURLOPT_HTTPAUTH , CURLAUTH_BASIC );
curl_setopt ( $Curl , CURLOPT_USERPWD , 'api:' . MAILGUN_API_KEY );
curl_setopt ( $Curl , CURLOPT_RETURNTRANSFER , 1 );
curl_setopt ( $Curl , CURLOPT_CONNECTTIMEOUT , 10 );
curl_setopt ( $Curl , CURLOPT_POST , true );
curl_setopt ( $Curl , CURLOPT_POSTFIELDS , $OutgoingEmail );
$RequestResult = curl_exec ( $Curl );
$RequestStatusCode = curl_getinfo ( $Curl , CURLINFO_HTTP_CODE );
curl_close ( $Curl );
// alert on failed emails
if ( $RequestStatusCode != 200 ) {
send_irc ( 'PRIVMSG ' . STATUS_CHAN . " !dev email failed to $To with error message $RequestResult " );
}
break ;
default :
die ( 'You have either not configured an email delivery method in config.php or your value is incorrect.' );
break ;
}
}
2012-10-11 08:00:15 +00:00
/**
* Sanitize a string to be allowed as a filename .
*
* @ param string $EscapeStr the string to escape
* @ return the string with all banned characters removed .
*/
public static function file_string ( $EscapeStr ) {
2014-02-27 08:00:30 +00:00
return str_replace ( array ( '"' , '*' , '/' , ':' , '<' , '>' , '?' , '\\' , '|' ), '' , $EscapeStr );
2012-10-11 08:00:15 +00:00
}
/**
* Sends a PM from $FromId to $ToId .
*
* @ param string $ToID ID of user to send PM to . If $ToID is an array and $ConvID is empty , a message will be sent to multiple users .
* @ param string $FromID ID of user to send PM from , 0 to send from system
* @ param string $Subject
* @ param string $Body
* @ param int $ConvID The conversation the message goes in . Leave blank to start a new conversation .
* @ return
*/
2013-05-27 08:00:58 +00:00
public static function send_pm ( $ToID , $FromID , $Subject , $Body , $ConvID = '' ) {
2013-08-28 23:08:41 +00:00
global $Time ;
2014-01-20 08:01:21 +00:00
$UnescapedSubject = $Subject ;
$UnescapedBody = $Body ;
2013-03-10 08:00:41 +00:00
$Subject = db_string ( $Subject );
$Body = db_string ( $Body );
2012-10-11 08:00:15 +00:00
if ( $ToID == 0 || $ToID == $FromID ) {
// Don't allow users to send messages to the system or themselves
return ;
}
2013-08-28 23:08:41 +00:00
$QueryID = G :: $DB -> get_query_id ();
2013-05-27 08:00:58 +00:00
if ( $ConvID == '' ) {
2012-10-11 08:00:15 +00:00
// Create a new conversation.
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-27 08:00:58 +00:00
INSERT INTO pm_conversations ( Subject )
VALUES ( '$Subject' ) " );
2013-08-28 23:08:41 +00:00
$ConvID = G :: $DB -> inserted_id ();
G :: $DB -> query ( "
2013-05-27 08:00:58 +00:00
INSERT INTO pm_conversations_users
( UserID , ConvID , InInbox , InSentbox , SentDate , ReceivedDate , UnRead )
VALUES
2012-10-11 08:00:15 +00:00
( '$ToID' , '$ConvID' , '1' , '0' , '".sqltime()."' , '".sqltime()."' , '1' ) " );
if ( $FromID != 0 ) {
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-27 08:00:58 +00:00
INSERT INTO pm_conversations_users
( UserID , ConvID , InInbox , InSentbox , SentDate , ReceivedDate , UnRead )
VALUES
( '$FromID' , '$ConvID' , '0' , '1' , '".sqltime()."' , '".sqltime()."' , '0' ) " );
2012-10-11 08:00:15 +00:00
}
$ToID = array ( $ToID );
} else {
// Update the pre-existing conversations.
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-27 08:00:58 +00:00
UPDATE pm_conversations_users
SET
2014-02-27 08:00:30 +00:00
InInbox = '1' ,
UnRead = '1' ,
ReceivedDate = '".sqltime()."'
2013-05-27 08:00:58 +00:00
WHERE UserID IN ( " .implode(',', $ToID ). " )
2014-02-27 08:00:30 +00:00
AND ConvID = '$ConvID' " );
2012-10-11 08:00:15 +00:00
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
UPDATE pm_conversations_users
SET
2014-02-27 08:00:30 +00:00
InSentbox = '1' ,
SentDate = '".sqltime()."'
WHERE UserID = '$FromID'
AND ConvID = '$ConvID' " );
2012-10-11 08:00:15 +00:00
}
// Now that we have a $ConvID for sure, send the message.
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
INSERT INTO pm_messages
( SenderID , ConvID , SentDate , Body )
VALUES
( '$FromID' , '$ConvID' , '".sqltime()."' , '$Body' ) " );
2012-10-11 08:00:15 +00:00
// Update the cached new message count.
foreach ( $ToID as $ID ) {
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
SELECT COUNT ( ConvID )
FROM pm_conversations_users
WHERE UnRead = '1'
2014-02-27 08:00:30 +00:00
AND UserID = '$ID'
2013-05-16 16:15:57 +00:00
AND InInbox = '1' " );
2013-08-28 23:08:41 +00:00
list ( $UnRead ) = G :: $DB -> next_record ();
2014-02-27 08:00:30 +00:00
G :: $Cache -> cache_value ( " inbox_new_ $ID " , $UnRead );
2012-10-11 08:00:15 +00:00
}
2013-02-22 08:00:24 +00:00
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-06-06 08:01:03 +00:00
SELECT Username
FROM users_main
WHERE ID = '$FromID' " );
2013-08-28 23:08:41 +00:00
list ( $SenderName ) = G :: $DB -> next_record ();
2013-04-18 08:00:54 +00:00
foreach ( $ToID as $ID ) {
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
SELECT COUNT ( ConvID )
FROM pm_conversations_users
WHERE UnRead = '1'
2014-02-27 08:00:30 +00:00
AND UserID = '$ID'
2013-05-16 16:15:57 +00:00
AND InInbox = '1' " );
2013-08-28 23:08:41 +00:00
list ( $UnRead ) = G :: $DB -> next_record ();
2014-02-27 08:00:30 +00:00
G :: $Cache -> cache_value ( " inbox_new_ $ID " , $UnRead );
2013-05-16 16:15:57 +00:00
2014-01-20 08:01:21 +00:00
NotificationsManager :: send_push ( $ID , " Message from $SenderName , Subject: $UnescapedSubject " , $UnescapedBody , site_url () . 'inbox.php' , NotificationsManager :: INBOX );
2013-04-18 08:00:54 +00:00
}
2012-10-11 08:00:15 +00:00
2013-08-28 23:08:41 +00:00
G :: $DB -> set_query_id ( $QueryID );
2012-10-11 08:00:15 +00:00
return $ConvID ;
}
2013-05-16 16:15:57 +00:00
2012-10-11 08:00:15 +00:00
/**
* Create thread function , things should already be escaped when sent here .
*
* @ param int $ForumID
* @ param int $AuthorID ID of the user creating the post .
* @ param string $Title
* @ param string $PostBody
* @ return - 1 on error , - 2 on user not existing , thread id on success .
*/
public static function create_thread ( $ForumID , $AuthorID , $Title , $PostBody ) {
2013-08-28 23:08:41 +00:00
global $Time ;
2012-10-11 08:00:15 +00:00
if ( ! $ForumID || ! $AuthorID || ! is_number ( $AuthorID ) || ! $Title || ! $PostBody ) {
return - 1 ;
}
2013-08-28 23:08:41 +00:00
$QueryID = G :: $DB -> get_query_id ();
G :: $DB -> query ( "
2013-06-06 08:01:03 +00:00
SELECT Username
FROM users_main
2013-09-06 08:00:41 +00:00
WHERE ID = $AuthorID " );
2013-08-28 23:08:41 +00:00
if ( ! G :: $DB -> has_results ()) {
G :: $DB -> set_query_id ( $QueryID );
2012-10-11 08:00:15 +00:00
return - 2 ;
}
2013-08-28 23:08:41 +00:00
list ( $AuthorName ) = G :: $DB -> next_record ();
2012-10-11 08:00:15 +00:00
$ThreadInfo = array ();
$ThreadInfo [ 'IsLocked' ] = 0 ;
$ThreadInfo [ 'IsSticky' ] = 0 ;
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
INSERT INTO forums_topics
2013-09-14 08:00:52 +00:00
( Title , AuthorID , ForumID , LastPostTime , LastPostAuthorID , CreatedTime )
2013-05-16 16:15:57 +00:00
VALUES
2013-09-14 08:00:52 +00:00
( '$Title' , '$AuthorID' , '$ForumID' , '".sqltime()."' , '$AuthorID' , '".sqltime()."' ) " );
2013-08-28 23:08:41 +00:00
$TopicID = G :: $DB -> inserted_id ();
2012-10-11 08:00:15 +00:00
$Posts = 1 ;
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
INSERT INTO forums_posts
2012-10-11 08:00:15 +00:00
( TopicID , AuthorID , AddedTime , Body )
2013-05-16 16:15:57 +00:00
VALUES
( '$TopicID' , '$AuthorID' , '".sqltime()."' , '$PostBody' ) " );
2013-08-28 23:08:41 +00:00
$PostID = G :: $DB -> inserted_id ();
2012-10-11 08:00:15 +00:00
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
UPDATE forums
SET
2013-09-06 08:00:41 +00:00
NumPosts = NumPosts + 1 ,
NumTopics = NumTopics + 1 ,
2013-05-16 16:15:57 +00:00
LastPostID = '$PostID' ,
LastPostAuthorID = '$AuthorID' ,
LastPostTopicID = '$TopicID' ,
LastPostTime = '".sqltime()."'
WHERE ID = '$ForumID' " );
2012-10-11 08:00:15 +00:00
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
UPDATE forums_topics
SET
2013-09-06 08:00:41 +00:00
NumPosts = NumPosts + 1 ,
2012-10-11 08:00:15 +00:00
LastPostID = '$PostID' ,
2013-05-16 16:15:57 +00:00
LastPostAuthorID = '$AuthorID' ,
2012-10-11 08:00:15 +00:00
LastPostTime = '".sqltime()."'
2013-05-16 16:15:57 +00:00
WHERE ID = '$TopicID' " );
2012-10-11 08:00:15 +00:00
// Bump this topic to head of the cache
2013-09-06 08:00:41 +00:00
list ( $Forum ,,, $Stickies ) = G :: $Cache -> get_value ( " forums_ $ForumID " );
2012-10-11 08:00:15 +00:00
if ( ! empty ( $Forum )) {
if ( count ( $Forum ) == TOPICS_PER_PAGE && $Stickies < TOPICS_PER_PAGE ) {
array_pop ( $Forum );
}
2013-08-28 23:08:41 +00:00
G :: $DB -> query ( "
2013-11-17 08:00:47 +00:00
SELECT IsLocked , IsSticky , NumPosts
FROM forums_topics
WHERE ID = '$TopicID' " );
2013-08-28 23:08:41 +00:00
list ( $IsLocked , $IsSticky , $NumPosts ) = G :: $DB -> next_record ();
2013-05-16 16:15:57 +00:00
$Part1 = array_slice ( $Forum , 0 , $Stickies , true ); //Stickys
2012-10-11 08:00:15 +00:00
$Part2 = array (
2013-05-16 16:15:57 +00:00
$TopicID => array (
2012-10-11 08:00:15 +00:00
'ID' => $TopicID ,
'Title' => $Title ,
'AuthorID' => $AuthorID ,
'IsLocked' => $IsLocked ,
'IsSticky' => $IsSticky ,
'NumPosts' => $NumPosts ,
'LastPostID' => $PostID ,
'LastPostTime' => sqltime (),
'LastPostAuthorID' => $AuthorID ,
)
); //Bumped thread
2013-05-16 16:15:57 +00:00
$Part3 = array_slice ( $Forum , $Stickies , TOPICS_PER_PAGE , true ); //Rest of page
2012-10-11 08:00:15 +00:00
if ( $Stickies > 0 ) {
2013-05-16 16:15:57 +00:00
$Part1 = array_slice ( $Forum , 0 , $Stickies , true ); //Stickies
$Part3 = array_slice ( $Forum , $Stickies , TOPICS_PER_PAGE - $Stickies - 1 , true ); //Rest of page
2012-10-11 08:00:15 +00:00
} else {
$Part1 = array ();
$Part3 = $Forum ;
}
2014-02-27 08:00:30 +00:00
if ( is_null ( $Part1 )) {
$Part1 = array ();
}
if ( is_null ( $Part3 )) {
$Part3 = array ();
}
2012-10-11 08:00:15 +00:00
$Forum = $Part1 + $Part2 + $Part3 ;
2013-09-06 08:00:41 +00:00
G :: $Cache -> cache_value ( " forums_ $ForumID " , array ( $Forum , '' , 0 , $Stickies ), 0 );
2012-10-11 08:00:15 +00:00
}
//Update the forum root
2013-08-28 23:08:41 +00:00
G :: $Cache -> begin_transaction ( 'forums_list' );
2012-10-11 08:00:15 +00:00
$UpdateArray = array (
2014-02-27 08:00:30 +00:00
'NumPosts' => '+1' ,
'NumTopics' => '+1' ,
'LastPostID' => $PostID ,
'LastPostAuthorID' => $AuthorID ,
'LastPostTopicID' => $TopicID ,
'LastPostTime' => sqltime (),
'Title' => $Title ,
'IsLocked' => $ThreadInfo [ 'IsLocked' ],
'IsSticky' => $ThreadInfo [ 'IsSticky' ]
2012-10-11 08:00:15 +00:00
);
2013-05-16 16:15:57 +00:00
$UpdateArray [ 'NumTopics' ] = '+1' ;
2012-10-11 08:00:15 +00:00
2013-08-28 23:08:41 +00:00
G :: $Cache -> update_row ( $ForumID , $UpdateArray );
G :: $Cache -> commit_transaction ( 0 );
2012-10-11 08:00:15 +00:00
2013-05-16 16:15:57 +00:00
$CatalogueID = floor (( POSTS_PER_PAGE * ceil ( $Posts / POSTS_PER_PAGE ) - POSTS_PER_PAGE ) / THREAD_CATALOGUE );
2013-08-28 23:08:41 +00:00
G :: $Cache -> begin_transaction ( 'thread_' . $TopicID . '_catalogue_' . $CatalogueID );
2012-10-11 08:00:15 +00:00
$Post = array (
2014-02-27 08:00:30 +00:00
'ID' => $PostID ,
'AuthorID' => G :: $LoggedUser [ 'ID' ],
'AddedTime' => sqltime (),
'Body' => $PostBody ,
'EditedUserID' => 0 ,
'EditedTime' => '0000-00-00 00:00:00' ,
'Username' => ''
2012-10-11 08:00:15 +00:00
);
2013-08-28 23:08:41 +00:00
G :: $Cache -> insert ( '' , $Post );
G :: $Cache -> commit_transaction ( 0 );
2012-10-11 08:00:15 +00:00
2013-08-28 23:08:41 +00:00
G :: $Cache -> begin_transaction ( 'thread_' . $TopicID . '_info' );
2014-02-27 08:00:30 +00:00
G :: $Cache -> update_row ( false , array ( 'Posts' => '+1' , 'LastPostAuthorID' => $AuthorID ));
2013-08-28 23:08:41 +00:00
G :: $Cache -> commit_transaction ( 0 );
G :: $DB -> set_query_id ( $QueryID );
2012-10-11 08:00:15 +00:00
return $TopicID ;
}
/**
* If the suffix of $Haystack is $Needle
*
* @ param string $Haystack String to search in
* @ param string $Needle String to search for
* @ return boolean True if $Needle is a suffix of $Haystack
*/
public static function ends_with ( $Haystack , $Needle ) {
return substr ( $Haystack , strlen ( $Needle ) * - 1 ) == $Needle ;
}
/**
2014-02-27 08:00:30 +00:00
* If the prefix of $Haystack is $Needle
2012-10-11 08:00:15 +00:00
*
* @ param string $Haystack String to search in
* @ param string $Needle String to search for
2014-02-27 08:00:30 +00:00
* @ return boolean True if $Needle is a prefix of $Haystack
2012-10-11 08:00:15 +00:00
*/
public static function starts_with ( $Haystack , $Needle ) {
return strpos ( $Haystack , $Needle ) === 0 ;
}
/**
* Variant of in_array () with trailing wildcard support
*
* @ param string $Needle , array $Haystack
* @ return boolean true if ( substring of ) $Needle exists in $Haystack
*/
public static function in_array_partial ( $Needle , $Haystack ) {
static $Searches = array ();
if ( array_key_exists ( $Needle , $Searches )) {
return $Searches [ $Needle ];
}
foreach ( $Haystack as $String ) {
if ( substr ( $String , - 1 ) == '*' ) {
2013-05-16 16:15:57 +00:00
if ( ! strncmp ( $Needle , $String , strlen ( $String ) - 1 )) {
2012-10-11 08:00:15 +00:00
$Searches [ $Needle ] = true ;
return true ;
}
} elseif ( ! strcmp ( $Needle , $String )) {
$Searches [ $Needle ] = true ;
return true ;
}
}
$Searches [ $Needle ] = false ;
return false ;
}
/**
* Used to check if keys in $_POST and $_GET are all set , and throws an error if not .
* This reduces 'if' statement redundancy for a lot of variables
*
* @ param array $Request Either $_POST or $_GET , or whatever other array you want to check .
* @ param array $Keys The keys to ensure are set .
* @ param boolean $AllowEmpty If set to true , a key that is in the request but blank will not throw an error .
* @ param int $Error The error code to throw if one of the keys isn ' t in the array .
*/
2013-10-15 08:01:05 +00:00
public static function assert_isset_request ( $Request , $Keys = null , $AllowEmpty = false , $Error = 0 ) {
2012-10-11 08:00:15 +00:00
if ( isset ( $Keys )) {
foreach ( $Keys as $K ) {
2013-10-15 08:01:05 +00:00
if ( ! isset ( $Request [ $K ]) || ( $AllowEmpty == false && $Request [ $K ] == '' )) {
2012-10-11 08:00:15 +00:00
error ( $Error );
break ;
}
}
} else {
foreach ( $Request as $R ) {
2013-10-15 08:01:05 +00:00
if ( ! isset ( $R ) || ( $AllowEmpty == false && $R == '' )) {
2012-10-11 08:00:15 +00:00
error ( $Error );
break ;
}
}
}
}
/**
* Given an array of tags , return an array of their IDs .
*
2014-04-06 08:00:49 +00:00
* @ param array $TagNames
2012-10-11 08:00:15 +00:00
* @ return array IDs
*/
public static function get_tags ( $TagNames ) {
$TagIDs = array ();
foreach ( $TagNames as $Index => $TagName ) {
2013-10-15 08:01:05 +00:00
$Tag = G :: $Cache -> get_value ( " tag_id_ $TagName " );
2012-10-11 08:00:15 +00:00
if ( is_array ( $Tag )) {
unset ( $TagNames [ $Index ]);
$TagIDs [ $Tag [ 'ID' ]] = $Tag [ 'Name' ];
}
}
if ( count ( $TagNames ) > 0 ) {
2013-08-28 23:08:41 +00:00
$QueryID = G :: $DB -> get_query_id ();
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
SELECT ID , Name
FROM tags
WHERE Name IN ( '".implode("' , '", $TagNames)."' ) " );
2013-08-28 23:08:41 +00:00
$SQLTagIDs = G :: $DB -> to_array ();
G :: $DB -> set_query_id ( $QueryID );
2012-10-11 08:00:15 +00:00
foreach ( $SQLTagIDs as $Tag ) {
$TagIDs [ $Tag [ 'ID' ]] = $Tag [ 'Name' ];
2013-08-28 23:08:41 +00:00
G :: $Cache -> cache_value ( 'tag_id_' . $Tag [ 'Name' ], $Tag , 0 );
2012-10-11 08:00:15 +00:00
}
}
return ( $TagIDs );
}
/**
2014-02-27 08:00:30 +00:00
* Gets the alias of the tag ; if there is no alias , silently returns the original tag .
2012-10-11 08:00:15 +00:00
*
* @ param string $BadTag the tag we want to alias
* @ return string The aliased tag .
*/
public static function get_alias_tag ( $BadTag ) {
2013-08-28 23:08:41 +00:00
$QueryID = G :: $DB -> get_query_id ();
G :: $DB -> query ( "
2013-05-27 08:00:58 +00:00
SELECT AliasTag
FROM tag_aliases
WHERE BadTag = '$BadTag'
LIMIT 1 " );
2013-08-28 23:08:41 +00:00
if ( G :: $DB -> has_results ()) {
list ( $AliasTag ) = G :: $DB -> next_record ();
} else {
$AliasTag = $BadTag ;
2013-05-16 16:15:57 +00:00
}
2013-08-28 23:08:41 +00:00
G :: $DB -> set_query_id ( $QueryID );
return $AliasTag ;
2012-10-11 08:00:15 +00:00
}
/*
* Write a message to the system log .
*
* @ param string $Message the message to write .
*/
public static function write_log ( $Message ) {
2013-08-28 23:08:41 +00:00
global $Time ;
$QueryID = G :: $DB -> get_query_id ();
G :: $DB -> query ( "
2013-05-16 16:15:57 +00:00
INSERT INTO log ( Message , Time )
VALUES ( '" . db_string($Message) . "' , '" . sqltime() . "' ) " );
2013-08-28 23:08:41 +00:00
G :: $DB -> set_query_id ( $QueryID );
2012-10-11 08:00:15 +00:00
}
/**
* Get a tag ready for database input and display .
*
* @ param string $Str
* @ return sanitized version of $Str
*/
public static function sanitize_tag ( $Str ) {
$Str = strtolower ( $Str );
$Str = preg_replace ( '/[^a-z0-9.]/' , '' , $Str );
2013-06-18 08:00:48 +00:00
$Str = preg_replace ( '/(^[.,]*)|([.,]*$)/' , '' , $Str );
2012-10-11 08:00:15 +00:00
$Str = htmlspecialchars ( $Str );
$Str = db_string ( trim ( $Str ));
return $Str ;
}
/**
* HTML escape an entire array for output .
* @ param array $Array , what we want to escape
* @ param boolean / array $Escape
* if true , all keys escaped
* if false , no escaping .
* If array , it ' s a list of array keys not to escape .
* @ return mutated version of $Array with values escaped .
*/
public static function display_array ( $Array , $Escape = array ()) {
foreach ( $Array as $Key => $Val ) {
2013-04-18 08:00:54 +00:00
if (( ! is_array ( $Escape ) && $Escape == true ) || ! in_array ( $Key , $Escape )) {
2012-10-11 08:00:15 +00:00
$Array [ $Key ] = display_str ( $Val );
}
}
return $Array ;
}
2013-02-21 08:00:30 +00:00
2013-06-16 08:01:11 +00:00
/**
* Searches for a key / value pair in an array .
*
* @ return array of results
*/
public static function search_array ( $Array , $Key , $Value ) {
$Results = array ();
if ( is_array ( $Array ))
{
if ( isset ( $Array [ $Key ]) && $Array [ $Key ] == $Value ) {
$Results [] = $Array ;
}
foreach ( $Array as $subarray ) {
$Results = array_merge ( $Results , self :: search_array ( $subarray , $Key , $Value ));
}
}
return $Results ;
}
2013-06-27 08:01:06 +00:00
/**
* Search for $Needle in the string $Haystack which is a list of values separated by $Separator .
* @ param string $Haystack
* @ param string $Needle
* @ param string $Separator
* @ param boolean $Strict
* @ return boolean
*/
public static function search_joined_string ( $Haystack , $Needle , $Separator = '|' , $Strict = true ) {
return ( array_search ( $Needle , explode ( $Separator , $Haystack ), $Strict ) !== false );
}
2013-02-21 08:00:30 +00:00
/**
2014-02-27 08:00:30 +00:00
* Check for a " : " in the beginning of a torrent meta data string
2013-02-21 08:00:30 +00:00
* to see if it ' s stored in the old base64 - encoded format
*
* @ param string $Torrent the torrent data
* @ return true if the torrent is stored in binary format
*/
public static function is_new_torrent ( & $Data ) {
return strpos ( substr ( $Data , 0 , 10 ), ':' ) !== false ;
}
2013-03-05 08:00:26 +00:00
public static function display_recommend ( $ID , $Type , $Hide = true ) {
2013-04-18 08:00:54 +00:00
if ( $Hide ) {
2013-05-16 16:15:57 +00:00
$Hide = ' style="display: none;"' ;
2013-03-05 08:00:26 +00:00
}
?>
2013-05-16 16:15:57 +00:00
< div id = " recommendation_div " data - id = " <?= $ID ?> " data - type = " <?= $Type ?> " < ? = $Hide ?> class="center">
2013-04-18 08:00:54 +00:00
< div style = " display: inline-block; " >
< strong > Recommend to :</ strong >
< select id = " friend " name = " friend " >
< option value = " 0 " selected = " selected " > Choose friend </ option >
2013-03-05 08:00:26 +00:00
</ select >
2013-04-18 08:00:54 +00:00
< input type = " text " id = " recommendation_note " placeholder = " Add note... " />
< button id = " send_recommendation " disabled = " disabled " > Send </ button >
2013-03-05 08:00:26 +00:00
</ div >
2013-04-18 08:00:54 +00:00
< div class = " new " id = " recommendation_status " >< br /></ div >
2013-03-05 08:00:26 +00:00
</ div >
2013-05-16 16:15:57 +00:00
< ?
2013-03-05 08:00:26 +00:00
}
2013-08-28 23:08:41 +00:00
public static function is_valid_url ( $URL ) {
return preg_match ( '|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i' , $URL );
}
2012-10-11 08:00:15 +00:00
}
?>