How to access a String function with PHP? - php

I am using the PHP CSS preprocessor CssCrush for the first time and I am needing to implement the csscrush_string() function into my custom assets manager. Previously I was using CssMin but now I need use of the preprocessor.
The following snippet below obviously has a syntax error at $data = csscrush_string( string $string [, array $options ] ); and I do not know how to correct it.
I have also tried: $data = Crush::csscrush_string(); but I get PHP Fatal error: Class 'Crush' not found... even-though Crush is a class.
Using CssCrush (replacing CssMin):
<?php
private function process_resource($file, $type)
{
//...Files are combined and dumped into $data up here
// before running the preprocessor and minifier...
$data = str_replace($keys, $vals, $data);
$css_lib = VENDOR_DIR . "css-crush/css-crush/CssCrush.php";
if ($this->settings[$type]["minify"])
{
require $css_lib;
$data = csscrush_string( string $string [, array $options ] );
$data = trim($data);
}
}
Using CssMin (works):
<?php
$data = str_replace($keys, $vals, $data);
css_lib = VENDOR_DIR . "natxet/CssMin/src/CssMin.php";
if ($this->settings[$type]["minify"])
{
require $css_lib;
$data = CssMin::minify($data);
$data = trim($data);
}

It just guess because I don't know what $data contains but it seems to be string
change
$data = csscrush_string( string $string [, array $options ] );
$data = trim($data);
to
$data = csscrush_string(trim($data));
I've checked source of this function and there is only one option to be passed called 'boilerplate' if you want to pass it then you should invoke function as below
$data = csscrush_string(trim($data), array('boilerplate' => true));
Also consider that this function is within namespace so you should put
use \CssCrush\Crush\csscrush_string; or class function like
$data = \CssCrush\Crush\csscrush_string(trim($data));

Related

json_decode() - What am I doing wrong?

So I decided to make my own helper in Codeigniter to get JSON files and save PokeAPI calls as JSON.
The save JSON method I created works fine:
if ( ! function_exists('saveJson')) {
function saveJson($file, $data) {
$fp = fopen($file, 'w');
fwrite($fp, json_encode($data));
fclose($fp);
}
}
However the getJSON function works very randomly. It works for fetching certain files but others it throws this error: Message: json_decode() expects parameter 1 to be string, array given. (all json files are the same format)
getJSON function:
if ( ! function_exists('getJson')) {
function getJson($file) {
$json = file_get_contents($file);
$data = json_decode($json, true);
$pkm = json_decode($data, true);
return $pkm;
}
}
Its odd, I have to decode the JSON twice or can I cannot access the array in my views.
My model and controller for further depth on the issue:
Model function example:
function getPokemonById($id) {
$filepath = './assets/jsonsaves/pokemoncalls/'. $id. '.json';
if(file_exists($filepath)) {
$pokemonByIdData = getJson($filepath);
} else {
$url = $this->pokemonApiAddress.$id.'/';
$response = Requests::get($url);
saveJson($filepath, $response);
$pokemonByIdData = json_decode($response->body, true);
}
return $pokemonByIdData;
}
Controller function example:
public function viewPokemon($id) {
$singlePokemon['pokemon'] = $this->pokemon_model->getPokemonById($id);
$singlePokemon['species'] = $this->pokemon_model->getPokemonSpecies($id);
$data['thepokemon'] = $this->pokemon_model->getAllPokemon();
$this->load->view('template/header', $data);
$this->load->view('pokemonpage', $singlePokemon);
$this->load->view('template/footer');
}
So there is some variation in my JSON file. In one JSON file that does not work it has, at the beginning:
{"body":"{\"forms\":[{\"url\":\"https:\\\/\\\/pokeapi.co\\\/api\\\/v2\\\/pokemon-form\\\/142\\\/\",\"name\":\"aerodactyl\"}],...
However this one works:
"{\"forms\":[{\"url\":\"https:\\\/\\\/pokeapi.co\\\/api\\\/v2\\\/pokemon-form\\\/6\\\/\",\"name\":\"charizard\"}],...
I fixed the issue thanks to #ccKep.
I removed the JSON encode from my saveJSON function like so:
if ( ! function_exists('saveJson')) {
function saveJson($file, $data) {
$fp = fopen($file, 'w');
fwrite($fp, $data);
fclose($fp);
}
}
And then removed the second json_decode from my getJSON function:
if ( ! function_exists('getJson')) {
function getJson($file) {
$json = file_get_contents($file);
$data = json_decode($json, true);
return $data;
}
}
This fixed the errors I was receiving.

Symfony 2 set Request content and retrieve it

I am working in Symfony2 and I want to se the content of a Request with a JSON string and use i.e.: $request->get('name') to access the content.
JSON string:
$string = '{
"name":"Bob",
"surname":"White",
"email":"bobwhite#gmail.com",
"nationality":"",
}';
$request = new Request ($query = array(), $request = array(), $attributes = array(), $cookies = array(), $files = array(), $server = array(), $content = $string);
var_dump($request->get('name'));die;
To me the above is a valid way but the var dump gives me null... can anyone see where I m going wrong here...?
You want something like this?
use Symfony\Component\HttpFoundation\Request;
$input = '{
"name":"Bob",
"surname":"White",
"email":"bobwhite#gmail.com",
"nationality":""
}';
$data = json_decode($input, true);
$request = new Request (array(), $data);
var_dump($request->request->get('name'));
die;

