HtmlPurifier and pre tag content - htmlpurifier

Does anyone know how should I configure HtmlPurifier to ignore content within <pre> tag?
Currently I have something like this:
require_once('HTMLPurifier.auto.php');
$config = HTMLPurifier_Config::createDefault();
$config->set('HTML.Doctype', 'XHTML 1.0 Transitional');
$config->set('Attr.AllowedFrameTargets', array('_blank'));
$config->set('HTML.Trusted', true);
$config->set('HTML.SafeObject', true);
$config->set('Output.FlashCompat', true);
$config->set('Filter.Custom', array( new HTMLPurifier_Filter_SafeIframe() ));
$config->set('Attr.EnableID', true);
$def = $config->getHTMLDefinition(true);
$def->addAttribute('a', 'href*', 'URI');
$def->addAttribute('a', 'rel', 'CDATA');
$objPurifier = new HTMLPurifier($config);
return $objPurifier->purify($string);

Try %Core.HiddenElements.

I solved adding
pre[class]
to the property
HTML.Allowed

Related

How to generate new page in zend PDF in foreach loop

please explain how can generate new pages in pdf for foreach loop content
$pdf = new Zend_Pdf();
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
$items = array();
foreach ($items as $item) {
$page->setFont($font, 9)
->setFillColor(Zend_Pdf_Color_Html::color('#000000'))
->drawText('Text 1', 0, 800);
}
$pdf->pages[] = $page;
$pdf->save('wishlist.pdf');
You have to use the below code to add new blank pages in your pdf file.
$page = new Zend_Pdf_Page(Zend_Pdf_Page::SIZE_A4);
and than assign to
$pdf->pages[] = $page;

CakePHP 2.5 Datasource, create and return response

I have a specific task to connect CakePHP web application to a remote restful server . I create a datasource, read method works great, but the api after save data return an array of processed data.
Looking for a way to return the data array and use in controller.
My Controller code
public function admin_generate()
{
$data = $this->request->data;
$data['path'] = 'special/generate';
$this->Tool->create();
if($this->Tool->save($data)){
// handle response ????
}
$this->set('data',$data);
$this->set('_serialize','data');
}
In datasource file
public function create(Model $model, $fields = null, $values = null)
{
$data = array_combine($fields, $values);
$api = $this->config['api_path'].$data['path'].'?auth_key='.$this->config['auth_key'];
$json = $this->Http->post($api, $data);
$response = json_decode($json, true);
if (is_null($response)) {
$error = json_last_error();
throw new CakeException($error);
}
return $response; // ??????
}
Can someone show me the correct way to use the api response data in the controller?
I found a solution, a few minutes after a post question. This can help one of you.
datasource
....
if (is_null($response)) {
$error = json_last_error();
throw new CakeException($error);
}
// SOLUTION
$model -> code = $response['code'];
$model -> key = $response['key'];
$model -> code_id = $response['code_id'];
return true;
.....
in controller
.....
if($this->Tool->save($data)){
unset($data['path']);
$data['code'] = $this->Tool->code;
$data['key'] = $this->Tool->key;
$data['code_id'] = $this->Tool->code_id;
}
.....

cakePHP model update issue

I've been runing on my issue all night long tried many method to update an entry of User model and I didn't figure it out.
I tried with saveField, updateAll and findBy then save but all i've got is new entry on my DB or no update.
Here is the code, Hope you'll help
<?php
public function email_verification($token) {
$this->User->create();
$user = $this->User->findByEmailToken($token);
$this->User->set(array('email_token' => 'valid'), $user);
$this->User->save($user, false);
}
?>
Thanks in advance
Ahum:
$this->User->set(array('email_token' => 'valid'), $user);
$this->User->save($user, false);
The save() call saves your record with your original data..... since you pass in $user!
Replace the call to $this->user->set() with:
$user['User']['email_token'] => 'valid';
Try code below:
<?php
public function email_verification($token) {
$user = $this->User->findByEmailToken($token);
$user['User']['email_token'] = 'valid';
if ($this->User->save($user)) {
....
....
} else {
debug($this->User->validationErrors);
}
}
?>

Pear::Image_Barcode output in CakePHP Controller

I want to output a barcode generated with PEAR::Image_Barcode in a CakePHP Controller:
public function gen_bc($bc_text = null)
{
$this->autoRender = false;
require_once("Image/Barcode.php");
$bc = new Image_Barcode;
$bc->draw($bc_text, "Code39", "jpg");
exit();
}
But i get no output # all.
You need to capture your output. Use ob_start(). Otherwise, you will only have the barcode image in your screen.
require_once('Image/Barcode.php');
public function gen_bc($bc_text){
ob_start();
$img = Image_Barcode::draw( $bc_text ,'code128', 'png', false);
$cif_data = ob_get_contents ();
ob_end_clean();
}
If you get an error, try to ommit the errors:
require_once('Image/Barcode.php');
error_reporting(0);
public function gen_bc($bc_text){
ob_start();
$img = Image_Barcode::draw( $bc_text ,'code128', 'png', false);
$cif_data = ob_get_contents ();
ob_end_clean();
}

XPATH - how to use multiple paths

I am using xpath to retrieve the text value of some links (a) in a given element. I then push the results to an array called $tableau. Everything is working fine :) The thing is, I would like now to retrieve also the href of those links. So have both the text value and the href in my array. The other thing is, I have no idea how to do that. Hope someone can help. Thank you in advance for your replies. Cheers. Marc
$url = 'http://www.somesite.com';
$path = '.../a';
$tableau = array();
$dom = new DOMDocument();
#$dom->loadHTML($url);
$xpath = new DomXPath($dom);
$x = $xpath->query($path);
foreach ($x as $value)
array_push($tableau, $value->nodeValue);
Try this:
foreach ($x as $value)
{
array_push($tableau, $value->nodeValue, $value->getAttribute('href'));
}
you can use an associative array
the code might look like this(not sure abt the syntax though)
arr = [ "1" => [ "text" => "value1" , "href" => "value1"],
"2" => [ "text" => "value2", "href" => "value2"]];
the xpath xpression to retrieve the href of all the anchor tags is something like this
("//a/#href");
// --> select a from all the descendants of the root
a --> select the anchor tag
#href --> select the href of this anchor (# to select attributes)
convert this expression to equivalent PHP code
Thanks to the hints given by javram and vireshad I manage to find the solution. See here below:
$url = 'http://www.somesite.com';
$path = '.../a';
$tableau = array();
$dom = new DOMDocument();
#$dom->loadHTML($url);
$xpath = new DomXPath($dom);
$x = $xpath->query($path);
foreach ($x as $value)
array_push($tableau, array($value->nodeValue, $value->getAttribute('href')));

Resources