| 1 |
<?php
|
| 2 |
// $Id: imood.module,v 1.1 2005/09/14 08:47:31 breyten Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* Helper class to parse XML into an array structure. Taken from http://www.php.net/xml_parse.
|
| 6 |
* Slightly modified to prevent the special array member for data from clashing with XML tag names.
|
| 7 |
* and made tags case not folding
|
| 8 |
*/
|
| 9 |
class _imoodXML2Array {
|
| 10 |
|
| 11 |
var $stack=array();
|
| 12 |
var $stack_ref;
|
| 13 |
var $arrOutput = array();
|
| 14 |
var $resParser;
|
| 15 |
var $strXmlData;
|
| 16 |
|
| 17 |
function push_pos(&$pos) {
|
| 18 |
$this->stack[count($this->stack)]=&$pos;
|
| 19 |
$this->stack_ref=&$pos;
|
| 20 |
}
|
| 21 |
|
| 22 |
function pop_pos() {
|
| 23 |
unset($this->stack[count($this->stack)-1]);
|
| 24 |
$this->stack_ref=&$this->stack[count($this->stack)-1];
|
| 25 |
}
|
| 26 |
|
| 27 |
function parse($strInputXML) {
|
| 28 |
if (function_exists('drupal_xml_parser_create')) {
|
| 29 |
$this->resParser = drupal_xml_parser_create($strInputXML);
|
| 30 |
}
|
| 31 |
else {
|
| 32 |
$this->resParser = xml_parser_create();
|
| 33 |
}
|
| 34 |
xml_set_object($this->resParser, $this);
|
| 35 |
xml_parser_set_option($this->resParser, XML_OPTION_CASE_FOLDING, false);
|
| 36 |
xml_set_element_handler($this->resParser, 'tagOpen', 'tagClosed');
|
| 37 |
|
| 38 |
xml_set_character_data_handler($this->resParser, 'tagData');
|
| 39 |
|
| 40 |
$this->push_pos($this->arrOutput);
|
| 41 |
|
| 42 |
$this->strXmlData = xml_parse($this->resParser,$strInputXML );
|
| 43 |
if(!$this->strXmlData) {
|
| 44 |
die(sprintf('XML error: %s at line %d',
|
| 45 |
xml_error_string(xml_get_error_code($this->resParser)),
|
| 46 |
xml_get_current_line_number($this->resParser)));
|
| 47 |
}
|
| 48 |
|
| 49 |
xml_parser_free($this->resParser);
|
| 50 |
|
| 51 |
return $this->arrOutput;
|
| 52 |
}
|
| 53 |
|
| 54 |
function tagOpen($parser, $name, $attrs) {
|
| 55 |
if (isset($this->stack_ref[$name])) {
|
| 56 |
if (!isset($this->stack_ref[$name][0])) {
|
| 57 |
$tmp=$this->stack_ref[$name];
|
| 58 |
unset($this->stack_ref[$name]);
|
| 59 |
$this->stack_ref[$name][0] = $tmp;
|
| 60 |
}
|
| 61 |
$cnt=count($this->stack_ref[$name]);
|
| 62 |
$this->stack_ref[$name][$cnt]=array();
|
| 63 |
if (isset($attrs))
|
| 64 |
$this->stack_ref[$name][$cnt] = $attrs;
|
| 65 |
$this->push_pos($this->stack_ref[$name][$cnt]);
|
| 66 |
}
|
| 67 |
else {
|
| 68 |
$this->stack_ref[$name]=array();
|
| 69 |
if (isset($attrs))
|
| 70 |
$this->stack_ref[$name] = $attrs;
|
| 71 |
$this->push_pos($this->stack_ref[$name]);
|
| 72 |
}
|
| 73 |
}
|
| 74 |
|
| 75 |
function tagData($parser, $tagData) {
|
| 76 |
if(trim($tagData) != '') {
|
| 77 |
if(isset($this->stack_ref['_data']))
|
| 78 |
$this->stack_ref['_data'] .= $tagData;
|
| 79 |
else
|
| 80 |
$this->stack_ref['_data'] = $tagData;
|
| 81 |
}
|
| 82 |
}
|
| 83 |
|
| 84 |
function tagClosed($parser, $name) {
|
| 85 |
$this->pop_pos();
|
| 86 |
}
|
| 87 |
}
|
| 88 |
|
| 89 |
function imood_help($section) {
|
| 90 |
switch ($section) {
|
| 91 |
case 'admin/modules#description':
|
| 92 |
return t('imood module.');
|
| 93 |
case 'admin/help#foaf':
|
| 94 |
return t('Make it possible to use profile fields as moods, to interface with <a href="%url">imood</a>.', array('%url' => 'http://www.imood.com/'));
|
| 95 |
}
|
| 96 |
}
|
| 97 |
|
| 98 |
function imood_settings() {
|
| 99 |
$form = array();
|
| 100 |
$form['imood_base_field'] = array(
|
| 101 |
'#type' => 'select', '#title' => t('Field for base mood'), '#default_value' => variable_get('imood_base_field', ''),
|
| 102 |
'#options' => _imood_profile_fields()
|
| 103 |
);
|
| 104 |
$form['imood_personal_field'] = array(
|
| 105 |
'#type' => 'select', '#title' => t('Field for personal mood'), '#default_value' => variable_get('imood_personal_field', ''),
|
| 106 |
'#options' => _imood_profile_fields()
|
| 107 |
);
|
| 108 |
|
| 109 |
return $form;
|
| 110 |
}
|
| 111 |
|
| 112 |
function imood_user_form($user) {
|
| 113 |
$form = array();
|
| 114 |
|
| 115 |
$form['profile_imood'] = array(
|
| 116 |
'#type' => 'fieldset', '#title' => t('imood'), '#weight' => 3
|
| 117 |
);
|
| 118 |
$form['profile_imood']['profile_imood_user'] = array(
|
| 119 |
'#type' => 'textfield', '#title' => t('imood e-mail address'), '#size' => 30, '#maxlength' => 255,
|
| 120 |
'#default_value' => $user->profile_imood_user
|
| 121 |
);
|
| 122 |
$form['profile_imood']['profile_imood_passwd'] = array(
|
| 123 |
'#type' => 'password', '#title' => t('imood password'), '#size' => 30, '#maxlength' => 255,
|
| 124 |
'#default_value' => $user->profile_imood_passwd
|
| 125 |
);
|
| 126 |
|
| 127 |
return $form;
|
| 128 |
}
|
| 129 |
|
| 130 |
function imood_user($op, &$edit, &$user, $category = NULL) {
|
| 131 |
switch ($op) {
|
| 132 |
case 'update':
|
| 133 |
if ($user->profile_imood_user != '') {
|
| 134 |
$imood_base = _imood_profile_get_name(variable_get('imood_base_field', ''));
|
| 135 |
$imood_personal = _imood_profile_get_name(variable_get('imood_personal_field', ''));
|
| 136 |
$mood = imood_query($user->profile_imood_user);
|
| 137 |
|
| 138 |
if ($mood && (($mood->base != $user->$imood_base) || ($mood->personal != $user->$imood_personal))) {
|
| 139 |
$result = imood_request('update', array(
|
| 140 |
'email' => $user->profile_imood_user,
|
| 141 |
'password' => $user->profile_imood_passwd,
|
| 142 |
'base' => $user->$imood_base,
|
| 143 |
'personal' => $user->$imood_personal,
|
| 144 |
'face' => $mood->face
|
| 145 |
));
|
| 146 |
|
| 147 |
$bundle = $result['imood']['bundle'];
|
| 148 |
$error_code = imood_is_error($bundle);
|
| 149 |
if ($error_code != 400) {
|
| 150 |
watchdog('imood', _imood_field($bundle, 'error'), WATCHDOG_WARNING);
|
| 151 |
}
|
| 152 |
}
|
| 153 |
else {
|
| 154 |
watchdog('imood', "mood for $user->profile_imood_user could not be retrieved", WATCHDOG_WARNING);
|
| 155 |
}
|
| 156 |
}
|
| 157 |
break;
|
| 158 |
case 'form':
|
| 159 |
if ($category == 'account') {
|
| 160 |
return imood_user_form($user);
|
| 161 |
}
|
| 162 |
break;
|
| 163 |
}
|
| 164 |
}
|
| 165 |
|
| 166 |
function imood_request($script, $parameters = array()) {
|
| 167 |
$imood_url = "http://xml.imood.org/$script.cgi";
|
| 168 |
|
| 169 |
if (count($parameters) > 0) {
|
| 170 |
$i = 0;
|
| 171 |
foreach($parameters as $param => $val) {
|
| 172 |
$imood_url .= ($i == 0) ? '?' : '&';
|
| 173 |
$imood_url .= "$param=". urlencode($val);
|
| 174 |
$i++;
|
| 175 |
}
|
| 176 |
}
|
| 177 |
|
| 178 |
if (function_exists('drupal_http_request')) {
|
| 179 |
$response = drupal_http_request($imood_url);
|
| 180 |
}
|
| 181 |
else {
|
| 182 |
$response = new StdClass();
|
| 183 |
$response->data = join('', file($imood_url));
|
| 184 |
$response->error = '';
|
| 185 |
}
|
| 186 |
|
| 187 |
if ($response->error) {
|
| 188 |
return FALSE;
|
| 189 |
}
|
| 190 |
|
| 191 |
$parser = new _imoodXML2Array();
|
| 192 |
$result = $parser->parse($response->data);
|
| 193 |
|
| 194 |
return $result;
|
| 195 |
}
|
| 196 |
|
| 197 |
function _imood_field($struct, $field) {
|
| 198 |
return is_array($struct[$field]) ? $struct[$field]['_data'] : $struct[$field];
|
| 199 |
}
|
| 200 |
|
| 201 |
function imood_is_error($bundle) {
|
| 202 |
if (_imood_field($bundle, 'type') == 'error') {
|
| 203 |
return _imood_field($bundle, 'code');
|
| 204 |
}
|
| 205 |
}
|
| 206 |
|
| 207 |
function imood_parse_mood($mood_info) {
|
| 208 |
$mood = new StdClass();
|
| 209 |
$mood->base = _imood_field($mood_info, 'base');
|
| 210 |
$mood->personal = _imood_field($mood_info, 'personal');
|
| 211 |
$mood->face = _imood_field($mood_info, 'face');
|
| 212 |
$mood->date = _imood_field($mood_info, 'date');
|
| 213 |
return $mood;
|
| 214 |
}
|
| 215 |
|
| 216 |
function imood_query($email) {
|
| 217 |
$result = imood_request('query', array('email' => $email));
|
| 218 |
$bundle = $result['imood']['bundle'];
|
| 219 |
|
| 220 |
if (!imood_is_error($bundle)) {
|
| 221 |
return imood_parse_mood($bundle['mood']);
|
| 222 |
}
|
| 223 |
}
|
| 224 |
|
| 225 |
function imood_moods() {
|
| 226 |
$result = imood_request('moods');
|
| 227 |
$bundle = $result['imood']['bundle'];
|
| 228 |
|
| 229 |
if ($bundle['type'] == 'moods') {
|
| 230 |
$moods = array();
|
| 231 |
|
| 232 |
foreach($bundle['mood'] as $mood) {
|
| 233 |
$moods[] = $mood['base']['_data'];
|
| 234 |
}
|
| 235 |
|
| 236 |
return $moods;
|
| 237 |
}
|
| 238 |
}
|
| 239 |
|
| 240 |
function imood_faces() {
|
| 241 |
$result = imood_request('faces');
|
| 242 |
$bundle = $result['imood']['bundle'];
|
| 243 |
|
| 244 |
if ($bundle['type'] == 'faces') {
|
| 245 |
$faces = array();
|
| 246 |
foreach($bundle['face'] as $face) {
|
| 247 |
$faces[$face['id']['_data']] = $face['link']['_data'];
|
| 248 |
}
|
| 249 |
|
| 250 |
return $faces;
|
| 251 |
}
|
| 252 |
}
|
| 253 |
|
| 254 |
function _imood_profile_get_name($fid) {
|
| 255 |
return db_result(db_query("SELECT name FROM {profile_fields} WHERE fid = %d", $fid));
|
| 256 |
}
|
| 257 |
|
| 258 |
// from the foaf module, slightly modified
|
| 259 |
function _imood_profile_fields() {
|
| 260 |
$output = array('' => t('Select an option'));
|
| 261 |
|
| 262 |
$result = db_query("SELECT fid, title, category, weight FROM {profile_fields} WHERE visibility=%d OR visibility=%d ORDER BY category, weight", PROFILE_PUBLIC, PROFILE_PUBLIC_LISTINGS);
|
| 263 |
while ($row = db_fetch_object($result)) {
|
| 264 |
$output[$row->category][$row->fid] = $row->title;
|
| 265 |
}
|
| 266 |
return $output;
|
| 267 |
}
|
| 268 |
|
| 269 |
function imood_cron() {
|
| 270 |
$imood_moods_field = variable_get('imood_base_field', '');
|
| 271 |
$imood_moods_lastupdate = variable_get('imood_moods_lastupdated', 0);
|
| 272 |
if ((($imoods_last_update + 86400) <= time()) && ($imood_moods_field != '')) {
|
| 273 |
$moods = imood_moods();
|
| 274 |
db_query("UPDATE {profile_fields} SET options = '%s' WHERE fid = %d", implode("\n", $moods), $imood_moods_field);
|
| 275 |
}
|
| 276 |
}
|