XSS EXPLOIT JAVA SCRIPT

i have a problem with my website many hackers came to me and stealing members cookies and redirecting to their website . I searched a little bit and i found a script that blocks xss exploiting but i am new to php and i don't know how to use it.
I tried to use include and the name of the php file.
This script:
/*
* XSS filter
*
* This was built from numerous sources
* (thanks all, sorry I didn't track to credit you)
*
* It was tested against *most* exploits here: http://ha.ckers.org/xss.html
* WARNING: Some weren't tested!!!
* Those include the Actionscript and SSI samples, or any newer than Jan 2011
*
*
* TO-DO: compare to SymphonyCMS filter:
* https://github.com/symphonycms/xssfilter/blob/master/extension.driver.php
* (Symphony's is probably faster than my hack)
*/
function xss_clean($data)
{
// Fix &entity\n;
$data = str_replace(array('&','<','>'), array('&amp;','&lt;','&gt;'), $data);
$data = preg_replace('/(&#*\w+)[\x00-\x20]+;/u', '$1;', $data);
$data = preg_replace('/(&#x*[0-9A-F]+);*/iu', '$1;', $data);
$data = html_entity_decode($data, ENT_COMPAT, 'UTF-8');
// Remove any attribute starting with "on" or xmlns
$data = preg_replace('#(<[^>]+?[\x00-\x20"\'])(?:on|xmlns)[^>]*+>#iu', '$1>', $data);
// Remove javascript: and vbscript: protocols
$data = preg_replace('#([a-z]*)[\x00-\x20]*=[\x00-\x20]*([`\'"]*)[\x00-\x20]*j[\x00-\x20]*a[\x00-\x20]*v[\x00-\x20]*a[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2nojavascript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*v[\x00-\x20]*b[\x00-\x20]*s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:#iu', '$1=$2novbscript...', $data);
$data = preg_replace('#([a-z]*)[\x00-\x20]*=([\'"]*)[\x00-\x20]*-moz-binding[\x00-\x20]*:#u', '$1=$2nomozbinding...', $data);
// Only works in IE: <span style="width: expression(alert('Ping!'));"></span>
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?expression[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?behaviour[\x00-\x20]*\([^>]*+>#i', '$1>', $data);
$data = preg_replace('#(<[^>]+?)style[\x00-\x20]*=[\x00-\x20]*[`\'"]*.*?s[\x00-\x20]*c[\x00-\x20]*r[\x00-\x20]*i[\x00-\x20]*p[\x00-\x20]*t[\x00-\x20]*:*[^>]*+>#iu', '$1>', $data);
// Remove namespaced elements (we do not need them)
$data = preg_replace('#</*\w+:\w[^>]*+>#i', '', $data);
do
{
// Remove really unwanted tags
$old_data = $data;
$data = preg_replace('#</*(?:applet|b(?:ase|gsound|link)|embed|frame(?:set)?|i(?:frame|layer)|l(?:ayer|ink)|meta|object|s(?:cript|tyle)|title|xml)[^>]*+>#i', '', $data);
}
while ($old_data !== $data);
// we are done...
return $data;
}
How to use it ? Please explain where to put it ?
You need to include this file, then wherever you read anything from the client (or to be more precise: where you output your client's input) you need to replace $_GET['something'] with xss_clean($_GET['something']) and $_POST['sth'] with xss_clean($_POST['sth'])

how to retrieve array value using php

I want to retrieve array value.
This is my array value:
overlay.txt:
{"title":"sss","description":"sss","code":"sss"}
{"title":"trtr","description":"trtr","code":"tyrytr"}
{"title":"ret54","description":"56tr","code":"ty76"}
{"title":"rgfdg","description":"dfgdfg","code":"dfgdfg"}
{"title":"asfafdsf","description":"sdfsdf","code":"sdfsdfsdf"}
This is my code: but this is not working.why?
How to retrieve value from overlay.txt file.
I did not get all title value.
I do not known how to get title value from overlay.txt
The $title is showing empty.
Where I want to change in my code to get $title value.
$info = array();
$folder_name = $this->input->post('folder_name');
$info['title'] = $this->input->post('title');
$info['description'] = $this->input->post('description');
$info['code'] = $this->input->post('code');
$json = json_encode($info);
$file = "./videos/overlay.txt";
$fd = fopen($file, "a"); // a for append, append text to file
fwrite($fd, $json);
fclose($fd);
$filecon = file_get_contents('./videos/overlay.txt', true);
$this->load->view('includes/overlays',$filecon);
//overlays page;
foreach($filecon as $files)
{
$title=$files['title'];
echo $title;
}
You're encoding your array to JSON, so at some point you need to decode it again into a PHP array. Since you actually have several JSON objects in the file, you need to decode each one individually. Assuming it's always one JSON object per line, this'll do:
$jsonObjects = file('overlay.txt', FILE_IGNORE_NEW_LINES);
foreach ($jsonObjects as $json) {
$array = json_decode($json, true);
echo $array['title'];
...
}
This will very quickly break if there are line breaks within the serialized JSON, e.g.:
{"title":"ret54","description":"foo
bar","code":"ty76"}
That way of storing the data is not very reliable.
make overlay.txt fully json format:
[
{"title":"sss","description":"sss","code":"sss"},
{"title":"trtr","description":"trtr","code":"tyrytr"},
...
]
and try this:
$raw = file_get_contents('./videos/overlay.txt', true);
$this->load->view('includes/overlays', array("filecon" => json_decode($raw)));
overlay page:
<?php
foreach($filecon as $files) {
echo $files['title'];
}
?>
If you want to use $filecon in view file,
set an array which has the key "filecon" in $this->load->view()'s second argument.
http://codeigniter.com/user_guide/general/views.html

HTTP PUT Parameter

I create a rest-webservice with the php framework "tonic".
I have a User Class and handle it with the library.
According to CRUD i use HTTP_PUT to UPDATE the User:
function put($request) {
$response = new Response($request);
$split = explode ('&',$request);
$para = array();
foreach($split as $i) {
$names = explode('=',$i);
$para[$names[0]] = $names[1];
}
$response->body = var_dump($para);
return $response;
}
My Question is how do I access the calling parameters?
At the moment I parse it manually into an array.
PHP will not translate a classic "application/x-www-form-urlencoded" request into $_POST / $_GET if the method is PUT (and there is no $_PUT).
So if you use this content type you have to parse the query string manually:
<?php
$putdata = fopen("php://input", "r");
$para = parse_str($putdata);
http://www.php.net/manual/en/features.file-upload.put-method.php

Categories