Send and receive messages via (libpurple) messenger protocols - php

I had an idea that would require me be able to send and receive messages via the standard messenger protocols such as msn, icq, aim, skype, etc...
I am currently only familiar with PHP and Python and would thus enjoy a library which I can access from said languages. I have found phurple (http://sourceforge.net/projects/phurple/) for php and python-purple (http://developer.pidgin.im/wiki/PythonHowTo) which don't seem to be to up to date. What would you guys suggest to do? My goal will be to write a webapplication in a distant way like meebo.com
The answer should include a tutorial or example implementation and a decent documentations.. the pidgin.im doesn't really have a useful tutorial..
alternativly you can also just tell me different kinds of implementations, so that I would build my own class out of an existing icq, aim, msn etc implementation.
An example of how to connect to an account (login) and then sending one message would be the ultimate help!
Come one guys :)

Here is how to connect to the Pidgin DBus server.
#!/usr/bin/env python
import dbus
bus = dbus.SessionBus()
if "im.pidgin.purple.PurpleService" in bus.list_names():
purple = bus.get_object("im.pidgin.purple.PurpleService",
"/im/pidgin/purple/PurpleObject",
"im.pidgin.purple.PurpleInterface")
print "Connected to the pidgin DBus."
for conv in purple.PurpleGetIms():
purple.PurpleConvImSend(purple.PurpleConvIm(conv), "Ignore this message.")
else:
print "Could not find piding DBus service, make sure Pidgin is running."
Don't know if you have seen this, but here is the official python DBus tutorial: link.
EDIT: Re-adding link to the pidgin dev wiki. It teaches you everything I posted above,
just scroll further down the page. http://developer.pidgin.im/wiki/PythonHowTo

A good bet would be to go through the DBus interface: Pidgin (purple) fully supports it and the DBus interface library for Python is quite stable.

If you decompress the file from phurple you get some example like this:
<?php
if(!extension_loaded('phurple')) {
dl('phurple.' . PHP_SHLIB_SUFFIX);
}
class CustomPhurpleClient extends PhurpleClient {
private $someVar;
protected function initInternal() {
$this->someVar = "Hello World";
}
protected function writeIM($conversation, $buddy, $message, $flags, $time) {
if(PhurpleClient::MESSAGE_RECV == $flags) {
printf( "(%s) %s %s: %s\n",
$conversation->getName() ? $conversation->getName() : $buddy->getName(),
date("H:i:s", $time),
is_object($buddy) ? $buddy->getAlias() : $buddy,
$message
);
}
}
protected function onSignedOn($connection) {
print $this->justForFun($this->someVar);
}
public function justForFun($param) {
return "just for fun, the param is: $param";
}
}
// end Class CustomPhurpleClient
// Example Code Below:
try {
$user_dir = "/tmp/phphurple-test";
if(!file_exists($user_dir) || !is_dir($user_dir)) {
mkdir($user_dir);
}
PhurpleClient::setUserDir($user_dir);
PhurpleClient::setDebug(true);
PhurpleClient::setUiId("TestUI");
$client = CustomPhurpleClient::getInstance();
$client->addAccount("msn://nick#hotmail.com:password#messenger.hotmail.com:1863");
$client->connect();
$client->runLoop();
} catch (Exception $e) {
echo "[Phurple]: " . $e->getMessage() . "\n";
die();
}
?>

I use WebIcqLite: ICQ messages sender for the ICQ protocol. It works and the class is easy to understand. I don't know about other protocols, though. What's wrong with the Phurple library?

Related

PHP Onvif - GetSnapshotUri

