skala
parent
63084b3b13
commit
9a6d15368c
@ -1,395 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* This is a draft |
||||
* |
||||
*/ |
||||
use Doctrine\ORM\Tools\Setup; |
||||
use Doctrine\ORM\EntityManager; |
||||
use Doctrine\ORM\Configuration; |
||||
use Tools\EntityGenerator; |
||||
use Tools\EntityRepositoryGenerator; |
||||
use Doctrine\Common\Util\Inflector; |
||||
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
||||
|
||||
function save($data) |
||||
{ |
||||
db::instance()->save($data); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @license see /license.txt |
||||
*/ |
||||
class db |
||||
{ |
||||
|
||||
/** |
||||
* @return db |
||||
*/ |
||||
public static function instance() |
||||
{ |
||||
static $result = null; |
||||
if (empty($result)) { |
||||
$result = new self(); |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
private static function register_autoload() |
||||
{ |
||||
static $has_run = false; |
||||
if ($has_run) { |
||||
return true; |
||||
} |
||||
require_once api_get_path(LIBRARY_PATH) . 'symfony/Doctrine/ORM/Tools/Setup.php'; |
||||
$directory = api_get_path(LIBRARY_PATH) . 'symfony'; |
||||
|
||||
if (!class_exists('Doctrine\Common\ClassLoader', false)) { |
||||
require_once $directory . '/doctrine/Common/ClassLoader.php'; |
||||
} |
||||
|
||||
$loader = new Doctrine\Common\ClassLoader('Doctrine', $directory); |
||||
$loader->register(); |
||||
|
||||
$loader = new Doctrine\Common\ClassLoader('Symfony\Component', $directory); |
||||
$loader->register(); |
||||
|
||||
$has_run = true; |
||||
} |
||||
|
||||
protected $config; |
||||
protected $em; |
||||
|
||||
protected function __construct() |
||||
{ |
||||
self::register_autoload(); |
||||
$connection_parameters = $this->get_connection_parameters(); |
||||
$this->config = Setup::createYAMLMetadataConfiguration(array($this->get_entity_path() . '/mapping'), true); |
||||
$this->em = EntityManager::create($connection_parameters, $this->config); |
||||
} |
||||
|
||||
public function save($data) |
||||
{ |
||||
$em = $this->em; |
||||
$em->persist($data); |
||||
$em->flush(); |
||||
} |
||||
|
||||
public function remove($data) |
||||
{ |
||||
$em = $this->em; |
||||
$em->remove($data); |
||||
$em->flush(); |
||||
} |
||||
|
||||
public function flush($data = null) |
||||
{ |
||||
$em = $this->em; |
||||
$em->flush($data); |
||||
} |
||||
|
||||
function test() |
||||
{ |
||||
$_SESSION['_real_cid'] = 2; |
||||
|
||||
// $repo = Entity\User::repository(); |
||||
// $user = Entity\User::create(); |
||||
// $user->set_username('salut1'); |
||||
// $user->set_password('salut'); |
||||
// $user->set_status(1); |
||||
// $user->set_chatcall_user_id(1); |
||||
// $user->set_chatcall_date(new DateTime()); |
||||
// $user->set_chatcall_text(''); |
||||
// $user->set_registration_date(new DateTime()); |
||||
// $user->set_expiration_date(new DateTime()); |
||||
// $user->set_active(true); |
||||
// $user->set_hr_dept_id(0); |
||||
// $em = $this->em(); |
||||
// $em->persist($user); |
||||
// $em->flush(); |
||||
// echo "Created User with ID " . $user->get_user_id() . "\n"; |
||||
// $user = $repo->find(3); |
||||
// $this->remove($user); |
||||
// $this->flush(); |
||||
|
||||
|
||||
$doc = \Entity\Document::create(); |
||||
$doc->set_path('path'); |
||||
$doc->set_title('title'); |
||||
//$doc->set_c_id(1); |
||||
$doc->set_comment('comment'); |
||||
$doc->set_size(0); |
||||
$doc->set_session_id(0); |
||||
$doc->set_filetype('dd'); |
||||
$doc->set_readonly(false); |
||||
|
||||
//$repo = \Entity\Document::repository(); |
||||
//$id = $repo->next_id($doc); |
||||
//echo "next id: $id <br/>"; |
||||
|
||||
|
||||
//$doc->set_id($id); |
||||
$this->save($doc); |
||||
$this->flush(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @return EntityManager The created EntityManager. |
||||
*/ |
||||
public function em() |
||||
{ |
||||
return $this->em; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param string $entity_name |
||||
* @return EntityRepository |
||||
*/ |
||||
public function get_repository($entity_name) |
||||
{ |
||||
return $this->em()->getRepository($entity_name); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @return type |
||||
*/ |
||||
public function get_entity_path() |
||||
{ |
||||
$result = __DIR__ . '/../entity'; |
||||
$result = realpath($result); |
||||
return $result; |
||||
} |
||||
|
||||
public function is_production() |
||||
{ |
||||
return Chamilo::is_production_server(); |
||||
} |
||||
|
||||
public function is_dev() |
||||
{ |
||||
return !Chamilo::is_production_server(); |
||||
} |
||||
|
||||
/** |
||||
* Reverse engineering of the model from the database structure. |
||||
* Write result to the entity folder |
||||
* |
||||
* WARNING THIS WILL OVERWRITE EXISTING MODEL. |
||||
* |
||||
* @return boolean |
||||
*/ |
||||
public function generate_model() |
||||
{ |
||||
if (!$this->is_dev()) { |
||||
return false; |
||||
} |
||||
|
||||
$root = $this->get_entity_path(); |
||||
|
||||
$connection_parameters = $this->get_connection_parameters(); |
||||
$connection = Doctrine\DBAL\DriverManager::getConnection($connection_parameters); |
||||
$platform = $connection->getDatabasePlatform(); |
||||
$platform->registerDoctrineTypeMapping('enum', 'string'); |
||||
$platform->registerDoctrineTypeMapping('set', 'string'); |
||||
|
||||
$config = Setup::createConfiguration($this->is_dev()); |
||||
$config->setMetadataDriverImpl(new Doctrine\ORM\Mapping\Driver\DatabaseDriver(new Doctrine\DBAL\Schema\MySqlSchemaManager($connection))); |
||||
|
||||
$em = EntityManager::create($connection, $config); |
||||
|
||||
$cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory(); |
||||
$cmf->setEntityManager($em); |
||||
$metadatas = $cmf->getAllMetadata(); |
||||
|
||||
|
||||
$repo_factory = new EntityRepositoryGenerator(); |
||||
|
||||
$course = null; |
||||
foreach ($metadatas as $metadata) { |
||||
$n = strtolower($metadata->name); |
||||
if ($n == 'course') { |
||||
$course = $metadata; |
||||
break; |
||||
} |
||||
} |
||||
|
||||
foreach ($metadatas as $metadata) { |
||||
echo sprintf('Processing entity "<info>%s</info>"', $metadata->name) . '<br/>'; |
||||
|
||||
foreach ($metadata->identifier as $key => $value) { |
||||
//$mapping = $metadata->fieldMappings[$value]; |
||||
$metadata->identifier[$key] = Inflector::tableize($value); |
||||
} |
||||
|
||||
$fields = array(); |
||||
foreach ($metadata->fieldMappings as $fieldMapping) { |
||||
$name = Inflector::tableize($fieldMapping['fieldName']); |
||||
$fieldMapping['fieldName'] = $name; |
||||
$fields[$name] = $fieldMapping; |
||||
} |
||||
$metadata->fieldMappings = $fields; |
||||
|
||||
$n = $metadata->name; |
||||
if ($n == 'CDocument') { |
||||
$i = 1; |
||||
} |
||||
$name = $metadata->name; |
||||
$name = Inflector::tableize($name); |
||||
$is_course_table = (strpos($name, 'c_') === 0); |
||||
if ($is_course_table) { |
||||
$name = substr($name, 2, strlen($name) - 2); |
||||
} |
||||
//$metadata->namespace = 'Entity'; |
||||
$metadata->customRepositoryClassName = 'Entity\\Repository\\' . Inflector::classify($name) . 'Repository'; |
||||
//if(is_course_table){ |
||||
$metadata->name = 'Entity\\' . Inflector::classify($name); |
||||
|
||||
$metadata->lifecycleCallbacks['prePersist'] = array('before_save'); |
||||
|
||||
//} |
||||
//$metadata->rootEntityName = Inflector::classify($name); |
||||
if ($is_course_table) { |
||||
foreach ($metadata->fieldMappings as $mapping) { |
||||
$name = $mapping['columnName']; |
||||
$is_id = isset($mapping['id']) ? $mapping['id'] : false; |
||||
if ($name != 'c_id' && $is_id) { |
||||
|
||||
} |
||||
} |
||||
} |
||||
if ($is_course_table) { |
||||
$metadata->is_course_table = true; |
||||
// $mapping = array(); |
||||
// $mapping['cascade'] = array(); |
||||
// $mapping['joinColumns'][0] = array('name' => 'c_id', 'referencedColumnName' => 'id'); |
||||
// $mapping['sourceToTargetKeyColumns']['c_id'] = 'id'; |
||||
// $mapping['joinColumnFieldNames']['c_id'] = 'c_id'; |
||||
// $mapping['targetToSourceKeyColumns']['id'] = 'c_id'; |
||||
// $mapping['id'] = 1; |
||||
// $mapping['isOwningSide'] = 0; |
||||
// $mapping['isCascadeRemove'] = 0; |
||||
// $mapping['isCascadePersist'] = 0; |
||||
// $mapping['isCascadeRefresh'] = 0; |
||||
// $mapping['isCascadeMerge'] = 0; |
||||
// $mapping['isCascadeDetach'] = 0; |
||||
// $mapping['orphanRemoval'] = 0; |
||||
// $mapping['type'] = ClassMetadataInfo::MANY_TO_ONE; |
||||
// $mapping['fetch'] = ClassMetadataInfo::FETCH_LAZY; |
||||
// $mapping['fieldName'] = 'course'; |
||||
// $mapping['targetEntity'] = 'Entity\\Course'; |
||||
// $mapping['sourceEntity'] = $metadata->name; |
||||
// |
||||
// $metadata->associationMappings['course'] = $mapping; |
||||
// $metadata->identifier['course']; |
||||
|
||||
// unset($metadata->identifier['c_id']); |
||||
// unset($metadata->fieldMappings['c_id']); |
||||
|
||||
|
||||
// $mapping = array(); |
||||
// $mapping['cascade'] = array(); |
||||
// $mapping['joinColumns'][0] = array('name' => 'id', 'referencedColumnName' => 'c_id'); |
||||
// $mapping['sourceToTargetKeyColumns']['id'] = 'c_id'; |
||||
// $mapping['joinColumnFieldNames']['id'] = 'id'; |
||||
// $mapping['targetToSourceKeyColumns']['c_id'] = 'id'; |
||||
// $mapping['id'] = 1; |
||||
// $mapping['isOwningSide'] = 1; |
||||
// $mapping['isCascadeRemove'] = 0; |
||||
// $mapping['isCascadePersist'] = 0; |
||||
// $mapping['isCascadeRefresh'] = 0; |
||||
// $mapping['isCascadeMerge'] = 0; |
||||
// $mapping['isCascadeDetach'] = 0; |
||||
// $mapping['orphanRemoval'] = 0; |
||||
// $mapping['type'] = ClassMetadataInfo::ONE_TO_MANY; |
||||
// $mapping['fetch'] = ClassMetadataInfo::FETCH_LAZY; |
||||
// $name = explode('\\' ,$metadata->name); |
||||
// $name = end($name); |
||||
// $name = Inflector::tableize($name); |
||||
// $mapping['fieldName'] = $name; |
||||
// $mapping['targetEntity'] = $metadata->name; |
||||
// $mapping['sourceEntity'] = 'Entity\\Course'; |
||||
// $course->associationMappings[$name] = $mapping; |
||||
} |
||||
$metadata->class_to_extend = $is_course_table ? 'CourseEntity' : 'Entity'; |
||||
|
||||
|
||||
$repo_factory->writeEntityRepositoryClass($metadata->name, $root . '\\repository\\'); |
||||
} |
||||
|
||||
$generator = new EntityGenerator(); |
||||
|
||||
$generator->setClassToExtend('Entity'); |
||||
$generator->setGenerateAnnotations(false); |
||||
$generator->setGenerateStubMethods(true); |
||||
$generator->setRegenerateEntityIfExists(false); |
||||
$generator->setUpdateEntityIfExists(false); |
||||
$generator->setBackupExisting(false); |
||||
$generator->setExtension('.class.php'); |
||||
$generator->setNumSpaces(4); |
||||
|
||||
// Generating Entities |
||||
$generator->generate($metadatas, $root); |
||||
|
||||
$exporter = new \Doctrine\ORM\Tools\Export\Driver\YamlExporter(); |
||||
$exporter->setOutputDir($root . '/mapping'); |
||||
foreach ($metadatas as $metadata) { |
||||
echo $metadata->name . '<br/>'; |
||||
try { |
||||
$exporter->setMetadata(array($metadata)); |
||||
$exporter->export(); |
||||
} catch (Exception $e) { |
||||
echo $e->getMessage(); |
||||
} |
||||
} |
||||
} |
||||
|
||||
public function update_schema($paths, $save_mode) |
||||
{ |
||||
// $is_dev = $this->is_dev(); |
||||
// |
||||
// $ext = explode('.', $path); |
||||
// $ext = end($ext); |
||||
// if ($ext == 'yml') { |
||||
// $config = Setup::createYAMLMetadataConfiguration($paths, $is_dev); |
||||
// } else if ($ext == 'xml') { |
||||
// $config = Setup::createXMLMetadataConfiguration($paths, $is_dev); |
||||
// } else if ($ext == 'php') { |
||||
// $config = Setup::createAnnotationMetadataConfiguration($paths, $is_dev); |
||||
// } else { |
||||
// return false; |
||||
// } |
||||
// |
||||
// $em = $this->em; |
||||
// |
||||
// $cmf = new Doctrine\ORM\Tools\DisconnectedClassMetadataFactory(); |
||||
// $cmf->setEntityManager($em); |
||||
// $classes = $cmf->getAllMetadata(); |
||||
// |
||||
// $tool = new Doctrine\ORM\Tools\SchemaTool($em); |
||||
// return $tool->updateSchema($classes, $save_mode); |
||||
} |
||||
|
||||
protected function get_config() |
||||
{ |
||||
return $this->config; |
||||
} |
||||
|
||||
protected function get_connection_parameters() |
||||
{ |
||||
global $_configuration; |
||||
$result = array( |
||||
'dbname' => $_configuration['main_database'], |
||||
'user' => $_configuration['db_user'], |
||||
'password' => $_configuration['db_password'], |
||||
'host' => $_configuration['db_host'], |
||||
'driver' => 'pdo_mysql', |
||||
); |
||||
return $result; |
||||
} |
||||
|
||||
} |
||||
|
@ -1,92 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Proxy for the closure compiler. Allows to minify javascript files. |
||||
* This makes use of CURL and calls the closure service. |
||||
* |
||||
* @license see /license.txt |
||||
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva |
||||
* @see http://closure-compiler.appspot.com/home |
||||
* @see https://developers.google.com/closure/compiler/ |
||||
* |
||||
*/ |
||||
class ClosureCompiler |
||||
{ |
||||
|
||||
const PARAM_OUTPUT_FORMAT = 'output_format'; |
||||
const PARAM_OUTPUT_INFO = 'output_info'; |
||||
const PARAM_COMPILATION_LEVEL = 'compilation_level'; |
||||
const PARAM_JS_CODE = 'js_code'; |
||||
const LEVEL_SIMPLE = 'SIMPLE_OPTIMIZATIONS'; |
||||
const LEVEL_WHITESPACE = 'WHITESPACE_ONLY'; |
||||
const LEVEL_ADVANCED = 'ADVANCED_OPTIMIZATIONS'; |
||||
const OUTPUT_FORMAT_TEXT = 'text'; |
||||
const OUTPUT_INFO_CODE = 'compiled_code'; |
||||
|
||||
/** |
||||
* Url of the service |
||||
* |
||||
* @return string |
||||
*/ |
||||
public static function url() |
||||
{ |
||||
return 'closure-compiler.appspot.com/compile'; |
||||
} |
||||
|
||||
/** |
||||
* Post data the closure compiler service. By default it returns minimized code |
||||
* with the simple option. |
||||
* |
||||
* @param string $code Javascript code to minimify |
||||
* @param array $params parameters to pass to the service |
||||
* @return string minimified code |
||||
*/ |
||||
public static function post($code, $params = array()) |
||||
{ |
||||
if (!isset($params[self::PARAM_OUTPUT_FORMAT])) |
||||
{ |
||||
$params[self::PARAM_OUTPUT_FORMAT] = self::OUTPUT_FORMAT_TEXT; |
||||
} |
||||
if (!isset($params[self::PARAM_OUTPUT_INFO])) |
||||
{ |
||||
$params[self::PARAM_OUTPUT_INFO] = self::OUTPUT_INFO_CODE; |
||||
} |
||||
if (!isset($params[self::PARAM_COMPILATION_LEVEL])) |
||||
{ |
||||
$params[self::PARAM_JS_CODE] = $code; |
||||
} |
||||
|
||||
$params[self::PARAM_COMPILATION_LEVEL] = self::LEVEL_SIMPLE; |
||||
|
||||
$fields = array(); |
||||
foreach ($params as $key => $value) |
||||
{ |
||||
$fields[] = $key . '=' . urlencode($value); |
||||
} |
||||
$fields = implode('&', $fields); |
||||
|
||||
$headers = array("Content-type: application/x-www-form-urlencoded"); |
||||
|
||||
$url = self::url(); |
||||
|
||||
$ch = curl_init(); |
||||
|
||||
curl_setopt($ch, CURLOPT_URL, $url); |
||||
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||||
curl_setopt($ch, CURLOPT_POST, true); |
||||
curl_setopt($ch, CURLOPT_POSTFIELDS, $fields); |
||||
//curl_setopt($ch, CURLOPT_HEADER, false); |
||||
//curl_setopt($ch, CURLOPT_VERBOSE, true); |
||||
|
||||
$content = curl_exec($ch); |
||||
$error = curl_error($ch); |
||||
$info = curl_getinfo($ch); |
||||
|
||||
curl_close($ch); |
||||
|
||||
return $content; |
||||
} |
||||
|
||||
} |
@ -1,92 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Description of code_utilities |
||||
* |
||||
* @license see /license.txt |
||||
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva |
||||
*/ |
||||
class CodeUtilities |
||||
{ |
||||
|
||||
const CLASS_PATTERN = '/(?:\s+class\s+[a-zA-Z_0-9\x7f-\xff]+\s*{)|(?:\s+class\s+[a-zA-Z_0-9\x7f-\xff]+\s*extends)|(?:\s+class\s+[a-zA-Z_0-9\x7f-\xff]+\s*implements)|(?:\s+interface\s+[a-zA-Z_0-9\x7f-\xff]+\s*{)|(?:\s+interface\s+[a-zA-Z_0-9\x7f-\xff]+\s*extends)/mi'; |
||||
const INLINE_COMMENT_PATTERN = '#//.*$#m'; |
||||
const MULTILINE_COMMENT_PATTERN = '#/\*.*?\*/#ms'; |
||||
const NAMESPACE_PATTERN = '/namespace\s*(.*);/'; |
||||
const IDENTIFIER_PATTERN = '/^[a-zA-Z_][a-zA-Z0-9_]*$/'; |
||||
|
||||
static function remove_comments($content) |
||||
{ |
||||
$content = preg_replace(self::INLINE_COMMENT_PATTERN, '', $content); |
||||
$content = preg_replace(self::MULTILINE_COMMENT_PATTERN, '', $content); |
||||
return $content; |
||||
} |
||||
|
||||
/** |
||||
* Returns the name of classes and interfaces contained in content. |
||||
* |
||||
* @param text $content |
||||
* @return array |
||||
*/ |
||||
static function get_classes($content) |
||||
{ |
||||
$result = array(); |
||||
$cls_pattern = self::CLASS_PATTERN; |
||||
|
||||
$content = self::remove_comments($content); //comments may contains class declaration we don't want to capture. |
||||
|
||||
$matches = array(); |
||||
if (preg_match_all($cls_pattern, $content, $matches)) { |
||||
$matches = reset($matches); |
||||
foreach ($matches as $match) { |
||||
$match = str_replace("\n", ' ', $match); |
||||
$match = str_replace('{', ' ', $match); |
||||
$words = explode(' ', $match); |
||||
foreach ($words as $word) { |
||||
$word = trim($word); |
||||
//we capture the interface/class name with the current pattern |
||||
if (strtolower($word) != 'class' && strtolower($word) != 'interface' && strtolower($word) != 'implements' && strtolower($word) != 'extends' && !empty($word)) { |
||||
$result[] = $word; |
||||
break; //we only take the first name as we don't want to capture the name of the interface or of the parent class name |
||||
} |
||||
} |
||||
} |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
static function get_namespace($content) |
||||
{ |
||||
$namespace_pattern = self::NAMESPACE_PATTERN; |
||||
if (preg_match($namespace_pattern, $content, $matches)) { |
||||
$result = end($matches); |
||||
return trim($result); |
||||
} else { |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
static function is_valid_identifier($name) |
||||
{ |
||||
$pattern = self::IDENTIFIER_PATTERN; |
||||
$r = preg_match($pattern, $name); |
||||
return $r; |
||||
} |
||||
|
||||
/** |
||||
* Make path relative to root. |
||||
* |
||||
* @param string $root |
||||
* @param string $path |
||||
* @return string |
||||
*/ |
||||
static function relative_path($root, $path) |
||||
{ |
||||
$path = realpath($path); |
||||
$root = realpath($root); |
||||
$path = str_ireplace($root, '', $path); |
||||
$path = str_ireplace('\\', '/', $path); |
||||
return $path; |
||||
} |
||||
|
||||
} |
@ -1 +0,0 @@ |
||||
Used to extract metadata from external resources : youtube, dailymotion, etc |
@ -1,56 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Process all renderers attached in order. |
||||
* |
||||
* If a renderer returns an key value that has not already been set add it |
||||
* to the result. Otherwise let the previous value unchanged. |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetAggregatedRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
protected $renderers = array(); |
||||
|
||||
public function __construct($renderers) |
||||
{ |
||||
$this->renderers = $renderers; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function renderers() |
||||
{ |
||||
return $this->renderers; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
* @return array |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
$result = array(); |
||||
$plugins = self::plugins(); |
||||
foreach ($this->renderers as $renderer) |
||||
{ |
||||
$data = $renderer->render($asset); |
||||
$data = $data ? $data : array(); |
||||
foreach ($data as $key => $value) |
||||
{ |
||||
if (!isset($result[$key]) && !empty($value)) |
||||
{ |
||||
$result[$key] = $value; |
||||
} |
||||
} |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,107 +0,0 @@ |
||||
<?php |
||||
|
||||
require_once dirname(__FILE__) . '/http_resource.class.php'; |
||||
require_once dirname(__FILE__) . '/asset_aggregated_renderer.class.php'; |
||||
|
||||
/** |
||||
* Renderer for an http resource. |
||||
* Extract meta data and snippet html view from an given url/resource. |
||||
* |
||||
* Base class. Other renderers must inherit from it. |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetRenderer |
||||
{ |
||||
|
||||
const THUMBNAIL = 'thumbnail'; |
||||
const EMBED_SNIPPET = 'embed_snippet'; |
||||
const EMBED_TYPE = 'embed_type'; |
||||
const EMBED_URL = 'embed_url'; |
||||
const WIDTH = 'width'; |
||||
const HEIGHT = 'height'; |
||||
const LANGUAGE = 'language'; |
||||
const URL = 'url'; |
||||
const TAGS = 'tags'; |
||||
const TITLE = 'title'; |
||||
const CREATED_TIME = 'created_time'; |
||||
const DURATION = 'duration'; |
||||
const DESCRIPTION = 'description'; |
||||
const ICON = 'icon'; |
||||
|
||||
static function get($url, $config = array()) |
||||
{ |
||||
if (strpos('url', 'javascript:') !== false) |
||||
{ |
||||
return array(); |
||||
} |
||||
$result = array(); |
||||
$url = trim($url); |
||||
if (empty($url)) |
||||
{ |
||||
return array(); |
||||
} |
||||
$asset = new HttpResource($url, $config); |
||||
$renderer = new AssetAggregatedRenderer(self::plugins()); |
||||
$result = $renderer->render($asset); |
||||
$result['url'] = $url; |
||||
return $result; |
||||
} |
||||
|
||||
static function plugins() |
||||
{ |
||||
static $result = array(); |
||||
if (!empty($result)) |
||||
{ |
||||
return $result; |
||||
} |
||||
|
||||
/* |
||||
* We make sure we load them from most specialized to less specialized. |
||||
* The first that provides a value for a field wins. |
||||
*/ |
||||
$protocols = array( |
||||
'oembed', |
||||
'og', |
||||
'image', |
||||
'media', |
||||
'rss', |
||||
'google_map', |
||||
'google_document', |
||||
'google_document_viewer', |
||||
'google_widget', |
||||
'mediaserver', |
||||
'scratch', |
||||
'page'); |
||||
|
||||
foreach ($protocols as $protocol) |
||||
{ |
||||
$file = "asset_{$protocol}_renderer.class.php"; |
||||
require_once dirname(__FILE__) . '/protocol/' . $file; |
||||
|
||||
$class = "asset_{$protocol}_renderer"; |
||||
$class = explode('_', $class); |
||||
$class = array_map('ucfirst', $class); |
||||
$class = implode($class); |
||||
|
||||
$result[] = new $class(); |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* Renderer function. Take a http asset as input and return an array containing |
||||
* various properties: metadata, html snippet, etc. |
||||
* |
||||
* @param HttpResource $asset |
||||
* @return array |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
$result = array(); |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,662 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* An HTTP resource. In most cases an HTML document. |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class HttpResource |
||||
{ |
||||
|
||||
/** |
||||
* Fetch the content and metadata of an url. |
||||
* |
||||
* If the content type is not parsable, i.e. it is not made of text, |
||||
* only fetch the metadata and not the content. This is mostly done to |
||||
* avoid downloading big files - videos, images, etc - which is unnecessary. |
||||
* |
||||
* @param string $url the url to fetch |
||||
* @return array array containing the content and various info |
||||
*/ |
||||
static function fetch($url, $fetch_content = null) |
||||
{ |
||||
static $cache = array(); |
||||
if (isset($cache[$url])) |
||||
{ |
||||
return $cache; |
||||
} |
||||
|
||||
if (is_null($fetch_content) || $fetch_content === false) |
||||
{ |
||||
// create a new cURL resource |
||||
$ch = curl_init(); |
||||
|
||||
// set URL and other appropriate options |
||||
curl_setopt($ch, CURLOPT_URL, $url); |
||||
curl_setopt($ch, CURLOPT_HEADER, false); |
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||||
curl_setopt($ch, CURLOPT_NOBODY, true); |
||||
|
||||
$content = curl_exec($ch); |
||||
$error = curl_error($ch); |
||||
$info = curl_getinfo($ch); |
||||
|
||||
// close cURL resource, and free up system resources |
||||
curl_close($ch); |
||||
$info['content'] = $content; |
||||
$info['error'] = $error; |
||||
|
||||
if ($fetch_content === false) |
||||
{ |
||||
return $cache[$url] = $info; |
||||
} |
||||
|
||||
if (isset($info['content_type']) && strpos($info['content_type'], 'text') === false) |
||||
{ |
||||
return $cache[$url] = $info; |
||||
} |
||||
} |
||||
|
||||
// create a new cURL resource |
||||
$ch = curl_init(); |
||||
|
||||
// set URL and other appropriate options |
||||
curl_setopt($ch, CURLOPT_URL, $url); |
||||
curl_setopt($ch, CURLOPT_HEADER, false); |
||||
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||||
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); |
||||
|
||||
//curl_setopt($ch, CURLOPT_VERBOSE, true); |
||||
|
||||
$content = curl_exec($ch); |
||||
$error = curl_error($ch); |
||||
$info = curl_getinfo($ch); |
||||
|
||||
// close cURL resource, and free up system resources |
||||
curl_close($ch); |
||||
$info['content'] = $content; |
||||
$info['error'] = $error; |
||||
|
||||
return $cache[$url] = $info; |
||||
} |
||||
|
||||
static function fetch_json($url) |
||||
{ |
||||
$content = self::fetch($url, true); |
||||
$content = $content['content']; |
||||
if ($content) |
||||
{ |
||||
$result = (array) json_decode($content); |
||||
} |
||||
else |
||||
{ |
||||
$result = array(); |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
protected $url; |
||||
protected $url_params = null; |
||||
protected $info = null; |
||||
protected $source = null; |
||||
protected $metadata = null; |
||||
protected $links = null; |
||||
protected $title = null; |
||||
protected $mime = null; |
||||
protected $doc = null; |
||||
protected $config = array(); |
||||
|
||||
public function __construct($url, $config = array()) |
||||
{ |
||||
$this->url = $url; |
||||
$this->config = $config; |
||||
} |
||||
|
||||
public function config($key = '', $default = null) |
||||
{ |
||||
return isset($this->config[$key]) ? $this->config[$key] : $default; |
||||
} |
||||
|
||||
/** |
||||
* Url of the resource |
||||
* |
||||
* @return string |
||||
*/ |
||||
public function url() |
||||
{ |
||||
return $this->url; |
||||
} |
||||
|
||||
public function url_domain() |
||||
{ |
||||
$url = $this->url(); |
||||
$url = trim($url, '/'); |
||||
if (strpos($url, '//') !== false) |
||||
{ |
||||
$parts = explode('//', $url); |
||||
$url = end($parts); |
||||
} |
||||
$parts = explode('/', $url); |
||||
$result = reset($parts); |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param array|string $part |
||||
* @return boolean |
||||
*/ |
||||
public function url_match($part) |
||||
{ |
||||
$params = func_get_args(); |
||||
$params = is_array($params) ? $params : array($params); |
||||
|
||||
$url = strtolower($this->url()); |
||||
foreach ($params as $param) |
||||
{ |
||||
if (strpos($url, strtolower($param)) !== false) |
||||
{ |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public function url_params() |
||||
{ |
||||
if (!is_null($this->url_params)) |
||||
{ |
||||
return $this->url_params; |
||||
} |
||||
|
||||
$url = $this->url(); |
||||
if (strpos($url, '?') === false) |
||||
{ |
||||
return $this->url_params = array(); |
||||
} |
||||
|
||||
$result = array(); |
||||
$params = explode('?', $url); |
||||
$params = end($params); |
||||
$params = explode('&', $params); |
||||
foreach ($params as $param) |
||||
{ |
||||
list($key, $val) = explode('=', $param); |
||||
$result[$key] = $val; |
||||
} |
||||
|
||||
return $this->url_params = $result; |
||||
} |
||||
|
||||
public function url_param($name, $default = false) |
||||
{ |
||||
$params = $this->url_params(); |
||||
return isset($params[$name]) ? $params[$name] : $default; |
||||
} |
||||
|
||||
/** |
||||
* The name of the resource. I.e. the last part of the url without the ext |
||||
* |
||||
* @return string |
||||
*/ |
||||
public function name() |
||||
{ |
||||
$url = $this->url(); |
||||
$url = explode('/', $url); |
||||
$title = end($url); |
||||
$title = explode('.', $title); |
||||
$title = reset($title); |
||||
return $title; |
||||
} |
||||
|
||||
/** |
||||
* Extention of the url |
||||
* |
||||
* @return string |
||||
*/ |
||||
public function ext() |
||||
{ |
||||
$url = $this->url(); |
||||
$url = explode('.', $url); |
||||
$ext = end($url); |
||||
$ext = strtolower($ext); |
||||
return $ext; |
||||
} |
||||
|
||||
/** |
||||
* Return true if the object has one of the extentions. Overloaded: |
||||
* |
||||
* $res->has_ext('pdf'); |
||||
* $res->has_ext('pdf', 'doc'); |
||||
* $res->has_ext(array('pdf', 'doc')); |
||||
* |
||||
* @param array|string $_ |
||||
* @return boolean true if the resource has one of the extentions passed |
||||
*/ |
||||
public function has_ext($_) |
||||
{ |
||||
if (is_array($_)) |
||||
{ |
||||
$params = $_; |
||||
} |
||||
else |
||||
{ |
||||
$params = func_get_args(); |
||||
$params = is_array($params) ? $params : array($params); |
||||
} |
||||
$ext = $this->ext(); |
||||
foreach ($params as $param) |
||||
{ |
||||
if (strtolower($param) == $ext) |
||||
{ |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public function charset() |
||||
{ |
||||
$info = $this->info(); |
||||
|
||||
$content_type = isset($info['content_type']) ? $info['content_type'] : ''; |
||||
if (empty($content_type)) |
||||
{ |
||||
return null; |
||||
} |
||||
$items = explode(';', $content_type); |
||||
foreach ($items as $item) |
||||
{ |
||||
$parts = explode('=', $item); |
||||
if (count($parts) == 2 && reset($parts) == 'charset') |
||||
{ |
||||
return strtolower(end($parts)); |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
/** |
||||
* The mime type of the resource or the empty string if none has been specified |
||||
* |
||||
* @return string |
||||
*/ |
||||
public function mime() |
||||
{ |
||||
if (!is_null($this->mime)) |
||||
{ |
||||
return $this->mime; |
||||
} |
||||
$info = $this->info(); |
||||
|
||||
$content_type = isset($info['content_type']) ? $info['content_type'] : ''; |
||||
if ($content_type) |
||||
{ |
||||
$result = reset(explode(';', $content_type)); |
||||
$result = strtolower($result); |
||||
return $this->mime = $result; |
||||
} |
||||
|
||||
return $this->mime = ''; |
||||
} |
||||
|
||||
public function is_xml() |
||||
{ |
||||
$mime = $this->mime(); |
||||
if (!empty($mime)) |
||||
{ |
||||
return strpos($mime, 'xml') !== false; |
||||
} |
||||
return $this->ext() == 'xml'; |
||||
} |
||||
|
||||
public function is_image() |
||||
{ |
||||
$mime = $this->mime(); |
||||
if ($mime) |
||||
{ |
||||
return strpos($mime, 'image') !== false; |
||||
} |
||||
|
||||
$ext = $this->ext(); |
||||
$formats = array('gif', 'jpeg', 'jpg', 'jpe', 'pjpeg', 'png', 'svg', 'tiff', 'ico'); |
||||
foreach ($formats as $format) |
||||
{ |
||||
if ($format == $ext) |
||||
{ |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public function is_video() |
||||
{ |
||||
$mime = $this->mime(); |
||||
if ($mime) |
||||
{ |
||||
return strpos($mime, 'video') !== false; |
||||
} |
||||
|
||||
$ext = $this->ext(); |
||||
$formats = array('mpeg', 'mp4', 'ogg', 'wmv', 'mkv'); |
||||
foreach ($formats as $format) |
||||
{ |
||||
if ($format == $ext) |
||||
{ |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public function is_audio() |
||||
{ |
||||
$mime = $this->mime(); |
||||
if ($mime) |
||||
{ |
||||
return strpos($mime, 'audio') !== false; |
||||
} |
||||
|
||||
$ext = $this->ext(); |
||||
$formats = array('mp3'); |
||||
foreach ($formats as $format) |
||||
{ |
||||
if ($format == $ext) |
||||
{ |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public function is_rss() |
||||
{ |
||||
if (!$this->is_xml()) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
$doc = $this->doc(); |
||||
$nodes = $doc->getElementsByTagName('rss'); |
||||
return $nodes->length != 0; |
||||
} |
||||
|
||||
public function is_gadget() |
||||
{ |
||||
if (!$this->is_xml()) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
$doc = $this->doc(); |
||||
$nodes = $doc->getElementsByTagName('ModulePrefs'); |
||||
return $nodes->length != 0; |
||||
} |
||||
|
||||
public function canonic_url($src) |
||||
{ |
||||
if (strpos($src, '//') === 0) |
||||
{ |
||||
$src = "http:$src"; |
||||
} |
||||
else if (strpos($src, '/') === 0) //relative url to the root |
||||
{ |
||||
$url = $this->url(); |
||||
$protocol = reset(explode('://', $url)); |
||||
$domain = end(explode('://', $url)); |
||||
$domain = reset(explode('/', $domain)); |
||||
$src = "$protocol://$domain/$src"; |
||||
} |
||||
else if (strpos($src, 'http') !== 0) //relative url to the document |
||||
{ |
||||
$url = $this->url(); |
||||
$tail = end(explode('/', $url)); |
||||
$base = str_replace($tail, '', $url); |
||||
|
||||
$src = $base . $src; |
||||
} |
||||
return $src; |
||||
} |
||||
|
||||
/** |
||||
* Content of the resource. |
||||
* |
||||
* @return string |
||||
*/ |
||||
public function source() |
||||
{ |
||||
if (!is_null($this->source)) |
||||
{ |
||||
return $this->source; |
||||
} |
||||
$info = $this->info(); |
||||
|
||||
return $this->source = $info['content']; |
||||
} |
||||
|
||||
/** |
||||
* Array of arrays containing the page's metadata. |
||||
* |
||||
* @return array |
||||
*/ |
||||
public function metadata() |
||||
{ |
||||
if (!is_null($this->metadata)) |
||||
{ |
||||
return $this->metadata; |
||||
} |
||||
return $this->metadata = $this->get_metadata(); |
||||
} |
||||
|
||||
public function title() |
||||
{ |
||||
if (!is_null($this->title)) |
||||
{ |
||||
return $this->title; |
||||
} |
||||
return $this->title = $this->get_title(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @return DOMDocument|boolean |
||||
*/ |
||||
public function doc() |
||||
{ |
||||
if (!is_null($this->doc)) |
||||
{ |
||||
return $this->doc; |
||||
} |
||||
return $this->doc = $this->get_doc($this->source()); |
||||
} |
||||
|
||||
function get_meta($name) |
||||
{ |
||||
$metadata = $this->metadata(); |
||||
$name = strtolower($name); |
||||
foreach ($metadata as $attributes) |
||||
{ |
||||
$key = isset($attributes['name']) ? $attributes['name'] : false; |
||||
$key = $key ? strtolower($key) : $key; |
||||
if ($name == $key) |
||||
{ |
||||
return $attributes['content']; |
||||
} |
||||
$key = isset($attributes['property']) ? $attributes['property'] : false; |
||||
$key = $key ? strtolower($key) : $key; |
||||
if ($name == $key) |
||||
{ |
||||
return isset($attributes['content']) ? $attributes['content'] : false; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
function get_link($key, $value) |
||||
{ |
||||
$links = $this->links(); |
||||
$key = strtolower($key); |
||||
$value = strtolower($value); |
||||
foreach ($links as $attributes) |
||||
{ |
||||
$a = isset($attributes[$key]) ? $attributes[$key] : false; |
||||
$a = $a ? strtolower($a) : $a; |
||||
if ($a == $value) |
||||
{ |
||||
return $attributes; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
public function links() |
||||
{ |
||||
if (!is_null($this->links)) |
||||
{ |
||||
return $this->links; |
||||
} |
||||
return $this->links = $this->get_links(); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param string $xpath dom xpath |
||||
* @return string |
||||
*/ |
||||
public function findx($query) |
||||
{ |
||||
$doc = $this->doc(); |
||||
if (empty($doc)) |
||||
{ |
||||
return array(); |
||||
} |
||||
$xpath = new DOMXpath($doc); |
||||
$nodes = $xpath->query($query); |
||||
if ($nodes->length > 0) |
||||
{ |
||||
return $doc->saveXML($nodes->item(0)); |
||||
} |
||||
else |
||||
{ |
||||
return ''; |
||||
} |
||||
} |
||||
|
||||
protected function info() |
||||
{ |
||||
if (!is_null($this->info)) |
||||
{ |
||||
return $this->info; |
||||
} |
||||
return $this->info = self::fetch($this->url()); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param string $source |
||||
* @return boolean|DOMDocument |
||||
*/ |
||||
protected function get_doc($source) |
||||
{ |
||||
if ($source == false) |
||||
{ |
||||
return false; |
||||
} |
||||
$source = $this->source(); |
||||
$result = new DOMDocument(); |
||||
libxml_clear_errors(); |
||||
libxml_use_internal_errors(true); |
||||
if ($this->is_xml()) |
||||
{ |
||||
$success = $result->loadXML($source); |
||||
} |
||||
else |
||||
{ |
||||
$success = $result->loadHTML($source); |
||||
} |
||||
//$e = libxml_get_errors(); |
||||
return $result ? $result : false; |
||||
} |
||||
|
||||
protected function get_metadata() |
||||
{ |
||||
$result = array(); |
||||
|
||||
$doc = $this->doc(); |
||||
if ($doc == false) |
||||
{ |
||||
return array(); |
||||
} |
||||
$metas = $doc->getElementsByTagName('meta'); |
||||
if ($metas->length == 0) |
||||
{ |
||||
return $result; |
||||
} |
||||
foreach ($metas as $meta) |
||||
{ |
||||
$values = array(); |
||||
$attributes = $meta->attributes; |
||||
$length = $attributes->length; |
||||
for ($i = 0; $i < $length; ++$i) |
||||
{ |
||||
$name = $attributes->item($i)->name; |
||||
$value = $attributes->item($i)->value; |
||||
$value = $attributes->item($i)->value; |
||||
$values[$name] = $value; |
||||
} |
||||
$result[] = $values; |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
protected function get_title() |
||||
{ |
||||
$doc = $this->doc(); |
||||
if ($doc == false) |
||||
{ |
||||
return ''; |
||||
} |
||||
$titles = $doc->getElementsByTagName('title'); |
||||
if ($titles->length == 0) |
||||
{ |
||||
return false; |
||||
} |
||||
$result = $titles->item(0)->nodeValue; |
||||
return $result; |
||||
} |
||||
|
||||
protected function get_links() |
||||
{ |
||||
$doc = $this->doc(); |
||||
if ($doc == false) |
||||
{ |
||||
return array(); |
||||
} |
||||
$result = array(); |
||||
|
||||
$metas = $doc->getElementsByTagName('link'); |
||||
if ($metas->length == 0) |
||||
{ |
||||
return $result; |
||||
} |
||||
foreach ($metas as $meta) |
||||
{ |
||||
$values = array(); |
||||
$attributes = $meta->attributes; |
||||
$length = $attributes->length; |
||||
for ($i = 0; $i < $length; ++$i) |
||||
{ |
||||
$name = $attributes->item($i)->name; |
||||
$value = $attributes->item($i)->value; |
||||
$values[$name] = $value; |
||||
} |
||||
$result[] = $values; |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,113 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Google calendar renderer. |
||||
* |
||||
* @todo: |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetGoogleCalendarRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function accept($asset) |
||||
{ |
||||
$url = $asset->url(); |
||||
$url = str_replace('http://', '', $url); |
||||
$url = str_replace('https://', '', $url); |
||||
|
||||
$domain = reset(split('/', $url)); |
||||
return strpos($domain, 'google.com/calendar') !== false; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param string $url |
||||
*/ |
||||
public function explode_url_parameters($url = null) |
||||
{ |
||||
if (strpos($url, '?') === false) |
||||
{ |
||||
return array(); |
||||
} |
||||
|
||||
$result = array(); |
||||
$params = explode('?', $url); |
||||
$params = end($params); |
||||
$params = explode('&', $params); |
||||
foreach ($params as $param) |
||||
{ |
||||
list($key, $val) = explode('=', $param); |
||||
$result[$key] = $val; |
||||
} |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
public function implode_url_parameters($params) |
||||
{ |
||||
$result = array(); |
||||
foreach ($params as $key => $value) |
||||
{ |
||||
if ($value) |
||||
{ |
||||
$result[] = "$key=$value"; |
||||
} |
||||
} |
||||
return join('&', $result); |
||||
} |
||||
|
||||
protected function url($base = 'http:://map.google.com/', $params = array()) |
||||
{ |
||||
$head = reset(explode('?', $base)); |
||||
$items = $this->explode_url_parameters($base); |
||||
foreach ($params as $key => $value) |
||||
{ |
||||
$items[$key] = $value; |
||||
} |
||||
$tail = $this->implode_url_parameters($items); |
||||
$tail = empty($tail) ? '' : "?$tail"; |
||||
return $head . $tail; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (!$this->accept($asset)) |
||||
{ |
||||
return; |
||||
} |
||||
$params = array('output' => 'embed'); |
||||
|
||||
$base = $asset->url(); |
||||
$url = $this->url($base, $params); |
||||
|
||||
$title = $asset->title(); |
||||
$description = $asset->get_meta('description'); |
||||
|
||||
$keywords = $asset->get_meta('keywords'); |
||||
|
||||
$embed = <<<EOT |
||||
<iframe width="100%" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="$url"></iframe> |
||||
EOT; |
||||
|
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
$result[self::THUMBNAIL] = $image_src; |
||||
$result[self::DESCRIPTION] = $description; |
||||
$result[self::TAGS] = $keywords; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,57 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Internal group. I.e. a group from this instance of Mahara. |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetMaharaGroupRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
public static function get_group_id($ref) |
||||
{ |
||||
$ref = trim($ref); |
||||
$pattern = '#group/view.php\?id\=([0123456789]+)#'; |
||||
$matches = array(); |
||||
//mahara group's profile |
||||
if (preg_match($pattern, $ref, $matches)) |
||||
{ |
||||
return $matches[1]; |
||||
} |
||||
//group id |
||||
if ($val = intval($ref)) |
||||
{ |
||||
return $val; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
$url = $asset->url(); |
||||
$group_id = self::get_group_id($url); |
||||
if (empty($group_id)) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
$data = get_record('group', 'id', $group_id); |
||||
|
||||
$result = array(); |
||||
safe_require('blocktype', 'ple/group'); |
||||
$result[self::EMBED_SNIPPET] = PluginBlocktypeGroup::render_preview($group_id); |
||||
$result[self::THUMBNAIL] = PluginBlocktypeGroup::get_thumbnail($group_id); |
||||
$result[self::TITLE] = $data->name; |
||||
$result[self::DESCRIPTION] = $data->description; |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,63 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* |
||||
* Internal person. I.e. a person from this instance of Mahara. |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetMaharaPersonRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
public static function get_user_id($ref) |
||||
{ |
||||
$ref = trim($ref); |
||||
$pattern = '#user/view.php\?id\=([0123456789]+)#'; |
||||
$matches = array(); |
||||
//mahara user's profile |
||||
if (preg_match($pattern, $ref, $matches)) |
||||
{ |
||||
return $matches[1]; |
||||
} |
||||
//email |
||||
if ($user = get_record('usr', 'email', $ref)) |
||||
{ |
||||
return $user->id; |
||||
} |
||||
//user id |
||||
if ($val = intval($ref)) |
||||
{ |
||||
return $val; |
||||
} |
||||
|
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
$url = $asset->url(); |
||||
$id = self::get_user_id($url); |
||||
if (empty($id)) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
$data = get_record('usr', 'id', $id); |
||||
|
||||
$result = array(); |
||||
safe_require('blocktype', 'ple/person'); |
||||
$result[self::EMBED_SNIPPET] = PluginBlocktypePerson::render_preview($id); |
||||
$result[self::THUMBNAIL] = PluginBlocktypePerson::get_thumbnail($id); |
||||
$result[self::TITLE] = $data->prefferedname ? $data->prefferedname : $data->firstname . ' ' . $data->lastname; |
||||
$result[self::DESCRIPTION] = isset($data->description) ? $data->description : ''; |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,46 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Wiki renderer. |
||||
* |
||||
* @see http://en.wikipedia.org/w/api.php |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetWikiRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function accept($asset) |
||||
{ |
||||
return $asset->url_match('wikipedia.org/wiki', 'mediawiki.org/wiki'); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
* |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (!$this->accept($asset)) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
$domain = $asset->url_domain(); |
||||
$description = $asset->findx('//div[@id="bodyContent"]/p'); |
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $description; |
||||
$result[self::TITLE] = $title; |
||||
$result[self::DESCRIPTION] = $description; |
||||
$result[self::TAGS] = $keywords; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,75 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Google document renderer. |
||||
* |
||||
* @see http://support.google.com/docs/bin/answer.py?hl=en&answer=86101&topic=1360911&ctx=topic |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetGoogleDocumentRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function accept($asset) |
||||
{ |
||||
$url = $asset->url(); |
||||
|
||||
return strpos($url, 'docs.google.com/document/pub') !== false; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (!$this->accept($asset)) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
$url = $asset->url(); |
||||
|
||||
$title = $asset->title(); |
||||
$description = $asset->get_meta('description'); |
||||
$keywords = $asset->get_meta('keywords'); |
||||
|
||||
$size = (int) $asset->config('size'); |
||||
$size = (24 <= $size && $size <= 800) ? $size : 300; |
||||
|
||||
$embed = <<<EOT |
||||
|
||||
<div style="height:{$size}px;" class="resize vertical" > |
||||
<iframe style=background-color:#ffffff;" width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="$url"></iframe> |
||||
</div> |
||||
<style type="text/css"> |
||||
div.resize.vertical { |
||||
background-color: #EEEEEE; |
||||
border-color: #EEEEEE; |
||||
border-style: solid; |
||||
border-width: 1px; |
||||
resize:vertical; |
||||
overflow: hidden; |
||||
padding-bottom:15px; |
||||
min-height:24px; |
||||
max-height:800px; |
||||
} |
||||
</style> |
||||
EOT; |
||||
|
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
$result[self::DESCRIPTION] = $description; |
||||
$result[self::TAGS] = $keywords; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,95 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Google document viewer renderer. Provide an embeded viewer for the following |
||||
* documetn types: |
||||
* |
||||
* Microsoft Word (.DOC and .DOCX) |
||||
* Microsoft Excel (.XLS and .XLSX) |
||||
* Microsoft PowerPoint (.PPT and .PPTX) |
||||
* Adobe Portable Document Format (.PDF) |
||||
* Apple Pages (.PAGES) |
||||
* Adobe Illustrator (.AI) |
||||
* Adobe Photoshop (.PSD) |
||||
* Tagged Image File Format (.TIFF) |
||||
* Autodesk AutoCad (.DXF) |
||||
* Scalable Vector Graphics (.SVG) |
||||
* PostScript (.EPS, .PS) |
||||
* TrueType (.TTF) |
||||
* XML Paper Specification (.XPS) |
||||
* Archive file types (.ZIP and .RAR) |
||||
* |
||||
* @see https://docs.google.com/viewer |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetGoogleDocumentViewerRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function accept($asset) |
||||
{ |
||||
$supported_extentions = array('doc', 'docx', 'xls', 'xlsx', 'ppt', 'pptx', 'pdf', 'pages', 'ai', 'psd', 'tiff', 'dxf', 'svg', 'eps', 'ps', 'eps', 'ttf', 'zip', 'rar'); |
||||
return $asset->has_ext($supported_extentions); |
||||
} |
||||
|
||||
protected function url($document_url) |
||||
{ |
||||
return 'https://docs.google.com/viewer?embedded=true&url=' . urlencode($document_url); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (!$this->accept($asset)) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
$url = $this->url($asset->url()); |
||||
|
||||
$title = $asset->title(); |
||||
$description = $asset->get_meta('description'); |
||||
|
||||
$keywords = $asset->get_meta('keywords'); |
||||
|
||||
$size = (int) $asset->config('size'); |
||||
$size = (24 <= $size && $size <= 800) ? $size : 300; |
||||
|
||||
$embed = <<<EOT |
||||
<div style="height:{$size}px;" class="resize vertical" > |
||||
<iframe style=background-color:#ffffff;" width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="$url"></iframe> |
||||
</div> |
||||
<style type="text/css"> |
||||
div.resize.vertical { |
||||
background-color: #EEEEEE; |
||||
border-color: #EEEEEE; |
||||
border-style: solid; |
||||
border-width: 1px; |
||||
resize:vertical; |
||||
overflow: hidden; |
||||
padding-bottom:15px; |
||||
min-height:24px; |
||||
max-height:800px; |
||||
} |
||||
</style> |
||||
EOT; |
||||
|
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
//$result[self::THUMBNAIL] = $image_src; |
||||
$result[self::DESCRIPTION] = $description; |
||||
$result[self::TAGS] = $keywords; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,129 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Google map page renderer. |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetGoogleMapRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function accept($asset) |
||||
{ |
||||
$url = $asset->url(); |
||||
$url = str_replace('http://', '', $url); |
||||
$url = str_replace('https://', '', $url); |
||||
|
||||
$domain = reset(explode('/', $url)); |
||||
return strpos($domain, 'maps.google') !== false; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param string $url |
||||
*/ |
||||
public function explode_url_parameters($url = null) |
||||
{ |
||||
if (strpos($url, '?') === false) |
||||
{ |
||||
return array(); |
||||
} |
||||
|
||||
$result = array(); |
||||
$params = explode('?', $url); |
||||
$params = end($params); |
||||
$params = explode('&', $params); |
||||
foreach ($params as $param) |
||||
{ |
||||
list($key, $val) = explode('=', $param); |
||||
$result[$key] = $val; |
||||
} |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
public function implode_url_parameters($params) |
||||
{ |
||||
$result = array(); |
||||
foreach ($params as $key => $value) |
||||
{ |
||||
if ($value) |
||||
{ |
||||
$result[] = "$key=$value"; |
||||
} |
||||
} |
||||
return join('&', $result); |
||||
} |
||||
|
||||
protected function url($base = 'http:://map.google.com/', $params = array()) |
||||
{ |
||||
$head = reset(explode('?', $base)); |
||||
$items = $this->explode_url_parameters($base); |
||||
foreach ($params as $key => $value) |
||||
{ |
||||
$items[$key] = $value; |
||||
} |
||||
$tail = $this->implode_url_parameters($items); |
||||
$tail = empty($tail) ? '' : "?$tail"; |
||||
return $head . $tail; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (!$this->accept($asset)) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
$params = array('output' => 'embed'); |
||||
|
||||
$base = $asset->url(); |
||||
$url = $this->url($base, $params); |
||||
|
||||
$title = $asset->title(); |
||||
$description = $asset->get_meta('description'); |
||||
|
||||
$keywords = $asset->get_meta('keywords'); |
||||
|
||||
$size = (int) $asset->config('size'); |
||||
$size = (24 <= $size && $size <= 800) ? $size : 300; |
||||
|
||||
$embed = <<<EOT |
||||
<div style="height:{$size}px;" class="resize vertical" > |
||||
<iframe style=background-color:#ffffff;" width="100%" height="100%" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="$url"></iframe> |
||||
</div> |
||||
<style type="text/css"> |
||||
div.resize.vertical { |
||||
background-color: #EEEEEE; |
||||
border-color: #EEEEEE; |
||||
border-style: solid; |
||||
border-width: 1px; |
||||
resize:vertical; |
||||
overflow: hidden; |
||||
padding-bottom:15px; |
||||
min-height:24px; |
||||
max-height:800px; |
||||
} |
||||
</style> |
||||
EOT; |
||||
|
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
$result[self::DESCRIPTION] = $description; |
||||
$result[self::TAGS] = $keywords; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,90 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Google widget/gadget renderer. |
||||
* Accept the following urls: |
||||
* |
||||
* module: http://www.google.com/ig/directory?type=gadgets&url=www.google.com/ig/modules/eyes/eyes.xml |
||||
* directory entry: www.google.com/ig/modules/eyes/eyes.xml |
||||
* configured url: www.gmodules.com/ig/ifr?url=http://www.google.com/ig/modules/eyes/eyes.xml&synd=open&w=320&h=121&title=__MSG_title__&lang=fr&country=ALL&border=%23ffffff%7C3px%2C1px+solid+%23999999&output=js |
||||
* |
||||
* @see http://www.google.com/ig/directory?type=gadgets&url=www.google.com/ig/modules/eyes/eyes.xml |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetGoogleWidgetRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
|
||||
if ($asset->url_match('gmodules.com/ig/') && $asset->url_param('url') != false) |
||||
{ |
||||
$url = $asset->url(); |
||||
$title = $asset->url_param('title'); |
||||
$title = ($title == '__MSG_title__') ? '' : $title; |
||||
|
||||
$embed = <<<EOT |
||||
<script src="$url"></script> |
||||
EOT; |
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
return $result; |
||||
} |
||||
|
||||
if (!$asset->is_gadget()) |
||||
{ |
||||
$url = $asset->url(); |
||||
|
||||
if (!$asset->url_match('google.com/ig/directory')) |
||||
{ |
||||
return false; |
||||
} |
||||
if (!$asset->url_match('type=gadgets')) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
$url = $asset->url_param('url'); |
||||
if (empty($url)) |
||||
{ |
||||
return false; |
||||
} |
||||
$asset = new HttpResource($url); |
||||
if (!$asset->is_gadget()) |
||||
{ |
||||
return false; |
||||
} |
||||
} |
||||
|
||||
$url = $asset->url(); |
||||
if (strpos($url, 'http') !== 0) |
||||
{ |
||||
$url = "http://$url"; |
||||
} |
||||
$url = urlencode($url); |
||||
$title = $asset->title(); |
||||
$title = $title ? $title : $asset->name(); |
||||
|
||||
$size = (int) $asset->config('size'); |
||||
$size = (24 <= $size && $size <= 800) ? $size : 300; |
||||
|
||||
$embed = <<<EOT |
||||
<script src="//www.gmodules.com/ig/ifr?url=$url&w=$size&output=js"></script> |
||||
EOT; |
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,44 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Process image resources. I.e. png, jpeg, etc. |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetImageRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (! $asset->is_image()) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
global $THEME; |
||||
$url = $asset->url(); |
||||
$title = $asset->title(); |
||||
$title = $title ? $title : $asset->name(); |
||||
|
||||
$size = (int) $asset->config('size'); |
||||
$size = (24 <= $size && $size <= 800) ? $size : 300; |
||||
|
||||
$embed = <<<EOT |
||||
<div style="text-align:center"><a href="$url"><img src="{$url}" width="$size" alt="{$title}" title="{$title}"></a></div> |
||||
EOT; |
||||
|
||||
$result = array(); |
||||
$result[self::URL] = $url; |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
$result[self::THUMBNAIL] = $url; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,65 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Media renderer. I.e. video streams that can be embeded through an embed tag. |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetMediaRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function accept($asset) |
||||
{ |
||||
if ($asset->is_video()) |
||||
{ |
||||
return true; |
||||
} |
||||
|
||||
//swf mime type is application/x-shockwave-flash |
||||
return $asset->has_ext('swf'); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (!$this->accept($asset)) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
$url = $asset->url(); |
||||
|
||||
$title = $asset->title(); |
||||
$description = $asset->get_meta('description'); |
||||
$keywords = $asset->get_meta('keywords'); |
||||
|
||||
|
||||
$size = (int) $asset->config('size'); |
||||
$size = (24 <= $size && $size <= 800) ? $size : 300; |
||||
|
||||
$width = $size; |
||||
$height = $size *9/16; |
||||
|
||||
$embed = <<<EOT |
||||
<div style="text-align:center;"><embed style="display:inline-block;" width="{$width}px" height="{$height}px" name="plugin" src="$url" ></div> |
||||
EOT; |
||||
|
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
$result[self::DESCRIPTION] = $description; |
||||
$result[self::TAGS] = $keywords; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,65 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Media Server renderer. |
||||
* |
||||
* Note that some videos are protected. It is therefore not possible to use the |
||||
* autodiscovery feature. That is to get the web page, look at the meta data headers |
||||
* and read the oembed api call. This would only work for public content/javascript |
||||
* bookmarklet. |
||||
* |
||||
* So here we bypass the discovery service and directly call the API endpoint |
||||
* with the page url to retrieve oembed metadata - which happens to be public. |
||||
* |
||||
* @see https://mediaserver.unige.ch |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetMediaserverRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
const API_ENDPOINT = 'http://129.194.20.121/oembed/unige-oembed-provider-test.php'; |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function accept($asset) |
||||
{ |
||||
return $asset->url_match('https://mediaserver.unige.ch/play/') ||$asset->url_match('http://mediaserver.unige.ch/play/'); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (!$this->accept($asset)) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
$width = (int) $asset->config('size'); |
||||
$width = (24 <= $width && $width <= 800) ? $width : 300; |
||||
|
||||
$url = $asset->url(); |
||||
|
||||
$oembed = self::API_ENDPOINT . '?url=' . urlencode($url) . '&maxwidth=' . $width; |
||||
|
||||
$data = HttpResource::fetch_json($oembed); |
||||
if (empty($data)) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
$result[self::THUMBNAIL] = isset($data['thumbnail_url']) ? $data['thumbnail_url'] : ''; |
||||
$result[self::TITLE] = isset($data['title']) ? $data['title'] : ''; |
||||
$result[self::EMBED_SNIPPET] = isset($data['html']) ? $data['html'] : ''; |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,124 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Process html pages that support the oembed protocol. |
||||
* |
||||
* Note that here we rely on the discovery service. That is each page that contains in |
||||
* its metadata the oembed request. |
||||
* |
||||
* @see http://oembed.com/ |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
* |
||||
*/ |
||||
class AssetOembedRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
$link = $asset->get_link('type', 'application/json+oembed'); |
||||
if (empty($link)) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
$width = (int) $asset->config('size'); |
||||
$width = (24 <= $width && $width <= 800) ? $width : 300; |
||||
|
||||
$href = $link['href']; |
||||
$data = HttpResource::fetch_json("$href&maxwidth=$width"); //&maxheight=$height |
||||
if (empty($data)) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
$data['title'] = isset($data['title']) ? $data['title'] : ''; |
||||
$data['width'] = isset($data['width']) ? intval($data['width']) : ''; |
||||
$data['height'] = isset($data['height']) ? intval($data['height']) : ''; |
||||
|
||||
$type = $data['type']; |
||||
$f = array($this, "render_$type"); |
||||
if (is_callable($f)) |
||||
{ |
||||
$result = call_user_func($f, $asset, $data); |
||||
} |
||||
else |
||||
{ |
||||
$result = array(); |
||||
} |
||||
$result[self::THUMBNAIL] = isset($data['thumbnail_url']) ? $data['thumbnail_url'] : ''; |
||||
$result[self::TITLE] = isset($data['title']) ? $data['title'] : ''; |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
protected function render_photo($asset, $data) |
||||
{ |
||||
if ($data['type'] != 'photo') |
||||
{ |
||||
return array(); |
||||
} |
||||
|
||||
$result = array(); |
||||
$html = isset($data['html']) ? $data['html'] : ''; |
||||
if ($html) |
||||
{ |
||||
$result[self::EMBED_SNIPPET] = '<div style="display:inline-block">' . $html . '</div>'; |
||||
return $result; |
||||
} |
||||
|
||||
$title = $data['title']; |
||||
$width = (int)$data['width']; |
||||
$height = (int)$data['height']; |
||||
// $ratio = $height / $width; |
||||
// $height = $ratio * $width; |
||||
|
||||
$url = $data['url']; |
||||
|
||||
$embed = <<<EOT |
||||
<div><a href="$url"><img src="{$url}" width="{$width}" height="{$height}" "alt="{$title}" title="{$title}"></a></div> |
||||
EOT; |
||||
|
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
return $result; |
||||
} |
||||
|
||||
protected function render_video($asset, $data) |
||||
{ |
||||
if ($data['type'] != 'video') |
||||
{ |
||||
return array(); |
||||
} |
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = '<div style="display:inline-block">' . $data['html'] . '</div>'; |
||||
return $result; |
||||
} |
||||
|
||||
protected function render_rich($asset, $data) |
||||
{ |
||||
if ($data['type'] != 'rich') |
||||
{ |
||||
return array(); |
||||
} |
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = '<div style="display:inline-block">' . $data['html'] . '</div>'; |
||||
return $result; |
||||
} |
||||
|
||||
protected function render_link($asset, $data) |
||||
{ |
||||
if ($data['type'] != 'link') |
||||
{ |
||||
return array(); |
||||
} |
||||
return array(); |
||||
} |
||||
|
||||
} |
@ -1,201 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Process pages that support the open graph protocol |
||||
* |
||||
* @see http://ogp.me/ |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetOgRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* Renderer function. Take a http asset as input and return an array containing |
||||
* various properties: metadata, html snippet, etc. |
||||
* |
||||
* @param HttpResource $asset |
||||
* @return array |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
$type = $asset->get_meta('og:type'); |
||||
if (empty($type)) |
||||
{ |
||||
if ($video = $asset->get_meta('og:video')) |
||||
{ |
||||
$type = 'video'; |
||||
} |
||||
else if ($video = $asset->get_meta('og:image')) |
||||
{ |
||||
$type = 'default'; |
||||
} |
||||
} |
||||
if (empty($type)) |
||||
{ |
||||
return array(); |
||||
} |
||||
|
||||
$type = explode('.', $type); |
||||
$type = reset($type); |
||||
$f = array($this, "render_$type"); |
||||
if (is_callable($f)) |
||||
{ |
||||
$result = call_user_func($f, $asset); |
||||
} |
||||
else |
||||
{ |
||||
$result = $this->render_default($asset); |
||||
} |
||||
|
||||
$result[self::TITLE] = $asset->get_meta('og:title'); |
||||
$result[self::THUMBNAIL] = $asset->get_meta('og:image'); |
||||
$result[self::LANGUAGE] = $asset->get_meta('og:language'); |
||||
|
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* @param HttpResource $asset |
||||
* @return array |
||||
*/ |
||||
protected function render_video($asset) |
||||
{ |
||||
$url = $asset->get_meta('og:video'); |
||||
$url = str_replace('?autoPlay=1', '?', $url); |
||||
$url = str_replace('&autoPlay=1', '', $url); |
||||
|
||||
if (empty($url)) |
||||
{ |
||||
return array(); |
||||
} |
||||
|
||||
$type = $asset->get_meta('og:video:type'); |
||||
if ($type) |
||||
{ |
||||
$type = ' type="' . $type . '" '; |
||||
} |
||||
|
||||
$size = (int) $asset->config('size'); |
||||
$size = (24 <= $size && $size <= 800) ? $size : 300; |
||||
|
||||
$width = $asset->get_meta('og:video:width'); |
||||
$width = $width ? $width : $asset->get_meta('video_width'); |
||||
$height = $asset->get_meta('og:video:height'); |
||||
$height = $height ? $height : $asset->get_meta('video_height'); |
||||
|
||||
if ($width) |
||||
{ |
||||
$ratio = $height / $width; |
||||
$base = min($size, $width); |
||||
$width = $base; |
||||
$height = $ratio * $base; |
||||
$size = 'width="' . $width . '" height="' . $height . '"'; |
||||
} |
||||
else |
||||
{ |
||||
$size = 'width="' . $size . '"'; |
||||
} |
||||
|
||||
$embed = <<<EOT |
||||
<embed $type $size src="$url" /> |
||||
EOT; |
||||
|
||||
$result[self::EMBED_TYPE] = $type; |
||||
$result[self::EMBED_URL] = $url; |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TAGS] = $asset->get_meta('og:video:tag'); |
||||
$result[self::CREATED_TIME] = $asset->get_meta('og:video:release_date'); |
||||
$result[self::DURATION] = $asset->get_meta('og:duration'); |
||||
return $result; |
||||
} |
||||
|
||||
protected function render_article($asset) |
||||
{ |
||||
$result = $this->render_default($asset); |
||||
return $result; |
||||
} |
||||
|
||||
protected function render_audio($asset) |
||||
{ |
||||
$result = $this->render_default($asset); |
||||
return $result; |
||||
} |
||||
|
||||
protected function render_book($asset) |
||||
{ |
||||
$result = $this->render_default($asset); |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
* @return array |
||||
*/ |
||||
protected function render_default($asset) |
||||
{ |
||||
$url = $asset->get_meta('og:url'); |
||||
$url = htmlentities($url); |
||||
$title = $asset->get_meta('og:title'); |
||||
$image = $asset->get_meta('og:image'); |
||||
$image = htmlentities($image); |
||||
$width = $asset->get_meta('og:image:width'); |
||||
$height = $asset->get_meta('og:image:height'); |
||||
$description = $asset->get_meta('og:description'); |
||||
$description = $description ? $description : $asset->get_meta('description'); |
||||
|
||||
$size = (int) $asset->config('size'); |
||||
$size = (24 <= $size && $size <= 800) ? $size : 300; |
||||
|
||||
if ($width) |
||||
{ |
||||
$ratio = $height / $width; |
||||
$base = min($size, $width); |
||||
$width = $base; |
||||
$height = $ratio * $base; |
||||
$size = 'width="' . $width . '" height="' . $height . '"'; |
||||
} |
||||
else |
||||
{ |
||||
$size = 'width="' . $size . '"'; |
||||
} |
||||
$embed = <<<EOT |
||||
<div> |
||||
<a href="$url" style="float:left; margin-right:5px; margin-bottom:5px; display:block;"><img src="{$image}" {$size} alt="{$title}" title="{$title}"></a> |
||||
<div style="clear:both;"></div> |
||||
</div> |
||||
EOT; |
||||
|
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::DESCRIPTION] = $asset->get_meta('description'); |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* @param HttpResource $asset |
||||
* @return array |
||||
*/ |
||||
protected function render_image($asset) |
||||
{ |
||||
$size = (int) $asset->config('size'); |
||||
$size = (24 <= $size && $size <= 800) ? $size : 300; |
||||
|
||||
$title = $data['title']; |
||||
$width = $data['width']; |
||||
$height = $data['height']; |
||||
$ratio = $height / $width; |
||||
$base = min($size, $width); |
||||
$width = $base; |
||||
$height = $ratio * $base; |
||||
|
||||
$url = $data['url']; |
||||
|
||||
$embed = <<<EOT |
||||
<a href="$url"><img src="{$url}" width="{$width}" height="{$height} "alt="{$title}" title="{$title}"></a> |
||||
EOT; |
||||
} |
||||
|
||||
} |
@ -1,83 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Generic HTML page renderer. Process any html page. |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetPageRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
global $THEME; |
||||
$url = $asset->url(); |
||||
$title = $asset->title(); |
||||
$title = $title ? $title : $asset->name(); |
||||
$description = $asset->get_meta('description'); |
||||
$description = $description; |
||||
|
||||
$keywords = $asset->get_meta('keywords'); |
||||
|
||||
$image_src = $asset->get_link('rel', 'image_src'); |
||||
$image_src = $image_src ? $image_src['href'] : false; |
||||
|
||||
if (empty($image_src)) |
||||
{ |
||||
$image_src = $this->get_icon($asset); |
||||
} |
||||
|
||||
$icon = $this->get_icon($asset); |
||||
|
||||
$image_src = $asset->canonic_url($image_src); |
||||
$icon = $asset->canonic_url($icon); |
||||
|
||||
$embed = <<<EOT |
||||
<a href="$url"> |
||||
<img src="{$image_src}" alt="{$title}" title="{$title}" style="float:left; margin-right:5px; margin-bottom:5px; " > |
||||
</a> |
||||
$description |
||||
<span style="clear:both;"></span> |
||||
EOT; |
||||
|
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
$result[self::THUMBNAIL] = $image_src; |
||||
$result[self::DESCRIPTION] = $description; |
||||
$result[self::ICON] = $icon; |
||||
$result[self::TAGS] = $keywords; |
||||
return $result; |
||||
} |
||||
|
||||
function get_icon($asset) |
||||
{ |
||||
|
||||
$icon = $asset->get_link('rel', 'apple-touch-icon'); |
||||
$icon = $icon ? $icon['href'] : false; |
||||
if (empty($icon)) |
||||
{ |
||||
$icon = $asset->get_link('rel', 'fluid-icon'); |
||||
$icon = $icon ? $icon['href'] : false; |
||||
} |
||||
if (empty($icon)) |
||||
{ |
||||
$icon = $asset->get_link('rel', 'shortcut icon'); |
||||
$icon = $icon ? $icon['href'] : false; |
||||
} |
||||
if (empty($icon)) |
||||
{ |
||||
$icon = $asset->get_link('rel', 'icon'); |
||||
$icon = $icon ? $icon['href'] : false; |
||||
} |
||||
return $icon; |
||||
} |
||||
|
||||
} |
@ -1,95 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Rss renderer. Display RSS thanks to Google feed control. |
||||
* |
||||
* @see http://www.google.com/uds/solutions/dynamicfeed/reference.html |
||||
* @see http://code.google.com/apis/ajax/playground/#dynamic_feed_control_-_vertical |
||||
* |
||||
* @copyright (c) 2011 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetRssRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (!$asset->is_rss()) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
$url = $asset->url(); |
||||
$title = $asset->title(); |
||||
$id = 'a' . md5($url); |
||||
|
||||
$embed = <<<EOT |
||||
<style type="text/css"> |
||||
.gfg-root { |
||||
border: none; |
||||
font-family: inherit; |
||||
} |
||||
</style> |
||||
<script type="text/javascript" src="http://www.google.com/jsapi"></script> |
||||
<script src="http://www.google.com/uds/solutions/dynamicfeed/gfdynamicfeedcontrol.js" type="text/javascript"></script> |
||||
<script type="text/javascript"> |
||||
|
||||
function init() |
||||
{ |
||||
if (typeof this.has_run == 'undefined' ) |
||||
{ |
||||
this.has_run = true; |
||||
} |
||||
else |
||||
{ |
||||
return; |
||||
} |
||||
var head = document.getElementsByTagName('head')[0]; |
||||
|
||||
var element = document.createElement('link'); |
||||
element.type = 'text/css'; |
||||
element.rel = 'stylesheet'; |
||||
element.href = 'http://www.google.com/uds/solutions/dynamicfeed/gfdynamicfeedcontrol.css'; |
||||
head.appendChild(element); |
||||
} |
||||
|
||||
function load_$id() { |
||||
var feeds = [ |
||||
{ |
||||
title: ' ', |
||||
url: '$url' |
||||
} |
||||
]; |
||||
|
||||
var options = { |
||||
stacked : false, |
||||
horizontal : false, |
||||
title : '', |
||||
numResults : 10 |
||||
}; |
||||
|
||||
new GFdynamicFeedControl(feeds, '$id', options); |
||||
document.getElementById('content').style.width = "500px"; |
||||
} |
||||
|
||||
|
||||
init(); |
||||
google.load('feeds', '1'); |
||||
google.setOnLoadCallback(load_$id); |
||||
</script> |
||||
<div id="$id" style="min-height:271px;">Loading...</div> |
||||
EOT; |
||||
|
||||
|
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::TITLE] = $title; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,76 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Scratch renderer. |
||||
* |
||||
* @see http://scratch.mit.edu/projects/ |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht |
||||
*/ |
||||
class AssetScratchRenderer extends AssetRenderer |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function accept($asset) |
||||
{ |
||||
return $asset->url_match('http://scratch.mit.edu/projects/'); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param HttpResource $asset |
||||
*/ |
||||
public function render($asset) |
||||
{ |
||||
if (!$this->accept($asset)) |
||||
{ |
||||
return; |
||||
} |
||||
|
||||
$matches = array(); |
||||
$pattern = "#http:\/\/scratch.mit.edu\/projects\/(\w+)/(\d*)\/?#ims"; |
||||
preg_match($pattern, $asset->url(), $matches); |
||||
|
||||
$url = $matches[0]; |
||||
$author = $matches[1]; |
||||
$project_id = $matches[2]; |
||||
|
||||
$project_url = "../../static/projects/$author/$project_id.sb"; |
||||
$image_url = "http://scratch.mit.edu/static/projects/$author/{$project_id}_med.png"; |
||||
$thumb_url = "http://scratch.mit.edu/static/projects/$author/{$project_id}_sm.png"; |
||||
|
||||
$height = 387; |
||||
$width = 482; |
||||
|
||||
if (function_exists('get_string')) |
||||
{ |
||||
$no_java = get_string('no_java', 'artefact.extresource'); |
||||
} |
||||
else |
||||
{ |
||||
$no_java = 'Java is not installed on your computer. You must install java first.'; |
||||
} |
||||
|
||||
$embed = <<<EOT |
||||
<object type="application/x-java-applet" width="$width" height="$height" style="display:block" id="ProjectApplet"> |
||||
|
||||
<param name="codebase" value="http://scratch.mit.edu/static/misc"> |
||||
<param name="archive" value="ScratchApplet.jar"> |
||||
<param name="code" value="ScratchApplet"> |
||||
<param name="project" value="$project_url"> |
||||
<pre>$no_java</pre> |
||||
<img alt="" src="$image_url"> |
||||
</object> |
||||
EOT; |
||||
$result = array(); |
||||
$result[self::EMBED_SNIPPET] = $embed; |
||||
$result[self::THUMBNAIL] = $thumb_url; |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,131 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class Curl |
||||
{ |
||||
|
||||
protected static $default_options = array(); |
||||
|
||||
static function get_default_options($options = array()) |
||||
{ |
||||
if (empty(self::$default_options)) { |
||||
self::$default_options[CURLOPT_HEADER] = false; |
||||
self::$default_options[CURLOPT_RETURNTRANSFER] = true; |
||||
self::$default_options[CURLOPT_SSL_VERIFYPEER] = false; |
||||
} |
||||
|
||||
$result = self::$default_options; |
||||
foreach ($options as $key => $value) { |
||||
$result[$key] = $value; |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
static function set_default_option($key, $value) |
||||
{ |
||||
$options = $this->get_options(array($key => $value)); |
||||
self::$default_options = $options; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param string $url |
||||
* @param array $options |
||||
* @return Curl |
||||
*/ |
||||
static function get($url, $options = array()) |
||||
{ |
||||
$options[CURLOPT_HTTPGET] = true; |
||||
$result = new self($url, $options); |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param string $url |
||||
* @param array $fields |
||||
* @param array $options |
||||
* @return Curl |
||||
*/ |
||||
static function post($url, $fields, $options = array()) |
||||
{ |
||||
$options[CURLOPT_POST] = true; |
||||
$options[CURLOPT_POSTFIELDS] = $fields; |
||||
$result = new self($url, $options); |
||||
return $result; |
||||
} |
||||
|
||||
protected $url = ''; |
||||
protected $options = array(); |
||||
protected $content = ''; |
||||
protected $info = array(); |
||||
protected $error = ''; |
||||
protected $error_no = 0; |
||||
|
||||
function __construct($url, $options = array()) |
||||
{ |
||||
$this->url = $url; |
||||
$this->options = self::get_default_options($options); |
||||
} |
||||
|
||||
function url() |
||||
{ |
||||
return $this->url; |
||||
} |
||||
|
||||
function options() |
||||
{ |
||||
return $this->options; |
||||
} |
||||
|
||||
function execute() |
||||
{ |
||||
$ch = curl_init(); |
||||
|
||||
$options = $this->options; |
||||
$options[CURLOPT_URL] = $this->url; |
||||
curl_setopt_array($ch, $options); |
||||
|
||||
$this->content = curl_exec($ch); |
||||
$this->error = curl_error($ch); |
||||
$this->info = curl_getinfo($ch); |
||||
$this->error_no = curl_errno($ch); |
||||
|
||||
curl_close($ch); |
||||
|
||||
return $this->content; |
||||
} |
||||
|
||||
function content() |
||||
{ |
||||
return $this->content; |
||||
} |
||||
|
||||
/** |
||||
* @return array|string |
||||
*/ |
||||
function info($key = false) |
||||
{ |
||||
if ($key) { |
||||
return isset($this->info[$key]) ? $this->info[$key] : false; |
||||
} else { |
||||
return $this->info; |
||||
} |
||||
} |
||||
|
||||
function error() |
||||
{ |
||||
return $this->error; |
||||
} |
||||
|
||||
function error_no() |
||||
{ |
||||
return $this->error_no; |
||||
} |
||||
|
||||
} |
@ -1,72 +0,0 @@ |
||||
<?php |
||||
|
||||
namespace net; |
||||
|
||||
use Curl; |
||||
|
||||
/** |
||||
* Description of channel |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class HttpChannel |
||||
{ |
||||
|
||||
/** |
||||
* |
||||
* @param string $url |
||||
* @param type $modules |
||||
* @return HttpChannel |
||||
*/ |
||||
static function create($url, $modules = array()) |
||||
{ |
||||
return new self($url, $modules); |
||||
} |
||||
|
||||
protected $base_url = ''; |
||||
protected $modules = array(); |
||||
|
||||
public function __construct($base_url = '', $modules = array()) |
||||
{ |
||||
$this->base_url = $base_url; |
||||
$this->modules = $modules; |
||||
} |
||||
|
||||
function modules() |
||||
{ |
||||
return $this->modules; |
||||
} |
||||
|
||||
function get($url, $parameters) |
||||
{ |
||||
$options = $this->get_options(); |
||||
$url = $this->base_url . $url; |
||||
return Curl::get($url, $options)->execute(); |
||||
} |
||||
|
||||
function post($url, $fields) |
||||
{ |
||||
$options = $this->get_options(); |
||||
$url = $this->base_url . $url; |
||||
return Curl::post($url, $fields, $options)->execute(); |
||||
} |
||||
|
||||
protected function get_options() |
||||
{ |
||||
$result = array(); |
||||
$modules = $this->modules(); |
||||
foreach ($modules as $module) { |
||||
if (is_array($module)) { |
||||
$options = $module; |
||||
} else { |
||||
|
||||
$options = $module->get_options(); |
||||
} |
||||
$result = array_merge($result, $options); |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
} |
@ -1,149 +0,0 @@ |
||||
<?php |
||||
|
||||
namespace Portfolio; |
||||
|
||||
/** |
||||
* An artefact is any object the user can display in its portfolio. |
||||
* |
||||
* The artefact point either to a local file or to a url from which the object's content |
||||
* can be fetched. |
||||
* |
||||
* Usage |
||||
* |
||||
* $artefact = new artefact(); |
||||
* $artefact->set_path('...'); |
||||
* |
||||
* or |
||||
* |
||||
* |
||||
* $artefact = new artefact(); |
||||
* $artefact->set_url('...'); |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class Artefact |
||||
{ |
||||
|
||||
protected $id = ''; |
||||
protected $mime_type = ''; |
||||
protected $name = ''; |
||||
protected $description = ''; |
||||
protected $path = ''; |
||||
protected $url = ''; |
||||
protected $creation_date = ''; |
||||
protected $modification_date = ''; |
||||
protected $metadata = null; |
||||
|
||||
/** |
||||
* |
||||
* @param string $file Either url or file path |
||||
*/ |
||||
public function __construct($file = '') |
||||
{ |
||||
if ($file) { |
||||
if (strpos($file, 'http') !== false) { |
||||
$this->url = $file; |
||||
} else { |
||||
$this->path = $file; |
||||
} |
||||
} |
||||
$this->id = uniqid('', true); |
||||
$this->mime_type = ''; |
||||
$time = time(); |
||||
$this->creation_date = $time; |
||||
$this->modification_date = $time; |
||||
} |
||||
|
||||
public function get_id() |
||||
{ |
||||
return $this->id; |
||||
} |
||||
|
||||
public function set_id($value) |
||||
{ |
||||
$this->id = $value; |
||||
} |
||||
|
||||
public function get_name() |
||||
{ |
||||
return $this->name; |
||||
} |
||||
|
||||
public function set_name($value) |
||||
{ |
||||
$this->name = $value; |
||||
} |
||||
|
||||
public function get_mime_type() |
||||
{ |
||||
return $this->mime_type; |
||||
} |
||||
|
||||
public function set_mime_type($value) |
||||
{ |
||||
$this->mime_type = $value; |
||||
} |
||||
|
||||
public function get_description() |
||||
{ |
||||
return $this->description; |
||||
} |
||||
|
||||
public function set_description($value) |
||||
{ |
||||
$this->description = $value; |
||||
} |
||||
|
||||
public function get_path() |
||||
{ |
||||
return $this->path; |
||||
} |
||||
|
||||
public function set_path($value) |
||||
{ |
||||
$this->path = $value; |
||||
} |
||||
|
||||
public function get_url() |
||||
{ |
||||
return $this->url; |
||||
} |
||||
|
||||
public function set_url($value) |
||||
{ |
||||
$this->url = $value; |
||||
} |
||||
|
||||
public function get_creation_date() |
||||
{ |
||||
return $this->creation_date; |
||||
} |
||||
|
||||
public function set_creation_date($value) |
||||
{ |
||||
$this->creation_date = $value; |
||||
} |
||||
|
||||
public function get_modification_date() |
||||
{ |
||||
return $this->modification_date; |
||||
} |
||||
|
||||
public function set_modification_date($value) |
||||
{ |
||||
$this->modification_date = $value; |
||||
} |
||||
|
||||
public function get_metadata() |
||||
{ |
||||
return $this->metadata; |
||||
} |
||||
|
||||
public function set_metadata($value) |
||||
{ |
||||
$this->metadata = $value; |
||||
} |
||||
|
||||
} |
@ -1,35 +0,0 @@ |
||||
<?php |
||||
|
||||
namespace Portfolio; |
||||
|
||||
use Header; |
||||
|
||||
/** |
||||
* Download file to your desktop |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class Download extends Portfolio |
||||
{ |
||||
|
||||
function __construct() |
||||
{ |
||||
parent::__construct('download', null); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param User $user |
||||
* @param Artefact $artefact |
||||
* @return bool |
||||
*/ |
||||
function send($user, $artefact) |
||||
{ |
||||
if ($artefact->get_url()) { |
||||
Header::location($artefact->get_url()); |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,59 +0,0 @@ |
||||
<?php |
||||
|
||||
namespace Portfolio; |
||||
|
||||
use Header; |
||||
|
||||
/** |
||||
* Interface with a Mahara portfolio. |
||||
* |
||||
* This class requires that the connect mahara plugin is installed and enabled. |
||||
* |
||||
* @see https://mahara.org/ |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class Mahara extends Portfolio |
||||
{ |
||||
|
||||
protected $url = ''; |
||||
|
||||
/** |
||||
* |
||||
* @param string $url The root url |
||||
*/ |
||||
function __construct($url) |
||||
{ |
||||
$name = md5($url); |
||||
parent::__construct($name, null); |
||||
$this->url = $url; |
||||
} |
||||
|
||||
function get_url() |
||||
{ |
||||
return $this->url; |
||||
} |
||||
|
||||
function get_title(){ |
||||
$result = parent::get_title(); |
||||
$result = $result ? $result : 'Mahara'; |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param User $user |
||||
* @param Artefact $artefact |
||||
* @return bool |
||||
*/ |
||||
function send($user, $artefact) |
||||
{ |
||||
$root = $this->get_url(); |
||||
rtrim($root, '/'); |
||||
$url = $artefact->get_url(); |
||||
$url = $root . '/artefact/connect/upload.php?url=' . urlencode($url) . '&extract=true'; |
||||
Header::location($url); |
||||
} |
||||
|
||||
} |
@ -1,106 +0,0 @@ |
||||
<?php |
||||
|
||||
namespace Portfolio; |
||||
|
||||
/** |
||||
* Portfolio are used to display and share content. The porfolio class represents |
||||
* one (external) portfolio application and allows to share content with an it. |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class Portfolio |
||||
{ |
||||
|
||||
public static function none() |
||||
{ |
||||
static $result = null; |
||||
if (empty($result)) { |
||||
$result = new self('empty', null); |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
public static function all() |
||||
{ |
||||
|
||||
} |
||||
|
||||
protected $name; |
||||
protected $title = ''; |
||||
protected $description = ''; |
||||
protected $channel; |
||||
|
||||
function __construct($name, $channel = null) |
||||
{ |
||||
$this->name = $name; |
||||
$this->title = $name; |
||||
$this->channel = $channel; |
||||
} |
||||
|
||||
/** |
||||
* The name of the portfolio - i.e. the unique id. |
||||
* @return type |
||||
*/ |
||||
function get_name() |
||||
{ |
||||
return $this->name; |
||||
} |
||||
|
||||
/** |
||||
* Title for the end user. |
||||
* |
||||
* @return type |
||||
*/ |
||||
function get_title() |
||||
{ |
||||
return $this->title; |
||||
} |
||||
|
||||
function set_title($value) |
||||
{ |
||||
$this->title = $value; |
||||
} |
||||
|
||||
/** |
||||
* Description for the end user. |
||||
* |
||||
* @return type |
||||
*/ |
||||
function get_description() |
||||
{ |
||||
return $this->description; |
||||
} |
||||
|
||||
function set_description($value) |
||||
{ |
||||
$this->description = $value; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @return HttpChannel |
||||
*/ |
||||
function channel() |
||||
{ |
||||
return $this->channel; |
||||
} |
||||
|
||||
function __toString() |
||||
{ |
||||
return $this->name; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param User $user |
||||
* @param Artefact $artefact |
||||
* @return bool |
||||
*/ |
||||
function send($user, $artefact) |
||||
{ |
||||
return false; |
||||
} |
||||
|
||||
} |
@ -1,19 +0,0 @@ |
||||
<?php |
||||
|
||||
namespace Portfolio; |
||||
|
||||
/** |
||||
* A portfolio user is used to share identity between two applications. |
||||
* Not used at this point. |
||||
* |
||||
* @copyright (c) 2012 University of Geneva |
||||
* @license GNU General Public License - http://www.gnu.org/copyleft/gpl.html |
||||
* @author Laurent Opprecht <laurent@opprecht.info> |
||||
*/ |
||||
class User |
||||
{ |
||||
public $id; |
||||
public $email; |
||||
public $token; |
||||
|
||||
} |
@ -1,86 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Header utility functions. |
||||
* |
||||
* @license see /license.txt |
||||
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva |
||||
*/ |
||||
class Header |
||||
{ |
||||
|
||||
public static function response_code($response_code) |
||||
{ |
||||
if (function_exists('http_response_code')) { |
||||
http_response_code($response_code); |
||||
return; |
||||
} |
||||
|
||||
switch ($response_code) { |
||||
case 400: |
||||
header("HTTP/1.0 $response_code Bad Request"); |
||||
case 401: |
||||
header("HTTP/1.0 $response_code Unauthorized"); |
||||
case 402: |
||||
header("HTTP/1.0 $response_code Payment Required"); |
||||
case 403: |
||||
header("HTTP/1.0 $response_code Forbidden"); |
||||
case 404: |
||||
header("HTTP/1.0 $response_code Not Found"); |
||||
default: |
||||
header("HTTP/1.0 $response_code"); |
||||
} |
||||
} |
||||
|
||||
public static function content_type($mime_type, $charset = '') |
||||
{ |
||||
if (empty($mime_type)) { |
||||
return; |
||||
} |
||||
$type = $charset ? "$mime_type;charset=$charset" : $mime_type; |
||||
header('Content-type: ' . $type); |
||||
} |
||||
|
||||
public static function content_type_xml() |
||||
{ |
||||
header('Content-type: text/xml'); |
||||
} |
||||
|
||||
public static function content_type_json() |
||||
{ |
||||
header('Content-type: application/json'); |
||||
} |
||||
|
||||
public static function content_type_javascript() |
||||
{ |
||||
header('Content-type: application/javascript'); |
||||
} |
||||
|
||||
/** |
||||
* Redirect the navigator to the specified url. |
||||
* |
||||
* @param string $url |
||||
*/ |
||||
public static function location($url) |
||||
{ |
||||
header("Location: $url"); |
||||
exit; |
||||
} |
||||
|
||||
public static function expires($timestamp) |
||||
{ |
||||
$value = gmdate('D, d M Y H:i:s \G\M\T', $timestamp); |
||||
header('Expires: ' . $value); |
||||
} |
||||
|
||||
public static function cache_control($value) |
||||
{ |
||||
header('Cache-Control: ' . $value); |
||||
} |
||||
|
||||
public static function pragma($value) |
||||
{ |
||||
header('Pragma: ' . $value); |
||||
} |
||||
|
||||
} |
@ -1,70 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Provides access to various HTTP request elements: GET, POST, FILE, etc paramaters. |
||||
|
||||
* @license see /license.txt |
||||
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva |
||||
*/ |
||||
|
||||
class Request2 |
||||
{ |
||||
|
||||
public static function get($key, $default = null) |
||||
{ |
||||
return isset($_REQUEST[$key]) ? $_REQUEST[$key] : $default; |
||||
} |
||||
|
||||
public static function has($key){ |
||||
return isset($_REQUEST[$key]); |
||||
} |
||||
|
||||
/** |
||||
* Returns true if the request is a GET request. False otherwise. |
||||
* |
||||
* @return bool |
||||
*/ |
||||
public static function is_get() |
||||
{ |
||||
$method = self::server()->request_method(); |
||||
$method = strtoupper($method); |
||||
return $method == 'GET'; |
||||
} |
||||
|
||||
public static function post($key, $default = null) |
||||
{ |
||||
return isset($_POST[$key]) ? $_POST[$key] : $default; |
||||
} |
||||
|
||||
/** |
||||
* Returns true if the request is a POST request. False otherwise. |
||||
* |
||||
* @return bool |
||||
*/ |
||||
public static function is_post() |
||||
{ |
||||
$method = self::server()->request_method(); |
||||
$method = strtoupper($method); |
||||
return $method == 'POST'; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @return RequestServer |
||||
*/ |
||||
static function server() |
||||
{ |
||||
return RequestServer::instance(); |
||||
} |
||||
|
||||
static function file($key, $default = null) |
||||
{ |
||||
return isset($_FILES[$key]) ? $_FILES[$key] : $default; |
||||
} |
||||
|
||||
static function environment($key, $default = null) |
||||
{ |
||||
return isset($_ENV[$key]) ? $_ENV[$key] : $default; |
||||
} |
||||
|
||||
} |
@ -1,302 +0,0 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Provides access to the $_SERVER variable. Useful to have autocompletion working. |
||||
* Access through the Request class. |
||||
* |
||||
* Example: |
||||
* |
||||
* Request :: server()-> request_uri() |
||||
* |
||||
* |
||||
* @license see /license.txt |
||||
* @author Laurent Opprecht <laurent@opprecht.info> for the Univesity of Geneva |
||||
*/ |
||||
class RequestServer |
||||
{ |
||||
|
||||
public static function instance() |
||||
{ |
||||
static $result = null; |
||||
if (empty($result)) { |
||||
$result = new self(); |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
function get($key, $default = null) |
||||
{ |
||||
return isset($_SERVER[$key]) ? $_SERVER[$key] : null; |
||||
} |
||||
|
||||
/** |
||||
* The timestamp of the start of the request. Available since PHP 5.1.0. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function request_time() |
||||
{ |
||||
return isset($_SERVER['REQUEST_TIME']) ? $_SERVER['REQUEST_TIME'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Contents of the Host: header from the current request, if there is one. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function http_host() |
||||
{ |
||||
return isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page. A typical example is: Mozilla/4.5 [en] (X11; U; Linux 2.2.9 i586). Among other things, you can use this value with get_browser() to tailor your page's output to the capabilities of the user agent. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function http_user_agent() |
||||
{ |
||||
return isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Contents of the Accept: header from the current request, if there is one. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function http_accept() |
||||
{ |
||||
return isset($_SERVER['HTTP_ACCEPT']) ? $_SERVER['HTTP_ACCEPT'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function http_accept_language() |
||||
{ |
||||
return isset($_SERVER['HTTP_ACCEPT_LANGUAGE']) ? $_SERVER['HTTP_ACCEPT_LANGUAGE'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Contents of the Accept-Encoding: header from the current request, if there is one. Example: 'gzip'. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function http_accept_encoding() |
||||
{ |
||||
return isset($_SERVER['HTTP_ACCEPT_ENCODING']) ? $_SERVER['HTTP_ACCEPT_ENCODING'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Contents of the Connection: header from the current request, if there is one. Example: 'Keep-Alive'. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function http_connection() |
||||
{ |
||||
return isset($_SERVER['HTTP_CONNECTION']) ? $_SERVER['HTTP_CONNECTION'] : null; |
||||
} |
||||
|
||||
function http_cookie() |
||||
{ |
||||
return isset($_SERVER['HTTP_COOKIE']) ? $_SERVER['HTTP_COOKIE'] : null; |
||||
} |
||||
|
||||
function http_cache_control() |
||||
{ |
||||
return isset($_SERVER['HTTP_CACHE_CONTROL']) ? $_SERVER['HTTP_CACHE_CONTROL'] : null; |
||||
} |
||||
|
||||
function path() |
||||
{ |
||||
return isset($_SERVER['PATH']) ? $_SERVER['PATH'] : null; |
||||
} |
||||
|
||||
function systemroot() |
||||
{ |
||||
return isset($_SERVER['SystemRoot']) ? $_SERVER['SystemRoot'] : null; |
||||
} |
||||
|
||||
function comspec() |
||||
{ |
||||
return isset($_SERVER['COMSPEC']) ? $_SERVER['COMSPEC'] : null; |
||||
} |
||||
|
||||
function pathext() |
||||
{ |
||||
return isset($_SERVER['PATHEXT']) ? $_SERVER['PATHEXT'] : null; |
||||
} |
||||
|
||||
function windir() |
||||
{ |
||||
return isset($_SERVER['WINDIR']) ? $_SERVER['WINDIR'] : null; |
||||
} |
||||
|
||||
function server_signature() |
||||
{ |
||||
return isset($_SERVER['SERVER_SIGNATURE']) ? $_SERVER['SERVER_SIGNATURE'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Server identification string, given in the headers when responding to requests. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function server_software() |
||||
{ |
||||
return isset($_SERVER['SERVER_SOFTWARE']) ? $_SERVER['SERVER_SOFTWARE'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function server_name() |
||||
{ |
||||
return isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The IP address of the server under which the current script is executing. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function server_addr() |
||||
{ |
||||
return isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : null; |
||||
} |
||||
|
||||
function server_port() |
||||
{ |
||||
return isset($_SERVER['SERVER_PORT']) ? $_SERVER['SERVER_PORT'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The IP address from which the user is viewing the current page. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function remote_addr() |
||||
{ |
||||
return isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The document root directory under which the current script is executing, as defined in the server's configuration file. |
||||
* @return string |
||||
*/ |
||||
function document_root() |
||||
{ |
||||
return isset($_SERVER['DOCUMENT_ROOT']) ? $_SERVER['DOCUMENT_ROOT'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function server_admin() |
||||
{ |
||||
return isset($_SERVER['SERVER_ADMIN']) ? $_SERVER['SERVER_ADMIN'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The absolute pathname of the currently executing script. |
||||
* |
||||
* Note: |
||||
* |
||||
* If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function script_filename() |
||||
{ |
||||
return isset($_SERVER['SCRIPT_FILENAME']) ? $_SERVER['SCRIPT_FILENAME'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The port being used on the user's machine to communicate with the web server. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function remote_port() |
||||
{ |
||||
return isset($_SERVER['REMOTE_PORT']) ? $_SERVER['REMOTE_PORT'] : null; |
||||
} |
||||
|
||||
/** |
||||
* What revision of the CGI specification the server is using; i.e. 'CGI/1.1'. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function gateway_interface() |
||||
{ |
||||
return isset($_SERVER['GATEWAY_INTERFACE']) ? $_SERVER['GATEWAY_INTERFACE'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0'; |
||||
* |
||||
* @return string |
||||
*/ |
||||
function server_protocol() |
||||
{ |
||||
return isset($_SERVER['SERVER_PROTOCOL']) ? $_SERVER['SERVER_PROTOCOL'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'. |
||||
* |
||||
* Note: |
||||
* PHP script is terminated after sending headers (it means after producing any output without output buffering) if the request method was HEAD. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function request_method() |
||||
{ |
||||
return isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The query string, if any, via which the page was accessed. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function query_string() |
||||
{ |
||||
return isset($_SERVER['QUERY_STRING']) ? $_SERVER['QUERY_STRING'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The URI which was given in order to access this page; for instance, '/index.html'. |
||||
* @return string |
||||
*/ |
||||
function request_uri() |
||||
{ |
||||
return isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null; |
||||
} |
||||
|
||||
/** |
||||
* Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function script_name() |
||||
{ |
||||
return isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : null; |
||||
} |
||||
|
||||
/** |
||||
* The filename of the currently executing script, relative to the document root. For instance, $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar would be /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (i.e. included) file. If PHP is running as a command-line processor this variable contains the script name since PHP 4.3.0. Previously it was not available. |
||||
* |
||||
* @return string |
||||
*/ |
||||
function php_self() |
||||
{ |
||||
return isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : null; |
||||
} |
||||
|
||||
} |
File diff suppressed because it is too large
Load Diff
@ -1,138 +0,0 @@ |
||||
<?php |
||||
|
||||
/* |
||||
* $Id$ |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
* This software consists of voluntary contributions made by many individuals |
||||
* and is licensed under the LGPL. For more information, see |
||||
* <http://www.doctrine-project.org>. |
||||
*/ |
||||
|
||||
namespace Tools; |
||||
|
||||
use Doctrine\ORM\Mapping\ClassMetadataInfo, |
||||
Doctrine\ORM\Mapping\AssociationMapping, |
||||
Doctrine\Common\Util\Inflector; |
||||
|
||||
/** |
||||
* Class to generate entity repository classes |
||||
* |
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
||||
* @link www.doctrine-project.org |
||||
* @since 2.0 |
||||
* @version $Revision$ |
||||
* @author Benjamin Eberlei <kontakt@beberlei.de> |
||||
* @author Guilherme Blanco <guilhermeblanco@hotmail.com> |
||||
* @author Jonathan Wage <jonwage@gmail.com> |
||||
* @author Roman Borschel <roman@code-factory.org> |
||||
*/ |
||||
class EntityRepositoryGenerator |
||||
{ |
||||
|
||||
protected static $_template = |
||||
'<?php |
||||
|
||||
namespace Entity\Repository; |
||||
use \db; |
||||
|
||||
/** |
||||
* |
||||
* @license see /license.txt |
||||
* @author autogenerated |
||||
*/ |
||||
class <className> extends <extends> |
||||
{ |
||||
|
||||
/** |
||||
* @return \Entity\Repository\<className> |
||||
*/ |
||||
public static function instance(){ |
||||
static $result = false; |
||||
if($result === false){ |
||||
$result = db::instance()->get_repository(\'\\Entity\\<entityName>\'); |
||||
} |
||||
return $result; |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param EntityManager $em The EntityManager to use. |
||||
* @param ClassMetadata $class The class descriptor. |
||||
*/ |
||||
public function __construct($em, $class){ |
||||
parent::__construct($em, $class); |
||||
} |
||||
|
||||
}'; |
||||
|
||||
public function generateEntityRepositoryClass($name) |
||||
{ |
||||
$name = Inflector::tableize($name); |
||||
$is_course_table = (strpos($name, 'c_') === 0); |
||||
if ($is_course_table) { |
||||
$name = substr($name, 2, strlen($name) - 2); |
||||
} |
||||
$name = Inflector::classify($name); |
||||
$className = $name; |
||||
//$namespace = substr($fullClassName, 0, strrpos($fullClassName, '\\')); |
||||
//$className = substr($fullClassName, strrpos($fullClassName, '\\') + 1, strlen($fullClassName)); |
||||
|
||||
$is_course_table = $metadata->is_course_table; |
||||
|
||||
|
||||
$variables = array( |
||||
'<namespace>' => $namespace, |
||||
'<className>' => $className, |
||||
'<entityName>' => str_replace('Repository', '', $className), |
||||
'<extends>' => $is_course_table ? '\CourseEntityRepository' : '\EntityRepository' |
||||
); |
||||
return str_replace(array_keys($variables), array_values($variables), self::$_template); |
||||
} |
||||
|
||||
/** |
||||
* |
||||
* @param type $name |
||||
* @param type $outputDirectory |
||||
*/ |
||||
public function writeEntityRepositoryClass($name, $outputDirectory) |
||||
{ |
||||
$name = explode('\\', $name); |
||||
$name = end($name); |
||||
$name = Inflector::tableize($name); |
||||
$is_course_table = (strpos($name, 'c_') === 0); |
||||
if ($is_course_table) { |
||||
$name = substr($name, 2, strlen($name) - 2); |
||||
} |
||||
$name = Inflector::classify($name) . 'Repository'; |
||||
$fullClassName = $name; |
||||
|
||||
$file_name = Inflector::tableize($name); |
||||
|
||||
$code = $this->generateEntityRepositoryClass($fullClassName); |
||||
|
||||
$path = $outputDirectory . DIRECTORY_SEPARATOR |
||||
. str_replace('\\', \DIRECTORY_SEPARATOR, $file_name) . '.class.php'; |
||||
$dir = dirname($path); |
||||
|
||||
if (!is_dir($dir)) { |
||||
mkdir($dir, 0777, true); |
||||
} |
||||
|
||||
if (!file_exists($path)) { |
||||
file_put_contents($path, $code); |
||||
} |
||||
} |
||||
|
||||
} |
@ -1,214 +0,0 @@ |
||||
<?php |
||||
|
||||
/* |
||||
* $Id$ |
||||
* |
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
||||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||
* |
||||
* This software consists of voluntary contributions made by many individuals |
||||
* and is licensed under the LGPL. For more information, see |
||||
* <http://www.doctrine-project.org>. |
||||
*/ |
||||
|
||||
namespace Tools; |
||||
|
||||
use Doctrine\ORM\Mapping\ClassMetadataInfo; |
||||
use Doctrine\ORM\Tools\Export\Driver\AbstractExporter; |
||||
|
||||
/** |
||||
* ClassMetadata exporter for Doctrine YAML mapping files |
||||
* |
||||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
||||
* @link www.doctrine-project.org |
||||
* @since 2.0 |
||||
* @version $Revision$ |
||||
* @author Jonathan Wage <jonwage@gmail.com> |
||||
*/ |
||||
class YamlExporter extends AbstractExporter |
||||
{ |
||||
protected $_extension = '.dcm.yml'; |
||||
|
||||
/** |
||||
* Converts a single ClassMetadata instance to the exported format |
||||
* and returns it |
||||
* |
||||
* TODO: Should this code be pulled out in to a toArray() method in ClassMetadata |
||||
* |
||||
* @param ClassMetadataInfo $metadata |
||||
* @return mixed $exported |
||||
*/ |
||||
public function exportClassMetadata(ClassMetadataInfo $metadata) |
||||
{ |
||||
$array = array(); |
||||
|
||||
if ($metadata->isMappedSuperclass) { |
||||
$array['type'] = 'mappedSuperclass'; |
||||
} else { |
||||
$array['type'] = 'entity'; |
||||
} |
||||
|
||||
$array['table'] = $metadata->table['name']; |
||||
|
||||
if (isset($metadata->table['schema'])) { |
||||
$array['schema'] = $metadata->table['schema']; |
||||
} |
||||
|
||||
$inheritanceType = $metadata->inheritanceType; |
||||
if ($inheritanceType !== ClassMetadataInfo::INHERITANCE_TYPE_NONE) { |
||||
$array['inheritanceType'] = $this->_getInheritanceTypeString($inheritanceType); |
||||
} |
||||
|
||||
if ($column = $metadata->discriminatorColumn) { |
||||
$array['discriminatorColumn'] = $column; |
||||
} |
||||
|
||||
if ($map = $metadata->discriminatorMap) { |
||||
$array['discriminatorMap'] = $map; |
||||
} |
||||
|
||||
if ($metadata->changeTrackingPolicy !== ClassMetadataInfo::CHANGETRACKING_DEFERRED_IMPLICIT) { |
||||
$array['changeTrackingPolicy'] = $this->_getChangeTrackingPolicyString($metadata->changeTrackingPolicy); |
||||
} |
||||
|
||||
if (isset($metadata->table['indexes'])) { |
||||
$array['indexes'] = $metadata->table['indexes']; |
||||
} |
||||
|
||||
if ($metadata->customRepositoryClassName) { |
||||
$array['repositoryClass'] = $metadata->customRepositoryClassName; |
||||
} |
||||
|
||||
if (isset($metadata->table['uniqueConstraints'])) { |
||||
$array['uniqueConstraints'] = $metadata->table['uniqueConstraints']; |
||||
} |
||||
|
||||
$fieldMappings = $metadata->fieldMappings; |
||||
|
||||
$ids = array(); |
||||
foreach ($fieldMappings as $name => $fieldMapping) { |
||||
$fieldMapping['column'] = $fieldMapping['columnName']; |
||||
unset( |
||||
$fieldMapping['columnName'], |
||||
$fieldMapping['fieldName'] |
||||
); |
||||
|
||||
if ($fieldMapping['column'] == $name) { |
||||
unset($fieldMapping['column']); |
||||
} |
||||
|
||||
if (isset($fieldMapping['id']) && $fieldMapping['id']) { |
||||
$ids[$name] = $fieldMapping; |
||||
unset($fieldMappings[$name]); |
||||
continue; |
||||
} |
||||
|
||||
$fieldMappings[$name] = $fieldMapping; |
||||
} |
||||
|
||||
if ($idGeneratorType = $this->_getIdGeneratorTypeString($metadata->generatorType)) { |
||||
$ids[$metadata->getSingleIdentifierFieldName()]['generator']['strategy'] = $this->_getIdGeneratorTypeString($metadata->generatorType); |
||||
}else{ |
||||
if(count($metadata->identifier) == 2){ |
||||
foreach($metadata->identifier as $identifier){ |
||||
if($identifier != 'c_id'){ |
||||
$ids[$identifier]['generator']['strategy'] = 'IDENTITY'; |
||||
} |
||||
} |
||||
} |
||||
|
||||
} |
||||
|
||||
if ($ids) { |
||||
$array['fields'] = $ids; |
||||
} |
||||
|
||||
if ($fieldMappings) { |
||||
if ( ! isset($array['fields'])) { |
||||
$array['fields'] = array(); |
||||
} |
||||
$array['fields'] = array_merge($array['fields'], $fieldMappings); |
||||
} |
||||
|
||||
$associations = array(); |
||||
foreach ($metadata->associationMappings as $name => $associationMapping) { |
||||
$cascade = array(); |
||||
if ($associationMapping['isCascadeRemove']) { |
||||
$cascade[] = 'remove'; |
||||
} |
||||
if ($associationMapping['isCascadePersist']) { |
||||
$cascade[] = 'persist'; |
||||
} |
||||
if ($associationMapping['isCascadeRefresh']) { |
||||
$cascade[] = 'refresh'; |
||||
} |
||||
if ($associationMapping['isCascadeMerge']) { |
||||
$cascade[] = 'merge'; |
||||
} |
||||
if ($associationMapping['isCascadeDetach']) { |
||||
$cascade[] = 'detach'; |
||||
} |
||||
if (count($cascade) === 5) { |
||||
$cascade = array('all'); |
||||
} |
||||
$associationMappingArray = array( |
||||
'targetEntity' => $associationMapping['targetEntity'], |
||||
'cascade' => $cascade, |
||||
); |
||||
|
||||
if ($associationMapping['type'] & ClassMetadataInfo::TO_ONE) { |
||||
$joinColumns = $associationMapping['joinColumns']; |
||||
$newJoinColumns = array(); |
||||
foreach ($joinColumns as $joinColumn) { |
||||
$newJoinColumns[$joinColumn['name']]['referencedColumnName'] = $joinColumn['referencedColumnName']; |
||||
if (isset($joinColumn['onDelete'])) { |
||||
$newJoinColumns[$joinColumn['name']]['onDelete'] = $joinColumn['onDelete']; |
||||
} |
||||
} |
||||
$oneToOneMappingArray = array( |
||||
'mappedBy' => $associationMapping['mappedBy'], |
||||
'inversedBy' => $associationMapping['inversedBy'], |
||||
'joinColumns' => $newJoinColumns, |
||||
'orphanRemoval' => $associationMapping['orphanRemoval'], |
||||
); |
||||
|
||||
$associationMappingArray = array_merge($associationMappingArray, $oneToOneMappingArray); |
||||
$array['oneToOne'][$name] = $associationMappingArray; |
||||
} else if ($associationMapping['type'] == ClassMetadataInfo::ONE_TO_MANY) { |
||||
$oneToManyMappingArray = array( |
||||
'mappedBy' => $associationMapping['mappedBy'], |
||||
'inversedBy' => $associationMapping['inversedBy'], |
||||
'orphanRemoval' => $associationMapping['orphanRemoval'], |
||||
'orderBy' => isset($associationMapping['orderBy']) ? $associationMapping['orderBy'] : null |
||||
); |
||||
|
||||
$associationMappingArray = array_merge($associationMappingArray, $oneToManyMappingArray); |
||||
$array['oneToMany'][$name] = $associationMappingArray; |
||||
} else if ($associationMapping['type'] == ClassMetadataInfo::MANY_TO_MANY) { |
||||
$manyToManyMappingArray = array( |
||||
'mappedBy' => $associationMapping['mappedBy'], |
||||
'inversedBy' => $associationMapping['inversedBy'], |
||||
'joinTable' => isset($associationMapping['joinTable']) ? $associationMapping['joinTable'] : null, |
||||
'orderBy' => isset($associationMapping['orderBy']) ? $associationMapping['orderBy'] : null |
||||
); |
||||
|
||||
$associationMappingArray = array_merge($associationMappingArray, $manyToManyMappingArray); |
||||
$array['manyToMany'][$name] = $associationMappingArray; |
||||
} |
||||
} |
||||
if (isset($metadata->lifecycleCallbacks)) { |
||||
$array['lifecycleCallbacks'] = $metadata->lifecycleCallbacks; |
||||
} |
||||
|
||||
return \Symfony\Component\Yaml\Yaml::dump(array($metadata->name => $array), 10); |
||||
} |
||||
} |
Loading…
Reference in new issue