Permalink
Browse files

Modularized feediron a bit

  • Loading branch information...
m42e committed Dec 23, 2014
1 parent 935e0d6 commit 742e942907989fd2818f576456b919f6785f1855
Showing with 330 additions and 303 deletions.
  1. +34 −0 Functions.php
  2. +94 −0 Json.php
  3. +58 −0 Logger.php
  4. +79 −0 PrefTab.php
  5. +6 −2 RecipeManager.php
  6. +59 −301 init.php
View
@@ -0,0 +1,34 @@
<?php
/*
json_last_error_msg requires php >= 5.5.0 see http://php.net/manual/en/function.json-last-error-msg.php
a possible fix would be:
*/
if (!function_exists('json_last_error_msg'))
{
function json_last_error_msg()
{
switch (json_last_error()) {
default:
return;
case JSON_ERROR_DEPTH:
$error = 'Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
$error = 'Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
$error = 'Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error = 'Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
$error = 'Malformed UTF-8 characters, possibly incorrectly encoded';
break;
}
return $error;
}
}
View
@@ -0,0 +1,94 @@
<?php
class Feediron_Json{
private static $json_error;
public static function format($json){
$result = '';
$pos = 0;
$strLen = strlen($json);
$indentStr = ' ';
$newLine = "\n";
$prevChar = '';
$outOfQuotes = true;
$currentline = 0;
$possible_errors = array (
',]' => 'Additional comma before ] (%s)',
'""' => 'Missing seperator between after " (%s)',
',}' => 'Additional comma before } (%s)',
',:' => 'Comma before :(%s)',
']:' => '] before :(%s)',
'}:' => '} before :(%s)',
'[:' => '[ before :(%s)',
'{:' => '{ before :(%s)',
);
for ($i=0; $i<=$strLen; $i++) {
// Grab the next character in the string.
$char = substr($json, $i, 1);
if($char == $newLine){
$currentline++;
continue;
}
if($char == ' '){
continue;
}
if (array_key_exists($prevChar.$char, $possible_errors)){
self::$json_error = sprintf($possible_errors[$prevChar.$char]. ' in line %s', substr($result, self::strrpos_count($result,$newLine,3)), $currentline);
}
// Are we inside a quoted string?
if ($char == '"' && $prevChar != '\\') {
$outOfQuotes = !$outOfQuotes;
// If this character is the end of an element,
// output a new line and indent the next line.
} else if(($char == '}' || $char == ']') && $outOfQuotes) {
$result .= $newLine;
$pos --;
for ($j=0; $j<$pos; $j++) {
$result .= $indentStr;
}
}
// Add the character to the result string.
$result .= $char;
// If the last character was the beginning of an element,
// output a new line and indent the next line.
if (($char == ',' || $char == '{' || $char == '[') && $outOfQuotes) {
$result .= $newLine;
if ($char == '{' || $char == '[') {
$pos ++;
}
for ($j = 0; $j < $pos; $j++) {
$result .= $indentStr;
}
}
else if($char == ':' && $outOfQuotes){
$result .= ' ';
}
$prevChar = $char;
}
return $result;
}
private static function strrpos_count($haystack, $needle, $count)
{
if($count <= 0)
return false;
$len = strlen($haystack);
$pos = $len;
for($i = 0; $i < $count && $pos; $i++)
$pos = strrpos($haystack, $needle, $pos - $len - 1);
return $pos;
}
public static function get_error(){
return self::$json_error;
}
}
View
@@ -0,0 +1,58 @@
<?php
class Feediron_Logger{
private static $logger = FALSE;
const LOG_NONE = 0;
const LOG_TTRSS = 1;
const LOG_TEST = 2;
const LOG_VERBOSE = 3;
public static function get(){
if(self::$logger === FALSE){
self::$logger = new Feediron_Logger();
}
return self::$logger;
}
private $loglevel = LOG_NONE;
private $testlog = array();
private $time_measure = false;
public function get_log_level(){
return $this->loglevel;
}
public function set_log_level($level){
$this->loglevel = $level;
}
public function log($level, $msg, $details=''){
if($level > $this->loglevel)
return;
if($level == self::LOG_TTRSS){
trigger_error($msg, E_USER_WARNING);
array_push($this->testlog, "<h2>LOG:</h2><pre>$msg</pre>");
}else{
array_push($this->testlog, "<h2>$msg</h2>");
array_push($this->testlog, "<pre>$details</pre>");
}
}
public function log_json($level, $msg, $json){
if($level > $this->loglevel)
return;
$this->log($level, $msg, Feediron_Json::format($json));
}
public function log_object($level, $msg, $obj){
if($level > $this->loglevel)
return;
$this->log_json($level, $msg, json_encode($obj));
}
public function log_html($level, $msg, $html){
if($level > $this->loglevel)
return;
$this->log($level, $msg, htmlentities($html));
}
public function get_testlog(){
return $this->testlog;
}
}
View
@@ -0,0 +1,79 @@
<?php
class Feediron_PrefTab{
public static function get_pref_tab($json_conf){
$tab = '';
$rm = new RecipeManager();
$rm->loadAvailableRecipes();
$tab .= '<div data-dojo-type="dijit/layout/TabContainer" style="width: 100%;" doLayout="false">';
$tab .= '<div data-dojo-type="dijit/layout/ContentPane" title="Configuration" data-dojo-props="selected:true" id="config">';
print self::get_form_start('save');
print self::get_script(' notify_info(transport.responseJSON.message); dojo.query("#json_conf").attr("value",transport.responseJSON.json_conf); dojo.query("#json_error").attr("innerHTML", "").attr("class",""); ','dojo.query("#json_error").attr("innerHTML", transport.responseJSON.json_error).attr("class","error");');
$tab .= '<textarea dojoType="dijit.form.SimpleTextarea" id="json_conf" name="json_conf" style="font-size: 12px; width: 99%; height: 400px;">'.$json_conf.'</textarea>';
$tab .= '<p /><button dojoType="dijit.form.Button" type="submit">'.__("Save").'</button>';
$tab .= '</form>';
$tab .= self::get_form_start('add');
$tab .= self::get_script('notify_info(transport.responseJSON.message); dojo.query("#json_conf").attr("value",transport.responseJSON.json_conf); ');
$tab .= '<label for="addrecipe">'.__("Add recipe").': </label>';
$tab .= '<select dojoType="dijit.form.Select" name="addrecipe">';
foreach($rm->getRecipes() as $recipe){
$tab .= '<option value="'.$recipe.'">'.$recipe.'</option>';
}
$tab .= '</select>&nbsp;';
$tab .= '<button dojoType="dijit.form.Button" type="submit">'.__("Add").'</button>';
$tab .= '</form><p /><div id="json_error"></div>';
$tab .= __("Save after adding config!").'<br />';
$tab .= '</div>';
$tab .= '<div data-dojo-type="dijit/layout/ContentPane" title="Testing" data-dojo-props="selected:true" id="testing">';
$tab .= self::get_form_start('test');
$tab .= self::get_script('notify_info("Updated"); dojo.query("#test_url").attr("innerHTML", "<pre>"+transport.responseJSON.url+"</pre>"); dojo.query("#test_result").attr("innerHTML", transport.responseJSON.content); dojo.query("#test_log").attr("innerHTML", transport.responseJSON.log.join("\n"));');
$tab .= __("Save before you test!").'<br />';
$tab .= '<table width="100%"><tr><td>';
$tab .= '<input dojoType="dijit.form.TextBox" name="test_url" style="font-size: 12px; width: 99%;" />';
$tab .= '</td></tr></table>';
$tab .= '<p><button dojoType="dijit.form.Button" type="submit">'.__("Test").'</button> <input id="verbose" dojoType="dijit.form.CheckBox" name="verbose" /><label for="verbose">'.__("Show every step").'</label> </p>';
$tab .= '</form>';
$tab .= '<div data-dojo-type="dijit/layout/TabContainer" style="width: 100%;" doLayout="false">';
$tab .= '<div data-dojo-type="dijit/layout/ContentPane" title="log" data-dojo-props="selected:true" id="test_log"></div>';
$tab .= '<div data-dojo-type="dijit/layout/ContentPane" title="result" data-dojo-props="selected:true" id="test_result"></div>';
$tab .= '<div data-dojo-type="dijit/layout/ContentPane" title="url" data-dojo-props="selected:true" id="test_url"></div>';
$tab .= '</div>';
$tab .= '</div>';
$tab .= '</div>';
return $tab;
}
private static function get_script($successaction,$failaction=''){
$script = '<script type="dojo/method" event="onSubmit" args="evt">
evt.preventDefault();
dojo.query("#test_result").attr("innerHTML", "");
new Ajax.Request("backend.php", {
parameters: dojo.objectToQuery(this.getValues()),
onComplete: function(transport) {
if (transport.responseJSON.success == false){
notify_error(transport.responseJSON.errormessage);
'.$failaction.'
}else{
'.$successaction.'
}
}
}
);
</script>';
return $script;
}
private static function get_form_start($method){
$form = '<form dojoType="dijit.form.Form">';
$form .='<input dojoType="dijit.form.TextBox" style="display : none" name="op" value="pluginhandler">';
$form .= '<input dojoType="dijit.form.TextBox" style="display : none" name="method" value="'.$method.'">';
$form .= '<input dojoType="dijit.form.TextBox" style="display : none" name="plugin" value="feediron">';
return $form;
}
}
View
@@ -25,12 +25,16 @@ public function getRecipes(){
}
public function getRecipe($recipename){
$content = fetch_file_contents (self::recipes_location.'/'.$recipename.self::recipes_branch);
$url = self::recipes_location.'/'.$recipename.self::recipes_branch;
$content = fetch_file_contents ($url);
Feediron_Logger::get()->log(Feediron_Logger::LOG_TTRSS, "Recipe url: $url");
$filedata = json_decode($content, true);
Feediron_Logger::get()->log(Feediron_Logger::LOG_TTRSS, "Recipe content: $content");
if(isset ($filedata['message'])){
return $filedata;
}
return json_decode(base64_decode($filedata['content']),true);
$obj = json_decode(base64_decode($filedata['content']),true);
return $obj;
}
}
Oops, something went wrong.

0 comments on commit 742e942

Please sign in to comment.