array('label' => t('Mugshot')), ); return $fields; } /** Implementation of hook_field_settings **/ function mugshot_cck_field_settings($op, $field) { switch ($op) { case 'database columns': $columns = array( 'mid' => array('type' => 'int', 'not null' => TRUE, 'default' => '0'), ); return $columns; } } /** Implementation of hook_field **/ function mugshot_cck_field($op, &$node, $field, &$items, $teaser, $page) { switch ($op) { case 'validate': // Check for existence of the most recent mugshot, deny if it doesn't exist if (!mugshot_cck_recent_mugshot($_SESSION['mugshot_recent_timestamp'])) { form_set_error('mugshot', t('You did not capture an image using the Flash browser.')); } break; case 'submit': $timestamp = $_SESSION['mugshot_recent_timestamp']; unset($_SESSION['mugshot_recent_timestamp']); // Save mugshut ID as part of the node $mugshot = mugshot_cck_recent_mugshot($timestamp); $items[0]['mid'] = $mugshot->mid; // Update status of mugshot db_query("UPDATE {mugshot} SET preview = 0 WHERE timestamp = %d", $timestamp); // Delete old mugshut images $result = db_query("SELECT mid, path, path_full FROM {mugshot} WHERE preview = 1 AND sid = '%s'", session_id()); $count = 0; while ($mugshot_elem = db_fetch_object($result)) { if (file_exists($mugshot_elem->path)) { file_delete($mugshot_elem->path); } if (file_exists($mugshot_elem->path_full)) { file_delete($mugshot_elem->path_full); } $count++; db_query('DELETE FROM {mugshot} WHERE mid = %d', $mugshot_elem->mid); } break; case 'insert': case 'update': break; case 'load': $field_name = $field['field_name']; $items[0] = mugshot_cck_mugshot($items[0]['mid']); $return = array(); $return[$field_name] = $items; return $return; break; } } /** Implementation of hook_field_formatter_info **/ function mugshot_cck_field_formatter_info() { return array( 'default' => array( 'label' => t('Default Image'), 'field types' => array('mugshot_cck'), ), ); } /** Implementation of hook_field_formatter **/ function mugshot_cck_field_formatter($field, $item, $formatter, $node) { switch ($formatter) { case 'default': $mugshot = mugshot_cck_mugshot($item['mid']); return theme('mugshot_cck', $mugshot); } } /** Widgets **/ /** Implementation of hook_widget_info **/ function mugshot_cck_widget_info() { return array( 'mugshot_cck_flash' => array( 'label' => t('Mugshot Flash'), 'field types' => array('mugshot_cck',), ), ); } function mugshot_cck_widget_settings($op, $widget) { switch ($op) { case 'form': $form = array(); if ($widget['type'] == 'mugshot_cck_flash') { $form['mugshot_flash_resolution'] = array( '#type' => 'select', '#title' => t('Flash module size'), '#default_value' => $widget['mugshot_flash_resolution'], '#options' => array( '320x240' => '320x240', '512x384' => '512x384', '640x480' => '640x480', ), '#description' => t("Flash module size"), '#required' => TRUE ); $form['mugshot_flash_jpeg_quality'] = array( '#type' => 'select', '#title' => t('Flash JPEG quality'), '#default_value' => $widget['mugshot_flash_jpeg_quality'], '#options' => array( '10' => t('Ugly'), '25' => t('Low'), '50' => t('Average'), '75' => t('Normal'), '100' => t('Best'), ), '#description' => t("Flash JPEG quality"), '#required' => TRUE ); $form['mugshot_flash_sound'] = array( '#type' => 'checkbox', '#title' => t("Activate the trigger sound's"), '#default_value' => $widget['mugshot_flash_sound'], '#description' => t("Activate the trigger sound's"), '#return_value' => 1, '#required' => FALSE ); $form['mugshot_flash_gooeffect'] = array( '#type' => 'checkbox', '#title' => t("Activate Goo effect"), '#default_value' => $widget['mugshot_flash_gooeffect'], '#description' => t("Activate Goo effect"), '#return_value' => 1, '#required' => FALSE ); } return $form; case 'validate': break; case 'save': return array('mugshot_flash_resolution', 'mugshot_flash_jpeg_quality', 'mugshot_flash_sound', 'mugshot_flash_gooeffect'); break; } } /** Implementation of hook_widget **/ function mugshot_cck_widget($op, &$node, $field, &$node_field) { switch ($op) { case 'form': $form = array(); $form[$field['field_name']] = array('#tree' => TRUE); $form[$field['field_name']]['mugshot'] = array( '#type' => 'item', '#value' => mugshot_cck_display($field), ); return $form; default: break; } } /** Grabs recent mugshot for this session and timestamp **/ function mugshot_cck_recent_mugshot($timestamp) { return db_fetch_object(db_query("SELECT * FROM {mugshot} WHERE sid = '%s' AND timestamp = %d", session_id(), $timestamp)); } function theme_mugshot_cck($mugshot, $thumb = false) { global $user; if (file_exists($mugshot['path_full'])) { //$content = 'hdf'; $info = image_get_info($mugshot['path_full']); $content .= ''; } return $content; } /** Loads given mugshot from the database, couldn't find this function in mugshot.module, strange... **/ function mugshot_cck_mugshot($mid) { return db_fetch_array(db_query("SELECT * FROM {mugshot} WHERE mid = %d", $mid)); } function mugshot_cck_display($field) { global $user; if (!file_check_directory(file_create_path(mugshot_storage_directory()), FILE_CREATE_DIRECTORY)) { drupal_set_message('The mugshot directory does not exist, or is not writable.', 'error'); } drupal_add_js(drupal_get_path('module', 'mugshot') .'/swfobject.js'); drupal_add_js(drupal_get_path('module', 'mugshot') .'/mugshot.js'); drupal_add_css(drupal_get_path('module', 'mugshot') .'/mugshot.css'); if ($user->uid!=0) { $username = $user->name; } $mugshot_flash_sound = $field['widget']['mugshot_flash_sound'] ? "true" : "false"; $mugshot_flash_gooeffect = $field['widget']['mugshot_flash_gooeffect'] ? "true" : "false"; $mugshot_flash_jpeg_quality = $field['widget']['mugshot_flash_jpeg_quality']; list($flash_width, $flash_height) = explode('x', $field['widget']['mugshot_flash_resolution']); $output .= "

"; $output .= t('Guide : '); if ($mugshot_flash_gooeffect=='true') { $output .= t('You can distort your mugshot with the Goo effect by clicking and moving the mouse on the screen. '); $output .= t('Click on the \'reset\' button to cancel the Goo effect. '); } $output .= t('Click on the \'capture\' button to make a preview. The last preview will be submitted when you click the submit button below. '); $output .= "

"; $url_add = url('mugshot/shot/cck_callback', NULL, NULL, TRUE); $preview_param = 'true'; $url = url('', NULL, NULL, TRUE).drupal_get_path('module', 'mugshot').'/mugshot2.swf?build=3'; $js_callback = 'mugshotTake'; $_SESSION['mugshot_token'] = md5(rand()); $flashvars = '&post_url='.urlencode($url_add).'&sound='.urlencode($mugshot_flash_sound).'&preview='.urlencode($preview_param).'&username='.urlencode($username).'&goo_enable='.urlencode($mugshot_flash_gooeffect).'&jpeg_quality='.urlencode($mugshot_flash_jpeg_quality).'&js_callback='.urlencode($js_callback); $output .= "
\n"; $output .= t('You need to upgrade your Flash Player'); $output .= "
\n"; $output .= "\n"; $output .= "
\n"; $output .= t('What is the privacy pop-up question ?'); $output .= ''; return $output; } function mugshot_cck_add() { // Debugging // watchdog('mugshot', $_SESSION['mugshot_token'] . '----' . print_r($_POST, 1)); global $user; $result = "KO"; // Decode image data and verify image is JFIF $data = base64_decode($_POST['img']); $head = chr(255).chr(216).chr(255).chr(224).chr(0).chr(16)."JFIF"; if ($head == substr($data,0,10)) { // Get $_POST values $width = $_POST['width']; $height = $_POST['height']; $goo_level = $_POST['goo_level']; $camera = substr(trim($_POST['camera']),0,128); // camera model // $username = substr(trim($_POST['username']),0,20); // username typed in the field or transported in url // Create filenames $mid = db_next_id('{mugshot}_mid'); $mugshot_file_full = file_create_filename("mugshot_".md5($mid.time())."_".$width."x".$height.".jpg",mugshot_storage_directory()); $mugshot_file_thumbnail = file_create_filename("mugshot_".md5($mid.time())."_160x120.jpg",mugshot_storage_directory()); // Write large JPEG to mugshot directory $fp = fopen($mugshot_file_full, "w+"); if ($fp) { fwrite($fp, $data); fclose($fp); $gd_full = imagecreatefromjpeg($mugshot_file_full); // Detect if the image is blank, throw it out if so. $blank_status = mugshot_detect_blank($gd_full, $width, $height); if ($blank_status == true) { @unlink($mugshot_file_full); watchdog('mugshot',t('Blank mugshot detected'),WATCHDOG_WARNING); $result = "BLANK"; } else { $visible = 1; $preview = 1; // make thumbnail $gd_thumb = @imageCreateTrueColor(160,120); imagecopyresampled($gd_thumb,$gd_full,0,0,0,0,160,120,$width,$height); imagejpeg($gd_thumb,$mugshot_file_thumbnail,variable_get('mugshot_comment_flash_jpeg_quality', '75')); $ccc = imagecolorat($gd_full, 0, 0); imagedestroy($gd_full); imagedestroy($gd_thumb); $timestamp = time(); $_SESSION['mugshot_recent_timestamp'] = $timestamp; db_query("INSERT INTO {mugshot} (mid, path, hostname, sid, uid, visible, preview, timestamp, camera_model, user_name, goo_level, version, path_full) VALUES (%d ,'%s', '%s', '%s', %d, %d, %d, %d, '%s', '%s', %d, %d, '%s')", $mid, $mugshot_file_thumbnail, $_SERVER['REMOTE_ADDR'], session_id(), $user->uid, $visible, $preview, $timestamp, $camera, $username, $goo_level, 2, $mugshot_file_full); watchdog('mugshot',t('Add mugshot'),WATCHDOG_NOTICE,l(t('See'),'mugshot/'.$mid)); $result = "OK"; } } else { watchdog('mugshot',t('Can\'t write file ').$mugshot_file_full,WATCHDOG_ERROR); } } else { watchdog('mugshot',t('Receive non JPEG data'),WATCHDOG_WARNING); } echo "result=".$result."&mid=".$mid.'&murl='.urlencode(url('',NULL,NULL,TRUE).$mugshot_file_full).'&mturl='.urlencode(url('',NULL,NULL,TRUE).$mugshot_file_thumbnail); flush(); exit; } function mugshot_cck_menu($may_cache) { $items = array(); if ($may_cache) { $items[] = array( 'path' => 'mugshot/shot/cck_callback', 'title' => t("Mugshot CCK Callback"), 'callback' => 'mugshot_cck_add', // TODO: Use proper node access instead of mugshot permissions // (i.e. 'create mugshot content' instead of 'mugshot add photo') 'access' => user_access('mugshot add photo'), 'type' => MENU_LOCAL_TASK); } return $items; }