# AJAX Chat Standalone

v 0.8.5 standalone
https://blueimp.net/ajax/

This is the standalone version of blueimp's AJAX Chat designed to run on its own, without another web application.
If you want to integrate AJAX Chat with one of the forums we support, go back and choose the right version.
This version is good for customizing your own integration, or using on its own.

AJAX stands for "Asynchronous JavaScript and XML".
The AJAX Chat client (your browser) uses JavaScript to query the web server for updates.
Instead of delivering a complete HTML page only updated data is sent in XML format.
By using JavaScript the chat page can be updated without having to reload the whole page.
PHP is used to communicate with the database and authenticate users.

## Requirements

Server-Side | Client-Side
--- | ---
PHP >= 4 MySQL >= 4 Ruby >= 1.8 (optional) | Enabled JavaScript Enabled Cookies Flash Plugin >= 9 (optional)

## Installation

Download your preferred version of AJAX Chat and unzip the file on your computer.

### Before You Begin

In order to edit PHP files you will need a good text editor. You should not use Windows notepad, wordpad, or Microsoft Word to edit PHP files. These programs will add something called a byte-order-mark (BOM) to the files and this may prevent chat from functioning properly. We recommend using Notepad ++ ( http://notepad-plus.sourceforge.net ) for editing all files. It also has the benefit of color-coding your files so you can edit them more easily.
If you get an error message like "Cannot modify header information - headers already sent" it is likely because you have used one of the above programs to edit files.

### Configure Database Settings

The first and most important thing you need to do is tell AJAX Chat how to connect to your database. This, and all core settings must be located inside the file lib/config.php.
You need to create this file.
An example config.php file can be found in lib/config.php.example that shipped with chat.
Duplicate this file and save it as config.php (or just delete .example from the end of the file name) and then fill out at least the following four fields in the file:

`$config['dbConnection']['host'] = 'your_database_hostname';
$config['dbConnection']['user'] = 'your_database_username';
$config['dbConnection']['pass'] = 'your_database_password';
$config['dbConnection']['name'] = 'your_database_name';`

Sufficed to say you need this information. Talk to your hosting provider if you don't know.

In most cases, chat will function with only these fields filled out and you can proceed to the next step.

If your host does not use mysqli you will need to change the connection type field:
$config['dbConnection']['type'] = null;
If this is set to "null" it defaults to "mysqli" if existing, else to "mysql". In most cases this field can be left as null.
You can reference an existing database connection link or object by changing:
$config['dbConnection']['link'] = null;
If this is set to null, a new database connection is created.

### Choose Your Channel Settings

Edit the file lib/data/channels.php.
We have provided you with two sample channels, named public and private. You can add your own, or leave it as-is.
Channels follow the following format:
`$channels[channel id] = 'channel name';` Each channel must have a unique channel id number and a unique name.
Whitespace in the channel names will be converted to the underscore "_".

### Add Your Users

Edit users in lib/data/users.php.
Users follow the following format:

`$users[user id] = array();
$users[user id]['userRole'] = AJAX_CHAT_ROLE;
$users[user id]['userName'] = 'user name';
$users[user id]['password'] = 'user password';
$users[user id]['channels'] = array(allowed channel ids);`
Each user must have a unique user id number and a unique name.
The first user in the list (user id 0) is used for the guest user settings. All guest users will have access to the channels set for this user and the user role AJAX_CHAT_GUEST.
Registered users can have the user roles AJAX_CHAT_USER, AJAX_CHAT_MODERATOR or AJAX_CHAT_ADMIN. (this is case sensitive, type it exactly)
The list of channels a user has access to can be set for each user individually. Channel id's are separated by commas. eg: array(0,1,23); allows channels 0, 1 and 23.
Whitespace in the user names will be converted to the underscore "_".

### Upload to Your Server

Upload the chat folder to your server somewhere under your document root:
e.g. http://example.org/path/to/chat/

### Create the Database Tables

There are two options available to you to create the database. The first, and usually the easiest option, is to run the installation script included with AJAX Chat. Alternatively, you may use a database tool like PHPMyAdmin to manually create the tables.

1. To use the installation script, visit the following URL in your browser:
   http://example.org/path/to/chat/install.php
   Where "http://example.org/path/to/chat/" is the real URL to your chat directory.
2. To install it manually using PHPMyAdmin or a similar tool, copy the contents of the chat.sql file and run it as a query.

Either of these methods will create the tables your database needs to store chat messages and other information.

### Delete the Installation Script

Delete the file install.php from the chat directory on your server. You may also delete the file chat.sql.

### Congradulation! You Are Winner!

Yay! You're done! To test your chat, navigate to your chat URL in a browser: http://example.org/path/to/chat/index.php
You are now free to customize chat to further suit your needs.

## Configuring and Customizing

### Configuration Files

AJAX Chat is fully customizable and contains two configuration files:

1. lib/config.php: This file contains the core configuration options for chat. Essential options for configuring the database, security, available languages, etc, are found here.
2. js/config.js: This file contains client side settings that change your users' default options in chat. Many of these settings can be changed by users in their options but some (like the refresh rate) cannot.

Both of these files are well commented with information on what the settings mean.

### Customizing the Layout

The layout of AJAX Chat is fully customizable by using CSS (Cascaded Style Sheets).
AJAX Chat comes with a predefined set of styles. To add your own style, do the following:

1. Add a new CSS file (e.g. mystyle.css) by copying one of the existing styles from the CSS directory.
2. Edit your file (css/mystyle.css) and adjust the CSS settings to your liking.
3. Add the name of your style without file extension to the available styles in lib/config.php:
   `// Available styles:
   $config['styleAvailable'] = array('mystyle','beige','black','grey');
   // Default style:
   $config['styleDefault'] = 'mystyle';`

To further customize the layout you can adjust the template files in lib/template/.

Make sure you are creating valid XHTML, else you will produce errors in modern browsers.
This is due to the page content-type served as "application/xhtml+xml".
Using this content-type improves performance when manipulating the Document Object Model (DOM).

If for some reason you cannot create valid XHTML you can force a HTML content-type.
Just edit lib/config.php and set the following option:

`$config['contentType'] = 'text/html';`

### Adjusting the Language Settings

AJAX Chat comes with two language file directories:

1. js/lang/: This directory contains the language files used for the chat messages localization. These are JavaScript files with the extension ".js".
2. lib/lang/: This directory contains the language files used for the template output. These are PHP files with the extension ".php".

Many languages are already included with the download and you can customize them by editing these files.
For each language, you need a file in both of these directories, with the language code as file name (such as en.js and en.php)..
The language code is used following the ISO 639 standards.

The files for the english (language code "en") localization are js/lang/en.js and lib/lang/en.php.

If you create your own localization, you must put the files in the correct folders and then make two changes to config.php:

1. Add the language code (this must match the filename you chose for the language. Remember to use commas correctly to separate multiple language codes):
   `$config['langAvailable'] = array('en');`
2. Add the language name (this is what users see in the dropdown menu to choose the language):
   `$config['langNames'] = array('en'=>'English');`

To avoid errors, you should follow these rules:

1. Make sure you encode your localization files in UTF-8 (without Byte-order mark).
2. Don't use HTML entities in your localization files.
3. Don't remove any "%s" inside the JavaScript language files - these are filled with dynamic data.

### Adding Features

AJAX Chat is designed with numerous hooks and overrides available to improve core functionality without requiring you to edit the core files. With an intermediate understading of PHP and javascript you can modify your chat to suit your needs.

Have a look through a few examples available on the sourceforge wiki: https://sourceforge.net/apps/mediawiki/ajax-chat/index.php?title=General_Modifications

## Logs

### Accessing the Logs

By default, AJAX Chat stores all chat messages in the database.
To access the logs you have to add the GET parameter view=logs to your chat url (add ?view=logs to the end of the url):

e.g. http://example.org/path/to/chat/?view=logs

If you are not already logged in, you have to login as administrator to access the logs.

The log view enables you to monitor the latest chat messages on all channels.
It is also possible to view the logs of private rooms and private messages.
You have the option to filter the logs by date, time and search strings.

The search filter accepts MySQL style regular expressions as described here: http://dev.mysql.com/doc/refman/5.1/en/regexp.html
You can search for IPs, using the following syntax: ip=127.0.0.1

## Shoutbox

AJAX Chat is also usable as shoutbox - this is a short guide on how to set it up:

### Shoutbox Stylesheet

Add the following line to the stylesheet (CSS) of all pages displaying the shoutbox:

`@import url("http://example.org/path/to/chat/css/shoutbox.css");`

Replace http://example.org/path/to/chat/ with the URL to the chat.
Modify css/shoutbox.css to your liking.

### Shoutbox Function

Add the following function to your PHP code:

`<?php
function getShoutBoxContent() {
// URL to the chat directory:
if(!defined('AJAX_CHAT_URL')) {
define('AJAX_CHAT_URL', './chat/');
}
// Path to the chat directory:
if(!defined('AJAX_CHAT_PATH')) {
define('AJAX_CHAT_PATH', realpath(dirname($_SERVER['SCRIPT_FILENAME']).'/chat').'/');
}
// Validate the path to the chat:
if(@is_file(AJAX_CHAT_PATH.'lib/classes.php')) {
// Include Class libraries:
require_once(AJAX_CHAT_PATH.'lib/classes.php');
// Initialize the shoutbox:
$ajaxChat = new CustomAJAXChatShoutBox();
// Parse and return the shoutbox template content:
return $ajaxChat->getShoutBoxContent();
}
return null;
}
?>`

Make sure AJAX_CHAT_URL and AJAX_CHAT_PATH point to the chat directory.

### Shoutbox Output

Display the shoutbox content using the shoutbox function:

`<div style="width:200px;"><?php echo getShoutBoxContent(); ?></div>`

## Socket Server

Using the AJAX technology alone the chat clients have to permanently pull updates from the server.
This is due to AJAX being a web technology and HTTP being a stateless protocol.
Events pushed from server-side need a permanent or long-lasting socket connection between clients and server.
This requires either a custom HTTP server (called "comet") or another custom socket server.

AJAX Chat uses a JavaScript-to-Flash bridge to establish a permanent socket connection from client side.
The JavaScript-to-Flash bridge requires a Flash plugin >= 9 installed on the user browser.
Clients without this requirement will fall back to pull the server for updates.

This part of the setup is OPTIONAL and meant for experienced users only.

### Installation

The socket server coming with AJAX Chat is implemented in Ruby.
You need to be able to run a Ruby script as a service to run the socket server.
To be able to start the service, the script files in the socket/ directory have to be executable:

`$ chmod +x server
$ chmod +x server.rb`

"server" is a simple bash script to start and stop a service.
"server.rb" is the ruby socket server script.
"server.conf" is a configuration file - each setting is explained with a comment.

To start the service, execute the "server" script with the parameter "start":

`$ ./server start`

This will create two additional files:

"server.pid" contains the process id of the service.
"server.log" is filled with the socket server log.

To monitor the socket server logs, you can use the "tail" command included in most GNU/Linux distributions:

`$ tail -f server.log`

By default only errors and start/stop of the server are logged.
To get more detailed logs configure the log level by editing the configuration file.

To stop the service, execute the "server" script with the parameter "stop":

`$ ./server stop`

If the socket server is running, you have to enable the following option in lib/config.php:

`$config['socketServerEnabled'] = true;`
This tells the server-side chat script to broadcast chat messages via the socket server.
Chat clients will establish a permanent connection to the socket server to listen for chat messages.

By default only local clients (127.0.0.1,::1) may broadcast messages.
Clients allowed to broadcast messages may also handle the channel authentication.
If your socket server is running on another host you should set the broadcast_clients option to the chat server IP.

Using the socket server increases response time while improving server performance at the same time.

### Flash Permissions

Since Flash 9.0.115.0 and all Flash 10 versions, permissions for creating sockets using Flash have changed.
Now an explicit permission (using xml-syntax) is required for creating socket connections.
In the current state, socket server won't work with the newest Flash versions.
You will get a "Flash security error" in the browser.

A solution is to use a policy-files server which will listen to connections in port 843 in the server.
Each time a client tries to connect to the chat, the Flash client will request the policy authorization to the server.
The policy-files server is downloadable from http://ammonlauritzen.com/FlashPolicyService-09b.zip
It works with FF3 and IE7 (not yet tested in other browsers).

A more detailed explanation can be found here:

* http://ammonlauritzen.com/blog/2007/12/13/new-flash-security-policies/
* http://ammonlauritzen.com/blog/2008/04/22/flash-policy-service-daemon/

Official Adobe documentation:

* http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security.html
* http://www.adobe.com/devnet/flashplayer/articles/fplayer9_security_04.html

## Donate

Your donations contribute to the growth and development of this project and are always appreciated.

## License

Bluimp's AJAX Chat is released under the GNU Affero General Public License.

You should also find this license included with your download of this project.

back to top