Archive

Archive for the ‘Web Development’ Category

Creating multiple FTP accounts in cPanel

March 17, 2013 2 comments

I’m giving the Computer Applications course this term. It’s basically about Web Development basics (HTML, CSS, JavaScript,..).

I wanted to get students familiar with creating online webpages instead of doing it all on their local machines.

I decided to give every student an FTP account on my website to do some of their projects and assignments there.

After I could find a suitable host with unlimited FTP accounts (HostGator), I had a problem that I had to create 133 FTP accounts manually from the cPanel … So tiring!

In short, I could do this programatically through the cPanel API.

A php webpage from which I make a request/call to the cPanel webpage responsible for adding FTP accounts (doaddftp.html):

XXX.php


<!--?php
$cpanel_user = 'username';
// your cPanel password $cpanel_pass = 'password';
// your cPanel skin $cpanel_skin = 'x3';
// your cPanel domain $cpanel_host = 'mydomain.com';
// ftp username $ftp_user = $_POST['ftpuser'];
// ftp password $ftp_pass = $_POST['ftppass'];
// ftp quota $ftp_quota = '20';

// create the ftp account
$request = "http://{$cpanel_user}:{$cpanel_pass}@{$cpanel_host}:2082";
$request .= "/frontend/{$cpanel_skin}/ftp/doaddftp.html";
$request .= "?login={$ftp_user}";
$request .= "&password={$ftp_pass}";
$request .= "&homedir=public_html/projects/{$ftp_user}";
$request .= "&quota={$ftp_quota}";
$result = file_get_contents($request);
echo $result;
?-->

The XXX.php accepts the FTP username and password through POST method.

I’ve then created a java program that requests the XXX.php webpage 133 times each with a unique username and password for each student.


import java.io.*;
import java.net.*;
import java.util.Scanner;

/**
 *
 * @author Mindhunter - Ahmed Abdullah Hussein
 * mindhunter74@yahoo.com
 */
public class ftpAccountCreator {

    public static void main(String[] args) throws Exception {
        Scanner sc = new Scanner(new File("inputForAccounts.txt"));
        int numOfStudents = 133;
        for (int i = 1; i <= numOfStudents; i++) {
            String ftpuser = sc.next(), ftppass = sc.next();
            String params = "ftpuser=" + ftpuser + "&ftppass=" + ftppass;
            String response = excutePost("http://www.mydomain.com/XXX.php", params);
            if (response.contains("Account created successfully")) {
                System.out.println("Student " + i + " created successfully " + ftpuser + " " + ftppass);
            } else {
                System.out.println("Failure!!");
                System.exit(0);
            }
            Thread.sleep(3000);
        }
    }

    public static String excutePost(String targetURL, String urlParameters) {
        URL url;
        HttpURLConnection connection = null;
        try {
            //Create connection
            url = new URL(targetURL);
            connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type",
                    "application/x-www-form-urlencoded");

            connection.setRequestProperty("Content-Length", ""
                    + Integer.toString(urlParameters.getBytes().length));
            connection.setRequestProperty("Content-Language", "en-US");

            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            //Send request
            DataOutputStream wr = new DataOutputStream(
                    connection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            //Get Response
            InputStream is = connection.getInputStream();
            BufferedReader rd = new BufferedReader(new InputStreamReader(is));
            String line;
            StringBuffer response = new StringBuffer();
            while ((line = rd.readLine()) != null) {
                response.append(line);
                response.append('\r');
            }
            rd.close();
            return response.toString();

        } catch (Exception e) {

            e.printStackTrace();
            return null;

        } finally {

            if (connection != null) {
                connection.disconnect();
            }
        }
    }
    // To check if any names are repeated before creating accounts (one time call)
    public boolean noNamesRepeated() throws Exception {
        Scanner sc = new Scanner(new File("inputForAccounts.txt"));
        String[] names = new String[133];
        for (int i = 0; i < names.length; i++) {
            names[i] = sc.next();
            sc.next();
        }
        for (int i = 0; i < names.length; i++) {
            String string = names[i];
            for (int j = 0; j < names.length; j++) {
                if (i != j && names[i].equals(names[j])) {
                    System.out.println(names[i]);
                    return false;
                }
            }
        }
        return true;
    }
    // To check if any passwords are repeated before creating accounts (one time call)
    public boolean noNPasswordsRepeated() throws Exception {
        Scanner sc = new Scanner(new File("inputForAccounts.txt"));
        String[] names = new String[133];
        for (int i = 0; i < names.length; i++) {
            sc.next();
            names[i] = sc.next();
        }
        for (int i = 0; i < names.length; i++) {
            String string = names[i];
            for (int j = 0; j < names.length; j++) {
                if (i != j && names[i].equals(names[j])) {
                    System.out.println(names[i]);
                    return false;
                }
            }
        }
        return true;
    }
}

