faa279ca7d988c97fbbe148e18c1e22d655f6872
[mailer.git] / inc / libs / user_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 07/16/2004 *
4  * ===============                              Last change: 10/27/2004 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : user_functions.php                               *
8  * -------------------------------------------------------------------- *
9  * Short description : Special functions for user extension             *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Spezielle Funktionen fuer die user-Erweiterung   *
12  * -------------------------------------------------------------------- *
13  *                                                                      *
14  * -------------------------------------------------------------------- *
15  * Copyright (c) 2003 - 2008 by Roland Haeder                           *
16  * For more information visit: http://www.mxchange.org                  *
17  *                                                                      *
18  * This program is free software; you can redistribute it and/or modify *
19  * it under the terms of the GNU General Public License as published by *
20  * the Free Software Foundation; either version 2 of the License, or    *
21  * (at your option) any later version.                                  *
22  *                                                                      *
23  * This program is distributed in the hope that it will be useful,      *
24  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
25  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        *
26  * GNU General Public License for more details.                         *
27  *                                                                      *
28  * You should have received a copy of the GNU General Public License    *
29  * along with this program; if not, write to the Free Software          *
30  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,               *
31  * MA  02110-1301  USA                                                  *
32  ************************************************************************/
33
34 // Some security stuff...
35 if (!defined('__SECURITY')) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Add links for selecting some users
41 function alpha($sortby, $colspan, $return=false)
42 {
43         global $_CONFIG;
44         if (empty($_GET['offset'])) $_GET['offset'] = 0;
45         $ADD = "&amp;page=".$_GET['page']."&amp;offset=".$_GET['offset'];
46         if (!empty($_GET['mode'])) $ADD .= "&amp;mode=".SQL_ESCAPE($_GET['mode']);
47
48         /* Creates the list of letters and makes them a link. */
49         $alphabet = array(_ALL2,"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z",_OTHERS);
50         $num = count($alphabet) - 1;
51         $OUT = "";
52         while (list($counter, $ltr) = each($alphabet))
53         {
54                 if ($_GET['letter'] == $ltr)
55                 {
56                         // Current letter is letter from URL
57                         $OUT .= "<STRONG>".$ltr."</STRONG>";
58                 }
59                  else
60                 {
61                         // Output link to letter
62                         $OUT .= "<A href=\"".URL."/modules.php?module=admin&amp;what=".$GLOBALS['what'];
63                         if (!empty($_GET['mode'])) $OUT .= "&amp;mode=".SQL_ESCAPE($_GET['mode']);
64                         $OUT .= "&amp;letter=".$ltr."&amp;sortby=".$sortby.$ADD."\">".$ltr."</A>";
65                 }
66                 if ((($counter / $_CONFIG['user_alpha']) == round($counter / $_CONFIG['user_alpha'])) && ($counter > 0))
67                 {
68                         $OUT .= "&nbsp;]<br />[&nbsp;";
69                 }
70                  elseif ( $counter != $num )
71                 {
72                         $OUT .= "&nbsp;|&nbsp;";
73                 }
74         }
75         define('__ALPHA_LIST', $OUT);
76
77         // Load template
78         $OUT = LOAD_TEMPLATE("admin_list_user_alpha", true);
79         if ($return)
80         {
81                 // Return generated code
82                 return $OUT;
83         }
84          else
85         {
86                 // Output generated code
87                 OUTPUT_HTML($OUT);
88         }
89 }
90
91 // Add links for sorting
92 function SortLinks($letter, $sortby, $colspan, $return=false)
93 {
94         $OUT = "";
95         if (empty($_GET['offset'])) $_GET['offset'] = 0;
96         $ADD = "&amp;page=".$_GET['page']."&amp;offset=".$_GET['offset'];
97         if (!empty($_GET['mode'])) $ADD .= "&amp;mode=".SQL_ESCAPE($_GET['mode']);
98
99         // Makes order by links..
100         if ($letter == "front") $letter = _ALL2;
101
102         // Prepare array with all possible sorters
103         $list = array(
104                 'userid'                => _UID,
105                 'family'                => FAMILY_NAME,
106                 'email'         => ADDY,
107                 'REMOTE_ADDR'   => REMOTE_IP
108         );
109
110         // Add nickname if extension is installed
111         if (EXT_IS_ACTIVE("nickname")) {
112                 $list['nickname'] = NICKNAME;
113         }
114
115         foreach ($list as $sort => $title) {
116                 if ($sortby == $sort) {
117                         $OUT .= "<STRONG>".$title."</STRONG>&nbsp;|&nbsp;";
118                 } else {
119                         $OUT .= "<A href=\"".URL."/modules.php?module=admin&amp;what=list_user&amp;letter=".$letter."&amp;sortby=".$sort.$ADD."\">".$title."</a>&nbsp;|&nbsp;";
120                 }
121         }
122         define('__SORT_LIST', substr($OUT, 0, -13));
123
124         // Load template
125         $OUT = LOAD_TEMPLATE("admin_list_user_sort", true);
126         if ($return)
127         {
128                 // Return code
129                 return $OUT;
130         }
131          else
132         {
133                 // Output code
134                 OUTPUT_HTML($OUT);
135         }
136 }
137
138 // Add page navigation
139 function ADD_PAGENAV($PAGES, $offset, $show_form, $colspan,$return=false)
140 {
141         if (!$show_form)
142         {
143                 // Empty row
144                 define('__FORM_HEADER', "<TR><TD colspan=\"".$colspan."\" class=\"seperator\">&nbsp;</TD></TR>");
145         }
146          else
147         {
148                 // Load form for changing number of lines
149                 define('__FORM_HEADER', LOAD_TEMPLATE("admin_list_user_sort_form", true));
150         }
151         if (!$show_form)
152         {
153                 // Add line with bottom border
154                 define('__FORM_FOOTER', "<TR><TD colspan=\"".__COLSPAN2."\" class=\"seperator bottom2\">&nbsp;</TD></TR>");
155         }
156          else
157         {
158                 // Add line without bottom border
159                 define('__FORM_FOOTER', "<TR><TD colspan=\"".__COLSPAN2."\" class=\"seperator bottom2\">&nbsp;</TD></TR>");
160         }
161
162         $OUT = "";
163         for ($page = 1; $page <= $PAGES; $page++)
164         {
165                 if (($page == $_GET['page']) || ((empty($_GET['page'])) && ($page == "1")))
166                 {
167                         $OUT .= "<STRONG>-";
168                 }
169                  else
170                 {
171                         if (empty($_GET['letter'])) $_GET['letter'] = _ALL2;
172                         if (empty($_GET['sortby'])) $_GET['sortby'] = "userid";
173                         $OUT .= "<A href=\"".URL."/modules.php?module=admin&amp;what=".$GLOBALS['what'];
174                         if (!empty($_GET['mode'])) $OUT .= "&amp;mode=".SQL_ESCAPE($_GET['mode']);
175                         $OUT .= "&amp;letter=".$_GET['letter']."&amp;sortby=".$_GET['sortby']."&amp;page=".$page."&amp;offset=".$offset."\">";
176                 }
177                 $OUT .= $page;
178                 if (($page == $_GET['page']) || ((empty($_GET['page'])) && ($page == "1")))
179                 {
180                         $OUT .= "-</STRONG>";
181                 }
182                  else
183                 {
184                         $OUT .= "</A>";
185                 }
186                 if ($page < $PAGES) $OUT .= "&nbsp;|&nbsp;";
187         }
188         define('__PAGENAV_LIST', $OUT);
189
190         // Load template
191         $OUT = LOAD_TEMPLATE("admin_list_user_pagenav", true);
192         if ($return)
193         {
194                 // Return code
195                 return $OUT;
196         }
197          else
198         {
199                 // Output code
200                 OUTPUT_HTML($OUT);
201         }
202 }
203
204 // Create email link to user's account
205 function USER_CREATE_EMAIL_LINK($email, $mod="admin")
206 {
207         $locked = " AND status='CONFIRMED'";
208         if (IS_ADMIN()) $locked = "";
209         $result = SQL_QUERY_ESC("SELECT userid
210 FROM "._MYSQL_PREFIX."_user_data
211 WHERE email='%s'".$locked." LIMIT 1",
212          array($email), __FILE__, __LINE__);
213         if (SQL_NUMROWS($result) == 1)
214         {
215                 // Load userid
216                 list($uid) = SQL_FETCHROW($result);
217
218                 // Rewrite email address to contact link
219                 $email = URL."/modules.php?module=".$mod."&amp;what=user_contct&amp;u_id=".bigintval($uid);
220         }
221
222         // Free memory
223         SQL_FREERESULT($result);
224
225         // Return rewritten (?) email address
226         return $email;
227 }
228
229 // Selects a random user id as the new referal id if they have at least X confirmed mails in this run
230 function SELECT_RANDOM_REFID () {
231         global $_CONFIG;
232
233         // Default is zero refid
234         $refid = 0;
235
236         // Is the extension version fine?
237         if (GET_EXT_VERSION("user") >= "0.3.4") {
238                 // Get all user ids
239                 $totalUsers = GET_TOTAL_DATA("CONFIRMED", "user_data", "userid", "status", true, " AND `rand_confirmed` >= ".$_CONFIG['user_min_confirmed']."");
240
241                 // Do we have at least one?
242                 if ($totalUsers > 0) {
243                         // Then choose random number
244                         $randNum = mt_rand(0, ($totalUsers - 1));
245
246                         // Look for random user
247                         $result = SQL_QUERY_ESC("SELECT `userid` FROM `"._MYSQL_PREFIX."_user_data` WHERE `status`='CONFIRMED' AND `rand_confirmed` >= %s ORDER BY `rand_confirmed` DESC LIMIT %s, 1",
248                                 array($_CONFIG['user_min_confirmed'], $randNum), __FILE__, __LINE__);
249
250                         // Do we have one entry there?
251                         if (SQL_NUMROWS($result) == 1) {
252                                 // Use that userid as new referal id
253                                 list($refid) = SQL_FETCHROW($result);
254
255                                 // Reset this user's counter
256                                 SQL_QUERY_ESC("UPDATE `"._MYSQL_PREFIX."_iser_data` SET `rand_confirmed`=0 WHERE userid=%s LIMIT 1",
257                                         array($refid), __FILE__, __LINE__);
258                         } // END - if
259
260                         // Free result
261                         SQL_FREERESULT($result);
262                 } // END - if
263         } // END - if
264
265         // Return result
266         return $refid;
267 }
268
269 // [EOF]
270 ?>