Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

toolkit.inc.php 20KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. <?php
  2. /* Poweradmin, a friendly web-based admin tool for PowerDNS.
  3. * See <http://www.poweradmin.org> for more details.
  4. *
  5. * Copyright 2007-2009 Rejo Zenger <rejo@zenger.nl>
  6. * Copyright 2010-2014 Poweradmin Development Team
  7. * <http://www.poweradmin.org/credits.html>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * Toolkit functions
  24. *
  25. * @package Poweradmin
  26. * @copyright 2007-2010 Rejo Zenger <rejo@zenger.nl>
  27. * @copyright 2010-2014 Poweradmin Development Team
  28. * @license http://opensource.org/licenses/GPL-3.0 GPL
  29. */
  30. // Fix for Strict Standards: Non-static method PEAR::setErrorHandling() should not be called statically
  31. // TODO: remove after PEAR::MDB2 replacement with PDO
  32. ini_set('error_reporting', E_ALL & ~ (E_NOTICE | E_STRICT));
  33. // TODO: display elapsed time and memory consumption,
  34. // used to check improvements in refactored version
  35. $display_stats = false;
  36. if ($display_stats)
  37. include('inc/benchmark.php');
  38. ob_start();
  39. require_once("error.inc.php");
  40. if (!function_exists('session_start'))
  41. die(error('You have to install PHP session extension!'));
  42. if (!function_exists('_'))
  43. die(error('You have to install PHP gettext extension!'));
  44. if (!function_exists('mcrypt_encrypt'))
  45. die(error('You have to install PHP mcrypt extension!'));
  46. session_start();
  47. include_once("config-me.inc.php");
  48. if (!@include_once("config.inc.php")) {
  49. error(_('You have to create a config.inc.php!'));
  50. }
  51. /* * ***********
  52. * Constants *
  53. * *********** */
  54. if (isset($_GET["start"])) {
  55. define('ROWSTART', (($_GET["start"] - 1) * $iface_rowamount));
  56. } else {
  57. /** Starting row
  58. */
  59. define('ROWSTART', 0);
  60. }
  61. if (isset($_GET["letter"])) {
  62. define('LETTERSTART', $_GET["letter"]);
  63. $_SESSION["letter"] = $_GET["letter"];
  64. } elseif (isset($_SESSION["letter"])) {
  65. define('LETTERSTART', $_SESSION["letter"]);
  66. } else {
  67. /** Starting letter
  68. */
  69. define('LETTERSTART', "a");
  70. }
  71. if (isset($_GET["zone_sort_by"]) && preg_match("/^[a-z_]+$/", $_GET["zone_sort_by"])) {
  72. define('ZONE_SORT_BY', $_GET["zone_sort_by"]);
  73. $_SESSION["zone_sort_by"] = $_GET["zone_sort_by"];
  74. } elseif (isset($_POST["zone_sort_by"]) && preg_match("/^[a-z_]+$/", $_POST["zone_sort_by"])) {
  75. define('ZONE_SORT_BY', $_POST["zone_sort_by"]);
  76. $_SESSION["zone_sort_by"] = $_POST["zone_sort_by"];
  77. } elseif (isset($_SESSION["zone_sort_by"])) {
  78. define('ZONE_SORT_BY', $_SESSION["zone_sort_by"]);
  79. } else {
  80. /** Field to sort zone by
  81. */
  82. define('ZONE_SORT_BY', "name");
  83. }
  84. if (isset($_SESSION["userlang"])) {
  85. $iface_lang = $_SESSION["userlang"];
  86. }
  87. if (isset($_GET["record_sort_by"]) && preg_match("/^[a-z_]+$/", $_GET["record_sort_by"])) {
  88. define('RECORD_SORT_BY', $_GET["record_sort_by"]);
  89. $_SESSION["record_sort_by"] = $_GET["record_sort_by"];
  90. } elseif (isset($_POST["record_sort_by"]) && preg_match("/^[a-z_]+$/", $_POST["record_sort_by"])) {
  91. define('RECORD_SORT_BY', $_POST["record_sort_by"]);
  92. $_SESSION["record_sort_by"] = $_POST["record_sort_by"];
  93. } elseif (isset($_SESSION["record_sort_by"])) {
  94. define('RECORD_SORT_BY', $_SESSION["record_sort_by"]);
  95. } else {
  96. /** Record to sort zone by
  97. */
  98. define('RECORD_SORT_BY', "name");
  99. }
  100. $valid_tlds = array("ac", "academy", "actor", "ad", "ae", "aero", "af", "ag",
  101. "agency", "ai", "al", "am", "an", "ao", "aq", "ar", "arpa", "as", "asia",
  102. "at", "au", "aw", "ax", "az", "ba", "bar", "bargains", "bb", "bd", "be",
  103. "berlin", "best", "bf", "bg", "bh", "bi", "bid", "bike", "biz", "bj", "blue",
  104. "bm", "bn", "bo", "boutique", "br", "bs", "bt", "build", "builders", "buzz",
  105. "bv", "bw", "by", "bz", "ca", "cab", "camera", "camp", "cards", "careers",
  106. "cat", "catering", "cc", "cd", "center", "ceo", "cf", "cg", "ch", "cheap",
  107. "christmas", "ci", "ck", "cl", "cleaning", "clothing", "club", "cm", "cn",
  108. "co", "codes", "coffee", "com", "community", "company", "computer", "condos",
  109. "construction", "contractors", "cool", "coop", "cr", "cruises", "cu", "cv",
  110. "cw", "cx", "cy", "cz", "dance", "dating", "de", "democrat", "diamonds",
  111. "directory", "dj", "dk", "dm", "do", "domains", "dz", "ec", "edu",
  112. "education", "ee", "eg", "email", "enterprises", "equipment", "er", "es",
  113. "estate", "et", "eu", "events", "expert", "exposed", "farm", "fi", "fish",
  114. "fj", "fk", "flights", "florist", "fm", "fo", "foundation", "fr", "futbol",
  115. "ga", "gallery", "gb", "gd", "ge", "gf", "gg", "gh", "gi", "gift", "gl",
  116. "glass", "gm", "gn", "gov", "gp", "gq", "gr", "graphics", "gs", "gt", "gu",
  117. "guitars", "guru", "gw", "gy", "hk", "hm", "hn", "holdings", "holiday",
  118. "house", "hr", "ht", "hu", "id", "ie", "il", "im", "immobilien", "in",
  119. "industries", "info", "institute", "int", "international", "io", "iq", "ir",
  120. "is", "it", "je", "jm", "jo", "jobs", "jp", "kaufen", "ke", "kg", "kh", "ki",
  121. "kim", "kitchen", "kiwi", "km", "kn", "koeln", "kp", "kr", "kred", "kw", "ky",
  122. "kz", "la", "land", "lb", "lc", "li", "lighting", "limo", "link", "lk", "lr",
  123. "ls", "lt", "lu", "luxury", "lv", "ly", "ma", "maison", "management", "mango",
  124. "marketing", "mc", "md", "me", "menu", "mg", "mh", "mil", "mk", "ml", "mm",
  125. "mn", "mo", "mobi", "moda", "monash", "mp", "mq", "mr", "ms", "mt", "mu",
  126. "museum", "mv", "mw", "mx", "my", "mz", "na", "nagoya", "name", "nc", "ne",
  127. "net", "neustar", "nf", "ng", "ni", "ninja", "nl", "no", "np", "nr", "nu",
  128. "nz", "okinawa", "om", "onl", "org", "pa", "partners", "parts", "pe", "pf",
  129. "pg", "ph", "photo", "photography", "photos", "pics", "pink", "pk", "pl",
  130. "plumbing", "pm", "pn", "post", "pr", "pro", "productions", "properties",
  131. "ps", "pt", "pub", "pw", "py", "qa", "qpon", "re", "recipes", "red",
  132. "rentals", "repair", "report", "reviews", "rich", "ro", "rs", "ru", "ruhr",
  133. "rw", "sa", "sb", "sc", "sd", "se", "sexy", "sg", "sh", "shiksha", "shoes",
  134. "si", "singles", "sj", "sk", "sl", "sm", "sn", "so", "social", "solar",
  135. "solutions", "sr", "st", "su", "supplies", "supply", "support", "sv", "sx",
  136. "sy", "systems", "sz", "tattoo", "tc", "td", "technology", "tel", "tf", "tg",
  137. "th", "tienda", "tips", "tj", "tk", "tl", "tm", "tn", "to", "today", "tokyo",
  138. "tools", "tp", "tr", "training", "travel", "tt", "tv", "tw", "tz", "ua", "ug",
  139. "uk", "uno", "us", "uy", "uz", "va", "vacations", "vc", "ve", "ventures",
  140. "vg", "vi", "viajes", "villas", "vision", "vn", "vote", "voting", "voto",
  141. "voyage", "vu", "wang", "watch", "wed", "wf", "wien", "wiki", "works", "ws",
  142. "xn--3bst00m", "xn--3ds443g", "xn--3e0b707e", "xn--45brj9c", "xn--55qw42g",
  143. "xn--55qx5d", "xn--6frz82g", "xn--6qq986b3xl", "xn--80ao21a", "xn--80asehdb",
  144. "xn--80aswg", "xn--90a3ac", "xn--c1avg", "xn--cg4bki",
  145. "xn--clchc0ea0b2g2a9gcd", "xn--d1acj3b", "xn--fiq228c5hs", "xn--fiq64b",
  146. "xn--fiqs8s", "xn--fiqz9s", "xn--fpcrj9c3d", "xn--fzc2c9e2c", "xn--gecrj9c",
  147. "xn--h2brj9c", "xn--i1b6b1a6a2e", "xn--io0a7i", "xn--j1amh", "xn--j6w193g",
  148. "xn--kprw13d", "xn--kpry57d", "xn--l1acc", "xn--lgbbat1ad8j", "xn--mgb9awbf",
  149. "xn--mgba3a4f16a", "xn--mgbaam7a8h", "xn--mgbab2bd", "xn--mgbayh7gpa",
  150. "xn--mgbbh1a71e", "xn--mgbc0a9azcg", "xn--mgberp4a5d4ar", "xn--mgbx4cd0ab",
  151. "xn--ngbc5azd", "xn--nqv7f", "xn--nqv7fs00ema", "xn--o3cw4h", "xn--ogbpf8fl",
  152. "xn--p1ai", "xn--pgbs0dh", "xn--q9jyb4c", "xn--s9brj9c", "xn--unup4y",
  153. "xn--wgbh1c", "xn--wgbl6a", "xn--xkc2al3hye2a", "xn--xkc2dl3a5ee0h",
  154. "xn--yfro4i67o", "xn--ygbi2ammx", "xn--zfr164b", "xxx", "xyz", "ye", "yt",
  155. "za", "zm", "zone", "zw");
  156. // Special TLDs for testing and documentation purposes
  157. // http://tools.ietf.org/html/rfc2606#section-2
  158. array_push($valid_tlds, 'test', 'example', 'invalid', 'localhost');
  159. /* Database connection */
  160. require_once("database.inc.php");
  161. // Generates $db variable to access database.
  162. // Array of the available zone types
  163. $server_types = array("MASTER", "SLAVE", "NATIVE");
  164. // $rtypes - array of possible record types
  165. $rtypes = array(
  166. 'A',
  167. 'AAAA',
  168. 'AFSDB',
  169. 'CERT',
  170. 'CNAME',
  171. 'DHCID',
  172. 'DLV',
  173. 'DNSKEY',
  174. 'DS',
  175. 'EUI48',
  176. 'EUI64',
  177. 'HINFO',
  178. 'IPSECKEY',
  179. 'KEY',
  180. 'KX',
  181. 'LOC',
  182. 'MINFO',
  183. 'MR',
  184. 'MX',
  185. 'NAPTR',
  186. 'NS',
  187. 'NSEC',
  188. 'NSEC3',
  189. 'NSEC3PARAM',
  190. 'OPT',
  191. 'PTR',
  192. 'RKEY',
  193. 'RP',
  194. 'RRSIG',
  195. 'SOA',
  196. 'SPF',
  197. 'SRV',
  198. 'SSHFP',
  199. 'TLSA',
  200. 'TSIG',
  201. 'TXT',
  202. 'WKS',
  203. );
  204. // If fancy records is enabled, extend this field.
  205. if ($dns_fancy) {
  206. $rtypes[] = 'URL';
  207. $rtypes[] = 'MBOXFW';
  208. $rtypes[] = 'CURL';
  209. }
  210. /* * ***********
  211. * Includes *
  212. * *********** */
  213. require_once("i18n.inc.php");
  214. require_once("auth.inc.php");
  215. require_once("users.inc.php");
  216. require_once("dns.inc.php");
  217. require_once("record.inc.php");
  218. require_once("dnssec.inc.php");
  219. require_once("templates.inc.php");
  220. $db = dbConnect();
  221. doAuthenticate();
  222. /* * ***********
  223. * Functions *
  224. * *********** */
  225. /** Print paging menu
  226. *
  227. * Display the page option: [ < ][ 1 ] .. [ 8 ][ 9 ][ 10 ][ 11 ][ 12 ][ 13 ][ 14 ][ 15 ][ 16 ] .. [ 34 ][ > ]
  228. *
  229. * @param int $amount Total number of items
  230. * @param int $rowamount Per page number of items
  231. * @param int $id Page specific ID (Zone ID, Template ID, etc)
  232. *
  233. * @return null
  234. */
  235. function show_pages($amount, $rowamount, $id = '') {
  236. if ($amount > $rowamount) {
  237. $num = 8;
  238. $poutput = '';
  239. $lastpage = ceil($amount / $rowamount);
  240. $startpage = 1;
  241. if (!isset($_GET["start"]))
  242. $_GET["start"] = 1;
  243. $start = $_GET["start"];
  244. if ($lastpage > $num & $start > ($num / 2)) {
  245. $startpage = ($start - ($num / 2));
  246. }
  247. echo _('Show page') . ":<br>";
  248. if ($lastpage > $num & $start > 1) {
  249. $poutput .= '<a href=" ' . htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES);
  250. $poutput .= '?start=' . ($start - 1);
  251. if ($id != '')
  252. $poutput .= '&id=' . $id;
  253. $poutput .= '">';
  254. $poutput .= '[ < ]';
  255. $poutput .= '</a>';
  256. }
  257. if ($start != 1) {
  258. $poutput .= '<a href=" ' . htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES);
  259. $poutput .= '?start=1';
  260. if ($id != '')
  261. $poutput .= '&id=' . $id;
  262. $poutput .= '">';
  263. $poutput .= '[ 1 ]';
  264. $poutput .= '</a>';
  265. if ($startpage > 2)
  266. $poutput .= ' .. ';
  267. }
  268. for ($i = $startpage; $i <= min(($startpage + $num), $lastpage); $i++) {
  269. if ($start == $i) {
  270. $poutput .= '[ <b>' . $i . '</b> ]';
  271. } elseif ($i != $lastpage & $i != 1) {
  272. $poutput .= '<a href=" ' . htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES);
  273. $poutput .= '?start=' . $i;
  274. if ($id != '')
  275. $poutput .= '&id=' . $id;
  276. $poutput .= '">';
  277. $poutput .= '[ ' . $i . ' ]';
  278. $poutput .= '</a>';
  279. }
  280. }
  281. if ($start != $lastpage) {
  282. if (min(($startpage + $num), $lastpage) < ($lastpage - 1))
  283. $poutput .= ' .. ';
  284. $poutput .= '<a href=" ' . htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES);
  285. $poutput .= '?start=' . $lastpage;
  286. if ($id != '')
  287. $poutput .= '&id=' . $id;
  288. $poutput .= '">';
  289. $poutput .= '[ ' . $lastpage . ' ]';
  290. $poutput .= '</a>';
  291. }
  292. if ($lastpage > $num & $start < $lastpage) {
  293. $poutput .= '<a href=" ' . htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES);
  294. $poutput .= '?start=' . ($start + 1);
  295. if ($id != '')
  296. $poutput .= '&id=' . $id;
  297. $poutput .= '">';
  298. $poutput .= '[ > ]';
  299. $poutput .= '</a>';
  300. }
  301. echo $poutput;
  302. }
  303. }
  304. /** Print alphanumeric paging menu
  305. *
  306. * Display the alphabetic option: [0-9] [a] [b] .. [z]
  307. *
  308. * @param string $letterstart Starting letter/number or 'all'
  309. * @param boolean $userid unknown usage
  310. *
  311. * @return null
  312. */
  313. function show_letters($letterstart, $userid = true) {
  314. echo _('Show zones beginning with') . ":<br>";
  315. $letter = "[[:digit:]]";
  316. if ($letterstart == "1") {
  317. echo "<span class=\"lettertaken\">[ 0-9 ]</span> ";
  318. } elseif (zone_letter_start($letter, $userid)) {
  319. echo "<a href=\"" . htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) . "?letter=1\">[ 0-9 ]</a> ";
  320. } else {
  321. echo "[ <span class=\"letternotavailable\">0-9</span> ] ";
  322. }
  323. foreach (range('a', 'z') as $letter) {
  324. if ($letter == $letterstart) {
  325. echo "<span class=\"lettertaken\">[ " . $letter . " ]</span> ";
  326. } elseif (zone_letter_start($letter, $userid)) {
  327. echo "<a href=\"" . htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) . "?letter=" . $letter . "\">[ " . $letter . " ]</a> ";
  328. } else {
  329. echo "[ <span class=\"letternotavailable\">" . $letter . "</span> ] ";
  330. }
  331. }
  332. if ($letterstart == '_') {
  333. echo "<span class=\"lettertaken\">[ _ ]</span> ";
  334. } elseif (zone_letter_start('_', $userid)) {
  335. echo "<a href=\"" . htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) . "?letter=_\">[ _ ]</a> ";
  336. } else {
  337. echo "[ <span class=\"letternotavailable\">_</span> ] ";
  338. }
  339. if ($letterstart == 'all') {
  340. echo "<span class=\"lettertaken\">[ Show all ]</span>";
  341. } else {
  342. echo "<a href=\"" . htmlentities($_SERVER["PHP_SELF"], ENT_QUOTES) . "?letter=all\">[ Show all ]</a> ";
  343. }
  344. }
  345. /** Check if any zones start with letter
  346. *
  347. * @param string $letter Starting Letter
  348. * @param boolean $userid unknown usage
  349. *
  350. * @return int 1 if rows found, 0 otherwise
  351. */
  352. function zone_letter_start($letter, $userid = true) {
  353. global $db;
  354. global $sql_regexp;
  355. $query = "SELECT
  356. domains.id AS domain_id,
  357. zones.owner,
  358. domains.name AS domainname
  359. FROM domains
  360. LEFT JOIN zones ON domains.id=zones.domain_id
  361. WHERE substring(domains.name,1,1) " . $sql_regexp . " " . $db->quote("^" . $letter, 'text');
  362. $db->setLimit(1);
  363. $result = $db->queryOne($query);
  364. return ($result ? 1 : 0);
  365. }
  366. /** Print success message (toolkit.inc)
  367. *
  368. * @param string $msg Success message
  369. *
  370. * @return null
  371. */
  372. function success($msg) {
  373. if ($msg) {
  374. echo " <div class=\"success\">" . $msg . "</div>\n";
  375. } else {
  376. echo " <div class=\"success\">" . _('Something has been successfully performed. What exactly, however, will remain a mystery.') . "</div>\n";
  377. }
  378. }
  379. /** Print message
  380. *
  381. * Something has been done nicely, display a message and a back button.
  382. *
  383. * @param string $msg Message
  384. *
  385. * @return null
  386. */
  387. function message($msg) {
  388. include_once("header.inc.php");
  389. ?>
  390. <P><TABLE CLASS="messagetable"><TR><TD CLASS="message"><H2><?php echo _('Success!'); ?></H2>
  391. <BR>
  392. <FONT STYLE="font-weight: Bold">
  393. <P>
  394. <?php
  395. if ($msg) {
  396. echo nl2br($msg);
  397. } else {
  398. echo _('Successful!');
  399. }
  400. ?>
  401. </P>
  402. <BR>
  403. <P>
  404. <a href="javascript:history.go(-1)">&lt;&lt; <?php echo _('back'); ?></a></FONT>
  405. </P>
  406. </TD></TR></TABLE></P>
  407. <?php
  408. include_once("footer.inc.php");
  409. }
  410. /** Send 302 Redirect with optional argument
  411. *
  412. * Reroute a user to a cleanpage of (if passed) arg
  413. *
  414. * @param string $arg argument string to add to url
  415. *
  416. * @return null
  417. */
  418. function clean_page($arg = '') {
  419. if (!$arg) {
  420. header("Location: " . htmlentities($_SERVER['SCRIPT_NAME'], ENT_QUOTES) . "?time=" . time());
  421. exit;
  422. } else {
  423. if (preg_match('!\?!si', $arg)) {
  424. $add = "&time=";
  425. } else {
  426. $add = "?time=";
  427. }
  428. header("Location: $arg$add" . time());
  429. exit;
  430. }
  431. }
  432. /** Print active status
  433. *
  434. * @param int $res status, 0 for inactive, 1 active
  435. *
  436. * @return string html containing status
  437. */
  438. function get_status($res) {
  439. if ($res == '0') {
  440. return "<FONT CLASS=\"inactive\">" . _('Inactive') . "</FONT>";
  441. } elseif ($res == '1') {
  442. return "<FONT CLASS=\"active\">" . _('Active') . "</FONT>";
  443. }
  444. }
  445. /** Validate email address string
  446. *
  447. * @param string $address email address string
  448. *
  449. * @return boolean true if valid, false otherwise
  450. */
  451. function is_valid_email($address) {
  452. $fields = preg_split("/@/", $address, 2);
  453. if ((!preg_match("/^[0-9a-z]([-_.]?[0-9a-z])*$/i", $fields[0])) || (!isset($fields[1]) || $fields[1] == '' || !is_valid_hostname_fqdn($fields[1], 0))) {
  454. return false;
  455. }
  456. return true;
  457. }
  458. /** Validate numeric string
  459. *
  460. * @param string $string number
  461. *
  462. * @return boolean true if number, false otherwise
  463. */
  464. function v_num($string) {
  465. if (!preg_match("/^[0-9]+$/i", $string)) {
  466. return false;
  467. } else {
  468. return true;
  469. }
  470. }
  471. /** Debug print
  472. *
  473. * @param string $var debug statement
  474. *
  475. * @return null
  476. */
  477. function debug_print($var) {
  478. echo "<pre style=\"border: 2px solid blue;\">\n";
  479. if (is_array($var)) {
  480. print_r($var);
  481. } else {
  482. echo $var;
  483. }
  484. echo "</pre>\n";
  485. }
  486. /** Set timezone (required for PHP5)
  487. *
  488. * Set timezone to configured tz or UTC it not set
  489. *
  490. * @return null
  491. */
  492. function set_timezone() {
  493. global $timezone;
  494. if (function_exists('date_default_timezone_set')) {
  495. if (isset($timezone)) {
  496. date_default_timezone_set($timezone);
  497. } else if (!ini_get('date.timezone')) {
  498. date_default_timezone_set('UTC');
  499. }
  500. }
  501. }
  502. /** Generate random salt for encryption
  503. *
  504. * @param int $len salt length (default=5)
  505. *
  506. * @return string salt string
  507. */
  508. function generate_salt($len = 5) {
  509. $valid_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890@#$%^*()_-!';
  510. $valid_len = strlen($valid_characters) - 1;
  511. $salt = "";
  512. for ($i = 0; $i < $len; $i++) {
  513. $salt .= $valid_characters[rand(0, $valid_len)];
  514. }
  515. return $salt;
  516. }
  517. /** Extract salt from password
  518. *
  519. * @param string $password salted password
  520. *
  521. * @return string salt
  522. */
  523. function extract_salt($password) {
  524. return substr(strchr($password, ':'), 1);
  525. }
  526. /** Generate salted password
  527. *
  528. * @param string $salt salt
  529. * @param string $pass password
  530. *
  531. * @return string salted password
  532. */
  533. function mix_salt($salt, $pass) {
  534. return md5($salt . $pass) . ':' . $salt;
  535. }
  536. /** Generate random salt and salted password
  537. *
  538. * @param string $pass password
  539. *
  540. * @return salted password
  541. */
  542. function gen_mix_salt($pass) {
  543. $salt = generate_salt();
  544. return mix_salt($salt, $pass);
  545. }
  546. function do_log($syslog_message,$priority){
  547. global $syslog_use, $syslog_ident, $syslog_facility;
  548. if ($syslog_use) {
  549. openlog($syslog_ident, LOG_PERROR, $syslog_facility);
  550. syslog($priority, $syslog_message);
  551. closelog();
  552. }
  553. }
  554. function log_error($syslog_message) {
  555. do_log($syslog_message,LOG_ERR);
  556. }
  557. function log_warn($syslog_message) {
  558. do_log($syslog_message,LOG_WARNING);
  559. }
  560. function log_notice($syslog_message) {
  561. do_log($syslog_message,LOG_NOTICE);
  562. }
  563. function log_info($syslog_message) {
  564. do_log($syslog_message,LOG_INFO);
  565. }