Symfony 1.4 deprecated function in php - symfony-1.4

Anyone know what is this error? I need help with this Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in C:\xampp\htdocs\sfprojects\jobeet\lib\vendor\symfony\lib\response\sfWebResponse.class.php on line 409. I'm using xampp 1.8.3 and symfony 1.4.
I couldn't get to step forward because of this a weeke a go :'(. Any help will be appreciated. Thank you.

In myproject/lib/vendor/symfony/lib/response/sfWebResponse.class.php on line 409
protected function normalizeHeaderName($name)
{
// return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", strtr(ucfirst(strtolower($name)), '_', '-'));
return preg_replace_callback(
'/\-(.)/',
function ($matches) {
return '-'.strtoupper($matches[1]);
},
strtr(ucfirst(strtolower($name)), '_', '-')
);
}
FIX for pregtr method in /lib/vendor/symfony/lib/util/sfToolkit.class.php on line 360
public static function pregtr($search, $replacePairs){
// return preg_replace(array_keys($replacePairs), array_values($replacePairs), $search);
foreach($replacePairs as $pattern => $replacement)
{
if (preg_match('/(.*)e$/', $pattern, $matches))
{
$pattern = $matches[1];
$search = preg_replace_callback($pattern, function ($matches) use ($replacement) {
preg_match("/('::'\.)?([a-z]*)\('\\\\([0-9]{1})'\)/", $replacement, $match);
return ($match[1]==''?'':'::').call_user_func($match[2], $matches[$match[3]]);
}, $search);
}
else
{
$search = preg_replace($pattern, $replacement, $search);
}
}
return $search;
}
I've combined two answers from duplicate thread Symfony 1.4 using deprecated functions in php 5.5 answered by mika and xtech (up-vote them please)
It also affects /lib/form/addon/sfFormObject.class.php on line 281
protected function camelize($text)
{
//return preg_replace(array('#/(.?)#e', '/(^|_|-)+(.)/e'), array("'::'.strtoupper('\\1')", "strtoupper('\\2')"), $text); //ORIGINAL
return strtr(ucwords(strtr($text, array('/' => ':: ', '_' => ' ', '-' => ' '))), array(' ' => ''));
}

Found this:
http://blog.jakoubek.cz/symfony-1-4-deprecated-e-modifier
Tested, all changes look good

as this function is called in several files in sf 1.4, here are all changes that i made in my projects (based on different sources, including sf 1.4 forks):
lib/vendor/symfony/lib/command/sfCommandManager.class.php
## -108,7 +108,9 ## class sfCommandManager
else if (!is_array($arguments))
{
// hack to split arguments with spaces : --test="with some spaces"
- $arguments = preg_replace('/(\'|")(.+?)\\1/e', "str_replace(' ', '=PLACEHOLDER=', '\\2')", $arguments);
+ $arguments = preg_replace_callback('/(\'|")(.+?)\\1/', function ($match) {
+ return str_replace(' ', '=PLACEHOLDER=', $match[2]);
+ }, $arguments);
$arguments = preg_split('/\s+/', $arguments);
$arguments = str_replace('=PLACEHOLDER=', ' ', $arguments);
}
lib/vendor/symfony/lib/form/addon/sfFormObject.class.php
## -278,6 +278,6 ## abstract class sfFormObject extends BaseForm
protected function camelize($text)
{
- return preg_replace(array('#/(.?)#e', '/(^|_|-)+(.)/e'), array("'::'.strtoupper('\\1')", "strtoupper('\\2')"), $text);
+ return strtr(ucwords(strtr($text, array('/' => ':: ', '_' => ' ', '-' => ' '))), array(' ' => ''));
}
lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/form/sfFormFilterDoctrine.class.php
## -323,7 +323,7 ## abstract class sfFormFilterDoctrine extends sfFormFilter
protected function camelize($text)
{
- return sfToolkit::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\\1')", '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
+ return strtr(ucwords(strtr($text, array('/' => ':: ', '_' => ' ', '-' => ' '))), array(' ' => ''));
}
lib/vendor/symfony/lib/plugins/sfPropelPlugin/lib/form/sfFormFilterPropel.class.php
## -263,6 +263,6 ## abstract class sfFormFilterPropel extends sfFormFilter
protected function camelize($text)
{
- return sfToolkit::pregtr($text, array('#/(.?)#e' => "'::'.strtoupper('\\1')", '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
+ return strtr(ucwords(strtr($text, array('/' => ':: ', '_' => ' ', '-' => ' '))), array(' ' => ''));
}
lib/vendor/symfony/lib/response/sfWebResponse.class.php
## -406,7 +406,7 ## class sfWebResponse extends sfResponse
protected function normalizeHeaderName($name)
{
- return preg_replace('/\-(.)/e', "'-'.strtoupper('\\1')", strtr(ucfirst(strtolower($name)), '_', '-'));
+ return strtr(ucwords(strtr(strtolower($name), array('_' => ' ', '-' => ' '))), array(' ' => '-'));
}
lib/vendor/symfony/lib/util/sfInflector.class.php
## -27,11 +27,7 ## class sfInflector
public static function camelize($lower_case_and_underscored_word)
{
- $tmp = $lower_case_and_underscored_word;
- $tmp = sfToolkit::pregtr($tmp, array('#/(.?)#e' => "'::'.strtoupper('\\1')",
- '/(^|_|-)+(.)/e' => "strtoupper('\\2')"));
-
- return $tmp;
+ return strtr(ucwords(strtr($lower_case_and_underscored_word, array('/' => ':: ', '_' => ' ', '-' => ' '))), array(' ' => ''));
}
beside, here is the fix for the E_NOTICE lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Query/Abstract.php:
lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/vendor/doctrine/Doctrine/Query/Abstract.php
## -1149,7 +1149,16 ## abstract class Doctrine_Query_Abstract
$copy->free();
if ($componentsBefore !== $componentsAfter) {
- return array_diff($componentsAfter, $componentsBefore);
+ $diff = array();
+
+ foreach($componentsAfter as $key => $val) {
+ if(!isset($componentsBefore[$key])) {
+ $diff[$key] = $val;
+ } elseif(is_array($componentsBefore[$key]) && !is_array($val)) {
+ $diff[$key] = $val;
+ }
+ }
+ return $diff;
} else {
return $componentsAfter;
}

You can unset the E_DEPRECATED flag in every application settings.yml :
dev:
.settings:
error_reporting: <?php echo ((E_ALL | E_STRICT) ^ E_DEPRECATED)."\n" ?>

Related

Symfony Process Component mysqldump gzip

I'm trying to figure out how to add pipe with gzip compression to the mysqldump command using Symfony Process Component. What I have, and what doesn't seem to work is:
$date = Carbon::now();
$fileName = $date->format('Y-m-d_H-i-s').'.sql';
$command = [
$this->mysqldumpPath,
'--add-drop-table',
'--skip-comments',
'--default-character-set=utf8mb4',
$this->ignoreTables(),
'--user='.$this->dbConfig['username'],
'--password='.$this->dbConfig['password'],
$this->dbConfig['database'],
];
if ($this->compress) {
$command[] = '| gzip -9';
$fileName = $fileName.'.gz';
}
$file = $this->backupPath($fileName);
$process = new Process(array_merge(
$command,
['--result-file='.$file]
));
$process->run();
The error I'm getting is
mysqldump: Couldn't find table: "| gzip -9"
The ignoreTables() method
private function ignoreTables(): ?string
{
if (empty($this->ignoreTables)) {
return null;
}
return collect($this->ignoreTables)->map(
fn(string $table) => sprintf(
'--ignore-table=%s.%s',
$this->dbConfig['database'],
$table
)
)->implode(' ');
}
Any idea how I can pass piped gzip flag to the command?
You can still build your command as a string and run it in a shell with Process::fromShellCommandline.
I think your command won't work as written, though, so I'm making the changes I think are necessary (you can also clean this up and not use arrays to build the command).
Main:
$date = Carbon::now();
$fileName = $date->format('Y-m-d_H-i-s').'.sql';
$file = $this->backupPath($fileName);
$command = [
$this->mysqldumpPath,
'--add-drop-table',
'--skip-comments',
'--default-character-set=utf8mb4',
'--user="${:USER}"',
'--password="${:PASSWORD}"',
'"${:DATABASE}"',
];
$command = array_merge($command, $this->getIgnoredTablesArguments());
if ($this->compress) {
$command[] = ' | gzip -9 > "${:OUTPUT_FILE}"';
$file .= '.gz';
} else {
$command[] = '--result-file="${:OUTPUT_FILE}"';
}
$process = Process::fromShellCommandline(implode(' ', $command));
$parameters = [
'USER' => $this->dbConfig['username'],
'PASSWORD' => $this->dbConfig['password'],
'DATABASE' => $this->dbConfig['database'],
'OUTPUT_FILE' => $file,
];
$parameters = array_merge($parameters, $this->getIgnoredTablesParameters());
$process->run(null, $parameters);
Helper methods:
private function ignoreTables(): array
{
return collect($this->ignoreTables)->map(
fn(string $table) => sprintf(
'%s.%s',
$this->dbConfig['database'],
$table
)
);
}
private function getIgnoredTablesArguments(): array
{
$arguments = [];
foreach ($this->ignoreTables as $k => $v) {
$argName = sprintf('${:IGNORE_%s}', $k);
$arguments[] = sprintf('--ignore-table="%s"', $argName);
}
return $arguments;
}
private function getIgnoredTablesParameters(): array
{
$envs = [];
foreach ($this->ignoreTables() as $k => $v) {
$argName = sprintf('IGNORE_%s', $k);
$envs = array_merge($envs, [$argName => $v]);
}
return $envs;
}

Sending an email from WordPress through REST API with an Axios call

I'm using this snippet to send an email from WordPress through the REST API.
My Vue-based front end is posting form data to WordPress using an Axios put() call:
sendEmail () {
const formData = new FormData()
formData.append('contact_name', this.contactName)
formData.append('contact_email', this.contactEmail)
formData.append('contact_message', this.contactMessage)
this.$axios.$put(`${this.baseUrl}/wp-json/contact/v1/send`, formData)
.then((res) => {
this.success = true
})
.catch((err) => {
this.$toast.error(err.response)
})
}
I suppose the code above is correct, and that the issue lies on the WordPress side:
Functions.php:
function sendContactMail(WP_REST_Request $request) {
$response = array(
'status' => 304,
'message' => 'There was an error sending the form.'
);
$parameters = $request->get_json_params();
if (count($_POST) > 0) {
$parameters = $_POST;
}
$siteName = wp_strip_all_tags(trim(get_option('blogname')));
$contactName = wp_strip_all_tags(trim($parameters['contact_name']));
$contactEmail = wp_strip_all_tags(trim($parameters['contact_email']));
$contactMessage = wp_strip_all_tags(trim($parameters['contact_message']));
if (!empty($contactName) && !empty($contactEmail) && !empty($contactMessage)) {
$subject = "(New message sent from site $siteName) $contactName <$contactEmail>";
$body = "<h3>$subject</h3><br/>";
$body .= "<p><b>Name:</b> $contactName</p>";
$body .= "<p><b>Email:</b> $contactEmail</p>";
$body .= "<p><b>Message:</b> $contactMessage</p>";
if (send_email($contactEmail, $contactName, $body)) {
$response['status'] = 200;
$response['message'] = 'Form sent successfully.';
}
}
return json_decode(json_encode($response));
exit();
}
add_action('rest_api_init', function () {
register_rest_route( 'contact/v1', '/send', array(
'methods' => 'POST',
'callback' => 'sendContactMail'
));
});
However, I have no idea how to troubleshoot the issue, because whichever problem is occuring here doesn't produce any input that I can see (I don't have access to server PHP log unfortunately).
Any idea about what's wrong in my code or how I could troubleshoot it?
Just in case below is the code of the send_email() function as well:
function send_email($form_email, $form_name, $form_message) {
$email_subject = 'Message from '. get_bloginfo('name') . ' - ' . $form_email;
$headers = "From: '" . $form_name . "' <" . $form_email . "> \r\n";
$headers .= "Reply-To: ". strip_tags($form_email) . "\r\n";
$headers .= "Content-Type:text/html;charset=utf-8";
$email_message = '<html><body>';
$email_message .= "<table>";
$email_message .= "<tr><td>NAME: </td><td>" . $form_name . "</td></tr>";
$email_message .= "<tr><td>MESSAGE: </td><td>" . $form_message . "</td></tr>";
$email_message .= "</table>";
$email_message .= "</body></html>";
$email_message = nl2br($email_message);
wp_mail('me#gmail.com', $email_subject, $email_message, $headers);
}
the problem was in your axios put request. your form was not submitted correctly to the server due to missing header:
this.$axios.$put(`${this.baseUrl}/wp-json/contact/v1/send`, formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
.then((res) => {
this.success = true
})
.catch((err) => {
this.$toast.error(err.response)
})
i debugged it via more parameters in your wordpress function request response. i saw that all the form parameters where missing, so i investigated on that end and viola: its working now.

Drupal 7: How to send HTML Email

Could someone tell me what i am missing to send an HTML email using Drupal's function? Here is my call:
try{
drupal_mail('my_module', 'forgot', $node->field_email_address['und'][0]['value'], language_default(), array('reset_key' => $key),'do-not-reply#myemailaddress.com');
}catch(Exception $e){
print_r($e->getMessage());die();
}
And here is the function:
function my_module_mail($key, &$message, $params) {
$body = '<p>Click the link below to reset your password.</p>
<p>Click this link to reset your password</p>
';
// $headers = array(
// 'MIME-Version' => '1.0',
// 'Content-Type' => 'text/html; charset=UTF-8; format=flowed',
// 'Content-Transfer-Encoding' => '8Bit',
// 'X-Mailer' => 'Drupal'
// );
// $message['headers'] = $headers;
$message['subject'] = 'Why wont this send html??';
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
$message['body'][] = $body;
$message['from'] = 'do-not-reply#myemailaddress.com';
}
I tired just the html header and the full set that is commented out. What am I missing? The email sends fine but it's plain text. Thanks and let me know!
You can use this function
function my_module_custom_drupal_mail($target = NULL, $from = null, $subject, $message, $attachment = NULL){
$my_module = 'my_module';
$my_mail_token = microtime();
$message = array(
'id' => $my_module . '_' . $my_mail_token,
'to' => $target,
'subject' => $subject,
'body' => array($message),
'module' => $my_module,
'key' => $my_mail_token,
'from' => "$from <email#email.com>",
'headers' => array(
'From' => "$from <email#email.com>",
'Sender' => "$from <email#email.com>",
'Return-Path' => "$from <email#email.com>",
'Content-Type' => 'text/html; charset=utf-8'
),
);
if ($attachment) {
$file_content = file_get_contents($attachment[0]);
$message['params']['attachments'][] = array(
'filecontent' => $file_content,
'filename' => $attachment[1],
'filemime' => $attachment[2],
);
}
$system = drupal_mail_system($my_module, $my_mail_token);
$message = $system->format($message);
if ($system->mail($message)) {
return TRUE;
}
else {
return FALSE;
}
}
AND call it like :
$body = '<p>Click the link below to reset your password.</p>
<p>Click this link to reset your password</p>
';
$subject ='Why wont this send html??';
$from = 'myemail#email.com';
$sent = my_module_custom_drupal_mail($node->field_email_address['und'][0]['value'], $from, $subject, $body);
Customize it like you want ! :)
A few things need to be done:
/**
* Class SomeCustomModuleMailSystem Implements MailSystemInterface.
*
* Used to enable HTML email to be sent.
*/
class SomeCustomModuleMailSystem extends DefaultMailSystem {
public function format(array $message) {
$message['body'] = implode("\n\n", $message['body']);
$message['body'] = drupal_wrap_mail($message['body']);
return $message;
}
}
This to be done one time, so probably in a hook_enable or hook_update:
$current = variable_get('mail_system', ['default-system' => 'DefaultMailSystem']);
$addition = ['some_custom_module' => 'SomeCustomModuleMailSystem'];
variable_set('mail_system', array_merge($current, $addition));
Invoke hook_mail as normal, e.g.
/**
* Implements hook_mail().
*/
function some_custom_module_mail($key, &$message, $params) {
switch ($key) {
case 'some_mail_key':
$message['headers']['Content-Type'] = 'text/html; charset=UTF-8;';
$message['subject'] = $params['subject'];
$message['body'][] = $params['body'];
break;
}
}
Finally call it with something like this:
// Set variables required for the email.
$module = 'some_custom_module';
$key = 'some_mail_key';
$to = $email = 'thetoaddress#something.com';
$language = language_default();
$params['subject'] = 'Email subject';
$params['body'] = '<html><body>The HTML!</body></html>';
$from = 'thefromaddress#something.com';
$send = TRUE;
// Send the mail and log the result.
$result = drupal_mail($module, $key, $to, $language, $params, $from, $send);
if ($result['result'] === TRUE) {
watchdog('some_custom_module', 'HTML email successfully sent.', [], WATCHDOG_INFO);
}
else {
watchdog('some_custom_module', 'HTML email failed to send', [], WATCHDOG_ERROR);
}

Can't send email from Nginx php - fpm based dedi?

I have a site and it has contact page. in that page there is Email & Comment tab. Whenever someone Put his email & comment & click on submit mail should come to my email. but it is not working.
I am using this php file for mail function.
I have nginx & php - fpm installed in my dedicated server.
<?php
/*
This first bit sets the email address that you want the form to be submitted to.
You will need to change this value to a valid email address that you can access.
*/
$webmaster_email = "myemail#yahoo.com";
/*
This bit sets the URLs of the supporting pages.
If you change the names of any of the pages, you will need to change the values here.
*/
$feedback_page = "contact.html";
$error_page = "error_message.html";
$thankyou_page = "thank_you.html";
/*
This next bit loads the form field data into variables.
If you add a form field, you will need to add it here.
*/
$email_address = $_REQUEST['email_address'] ;
$comments = $_REQUEST['comments'] ;
/*
The following function checks for email injection.
Specifically, it checks for carriage returns - typically used by spammers to inject a CC list.
*/
function isInjected($str) {
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str)) {
return true;
}
else {
return false;
}
}
// If the user tries to access this script directly, redirect them to the feedback form,
if (!isset($_REQUEST['email_address'])) {
header( "Location: $feedback_page" );
}
// If the form fields are empty, redirect to the error page.
elseif (empty($email_address) || empty($comments)) {
header( "Location: $error_page" );
}
// If email injection is detected, redirect to the error page.
elseif ( isInjected($email_address) ) {
header( "Location: $error_page" );
}
// If we passed all previous tests, send the email then redirect to the thank you page.
else {
mail( "$webmaster_email", "Feedback Form Results",
$comments, "From: $email_address" );
header( "Location: $thankyou_page" );
}
?>
But mail is not coming to my email. it was working when i have apache. I am new to nginx. so how can i make it work for nginx.
Any help would be appreciated.
There you go a very sweet alternative to using mail() function.
See last function for configuration.
<?php
class Xmail{
# LOG VAR
public $log = Array();
# NEW LINE
private $line = "\r\n";
# ATTACHED FILES
public $files = Array();
# CONFIG GENERAL
private $tpl = "";
private $mode = "mail";
function setTPL($value) { if($value != "") $this->tpl = $value; }
function setMODE($value) { if($value != "") $this->mode = $value; }
# CONFIG SMTP
private $smtp_host = "localhost";
private $smtp_port = "25";
private $smtp_username = "";
private $smtp_password = "";
function setSmtpHost($value) { if($value != "") $this->smtp_host = $value; }
function setSmtpPort($value) { if($value != "") $this->smtp_port = $value; }
function setSmtpUser($value) { if($value != "") $this->smtp_username = $value; }
function setSmtpPass($value) { if($value != "") $this->smtp_password = $value; }
# CONFIG SOCKET
private $from = "sokmail#localhost"; // sender email address
private $host = "localhost"; // your domain name here
private $port = "25"; // it is always 25 but i think it's best to have this for tests when developper pc has port 25 blocked and server has alternate port [i use 26 cause 25 is locked for anti SPAM by ISP]
private $time = "30"; // timeout [time short :D]
private $test = false; // test mode, does not send the email but you can see the log up to the point of sending email, good to check email addresses if valid or server if black-listed
function setFrom($value) { if($value != "") $this->from = $value; }
function setHost($value) { if($value != "") $this->host = $value; }
function setPort($value) { if($value != "") $this->port = $value; }
function setTime($value) { if($value != "") $this->time = $value; }
function setTest($value) { if($value != "") $this->test = $value; }
# MAIN FUNCTION
function mail($to, $subject, $msg, $headers, $attachments = NULL) {
# MESSAGE HTML
$msg = str_replace("\'","'",$msg);
$msg = str_replace('\"','"',$msg);
# Use template if case
if(is_file($this->tpl)){
$html = implode("", file($this->tpl));
$html = str_replace("{MESSAGE}", $msg, $html);
}else
$html = $msg;
$boundary1 = '-----='.md5(uniqid(rand()));
$boundary2 = '-----='.md5(uniqid(rand()));
$message .= "\r\nThis is a multi-part message in MIME format.\r\n\r\n";
$message .= "--".$boundary1."\r\n";
$message .= "Content-Type: multipart/alternative;\r\n boundary=\"$boundary2\"\r\n\r\n";
# MESSAGE TEXT
$message .= "--".$boundary2."\r\n";
$message .= "Content-Type: text/plain;\r\n charset=\"UTF-8\"\r\n";
$message .= "Content-Transfer-Encoding: 7bit\r\n";
$message .= strip_tags($msg) . "\r\n";
$message .= "\r\n\r\n";
# MESSAGE HTML
$message .= "--".$boundary2."\r\n";
$message .= "Content-Type: text/html;\r\n charset=\"UTF-8\"\r\n";
$message .= "Content-Transfer-Encoding: quoted-printable\r\n\r\n";
$message .= "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n";
$message .= "<html>\r\n";
$message .= "<body>\r\n";
$message .= str_replace("
", "<br/>", $html) . "<br/>\r\n";
$message .= "</body>\r\n";
$message .= "</html>\r\n\r\n";
$message .= "--".$boundary2."--\r\n\r\n";
if(is_array($attachments)) {
foreach($attachments AS $file_url) {
if(is_file($file_url)) {
$file_name = pathinfo($file_url, PATHINFO_BASENAME);
$file_type = $this->find_mime(pathinfo($file_url, PATHINFO_EXTENSION));
# ATTACHMENT
$message .= "--".$boundary1."\r\n";
$message .= "Content-Type: ".$file_type.";\r\n name=\"$file_name\"\r\n";
$message .= "Content-Transfer-Encoding: base64\r\n";
$message .= "Content-Disposition: attachment;\r\n filename=\"$file_name\"\r\n\r\n";
$fp = fopen($file_url, 'r');
do {
$data = fread($fp, 8192);
if (strlen($data) == 0) break;
$content .= $data;
}
while (true);
$content_encode = chunk_split(base64_encode($content));
$message .= $content_encode."\r\n\r\n";
$content = '';
unset($content);
}
}
}
$message .= "--".$boundary1."--\r\n\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed;\r\n boundary=\"$boundary1\"\r\n";
if($this->mode == "smtp" || $this->mode == "mx")
return $this->sokmail($to, $subject, $message, $headers);
else {
if(mail($to, $subject, $message, $headers)) return true;
return false;
}
}
# send mail directly to destination MX server
function sokmail($to, $subject, $message, $headers) {
// get server based on mode
if($this->mode == "mx") {
list($user, $domain) = split("#",$to);
getmxrr($domain, $mxhosts);
$server = $mxhosts['0'];
}else{
$server = $this->smtp_host;
}
# open socket
$socket = #fsockopen($server, $this->port, $errno, $errstr, $this->time);
if(empty($socket)) { return false; }
if($this->parse_response($socket, 220, "SOCKET") != 220) { fclose($socket); return false; }
# say HELO to our little friend
fputs($socket, "EHLO " . $this->host . $this->line);
if($this->parse_response($socket, 250, "HELO") != 250) { fclose($socket); return false; }
# if SMTP
if($this->mode == "smtp" && !empty($this->smtp_username) && !empty($this->smtp_password) ) {
# start login
fputs($socket, "AUTH LOGIN" . $this->line);
if($this->parse_response($socket, 334, "AUTH LOGIN") != 334) { fclose($socket); return false; }
fputs($socket, base64_encode($this->smtp_username) . $this->line);
if($this->parse_response($socket, 334, "USERNAME") != 334) { fclose($socket); return false; }
fputs($socket, base64_encode($this->smtp_password) . $this->line);
if($this->parse_response($socket, 235, "PASSWORD") != 235) { fclose($socket); return false; }
}
# email from
fputs($socket, "MAIL FROM: <" . $this->from . ">" . $this->line);
if($this->parse_response($socket, 250, "MAIL FROM") != 250) { fclose($socket); return false; }
# email to
fputs($socket, "RCPT TO: <" . $to . ">" . $this->line);
if($this->parse_response($socket, 250, "RCPT TO") != 250) { fclose($socket); return false; }
# check for test mode
if($this->test != true) {
# send data start command
fputs($socket, "DATA" . $this->line);
if($this->parse_response($socket, 354, "DATA") != 354) { fclose($socket); return false; }
# make the deposit :)
fputs($socket, "Subject: " . $subject . $this->line);
fputs($socket, "To: " . $to . $this->line);
fputs($socket, $headers . $this->line);
fputs($socket, $message . $this->line);
fputs($socket, "." . $this->line); # this line sends a dot to mark the end of message
if($this->parse_response($socket, 250, ".") != 250) { fclose($socket); return false; }
}
# say goodbye
fputs($socket,"QUIT" . $this->line);
$this->parse_response($socket, 221, "QUIT");
fclose($socket);
return true;
}
# parse server responces for above function
function parse_response($socket, $expected, $cmd) {
$response = '';
$this->log[$cmd] = "";
while (substr($response, 3, 1) != ' ') {
if(!($response = fgets($socket, 256))) $this->log["ERROR RESPONSE"] = "Couldn't get mail server response codes.";
else $this->log[$cmd] .= $response;
# for security we break the loop after 10 cause this should not happen ever
$i++;
if($i == 10) return false;
}
# shows an error if expected code not received
if(substr($response, 0, 3) != $expected) $this->log["ERROR CODES"] = "Ran into problems sending Mail. Received: " . substr($response, 0, 3) . ".. but expected: " . $expected;
# access denied..quit
if(substr($response, 0, 3) == 451) $this->log["ERROR QUIT"] = "Server declined access. Quitting.";
return substr($response, 0, 3);
}
function find_mime($ext) {
# create mimetypes array
$mimetypes = $this->mime_array();
# return mime type for extension
if (isset($mimetypes[$ext])) {
return $mimetypes[$ext];
# if the extension wasn't found return octet-stream
} else {
return 'application/octet-stream';
}
}
function mime_array() {
return array(
"ez" => "application/andrew-inset",
"hqx" => "application/mac-binhex40",
"cpt" => "application/mac-compactpro",
"doc" => "application/msword",
"bin" => "application/octet-stream",
"dms" => "application/octet-stream",
"lha" => "application/octet-stream",
"lzh" => "application/octet-stream",
"exe" => "application/octet-stream",
"class" => "application/octet-stream",
"so" => "application/octet-stream",
"dll" => "application/octet-stream",
"oda" => "application/oda",
"pdf" => "application/pdf",
"ai" => "application/postscript",
"eps" => "application/postscript",
"ps" => "application/postscript",
"smi" => "application/smil",
"smil" => "application/smil",
"wbxml" => "application/vnd.wap.wbxml",
"wmlc" => "application/vnd.wap.wmlc",
"wmlsc" => "application/vnd.wap.wmlscriptc",
"bcpio" => "application/x-bcpio",
"vcd" => "application/x-cdlink",
"pgn" => "application/x-chess-pgn",
"cpio" => "application/x-cpio",
"csh" => "application/x-csh",
"dcr" => "application/x-director",
"dir" => "application/x-director",
"dxr" => "application/x-director",
"dvi" => "application/x-dvi",
"spl" => "application/x-futuresplash",
"gtar" => "application/x-gtar",
"hdf" => "application/x-hdf",
"js" => "application/x-javascript",
"skp" => "application/x-koan",
"skd" => "application/x-koan",
"skt" => "application/x-koan",
"skm" => "application/x-koan",
"latex" => "application/x-latex",
"nc" => "application/x-netcdf",
"cdf" => "application/x-netcdf",
"sh" => "application/x-sh",
"shar" => "application/x-shar",
"swf" => "application/x-shockwave-flash",
"sit" => "application/x-stuffit",
"sv4cpio" => "application/x-sv4cpio",
"sv4crc" => "application/x-sv4crc",
"tar" => "application/x-tar",
"tcl" => "application/x-tcl",
"tex" => "application/x-tex",
"texinfo" => "application/x-texinfo",
"texi" => "application/x-texinfo",
"t" => "application/x-troff",
"tr" => "application/x-troff",
"roff" => "application/x-troff",
"man" => "application/x-troff-man",
"me" => "application/x-troff-me",
"ms" => "application/x-troff-ms",
"ustar" => "application/x-ustar",
"src" => "application/x-wais-source",
"xhtml" => "application/xhtml+xml",
"xht" => "application/xhtml+xml",
"zip" => "application/zip",
"au" => "audio/basic",
"snd" => "audio/basic",
"mid" => "audio/midi",
"midi" => "audio/midi",
"kar" => "audio/midi",
"mpga" => "audio/mpeg",
"mp2" => "audio/mpeg",
"mp3" => "audio/mpeg",
"aif" => "audio/x-aiff",
"aiff" => "audio/x-aiff",
"aifc" => "audio/x-aiff",
"m3u" => "audio/x-mpegurl",
"ram" => "audio/x-pn-realaudio",
"rm" => "audio/x-pn-realaudio",
"rpm" => "audio/x-pn-realaudio-plugin",
"ra" => "audio/x-realaudio",
"wav" => "audio/x-wav",
"pdb" => "chemical/x-pdb",
"xyz" => "chemical/x-xyz",
"bmp" => "image/bmp",
"gif" => "image/gif",
"ief" => "image/ief",
"jpeg" => "image/jpeg",
"jpg" => "image/jpeg",
"jpe" => "image/jpeg",
"png" => "image/png",
"tiff" => "image/tiff",
"tif" => "image/tif",
"djvu" => "image/vnd.djvu",
"djv" => "image/vnd.djvu",
"wbmp" => "image/vnd.wap.wbmp",
"ras" => "image/x-cmu-raster",
"pnm" => "image/x-portable-anymap",
"pbm" => "image/x-portable-bitmap",
"pgm" => "image/x-portable-graymap",
"ppm" => "image/x-portable-pixmap",
"rgb" => "image/x-rgb",
"xbm" => "image/x-xbitmap",
"xpm" => "image/x-xpixmap",
"xwd" => "image/x-windowdump",
"igs" => "model/iges",
"iges" => "model/iges",
"msh" => "model/mesh",
"mesh" => "model/mesh",
"silo" => "model/mesh",
"wrl" => "model/vrml",
"vrml" => "model/vrml",
"css" => "text/css",
"html" => "text/html",
"htm" => "text/html",
"asc" => "text/plain",
"txt" => "text/plain",
"rtx" => "text/richtext",
"rtf" => "text/rtf",
"sgml" => "text/sgml",
"sgm" => "text/sgml",
"tsv" => "text/tab-seperated-values",
"wml" => "text/vnd.wap.wml",
"wmls" => "text/vnd.wap.wmlscript",
"etx" => "text/x-setext",
"xml" => "text/xml",
"xsl" => "text/xml",
"mpeg" => "video/mpeg",
"mpg" => "video/mpeg",
"mpe" => "video/mpeg",
"qt" => "video/quicktime",
"mov" => "video/quicktime",
"mxu" => "video/vnd.mpegurl",
"avi" => "video/x-msvideo",
"movie" => "video/x-sgi-movie",
"ice" => "x-conference-xcooltalk"
);
}
}
# PHP does not have getmxr function on windows so I built one
function win_getmxrr($hostname, &$mxhosts, &$mxweight=false) {
if (strtoupper(substr(PHP_OS, 0, 3)) != 'WIN') return;
if (!is_array ($mxhosts) ) $mxhosts = array();
if (empty($hostname)) return;
$exec='nslookup -type=MX '.escapeshellarg($hostname);
#exec($exec, $output);
if (empty($output)) return;
$i=-1;
foreach ($output as $line) {
$i++;
if (preg_match("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.+)$/i", $line, $parts)) {
$mxweight[$i] = trim($parts[1]);
$mxhosts[$i] = trim($parts[2]);
}
if (preg_match('/responsible mail addr = (.+)$/i', $line, $parts)) {
$mxweight[$i] = $i;
$mxhosts[$i] = trim($parts[1]);
}
}
return ($i!=-1);
}
if (!function_exists('getmxrr')) {
function getmxrr($hostname, &$mxhosts, &$mxweight=false) {
return win_getmxrr($hostname, $mxhosts, $mxweight);
}
}
function xmail($to, $subject, $message, $headers="", $attachments=""){
$xmail = new Xmail();
$xmail->setMODE("mx"); // default is mail; options: mail,smtp,mx
// MX setup if in 'mx' mode
$xmail->setFrom("someone#somedomain.com");
$xmail->setHost($_SERVER['HTTP_HOST']);
// SMTP SETUP if in 'smtp' mode
$xmail->setSmtpHost("localhost");
$xmail->setSmtpPort(25);
$xmail->setSmtpUser("someuser");
$xmail->setSmtpPass("somepassword");
// from this point on you have to provide the same info as if you would use mail()
if($headers == "")
$headers = "From: <someone#somedomain.com>\r\n";
// !!! DO NOT ADD MIME VERSION OR CONTENT TYPE HEADERS !!!
// send the email
$xmail->mail($to, $subject, $message, $headers, $attachments);
}
?>

Extending WP_List_Table / handling checkbox options in plugin administration

I'm working on a WordPress plugin, and part of that plugin requires extending WP_List_Table and storing any of the items which are checked in that table to an option. I've managed to figure out how to properly setup and display the required table, but how do I handle storing the checked options?
Here's what I've got so far...
class TDBar_List_Table extends WP_List_Table {
// Reference parent constructor
function __construct() {
global $status, $page;
// Set defaults
parent::__construct( array(
'singular' => 'theme',
'plural' => 'themes',
'ajax' => false
));
}
// Set table classes
function get_table_classes() {
return array('widefat', 'wp-list-table', 'themes');
}
// Setup default column
function column_default($item, $column_name) {
switch($column_name) {
case 'Title':
case 'URI':
case'Description':
return $item[$column_name];
default:
return print_r($item, true);
}
}
// Displaying checkboxes!
function column_cb($item) {
return sprintf(
'<input type="checkbox" name="%1$s" id="%2$s" value="checked" />',
//$this->_args['singular'],
$item['Stylesheet'] . '_status',
$item['Stylesheet'] . '_status'
);
}
// Display theme title
function column_title($item) {
return sprintf(
'<strong>%1$s</strong>',
$item['Title']
);
}
// Display theme preview
function column_preview($item) {
if (file_exists(get_theme_root() . '/' . $item['Stylesheet'] . '/screenshot.png')) {
$preview = get_theme_root_uri() . '/' . $item['Stylesheet'] . '/screenshot.png';
} else {
$preview = '';
}
return sprintf(
'<img src="%3$s" style="width: 150px;" />',
$preview,
$item['Title'],
$preview
);
}
// Display theme description
function column_description($item) {
if (isset($item['Version'])) {
$version = 'Version ' . $item['Version'];
if (isset($item['Author']) || isset($item['URI']))
$version .= ' | ';
} else {
$version = '';
}
if (isset($item['Author'])) {
$author = 'By ' . $item['Author'];
if (isset($item['URI']))
$author .= ' | ';
} else {
$author = '';
}
if (isset($item['URI'])) {
$uri = $item['URI'];
} else {
$uri = '';
}
return sprintf(
'<div class="theme-description"><p>%1$s</p></div><div class="second theme-version-author-uri">%2$s%3$s%4$s',
$item['Description'],
$version,
$author,
$uri
);
}
// Setup columns
function get_columns() {
$columns = array(
'cb' => '<input type="checkbox" />',
'title' => 'Theme',
'preview' => 'Preview',
'description' => 'Description'
);
return $columns;
}
// Make title column sortable
function get_sortable_columns() {
$sortable_columns = array(
'title' => array('Title', true)
);
return $sortable_columns;
}
// Setup bulk actions
function get_bulk_actions() {
$actions = array(
'update' => 'Update'
);
return $actions;
}
// Handle bulk actions
function process_bulk_action() {
// Define our data source
if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE == true) {
$themes = get_allowed_themes();
} else {
$themes = get_themes();
}
if ('update' === $this->current_action()) {
foreach ($themes as $theme) {
if ($theme['Stylesheet'] . '_status' == 'checked') {
// Do stuff - here's the problem
}
}
}
}
// Handle data preparation
function prepare_items() {
// How many records per page?
$per_page = 10;
// Define column headers
$columns = $this->get_columns();
$hidden = array();
$sortable = $this->get_sortable_columns();
// Build the array
$this->_column_headers = array($columns, $hidden, $sortable);
// Pass off bulk action
$this->process_bulk_action();
// Define our data source
if (defined('WP_ALLOW_MULTISITE') && WP_ALLOW_MULTISITE == true) {
$themes = get_allowed_themes();
} else {
$themes = get_themes();
}
// Handle sorting
function usort_reorder($a,$b) {
$orderby = (!empty($_REQUEST['orderby'])) ? $_REQUEST['orderby'] : 'Title';
$order = (!empty($_REQUEST['order'])) ? $_REQUEST['order'] : 'asc';
$result = strcmp($a[$orderby], $b[$orderby]);
return ($order === 'asc') ? $result : -$result;
}
usort($themes, 'usort_reorder');
//MAIN STUFF HERE
//for ($i = 0; i < count($themes); $i++) {
//}
// Figure out the current page and how many items there are
$current_page = $this->get_pagenum();
$total_items = count($themes);
// Only show the current page
$themes = array_slice($themes,(($current_page-1)*$per_page),$per_page);
// Display sorted data
$this->items = $themes;
// Register pagination options
$this->set_pagination_args( array(
'total_items' => $total_items,
'per_page' => $per_page,
'total_pages' => ceil($total_items/$per_page)
));
}
}
Problem is, I can't get it to save properly. I select the rows I want, hit save and it just resets.
I assume you are talking about the checkboxes in your table listing, so this will be how to process bulk actions.
All you need to do is add two new methods to your class and initialize it in the prepare_items method. I use the code below in one of my plugins to delete or export, but you can just as easily run an update.
/**
* Define our bulk actions
*
* #since 1.2
* #returns array() $actions Bulk actions
*/
function get_bulk_actions() {
$actions = array(
'delete' => __( 'Delete' , 'visual-form-builder'),
'export-all' => __( 'Export All' , 'visual-form-builder'),
'export-selected' => __( 'Export Selected' , 'visual-form-builder')
);
return $actions;
}
/**
* Process our bulk actions
*
* #since 1.2
*/
function process_bulk_action() {
$entry_id = ( is_array( $_REQUEST['entry'] ) ) ? $_REQUEST['entry'] : array( $_REQUEST['entry'] );
if ( 'delete' === $this->current_action() ) {
global $wpdb;
foreach ( $entry_id as $id ) {
$id = absint( $id );
$wpdb->query( "DELETE FROM $this->entries_table_name WHERE entries_id = $id" );
}
}
}
Now, call this method inside prepare_items() like so:
function prepare_items() {
//Do other stuff in here
/* Handle our bulk actions */
$this->process_bulk_action();
}
There's a fantastic helper plugin called Custom List Table Example that makes figuring out the WP_List_Table class much easier.

Resources