summaryrefslogtreecommitdiffstats
path: root/fb_actions.module
blob: 20d452d992e5c5c3f5991d3d523212f40cfd7736 (plain)
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
  /**
   * @file
   * 
   * Actions defined here interact with Facebook's API.  This makes it
   * possible to notify facebook of various activities as they happen.
   */


function fb_actions_action_info() {
  return array('fb_actions_profile_fbml' =>
               array('type' => 'fb_action',
                     'description' => t('Write to Facebook profile'),
                     'configurable' => TRUE,
                     'hooks' => array('nodeapi' => array('delete', 'insert', 'update', 'view'),
                                      'comment' => array('delete', 'insert', 'update', 'view'),
                                      'user' => array('insert', 'update', 'delete', 'login', 'logout'),
                                      'cron' => array('run'),
                     ),
               ),               
               'fb_actions_ref_fbml' =>
               array('type' => 'fb_action',
                     'description' => t('Write to Facebook reference FBML'),
                     'configurable' => TRUE,
                     'hooks' => array('nodeapi' => array('delete', 'insert', 'update', 'view'),
                                      'comment' => array('delete', 'insert', 'update', 'view'),
                                      'user' => array('insert', 'update', 'delete', 'login', 'logout'),
                                      'cron' => array('run'),
                     ),
               ),
               'fb_actions_cron_per_user' =>
               array('type' => 'fb_app',
                     'description' => t('Perform an Action once for each user of a Facebook App'),
                     'configurable' => TRUE,
                     'hooks' => array('cron' => array('run')),
               ),
  );
  
  }




/**
 * Implementation of an Action.  See Actions module.
 * 
 * This action updates a user's profile FBML.
 */
function fb_actions_profile_fbml_form($values) {
  $options = fb_get_app_options(TRUE);
  $form['description'] = array('#value' => t('This action will update the current user\'s Facebook profile.  Suitable for canvas pages and cron jobs (if infinite session is configured).'));
  $form['fb_app_nid'] = 
    array('#type' => 'select',
          '#title' => t('Application'),
          '#default_value' => $values['fb_app_nid'],
          '#options' => $options,
          '#description' => t('Log into Facebook as which application?  %current is OK when invoked from canvas pages or cron jobs.',
                              array('%current' => $options[FB_APP_CURRENT])),
    );
  
  $form['body'] = array('#type' => 'textarea',
                        '#title' => t('Profile Box FBML'),
                        '#default_value' => $values['body'],
                        '#description' => t('FBML to write to user\'s profile box.  Make sure to choose an input filter that allows FBML!<br />The following token will be replaced: !token_help',
                                            array('!token_help' => theme('token_help', 'fb_app'))),
  );
  $form['body_filter'] = filter_form($values['body_filter'],
                                     NULL,
                                     array('body_filter'));

  $form['profile_main'] = array('#type' => 'textarea',
                                '#title' => t('Main Profile Box'),
                                '#default_value' => $values['profile_main'],
                                '#description' => t('FBML for the <a href=!url>narrow profile box</a> on the Wall and Info tabs page.',
                                                    array('!url' => 'http://wiki.developers.facebook.com/index.php/New_Design_Narrow_Boxes')),
  );
  $form['mobile_profile'] = array('#type' => 'textarea',
                                  '#title' => t('Mobile Profile Box'),
                                  '#default_value' => $values['mobule_profile'],
                                  '#description' => t('FBML intended for mobile devices.'),
  );
  
  return $form;
}

function fb_actions_profile_fbml_validate($form_id, $values) {
  // TODO
}

function fb_actions_profile_fbml_submit($form_id, $values) {
  return array('fb_app_nid' => $values['fb_app_nid'],
               'body' => $values['body'],
               'body_filter' => $values['body_filter'],
               'profile_main' => $values['profile_main'],
               'mobile_profile' => $values['mobile_profile'],
  );
}

