Thread: Client-servers transfers, running scripts and sending it all back...

  1. #16
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by MutantJohn View Post
    So far, the only way I know how to do this is using onclick events for my HTML
    You can do it with pure CSS and HTML, too. (Replace the links with checkbox inputs in that example, and include the tree in your form, and you're done.)

    If you want to use Javascript, the standard technique is to generate the HTML for all input elements, just hide the ones that the user does not want/need to see right away, something like
    PHP Code:
    <div class="dir">
     <
    input type="checkbox" name="files" value="dir1" onchange="subdir('subdir1', this.checked);"dir1
    </div>
    <
    div class="subdir" id="subdir1" style="display: none;">
     <
    div class="file">
      <
    input type="checkbox" name="files" value="file1"file1
     
    </div>
    </
    div
    where the subdir1 has to be unique across all ids on the page, and the Javascript function is something like
    Code:
    function subdir(id, visible) { document.getElementById(id).style.display = (visible) ? "block" : "none"; }
    and changes the nested div element visibility whenever the controlling checkbox state changes.

    That does not work if the user reloads the page, as the display attributes reset to none, but the form contents stay unchanged. However, you can fix that by adding a body onload or onpageshow event, that checks all input elements in all forms on the page, and executes their onchange events explicitly.

    The pure CSS+HTML option is a bit sensitive to browser versions, whereas the above Javascript stuff is trivial to implement in PHP (or other scripting languages). (In particular, you can use a fixed prefix plus an increasing integer for the id attributes.)

    Using the familiar icons or suitable Unicode glyphs, it is not difficult at all to have the script produce a directory tree with quite intuitive collapse/expand/select semantics. In that case, I recommend using a dummy form and a hidden input field to list the expanded elements: that input field is retained over page reload, and you can reset the visibilities using its contents. Even with thousands of entries, this should work really snappy even on slow computers. (AFAIK, the browser memory use -- they're memory hogs! -- is what limits how large a tree you can use in a browser.)

  2. #17
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Thank you, Nominal. Very helpful.

    Okay, one more thing.

    I'm now trying to let the Apache server make php and java calls.

    My php file looks like :
    Code:
    <?php
        echo shell_exec('whoami');
        exec('java -jar /*...*/');
    I'm calling the php code from an html page. I'm using a link on the html page for the php too because that's just how I got it work. Anyway, I can get the whoami to display but the exec('java... thing doesn't seem to be working.

    When I run the php in my browser (Chrome, if that matters) it outputs http as the user which is good. Everything is owned by http and in the http group. I can call php and whoami from the http user/group but I can't call Java? Or am I able to, just I'm missing something in my actual Java command?

    This whole Linux permission thing is throwing me through a loop... Though I do feel myself becoming quite the little adminstrator.

  3. #18
    Ticked and off
    Join Date
    Oct 2011
    Location
    La-la land
    Posts
    1,728
    Quote Originally Posted by MutantJohn View Post
    I'm now trying to let the Apache server make php and java calls.
    If you look at the PHP documentation for exec(), you'll notice it returns the last line of the output.
    Why not use shell_exec() for it, too?

    If the Java code is supposed to produce a PDF file as output, try
    PHP Code:
    <?PHP
    header
    ("Content-Type: application/pdf");
    echo 
    shell_exec("env PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin java -jar ...");
    ?>
    If the Java code outputs a number of lines, with the name of the PDF file on the second line, you could use
    PHP Code:
    <!DOCTYPE html>
    <html><head><title>Processing</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"></head><body><p><?PHP
    $line 
    = @explode("\n", @shell_exec("env PATH=/sbin:/usr/sbin:/usr/local/sbin:/bin:/usr/bin:/usr/local/bin java -jar ..."));
    if (
    strlen(@$line[1]) > 0)
        echo 
    "<a href=\"path-to/"htmlentities(basename($line[1])), "\">Results (PDF)</a>";
    else
        echo 
    "Conversion failed.";
    ?></p></body></html>
    The @ is used to suppress unnecessary error messages, in case PHP is configured to emit warnings/errors.

    Quote Originally Posted by MutantJohn View Post
    This whole Linux permission thing is throwing me through a loop...
    1. By default, scripts are run as the user Apache is configured to run as
      (www-data on Ubuntu, apache on RHEL/CentOS (IIRC!), and so on)
    2. You can use SuEXEC (or other mechanisms, I use my own) to specify another user to run script as (on a per-virtualhost basis)
      SuEXEC requires the target directory to be owned by that user (and has some other requirements too)
    3. You can use the UserDir mechanism in Apache to allow scripts using /~username URLs to be run as that user
    4. The script user needs execute access to all directories along the path from root to the target script, and read access to the script file
      (Directory read access is only needed if you wish Apache to generate directory lists)
    5. You need a temporary directory for uploaded files (PHP uses /tmp by default, which should be okay)
    6. To save new files, the script user needs write access to that directory

    It's pretty straightforward, actually. I'd say the documentation is just pretty poor.

    I personally use much more stringent requirements (thus my own suexec), so that all scripts are run as a dedicated user, with minimal filesystem access, to stop script-drops et cetera. Actually, I'm hoping to push optional limiting schemes to Python 3 and PHP, and get Apache users (mainly management panel users) to eventually switch to a more robust script security model.

    Good documentation would need to be built atop a narrative, explaining how the access rules work, and why they are needed. Otherwise this stuff is too dry for anyone to remember offhand; no wonder it's hard to learn at first.

  4. #19
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Okay, so this is the HTML I came up with :
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html>
    <html xmlns="http://www.w3.org/1999/xhtml">
        <head>
            <script>
                function subdir(id, visible)
                {
                    document.getElementById(id).style.display = (visible) ? "block" : "none";
                }
            </script>
        </head>
        <body>
            <div class="dirs0">
                <ul>
                    <li>
                        <input type="checkbox" id="introduction" name="introduction" />
                        <label for="introduction">Introduction</label>
                    </li>
                    <li>
                        <input type="checkbox" id="care" name="care"
                            onchange="subdir('care-subdirs', this.checked);" />
                        <label for="care">Care</label>
                        <div class="care-subdirs" id="care-subdirs" style="display : none;">
                            <ul>
                                <li>
                                    <input type="checkbox" id="pruning" name="pruning" />
                                    <label for="pruning">Pruning</label>
                                </li>
                                <li>
                                    <input type="checkbox" id="gardenPreparation"
                                        name="gardenPreparation" />
                                    <label for="gardenPreparation">Garden Preparation</label>
                                </li>
                            </ul>
                        </div>
                    </li>
                    <li>
                        <input type="checkbox" id="index" name="index"
                            onchange="subdir('index-subdirs', this.checked);" />
                        <label for="index">Index</label>
                        <div class="index-subdirs" id="index-subdirs" style="display : none;">
                            <ul>
                                <li>
                                    <input type="checkbox" id="springFlowers" name="springFlowers"
                                        onchange="subdir('springFlowers-subdirs', this.checked);" />
                                    <label for="springFlowers">Spring Flowers</label>
                                    <div class="springFlowers-subdirs" id="springFlowers-subdirs"
                                        style="display : none;">
                                        <ul>
                                            <li>
                                                <input type="checkbox" id="iris" name="iris" />
                                                <label for="iris">Iris</label>
                                            </li>
                                            <li>
                                                <input type="checkbox" id="snowdrop" name="snowdrop" />
                                                <label for="snowdrop">Snowdrop</label>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                                <li>
                                    <input type="checkbox" id="summerFlowers" name="summerFlowers"
                                        onchange="subdir('summerFlowers-subdirs', this.checked);" />
                                    <label for="summerFlowers">Summer Flowers</label>
                                    <div class="summerFlowers-subdirs" id="summerFlowers-subdirs"
                                        style="display : none;">
                                        <ul>
                                            <li>
                                                <input type="checkbox" id="gardenia" name="gardenia" />
                                                <label for="gardenia">Gardenia</label>
                                            </li>
                                            <li>
                                                <input type="checkbox" id="lilac" name="lilac" />
                                                <label for="lilac">Lilac</label>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                                <li>
                                    <input type="checkbox" id="autumnFlowers" name="autumnFlowers"
                                        onchange="subdir('autumnFlowers-subdirs', this.checked);" />
                                    <label for="autumnFlowers">Autumn Flowers</label>
                                    <div class="autumnFlowers-subdirs" id="autumnFlowers-subdirs"
                                        style="display : none;">
                                        <ul>
                                            <li>
                                                <input type="checkbox" id="chrysanthemum"
                                                    name="chrysanthemum" />
                                                <label for="chrysanthemum">Chrysanthemum</label>
                                            </li>
                                            <li>
                                                <input type="checkbox" id="salvia" name="salvia" />
                                                <label for="salvia">Salvia</label>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                                <li>
                                    <input type="checkbox" id="winterFlowers" name="winterFlowers"
                                        onchange="subdir('winterFlowers-subdirs', this.checked);" />
                                    <label for="winterFlowers">Winter Flowers</label>
                                    <div class="winterFlowers-subdirs" id="winterFlowers-subdirs"
                                        style="display : none;">
                                        <ul>
                                            <li>
                                                <input type="checkbox" id="gerbera" name="gerbera" />
                                                <label for="gerbera">Gerbera</label>
                                            </li>
                                        </ul>
                                    </div>
                                </li>
                            </ul>
                        </div>
                    </li>
                    <li>
                        <input type="checkbox" id="copyright" name="copyright" />
                        <label for="copyright">Copyright</label>
                    </li>
                </ul>
            </div>
        </body>
    </html>
    Is there anything I can do "better" here? I'm such a noob...

    I'm also going to search out a good HTML forum as well instead of a dedicated C board.

  5. #20
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Not to dismiss the discussion taking place (I'm glad Nominal Animal is giving a hand), but one piece of advice...

    Most of us have done extensive server and client side scripting in the past. And will continue doing it in the future. Like you have learned, get into programming and eventually one will need to deal with web servers and whatnot. However, as application developers, we usually deal with it on a as-needed basis. Once the need arises, we re-study the (X)HTML/CSS we have forgotten, along with whatever scripting language(s) we decided to use at the time. And once the need is satisfied, we tend to forget all about it.

    What this means is that you should register your name in some webserver development discussion group. Asking about this stuff on an application development forum will tend to reduce the number of available people to answer your doubts and increase the likelihood of receiving less than ideal answers because most of us will be replying from memory.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #21
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    That code contains a lot of unnecessary markup.

    *shrug*

    You could change the display in advance with CSS and displaying it as you are doing, but you could employ data polymorphisms within your markup allowing you display everything by default only to quickly add the relevant markup and change state after the page has loaded.

    The has the benefit of less markup and working who browse without Javascript enabled by default.

    <?xml version="1.0" encoding="UTF-8"?>
    Please use HTML5 for new work.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  7. #22
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Quote Originally Posted by phantomotap View Post
    You could change the display in advance with CSS and displaying it as you are doing, but you could employ data polymorphisms within your markup allowing you display everything by default only to quickly add the relevant markup and change state after the page has loaded.

    The has the benefit of less markup and working who browse without Javascript enabled by default.
    Whoa, how do I do that? The employing data polymorphisms thing...

  8. #23
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    2,665
    Btw Nominal, I tried your way with the env thing but I'm not sure if it worked. I did get it to work by using putenv() though. If using env in the shell_exec is the same thing then my bad, I nubbed up pretty hard yesterday about setting the actual paths (paths http doesn't have rights to) and I got all frustrated.

    I learned that servers send their own unique set of environment variables though and that's why I was having such trouble. This web thing is really tough but it feels really good to see progress. Thank you everyone for your help in this thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. function not sending back values
    By Sal Zaydon in forum C Programming
    Replies: 2
    Last Post: 11-05-2013, 07:37 AM
  2. Replies: 0
    Last Post: 03-12-2011, 02:10 AM
  3. intercepting packets for web servers and replying back
    By liri in forum Networking/Device Communication
    Replies: 1
    Last Post: 10-21-2007, 10:43 PM
  4. 1 client, 2 servers
    By maxorator in forum Networking/Device Communication
    Replies: 3
    Last Post: 10-04-2006, 08:59 PM
  5. any good Tutorial about building Servers/Client
    By Bug in forum Windows Programming
    Replies: 1
    Last Post: 06-26-2003, 04:48 AM