PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/statistic/install/index.php

https://gitlab.com/alexprowars/bitrix
PHP | 400 lines | 346 code | 49 blank | 5 comment | 44 complexity | d40b59d4264f7404252f6bfee85cbfbc MD5 | raw file
  1. <?
  2. IncludeModuleLangFile(__FILE__);
  3. if(class_exists("statistic")) return;
  4. Class statistic extends CModule
  5. {
  6. var $MODULE_ID = "statistic";
  7. var $MODULE_VERSION;
  8. var $MODULE_VERSION_DATE;
  9. var $MODULE_NAME;
  10. var $MODULE_DESCRIPTION;
  11. var $MODULE_CSS;
  12. var $MODULE_GROUP_RIGHTS = "Y";
  13. var $errors;
  14. public function __construct()
  15. {
  16. $arModuleVersion = array();
  17. include(__DIR__.'/version.php');
  18. if (is_array($arModuleVersion) && array_key_exists("VERSION", $arModuleVersion))
  19. {
  20. $this->MODULE_VERSION = $arModuleVersion["VERSION"];
  21. $this->MODULE_VERSION_DATE = $arModuleVersion["VERSION_DATE"];
  22. }
  23. else
  24. {
  25. $this->MODULE_VERSION = $STATISTIC_VERSION;
  26. $this->MODULE_VERSION_DATE = $STATISTIC_VERSION_DATE;
  27. }
  28. $this->MODULE_NAME = GetMessage("STAT_MODULE_NAME");
  29. $this->MODULE_DESCRIPTION = GetMessage("STAT_MODULE_DESCRIPTION");
  30. $this->MODULE_CSS = "/bitrix/modules/statistic/statistic.css";
  31. }
  32. function InstallDB($arParams = array())
  33. {
  34. global $DBType, $APPLICATION;
  35. $node_id = $arParams["DATABASE"] <> ''? intval($arParams["DATABASE"]): false;
  36. if($node_id !== false)
  37. $DB = $GLOBALS["DB"]->GetDBNodeConnection($node_id);
  38. else
  39. $DB = $GLOBALS["DB"];
  40. $this->errors = false;
  41. $arAllErrors = array();
  42. // check if module was deinstalled without table save
  43. $DATE_INSTALL_TABLES = "";
  44. $no_tables = "N";
  45. if(!$DB->Query("SELECT count('x') FROM b_stat_day WHERE 1=0", true))
  46. {
  47. // last installation date have to be current
  48. $DATE_INSTALL_TABLES = date("d.m.Y H:i:s",time());
  49. $no_tables = "Y";
  50. }
  51. if($no_tables == "Y")
  52. {
  53. $this->errors = $DB->RunSQLBatch($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/db/".mb_strtolower($DB->type)."/install.sql");
  54. }
  55. if($this->errors !== false)
  56. {
  57. $APPLICATION->ThrowException(implode("<br>", $this->errors));
  58. return false;
  59. }
  60. RegisterModule("statistic");
  61. RegisterModuleDependences("main", "OnPageStart", "statistic", "CStopList", "Check", "100");
  62. RegisterModuleDependences("main", "OnBeforeProlog", "statistic", "CStatistics", "Keep", "100");
  63. RegisterModuleDependences("main", "OnEpilog", "statistic", "CStatistics", "Set404", "100");
  64. RegisterModuleDependences("main", "OnBeforeProlog", "statistic", "CStatistics", "StartBuffer", "1000");
  65. RegisterModuleDependences("main", "OnEndBufferContent", "statistic", "CStatistics", "EndBuffer", "900");
  66. RegisterModuleDependences("main", "OnEventLogGetAuditTypes", "statistic", "CStatistics", "GetAuditTypes", 10);
  67. RegisterModuleDependences("statistic", "OnCityLookup", "statistic", "CCityLookup_geoip_mod", "OnCityLookup", "100");
  68. RegisterModuleDependences("statistic", "OnCityLookup", "statistic", "CCityLookup_geoip_extension", "OnCityLookup", "200");
  69. RegisterModuleDependences("statistic", "OnCityLookup", "statistic", "CCityLookup_geoip_pure", "OnCityLookup", "300");
  70. RegisterModuleDependences("statistic", "OnCityLookup", "statistic", "CCityLookup_stat_table", "OnCityLookup", "400");
  71. RegisterModuleDependences("cluster", "OnGetTableList", "statistic", "statistic", "OnGetTableList");
  72. if ($DATE_INSTALL_TABLES <> '')
  73. COption::SetOptionString("main", "INSTALL_STATISTIC_TABLES", $DATE_INSTALL_TABLES, "Date of installation of statistics module tables");
  74. if($node_id !== false)
  75. {
  76. COption::SetOptionString("statistic", "dbnode_id", $node_id);
  77. if(CModule::IncludeModule('cluster'))
  78. CClusterDBNode::SetOnline($node_id);
  79. }
  80. else
  81. {
  82. COption::SetOptionString("statistic", "dbnode_id", "N");
  83. }
  84. COption::SetOptionString("statistic", "dbnode_status", "ok");
  85. // init counters
  86. if(array_key_exists("allow_initial", $arParams) && ($arParams["allow_initial"] == "Y"))
  87. {
  88. $strSql = "SELECT ID FROM b_stat_day";
  89. $e = $DB->Query($strSql, false, $err_mess.__LINE__);
  90. if (!($er = $e->Fetch()))
  91. {
  92. if (intval($arParams["START_HITS"])>0 || intval($arParams["START_HOSTS"])>0 || intval($arParams["START_GUESTS"])>0)
  93. {
  94. $arFields = Array(
  95. "DATE_STAT" => $DB->GetNowDate(),
  96. "HITS" => intval($arParams["START_HITS"]),
  97. "C_HOSTS" => intval($arParams["START_HOSTS"]),
  98. "GUESTS" => intval($arParams["START_GUESTS"]),
  99. "NEW_GUESTS" => intval($arParams["START_GUESTS"]),
  100. );
  101. $DB->Insert("b_stat_day",$arFields, $err_mess.__LINE__);
  102. }
  103. }
  104. }
  105. $arr = getdate();
  106. $ndate = mktime(0, 1, 0, $arr["mon"], $arr["mday"], $arr["year"]);
  107. CAgent::AddAgent("CStatistics::SetNewDay();","statistic", "Y", 86400, "", "Y", ConvertTimeStamp($ndate+CTimeZone::GetOffset(), "FULL"), 200);
  108. $ndate = mktime(3, 0, 0, $arr["mon"], $arr["mday"], $arr["year"]);
  109. CAgent::AddAgent("CStatistics::CleanUpStatistics_1();","statistic", "Y", 86400, "", "Y", ConvertTimeStamp($ndate+CTimeZone::GetOffset(), "FULL"), 50);
  110. $ndate = mktime(4, 0, 0, $arr["mon"], $arr["mday"], $arr["year"]);
  111. CAgent::AddAgent("CStatistics::CleanUpStatistics_2();","statistic", "Y", 86400, "", "Y", ConvertTimeStamp($ndate+CTimeZone::GetOffset(), "FULL"), 30);
  112. CAgent::AddAgent("CStatistics::CleanUpSessionData();","statistic","N",7200);
  113. CAgent::AddAgent("CStatistics::CleanUpPathCache();","statistic", "N", 3600);
  114. CAgent::RemoveAgent("SendDailyStatistics();","statistic");
  115. if(mb_strpos($_SERVER["SERVER_SOFTWARE"], "(Win32)") <= 0)
  116. {
  117. $ndate = mktime(9, 0, 0, $arr["mon"], $arr["mday"], $arr["year"]);
  118. CAgent::AddAgent("SendDailyStatistics();", "statistic", "Y", 86400, "", "Y", ConvertTimeStamp($ndate+CTimeZone::GetOffset(), "FULL"), 25);
  119. }
  120. if($no_tables=="Y")
  121. {
  122. $arAllErrors[] = $DB->RunSQLBatch($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/db/".mb_strtolower($DB->type)."/searchers.sql");
  123. $arAllErrors[] = $DB->RunSQLBatch($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/db/".mb_strtolower($DB->type)."/browsers.sql");
  124. $arAllErrors[] = $DB->RunSQLBatch($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/db/".mb_strtolower($DB->type)."/adv.sql");
  125. }
  126. // ip-to-country
  127. require_once($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/ip_tools.php");
  128. i2c_load_countries();
  129. if(!array_key_exists("CREATE_I2C_INDEX", $arParams) || ($arParams["CREATE_I2C_INDEX"] == "Y"))
  130. i2c_create_db($total_reindex, $reindex_success, $step_reindex, $int_prev);
  131. $fname = $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/db/".mb_strtolower($DB->type)."/optimize.sql";
  132. if(file_exists($fname))
  133. {
  134. $arAllErrors[] = $DB->RunSQLBatch($fname);
  135. }
  136. $this->errors = array();
  137. foreach($arAllErrors as $ar)
  138. {
  139. if(is_array($ar))
  140. {
  141. foreach($ar as $strError)
  142. $this->errors[] = $strError;
  143. }
  144. }
  145. if(count($this->errors) < 1)
  146. $this->errors = false;
  147. if($this->errors !== false)
  148. {
  149. $APPLICATION->ThrowException(implode("<br>", $this->errors));
  150. return false;
  151. }
  152. return true;
  153. }
  154. function UnInstallDB($arParams = array())
  155. {
  156. global $DBType, $APPLICATION;
  157. $DB = CDatabase::GetModuleConnection('statistic', true);
  158. $this->errors = false;
  159. if(is_object($DB))
  160. {
  161. if(!array_key_exists("savedata", $arParams) || ($arParams["savedata"] != "Y"))
  162. {
  163. $this->errors = $DB->RunSQLBatch($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/db/".mb_strtolower($DB->type)."/uninstall.sql");
  164. COption::RemoveOption("main","INSTALL_STATISTIC_TABLES");
  165. $db_res = $GLOBALS["DB"]->Query("SELECT ID FROM b_file WHERE MODULE_ID = 'statistic'");
  166. while($arRes = $db_res->Fetch())
  167. CFile::Delete($arRes["ID"]);
  168. }
  169. }
  170. UnRegisterModuleDependences("main", "OnPageStart", "statistic", "CStopList", "Check");
  171. UnRegisterModuleDependences("main", "OnBeforeProlog", "statistic", "CStatistics", "Keep");
  172. UnRegisterModuleDependences("main", "OnEpilog", "statistic", "CStatistics", "Set404");
  173. UnRegisterModuleDependences("main", "OnEventLogGetAuditTypes", "statistic", "CStatistics", "GetAuditTypes");
  174. UnRegisterModuleDependences("main", "OnBeforeProlog", "statistic", "CStatistics", "StartBuffer");
  175. UnRegisterModuleDependences("main", "OnEndBufferContent", "statistic", "CStatistics", "EndBuffer");
  176. UnRegisterModuleDependences("cluster", "OnGetTableList", "statistic", "statistic", "OnGetTableList");
  177. UnRegisterModule("statistic");
  178. COption::SetOptionString("statistic", "dbnode_id", "N");
  179. COption::SetOptionString("statistic", "dbnode_status", "ok");
  180. if($this->errors !== false)
  181. {
  182. $APPLICATION->ThrowException(implode("<br>", $this->errors));
  183. return false;
  184. }
  185. return true;
  186. }
  187. function InstallEvents()
  188. {
  189. global $DB;
  190. $sIn = "'STATISTIC_DAILY_REPORT', 'STATISTIC_ACTIVITY_EXCEEDING'";
  191. $rs = $DB->Query("SELECT count(*) C FROM b_event_type WHERE EVENT_NAME IN (".$sIn.") ", false, "File: ".__FILE__."<br>Line: ".__LINE__);
  192. $ar = $rs->Fetch();
  193. if($ar["C"] <= 0)
  194. {
  195. include($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/events/set_events.php");
  196. }
  197. return true;
  198. }
  199. function UnInstallEvents()
  200. {
  201. global $DB;
  202. $sIn = "'STATISTIC_DAILY_REPORT', 'STATISTIC_ACTIVITY_EXCEEDING'";
  203. $DB->Query("DELETE FROM b_event_message WHERE EVENT_NAME IN (".$sIn.") ", false, "File: ".__FILE__."<br>Line: ".__LINE__);
  204. $DB->Query("DELETE FROM b_event_type WHERE EVENT_NAME IN (".$sIn.") ", false, "File: ".__FILE__."<br>Line: ".__LINE__);
  205. return true;
  206. }
  207. function InstallFiles()
  208. {
  209. if($_ENV["COMPUTERNAME"]!='BX')
  210. {
  211. CopyDirFiles($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/public/bitrix", $_SERVER["DOCUMENT_ROOT"]."/bitrix", true, true);//all from bitrix
  212. CopyDirFiles($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/components/bitrix", $_SERVER["DOCUMENT_ROOT"]."/bitrix/components/bitrix", true, true);
  213. CopyDirFiles($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/gadgets/bitrix", $_SERVER["DOCUMENT_ROOT"]."/bitrix/gadgets/bitrix", true, true);
  214. }
  215. return true;
  216. }
  217. function UnInstallFiles()
  218. {
  219. if($_ENV["COMPUTERNAME"]!='BX')
  220. {
  221. DeleteDirFiles($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/public/bitrix/admin", $_SERVER["DOCUMENT_ROOT"]."/bitrix/admin");
  222. DeleteDirFiles($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/public/bitrix/themes/.default/", $_SERVER["DOCUMENT_ROOT"]."/bitrix/themes/.default");//css
  223. DeleteDirFilesEx("/bitrix/themes/.default/icons/statistic/");//icons
  224. DeleteDirFilesEx("/bitrix/images/statistic/");//images
  225. }
  226. return true;
  227. }
  228. function DoInstall()
  229. {
  230. global $APPLICATION, $step;
  231. $STAT_RIGHT = $APPLICATION->GetGroupRight("statistic");
  232. $step = intval($step);
  233. if($STAT_RIGHT < "W")
  234. return;
  235. if(!CBXFeatures::IsFeatureEditable("Analytics"))
  236. {
  237. $this->errors = array(GetMessage("MAIN_FEATURE_ERROR_EDITABLE"));
  238. $GLOBALS["errors"] = $this->errors;
  239. $APPLICATION->ThrowException(implode("<br>", $this->errors));
  240. $APPLICATION->IncludeAdminFile(GetMessage("STAT_INSTALL_TITLE"), $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/step2.php");
  241. }
  242. elseif($step < 2)
  243. {
  244. $APPLICATION->IncludeAdminFile(GetMessage("STAT_INSTALL_TITLE"), $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/step1.php");
  245. }
  246. elseif($step == 2)
  247. {
  248. $db_install_ok = $this->InstallDB(array(
  249. "allow_initial" => $_REQUEST["allow_initial"],
  250. "START_HITS" => $_REQUEST["START_HITS"],
  251. "START_HOSTS" => $_REQUEST["START_HOSTS"],
  252. "START_GUESTS" => $_REQUEST["START_GUESTS"],
  253. "CREATE_I2C_INDEX" => $_REQUEST["CREATE_I2C_INDEX"],
  254. "DATABASE" => $_REQUEST["DATABASE"],
  255. ));
  256. if($db_install_ok)
  257. {
  258. $this->InstallEvents();
  259. $this->InstallFiles();
  260. CBXFeatures::SetFeatureEnabled("Analytics", true);
  261. }
  262. $GLOBALS["errors"] = $this->errors;
  263. $APPLICATION->IncludeAdminFile(GetMessage("STAT_INSTALL_TITLE"), $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/step2.php");
  264. }
  265. }
  266. function DoUninstall()
  267. {
  268. global $APPLICATION, $step;
  269. $STAT_RIGHT = $APPLICATION->GetGroupRight("statistic");
  270. if ($STAT_RIGHT>="W")
  271. {
  272. $step = intval($step);
  273. if($step < 2)
  274. {
  275. $APPLICATION->IncludeAdminFile(GetMessage("STAT_UNINSTALL_TITLE"), $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/unstep1.php");
  276. }
  277. elseif($step == 2)
  278. {
  279. $this->UnInstallDB(array(
  280. "savedata" => $_REQUEST["savedata"],
  281. ));
  282. //message types and templates
  283. if($_REQUEST["save_templates"] != "Y")
  284. {
  285. $this->UnInstallEvents();
  286. }
  287. $this->UnInstallFiles();
  288. CBXFeatures::SetFeatureEnabled("Analytics", false);
  289. $GLOBALS["errors"] = $this->errors;
  290. $APPLICATION->IncludeAdminFile(GetMessage("STAT_UNINSTALL_TITLE"), $_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/statistic/install/unstep2.php");
  291. }
  292. }
  293. }
  294. function GetModuleRightList()
  295. {
  296. $arr = array(
  297. "reference_id" => array("D","M","R","W"),
  298. "reference" => array(
  299. "[D] ".GetMessage("STAT_DENIED"),
  300. "[M] ".GetMessage("STAT_VIEW_WITHOUT_MONEY"),
  301. "[R] ".GetMessage("STAT_VIEW"),
  302. "[W] ".GetMessage("STAT_ADMIN"))
  303. );
  304. return $arr;
  305. }
  306. public static function OnGetTableList()
  307. {
  308. return array(
  309. "MODULE" => new statistic,
  310. "TABLES" => array(
  311. "b_stat_adv_searcher" =>"ID",
  312. "b_stat_adv" =>"ID",
  313. "b_stat_adv_event" =>"ID",
  314. "b_stat_adv_guest" =>"ID",
  315. "b_stat_adv_page" =>"ID",
  316. "b_stat_day" =>"ID",
  317. "b_stat_day_site" =>"ID",
  318. "b_stat_event" =>"ID",
  319. "b_stat_event_day" =>"ID",
  320. "b_stat_event_list" =>"ID",
  321. "b_stat_guest" =>"ID",
  322. "b_stat_hit" =>"ID",
  323. "b_stat_searcher_hit" =>"ID",
  324. "b_stat_phrase_list" =>"ID",
  325. "b_stat_referer" =>"ID",
  326. "b_stat_referer_list" =>"ID",
  327. "b_stat_searcher" =>"ID",
  328. "b_stat_searcher_params" =>"ID",
  329. "b_stat_session" =>"ID",
  330. "b_stat_page" =>"ID",
  331. "b_stop_list" =>"ID",
  332. "b_stat_browser" =>"ID",
  333. "b_stat_adv_day" =>"ID",
  334. "b_stat_adv_event_day" =>"ID",
  335. "b_stat_searcher_day" =>"ID",
  336. "b_stat_country" =>"ID",
  337. "b_stat_city" =>"ID",
  338. "b_stat_city_day" =>"ID",
  339. "b_stat_city_ip" =>"START_IP",
  340. "b_stat_session_data" =>"ID",
  341. "b_stat_country_day" =>"ID",
  342. "b_stat_path" =>"ID",
  343. "b_stat_path_adv" =>"ID",
  344. "b_stat_path_cache" =>"ID",
  345. "b_stat_page_adv" =>"ID",
  346. "b_stat_ddl" =>"ID",
  347. ),
  348. );
  349. }
  350. }
  351. ?>