View source
<?php
define('VKXP_ACCESS_TOKEN_URI', 'https://oauth.vk.com/access_token');
define('VKXP_ACCESS_TOKEN_REDIRECT_URI', 'http://oauth.vk.com/blank.html');
define('VKXP_AUTHORIZE_URI', 'https://oauth.vk.com/authorize');
define('VKXP_AUTHORIZE_REDIRECT_URI', 'http://oauth.vk.com/blank.html');
define('VKXP_AUTHORIZE_SCOPE', 'wall,groups,photos,offline');
define('VKXP_AUTHORIZE_RESPONSE_TYPE', 'code');
define('VKXP_API_REQUEST_URI', 'https://api.vk.com/method');
function vkxp_help($path) {
switch ($path) {
case 'admin/settings/vkxp':
return t('Vkontakte CrossPoster module allows to post nodes automatically to social network VK.');
case 'admin/help#vkxp':
$output = '';
$output .= '<p>' . t('Vkontakte CrossPoster module allows users to post nodes automatically to social network VK.') . '</p>';
$output .= '<p>' . t('It requires creating web site application at http://vk.com/editapp?act=create') . '</p>';
$output .= '<p>' . t('After creating application you should go to the module settings page and copy there Application ID and Secret code.') . '</p>';
return $output;
}
}
function vkxp_menu() {
$items['admin/settings/vkxp'] = array(
'title' => 'VKontakte CrossPoster',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'vkxp_admin_main_settings',
),
'access arguments' => array(
'administer vkontakte crossposter',
),
'file' => 'vkxp.admin.inc',
);
$items['admin/settings/vkxp/settings'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'vkxp_admin_main_settings',
),
'access arguments' => array(
'administer vkontakte crossposter',
),
'file' => 'vkxp.admin.inc',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => 1,
);
$items['admin/settings/vkxp/access_token'] = array(
'title' => 'Recieve access token',
'page callback' => 'drupal_get_form',
'page arguments' => array(
'vkxp_admin_access_token',
),
'access arguments' => array(
'administer vkontakte crossposter',
),
'file' => 'vkxp.admin.inc',
'type' => MENU_LOCAL_TASK,
'weight' => 2,
);
return $items;
}
function vkxp_perm() {
return array(
'administer vkontakte crossposter',
'post to vkontakte',
);
}
function vkxp_form_node_type_form_alter(&$form, &$form_state) {
module_load_include('forms.inc', 'vkxp');
_vkxp_process_node_settings_form($form);
}
function vkxp_form_alter(&$form, &$form_state, $form_id) {
if (empty($form['#node']->type) || $form_id != $form['#node']->type . '_node_form') {
return;
}
$type_enabled = variable_get('vkxp_node_enabled_' . $form['#node']->type, NULL);
$user_access = user_access('post to vkontakte');
if ($type_enabled && $user_access) {
module_load_include('forms.inc', 'vkxp');
_vkxp_process_node_form($form);
}
}
function vkxp_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) {
switch ($op) {
case 'insert':
case 'update':
_vkxp_process_node($node);
break;
}
}
function _vkxp_process_node($node) {
$vkxp_enabled = variable_get('vkxp_node_enabled_' . $node->type, NULL);
$access_token = variable_get('vkxp_access_token', NULL);
$user_access = user_access('post to vkontakte');
$post_node = isset($node->vkxp_post_this_node) ? $node->vkxp_post_this_node : FALSE;
if ($vkxp_enabled && $access_token && $user_access && $post_node) {
$message = _vkxp_get_node_message($node);
$images = _vkxp_get_node_images($node);
$url = url('node/' . $node->nid, array(
'absolute' => TRUE,
));
_vkxp_post_to_wall($message, $images, $url);
}
}
function _vkxp_get_node_images($node) {
$image_ids = '';
$image_amount = variable_get('vkxp_node_image_limit_' . $node->type, 0);
if ($image_amount) {
$field = variable_get('vkxp_node_image_field_' . $node->type, 0);
$i = 0;
$items = $node->{$field};
if (!empty($items) && is_array($items)) {
$images = array();
foreach ($items as $image) {
if (isset($image['fid'])) {
if ($i++ == $image_amount) {
break;
}
if ($file = field_file_load($image['fid'])) {
$images[] = '@' . realpath($file['filepath']);
}
}
}
}
if (!empty($images)) {
$upload_url = _vkxp_get_upload_server();
$image_ids = _vkxp_upload_images($upload_url, $images);
}
}
return $image_ids;
}
function _vkxp_get_node_message($node) {
$message_field = variable_get('vkxp_node_message_field_' . $node->type, 'title');
if ($message_field == 'title') {
$message = $node->title;
}
else {
$item = $node->{$message_field};
if ($message_field == 'body') {
$message = trim(strip_tags($item));
}
else {
$message = trim(strip_tags($item[0]['value']));
}
}
$message_length = variable_get('vkxp_node_message_length_' . $node->type, 255, '');
if (!empty($message_length) && drupal_strlen($message) > $message_length) {
$message = drupal_substr($message, 0, $message_length - 3) . '...';
}
$message = html_entity_decode($message, ENT_QUOTES, 'UTF-8');
$message = htmlspecialchars_decode($message);
return $message;
}
function vkxp_query($method, $params, $request_url = VKXP_API_REQUEST_URI) {
$query = array();
$query['method'] = $method;
$query['params'] = $params;
$query['request_url'] = $request_url;
drupal_alter('vkxp_query', $query);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $query['request_url'] . '/' . $query['method']);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $query['params']);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
return json_decode($result, TRUE);
}
function _vkxp_get_upload_server() {
$params = array();
if (variable_get('vkxp_wall_owner', 'group') == 'group') {
$params['gid'] = variable_get('vkxp_owner_id', '');
}
else {
$params['uid'] = variable_get('vkxp_owner_id', '');
}
$params['access_token'] = variable_get('vkxp_access_token', '');
$result = vkxp_query('photos.getWallUploadServer', $params);
if ($result['response']['upload_url']) {
return $result['response']['upload_url'];
}
elseif ($result['error']) {
_vkxp_watchdog(array(
'text' => t('Unable to recieve upload server. Error: !error', array(
'!error' => $result['error']['error_msg'],
)),
'severity' => 'error',
));
}
return FALSE;
}
function _vkxp_upload_images($upload_url, $images) {
$image_ids = array();
foreach ($images as $image) {
$upload_result = vkxp_query('', array(
'photo' => $image,
), $upload_url);
if (isset($upload_result['server']) && isset($upload_result['photo']) && isset($upload_result['hash'])) {
$params = array();
$params['access_token'] = variable_get('vkxp_access_token', '');
$params['server'] = $upload_result['server'];
$params['photo'] = $upload_result['photo'];
$params['hash'] = $upload_result['hash'];
if (variable_get('vkxp_wall_owner', 'group') == 'group') {
$params['gid'] = variable_get('vkxp_owner_id', '');
}
else {
$params['uid'] = variable_get('vkxp_owner_id', '');
}
$save_result = vkxp_query('photos.saveWallPhoto', $params);
if (isset($save_result['response'][0]['id'])) {
$image_ids[] = $save_result['response'][0]['id'];
}
}
}
return !empty($image_ids) ? implode(',', $image_ids) : '';
}
function _vkxp_post_to_wall($message, $images, $url) {
$params = array();
$params['from_group'] = variable_get('vkxp_official', TRUE);
if (variable_get('vkxp_wall_owner', 'group') == 'group') {
$params['owner_id'] = '-' . variable_get('vkxp_owner_id', '');
}
else {
$params['owner_id'] = variable_get('vkxp_owner_id', '');
}
$params['message'] = $message;
$params['attachments'] = $images;
if (variable_get('vkxp_add_link', FALSE)) {
$params['attachments'] .= !empty($images) ? ',' . $url : $url;
}
$params['access_token'] = variable_get('vkxp_access_token', '');
return vkxp_query('wall.post', $params);
}
function _vkxp_watchdog($message, $link = NULL) {
drupal_set_message($message['text'], $message['severity']);
if ($message['severity'] == 'status') {
$severity = WATCHDOG_INFO;
}
elseif ($message['severity'] == 'warning') {
$severity = WATCHDOG_WARNING;
}
else {
$severity = WATCHDOG_ERROR;
}
watchdog('vkxp', $message['text'], $severity, $link);
}