Example #1
0
function sql_connect_anyslave()
{
    global $db, $opt, $login;
    if ($db['dblink_slave'] !== false) {
        return;
    }
    $nMaxTimeDiff = $opt['db']['slave']['max_behind'];
    if ($login->userid != 0) {
        $nMaxTimeDiff = sql_value("SELECT TIMESTAMP(NOW())-TIMESTAMP(`datExclude`)\n            FROM `sys_repl_exclude`\n            WHERE `user_id`='&1'", $opt['db']['slave']['max_behind'], $login->userid);
        if ($nMaxTimeDiff > $opt['db']['slave']['max_behind']) {
            $nMaxTimeDiff = $opt['db']['slave']['max_behind'];
        }
    }
    $id = sqlf_value("SELECT `id`, `weight`*RAND() AS `w`\n        FROM `sys_repl_slaves`\n        WHERE `active`= 1 \n        AND `online`= 1 \n        AND (TIMESTAMP(NOW())-TIMESTAMP(`last_check`)+`time_diff`<'&1')\n        ORDER BY `w` DESC LIMIT 1", -1, $nMaxTimeDiff);
    sql_connect_slave($id);
}
 public function importFromTable($table, $fname = 'name', $fid = 'trans_id')
 {
     $rs = sqlf("SELECT `&1`.`&2`\n            FROM `&1`\n            LEFT JOIN `sys_trans`\n                ON `&1`.`&3`=`sys_trans`.`id`\n                AND `&1`.`&2`=`sys_trans`.`text`", $table, $fname, $fid);
     while ($r = sql_fetch_array($rs)) {
         if ($r[$fname] == '') {
             continue;
         }
         $lastId = sqlf_value("SELECT `id` FROM `sys_trans` WHERE `text`='&1'", 0, $r[$fname]);
         if ($lastId == 0) {
             sqlf("INSERT INTO `sys_trans` (`text`) VALUES ('&1')", $r[$fname]);
             $lastId = sql_insert_id();
         }
         sqlf("INSERT IGNORE INTO `sys_trans_ref` (`trans_id`, `resource_name`, `line`) VALUES ('&1', '&2', 0)", $lastId, 'table:' . $table . ';field=' . $fname);
     }
     sql_free_result($rs);
     sqlf("UPDATE `&1` SET `&2`=0", $table, $fid);
     sqlf("UPDATE `&1`, `sys_trans`\n            SET `&1`.`&3`=`sys_trans`.`id`\n            WHERE `&1`.`&2`=`sys_trans`.`text`", $table, $fname, $fid);
 }
Example #3
0
 public function checkLoginsCount()
 {
     global $opt;
     // cleanup old entries
     // (execute only every 50 search calls)
     if (rand(1, 50) == 1) {
         sqlf("DELETE FROM `sys_logins` WHERE `date_created`<'&1'", date('Y-m-d H:i:s', time() - 3600));
     }
     // check the number of logins in the last hour ...
     $logins_count = sqlf_value("\n            SELECT COUNT(*) `count`\n            FROM `sys_logins`\n            WHERE `remote_addr`='&1'\n            AND `date_created`>'&2'", 0, $_SERVER['REMOTE_ADDR'], date('Y-m-d H:i:s', time() - 3600));
     if ($logins_count > $opt['page']['max_logins_per_hour']) {
         return false;
     } else {
         return true;
     }
 }
 function pGetMenuSublevel($id)
 {
     $parent = sqlf_value("SELECT `parent` FROM `sys_menu` WHERE `id`='&1'", 0, $id);
     if ($parent != 0) {
         return $this->pGetMenuSublevel($parent) + 1;
     }
     return 0;
 }
Example #5
0
 function try_login_md5($user, $pwmd5, $permanent)
 {
     global $opt;
     $this->pClear();
     if ($user == '' || $pwmd5 == '') {
         return LOGIN_EMPTY_USERPASSWORD;
     }
     if ($this->checkLoginsCount() == false) {
         return LOGIN_TOOMUCHLOGINS;
     }
     // delete old sessions
     $min_lastlogin_permanent = date('Y-m-d H:i:s', time() - LOGIN_TIME_PERMANENT);
     sqlf("DELETE FROM `sys_sessions` WHERE `last_login`<'&1'", $min_lastlogin_permanent);
     // compare $user with email and username, if both matches use email
     $rsUser = sqlf("SELECT `user_id`, `username`, 2 AS `prio`, `is_active_flag`, `permanent_login_flag`, `admin` FROM `user` WHERE `username`='&1' AND `password`='&2' UNION\n\t\t                SELECT `user_id`, `username`, 1 AS `prio`, `is_active_flag`, `permanent_login_flag`, `admin` FROM `user` WHERE `email`='&1' AND `password`='&2' ORDER BY `prio` ASC LIMIT 1", $user, $pwmd5);
     $rUser = sql_fetch_assoc($rsUser);
     sql_free_result($rsUser);
     if ($permanent == null) {
         $permanent = $rUser['permanent_login_flag'] == 1;
     }
     if ($rUser) {
         // ok, there is a valid login
         if ($rUser['is_active_flag'] != 0) {
             // begin session
             $uuid = sqlf_value('SELECT UUID()', '');
             sqlf("INSERT INTO `sys_sessions` (`uuid`, `user_id`, `permanent`, `last_login`) VALUES ('&1', '&2', '&3', NOW())", $uuid, $rUser['user_id'], $permanent != false ? 1 : 0);
             $this->userid = $rUser['user_id'];
             $this->username = $rUser['username'];
             $this->permanent = $permanent;
             $this->lastlogin = date('Y-m-d H:i:s');
             $this->sessionid = $uuid;
             $this->admin = $rUser['admin'];
             $this->verified = true;
             $retval = LOGIN_OK;
         } else {
             $retval = LOGIN_USERNOTACTIVE;
         }
     } else {
         // sorry, bad login
         $retval = LOGIN_BADUSERPW;
     }
     sqlf("INSERT INTO `sys_logins` (`remote_addr`, `success`) VALUES ('&1', '&2')", $_SERVER['REMOTE_ADDR'], $rUser === false ? 0 : 1);
     // store to cookie
     $this->pStoreCookie();
     return $retval;
 }