Assembla home | Assembla project page
 

root/trunk/Phergie/Plugin/Set.php

Revision 260, 3.9 kB (checked in by Slynderdale, 5 months ago)

Fixed a bug in Set that adds quotes about the value that gets set to the INI value.

Line 
1 <?php
2
3 /**
4  * Handles requests from administrators for the bot to set a .ini value.
5  */
6 class Phergie_Plugin_Set extends Phergie_Plugin_Abstract_Command
7 {
8     /**
9      * Flag indicating whether or not the plugin is an admin plugin or not
10      *
11      * @var bool
12      */
13     public $needsAdmin = true;
14
15     /**
16      * Sets an ini value using <Botname>: set [-a|append] varname value
17      */
18     public function onDoSet($var, $value, $tmp = null)
19     {
20         $user = $this->event->getNick();
21         if ($this->fromAdmin(true)) {
22             $append = $var === '-a' || $var === 'append' || $var === '-append';
23             // Got an append parameter, so we shift value/tmp down to var/value
24             if ($append) {
25                 $var = $value;
26                 $value = $tmp;
27             } elseif (!empty($tmp)) {
28                 $value .= ' ' . $tmp;
29             }
30
31             // Get ini file
32             $contents = preg_replace("#\r\n|\r|\n#", PHP_EOL, file_get_contents(PHERGIE_INI_PATH));
33             // Replace var/value
34             if (preg_match('#^(' . str_replace('.', '\\.', $var) . '\s*= *)(.*)$#im', $contents, $m)) {
35                 $contents = preg_replace('#^' . str_replace('.', '\\.', $var) . '\s*=.*$#im', $var . ' = ' . str_replace('$', '\\$', $this->makeIniValue($value, $m[2], $append)), $contents);
36                 $this->setIni($var, $this->parseIniValue($this->makeIniValue($value, $m[2], $append, true)));
37                 $this->doNotice($user, 'Updated Setting: ' . $var . ' = ' . $this->makeIniValue($this->getIni($var)));
38             // Insert it if not set
39             } else {
40                 $contents .= PHP_EOL . $var . ' = ' . $this->makeIniValue($value);
41                 $this->setIni($var, $this->parseIniValue($value));
42                 $this->doNotice($user, 'Inserted Setting: ' . $var . ' = ' . $this->makeIniValue($this->getIni($var)));
43             }
44             // Save ini file
45             file_put_contents(PHERGIE_INI_PATH, $contents);
46         } else {
47             $this->doNotice($user, 'You do not have permission to set any settings.');
48         }
49     }
50
51     /**
52      * Returns a ini setting
53      */
54     public function onDoGet($var)
55     {
56         $user = $this->event->getNick();
57         if ($this->fromAdmin(true)) {
58             $this->doNotice($user, $var . ' = ' . $this->makeIniValue($this->getIni($var)));
59         } else {
60             $this->doNotice($user, 'You do not have permission to get any settings.');
61         }
62     }
63
64     /**
65      * Builds a ini value
66      */
67     protected function makeIniValue($new, $old = "", $append = false, $noquotes = false)
68     {
69         $new = trim($new, "\" '");
70         $old = trim($old, "\" \n\r");
71
72         if ($append && !empty($old)) {
73             return ($noquotes?'':'"') . $old . ', ' . $new . ($noquotes?'':'"');
74         }
75
76         if (is_numeric($new)) {
77             return $new;
78         }
79
80         if ($this->parseIniValue($new) != $new) {
81             return (strtolower($new) != 'null' ? $new : '');
82         } else {
83             return ($noquotes?'':'"') . $new . ($noquotes?'':'"');
84         }
85     }
86
87     /**
88      * Reads a ini value similarly to parse_ini_file()
89      */
90     protected function parseIniValue($value)
91     {
92         $value = trim($value);
93         if (strpos($value, ':') !== false) {
94             return $value;
95         }
96
97         $const = get_defined_constants();
98
99         switch (strtolower($value)) {
100             case 'true':
101             case 'yes':
102             case 'on':
103             case '1':
104                 $return = '1';
105             break;
106             case 'false':
107             case 'no':
108             case 'off':
109             case '0':
110             case '':
111                 $return = '';
112             break;
113             case 'null':
114                 $return = null;
115             break;
116             default:
117                 $return = (isset($const[$value]) ? $const[$value] : $value);
118             break;
119         }
120         unset($const, $value);
121
122         return $return;
123     }
124 }
125
Note: See TracBrowser for help on using the browser.