function fb_actions_profile_fbml(&$object, $values) {
  //dpm(func_get_args(), "fb_actions_profile_fbml");

  if ($values['hook'] == 'nodeapi') {
    $node = $values['node'];
  }
  else if ($values['hook'] == 'comment') {
    $comment = $values['comment'];
    $node = node_load($comment->nid);
  }
  else if ($values['hook'] == 'user') {
    $account = $values['user']; // untested
  }
  
  // Determine which app to use
  if ($object) {
    $fb_app = $object;
  }
  else if ($values['fb_app_nid'] && is_numeric($values['fb_app_nid'])) {
    $fb_app = fb_get_app(array('nid' => $values['fb_app_nid']));
  }
  else if ($values['fb_app_nid'] == FB_APP_CURRENT) {
    $fb_app = $GLOBALS['fb_app'];
  }
  
  // Use already configured $fb if its passed in (cron jobs)
  $fb = $values['fb'];
  if (!$fb && $fb_app)
    // Otherwise, login to facebook as either the current user or the infinite session
    $fb = fb_api_init($fb_app, FB_FBU_ANY);
  
  if ($fb) {
    global $user;
    $fbu = fb_get_fbu($user->uid, $fb_app);
    // Replace fb_app related tokens
    $body = token_replace($values['body'], 'fb_app', $fb_app);
    $body = check_markup($body, $values['body_filter'], FALSE);
    if ($profile_main)
      $profile_main = check_markup($profile_main, $values['body_filter'], FALSE);
    if ($mobile_profile)
      $mobile_profile = check_markup($mobile_profile, $values['body_filter'], FALSE);
    
    // Links in profile FBML must be fully-qualified.  Here we attempt to
    // replace relative links with fully qualified ones.
    global $base_url, $base_path;
    $patterns[] = '/"'.str_replace('/','\/', $base_path).'([^"]*)"/';
    $replacements[] = '"'.$base_url.'/$1"';
    $body = preg_replace($patterns, $replacements, $body);
    if ($profile_main)
      $profile_main = preg_replace($patterns, $replacements, $profile_main);
    if ($mobile_profile)
      $mobile_profile = preg_replace($patterns, $replacements, $mobile_profile);
    
    // Rewrite links to reference canvas pages
    if ($fb_app->canvas && function_exists('fb_canvas_process_fbml')) {
      $body = fb_canvas_process_fbml($body, $fb_app);
      if ($profile_main)
        $profile_main = fb_canvas_process_fbml($profile_main, $fb_app);
      if ($mobile_profile)
        $mobile_profile = fb_canvas_process_fbml($mobile_profile, $fb_app);
    }
    
    if (variable_get('fb_actions_verbose', FALSE))
      watchdog('fb_actions', t('Setting Facebook profile markup for !user to !body',
                               array('!user' => theme('username', $user),
                                     '!body' => htmlentities($body))));
    try {
      // http://wiki.developers.facebook.com/index.php/Profile.setFBML
      $fb->api_client->profile_setFBML(NULL, $fbu, $body, NULL, $mobile_profile, $profile_main);
    } catch (Exception $e) {
      fb_log_exception($e, t('Failed to set profile FBML'), $fb);
    }
  }
}

/**
 * Implementation of an Action.
 * 
 * This action updates reference FBML.
 * http://developer.facebook.com/documentation.php?v=1.0&method=fbml.setRefHandle
 */
