Skip to content

Commit

Permalink
add namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
michield committed Jun 16, 2014
1 parent 26ed970 commit a203ec8
Show file tree
Hide file tree
Showing 15 changed files with 145 additions and 174 deletions.
40 changes: 21 additions & 19 deletions plugins/restapi/call.php
@@ -1,5 +1,7 @@
<?php

namespace phpListRestapi;

//No HTML-output, please!
ob_end_clean();

Expand All @@ -19,39 +21,39 @@

include 'doc/doc.php';

if (function_exists('api_request_log')) {
api_request_log();
}

//Check if this is called outside phpList auth, this should never occur!
if ( empty( $plugin->coderoot ) ){
phpList_RESTAPI_Response::outputErrorMessage( 'Not authorized! Please login with [login] and [password] as admin first!' );
Response::outputErrorMessage( 'Not authorized! Please login with [login] and [password] as admin first!' );
}

//If other than POST then assume documentation report
if ( strcmp( $_SERVER['REQUEST_METHOD'], "POST") ){

$doc = new phpList_RESTAPI_Doc();
$doc->addClass( 'phpList_RESTAPI_Actions' );
$doc->addClass( 'phpList_RESTAPI_Lists' );
$doc->addClass( 'phpList_RESTAPI_Users' );
$doc->addClass( 'phpList_RESTAPI_Templates' );
$doc->addClass( 'phpList_RESTAPI_Messages' );
if ( strcmp( $_SERVER['REQUEST_METHOD'], "POST") ) {
$doc = new \phpListRestapiDoc();
$doc->addClass( 'Actions' );
$doc->addClass( 'Lists' );
$doc->addClass( 'Users' );
$doc->addClass( 'Templates' );
$doc->addClass( 'Messages' );
$doc->output();

}

//Check if command is empty!
$cmd = $_REQUEST['cmd'];
$cmd = preg_replace('/\W/','',$cmd);
if ( empty($cmd) ){
phpList_RESTAPI_Response::outputMessage('OK! For action, please provide Post Param Key [cmd] !');
Response::outputMessage('OK! For action, please provide Post Param Key [cmd] !');
}

//Now bind the commands with static functions
if ( is_callable( array( 'phpList_RESTAPI_Lists', $cmd ) ) ) phpList_RESTAPI_Lists::$cmd();
if ( is_callable( array( 'phpList_RESTAPI_Actions', $cmd ) ) ) phpList_RESTAPI_Actions::$cmd();
if ( is_callable( array( 'phpList_RESTAPI_Users', $cmd ) ) ) phpList_RESTAPI_Users::$cmd();
if ( is_callable( array( 'phpList_RESTAPI_Templates', $cmd ) ) ) phpList_RESTAPI_Templates::$cmd();
if ( is_callable( array( 'phpList_RESTAPI_Messages', $cmd ) ) ) phpList_RESTAPI_Messages::$cmd();
if ( is_callable( array( 'phpListRestapi\Lists', $cmd ) ) ) Lists::$cmd();
if ( is_callable( array( 'phpListRestapi\Actions', $cmd ) ) ) Actions::$cmd();
if ( is_callable( array( 'phpListRestapi\Users', $cmd ) ) ) Users::$cmd();
if ( is_callable( array( 'phpListRestapi\Templates', $cmd ) ) ) Templates::$cmd();
if ( is_callable( array( 'phpListRestapi\Messages', $cmd ) ) ) Messages::$cmd();

//If no command found, return error message!
phpList_RESTAPI_Response::outputErrorMessage( 'No function for provided [cmd] found!' );

?>
Response::outputErrorMessage( 'No function for provided [cmd] found!' );
10 changes: 3 additions & 7 deletions plugins/restapi/doc/doc.php
@@ -1,7 +1,6 @@
<?php


class phpList_RESTAPI_Doc{
class phpListRestapiDoc {

private $classes;

Expand All @@ -11,7 +10,7 @@ function __construct()

function addClass( $classname ){

$this->classes[] = $classname;
$this->classes[] = "phpListRestapi\\$classname";

}

Expand All @@ -21,7 +20,7 @@ function output(){

foreach( $this->classes as $class ){

$reflect = new ReflectionClass( $class );
$reflect = new \ReflectionClass( $class );
$methods = $reflect->getMethods();
foreach( $methods as $method ){

Expand Down Expand Up @@ -117,6 +116,3 @@ function footer(){


}


?>
30 changes: 8 additions & 22 deletions plugins/restapi/includes/actions.php
@@ -1,6 +1,8 @@
<?php

class phpList_RESTAPI_Actions{
namespace phpListRestapi;

class Actions {

/**
* <p>Function to call for login.<p>
Expand All @@ -9,8 +11,8 @@ class phpList_RESTAPI_Actions{
* [*password] {string} the password
* </p>
*/
static function login(){
phpList_RESTAPI_Response::outputMessage( 'Welcome!' );
static function login() {
Response::outputMessage( 'Welcome!' );
}

/**
Expand All @@ -21,23 +23,10 @@ static function login(){
* [*password] {string} the password
*
*/
static function processQueue( ){
static function processQueue() {

$admin_id = $_SESSION["logindetails"]["id"];

//Get the password from db!
$db = phpList_RESTAPI_PDO::getConnection();

$sql = "SELECT * FROM " . $GLOBALS['table_prefix'] . "admin WHERE id = :id;";
$stmt = $db->prepare($sql);
$stmt->execute( array( ':id' => $admin_id ) );
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;

$login = $result[0]->loginname;
$password = $result[0]->password;

$url = phpList_RESTAPI_Common::apiUrl( $_SERVER['HTTP_HOST'] );
$url = Common::apiUrl( $_SERVER['HTTP_HOST'] );
$url = str_replace( 'page=call&pi=restapi', 'page=processqueue&login=' . $login . '&password=' . $password . '&ajax=1', $url );

//open connection
Expand All @@ -51,11 +40,8 @@ static function processQueue( ){

//ob_end_clean();

phpList_RESTAPI_Response::outputMessage( 'Queue is processed!' );
Response::outputMessage( 'Queue is processed!' );

}

}


?>
10 changes: 6 additions & 4 deletions plugins/restapi/includes/common.php
@@ -1,15 +1,17 @@
<?php

class phpList_RESTAPI_Common{
namespace phpListRestapi;

class Common {

static function select( $type, $sql, $single=false ){
$response = new phpList_RESTAPI_Response();
$response = new Response();
try {
$db = phpList_RESTAPI_PDO::getConnection();
$db = PDO::getConnection();
$stmt = $db->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
if ($single && is_array($result)) $result = $result[0];
if ($single && is_array($result) && isset($result[0])) $result = $result[0];
$response->setData($type, $result);
} catch( PDOException $e ) {
$response->setError( $e->getCode(), $e->getMessage() );
Expand Down
59 changes: 28 additions & 31 deletions plugins/restapi/includes/lists.php
@@ -1,10 +1,11 @@
<?php
namespace phpListRestapi;

/**
* Class phpList_RESTAPI_Lists
* Getting lists, adding and removing its users and messages
*/
class phpList_RESTAPI_Lists{
class Lists{

/**
* <p>Gets all lists in phpList as an array.</p>
Expand All @@ -15,7 +16,7 @@ class phpList_RESTAPI_Lists{
* </p>
*/
static function listsGet() {
phpList_RESTAPI_Common::select( 'Lists', "SELECT * FROM " . $GLOBALS['table_prefix'] . "list ORDER BY listorder;" );
Common::select( 'Lists', "SELECT * FROM " . $GLOBALS['table_prefix'] . "list ORDER BY listorder;" );
}

/**
Expand All @@ -28,7 +29,7 @@ static function listsGet() {
*/
static function listGet( $id=0 ) {
if ( $id==0 ) $id = $_REQUEST['id'];
phpList_RESTAPI_Common::select( 'List', "SELECT * FROM " . $GLOBALS['table_prefix'] . "list WHERE id = $id;", true );
Common::select( 'List', "SELECT * FROM " . $GLOBALS['table_prefix'] . "list WHERE id = $id;", true );
}

/**
Expand All @@ -48,7 +49,7 @@ static function listAdd(){

$sql = "INSERT INTO " . $GLOBALS['table_prefix'] . "list (name, description, listorder, prefix, rssfeed, active) VALUES (:name, :description, :listorder, :prefix, :rssfeed, :active);";
try {
$db = phpList_RESTAPI_PDO::getConnection();
$db = PDO::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("name", $_REQUEST['name']);
$stmt->bindParam("description", $_REQUEST['description']);
Expand All @@ -59,9 +60,9 @@ static function listAdd(){
$stmt->execute();
$id = $db->lastInsertId();
$db = null;
phpList_RESTAPI_Lists::listGet( $id );
Lists::listGet( $id );
} catch(PDOException $e) {
phpList_RESTAPI_Response::outputError($e);
Response::outputError($e);
}
die(0);
}
Expand All @@ -85,7 +86,7 @@ static function listUpdate(){
$sql = "UPDATE " . $GLOBALS['table_prefix'] . "list SET name=:name, description=:description, listorder=:listorder, prefix=:prefix, rssfeed=:rssfeed, active=:active WHERE id=:id;";

try {
$db = phpList_RESTAPI_PDO::getConnection();
$db = PDO::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $_REQUEST['id']);
$stmt->bindParam("name", $_REQUEST['name'] );
Expand All @@ -96,9 +97,9 @@ static function listUpdate(){
$stmt->bindParam("active", $_REQUEST['active'] );
$stmt->execute();
$db = null;
phpList_RESTAPI_Lists::listGet( $_REQUEST['id'] );
Lists::listGet( $_REQUEST['id'] );
} catch(PDOException $e) {
phpList_RESTAPI_Response::outputError($e);
Response::outputError($e);
}
die(0);
}
Expand All @@ -115,14 +116,14 @@ static function listDelete(){

$sql = "DELETE FROM " . $GLOBALS['table_prefix'] . "list WHERE id=:id;";
try {
$db = phpList_RESTAPI_PDO::getConnection();
$db = PDO::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("id", $_REQUEST['id']);
$stmt->execute();
$db = null;
phpList_RESTAPI_Response::outputDeleted( 'List', $_REQUEST['id'] );
Response::outputDeleted( 'List', $_REQUEST['id'] );
} catch(PDOException $e) {
phpList_RESTAPI_Response::outputError($e);
Response::outputError($e);
}
die(0);
}
Expand All @@ -136,18 +137,18 @@ static function listDelete(){
* </p>
*/
static function listsUser( $user_id=0 ) {
$response = new phpList_RESTAPI_Response();
$response = new Response();
if ( $user_id==0 ) $user_id = $_REQUEST['user_id'];
$sql = "SELECT * FROM " . $GLOBALS['table_prefix'] . "list WHERE id IN (SELECT listid FROM " . $GLOBALS['table_prefix'] . "listuser WHERE userid=" . $user_id . ") ORDER BY listorder;";
try {
$db = phpList_RESTAPI_PDO::getConnection();
$db = PDO::getConnection();
$stmt = $db->query($sql);
$result = $stmt->fetchAll(PDO::FETCH_OBJ);
$db = null;
$response->setData('Lists', $result);
$response->output();
} catch(PDOException $e) {
phpList_RESTAPI_Response::outputError($e);
Response::outputError($e);
}
die(0);
}
Expand All @@ -167,15 +168,15 @@ static function listUserAdd( $list_id=0, $user_id=0 ){
if ( $user_id==0 ) $user_id = $_REQUEST['user_id'];
$sql = "INSERT INTO " . $GLOBALS['table_prefix'] . "listuser (userid, listid, entered) VALUES (:user_id, :list_id, now());";
try {
$db = phpList_RESTAPI_PDO::getConnection();
$db = PDO::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("user_id", $user_id );
$stmt->bindParam("list_id", $list_id );
$stmt->execute();
$db = null;
phpList_RESTAPI_Lists::listsUser( $user_id );
Lists::listsUser( $user_id );
} catch(PDOException $e) {
phpList_RESTAPI_Response::outputError($e);
Response::outputError($e);
}
die(0);
}
Expand All @@ -194,15 +195,15 @@ static function listUserDelete( $list_id=0, $user_id=0 ){
if ( $user_id==0 ) $user_id = $_REQUEST['user_id'];
$sql = "DELETE FROM " . $GLOBALS['table_prefix'] . "listuser WHERE listid=:list_id AND userid=:user_id;";
try {
$db = phpList_RESTAPI_PDO::getConnection();
$db = PDO::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("user_id", $user_id );
$stmt->bindParam("list_id", $list_id );
$stmt->execute();
$db = null;
phpList_RESTAPI_Response::outputMessage( 'User ' . $user_id . ' is unassigned from list ' . $list_id );
Response::outputMessage( 'User ' . $user_id . ' is unassigned from list ' . $list_id );
} catch(PDOException $e) {
phpList_RESTAPI_Response::outputError($e);
Response::outputError($e);
}
die(0);
}
Expand All @@ -221,15 +222,15 @@ static function listMessageAdd( $list_id=0, $message_id=0 ){
if ( $message_id==0 ) $message_id = $_REQUEST['message_id'];
$sql = "INSERT INTO " . $GLOBALS['table_prefix'] . "listmessage (messageid, listid, entered) VALUES (:message_id, :list_id, now());";
try {
$db = phpList_RESTAPI_PDO::getConnection();
$db = PDO::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("message_id", $message_id );
$stmt->bindParam("list_id", $list_id );
$stmt->execute();
$db = null;
phpList_RESTAPI_Lists::listGet( $list_id );
Lists::listGet( $list_id );
} catch(PDOException $e) {
phpList_RESTAPI_Response::outputError($e);
Response::outputError($e);
}
die(0);
}
Expand All @@ -249,22 +250,18 @@ static function listMessageDelete( $list_id=0, $message_id=0 ){
if ( $message_id==0 ) $message_id = $_REQUEST['message_id'];
$sql = "DELETE FROM " . $GLOBALS['table_prefix'] . "listmessage WHERE listid=:list_id AND messageid=:message_id;";
try {
$db = phpList_RESTAPI_PDO::getConnection();
$db = PDO::getConnection();
$stmt = $db->prepare($sql);
$stmt->bindParam("message_id", $message_id );
$stmt->bindParam("list_id", $list_id );
$stmt->execute();
$db = null;
phpList_RESTAPI_Response::outputMessage( 'Message ' . $message_id . ' is unassigned from list ' . $list_id );
Response::outputMessage( 'Message ' . $message_id . ' is unassigned from list ' . $list_id );
} catch(PDOException $e) {
phpList_RESTAPI_Response::outputError($e);
Response::outputError($e);
}
die(0);
}


}



?>

0 comments on commit a203ec8

Please sign in to comment.