1, CURLOPT_CONNECTTIMEOUT => 10 ); // Domyśle opcje dla pobierania wpisów protected $defaults = array( 'avatars' => 'medium', 'limit' => 1, 'sort' => 'desc', 'comments' => 'false', ); public function __construct($username = '', $password = '', $includes = '') { $this->curl = curl_init(); curl_setopt_array($this->curl, $this->curloptions); $this->username($username); $this->password($password); if(!empty($includes)) $this->default['includes'] = $includes; } // Ewentualne wywołanie metody lub pobranie wartości z tablicy defaults public function __get($name) { $name = strtolower($name); if(array_key_exists($name, $this->defaults)) { return $this->defaults[$name]; } return call_user_func_array(array('SimpleFlaker', $name), array()); } // Ewentualne wywołanie metody z jednym parametrem public function __set($name, $value) { $this->defaults[$name] = $value; return $this; } // Jeśli dana funkcja nie istnieje, pobież zmienną o takiej nazwie public function __call($name, $args) { array_unshift($args, $name); return call_user_func_array(array('SimpleFlaker', 'entries'), $args); } // Ustawnia bieżącą nazwę użytkownika public function username($username = '') { if(!empty($username)) { $this->username = $username; $this->logged = $this->logged(); } return $this->username; } // Ustawia bieżące hasło public function password($password = '') { if(!empty($password)) { $this->password = $password; $this->logged = $this->logged(); } return $this->password; } // Sprawdza czy podana nazwa użytkownika i hasło są prawidłowe public function logged() { if(!empty($this->username) && !empty($this->password)) { $this->curloptions[CURLOPT_HTTPAUTH] = CURLAUTH_BASIC; $this->curloptions[CURLOPT_USERPWD] = $this->username . ':' . $this->password; try { $this->request(array('type' => 'auth')); } catch(Exception $e) { return false; } return true; } return false; } // Pobieranie wpisów i zwrócenie obiektu json public function request($get = '', $post = '') { $path = ''; foreach($get as $k => $v) $path .= '/' . $k . ':' . $v; $this->curl = curl_init($this->root . $path); curl_setopt_array($this->curl, $this->curloptions); if(!empty($post)) { curl_setopt($this->curl, CURLOPT_POST, 1); curl_setopt($this->curl, CURLOPT_POSTFIELDS, $post); } $data = curl_exec($this->curl); $code = intval(curl_getinfo($this->curl, CURLINFO_HTTP_CODE)); if($code >= 400) { throw new Exception($data, $code); } curl_close($this->curl); $this->curl = null; return json_decode($data); } public function entries($type, $more = '', $options = array()) { if($type != 'tag') $get = array('type' => $type); $post = array(); foreach($this->defaults as $k => $v) { if($v) $get[$k] = $v; } foreach($options as $k => $v) { if($v) $get[$k] = $v; } if($more) { $more = urlencode($more); if($type == 'tag') { $get['tag'] = $more; } else if($type == 'user' || $type == 'friends' || $type == 'tags') { $get['login'] = $more; } elseif($type == 'source') { $get['source'] = $more; } elseif($type == 'site' || $type == 'traker') { $get['url'] = $more; } elseif($type == 'show') { $get['entry_id'] = $more; } elseif($type == 'story') { $get['story'] = $more; } elseif($type == 'search') { $post['query'] = $more; } elseif($type == 'flakosfera') { $get['limit'] = $more; } } else { $get['tag'] = 'all'; } return $this->request($get, $post); } public function bookmark($action, $id) { $get = array( 'type' => 'bookmark', 'action' => $action, 'entry_id' => $id, 'login' => $this->username ); return $this->request($get); } public function following($username = '') { $get = array( 'type' => 'list', 'source' => 'following', 'login' => ($username?$username:$this->username) ); return $this->request($get); } public function followedby($username = '') { $get = array( 'type' => 'list', 'source' => 'followedby', 'login' => ($username?$username:$this->username) ); return $this->request($get); } public function bookmarks($username = '') { $get = array( 'type' => 'list', 'source' => 'bookmarks', 'login' => ($username?$username:$this->username) ); return $this->request($get); } public function send($message, $link = '') { $get = array('type' => 'submit'); $post = array('text' => $message); if(!empty($link)) { $post['link'] = $link; } return $this->request($get, $post); } public function submit($message, $link = '') { return $this->send($message, $link = ''); } } ?>