All rights reserved. * * Copyright (c) 2005-2007. * Jeremy Andrews . All rights reserved. */ /** * Function used to display the selected ad. */ function ad_flash_display_ad($ad, $hostid = NULL) { $flash = db_fetch_object(db_query('SELECT a.fid, a.url, a.width, a.height, f.filepath FROM {ad_flash} a INNER JOIN {files} f ON a.fid = f.fid WHERE a.aid = %d AND f.nid = %d', $ad->aid, $ad->aid)); // Strip module path from flash path if serving flash files from adserve.php. $flash->path = preg_replace('&'. drupal_get_path('module', 'ad') .'/&', '', file_create_url($flash->filepath)); return theme('ad_flash_swf_render', $ad, $flash); } /** * Drupal _init hook. Include ad_flash_views.inc if the views.module is * enabled. */ function ad_flash_init() { if (function_exists('drupal_set_content')) { if (module_exists('views')) { include drupal_get_path('module', 'ad_flash'). '/ad_flash_views.inc'; } } } /** * Implementation of hook_help(). */ function ad_flash_help($path) { switch ($path) { case 'node/add/ad#flash': $output = t('A flash based banner advertisement.'); break; } return $output; } /** * Implementation of hook_menu(). */ function ad_flash_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'admin/content/ad/configure/ad_flash', 'title' => t('Flash ads'), 'callback' => 'drupal_get_form', 'callback arguments' => array('ad_flash_group_settings'), 'type' => MENU_LOCAL_TASK, 'weight' => 3, ); } else { if (arg(0) == 'node' && is_numeric(arg(1)) && ad_adaccess(arg(1), 'convert urls in flash ads')) { $node = node_load(array('nid' => arg(1))); // node_load() is cached if ($node->adtype == 'flash') { $items[] = array( 'path' => "node/$node->nid/flash_converturls", 'title' => t('Convert links URLs in flash file'), 'callback' => 'drupal_get_form', 'callback arguments' => array('ad_flash_convertform', $node->nid), 'type' => MENU_LOCAL_TASK, 'access' => ad_adaccess($node->nid, 'convert urls in flash ads')); } } } return $items; } /** * Display the convertion form */ function ad_flash_convertform($nid) { $node = node_load($nid); $form = array(); $form['nid'] = array( '#type' => 'value', '#value' => $nid, ); // include the flash url converting logic _ad_flash_include_swfconverter(); $file = (object) ad_flash_active_file($node->files); // let's open the flash file $swf_file = @fread(@fopen($file->filepath, 'rb'), @filesize($file->filepath)); // if we were unable to open the file then skips url rewriting if(!$swf_file) { drupal_set_message(t('Unable to open the Flash file. Links conversion not available.'), 'error'); return; } // let's get swf urls info $links = phpAds_SWFInfo($swf_file); if($links && count($links)) { // urls have been found in the submitted file. Let's the user able to configure what to do with them. // poputate the #options array from the found links $options = array(); foreach($links as $id => $link) { $options[$id] = $link[0]; } $form['converturls'] = array ( '#type' => 'checkboxes', '#title' => t('Select the link urls to override'), '#options' => $options, '#description' => t('Checked URLs will be overridden.'), ); $form['save'] = array( '#type' => 'submit', '#value' => t('Convert'), ); } else { drupal_set_message(t('Unable to find link urls in the Flash file. Links conversion not available.'), 'error'); } return $form; } /** * This function is called on the convertion form submit. * It implements the URLs convertion logic */ function ad_flash_convertform_submit($form_id, $form_values) { $node = node_load($form_values['nid']); // load the active flash file $file = (object) ad_flash_active_file($node->files); // open the original file $swf_file = @fread(@fopen($file->filepath, 'rb'), @filesize($file->filepath)); if($swf_file) { _ad_flash_include_swfconverter(); list($newbuffer, $params) = phpAds_SWFConvert($swf_file, true, $form_values['converturls']); // generate a new temp file path $newpath = tempnam(file_directory_temp(), 'ad_flash_converted_'); // open the new temp file $newfileondisk = fopen($newpath, 'wb'); // store the converted swf on the new file fwrite($newfileondisk, $newbuffer); // close buffer streams fclose($newfileondisk); // fclose($swf_file); -- this fails for some reasons // Now let's save the new converted file into the files table // move the video to the definitive location $convfile = array( 'filename' => basename('converted_' . $file->filename), 'filemime' => 'application/x-shockwave-flash', 'filesize' => filesize($newpath), 'filepath' => $newpath, 'nid' => $node->nid, ); $convfile = ((object) $convfile); if (file_copy($convfile, file_directory_path())) { $convfile->fid = db_next_id('{files}_fid'); // add the new file to files tables db_query("INSERT INTO {files} (fid, nid, filename, filepath, filemime, filesize) VALUES (%d, %d, '%s', '%s', '%s', %d)", $convfile->fid, $node->nid, $convfile->filename, $convfile->filepath, $convfile->filemime, $convfile->filesize); db_query("INSERT INTO {file_revisions} (fid, vid, list, description) VALUES (%d, %d, %d, '%s')", $convfile->fid, $node->vid, 1, $convfile->description); // change the ad_flash table to point to the new swf file db_query('UPDATE {ad_flash} SET fid = %d WHERE aid=%d', $convfile->fid, $node->nid); // do not list the original file db_query("UPDATE {file_revisions} SET list=0 WHERE fid=%d AND vid=%d", $file->fid, $node->vid); // delete the temp file unlink($newpath); } else { drupal_set_message(t('Unable to save the converted flash file Flash file. Links conversion not available.'), 'error'); } } else { drupal_set_message(t('Unable to open the Flash file. Links conversion not available.'), 'error'); return; } // redirect the user to the node view drupal_goto('node/'. $node->nid); } function ad_flash_group_settings($edit = array()) { $form = array(); $result = db_query('SELECT * FROM {ad_groups}'); while ($group = db_fetch_object($result)) { $form["group-$group->gid"] = array( '#type' => 'fieldset', '#title' => $group->name, '#collapsible' => TRUE, ); $form["group-$group->gid"]["description-$group->gid"] = array( '#type' => 'markup', '#prefix' => '
', '#suffix' => '
', '#value' => $group->description, ); $format = db_fetch_object(db_query('SELECT * FROM {ad_flash_format} WHERE gid = %d', $group->gid)); $form["group-$group->gid"]["min-height-$group->gid"] = array( '#type' => 'textfield', '#title' => t('Minimum height'), '#size' => 5, '#maxlength' => 5, '#default_value' => $format->min_height ? $format->min_height : 0, '#description' => t('Optionally specify a minimum height in pixels for flash files in this group. To specify no minimum height, enter 0.'), ); $form["group-$group->gid"]["min-width-$group->gid"] = array( '#type' => 'textfield', '#title' => t('Minimum width'), '#size' => 5, '#maxlength' => 5, '#default_value' => $format->min_width ? $format->min_width : 0, '#description' => t('Optionally specify a minimum width in pixels for flash files in this group. To specify no minimum width, enter 0.'), ); $form["group-$group->gid"]["max-height-$group->gid"] = array( '#type' => 'textfield', '#title' => t('Maximum height'), '#size' => 5, '#maxlength' => 5, '#default_value' => $format->max_height ? $format->max_height : 0, '#description' => t('Optionally specify a maximum height in pixels for flash files in this group. To specify no maximum height, enter 0.'), ); $form["group-$group->gid"]["max-width-$group->gid"] = array( '#type' => 'textfield', '#title' => t('Maximum width'), '#size' => 5, '#maxlength' => 5, '#default_value' => $format->max_width ? $format->max_width : 0, '#description' => t('Optionally specify a maximum width in pixels for flash files in this group. To specify no maximum width, enter 0.'), ); } $form['save'] = array( '#type' => 'submit', '#value' => t('Save'), ); return $form; } /** * Save min and max flash width and height values for ad groups. */ function ad_flash_group_settings_submit($form_id, $form_values) { $result = db_query('SELECT gid FROM {ad_groups}'); while ($group = db_fetch_object($result)) { if (db_result(db_query('SELECT gid FROM {ad_flash_format} WHERE gid = %d', $group->gid))) { db_query("UPDATE {ad_flash_format} SET min_width = %d, max_width = %d, min_height = %d, max_height = %d WHERE gid = %d", $form_values["min-width-$group->gid"], $form_values["max-width-$group->gid"], $form_values["min-height-$group->gid"], $form_values["max-height-$group->gid"], $group->gid); } else { db_query("INSERT INTO {ad_flash_format} (gid, min_width, max_width, min_height, max_height) VALUES (%d, %d, %d, %d, %d)", $group->gid, $form_values["min-width-$group->gid"], $form_values["max-width-$group->gid"], $form_values["min-height-$group->gid"], $form_values["max-height-$group->gid"]); } } drupal_set_message('Flash ad global settings updated.'); } /** * Adapi implementation. */ function ad_flash_adapi($op, &$node) { $output = NULL; switch ($op) { case 'load': return db_fetch_array(db_query("SELECT * FROM {ad_flash} WHERE aid = %d", $node['aid'])); case 'validate': if (!valid_url($node->url, TRUE)) { form_set_error('url', t('You must specify a valid %field.', array('%field' => t('Destination URL')))); } break; case 'submit': break; case 'insert': case 'update': $flash = ad_flash_load_flash($node); $file = (object) ad_flash_active_file($node->files); $fid = (int) $file->fid; // This is ugly, but as "a" comes before "u" we don't seem to be able // to modify the upload module's form. Instead, we check after the fact // if someone is editing flash files when they're not allowed, and if so we // prevent the ad from being saved. if ($op == 'update' && !ad_adaccess($node->nid, 'manage active ad')) { // See if fid is changing -- it's okay if new flash files are uploaded, it's // just not okay if the active fid is changed. if ($fid != $flash->fid) { drupal_set_message('You do not have the necessary permissions to change the active advertisement.', 'error'); // This causes upload_save() to simply return without making any // changes to the files attached to this node. unset($node->files); } } else { if ($flash !== FALSE) { $node->fid = $flash->fid; $node->width = $flash->width; $node->height = $flash->height; } } if ($op == 'insert') { db_query("INSERT INTO {ad_flash} (aid, fid, url, width, height) VALUES(%d, %d, '%s', %d, %d)", $node->nid, $fid, $node->url, $node->width, $node->height); } else { db_query("UPDATE {ad_flash} SET fid = %d, url = '%s', width = %d, height = %d WHERE aid = %d", $fid, $node->url, $node->width, $node->height, $node->nid); } // No valid flash has been uploaded, don't allow ad to be 'active'. //$ad_active = (object) ad_flash_active_file($node->files); if(!ad_flash_active_file($node->files)) { db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); if (db_affected_rows()) { drupal_set_message(t('Unable to mark ad as %active as you have not uploaded any valid flash files. Setting ad as %pending.', array('%active' => t('active'), '%pending' => t('pending'))), 'error'); } } else if (!$fid) { db_query("UPDATE {ads} SET adstatus = '%s' WHERE aid = %d AND adstatus = '%s'", t('pending'), $node->nid, t('active')); if (db_affected_rows()) { drupal_set_message(t('Unable to mark ad as active until uploaded flash is validated. If you do not see any more errors, you should now be able to set your ad as active.'), 'error'); } } break; case 'delete': db_query('DELETE FROM {ad_flash} WHERE aid = %d', $node->nid); break; case 'form': return ad_flash_node_form($node); case 'view': return ad_flash_node_view($node); case 'redirect': return db_result(db_query('SELECT url FROM {ad_flash} WHERE aid = %d', $node->nid)); case 'type': return 'flash'; case 'permissions': if (!isset($node->adtype) || $node->adtype == 'flash') { return array(t('manage active ad'), t('convert urls in flash ads')); } case 'check_install': if (!module_exists('upload')) { drupal_set_message(t("The required upload module is not enabled, you will not be able to upload flash ads. Please %enable the upload module, or %disable the ad_flash module.", array('%enable' => l('enable', 'admin/modules'), '%disable' => l('disable', 'admin/modules'))), 'error'); } break; } return $output; } /** * Determine the currently active ad. */ function ad_flash_active_file($files = array()) { foreach ($files as $fid => $data) { if (is_array($data)) { if ($data['list'] && !$data['remove']) { return $data; } } else if ($data->list && !$data->remove) { return $data; } } } function ad_flash_format_load($gid) { static $format; if (!is_null($format[$gid])) { return $format[$gid]; } $format[$gid] = db_fetch_object(db_query('SELECT * FROM {ad_flash_format} WHERE gid = %d', $gid)); return $format[$gid]; } /** * Validate that the size of the uploaded flash is within the defined limits. */ function ad_flash_validate_size($file, $gid) { $size = NULL; $error = FALSE; if (is_object($file)) { $format = ad_flash_format_load($gid); list($size->width, $size->height) = getimagesize($file->filepath); if ($size->width < $format->min_width) { drupal_set_message(t('The flash %name is only %current pixels wide, which is less than the minimum of %minimum pixels allowed in the selected ad group.', array('%name' => $file->filename, '%current' => $size->width, '%minimum' => $format->min_width)), 'error'); $error = TRUE; } else if ($format->max_width && ($size->width > $format->max_width)) { drupal_set_message(t('The flash %name is %current pixels wide, which is more than the maximum of %maximum pixels allowed in the selected ad group.', array('%name' => $file->filename, '%current' => $size->width, '%maximum' => $format->max_width)), 'error'); $error = TRUE; } if ($size->height < $format->min_height) { drupal_set_message(t('The flash %name is only %current pixels high, which is less than the minimum of %minimum pixels allowed in the selected ad group.', array('%name' => $file->filename, '%current' => $size->height, '%minimum' => $format->min_height)), 'error'); $error = TRUE; } else if ($format->max_height && $size->height > $format->max_height) { drupal_set_message(t('The flash %name is %current pixels high, which is more than the maximum of %maximum pixels allowed in the selected ad group.', array('%name' => $file->filename, '%current' => $size->height, '%maximum' => $format->max_height)), 'error'); $error = TRUE; } } if ($error) { return FALSE; } else { return $size; } } /** * */ function ad_flash_update_node(&$data) { } /** * */ function ad_flash_load_flash($node) { if (is_array($node->files)) { foreach ($node->files as $file) { if (is_array($file)) { if ($file['list'] && file_exists($file['filepath'])) { $flash = ad_flash_validate_size((object)$file, $node->gid); if ($flash !== FALSE) { $flash->fid = $file['fid']; return $flash; } } } else { if ($file->list && file_exists($file->filepath)) { $flash = ad_flash_validate_size($file, $node->gid); if ($flash !== FALSE) { $flash->fid = $file->fid; return $flash; } } } } } return FALSE; } /** * Adapi helper function for displaying a node form. */ function ad_flash_node_form(&$node) { $form = array(); ad_flash_adapi('check_install', $node); $form['ad_flash'] = array( '#type' => 'fieldset', '#title' => t('Flash File'), '#collapsible' => TRUE, ); if (is_array($node->files)) { $files = $node->files; } else { $files = module_invoke('upload', 'load', $node); } $num = sizeof($files); $path = NULL; $active = 0; if ($num) { foreach ($files as $file) { if ($file->list && file_exists($file->filepath)) { list($flash->width, $flash->height) = getimagesize($file->filepath); $flash->path = file_create_url($file->filepath); $path .= theme('ad_flash_swf_render', null, $flash); $flash = ad_flash_validate_size($file, $node->gid); if ($flash === FALSE) { $path .= t('(invalid flash file)'). '
'; } else if (!$active++) { $path .= t('(active)'). '
'; } else { $path .= t('(inactive)'). '
'; } } else if (!file_exists($file->filepath)) { drupal_set_message(t('Unable to locate flash %flash.', array('%flash' => "$file->filepath"))); $path .= t('Unable to locate the uploaded flash.'); } } } if ($path == NULL) { $path = t('No flash files have been uploaded. Please upload an flash file via the File attachments form section below.
'); // Only set error if node has been previewed or submitted. if (isset($_POST['edit'])) { form_set_error('upload', t('It is required that you upload an flash for your flash advertisement.')); } } $path .= t('
Only the first uploaded flash file that has List checked in the File attachments form section below will be displayed as an advertisement. The flash that will be displayed is marked as active above.'); $form['ad_flash']['flash'] = array( '#type' => 'markup', '#value' => $path, '#prefix' => '
', '#suffix' => '
', ); $form['ad_flash']['flash'] = array( '#type' => 'markup', '#value' => $path, '#prefix' => '
', '#suffix' => '
', ); $form['ad_flash']['url'] = array( '#type' => 'textfield', '#title' => t('Destination URL'), '#required' => TRUE, '#default_value' => $node->url, '#description' => t('Enter the complete URL where you want people to be redirected when they click on this advertisement. The URL must begin with http:// or https://. For example, %url.', array('%url' => t('http://www.sample.org/'))), ); return $form; } function ad_flash_node_view(&$node) { $ad = db_fetch_object(db_query('SELECT aid, redirect FROM {ads} WHERE aid = %d', $node->nid)); $node->content['ad'] = array( '#value' => ad_flash_display_ad($ad) .'
'. t('Links to %url.', array('%url' => $node->url)), '#weight' => -1, ); } /* Themeable functions */ /** * Displays a flash advertisement */ function theme_ad_flash_swf_render($ad, $flash) { $output = "
aid\">"; $redirect_url = urlencode(url('ad/redirect/'.$ad->aid, NULL, NULL, true)); // we also add clicktag so that already $swfurl = $flash->path . '?alink1=' . $redirect_url . '&atar1=_blank&clicktag=' . $redirect_url; // this will be executed by not Internet Explorer browsers $output .= ' ' . "\n"; // this will be executed by Internet Explorer $output .= '' . "\n"; // params will be passed to both IE or not IE browsers $output .= '' . "\n" . '

'. t('Your browser is not able to display this multimedia content.
Please download the latest Flash player.') .'

'; $output .= "
"; return $output; } /* Helper functions */ /** * Include the flash urls converter logic */ function _ad_flash_include_swfconverter() { include_once(drupal_get_path('module', 'ad_flash') .'/lib-swf.inc.php'); } ?>