1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8/** 9 * \brief Watch command for debugger 10 * \author zaufi <zaufi@sendmail.ru> 11 */ 12require_once('lib/debug/debugger-ext.php'); 13 14/** 15 * \brief Command 'watch' 16 */ 17class DbgCmd_Watch extends DebuggerCommand 18{ 19 /// Array of variables to watch in format: [md5hash] = var_name 20 var $watches; 21 22 /// Restore watches list at construction time 23 function __construct() 24 { 25 global $user; 26 27 $this->watches = []; 28 29 if (is_readable($this->watchfile())) { 30 $s = implode('', @file($this->watchfile())); 31 32 $a = unserialize($s); 33 34 if (count($a) > 0) { 35 $this->watches = (array)$a; 36 } 37 } 38 } 39 40 function name() 41 { 42 return 'watch'; 43 } 44 45 function description() 46 { 47 return 'Manage variables watch list'; 48 } 49 50 function syntax() 51 { 52 return 'watch (add|rm) $php_var1 smarty_var2 $php_var3 smarty_var4 ...'; 53 } 54 55 function example() 56 { 57 return 'watch add $user tiki_p_view' . "\n" . 'watch rm user $_REQUEST $_SERVER["HTTP_USER_AGENT"]'; 58 } 59 60 function execute($params) 61 { 62 global $user; 63 64 // NOTE: Don't forget to set result type! By default it is NO_RESULT. 65 $this->set_result_type(TEXT_RESULT); 66 $result = ''; 67 $args = explode(' ', trim($params)); 68 69 // 70 if (count($args) > 0) { 71 $cmd = trim($args[0]); 72 73 if ($cmd == 'add' || $cmd == 'rm') { 74 $a_r = ($cmd == 'add'); 75 76 array_shift($args); 77 78 if (count($args) > 0) { 79 foreach ($args as $a) { 80 if (strlen(trim(str_replace('$', '', $a))) > 0) { // Is there smth 'cept '$'?? 81 $a = trim($a); 82 83 if ($a_r) { 84 $result .= "add '" . $a . "' to watch list\n"; 85 $this->watches[md5($a)] = $a; 86 } else { 87 $result .= "remove '" . $a . "' from watch list\n"; 88 89 if (isset($this->watches[md5($a)])) { 90 unset($this->watches[md5($a)]); 91 } else { 92 $result .= "ERROR: No such variable in watch list\n"; 93 } 94 } 95 } 96 } 97 98 // Store changes in watchlist to disk 99 $this->store_watches(); 100 } else { 101 $result .= "ERROR: No variable to add given"; 102 } 103 } elseif (strlen(trim($args[0])) > 0) { 104 $result .= "ERROR: Unknown subcommand '$arg[0]'"; 105 } else { 106 $result .= "ERROR: No subcommand for 'watch' given"; 107 } 108 } else { 109 $result .= "ERROR: No subcommand for 'watch' given"; 110 } 111 112 return $result; 113 } 114 115 /// Return the name of watches file 116 function watchfile() 117 { 118 global $user; 119 120 return 'temp/dbg-watch.' . $user; 121 } 122 123 /// Save watchlist for given user. If current list is empty --> remove file. 124 function store_watches() 125 { 126 if (count($this->watches) > 0) { 127 $s = serialize($this->watches); 128 129 $fp = fopen($this->watchfile(), 'w'); 130 fputs($fp, $s); 131 fclose($fp); 132 } else { 133 if (is_writable($this->watchfile())) { 134 unlink($this->watchfile()); 135 } 136 } 137 } 138 139 /// Function to create interface part of command: return ["button name"] = <html code> 140 function draw_interface() 141 { 142 $result = []; 143 144 // Iterate through all variables 145 foreach ($this->watches as $v) { 146 // NOTE: PHP variables must start with '$', else assumed smarty variable 147 $result[] = [ 148 'var' => $v, 149 'value' => ((substr($v, 0, 1) == '$') ? $this->value_of_php_var($v) : $this->value_of_smarty_var($v)) 150 ]; 151 } 152 153 // 154 $smarty = TikiLib::lib('smarty'); 155 $smarty->assign_by_ref('watchlist', $result); 156 return $smarty->fetch("debug/tiki-debug_watch_tab.tpl"); 157 } 158 159 /// 160 function value_of_smarty_var($v) 161 { 162 $smarty = TikiLib::lib('smarty'); 163 164 $result = ''; 165 166 if (strlen($v) != 0) { 167 $tmp = $smarty->getTemplateVars(); 168 169 if (is_array($tmp) && isset($tmp[$v])) { 170 $result .= print_r($tmp[$v], true) . "\n"; 171 } else { 172 $result .= 'Smarty variable "' . $v . '" not found'; 173 } 174 } 175 176 return $result; 177 } 178 179 /// 180 function value_of_php_var($v) 181 { 182 global $debugger; 183 184 require_once('lib/debug/debugger.php'); 185 return $debugger->str_var_dump($v); 186 } 187 188 /// Function to return caption string to draw plugable tab in interface 189 function caption() 190 { 191 return 'watches'; 192 } 193 194 /// Need to display button if we have smth to show 195 function have_interface() 196 { 197 return count($this->watches) > 0; 198 } 199} 200 201/// Class factory 202function dbg_command_factory_watch() 203{ 204 return new DbgCmd_Watch(); 205} 206