Note that the java program reads accounts information from the file inputForAccounts.txt which is formatted as follows:
username1 pass1
username2 pass2

It took about only 6 minutes to create the 133 FTP accounts with the program and I could make it less!

Sources:

  1. http://stackoverflow.com/questions/8888287/create-ftp-account-in-cpanel-without-domain
  2. http://www.xyzws.com/Javafaq/how-to-use-httpurlconnection-post-data-to-web-server/139
Categories: Web Development

Abosolute vs. Relative Path

March 12, 2010 2 comments

When we create a web-page at which there are some links (URLs) to images or other pages, links are written either in an absolute or relative way (commonly known as absolute and relative paths).

Suppose that we’ve a website whose domain name is http://www.shoubraeng.com and there’s a web-page that’s in the directory http://www.shoubraeng.com/pages/project/index.html.

Let’s also assume that there’s a subdirectory of the project directory called images.

Here’s a description of the hierarchy (directories are written in bold):

|___ pages

____|____ picture1.jpg

____|____ project

_________|_____ picture2.jpg

_________|_____ index.html

_________|_____ images

_______________|_____ picture3.jpg

The index.html page is the web-page of interest. We’re trying to make a background picture to it from the available 3 pictures. picture1.jpg is in the pages directory, picture2.jpg is in the project directory and picture3.jpg is in the images directory.

Now let’s see the different ways to link to any of these pictures.

Absolute Path

The easy way is to use the absolute/full path of the image that looks like:

<body background=”http://www.shoubraeng.com/pages/picture1.jpg“> or

<body background=”http://http://www.shoubraeng.com/pages/project/images/picture3.jpg“>

You might have guessed that it’s called absolute/full because it contains the very specific path to the image.

One more note about absolute paths is that if the file is on the same domain of your web-page then you may not write the domain name but just start with a slash (/) like:

<body background=”/pages/picture1.jpg“>

Relative Path

It’s called relative because it’s described relative to the path of your web-page regardless of its position.

1- If you are linking to picture2.jpg that’s in the same directory of your index.html web-page then – using relative path – you may just write its name as follows:

<body background=”picture2.jpg“>

Writing only the name of the image in the path tells the browser that it’s in the same directory of the web-page that is being loaded now.

2- If you are linking to picture3.jpg that’s in a subdirectory of your index.html webp-page, it will then look like:

<body background=”images/picture3.jpg“>

It should also be noticeable that starting with a directory name (with no / at the beginning) tells the browser to find the image in the subdirectory located in the directory of the web-page that’s being loaded now.

3- If you are linking to picture1.jpg that’s up on directory from the web-page it will then look like:

<body background=”../picture1.jpg“>

The double dots (..) here tells the browser to go up on directory to find the image.

Which is better … absolute or relative path?

Each is preferred at certain conditions.

Absolute path is better when what you link to is used in multiple pages across your website so that it doesn’t matter where your page is. Suppose that there’s a banner image that you use for all pages in your website … is it good to replicate the image in every folder of your website to use a relative path of it in every web-page there? Of course not.

Relative path is better when what you link to is very specific to the web-page and is located in the same directory containing that web-page or a subdirectory of it. Suppose a situation where you are writing an article web-page that contains many related pictures inside it. You’ve made a directory containing your article and a subdirectory containing all the article-related images. Now you are going to link to these images using either absolute or relative paths and you wonder which is better! Ask yourself the following question and it should reply you …. what if you later moved the article to a different domain? If were’re linking to images in an absolute way then you will have to change all paths again to the new domain but if you were using relative paths then you will not have to change anything.

This is because what is relative here is relative there!

Ahmed Abdullah Hussein