Advertisement
alanfluff

cnk_versioning for txp 4.41 - modified code once plugin is i

Feb 1st, 2012
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.45 KB | None | 0 0
  1. if (!function_exists('file_put_contents'))
  2. {
  3.     function file_put_contents($filename, $data)
  4.     {
  5.         $f = @fopen($filename, 'w');
  6.        
  7.         if (!$f)
  8.         {
  9.             return false;
  10.         }
  11.         else
  12.         {
  13.             $bytes = fwrite($f, $data);
  14.             fclose($f);
  15.             return $bytes;
  16.         }
  17.     }
  18. }
  19.  
  20. global $CNK_VER_OUTPUT_PATH, $CNK_VER_EXT, $CNK_VER_EXT_CSS;
  21.  
  22. $CNK_VER_OUTPUT_PATH = 'my-nice-folder/cnk_templates/'; //e.g. 'textpattern/_templates/versioning/'
  23. $CNK_VER_EXT = 'php';
  24. $CNK_VER_EXT_CSS = 'css';
  25.  
  26. /*
  27.     DO NOT EDIT BELOW THIS LINE!
  28. */
  29.  
  30. $CNK_VER_OUTPUT_PATH = trim($CNK_VER_OUTPUT_PATH, '/').($CNK_VER_OUTPUT_PATH?'/':'');
  31.  
  32. if(@txpinterface == 'admin')
  33. {
  34.     add_privs('cnk_versioning','1,2');
  35.     register_tab('presentation', 'cnk_versioning', "Versioning");
  36.     register_callback('cnk_ver_handler', 'cnk_versioning');
  37.     register_callback('cnk_ver_disable_online_editing', 'page');
  38.     register_callback('cnk_ver_disable_online_editing', 'form');
  39.     register_callback('cnk_ver_disable_online_editing', 'css');
  40. }
  41. else if (@txpinterface == 'public')
  42. {
  43.     register_callback('cnk_ver_textpattern', 'textpattern');
  44. }
  45.  
  46. function cnk_ver_textpattern()
  47. {
  48.     global $production_status, $CNK_VER_OUTPUT_PATH, $CNK_VER_EXT, $CNK_VER_EXT_CSS;
  49.    
  50.     if ($production_status != 'live')
  51.     {
  52.         $error = false;
  53.        
  54.         // read all files
  55.         $forms = glob($CNK_VER_OUTPUT_PATH.'forms/*.'.$CNK_VER_EXT);
  56.         $pages = glob($CNK_VER_OUTPUT_PATH.'pages/*.'.$CNK_VER_EXT);
  57.         $css = glob($CNK_VER_OUTPUT_PATH.'css/*.'.$CNK_VER_EXT_CSS);
  58.        
  59.         if ($forms !== false) $error = !cnk_ver_push_forms($forms);
  60.         if ($pages !== false) $error = !cnk_ver_push_pages($pages);
  61.         if ($css !== false) $error = !cnk_ver_push_css($css);
  62.        
  63.         if ($error) echo 'Errors while synchronising database and files';
  64.     }
  65. }
  66.  
  67. function cnk_ver_push_forms($form_files)
  68. {
  69.     global $CNK_VER_OUTPUT_PATH, $CNK_VER_EXT;
  70.    
  71.     $forms = array();
  72.  
  73.     $rs = safe_rows_start('name,type,IFNULL(unix_timestamp(file_mod_time), 0) as mod_time', 'txp_form', '1=1');
  74.    
  75.     while ($rs && $r = nextRow($rs))
  76.     {
  77.         $forms[$CNK_VER_OUTPUT_PATH.'forms/'.$r['name'].'.'.$r['type'].'.'.$CNK_VER_EXT] = $r['mod_time'];
  78.     }
  79.    
  80.     for ($i=0; $i < count($form_files); $i++)
  81.     {
  82.         if (isset($forms[$form_files[$i]]) && $forms[$form_files[$i]] < filemtime($form_files[$i]))
  83.         {
  84.             cnk_ver_update_form($form_files[$i]);
  85.         }
  86.         else if (!isset($forms[$form_files[$i]]))
  87.         {
  88.             cnk_ver_update_form($form_files[$i], true);
  89.         }
  90.         $forms[$form_files[$i]] = 'processed';
  91.     }
  92.        
  93.     // delete removed forms
  94.     foreach ($forms as $key => $value)
  95.     {
  96.         if ($value != 'processed')
  97.         {
  98.             $cols = explode('.', basename($key, '.'.$CNK_VER_EXT));
  99.             $name = $cols[0];
  100.             $type = $cols[1];
  101.            
  102.             safe_delete('txp_form', "name = '$name' AND type = '$type'");
  103.         }
  104.     }
  105.  
  106.     return true;
  107. }
  108.  
  109. function cnk_ver_update_form($filename, $create = false)
  110. {
  111.     global $CNK_VER_EXT;
  112.    
  113.     $cols = explode('.', basename($filename, '.'.$CNK_VER_EXT));
  114.  
  115.     $name = $cols[0];
  116.     $type = $cols[1];
  117.     $form = doSlash(file_get_contents($filename));
  118.    
  119.     if ($create)
  120.     {
  121.         safe_insert('txp_form', "name = '$name', type = '$type', form = '$form', file_mod_time = FROM_UNIXTIME('".time()."')");
  122.     }
  123.     else
  124.     {
  125.         safe_update('txp_form', "form = '$form', file_mod_time = FROM_UNIXTIME('".time()."')", "name = '$name' AND type = '$type'");
  126.     }
  127. }
  128.  
  129. function cnk_ver_push_pages($page_files)
  130. {
  131.     global $CNK_VER_OUTPUT_PATH, $CNK_VER_EXT;
  132.    
  133.     $pages = array();
  134.  
  135.     $rs = safe_rows_start('name,IFNULL(unix_timestamp(file_mod_time), 0) as mod_time', 'txp_page', '1=1');
  136.    
  137.     while ($rs && $r = nextRow($rs))
  138.     {
  139.         $pages[$CNK_VER_OUTPUT_PATH.'pages/'.$r['name'].'.'.$CNK_VER_EXT] = $r['mod_time'];
  140.     }
  141.    
  142.     for ($i=0; $i < count($page_files); $i++)
  143.     {
  144.         if (isset($pages[$page_files[$i]]) && $pages[$page_files[$i]] < filemtime($page_files[$i]))
  145.         {
  146.             cnk_ver_update_page($page_files[$i]);
  147.         }
  148.         else if (!isset($pages[$page_files[$i]]))
  149.         {
  150.             cnk_ver_update_page($page_files[$i], true);
  151.         }
  152.         $pages[$page_files[$i]] = 'processed';
  153.     }
  154.        
  155.     // delete removed pages
  156.     foreach ($pages as $key => $value)
  157.     {
  158.         if ($value != 'processed')
  159.         {
  160.             $name = basename($key, '.'.$CNK_VER_EXT);
  161.            
  162.             safe_delete('txp_page', "name = '$name'");
  163.         }
  164.     }
  165.  
  166.     return true;
  167. }
  168.  
  169. function cnk_ver_update_page($filename, $create = false)
  170. {
  171.     global $CNK_VER_EXT;
  172.    
  173.     $name = basename($filename, '.'.$CNK_VER_EXT);
  174.     $user_html = doSlash(file_get_contents($filename));
  175.    
  176.     if ($create)
  177.     {
  178.         safe_insert('txp_page', "name = '$name', user_html = '$user_html', file_mod_time = FROM_UNIXTIME('".time()."')");
  179.     }
  180.     else
  181.     {
  182.         safe_update('txp_page', "user_html = '$user_html', file_mod_time = FROM_UNIXTIME('".time()."')", "name = '$name'");
  183.     }
  184. }
  185.  
  186. function cnk_ver_push_css($css_files)
  187. {
  188.     global $CNK_VER_OUTPUT_PATH, $CNK_VER_EXT_CSS;
  189.    
  190.     $css = array();
  191.  
  192.     $rs = safe_rows_start('name,IFNULL(unix_timestamp(file_mod_time), 0) as mod_time', 'txp_css', '1=1');
  193.    
  194.     while ($rs && $r = nextRow($rs))
  195.     {
  196.         $css[$CNK_VER_OUTPUT_PATH.'css/'.$r['name'].'.'.$CNK_VER_EXT_CSS] = $r['mod_time'];
  197.     }
  198.    
  199.     for ($i=0; $i < count($css_files); $i++)
  200.     {
  201.         if (isset($css[$css_files[$i]]) && $css[$css_files[$i]] < filemtime($css_files[$i]))
  202.         {
  203.             cnk_ver_update_css($css_files[$i]);
  204.         }
  205.         else if (!isset($css[$css_files[$i]]))
  206.         {
  207.             cnk_ver_update_css($css_files[$i], true);
  208.         }
  209.         $css[$css_files[$i]] = 'processed';
  210.     }
  211.        
  212.     // delete removed css files
  213.     foreach ($css as $key => $value)
  214.     {
  215.         if ($value != 'processed')
  216.         {
  217.             $name = basename($key, '.'.$CNK_VER_EXT_CSS);
  218.            
  219.             safe_delete('txp_css', "name = '$name'");
  220.         }
  221.     }
  222.  
  223.     return true;
  224. }
  225.  
  226. function cnk_ver_update_css($filename, $create = false)
  227. {
  228.     global $CNK_VER_EXT_CSS;
  229.    
  230.     $name = basename($filename, '.'.$CNK_VER_EXT_CSS);
  231.     $css = doSlash(base64_encode(file_get_contents($filename)));
  232.    
  233.     if ($create)
  234.     {
  235.         safe_insert('txp_css', "name = '$name', css = '$css', file_mod_time = FROM_UNIXTIME('".time()."')");
  236.     }
  237.     else
  238.     {
  239.         safe_update('txp_css', "css = '$css', file_mod_time = FROM_UNIXTIME('".time()."')", "name = '$name'");
  240.     }
  241. }
  242.  
  243. function cnk_ver_handler($event, $step)
  244. {
  245.     if(!$step or !in_array($step, array('cnk_ver_menu',
  246.                                         'cnk_ver_pull_all',
  247.                                         'cnk_ver_install',
  248.                                         'cnk_ver_deinstall')))
  249.     {
  250.         cnk_ver_menu();
  251.     }
  252.     else
  253.     {
  254.         $step();
  255.     }
  256. }
  257.  
  258. function cnk_ver_disable_online_editing($event, $step)
  259. {
  260.     // clear ob
  261.     ob_end_clean();
  262.    
  263.     // return error message
  264.    
  265.     pagetop('Versioning');
  266.    
  267.     echo '<div style="margin: auto; text-align: center"><ul>';
  268.    
  269.     echo 'While cnk_versioning is enabled, this function is disabled.';
  270.    
  271.     echo '</div>';
  272. }
  273.  
  274. function cnk_ver_menu($message = '')
  275. {
  276.     pagetop('Versioning', $message);
  277.    
  278.     echo '<div style="margin: auto; text-align: center"><ul>';
  279.    
  280.     echo '<li><a href="?event=cnk_versioning'.a.'step=cnk_ver_pull_all">Write pages & forms to files</a></li>';
  281.    
  282.     echo '</ul>';
  283.    
  284.     echo '<ul style="margin-top: 100px">'; 
  285.    
  286.     echo '<li><a href="?event=cnk_versioning'.a.'step=cnk_ver_install">Install</a></li>';
  287.    
  288.     echo '<li><a href="?event=cnk_versioning'.a.'step=cnk_ver_deinstall">Deinstall</a></li>';
  289.  
  290.     echo '</ul>';
  291.    
  292.     echo '</div>';
  293. }
  294.  
  295. function cnk_ver_pull_all()
  296. {
  297.     global $CNK_VER_OUTPUT_PATH, $CNK_VER_EXT, $CNK_VER_EXT_CSS;
  298.    
  299.     pagetop('Write to files', '');
  300.    
  301.     echo '<div style="margin: auto; text-align: center">';
  302.                
  303.     if (gps('do_pull') || (glob('../'.$CNK_VER_OUTPUT_PATH.'pages/*.'.$CNK_VER_EXT) === false && glob('../'.$CNK_VER_OUTPUT_PATH.'forms/*.'.$CNK_VER_EXT) === false && glob('../'.$CNK_VER_OUTPUT_PATH.'css/*.'.$CNK_VER_EXT_CSS) === false))
  304.     {
  305.         $error = false;
  306.        
  307.         // test if folders exist and have write permissions
  308.         if (@is_writable('../'.$CNK_VER_OUTPUT_PATH.'forms/') === false)
  309.         {
  310.             $error = true;
  311.             echo 'Folder "/forms/" is not writable.<br /><br />';
  312.         }
  313.        
  314.         if (@is_writable('../'.$CNK_VER_OUTPUT_PATH.'pages/') === false)
  315.         {
  316.             $error = true;
  317.             echo 'Folder "/pages/" is not writable.<br /><br />';
  318.         }
  319.        
  320.         if (@is_writable('../'.$CNK_VER_OUTPUT_PATH.'css/') === false)
  321.         {
  322.             $error = true;
  323.             echo 'Folder "/css/" is not writable.';
  324.         }
  325.        
  326.         if (!$error)
  327.         {
  328.                 if (cnk_ver_pull_forms())
  329.                 {
  330.                     echo 'Forms were successfully written to the "/forms/" directory.<br /><br />';
  331.                 }
  332.                 else
  333.                 {
  334.                     echo 'There was an error while processing forms.<br /><br />';
  335.                 }
  336.                
  337.                 if (cnk_ver_pull_pages())
  338.                 {
  339.                     echo 'Pages were successfully written to the "/pages/" directory.<br /><br />';
  340.                 }
  341.                 else
  342.                 {
  343.                     echo 'There was an error while processing pages.<br /><br />';
  344.                 }
  345.                
  346.                 if (cnk_ver_pull_css())
  347.                 {
  348.                     echo 'Styles were successfully written to the "/css/" directory.';
  349.                 }
  350.                 else
  351.                 {
  352.                     echo 'There was an error while processing css.';
  353.                 }
  354.         }
  355.        
  356.         echo '<br /><br /><a href="?event=cnk_versioning">Back to menu...</a>';
  357.     }
  358.     else
  359.     {
  360.         echo 'There are already some files in the pages, forms or css directory, which will be overriden. Do you want to continue?<br /><br />'.
  361.        
  362.         '<a href="?event=cnk_versioning'.a.'step=cnk_ver_pull_all'.a.'do_pull=1">Yes, overwrite existing files...</a><br /><br />'.
  363.        
  364.         '<a href="?event=cnk_versioning">No, bring me back to the menu...</a>';
  365.     }
  366.    
  367.     echo '</div>';
  368. }
  369.  
  370. function cnk_ver_pull_forms()
  371. {
  372.     global $CNK_VER_OUTPUT_PATH, $CNK_VER_EXT;
  373.    
  374.     $rs = safe_rows_start('name, type, form', 'txp_form', '1=1');
  375.    
  376.     while ($rs && $r = nextRow($rs))
  377.     {
  378.         if (@file_put_contents('../'.$CNK_VER_OUTPUT_PATH.'forms/'.$r['name'].'.'.$r['type'].'.'.$CNK_VER_EXT, $r['form']) === false) return false;
  379.     }
  380.    
  381.     safe_update('txp_form', "file_mod_time = FROM_UNIXTIME('".time()."')", '1=1');
  382.    
  383.     return true;
  384. }
  385.  
  386. function cnk_ver_pull_pages()
  387. {
  388.     global $CNK_VER_OUTPUT_PATH, $CNK_VER_EXT;
  389.    
  390.     $rs = safe_rows_start('name, user_html', 'txp_page', '1=1');
  391.    
  392.     while ($rs && $r = nextRow($rs))
  393.     {
  394.         if (@file_put_contents('../'.$CNK_VER_OUTPUT_PATH.'pages/'.$r['name'].'.'.$CNK_VER_EXT, $r['user_html']) === false) return false;
  395.     }
  396.    
  397.     safe_update('txp_page', "file_mod_time = FROM_UNIXTIME('".time()."')", '1=1');
  398.    
  399.     return true;
  400. }
  401.  
  402. function cnk_ver_pull_css()
  403. {
  404.     global $CNK_VER_OUTPUT_PATH, $CNK_VER_EXT_CSS;
  405.    
  406.     $rs = safe_rows_start('name, css', 'txp_css', '1=1');
  407.    
  408.     while ($rs && $r = nextRow($rs))
  409.     {
  410.         if (@file_put_contents('../'.$CNK_VER_OUTPUT_PATH.'css/'.$r['name'].'.'.$CNK_VER_EXT_CSS, base64_decode($r['css'])) === false) return false;
  411.     }
  412.    
  413.     safe_update('txp_css', "file_mod_time = FROM_UNIXTIME('".time()."')", '1=1');
  414.    
  415.     return true;
  416. }
  417.  
  418. function cnk_ver_install()
  419. {
  420.     pagetop('Versioning Plugin Installation', '');
  421.    
  422.     echo '<div style="margin:auto; text-align:center">';
  423.    
  424.     if (cnk_ver_do_install())
  425.     {
  426.         echo '<p>Installation was successful</p>';
  427.     }
  428.     else
  429.     {
  430.         echo '<p>Installation aborted</p>';
  431.     }
  432.  
  433.     echo '</div>';
  434. }
  435.  
  436. function cnk_ver_do_install()
  437. {
  438.     $sql = "ALTER TABLE ".safe_pfx('txp_form')." ADD `file_mod_time` DATETIME NULL;ALTER TABLE ".safe_pfx('txp_page')." ADD `file_mod_time` DATETIME NULL;ALTER TABLE ".safe_pfx('txp_css')." ADD `file_mod_time` DATETIME NULL";
  439.    
  440.     if (!cnk_ver_batch_query($sql))
  441.     {
  442.         return false;
  443.     }
  444.     else
  445.     {  
  446.         return true;
  447.     }
  448. }
  449.  
  450. function cnk_ver_deinstall()
  451. {
  452.     pagetop('Versioning Plugin Deinstallation', '');
  453.    
  454.     echo '<div style="margin:auto; text-align:center">';
  455.    
  456.     if (gps('do_deinstall'))
  457.     {
  458.         if (cnk_ver_do_deinstall())
  459.         {
  460.             echo '<p>Deinstallation was successful</p>';
  461.         }
  462.         else
  463.         {
  464.             echo '<p>Deinstallation aborted</p>';
  465.         }
  466.     }
  467.     else
  468.     {
  469.         echo '<a href="?event=cnk_versioning'.a.'step=cnk_ver_deinstall'.a.'do_deinstall=1">Yes, I really want to deinstall</a>';
  470.     }
  471.    
  472.     echo '</div>';
  473. }
  474.  
  475. function cnk_ver_do_deinstall()
  476. {
  477.     $sql = "ALTER TABLE ".safe_pfx('txp_form')." DROP `file_mod_time`;ALTER TABLE ".safe_pfx('txp_page')." DROP `file_mod_time`;ALTER TABLE ".safe_pfx('txp_css')." DROP `file_mod_time`;";
  478.    
  479.     if (!cnk_ver_batch_query($sql))
  480.     {
  481.         return false;
  482.     }
  483.     else
  484.     {  
  485.         return true;
  486.     }
  487. }
  488.  
  489. function cnk_ver_batch_query ($p_query, $p_transaction_safe = true)
  490. {
  491.     if ($p_transaction_safe)
  492.     {
  493.         $p_query = 'START TRANSACTION;' . $p_query . '; COMMIT;';
  494.     }
  495.      
  496.     $query_split = preg_split ("/[;]+/", $p_query);
  497.    
  498.     foreach ($query_split as $command_line)
  499.     {
  500.         $command_line = trim($command_line);
  501.    
  502.         if ($command_line != '')
  503.         {
  504.             $query_result = safe_query($command_line);
  505.        
  506.             if ($query_result === false)
  507.             {
  508.                 break;
  509.             }
  510.         }
  511.     }
  512.    
  513.     return $query_result;
  514. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement