Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added EE3 version and split ee2/ee3 paths #59

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@ For older versions of EE use JSON version [1.0.3](https://github.com/rsanchez/js

## Installation

#### EE2
* Copy the /system/expressionengine/third_party/json/ folder to your /system/expressionengine/third_party/ folder

#### EE3
* Copy the /ee/user/addons/json/ folder to your /ee/user/addons/ folder

## Global Parameters

### `xhr="yes"`
Expand Down
Binary file added ee3/ee/user/addons/json/.DS_Store
Binary file not shown.
12 changes: 12 additions & 0 deletions ee3/ee/user/addons/json/addon.setup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

return array(
'author' => 'rsanchez',
'author_url' => 'https://github.com/rsanchez/json',
'description' => 'Output ExpressionEngine data in JSON format.',
'docs_url' => 'https://github.com/rsanchez/json',
'name' => 'Json',
'settings_exist' => false,
'version' => '1.0.0',
'namespace' => 'json/Json'
);
45 changes: 45 additions & 0 deletions ee3/ee/user/addons/json/libraries/Json_Template.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Json_Template extends EE_Template {
/**
* A copy of EE's TMPL object
* @var EE_Template
*/
protected $TMPL;

/**
* Variables collected from parse_variables or parse_variables row
* @var array
*/
public $variables = array();

public function __construct()
{
parent::__construct();

// Store a local reference to the "real" TMPL object, so it can be restored on __destruct
$this->TMPL =& ee()->TMPL;

// Override the "real" TMPL object
ee()->TMPL =& $this;
}

public function __destruct()
{
// Restore the "real" TMPL object
ee()->TMPL =& $this->TMPL;
}

public function parse_variables($tagdata, $variables, $enable_backspace = TRUE)
{
$output = parent::parse_variables($tagdata, $variables, $enable_backspace);
$this->variables = $variables;
return $output;
}

public function parse_variables_row($tagdata, $variables, $solo = TRUE)
{
$this->variables = $variables;
return parent::parse_variables_row($tagdata, $variables, $solo);
}
}
168 changes: 168 additions & 0 deletions ee3/ee/user/addons/json/libraries/Jsonp.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
<?php

/**
* Courtesy of Erik Pettersson
*
* https://gist.github.com/1217080
* https://github.com/ptz0n
* http://ptz0n.se/
*/

/**
* Validate JSONP Callback
*
* https://github.com/tav/scripts/blob/master/validate_jsonp.py
* https://github.com/talis/jsonp-validator/blob/master/src/main/java/com/talis/jsonp/JsonpCallbackValidator.java
* http://tav.espians.com/sanitising-jsonp-callback-identifiers-for-security.html
* http://news.ycombinator.com/item?id=809291
*
* ^[a-zA-Z_$][0-9a-zA-Z_$]*(?:\[(?:".+"|\'.+\'|\d+)\])*?$
*
*/
class Jsonp {

/**
* Validation tests
*
* @var array
*
* @access private
*/
private $_tests = array(
'' => false,
'hello' => true,
'alert()' => false,
'test()' => false,
'a-b' => false,
'23foo' => false,
'foo23' => true,
'$210' => true,
'_bar' => true,
'some_var' => true,
'$' => true,
'somevar' => true,
'function' => false,
' somevar' => false,
'$.ajaxHandler' => true,
'$.23' => false,
'array_of_functions[42]' => true,
'array_of_functions[42][1]' => true,
'$.ajaxHandler[42][1].foo' => true,
'array_of_functions[42]foo[1]' => false,
'array_of_functions[]' => false,
'array_of_functions["key"]' => true,
'myFunction[123].false' => false,
'myFunction .tester' => false,
'_function' => true,
'petersCallback1412331422[12]' => true,
':myFunction' => false
);

/**
* Is valid callback
*
* @param string $callback
*
* @return boolean
*/
function isValidCallback($callback)
{
$reserved = array(
'break',
'do',
'instanceof',
'typeof',
'case',
'else',
'new',
'var',
'catch',
'finally',
'return',
'void',
'continue',
'for',
'switch',
'while',
'debugger',
'function',
'this',
'with',
'default',
'if',
'throw',
'delete',
'in',
'try',
'class',
'enum',
'extends',
'super',
'const',
'export',
'import',
'implements',
'let',
'private',
'public',
'yield',
'interface',
'package',
'protected',
'static',
'null',
'true',
'false'
);

foreach(explode('.', $callback) as $identifier) {
if(!preg_match('/^[a-zA-Z_$][0-9a-zA-Z_$]*(?:\[(?:".+"|\'.+\'|\d+)\])*?$/', $identifier)) {
return false;
}
if(in_array($identifier, $reserved)) {
return false;
}
}

return true;
}

/**
* Test callback strings
*
* @param string $callback
*
* @return void
*
* @access private
*/
private function _test($callback, $valid)
{
$vocal = $valid ? 'valid' : 'invalid';
if($this->isValidCallback($callback) === $valid) {
echo '"'.$callback.'" <span style="color:green">passed as '.$vocal.'</span>.', "\n";
return true;
}
else {
echo '"'.$callback.'" <span style="color:red;font-weight:700;">failed as '.$vocal.'</span>.', "\n";
return false;
}
}

/**
* Run all tests
*
* @return void
*
* @access public
*/
function runTests()
{
echo '<strong>Testing ', count($this->_tests), ' callback methods:</strong>', "\n\n";
$passed = 0;
foreach($this->_tests as $callback => $valid) {
$passed = self::_test($callback, $valid) ? $passed+1 : $passed;
}
echo "\n", $passed, ' of ', count($this->_tests), ' tests passed.';
}
}
Loading