]> git.mxchange.org Git - friendica.git/blobdiff - include/auth_ejabberd.php
onepoll.php is reworked
[friendica.git] / include / auth_ejabberd.php
index 339acd15c217e47de7b93420e6f9e702e16ac468..14f5dcfe0c797271ab5d09720223c2a8675d82a8 100755 (executable)
@@ -32,6 +32,8 @@
  *
  */
 
+use Friendica\App;
+
 if (sizeof($_SERVER["argv"]) == 0)
        die();
 
@@ -45,17 +47,16 @@ $directory = realpath($directory."/..");
 chdir($directory);
 require_once("boot.php");
 
-global $a, $db;
+global $a;
 
-if (is_null($a))
-       $a = new App;
+if (empty($a)) {
+       $a = new App(dirname(__DIR__));
+}
 
-if (is_null($db)) {
-       @include(".htconfig.php");
-       require_once("include/dba.php");
-       $db = new dba($db_host, $db_user, $db_pass, $db_data);
-       unset($db_host, $db_user, $db_pass, $db_data);
-};
+@include(".htconfig.php");
+require_once("include/dba.php");
+dba::connect($db_host, $db_user, $db_pass, $db_data);
+unset($db_host, $db_user, $db_pass, $db_data);
 
 // the logfile to which to write, should be writeable by the user which is running the server
 $sLogFile = get_config('jabber','logfile');
