<?php // $Id: sms.php.txt,v 1.2 2007/05/05 11:17:59 janne Exp $
/// configuration routines for HTMLArea editor

    require_once("../config.php");
    require_login();

    if (!$site = get_site()) {
        error("Site isn't defined!");
    }

    if (!isadmin()) {
        error("Only admins can access this page");
    }

    if (($data = data_submitted()) && confirm_sesskey()) {

        $err = _sms_update_config($data);

        if ( !is_array($err) ) {
            redirect("$CFG->wwwroot/$CFG->admin/sms.php", get_string("changessaved"), 1);
        }

    }

    // Generate edit form

    $formdata = get_records('sms_config');

    if (is_array($formdata)) {
        $form = new stdClass;

        foreach ($formdata as $data) {
            $form->{$data->name} = $data->value;
        }
    }

    $stradmin = get_string("administration");
    $strconfiguration = get_string("configuration");
    $strsmsconfig = get_string("smsconfiguration","sms");
    $strsmsconfighelp = get_string('helpsmsconfiguration','sms');
    $strsmsconfighelp .= '<br />' . get_string('helpsmsconfiguration1','sms');

    print_header("$site->shortname", $site->fullname,
                 "<a href=\"index.php\">$stradmin</a> -> ".
                 "<a href=\"configure.php\">$strconfiguration</a> -> $strsmsconfig");
    print_heading($strsmsconfig);
    print_simple_box("<center>$strsmsconfighelp</center>","center","50%");
    print("<br />\n");
    print_simple_box_start("center");
    include("sms.html");
    print_simple_box_end();
    print_footer();

/////////////////////////// FUNCTIONS ///////////////////////////

function _sms_get_interfaces () {

    $interfaces = array();

    if (function_exists('fsockopen')) {
        $interfaces['fsockopen'] = 'fsockopen -function';
    }

    if (function_exists('socket_create')) {
        $interfaces['socket'] = 'socket -function';
    }

    if (function_exists('curl_init')) {
        $interfaces['curl'] = 'Client URL library';
    }

    return $interfaces;

}

function _sms_get_clients () {

    global $CFG;

    $exclude = array('install','connectors','cvs');
    $clients = array();
    $dir = $CFG->libdir .'/sms/';

    if ($handle = @opendir($dir)) {

        while (($file = @readdir($handle)) !== false) {

            if (is_dir($dir . $file)) {

                $chr = substr($file, 0, 1);

                if ($chr == '.') {
                    continue;
                }

                if ( !in_array(strtolower($file), $exclude) ) {

                    $clients[$file] = $file;

                }

            }
        }
    }

    @closedir($handle);

    return $clients;

}

function _sms_update_config ($data) {

    global $CFG;

    include_once($CFG->dirroot .'/lib/validateurlsyntax.php');
    $protocols = array("http","https","udp","tcp","tls","ssl");

    $newconfig = (array) $data;
    $err = array();

    foreach ($newconfig as $name => $value) {

        if ($name == 'sesskey') {
            continue;
        }

        switch (strtolower($name)) {

            case 'gwhost':
                if ( !empty($value) && !validateurlsyntax($value, 's-u-P-I?p-f-q-r-') ) {
                    // TODO: generate errors and show them in userinterface.
                    $err['gwhost'] = get_string('smsinvalidgwhost','sms');
                } else {
                    $value = addslashes($value);
                }
                break;

            case 'gwport':

                if (!empty($value)) {
                    $value = intval($value);

                    if (empty($value)) {
                        $err['gwport'] = get_string('smsinvalidgwport','sms');
                    }

                }

                break;

            case 'gwproto':

                if (!empty($value)) {
                    if (! in_array($value, $protocols) ) {
                        $err['gwproto'] = get_string('smsinvalidgwproto','sms');
                    }
                }

                break;

        }

        if (! empty($err) ) {
            return $err;
            exit;
        }

        if ($exists = get_field('sms_config', 'name', 'name', $name)) {

            if (! set_field('sms_config', 'value', $value, 'name', $name)) {
                return false;
            }

        } else {

            // Add new record
            $newval = new stdClass;
            $newval->id = NULL;
            $newval->name  = addslashes(trim($name));
            $newval->value = addslashes(trim($value));

            insert_record('sms_config', $newval);

        }
    }

    return true;

}
?>