<?php
Doo::loadModelAt('auser', 'admin');
Doo::loadModelAt('ausers', 'admin');
Doo::loadModel('users');
Doo::loadClass('profile');
Doo::loadClass('PasswordHash');
Doo::loadClass('mailer');
Doo::loadClass('nusoap');
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

// 列表停用 编辑 重置密码
// 管理员权限管理
// 管理员修改密码
class ServiceController extends DooController
{
    private $data, $users, $user, $profile, $ph, $userz, $mailer;


    public function __construct()
    {
        $this->users = new AUsers();
        $this->user = new AUser();
        $this->userz = new Users();
        $this->profile = new Profile();
        $this->mailer = new Mailer();
        $this->ph = new PasswordHash(8, FALSE);
        $this->data['rootUrl'] = Doo::conf()->APP_URL;
    }

    public function ZGServer()
    {
        ini_set("soap.wsdl_cache_enabled", "0");
        $server = new SoapServer(Doo::conf()->APP_URL . 'service/getwsdl?wsdl', array('soap_version' => SOAP_1_2));
        $server->setClass('ServiceController');
        $server->addFunction('addUser');
        $server->addFunction(SOAP_FUNCTIONS_ALL);
        $server->handle();
    }

    public function getWSDL()
    {
        ini_set("soap.wsdl_cache_enabled", "0");
        $soap = new soap_server;
        $soap->configureWSDL('UserService', 'urn:UserService', Doo::conf()->APP_URL . 'service/user');
        $soap->soap_defencoding = 'UTF-8';
        $soap->register(
            'addUser',
            array('username' => 'xsd:string', 'realname' => 'xsd:string'),
            array('retval' => 'xsd:boolean'),
            'urn:UserService',
            'urn:UserService#addUser'
        );
//        $soap->register(
//            'updateUser',
//            array('username' => 'xsd:string')
//        );
//        $soap->service(file_get_contents("php://input"));
        $soap->service(isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '');
    }

    function addUser($username, $realname)
    {
        if (isset($username) && isset($realname)) {
            $uid = $this->users->createUser($username, $username);
            if ($uid) {
                $this->profile->ZGinsertProfile($uid, urldecode($realname));
                return true;
            } else {
                return false;
            }
        } else {
            return false;
        }
    }

    public function randomPassword()
    {
        $alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
        $pass = array(); //remember to declare $pass as an array
        $alphaLength = strlen($alphabet) - 1; //put the length -1 in cache
        for ($i = 0; $i < 8; $i++) {
            $n = rand(0, $alphaLength);
            $pass[] = $alphabet[$n];
        }
        return implode($pass); //turn the array into a string
    }
}
