HTML from database replace with bbcode - html

I found some script on internet to from bbcode like [b] replace
function bbcodehtml($bbtext){
$bbtags = array(
'[b]' => '<strong>','[/b]' => '</strong>',
'[i]' => '<em>','[/i]' => '</em>',
'[u]' => '<span style="text-decoration: underline;">', '[/u]' => '</span>',
'[code]' => '<blockquote>','[/code]' => '</blockquote>'
);
$bbtext = str_ireplace(array_keys($bbtags), array_values($bbtags), $bbtext);
$bbextended = array(
"/\[url](.*?)\[\/url]/i" => "<a target=\"_blank\" href=\"http://$1\" title=\"$1\">$1</a>",
"/\[url=(.*?)\](.*?)\[\/url\]/i" => "<a target=\"_blank\" href=\"$1\" title=\"$1\">$2</a>",
"/\[img\]([^[]*)\[\/img\]/i" => "<img style=\"max-width:700px;\" src=\"$1\" alt=\" \" />",
"/\[image\]([^[]*)\[\/image\]/i" => "<img style=\"max-width:700px;\" src=\"$1\" alt=\" \" />"
);
foreach($bbextended as $match=>$replacement){
$bbtext = preg_replace($match, $replacement, $bbtext);
}
return $bbtext;
}
But now i have from when i want to display content in textarea i cant replace HTML for BBcodes..
I tried to change values for preg_replace and i got errors..

Hmm...
1) Your code have many security problems. One for example. User can write text for XSS attack like this:
[img]" onmouseover="alert(document.cookie);[/img]
2) You must have TWO fields on DB, where first field contains source (unreplaced bb-codes) text, and second field contains replaced (readonly html) text. For this case you can run replace process (bb to html) only for create or edit text action.

Related

Replace variables extracted from database with theirs values