I am using an opensource PHP library to communicate to a Onvif capable IP camera.
There was one function missing in the library, GetSnapshotUri which returns an URL where one can get a snapshot of the main stream.
Here is the source: http://pastebin.com/ekJa4D2h
Here is main page:
<?php
require 'class.ponvif.php';
$onvif = new Ponvif();
$onvif->setUsername('admin');
$onvif->setPassword('admin');
$onvif->setIPAddress('192.168.1.100');
try
{
$onvif->initialize();
$sources = $onvif->getSources();
$profileToken = $sources[0][0]['profiletoken'];
$uri = $onvif->media_GetSnapshotUri($profileToken);
}
catch(Exception $e)
{
print $e;
}
?>
For some reason, the isFault function is true and i have no clue why.
The XML is valid, i checked it with Wireshark.
Does anybody have another camera to this if this works?
I have tried with 2 different camera's, Grandstream and Hikvision.
Also with ONVIF Device Manager v2.2.250 everything works as it should.
I know this is a long shot, but i have absolutely no clue.
fix is here: http://pastebin.com/ryqxFjdR
mediaurl instead of getsnapshoturl in the function.

How to integrate CakePHP 3 application with Firebase

Having read documentations and tutorials for hours, I ended up in more confusion after all. So, here I'm asking for your help/tips and I'd really appreciate any effort helps me take a step further. Sorry for any mistakes I might possibly make, I'm quite newbie on this topic, so to say.
I'm building a web application, actually a single web page, that will start running as soon as it receives a GET/POST request from an external source. So, the page will be updated asynchronously if any request is received, for which I thought of using AngularJS and AngularFire on the front-end later on. I know there are many other ways and probably much simpler too, but I'm quite curious about how to integrate my CakePHP application with Firebase platform. So, let's stick with CakePHP + Firebase solutions for now.
So far, using the SDK Firebase PHP Client made much sense, however, I'm still confused about the files that needs to be manipulated. Since there are simply not many -I've found none so far- examples that use CakePHP3 Framework with Firebase, I'm stuck here and I'd really appreciate any help here. Firstly, this code is given in the link and I wonder how it works and what those vars and constants stand for.
const DEFAULT_URL = 'https://kidsplace.firebaseio.com/';
const DEFAULT_TOKEN = 'MqL0c8tKCtheLSYcygYNtGhU8Z2hULOFs9OKPdEp';
const DEFAULT_PATH = '/firebase/example';
$firebase = new \Firebase\FirebaseLib(DEFAULT_URL, DEFAULT_TOKEN);
// --- storing an array ---
$test = array(
"foo" => "bar",
"i_love" => "lamp",
"id" => 42
);
$dateTime = new DateTime();
$firebase->set(DEFAULT_PATH . '/' . $dateTime->format('c'), $test);
// --- storing a string ---
$firebase->set(DEFAULT_PATH . '/name/contact001', "John Doe");
// --- reading the stored string ---
$name = $firebase->get(DEFAULT_PATH . '/name/contact001');
And here is the main question, assuming that I have a test function on one of the end points of my application, let say www.example.com/visits/test, how do I make sure that my application is integrated to Firebase platform and any request sent to that end point is being listened continuously?
Here is how I solved it, just so you know. It's needed to add a test end point to the controller file that you're using. You can find your token using Project Settings in your project file. A quick example is below.
public function test()
{
$DEFAULT_URL = 'YOUR_URL';
$DEFAULT_TOKEN = 'YOUR_TOKEN';
$DEFAULT_PATH = '/';
$firebase = new \Firebase\FirebaseLib($DEFAULT_URL, $DEFAULT_TOKEN);
$lines = [
"I had a problem once",
"I used Java to solve it",
"Now, I have ProblemFactory",
];
foreach ($lines as $line) {
$foo = $firebase->push($DEFAULT_PATH."code/",$line);
echo "child_added";
sleep(0.1);
echo "<br> ";
}
Then, in order to capture the lines added on the front-end, you may use a code similar to the code below. You can easily reach the version that fits your code from your firebase console by clicking Add "firebase to your web app" button or sth like this.
<script src="https://www.gstatic.com/firebasejs/live/3.0/firebase.js"> </script>
<script>
// Initialize Firebase
var config = {
apiKey: "YOUR_API_KEY",
authDomain: "YOUR-DOMAIN",
databaseURL: "YOUR_URL",
storageBucket: "YOUR_BUCKET",
};
firebase.initializeApp(config);
var codeRef = firebase.database().ref('code/');
codeRef.on('child_added', function(data) {
console.log(data.val());
});
</script>
I'm not done with the app yet, however, this insight may help you deal with similar issues.

How to determine if the device is Mobile or Desktop?

I have read a lot of articles and looked for solutions to detect mobile devices. Actually a came across https://github.com/serbanghita/mobile-detect but it's a quite massive php class.
I actually want a very simple solution. I want to determine if the user's browser is Mobile/iPad/etc OR Desktop. So I want something like this:
<?php
require('detector.php');
if(isMobile() === true)
{
header('mobile.php');
exit();
}
else
{
header('desktop.php');
exit();
}
?>
A very simple solution is needed which I can place to any page without installing composer or any php framework.
How is this actually possible?
Have you actually tried to use the project you discovered. I'd say that server side mobile detection IS a huge task with plenty of detail checks to ensure the correct outcome.
And using this class is completely easy. From the example directory:
require_once '../Mobile_Detect.php';
$detect = new Mobile_Detect;
$deviceType = ($detect->isMobile() ? ($detect->isTablet() ? 'tablet' : 'phone') : 'computer');
Now you have a variable with one of three values: "tablet", "phone" or "computer", and you can react to this.
Please note that even if you are able to use this library without Composer, it will be updated regularly (as in "once every month"), because new devices get on the market and need to be detected. You will have to update this library at some point. Using Composer makes this very easy.
If you really don't want to include that class into your code, Mozilla indicates that it is "good enough" to search for the string "mobi" in the user agent.
<?php
if (stristr($_SERVER['HTTP_USER_AGENT'],'mobi')!==FALSE) {
echo 'mobile device detected';
}
?>
You can redirect the link with
and in controller you can check with
$keybord = app::get('keyboard')
if($keyboard == mobile ){
redirect ('mobile');
}else{
redirect ('desktop');
}
I've found this simple line to be pretty reliable and easy to implement.. without having the need to add one extra class.
if(strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'mobile') || strstr(strtolower($_SERVER['HTTP_USER_AGENT']), 'android')) {
echo "running on mobile";
}

