| 1 |
<?php
|
| 2 |
// $Id$
|
| 3 |
/**
|
| 4 |
* Implementation of hook_menu()
|
| 5 |
*/
|
| 6 |
|
| 7 |
function urlborg_menu($may_cache) {
|
| 8 |
if ($may_cache) {
|
| 9 |
$items[] = array(
|
| 10 |
'path' => 'admin/settings/urlborg',
|
| 11 |
'title' => t('urlBorg settings'),
|
| 12 |
'description' => t('Configure urlBorg settings.'),
|
| 13 |
'callback' => 'drupal_get_form',
|
| 14 |
'callback arguments' => array('urlborg_admin_settings'),
|
| 15 |
'access' => user_access('administer urlborg'),
|
| 16 |
);
|
| 17 |
}
|
| 18 |
return $items;
|
| 19 |
}
|
| 20 |
|
| 21 |
/**
|
| 22 |
* Implementation of hook_perm()
|
| 23 |
*/
|
| 24 |
|
| 25 |
function urlborg_perm(){
|
| 26 |
return array('administer urlborg');
|
| 27 |
}
|
| 28 |
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Implementation of hook_admin_settings()
|
| 32 |
*/
|
| 33 |
|
| 34 |
function urlborg_admin_settings() {
|
| 35 |
$form['urlborg_apy_key'] = array(
|
| 36 |
'#type' => 'textfield',
|
| 37 |
'#title' => t('urlBorg api key'),
|
| 38 |
'#description' => t('Your api key.'),
|
| 39 |
'#default_value' => variable_get('urlborg_apy_key', $urlborg_apy_key),
|
| 40 |
'#size' => 6,
|
| 41 |
);
|
| 42 |
|
| 43 |
$form['urlborg_new_sotr_link'] = array(
|
| 44 |
'#type' => 'checkbox',
|
| 45 |
'#title' => t('Create a new short URL if one exists already'),
|
| 46 |
'#return_value' => 1,
|
| 47 |
'#default_value' => variable_get('urlborg_new_sotr_link', 0),
|
| 48 |
);
|
| 49 |
|
| 50 |
$form['urlborg_twitte'] = array(
|
| 51 |
'#type' => 'checkbox',
|
| 52 |
'#title' => t('Twitte it link (pass the node title and sort url to your twitter account)'),
|
| 53 |
'#return_value' => 1,
|
| 54 |
'#default_value' => variable_get('urlborg_twitte', 0),
|
| 55 |
);
|
| 56 |
|
| 57 |
$types = node_get_types();
|
| 58 |
foreach ($types as $type) {
|
| 59 |
$options[$type->type] = t($type->name);
|
| 60 |
}
|
| 61 |
|
| 62 |
$form['urlborg_node_types'] = array(
|
| 63 |
'#type' => 'checkboxes',
|
| 64 |
'#title' => t('Which node types do you want to create sort links?'),
|
| 65 |
'#options' => $options,
|
| 66 |
'#default_value' => variable_get('urlborg_node_types', $urlborg_node_types),
|
| 67 |
);
|
| 68 |
|
| 69 |
return system_settings_form($form);
|
| 70 |
}
|
| 71 |
|
| 72 |
/**
|
| 73 |
* Implementation of hook_admin_settings_validate()
|
| 74 |
*/
|
| 75 |
|
| 76 |
function urlborg_admin_settings_validate($form, &$form_state){
|
| 77 |
$key = $form_state['urlborg_apy_key'];
|
| 78 |
$xml_file = 'http://urlborg.com/api/'. $key .'/key/validate';
|
| 79 |
$xml = simplexml_load_file($xml_file);
|
| 80 |
|
| 81 |
if ( $xml->key_is_valid != "1"){
|
| 82 |
form_set_error('urlborg_apy_key', t('The urlBorg api key is not valid.'));
|
| 83 |
}
|
| 84 |
}
|
| 85 |
|
| 86 |
/**
|
| 87 |
* Implementation of hook_nodeapi()
|
| 88 |
*/
|
| 89 |
function urlborg_nodeapi(&$node, $op, $teaser, $page){
|
| 90 |
$type = variable_get('urlborg_node_types', 0);
|
| 91 |
if ($type[$node->type]){
|
| 92 |
switch ($op){
|
| 93 |
case 'load':
|
| 94 |
if (!$teaser) {
|
| 95 |
if(arg(0) == 'node' & is_numeric(arg(1))){
|
| 96 |
$urlb = urlborg_get_urlb($node->nid);
|
| 97 |
if($urlb){
|
| 98 |
$node->content['urlb'] = array('#value' => urlborg_theme_s_url($urlb->s_url, $node), '#weight' => "20" );
|
| 99 |
}
|
| 100 |
}
|
| 101 |
}
|
| 102 |
break;
|
| 103 |
}
|
| 104 |
}
|
| 105 |
}
|
| 106 |
|
| 107 |
/**
|
| 108 |
* @return theming url urlb
|
| 109 |
* @param object $s_url, $node
|
| 110 |
*/
|
| 111 |
|
| 112 |
function urlborg_theme_s_url($s_url, $node){
|
| 113 |
$output = '<p class="urlborg">';
|
| 114 |
$output .='<span class="label"><a href="' .$s_url .'/p" title="urlBorg Preview" rel="nofollow">Sort URL:</a></span> '.$s_url ;
|
| 115 |
if (variable_get('urlborg_twitte', 0)){
|
| 116 |
$output .= ' <spam "twitteit"><a href="http://twitter.com/home?status=' .$node->title. ' ' .$s_url .'">twitte it</a></spam>';
|
| 117 |
}
|
| 118 |
$output .='</p>';
|
| 119 |
return $output;
|
| 120 |
}
|
| 121 |
|
| 122 |
/**
|
| 123 |
* @return object $urlb
|
| 124 |
* @param $url
|
| 125 |
*/
|
| 126 |
|
| 127 |
function urlborg_get_xml($url){
|
| 128 |
$key = variable_get('urlborg_apy_key', '');
|
| 129 |
$urlborg_new_sotr_link = variable_get('urlborg_new_sotr_link', 0);
|
| 130 |
if ($urlborg_new_sotr_link){
|
| 131 |
$xml_file = 'http://urlborg.com/api/'. $key .'/url/create.xml/' .$url;
|
| 132 |
}
|
| 133 |
else{
|
| 134 |
// 'create_or_reuse' works exactly like 'create' BUT will return an already created short url if the same combination of api_key
|
| 135 |
// and long URL has already created one. In short, it won't create a new short URL if one exists already.
|
| 136 |
$xml_file = 'http://urlborg.com/api/'. $key .'/url/create_or_reuse.xml/' .$url;
|
| 137 |
}
|
| 138 |
echo" ";
|
| 139 |
$xml = simplexml_load_file($xml_file);
|
| 140 |
if(isset($xml->error)){
|
| 141 |
watchdog("urlborg", $xml->error[0], $severity = WATCHDOG_ERROR);
|
| 142 |
$xml = false;
|
| 143 |
}
|
| 144 |
|
| 145 |
return $xml;
|
| 146 |
}
|
| 147 |
|
| 148 |
/**
|
| 149 |
* @return $url path alias
|
| 150 |
* @param $nid
|
| 151 |
*/
|
| 152 |
|
| 153 |
function urlborg_get_path($nid){
|
| 154 |
$url = 'http://';
|
| 155 |
$url .= $_SERVER['HTTP_HOST'];
|
| 156 |
$url .= base_path();
|
| 157 |
$url .= drupal_get_path_alias('node/'.$nid);
|
| 158 |
return $url;
|
| 159 |
}
|
| 160 |
|
| 161 |
/**
|
| 162 |
* @return object $urlb
|
| 163 |
* @param $nid
|
| 164 |
*/
|
| 165 |
function urlborg_get_urlb($nid){
|
| 166 |
$url = urlborg_get_path($nid);
|
| 167 |
$result = db_query('SELECT * FROM {urlborg} WHERE o_url LIKE "%s"', $url);
|
| 168 |
if ( db_affected_rows($result) > 0 ){
|
| 169 |
while ($data = db_fetch_object($result)){
|
| 170 |
$urlb = $data;
|
| 171 |
}
|
| 172 |
}
|
| 173 |
else {
|
| 174 |
urlborg_get_new_urlb($nid);
|
| 175 |
}
|
| 176 |
return $urlb;
|
| 177 |
}
|
| 178 |
|
| 179 |
/**
|
| 180 |
*
|
| 181 |
* @return object $urlb
|
| 182 |
* @param $nid
|
| 183 |
*/
|
| 184 |
function urlborg_get_new_urlb($nid){
|
| 185 |
$url = urlborg_get_path($nid);
|
| 186 |
$urlb = urlborg_get_xml($url);
|
| 187 |
if ($urlb){
|
| 188 |
db_query("INSERT INTO {urlborg} (nid, s_key, created, o_url, s_domain, s_url) VALUES (%d, '%s', %d, '%s', '%s', '%s')",
|
| 189 |
$nid, $urlb->s_key, time(), $urlb->o_url, $urlb->s_domain, $urlb->s_url );
|
| 190 |
return $urlb;
|
| 191 |
}
|
| 192 |
}
|
| 193 |
|