I'd like to translate a perl web site in several languages. I search for and tried many ideas, but I think the best one is to save all translations inside the mySQL database. But I get a problem...
When a sentence extracted from the database contains a variable (scalar), it prints on the web page as a scalar:
You have $number new messages.
Is there a proper way to reassign $number its actual value ?
Thank you for your help !
You can use printf format strings in your database and pass in values to that.
printf allows you to specify the position of the argument so only have know what position with the list of parameters "$number" is.
For example
#!/usr/bin/perl
use strict;
my $Details = {
'Name' => 'Mr Satch',
'Age' => '31',
'LocationEn' => 'England',
'LocationFr' => 'Angleterre',
'NewMessages' => 20,
'OldMessages' => 120,
};
my $English = q(
Hello %1$s, I see you are %2$s years old and from %3$s
How are you today?
You have %5$i new messages and %6$i old messages
Have a nice day
);
my $French = q{
Bonjour %1$s, je vous vois d'%4$s et âgés de %2$s ans.
Comment allez-vous aujourd'hui?
Vous avez %5$i nouveaux messages et %6$i anciens messages.
Bonne journée.
};
printf($English, #$Details{qw/Name Age LocationEn LocationFr NewMessages OldMessages/});
printf($French, #$Details{qw/Name Age LocationEn LocationFr NewMessages OldMessages/});
This would be a nightmare to maintain so an alternative might be to include an argument list in the database:
#!/usr/bin/perl
use strict;
sub fetch_row {
return {
'Format' => 'You have %i new messages and %i old messages' . "\n",
'Arguments' => 'NewMessages OldMessages',
}
}
sub PrintMessage {
my ($info,$row) = #_;
printf($row->{'Format'},#$info{split(/ +/,$row->{'Arguments'})});
}
my $Details = {
'Name' => 'Mr Satch',
'Age' => '31',
'LocationEn' => 'England',
'LocationFr' => 'Angleterre',
'NewMessages' => 20,
'OldMessages' => 120,
};
my $row = fetch_row();
PrintMessage($Details,$row)

CakePHP find query using %like% with spaces

I'm trying to query a page based on either a category ID or sub category name.
The variable $cat will either have an integer or varchar grabbed from my database.
I've been using cakephp 1.3 with a sql find all articles with a category of $cat OR sub-category LIKE $cat
It works great but a problem arises when $cat has a space between words, "google forms".
I've looked through this site and tried a number of methods with no luck. Appreciate any advice.
Here's my controller routines:
$cat = Sanitize::escape($cat);
$cat = trim($cat);
$title_a = str_replace($cat, "%".$cat."%", $cat);
$a_t = str_replace('"', $title_a, $title_a);
//var_dump($cat);
if(!empty($cat))
{
$sqlConditions = array('OR'=>array('Article.categories LIKE' => $a_t, 'Article.event_category_id' => $cat));
$sqlParams = array('conditions'=>$sqlConditions);
$catdata=$this->Article->find('all',$sqlParams);
return $catdata;
}
I've tried many different alternatives:
RLIKE instead of LIKE
Different query using MATCH
$sqlConditions = array(
'OR' => array(
'MATCH(Article.categories AGAINST(? IN BOOLEAN MODE)' => $cat,
'MATCH(Article.event_category_id) AGAINST(? IN BOOLEAN MODE)' => $cat
)
);
$sqlConditions = array('OR'=>array('Article.categories LIKE' => "%".$cat."%", 'Article.event_category_id' => $cat));
I think a decent solution would be to remove all of the spaces and make the characters of $cat lower case.
$likeCat = strtolower(str_replace(' ', '', trim($cat)));
$sqlConditions = array(
'OR'=> array(
'LOWER(REPLACE(Article.categories, ' ', ''))' => $likeCat,
'Article.event_category_id' => $cat
)
);

knpsnappybundle create issue at page break

i am using simfony2.x and i want to use knpsnappybundle budle to create pdf file from html. so i had use this configuration,
knp_snappy:
pdf:
enabled: true
binary: "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe\""
options: []
image:
enabled: true
binary: "\"C:\\Program Files\\wkhtmltopdf\\bin\\wkhtmltoimage.exe\""
options: []
then after in my contoller i am using this service like
$html = $this->renderView('MyBundle:MyController:view.html.twig', array(
'project' => $project,
'answers' => $answers
));
return new Response(
$this->get('knp_snappy.pdf')->getOutputFromHtml($html), 200, array(
'Content-Type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="test.pdf"',
'encoding' => 'UTF-8',
)
);
it create pdf file for me but pdf contain wrong design at the time of page break, this is the screen cap of my pdf
and here is my actual Html (from which, this pdf is generated)
so can anyone help me to avoid this issue of html page break ?
thanks

Codeigniter insert horizontal lines in form_dropdown()

I need to add horizontal lines in my drop down list. I have researched and found this way:
<select>
<option>First</option>
<option disabled>──────────</option>
<option>Second</option>
<option>Third</option>
</select>
The question is that I use Codeigniter form_dropdown() and cannot insert lines in my code. Could you please help me to insert horizontal lines in the code below.
$options = array(
'' => 'Select Size',
'' => '-----------', //does not work
'small' => 'Small Shirt',
'med' => 'Medium Shirt',
'' => '-----------', // does not work
'large' => 'Large Shirt',
'xlarge' => 'Extra Large Shirt',
);
echo form_dropdown('shirts', $options, 'set_value('shirts')');
Check your syntax. I think you are mixing single and double quotes there when you are ehco-ing out the actual form element. Also, your last item in your options array does not need the trailing ,
Otherwise, your code looks "good".
php
$options = array(
'' => 'Select Size',
'-----------',
'small' => 'Small',
'medium' => 'Medium',
'-----------',
'large' => 'Large',
'xlarge' => 'Extra Large'
);
echo form_dropdown('shirts', $options, $this->input->post('shirts'));
EDIT
To create your dropdown to use opt groups: "If the array passed as $options is a multidimensional array, form_dropdown() will produce an with the array key as the label."
$options = array(
'' => 'Select Size',
'Children' => array(
'small' => 'Small',
'medium' => 'Medium'
),
'Adults' => array(
'large' => 'Large',
'xlarge' => 'Extra Large'
)
);
echo form_dropdown( 'shirts', $options, $this->input->post( 'shirts'));
What I have found though, is that your optgroup label(s) need to be unique. "Children"/"Adults" otherwise it will only render the last group. So, you could run into a case where you need to have your data be 'child large' instead of just 'large'.
If you want to use disabled options while using form_dropdown, you might have to extend the form helper library and build your own. Otherwise, you could just use plain old' HTML syntax. Then you could just add the disabled="disabled" right on the option(s).
Hope this helps...

How do I get JSON output in PERL without \n but in a readable format?

I need to get a readable JSON object from a PERL script but it's not in a readable format.
This is the code that produces the JSON.
while (my ($orderID, $possessorName, $itemDescription, $customerPickUpTime, $customerDropOffTime, $paymentAmount, $originAddress1, $originAddress2, $originNeighborhood, $originZipCode, $destinationAddress1, $destinationAddress2, $destinationNeighborhood, $destinationZipCode) = $sth->fetchrow_array)
{
%data = (orderID => $orderID, possessorName => $possessorName, itemDescription => $itemDescription, customerPickUpTime => $customerPickUpTime, customerDropOffTime => $customerDropOffTime, paymentAmount => $paymentAmount, originAddress1 => $originAddress1, originAddress2 => $originAddress2, originNeighborhood => $originNeighborhood, originZipCode => $originZipCode, destinationAddress1 => $destinationAddress1, destinationAddress2 => $destinationAddress2, destinationNeighborhood => $destinationNeighborhood, destinationZipCode => $destinationZipCode);
$json_obj = JSON->new->allow_nonref;
my $json_text = $json_obj->pretty->encode(\%data);
$query_results{"job$index"} = {"data" => $json_text};
$index++;
}
return $json_obj->pretty->encode(\%query_results, {ascii => 1, pretty => 1});
Everything works except when I look into the file (here's the printing line):
open (resultsFile, ">", "json_file.txt") || die "This doesn't work.";
print resultsFile "Results: \n\n $results";
The results are as follows:
{
"job3" : {
"data" : "{\n \"originAddress1\" : \"101 East 105th Street\",\n \"destinationZipCode\" : \"10128\",\n \"destinationNeighborhood\" : \"Upper East Side\",\n \"customerDropOffTime\" : \"2013-01-22 23:41:37\",\n \"originAddress2\" : \"\",\n \"paymentAmount\" : \"19.00\",\n \"customerPickUpTime\" : \"2013-01-22 22:56:37\",\n \"itemDescription\" : \"body\",\n \"destinationAddress1\" : \"180 East 93rd Street\",\n \"destinationAddress2\" : \"\",\n \"possessorName\" : \"Lisa Howard\",\n \"originZipCode\" : \"10029\",\n \"originNeighborhood\" : \"East Harlem\",\n \"orderID\" : \"723\"\n}\n"
},
The JSON object is formed correctly but the \n is the problem. It's not outputting with an actual newline. That's the issue.
This is because, $json_text is a string and not a hash. If you want to encode the whole thing as JSON, you must create an appropriate data structure
$query_results{"job$index"} = {"data" => \%data};
and give that as a whole to encode.