<commit_before><?php
/**
 * HUBzero CMS
 *
 * Copyright 2005-2015 Purdue University. All rights reserved.
 *
 * This file is part of: The HUBzero(R) Platform for Scientific Collaboration
 *
 * The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
 * software: you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * HUBzero is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * HUBzero is a registered trademark of Purdue University.
 *
 * @package   hubzero-cms
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright 2011 Purdue University. All rights reserved.
 * @license   http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
 */

namespace Components\Courses\Api\Controllers;

use Components\Courses\Tables\AssetAssociation;
use Components\Courses\Tables\Asset as AssetTbl;
use Components\Courses\Models\Asset;
use Components\Courses\Models\Assets\Handler;
use Request;
use Component;
use App;
use Date;
use Filesystem;

require_once __DIR__ . DS . 'base.php';
require_once dirname(dirname(__DIR__)) . DS . 'models' . DS . 'asset.php';
require_once dirname(dirname(__DIR__)) . DS . 'models' . DS . 'assets' . DS . 'handler.php';
require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'asset.php';
require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'asset.association.php';

/**
 * API controller for the course assets
 */
class Assetv1_0 extends base
{
	/**
	 * Gets the asset handlers for a given extension
	 *
	 * @apiMethod POST
	 * @apiUri    /courses/asset/handlers
	 * @apiParameter {
	 * 		"name":        "name",
	 * 		"description": "Name of file to be uploaded",
	 * 		"type":        "string",
	 * 		"required":    true,
	 * 		"default":     null
	 * }
	 * @return    void
	 */
	public function handlersTask()
	{
		// Get the incomming file name
		$name = Request::getCmd('name');

		// Get the file extension
		$exts = explode('.', $name);
		$ext  = strtolower(array_pop($exts));

		// Initiate our file handler
		$database     = App::get('db');
		$assetHandler = new Handler($database, $ext);

		// Get the handlers
		$handlers = $assetHandler->getHandlers();

		// Also check the PHP max post and upload values
		$max_upload = min((int)(ini_get('upload_max_filesize')), (int)(ini_get('post_max_size')));

		// Return message
		$this->send(['ext'=>$ext, 'handlers'=>$handlers, 'max_upload'=>$max_upload]);
	}

	/**
	 * Creates a new asset
	 *
	 * @apiMethod POST
	 * @apiUri    /courses/asset/new
	 * @apiParameter {
	 * 		"name":        "files",
	 * 		"description": "Files to upload",
	 * 		"type":        "array",
	 * 		"required":    false,
	 * 		"default":     null
	 * }
	 * @apiParameter {
	 * 		"name":        "type",
	 * 		"description": "Content type being created",
	 * 		"type":        "string",
	 * 		"required":    false,
	 * 		"default":     null
	 * }
	 * @apiParameter {
	 * 		"name":        "handler",
	 * 		"description": "The file handler to use",
	 * 		"type":        "string",
	 * 		"required":    false,
	 * 		"default":     null
	 * }
	 * @return    void
	 */
	public function newTask()
	{
		// Require authentication and authorization
		$this->authorizeOrFail();

		// Grab the incoming file (incoming type overrides files)
		if (isset($_FILES['files']) && !Request::getWord('type', false))
		{
			$file_name = $_FILES['files']['name'][0];
			$file_size = (int) $_FILES['files']['size'];

			// Get the extension
			$pathinfo = pathinfo($file_name);
			$ext      = $pathinfo['extension'];
		}
		elseif ($contentType = Request::getWord('type', false))
		{
			// @FIXME: having this here breaks the responder model idea
			// The content type handlers could respond to a function that assesses the incoming data?
			switch ($contentType)
			{
				case 'link':
					$ext = 'url';
					break;
				case 'object':
					$ext = 'object';
					break;
				case 'wiki':
					$ext = 'wiki';
					break;
			}
		}
		else
		{
			App::abort(500, 'No assets given');
		}

		// Initiate our file handler
		$database     = App::get('db');
		$assetHandler = new Handler($database, $ext);

		// Create the new asset
		$return = $assetHandler->doCreate(Request::getVar('handler', null));

		// Check for errors in response
		if (array_key_exists('error', $return))
		{
			App::abort(500, $return['error']);
		}

		// Return message
		$this->send(['assets'=>$return], 201);
	}

