functions.inc.php 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821
  1. <?php
  2. /**
  3. * Postfix Admin
  4. *
  5. * LICENSE
  6. * This source file is subject to the GPL license that is bundled with
  7. * this package in the file LICENSE.TXT.
  8. *
  9. * Further details on the project are available at http://postfixadmin.sf.net
  10. *
  11. * @version $Id: functions.inc.php 1799 2015-09-26 10:39:05Z christian_boltz $
  12. * @license GNU GPL v2 or later.
  13. *
  14. * File: functions.inc.php
  15. * Contains re-usable code.
  16. */
  17. $version = '2.93';
  18. /**
  19. * check_session
  20. * Action: Check if a session already exists, if not redirect to login.php
  21. * Call: check_session ()
  22. * @return String username (e.g. foo@example.com)
  23. */
  24. function authentication_get_username() {
  25. if (defined('POSTFIXADMIN_CLI')) {
  26. return 'CLI';
  27. }
  28. if (defined('POSTFIXADMIN_SETUP')) {
  29. return 'SETUP.PHP';
  30. }
  31. if (!isset($_SESSION['sessid'])) {
  32. header ("Location: login.php");
  33. exit(0);
  34. }
  35. $SESSID_USERNAME = $_SESSION['sessid']['username'];
  36. return $SESSID_USERNAME;
  37. }
  38. /**
  39. * Returns the type of user - either 'user' or 'admin'
  40. * Returns false if neither (E.g. if not logged in)
  41. * @return String admin or user or (boolean) false.
  42. */
  43. function authentication_get_usertype() {
  44. if(isset($_SESSION['sessid'])) {
  45. if(isset($_SESSION['sessid']['type'])) {
  46. return $_SESSION['sessid']['type'];
  47. }
  48. }
  49. return false;
  50. }
  51. /**
  52. *
  53. * Used to determine whether a user has a particular role.
  54. * @param String role-name. (E.g. admin, global-admin or user)
  55. * @return boolean True if they have the requested role in their session.
  56. * Note, user < admin < global-admin
  57. */
  58. function authentication_has_role($role) {
  59. if(isset($_SESSION['sessid'])) {
  60. if(isset($_SESSION['sessid']['roles'])) {
  61. if(in_array($role, $_SESSION['sessid']['roles'])) {
  62. return true;
  63. }
  64. }
  65. }
  66. return false;
  67. }
  68. /**
  69. * Used to enforce that $user has a particular role when
  70. * viewing a page.
  71. * If they are lacking a role, redirect them to login.php
  72. *
  73. * Note, user < admin < global-admin
  74. */
  75. function authentication_require_role($role) {
  76. // redirect to appropriate page?
  77. if(authentication_has_role($role)) {
  78. return True;
  79. }
  80. header("Location: login.php");
  81. exit(0);
  82. }
  83. /**
  84. * Add an error message for display on the next page that is rendered.
  85. * @param String/Array message(s) to show.
  86. *
  87. * Stores string in session. Flushed through header template.
  88. * @see _flash_string()
  89. */
  90. function flash_error($string) {
  91. _flash_string('error', $string);
  92. }
  93. /**
  94. * Used to display an info message on successful update.
  95. * @param String/Array message(s) to show.
  96. * Stores data in session.
  97. * @see _flash_string()
  98. */
  99. function flash_info($string) {
  100. _flash_string('info', $string);
  101. }
  102. /**
  103. * 'Private' method used for flash_info() and flash_error().
  104. */
  105. function _flash_string($type, $string) {
  106. if (is_array($string)) {
  107. foreach ($string as $singlestring) {
  108. _flash_string($type, $singlestring);
  109. }
  110. return;
  111. }
  112. if(!isset($_SESSION['flash'])) {
  113. $_SESSION['flash'] = array();
  114. }
  115. if(!isset($_SESSION['flash'][$type])) {
  116. $_SESSION['flash'][$type] = array();
  117. }
  118. $_SESSION['flash'][$type][] = $string;
  119. }
  120. //
  121. // check_language
  122. // Action: checks what language the browser uses
  123. // Call: check_language
  124. // Parameter: $use_post - set to 0 if $_POST should NOT be read
  125. //
  126. function check_language ($use_post = 1) {
  127. global $supported_languages; # from languages/languages.php
  128. $lang = Config::read('default_language');
  129. if(isset($_SERVER['HTTP_ACCEPT_LANGUAGE'])) {
  130. $lang_array = preg_split ('/(\s*,\s*)/', $_SERVER['HTTP_ACCEPT_LANGUAGE']);
  131. if (safecookie('lang')) {
  132. array_unshift($lang_array, safecookie('lang')); # prefer language from cookie
  133. }
  134. if ( $use_post && safepost('lang')) {
  135. array_unshift($lang_array, safepost('lang')); # but prefer $_POST['lang'] even more
  136. }
  137. for($i = 0; $i < count($lang_array); $i++) {
  138. $lang_next = $lang_array[$i];
  139. $lang_next = strtolower(trim($lang_next));
  140. $lang_next = preg_replace('/;.*$/', '', $lang_next); # remove things like ";q=0.8"
  141. if(array_key_exists($lang_next, $supported_languages)) {
  142. $lang = $lang_next;
  143. break;
  144. }
  145. }
  146. }
  147. return $lang;
  148. }
  149. //
  150. // language_selector
  151. // Action: returns a language selector dropdown with the browser (or cookie) language preselected
  152. // Call: language_selector()
  153. //
  154. function language_selector() {
  155. global $supported_languages; # from languages/languages.php
  156. $current_lang = check_language();
  157. $selector = '<select name="lang" xml:lang="en" dir="ltr">';
  158. foreach($supported_languages as $lang => $lang_name) {
  159. if ($lang == $current_lang) {
  160. $selected = ' selected="selected"';
  161. } else {
  162. $selected = '';
  163. }
  164. $selector .= "<option value='$lang'$selected>$lang_name</option>";
  165. }
  166. $selector .= "</select>";
  167. return $selector;
  168. }
  169. /**
  170. * Checks if a domain is valid
  171. * @param string $domain
  172. * @return empty string if the domain is valid, otherwise string with the errormessage
  173. *
  174. * TODO: make check_domain able to handle as example .local domains
  175. * TODO: skip DNS check if the domain exists in PostfixAdmin?
  176. */
  177. function check_domain ($domain) {
  178. if (!preg_match ('/^([-0-9A-Z]+\.)+' . '([0-9A-Z]){2,13}$/i', ($domain))) {
  179. return sprintf(Config::lang('pInvalidDomainRegex'), htmlentities($domain));
  180. }
  181. if (Config::bool('emailcheck_resolve_domain') && 'WINDOWS'!=(strtoupper(substr(php_uname('s'), 0, 7)))) {
  182. // Look for an AAAA, A, or MX record for the domain
  183. if(function_exists('checkdnsrr')) {
  184. $start = microtime(true); # check for slow nameservers, part 1
  185. // AAAA (IPv6) is only available in PHP v. >= 5
  186. if (version_compare(phpversion(), "5.0.0", ">=") && checkdnsrr($domain,'AAAA')) {
  187. $retval = '';
  188. } elseif (checkdnsrr($domain,'A')) {
  189. $retval = '';
  190. } elseif (checkdnsrr($domain,'MX')) {
  191. $retval = '';
  192. } else {
  193. $retval = sprintf(Config::lang('pInvalidDomainDNS'), htmlentities($domain));
  194. }
  195. $end = microtime(true); # check for slow nameservers, part 2
  196. $time_needed = $end - $start;
  197. if ($time_needed > 2) {
  198. error_log("Warning: slow nameserver - lookup for $domain took $time_needed seconds");
  199. }
  200. return $retval;
  201. } else {
  202. return 'emailcheck_resolve_domain is enabled, but function (checkdnsrr) missing!';
  203. }
  204. }
  205. return '';
  206. }
  207. /**
  208. * check_email
  209. * Checks if an email is valid - if it is, return true, else false.
  210. * @param String $email - a string that may be an email address.
  211. * @return empty string if it's a valid email address, otherwise string with the errormessage
  212. * TODO: make check_email able to handle already added domains
  213. */
  214. function check_email ($email) {
  215. $ce_email=$email;
  216. //strip the vacation domain out if we are using it
  217. //and change from blah#foo.com@autoreply.foo.com to blah@foo.com
  218. if (Config::bool('vacation')) {
  219. $vacation_domain = Config::read('vacation_domain');
  220. $ce_email = preg_replace("/@$vacation_domain\$/", '', $ce_email);
  221. $ce_email = preg_replace("/#/", '@', $ce_email);
  222. }
  223. // Perform non-domain-part sanity checks
  224. if (!preg_match ('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_{|}~]+' . '@' . '[^@]+$/i', $ce_email)) {
  225. return Config::lang_f('pInvalidMailRegex', $email);
  226. }
  227. // Determine domain name
  228. $matches=array();
  229. if (!preg_match('|@(.+)$|',$ce_email,$matches)) {
  230. return Config::lang_f('pInvalidMailRegex', $email);
  231. }
  232. $domain=$matches[1];
  233. # check domain name
  234. return check_domain($domain);
  235. }
  236. /**
  237. * Clean a string, escaping any meta characters that could be
  238. * used to disrupt an SQL string. i.e. "'" => "\'" etc.
  239. *
  240. * @param String (or Array)
  241. * @return String (or Array) of cleaned data, suitable for use within an SQL
  242. * statement.
  243. */
  244. function escape_string ($string) {
  245. global $CONF;
  246. // if the string is actually an array, do a recursive cleaning.
  247. // Note, the array keys are not cleaned.
  248. if(is_array($string)) {
  249. $clean = array();
  250. foreach(array_keys($string) as $row) {
  251. $clean[$row] = escape_string($string[$row]);
  252. }
  253. return $clean;
  254. }
  255. if (get_magic_quotes_gpc ()) {
  256. $string = stripslashes($string);
  257. }
  258. if (!is_numeric($string)) {
  259. $link = db_connect();
  260. if ($CONF['database_type'] == "mysql") {
  261. $escaped_string = mysql_real_escape_string($string, $link);
  262. }
  263. if ($CONF['database_type'] == "mysqli") {
  264. $escaped_string = mysqli_real_escape_string($link, $string);
  265. }
  266. if (db_pgsql()) {
  267. // php 5.2+ allows for $link to be specified.
  268. if (version_compare(phpversion(), "5.2.0", ">=")) {
  269. $escaped_string = pg_escape_string($link, $string);
  270. } else {
  271. $escaped_string = pg_escape_string($string);
  272. }
  273. }
  274. } else {
  275. $escaped_string = $string;
  276. }
  277. return $escaped_string;
  278. }
  279. /**
  280. * safeget
  281. * Action: get value from $_GET[$param], or $default if $_GET[$param] is not set
  282. * Call: $param = safeget('param') # replaces $param = $_GET['param']
  283. * - or -
  284. * $param = safeget('param', 'default')
  285. *
  286. * @param String parameter name.
  287. * @param String (optional) - default value if key is not set.
  288. * @return String
  289. */
  290. function safeget ($param, $default="") {
  291. $retval=$default;
  292. if (isset($_GET[$param])) $retval=$_GET[$param];
  293. return $retval;
  294. }
  295. /**
  296. * safepost - similar to safeget()
  297. * @see safeget()
  298. * @param String parameter name
  299. * @param String (optional) default value (defaults to "")
  300. * @return String - value in $_POST[$param] or $default
  301. * same as safeget, but for $_POST
  302. */
  303. function safepost ($param, $default="") {
  304. $retval=$default;
  305. if (isset($_POST[$param])) $retval=$_POST[$param];
  306. return $retval;
  307. }
  308. /**
  309. * safeserver
  310. * @see safeget()
  311. * @param String $param
  312. * @param String $default (optional)
  313. * @return String value from $_SERVER[$param] or $default
  314. */
  315. function safeserver ($param, $default="") {
  316. $retval=$default;
  317. if (isset($_SERVER[$param])) $retval=$_SERVER[$param];
  318. return $retval;
  319. }
  320. /**
  321. * safecookie
  322. * @see safeget()
  323. * @param String $param
  324. * @param String $default (optional)
  325. * @return String value from $_COOKIE[$param] or $default
  326. */
  327. function safecookie ($param, $default="") {
  328. $retval=$default;
  329. if (isset($_COOKIE[$param])) $retval=$_COOKIE[$param];
  330. return $retval;
  331. }
  332. /**
  333. * safesession
  334. * @see safeget()
  335. * @param String $param
  336. * @param String $default (optional)
  337. * @return String value from $_SESSION[$param] or $default
  338. */
  339. function safesession ($param, $default="") {
  340. $retval=$default;
  341. if (isset($_SESSION[$param])) $retval=$_SESSION[$param];
  342. return $retval;
  343. }
  344. /**
  345. * pacol
  346. * @param int $allow_editing
  347. * @param int $display_in_form
  348. * @param int display_in_list
  349. * @param String $type
  350. * @param String PALANG_label
  351. * @param String PALANG_desc
  352. * @param any optional $default
  353. * @param array optional $options
  354. * @param int or $not_in_db - if array, can contain the remaining parameters as associated array
  355. * @param ...
  356. * @return array for $struct
  357. */
  358. function pacol($allow_editing, $display_in_form, $display_in_list, $type, $PALANG_label, $PALANG_desc, $default = "", $options = array(), $multiopt=0, $dont_write_to_db=0, $select="", $extrafrom="", $linkto="") {
  359. if ($PALANG_label != '') $PALANG_label = Config::lang($PALANG_label);
  360. if ($PALANG_desc != '') $PALANG_desc = Config::lang($PALANG_desc );
  361. if (is_array($multiopt)) { # remaining parameters provided in named array
  362. $not_in_db = 0; # keep default value
  363. foreach ($multiopt as $key => $value) {
  364. $$key = $value; # extract everything to the matching variable
  365. }
  366. } else {
  367. $not_in_db = $multiopt;
  368. }
  369. return array(
  370. 'editable' => $allow_editing,
  371. 'display_in_form' => $display_in_form,
  372. 'display_in_list' => $display_in_list,
  373. 'type' => $type,
  374. 'label' => $PALANG_label, # $PALANG field label
  375. 'desc' => $PALANG_desc, # $PALANG field description
  376. 'default' => $default,
  377. 'options' => $options,
  378. 'not_in_db' => $not_in_db,
  379. 'dont_write_to_db' => $dont_write_to_db,
  380. 'select' => $select, # replaces the field name after SELECT
  381. 'extrafrom' => $extrafrom, # added after FROM xy - useful for JOINs etc.
  382. 'linkto' => $linkto, # make the value a link - %s will be replaced with the ID
  383. );
  384. }
  385. //
  386. // get_domain_properties
  387. // Action: Get all the properties of a domain.
  388. // Call: get_domain_properties (string domain)
  389. //
  390. function get_domain_properties ($domain) {
  391. $handler = new DomainHandler();
  392. if (!$handler->init($domain)) {
  393. die("Error: " . join("\n", $handler->errormsg));
  394. }
  395. if (!$handler->view()) {
  396. die("Error: " . join("\n", $handler->errormsg));
  397. }
  398. $result = $handler->result();
  399. return $result;
  400. }
  401. /**
  402. * create_page_browser
  403. * Action: Get page browser for a long list of mailboxes, aliases etc.
  404. * Call: $pagebrowser = create_page_browser('table.field', 'query', 50) # replaces $param = $_GET['param']
  405. *
  406. * @param String idxfield - database field name to use as title
  407. * @param String query - core part of the query (starting at "FROM")
  408. * @return String
  409. */
  410. function create_page_browser($idxfield, $querypart) {
  411. global $CONF;
  412. $page_size = (int) $CONF['page_size'];
  413. $label_len = 2;
  414. $pagebrowser = array();
  415. if ($page_size < 2) { # will break the page browser
  416. die('$CONF[\'page_size\'] must be 2 or more!');
  417. }
  418. # get number of rows
  419. $query = "SELECT count(*) as counter FROM (SELECT $idxfield $querypart) AS tmp";
  420. $result = db_query ($query);
  421. if ($result['rows'] > 0) {
  422. $row = db_array ($result['result']);
  423. $count_results = $row['counter'] -1; # we start counting at 0, not 1
  424. }
  425. # echo "<p>rows: " . ($count_results +1) . " --- $query";
  426. if ($count_results < $page_size) {
  427. return array(); # only one page - no pagebrowser required
  428. }
  429. # init row counter
  430. $initcount = "SET @row=-1";
  431. if (db_pgsql()) {
  432. $initcount = "CREATE TEMPORARY SEQUENCE rowcount MINVALUE 0";
  433. }
  434. $result = db_query($initcount);
  435. # get labels for relevant rows (first and last of each page)
  436. $page_size_zerobase = $page_size - 1;
  437. $query = "
  438. SELECT * FROM (
  439. SELECT $idxfield AS label, @row := @row + 1 AS row $querypart
  440. ) idx WHERE MOD(idx.row, $page_size) IN (0,$page_size_zerobase) OR idx.row = $count_results
  441. ";
  442. if (db_pgsql()) {
  443. $query = "
  444. SELECT * FROM (
  445. SELECT $idxfield AS label, nextval('rowcount') AS row $querypart
  446. ) idx WHERE MOD(idx.row, $page_size) IN (0,$page_size_zerobase) OR idx.row = $count_results
  447. ";
  448. }
  449. # echo "<p>$query";
  450. # TODO: $query is MySQL-specific
  451. # PostgreSQL:
  452. # http://www.postgresql.org/docs/8.1/static/sql-createsequence.html
  453. # http://www.postgresonline.com/journal/archives/79-Simulating-Row-Number-in-PostgreSQL-Pre-8.4.html
  454. # http://www.pg-forum.de/sql/1518-nummerierung-der-abfrageergebnisse.html
  455. # CREATE TEMPORARY SEQUENCE foo MINVALUE 0 MAXVALUE $page_size_zerobase CYCLE
  456. # afterwards: DROP SEQUENCE foo
  457. $result = db_query ($query);
  458. if ($result['rows'] > 0) {
  459. while ($row = db_array ($result['result'])) {
  460. if ($row2 = db_array ($result['result'])) {
  461. $label = substr($row['label'],0,$label_len) . '-' . substr($row2['label'],0,$label_len);
  462. $pagebrowser[] = $label;
  463. } else { # only one row remaining
  464. $label = substr($row['label'],0,$label_len);
  465. $pagebrowser[] = $label;
  466. }
  467. }
  468. }
  469. if (db_pgsql()) {
  470. db_query ("DROP SEQUENCE rowcount");
  471. }
  472. return $pagebrowser;
  473. }
  474. //
  475. // divide_quota
  476. // Action: Recalculates the quota from MBs to bytes (divide, /)
  477. // Call: divide_quota (string $quota)
  478. //
  479. function divide_quota ($quota) {
  480. if ($quota == -1) return $quota;
  481. $value = round($quota / Config::read('quota_multiplier'),2);
  482. return $value;
  483. }
  484. //
  485. // check_owner
  486. // Action: Checks if the admin is the owner of the domain (or global-admin)
  487. // Call: check_owner (string admin, string domain)
  488. //
  489. function check_owner ($username, $domain) {
  490. $table_domain_admins = table_by_key('domain_admins');
  491. $E_username = escape_string($username);
  492. $E_domain = escape_string($domain);
  493. $result = db_query ("SELECT 1 FROM $table_domain_admins WHERE username='$E_username' AND (domain='$E_domain' OR domain='ALL') AND active='1'");
  494. if ($result['rows'] == 1 || $result['rows'] == 2) { # "ALL" + specific domain permissions is possible
  495. # TODO: if superadmin, check if given domain exists in the database
  496. return true;
  497. } else {
  498. if ($result['rows'] > 2) { # more than 2 results means something really strange happened...
  499. flash_error("Permission check returned multiple results. Please go to 'edit admin' for your username and press the save "
  500. . "button once to fix the database. If this doesn't help, open a bugreport.");
  501. }
  502. return false;
  503. }
  504. }
  505. /**
  506. * List domains for an admin user.
  507. * @param String $username
  508. * @return array of domain names.
  509. */
  510. function list_domains_for_admin ($username) {
  511. $table_domain = table_by_key('domain');
  512. $table_domain_admins = table_by_key('domain_admins');
  513. $E_username = escape_string($username);
  514. $query = "SELECT $table_domain.domain FROM $table_domain ";
  515. $condition[] = "$table_domain.domain != 'ALL'";
  516. $result = db_query ("SELECT username FROM $table_domain_admins WHERE username='$E_username' AND domain='ALL'");
  517. if ($result['rows'] < 1) { # not a superadmin
  518. $query .= " LEFT JOIN $table_domain_admins ON $table_domain.domain=$table_domain_admins.domain ";
  519. $condition[] = "$table_domain_admins.username='$E_username' ";
  520. $condition[] = "$table_domain.active='" . db_get_boolean(true) . "'"; # TODO: does it really make sense to exclude inactive...
  521. $condition[] = "$table_domain.backupmx='" . db_get_boolean(False) . "'"; # TODO: ... and backupmx domains for non-superadmins?
  522. }
  523. $query .= " WHERE " . join(' AND ', $condition);
  524. $query .= " ORDER BY $table_domain.domain";
  525. $list = array ();
  526. $result = db_query ($query);
  527. if ($result['rows'] > 0) {
  528. $i = 0;
  529. while ($row = db_array ($result['result'])) {
  530. $list[$i] = $row['domain'];
  531. $i++;
  532. }
  533. }
  534. return $list;
  535. }
  536. //
  537. // list_domains
  538. // Action: List all available domains.
  539. // Call: list_domains ()
  540. //
  541. function list_domains () {
  542. $list = array();
  543. $table_domain = table_by_key('domain');
  544. $result = db_query ("SELECT domain FROM $table_domain WHERE domain!='ALL' ORDER BY domain");
  545. if ($result['rows'] > 0) {
  546. $i = 0;
  547. while ($row = db_array ($result['result'])) {
  548. $list[$i] = $row['domain'];
  549. $i++;
  550. }
  551. }
  552. return $list;
  553. }
  554. //
  555. // list_admins
  556. // Action: Lists all the admins
  557. // Call: list_admins ()
  558. //
  559. // was admin_list_admins
  560. //
  561. function list_admins () {
  562. $handler = new AdminHandler();
  563. $handler->getList('');
  564. return $handler->result();
  565. }
  566. //
  567. // encode_header
  568. // Action: Encode a string according to RFC 1522 for use in headers if it contains 8-bit characters.
  569. // Call: encode_header (string header, string charset)
  570. //
  571. function encode_header ($string, $default_charset = "utf-8") {
  572. if (strtolower ($default_charset) == 'iso-8859-1') {
  573. $string = str_replace ("\240",' ',$string);
  574. }
  575. $j = strlen ($string);
  576. $max_l = 75 - strlen ($default_charset) - 7;
  577. $aRet = array ();
  578. $ret = '';
  579. $iEncStart = $enc_init = false;
  580. $cur_l = $iOffset = 0;
  581. for ($i = 0; $i < $j; ++$i) {
  582. switch ($string{$i}) {
  583. case '=':
  584. case '<':
  585. case '>':
  586. case ',':
  587. case '?':
  588. case '_':
  589. if ($iEncStart === false) {
  590. $iEncStart = $i;
  591. }
  592. $cur_l+=3;
  593. if ($cur_l > ($max_l-2)) {
  594. $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
  595. $aRet[] = "=?$default_charset?Q?$ret?=";
  596. $iOffset = $i;
  597. $cur_l = 0;
  598. $ret = '';
  599. $iEncStart = false;
  600. } else {
  601. $ret .= sprintf ("=%02X",ord($string{$i}));
  602. }
  603. break;
  604. case '(':
  605. case ')':
  606. if ($iEncStart !== false) {
  607. $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
  608. $aRet[] = "=?$default_charset?Q?$ret?=";
  609. $iOffset = $i;
  610. $cur_l = 0;
  611. $ret = '';
  612. $iEncStart = false;
  613. }
  614. break;
  615. case ' ':
  616. if ($iEncStart !== false) {
  617. $cur_l++;
  618. if ($cur_l > $max_l) {
  619. $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
  620. $aRet[] = "=?$default_charset?Q?$ret?=";
  621. $iOffset = $i;
  622. $cur_l = 0;
  623. $ret = '';
  624. $iEncStart = false;
  625. } else {
  626. $ret .= '_';
  627. }
  628. }
  629. break;
  630. default:
  631. $k = ord ($string{$i});
  632. if ($k > 126) {
  633. if ($iEncStart === false) {
  634. // do not start encoding in the middle of a string, also take the rest of the word.
  635. $sLeadString = substr ($string,0,$i);
  636. $aLeadString = explode (' ',$sLeadString);
  637. $sToBeEncoded = array_pop ($aLeadString);
  638. $iEncStart = $i - strlen ($sToBeEncoded);
  639. $ret .= $sToBeEncoded;
  640. $cur_l += strlen ($sToBeEncoded);
  641. }
  642. $cur_l += 3;
  643. // first we add the encoded string that reached it's max size
  644. if ($cur_l > ($max_l-2)) {
  645. $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
  646. $aRet[] = "=?$default_charset?Q?$ret?= ";
  647. $cur_l = 3;
  648. $ret = '';
  649. $iOffset = $i;
  650. $iEncStart = $i;
  651. }
  652. $enc_init = true;
  653. $ret .= sprintf ("=%02X", $k);
  654. } else {
  655. if ($iEncStart !== false) {
  656. $cur_l++;
  657. if ($cur_l > $max_l) {
  658. $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
  659. $aRet[] = "=?$default_charset?Q?$ret?=";
  660. $iEncStart = false;
  661. $iOffset = $i;
  662. $cur_l = 0;
  663. $ret = '';
  664. } else {
  665. $ret .= $string{$i};
  666. }
  667. }
  668. }
  669. break;
  670. # end switch
  671. }
  672. }
  673. if ($enc_init) {
  674. if ($iEncStart !== false) {
  675. $aRet[] = substr ($string,$iOffset,$iEncStart-$iOffset);
  676. $aRet[] = "=?$default_charset?Q?$ret?=";
  677. } else {
  678. $aRet[] = substr ($string,$iOffset);
  679. }
  680. $string = implode ('',$aRet);
  681. }
  682. return $string;
  683. }
  684. //
  685. // generate_password
  686. // Action: Generates a random password
  687. // Call: generate_password ()
  688. //
  689. function generate_password () {
  690. // length of the generated password
  691. $length = 8;
  692. // define possible characters
  693. $possible = "2345678923456789abcdefghijkmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; # skip 0 and 1 to avoid confusion with O and l
  694. // add random characters to $password until $length is reached
  695. $password = "";
  696. while (strlen($password) < $length) {
  697. // pick a random character from the possible ones
  698. $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  699. // we don't want this character if it's already in the password
  700. if (!strstr($password, $char)) {
  701. $password .= $char;
  702. }
  703. }
  704. return $password;
  705. }
  706. /**
  707. * Check if a password is strong enough based on the conditions in $CONF['password_validation']
  708. * @param String $password
  709. * @return array of error messages, or empty array if the password is ok
  710. */
  711. function validate_password($password) {
  712. $val_conf = Config::read('password_validation');
  713. $result = array();
  714. $minlen = (int) Config::read('min_password_length'); # used up to 2.3.x - check it for backward compatibility
  715. if ($minlen > 0) {
  716. $val_conf['/.{' . $minlen . '}/'] = "password_too_short $minlen";
  717. }
  718. foreach ($val_conf as $regex => $message) {
  719. if (!preg_match($regex, $password)) {
  720. $msgparts = preg_split("/ /", $message, 2);
  721. if (count($msgparts) == 1) {
  722. $result[] = Config::lang($msgparts[0]);
  723. } else {
  724. $result[] = sprintf(Config::lang($msgparts[0]), $msgparts[1]);
  725. }
  726. }
  727. }
  728. return $result;
  729. }
  730. /**
  731. * Encrypt a password, using the apparopriate hashing mechanism as defined in
  732. * config.inc.php ($CONF['encrypt']).
  733. * When wanting to compare one pw to another, it's necessary to provide the salt used - hence
  734. * the second parameter ($pw_db), which is the existing hash from the DB.
  735. *
  736. * @param string $pw
  737. * @param string $encrypted password
  738. * @return string encrypted password.
  739. */
  740. function pacrypt ($pw, $pw_db="") {
  741. global $CONF;
  742. $pw = stripslashes($pw);
  743. $password = "";
  744. $salt = "";
  745. if ($CONF['encrypt'] == 'md5crypt') {
  746. $split_salt = preg_split ('/\$/', $pw_db);
  747. if (isset ($split_salt[2])) {
  748. $salt = $split_salt[2];
  749. }
  750. $password = md5crypt ($pw, $salt);
  751. }
  752. elseif ($CONF['encrypt'] == 'md5') {
  753. $password = md5($pw);
  754. }
  755. elseif ($CONF['encrypt'] == 'system') {
  756. if ($pw_db) {
  757. $password = crypt($pw, $pw_db);
  758. } else {
  759. $password = crypt($pw);
  760. }
  761. }
  762. elseif ($CONF['encrypt'] == 'cleartext') {
  763. $password = $pw;
  764. }
  765. // See https://sourceforge.net/tracker/?func=detail&atid=937966&aid=1793352&group_id=191583
  766. // this is apparently useful for pam_mysql etc.
  767. elseif ($CONF['encrypt'] == 'mysql_encrypt') {
  768. $pw = escape_string($pw);
  769. if ($pw_db!="") {
  770. $salt=escape_string(substr($pw_db,0,2));
  771. $res=db_query("SELECT ENCRYPT('".$pw."','".$salt."');");
  772. } else {
  773. $res=db_query("SELECT ENCRYPT('".$pw."');");
  774. }
  775. $l = db_row($res["result"]);
  776. $password = $l[0];
  777. }
  778. elseif ($CONF['encrypt'] == 'authlib') {
  779. $flavor = $CONF['authlib_default_flavor'];
  780. $salt = substr(create_salt(), 0, 2); # courier-authlib supports only two-character salts
  781. if(preg_match('/^{.*}/', $pw_db)) {
  782. // we have a flavor in the db -> use it instead of default flavor
  783. $result = preg_split('/[{}]/', $pw_db, 3); # split at { and/or }
  784. $flavor = $result[1];
  785. $salt = substr($result[2], 0, 2);
  786. }
  787. if(stripos($flavor, 'md5raw') === 0) {
  788. $password = '{' . $flavor . '}' . md5($pw);
  789. } elseif(stripos($flavor, 'md5') === 0) {
  790. $password = '{' . $flavor . '}' . base64_encode(md5($pw, TRUE));
  791. } elseif(stripos($flavor, 'crypt') === 0) {
  792. $password = '{' . $flavor . '}' . crypt($pw, $salt);
  793. } elseif(stripos($flavor, 'SHA') === 0) {
  794. $password = '{' . $flavor . '}' . base64_encode(sha1($pw, TRUE));
  795. } else {
  796. die("authlib_default_flavor '" . $flavor . "' unknown. Valid flavors are 'md5raw', 'md5', 'SHA' and 'crypt'");
  797. }
  798. }
  799. elseif (preg_match("/^dovecot:/", $CONF['encrypt'])) {
  800. $split_method = preg_split ('/:/', $CONF['encrypt']);
  801. $method = strtoupper($split_method[1]); # TODO: if $pw_db starts with {method}, change $method accordingly
  802. if (! preg_match("/^[A-Z0-9.-]+$/", $method)) { die("invalid dovecot encryption method"); } # TODO: check against a fixed list?
  803. # if (strtolower($method) == 'md5-crypt') die("\$CONF['encrypt'] = 'dovecot:md5-crypt' will not work because dovecotpw generates a random salt each time. Please use \$CONF['encrypt'] = 'md5crypt' instead.");
  804. # $crypt_method = preg_match ("/.*-CRYPT$/", $method);
  805. # digest-md5 and SCRAM-SHA-1 hashes include the username - until someone implements it, let's declare it as unsupported
  806. if (strtolower($method) == 'digest-md5') die("Sorry, \$CONF['encrypt'] = 'dovecot:digest-md5' is not supported by PostfixAdmin.");
  807. if (strtoupper($method) == 'SCRAM-SHA-1') die("Sorry, \$CONF['encrypt'] = 'dovecot:scram-sha-1' is not supported by PostfixAdmin.");
  808. # TODO: add -u option for those hashes, or for everything that is salted (-u was available before dovecot 2.1 -> no problem with backward compability)
  809. $dovecotpw = "doveadm pw";
  810. if (!empty($CONF['dovecotpw'])) $dovecotpw = $CONF['dovecotpw'];
  811. # Use proc_open call to avoid safe_mode problems and to prevent showing plain password in process table
  812. $spec = array(
  813. 0 => array("pipe", "r"), // stdin
  814. 1 => array("pipe", "w"), // stdout
  815. 2 => array("pipe", "w"), // stderr
  816. );
  817. $nonsaltedtypes = "SHA|SHA1|SHA256|SHA512|CLEAR|CLEARTEXT|PLAIN|PLAIN-TRUNC|CRAM-MD5|HMAC-MD5|PLAIN-MD4|PLAIN-MD5|LDAP-MD5|LANMAN|NTLM|RPA";
  818. $salted = ! preg_match("/^($nonsaltedtypes)(\.B64|\.BASE64|\.HEX)?$/", strtoupper($method) );
  819. $dovepasstest = '';
  820. if ( $salted && (!empty($pw_db)) ) {
  821. # only use -t for salted passwords to be backward compatible with dovecot < 2.1
  822. $dovepasstest = " -t " . escapeshellarg($pw_db);
  823. }
  824. $pipe = proc_open("$dovecotpw '-s' $method$dovepasstest", $spec, $pipes);
  825. if (!$pipe) {
  826. die("can't proc_open $dovecotpw");
  827. } else {
  828. // use dovecot's stdin, it uses getpass() twice (except when using -t)
  829. // Write pass in pipe stdin
  830. if (empty($dovepasstest)) {
  831. fwrite($pipes[0], $pw . "\n", 1+strlen($pw)); usleep(1000);
  832. }
  833. fwrite($pipes[0], $pw . "\n", 1+strlen($pw));
  834. fclose($pipes[0]);
  835. // Read hash from pipe stdout
  836. $password = fread($pipes[1], "200");
  837. if (empty($dovepasstest)) {
  838. if ( !preg_match('/^\{' . $method . '\}/', $password)) {
  839. $stderr_output = stream_get_contents($pipes[2]);
  840. error_log('dovecotpw password encryption failed.');
  841. error_log('STDERR output: ' . $stderr_output);
  842. die("can't encrypt password with dovecotpw, see error log for details");
  843. }
  844. } else {
  845. if ( !preg_match('(verified)', $password)) {
  846. $password="Thepasswordcannotbeverified";
  847. } else {
  848. $password = rtrim(str_replace('(verified)', '', $password));
  849. }
  850. }
  851. fclose($pipes[1]);
  852. fclose($pipes[2]);
  853. proc_close($pipe);
  854. if ( (!empty($pw_db)) && (substr($pw_db,0,1) != '{') ) {
  855. # for backward compability with "old" dovecot passwords that don't have the {method} prefix
  856. $password = str_replace('{' . $method . '}', '', $password);
  857. }
  858. $password = rtrim($password);
  859. }
  860. }
  861. else {
  862. die ('unknown/invalid $CONF["encrypt"] setting: ' . $CONF['encrypt']);
  863. }
  864. return $password;
  865. }
  866. //
  867. // md5crypt
  868. // Action: Creates MD5 encrypted password
  869. // Call: md5crypt (string cleartextpassword)
  870. //
  871. function md5crypt ($pw, $salt="", $magic="") {
  872. $MAGIC = "$1$";
  873. if ($magic == "") $magic = $MAGIC;
  874. if ($salt == "") $salt = create_salt ();
  875. $slist = explode ("$", $salt);
  876. if ($slist[0] == "1") $salt = $slist[1];
  877. $salt = substr ($salt, 0, 8);
  878. $ctx = $pw . $magic . $salt;
  879. $final = hex2bin (md5 ($pw . $salt . $pw));
  880. for ($i=strlen ($pw); $i>0; $i-=16) {
  881. if ($i > 16) {
  882. $ctx .= substr ($final,0,16);
  883. } else {
  884. $ctx .= substr ($final,0,$i);
  885. }
  886. }
  887. $i = strlen ($pw);
  888. while ($i > 0) {
  889. if ($i & 1) $ctx .= chr (0);
  890. else $ctx .= $pw[0];
  891. $i = $i >> 1;
  892. }
  893. $final = hex2bin (md5 ($ctx));
  894. for ($i=0;$i<1000;$i++) {
  895. $ctx1 = "";
  896. if ($i & 1) {
  897. $ctx1 .= $pw;
  898. } else {
  899. $ctx1 .= substr ($final,0,16);
  900. }
  901. if ($i % 3) $ctx1 .= $salt;
  902. if ($i % 7) $ctx1 .= $pw;
  903. if ($i & 1) {
  904. $ctx1 .= substr ($final,0,16);
  905. } else {
  906. $ctx1 .= $pw;
  907. }
  908. $final = hex2bin (md5 ($ctx1));
  909. }
  910. $passwd = "";
  911. $passwd .= to64 (((ord ($final[0]) << 16) | (ord ($final[6]) << 8) | (ord ($final[12]))), 4);
  912. $passwd .= to64 (((ord ($final[1]) << 16) | (ord ($final[7]) << 8) | (ord ($final[13]))), 4);
  913. $passwd .= to64 (((ord ($final[2]) << 16) | (ord ($final[8]) << 8) | (ord ($final[14]))), 4);
  914. $passwd .= to64 (((ord ($final[3]) << 16) | (ord ($final[9]) << 8) | (ord ($final[15]))), 4);
  915. $passwd .= to64 (((ord ($final[4]) << 16) | (ord ($final[10]) << 8) | (ord ($final[5]))), 4);
  916. $passwd .= to64 (ord ($final[11]), 2);
  917. return "$magic$salt\$$passwd";
  918. }
  919. function create_salt () {
  920. srand ((double) microtime ()*1000000);
  921. $salt = substr (md5 (rand (0,9999999)), 0, 8);
  922. return $salt;
  923. }
  924. /**/ if (!function_exists('hex2bin')) { # PHP around 5.3.8 includes hex2bin as native function - http://php.net/hex2bin
  925. function hex2bin ($str) {
  926. $len = strlen ($str);
  927. $nstr = "";
  928. for ($i=0;$i<$len;$i+=2) {
  929. $num = sscanf (substr ($str,$i,2), "%x");
  930. $nstr.=chr ($num[0]);
  931. }
  932. return $nstr;
  933. }
  934. /**/ }
  935. /*
  936. * remove item $item from array $array
  937. */
  938. function remove_from_array($array, $item) {
  939. # array_diff might be faster, but doesn't provide an easy way to know if the value was found or not
  940. # return array_diff($array, array($item));
  941. $ret = array_search($item, $array);
  942. if ($ret === false) {
  943. $found = 0;
  944. } else {
  945. $found = 1;
  946. unset ($array[$ret]);
  947. }
  948. return array($found, $array);
  949. }
  950. function to64 ($v, $n) {
  951. $ITOA64 = "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
  952. $ret = "";
  953. while (($n - 1) >= 0) {
  954. $n--;
  955. $ret .= $ITOA64[$v & 0x3f];
  956. $v = $v >> 6;
  957. }
  958. return $ret;
  959. }
  960. /**
  961. * smtp_mail
  962. * Action: Send email
  963. * Call: smtp_mail (string to, string from, string subject, string body]) - or -
  964. * Call: smtp_mail (string to, string from, string data) - DEPRECATED
  965. * @param String - To:
  966. * @param String - From:
  967. * @param String - Subject: (if called with 4 parameters) or full mail body (if called with 3 parameters)
  968. * @param String (optional, but recommended) - mail body
  969. * @return bool - true on success, otherwise false
  970. * TODO: Replace this with something decent like PEAR::Mail or Zend_Mail.
  971. */
  972. function smtp_mail ($to, $from, $data, $body = "") {
  973. global $CONF;
  974. $smtpd_server = $CONF['smtp_server'];
  975. $smtpd_port = $CONF['smtp_port'];
  976. //$smtp_server = $_SERVER["SERVER_NAME"];
  977. $smtp_server = php_uname("n");
  978. $errno = "0";
  979. $errstr = "0";
  980. $timeout = "30";
  981. if ($body != "") {
  982. $maildata =
  983. "To: " . $to . "\n"
  984. . "From: " . $from . "\n"
  985. . "Subject: " . encode_header ($data) . "\n"
  986. . "MIME-Version: 1.0\n"
  987. . "Content-Type: text/plain; charset=utf-8\n"
  988. . "Content-Transfer-Encoding: 8bit\n"
  989. . "\n"
  990. . $body
  991. ;
  992. } else {
  993. $maildata = $data;
  994. }
  995. $fh = @fsockopen ($smtpd_server, $smtpd_port, $errno, $errstr, $timeout);
  996. if (!$fh) {
  997. error_log("fsockopen failed - errno: $errno - errstr: $errstr");
  998. return false;
  999. } else {
  1000. $res = smtp_get_response($fh);
  1001. fputs ($fh, "EHLO $smtp_server\r\n");
  1002. $res = smtp_get_response($fh);
  1003. fputs ($fh, "MAIL FROM:<$from>\r\n");
  1004. $res = smtp_get_response($fh);
  1005. fputs ($fh, "RCPT TO:<$to>\r\n");
  1006. $res = smtp_get_response($fh);
  1007. fputs ($fh, "DATA\r\n");
  1008. $res = smtp_get_response($fh);
  1009. fputs ($fh, "$maildata\r\n.\r\n");
  1010. $res = smtp_get_response($fh);
  1011. fputs ($fh, "QUIT\r\n");
  1012. $res = smtp_get_response($fh);
  1013. fclose ($fh);
  1014. }
  1015. return true;
  1016. }
  1017. /**
  1018. * smtp_get_admin_email
  1019. * Action: Get configured email address or current user if nothing configured
  1020. * Call: smtp_get_admin_email
  1021. * @return String - username/mail address
  1022. */
  1023. function smtp_get_admin_email() {
  1024. $admin_email = Config::read('admin_email');
  1025. if(!empty($admin_email))
  1026. return $admin_email;
  1027. else
  1028. return authentication_get_username();
  1029. }
  1030. //
  1031. // smtp_get_response
  1032. // Action: Get response from mail server
  1033. // Call: smtp_get_response (string FileHandle)
  1034. //
  1035. function smtp_get_response ($fh) {
  1036. $res ='';
  1037. do {
  1038. $line = fgets($fh, 256);
  1039. $res .= $line;
  1040. }
  1041. while (preg_match("/^\d\d\d\-/", $line));
  1042. return $res;
  1043. }
  1044. $DEBUG_TEXT = "\n
  1045. <p />\n
  1046. Please check the documentation and website for more information.\n
  1047. <p />\n
  1048. <a href=\"http://postfixadmin.sf.net/\">Postfix Admin</a><br />\n
  1049. <a href='https://sourceforge.net/p/postfixadmin/discussion/676076'>Forums</a>
  1050. ";
  1051. /**
  1052. * db_connect
  1053. * Action: Makes a connection to the database if it doesn't exist
  1054. * Call: db_connect ()
  1055. * Optional parameter: $ignore_errors = TRUE, used by setup.php
  1056. *
  1057. * Return value:
  1058. * a) without $ignore_errors or $ignore_errors == 0
  1059. * - $link - the database connection -OR-
  1060. * - call die() in case of connection problems
  1061. * b) with $ignore_errors == TRUE
  1062. * array($link, $error_text);
  1063. */
  1064. function db_connect ($ignore_errors = 0) {
  1065. global $CONF;
  1066. global $DEBUG_TEXT;
  1067. if ($ignore_errors != 0) $DEBUG_TEXT = '';
  1068. $error_text = '';
  1069. $link = 0;
  1070. if ($CONF['database_type'] == "mysql") {
  1071. if (function_exists ("mysql_connect")) {
  1072. $link = @mysql_connect ($CONF['database_host'], $CONF['database_user'], $CONF['database_password']) or $error_text .= ("<p />DEBUG INFORMATION:<br />Connect: " . mysql_error () . "$DEBUG_TEXT");
  1073. if ($link) {
  1074. @mysql_query("SET CHARACTER SET utf8",$link);
  1075. @mysql_query("SET COLLATION_CONNECTION='utf8_general_ci'",$link);
  1076. $succes = @mysql_select_db ($CONF['database_name'], $link) or $error_text .= ("<p />DEBUG INFORMATION:<br />MySQL Select Database: " . mysql_error () . "$DEBUG_TEXT");
  1077. }
  1078. } else {
  1079. $error_text .= "<p />DEBUG INFORMATION:<br />MySQL 3.x / 4.0 functions not available! (php5-mysql installed?)<br />database_type = 'mysql' in config.inc.php, are you using a different database? $DEBUG_TEXT";
  1080. }
  1081. } elseif ($CONF['database_type'] == "mysqli") {
  1082. if (function_exists ("mysqli_connect")) {
  1083. $link = @mysqli_connect ($CONF['database_host'], $CONF['database_user'], $CONF['database_password']) or $error_text .= ("<p />DEBUG INFORMATION:<br />Connect: " . mysqli_connect_error () . "$DEBUG_TEXT");
  1084. if ($link) {
  1085. @mysqli_query($link,"SET CHARACTER SET utf8");
  1086. @mysqli_query($link,"SET COLLATION_CONNECTION='utf8_general_ci'");
  1087. $success = @mysqli_select_db ($link, $CONF['database_name']) or $error_text .= ("<p />DEBUG INFORMATION:<br />MySQLi Select Database: " . mysqli_error ($link) . "$DEBUG_TEXT");
  1088. }
  1089. } else {
  1090. $error_text .= "<p />DEBUG INFORMATION:<br />MySQL 4.1 functions not available! (php5-mysqli installed?)<br />database_type = 'mysqli' in config.inc.php, are you using a different database? $DEBUG_TEXT";
  1091. }
  1092. } elseif (db_pgsql()) {
  1093. if (function_exists ("pg_pconnect")) {
  1094. if(!isset($CONF['database_port'])) {
  1095. $CONF['database_port'] = '5432';
  1096. }
  1097. $connect_string = "host=" . $CONF['database_host'] . " port=" . $CONF['database_port'] . " dbname=" . $CONF['database_name'] . " user=" . $CONF['database_user'] . " password=" . $CONF['database_password'];
  1098. $link = @pg_pconnect ($connect_string) or $error_text .= ("<p />DEBUG INFORMATION:<br />Connect: failed to connect to database. $DEBUG_TEXT");
  1099. if ($link) pg_set_client_encoding($link, 'UNICODE');
  1100. } else {
  1101. $error_text .= "<p />DEBUG INFORMATION:<br />PostgreSQL functions not available! (php5-pgsql installed?)<br />database_type = 'pgsql' in config.inc.php, are you using a different database? $DEBUG_TEXT";
  1102. }
  1103. } else {
  1104. $error_text = "<p />DEBUG INFORMATION:<br />Invalid \$CONF['database_type']! Please fix your config.inc.php! $DEBUG_TEXT";
  1105. }
  1106. if ($ignore_errors) {
  1107. return array($link, $error_text);
  1108. } elseif ($error_text != "") {
  1109. print $error_text;
  1110. die();
  1111. } elseif ($link) {
  1112. return $link;
  1113. } else {
  1114. print "DEBUG INFORMATION:<br />\n";
  1115. print "Connect: Unable to connect to database<br />\n";
  1116. print "<br />\n";
  1117. print "Make sure that you have set the correct database type in the config.inc.php file<br />\n";
  1118. print $DEBUG_TEXT;
  1119. die();
  1120. }
  1121. }
  1122. /**
  1123. * Returns the appropriate boolean value for the database.
  1124. * Currently only PostgreSQL and MySQL are supported.
  1125. * @param boolean $bool (REQUIRED)
  1126. * @return String or int as appropriate.
  1127. */
  1128. function db_get_boolean($bool) {
  1129. if(! (is_bool($bool) || $bool == '0' || $bool == '1') ) {
  1130. error_log("Invalid usage of 'db_get_boolean($bool)'");
  1131. die("Invalid usage of 'db_get_boolean($bool)'");
  1132. }
  1133. if(db_pgsql()) {
  1134. // return either true or false (unquoted strings)
  1135. if($bool) {
  1136. return 't';
  1137. }
  1138. return 'f';
  1139. } elseif(Config::Read('database_type') == 'mysql' || Config::Read('database_type') == 'mysqli') {
  1140. if($bool) {
  1141. return 1;
  1142. }
  1143. return 0;
  1144. } else {
  1145. die('Unknown value in $CONF[database_type]');
  1146. }
  1147. }
  1148. /**
  1149. * Returns a query that reports the used quota ("x / y")
  1150. * @param string column containing used quota
  1151. * @param string column containing allowed quota
  1152. * @param string column that will contain "x / y"
  1153. * @return string
  1154. */
  1155. function db_quota_text($count, $quota, $fieldname) {
  1156. return " CASE $quota
  1157. WHEN '-1' THEN CONCAT(coalesce($count,0), ' / -')
  1158. WHEN '0' THEN CONCAT(coalesce($count,0), ' / ', '" . escape_string(html_entity_decode('&infin;')) . "')
  1159. ELSE CONCAT(coalesce($count,0), ' / ', $quota)
  1160. END AS $fieldname";
  1161. }
  1162. /**
  1163. * Returns a query that reports the used quota ("x / y")
  1164. * @param string column containing used quota
  1165. * @param string column containing allowed quota
  1166. * @param string column that will contain "x / y"
  1167. * @return string
  1168. */
  1169. function db_quota_percent($count, $quota, $fieldname) {
  1170. return " CASE $quota
  1171. WHEN '-1' THEN -1
  1172. WHEN '0' THEN -1
  1173. ELSE round(100 * coalesce($count,0) / $quota)
  1174. END AS $fieldname";
  1175. }
  1176. /**
  1177. * returns true if PostgreSQL is used, false otherwise
  1178. */
  1179. function db_pgsql() {
  1180. if(Config::Read('database_type')=='pgsql') {
  1181. return true;
  1182. } else {
  1183. return false;
  1184. }
  1185. }
  1186. //
  1187. // db_query
  1188. // Action: Sends a query to the database and returns query result and number of rows
  1189. // Call: db_query (string query)
  1190. // Optional parameter: $ignore_errors = TRUE, used by upgrade.php
  1191. //
  1192. function db_query ($query, $ignore_errors = 0) {
  1193. global $CONF;
  1194. global $DEBUG_TEXT;
  1195. $result = "";
  1196. $number_rows = "";
  1197. static $link;
  1198. $error_text = "";
  1199. if ($ignore_errors) $DEBUG_TEXT = "";
  1200. # mysql and pgsql $link are resources, mysqli $link is an object
  1201. if (! (is_resource($link) || is_object($link) ) ) $link = db_connect ();
  1202. if ($CONF['database_type'] == "mysql") $result = @mysql_query ($query, $link)
  1203. or $error_text = "Invalid query: " . mysql_error($link);
  1204. if ($CONF['database_type'] == "mysqli") $result = @mysqli_query ($link, $query)
  1205. or $error_text = "Invalid query: " . mysqli_error($link);
  1206. if (db_pgsql()) {
  1207. $result = @pg_query ($link, $query)
  1208. or $error_text = "Invalid query: " . pg_last_error();
  1209. }
  1210. if ($error_text != "" && $ignore_errors == 0) {
  1211. error_log($error_text);
  1212. error_log("caused by query: $query");
  1213. die("<p />DEBUG INFORMATION:<br />$error_text <p>Check your error_log for the failed query. $DEBUG_TEXT");
  1214. }
  1215. if ($error_text == "") {
  1216. if (preg_match("/^SELECT/i", trim($query))) {
  1217. // if $query was a SELECT statement check the number of rows with [database_type]_num_rows ().
  1218. if ($CONF['database_type'] == "mysql") $number_rows = mysql_num_rows ($result);
  1219. if ($CONF['database_type'] == "mysqli") $number_rows = mysqli_num_rows ($result);
  1220. if (db_pgsql() ) $number_rows = pg_num_rows ($result);
  1221. } else {
  1222. // if $query was something else, UPDATE, DELETE or INSERT check the number of rows with
  1223. // [database_type]_affected_rows ().
  1224. if ($CONF['database_type'] == "mysql") $number_rows = mysql_affected_rows ($link);
  1225. if ($CONF['database_type'] == "mysqli") $number_rows = mysqli_affected_rows ($link);
  1226. if (db_pgsql() ) $number_rows = pg_affected_rows ($result);
  1227. }
  1228. }
  1229. $return = array (
  1230. "result" => $result,
  1231. "rows" => $number_rows,
  1232. "error" => $error_text
  1233. );
  1234. return $return;
  1235. }
  1236. // db_row
  1237. // Action: Returns a row from a table
  1238. // Call: db_row (int result)
  1239. function db_row ($result) {
  1240. global $CONF;
  1241. $row = "";
  1242. if ($CONF['database_type'] == "mysql") $row = mysql_fetch_row ($result);
  1243. if ($CONF['database_type'] == "mysqli") $row = mysqli_fetch_row ($result);
  1244. if (db_pgsql() ) $row = pg_fetch_row ($result);
  1245. return $row;
  1246. }
  1247. // db_array
  1248. // Action: Returns a row from a table
  1249. // Call: db_array (int result)
  1250. //
  1251. function db_array ($result) {
  1252. global $CONF;
  1253. $row = "";
  1254. if ($CONF['database_type'] == "mysql") $row = mysql_fetch_array ($result);
  1255. if ($CONF['database_type'] == "mysqli") $row = mysqli_fetch_array ($result);
  1256. if (db_pgsql() ) $row = pg_fetch_array ($result);
  1257. return $row;
  1258. }
  1259. // db_assoc
  1260. // Action: Returns a row from a table
  1261. // Call: db_assoc(int result)
  1262. //
  1263. function db_assoc ($result) {
  1264. global $CONF;
  1265. $row = "";
  1266. if ($CONF['database_type'] == "mysql") $row = mysql_fetch_assoc ($result);
  1267. if ($CONF['database_type'] == "mysqli") $row = mysqli_fetch_assoc ($result);
  1268. if (db_pgsql() ) $row = pg_fetch_assoc ($result);
  1269. return $row;
  1270. }
  1271. //
  1272. // db_delete
  1273. // Action: Deletes a row from a specified table
  1274. // Call: db_delete (string table, string where, string delete)
  1275. //
  1276. function db_delete ($table,$where,$delete,$additionalwhere='') {
  1277. $table = table_by_key($table);
  1278. $query = "DELETE FROM $table WHERE " . escape_string($where) . "='" . escape_string($delete) . "' " . $additionalwhere;
  1279. $result = db_query ($query);
  1280. if ($result['rows'] >= 1) {
  1281. return $result['rows'];
  1282. } else {
  1283. return 0;
  1284. }
  1285. }
  1286. /**
  1287. * db_insert
  1288. * Action: Inserts a row from a specified table
  1289. * Call: db_insert (string table, array values [, array timestamp])
  1290. * @param String - table name
  1291. * @param array - key/value map of data to insert into the table.
  1292. * @param array (optional) - array of fields to set to now() - default: array('created', 'modified')
  1293. * @return int - number of inserted rows
  1294. */
  1295. function db_insert ($table, $values, $timestamp = array('created', 'modified') ) {
  1296. $table = table_by_key ($table);
  1297. foreach(array_keys($values) as $key) {
  1298. $values[$key] = "'" . escape_string($values[$key]) . "'";
  1299. }
  1300. foreach($timestamp as $key) {
  1301. $values[$key] = "now()";
  1302. }
  1303. $sql_values = "(" . implode(",",escape_string(array_keys($values))).") VALUES (".implode(",",$values).")";
  1304. $result = db_query ("INSERT INTO $table $sql_values");
  1305. return $result['rows'];
  1306. }
  1307. /**
  1308. * db_update
  1309. * Action: Updates a specified table
  1310. * Call: db_update (string table, string where_col, string where_value, array values [, array timestamp])
  1311. * @param String - table name
  1312. * @param String - column of WHERE condition
  1313. * @param String - value of WHERE condition
  1314. * @param array - key/value map of data to insert into the table.
  1315. * @param array (optional) - array of fields to set to now() - default: array('modified')
  1316. * @return int - number of updated rows
  1317. */
  1318. function db_update ($table, $where_col, $where_value, $values, $timestamp = array('modified') ) {
  1319. $where = $where_col . " = '" . escape_string($where_value) . "'";
  1320. return db_update_q ($table, $where, $values, $timestamp );
  1321. }
  1322. /**
  1323. * db_update_q
  1324. * Action: Updates a specified table
  1325. * Call: db_update_q (string table, string where, array values [, array timestamp])
  1326. * @param String - table name
  1327. * @param String - WHERE condition (as SQL)
  1328. * @param array - key/value map of data to insert into the table.
  1329. * @param array (optional) - array of fields to set to now() - default: array('modified')
  1330. * @return int - number of updated rows
  1331. */
  1332. function db_update_q ($table, $where, $values, $timestamp = array('modified') ) {
  1333. $table = table_by_key ($table);
  1334. foreach(array_keys($values) as $key) {
  1335. $sql_values[$key] = escape_string($key) . "='" . escape_string($values[$key]) . "'";
  1336. }
  1337. foreach($timestamp as $key) {
  1338. $sql_values[$key] = escape_string($key) . "=now()";
  1339. }
  1340. $sql="UPDATE $table SET ".implode(",",$sql_values)." WHERE $where";
  1341. $result = db_query ($sql);
  1342. return $result['rows'];
  1343. }
  1344. /**
  1345. * db_begin / db_commit / db_rollback
  1346. * Action: BEGIN / COMMIT / ROLLBACK transaction (PostgreSQL only!)
  1347. * Call: db_begin()
  1348. */
  1349. function db_begin () {
  1350. if (db_pgsql()) { # TODO: also enable for mysql? (not supported by MyISAM, which is used for most tables)
  1351. db_query('BEGIN');
  1352. }
  1353. }
  1354. function db_commit () {
  1355. if (db_pgsql()) {
  1356. db_query('COMMIT');
  1357. }
  1358. }
  1359. function db_rollback () {
  1360. if (db_pgsql()) {
  1361. db_query('ROLLBACK');
  1362. }
  1363. }
  1364. /**
  1365. * db_log
  1366. * Action: Logs actions from admin
  1367. * Call: db_log (string domain, string action, string data)
  1368. * Possible actions are defined in $LANG["pViewlog_action_$action"]
  1369. */
  1370. function db_log ($domain,$action,$data) {
  1371. $REMOTE_ADDR = getRemoteAddr();
  1372. $username = authentication_get_username();
  1373. if (Config::Lang("pViewlog_action_$action") == '') {
  1374. die("Invalid log action : $action"); // could do with something better?
  1375. }
  1376. if (Config::bool('logging')) {
  1377. $logdata = array(
  1378. 'username' => "$username ($REMOTE_ADDR)",
  1379. 'domain' => $domain,
  1380. 'action' => $action,
  1381. 'data' => $data,
  1382. );
  1383. $result = db_insert('log', $logdata, array('timestamp') );
  1384. if ($result != 1) {
  1385. return false;
  1386. } else {
  1387. return true;
  1388. }
  1389. }
  1390. }
  1391. /**
  1392. * db_in_clause
  1393. * Action: builds and returns the "field in(x, y)" clause for database queries
  1394. * Call: db_in_clause (string field, array values)
  1395. */
  1396. function db_in_clause($field, $values) {
  1397. return " $field IN ('"
  1398. . implode("','",escape_string(array_values($values)))
  1399. . "') ";
  1400. }
  1401. /**
  1402. * db_where_clause
  1403. * Action: builds and returns a WHERE clause for database queries. All given conditions will be AND'ed.
  1404. * Call: db_where_clause (array $conditions, array $struct)
  1405. * param array $conditios: array('field' => 'value', 'field2' => 'value2, ...)
  1406. * param array $struct - field structure, used for automatic bool conversion
  1407. * param string $additional_raw_where - raw sniplet to include in the WHERE part - typically needs to start with AND
  1408. * param array $searchmode - operators to use (=, <, > etc.) - defaults to = if not specified for a field (see
  1409. * $allowed_operators for available operators)
  1410. */
  1411. function db_where_clause($condition, $struct, $additional_raw_where = '', $searchmode = array()) {
  1412. if (!is_array($condition)) {
  1413. die('db_where_cond: parameter $cond is not an array!');
  1414. } elseif(!is_array($searchmode)) {
  1415. die('db_where_cond: parameter $searchmode is not an array!');
  1416. } elseif (count($condition) == 0 && trim($additional_raw_where) == '') {
  1417. die("db_where_cond: parameter is an empty array!"); # die() might sound harsh, but can prevent information leaks
  1418. } elseif(!is_array($struct)) {
  1419. die('db_where_cond: parameter $struct is not an array!');
  1420. }
  1421. $allowed_operators = explode(' ', '< > >= <= = != <> CONT LIKE');
  1422. $where_parts = array();
  1423. $having_parts = array();
  1424. foreach($condition as $field => $value) {
  1425. if (isset($struct[$field]) && $struct[$field]['type'] == 'bool') $value = db_get_boolean($value);
  1426. $operator = '=';
  1427. if (isset($searchmode[$field])) {
  1428. if (in_array($searchmode[$field], $allowed_operators)) {
  1429. $operator = $searchmode[$field];
  1430. if ($operator == 'CONT') { # CONT - as in "contains"
  1431. $operator = ' LIKE '; # add spaces
  1432. $value = '%' . $value . '%';
  1433. } elseif ($operator == 'LIKE') { # LIKE -without adding % wildcards (the search value can contain %)
  1434. $operator = ' LIKE '; # add spaces
  1435. }
  1436. } else {
  1437. die('db_where_clause: Invalid searchmode for ' . $field);
  1438. }
  1439. }
  1440. $querypart = $field . $operator . "'" . escape_string($value) . "'";
  1441. if($struct[$field]['select'] != '') {
  1442. $having_parts[$field] = $querypart;
  1443. } else {
  1444. $where_parts[$field] = $querypart;
  1445. }
  1446. }
  1447. $query = ' WHERE 1=1 ';
  1448. $query .= " $additional_raw_where ";
  1449. if (count($where_parts) > 0) $query .= " AND ( " . join(" AND ", $where_parts) . " ) ";
  1450. if (count($having_parts) > 0) $query .= " HAVING ( " . join(" AND ", $having_parts) . " ) ";
  1451. return $query;
  1452. }
  1453. //
  1454. // table_by_key
  1455. // Action: Return table name for given key
  1456. // Call: table_by_key (string table_key)
  1457. //
  1458. function table_by_key ($table_key) {
  1459. global $CONF;
  1460. if (empty($CONF['database_tables'][$table_key])) {
  1461. $table = $table_key;
  1462. } else {
  1463. $table = $CONF['database_tables'][$table_key];
  1464. }
  1465. return $CONF['database_prefix'].$table;
  1466. }
  1467. /*
  1468. Called after an alias_domain has been deleted in the DBMS.
  1469. Returns: boolean.
  1470. */
  1471. # TODO: This function is never called
  1472. function alias_domain_postdeletion($alias_domain) {
  1473. global $CONF;
  1474. $confpar='alias_domain_postdeletion_script';
  1475. if (!isset($CONF[$confpar]) || empty($CONF[$confpar])) {
  1476. return true;
  1477. }
  1478. if (empty($alias_domain)) {
  1479. print '<p>Warning: empty alias_domain parameter.</p>';
  1480. return false;
  1481. }
  1482. $cmdarg1=escapeshellarg($alias_domain);
  1483. $command=$CONF[$confpar]." $cmdarg1";
  1484. $retval=0;
  1485. $output=array();
  1486. $firstline='';
  1487. $firstline=exec($command,$output,$retval);
  1488. if (0!=$retval) {
  1489. error_log("Running $command yielded return value=$retval, first line of output=$firstline");
  1490. print '<p>WARNING: Problems running alias_domain postdeletion script!</p>';
  1491. return FALSE;
  1492. }
  1493. return TRUE;
  1494. }
  1495. //
  1496. // gen_show_status
  1497. // Action: Return a string of colored &nbsp;'s that indicate
  1498. // the if an alias goto has an error or is sent to
  1499. // addresses list in show_custom_domains
  1500. // Call: gen_show_status (string alias_address)
  1501. //
  1502. function gen_show_status ($show_alias) {
  1503. global $CONF;
  1504. $table_alias = table_by_key('alias');
  1505. $stat_string = "";
  1506. $show_alias = escape_string($show_alias);
  1507. $stat_goto = "";
  1508. $stat_result = db_query ("SELECT goto FROM $table_alias WHERE address='$show_alias'");
  1509. if ($stat_result['rows'] > 0) {
  1510. $row = db_row ($stat_result['result']);
  1511. $stat_goto = $row[0];
  1512. }
  1513. if (!empty($CONF['recipient_delimiter'])) {
  1514. $delimiter = preg_quote($CONF['recipient_delimiter'], "/");
  1515. $delimiter_regex = '/' .$delimiter. '[^' .$delimiter. '@]*@/';
  1516. }
  1517. // UNDELIVERABLE CHECK
  1518. if ( $CONF['show_undeliverable'] == 'YES' ) {
  1519. $gotos=array();
  1520. $gotos=explode(',',$stat_goto);
  1521. $undel_string="";
  1522. //make sure this alias goes somewhere known
  1523. $stat_ok = 1;
  1524. while ( ($g=array_pop($gotos)) && $stat_ok ) {
  1525. list(/*NULL*/,$stat_domain) = explode('@',$g);
  1526. $stat_delimiter = "";
  1527. if (!empty($CONF['recipient_delimiter'])) {
  1528. $stat_delimiter = "OR address = '" . preg_replace($delimiter_regex, "@", $g) . "'";
  1529. }
  1530. $stat_result = db_query ("SELECT address FROM $table_alias WHERE address = '$g' OR address = '@$stat_domain' $stat_delimiter");
  1531. if ($stat_result['rows'] == 0) {
  1532. $stat_ok = 0;
  1533. }
  1534. if ( $stat_ok == 0 ) {
  1535. if ( $stat_domain == $CONF['vacation_domain'] || in_array($stat_domain, $CONF['show_undeliverable_exceptions']) ) {
  1536. $stat_ok = 1;
  1537. }
  1538. }
  1539. } // while
  1540. if ( $stat_ok == 0 ) {
  1541. $stat_string .= "<span style='background-color:" . $CONF['show_undeliverable_color'] .
  1542. "'>" . $CONF['show_status_text'] . "</span>&nbsp;";
  1543. } else {
  1544. $stat_string .= $CONF['show_status_text'] . "&nbsp;";
  1545. }
  1546. }
  1547. // POP/IMAP CHECK
  1548. if ( $CONF['show_popimap'] == 'YES' ) {
  1549. $stat_delimiter = "";
  1550. if (!empty($CONF['recipient_delimiter'])) {
  1551. $stat_delimiter = ',' . preg_replace($delimiter_regex, "@", $stat_goto);
  1552. }
  1553. //if the address passed in appears in its own goto field, its POP/IMAP
  1554. # TODO: or not (might also be an alias loop) -> check mailbox table!
  1555. if ( preg_match ('/,' . $show_alias . ',/', ',' . $stat_goto . $stat_delimiter . ',') ) {
  1556. $stat_string .= "<span style='background-color:" . $CONF['show_popimap_color'] .
  1557. "'>" . $CONF['show_status_text'] . "</span>&nbsp;";
  1558. } else {
  1559. $stat_string .= $CONF['show_status_text'] . "&nbsp;";
  1560. }
  1561. }
  1562. // CUSTOM DESTINATION CHECK
  1563. if ( count($CONF['show_custom_domains']) > 0 ) {
  1564. for ($i = 0; $i < sizeof ($CONF['show_custom_domains']); $i++) {
  1565. if (preg_match ('/^.*' . $CONF['show_custom_domains'][$i] . '.*$/', $stat_goto)) {
  1566. $stat_string .= "<span style='background-color:" . $CONF['show_custom_colors'][$i] .
  1567. "'>" . $CONF['show_status_text'] . "</span>&nbsp;";
  1568. } else {
  1569. $stat_string .= $CONF['show_status_text'] . "&nbsp;";
  1570. }
  1571. }
  1572. } else {
  1573. $stat_string .= ";&nbsp;";
  1574. }
  1575. // $stat_string .= "<span style='background-color:green'> &nbsp; </span> &nbsp;" .
  1576. // "<span style='background-color:blue'> &nbsp; </span> &nbsp;";
  1577. return $stat_string;
  1578. }
  1579. function getRemoteAddr() {
  1580. $REMOTE_ADDR = 'localhost';
  1581. if (isset($_SERVER['REMOTE_ADDR']))
  1582. $REMOTE_ADDR = $_SERVER['REMOTE_ADDR'];
  1583. return $REMOTE_ADDR;
  1584. }
  1585. /* vim: set expandtab softtabstop=4 tabstop=4 shiftwidth=4: */