phpwebsockets yii implementation

I am trying to implement websocket using php and as an extension for yii so that i can create a notification like feature for my web application
The code from below link is my starting point:
http://www.flynsarmy.com/2012/02/php-websocket-chat-application-2-0/
Its works perfectly in my local xampp..
I tried converting it as a Yii extension Steps that i followed..
I have put the class PHPWebSocket.php in the yii extension folder..
I created a controller websocket and a action startserver and a action index(for the chat interface to test out the example from the above link)
here is the code snippet
<?php
Yii::import("ext.websocket.PHPWebSocket");
class WebSocketController extends Controller {
public $layout = '//layouts/empty';
public function actionStartServer() {
set_time_limit(0);
function wsOnMessage($clientID, $message, $messageLength, $binary) {
global $Server;
$ip = long2ip($Server->wsClients[$clientID][6]);
// check if message length is 0
if ($messageLength == 0) {
$Server->wsClose($clientID);
return;
}
//The speaker is the only person in the room. Don't let them feel lonely.
if (sizeof($Server->wsClients) == 1)
$Server->wsSend($clientID, "There isn't anyone else in the room, but I'll still listen to you. --Your Trusty Server");
else
//Send the message to everyone but the person who said it
foreach ($Server->wsClients as $id => $client)
if ($id != $clientID)
$Server->wsSend($id, "Visitor $clientID ($ip) said \"$message\"");
}
// when a client connects
function wsOnOpen($clientID) {
global $Server;
$ip = long2ip($Server->wsClients[$clientID][6]);
$Server->log("$ip ($clientID) has connected.");
//Send a join notice to everyone but the person who joined
foreach ($Server->wsClients as $id => $client)
if ($id != $clientID)
$Server->wsSend($id, "Visitor $clientID ($ip) has joined the room.");
}
// when a client closes or lost connection
function wsOnClose($clientID, $status) {
global $Server;
$ip = long2ip($Server->wsClients[$clientID][6]);
$Server->log("$ip ($clientID) has disconnected.");
//Send a user left notice to everyone in the room
foreach ($Server->wsClients as $id => $client)
$Server->wsSend($id, "Visitor $clientID ($ip) has left the room.");
}
$Server = new PHPWebSocket();
$Server->bind('message', 'wsOnMessage');
$Server->bind('open', 'wsOnOpen');
$Server->bind('close', 'wsOnClose');
$Server->wsStartServer('127.0.0.1', 9300);
}
public function actionIndex() {
$this->render('index');
}
}
Is my approach to creating a websocket using php is correct or is it impossible to do so..
I want to implement the same using php only because i am preferred to use node.js or any other scripts
When using PHP with Apache, each request to PHP (usually) creates new process/thread. As web sockets are (somewhat) permanent connections, these PHP requests last for quite some time. Each process takes memory on the server. So, as I think this is possible, your server can probably just crash or reject requests if you would have many (or even not so many) users online at one time.
Node.js approach is different - each connection does not require separate process and so it can process many active connections at once.
You could use Node.js together with PHP connecting those two using queues or some other communication mechanism.
Just in case someone else stumbles upon this.
I was looking for a way to implement real-time-events to a Yii application myself.
In the comments above this (Yii) tutorial on HTML5 SSE is mentioned. As it seems to be very easy, it's just not enough if you need to support older browsers and mobile devices.
Browser support aka does it work in IE? Internet Explorer and Android browser (all versions) don't support Server-Sent Events out of
the box. Neither do older versions of Firefox (< 6), Chrome (< 6),
Safari (< 5), iOS Safari (< 4), or Opera (< 11).
Another solution is a fairly new Yii node socket extension. It's based on the node.js socket.io library and uses elephant.io to communicate with the server through php. Above all, the extension seems (I've only been using it for a month) to be well written. It has both Linux and Windows support, uses CLI to execute commands and it even has it's own database driver provided.
Other solutions are still welcome.

