if(@txpinterface == 'admin') { add_privs('ied_plugin_composer','1,2'); register_tab('extensions', 'ied_plugin_composer', "Plugin Composer"); register_callback('ied_plugin_composer', 'ied_plugin_composer'); global $ied_plugin_composer; } function ied_plugin_composer ($event, $step) { if(!$step or !in_array($step, array('ied_plugin_delete','ied_plugin_edit','ied_plugin_help','ied_plugin_list','ied_plugin_install','ied_plugin_save','ied_plugin_save_file','ied_plugin_verify','ied_switch_status','ied_plugin_create_new','ied_plugin_save_as_file','ied_plugin_save_as_php_file','ied_plugin_upload'))) { ied_plugin_list(); } else $step(); } // ------------------------------------------------------------- function ied_plugin_list($message='') { pagetop(gTxt('edit_plugins'),$message); echo startTable('list'). tr(tda( form( fInput('text','name','','','','','30','0') . fInput('submit','submit','create new plugin','smallerbox') . sInput('ied_plugin_create_new') . eInput('ied_plugin_composer') ) ,' colspan="6" style="border:0;height:50px"')). assHead('plugin','author','version','description','','active',''); $rs = safe_rows_start("*","txp_plugin", "1=1 order by name"); while ($a = nextRow($rs)) { extract($a); $elink = ''.gTxt('edit').''; echo tr(td($name) .td(''.$author.'') .td($version,10) .td($description,260) .td(strong($elink)) .td(ied_status_link($status,$name,yes_no($status)),30) .td(dLink('ied_plugin_composer','ied_plugin_delete','name',$name),30) ); unset($name,$page,$deletelink); } if (!empty($GLOBALS['prefs']['plugin_cache_dir'])) { $filenames = array(); $dir = dir($GLOBALS['prefs']['plugin_cache_dir']); while ($file = $dir->read()) { if($file != "." && $file != "..") { $fileaddr = $GLOBALS['prefs']['plugin_cache_dir'].DS.$file; if (!is_dir($fileaddr)) { $filenames[]=$file; } } } $dir->close(); ($filenames)?natcasesort($filenames):''; echo tr( tda( tag('Plugins that run from '.tag('plugin_cache_dir','acronym',' title="'.$GLOBALS['prefs']['plugin_cache_dir'].'" style="border-bottom:1px dashed black"').':','h1') ,' colspan="7" style="border:0;height:50px;text-align:left"') ); echo assHead('plugin','author','version','description','','publish',''); foreach($filenames as $filename) { $elink = ''.gTxt('edit').''; $fileext= array_pop(explode ('.',$filename)); if ($fileext=='php') { $basename=basename($filename); $plugin = ied_plugin_read_file($GLOBALS['prefs']['plugin_cache_dir'].DS.$filename); echo tr( td( tag($filename,'div',' style="color:gray;border:0px solid gray;padding:1px 2px 2px 1px;"') .(isset($plugin['name'])?$plugin['name'].'
':'').' ' ) .td( ( isset($plugin['author_uri'])?'':'' ) . ( isset($plugin['author'])?$plugin['author']:' ' ). ( isset($plugin['author_uri'])?'':'' ) ) .td((isset($plugin['version'])?$plugin['version']:' '),10) .td((isset($plugin['description'])?$plugin['description']:' '),260) .td(strong($elink) //.ied_status_link($status,$basename,yes_no($status)) ) .td((isset($plugin['name'])? tag(gTxt('publish'),'a',' href="?event=ied_plugin_composer&step=ied_plugin_save_as_file&filename='.$filename.'" title="save as ' . $plugin['name'].'_v'.$plugin['version']. '.txt"') .tag(' [zip]','a',' href="?event=ied_plugin_composer&step=ied_plugin_save_as_file&filename='.$filename.'&zip=true" title="save as ' . $plugin['name'].'_v'.$plugin['version']. '_zip.txt"'): tag(' ','span',' style="color:gray"') )) .td('') ); } } } echo tr( tda( '
'. startTable('edit','left'). tr(tda( hInput('MAX_FILE_SIZE',1000000). graf('Upload plugin from php file'.': '. fInput('file','thefile','','edit'). fInput('submit','submit',gTxt('upload'),'smallerbox')). eInput('ied_plugin_composer'). sInput('ied_plugin_upload') , ' style="border:0px solid gray"' ). tda( tag('name','acronym',' title="needed only if the php file you are uploading does not contain metadata" style="border-bottom:1px dashed black"').': '. fInput('text','name','xxx_xxxxxx','','','','30','0') , ' style="border:0px solid gray;vertical-align:middle;"' )). endTable(). '
' ,' colspan="6" style="border:0;height:50px;text-align:left"') ); echo endTable(); } // ------------------------------------------------------------- function ied_status_link($status,$name,$linktext) { $out = ''; return $out; } // ------------------------------------------------------------- function ied_switch_status() { extract(gpsa(array('name','status'))); $change = ($status) ? 0 : 1; safe_update("txp_plugin", "status=$change", "name='$name'"); ied_plugin_list(messenger('plugin',$name,'updated')); } // ------------------------------------------------------------- function ied_plugin_delete() { $name = doSlash(ps('name')); safe_delete("txp_plugin","name='$name'"); ied_plugin_list(messenger('plugin',$name,'deleted')); } // ------------------------------------------------------------- function ied_plugin_edit($message='') { $name = gps('newname')?gps('newname'):gps('name'); pagetop(gTxt('edit_plugins'),$message); echo ied_plugin_edit_form((gps('filename'))?gps('filename'):$name, gps('filename')? 1: 0); } // ------------------------------------------------------------- function ied_plugin_edit_form($name='',$editfile=0) { if (!$editfile) { $author = ($name) ? fetch('author','txp_plugin','name',$name) : ''; $author_uri = ($name) ? fetch('author_uri','txp_plugin','name',$name) : ''; $version = ($name) ? fetch('version','txp_plugin','name',$name) : ''; $description = ($name) ? fetch('description','txp_plugin','name',$name) : ''; $code = ($name) ? fetch('code','txp_plugin','name',$name) : ''; $help = ($name) ? fetch('help','txp_plugin','name',$name) : ''; $status = ($name) ? fetch('status','txp_plugin','name',$name) : ''; $type = ($name) ? fetch('type','txp_plugin','name',$name) : ''; list($css,$help) = ($help) ? ied_plugin_extract_hunk($help, "CSS", "//", true) : array('',$help); } else { $plugin = ied_plugin_read_file($GLOBALS['prefs']['plugin_cache_dir'].DS.$name); $filename=$name; $fileext= array_pop($name=explode ('.',$name)); $name=implode($name); extract ($plugin); // code, help, css, name, version, type, description, author, author_uri $status= ($fileext=='php')? 1: 0; } $plugin['name'] = $name; $plugin['author'] = $author; $plugin['author_uri'] = $author_uri; $plugin['version'] = $version; $plugin['description'] = $description; $plugin['help'] = $help; $plugin['code'] = $code; $plugin['type'] = $type; $plugin['md5'] = md5( $plugin['code'] ); $distribution=''; $slink = 'save as ' . $name.'_v'.$version. '.txt'; $sziplink = 'save as ' . $name.'_v'.$version. '.txt (compressed)'; $sphplink = 'save as ' . $name.'_v'.$version. '.php'; $newname =fInput('text','newname',$name,'edit','78','','90','2'); $author =fInput('text','author',$author,'edit','78','','90','2'); $author_uri =fInput('text','author_uri',$author_uri,'edit','78','','90','2'); $version =fInput('text','version',$version,'edit','78','','90','2'); $description =fInput('text','description',$description,'edit','78','','90','2'); $code = ''; $help = ''; $css = ''; $prefs = ((!$editfile)?checkbox('status',1,$status). ' ':'') . ' '. radio('type',0,(($type==0)?1:0))."Client " . radio('type',1,(($type==1)?1:0))."Admin " . radio('type',2,(($type==2)?1:0))."Library"; $tinymce = ''. n.''; $sub = fInput('submit','',gTxt('save'),'publish'); return $tinymce . form(startTable('edit') . tr( fLabelCell(' ') . td($sub)) . ( (isset($filename))? tr( fLabelCell(' ') . td($filename.hInput('filename',$filename))) : '' ) . tr( fLabelCell( 'plugin' ) . td($newname)) . tr( fLabelCell( 'plugin' ) . td($code)) . tr( fLabelCell( 'prefs' ) . td($prefs)) . tr( fLabelCell( 'version' ) . td($version)) . tr( fLabelCell( 'author' ) . td($author)) . tr( fLabelCell( 'website' ) . td($author_uri)) . tr( fLabelCell( 'description' ) . td($description)) . tr( fLabelCell( 'plugin_help' ) . td($help)) . tr( fLabelCell( 'css' ) . td($css)) . tr( fLabelCell(' ') . td($sub)) . ( (!$editfile)? tr( tdcs('
',2)) . tr( fLabelCell(' ') . td('Utilities (use only after saving the data above):')) . tr( fLabelCell( 'plugin code
for distribution' ) . td($distribution)) . tr( fLabelCell(' ') . td($slink)) . tr( fLabelCell(' ') . td($sziplink)) . tr( fLabelCell(' ') . td($sphplink)) : '') . endTable() . sInput('ied_plugin_save') . eInput('ied_plugin_composer') .hInput('name',$name)); } // ------------------------------------------------------------- function ied_plugin_save() { extract(doSlash(gpsa(array('name','newname','filename','code','author','author_uri','version','description','help','css','status','type')))); $start_css = "// --- BEGIN PLUGIN CSS ---"; $end_css = "// --- END PLUGIN CSS ---"; if (empty($filename)) { safe_update("txp_plugin","name='$newname'", "name='$name'"); safe_update("txp_plugin","code='$code'", "name='$newname'"); safe_update("txp_plugin","author='$author'", "name='$newname'"); safe_update("txp_plugin","author_uri='$author_uri'", "name='$newname'"); safe_update("txp_plugin","version='$version'", "name='$newname'"); safe_update("txp_plugin","description='$description'", "name='$newname'"); safe_update("txp_plugin","help='".$help.(($css) ? n.$start_css.n.$css.n.$end_css:'')."'", "name='$newname'"); safe_update("txp_plugin","status='$status'", "name='$newname'"); safe_update("txp_plugin","type='$type'", "name='$newname'"); } else { $filecontent = file($GLOBALS['prefs']['plugin_cache_dir'].DS.$filename); $filecontent = ied_plugin_make_array($filecontent); $metavars = array("name" => "$newname", "version" => "$version", "author" => "$author", "author_uri" => "$author_uri", "description" => "$description", "type" => "$type" ); $hunkvars = array("CSS" => doStrip(str_replace('\r\n',' ',$css)), "HELP" => doStrip(str_replace('\r\n',' ',$help)), "CODE" => doStrip(str_replace('\r\n',' ',$code)), // workaround ); foreach ($metavars as $varname => $value) { for($idx = 0; $idx < count($filecontent); $idx++) { if (strpos($filecontent[$idx], '$plugin[\''.$varname.'\']') === 0) { $filecontent[$idx] = '$plugin[\''.$varname.'\'] = '.doQuote($value).';'; break; } } } foreach ($hunkvars as $varname => $hunk) { $start_delim = "# --- BEGIN PLUGIN $varname ---"; $end_delim = "# --- END PLUGIN $varname ---"; $start = array_search($start_delim, $filecontent) + 1; $end = array_search($end_delim, $filecontent); array_splice($filecontent, $start, $end-$start, $hunk); } $filecontent = implode("\r\n", $filecontent); $fh = fopen($GLOBALS['prefs']['plugin_cache_dir'].DS.$filename, 'w+'); fwrite($fh, $filecontent); fclose($fh); } ied_plugin_edit(messenger('plugin',$newname,'saved')); } // ------------------------------------------------------------- function ied_plugin_create_new() { extract(doSlash(gpsa(array('name')))); if ($name=='') { ied_plugin_list('You are supposed to name the new plugin ;)'); return; } $exists = fetch('name','txp_plugin','name',$name); if (!$exists) { safe_insert("txp_plugin", " name='$name', status='0', type='0', version='0.1' "); pagetop(gTxt('edit_plugins'), 'edit your new plugin'); echo ied_plugin_edit_form($name); } else { ied_plugin_list(gTxt('plugin').' '.$name.' '.gTxt('already_exists')); } } // ------------------------------------------------------------- function ied_plugin_save_as_file() { if (gps('name')) { $name = gps('name'); $description = ($name) ? fetch('description','txp_plugin','name',$name) : ''; $author = ($name) ? fetch('author','txp_plugin','name',$name) : ''; $author_uri = ($name) ? fetch('author_uri','txp_plugin','name',$name) : ''; $version = ($name) ? fetch('version','txp_plugin','name',$name) : ''; $code = ($name) ? fetch('code','txp_plugin','name',$name) : ''; $help = ($name) ? fetch('help','txp_plugin','name',$name) : ''; $type = ($name) ? fetch('type','txp_plugin','name',$name) : ''; list($css,$help) = ($help) ? ied_plugin_extract_hunk($help, "CSS", "//", true) : array('',$help); } elseif (gps('filename')) { $plugin=ied_plugin_read_file($GLOBALS['prefs']['plugin_cache_dir'].DS.gps('filename')); extract($plugin); } $zip = gps('zip'); if (gps('trim')==1) { $code=explode("\r\n",$code); $code=array_map('trim',$code); $code=implode("\r\n",$code); } header("Content-type: text/plain"); header('Content-Disposition: attachment; filename="' . $name.'_v'.$version. (($zip)?'_zip':'') . '.txt'); $types = array( 'Client side' , 'Admin + Client' , 'Library' ); $plugin['name'] = $name; $plugin['author'] = $author; $plugin['author_uri'] = $author_uri; $plugin['version'] = $version; $plugin['description'] = $description; $plugin['help'] = $help; $plugin['code'] = $code; $plugin['type'] = $type; $plugin['md5'] = md5( $plugin['code'] ); @include(txpath.'/lib/classTextile.php'); if (class_exists('Textile')) { $textile = new Textile(); $plugin['help'] = "$css\n".$textile->TextileThis($plugin['help']); } echo '# Name: '.$name.' v'.$version.' '.(($zip) ? "(compressed)" : "").' # Type: '.$types[$type].' plugin # '.$description.' # Author: '.$author.' # URL: '.$author_uri.' # ..................................................................... # This is a plugin for Textpattern - http://textpattern.com/ # To install: textpattern > admin > plugins # Paste the following text into the \'Install plugin\' box: # ..................................................................... '.(($zip) ? chunk_split(base64_encode(gzencode(serialize($plugin))), 72) : chunk_split(base64_encode(serialize($plugin)), 72)); die(); } // ------------------------------------------------------------- function ied_plugin_save_as_php_file() { $name = gps('name'); $description = ($name) ? fetch('description','txp_plugin','name',$name) : ''; $author = ($name) ? fetch('author','txp_plugin','name',$name) : ''; $author_uri = ($name) ? fetch('author_uri','txp_plugin','name',$name) : ''; $version = ($name) ? fetch('version','txp_plugin','name',$name) : ''; $code = ($name) ? fetch('code','txp_plugin','name',$name) : ''; $help = ($name) ? fetch('help','txp_plugin','name',$name) : ''; $type = ($name) ? fetch('type','txp_plugin','name',$name) : ''; list($css,$help) = ($help) ? ied_plugin_extract_hunk($help, "CSS", "//", true) : array('',$help); header("Content-type: text/php"); header('Content-Disposition: attachment; filename="' . $name.'_v'.$version. '.php'); echo ' abc_myplugin-0.1.txt'.n .n .'// Plugin name is optional. If unset, it will be extracted from the current'.n .'// file name. Uncomment and edit this line to override:'.n .'$plugin[\'name\'] = '.doQuote($name).';'.n .'$plugin[\'version\'] = '.doQuote($version).';'.n .'$plugin[\'author\'] = '.doQuote($author).';'.n .'$plugin[\'author_uri\'] = '.doQuote($author_uri).';'.n .'$plugin[\'description\'] = '.doQuote($description).';'.n .n .'// Plugin types:'.n .'// 0 = regular plugin; loaded on the public web side only'.n .'// 1 = admin plugin; loaded on both the public and admin side'.n .'// 2 = library; loaded only when include_plugin() or require_plugin() is called'.n .'$plugin[\'type\'] = '.doQuote($type).';'.n .n .'@include_once(\'zem_tpl.php\');'.n .n .'if (0) {'.n .'?>'.n .''.n .''.n .''; die(); } // ------------------------------------------------------------- function ied_plugin_upload() { $start_css = "// --- BEGIN PLUGIN CSS ---"; $end_css = "// --- END PLUGIN CSS ---"; if (!$_FILES['thefile']['tmp_name']) { ied_plugin_list('You are supposed to choose the file ;)');return; } $plugin = ied_plugin_read_file($_FILES['thefile']['tmp_name']); $newname = (empty($plugin['name'])) ? basename($_FILES['thefile']['name'], '.php') : $plugin['name']; if (gps('name') and gps('name')!='xxx_xxxxxx') { $newname=gps('name'); } if (empty($plugin['code'])) { $plugin['code'] = implode ("", file($_FILES['thefile']['tmp_name'])); } if (MAGIC_QUOTES_GPC) { foreach ($plugin as $item => $value) { $plugin[$item] = addslashes($value); } } extract($plugin); $md5=md5($code); $version = (empty($version)) ? "0.1" : $version; $help = $help.(($css) ? n.$start_css.n.$css.n.$end_css : ''); $exists = fetch('name','txp_plugin','name',$newname); if ($exists) { $status = fetch('status','txp_plugin','name',$newname); $rs = safe_update( "txp_plugin", "status = '$status', type = '$type', author = '$author', author_uri = '$author_uri', version = '$version', description = '$description', help = '$help', code = '$code', code_restore = '$code', code_md5 = '$md5'", "name = '$newname'" ); } else { $rs = safe_insert( "txp_plugin", "name = '$newname', status = 0, type = '$type', author = '$author', author_uri = '$author_uri', version = '$version', description = '$description', help = '$help', code = '$code', code_restore = '$code', code_md5 = '$md5'" ); } pagetop(gTxt('edit_plugins'), messenger('plugin',$newname,($exists)?'updated':'uploaded')); echo ied_plugin_edit_form($newname); } // ------------------------------------------------------------- function ied_plugin_read_file($filepath) { $content = file($filepath); $source_lines = count($content); $metadata = array( 'name' => '', 'version' => '', 'author' => '', 'author_uri' => '', 'description' => '', 'type' => '', ); for ($idx=0; $idx < $source_lines; $idx++) { $content[$idx] = rtrim($content[$idx]); if (strpos($content[$idx], '$plugin[') === 0) { // Found a plugin variable so extract it $parts = explode(" = ", $content[$idx]); $parts[0] = str_replace("'", "", $parts[0]); preg_match("/plugin\[(.*)\]/", $parts[0], $var); $val = trim($parts[1], "';"); $metadata[$var[1]] = $val; } } $metadata['help'] = ied_plugin_extract_hunk($content, 'HELP'); $metadata['css'] = ied_plugin_extract_hunk($content, 'CSS' ); $metadata['code'] = ied_plugin_extract_hunk($content, 'CODE'); return $metadata; } // ------------------------------------------------------------- // ripped and modded from net-carver's version of the the zem_tpl template function ied_plugin_extract_hunk($content, $hunk, $cmnt="#", $delete=false) { $lines = ied_plugin_make_array($content); $start_delim = "$cmnt --- BEGIN PLUGIN $hunk ---"; $end_delim = "$cmnt --- END PLUGIN $hunk ---"; $start = array_search($start_delim, $lines) + 1; $end = array_search($end_delim, $lines); $extracted = array(); if (is_numeric($start) && is_numeric($end) && $end > $start) { $extracted = array_slice($lines, $start, $end-$start); if ($delete) { $chopped = array_splice($lines, $start-1, $end-$start+2, ""); } } $remains = $lines; // Check if the CSS section has ') === false) { $extracted[] = ''; } if (strpos($extracted[0], '