2 /************************************************************************
3 * Mailer v0.2.1-FINAL Start: 07/25/2011 *
4 * =================== Last change: 07/25/2011 *
6 * -------------------------------------------------------------------- *
7 * File : forced_functions.php *
8 * -------------------------------------------------------------------- *
9 * Short description : Functions for ext-forced *
10 * -------------------------------------------------------------------- *
11 * Kurzbeschreibung : Funktionen fuer ext-forced *
12 * -------------------------------------------------------------------- *
15 * $Tag:: 0.2.1-FINAL $ *
17 * -------------------------------------------------------------------- *
18 * Copyright (c) 2003 - 2009 by Roland Haeder *
19 * Copyright (c) 2009 - 2016 by Mailer Developer Team *
20 * For more information visit: http://mxchange.org *
22 * This program is free software; you can redistribute it and/or modify *
23 * it under the terms of the GNU General Public License as published by *
24 * the Free Software Foundation; either version 2 of the License, or *
25 * (at your option) any later version. *
27 * This program is distributed in the hope that it will be useful, *
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
30 * GNU General Public License for more details. *
32 * You should have received a copy of the GNU General Public License *
33 * along with this program; if not, write to the Free Software *
34 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *
36 ************************************************************************/
38 // Some security stuff...
39 if (!defined('__SECURITY')) {
43 // Translate forced ad type
44 function translateForcedAdsType ($status) {
45 // Is the status null or empty?
46 if (is_null($status)) {
49 } elseif (empty($status)) {
54 // Default status is unknown
55 $return = '{%message,ADMIN_FORCED_ADS_TYPE_UNKNOWN=' . $status . '%}';
57 // Construct message id
58 $messageId = 'ADMIN_FORCED_ADS_TYPE_' . $status;
61 if (isMessageIdValid($messageId)) {
62 // Then use it as message string
63 $return = '{--' . $messageId . '--}';
70 // Translate forced campaign status
71 function translateForcedCampaignStatus ($status) {
72 // Is the status null or empty?
73 if (is_null($status)) {
76 } elseif (empty($status)) {
81 // Default status is unknown
82 $return = '{%message,ADMIN_FORCED_CAMPAIGN_TYPE_UNKNOWN=' . $status . '%}';
84 // Construct message id
85 $messageId = 'ADMIN_FORCED_CAMPAIGN_TYPE_' . $status;
88 if (isMessageIdValid($messageId)) {
89 // Then use it as message string
90 $return = '{--' . $messageId . '--}';
97 // Translate visibility
98 function translateForcedCostsVisibility ($visibility) {
99 // Is the status null or empty?
100 if (is_null($visibility)) {
102 $visibility = 'NULL';
103 } elseif (empty($visibility)) {
105 $visibility = 'EMPTY';
108 // Default status is unknown
109 $return = '{%message,ADMIN_FORCED_ADS_VISIBILITY_UNKNOWN=' . $visibility . '%}';
111 // Construct message id
112 $messageId = 'ADMIN_FORCED_ADS_VISIBILITY_' . $visibility;
115 if (isMessageIdValid($messageId)) {
116 // Then use it as message string
117 $return = '{--' . $messageId . '--}';
124 // "Getter" for an array of data from all forced-popups except user's own
125 function getForcedPopupDataByEarningArray ($content) {
126 die(__FUNCTION__.':content=<pre>' . print_r($content, TRUE) . '</pre>' . debug_get_printable_backtrace());
128 // Finally return the content
132 // Generator (somewhat getter) for forced-ads type options
133 function generateForcedAdsTypeOptions ($type = NULL) {
135 if (!isset($GLOBALS[__FUNCTION__][$type])) {
136 // Generate output and cache it
137 $GLOBALS[__FUNCTION__][$type] = generateOptions(
148 'translateForcedAdsType'
153 return $GLOBALS[__FUNCTION__][$type];
156 // Generator (somewhat getter) for forcec-costs visibility options
157 function generateForcedCostsVisibilityOptions ($visibility = NULL) {
159 if (!isset($GLOBALS[__FUNCTION__][$visibility])) {
160 // Generate output and cache it
161 $GLOBALS[__FUNCTION__][$visibility] = generateOptions(
173 'translateForcedCostsVisibility'
178 return $GLOBALS[__FUNCTION__][$visibility];
181 // "Getter" for an array of all given forced ads
182 function getArrayFromForcedAdsByType ($forcedType = NULL) {
184 $forcedAds = array();
186 // Init WHERE (default)
189 // Is forced type not NULL?
190 if (!is_null($forcedType)) {
191 // Then set proper WHERE
192 $where = sprintf("WHERE `forced_ads_type`='%s'", sqlEscapeString($forcedType));
195 // Search all given types
196 $result = sqlQuery('SELECT
199 `forced_ads_click_url`,
202 `forced_ads_alt_text`,
203 `forced_ads_framebreaker`,
205 `forced_ads_views_counter`,
206 `forced_ads_clicks_counter`,
207 UNIX_TIMESTAMP(`forced_ads_added`) AS `forced_ads_added`,
210 `{?_MYSQL_PREFIX?}_forced_ads`
213 `forced_ads_id` ASC', __FUNCTION__, __LINE__);
215 // Is there at least one entry?
216 if (!ifSqlHasZeroNumRows($result)) {
218 while ($content = sqlFetchArray($result)) {
220 $forcedAds[$content['forced_ads_id']] = $content;
225 sqlFreeResult($result);
231 // "Getter" for an array of all forced costs from given visibility or all
232 function getArrayFromForcedCostsByVisibility ($visibility = NULL) {
234 $forcedCosts = array();
237 // Is visibility not NULL?
238 if (!is_null($visibility)) {
239 // Then add WHERE statement
240 $where = sprintf(" WHERE `forced_costs_visibility`='%s'", sqlEscapeString($visibility));
243 // Search all given types
244 $result = sqlQuery("SELECT
246 `forced_costs_price_click`,
247 `forced_costs_payment_click`,
248 `forced_costs_visibility`
250 `{?_MYSQL_PREFIX?}_forced_costs`
253 `forced_costs_id` ASC", __FUNCTION__, __LINE__);
255 // Is there at least one entry?
256 if (!ifSqlHasZeroNumRows($result)) {
258 while ($content = sqlFetchArray($result)) {
260 $forcedCosts[$content['forced_costs_id']] = $content;
265 sqlFreeResult($result);
271 // "Getter" for an array of all forced campaigns from given array and status
272 function getArrayFromForcedCampaignsByArray ($ids, $status) {
274 $forcedCampaigns = array();
276 // Search all given types
277 $result = sqlQueryEscaped("SELECT
278 `forced_campaign_id`,
279 UNIX_TIMESTAMP(`forced_campaign_created`) AS `forced_campaign_created`,
280 `forced_campaign_status`,
281 UNIX_TIMESTAMP(`forced_campaign_last_locked`) AS `forced_campaign_last_locked`,
282 `forced_campaign_lock_reason`,
283 `forced_campaign_expired`,
284 `forced_campaign_ordered_clicks`,
285 `forced_campaign_payment_api`,
286 `forced_campaign_reload_lock`,
287 `forced_campaign_minimum_stay`,
289 `forced_campaign_userid`,
292 `{?_MYSQL_PREFIX?}_forced_campaigns`
294 `forced_campaign_id` IN (%s) AND
295 `forced_campaign_status`='%s'
297 `forced_campaign_id` ASC",
301 ), __FUNCTION__, __LINE__);
303 // Is there at least one entry?
304 if (!ifSqlHasZeroNumRows($result)) {
306 while ($content = sqlFetchArray($result)) {
308 $forcedCampaigns[$content['forced_campaign_id']] = $content;
313 sqlFreeResult($result);
316 return $forcedCampaigns;
319 // "Activates" forced campaigns from POST data
320 function doForcedAdminActivateCampaigns () {
321 // Do some sanity checks
323 // Not allowed to call
325 } elseif (countPostSelection('forced_campaign_id') == 0) {
326 // No selections made
330 // Get an array from all pending campaigns
331 $forcedCampaigns = getArrayFromForcedCampaignsByArray(array_keys(postRequestElement('forced_campaign_id')), 'PENDING');
333 // By default all worked (at this point)
336 // Run through all entries
337 foreach ($forcedCampaigns as $campaignId => $campaignData) {
338 // Change the status of this campaign
339 if (!changeForcedCampaignStatus($campaignId, 'PENDING', 'ACTIVE', $campaignData)) {
340 // One didn't work, but continue with others
349 // Changes the status of a forced campaign from previous to new status
350 function changeForcedCampaignStatus ($campaignId, $prevStatus, $newStatus, $data) {
351 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'campaignId=' . $campaignId . ',prevStatus=' . $prevStatus . ',data[]=' . gettype($data) . ',newStatus=' . $newStatus . ' - ENTERED!');
352 // Make new status always lower-case
353 $newStatus = strtolower($newStatus);
357 'campaign_id' => $campaignId,
358 'prev_status' => $prevStatus,
359 'new_status' => $newStatus,
364 // Run pre filter chain
365 $filterData = runFilterChain('pre_change_forced_campaign_status', $filterData);
368 if (!is_null($filterData['abort'])) {
370 return $filterData['abort'];
373 // Update the status now
374 // ---------- Comment out for debugging/developing member actions! ---------
375 sqlQueryEscaped("UPDATE `{?_MYSQL_PREFIX?}_forced_campaigns` SET `forced_campaign_status`='%s' WHERE `forced_campaign_id`=%s LIMIT 1",
378 bigintval($campaignId)
379 ), __FUNCTION__, __LINE__);
380 // ---------- Comment out for debugging/developing member actions! ---------
383 // ---------- Comment out for debugging/developing member actions! ---------
384 if (sqlAffectedRows() != 1) {
385 // No, something went wrong
388 // ---------- Comment out for debugging/developing member actions! ---------
390 // Run post filter chain
391 $filterData = runFilterChain('post_change_forced_campaign_status', $filterData);
393 // Check if generic 'data' is there
394 assert(isset($filterData['data']));
397 //* DEBUG: */ logDebugMessage(__FUNCTION__, __LINE__, 'campaignId=' . $campaignId . ',prevStatus=' . $prevStatus . ',newStatus=' . $newStatus . ' - EXIT!');
401 // Notify admin(s) with a selected message and content
402 function doForcedNotifyAdmin ($messageType, $content) {
403 // Prepare template name
404 $templateName = sprintf('admin_forced_%s', $messageType);
406 // Is the user id valid?
407 if (!isValidId($content['forced_campaign_userid'])) {
408 // No, then use admin-only template
409 $templateName = sprintf('admin_only_forced_%s', $messageType);
413 $subject = sprintf('{--ADMIN_FORCED_NOTIFY_%s_SUBJECT--}', strtoupper($messageType));
415 // Is the subject line there?
416 if ((substr($subject, 0, 1) == '!') && (substr($subject, -1, 1) == '!')) {
417 // Set default subject if following eval() wents wrong
418 $subject = '{%message,ADMIN_FORCED_NOTIFY_DEFAULT_SUBJECT=' . strtoupper($messageType) . '%}';
421 // Translate some data if present
422 $content = prepareForcedContentForTemplate($content);
424 // Send the notification out
425 return sendAdminNotification($subject, $templateName, $content, $content['forced_campaign_userid']);
428 // Notify the user about the performed action
429 function doForcedNotifyMember ($messageType, $content) {
430 // Skip notification if userid is NULL
431 if (!isValidId($content['forced_campaign_userid'])) {
432 // NULL is silently ignored
436 // Prepare template name
437 $templateName = sprintf('member_forced_%s', $messageType);
440 $subject = sprintf('{--MEMBER_FORCED_NOTIFY_%s_SUBJECT--}', strtoupper($messageType));
442 // Is the subject line there?
443 if ((substr($subject, 0, 1) == '!') && (substr($subject, -1, 1) == '!')) {
444 // Set default subject if following eval() wents wrong
445 $subject = '{--MEMBER_FORCED_NOTIFY_DEFAULT_SUBJECT--}';
448 // Translate some data if present
449 $content = prepareForcedContentForTemplate($content);
452 $mailText = loadEmailTemplate($templateName, $content, $content['forced_campaign_userid']);
455 return sendEmail($content['forced_campaign_userid'], $subject, $mailText);
458 // Translates some data for template usage
459 // @TODO Can't we use our new expression language instead of this ugly code?
460 function prepareForcedContentForTemplate ($content) {
462 if (isset($content['forced_campaign_created'])) $content['forced_campaign_created'] = generateDateTime($content['forced_campaign_created'], 2);
464 // Return translated content
468 // ----------------------------------------------------------------------------
469 // Template helper functions
470 // ----------------------------------------------------------------------------
472 // Template helper function to create selection box for all forced-ad types
473 function doTemplateForcedAdTypeSelectionBox ($templateName, $clear = FALSE, $default = NULL) {
474 // Add all types through the filter
475 $forcedAdTypes = runFilterChain('add_forced_ads_types', array());
477 // Handle it over to generateSelectionBoxFromArray()
478 $content = generateSelectionBoxFromArray($forcedAdTypes, 'forced_ads_type', 'type', '', '', '', $default);
480 // Return prepared content
484 // Template helper function to create selection box for costs visibility
485 function doTemplateForcedCostsVisiblitySelectionBox ($templateName, $clear = FALSE, $default = NULL) {
487 $costsVisibility = array(
488 0 => array('visibility' => 'ALL'),
489 1 => array('visibility' => 'MEMBER'),
490 2 => array('visibility' => 'SPONSOR'),
491 3 => array('visibility' => 'NONE')
494 // Handle it over to generateSelectionBoxFromArray()
495 $content = generateSelectionBoxFromArray($costsVisibility, 'forced_costs_visibility', 'visibility', '', '', '', $default);
497 // Return prepared content
501 // Template helper function to create selection box for foreced ads
502 function doTemplateForcedAdSelectionBox ($templateName, $clear, $default = NULL) {
503 // Get back an array of all forced popups
504 $forcedAds = getArrayFromForcedAdsByType();
506 // Handle it over to generateSelectionBoxFromArray()
507 $content = generateSelectionBoxFromArray($forcedAds, 'forced_ads_id', 'forced_ads_id', '', '', '', $default, 'forced_ads_type');
509 // Return prepared content
513 // Template helper function to create selection box for foreced costs
514 function doTemplateForcedCostsSelectionBox ($templateName, $clear, $default = NULL) {
515 // Get back an array of all forced popups
516 $forcedCosts = getArrayFromForcedCostsByVisibility();
518 // Handle it over to generateSelectionBoxFromArray()
519 $content = generateSelectionBoxFromArray($forcedCosts, 'forced_costs_id', 'forced_costs_id', '', '', '', $default, '', TRUE);
521 // Return prepared content
525 // Template helper function to create selection box for foreced campaign userid
526 function doTemplateForcedCampaignUseridSelectionBox ($templateName, $clear, $default = NULL) {
527 // Generate the selection box without all
528 $content = addMemberSelectionBox($default, FALSE, TRUE, TRUE, 'forced_campaign_userid');
530 // Return prepared content
534 // ----------------------------------------------------------------------------
535 // XML call-back functions
536 // ----------------------------------------------------------------------------
538 // For 'doing' add forced-ads, the column-index is required
539 function addXmlSpecialAdminAddDoForcedAds () {
540 // So set it all here
541 $GLOBALS['__COLUMN_INDEX']['doXmlCallbackFunction'] = 'column';
542 $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['column_index'] = 'column';
545 // For 'doing' add forced-campaigns, the column-index is required
546 function addXmlSpecialAdminAddDoForcedCampaigns () {
547 // So set it all here
548 $GLOBALS['__COLUMN_INDEX']['doXmlCallbackFunction'] = 'column';
549 $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['column_index'] = 'column';
552 // For 'doing' add forced-costs, the column-index is required
553 function addXmlSpecialAdminAddDoForcedCosts () {
554 // So set it all here
555 $GLOBALS['__COLUMN_INDEX']['doXmlCallbackFunction'] = 'column';
556 $GLOBALS['__XML_ARGUMENTS']['doXmlCallbackFunction']['column_index'] = 'column';