/** * Deletes row in entity table by primary key * * @param string|array $primary * @return DeleteResult */ public static function delete($primary) { // check primary static::normalizePrimary($primary); static::validatePrimary($primary); $entity = static::getEntity(); $result = new DeleteResult(); //event before delete $event = new DataManagerEvent($entity, "OnBeforeDelete", array("id" => $primary)); $event->send(); if ($event->getErrors($result)) { return $result; } //event on delete $event = new DataManagerEvent($entity, "OnDelete", array("id" => $primary)); $event->send(); // delete $connection = Main\Application::getDbConnection(); $helper = $connection->getSqlHelper(); $tableName = $entity->getDBTableName(); $id = array(); foreach ($primary as $k => $v) { $id[] = $k . " = '" . $helper->forSql($v) . "'"; } $where = implode(' AND ', $id); $sql = "DELETE FROM " . $tableName . " WHERE " . $where; $connection->queryExecute($sql); //event after delete $event = new DataManagerEvent($entity, "OnAfterDelete", array("id" => $primary)); $event->send(); // event POST return $result; }
public static function getActiveUsersCount() { $sql = "SELECT COUNT(ID) " . "FROM b_user " . "WHERE ACTIVE = 'Y' " . " AND LAST_LOGIN IS NOT NULL"; if (ModuleManager::isModuleInstalled("intranet")) { $sql = "SELECT COUNT(U.ID) " . "FROM b_user U " . "WHERE U.ACTIVE = 'Y' " . " AND U.LAST_LOGIN IS NOT NULL " . " AND EXISTS(" . " SELECT 'x' " . " FROM b_utm_user UF, b_user_field F " . " WHERE F.ENTITY_ID = 'USER' " . " AND F.FIELD_NAME = 'UF_DEPARTMENT' " . " AND UF.FIELD_ID = F.ID " . " AND UF.VALUE_ID = U.ID " . " AND UF.VALUE_INT IS NOT NULL " . " AND UF.VALUE_INT <> 0" . " )"; } $connection = Application::getDbConnection(); return $connection->queryScalar($sql); }
public static function getByDomainAndPath($domain, $path) { $connection = Application::getDbConnection(); $helper = $connection->getSqlHelper(); $domainForSql = $helper->forSql($domain, 255); $pathForSql = $helper->forSql($path); $sql = "\n\t\t\tSELECT L.*, L.LID as ID\n\t\t\tFROM b_lang L\n\t\t\t\tLEFT JOIN b_lang_domain LD ON L.LID = LD.LID AND '" . $domainForSql . "' LIKE CONCAT('%', LD.DOMAIN)\n\t\t\tWHERE ('" . $pathForSql . "' LIKE CONCAT(L.DIR, '%') OR LD.LID IS NOT NULL)\n\t\t\t\tAND L.ACTIVE = 'Y'\n\t\t\tORDER BY\n\t\t\t\tIF((L.DOMAIN_LIMITED = 'Y' AND LD.LID IS NOT NULL) OR L.DOMAIN_LIMITED <> 'Y',\n\t\t\t\t\tIF('" . $pathForSql . "' LIKE CONCAT(L.DIR, '%'), 3, 1),\n\t\t\t\t\tIF('" . $pathForSql . "' LIKE CONCAT(L.DIR, '%'), 2, 0)\n\t\t\t\t) DESC,\n\t\t\t\tLENGTH(L.DIR) DESC,\n\t\t\t\tL.DOMAIN_LIMITED DESC,\n\t\t\t\tSORT,\n\t\t\t\tLENGTH(LD.DOMAIN) DESC\n\t\t"; $siteList = $connection->query($sql); return $siteList->fetch(); }
public static function deleteBySiteId($siteId) { if (empty($siteId)) { throw new \Bitrix\Main\ArgumentNullException("siteId"); } $result = new \Bitrix\Main\Entity\DeleteResult(); // event PRE // delete $connection = \Bitrix\Main\Application::getDbConnection(); $helper = $connection->getSqlHelper(); $tableName = static::getEntity()->getDBTableName(); $sql = "DELETE FROM " . $tableName . " WHERE SITE_ID = '" . $helper->forSql($siteId) . "'"; $connection->queryExecute($sql); // event POST return $result; }
public static function getCurrentTemplateId($siteId) { $cacheFlags = Config\Configuration::getValue("cache_flags"); $ttl = isset($cacheFlags["site_template"]) ? $cacheFlags["site_template"] : 0; $connection = Application::getDbConnection(); $sqlHelper = $connection->getSqlHelper(); $field = $connection->getType() === "mysql" ? "`CONDITION`" : "CONDITION"; $path2templates = IO\Path::combine(Application::getDocumentRoot(), Application::getPersonalRoot(), "templates"); if ($ttl === false) { $sql = "\n\t\t\t\tSELECT " . $field . ", TEMPLATE\n\t\t\t\tFROM b_site_template\n\t\t\t\tWHERE SITE_ID = '" . $sqlHelper->forSql($siteId) . "'\n\t\t\t\tORDER BY IF(LENGTH(" . $field . ") > 0, 1, 2), SORT\n\t\t\t\t"; $recordset = $connection->query($sql); while ($record = $recordset->fetch()) { $condition = trim($record["CONDITION"]); if ($condition != '' && !@eval("return " . $condition . ";")) { continue; } if (IO\Directory::isDirectoryExists($path2templates . "/" . $record["TEMPLATE"])) { return $record["TEMPLATE"]; } } } else { $managedCache = Application::getInstance()->getManagedCache(); if ($managedCache->read($ttl, "b_site_template")) { $arSiteTemplateBySite = $managedCache->get("b_site_template"); } else { $arSiteTemplateBySite = array(); $sql = "\n\t\t\t\t\tSELECT " . $field . ", TEMPLATE, SITE_ID\n\t\t\t\t\tFROM b_site_template\n\t\t\t\t\tWHERE SITE_ID = '" . $sqlHelper->forSql($siteId) . "'\n\t\t\t\t\tORDER BY SITE_ID, IF(LENGTH(" . $field . ") > 0, 1, 2), SORT\n\t\t\t\t\t"; $recordset = $connection->query($sql); while ($record = $recordset->fetch()) { $arSiteTemplateBySite[$record['SITE_ID']][] = $record; } $managedCache->set("b_site_template", $arSiteTemplateBySite); } if (is_array($arSiteTemplateBySite[$siteId])) { foreach ($arSiteTemplateBySite[$siteId] as $record) { $condition = trim($record["CONDITION"]); if ($condition != '' && !@eval("return " . $condition . ";")) { continue; } if (IO\Directory::isDirectoryExists($path2templates . "/" . $record["TEMPLATE"])) { return $record["TEMPLATE"]; } } } } return ".default"; }
protected function initializeSite() { $context = $this->application->getContext(); $request = $context->getRequest(); $currentDirectory = $request->getRequestedPageDirectory(); $currentHost = ""; $currentHostScheme = ""; if ($request instanceof IHttpRequest) { /** @var $request \Bitrix\Main\HttpRequest */ $currentHost = $request->getHttpHost(); $currentHostScheme = $request->isHttps() ? "https://" : "http://"; } $url = new Web\Uri($currentHostScheme . $currentHost, Web\UriType::ABSOLUTE); $currentDomain = $url->parse(Web\UriPart::HOST); $currentDomain = trim($currentDomain, "\t\r\n ."); $connection = Application::getDbConnection(); $helper = $connection->getSqlHelper(); $sql = "\n\t\t\tSELECT L.*, L.LID as ID, L.LID as SITE_ID\n\t\t\tFROM b_lang L\n\t\t\t\tLEFT JOIN b_lang_domain LD ON L.LID=LD.LID AND '" . $helper->forSql($currentDomain, 255) . "' LIKE CONCAT('%', LD.DOMAIN)\n\t\t\tWHERE ('" . $helper->forSql($currentDirectory) . "' LIKE CONCAT(L.DIR, '%') OR LD.LID IS NOT NULL)\n\t\t\t\tAND L.ACTIVE='Y'\n\t\t\tORDER BY\n\t\t\t\tIF((L.DOMAIN_LIMITED='Y' AND LD.LID IS NOT NULL) OR L.DOMAIN_LIMITED<>'Y',\n\t\t\t\t\tIF('" . $helper->forSql($currentDomain) . "' LIKE CONCAT(L.DIR, '%'), 3, 1),\n\t\t\t\t\tIF('" . $helper->forSql($currentDirectory) . "' LIKE CONCAT(L.DIR, '%'), 2, 0)\n\t\t\t\t) DESC,\n\t\t\t\tLENGTH(L.DIR) DESC,\n\t\t\t\tL.DOMAIN_LIMITED DESC,\n\t\t\t\tSORT,\n\t\t\t\tLENGTH(LD.DOMAIN) DESC\n\t\t"; //get site by path and domain $siteList = $connection->query($sql); $site = $siteList->fetch(); //get site by default sorting if ($site === false) { $sql = "\n\t\t\t\tSELECT L.*, L.LID as ID, L.LID as SITE_ID\n\t\t\t\tFROM b_lang L\n\t\t\t\tWHERE L.ACTIVE='Y'\n\t\t\t\tORDER BY L.DEF DESC, L.SORT\n\t\t\t"; $siteList = $connection->query($sql); $site = $siteList->fetch(); } if ($site !== false) { $culture = Context\Culture::wakeUp($site["CULTURE_ID"]); if ($culture === null) { $culture = new Context\Culture(); } $this->site = new Context\Site($site); $this->site->setCulture($culture); } else { throw new SystemException("Site not found."); } }
public static function delete($moduleId, $name = "", $siteId = "") { $cacheTtl = self::getCacheTtl(); if ($cacheTtl !== false) { $cache = \Bitrix\Main\Application::getInstance()->getManagedCache(); $cache->clean("b_option"); } $con = \Bitrix\Main\Application::getDbConnection(); $sqlHelper = $con->getSqlHelper(); $strSqlWhere = ""; if ($name != "") { $strSqlWhere .= " AND NAME = '" . $sqlHelper->forSql($name) . "' "; } if ($siteId != "") { $strSqlWhere .= " AND SITE_ID = '" . $sqlHelper->forSql($siteId) . "' "; } if ($moduleId == "main") { $con->queryExecute("DELETE FROM b_option " . "WHERE MODULE_ID = 'main' " . " AND NAME NOT LIKE '~%' " . " AND NAME <> 'crc_code' " . " AND NAME <> 'admin_passwordh' " . " AND NAME <> 'server_uniq_id' " . " AND NAME <> 'PARAM_MAX_SITES' " . " AND NAME <> 'PARAM_MAX_USERS' " . $strSqlWhere); } else { $con->queryExecute("DELETE FROM b_option " . "WHERE MODULE_ID = '" . $sqlHelper->forSql($moduleId) . "' " . " AND NAME <> '~bsm_stop_date' " . $strSqlWhere); } if ($siteId != "") { if ($name == "") { unset(self::$options[$siteId][$moduleId]); } else { unset(self::$options[$siteId][$moduleId][$name]); } } else { $arSites = array_keys(self::$options); foreach ($arSites as $s) { if ($name == "") { unset(self::$options[$s][$moduleId]); } else { unset(self::$options[$s][$moduleId][$name]); } } } }
function delayedDelete($count = 1, $level = 1) { $etime = time() + 2; for ($i = 0; $i < $count; $i++) { self::deleteOneDir($etime); if (time() > $etime) { break; } } $con = \Bitrix\Main\Application::getDbConnection(); //try to adjust cache cleanup speed to cache cleanups $rs = $con->query("SELECT * from b_cache_tag WHERE TAG='**'"); if ($ar = $rs->fetch()) { $last_count = intval($ar["RELATIVE_PATH"]); } else { $last_count = 0; } $bWasStatRecFound = is_array($ar); $this_count = $con->queryScalar("SELECT count(1) CNT from b_cache_tag WHERE TAG='*'"); $delta = $this_count - $last_count; if ($delta > 0) { $count = intval($this_count / 3600) + 1; } elseif ($count < 1) { $count = 1; } if ($bWasStatRecFound) { if ($last_count != $this_count) { $con->queryExecute("UPDATE b_cache_tag SET RELATIVE_PATH='" . $this_count . "' WHERE TAG='**'"); } } else { $con->queryExecute("INSERT INTO b_cache_tag (TAG, RELATIVE_PATH) VALUES ('**', '" . $this_count . "')"); } if ($this_count > 0) { return "CacheEngineFiles::delayedDelete(" . $count . ");"; } else { return ""; } }
protected function loadUserSecurityPolicy() { $this->policy = static::$defaultGroupPolicy; if ($this->policy["SESSION_TIMEOUT"] <= 0) { $this->policy["SESSION_TIMEOUT"] = ini_get("session.gc_maxlifetime") / 60; } $connection = Main\Application::getDbConnection(); $sqlHelper = $connection->getSqlHelper(); $sql = "SELECT G.SECURITY_POLICY " . "FROM b_group G " . "WHERE G.ID = 2 "; if ($this->isAuthenticated) { $sql .= "UNION " . "SELECT G.SECURITY_POLICY " . "FROM b_group G " . " INNER JOIN b_user_group UG ON (G.ID = UG.GROUP_ID) " . "WHERE UG.USER_ID = " . intval($this->userId) . " " . "\tAND ((UG.DATE_ACTIVE_FROM IS NULL) OR (UG.DATE_ACTIVE_FROM <= " . $sqlHelper->getCurrentDateTimeFunction() . ")) " . "\tAND ((UG.DATE_ACTIVE_TO IS NULL) OR (UG.DATE_ACTIVE_TO >= " . $sqlHelper->getCurrentDateTimeFunction() . ")) "; } $recordset = $connection->query($sql); while ($record = $recordset->fetch()) { if (!empty($record["SECURITY_POLICY"])) { $groupPolicy = unserialize($record["SECURITY_POLICY"]); } else { continue; } if (!is_array($groupPolicy)) { continue; } foreach ($groupPolicy as $key => $val) { switch ($key) { case "STORE_IP_MASK": case "SESSION_IP_MASK": if ($this->policy[$key] < $val) { $this->policy[$key] = $val; } break; case "SESSION_TIMEOUT": if ($this->policy[$key] <= 0 || $this->policy[$key] > $val) { $this->policy[$key] = $val; } break; case "PASSWORD_LENGTH": if ($this->policy[$key] <= 0 || $this->policy[$key] < $val) { $this->policy[$key] = $val; } break; case "PASSWORD_UPPERCASE": case "PASSWORD_LOWERCASE": case "PASSWORD_DIGITS": case "PASSWORD_PUNCTUATION": if ($val === "Y") { $this->policy[$key] = "Y"; } break; case "LOGIN_ATTEMPTS": if ($val > 0 && ($this->policy[$key] <= 0 || $this->policy[$key] > $val)) { $this->policy[$key] = $val; } break; default: if ($this->policy[$key] > $val) { $this->policy[$key] = $val; } } } } if ($this->policy["PASSWORD_LENGTH"] === false) { $this->policy["PASSWORD_LENGTH"] = 6; } }
public static function setAuthentication(CurrentUser $user, $isPersistent = false) { /** @var $context \Bitrix\Main\HttpContext */ $context = \Bitrix\Main\Application::getInstance()->getContext(); $context->setUser($user); static::copyToSession($user); /** @var $response \Bitrix\Main\HttpResponse */ $response = $context->getResponse(); if (!$user->isAuthenticated()) { $cookie = new \Bitrix\Main\Web\Cookie("UIDH", "", time() - 3600); $response->addCookie($cookie); return; } $connection = \Bitrix\Main\Application::getDbConnection(); $sqlHelper = $connection->getSqlHelper(); $connection->queryExecute("UPDATE b_user SET " . " STORED_HASH = NULL, " . " LAST_LOGIN = "******", " . " TIMESTAMP_X = TIMESTAMP_X, " . " LOGIN_ATTEMPTS = 0, " . " TIME_ZONE_OFFSET = " . \CTimeZone::getOffset() . " " . "WHERE ID = " . $user->getUserId() . " "); $cookie = new \Bitrix\Main\Web\Cookie("LOGIN", $user->getLogin(), time() + 60 * 60 * 24 * 30 * 60); $cookie->setSpread(\Bitrix\Main\Config\Option::get("main", "auth_multisite", "N") == "Y" ? \Bitrix\Main\Web\Cookie::SPREAD_SITES : \Bitrix\Main\Web\Cookie::SPREAD_DOMAIN); $response->addCookie($cookie); if ($isPersistent || \Bitrix\Main\Config\Option::get("main", "auth_multisite", "N") == "Y") { $hash = $user->getSessionHash(); /** @var $request \Bitrix\Main\HttpRequest */ $request = $context->getRequest(); if ($isPersistent) { $cookie = new \Bitrix\Main\Web\Cookie("UIDH", $hash, time() + 60 * 60 * 24 * 30 * 60); } else { $cookie = new \Bitrix\Main\Web\Cookie("UIDH", $hash, 0); } $cookie->setSecure(\Bitrix\Main\Config\Option::get("main", "use_secure_password_cookies", "N") == "Y" && $request->isHttps()); $response->addCookie($cookie); $storedId = static::getStoredHashId($user, $hash); if ($storedId) { $connection->queryExecute("UPDATE b_user_stored_auth SET " . "\tLAST_AUTH = " . $sqlHelper->getCurrentDateTimeFunction() . ", " . "\t" . ($user->getAuthType() === static::AUTHENTICATED_BY_HASH ? "" : "TEMP_HASH='" . ($isPersistent ? "N" : "Y") . "', ") . " " . "\tIP_ADDR = '" . sprintf("%u", ip2long($request->getRemoteAddress())) . "' " . "WHERE ID = " . intval($storedId)); } else { $sqlTmp1 = ""; $sqlTmp2 = ""; if ($connection->getType() === "oracle") { $storedId = $connection->getIdentity("sq_b_user_stored_auth"); $sqlTmp1 = "ID, "; $sqlTmp2 = intval($storedId) . ", "; } $sql = "INSERT INTO b_user_stored_auth (" . $sqlTmp1 . "USER_ID, DATE_REG, LAST_AUTH, TEMP_HASH, " . " IP_ADDR, STORED_HASH) " . "VALUES (" . $sqlTmp2 . intval($user->getUserId()) . ", " . $sqlHelper->getCurrentDateTimeFunction() . ", " . " " . $sqlHelper->getCurrentDateTimeFunction() . ", '" . ($isPersistent ? "N" : "Y") . "', " . " '" . $sqlHelper->forSql(sprintf("%u", ip2long($request->getRemoteAddress()))) . "', " . " '" . $sqlHelper->forSql($hash) . "')"; $connection->queryExecute($sql); if ($connection->getType() !== "oracle") { $storedId = $connection->getIdentity(); } } $user->setStoredAuthId($storedId); } $event = new Main\Event("main", "OnUserLogin", array("USER" => $user)); $event->send(); if (\Bitrix\Main\Config\Option::get("main", "event_log_login_success", "N") === "Y") { \CEventLog::log("SECURITY", "USER_AUTHORIZE", "main", $user->getUserId()); } }
public function clearByTag($tag) { $con = \Bitrix\Main\Application::getDbConnection(); $sqlHelper = $con->getSqlHelper(); if ($tag === true) { $sqlWhere = " WHERE TAG <> '*'"; } else { $sqlWhere = " WHERE TAG = '" . $sqlHelper->forSql($tag) . "'"; } $arDirs = array(); $rs = $con->query("SELECT * FROM b_cache_tag" . $sqlWhere); while ($ar = $rs->fetch()) { $arDirs[$ar["RELATIVE_PATH"]] = $ar; } $con->queryExecute("DELETE FROM b_cache_tag" . $sqlWhere); $obCache = Cache::createInstance(); foreach ($arDirs as $path => $ar) { $con->queryExecute("\n\t\t\t\tDELETE FROM b_cache_tag\n\t\t\t\tWHERE SITE_ID = '" . $sqlHelper->forSql($ar["SITE_ID"]) . "'\n\t\t\t\tAND CACHE_SALT = '" . $sqlHelper->forSql($ar["CACHE_SALT"]) . "'\n\t\t\t\tAND RELATIVE_PATH = '" . $sqlHelper->forSql($ar["RELATIVE_PATH"]) . "'\n\t\t\t"); if (preg_match("/^managed:(.+)\$/", $path, $match)) { $this->cleanDir($match[1]); } else { $obCache->cleanDir($path); } unset($this->dbCacheTags[$path]); } }
protected function getCookieDomain() { static $bCache = false; static $cache = false; if ($bCache) { return $cache; } $context = \Bitrix\Main\Application::getInstance()->getContext(); $server = $context->getServer(); $cacheFlags = \Bitrix\Main\Config\Configuration::getValue("cache_flags"); $cacheTtl = isset($cacheFlags["site_domain"]) ? $cacheFlags["site_domain"] : 0; if ($cacheTtl === false) { $connection = \Bitrix\Main\Application::getDbConnection(); $sqlHelper = $connection->getSqlHelper(); $sql = "SELECT DOMAIN " . "FROM b_lang_domain " . "WHERE '" . $sqlHelper->forSql('.' . $server->getHttpHost()) . "' like " . $sqlHelper->getConcatFunction("'%.'", "DOMAIN") . " " . "ORDER BY " . $sqlHelper->getLengthFunction("DOMAIN") . " "; $recordset = $connection->query($sql); if ($record = $recordset->fetch()) { $cache = $record['DOMAIN']; } } else { $managedCache = \Bitrix\Main\Application::getInstance()->getManagedCache(); if ($managedCache->read($cacheTtl, "b_lang_domain", "b_lang_domain")) { $arLangDomain = $managedCache->get("b_lang_domain"); } else { $arLangDomain = array("DOMAIN" => array(), "LID" => array()); $connection = \Bitrix\Main\Application::getDbConnection(); $sqlHelper = $connection->getSqlHelper(); $recordset = $connection->query("SELECT * " . "FROM b_lang_domain " . "ORDER BY " . $sqlHelper->getLengthFunction("DOMAIN")); while ($record = $recordset->fetch()) { $arLangDomain["DOMAIN"][] = $record; $arLangDomain["LID"][$record["LID"]][] = $record; } $managedCache->set("b_lang_domain", $arLangDomain); } foreach ($arLangDomain["DOMAIN"] as $domain) { if (strcasecmp(substr('.' . $server->getHttpHost(), -(strlen($domain['DOMAIN']) + 1)), "." . $domain['DOMAIN']) == 0) { $cache = $domain['DOMAIN']; break; } } } $bCache = true; return $cache; }