| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Drupal module: Zemanta
|
| 7 |
*
|
| 8 |
* Adds connection to Zemanta service
|
| 9 |
*/
|
| 10 |
|
| 11 |
define(ZEMANTA_RELEASE_ID_URL, 'http://static.zemanta.com/plugins/drupal/release.txt');
|
| 12 |
define(ZEMANTA_LOADER_URL, 'http://static.zemanta.com/plugins/drupal/5.x/loader.js');
|
| 13 |
define(ZEMANTA_API_URL, 'http://api.zemanta.com/services/rest/0.0/');
|
| 14 |
|
| 15 |
function zemanta_help($section) {
|
| 16 |
switch ($section) {
|
| 17 |
case 'admin/settings/zemanta':
|
| 18 |
return t("Zemanta integrates Zemanta's web service to make it easy to add contextual images, articles, links and tags to your post.");
|
| 19 |
}
|
| 20 |
}
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_menu().
|
| 24 |
*/
|
| 25 |
function zemanta_menu() {
|
| 26 |
$items = array();
|
| 27 |
|
| 28 |
$items[] = array(
|
| 29 |
'path' => 'admin/settings/zemanta',
|
| 30 |
'title' => t('Zemanta'),
|
| 31 |
'description' => t('Configure Zemanta module.'),
|
| 32 |
'callback' => 'drupal_get_form',
|
| 33 |
'callback arguments' => 'zemanta_admin',
|
| 34 |
'access' => user_access('access administration pages'),
|
| 35 |
'type' => MENU_NORMAL_ITEM,
|
| 36 |
);
|
| 37 |
|
| 38 |
$items[] = array(
|
| 39 |
'path' => 'zemanta/json-proxy',
|
| 40 |
'title' => 'Json proxy',
|
| 41 |
'callback' => 'zemanta_json_proxy',
|
| 42 |
'access' => TRUE,
|
| 43 |
'type' => MENU_CALLBACK,
|
| 44 |
);
|
| 45 |
|
| 46 |
return $items;
|
| 47 |
}
|
| 48 |
|
| 49 |
/**
|
| 50 |
* Implementation of hook_admin_settings() for configuring the module
|
| 51 |
*/
|
| 52 |
function zemanta_admin() {
|
| 53 |
zemanta_check_filter();
|
| 54 |
if (!file_exists(zemanta_loader_path())) {
|
| 55 |
zemanta_get_loader(ZEMANTA_LOADER_URL);
|
| 56 |
}
|
| 57 |
|
| 58 |
$node_types = array_map('check_plain', node_get_types('names'));
|
| 59 |
$defaults = isset($node_types['blog']) ? array('blog' => 1) : array();
|
| 60 |
$form['zemanta_node_types'] = array(
|
| 61 |
'#type' => 'checkboxes',
|
| 62 |
'#title' => t('Enable Zemanta for following content types'),
|
| 63 |
'#default_value' => variable_get('zemanta_node_types', $defaults),
|
| 64 |
'#options' => $node_types,
|
| 65 |
'#description' => t('Select content types for which you want to enable Zemanta.'),
|
| 66 |
);
|
| 67 |
return system_settings_form($form);
|
| 68 |
}
|
| 69 |
|
| 70 |
function zemanta_enable() {
|
| 71 |
if (!variable_get('zemanta_api_key',0)) {
|
| 72 |
$api_key = zemanta_api_key_fetch();
|
| 73 |
variable_set('zemanta_api_key', $api_key);
|
| 74 |
}
|
| 75 |
|
| 76 |
$release_id = zemanta_release_id_fetch();
|
| 77 |
variable_set('zemanta_release_id', $release_id);
|
| 78 |
zemanta_get_loader(ZEMANTA_LOADER_URL);
|
| 79 |
drupal_set_message(t('Zemanta module was successfully enabled.'));
|
| 80 |
}
|
| 81 |
|
| 82 |
function zemanta_block($op = 'list', $delta = 0) {
|
| 83 |
global $theme;
|
| 84 |
if ($op == "list") {
|
| 85 |
$block[0] = array(
|
| 86 |
'info' => t('Zemanta'),
|
| 87 |
'weight' => -9,
|
| 88 |
);
|
| 89 |
return $block;
|
| 90 |
}
|
| 91 |
else if ($op == 'view') {
|
| 92 |
if (zemanta_show('add')) {
|
| 93 |
$api_key = variable_get('zemanta_api_key', '');
|
| 94 |
$release_id = variable_get('zemanta_release_id', 0);
|
| 95 |
//$api_key_js = 'window.ZemantaGetAPIKey = function () { return "' . $api_key . '"; }';
|
| 96 |
//drupal_add_js($api_key_js, 'inline');
|
| 97 |
drupal_add_js(array('zemantaApiKey' => $api_key, 'zemantaReleaseID' => $release_id, 'zemantaCurrentTheme' => $theme), 'setting');
|
| 98 |
drupal_add_js(zemanta_loader_path(), 'module');
|
| 99 |
$block['subject'] = 'Zemanta';
|
| 100 |
$block['content'] = '<div id="zemanta-sidebar">Loading Zemanta...</div>';
|
| 101 |
return $block;
|
| 102 |
}
|
| 103 |
return;
|
| 104 |
}
|
| 105 |
}
|
| 106 |
|
| 107 |
function zemanta_cron() {
|
| 108 |
$current_release_id = variable_get('zemanta_release_id', 0);
|
| 109 |
$new_release_id = zemanta_release_id_fetch();
|
| 110 |
if ($current_release_id < $new_release_id) {
|
| 111 |
zemanta_get_loader(ZEMANTA_LOADER_URL);
|
| 112 |
variable_set('zemanta_release_id', $new_release_id);
|
| 113 |
}
|
| 114 |
}
|
| 115 |
|
| 116 |
function zemanta_api_key_fetch() {
|
| 117 |
// Fetch API key used with Zemanta calls
|
| 118 |
$api = '';
|
| 119 |
$response = drupal_http_request(ZEMANTA_API_URL, array(), 'POST', 'method=zemanta.auth.create_user');
|
| 120 |
|
| 121 |
// Parse returned result
|
| 122 |
$matches = zem_reg_match( '/<status>(.+?)<\/status>/', $response->data);
|
| 123 |
if ( 'ok' == $matches[1] ) {
|
| 124 |
$matches = zem_reg_match( '/<apikey>(.+?)<\/apikey>/', $response->data);
|
| 125 |
$api = $matches[1];
|
| 126 |
}
|
| 127 |
return $api;
|
| 128 |
}
|
| 129 |
|
| 130 |
function zemanta_release_id_fetch() {
|
| 131 |
$response = drupal_http_request(ZEMANTA_RELEASE_ID_URL);
|
| 132 |
if ($response->code == 200) {
|
| 133 |
return trim($response->data);
|
| 134 |
}
|
| 135 |
return 0;
|
| 136 |
}
|
| 137 |
|
| 138 |
function zem_reg_match($rstr, $str) {
|
| 139 |
// Make a regex match independantly of library available. Might work only
|
| 140 |
// for simple cases like ours.
|
| 141 |
if (function_exists('preg_match')) {
|
| 142 |
preg_match( $rstr, $str, $matches );
|
| 143 |
}
|
| 144 |
elseif (function_exists('ereg')) {
|
| 145 |
ereg( $rstr, $str, $matches );
|
| 146 |
}
|
| 147 |
else {
|
| 148 |
$matches = array('', '');
|
| 149 |
}
|
| 150 |
return $matches;
|
| 151 |
}
|
| 152 |
|
| 153 |
/**
|
| 154 |
* Shows zemanta block if we selected
|
| 155 |
* proper node type on settings pages.
|
| 156 |
* @param $op 'view' or 'add' depending on which page we want to include Zemanta
|
| 157 |
*/
|
| 158 |
function zemanta_show($op = 'add') {
|
| 159 |
$node_types = variable_get('zemanta_node_types', array());
|
| 160 |
if ($op == 'add') {
|
| 161 |
if (arg(0) == 'node' && arg(1) == 'add' && in_array(arg(2), $node_types, TRUE)) {
|
| 162 |
return TRUE;
|
| 163 |
}
|
| 164 |
}
|
| 165 |
else if ($op == 'view') {
|
| 166 |
return TRUE;
|
| 167 |
}
|
| 168 |
return FALSE;
|
| 169 |
}
|
| 170 |
|
| 171 |
/**
|
| 172 |
* JSON proxy helps us get around xss problems
|
| 173 |
*/
|
| 174 |
function zemanta_json_proxy() {
|
| 175 |
|
| 176 |
foreach ($_POST as $key => $value) {
|
| 177 |
$new_element = str_replace('&', '%26', $value);
|
| 178 |
$new_element = str_replace(';', '%3B', $new_element);
|
| 179 |
$postvars .= $key .'='. $new_element .'&';
|
| 180 |
}
|
| 181 |
|
| 182 |
$response = drupal_http_request(ZEMANTA_API_URL, array(), 'POST', $postvars);
|
| 183 |
drupal_set_header('Content-Type: text/plain');
|
| 184 |
echo $response->data;
|
| 185 |
}
|
| 186 |
|
| 187 |
/**
|
| 188 |
* Checks if currently default filter is using filter html
|
| 189 |
*/
|
| 190 |
function zemanta_check_filter() {
|
| 191 |
$needed_tags = array('fieldset', 'legend', 'h6', 'span', 'img', 'div', 'p');
|
| 192 |
$default_input_format = variable_get('filter_default_format', 1);
|
| 193 |
$filters = filter_list_format($default_input_format);
|
| 194 |
foreach ($filters as $id => $filter) {
|
| 195 |
if ($filter->name == 'HTML filter') {
|
| 196 |
$allowed_tags = preg_split('/\s+|<|>/', variable_get("allowed_html_". $default_input_format, '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>'), -1, PREG_SPLIT_NO_EMPTY);
|
| 197 |
$diff = array_diff($needed_tags, $allowed_tags);
|
| 198 |
if (!empty($diff)) {
|
| 199 |
drupal_set_message(t('Your input filter currently removes tags that Zemanta needs to operate normally. Please add following tags to your input filter: %tags', array('%tags' => implode(', ', $diff))), 'warning');
|
| 200 |
}
|
| 201 |
return FALSE;
|
| 202 |
}
|
| 203 |
}
|
| 204 |
return TRUE;
|
| 205 |
}
|
| 206 |
|
| 207 |
/**
|
| 208 |
* Saves loader.js file locally
|
| 209 |
* @param $url URL of the loader file to get
|
| 210 |
* @return FALSE if get fails or filename if file was saved
|
| 211 |
*/
|
| 212 |
function zemanta_get_loader($url) {
|
| 213 |
$directory = file_directory_path() .'/zemanta';
|
| 214 |
$file_destination = $directory .'/'. basename($url);
|
| 215 |
|
| 216 |
$result = drupal_http_request($url);
|
| 217 |
if ($result->code == 200) {
|
| 218 |
if (file_check_directory($directory, FILE_CREATE_DIRECTORY)) {
|
| 219 |
return file_save_data($result->data, $directory .'/'. basename($url), FILE_EXISTS_REPLACE);
|
| 220 |
}
|
| 221 |
}
|
| 222 |
else {
|
| 223 |
watchdog('zemanta', t('Loader.js get returned error code @error', array('@error', $result->code)), WATCHDOG_ERROR);
|
| 224 |
}
|
| 225 |
return FALSE;
|
| 226 |
}
|
| 227 |
|
| 228 |
/**
|
| 229 |
* Returns path to zemanta loader file
|
| 230 |
* @return path to loader.js file
|
| 231 |
*/
|
| 232 |
function zemanta_loader_path() {
|
| 233 |
return file_directory_path() .'/zemanta/loader.js';
|
| 234 |
}
|