';
echo '
';
echo '

';
echo '
';
echo '
';
echo '
';
//echo '
';
echo '
';
if ($user_id == $_USER['ID']) echo '

';
if($attached_file) {
echo '
';
echo '
';
echo '';
}
if($_USER && $type == 'public') {
if ($is_favorite)
echo '

';
else
echo '

';
}
if ($type != 'private') echo '

';
if($_USER && $_USER['username'] != $username && !defined('HIDE_PERMALINK_ICON')) {
echo '

';
}
echo '
';
echo '
';
echo showTimeAgo($timestamp);
echo ' (
';
if ($from == 'mobile') echo '
'; else echo $from;
echo ')';
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
}
}
function put_smileys($string) {
$string = preg_replace('/(\s|^)(:pedobear:)/i', '

', $string);
$string = preg_replace('/(\s|^)(:awesome:)/i', '

', $string);
$string = preg_replace('/(\s|^)(:roll:)/i', '

', $string);
$string = preg_replace('/(\s|^)(:-{0,1}\\\|;-{0,1}\\\|:-\/|:-{0,1}S|:-{0,1}\?)/', '

', $string);
$string = preg_replace('/(\s|^)(:-{0,1}\()/i', '

', $string);
$string = preg_replace('/(\s|^)(:-{0,1}D)/i', '

', $string);
$string = preg_replace('/(\s|^)(:-{0,1}O)/i', '

', $string);
$string = preg_replace('/(\s|^)(:-{0,1}\))/i', '

', $string);
$string = preg_replace('/(\s|^)(\^\^)/i', '

', $string);
$string = preg_replace('/(\s|^)(¬¬)/i', '

', $string);
$string = preg_replace('/(\s|^)(8-{0,1}\))/i', '

', $string);
$string = preg_replace('/(\s|^)(:-{0,1}P)/i', '

', $string);
$string = preg_replace('/(\s|^)(:\'-{0,1}\()/i', '

', $string);
$string = preg_replace('/(\s|^)(:\'-{0,1}\))/i', '

', $string);
$string = preg_replace('/(\s|^)(x-{0,1}D)/i', '

', $string);
$string = preg_replace('/(\s|^)(:-{0,1}\|)/i', '

', $string);
$string = preg_replace('/(\s|^)(:-{0,1}\@)/i', '

', $string);
$string = preg_replace('/(\s|^)(:-{0,1}\*)/i', '

', $string);
return $string;
}
function showTwit($noteID, $reducedAjax = false) {
global $db;
global $_USER;
//We get the info of the Tweet.
$noteInfo = $db->getTwit($noteID);
if (!$noteInfo) return false;
else extract($noteInfo);
if ($reducedAjax == true) $return = array();
$note = utf8_htmlentities($note);
//Highlighting links...
$note = preg_replace('#https?://[^.\s]+\.[^\s]+#ix', "
\\0", $note);
preg_match_all("#https?://[^.\s]+\.[^\s]+#ix", $note, $matches);
$shorter_service = unserialize($_USER['shorter_service']);
foreach ($matches as $uri) {
if ($shorter_service['preview'] == true) {
$uri[0] = str_replace('"', '', $uri[0]);
$parse_url = parse_url($uri[0]);
if ($parse_url['host'] == '3.ly') $note = str_replace($uri[0], $uri[0].'-', $note);
if ($parse_url['host'] == 'tinyurl.com') $note = str_replace($uri[0], 'http://preview.tinyurl.com'.$parse_url['path'], $note);
if ($parse_url['host'] == 'bit.ly' || ($parse_url['host'] == 'j.mp')) $note = str_replace($uri[0], $uri[0].'+', $note);
if ($parse_url['host'] == 'is.gd') $note = str_replace($uri[0], $uri[0].'-', $note);
if ($parse_url['host'] == 'u.nu') $note = str_replace($uri[0], $uri[0].'?', $note);
}
}
$note = stripslashes(put_smileys($note));
$note = preg_replace('/(\s|\A)(@){1}([a-zA-Z0-9_]+)/', '$1
$2$3', $note);
$note = preg_replace('/(\s|\A)(#){1}([a-zA-Z0-9_]+)/', '$1
$2$3', $note);
$serial = unserialize(stripslashes($serial));
extract($serial);
if ($reducedAjax == true) {
$return['avatar'] = $avatar;
$return['text'] = $note;
$return['id'] = $ID;
$return['time_ago'] = showTimeAgo($timestamp);
$return['username'] = $username;
$return['type'] = 'twitter';
return $return;
}
else {
$note_style = 'note_t';
echo '
';
echo '
';
echo '

';
echo '
';
echo '
';
echo '
';
echo '
';
echo '

';
if($_USER) {
echo ' ';
echo '

';
}
echo '
';
echo '
';
echo showTimeAgo($timestamp);
echo ' (twitter)';
echo '
';
echo '
';
echo '
'.$note.'
';
echo '
';
echo '
';
}
}
function showStatus($text, $status) {
switch ($status) {
case 'ok':
return '
'.$text.'
';
break;
case 'warning':
return '
'.$text.'
';
break;
case 'error':
return '
'.$text.'
';
break;
case 'info':
return '
'.$text.'
';
break;
}
}
function showFollower($follower) {
extract($follower);
if ($avatar) $avatar = get_avatar_url($username, $avatar, 'follow'); else $avatar = BASE.'static/img/avatar/default_follow.png';
echo '

';
}
function showAJAX($section = 'public', $page = 1) {
define('AJAX_SECTION', $section);
define('AJAX_PAGE', $page);
}
function showUser($to_userID) {
global $db;
global $_USER;
$userInfo = $db->getUserInfoNote($to_userID);
extract($userInfo);
if (!$avatar) $avatar = BASE.'static/img/avatar/default_note.png'; else $avatar = get_avatar_url($username, $avatar, 'note');
echo '
';
echo '
';
echo '

';
echo '
';
echo '
';
echo '
';
if ($realname) {
echo '
';
echo $realname;
echo '
';
}
echo '
';
echo '
';
if ($_USER) {
if ($_USER['ID'] !== $to_userID) {
$currentUserID = $_USER['ID'];
if($currentUserID != $userID) {
if (!in_array($to_userID, $_USER['ignored'])) {
$follows = $db->checkFollowing($_USER['ID'], $to_userID);
if ($follows) {
$button_text = __('Unfollow');
} else {
$button_text = __('Follow');
}
}
}
$btn = 'action'.md5($to_userID);
if (!in_array($to_userID, $_USER['ignored'])) {
echo '
';
$checker = $db->checkFollowing($to_userID, $_USER['ID']);
if ($checker) echo '

';
$last_note = $db->getLastNotes($to_userID, 1);
if (count($last_note) == 1) {
echo '
'.__('Last note:').' '.showtimeago($last_note[0]['timestamp']);
}
}
else echo __('To follow this user, first you have to stop ignoring him.');
}
}
echo '';
echo '
';
echo '
';
}
/* NOTE POSTING */
function postNote($note = 0, $userInfo = 0, $from = 'web', $auth = 0, $attached_file = 0, $replying = 0, $get_id = 0, $sendTwitter = true) {
global $_USER;
global $db;
global $globals;
global $skipauth;
global $mailing;
//We shouldn't try to post if we don't have any note.
if (!$db && (!$note)) return;
//If $userInfo is not set, then we post the note as the logged user.
if (!$userInfo) $userInfo = $_USER;
//Security check.
if (md5($_USER['salt']) != $auth) {
if (!$skipauth) return 'INVALID';
}
//Because we don't want to have 'spam' notes, we filter the 'Added hellotxt...' notes.
//if ($note == 'Added http://hellotxt.com') return;
if (!defined('ENABLE_MBSTRING') || (ENABLE_MBSTRING == false)) $length = strlen(utf8_decode(trim($note)));
else $length = mb_strlen(utf8_decode(trim($note)));
if ($length < 3) return 'SHORT_NOTE';
elseif ($length > 140) return 'LONG_NOTE';
if ((time() - $_USER['last_note']) < WAIT_UNTIL_REPOST) return 'COWBOY';
$userID = $userInfo['ID'];
$username = $userInfo['username'];
if ($_FILES['attach']['tmp_name']) {
$filename = str_replace(' ', '_', $_FILES['attach']['name']);
$extension = strtolower(pathinfo($filename, PATHINFO_EXTENSION));
$file_name = substr(md5(rand()), 0, 8) .'.'.$extension;
$size = $_FILES['attach']['size'] / 1024;
if (!in_array($extension, $globals['denied_extensions']) or strlen($extension) != 0) {
if ($size <= 8192) {
if (is_dir('users_files/'.$username.'/files') === false) @ mkdir('users_files/'.$username.'/files', 0777);
$dir_file = 'users_files/'.$username.'/files/'.$file_name;
if (copy($_FILES['attach']['tmp_name'], $dir_file)) $attached_file = $file_name;
} else return 'BIG_FILE';
} else return 'FILE_NOT_ALLOWED';
}
$shorter_service = unserialize($userInfo['shorter_service']);
preg_match_all("#https?://[^.\s]+\.[^\s]+#ix", $note, $matches);
foreach ($matches[0] as $uri) {
if ($shorter_service['service'] == 'default') {
if (defined('DEFAULT_SHORTER_SERVICE')) $sh_service = DEFAULT_SHORTER_SERVICE;
else $sh_service = 'none';
}
else $sh_service = $shorter_service['service'];
if ($sh_service != 'none') {
if ($sh_service == '3.ly') $mlen = 16;
elseif ($sh_service == 'tinyurl.com') $mlen = 28;
elseif ($sh_service == 'ves.cat') $mlen = 18;
elseif ($sh_service == 'pic.gd') $mlen = 21;
elseif ($sh_service == 'is.gd') $mlen = 19;
elseif ($sh_service == 'bit.ly') $mlen = 21;
elseif ($sh_service == 'j.mp') $mlen = 19;
elseif ($sh_service == 'urlal.com') $mlen = 22;
elseif ($sh_service == 'ta.gd') $mlen = 18;
elseif ($sh_service == 'tinyarro') $mlen = 18;
elseif ($sh_service == 'xn--ogi.ws') $mlen = 18;
elseif ($sh_service == 'xn--vgi.ws') $mlen = 18;
elseif ($sh_service == 'xn--3fi.ws') $mlen = 18;
elseif ($sh_service == 'xn--egi.ws') $mlen = 18;
elseif ($sh_service == 'xn--9gi.ws') $mlen = 18;
elseif ($sh_service == 'xn--5gi.ws') $mlen = 18;
elseif ($sh_service == 'xn--1ci.ws') $mlen = 18;
elseif ($sh_service == 'xn--odi.ws') $mlen = 18;
elseif ($sh_service == 'xn--rei.ws') $mlen = 18;
elseif ($sh_service == 'xn--cwg.ws') $mlen = 18;
elseif ($sh_service == 'xn--bih.ws') $mlen = 18;
elseif ($sh_service == 'xn--fwg.ws') $mlen = 18;
elseif ($sh_service == 'xn--l3h.ws') $mlen = 18;
elseif ($sh_service == 'wipi.es') $mlen = 20;
elseif ($sh_service == 'xrl.us') $mlen = 20;
elseif ($sh_service == 'cort.as') $mlen = 18;
if (strlen($uri) > $mlen) $note = str_replace($uri, shorter_url(array($uri), $sh_service), $note);
}
}
//We check if there's an API name registered
if ($from != 'web') {
$result = $db->checkAPIName($from);
if (!$result) $from = 'api';
}
//If the first character of the note is the '!' symbol, then we know it's a private note.
if ($note{0} == '!') {
preg_match('/!([\w\d]+)/', $note, $to_array);
$to = $to_array[1];
$to_userID = $db->getIdByUser($to);
if (!$to_userID) return 'INVALID_USER';
else {
if ($_USER['ID'] != $to_userID) {
$following = $db->checkFollowing($to_userID, $userID);
if (!$following) return 'NOT_FOLLOWING';
else {
$newPrivateNote = $db->newPrivateNote($userID, $to, $note, $attached_file, $from, $_SERVER['REMOTE_ADDR']);
if (!$newPrivateNote) return 'INVALID_USER';
else {
//We obtain the userInfo of the receiver of the note.
$info = $db->getUserInfo(false, $to);
if ($info['notification_level'] >= 2) {
$note = preg_replace('/(\s|\A)(!){1}(\w+)(\s|^)/', '', $note);
$mailing->newPrivateNote($info, stripslashes($note), $userInfo);
}
if ($get_id) return $newPrivateNote;
else return 'OK';
}
}
}
else return 'CANT_SEND_OWN_USER';
}
}
//Replies, Twitter or Normal notes.
else {
$twitter = false;
$send_to_twitter = true;
preg_match_all('/(?:\s|\A)(%|@){1}(\w+)/', $note, $matches);
foreach ($matches[1] as $key => $value) {
if ($value == '@') {
if (strtolower($from) == 'hellotxt') return;
else {
$to[] = $matches[2][$key];
$send_to_twitter = false;
$reply = $db->getLastNotePermalink($to[0]);
$replying = $reply['permalink'];
$reply_user = $reply['ID'];
}
} elseif ($value == '%') {
$to[] = $matches[2][$key];
$note = str_replace('%'.$matches[2][$key], '@'. $matches[2][$key], $note);
$twitter = true;
}
}
if ($userInfo['private'] == '1') $type = 'personal';
else $type = 'public';
//We create the note. It should return the ID of inserted note
$newNote = $db->newNote($userID, $to[0], $type, $twitter, trim($note), $attached_file, $from, $replying, $_SERVER['REMOTE_ADDR'], $reply_user);
//We look for tags in the post
preg_match_all('/(\s|\A)(#){1}([a-zA-Z0-9_]+)/', $note, $matches);
$added_tags = array('');
foreach ($matches[0] as $tag) {
$tag = trim(str_replace('#', '', $tag));
if (!in_array($tag, $added_tags)) {
if (strlen($tag) <= 20) $db->createTag($tag, $userID, $newNote, time());
}
$added_tags[] = $tag;
}
updateLastNote();
if ($send_to_twitter && $userInfo['twitter_username'] && $userInfo['twitter_password']) {
if ($sendTwitter == true) fork('cron/twitter?note='.$newNote.'&user_id='.$userID.'&auth='.md5($userInfo['salt']));
}
//We check the relationship between the users.
if ($userInfo['private'] == '1') $result = $db->getFriendCreator($userID, true);
else $result = $db->getFriendCreator($userID);
$bulk = array();
if ($result != 'false') {
foreach ($result as $follower) {
$bulk[] = "('$userID', '".$follower["creator"]."', '$newNote', '".time()."', '$reply_user', '$type')";
}
}
$bulk[] = "('$userID', '$userID', '$newNote', '".time()."', '0', '$type')";
$result = $db->post2id($bulk);
if ($get_id) return $newNote;
else return 'OK';
}
}
define('TSMINUTE', 60);
define('TSHOUR', 3600);
define('TSHALFHOUR', 1800);
define('TSHOURANDHALF', TSHOUR+TSHALFHOUR);
define('TSDAY', 86400);
function showTimeAgo($timestamp) {
$diff = time() - (int) $timestamp;
if($diff <= 1) {
return __('right now');
} elseif ($diff < 60) {
$string = sprintf(__("%ss ago"), $diff);
return $string;
} elseif ($diff < TSHOUR) {
$mdiff = round($diff / TSMINUTE);
$string = sprintf(__("%sm ago"), $mdiff);
return $string;
} elseif (($diff / TSHOUR) >= 1 && ($diff / TSHOUR) < 24) {
$mdiff = ($diff / TSHOUR);
$half = round($mdiff) !== floor($mdiff);
$floor = floor($mdiff);
if($half) {
if($floor == 1)
// $string = __("about one hour and half ago");
$string = __("about 1h ago");
else
// $string = sprintf(__("about %s hours and half ago"), $floor);
$string = sprintf(__("about %sh ago"), $floor);
} else {
if($floor == 1)
$string = __("about 1h ago");
else
// $string = sprintf(__("about %s hours ago"), $floor);
$string = sprintf(__("about %sh ago"), $floor);
}
return $string;
} elseif (($diff / TSDAY) >= 1 && ($diff / TSDAY) < 25) {
$mdiff = ($diff / TSDAY);
$half = round($mdiff) !== floor($mdiff);
$floor = floor($mdiff);
if($half) {
if($floor == 1)
// $string = __("about one day and half ago");
$string = __("about 1d ago");
else
// $string = sprintf(__("about %s days and half ago"), $floor);
$string = sprintf(__("about %sd ago"), $floor);
} else {
if($floor == 1)
$string = __("about 1d ago");
else
// $string = sprintf(__("about %s days ago"), $floor);
$string = sprintf(__("about %sd ago"), $floor);
}
return $string;
} else {
$date = sprintf(__("the %s at %s"), date('d/m/Y', $timestamp), date('H:i', $timestamp));
return $date;
}
}
function use_invitations () {
if(!defined('USE_INVITATION')) return false;
$c = constant('USE_INVITATION');
if($c) return true;
return false;
}
function check_invitation($token = '') {
global $db;
if(!use_invitations()) return true;
if(empty($token)) return false;
return ((bool) $db->checkToken($token));
}
/* REGISTRATION */
function register() {
global $globals;
global $db;
global $_USER;
global $mailing;
if(!empty($globals['recaptcha_public_key'])) import('recaptchalib');
$token = $_POST['token'];
if (!check_invitation($token)) return showStatus(__('You need a valid token for register an account!'), 'warning');
if (!$_POST['legal'] && (defined('TOS') && (TOS == true))) return showStatus(__('You must accept our legal terms'), 'error');
$username = $_POST['username'];
$email = $_POST['email'];
$salt = substr(md5(rand()), 0, 5);
$api = substr(md5($_POST['username'].rand()), 0, 16);
$password = md5(md5($_POST['password']).md5($salt));
$password2 = md5(md5($_POST['password2']).md5($salt));
$ip = $_SERVER['REMOTE_ADDR'];
$language = $_POST['language'];
if (!defined('NO_CONFIRMATION_EMAIL') || (NO_CONFIRMATION_EMAIL == false)) $noc = false; else $noc = true;
if(!empty($globals['recaptcha_public_key'])) $human = recaptcha_check_answer($globals['recaptcha_private_key'], $ip, $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
if ($db->checkUsername($username)) return showStatus(__('Taken username, please choose another!'), 'warning');
if ($db->checkEmail($email)) return showStatus(__('Email is taken, please choose another!'), 'warning');
if ($password != $password2) return showStatus(__('Password doesn\'t match!'), 'warning');
if(!empty($globals['recaptcha_public_key']) && !$human->is_valid) return showStatus(__('Incorrect reCAPTCHA code!'), 'warning');
if (!validUsername($username)) return showStatus(__('Username has more than 15 characters or it\'s invalid!'), 'warning');
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) return showStatus(__('Invalid email!'), 'warning');
$tmpKey = substr(md5(rand()), 0, 6);
$newUser = $db->newUser($username, $password, $api, $salt, $language, DEFAULT_THEME, $email, $ip, $tmpKey, $noc);
if(!empty($token)) $deleteToken = $db->deleteToken($token);
if ($noc == false) {
$mailing->confirmRegistration($email, $newUser, $tmpKey);
}
/* $SID = md5(time().$salt.$username);
$db->newSession($newUser, $SID);
$userInfo = $db->getUserInfo($userID);
$_USER = $userInfo;
setcookie(NAME, $SID, time()+(86400*60)); */
createUserDirs($username);
header('Location: '.BASE.'register?status=done');
}
function createUserDirs($username) {
@mkdir(PATH."users_files/$username", 0777);
@mkdir(PATH."users_files/$username/img", 0777);
@mkdir(PATH."users_files/$username/img/avatar", 0777);
@mkdir(PATH."users_files/$username/img/background", 0777);
@mkdir(PATH."users_files/$username/files", 0777);
}
/* INVITATIONS */
function newInvitation($email, $token) {
global $db;
global $_USER;
global $mailing;
$result = $db->newInvitation($_USER['ID'], $email, $token);
if ($result) {
$mailing->newInvitation($email, $token);
$result = $db->updateInvitations($_USER['ID']);
}
}
/* API FUNCTIONS */
function apiError($format, $request, $error, $no401 = false) {
header('HTTP/1.1 401 Unauthorized');
if ($format == 'xml' || ($format == 'rss')) {
header('Content-Type: text/xml charset=UTF-8');
$XMLWriter = new XMLWriter();
$XMLWriter->openURI('php://output');
$XMLWriter->startDocument('1.0', 'UTF-8');
$XMLWriter->startElement('hash');
$XMLWriter->writeElement('request', $request);
$XMLWriter->writeElement('error', $error);
$XMLWriter->endElement();
} elseif ($format == 'json') {
header('Content-Type: text/javascript; charset=utf-8');
$array = array(
'request' => $request,
'error' => $error
);
echo json_encode($array);
} elseif ($format == 'basic') {
echo $error;
}
}
function createXML($type, $result) {
global $db;
global $_USER;
$XMLWriter = new XMLWriter();
$XMLWriter->openURI('php://output');
$XMLWriter->startDocument('1.0', 'UTF-8');
if ($type == 'notes') {
if (count($result) != 1) $XMLWriter->startElement('statuses'); // starting
}
elseif ($type == 'direct_messages') $XMLWriter->startElement('direct-messages'); //starting
elseif ($type == 'users') $XMLWriter->startElement('users'); //starting
elseif ($type == 'ids') $XMLWriter->startElement('ids'); //starting
if ($type != 'ids' && ($type != 'notess')) {
$XMLWriter->writeAttribute('type', 'array');
}
foreach ($result as $note) {
if ($type == 'ids') {
$XMLWriter->writeElement('id', $note['ID']);
}
else {
if ($type != 'users') {
if ($type == 'notess') $noteArray = $db->getNoteCombined($note);
else $noteArray = $db->getNoteCombined($note['id']);
$favorite = $db->checkFavorite($_USER['ID'], $noteArray['ID']);
$note = trim($noteArray['note']);
if ($type == 'notes' || ($type == 'notess')) $id_to_check = array('user'=>$noteArray['user_id']);
elseif ($type == 'direct_messages') $id_to_check = array('sender'=>$noteArray['user_id'], 'recipient'=>$db->getIdByUser($noteArray['to']));
if ($type == 'notes' || ($type == 'notess')) $XMLWriter->startElement('status'); // starting in
elseif ($type == 'direct_messages') $XMLWriter->startElement('direct_message'); // starting in
$XMLWriter->writeElement('id', $noteArray['ID']);
$XMLWriter->writeElement('created_at', date('D M j G:i:s O Y', $noteArray['timestamp']));
$XMLWriter->writeElement('text', stripslashes($note));
if ($type == 'direct_messages') {
$XMLWriter->writeElement('sender_id', $db->getIdByUser($noteArray['to']));
$XMLWriter->writeElement('recipient_id', $noteArray['user_id']);
$XMLWriter->writeElement('sender_screen_name', $noteArray['username']);
$XMLWriter->writeElement('recipient_screen_name', $noteArray['to']);
} elseif ($type == 'notes') {
$XMLWriter->writeElement('source', $noteArray['from']);
$XMLWriter->writeElement('truncated', 'false');
if ($noteArray['replying']) $XMLWriter->writeElement('in_reply_to_status_id', $noteArray['replying']);
else $XMLWriter->writeElement('in_reply_to_status_id', null);
if ($noteArray['replying']) $XMLWriter->writeElement('in_reply_to_user_id', $noteArray['reply_user']);
else $XMLWriter->writeElement('in_reply_to_user_id', null);
$XMLWriter->writeElement('favorited', (bool)$noteArray['favorite']);
if ($noteArray['replying']) $XMLWriter->writeElement('in_reply_to_screen_name', $noteArray['to']);
else $XMLWriter->writeElement('in_reply_to_screen_name', null);
}
}
else $id_to_check = array('user'=>$note);
foreach ($id_to_check as $key => $userto) {
$profile = $db->getUserInfo($userto);
//if the user has no avatar, the default avatar url is returned
if (!$profile['avatar']) $avatar = BASE.'static/img/avatar/default_note.png'; else $avatar = get_avatar_url($profile['username'], $profile['avatar'], 'note');
$XMLWriter->startElement($key); // starting in in
$XMLWriter->writeElement('id', $userto['ID']);
if ($profile['realname']) $XMLWriter->writeElement('name', $profile['realname']);
else $XMLWriter->writeElement('name', $profile['username']);
$XMLWriter->writeElement('screen_name', $profile['username']);
if ($profile['location']) $XMLWriter->writeElement('location', $profile['location']);
else $XMLWriter->writeElement('location', null);
if ($profile['bio']) $XMLWriter->writeElement('description', $profile['bio']);
else $XMLWriter->writeElement('description', null);
$XMLWriter->writeElement('profile_image_url', $avatar);
if ($profile['url']) $XMLWriter->writeElement('url', $profile['url']);
else $XMLWriter->writeElement('url', null);
$XMLWriter->writeElement('protected', (bool)$profile['private']);
$XMLWriter->writeElement('followers_count', $db->countFollowers($profile['ID']));
$XMLWriter->writeElement('created_at', date('D M j G:i:s O Y', $profile['since']));
$XMLWriter->writeElement('favourites_count', $db->countNotes('favorites', $profile['user_id']));
$XMLWriter->writeElement('statuses_count', $db->countNotes('archive', $profile['user_id']));
$XMLWriter->writeElement('friends_count', $db->countFollowing($profile['user_id']));
$XMLWriter->writeElement('following', $db->checkFollowing($_USER['ID'], $profile['user_id']));
$XMLWriter->writeElement('utc_offset', null); //TODO
$XMLWriter->writeElement('time_zone', null); //TODO
$XMLWriter->writeElement('profile_background_image_url', $profile['background']);
$XMLWriter->writeElement('profile_background_tile', null); //TODO
$XMLWriter->writeElement('notifications', null); //TODO??
$XMLWriter->writeElement('verified', false); //TODO???
if ($type == 'users') {
$result = $db->getNotes('archive', getStart(1), 1, $_USER['ID'], false);
$noteArray = $db->getNoteCombined($result[0]);
$XMLWriter->startElement('status');
$XMLWriter->writeElement('created_at', date('D M j G:i:s O Y', $noteArray['timestamp']));
$XMLWriter->writeElement('id', $noteArray['ID']);
$XMLWriter->writeElement('text', stripslashes($noteArray['note']));
$XMLWriter->writeElement('source', $noteArray['from']);
$XMLWriter->writeElement('truncated', 'false');
if ($noteArray['replying']) $XMLWriter->writeElement('in_reply_to_status_id', $noteArray['replying']);
else $XMLWriter->writeElement('in_reply_to_status_id', null);
if ($noteArray['replying']) $XMLWriter->writeElement('in_reply_to_user_id', $noteArray['reply_user']);
else $XMLWriter->writeElement('in_reply_to_user_id', null);
$XMLWriter->writeElement('favorited', (bool)$noteArray['favorite']);
if ($noteArray['replying']) $XMLWriter->writeElement('in_reply_to_screen_name', $noteArray['to']);
else $XMLWriter->writeElement('in_reply_to_screen_name', null);
$XMLWriter->endElement(); //closing
}
$XMLWriter->endElement(); //closing
}
$XMLWriter->endElement();
}
}
if ($type == 'notes') {
if (count($result) != 1) $XMLWriter->endElement();
}
elseif ($type != 'notes') $XMLWriter->endElement();
$XMLWriter->endDocument();
header('Content-Type: text/xml charset=UTF-8');
$XMLWriter->flush();
}
function createJSON($type, $result) {
global $db;
global $_USER;
if ($type != 'acc_verif' && ($type != 'notess')) $final = '[';
$count = 0;
foreach ($result as $note) {
$count = $count + 1;
if ($type == 'ids') $final .= $note['ID'];
else {
if ($type != 'users') {
if ($type == 'notess') $noteArray = $db->getNoteCombined($note);
else $noteArray = $db->getNoteCombined($note['id']);
$favorite = $db->checkFavorite($_USER['ID'], $noteArray['ID']);
$note = trim($noteArray['note']);
if ($type == 'notes' || ($type == 'notess')) $id_to_check = array('user'=>$noteArray['user_id']);
elseif ($type == 'direct_messages') $id_to_check = array('sender'=>$noteArray['user_id'], 'recipient'=>$db->getIdByUser($noteArray['to']));
$json = array(
'id' => $noteArray['ID'],
'created_at' => date('D M j G:i:s O Y', $noteArray['timestamp']),
'text' => stripslashes($note)
);
if ($type == 'direct_messages') {
$json['sender_id'] = $db->getIdByUser($noteArray['to']);
$json['recipient_id'] = $noteArray['user_id'];
$json['sender_screen_name'] = $noteArray['username'];
$json['recipient_screen_name'] = $noteArray['to'];
} elseif ($type == 'notes') {
$json['source'] = $noteArray['from'];
$json['truncated'] = false;
if ($noteArray['replying']) $json['in_reply_to_status_id'] = $noteArray['replying'];
else $json['in_reply_to_status_id'] = null;
if ($noteArray['replying']) $json['in_reply_to_user_id'] = $noteArray['reply_user'];
else $json['in_reply_to_user_id'] = null;
$json['favorited'] = (bool)$noteArray['favorite'];
if ($noteArray['replying']) $json['in_reply_to_screen_name'] = $noteArray['to'];
else $json['in_reply_to_screen_name'] = null;
}
}
if ($type == 'users' || ($type == 'acc_verif')) $id_to_check = array('user'=>$note);
foreach ($id_to_check as $key => $userto) {
$profile = $db->getUserInfo($userto);
//if the user has no avatar, the default avatar url is returned
if (!$profile['avatar']) $avatar = BASE.'static/img/avatar/default_note.png'; else $avatar = get_avatar_url($profile['username'], $profile['avatar'], 'note');
$array = array();
$array['id'] = $profile['userto'];
if ($profile['realname']) $array['name'] = $profile['realname'];
else $array['name'] = $profile['username'];
$array['screen_name'] = $profile['username'];
if ($profile['location']) $array['location'] = $profile['location'];
else $array['location'] = null;
if ($profile['bio']) $array['description'] = $profile['bio'];
else $array['description'] = null;
$array['profile_image_url'] = $avatar;
if ($profile['url']) $array['url'] = $profile['url'];
else $array['url'] = null;
$array['protected'] = (bool)$profile['private'];
$array['followers_count'] = $db->countFollowers($profile['ID']);
$array['created_at'] = date('D M j G:i:s O Y', $profile['since']);
$array['favourites_count'] = $db->countNotes('favorites', $profile['user_id']);
$array['statuses_count'] = $db->countNotes('archive', $profile['user_id']);
$array['friends_count'] = $db->countFollowing($profile['user_id']);
$array['following'] = $db->checkFollowing($_USER['ID'], $profile['user_id']);
$array['utc_offset'] = null; //TODO
$array['time_zone'] = null; //TODO
$array['profile_background_image_url'] = $profile['background'];
$array['profile_background_tile'] = null; //TODO
$array['notifications'] = null; //TODO??
$array['verified'] = false; //TODO???
if ($type == 'users') {
$result = $db->getNotes('archive', getStart(1), 1, $_USER['ID'], false);
$noteArray = $db->getNoteCombined($result[0]);
$userArray = array();
$userArray['created_at'] = date('D M j G:i:s O Y', $noteArray['timestamp']);
$userArray['id'] = $noteArray['ID'];
$userArray['source'] = $noteArray['from'];
$userArray['truncated'] = 'false';
if ($noteArray['replying']) $userArray['in_reply_to_status_id'] = $noteArray['replying'];
else $userArray['in_reply_to_status_id'] = null;
if ($noteArray['replying']) $userArray['in_reply_to_user_id'] = $noteArray['reply_user'];
else $userArray['in_reply_to_user_id'] = null;
$userArray['favorited'] = (bool)$noteArray['favorite'];
if ($noteArray['replying']) $userArray['in_reply_to_screen_name'] = $noteArray['to'];
else $userArray['in_reply_to_screen_name'] = null;
$array['status'] = $userArray;
}
$json[$key] = $array;
}
}
if ($type != 'ids') $final .= json_encode($json);
if ($count < count($result)) $final .= ',';
}
header('Content-Type: text/javascript; charset=utf-8');
echo $final;
if ($type != 'acc_verif' && ($type != 'notess')) echo ']';
}
function createRSS($result, $title, $desc, $link) {
global $db;
$XMLWriter = new XMLWriter();
$XMLWriter->openURI('php://output');
$XMLWriter->startDocument('1.0', 'UTF-8');
$XMLWriter->setIndent(4);
$XMLWriter->startElement('rss');
$XMLWriter->writeAttribute('version', '2.0');
$XMLWriter->writeAttribute('xmlns:atom', 'http://www.w3.org/2005/Atom');
$XMLWriter->writeAttribute('xmlns:content', 'http://purl.org/rss/1.0/modules/content/');
$XMLWriter->writeAttribute('xmlns:wfw', 'http://wellformedweb.org/CommentAPI/');
$XMLWriter->writeAttribute('xmlns:dc', 'http://purl.org/dc/elements/1.1/');
$XMLWriter->startElement('channel');
$XMLWriter->startElement('title');
$XMLWriter->writeCData(NAME.' '.SEPARATOR.' '.$title);
$XMLWriter->endElement();
$XMLWriter->startElement('description');
$XMLWriter->writeCData($desc);
$XMLWriter->endElement();
$XMLWriter->writeElement('link', BASE.$link);
$XMLWriter->startElement('atom:link');
$XMLWriter->writeAttribute('href', BASE.$link);
$XMLWriter->writeAttribute('rel', 'self');
$XMLWriter->writeAttribute('type', 'application/rss+xml');
$XMLWriter->endElement();
$XMLWriter->writeElement('generator', 'Jisko');
$XMLWriter->writeElement('ttl', 10);
foreach ($result as $note) {
if ($note['type'] == 'personal') continue;
$array = $db->getNoteCombined($note['id']);
$XMLWriter->startElement('item');
$XMLWriter->startElement('title');
$XMLWriter->writeCData(stripslashes($array['username'].': '.$array['note']));
$XMLWriter->endElement();
$XMLWriter->startElement('description');
$XMLWriter->writeCData(utf8_htmlentities(stripslashes($array['username'].': '.$array['note'])));
$XMLWriter->endElement();
$XMLWriter->writeElement('pubDate', date('r', intval($array['timestamp'])));
$XMLWriter->writeElement('link', stripslashes(BASE.$array['username'].'/'.$array['ID']));
$XMLWriter->writeElement('guid', stripslashes(BASE.$array['username'].'/'.$array['ID']));
$XMLWriter->writeElement('dc:creator', stripslashes($array['username']));
$XMLWriter->startElement('source');
$XMLWriter->writeAttribute('url', BASE.'rss/profile?user='.urlencode($array['username']));
$XMLWriter->text($array['username'].'\'s notes feed');
$XMLWriter->endElement();
$XMLWriter->endElement();
}
$XMLWriter->endElement();
$XMLWriter->endElement();
header('Content-Type: application/rss+xml;');
$XMLWriter->flush();
}
/* HTMLENTITIES */
function utf8_htmlentities($input) {
return stripslashes(htmlentities($input, ENT_QUOTES, 'UTF-8'));
}
/*
HTML2ASCII
Taken from "CommonTools" by Michael Tomason
function html2ascii($string) {
$string = strtr($string,array_flip(get_html_translation_table(HTML_ENTITIES))); // decode named entities
$string = preg_replace('//e','chr(\\1)',$string); // decode numbered entities
$string = strip_tags($string); // remove any remaining tags
return $string;
}*/
/* PAGINATION (DIGG-STYLE PAGINATION BY STRANGER STUDIOS) */
function getPaginationString($targetStart, $totalitems, $page, $limit = NOTES_PER_PAGE, $adjacents = 1, $pagestring = "?page=", $reduced = false) {
if (!$page) $page = 1;
$prev = $page - 1; //previous page is page - 1
$next = $page + 1; //next page is page + 1
if ($reduced == false) $lastpage = ceil($totalitems / $limit); //lastpage is = total items / items per page, rounded up.
else $lastpage = $page + 2;
$lpm1 = $lastpage - 1; //last page minus 1
$pagination = "";
if($lastpage > 1)
{
$pagination .= "
\n";
}
return $pagination;
}
/* MISC */
function fork($script) {
$sock = fsockopen(parse_url(BASE, PHP_URL_HOST), $_SERVER['SERVER_PORT'], $errno, $errstr, 1 );
$base = parse_url(BASE, PHP_URL_PATH);
$line = "GET {$base}{$script} HTTP/1.0\r\n" . "Host: {$_SERVER['HTTP_HOST']}\r\n\r\n";
if ($sock) {
fputs($sock, $line);
return true;
}
return false;
}
function deflang($lang) {
if(empty($lang)) return DEFAULT_LANG;
return $lang;
}
/* TWITTER FUNCTIONS */
import('twitter.class');
function setUpdate($every = 120) {
global $_USER;
global $db;
$userID = $_USER['ID'];
$diff = intval(time() - (int) $_USER['last_twitter_update']);
if($diff >= $every) {
$script = 'cron/update?user='.$userID;
fork($script);
$db->updateLastTwitter($userID);
}
}
/*function postTwitter($note, $user) {
global $db;
}*/
function updateTimeline ($userID) {
global $db;
global $USER;
$USER = $db->getUserInfo(intval($userID));
if(!$USER) return;
if(empty($USER['twitter_username']) or empty($USER['twitter_password'])) return;
if(empty($USER['twitter_key'])) {
$USER['twitter_key'] = twitter_update_key($USER);
}
$timeline = twitter_all($USER['twitter_key']);
foreach ($timeline as $id => $update){
if(eregi(NAME, $update['from'])) continue;
$hash = md5($id.'_'.$userID);
$serial = array('twitid' => $id, 'username' => $update['username'], 'avatar' => $update['avatar']);
$serial = mysql_real_escape_string(serialize($serial));
$db->newTwit($userID, $update['status'], $hash, $serial, $update['timestamp']);
}
$timeline_replies = twitter_all_replies($USER['twitter_key']);
foreach ($timeline_replies as $id => $update){
if(eregi(NAME, $update['from'])) continue;
$hash = md5($id.'_'.$userID);
$serial = array('twitid' => $id, 'username' => $update['username'], 'avatar' => $update['avatar']);
$serial = mysql_real_escape_string(serialize($serial));
$db->newTwit($userID, $update['status'], $hash, $serial, $update['timestamp'], 'twitter_reply');
}
}
/*function removeDir($dir, $DeleteMe) {
if(!$dh = @opendir($dir)) return;
while (false !== ($obj = readdir($dh))) {
if($obj=='.' || $obj=='..') continue;
if (!@unlink($dir.'/'.$obj)) removeDir($dir.'/'.$obj, true);
}
closedir($dh);
if ($DeleteMe){
@rmdir($dir);
}
}*/
function updateLastNote() {
global $_USER;
global $db;
$_USER['last_note'] = time();
$db->updateLastNote($_USER['ID']);
}
function twitter_update_key ($USER) {
global $db;
$username = $USER['twitter_username'];
$password = base64_decode($USER['twitter_password']);
if(!$username or !$password) return false;
$key = twitter::login($username, $password);
$db->updateTwitterKey($USER['ID'], $key);
$USER['twitter_key'] = $key;
return $key;
}
function twitter_all ($auth, $retries = 0) {
global $USER;
$pages = array(1,2);
$all = array();
foreach ($pages as $page) {
$tl = twitter::notes($auth,false,$page);
if($tl == 'ko' && $retries == 0) {
$auth = twitter_update_key($USER);
return twitter_all($auth, 1);
} else {
foreach ($tl as $id => $arr) {
$all[$id] = $arr;
}
sleep(1);
}
}
krsort($all, SORT_NUMERIC);
return $all;
}
function twitter_all_replies ($auth, $retries = 0) {
global $USER;
$pages = array(1,2);
$all = array();
foreach ($pages as $page) {
$tl = twitter::notes($auth,true,$page);
if($tl == 'ko' && $retries == 0) {
$auth = twitter_update_key($USER);
return twitter_all_replies($auth, 1);
} else {
foreach ($tl as $id => $arr) {
$all[$id] = $arr;
}
sleep(1);
}
}
krsort($all, SORT_NUMERIC);
return $all;
}
function shorter_url($url, $service) {
global $globals;
$parser = parse_url($url[0]);
if ($parser['host'] == $service) return $url[0];
else {
switch ($service) {
case 'tinyurl.com':
$ch = curl_init("http://tinyurl.com/api-create.php?url=".$url[0]);
break;
case '3.ly':
if (defined('THREELY_APICODE')) {
if (strlen(THREELY_APICODE)) $ch = curl_init("http://3.ly/?api=".THREELY_APICODE."&u=".urlencode($url[0]));
else return $url[0];
}
else return $url[0];
break;
case 'ves.cat':
$ch = curl_init("http://ves.cat/?url=".urlencode($url[0])."&format=json");
break;
case 'is.gd':
$ch = curl_init("http://is.gd/api.php?longurl=".urlencode($url[0]));
break;
case 'pic.gd':
$ch = curl_init("http://pic.gd/?module=ShortURL&file=Add&url=".urlencode($url[0])."&mode=API");
break;
case 'bit.ly':
if (defined('BITLY_LOGIN') && defined('BITLY_APICODE')) {
if (strlen(BITLY_LOGIN) && strlen(BITLY_APICODE)) $ch = curl_init("http://api.bit.ly/shorten?version=2.0.1&history=1&longUrl=".urlencode($url[0])."&login=".BITLY_LOGIN."&apiKey=".BITLY_APICODE);
else return $url[0];
}
else return $url[0];
break;
case 'j.mp':
if (defined('BITLY_LOGIN') && defined('BITLY_APICODE')) {
if (strlen(BITLY_LOGIN) && strlen(BITLY_APICODE)) $ch = curl_init("http://api.j.mp/shorten?version=2.0.1&history=1&longUrl=".urlencode($url[0])."&login=".BITLY_LOGIN."&apiKey=".BITLY_APICODE);
else return $url[0];
}
else return $url[0];
break;
case 'urlal.com':
$ch = curl_init("http://urlal.com/?u=".urlencode($url[0])."&o=j");
break;
case 'u.nu':
$ch = curl_init("http://u.nu/unu-api-simple?url=".urlencode($url[0]));
break;
case 'tinyarro':
$ch = curl_init("http://tinyarro.ws/api-create.php?utfpure=1&url=".urlencode($url[0]));
break;
case 'xn--ogi.ws':
case 'xn--vgi.ws':
case 'xn--3fi.ws':
case 'xn--egi.ws':
case 'xn--9gi.ws':
case 'xn--5gi.ws':
case 'xn--1ci.ws':
case 'xn--odi.ws':
case 'xn--rei.ws':
case 'xn--cwg.ws':
case 'xn--bih.ws':
case 'xn--fwg.ws':
case 'wn--l3h.ws':
case 'ta.gd':
$ch = curl_init("http://tinyarro.ws/api-create.php?utfpure=1&host=".$service.'&url='.$url[0]);
break;
case 'tr.im':
$ch = curl_init("http://api.tr.im/v1/trim_simple?url=".urlencode($url[0]));
break;
case 'wipi.es':
$ch = curl_init("http://wipi.es/create.php?url=".urlencode($url[0]));
break;
case 'xrl.us':
$ch = curl_init("http://metamark.net/api/rest/simple?long_url=".urlencode($url[0]));
break;
case 'cort.as':
$ch = curl_init("http://www.soitu.es/cortas/encode.pl?u=".urlencode($url[0])."&r=json");
break;
default:
return $url[0];
break;
}
}
if ($ch) {
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
$short_url = curl_exec($ch);
curl_close($ch);
if (empty($short_url)) return $url[0];
else {
switch ($service) {
case 'ves.cat':
$json = json_decode($short_url);
if ($json->status != 'Ok') return $url[0];
else return $json->link;
break;
case 'bit.ly':
case 'j.mp':
$json = json_decode($short_url, true);
if ($json['statusCode'] == 'ERROR') return $url[0];
else return $json['results'][$url[0]]['shortUrl'];
case 'urlal.com':
$json = json_decode($short_url);
if ($json->Status != 0) return $url[0];
else return $json->Message;
break;
case 'cort.as':
$json = json_decode($short_url);
if ($json['status'] != 'ok') return $url[0];
else return $json['urlCortas'];
case 'xn--ogi.ws':
case 'xn--vgi.ws':
case 'xn--3fi.ws':
case 'xn--egi.ws':
case 'xn--9gi.ws':
case 'xn--5gi.ws':
case 'xn--1ci.ws':
case 'xn--odi.ws':
case 'xn--rei.ws':
case 'xn--cwg.ws':
case 'xn--bih.ws':
case 'xn--fwg.ws':
case 'wn--l3h.ws':
case 'ta.gd':
case 'tinyarro':
return $short_url;
break;
case 'u.nu':
case 'tr.im':
case 'is.gd':
case 'pic.gd':
case 'wipi.es':
case 'xrl.us':
case '3.ly':
case 'tinyurl.com':
default:
if (filter_var(trim($short_url), FILTER_VALIDATE_URL)) return $short_url;
else return $url[0];
break;
}
}
}
}
?>