How to show tree list using phpseclib? - php

I am using library phpseclib. To show directory listing, I use function :
$sftp->rawlist();
But it don't show the tree list as image of its example (http://phpseclib.sourceforge.net/sftp/examples.html)
How can I do to show it as image below ? Thanks.

phpseclib's documentation is in git:
https://github.com/phpseclib/docs
Looking at it... the documentation website uses http://bassistance.de/jquery-plugins/jquery-plugin-treeview/ to achieve the effect you're asking about. To convert a PHP array to HTML that'll work with that plugin...
function array2html($array)
{
$result = '';
foreach ($array as $key => $value) {
$result.= '<li><span class="name">' . $key . '</span>' . (is_array($value) ? array2html($value) : '<ul><li>' . $value . '</li></ul>') . '</li>';
}
return '<ul>' . $result . '</ul>';
}
So in PHP you'll want to do this (once the above function is defined):
echo str_replace('<ul>', '<ul class="printr">', array2html($arr), 1);
In HTML you'll then need to do this:
$(document).ready(function() {
$('.printr').treeview({ persist: "location", collapsed: true, unique: true });
}
Keep in mind it does look like the phpseclib docs did make at least one change to the tree view library (modified some CSS and added a new image):
https://github.com/phpseclib/docs/commit/3406a94489c153ddf8f4a1a33f2ecbbcdd5ec61e
Hope that helps!

Related

array_unique() in php simple html dom

I wrote the code blow to get all unique links from a url:
include_once ('simple_html_dom.php');
$html = file_get_html('http://www.example.com');
foreach($html->find('a') as $element){
$input = array($element->href = $element->href . '<br />');
print_r(array_unique($input));}
but I really can't understand why it shows the duplicated links too!
is there any problem with the function array_unique and simple html dom?
and there's another thing I guess is related to the problem: when you execute this you see all of the link that it extracted are in one key I mean this :
array(key => all values)
Is there any one who can solve this?
I believe you want it more like this:
$temp = array();
foreach($html->find('a') as $element) {
$temp[] = $element->href;
}
echo '<pre>' . print_r(array_unique($temp), true) . '</pre>';

Handling empty lists in PHP SOAP

Initially if there was one item in the list it would return an object rather than an array of one object. I fixed that using:
https://eirikhoem.wordpress.com/2008/03/13/array-problems-with-soap-and-php-updated/
$x = new SoapClient($wsdl, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));
But I'm having problems when there is no items in the list.
The best I've come up with so far is:
$occulist = $result->GetWebOccurrencesResult->OccuList;
if (!empty((array)($occulist))) {
foreach($occulist->TOccu as $occurrence) {
echo $occurrence->Prog_Name . ' running from ' . $occurrence->StartDate . ' to ' . $occurrence->EndDate . '<br/>';
}
}
Originally it was
foreach($result->GetWebOccurrencesResult->OccuList->TOccu as $occurrence) {
I don't understand exactly what you're trying to achieve here, so if you can clarify in the comments, that'd be great.
What I'm taking from your question is that you want to handle when the array is empty, but want a clean method to do so? The loop you have there would do what you require, but here is another alternative:
do {
foreach($occulist->TOccu as $occurrence) {
echo $occurrence->Prog_Name . ' running from ' . $occurrence->StartDate . ' to ' . $occurrence->EndDate . '<br/>';
}
} while(empty((array)($occulist)) !== FALSE);
So you're looping through the foreach while the $occulist array isn't empty.
You could even do:
while((array)$occulist !== FALSE) {
foreach(....) {
...
}
}
On my server with Windows and PHP 5.4.23 this:
if (!empty((array)($occulist))) {
gives the following error:
Parse error: syntax error, unexpected '(array)' (array) (T_ARRAY_CAST)
I fixed it using:
if (get_object_vars($occulist)) {
I think that is a more elegant alternative to the original...

New line does not display when getting jira issue using php soap

I am trying to get issue details from JIRA 3.13 using PHP SOAP. I was able to login and get the issues; however, on one of my field, I could not get the new line formatting. So, all I got is the text for that particular field without new line character (everything just append into a single line of text). As of now, I am guessing php also did some re-formatting of the string from SOAP. The reason I am saying this is because I did some testing with SOAP UI and was able to get the text out with the formatting. Can anyone help me out with a way to displaying the text with the formatting? Thanks in advance.
This is my php code:
try {
$soap = new SoapClient("<<JIRA URL>>");
$auth = $soap->login($formUsername, $formPassword);
if ($auth)
{
$result0 = $soap->getIssue($auth,'<<JIRA ISSUE ID>>');
$result = (array) $result0;
foreach ($result as $key => $a)
{
$z = $z . '<br/>' . $key . ' = ' . $a;
}
echo $z;
}
}
catch(Exception $e){
$string = urlencode($e->getMessage());
header("Location: login.php?message=".$string);
die();
}
I just realize that I do not need to convert it into array.
Simply do the following:
foreach ($result0 as $key => $a)
{
$z = $z . '<br/>' . $key . ' = ' . $a;
}
This, however, still does not solve my problem with the new line.
Isn't it just because you don't change the linefeeds into <br/> before outputting it?
Should be easy to find out if that's the case just by looking at the source in the browser.
You need nl2br() to convert newline characters (\n et. al) into HTML <br> tags:
foreach ($result0 as $key => $a)
{
$z = $z . '<br/>' . $key . ' = ' . nl2br($a);
}
Could be that the text is stored with Unix line endings and you're displaying on a Windows machine? Which field is having the problem?

How to trace PHP files callstack used to render a page?

For many "on the rush" web development (CMS customisation or simple helping hand to a friend beggin "please help, I can't remove this div on my wordpress page") one common problem appears : what is the code behind the page where our problem lives.
Then my question is simple : is there any tool / method that can ease the searching of the scripts implied in a given page production on a php based webapp ?
Something that can build a call tree for a given page would be great !
xdebug provides a navigable callstack and much much more. On their documentation page there are a long list of wonderful things.
you're looking for debug_backtrace()
This is a particularly helpful function taken from the CakePHP framework:
function debug($var = false, $showHtml = false, $showFrom = true) {
if ($showFrom) {
$calledFrom = debug_backtrace();
echo '<strong>' . substr(str_replace(ROOT, '', $calledFrom[0]['file']), 1) . '</strong>';
echo ' (line <strong>' . $calledFrom[0]['line'] . '</strong>)';
}
echo "\n<pre class=\"debug\">\n";
$var = print_r($var, true);
if ($showHtml) {
$var = str_replace('<', '<', str_replace('>', '>', $var));
}
echo $var . "\n</pre>\n";
}

native php function to highlight javascript?

Is there any native PHP function as highlight_string(); but for javascript ?
Or, if not, is there any PHP function (homemade) to do it?
EDIT: I want to use PHP function to COLORIZE javascript
I have had great success with GeSHi. Easy to use and integrate in your app and it supports a lot of languages.
I understand you want a Syntax Highligher written in PHP. This one (Geshi) has worked for me in the past:
http://qbnz.com/highlighter/
Yes, the PHP function highlight_string() is a native PHP function for PHP.
No.
But there are a lot of javascript libraries that do syntax-highlight on several languages,
from bash-scripting to php and javascript.
eg, like snippet (JQuery) or jQuery.Syntax (my favorite)
Over here you can find an excellent library which enables syntax highlighting in a large amount of languages using javascripts and a css class.
There is no native php function to do this, so either you have to use existing libraries or you have to write something yourself.
Fastest way - you can use also PHP function "highlight_string" with a little trick
(capture function output and remove leading/trailing PHP tags):
$source = '... some javascript ...';
// option 1 - pure JS code
$htmlJs = highlight_string('<?php '.$source.' ?>', true);
$htmlJs = str_replace(array('<?php ', ' ?>'), array('', ''), $htmlJs);
// option 2 - when mixing up with PHP code inside of JS script
$htmlJs = highlight_string('START<?php '.$source.' ?>END', true);
$htmlJs = str_replace(array('START<span style="color: #0000BB"><?php </span>', ' ?>END'), array('', ''), $htmlJs);
// check PHP INI setting for "highlight.keyword" (#0000BB) - http://www.php.net/manual/en/misc.configuration.php#ini.syntax-highlighting
No native function, but rather than using a full stack library just to highlight some javascript you can use this single function :
function format_javascript($data, $options = false, $c_string = "#DD0000", $c_comment = "#FF8000", $c_keyword = "#007700", $c_default = "#0000BB", $c_html = "#0000BB", $flush_on_closing_brace = false)
{
if (is_array($options)) { // check for alternative usage
extract($options, EXTR_OVERWRITE); // extract the variables from the array if so
} else {
$advanced_optimizations = $options; // otherwise carry on as normal
}
#ini_set('highlight.string', $c_string); // Set each colour for each part of the syntax
#ini_set('highlight.comment', $c_comment); // Suppression has to happen as some hosts deny access to ini_set and there is no way of detecting this
#ini_set('highlight.keyword', $c_keyword);
#ini_set('highlight.default', $c_default);
#ini_set('highlight.html', $c_html);
if ($advanced_optimizations) { // if the function has been allowed to perform potential (although unlikely) code-destroying or erroneous edits
$data = preg_replace('/([$a-zA-z09]+) = \((.+)\) \? ([^]*)([ ]+)?\:([ ]+)?([^=\;]*)/', 'if ($2) {' . "\n" . ' $1 = $3; }' . "\n" . 'else {' . "\n" . ' $1 = $5; ' . "\n" . '}', $data); // expand all BASIC ternary statements into full if/elses
}
$data = str_replace(array(') { ', ' }', ";", "\r\n"), array(") {\n", "\n}", ";\n", "\n"), $data); // Newlinefy all braces and change Windows linebreaks to Linux (much nicer!)
$data = preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $data); // Regex identifies all extra empty lines produced by the str_replace above. It is quicker to do it like this than deal with a more complicated regular expression above.
$data = str_replace("<?php", "<script>", highlight_string("<?php \n" . $data . "\n?>", true));
$data = explode("\n", str_replace(array("<br />"), array("\n"), $data));
# experimental tab level highlighting
$tab = 0;
$output = '';
foreach ($data as $line) {
$lineecho = $line;
if (substr_count($line, "\t") != $tab) {
$lineecho = str_replace("\t", "", trim($lineecho));
$lineecho = str_repeat("\t", $tab) . $lineecho;
}
$tab = $tab + substr_count($line, "{") - substr_count($line, "}");
if ($flush_on_closing_brace && trim($line) == "}") {
$output .= '}';
} else {
$output .= str_replace(array("{}", "[]"), array("<span style='color:" . $c_string . "!important;'>{}</span>", "<span style='color:" . $c_string . " !important;'>[]</span>"), $lineecho . "\n"); // Main JS specific thing that is not matched in the PHP parser
}
}
$output = str_replace(array('?php', '?>'), array('script type="text/javascript">', '</script>'), $output); // Add nice and friendly <script> tags around highlighted text
return '<pre id="code_highlighted">' . $output . "</pre>";
}
Usage :
echo format_javascript('console.log("Here is some highlighted JS code using a single function !");') ;
Credit :
http://css-tricks.com/highlight-code-with-php/
Demo :
http://css-tricks.com/examples/HighlightJavaScript/
Well nice info here . Here is another nice one : http://code.google.com/p/google-code-prettify/

Categories