	/**
	 * Retrieves the asset edit page
	 *
	 * @apiMethod POST
	 * @apiUri    /courses/asset/edit
	 * @apiParameter {
	 * 		"name":        "id",
	 * 		"description": "ID of asset to edit",
	 * 		"type":        "integer",
	 * 		"required":    true,
	 * 		"default":     null
	 * }
	 * @return    void
	 */
	public function editTask()
	{
		// Require authentication and authorization
		$this->authorizeOrFail();

		// Make sure we have an asset id
		if (!$asset_id = Request::getInt('id', false))
		{
			App::abort(500, 'No asset id provided');
		}

		// Initiate our file handler
		$database     = App::get('db');
		$assetHandler = new Handler($database);

		// Edit the asset
		$return = $assetHandler->doEdit($asset_id);

		// Check for errors in response
		if (is_array($return) && array_key_exists('error', $return))
		{
			App::abort(500, $return['error']);
		}

		// Return message
		$this->send($return);
	}

	/**
	 * Previews an asset
	 *
	 * @apiMethod POST
	 * @apiUri    /courses/asset/preview
	 * @apiParameter {
	 * 		"name":        "id",
	 * 		"description": "ID of asset to preview",
	 * 		"type":        "integer",
	 * 		"required":    true,
	 * 		"default":     null
	 * }
	 * @return    void
	 */
	public function previewTask()
	{
		// Require authentication and authorization
		$this->authorizeOrFail();

		// Make sure we have an asset id
		if (!$asset_id = Request::getInt('id', false))
		{
			App::abort(500, 'No asset id provided');
		}

		// Initiate our file handler
		$database     = App::get('db');
		$assetHandler = new Handler($database);

		// Edit the asset
		$return = $assetHandler->preview($asset_id);

		// Check for errors in response
		if (is_array($return) && array_key_exists('error', $return))
		{
			App::abort(500, $return['error']);
		}

		// Return message
		$this->send($return);<commit_msg>[HUBZERO][#7871][COM_COURSES] throw 400s instead of 500s because it's not our fault<commit_after><?php
/**
 * HUBzero CMS
 *
 * Copyright 2005-2015 Purdue University. All rights reserved.
 *
 * This file is part of: The HUBzero(R) Platform for Scientific Collaboration
 *
 * The HUBzero(R) Platform for Scientific Collaboration (HUBzero) is free
 * software: you can redistribute it and/or modify it under the terms of
 * the GNU Lesser General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * HUBzero is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * HUBzero is a registered trademark of Purdue University.
 *
 * @package   hubzero-cms
 * @author    <NAME> <<EMAIL>>
 * @copyright Copyright 2011 Purdue University. All rights reserved.
 * @license   http://www.gnu.org/licenses/lgpl-3.0.html LGPLv3
 */

namespace Components\Courses\Api\Controllers;

use Components\Courses\Tables\AssetAssociation;
use Components\Courses\Tables\Asset as AssetTbl;
use Components\Courses\Models\Asset;
use Components\Courses\Models\Assets\Handler;
use Request;
use Component;
use App;
use Date;
use Filesystem;

require_once __DIR__ . DS . 'base.php';
require_once dirname(dirname(__DIR__)) . DS . 'models' . DS . 'asset.php';
require_once dirname(dirname(__DIR__)) . DS . 'models' . DS . 'assets' . DS . 'handler.php';
require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'asset.php';
require_once dirname(dirname(__DIR__)) . DS . 'tables' . DS . 'asset.association.php';

/**
 * API controller for the course assets
 */
class Assetv1_0 extends base
{
	/**
	 * Gets the asset handlers for a given extension
	 *
	 * @apiMethod POST
	 * @apiUri    /courses/asset/handlers
	 * @apiParameter {
	 * 		"name":        "name",
	 * 		"description": "Name of file to be uploaded",
	 * 		"type":        "string",
	 * 		"required":    true,
	 * 		"default":     null
	 * }
	 * @return    void
	 */
	public function handlersTask()
	{
		// Get the incomming file name
		$name = Request::getCmd('name');

		// Get the file extension
		$exts = explode('.', $name);
		$ext  = strtolower(array_pop($exts));

		// Initiate our file handler
		$database     = App::get('db');
		$assetHandler = new Handler($database, $ext);

		// Get the handlers
		$handlers = $assetHandler->getHandlers();

		// Also check the PHP max post and upload values
		$max_upload = min((int)(ini_get('upload_max_filesize')), (int)(ini_get('post_max_size')));

		// Return message
		$this->send(['ext'=>$ext, 'handlers'=>$handlers, 'max_upload'=>$max_upload]);
	}

	/**
	 * Creates a new asset
	 *
	 * @apiMethod POST
	 * @apiUri    /courses/asset/new
	 * @apiParameter {
	 * 		"name":        "files",
	 * 		"description": "Files to upload",
	 * 		"type":        "array",
	 * 		"required":    false,
	 * 		"default":     null
	 * }
	 * @apiParameter {
	 * 		"name":        "type",
	 * 		"description": "Content type being created",
	 * 		"type":        "string",
	 * 		"required":    false,
	 * 		"default":     null
	 * }
	 * @apiParameter {
	 * 		"name":        "handler",
	 * 		"description": "The file handler to use",
	 * 		"type":        "string",
	 * 		"required":    false,
	 * 		"default":     null
	 * }
	 * @return    void
	 */
	public function newTask()
	{
		// Require authentication and authorization
		$this->authorizeOrFail();

		// Grab the incoming file (incoming type overrides files)
		if (isset($_FILES['files']) && !Request::getWord('type', false))
		{
			$file_name = $_FILES['files']['name'][0];
			$file_size = (int) $_FILES['files']['size'];

			// Get the extension
			$pathinfo = pathinfo($file_name);
			$ext      = $pathinfo['extension'];
		}
		elseif ($contentType = Request::getWord('type', false))
		{
			// @FIXME: having this here breaks the responder model idea
			// The content type handlers could respond to a function that assesses the incoming data?
			switch ($contentType)
			{
				case 'link':
					$ext = 'url';
					break;
				case 'object':
					$ext = 'object';
					break;
				case 'wiki':
					$ext = 'wiki';
					break;
			}
		}
		else
		{
			App::abort(400, 'No assets given');
		}

		// Initiate our file handler
		$database     = App::get('db');
		$assetHandler = new Handler($database, $ext);

		// Create the new asset
		$return = $assetHandler->doCreate(Request::getVar('handler', null));

		// Check for errors in response
		if (array_key_exists('error', $return))
		{
			App::abort(400, $return['error']);
		}

		// Return message
		$this->send(['assets'=>$return], 201);
	}

	/**
	 * Retrieves the asset edit page
	 *
	 * @apiMethod POST
	 * @apiUri    /courses/asset/edit
	 * @apiParameter {
	 * 		"name":        "id",
	 * 		"description": "ID of asset to edit",
	 * 		"type":        "integer",
	 * 		"required":    true,
	 * 		"default":     null
	 * }
	 * @return    void
	 */
	public function editTask()
	{
		// Require authentication and authorization
		$this->authorizeOrFail();

		// Make sure we have an asset id
		if (!$asset_id = Request::getInt('id', false))
		{
			App::abort(500, 'No asset id provided');
		}

		// Initiate our file handler
		$database     = App::get('db');
		$assetHandler = new Handler($database);

		// Edit the asset
		$return = $assetHandler->doEdit($asset_id);

		// Check for errors in response
		if (is_array($return) && array_key_exists('error', $return))
		{
			App::abort(400, $return['error']);
		}

		// Return message
		$this->send($return);
	}

	/**
	 * Previews an asset
	 *
	 * @apiMethod POST
	 * @apiUri    /courses/asset/preview
	 * @apiParameter {
	 * 		"name":        "id",
	 * 		"description": "ID of asset to preview",
	 * 		"type":        "integer",
	 * 		"required":    true,
	 * 		"default":     null
	 * }
	 * @return    void
	 */
	public function previewTask()
	{
		// Require authentication and authorization
		$this->authorizeOrFail();

		// Make sure we have an asset id
		if (!$asset_id = Request::getInt('id', false))
		{
			App::abort(500, 'No asset id provided');
		}

		// Initiate our file handler
		$database     = App::get('db');
		$assetHandler = new Handler($database);

		// Edit the asset
		$return = $assetHandler->preview($asset_id);

		// Check for errors in response
		if (is_array($return) && array_key_exists('error', $return))
		{
			App::abort(400, $return['error']);
		}

		// Return message
		$this->send($return);
{'fraction_non_alphanumeric': 0.14970951884403397, 'fraction_numerical': 0.006703411291523909, 'mean_word_length': 3.02367395864549, 'pattern_counts': {'":': 60, '<': 13, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3432516', 'n_tokens_mistral': 4856, 'n_tokens_neox': 4238, 'n_words': 1233}