Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update index.php - json option #132

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

ruudboon
Copy link

@ruudboon ruudboon commented Jan 8, 2020

Would it possible to generate a simple json reply that holds the port information? I've added an example it this pull for this.
Would be useful for detecting latest version etc.

Would it possible to generate a simple json reply that holds the port information? I've added an example it this pull for this. 
Would be useful for detecting latest version etc.
Copy link
Contributor

@dlangille dlangille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still reviewing.

Copy link
Contributor

@dlangille dlangille left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this idea.

I am not sure of the potential for abuse, but I'm inclined to this we might want this in a API with logins etc.

header('Content-Type: application/json');
if (!empty($result)) {
$response = get_object_vars($myPort)
echo json_encode($response);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line fails to get json back. If I dump $response, it looks like this:

array(73) {
  ["dbh"]=>
  resource(2) of type (pgsql link)
  ["id"]=>
  string(5) "20643"
  ["element_id"]=>
  string(6) "272629"
  ["category_id"]=>
  string(1) "8"
  ["short_description"]=>
  string(48) "Open source Remote Desktop Protocol (RDP) server"
  ["long_description"]=>
  string(481) "Based on the work of rdesktop, xrdp uses the remote desktop protocol to
present a GUI to the user.

The goal of this project is to provide a fully functional Linux terminal
server, capable of accepting connections from rdesktop and Microsoft's own
terminal server / remote desktop clients.

Unlike Windows NT/2000/2003 server, xrdp will not display a Windows desktop
but an X window desktop to the user.

Xrdp uses Xvnc or X11rdp to manage the X session.

WWW: http://www.xrdp.org/"
  ["version"]=>
  string(6) "0.9.12"
  ["revision"]=>
  string(1) "0"
  ["epoch"]=>
  string(1) "1"
  ["maintainer"]=>
  string(16) "[email protected]"
  ["homepage"]=>
  string(20) "http://www.xrdp.org/"
  ["master_sites"]=>
  string(68) "https://codeload.github.com/neutrinolabs/xrdp/tar.gz/v0.9.12?dummy=/"
  ["extract_suffix"]=>
  NULL
  ["package_exists"]=>
  NULL
  ["depends_build"]=>
  string(445) "/usr/local/bin/nasm:devel/nasm pkgconf>=1.3.0_1:devel/pkgconf autoconf>=2.69:devel/autoconf automake>=1.16.1:devel/automake libtoolize:devel/libtool /usr/local/libdata/pkgconfig/ice.pc:x11/libICE /usr/local/libdata/pkgconfig/pixman-1.pc:x11/pixman /usr/local/libdata/pkgconfig/sm.pc:x11/libSM /usr/local/libdata/pkgconfig/x11.pc:x11/libX11 /usr/local/libdata/pkgconfig/xfixes.pc:x11/libXfixes /usr/local/libdata/pkgconfig/xrandr.pc:x11/libXrandr"
  ["depends_run"]=>
  string(459) "xterm:x11/xterm /usr/local/lib/xorg/modules/libxorgxrdp.so:x11-drivers/xorgxrdp /usr/local/lib/pulse-13.0/modules/module-xrdp-sink.so:audio/pulseaudio-module-xrdp /usr/local/libdata/pkgconfig/ice.pc:x11/libICE /usr/local/libdata/pkgconfig/pixman-1.pc:x11/pixman /usr/local/libdata/pkgconfig/sm.pc:x11/libSM /usr/local/libdata/pkgconfig/x11.pc:x11/libX11 /usr/local/libdata/pkgconfig/xfixes.pc:x11/libXfixes /usr/local/libdata/pkgconfig/xrandr.pc:x11/libXrandr"
  ["depends_lib"]=>
  string(80) "libfdk-aac.so:audio/fdk-aac libopus.so:audio/opus libjpeg.so:graphics/jpeg-turbo"
  ["last_commit_id"]=>
  string(6) "781307"
  ["found_in_index"]=>
  NULL
  ["forbidden"]=>
  NULL
  ["broken"]=>
  NULL
  ["deprecated"]=>
  NULL
  ["ignore"]=>
  NULL
  ["date_added"]=>
  string(20) "04 May 2007 01:13:19"
  ["categories"]=>
  string(3) "net"
  ["master_port"]=>
  NULL
  ["latest_link"]=>
  NULL
  ["no_latest_link"]=>
  NULL
  ["no_package"]=>
  NULL
  ["package_name"]=>
  string(4) "xrdp"
  ["restricted"]=>
  NULL
  ["no_cdrom"]=>
  NULL
  ["expiration_date"]=>
  NULL
  ["is_interactive"]=>
  NULL
  ["only_for_archs"]=>
  NULL
  ["not_for_archs"]=>
  NULL
  ["status"]=>
  string(1) "A"
  ["showconfig"]=>
  string(484) "===> The following configuration options are available for xrdp-0.9.12,1:
     DEBUG=off: Build with debugging support
     FUSE=off: Enable drive redirection via FUSE (experimental)
     IPV6=on: IPv6 protocol support
====> Options available for the group AUDIO_CODEC
     FDKAAC=on: Enable Fraunhofer FDK AAC for audio redirection
     MP3LAME=off: Enable MP3 Lame for audio redirection
     OPUS=on: Enable Opus for audio redirection
===> Use 'make config' to modify these settings"
  ["license"]=>
  string(8) "APACHE20"
  ["fetch_depends"]=>
  NULL
  ["extract_depends"]=>
  NULL
  ["patch_depends"]=>
  NULL
  ["uses"]=>
  string(72) "autoreconf:build compiler:c11 jpeg localbase libtool pkgconfig  ssl xorg"
  ["pkgmessage"]=>
  NULL
  ["distinfo"]=>
  string(570) "TIMESTAMP = 1577722873
SHA256 (xrdp/neutrinolabs-xrdp-v0.9.12_GH0.tar.gz) = 74ca5ff341a4428dba2ab409752adda9cfb788c298d8e0388dd6a4221e70ebaa
SIZE (xrdp/neutrinolabs-xrdp-v0.9.12_GH0.tar.gz) = 679189
SHA256 (xrdp/neutrinolabs-librfxcodec-v0.1.5_GH0.tar.gz) = 5710a65a9f877aca5b5bca9f2fb83b5904b59d465ab5c10bcf4ffc2b3416d0c8
SIZE (xrdp/neutrinolabs-librfxcodec-v0.1.5_GH0.tar.gz) = 48387
SHA256 (xrdp/neutrinolabs-libpainter-v0.1.1_GH0.tar.gz) = 6caee8444fbd72677ac5e125c7ac99490688474975451668e751a9d9187ee5e6
SIZE (xrdp/neutrinolabs-libpainter-v0.1.1_GH0.tar.gz) = 13929"
  ["license_restricted"]=>
  NULL
  ["manual_package_build"]=>
  NULL
  ["license_perms"]=>
  NULL
  ["conflicts"]=>
  string(16) "xrdp-devel[0-9]*"
  ["conflicts_build"]=>
  NULL
  ["conflicts_install"]=>
  NULL
  ["conflicts_matches"]=>
  bool(false)
  ["generate_plist"]=>
  NULL
  ["category"]=>
  string(3) "net"
  ["port"]=>
  string(4) "xrdp"
  ["needs_refresh"]=>
  NULL
  ["updated"]=>
  NULL
  ["onwatchlist"]=>
  string(1) "0"
  ["vulnerable_current"]=>
  NULL
  ["vulnerable_past"]=>
  NULL
  ["pkg_plist_library_matches"]=>
  NULL
  ["update_description"]=>
  NULL
  ["message_id"]=>
  NULL
  ["encoding_losses"]=>
  NULL
  ["last_commit_date"]=>
  string(22) "2020-01-08 19:36:47+00"
  ["svn_revision"]=>
  string(6) "522457"
  ["LocalResult"]=>
  NULL
  ["committer"]=>
  NULL
  ["svn_hostname"]=>
  string(18) "svnweb.freebsd.org"
  ["path_to_repo"]=>
  string(6) "/ports"
  ["element_pathname"]=>
  string(20) "/ports/head/net/xrdp"
  ["quarterly_revision"]=>
  string(8) "0.9.12,1"
  ["category_looking_at"]=>
  NULL
}

$result = $myPort->Fetch($category, $port);
header('Content-Type: application/json');
if (!empty($result)) {
$response = get_object_vars($myPort)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a trailing ; here

@ruudboon
Copy link
Author

ruudboon commented Jan 8, 2020

@dlangille I was planning to open an issue to ask for this but it made more sense to write it in code to show it. It could use some more love.

Regarding the json_encode.
Maybe the json extension is missing from you php install?

For the abuse part. What if we cache the response and only refresh after x hours? Would be nice if it can be open to hook it up with a shield service like shield.io.

We can also close this pull and rework the idea a bit. Maybe run it on a subdomain so we can put something like cloudflare in front? Let me know what you think. We can also take this offline and have chat or skype session if you like.

@dlangille
Copy link
Contributor

We have this installed:

$ pkg info -x json
json-c-0.13.1_1
php72-json-7.2.24

@ruudboon
Copy link
Author

ruudboon commented Jan 8, 2020

It’s loaded in the php.ini that php-fpm or apache is using as well?

@ruudboon
Copy link
Author

ruudboon commented Jan 8, 2020

Is there a db schema somewhere so I can setup some kind of dev tomorrow?

@dlangille
Copy link
Contributor

Good question. If this was Apache, I'd know what to do. This is nginx with php-fpm and there are no modules loaded by php.ini at all.

@dlangille
Copy link
Contributor

It's there. It's active.

$ php ~/tmp/test.php
{"a":1,"b":2,"c":3,"d":4,"e":5}
$ cat ~/tmp/test.php
<?php

$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
$ 

@ruudboon
Copy link
Author

ruudboon commented Jan 8, 2020

If you can upload a temp (secret filename) file with a phpinfo(); in there it should show you where you can find it.

@dlangille
Copy link
Contributor

Oh!

$ cat  /usr/local/etc/php/ext-20-json.ini
extension=json.so

@ruudboon
Copy link
Author

ruudboon commented Jan 8, 2020

Cmd line it can use a different config then fpm but will check my code tomorrow. Really need some sleep now ;)

@dlangille
Copy link
Contributor

The error is: Type is not supported

@dlangille
Copy link
Contributor

Yet echo json_last_error_msg(); indicates array.

@dlangille
Copy link
Contributor

This fixed it:

unset($response["dbh"]);

@dlangille
Copy link
Contributor

Try it at https://dev.freshports.org/ but please don't post any URLS here.

@dlangille
Copy link
Contributor

This is what I have now:

    case SCRIPT_JSON:
        require_once($_SERVER['DOCUMENT_ROOT'] . '/../classes/ports.php');

        $category_port = pg_escape_string($url_parts['port']);

        syslog(LOG_NOTICE, 'category_port=' . $category_port);
        
        list($category, $port) = explode('/', $category_port);
        syslog(LOG_NOTICE, 'category=' . $category);
        syslog(LOG_NOTICE, 'port=' . $port);
        
        $myPort = new Port($db);
        $result = $myPort->Fetch($category, $port);
        header('Content-Type: application/json');               
        if (!empty($result)) {
            syslog(LOG_NOTICE, 'we got results');
            $response = get_object_vars($myPort);
            unset($response["dbh"]);
#           var_dump($response);
            try {
               echo json_encode($response);
            } catch(JsonException $e ) {
#           echo json_last_error_msg();
            }
#           echo gettype($response);    
#           echo json_last_error_msg();
            exit;
        } else {
            syslog(LOG_NOTICE, 'we got errors');
            $error = ["error" => "Port not found"];
            echo json_encode($error);
            exit;
        }
        break;

@dlangille
Copy link
Contributor

Would be nice if it can be open to hook it up with a shield service like shield.io.

That reminds of of this older issue which died. It's about badges

#3

@dlangille
Copy link
Contributor

Is there a db schema somewhere so I can setup some kind of dev tomorrow?

No, but I should create one, that is a good idea. I may be able to repurpose an existing one which is getting much use just now. Please send me a public ssh key and preferred login name.

@ruudboon
Copy link
Author

ruudboon commented Jan 9, 2020

Would be nice if it can be open to hook it up with a shield service like shield.io.

That reminds of of this older issue which died. It's about badges

#3
If we have some sort of json I will implement a shield at shield.io. It will do some caching etc

@ruudboon
Copy link
Author

ruudboon commented Jan 9, 2020

Is there a db schema somewhere so I can setup some kind of dev tomorrow?

No, but I should create one, that is a good idea. I may be able to repurpose an existing one which is getting much use just now. Please send me a public ssh key and preferred login name.

It's in your mail. Preferred login name: ruud

@dlangille
Copy link
Contributor

Did you get logged in?

@dlangille dlangille changed the title Update index.php Update index.php - json option Feb 1, 2020
@dlangille
Copy link
Contributor

@ruudboon How's it going? What's up?

dlangille added a commit that referenced this pull request Apr 11, 2020
I'm hoping to hear back from him about #132 soon
@dlangille
Copy link
Contributor

@ruudboon What's up? Anything I can help with?

@dlangille
Copy link
Contributor

Still happy to proceed on this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants