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

Contents of /contributions/modules/skype_status/skype_status.module

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


Revision 1.5 - (show annotations) (download) (as text)
Tue Mar 3 02:18:01 2009 UTC (8 months, 3 weeks ago) by arto
Branch: MAIN
CVS Tags: DRUPAL-6--1-0-BETA1, HEAD
Changes since 1.4: +8 -8 lines
File MIME type: text/x-php
Always explicitly limit RDF API operations by repository.
1 <?php
2 // $Id$
3
4 //////////////////////////////////////////////////////////////////////////////
5 // Module settings
6
7 define('SKYPE_STATUS_URL', 'http://mystatus.skype.com/%s.xml');
8 define('SKYPE_STATUS_UNKNOWN', 0);
9 define('SKYPE_STATUS_PROFILE_FIELD', variable_get('skype_status_profile_field', ''));
10 define('SKYPE_STATUS_CACHE_LIFETIME', variable_get('skype_status_cache_lifetime', 60));
11
12 //////////////////////////////////////////////////////////////////////////////
13 // Core API hooks
14
15 /**
16 * Implementation of hook_menu().
17 */
18 function skype_status_menu() {
19 return array(
20 // Administer >> Site configuration >> Skype status
21 'admin/settings/skype_status' => array(
22 'title' => 'Skype status',
23 'access arguments' => array('administer site configuration'),
24 'page callback' => 'drupal_get_form',
25 'page arguments' => array('skype_status_admin_settings'),
26 'file' => 'skype_status.admin.inc',
27 ),
28 );
29 }
30
31 /**
32 * Implementation of hook_hook_info()
33 */
34 function skype_status_hook_info() {
35 return array(
36 'skype_status' => array(
37 'skype_status' => array(
38 'update' => array(
39 'runs when' => t('When a user\'s Skype presence status changes'),
40 ),
41 ),
42 ),
43 );
44 }
45
46 /**
47 * Implementation of hook_profile_alter().
48 */
49 function skype_status_profile_alter(&$account) {
50 $field = SKYPE_STATUS_PROFILE_FIELD;
51 if (!empty($account->$field)) {
52 $language = !empty($account->language) ? $account->language : 'en'; // FIXME
53 $status = skype_status_get_code($account) != SKYPE_STATUS_UNKNOWN ? skype_status_get($account, $language) : '';
54 $output = theme('skype_status', $account->$field, $status);
55 $account->content['Contact details'][$field]['#value'] = $output;
56 }
57 }
58
59 /**
60 * Implementation of hook_user().
61 */
62 function skype_status_user($op, &$edit, &$account, $category = NULL) {
63 switch ($op) {
64 case 'delete':
65 $field = SKYPE_STATUS_PROFILE_FIELD;
66 if (!empty($account->$field)) {
67 $url = sprintf(SKYPE_STATUS_URL, $account->$field);
68 rdf_delete(NULL, NULL, NULL, array('repository' => 'skype_status', 'context' => $url));
69 rdf_delete($url, dcterms::valid, NULL, array('repository' => 'skype_status'));
70 }
71 break;
72 }
73 }
74
75 /**
76 * Implementation of hook_user_operations().
77 */
78 function skype_status_user_operations() {
79 return array(
80 'skype_status' => array(
81 'label' => t('Update the Skype status of the selected users'),
82 'callback' => 'skype_status_operations_update',
83 ),
84 );
85 }
86
87 /**
88 * Implementation of hook_theme()
89 */
90 function skype_status_theme() {
91 return array(
92 'skype_status' => array(
93 'arguments' => array('name' => NULL, 'status' => NULL),
94 ),
95 );
96 }
97
98 //////////////////////////////////////////////////////////////////////////////
99 // Operations callbacks
100
101 function skype_status_operations_update($accounts) {
102 foreach ($accounts as $uid) {
103 if (($account = user_load(array('uid' => (int)$uid))) !== FALSE) {
104 skype_status_update($account);
105 }
106 }
107 }
108
109 //////////////////////////////////////////////////////////////////////////////
110 // Theming callbacks
111
112 function theme_skype_status($name, $status = '') {
113 return $status ?
114 t('<a href="callto://@name">@name</a> (%status)', array('@name' => $name, '%status' => $status)) :
115 t('<a href="callto://@name">@name</a>', array('@name' => $name));
116 }
117
118 //////////////////////////////////////////////////////////////////////////////
119 // Skype Status API
120
121 function skype_status_clear_cache() {
122 rdf_delete(NULL, NULL, NULL, array('repository' => 'skype_status'));
123 drupal_set_message(t('Cached status information cleared for all users.'));
124 }
125
126 function skype_status_get_code($account) {
127 return (int)skype_status_get($account, 'num');
128 }
129
130 function skype_status_get($account, $language = 'en', $update = TRUE) {
131 $field = SKYPE_STATUS_PROFILE_FIELD;
132 if (!empty($account->$field)) {
133 $url = sprintf(SKYPE_STATUS_URL, $account->$field);
134
135 if ($update && skype_status_has_expired($url)) {
136 skype_status_update($account);
137 }
138
139 $data = rdf_query('urn:skype:skype.com:skypeweb/1.1', 'http://www.skype.com/go/skypewebpresence', NULL, array('context' => $url, 'language' => $language, 'repository' => 'skype_status'));
140 $data = rdf_normalize($data);
141 if (!empty($data)) {
142 foreach (rdf_select_values($data) as $literal) {
143 if (is_object($literal) && $literal->language == $language) {
144 return $literal->value;
145 }
146 }
147 }
148 }
149 }
150
151 function skype_status_has_expired($url) {
152 $data = rdf_normalize(rdf_query($url, dcterms::valid, NULL, array('repository' => 'skype_status')));
153 if (empty($data)) {
154 return TRUE;
155 }
156 foreach (rdf_select_values($data) as $literal) {
157 if (!empty($literal->value)) {
158 $age = gmmktime() - strtotime((string)$literal->value);
159 return $age > SKYPE_STATUS_CACHE_LIFETIME;
160 }
161 }
162 }
163
164 function skype_status_update($account, $log = TRUE) {
165 $field = SKYPE_STATUS_PROFILE_FIELD;
166 if (!empty($account->$field)) {
167 if ($log) {
168 timer_start('skype_status');
169 }
170
171 // Retrieve and parse the user-specific RDF/XML file containing Skype status
172 $url = sprintf(SKYPE_STATUS_URL, $account->$field);
173 $text = file_get_contents($url);
174 $data = rdf_unserialize($text, array('format' => 'rdf+xml', 'uri' => $url));
175
176 // Limit all present RDF operations to our private repository
177 rdf_use_repository('skype_status');
178
179 // Delete all existing statements from the named graph
180 rdf_delete(NULL, NULL, NULL, array('context' => $url));
181
182 // Insert each retrieved statement into the named graph
183 rdf_insert_all($data, array('context' => $url));
184
185 // Update the timestamp used for the cache lifetime calculation
186 rdf_delete($url, dcterms::valid, NULL);
187 rdf_insert($url, dcterms::valid, rdf_datetime());
188
189 // Unlimit future RDF operations
190 rdf_use_repository();
191
192 module_invoke_all('skype_status', 'update', $account);
193
194 if ($log) {
195 list(, $time) = array_values(timer_stop('skype_status'));
196 watchdog('skype status', 'Updated Skype status: %name (stored @triples statements, processing time @time).', array('%name' => $account->name, '@triples' => count($data), '@time' => format_plural(round($time / 1000.0, 2), '1 second', '@count seconds')), WATCHDOG_NOTICE, l(t('view'), 'user/' . $account->uid));
197 }
198 return TRUE;
199 }
200 }

  ViewVC Help
Powered by ViewVC 1.1.2