function fb_actions_ref_fbml_form($values) {
  $form['description'] = array('#value' => t('This action will update reference FBML.  See Facebook\'s documentation of the &lt;fb:ref&gt; tag.  Suitable for canvas pages and cron jobs (if infinite session is configured).'));
  $options = fb_get_app_options(TRUE);
  $form['fb_app_nid'] = 
    array('#type' => 'select',
          '#title' => t('Application'),
          '#default_value' => $values['fb_app_nid'],
          '#options' => $options,
          '#description' => t('Log into Facebook as which application?  %current is OK when invoked from canvas pages or cron jobs.',
                              array('%current' => $options[FB_APP_CURRENT])),
	  );
  $form['handle'] = array('#type' => 'textfield',
                          '#title' => t('Handle'),
                          '#default_value' => $values['handle'],
  );
  $form['body'] = array('#type' => 'textarea',
                        '#title' => t('Reference FBML'),
                        '#default_value' => $values['body'],
                        '#description' => t('FBML to write to user\'s profile.<br />The following token will be replaced: !token_help',
                                            array('!token_help' => theme('token_help', 'fb_app'))),
  );
  $form['body_filter'] = filter_form($values['body_filter'],
                                     NULL,
                                     array('body_filter'));
  return $form;
}
function fb_actions_ref_fbml_validate($form_id, $values) {
  // TODO
}
function fb_actions_ref_fbml_submit($form_id, $values) {
  return array('fb_app_nid' => $values['fb_app_nid'],
               'handle' => $values['handle'],
               'body' => $values['body'],
               'body_filter' => $values['body_filter'],
  );
}
function fb_actions_ref_fbml(&$object, $values) {
  if ($values['hook'] == 'nodeapi') {
    $node = &$object;
  }
  else if ($values['hook'] == 'comment') {
    $node = node_load($object['nid']);
    $comment = &$object;
  }
  else if ($values['hook'] == 'user') {
    $account = &$object;
  }
  
  // Log into facebook as the current user.
  if ($fb_app = $GLOBALS['fb_app'])
    if ($values['fb_app_nid'] == FB_APP_CURRENT || 
        $fb_app->nid == $values['fb_app_nid'])
      // We're in a canvas page for the desired app.  We're already logged in.
      $fb = $GLOBALS['fb'];
  if (!$fb && $values['fb_app_nid'] != FB_APP_CURRENT) {
    // Need to log into this app.
    $fb_app = fb_get_app(array('nid' => $values['fb_app_nid']));
    $fb = fb_api_init($fb_app, FB_FBU_INFINITE_SESSION);
  }
  
  // This will be set on canvas pages and Facebook App cron actions.
  if ($fb && $fb_app) {
    // Replace fb_app related tokens
    $body = token_replace($values['body'], 'fb_app', $fb_app);

    $body = check_markup($body, $values['body_filter'], FALSE);
    if ($fb_app->canvas && function_exists('fb_canvas_process_fbml')) {
      $body = fb_canvas_process_fbml($body, $fb_app);
    }
    if (variable_get('fb_actions_verbose', FALSE))
      watchdog('fb_actions', t('Setting Facebook ref FBML for %handle to %body',
                               array('%handle' => $values['handle'],
                                     '%body' => htmlentities($body))));
    $fb->api_client->fbml_setRefHandle($values['handle'], $body);
    fb_report_errors($fb);
  }
}


function fb_actions_cron_per_user_form($values) {
  $form['description'] = array('#value' => t('This action will iterate through user\'s of a Facebook Application, attempt to log into the Facebook API as that user (or failing that, the infinite session user), and execute additional actions.  Use this to perform per-user actions during a Facebook cron job.'));
  
  $options = fb_get_app_options(FALSE);
  $form['fb_app_nids'] = 
    array('#type' => 'select',
          '#title' => t('Application'),
          '#multiple' => TRUE,
          '#required' => TRUE,
          '#default_value' => $values['fb_app_nids'],
          '#options' => $options,
          '#description' => t('Perform these actions for each user of which applications?'),
    );
  
  foreach (actions_get_all_actions() as $aid => $action) {
    $options[$action['type']][$aid] = $action['description'];
  }
  $form['actions'] = array('#type' => 'select',
                           '#title' => t('Actions'),
                           '#default_value' => $values['actions'],
                           '#multiple' => TRUE,
                           '#required' => TRUE,
                           '#options' => $options,
                           '#description' => t('Select one or more actions to perform while logged in as a Facebook Application user.'),
  );
  $form['throttle'] = array('#type' => 'textfield',
                            '#title' => t('Throttle'),
                            '#default_value' => $values['throttle'],
                            '#required' => TRUE,
                            '#description' => t('Number of users to iterate through each time this action is invoked.  Recommended: start with a small number and increase when you are sure things are working.'),
  );
  return $form;
}

