-
-
Save spiffin/477e8eaedd42bde3d258 to your computer and use it in GitHub Desktop.
Testing/development version of spf_js plugin for Textpattern CMS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//<?php // TESTING version based on txp_css.php (4.6-dev) | |
if (@txpinterface == 'admin') | |
{ | |
add_privs('spf_js_test', '1,2'); | |
register_tab('presentation', 'spf_js_test', gTxt('spf_javascript')); | |
register_callback('spf_js_bounce', 'spf_js_test'); | |
register_callback('spf_js_install', 'plugin_lifecycle.spf_js', 'installed'); | |
register_callback('spf_js_uninstall', 'plugin_lifecycle.spf_js', 'deleted'); | |
} | |
function spf_js_bounce() | |
{ | |
global $event, $step; | |
if ($event == 'spf_js_test') | |
{ | |
require_privs('spf_js_test'); | |
bouncer($step, | |
array( | |
'spf_js_edit' => false, | |
'pour' => false, | |
'spf_js_save' => true, | |
'spf_js_write' => true, | |
'spf_js_delete' => true, | |
) | |
); | |
switch (strtolower($step)) { | |
case '': spf_js_edit(); break; | |
case 'spf_js_edit': spf_js_edit(); break; | |
case 'pour': spf_js_edit(); break; | |
case 'spf_js_save': spf_js_save(); break; | |
case 'spf_js_write': spf_js_write(); break; | |
case 'spf_js_delete': spf_js_delete(); break; | |
} | |
} | |
} | |
/** | |
* Installer function | |
*/ | |
function spf_js_install($event, $step) | |
{ | |
global $prefs, $step; | |
// Version check | |
if (txp_version >= '4.5.7') { | |
if(!isset($prefs['spf_js_dir'])) { | |
safe_query( | |
"CREATE TABLE IF NOT EXISTS ".safe_pfx('spf_js')." ( | |
name varchar(255) NOT NULL default '', | |
js longtext NOT NULL, | |
PRIMARY KEY(name) | |
) CHARSET=utf8" | |
); | |
if(!safe_count("spf_js", "name='default'")) { | |
safe_insert("spf_js", "name='default', js='// Default javascript'"); | |
} | |
safe_insert( | |
'txp_prefs', | |
"prefs_id=1, | |
name='spf_js_dir', | |
val='js', | |
type=1, | |
event='admin', | |
html='text_input', | |
position=21" | |
); | |
} | |
install_textpack( | |
'#@spf_js | |
spf_javascript => Javascript | |
spf_js_dir => Javascript directory | |
spf_script_name => Script name | |
spf_js_code => Script code | |
spf_edit_script => You are editing script | |
spf_copy_script => …or copy script as | |
spf_all_scripts => All scripts | |
spf_create_new_script => Create new script | |
spf_save_failed => The script was not saved due to an error. Please try again. | |
spf_rename_default => Default script cannot be renamed. | |
spf_script_created => Script <strong>{name}</strong> created. | |
spf_script_exists => Script <strong>{name}</strong> already exists. | |
spf_script_name_required => Please provide a name for your script. | |
spf_script_updated => Script <strong>{name}</strong> updated. | |
spf_script_saved => Script <strong>{name}</strong> saved. | |
spf_script_deleted => Script <strong>{name}</strong> deleted.' | |
); | |
// If Txp version is less than 4.6 return message. | |
} else { | |
return 'The plugin spf_js version 1.0 requires Textpattern 4.6 or higher - you are running Textpattern ' . txp_version . ' - please delete spf_js.'; | |
} | |
} | |
/** | |
* Removal function | |
*/ | |
function spf_js_uninstall($event, $step) | |
{ | |
global $prefs, $step; | |
if(isset($prefs['spf_js_dir'])) { | |
safe_delete( | |
'txp_prefs', | |
"name='spf_js_dir'" | |
); | |
} | |
// Don't drop the table - just in case. | |
//@safe_query( | |
// 'DROP TABLE IF EXISTS '.safe_pfx('spf_js') | |
//); | |
// delete Textpack | |
safe_delete( | |
'txp_lang', | |
"event = 'spf_js'" | |
); | |
} | |
/** | |
* Output tag | |
*/ | |
function spf_js($atts) | |
{ | |
global $prefs; | |
extract(lAtts(array( | |
'name' => 'default' | |
), $atts)); | |
$name = strtolower(sanitizeForUrl($name)); | |
if(!safe_row('name', 'spf_js', "name='".doSlash($name)."'")) { | |
trigger_error(gTxt('404_not_found').sp.strong('"'.$name.'"')); | |
return; | |
} | |
} | |
/** | |
* List function | |
*/ | |
function spf_js_list($current, $default) | |
{ | |
$out = array(); | |
$criteria = 1; | |
$criteria .= callback_event('admin_criteria', 'spf_js_list', 0, $criteria); | |
$rs = safe_rows_start('name as jsname', 'spf_js', $criteria); | |
if ($rs) { | |
while ($a = nextRow($rs)) { | |
extract($a); | |
$active = ($current === $name); | |
if ($active) { | |
$edit = txpspecialchars($name); | |
} else { | |
$edit = eLink('spf_js', '', 'name', $jsname, $jsname); | |
} | |
if ($jsname != 'default') { | |
$edit .= dLink('spf_js', 'spf_js_delete', 'name', $jsname); // no delete icon for default script | |
} | |
$out[] = tag(n.$edit.n, 'li', array( | |
'class' => $active ? 'active' : '', | |
)); | |
} | |
$out = tag(join(n, $out), 'ul', array( | |
'class' => 'switcher-list', | |
)); | |
return wrapGroup('all_scripts', $out, 'spf_all_scripts'); | |
} | |
} | |
/** | |
* Javascript editor | |
*/ | |
function spf_js_edit($message='') | |
{ | |
global $event, $step; | |
pagetop(gTxt('spf_javascript'),$message); | |
$default_name = 'default'; | |
extract(array_map('assert_string', gpsa(array( | |
'copy', | |
'save_error', | |
'savenew', | |
)))); | |
$name = sanitizeForPage(assert_string(gps('name'))); | |
$newname = sanitizeForPage(assert_string(ps('newname'))); | |
if ($step == 'spf_js_delete' || empty($name) && $step != 'pour' && !$savenew) { | |
$name = $default_name; | |
} elseif (((($copy || $savenew) && $newname) || ($newname && ($newname != $name))) && !$save_error) { | |
$name = $newname; | |
} | |
$titleblock = inputLabel( | |
'new_spf_js', | |
fInput('text', 'newname', $name, 'input-medium', '', '', INPUT_MEDIUM, '', 'new_spf_js', false, true), | |
'spf_script_name', | |
array('', 'instructions_spf_js_name'), | |
array('class' => 'txp-form-field'), | |
'' | |
); | |
if ($name === '') { | |
$titleblock .= hInput('savenew', 'savenew'); | |
} else { | |
$titleblock .= hInput('name', $name); | |
} | |
$titleblock .= eInput('spf_js').sInput('spf_js_save'); | |
$thejs = gps('spf_js'); | |
if (!$save_error) { | |
$thejs = fetch('js', 'spf_js', 'name', $name); | |
} | |
// Scripts code columm. | |
echo n.tag( | |
hed(gTxt('spf_javascript'), 1, array('class' => 'txp-heading')). | |
form( | |
$titleblock. | |
inputLabel( | |
'js', | |
'<textarea class="txp-form-field-input code" id="spf_js" name="spf_js" cols="'.INPUT_LARGE.'" rows="'.TEXTAREA_HEIGHT_LARGE.'" dir="ltr">'.txpspecialchars($thejs).'</textarea>', | |
'spf_js_code', | |
array('', 'instructions_spf_js_code'), | |
array('class' => 'txp-form-field'), | |
'' | |
), '', '', 'post', '', '', 'spfjs_form'), | |
'div', array( | |
'class' => 'txp-layout-4col-cell-1-2-3', | |
'id' => 'main_content', | |
'role' => 'region', | |
) | |
); | |
// Scripts create/switcher column. | |
$buttonExtras = ''; | |
// Remove duplicate function = problematic?? | |
$buttons = graf( | |
tag_void('input', array( | |
'class' => 'publish', | |
'type' => 'submit', | |
'method' => 'post', | |
'value' => gTxt('save'), | |
'form' => 'spfjs_form', | |
)), ' class="txp-save"' | |
). | |
graf( | |
sLink('spf_js', 'pour', '<span class="ui-icon ui-extra-icon-new-document"></span> '.gTxt('spf_create_new_script'), 'txp-new'). | |
$buttonExtras, | |
array('class' => 'txp-actions') | |
); | |
echo n.tag( | |
$buttons. | |
spf_js_list($name, $default_name).n, | |
'div', array( | |
'class' => 'txp-layout-4col-cell-4alt', | |
'id' => 'content_switcher', | |
'role' => 'region', | |
) | |
); | |
} | |
/** | |
* Save function | |
*/ | |
function spf_js_save() | |
{ | |
extract(array_map('assert_string', gpsa(array( | |
'savenew', | |
'copy', | |
'spf_js', | |
)))); | |
$js = doSlash($spf_js); | |
$name = sanitizeForPage(assert_string(gps('name'))); | |
$newname = sanitizeForPage(assert_string(ps('newname'))); | |
$save_error = false; | |
$message = ''; | |
//echo "name=".$name." | newname=".$newname; // TESTING | |
if (!$newname) { | |
$message = array(gTxt('spf_script_name_required'), E_ERROR); | |
$save_error = true; | |
} else { | |
if ($copy && ($name === $newname)) { // not needed if not using duplicate? | |
$newname .= '_copy'; | |
$_POST['newname'] = $newname; | |
} | |
$exists = safe_field('name', 'spf_js', "name = '".doSlash($newname)."'"); | |
if (($newname !== $name) && $exists) { | |
$message = array(gTxt('spf_script_exists', array('{name}' => $newname)), E_ERROR); | |
if ($savenew) { | |
$_POST['newname'] = ''; | |
} | |
$save_error = true; | |
} else { | |
if ($savenew or $copy) { | |
if ($newname) { | |
if (safe_insert('spf_js', "name = '".doSlash($newname)."', js = '$js'")) { | |
update_lastmod('spf_script_created', compact('newname', 'name', 'js')); | |
$message = gTxt('spf_script_created', array('{name}' => $newname)); | |
spf_js_write(); | |
} else { | |
$message = array(gTxt('spf_save_failed'), E_ERROR); | |
$save_error = true; | |
} | |
} else { | |
$message = array(gTxt('spf_script_name_required'), E_ERROR); | |
$save_error = true; | |
} | |
} else { | |
if (($name == 'default') && ($newname !== 'default')) { // Prevent renaming of default script… | |
$message = array(gTxt('spf_rename_default'), E_ERROR); | |
$save_error = true; | |
} else { | |
if (safe_update('spf_js', "js = '$js', name = '".doSlash($newname)."'", "name = '".doSlash($name)."'")) { | |
update_lastmod('spf_script_saved', compact('newname', 'name', 'js')); | |
$message = gTxt('spf_script_updated', array('{name}' => $newname)); // Changed $name to $newname for renamed scripts | |
spf_js_write(); | |
} else { | |
$message = array(gTxt('spf_save_failed'), E_ERROR); | |
$save_error = true; | |
} | |
} | |
} | |
} | |
} | |
if ($save_error === true) { | |
$_POST['save_error'] = '1'; | |
} else { | |
callback_event('spf_js_saved', '', 0, $name, $newname); | |
} | |
spf_js_edit($message); | |
} | |
/** | |
* Write function | |
*/ | |
function spf_js_write() | |
{ | |
global $prefs; | |
extract(array_map('assert_string', gpsa(array( | |
'savenew', | |
'copy', | |
'spf_js', | |
)))); | |
$name = sanitizeForPage(assert_string(gps('name'))); | |
$newname = sanitizeForPage(assert_string(ps('newname'))); | |
// Allow for deleting old file on rename | |
$oldfilename = strtolower(sanitizeForUrl($name)); | |
$newfilename = strtolower(sanitizeForUrl($newname)); | |
$oldfile = $prefs['path_to_site'].'/'.$prefs['spf_js_dir'].'/'.$oldfilename; | |
$newfile = $prefs['path_to_site'].'/'.$prefs['spf_js_dir'].'/'.$newfilename; | |
if (empty($prefs['spf_js_dir']) or !$newfilename) { | |
return; | |
} else { | |
// If old file exists, delete it - when renaming | |
if (file_exists($oldfile.'.js')) { | |
@unlink($oldfile.'.js'); | |
} | |
$js_raw = fetch("js", "spf_js", 'name', $newname); // Moved here to save newly-created scripts | |
$handle = fopen($newfile.'.js', 'wb'); | |
fwrite($handle, $js_raw); | |
fclose($handle); | |
chmod($newfile.'.js', 0644); | |
} | |
} | |
/** | |
* Delete function | |
*/ | |
function spf_js_delete() | |
{ | |
global $prefs; | |
$dbname = trim(preg_replace('/[<>&"\']/', '', gps('name'))); | |
$name = strtolower(sanitizeForUrl(ps('name'))); | |
$file = $prefs['path_to_site'].'/'.$prefs['spf_js_dir'].'/'.$name; | |
$message = ''; | |
safe_delete("spf_js", "name = '".doSlash($dbname)."'"); | |
callback_event('spf_script_deleted', '', 0, $name); | |
$message = gTxt('spf_script_deleted', array('{name}' => $name)); | |
if (!empty($prefs['spf_js_dir']) and $name) { | |
@unlink($file.'.js'); | |
} | |
spf_js_edit($message); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment