+ $result = ($data) ? false : array();
+ }
+
+ return $result;
+ }
+
+ private function RESTCall($url, $data = null, $cookiesIn = '', $headers = null) {
+ $options = array(
+ CURLOPT_RETURNTRANSFER => true, // return web page
+ CURLOPT_HEADER => true, //return headers in addition to content
+ CURLOPT_FOLLOWLOCATION => true, // follow redirects
+ CURLOPT_ENCODING => "", // handle all encodings
+ CURLOPT_AUTOREFERER => true, // set referer on redirect
+ CURLOPT_CONNECTTIMEOUT => 120, // timeout on connect
+ CURLOPT_TIMEOUT => 120, // timeout on response
+ CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
+ CURLINFO_HEADER_OUT => true,
+ CURLOPT_SSL_VERIFYPEER => false, // Disabled SSL Cert checks
+ CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
+ CURLOPT_COOKIE => $cookiesIn
+ );
+
+ if ($data) {
+ $options[CURLOPT_POST] = 1;
+ $options[CURLOPT_POSTFIELDS] = $data;
+ }
+
+ if ($headers) {
+ $options[CURLOPT_HTTPHEADER] = $headers;
+ }
+
+ $ch = curl_init($url);
+ curl_setopt_array($ch, $options);
+ $rough_content = curl_exec($ch);
+ $err = curl_errno($ch);
+ $errmsg = curl_error($ch);
+ $header = curl_getinfo($ch);
+ curl_close($ch);
+
+ $header_content = substr($rough_content, 0, $header['header_size']);
+ $body_content = trim(str_replace($header_content, '', $rough_content));
+ $pattern = "#Set-Cookie:\\s+(?<cookie>[^=]+=[^;]+)#m";
+ preg_match_all($pattern, $header_content, $matches);
+ $cookiesOut = implode("; ", $matches['cookie']);
+
+ $header['errno'] = $err;
+ $header['errmsg'] = $errmsg;
+ $header['headers'] = $header_content;
+ $header['content'] = $body_content;
+ $header['cookies'] = $cookiesOut;
+
+ return $header;
+ }
+
+ public function logout() {
+ $this->log("logout", 4);
+
+ if (ini_get('session.use_cookies')) {
+ $params = session_get_cookie_params();
+ setcookie(session_name(), '', time() - 42000,
+ $params['path'], $params['domain'],
+ $params['secure'], $params['httponly']);
+ }
+
+ if ('' != session_id()) {
+ $_SESSION = array();
+ session_unset();
+ session_destroy();
+ }
+ $this->settings = array();
+ }
+
+ public function isAdmin() {
+ $admin = false;
+
+ $this->log("isAdmin", 4);
+
+ if (isset($this->settings['admin'])) {
+ $admin = $this->settings['admin'];
+ }
+
+ return $admin;
+ }
+
+ public function login($user, $pw) {
+ global $CFG;
+ $result = false;
+
+ $this->log("login", 4);
+
+ if ('' == session_id()) {
+ $this->startSession();
+ }
+
+ $this->settings['user'] = null;
+ $this->settings['admin'] = false;
+
+ $p = explode('@', $user);
+ if (count($p) != 2) {
+ $this->settings['loginStatus'] = 'Bad username';
+ } else {
+ $domain = $p[1];
+ $dn = "mail=$user,ou=Users,domainName=$domain,$CFG->ldap_base_dn";
+ $filter = "(&(objectclass=mailUser)(accountStatus=active)(mail=$user))";
+ $ds = @ldap_connect($CFG->ldap_dsn);
+ if ($ds) {
+ @ldap_set_option($ds, LDAP_OPT_PROTOCOL_VERSION, 3);
+ $r = @ldap_bind($ds, $dn, $pw);
+ if ($r) {
+ $sr = @ldap_search($ds, $CFG->ldap_base_dn, $filter, array('mail','domainglobaladmin'));
+ $info = @ldap_get_entries($ds, $sr); // array
+ if ($info['count'] > 0) {
+ // Log in to wblistadm server and get CSRFPreventionToken
+ if ($this->getRestTicket($user, $pw)) {
+ $this->settings['user'] = $user;
+ $result = true;
+ $this->settings['loginStatus'] = 'OK';
+ $admin = 'NO';
+ if (isset($info[0]['domainglobaladmin'])) {
+ $admin = $info[0]['domainglobaladmin'][0];
+ $admin = strtoupper($admin);
+ }
+ $this->settings['admin'] = ($admin == 'YES') ? true : false;
+ } else {
+ $this->settings['loginStatus'] = 'Login failed';
+ }
+ } else {
+ $this->settings['loginStatus'] = 'Login failed';
+ }
+ } else {
+ $this->settings['loginStatus'] = ldap_error($ds);
+ }
+ @ldap_close($ds);
+ } else {
+ $this->settings['loginStatus'] = 'Connect to LDAP server failed';
+ }
+ }
+
+ $_SESSION['settings'] = $this->settings;
+
+ return $result;
+ }
+
+ public function getLoginStatus() {
+ $status = 'Not logged in';
+
+ $this->log("getLoginStatus", 4);
+
+ if (isset($this->settings['loginStatus'])) {
+ $status = $this->settings['loginStatus'];
+ }
+
+ return $status;
+ }
+
+ public function isLoggedIn() {
+ global $CFG;
+ $loggedIn = false;
+
+ $this->log("isLoggedIn[1]: user ".var_export($this->settings['user'], true), 3);
+
+ if ('' == session_id()) {
+ $this->startSession();
+ }
+
+ $this->log("isLoggedIn[2]: user ".var_export($this->settings['user'], true), 3);
+ $this->checkSession();
+ $this->log("isLoggedIn[3]: user ".var_export($this->settings['user'], true), 3);
+
+ if (isset($this->settings['user'])) {
+ if ($this->settings['user'] != null) {
+ $loggedIn = true;
+ } else {
+ if ($CFG->auth_method == 'HTTP_AUTH') {
+ if (isset($_SERVER['PHP_AUTH_USER'])) {
+ $this->settings['user'] = $_SERVER['PHP_AUTH_USER'];
+ $loggedIn = true;
+ }
+ }
+ }
+ }
+
+ if ($loggedIn == false) {
+ $this->log('$this->settings: '.var_export($this->settings, true), 3);
+ $this->log('R_TIME: '.date('c', $_SERVER['REQUEST_TIME']).' L_ACT: '.date('c', $_SESSION['LAST_ACTIVITY']), 3);
+ }
+
+ $_SESSION['settings'] = $this->settings;
+
+ return $loggedIn;
+ }
+
+ public function getUser() {
+ $user = null;
+
+ $this->log("getUser", 4);
+
+ if ($this->isLoggedIn()) {
+ $user = $this->settings['user'];
+ }
+
+ return $user;
+ }
+
+ public function authorized($recipient) {
+ $authorized = false;
+
+ $this->log("authorized '$recipient'", 3);
+
+ if ($this->isAdmin() || $this->getUser() == $recipient) {
+ $authorized = true;
+ }
+ $msg = ($authorized) ? 'authorize' : 'not authorize';
+ $this->log("$msg '".$this->getUser()."' rcpt '$recipient'", 3);
+
+ return $authorized;
+ }
+
+ public function getHeader() {
+ $this->log("getHeader", 4);
+
+ return $this->header;
+ }
+
+ public function getFooter() {
+ $this->log("getFooter", 4);
+
+ return $this->footer;
+ }
+
+ public function getHeading() {
+ $this->log("getHeading", 4);
+
+ return $this->heading;
+ }
+
+ public function setHeading($heading) {
+ global $CFG;
+
+ $this->log("setHeading", 4);
+
+ $timeout = $CFG->session_timeout * 60 * 1000;
+ $this->heading = str_replace('__TITLE__', $heading, $this->heading);
+ $this->header = str_replace('__TITLE__', $heading, $this->header);
+ $this->header = str_replace('__ROOT__', $CFG->wwwroot, $this->header);
+ $this->header = str_replace('__TIMEOUT__', $timeout, $this->header);
+ }
+
+ public function convertContent($code) {
+ $this->log("convertContent", 4);
+
+ $table = array(
+ 'V' => 'Virus',
+ 'B' => 'Banned',
+ 'U' => 'Unchecked',
+ 'S' => 'Spam',
+ 'Y' => 'Spammy',
+ 'M' => 'Bad Mime',
+ 'H' => 'Bad Header',
+ 'O' => 'Over sized',
+ 'T' => 'MTA err',
+ 'C' => 'Clean'
+ );
+
+ $string = $table[$code];
+ if (empty($string))
+ $string = 'Unknown';
+
+ return $string;
+ }