~ubuntu-drupal-devs/ubuntu-drupal-countdown/6.x

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
// $Id$
 
/*
 * @file
 * Drupal Module: Ubuntu Release Countdown
 * Ubuntu Countdown displays a customizable block to inform the visitor
 * when a new version of Ubuntu will be released. It will contain the
 * ability to display either of the four options created by the Web
 * Presence team or one of your own.
 */
 
/**
 * Implementation of hook_perm - Defines user permissions.
 * The suggested naming convention for permissions is "action_verb modulename".
 *
 * @return An array of permissions strings.
 *
 * @ingroup base
 */

function udcountdown_perm() {
  return array('administer udcountdown', 'view udcountdown');
}

/**
 * Implementation of hook_help - Provides online user help.
 * 
 * @param $path A Drupal menu router path the help is being requested for
 * @param $arg
 * @return A localized string containing the help text.
 *
 * @ingroup base
 */

function udcountdown_help($path, $arg) {
  switch ($path) {
    case 'admin/help#udcountdown':
      $output = '<p>Ubuntu Release Countdown is a module that will allow Drupal administrators to add a code block that displays the countdown until the next version of Ubuntu. It allows you to use any of the precoded options, or your own. The available options are listed at http://ubuntu.com/getubuntu/countdown.</p>';
      $output .= '<p>To use planet, go to admin/settings/udcountdown and note the following sections.</p>';
      $output .= '<b>Standard:</b><ul>';
      $output .= '<li><b>Display Option</b> - This allows you to pick which banner will be used. The first option allows you to use your own custom code. The rest of the options are pulled from the <a href="http://www.ubuntu.com/getubuntu/countdown">countdown section</a> on the Ubuntu website.</li>';
      $output .= '<li><b>Custom Code</b> - If you do not like the available options, you can add your own custom code here.</li>';

      return $output;
  }
}

function udcountdown_menu() {

  $items = array();

  $items['admin/settings/udcountdown'] = array(
    'title' => 'UD Countdown Settings',
    'description' => 'Set the various options for the Ubuntu Release Countdown module.',
    'weight' => '0',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('udcountdown_admin_settings'),
    'access arguments' => array('administer udcountdown'),
    'type' => MENU_NORMAL_ITEM,
   );

  $items['admin/settings/udcountdown/settings'] = array(
    'title' => 'Standard',
    'description' => 'Set the various options for the Ubuntu Release Countdown module.',
    'weight' => '0',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('udcountdown_admin_settings'),
    'access arguments' => array('administer udcountdown'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
   );

  return $items;
}

function udcountdown_admin_settings() {

  // Standard Settings
  $form['standard'] = array(
    '#type' => 'fieldset',
    '#title' => t('Standard Settings'),
    '#tree' => FALSE,
  );
  $form['standard']['udcountdown_option'] = array(
    '#type' => 'radios',
    '#title' => t('Display Option'),
    '#default_value' =>  variable_get('udcountdown_option', 1),
    '#options' => udcountdown_list_options(),
    '#description' => t('Which options from the <a href="http://www.ubuntu.com/getubuntu/countdown" target="_blank">coundown collection</a> to use.'),
    '#required' => TRUE,
  );
  $form['standard']['udcountdown_custom'] = array(
    '#type' => 'textarea',
    '#title' => t('Custom Code'),
    '#default_value' => variable_get('udcountdown_custom', ''),
    '#description' => t("Use this area to create your custom block of code."),
    '#required' => FALSE,
  );

  return system_settings_form($form);
}

/**
 * Build list of available options
 */

function udcountdown_list_options() {
  $opts = array();

  $opt = t('Custom Code');
  $opts[] = $opt;

  $handle = fopen(file_directory_path() . '/countdown/data/desc.csv', 'r');
  while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $opt = t('Option ' . $data[0] . ' - ' . $data[1]);
    $opts[] = $opt;
  }

  return $opts;
}

/*
 * Implimentation of hook_cron
 */

function udcountdown_cron() {
  $cache = file_directory_path() . "/countdown/";
  $pointer = 'https://launchpad.net/ubuntu-drupal-countdown/banners/karmic';

  udcountdown_checkver($cache, $pointer);
}

/*
 * Check if banner source changed
 */

function udcountdown_checkver($cache, $pointer) {
  $cur = curl_init();
  curl_setopt($cur, CURLOPT_URL, $pointer .  '/+download/rv');
  curl_setopt($cur, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($cur, CURLOPT_FOLLOWLOCATION, true);
  $curval = curl_exec($cur);
  curl_close($cur);
  $curval = trim($curval);

  $relver = variable_get('udcountdown_relver', false);

  if ($relver != $curval) {
    udcountdown_pullver($cache, $pointer);
    variable_set('udcountdown_relver', $curval);
  }
}

/*
 * Pull latest banner source
 */

function udcountdown_pullver($cache, $pointer) {
  shell_exec('rm -rf ' . $cache);
  shell_exec('/usr/bin/wget -nd -m -P ' . $cache . ' ' . $pointer . '/+download/banners.tgz');
  shell_exec('cd ' . $cache . '; tar zxf banners.tgz ');
}

/**
 * Implementation of hook_block
 */

function udcountdown_block($op = 'list', $delta = 0, $edit = array()) { 
  if ($op == "list") {
    $block[0]["info"] = t('Ubuntu Release Countdown');
  } elseif ($op == 'view') {
    $block_content = '<div id="udcountdown" class="udcountdown">';
    $block_content .= udcountdown_content();
    $block_content .= '</div>';

    $block['subject'] = 'Ubuntu Release Countdown';
    $block['content'] = $block_content;
  }
  return $block;
}

/**
 * Create the block content
 */

function udcountdown_content() {
  $cache = file_directory_path() . '/countdown/';

  // Grab the specified data
  $opt = variable_get('udcountdown_option', 1);
  if (0 == $opt) {
    $rcc = variable_get('udcountdown_custom', '');
  } elseif (is_numeric($opt)) {
    $file = $cache . $opt;
    $rcc = file_get_contents($file);
  } else {
    $rcc = variable_get('udcountdown_custom', '');
  }

  // Handle JavaScript Disabled browsers
  if (ereg('script|iframe', $rcc)) {
    $rfile = $cache . 'noscript';
    $file = fopen($rfile,'r');
    $rcc .= '<noscript>' . file_get_contents($rfile) . '</noscript>';
    fclose($file);
  }

  return $rcc;
}