function fb_actions_cron_per_user_submit($form_id, $values) {
  return array('fb_app_nids' => $values['fb_app_nids'],
               'actions' => $values['actions'],
               'throttle' => $values['throttle'],
  );
}

/**
 * Trigger an action several times, emulating a different user each
 * time.  Useful for cron jobs in which we update each users profile
 * box, for example.
 */
function fb_actions_cron_per_user($obj, $values) {
  //$args = func_get_args();
  //watchdog('fb_action_debug', "fb_actions_cron_per_user" . dpr($args, 1), WATCHDOG_ERROR);

  foreach ($values['fb_app_nids'] as $nid) {
    $fb_app = fb_get_app(array('nid' => $nid));
    // Set paths as if on a canvas page.

    // perform per-user actions by manipulating the global user variable.
    $save_session = session_save_session();
    session_save_session(FALSE);
    // Save current settings
    $before_fb = $GLOBALS['fb'];
    $before_fb_app = $GLOBALS['fb_app'];
    $before_user = $GLOBALS['user'];
    
    // Find some users of the app, for whom cron has not run recently.
    $result = db_query("SELECT * FROM {fb_user_app} WHERE apikey='%s' AND fbu > 0 AND added > 0 ORDER BY time_cron ASC LIMIT %d",
                       $fb_app->apikey,
                       $values['throttle']);
    while ($data = db_fetch_object($result)) {
      // Find a local account for the application user
      $account = fb_user_get_local_user($data->fbu, $fb_app);
      if (variable_get('fb_actions_verbose', FALSE))
        watchdog('fb_action_debug', "fb_actions_cron_per_user fbu is $data->fbu, local user is " . theme('username', $account), WATCHDOG_ERROR);
      
      if (!$account || !$account->uid) {
        watchdog('fb cron', t('Facebook user %fbu does not correspond to a local account.',
                              array('%fbu' => $data->fbu)));
        //db_query("DELETE FROM {fb_user_app} WHERE apikey='%s' AND fbu = %d",
        //         $fb_app->apikey,
        //         $data->fbu);
      }
      else {
        // If here, local user has been found.
        // The older facebook API required us to log in as the current user.
	// In the new API we should not need a user account to perform tasks appropriate for cron jobs.
        $fb = fb_api_init($fb_app, FB_FBU_NO_SESSION);
	if ($fb) {
          // Set things up as if this were a canvas page.
          $GLOBALS['user'] = $account;
          $GLOBALS['fb'] = $fb;
          $GLOBALS['fb_app'] = $fb_app;
          
          // Invoke any actions that we've been configured to invoke.
          try {
            actions_do($values['actions'], $fb_app, 
		       array('fb' => $fb,
			     'fb_app' => $fb_app));
          } catch (Exception $e) {
            watchdog('fb cron', "Action per user failed. " . $e->getMessage(), WATCHDOG_ERROR);
          }

        } // end if able to log into facebook
      } // end if local user found.
      
      // Record how recently cron was run for this user.  We do this even if
      // we failed to log in, because we don't want persistent problems to
      // clog the cron queue.  We'll get to this user again, eventually.
      db_query("UPDATE {fb_user_app} SET time_cron=%d WHERE apikey='%s' AND fbu=%d",
               time(), $fb_app->apikey, $data->fbu);
      
    } // end loop per user
    
    // Restore global variables
    $GLOBALS['user'] = $before_user;
    $GLOBALS['fb'] = $before_fb;
    $GLOBALS['fb_app'] = $before_fb_app;
    session_save_session($save_session);
    
  }
}


?>