@@ -78,22 +79,20 @@ class exAuth {
         * @param boolean $bDebug Debug mode
         */
        public function __construct($sLogFile, $bDebug) {
-               global $db;
-
                // setter
                $this->sLogFile         = $sLogFile;
                $this->bDebug           = $bDebug;
 
                // Open the logfile if the logfile name is defined
                if ($this->sLogFile != '')
-                       $this->rLogFile = fopen($this->sLogFile, "a") or die("Error opening log file: ". $this->sLogFile);
+                       $this->rLogFile = fopen($this->sLogFile, "a") || die("Error opening log file: ". $this->sLogFile);
 
                $this->writeLog("[exAuth] start");
 
                // We are connected to the SQL server and are having a log file.
                do {
                        // Quit if the database connection went down
-                       if (!$db->connected()) {
+                       if (!dba::connected()) {
                                $this->writeDebugLog("[debug] the database connection went down");
                                return;
                        }
@@ -146,35 +145,42 @@ class exAuth {
         * @param array $aCommand The command array
         */
        private function isuser($aCommand) {
-               global $a;
+               $a = get_app();
 
                // Check if there is a username
                if (!isset($aCommand[1])) {
                        $this->writeLog("[exAuth] invalid isuser command, no username given");
                        fwrite(STDOUT, pack("nn", 2, 0));
+                       return;
+               }
+
+               // Now we check if the given user is valid
+               $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
+               $this->writeDebugLog("[debug] checking isuser for ". $sUser."@".$aCommand[2]);
+
+               // Does the hostname match? So we try directly
+               if ($a->get_hostname() == $aCommand[2]) {
+                       $sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
+                       $this->writeDebugLog("[debug] using query ". $sQuery);
+                       $r = q($sQuery);
+                       $found = dbm::is_result($r);
                } else {
-                       // Now we check if the given user is valid
-                       $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
-                       $this->writeDebugLog("[debug] checking isuser for ". $sUser."@".$aCommand[2]);
-
-                       // If the hostnames doesn't match, we try to check remotely
-                       if ($a->get_hostname() != $aCommand[2])
-                               $found = $this->check_user($aCommand[2], $aCommand[1], true);
-                       else {
-                               $sQuery = "SELECT `uid` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
-                               $this->writeDebugLog("[debug] using query ". $sQuery);
-                               $r = q($sQuery);
-                               $found = dbm::is_result($r);
-                       }
-                       if ($found) {
-                               // The user is okay
-                               $this->writeLog("[exAuth] valid user: ". $sUser);
-                               fwrite(STDOUT, pack("nn", 2, 1));
-                       } else {
-                               // The user isn't okay
-                               $this->writeLog("[exAuth] invalid user: ". $sUser);
-                               fwrite(STDOUT, pack("nn", 2, 0));
-                       }
+                       $found = false;
+               }
+
+               // If the hostnames doesn't match or there is some failure, we try to check remotely
+               if (!$found) {
+                       $found = $this->check_user($aCommand[2], $aCommand[1], true);
+               }
+
+               if ($found) {
+                       // The user is okay
+                       $this->writeLog("[exAuth] valid user: ". $sUser);
+                       fwrite(STDOUT, pack("nn", 2, 1));
+               } else {
+                       // The user isn't okay
+                       $this->writeLog("[exAuth] invalid user: ". $sUser);
+                       fwrite(STDOUT, pack("nn", 2, 0));
                }
        }
 
@@ -212,44 +218,51 @@ class exAuth {
         * @param array $aCommand The command array
         */
        private function auth($aCommand) {
-               global $a;
+               $a = get_app();
 
                // check user authentication
                if (sizeof($aCommand) != 4) {
                        $this->writeLog("[exAuth] invalid auth command, data missing");
                        fwrite(STDOUT, pack("nn", 2, 0));
-               } else {
-                       // We now check if the password match
-                       $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
-                       $this->writeDebugLog("[debug] doing auth for ".$sUser."@".$aCommand[2]);
-
-                       // If the hostnames doesn't match, we try to authenticate remotely
-                       if ($a->get_hostname() != $aCommand[2])
-                               $Error = !$this->check_credentials($aCommand[2], $aCommand[1], $aCommand[3], true);
-                       else {
-                               $sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
-                               $this->writeDebugLog("[debug] using query ". $sQuery);
-                               if ($oResult = q($sQuery)) {
-                                       $uid = $oResult[0]["uid"];
-                                       $Error = ($oResult[0]["password"] != hash('whirlpool',$aCommand[3]));
-                               } else {
-                                       $this->writeLog("[MySQL] invalid query: ". $sQuery);
-                                       $Error = true;
-                                       $uid = -1;
-                               }
-                               if ($Error) {
-                                       $oConfig = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = 'xmpp' AND `k`='password' LIMIT 1;", intval($uid));
-                                       $this->writeLog("[exAuth] got password ".$oConfig[0]["v"]);
-                                       $Error = ($aCommand[3] != $oConfig[0]["v"]);
-                               }
+                       return;
+               }
+
+               // We now check if the password match
+               $sUser = str_replace(array("%20", "(a)"), array(" ", "@"), $aCommand[1]);
+               $this->writeDebugLog("[debug] doing auth for ".$sUser."@".$aCommand[2]);
+
+               // Does the hostname match? So we try directly
+               if ($a->get_hostname() == $aCommand[2]) {
+                       $sQuery = "SELECT `uid`, `password` FROM `user` WHERE `nickname`='".dbesc($sUser)."'";
+                       $this->writeDebugLog("[debug] using query ". $sQuery);
+                       if ($oResult = q($sQuery)) {
+                               $uid = $oResult[0]["uid"];
+                               $Error = ($oResult[0]["password"] != hash('whirlpool',$aCommand[3]));
+                       } else {
+                               $this->writeLog("[MySQL] invalid query: ". $sQuery);
+                               $Error = true;
+                               $uid = -1;
                        }
                        if ($Error) {
-                               $this->writeLog("[exAuth] authentification failed for user ".$sUser."@". $aCommand[2]);
-                               fwrite(STDOUT, pack("nn", 2, 0));
-                       } else {
-                               $this->writeLog("[exAuth] authentificated user ".$sUser."@".$aCommand[2]);
-                               fwrite(STDOUT, pack("nn", 2, 1));
+                               $oConfig = q("SELECT `v` FROM `pconfig` WHERE `uid` = %d AND `cat` = 'xmpp' AND `k`='password' LIMIT 1;", intval($uid));
+                               $this->writeLog("[exAuth] got password ".$oConfig[0]["v"]);
+                               $Error = ($aCommand[3] != $oConfig[0]["v"]);
                        }
+               } else {
+                       $Error = true;
+               }
+
+               // If the hostnames doesn't match or there is some failure, we try to check remotely
+               if ($Error) {
+                       $Error = !$this->check_credentials($aCommand[2], $aCommand[1], $aCommand[3], true);
+               }
+
+               if ($Error) {
+                       $this->writeLog("[exAuth] authentification failed for user ".$sUser."@". $aCommand[2]);
+                       fwrite(STDOUT, pack("nn", 2, 0));
+               } else {
+                       $this->writeLog("[exAuth] authentificated user ".$sUser."@".$aCommand[2]);
+                       fwrite(STDOUT, pack("nn", 2, 1));
                }
        }
 
@@ -318,4 +331,3 @@ class exAuth {
                        fclose($this->rLogFile);
        }
 }
-?>