replace text with url in twitter feed - preg-replace

I have this code:
public function linkify($status_text)
{
$status_text = preg_replace('/(https?:\/\/\S+)/','\1', $status_text);
$status_text = preg_replace('/(^|\s)#(\w+)/','\1#\2',$status_text);
$status_text = preg_replace('/(^|\s)#(\w+)/','\1#\2',$status_text);
return $status_text;
}
and display feeds from twitter like this
foreach($feed as $feed_item) {
$html .= '<li>';
$html .= '' . $this->linkify($feed_item->text) . '';
$html .= '' . $this->relativedate((strtotime($feed_item->created_at))) . '';
$html .= '</li>';
}
echo $html;
result of this code is
<li>Twitter Feed Text http://t.co/TnkNfxCdRu</li>
if anyone can help me, how can I add text inside tag <a></a>. for example, to be exactly like this:
<li>Twitter Feed Text</li>
Thank you so much

Just change your first preg_replace to:
$status_text = preg_replace('~(https?://(\S+))~','$2',$status_text);

Related

Two contact forms, send to two different email addresses

I got two contact forms on my website. On in the contact section, and one in the careers section. I want to receive both forms in different email adresses.
<?php
include_once('class.phpmailer.php');
$mail = new PHPMailer;
$mail->setFrom('adres1','test');
$mail->Subject = 'Contactverzoek via careerspagina.';
$html = "Er is een contactverzoek gedaan via de careerspagina.<br>";
$html .= "<br>";
$html .= "<b>Naam:</b> " . $_POST['name']."<br>";
if(isset($_POST['lastname'])) {
$html .= '<b>Achternaam: </b> ' . $_POST['lastname']."<br>";
}
$html .= "<b>E-mail:</b> " . $_POST['email']."<br>";
if(isset($_POST['telefoon'])) {
$html .= '<b>Telefoon: </b> ' . $_POST['telefoon']."<br>";
}
if(isset($_POST['onderwerp'])) { //FIX NOG!!!
$html .= '<b>Onderwerp: </b> ' . $_POST['onderwerp']."<br>";
}
if(isset($_POST['Werkervaring'])) {
$html .= '<b>Werkervaring: </b> ' . $_POST['Werkervaring']."<br>";
}
if(isset($_POST['Functie'])) {
$html .= '<b>Functie: </b> ' . $_POST['Functie']."<br>";
}
if(isset($_POST['Opleidingsniveaus'])) {
$html .= '<b>Opleidingsniveaus: </b> ' . $_POST['Opleidingsniveaus']."<br>";
}
if(isset($_POST['comment'])) {
$html .= '<b>Bericht: </b> ' . $_POST['comment']."<br>";
}
$html .= "<br>";
if (isset($_FILES['resume']) &&
$_FILES['resume']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['resume']['tmp_name'],
$_FILES['resume']['name']);
}
if (isset($_FILES['letter']) &&
$_FILES['letter']['error'] == UPLOAD_ERR_OK) {
$mail->AddAttachment($_FILES['letter']['tmp_name'],
$_FILES['letter']['name']);
}
$mail->msgHTML($html);
$mail->addAddress('adres1', 'Info ');
$mail->addBCC('adres1', 'Test ');
if(!$mail->send())
{
echo "0";
}
else
{
echo "1";
}
This is my code, and everything seems to work.
So once again. I want to keep sending this contact form to email A. But There needs to be made and else statement which sends the other contact form to email B.
i'm very new to PHP. Excuse me if i'm not clear.
One simple solution is to send a form parameter indicating which email address should be used. Then you check the POST variable for that parameter in an if statement and set the correct email address to be used.
$to = "forma#example.com";
if($_POST['form'] === "formB") {
$to = "formb#example.com";
}
$mail->addAddress($to, 'Recipient name');
Add hidden inputs in your forms named "form" and set the value to either "formA" or "formB":
<input type="hidden" name="form" value="formA">
Do not set the receiving email address in the form and use that since it can easily be abused to send spam from your server.

perl CGI radio button form

