giobi
2/27/2016 - 3:02 PM

Utility php - librerie brevimano di vario tipo

Utility php - librerie brevimano di vario tipo

<?
class fb {
	function __construct() {
		extract(g('vendors')['facebook']);
		$this->fb = new Facebook\Facebook([
			'app_id' => $app,
			'app_secret' => $secret,
			'default_graph_version' => 'v2.2'
		]);

		$this->help = $this->fb->getRedirectLoginHelper();
		return $this;
	}
	
	function code2token() {
		try { $this->token = $this->help->getAccessToken(); }
		catch (Facebook\Exceptions\FacebookResponseException $e) {
			$this->error = $e->getMessage();
		} catch (Facebook\Exceptions\FacebookSDKException $e) {
			$this->error = $e->getMessage();
		}
		//$this->token->extend();
		
		//if (!$this->token) return $this->error('Autenticazione con facebook non riuscita');
		$this->fb->setDefaultAccessToken($this->token);
		return $this;
	}
	
	function code2user() {
		//if (!$this->code2token()->code) return $this->error($this->message);
		
		$this->code2token();
		
		$this->get('/me?fields=email,id,first_name,last_name,picture,locale,gender,timezone');
		$this->data = [
			'email' => $this->r->getField('email'),
			'fbid' => $this->r->getField('id'),
			'name' => $this->r->getField('first_name'),
			'surname' => $this->r->getField('last_name'),
			'fblocale' => $this->r->getField('locale'),
			'fbgender' => $this->r->getField('gender'),
			'fbtimezone' => $this->r->getField('timezone'),
			'fbpicture' => json_decode($this->r->getField('picture'))->url,
			'fbtoken' => $this->token->getValue(),
		];
		return $this;
	}
	
	
	function get($query = '/me?fields=email,id,name') {

		$this->r = $this->fb->get($query)->getGraphUser();
		$this->data = $this->r;
		return $this;
	}
	
	
	function loginurl($url = '/core/view/fbhub.php') {
		$fbh = $this->fb->getRedirectLoginHelper();
		$permissions = ['email', 'public_profile', 'user_friends'];
		$fburl = $fbh->getLoginUrl('http://'.$_SERVER['SERVER_NAME'].$url, $permissions);
		$fburl = htmlspecialchars($fburl);
		$this->data = $fburl;
		return $this;
	}
		
}
	


class curl {
	
	static function post($url, $post) {
		$ch = curl_init($url);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		$result = curl_exec($ch);
		curl_close($ch);

		return $result;
	}
}

class telegram {
	static function send($t) {
		extract(g('vendors')['telegram']);
		//p(g('vendors'));
		$url = 'https://api.telegram.org/bot'.$bot.'/sendMessage';
		curl::post($url, array('chat_id' => $me, 'text' => $t));
		
	}
}
//telegram::send('ciao');
register_shutdown_function('fatal');

function fatal() {
  extract(error_get_last());

  if ($type == 1) {
		$site = $_SERVER['HTTP_HOST'];
		$url = 'url: '.$_SERVER['REQUEST_URI'];
		$file = str_replace($_SERVER['DOCUMENT_ROOT'], '', $file);
    telegram::send("$site\n$file:$line\n$message\n$url", 'errors');
  }
}