How use Send() if the content has "" in it ? (autoIt) - autoit

Run("cmd.exe")
Global $String = "wmic process where name="example.exe" get commandline"
Send($String)
Result :
Global $String = "wmic process where name="example.exe" get commandline"
Global $String = "wmic process where name="^ ERROR

You just use single quotes to surround your string.
$string = 'test "example" and more stuff'
Or you can "escape" your double quotes by doubling them like stated in the Strings description of the Datatypes in AutoIt.
$string = "test ""example"" and more stuff"

Related

automatically generate tags from content in wordpress

my WordPress website contents have some words in every line,
I want a code to automatically convert every line in content to tag (every line not every word), let's say my content is like this:
Beagle puppy
Costumes
Wales
Dogs
I want tags from every line: Beagle puppy, Costumes, Wales, Dogs
I don't want to use plugins, because I used some but it needs a keyword list to match the content. I don't want to use any keyword list to match the content.
is it possible to convert every line in content into one tag?
Assuming you meant what you appeared to mean - for instance, that the source post consists solely of the lines to be converted, the lines won't include additional odd/inappropriate chracters to be handled, and so on - the following will work. If you need to convert a section of a post or some other element defined in some other way, or attach the lines as tags to the current post, etc., then you'll need to provide those details clearly.
Place shortcode [convert_post_lines_to_tags] in new output post.
Save draft, and preview (shortcode won't function yet, obviously)
Add functions to your theme functions.php
Provide "$source_post_id" where indicated.
Re-load output post
add_shortcode( 'convert_post_lines_to_tags', 'convert_post_lines_to_tags' ) ;
function convert_post_lines_to_tags() {
$source_post_id = '' ; //Provide ID Number of post with lines to be converted
$i = 0 ;
$newTags = 'New tags inserted: <br />' ;
//TIL - PHP requires double quotes to replace escaped characters
$post_content = str_replace(
array( "\r\n", "\r" ), ',', get_post( $source_post_id )->post_content
) ;
$post_line_array = explode( ',', $post_content ) ;
foreach ( $post_line_array as $line_tag ) {
$tag = wp_insert_term( $line_tag, 'post_tag' ) ;
if ( ! is_wp_error( $tag ) ) {
$i++ ;
$newTags .= $i . '. ' . get_term( $tag['term_id'] )->name . '<br />' ;
}
}
return $newTags ;
}

Prevent WordPress from escaping shortcode attributes

at the moment I'm developing a plugin, which hooks up to the content editor. My callback receives the post content after editing and calls do_shortcode(), but there is a problem and i don't know how to fix it.
add_filter('wp_insert_post_data', 'prepareContentSaving', 99, 2);
add_filter('wp_update_post_data', 'prepareContentSaving', 99, 2);
For instance if my post looks like (which obviously looks like valid shortcode syntax):
[foo bar="two words"]
my callback receives:
[foo bar=\"two words\"]
Looks right, right? But now whenever the shortcode is parsed via do_shortcode() the arguments are parsed like
[tag argument1=value1 argument2]
instead of
[tag argument="Foo bar"]
which then looks something like this in PHP:
array(
[0]=> string "bar=\"two"
[1]=> string "words\""
)
So how can I prevent the quotes inside the shortcode from being escaped? Is there something wrong with the post data hook? Changing the priority from 99 to 0 doesn't change something either. Am I using the right filter?
You can try to modify your code like this:
$post = array_map('stripslashes_deep', $_POST);
More info link: http://codex.wordpress.org/Function_Reference/stripslashes_deep
WordPress actually doesn't feature any option for preventing shortcodes to be escaped. The only way is to undo it is to convert all '\"' back to '"' (same for single quotes) inside the function 'prepareContentSaving':
add_filter('wp_insert_post_data', 'prepareContentSaving', 99, 2);
add_filter('wp_update_post_data', 'prepareContentSaving', 99, 2);
function prepareContentSaving($data, $post) {
$content = $post['post_content'];
$content = correctShortcodeSlashes($content);
... any further processing ...
$data['post_content'] = $content;
return $data;
}
After saving a post wordpress not only escapes quotes but also escapes backslashes. So '"' becomes '\"' and '\"' (if the editor wants to escape a quote) becomes '\\"'.
The first given PCRE converts all single escaped quotes inside shortcode brackets back to normal quotes, the second one converts all the double escaped ones inside brackets. This way the content stays the same which reduces the chances of code injection.
PHP Manual on preg_replace
function correct_shortcode_slashes($text) {
$attribute_escaped_slashes_pattern = '/(\[)((.|\s)*?)([^\\\\])\\\\("|\')(.*?)(\])/';
$attribute_escaped_slashes_replacement = '$1$2$4"$6$7';
$attribute_double_slashes_pattern = '/(\[)((.|\s)*?)\\\\+("|\')(.*?)(\])/';
$attribute_double_slashes_replacement = '$1$2"$5$6';
$result = $text;
$counter = 0;
while(true) {
$result = preg_replace($attribute_escaped_slashes_pattern, $attribute_escaped_slashes_replacement, $result, -1, $counter);
if($counter === 0) {
break;
}
}
while(true) {
$result = preg_replace($attribute_double_slashes_pattern, $attribute_double_slashes_replacement, $result, -1, $counter);
if($counter === 0) {
break;
}
}
return $result;
}
Please feel free to enhance this answer.

preg_replace to get part of url

i currenting use ereg_replace
$myurl="http://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
$theid= ereg_replace("[^0-9]", "", $myurl);
To get an id at the end of the url entered
Id like to use preg_replace and accomplish getting two types of ids
one looking like http:// site.com?id=64
like I do above
but I want to also get one that would contain http:// site.com?pick=7878787
I am trying to assign pick id's to one variable called $thepickupcode
and the other to a variable called $theid
Any help I am happy about.
A non regex way:
$res = parse_url($myurl);
parse_str($res['query'], $query);
$theid = $query['id'];
$thepickupcode = $query['pick'];
A regex way:
if (preg_match('~(?<=[?&])(id|pick)=([0-9]++)(?=&|$)~', $_SERVER['REQUEST_URI'], $match))
if ($match[1] == 'id') $theid = $match[2];
else $thepickupcode = $match[2];
Your question is not clear, but I think you want something like:
Then you want preg_match, not replace.
preg_match( '~(pick|id)=(.+)~i', $myurl, $res );
print_r( $res );
I am not sure what all the possibilities are, but this may be more appropriate:
preg_match( '~\?(pick|id)=([0-9]+)$~i', $myurl, $res );
print_r( $res );
When you want to change ereg... to preg..., you have to enclose the regex in delimiters.
For your example:
$theid= ereg_replace("[^0-9]", "", $myurl);
becomes:
$theid = preg_replace("/[^0-9]/", "", $myurl);
or
$theid = preg_replace("/\D+/", "", $myurl);
But this will give you wrong result if there are other numbers than the id.
I suggest you follow #CasimiretHippolyte 's answer.

Log4perl category as log file name

I'm sure I'm being dim and missing the obvious but is there a simple way of using the current category as the filename in a config file without resorting to a subroutine call?
So that in the following one could use ${category}.log instead of repeating bin.nh.tpp in the filename line
log4perl.logger.**bin.nh.tpp**=INFO, BIN_NH_TPP_LOGFILE
log4perl.appender.BIN_NH_TPP_LOGFILE=Log::Log4perl::Appender::File
log4perl.appender.BIN_NH_TPP_LOGFILE.filename=${LOGS}/nh/**bin.nh.tpp**.log
log4perl.appender.BIN_NH_TPP_LOGFILE.mode=append
log4perl.appender.BIN_NH_TPP_LOGFILE.layout=PatternLayout
log4perl.appender.BIN_NH_TPP_LOGFILE.layout.ConversionPattern=[%d] %F{1} %L %c - %m%n
It's somewhat more involved than a subroutine, I'm afraid. Subroutines in l4p conf files allow for including variables known at conf file parsing time, e.g. the time/date or a user id. You can't modify log time behavior that way.
The easiest way I can think of right now to accomplish what you want is a custom appender like
package FileByCategoryAppender;
use warnings;
use strict;
use base qw( Log::Log4perl::Appender::File );
sub new {
my( $class, %options ) = #_;
$options{filename } = "no-category.log";
my $self = $class->SUPER::new( %options );
bless $self, $class;
}
sub log {
my( $self, %params ) = #_;
my $category = $params{ log4p_category };
$self->SUPER::file_switch( $category . ".log" );
$self->SUPER::log( %params );
}
1;
and then use it in your script like
use strict;
use warnings;
use Log::Log4perl qw( get_logger );
my $conf = q(
log4perl.category = WARN, Logfile
log4perl.appender.Logfile = FileByCategoryAppender
log4perl.appender.Logfile.create_at_logtime = 1
log4perl.appender.Logfile.layout = \
Log::Log4perl::Layout::PatternLayout
log4perl.appender.Logfile.layout.ConversionPattern = %d %F{1} %L> %m %n
);
Log::Log4perl::init(\$conf);
my $logger = get_logger("Bar::Twix");
$logger->error("twix error");
$logger = get_logger("Bar::Mars");
$logger->error("mars error");
which will result in two log files being created at log time:
# Bar.Mars.log
2012/11/18 11:12:12 t 21> mars error
and
# Bar.Twix.log
2012/11/18 11:12:12 t 21> twix error

Rename files during upload within Wordpress

I am trying to rename upload filenames match the Post Title.
This other thread shows how to rename to hash:
Rename files during upload within Wordpress backend
Using this code:
function make_filename_hash($filename) {
$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$name = basename($filename, $ext);
return md5($name) . $ext;
}
add_filter('sanitize_file_name', 'make_filename_hash', 10);
Does anyone know the code to rename the file to match Post Title.extension?
barakadam's answer is almost correct, just a little correction based on the comment I left below his answer.
function new_filename($filename, $filename_raw) {
global $post;
$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
$new = $post->post_title . $ext;
// the if is to make sure the script goes into an indefinate loop
if( $new != $filename_raw ) {
$new = sanitize_file_name( $new );
}
return $new;
}
add_filter('sanitize_file_name', 'new_filename', 10, 2);
Explanation of code:
Lets assume you upload a file with the original filename called picture one.jpg to a post called "My Holiday in Paris/London".
When you upload a file, WordPress removes special characters from the original filename using the sanitize_file_name() function.
Right at the bottom of the function is where the filter is.
// line 854 of wp-includes/formatting.php
return apply_filters('sanitize_file_name', $filename, $filename_raw);
At this point, $filename would be picture-one.jpg. Because we used add_filter(), our new_filename() function will be called with $filename as picture-one.jpg and $filename_raw as picture one.jpg.
Our new_filename() function then replaces the filename with the post title with the original extension appended. If we stop here, the new filename $new would end up being My Holiday in Paris/London.jpg which all of us know is an invalid filename.
Here is when we call the sanitize_file_name function again. Note the conditional statement there. Since $new != $filename_raw at this point, it tries to sanitize the filename again.
sanitize_file_name() will be called and at the end of the function, $filename would be My-Holiday-in-Paris-London.jpg while $filename_raw would still be My Holiday in Paris/London.jpg. Because of the apply_filters(), our new_filename() function runs again. But this time, because $new == $filename_raw, thats where it ends.
And My-Holiday-in-Paris-London.jpg is finally returned.
Something like this? (considering $post is your post variable, make it global):
function new_filename($filename) {
global $post;
$info = pathinfo($filename);
$ext = empty($info['extension']) ? '' : '.' . $info['extension'];
return $post->post_title . $ext;
}
add_filter('sanitize_file_name', 'new_filename', 10);
Did I understand you?

Resources