- Templates for surfbar (admin/member) added when URL got added while unlocking
[mailer.git] / inc / libs / surfbar_functions.php
1 <?php
2 /************************************************************************
3  * MXChange v0.2.1                                    Start: 08/31/2008 *
4  * ===============                              Last change: 08/31/2008 *
5  *                                                                      *
6  * -------------------------------------------------------------------- *
7  * File              : surfbar_functions.php                            *
8  * -------------------------------------------------------------------- *
9  * Short description : Functions for surfbar                            *
10  * -------------------------------------------------------------------- *
11  * Kurzbeschreibung  : Funktionen fuer die Surfbar                      *
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 (ereg(basename(__FILE__), $_SERVER['PHP_SELF'])) {
36         $INC = substr(dirname(__FILE__), 0, strpos(dirname(__FILE__), "/inc") + 4) . "/security.php";
37         require($INC);
38 }
39
40 // Admin has added an URL with given user id
41 function SURFBAR_ADMIN_ADD_URL ($url, $uid, $reward) {
42         // Is this really an admin?
43         if (!IS_ADMIN()) {
44                 // Then leave here
45                 return false;
46         } // END - if
47
48         // Check if that URL does not exist
49         if (SURFBAR_LOOKUP_BY_URL($url, $uid)) {
50                 // Already found!
51                 return false;
52         } // END - if
53
54         // Register the new URL
55         return SURFBAR_REGISTER_URL($url, $uid, $reward, "CONFIRMED", "unlock");
56 }
57 // Looks up by an URL
58 function SURFBAR_LOOKUP_BY_URL ($url) {
59         // Now lookup that given URL by itself
60         $urlArray = SURFBAR_GET_URL_DATA($url, "url");
61
62         // Was it found?
63         return (count($urlArray) > 0);
64 }
65 // Load URL data by given search term and column
66 function SURFBAR_GET_URL_DATA ($searchTerm, $column="id", $order="id", $sort="ASC", $group="id") {
67         global $lastUrlData;
68
69         // By default nothing is found
70         $lastUrlData = array();
71
72         // Is the column an id number?
73         if (($column == "id") || ($column == "userid")) {
74                 // Extra secure input
75                 $searchTerm = bigintval($searchTerm);
76         } // END - if
77
78         // Look up the record
79         $result = SQL_QUERY_ESC("SELECT id, userid, url, reward, views_total, status, registered, last_locked, lock_reason
80 FROM "._MYSQL_PREFIX."_surfbar_urls
81 WHERE %s='%s'
82 ORDER BY %s %s",
83                 array($column, $searchTerm, $order, $sort), __FILE__, __LINE__);
84
85         // Is there at least one record?
86         if (SQL_NUMROWS($result) > 0) {
87                 // Then load all!
88                 while ($dataRow = SQL_FETCHARRAY($result)) {
89                         // Shall we group these results?
90                         if ($group == "id") {
91                                 // Add the row by id as index
92                                 $lastUrlData[$dataRow['id']] = $dataRow;
93                         } else {
94                                 // Group entries
95                                 $lastUrlData[$dataRow[$group]][$dataRow['id']] = $dataRow;
96                         }
97                 } // END - while
98         } // END - if
99
100         // Free the result
101         SQL_FREERESULT($result);
102
103         // Return the result
104         return $lastUrlData;
105 }
106 // Registers an URL with the surfbar. You should have called SURFBAR_LOOKUP_BY_URL() first!
107 function SURFBAR_REGISTER_URL ($url, $uid, $reward, $status="PENDING", $addMode="reg") {
108         global $_CONFIG;
109
110         // Make sure by the user registered URLs are always pending
111         if ($addMode == "reg") $status = "PENDING";
112
113         // Prepare content
114         $content = array(
115                 'url'         => $url,
116                 'frametester' => FRAMETESTER($url),
117                 'uid'         => $uid,
118                 'reward'      => TRANSLATE_COMMA($reward),
119                 'status'      => SURFBAR_TRANSLATE_STATUS($status)
120         );
121
122         // Insert the URL into database
123         $content['insert_id'] = SURFBAR_INSERT_URL_BY_ARRAY($content);
124
125         // If in reg-mode we notify admin
126         if (($addMode == "reg") || ($_CONFIG['surfbar_notify_admin_unlock'] == "Y")) {
127                 // Notify admin even when he as unlocked an email
128                 SURFBAR_NOTIFY_ADMIN("url_{$addMode}", $content);
129         } // END - if
130
131         // Send mail to user
132         SURFBAR_NOTIFY_USER("url_{$addMode}", $content);
133
134         // Return the insert id
135         return $content['insert_id'];
136 }
137 // Inserts an url by given data array and return the insert id
138 function SURFBAR_INSERT_URL_BY_ARRAY ($urlData) {
139         // Just run the insert query for now
140         SQL_QUERY_ESC("INSERT INTO "._MYSQL_PREFIX."_surfbar_urls (userid, url, reward, status) VALUES(%s, '%s', %s, '%s')",
141                 array(
142                         bigintval($urlData['uid']),
143                         bigintval($urlData['url']),
144                         (float)$urlData['reward'],
145                         $urlData['status']
146                 ), __FILE__, __LINE__
147         );
148
149         // Return insert id
150         return SQL_INSERTID();
151 }
152 // Notify admin(s) with a selected message and content
153 function SURFBAR_NOTIFY_ADMIN ($messageType, $content) {
154         // Prepare template name
155         $templateName = sprintf("admin_surfbar_%s", $messageType);
156
157         // Prepare subject
158         $eval = sprintf("\$subject = ADMIN_SURFBAR_NOTIFY_%s_SUBJECT;",
159                 strtoupper($messageType)
160         );
161         eval($eval);
162
163         // Send the notification out
164         SEND_ADMIN_NOTIFICATION($subject, $templateName, $content, $content['uid']);
165 }
166 // Notify the user about the performed action
167 function SURFBAR_NOTIFY_USER ($messageType, $content) {
168         // Prepare template name
169         $templateName = sprintf("member_surfbar_%s", $messageType);
170
171         // Prepare subject
172         $eval = sprintf("\$subject = MEMBER_SURFBAR_NOTIFY_%s_SUBJECT;",
173                 strtoupper($messageType)
174         );
175         eval($eval);
176
177         // Load template
178         $mailText = LOAD_EMAIL_TEMPLATE($templateName, $content);
179
180         // Send the email
181         SEND_EMAIL($content['uid'], $subject, $mailText);
182 }
183 // Translate the URL status
184 function SURFBAR_TRANSLATE_STATUS ($status) {
185         // Create constant name
186         $constantName = sprintf("SURFBAR_URL_STATUS_%s", strtoupper($status));
187
188         // Set default translated status
189         $statusTranslated = "!".$constantName."!";
190
191         // Generate eval() command
192         if (defined($constantName)) {
193                 $eval = "\$statusTranslated = ".$constantName.";";
194                 eval($eval);
195         } // END - if
196
197         // Return result
198         return $statusTranslated;
199 }
200 //
201 ?>