I have following CGI snippet from my code. its create textarea to type my message which i want to send, but i want to convert that into template style like "radio", so i select just radio button and it will inject whatever message into notification for customer, so i don't need to type message every time. just like template. Anybody has any example or code which can fix in my requirement?
use CGI;
use CGI::Carp;
...
...
$html .= $q->b("Customer Notification:") . $q->br;
$html .= $q->textarea(-name=>'notification',
-rows=>4,
-columns=>60) . $q->p;
$html .= $q->submit(-name=>' Send Notification ');
$html .= " ";
$html .= $q->reset(-name=>' Reset to Original Value ');
$html .= $q->p;
Like others have mentioned, you should really consider using one of the other solutions found in CGI::Alternatives.
Nevertheless...
my %labels = (
'comment' => 'General Comment',
'problem' => 'Non-critical Problem',
'emergency' => 'Critical Emergency',
);
$html .= $q->radio_group(
-name=>'notification',
-values=>['comment','problem','emergency'],
-default=>'comment',
-linebreak=>'true',
-labels=>\%labels,
);

htmlspecialchars($_SERVER['PHP_SELF'])

if($_POST) {
$mysql_pass = '';
$connect = #mysql_connect($_POST['dbHost'], $_POST['dbUser'], $mysql_pass);
if($connect) {
if(#mysql_select_db($_POST['dbName'])) {
$dir = $_SERVER['SERVER_NAME']. $_SERVER['REQUEST_URI'];
$redirectUrl = str_replace("configForm.php","site/index", $dir);
print $redirectUrl; exit;
$dbConf = dirname(__FILE__).'/dbConfig/dbconf.php';
$handle = fopen($dbConf, 'w') or die('Cannot open file: '.$dbConf);
$data = '<?php $userName="'.$_POST['dbUser'].'";'."\n";
$data .= '$passWord=" ";'."\n";
$data .= '$dbName="'.$_POST['dbName'].'";'."\n";
$data .= '$host="'.$_POST['dbHost'].'"; ?>';
fwrite($handle, $data);
header('Location: site/index');
}
else {
$error = 'Could not select database.';
}
}
else
$error = 'Not connected';
}
when the code above was executed, my webpage just displayed "localhost/www-edusec/site/index". i do not know where i do it wrong. my form action is like below:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
The action attribute defaults to the current page anyway, so that piece of code is surely redundant.
The problem is PHP_SELF returns the path to the file too. Just get rid of the action line completely and it should work fine.
EDIT:
Also, the reason your script is cutting off half way, you have an exit; after it prints out the directory.

Displaying images in atom feed

I have problems with displaying images in atom file. It doesn't include images in feed in google reader, opera or firefox.
As a starting point I did everything like in Listing 6. at [An overview of the Atom 1.0 Syndication Format] But it doesn't work.
Update
It is not problem with hotlink protected images. Described here: How to display item photo in atom feed?
Later I changed feed according to description posted here.
I added:
<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="path_to_image.jpg" />
But still it doesn't work
I had the same problem when trying to include images as enclosure, but it seemed that the easiest way for me was to include the image with the normal img tag to the html content.
(It's also wrapped in CDATA, which might affect the way Google Reader handles the content. I haven't tried without.)
The following example works for me to make atom feed images visible in Google Reader:
<content type="html">
<![CDATA[
<a href="http://test.lvh.me:3000/listings/341-test-pics?locale=en">
<img alt="test_pic" src="http://test.lvh.me:3000/system/images/20/medium/test_pic.jpg?1343246102" />
</a>
]]>
</content>
Wordpress uses the metafield enclosure to set the medias. This is the correct tag according to RSS specification. I have seen people suggest using media:content but if using that make sure to set the XML namespace for it.
Unfortunately due to some dodgy Wordpress code you can not set this dynamically. (Wordpress gets all metafields and then loops through them instead of calling the enclosure directly)
You can set the enclosure on save post. It should be an array with entries of the form "$url\n$length\n$type"
If you want to add the enclosure tags yourself you can do the following:
RSS
add_action( 'rss2_item', 'hughie_rss2_item_enclosure' );
function hughie_rss2_item_enclosure():void
{
$id = get_post_thumbnail_id();
$url = wp_get_attachment_url($id);
$length = filesize(get_attached_file($id));
$type = get_post_mime_type($id);
echo apply_filters( 'rss_enclosure', '<enclosure url="' . esc_url( $url ) . '" length="' . absint( $length ) . '" type="' . esc_attr( $type ) . '" />' . "\n" );
}
ATOM:
add_action( 'atom_entry', 'hughie_atom_entry_enclosure' );
function hughie_atom_entry_enclosure():void
{
$id = get_post_thumbnail_id();
$url = wp_get_attachment_url($id);
$length = filesize(get_attached_file($id));
$type = get_post_mime_type($id);
echo apply_filters( 'atom_enclosure', '<link rel="enclosure" href="' . esc_url( $url ) . '" length="' . absint( $length ) . '" type="' . esc_attr( $type ) . '" />' . "\n" );
}
The only way I found to set the enclosure dynamically is short-circuiting the get_metadata call. You can add checks to make sure that you are in a feed or even the check the stacktrace to make sure.
add_filter('get_post_metadata', 'hughie_get_post_metadata', 10, 5 );
function hughie_get_post_metadata($value, int $object_id, string $meta_key, bool $single, string $meta_type)
{
if (is_feed() && $meta_key === '') {
$backtrace = debug_backtrace();
if (isset($backtrace[7]['function']) && ( $backtrace[7]['function'] === 'rss_enclosure' || $backtrace[7]['function'] === 'atom_enclosure' ) ) {
if (!isset($value['enclosure'])) {
$value['enclosure'] = [];
}
$id = get_post_thumbnail_id();
$url = wp_get_attachment_url($id);
$length = filesize(get_attached_file($id));
$type = get_post_mime_type($id);
$value['enclosure'][] = "$url\n$length\n$type";
}
}
return $value;
}