How do you enable customers to log in to your site using their Google account?

I just saw http://uservoice.com/login. It uses Google accounts, Myspace, Yahoo, OpenID and all to sign in customers into its site? Can I do that?
I mean, customers need not register to my site. They can just sign in with their accounts on the above sites.
If you've a solution, I'd prefer a PHP and MySQL based one.
See here: Google Login PHP Class.
Also be sure to refer to the Google Federated Login site for more info.
You may want to look at this too: https://rpxnow.com/ - it will only need integrating at the HTML/javascript level.
It's what http://uservoice.com/login appears to use.
You should look at the OpenID Enablded PHP library (http://www.openidenabled.com/php-openid/).
This should play pretty nicely with any LAMP installation without needing to use Zend.
Zend_OpenId from Zend Framework
Zend_OpenId is a Zend Framework component that provides a simple API for building OpenID-enabled sites and identity providers.
http://openidenabled.com/php-openid/
Uservoice users RPX http://rpxnow.com . You can easily use it with PHP, just https and parse the json or xml repsonse. You don't even need to change your database schema or store anything locally.
i think is good solution for you
step by step
1-download openid
2-create file called login.php like this (in same directory or change require_one to your own ) :
<?php
require_once 'openid.php';
$myopenid = new LightOpenID("your-domain.com");//no problem even if u can write http://localhost
if ($myopenid->mode) {
if ($myopenid->mode == 'cancel') {
echo "User has canceled authentication !";
} elseif($myopenid->validate()) {
$data = $myopenid->getAttributes();
$email = $data['contact/email'];
$first = $data['namePerson/first'];
echo "Identity : $openid->identity <br>";
echo "Email : $email <br>";
echo "First name : $first";
} else {
echo "The user has not logged in";
}
} else {
echo "Go to index page to log in.";
}
?>
3-next is about creating file called index.php:
<?php
require_once 'openid.php';
$openid = new LightOpenID("your-domain.com");//no problem even if u can write http://localhost
$openid->identity = 'https://www.google.com/accounts/o8/id';
$openid->required = array(
'namePerson/first',
'namePerson/last',
'contact/email',
);
$openid->returnUrl = 'your-domain.com/login.php'
?>
Login with Google
i almost forgot for log out u can kill session;

Categories