Permalink
Browse files

Adding support for authentication

 - Adding ssl support and user/password to curl class
 - Fixed problem Updating resource
  • Loading branch information...
speedmax committed May 7, 2009
1 parent c511598 commit b50f9f086cf6ee6ce3b78312b5fec9f9c752f765
Showing with 60 additions and 28 deletions.
  1. +16 −7 active_resource.php
  2. +10 −0 lib/curl.php
  3. +34 −21 test/active_resource_test.php
View
@@ -5,16 +5,15 @@
class ActiveResource {
protected $attributes;
public $site = 'http://localhost:3000';
+ public $user, $password;
public $timeout = 2000;
public $connection;
public $response;
- public $resources;
public $extension = 'xml';
function __construct($attributes = array()) {
$this->attributes = $attributes;
$this->connection = new Curl;
- $this->resources = pluralize(strtolower(get_class($this)));;
}
/* Class Methods */
@@ -101,7 +100,7 @@ function save() {
if (isset($this->id)) {
$this->connection->headers = array("Content-Type" => "application/xml");
- $this->connection->put(self::element_url($this->id, $class), $data);
+ $resp = $this->connection->put(self::element_url($this->id, $class), $data);
return true;
}
$this->connection->headers = array("Content-Type" => "application/xml");
@@ -142,8 +141,8 @@ static function find_single($scope, $class, $options = array()) {
throw new Exception("Object not found");
}
- $xml = simplexml_load_string($resp->body)->{strtolower($class)};
- return new $class((array) $xml);
+ $xml = simplexml_load_string($response->body)->xpath("//". strtolower($class));
+ return new $class((array) $xml[0]);
}
static function find_every($class, $options = array()) {
@@ -170,7 +169,14 @@ static function config($class) {
extract(get_class_vars($class));
if ($site[strlen($site) - 1] === '/')
$site = substr($site, 0, -1);
- return compact('site', 'timeout', 'extension');
+
+ $parts = parse_url($site);
+
+ if (isset($parts['user']) && isset($parts['password'])) {
+ $this->user = $parts['user'];
+ $this->password = $parts['password'];
+ }
+ return compact('user', 'password', 'site', 'timeout', 'extension');
}
static function collection_name($class = null) {
@@ -201,8 +207,11 @@ function request_body($params) {
$src = '';
$element = strtolower(get_class($this));
- foreach ($params as $k => $v)
+ foreach ($params as $k => $v) {
+ if ($k === 'updated_at'|| $k === 'created_at') continue;
+
$src .= "<{$k}>". utf8_encode($v) ."</{$k}>";
+ }
return "<{$element}>{$src}</{$element}>";
}
}
View
@@ -75,6 +75,16 @@ protected function request($method, $url, $vars = array())
curl_setopt($this->handle, CURLOPT_URL, $url);
curl_setopt($this->handle, CURLOPT_USERAGENT, $this->user_agent);
+ $parts = parse_url($url);
+
+ if(strpos($url, 'https') === 0)
+ curl_setopt($this->handle, CURLOPT_SSL_VERIFYPEER, false);
+
+ if(!empty($parts['user']) && !empty($parts['pass'])) {
+ curl_setopt($this->handle, CURLOPT_USERPWD, $parts['user'] . ":" . $parts['pass']);
+ $url = str_replace($parts['user'] . ":" . $parts['pass'].'@', '', $url);
+ }
+
# Format custom headers for this request and set CURL option
$headers = array();
foreach ($this->headers as $key => $value) {
@@ -1,55 +1,68 @@
<?php
-require 'simpletest/runner.php';
require '../active_resource.php';
-define('RESOURCE_URI', 'http://creagency-insurance.local');
+define('RESOURCE_URI', 'http://admin:CheeseBurger@localhost');
class Article extends ActiveResource {
var $site = RESOURCE_URI;
-
function __toString() {
return $this->title;
}
}
class Question extends ActiveResource {
var $site = RESOURCE_URI;
-
function __toString() {
return $this->text;
}
}
+# edit form
+if (!empty($_GET['action']) && $_GET['action'] == 'edit'):
+ $article = Article::find($_GET['article']);
-## Find all
-
-//
-// $p = Page::create(array(
-// 'title' => 'Hello there elton an title',
-// 'body' => 'article body'
-// ));
-//
-
+ if (!empty($_POST['article'])) {
+ $article->title = $_POST['article']['title'];
+ $article->body = $_POST['article']['body'];
+ $article->save();
+
+ header("Location: {$_SERVER["PHP_SELF"]}");
+ }
?>
+<form method="post">
+ <p>
+ <label>Title</label><br/>
+ <input name="article[title]" value="<?=$article->title?>">
+ </p>
+
+ <p>
+ <label>Body</label><br/>
+ <textarea name="article[body]" rows="20" cols="80"><?=$article->body?></textarea><br/>
+ </p>
+ <input type="submit" value="Update">
+</form>
+<? die;endif ?>
-<h1> All articles </h1>
-<? foreach( Article::find('all') as $article): ?>
+<? if (isset($_GET['article'])): ?>
+ <? $article = Article::find($_GET['article']) ?>
+ <h1><?= $article->title ?></h1>
+ <a href="<?=$_SERVER['REQUEST_URI']?>&action=edit">edit</a>
+ <p><?= $article->body ?>
- <li><a href=""><?= $article ?></a></li>
-
-<? endforeach ?>
+<? endif ?>
+<h1> All articles </h1>
+<? foreach( Article::find('all') as $article): ?>
+ <li><a href="<?= $_SERVER["PHP_SELF"]?>?article=<?=$article->id?>"><?= $article ?></a></li>
+<? endforeach ?>
<hr>
<h1> All Question</h1>
-
<? foreach( Question::find('all') as $question): ?>
-
<li><a href=""><?= $question ?></a></li>
-
<? endforeach ?>

0 comments on commit b50f9f0

Please sign in to comment.