PageRenderTime 27ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/poweradmin-2.1.6/inc/toolkit.inc.php

#
PHP | 402 lines | 299 code | 55 blank | 48 comment | 57 complexity | 14e928c63ab03e1cb0067bfe68315725 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. ob_start();
  3. /* Poweradmin, a friendly web-based admin tool for PowerDNS.
  4. * See <https://www.poweradmin.org> for more details.
  5. *
  6. * Copyright 2007-2009 Rejo Zenger <rejo@zenger.nl>
  7. * Copyright 2010-2012 Poweradmin Development Team
  8. * <https://www.poweradmin.org/trac/wiki/Credits>
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. if (! function_exists('session_start')) die(error('You have to install PHP session extension!'));
  24. if (! function_exists('_')) die(error('You have to install PHP gettext extension!'));
  25. if (! function_exists('mcrypt_encrypt')) die(error('You have to install PHP mcrypt extension!'));
  26. session_start();
  27. include_once("config-me.inc.php");
  28. if(!@include_once("config.inc.php"))
  29. {
  30. error( _('You have to create a config.inc.php!') );
  31. }
  32. /*************
  33. * Constants *
  34. *************/
  35. if (isset($_GET["start"])) {
  36. define('ROWSTART', (($_GET["start"] - 1) * $iface_rowamount));
  37. } else {
  38. define('ROWSTART', 0);
  39. }
  40. if (isset($_GET["letter"])) {
  41. define('LETTERSTART', $_GET["letter"]);
  42. $_SESSION["letter"] = $_GET["letter"];
  43. } elseif(isset($_SESSION["letter"])) {
  44. define('LETTERSTART', $_SESSION["letter"]);
  45. } else {
  46. define('LETTERSTART', "a");
  47. }
  48. if (isset($_GET["zone_sort_by"]) && preg_match("/^[a-z_]+$/", $_GET["zone_sort_by"] ) ) {
  49. define('ZONE_SORT_BY', $_GET["zone_sort_by"]);
  50. $_SESSION["zone_sort_by"] = $_GET["zone_sort_by"];
  51. } elseif(isset($_POST["zone_sort_by"]) && preg_match("/^[a-z_]+$/", $_POST["zone_sort_by"] )) {
  52. define('ZONE_SORT_BY', $_POST["zone_sort_by"]);
  53. $_SESSION["zone_sort_by"] = $_POST["zone_sort_by"];
  54. } elseif(isset($_SESSION["zone_sort_by"])) {
  55. define('ZONE_SORT_BY', $_SESSION["zone_sort_by"]);
  56. } else {
  57. define('ZONE_SORT_BY', "name");
  58. }
  59. if (isset($_GET["record_sort_by"]) && preg_match("/^[a-z_]+$/", $_GET["record_sort_by"] )) {
  60. define('RECORD_SORT_BY', $_GET["record_sort_by"]);
  61. $_SESSION["record_sort_by"] = $_GET["record_sort_by"];
  62. } elseif(isset($_POST["record_sort_by"]) && preg_match("/^[a-z_]+$/", $_POST["record_sort_by"] )) {
  63. define('RECORD_SORT_BY', $_POST["record_sort_by"]);
  64. $_SESSION["record_sort_by"] = $_POST["record_sort_by"];
  65. } elseif(isset($_SESSION["record_sort_by"])) {
  66. define('RECORD_SORT_BY', $_SESSION["record_sort_by"]);
  67. } else {
  68. define('RECORD_SORT_BY', "name");
  69. }
  70. $valid_tlds = array(
  71. "ac", "ad", "ae", "aero", "af", "ag", "ai", "al", "am", "an", "ao", "aq", "ar",
  72. "arpa", "as", "asia", "at", "au", "aw", "ax", "az", "ba", "bb", "bd", "be",
  73. "bf", "bg", "bh", "bi", "biz", "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv",
  74. "bw", "by", "bz", "ca", "cat", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl",
  75. "cm", "cn", "co", "com", "coop", "cr", "cu", "cv", "cw", "cx", "cy", "cz", "de", "dj",
  76. "dk", "dm", "do", "dz", "ec", "edu", "ee", "eg", "er", "es", "et", "eu", "fi",
  77. "fj", "fk", "fm", "fo", "fr", "ga", "gb", "gd", "ge", "gf", "gg", "gh", "gi",
  78. "gl", "gm", "gn", "gov", "gp", "gq", "gr", "gs", "gt", "gu", "gw", "gy", "hk",
  79. "hm", "hn", "hr", "ht", "hu", "id", "ie", "il", "im", "in", "info", "int", "io",
  80. "iq", "ir", "is", "it", "je", "jm", "jo", "jobs", "jp", "ke", "kg", "kh", "ki",
  81. "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc", "li", "lk", "lr",
  82. "ls", "lt", "lu", "lv", "ly", "ma", "mc", "md", "me", "mg", "mh", "mil", "mk",
  83. "ml", "mm", "mn", "mo", "mobi", "mp", "mq", "mr", "ms", "mt", "mu", "museum",
  84. "mv", "mw", "mx", "my", "mz", "na", "name", "nc", "ne", "net", "nf", "ng", "ni",
  85. "nl", "no", "np", "nr", "nu", "nz", "om", "org", "pa", "pe", "pf", "pg", "ph",
  86. "pk", "pl", "pm", "pn", "pr", "pro", "ps", "pt", "pw", "py", "qa", "re", "ro",
  87. "rs", "ru", "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj", "sk",
  88. "sl", "sm", "sn", "so", "sr", "st", "su", "sv", "sx", "sy", "sz", "tc", "td", "tel",
  89. "tf", "tg", "th", "tj", "tk", "tl", "tm", "tn", "to", "tp", "tr", "travel",
  90. "tt", "tv", "tw", "tz", "ua", "ug", "uk", "us", "uy", "uz", "va", "vc",
  91. "ve", "vg", "vi", "vn", "vu", "wf", "ws", "xn--0zwm56d", "xn--11b5bs3a9aj6g",
  92. "xn--3e0b707e", "xn--45brj9c", "xn--80akhbyknj4f", "xn--80ao21a", "xn--90a3ac",
  93. "xn--9t4b11yi5a", "xn--clchc0ea0b2g2a9gcd", "xn--deba0ad", "xn--fiqs8s",
  94. "xn--fiqz9s", "xn--fpcrj9c3d", "xn--fzc2c9e2c", "xn--g6w251d", "xn--gecrj9c",
  95. "xn--h2brj9c", "xn--hgbk6aj7f53bba", "xn--hlcj6aya9esc7a", "xn--j6w193g",
  96. "xn--jxalpdlp", "xn--kgbechtv", "xn--kprw13d", "xn--kpry57d", "xn--lgbbat1ad8j",
  97. "xn--mgbaam7a8h", "xn--mgbayh7gpa", "xn--mgbbh1a71e", "xn--mgbc0a9azcg",
  98. "xn--mgberp4a5d4ar", "xn--o3cw4h", "xn--ogbpf8fl", "xn--p1ai", "xn--pgbs0dh",
  99. "xn--s9brj9c", "xn--wgbh1c", "xn--wgbl6a", "xn--xkc2al3hye2a", "xn--xkc2dl3a5ee0h",
  100. "xn--yfro4i67o", "xn--ygbi2ammx", "xn--zckzah", "xxx", "ye", "yt", "za", "zm", "zw");
  101. /* Database connection */
  102. require_once("database.inc.php");
  103. // Generates $db variable to access database.
  104. // Array of the available zone types
  105. $server_types = array("MASTER", "SLAVE", "NATIVE");
  106. // $rtypes - array of possible record types
  107. $rtypes = array('A', 'AAAA', 'CNAME', 'HINFO', 'MX', 'NAPTR', 'NS', 'PTR', 'SOA', 'SPF', 'SRV', 'SSHFP', 'TXT', 'RP');
  108. // If fancy records is enabled, extend this field.
  109. if($dns_fancy) {
  110. $rtypes[14] = 'URL';
  111. $rtypes[15] = 'MBOXFW';
  112. $rtypes[16] = 'CURL';
  113. $rtypes[17] = 'LOC';
  114. }
  115. /*************
  116. * Includes *
  117. *************/
  118. require_once("i18n.inc.php");
  119. require_once("error.inc.php");
  120. require_once("auth.inc.php");
  121. require_once("users.inc.php");
  122. require_once("dns.inc.php");
  123. require_once("record.inc.php");
  124. require_once("templates.inc.php");
  125. $db = dbConnect();
  126. doAuthenticate();
  127. /*************
  128. * Functions *
  129. *************/
  130. /*
  131. * Display the page option: [1] [2] .. [n]
  132. */
  133. function show_pages($amount,$rowamount,$id='')
  134. {
  135. if ($amount > $rowamount) {
  136. if (!isset($_GET["start"])) $_GET["start"]=1;
  137. echo _('Show page') . ":<br>";
  138. for ($i=1;$i<=ceil($amount / $rowamount);$i++) {
  139. if ($_GET["start"] == $i) {
  140. echo "[ <b>".$i."</b> ] ";
  141. } else {
  142. echo " <a href=\"".htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES)."?start=".$i;
  143. if ($id!='') echo "&id=".$id;
  144. echo "\">[ ".$i." ]</a> ";
  145. }
  146. }
  147. }
  148. }
  149. /*
  150. * Display the alphabetic option: [0-9] [a] [b] .. [z]
  151. */
  152. function show_letters($letterstart,$userid=true)
  153. {
  154. echo _('Show zones beginning with') . ":<br>";
  155. $letter = "[[:digit:]]";
  156. if ($letterstart == "1")
  157. {
  158. echo "[ <span class=\"lettertaken\">0-9</span> ] ";
  159. }
  160. elseif (zone_letter_start($letter,$userid))
  161. {
  162. echo "<a href=\"".htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES)."?letter=1\">[ 0-9 ]</a> ";
  163. }
  164. else
  165. {
  166. echo "[ <span class=\"letternotavailable\">0-9</span> ] ";
  167. }
  168. foreach (range('a','z') as $letter)
  169. {
  170. if ($letter == $letterstart)
  171. {
  172. echo "[ <span class=\"lettertaken\">".$letter."</span> ] ";
  173. }
  174. elseif (zone_letter_start($letter,$userid))
  175. {
  176. echo "<a href=\"".htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES)."?letter=".$letter."\">[ ".$letter." ]</a> ";
  177. }
  178. else
  179. {
  180. echo "[ <span class=\"letternotavailable\">".$letter."</span> ] ";
  181. }
  182. }
  183. if ($letterstart == 'all')
  184. {
  185. echo "[ <span class=\"lettertaken\"> Show all </span> ] ";
  186. } else {
  187. echo "<a href=\"".htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES)."?letter=all\">[ Show all ]</a> ";
  188. }
  189. }
  190. function zone_letter_start($letter,$userid=true)
  191. {
  192. global $db;
  193. global $sql_regexp;
  194. $query = "SELECT
  195. domains.id AS domain_id,
  196. zones.owner,
  197. domains.name AS domainname
  198. FROM domains
  199. LEFT JOIN zones ON domains.id=zones.domain_id
  200. WHERE substring(domains.name,1,1) ".$sql_regexp." ".$db->quote("^".$letter, 'text');
  201. $db->setLimit(1);
  202. $result = $db->query($query);
  203. $numrows = $result->numRows();
  204. if ( $numrows == "1" ) {
  205. return 1;
  206. } else {
  207. return 0;
  208. }
  209. }
  210. function error($msg) {
  211. if ($msg) {
  212. echo " <div class=\"error\">Error: " . $msg . "</div>\n";
  213. } else {
  214. echo " <div class=\"error\">" . _('An unknown error has occurred.') . "</div>\n";
  215. }
  216. }
  217. function success($msg) {
  218. if ($msg) {
  219. echo " <div class=\"success\">" . $msg . "</div>\n";
  220. } else {
  221. echo " <div class=\"success\">" . _('Something has been successfully performed. What exactly, however, will remain a mystery.') . "</div>\n";
  222. }
  223. }
  224. /*
  225. * Something has been done nicely, display a message and a back button.
  226. */
  227. function message($msg)
  228. {
  229. include_once("header.inc.php");
  230. ?>
  231. <P><TABLE CLASS="messagetable"><TR><TD CLASS="message"><H2><?php echo _('Success!'); ?></H2>
  232. <BR>
  233. <FONT STYLE="font-weight: Bold">
  234. <P>
  235. <?php
  236. if($msg)
  237. {
  238. echo nl2br($msg);
  239. }
  240. else
  241. {
  242. echo _('Successful!');
  243. }
  244. ?>
  245. </P>
  246. <BR>
  247. <P>
  248. <a href="javascript:history.go(-1)">&lt;&lt; <?php echo _('back'); ?></a></FONT>
  249. </P>
  250. </TD></TR></TABLE></P>
  251. <?php
  252. include_once("footer.inc.php");
  253. }
  254. /*
  255. * Reroute a user to a cleanpage of (if passed) arg
  256. */
  257. function clean_page($arg='')
  258. {
  259. if (!$arg)
  260. {
  261. header("Location: ".htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES)."?time=".time());
  262. exit;
  263. }
  264. else
  265. {
  266. if (preg_match('!\?!si', $arg))
  267. {
  268. $add = "&time=";
  269. }
  270. else
  271. {
  272. $add = "?time=";
  273. }
  274. header("Location: $arg$add".time());
  275. exit;
  276. }
  277. }
  278. function get_status($res)
  279. {
  280. if ($res == '0')
  281. {
  282. return "<FONT CLASS=\"inactive\">" . _('Inactive') . "</FONT>";
  283. }
  284. elseif ($res == '1')
  285. {
  286. return "<FONT CLASS=\"active\">" . _('Active') . "</FONT>";
  287. }
  288. }
  289. function parse_template_value($val, $domain)
  290. {
  291. $serial = date("Ymd");
  292. $serial .= "00";
  293. $val = str_replace('[ZONE]', $domain, $val);
  294. $val = str_replace('[SERIAL]', $serial, $val);
  295. return $val;
  296. }
  297. function is_valid_email($address) {
  298. $fields = preg_split("/@/", $address, 2);
  299. if((!preg_match("/^[0-9a-z]([-_.]?[0-9a-z])*$/i", $fields[0])) || (!isset($fields[1]) || $fields[1] == '' || !is_valid_hostname_fqdn($fields[1], 0))) {
  300. return false;
  301. }
  302. return true;
  303. }
  304. function v_num($string) {
  305. if (!preg_match("/^[0-9]+$/i", $string)) {
  306. return false ;
  307. } else {
  308. return true ;
  309. }
  310. }
  311. // Debug print
  312. function debug_print($var) {
  313. echo "<pre style=\"border: 2px solid blue;\">\n";
  314. if (is_array($var)) { print_r($var) ; } else { echo $var ; }
  315. echo "</pre>\n";
  316. }
  317. // Set timezone (required for PHP5)
  318. function set_timezone() {
  319. global $timezone;
  320. if (function_exists('date_default_timezone_set')) {
  321. if (isset($timezone)) {
  322. date_default_timezone_set($timezone);
  323. } else if (!ini_get('date.timezone')) {
  324. date_default_timezone_set('UTC');
  325. }
  326. }
  327. }
  328. function generate_salt($len = 5) {
  329. $valid_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#$%^*()_-!';
  330. $valid_len = strlen($valid_characters) - 1;
  331. $salt = "";
  332. for($i = 0; $i < $len; $i++) {
  333. $salt .= $valid_characters[rand(0, $valid_len)];
  334. }
  335. return $salt;
  336. }
  337. function extract_salt($password) {
  338. return substr(strchr($password, ':'), 1);
  339. }
  340. function mix_salt($salt, $pass) {
  341. return md5($salt.$pass).':'.$salt;
  342. }
  343. function gen_mix_salt($pass) {
  344. $salt = generate_salt();
  345. return mix_salt($salt, $pass);
  346. }
  347. ?>