PageRenderTime 1438ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/admin/include/global-functions.php

https://gitlab.com/billyprice1/notrack
PHP | 388 lines | 292 code | 28 blank | 68 comment | 51 complexity | 77de2a2a31ed4922211f07a2e6b05b9a MD5 | raw file
  1. <?php
  2. //Global Functions used in NoTrack Admin
  3. //Check Version------------------------------------------------------
  4. function Check_Version($LatestVersion) {
  5. //If LatestVersion is less than Current Version then function returns false
  6. //1. Split strings by '.'
  7. //2. Combine back together and multiply with Units array
  8. //e.g 1.0 - 1x10000 + 0x100 = 10,000
  9. //e.g 0.8.0 - 0x10000 + 8x100 + 0x1 = 800
  10. //e.g 0.7.10 - 0x10000 + 7x100 + 10x1 = 710
  11. //3. If Latest < Current Version return Current Version
  12. //4. Otherwise return Latest
  13. global $Version;
  14. $NumVersion = 0;
  15. $NumLatest = 0;
  16. $Units = array(10000,100,1);
  17. $SplitVersion = explode('.', $Version);
  18. $SplitLatest = explode('.', $LatestVersion);
  19. for ($i = 0; $i < count($SplitVersion); $i++) {
  20. $NumVersion += ($Units[$i] * intval($SplitVersion[$i]));
  21. }
  22. for ($i = 0; $i < count($SplitVersion); $i++) {
  23. $NumLatest += ($Units[$i] * intval($SplitLatest[$i]));
  24. }
  25. if ($NumLatest < $NumVersion) return false;
  26. return true;
  27. }
  28. //Check User Session-------------------------------------------------
  29. function Check_SessionID() {
  30. if (isset($_SESSION['sid'])) {
  31. if ($_SESSION['sid'] == 1) return true;
  32. }
  33. return false;
  34. }
  35. //Draw Sys Table-----------------------------------------------------
  36. function DrawSysTable($Title) {
  37. echo '<div class="sys-group"><div class="sys-title">'.PHP_EOL;
  38. echo '<h5>'.$Title.'</h5></div>'.PHP_EOL;
  39. echo '<div class="sys-items"><table class="sys-table">'.PHP_EOL;
  40. return null;
  41. }
  42. //Draw Sys Table with Help Button------------------------------------
  43. function DrawSysTableHelp($Title, $HelpPage) {
  44. echo '<div class="sys-group"><div class="sys-title">'.PHP_EOL;
  45. echo '<h5>'.$Title.'&nbsp;<a href="./help.php?p='.$HelpPage.'"><img class="btn" src="./svg/button_help.svg" alt="help"></a></h5></div>'.PHP_EOL;
  46. echo '<div class="sys-items"><table class="sys-table">'.PHP_EOL;
  47. return null;
  48. }
  49. //Draw Sys Row-------------------------------------------------------
  50. function DrawSysRow($Description, $Value) {
  51. echo '<tr><td>'.$Description.': </td><td>'.$Value.'</td></tr>'.PHP_EOL;
  52. return null;
  53. }
  54. //Execute Action-----------------------------------------------------
  55. function ExecAction($Action, $ExecNow, $Fork=false) {
  56. //Execute Action writes a command into /tmp/ntrk-exec.txt
  57. //It can then either wait inline for ntrk-exec to process command, or fork ntrk-exec
  58. //Options:
  59. //ExecNow - false: Write Action to file and then return
  60. //ExecNow - true: Run ntrk-exec and wait until its finished
  61. //Fork - true: Run ntrk-exec and fork to new process
  62. global $FileTmpAction;
  63. if (file_put_contents($FileTmpAction, $Action.PHP_EOL, FILE_APPEND) === false) {
  64. die('Unable to write to file '.$FileTmpAction);
  65. }
  66. if (($ExecNow) && (! $Fork)) {
  67. exec('sudo ntrk-exec 2>&1');
  68. //echo "<pre>\n";
  69. //$Msg = shell_exec('sudo ntrk-exec 2>&1');
  70. //echo $Msg;
  71. //echo "</pre>\n";
  72. }
  73. elseif (($ExecNow) && ($Fork)) {
  74. exec("sudo ntrk-exec > /dev/null &");
  75. }
  76. return null;
  77. }
  78. //Filter Bool from GET-----------------------------------------------
  79. function Filter_Bool($Str) {
  80. //1. Check Variable Exists
  81. //2. Check if Variable is 'true', then return boolean true
  82. //3. Otherwise return boolean false
  83. if (isset($_GET[$Str])) {
  84. if ($_GET[$Str] == 'true') return true;
  85. }
  86. return false;
  87. }
  88. //Filter Int from GET------------------------------------------------
  89. function Filter_Int($Str, $Min, $Max, $DefaltValue=false) {
  90. //1. Check Variable Exists
  91. //2. Check Value is between $Min and $Max
  92. //3. Return Value on success, and $DefaultValue on fail
  93. if (isset($_GET[$Str])) {
  94. if (is_numeric($_GET[$Str])) {
  95. if (($_GET[$Str] >= $Min) && ($_GET[$Str] < $Max)) {
  96. return intval($_GET[$Str]);
  97. }
  98. }
  99. }
  100. return $DefaltValue;
  101. }
  102. //Filter Int from POST-----------------------------------------------
  103. function Filter_Int_Post($Str, $Min, $Max, $DefaltValue=false) {
  104. //1. Check Variable Exists
  105. //2. Check Value is between $Min and $Max
  106. //3. Return Value on success, and $DefaultValue on fail
  107. if (isset($_POST[$Str])) {
  108. if (is_numeric($_POST[$Str])) {
  109. if (($_POST[$Str] >= $Min) && ($_POST[$Str] < $Max)) {
  110. return intval($_POST[$Str]);
  111. }
  112. }
  113. }
  114. return $DefaltValue;
  115. }
  116. //Filter Int Value---------------------------------------------------
  117. function Filter_Int_Value($Val, $Min, $Max, $DefaultValue=0) {
  118. if (is_numeric($Val)) {
  119. if (($Val >= $Min) && ($Val <= $Max)) {
  120. return intval($Val);
  121. }
  122. }
  123. return $DefaltValue;
  124. }
  125. //Filter String from GET---------------------------------------------
  126. function Filter_Str($Str) {
  127. //1. Check Variable Exists
  128. //2. Check String doesn't contain !"£$%^*()[]<>|/\
  129. //Return True on success, and False on fail
  130. if (isset($_GET[$Str])) {
  131. if (preg_match('/[!\"£\$%\^\*\(\)\[\]<>\|\/\\\\]/', $_GET[$Str]) == 0) return true;
  132. }
  133. return false;
  134. }
  135. //Filter String Value------------------------------------------------
  136. function Filter_Str_Value($Str, $DefaltValue='') {
  137. //1. Check String Length is > 0 AND String doesn't contain !"<>,|/\
  138. //2. Return Str on success, and Default on fail
  139. if (preg_match('/[!\"<>\|]/', $Str) == 0) {
  140. return $Str;
  141. }
  142. return $DefaltValue;
  143. }
  144. //Filter URL GET-----------------------------------------------------
  145. function Filter_URL($Str) {
  146. //1. Check Variable Exists
  147. //2. Check String Length is > 0 AND String doesn't contain !"£$%^&()+=<>,|/\
  148. //3. Check String matches the form of a URL "any.co"
  149. //Return True on success, and False on fail
  150. if (isset($_GET[$Str])) {
  151. if (((strlen($_GET[$Str]) > 0) && (preg_match('/[!\"£\$%\^&\(\)+=<>\,\|\/\\\\]/', $_GET[$Str]) == 0))) {
  152. if (preg_match('/.*\..{2,}/', $_GET[$Str]) == 1) return true;
  153. }
  154. }
  155. return false;
  156. }
  157. //Filter URL Str-----------------------------------------------------
  158. function Filter_URL_Str($Str) {
  159. //1. Check String Length is > 0 AND String doesn't contain !"£$^()<>,|
  160. //2. Check String matches the form of a URL "any.co"
  161. //Return True on success, and False on fail
  162. if (preg_match('/[!\"£\$\^\(\)<>\,\|]/', $Str) == 0) {
  163. if (preg_match('/.*\..{2,}$/', $Str) == 1) return true;
  164. }
  165. return false;
  166. }
  167. //Load Config File---------------------------------------------------
  168. function LoadConfigFile() {
  169. //1. Attempt to load Config from Memcache
  170. //2. Write DefaultConfig to Config, incase any variables are missing
  171. //3. Read Config File
  172. //4. Split Line between = (Var = Value)
  173. //5. Filter each value
  174. //6. Setup SearchUrl
  175. //7. Write Config to Memcache
  176. global $FileConfig, $Config, $DefaultConfig, $Mem, $Version;
  177. $Config=$Mem->get('Config'); //Load array from Memcache
  178. if (! empty($Config)) return; //Did it load from memory?
  179. $Config = $DefaultConfig; //Firstly Set Default Config
  180. if (file_exists($FileConfig)) { //Check file exists
  181. $FileHandle= fopen($FileConfig, 'r');
  182. while (!feof($FileHandle)) {
  183. $Line = trim(fgets($FileHandle)); //Read Line of LogFile
  184. if ($Line != '') {
  185. $SplitLine = explode('=', $Line);
  186. if (count($SplitLine == 2)) {
  187. $SplitLine[1] = trim($SplitLine[1]);
  188. switch (trim($SplitLine[0])) {
  189. case 'LatestVersion':
  190. $Config['LatestVersion'] = Filter_Str_Value($SplitLine[1], $Version);
  191. break;
  192. case 'NetDev':
  193. $Config['NetDev'] = $SplitLine[1];
  194. break;
  195. case 'IPVersion':
  196. $Config['IPVersion'] = $SplitLine[1];
  197. break;
  198. case 'Status':
  199. $Config['Status'] = Filter_Str_Value($SplitLine[1], 'Enabled');
  200. break;
  201. case 'BlockMessage':
  202. $Config['BlockMessage'] = Filter_Str_Value($SplitLine[1], 'pixel');
  203. break;
  204. case 'Search':
  205. $Config['Search'] = Filter_Str_Value($SplitLine[1], 'DuckDuckGo');
  206. break;
  207. case 'WhoIs':
  208. $Config['WhoIs'] = Filter_Str_Value($SplitLine[1], 'Who.is');
  209. break;
  210. case 'Username':
  211. $Config['Username'] = $SplitLine[1];
  212. break;
  213. case 'Password':
  214. $Config['Password'] = $SplitLine[1];
  215. break;
  216. case 'Delay':
  217. $Config['Delay'] = Filter_Int_Value($SplitLine[1], 0, 3600, 30);
  218. break;
  219. case 'Suppress':
  220. $Config['Suppress'] = Filter_Str_Value($SplitLine[1], '');
  221. break;
  222. case 'BL_Custom':
  223. $Config['BL_Custom'] = Filter_Str_Value($SplitLine[1], '');
  224. break;
  225. case 'BlockList_NoTrack':
  226. $Config['BlockList_NoTrack'] = Filter_Int_Value($SplitLine[1], 0, 1, 1);
  227. break;
  228. case 'BlockList_TLD':
  229. $Config['BlockList_TLD'] = Filter_Int_Value($SplitLine[1], 0, 1, 1);
  230. break;
  231. case 'BlockList_QMalware':
  232. $Config['BlockList_QMalware'] = Filter_Int_Value($SplitLine[1], 0, 1, 1);
  233. break;
  234. case 'BlockList_Hexxium':
  235. $Config['BlockList_Hexxium'] = Filter_Int_Value($SplitLine[1], 0, 1, 1);
  236. break;
  237. case 'BlockList_AdBlockManager':
  238. $Config['BlockList_AdBlockManager'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  239. break;
  240. case 'BlockList_DisconnectMalvertising':
  241. $Config['BlockList_DisconnectMalvertising'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  242. break;
  243. case 'BlockList_EasyList':
  244. $Config['BlockList_EasyList'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  245. break;
  246. case 'BlockList_EasyPrivacy':
  247. $Config['BlockList_EasyPrivacy'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  248. break;
  249. case 'BlockList_FBAnnoyance':
  250. $Config['BlockList_FBAnnoyance'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  251. break;
  252. case 'BlockList_FBEnhanced':
  253. $Config['BlockList_FBEnhanced'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  254. break;
  255. case 'BlockList_FBSocial':
  256. $Config['BlockList_FBSocial'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  257. break;
  258. case 'BlockList_hpHosts':
  259. $Config['BlockList_hpHosts'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  260. break;
  261. case 'BlockList_MalwareDomainList':
  262. $Config['BlockList_MalwareDomainList'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  263. break;
  264. case 'BlockList_MalwareDomains':
  265. $Config['BlockList_MalwareDomains'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  266. break;
  267. case 'BlockList_PglYoyo':
  268. $Config['BlockList_PglYoyo'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  269. break;
  270. case 'BlockList_SomeoneWhoCares':
  271. $Config['BlockList_SomeoneWhoCares'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  272. break;
  273. case 'BlockList_Spam404':
  274. $Config['BlockList_Spam404'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  275. break;
  276. case 'BlockList_SwissRansom':
  277. $Config['BlockList_SwissRansom'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  278. break;
  279. case 'BlockList_SwissZeus':
  280. $Config['BlockList_SwissZeus'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  281. break;
  282. case 'BlockList_Winhelp2002':
  283. $Config['BlockList_Winhelp2002'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  284. break;
  285. /*case 'BlockList_':
  286. $Config['BlockList_'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  287. break;*/
  288. //Region Specific
  289. case 'BlockList_CHNEasy':
  290. $Config['BlockList_CHNEasy'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  291. break;
  292. case 'BlockList_RUSEasy':
  293. $Config['BlockList_RUSEasy'] = Filter_Int_Value($SplitLine[1], 0, 1, 0);
  294. break;
  295. }
  296. }
  297. }
  298. }
  299. //Set SearchUrl if User hasn't configured a custom string via notrack.conf
  300. if ($Config['SearchUrl'] == '') {
  301. switch($Config['Search']) {
  302. case 'Baidu':
  303. $Config['SearchUrl'] = 'https://www.baidu.com/s?wd=';
  304. break;
  305. case 'Bing':
  306. $Config['SearchUrl'] = 'https://www.bing.com/search?q=';
  307. break;
  308. case 'DuckDuckGo':
  309. $Config['SearchUrl'] = 'https://duckduckgo.com/?q=';
  310. break;
  311. case 'Exalead':
  312. $Config['SearchUrl'] = 'https://www.exalead.com/search/web/results/?q=';
  313. break;
  314. case 'Gigablast':
  315. $Config['SearchUrl'] = 'https://www.gigablast.com/search?q=';
  316. break;
  317. case 'Google':
  318. $Config['SearchUrl'] = 'https://www.google.com/search?q=';
  319. break;
  320. case 'Ixquick':
  321. $Config['SearchUrl'] = 'https://ixquick.eu/do/search?q=';
  322. break;
  323. case 'Qwant':
  324. $Config['SearchUrl'] = 'https://www.qwant.com/?q=';
  325. break;
  326. case 'StartPage':
  327. $Config['SearchUrl'] = 'https://startpage.com/do/search?q=';
  328. break;
  329. case 'Yahoo':
  330. $Config['SearchUrl'] = 'https://search.yahoo.com/search?p=';
  331. break;
  332. case 'Yandex':
  333. $Config['SearchUrl'] = 'https://www.yandex.com/search/?text=';
  334. break;
  335. default:
  336. $Config['SearchUrl'] = 'https://duckduckgo.com/?q=';
  337. }
  338. }
  339. //Set WhoIsUrl if User hasn't configured a custom string via notrack.conf
  340. if ($Config['WhoIsUrl'] == '') {
  341. switch($Config['WhoIs']) {
  342. case 'DomainTools':
  343. $Config['WhoIsUrl'] = 'http://whois.domaintools.com/';
  344. break;
  345. case 'Icann':
  346. $Config['WhoIsUrl'] = 'https://whois.icann.org/lookup?name=';
  347. break;
  348. case 'Who.is':
  349. $Config['WhoIsUrl'] = 'https://who.is/whois/';
  350. break;
  351. default:
  352. $Config['WhoIsUrl'] = 'https://who.is/whois/';
  353. }
  354. }
  355. fclose($FileHandle);
  356. $Mem->set('Config', $Config, 0, 1200);
  357. }
  358. return null;
  359. }
  360. ?>