Redisplaying a form with fields filled in

I need a bit of help with redisplaying a form.
Basically, currently a user will fill out my contact form, the form and it's contents are passed to my verification page, and if the recaptcha was entered correctly it goes to a Thank You page.
When the recaptcha is entered INCORRECTLY, I want to redisplay the contact form with the fields already filled out. How do I do this? (As you'll see below, it currently goes to google on incorrect captcha)
Here is my verification code. Any help would be great:
<?php require('sbsquared.class.php'); ?>
<?php
require_once('recaptchalib.php');
$privatekey = "myprivatekey";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
if (!$resp->is_valid) {
// What happens when the CAPTCHA was entered incorrectly
header("Location: http://www.google.com"); <--- this is the bit that I want to redisplay the form with fields already filled out.
} else {
$sb = New SBSquared;
$name = $_POST['FullName'];
$post_keys = array_keys($_POST);
$my_db_string = "<table>";
$ip_address = $_SERVER['REMOTE_ADDR'];
foreach($post_keys as $field)
{
if($_POST[$field] != "" && $field != "submit_y" && $field != "submit_x" && $field != "submit_x")
{
$my_db_string .= "<tr><td><b>".$field.":</b></td><td>";
if($field == "Email")
{
$my_db_string .= ''.$_POST['Email'].'';
}
else
{
$my_db_string .= $_POST[$field];
}
$my_db_string .= "</td></tr>";
}
}
$my_db_string .= "<tr><td><b>IP ADDRESS LOGGED: </b></td><td>".$ip_address."</td></tr>";
$my_db_string .= "</table>";
if(get_magic_quotes_gpc() != 1)
{
$my_db_string = addslashes($my_db_string);
$name = addslashes($name);
}
$conn = $sb->openConnection();
$dts = time();
$sql = "INSERT INTO `contact_queries` VALUES ('', '$name', '$my_db_string', 'n/a', 0, $dts)";
$result = mysql_query($sql, $conn) or die(mysql_error());
$content = '<div id="main_middle">';
$content .= '<span class="title">'.$sb->dt('Contact').'</span>
<p>'.$sb->dt('Thank you for your enquiry. We will contact you shortly.').'</p>
</div>';
// admin auto email.
$dts = date("d.m.y h:ia", time());
$admin_content = "New contact query at $dts";
$admin_content .= "\n\n--\n\n \r\n\r\n";
mail("email address", 'NOTIFICATION: new query', $admin_content, 'From: email address');
$FILE=fopen("./log/auto-contact.txt","a");
fwrite($FILE, $admin_content);
fclose($FILE);
echo pageHeader($sb);
echo pageContent($sb, $content);
echo pageFooter($sb);
}
?>
You probably already answered this for yourself, but if not you can set ReCaptcha to validate prior to submitting the form, much the same as HTML5 validation. It just won't let the user submit until the Captcha is correct. Now, I don't know if it will refresh the captcha if it is incorrect but most of the time I see people putting it into an iFrame so it doesn't refresh the page when refreshing the captcha.
As an alternative, you can use sessions to keep the data filled in.