common.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?php
  2. /*
  3. * Copyright (C) 2012 Leah Rowe <info@minifree.org>
  4. *
  5. * Permission is hereby granted, free of charge, to any person obtaining a
  6. * copy of this software and associated documentation files (the
  7. * "Software"), to deal in the Software without restriction, including
  8. * without limitation the rights to use, copy, modify, merge, publish,
  9. * distribute, sublicense, and/or sell copies of the Software, and to
  10. * permit persons to whom the Software is furnished to do so, subject to
  11. * the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included
  14. * in all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  17. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  19. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  20. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  21. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  22. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23. */
  24. /*
  25. * This contains most common functions used by all of the OAchecker scripts.
  26. * Most crucial, contained here are functions for connecting to the database.
  27. */
  28. // common variables (regular expressions)
  29. $rxIPv4Addr = "/^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-".
  30. "9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/";
  31. $rxHostname = "/^(([a-zA-Z]|[a-zA-Z][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z]|".
  32. "[A-Za-z][A-Za-z0-9\-]*[A-Za-z0-9])$/";
  33. $rxEmail = "/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{".
  34. "2,3})$/";
  35. function dbConfig() {
  36. // IMPORTANT!!!!!!!!!!!!!
  37. // Make sure that the information here is correct
  38. // THIS IS THE DATA THAT YOU NEED IN ORDER TO ACCESS THE DATABASE
  39. // WITHOUT THESE SETTINGS PROPERLY IN PLACE, THIS SOFTWARE **WILL
  40. // NOT WORK**
  41. $db = "";
  42. $db["host"] = "";
  43. $db["database"] = "";
  44. $db["user"] = "";
  45. $db["password"] = "";
  46. return $db;
  47. }
  48. function inject($query)
  49. {
  50. $retval = "";
  51. $db = dbConfig();
  52. $host = $db["host"];
  53. $database = $db["database"];
  54. $user = $db["user"];
  55. $password = $db["password"];
  56. $con = mysql_connect($host,$user,$password);
  57. if (!$con)
  58. {
  59. die('Inject function Could not connect: ' . mysql_error());
  60. }
  61. mysql_select_db($database, $con);
  62. $retval = mysql_query($query);
  63. mysql_close($con);
  64. return $retval;
  65. }
  66. function urlExists($url=NULL)
  67. {
  68. if($url == NULL) return false;
  69. $ch = curl_init($url);
  70. curl_setopt($ch, CURLOPT_TIMEOUT, 5);
  71. curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
  72. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  73. $data = curl_exec($ch);
  74. $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  75. curl_close($ch);
  76. if($httpcode>=200 && $httpcode<300){
  77. return true;
  78. } else {
  79. return false;
  80. }
  81. }
  82. function strip_html_tags( $text )
  83. {
  84. $text = preg_replace(
  85. array(
  86. // Remove invisible content
  87. "@<style[^>]*?>.*?</style>@siu",
  88. "@<head[^>]*?>.*?</head>@siu",
  89. "@<script[^>]*?.*?</script>@siu",
  90. "@<object[^>]*?.*?</object>@siu",
  91. "@<embed[^>]*?.*?</embed>@siu",
  92. "@<applet[^>]*?.*?</applet>@siu",
  93. "@<noframes[^>]*?.*?</noframes>@siu",
  94. "@<noscript[^>]*?.*?</noscript>@siu",
  95. "@<noembed[^>]*?.*?</noembed>@siu",
  96. // Add line breaks before and after blocks
  97. "@</?((address)|(blockquote)|(center)|(del))@iu",
  98. "@</?((div)|(h[1-9])|(ins)|(isindex)|(p)|(pre))@iu",
  99. "@</?((dir)|(dl)|(dt)|(dd)|(li)|(menu)|(ol)|(ul))@iu",
  100. "@</?((table)|(th)|(td)|(caption))@iu",
  101. "@</?((form)|(button)|(fieldset)|(legend)|(input))@iu",
  102. "@</?((label)|(select)|(optgroup)|(option)|(textarea))@iu",
  103. "@</?((frameset)|(frame)|(iframe))@iu",
  104. ),
  105. array(
  106. ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ',
  107. "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0", "\n\$0",
  108. "\n\$0", "\n\$0",
  109. ),
  110. $text );
  111. return strip_tags( $text );
  112. }
  113. function clean($input) {
  114. $db = dbConfig();
  115. $host = $db["host"];
  116. $database = $db["database"];
  117. $user = $db["user"];
  118. $password = $db["password"];
  119. $con = mysql_connect($host,$user,$password);
  120. if (!$con)
  121. {
  122. die('Clean function Could not connect: ' . mysql_error());
  123. }
  124. mysql_select_db($database, $con);
  125. $output = $input;
  126. $output = strip_html_tags($output);
  127. $output = mysql_real_escape_string($output);
  128. mysql_close($con);
  129. return $output;
  130. }
  131. // record new log entry
  132. function record($info,$section)
  133. {
  134. if (!isset($info)) return "info not set";
  135. if (!isset($section)) return "section not set";
  136. if (strlen($info) == 0) return "info not set";
  137. if (strlen($section) == 0) return "section not set";
  138. $info = clean($info);
  139. $section = clean($section);
  140. $ip = clean($_SERVER["REMOTE_ADDR"]);
  141. $hostname = clean(gethostbyaddr($ip));
  142. // info
  143. // section
  144. $user = "";
  145. if (isset($_SESSION["user"])) $user = clean($_SESSION["user"]);
  146. $timestamp = clean(date("U"));
  147. inject("INSERT INTO logs VALUES('$ip','$hostname','$info','$section',
  148. '$user','$timestamp')");
  149. return "";
  150. }
  151. // send an email on behalf of...
  152. function compose($subject, $message)
  153. {
  154. $sqlAdmin = inject("SELECT * FROM admin");
  155. $sqlAdminNrows = mysql_numrows($sqlAdmin);
  156. $subject .= " (OAchecker)";
  157. $message .= " \n\nTHIS IS AN AUTOMATED MESSAGE, PLEASE DO NOT ".
  158. "REPLY\n\n"; // please do
  159. for ($i = 0; $i < $sqlAdminNrows; $i++) {
  160. $sqlAdminEmailAddr = mysql_result($sqlAdmin, $i, "emailaddr");
  161. $from = $sqlAdminEmailAddr;
  162. $headers = "From: $from\r\nReply-To: $from\r\nX-Mailer: PHP/".
  163. phpversion();
  164. mail($sqlAdminEmailAddr, $subject, $message, $headers, "-f".
  165. $from);
  166. }
  167. if ($sqlAdminNrows > 0) return "emails sent";
  168. else return "no email admins listed, emails not sent";
  169. }
  170. // CRON FUNCTIONS
  171. // (but could be used for other purposes)
  172. function curlGetPage($page) {
  173. $content = "";
  174. $ch = curl_init($page);
  175. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  176. curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
  177. $content = curl_exec($ch);
  178. curl_close($ch);
  179. return $content;
  180. }
  181. function serverArray()
  182. {
  183. // MySQL data
  184. $sqlServers = inject("SELECT * FROM servers");
  185. $sqlServersNrows = mysql_numrows($sqlServers);
  186. // ip:port
  187. $serversFull = array($sqlServersNrows);
  188. // ip
  189. $serversIPAddr = array($sqlServersNrows);
  190. // port
  191. $serversPortNum = array($sqlServersNrows);
  192. // serverdown
  193. $serversServerDown = array($sqlServersNrows);
  194. for ($i = 0; $i < $sqlServersNrows; $i++)
  195. {
  196. $sqlServersIPAddr = mysql_result($sqlServers, $i, "ipaddr");
  197. $sqlServersPortNum = mysql_result($sqlServers, $i, "portnum");
  198. $sqlServersServerDown = mysql_result($sqlServers, $i,
  199. "serverdown");
  200. // Full form ip:port array
  201. $serversFull[$i] = "$sqlServersIPAddr:$sqlServersPortNum";
  202. // Individual items
  203. $serversIPAddr[$i] = "$sqlServersIPAddr";
  204. $serversPortNum[$i] = "$sqlServersPortNum";
  205. $serversServerDown[$i] = "$sqlServersServerDown";
  206. }
  207. $server = "";
  208. $server["full"] = $serversFull;
  209. $server["ipaddr"] = $serversIPAddr;
  210. $server["portnum"] = $serversPortNum;
  211. $server["serverdown"] = $serversServerDown;
  212. return $server;
  213. }
  214. function qstatArray()
  215. {
  216. $sqlQStat = inject("SELECT * FROM qstat");
  217. $sqlQStatNrows = mysql_numrows($sqlQStat);
  218. // Content of each QStat page
  219. $qstatContent = array($sqlQStatNrows);
  220. // URL of each QStat page
  221. $qstatListURL = array($sqlQStatNrows);
  222. for ($i = 0; $i < $sqlQStatNrows; $i++)
  223. {
  224. $sqlQStatListURL = mysql_result($sqlQStat, $i, "listurl");
  225. $qstatContent[$i] = curlGetPage($sqlQStatListURL);
  226. $qstatListURL[$i] = $sqlQStatListURL;
  227. }
  228. $qstat = "";
  229. $qstat["listurl"] = $qstatListURL;
  230. $qstat["content"] = $qstatContent;
  231. return $qstat;
  232. }
  233. ?>