/[drupal]/contributions/modules/phpfreechat/phpfreechat.module
ViewVC logotype

Contents of /contributions/modules/phpfreechat/phpfreechat.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.8 - (show annotations) (download) (as text)
Sun Dec 9 03:25:25 2007 UTC (23 months, 2 weeks ago) by owahab
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +2 -1 lines
File MIME type: text/x-php
Fixing a "call to undefined function"
1 <?php
2 // $Id: phpfreechat.module,v 1.7 2007/12/07 20:22:30 owahab Exp $
3
4 /**
5 * @file
6 * Enables the creation of phpFreeChat channels on any page on the site.
7 */
8
9 /**
10 * Implementation of hook_help().
11 */
12 function phpfreechat_help($section) {
13 include_once('phpfreechat.inc');
14 return _phpfreechat_help($section);
15 }
16
17 /**
18 * Implementation of hook_help().
19 */
20 function phpfreechat_menu($may_cache) {
21 $items = array();
22 if ($may_cache) {
23 $items[] = array(
24 'path' => 'phpfreechat/nuke',
25 'title' => t('Clear chats!'),
26 'callback' => 'phpfreechat_nuke',
27 'callback arguments' => array(arg(2)),
28 'access' => user_access('admin phpfreechat'),
29 'type' => MENU_CALLBACK);
30 $items[] = array(
31 'path' => 'admin/settings/phpfreechat',
32 'title' => t('phpfreechat settings'),
33 'description' => t('Settings to configure phpFreeChat.'),
34 'callback' => 'drupal_get_form',
35 'callback arguments' => array('phpfreechat_settings'));
36 }
37 return $items;
38 }
39
40 /**
41 * Delete logs of a chat room.
42 * @params $rid
43 * Room Id
44 */
45 function phpfreechat_nuke($rid) {
46 include_once(drupal_get_path('module', 'phpfreechat') .'/phpfreechat.inc');
47 _phpfreechat_nuke($rid);
48 }
49
50 /**
51 * Implementation of hook_perm().
52 */
53 function phpfreechat_perm() {
54 return array('talk on chat channels', 'create nodes with chatrooms', 'moderate chat channels');
55 }
56
57 function phpfreechat_nodedata($type) {
58 if (variable_get('phpfreechat_nodeapi_'.$type, 'never') == 'pernode' ||
59 variable_get('phpfreechat_nodeapi_'.$type.'_custom', '') == true) {
60 return true;
61 }
62 }
63
64 /**
65 * Functions for nodeapi integration
66 */
67 function phpfreechat_form_alter($form_id, &$form) {
68 global $user;
69 $type = (isset($form['type']) && isset($form['type']['#value'])) ? $form['type']['#value'] : NULL;
70 $node = isset($form['#node']) ? $form['#node'] : NULL;
71
72 if ($form_id == 'node_type_form' && isset($form['identity']['type'])) {
73 $form['workflow']['phpfreechat_nodeapi'] = array(
74 '#type' => 'radios',
75 '#title' => t('Show chat room'),
76 '#default_value' => variable_get('phpfreechat_nodeapi_'.$form['#node_type']->type, 'never'),
77 '#options' => array('never' => t('Never'), 'always' => t('Always'), 'pernode' => t('Per Node')),
78 '#description' => t('None: Nodes of this content type can never have a chat room.<br />' .
79 'Always: Nodes of this content type will always have a chat room.<br />' .
80 'Per Node: Nodes of this content type can choose to have a chat room if desired.'),
81 );
82 $form['workflow']['phpfreechat_nodeapi_custom'] = array(
83 '#type' => 'checkbox',
84 '#title' => t('Allow node to set title & channels'),
85 '#return_value' => 1,
86 '#default_value' => variable_get('phpfreechat_nodeapi_custom_'.$form['#node_type']->type, ''),
87 '#description' => t('If this is checked then node editors will be able to set the chat ' .
88 'title and select what channels to join.'),
89 );
90 $form['workflow']['phpfreechat_nodeapi_title'] = array(
91 '#type' => 'textfield',
92 '#title' => t('Default Chat Channel Title'),
93 '#default_value' => variable_get('phpfreechat_nodeapi_title_'.$form['#node_type']->type, ''),
94 '#size' => 70,
95 '#maxlength' => 128,
96 '#description' => t('This is the default channel title for this content type. ' .
97 'Leaving this blank will use the global or node chat ' .
98 'title, or the node title if these are also blank.'),
99 );
100 $form['workflow']['phpfreechat_nodeapi_channels'] = array(
101 '#type' => 'textfield',
102 '#title' => t('Default Chat Channels'),
103 '#default_value' => variable_get('phpfreechat_nodeapi_channels_'.$form['#node_type']->type, ''),
104 '#size' => 70,
105 '#maxlength' => 128,
106 '#description' => t('Enter the default channel or channels to be joined for this content type. ' .
107 'Separate multiple channels by commas (e.g. <em>Channel1, Channel2</em>). Leaving this ' .
108 'blank will use the global or node channel(s), or the node title if these are also blank.'),
109 );
110 }
111 elseif (isset($form['type'])) {
112 // Node edit form
113 if (phpfreechat_nodedata($type) && user_access('create nodes with chatrooms')) {
114 $form['phpfreechat'] = array(
115 '#type' => 'fieldset',
116 '#title' => t('phpFreeChat Settings'),
117 );
118 if (variable_get('phpfreechat_nodeapi_'.$form['type']['#value'], 'never') == 'pernode') {
119 $form['phpfreechat']['phpfreechat_enabled'] = array(
120 '#type' => 'checkbox',
121 '#title' => t('Show Chat Room'),
122 '#return_value' => 1,
123 '#default_value' => ($node->phpfreechat_enabled == 1 ? true : false),
124 '#description' => 'If this is selected a chat room will be displayed below the content',
125 );
126 }
127 if (variable_get('phpfreechat_nodeapi_custom_'.$form['type']['#value'], '') == true) {
128 $form['phpfreechat']['phpfreechat_title'] = array(
129 '#type' => 'textfield',
130 '#title' => t('Chat Channel Title'),
131 '#default_value' => $node->phpfreechat_title,
132 '#size' => 70,
133 '#maxlength' => 128,
134 '#description' => t('This is the chat title. You can leave this blank to use the default.'),
135 );
136 $form['phpfreechat']['phpfreechat_channels'] = array(
137 '#type' => 'textfield',
138 '#title' => t('Chat Channel'),
139 '#default_value' => $node->phpfreechat_channels,
140 '#size' => 70,
141 '#maxlength' => 128,
142 '#description' => t('Enter the channel or channels to be joined for this chat. ' .
143 'Separate multiple channels by commas (e.g. <em>Channel1, Channel2</em>). ' .
144 'You can leave this blank to use the default.'),
145 );
146 }
147 }
148 }
149 }
150
151 /**
152 * Implementation of hook_nodeapi()
153 */
154 function phpfreechat_nodeapi(&$node, $op, $teaser = NULL, $page = NULL) {
155 switch ($op) {
156 case 'view':
157 $content_chat = variable_get('phpfreechat_nodeapi_'. $node->type, 'never');
158 if (($content_chat == 'always' || ($content_chat == 'pernode' && $node->phpfreechat_enabled)) && $teaser === FALSE) {
159 $node->content['phpfreechat'] = array(
160 '#value' => phpfreechat_room($node),
161 '#weight' => 40,
162 );
163 }
164 break;
165 case 'update': // Need to delete/insert because we can't assume an existing entry for 'pernode' content type
166 if (phpfreechat_nodedata($node->type)) {
167 // Clear the cache
168 include_once(drupal_get_path('module', 'phpfreechat') .'/phpfreechat.inc');
169 _phpfreechat_clear_cache();
170 }
171 case 'insert':
172 if (phpfreechat_nodedata($node->type)) {
173 db_query('DELETE FROM {phpfreechat} WHERE nid = %d', $node->nid);
174 db_query('INSERT INTO {phpfreechat} (nid, phpfreechat_enabled, phpfreechat_title, phpfreechat_channels) VALUES (%d, %d, "%s", "%s")', $node->nid, $node->phpfreechat_enabled, $node->phpfreechat_title, $node->phpfreechat_channels);
175 }
176 break;
177 case 'delete':
178 // Always try and delete - the node could have data from before the content type was set to 'never'
179 db_query('DELETE FROM {phpfreechat} WHERE nid = %d', $node->nid);
180 break;
181 case 'load':
182 if (phpfreechat_nodedata($node->type)) {
183 return db_fetch_array(db_query('SELECT * FROM {phpfreechat} WHERE nid = %d', $node->nid));
184 }
185 break;
186 }
187 }
188
189 /**
190 * Implementation of hook_user()
191 */
192 function phpfreechat_user($op, &$edit, &$user, $category = NULL) {
193 switch ($op) {
194 case 'logout':
195 case 'login':
196 // Remove the users cookie so their nick is not auto-set for anonymous user
197 setcookie('phpfreechat', '', time() - 3600);
198 break;
199 }
200 }
201
202 /**
203 * Implementation of hook_block().
204 */
205 function phpfreechat_block($op = 'list', $delta = 0, $edit = array()) {
206 global $user, $base_url;
207 if ($op == 'list') {
208 $block[0]['info'] = t('Who is chatting');
209 $block[1]['info'] = t('Who is chatting on...');
210 $block[2]['info'] = t('Latest from...');
211 return $block;
212 }
213 else if ($op == 'configure' && $delta > 0) {
214 $form['phpfreechat_block'] = array(
215 '#type' => 'textfield',
216 '#title' => t('Channel Name'),
217 '#default_value' => variable_get('phpfreechat_block_channel_'.$delta, ''),
218 '#description' => t('Enter the channel name to use')
219 );
220 return $form;
221 }
222 else if ($op == 'save' && $delta > 0) {
223 variable_set('phpfreechat_block_channel_'.$delta, $edit['phpfreechat_block']);
224 }
225 else if ($op == 'view') {
226 if (user_access('access content')) {
227 require_once(drupal_get_path('module', 'phpfreechat') .'/phpfreechat.inc');
228 if (!_phpfreechat_check_install()) {
229 $block['content'] = _phpfreechat_not_found();
230 return $block;
231 }
232 $params = phpfreechat_load_params();
233 require_once 'phpfreechat/src/pfcinfo.class.php';
234 $info = new pfcInfo(md5($base_url), $params['data_private_path']);
235
236 if ($delta == 0) {
237 $users = $info->getOnlineNick(NULL);
238 $block['content'] .= theme_item_list($users);
239 $block['subject'] = t('who is chatting');
240 }
241 if ($delta == 1) {
242 $channel = variable_get('phpfreechat_block_channel_1', '');
243 $users = $info->getOnlineNick($channel);
244 $block['content'] .= theme_item_list($users);
245 $block['subject'] = t('who is chatting on ').$channel;
246 }
247 if ($delta == 2) {
248 $channel = variable_get('phpfreechat_block_channel_2', '');
249 $lastmsg_raw = $info->getLastMsg($channel, 10);
250 $output = '';
251 foreach($lastmsg_raw["data"] as $m) {
252 $output .= $m["sender"].': ';
253 $output .= '<em>' . $m["param"].'</em><br />';
254 $date = $m["date"];
255 $time = $m["time"];
256 }
257 $output .= t('Last chat: ');
258 // Convert to US style date
259 $date = preg_replace("/^\s*([0-9]{1,2})[\/\. -]+([0-9]{1,2})[\/\. -]+([0-9]{1,4})/", "\\2/\\1/\\3", $date);
260 $output .= format_interval(time() - strtotime($date.' '.$time));
261 $output .= t(' ago');
262 $block['content'] .= $output;
263 $block['subject'] = t('latest from ').$channel;
264 }
265 return $block;
266 }
267 }
268 }
269
270 /**
271 * Implementation of hook_settings().
272 */
273 function phpfreechat_settings() {
274 require_once(drupal_get_path('module', 'phpfreechat') .'/phpfreechat.inc');
275 return system_settings_form(_phpfreechat_settings());
276 }
277
278 /**
279 * Returns an array of all (non-node & non-user specific) parameters
280 */
281 function phpfreechat_load_params() {
282 global $base_url;
283 $params = array();
284
285 require_once(drupal_get_path('module', 'phpfreechat') .'/phpfreechat.inc');
286 // Configure file paths
287 $params['data_private_path'] = file_check_location(PHPFREECHAT_PRIVATE_DIR);
288 $params['data_public_url'] = $base_url . '/'. PHPFREECHAT_PUBLIC_DIR;
289 $params['data_public_path'] = file_check_location(PHPFREECHAT_PUBLIC_DIR);
290 $params['theme_url'] = $base_url .'/'. drupal_get_path('module', 'phpfreechat') . '/extras/themes';
291 $params['theme_path'] = file_check_location(drupal_get_path('module', 'phpfreechat') . '/extras/themes');
292 $params['theme'] = 'drupal';
293 $params['serverid'] = md5($base_url); // Calculate a unique id for this chat
294 $params['server_script_url'] = $base_url .'/'. drupal_get_path('module', 'phpfreechat') .'/handler.php';
295 $params['server_script_path'] = file_check_location(drupal_get_path('module', 'phpfreechat') .'/handler.php');
296
297 // Load global settings
298 $settings = array('serverid','title','channels','frozen_nick','max_nick_len',
299 'max_text_len','refresh_delay','timeout','max_msg','connect_at_startup','start_minimized',
300 'height','width','shownotice','nickmarker','clock','openlinknewwindow','showwhosonline',
301 'showsmileys','btn_sh_whosonline','btn_sh_smileys','cmd_path','theme_path','theme','language',
302 'output_encoding','container_type','server_script_path','server_script_url','client_script_path',
303 'client_script_url','jspath','xajaxpath','data_private_path','data_public_path',
304 'data_public_url','debug','prefix');
305
306 foreach ($settings as $setting) {
307 $value = variable_get('phpfreechat_'.$setting, '');
308 if (!empty($value)) {
309 if ($value == 'true') $params[$setting] = true;
310 else if ($value == 'false') $params[$setting] = false;
311 else $params[$setting] = $value;
312 }
313 }
314 return $params;
315 }
316
317 /**
318 * Tunes parameters according to specific node's details.
319 *
320 * @param $params
321 * Default parameters which might be overriden by the function
322 * @param $node
323 * The node which will carry the chat instance
324 * @param $target
325 * The user loading the chat
326 *
327 */
328 function phpfreechat_prepare_params(&$params, &$node, &$target) {
329 global $user, $base_url;
330 // Set the serverid to the node ID
331 $params['serverid'] = md5('node'. $node->nid);
332
333 $params['title'] = t('!title', array('!title' => $node->title)); // This will be overriden by any global, content-type, or node settings below
334 $params['channels'] = array(t('!title', array('!title' => $node->title))); // This will be overriden by any global, content-type, or node settings below
335
336 // Setup nick
337 if (variable_get('phpfreechat_auto_nick', '') != 'false') {
338 if ($target->uid == 0) {
339 $params['nick'] = t('Anonymous') . rand(1, 1000); // Autonumber
340 }
341 else {
342 $params['nick'] = $target->name; // setup the intitial nickname
343 }
344 }
345 else {
346 $params['nick'] = ''; // Ask
347 }
348
349 // Overrides from content type
350 $content_title = variable_get('phpfreechat_nodeapi_'.$node->type.'_title', '');
351 if (!empty($content_title)) $params['title'] = $content_title;
352 $content_channels = variable_get('phpfreechat_nodeapi_'.$node->type.'_channels', '');
353 if (!empty($content_channels)) $params['channels'] = explode(',', $content_channels);
354
355 // Overrides from node
356 if (!empty($node->phpfreechat_title)) $params['title'] = $node->phpfreechat_title;
357 if (!empty($node->phpfreechat_channels)) $params['channels'] = explode(',', $node->phpfreechat_channels);
358
359 // Setup admins
360 if (user_access('moderate chat channels') || $node->uid == $target->uid) {
361 $params['isadmin'] = true;
362 }
363
364 // Add useful info to appear in user's menu
365 // only if the selected theme is the shipped one
366 if ($params['theme_path'] == file_check_location(drupal_get_path('module', 'phpfreechat') . '/extras/themes') && $params['theme'] == 'drupal') {
367 $params['nickmeta'] = array(
368 'drupal_base_url' => $base_url,
369 'drupal_user_uid' => $target->uid,
370 );
371 }
372
373 return $params;
374 }
375
376 /**
377 * Format a chat room for a nodeapi insert
378 *
379 * @param node
380 * The node which needs a chat room inserted
381 */
382 function phpfreechat_room($node) {
383 global $user, $base_url;
384 static $chat;
385 if (user_access('talk on chat channels')) {
386 require_once(drupal_get_path('module', 'phpfreechat') .'/phpfreechat.inc');
387 if (_phpfreechat_check_install() && _phpfreechat_check_files()) {
388 // now build the room HTML content
389 $output = '<div class="phpfreechat phpfreechat-'.$node->type.'">';
390 // Load global params
391 $params = phpfreechat_load_params();
392 // Prepare params for current node
393 $params = phpfreechat_prepare_params($params, $node, $user);
394 // We need to put user ID in session to be able to retrieve it from
395 // handler
396 if (is_numeric($user->uid)) {
397 $_SESSION['drupal_user'] = $user;
398 }
399 // Start/resume chat session
400 require_once($params['server_script_path']);
401
402 if (variable_get('phpfreechat_params', '') == 'true') {
403 // Handy for debugging
404 $output .= '<pre>Parameters: ' . print_r($params, TRUE) . '</pre>'; // Handy for debugging
405 $output .= '<pre>Session: ' . print_r($_SESSION, TRUE) . '</pre>';
406 $output .= '<pre>Cookies: ' . print_r($_COOKIE, TRUE) . '</pre>';
407 }
408
409 // This line outputs the actual chat
410 $output .= $chat->printChat(TRUE);
411
412 if (user_access('admin phpfreechat')) {
413 $output .= l('Clear chat log for '.$node->title, 'phpfreechat/nuke/'.strtolower(str_replace(' ', '', $node->title)));
414 }
415 $output .= '</div>';
416 }
417 else {
418 $output .= _phpfreechat_not_found();
419 }
420 }
421 else if ($user->uid == 0) {
422 $output .= t('Please !login to chat', array('!login' => l('login', 'user/login')));
423 }
424 return $output;
425 }
426
427 /**
428 * phpFreeChat APIs.
429 *
430 * @param $action
431 * The action to be taken, this might have one of these values:
432 * kick, notify, error and redirect
433 * @param $param
434 * Extra parameter for actions like kick to be shown to the user
435 * @param $target
436 * User to take actions on
437 * @param $nid
438 * An ID of a node to apply action on
439 *
440 */
441 function phpfreechat_api($action, $param, $target, $nid = NULL) {
442 global $user;
443 // get chat parameters
444 $params = phpfreechat_load_params();
445 // get few files from phpfreechat required to resume a chat session
446 require_once drupal_get_path('module', 'phpfreechat') .'/phpfreechat/src/pfcglobalconfig.class.php';
447 require_once drupal_get_path('module', 'phpfreechat') .'/phpfreechat/src/pfcuserconfig.class.php';
448 require_once drupal_get_path('module', 'phpfreechat') .'/phpfreechat/src/pfccommand.class.php';
449
450 if (!is_null($nid)) {
451 $query = db_query('SELECT nid FROM {phpfreechat} WHERE nid = %d', $nid);
452 }
453 else {
454 // Get all chat rooms:
455 $query = db_query('SELECT nid FROM {phpfreechat}');
456 }
457 while ($room = db_fetch_object($query)) {
458 $c = $ct = $u = null;
459 $node = node_load($room->nid);
460 $params = phpfreechat_prepare_params($params, $node, $target);
461 $c =& pfcGlobalConfig::Instance($params);
462 $ct =& pfcContainer::Instance();
463 $u =& pfcUserConfig::Instance();
464
465 // since we might have more than a channel per room:
466 foreach ($u->channels as $channel) {
467 // kicking a user just add a command to play to the aimed user metadata.
468 $nick = $ct->getNickId($target->name);
469 switch ($action) {
470 case 'kick':
471 // first notify all rooms that user was kicked:
472 phpfreechat_api('notice', t('!target was kicked by !source, reason: !reason.', array('!target' => $target->name, '!source' => $user->name, '!reason' => $param)));
473 $cmdstr = 'leave';
474 $cmdp = array();
475 $cmdp['params'][] = 'ch';
476 $cmdp['params'][] = $channel['name']; // channel name
477 $cmdp['params'][] = $param; // reason
478 pfcCommand::AppendCmdToPlay($nick, $cmdstr, $cmdp);
479 break;
480 case 'notify':
481 $cmdstr = 'notice';
482 $cmdp = array();
483 // notice level: 1, 2, 3
484 $cmdp['flag'] = 1;
485 $cmdp['param'] = $param; // reason
486 pfcCommand::AppendCmdToPlay($nick, $cmdstr, $cmdp);
487 break;
488 case 'error':
489 $cmdstr = 'error';
490 $cmdp = array();
491 $cmdp['flag'] = 1;
492 $cmdp['param'] = $param;
493 pfcCommand::AppendCmdToPlay($nick, $cmdstr, $cmdp);
494 break;
495 case 'redirect':
496 $cmdstr = 'redirect';
497 $cmdp = array();
498 $cmdp['param'] = $param;
499 pfcCommand::AppendCmdToPlay($nick, $cmdstr, $cmdp);
500 break;
501 }
502 }
503 }
504 }
505 ?>

  ViewVC Help
Powered by ViewVC 1.1.2