getFullTableName('redirect_twitter');
/*
* Build full path and make long_url
*/
$depth = '0';
$path = '';
$server = 'http://' . $_SERVER['SERVER_NAME'];
$server = str_replace('www.', '', $server);
while($parent != '0' || $depth >= $limit){
$depth++;
$pInfo = $modx->getPageInfo($parent, '1','parent, alias');
$parent = $pInfo['parent'];
if(in_array($parent,$tweetThis)) { $tweetMe = true; }
$path = $pInfo['alias'] . '/' . $path;
}
// If not in a directory that needs tweeting - return
if(!$tweetMe) { return; }
$long_url = $server . '/' . $path . $alias . '.html';
// ++++++++++++++++++++++++++++++++++++++++++++++++
/*
* Check if published - if not return
*/
$sql = "SELECT * FROM $tbl WHERE long_url = '$long_url' ";
$rs = $modx->db->query($sql);
if($modx->db->getRecordCount($rs) > '0') {
// If not published and in database - delete it
if($_POST['published'] == '0') {
$sql = "DELETE FROM $tbl WHERE long_url='$long_url' LIMIT 1";
$rs = $modx->db->query($sql);
return;
} else { return ; }
} else if($_POST['published'] == '0') {
// If not published and not in database - just return
return;
}
// ++++++++++++++++++++++++++++++++++++++++++++++++
// Get last short_url
$sql = "SELECT `short_url` FROM " . $modx->getFullTableName('redirect_twitter') . " ORDER BY `short_url` DESC LIMIT 0, 1";
$rs = $modx->db->query($sql);
$row = $modx->db->getRow($rs, 'assoc');
$last_url = $row['short_url'];
// Create next short url
$validExt = "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
if($last_char = substr($last_url, -1)) {
$prefix = substr($last_url, 0, -1);
}
// If it ends with a z we need a new cloumn
if($last_char == 'z') {
$short_url = $last_url . '1';
} else { // Strip the last character and concatenate the next one
$pos = strpos($validExt, $last_char) + 1;
$short_url = $prefix . substr($validExt, $pos, 1);
}
/*
* Prepare message and sent it to Twitter
*/
$message = $msgPrefix;
$msgTail = ' ' . $server . '/' . $htaccessTrigger . $short_url;
$len = strlen($message . $msgTail);
$maxLen = 140 - $len;
$msg = trim($_POST['longtitle']);
if(strlen($msg) > $maxLen){
// If too long chop and add an elipse
while(strlen($msg . $ellipses) > $maxLen) {
$pos = strrpos($msg, ' ');
$msg = substr($msg, 0, $pos);
}
$msg .= $ellipses;
}
$message .= $msg . $msgTail;
/*
* Send it to Twitter - is successful update the table if not write error message
*/
$data = postToTwitter($username,$password,$message);
if($data['error']) {
$msg = $data['msg'];
$sql= "INSERT INTO " . $modx->getFullTableName("event_log") .
" (eventid,type,createdon,source,description,user) " .
"VALUES('','$type','" . time() . "','Twitter Notify','$msg','0')";
$rs= $modx->db->query($sql);
return;
} else {
// Add to lookup table
$sql = "INSERT INTO $tbl (`short_url` , `long_url` ) VALUES ('$short_url', '$long_url')";
$rs = $modx->db->query($sql);
}
// +++++++++++++++++++++++++++++++++++++++++++++
return;
// A simple function using Curl to post (GET) to Twitter
// Kosso : March 14 2007
function postToTwitter($username,$password,$message){
$data['error'] = false;
$host = "http://twitter.com/statuses/update.xml?status=".urlencode(stripslashes(urldecode($message)));
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $host);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "");
$result = curl_exec($ch);
// Look at the returned header
$resultArray = curl_getinfo($ch);
curl_close($ch);
if($resultArray['http_code'] == "200"){
return $data;
} else {
$data['error'] = true;
$data['msg'] = "Twitter returned HTTP Code " . $resultArray['http_code'];
}
return $data;
}