symphonycms /
symphony-2
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * @package toolkit |
||
| 5 | */ |
||
| 6 | |||
| 7 | /** |
||
| 8 | * A manager to standardize the finding and listing of installed gateways. |
||
| 9 | */ |
||
| 10 | |||
| 11 | class EmailGatewayManager implements FileResource |
||
| 12 | { |
||
| 13 | /** |
||
| 14 | * The default gateway to use when one is not provided. This value can |
||
| 15 | * be overridden with the `setDefaultGateway` function. Defaults to 'sendmail'. |
||
| 16 | * |
||
| 17 | * @see setDefaultGateway() |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected static $_default_gateway = 'sendmail'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * Sets the default gateway. |
||
| 24 | * Will throw an exception if the gateway can not be found. |
||
| 25 | * |
||
| 26 | * @throws EmailGatewayException |
||
| 27 | * @param string $name |
||
| 28 | * @return void |
||
| 29 | */ |
||
| 30 | public static function setDefaultGateway($name) |
||
| 31 | { |
||
| 32 | if (self::__getClassPath($name)) { |
||
| 33 | Symphony::Configuration()->set('default_gateway', $name, 'Email'); |
||
| 34 | Symphony::Configuration()->write(); |
||
| 35 | } else { |
||
| 36 | throw new EmailGatewayException(__('This gateway can not be found. Can not save as default.')); |
||
| 37 | } |
||
| 38 | } |
||
| 39 | |||
| 40 | /** |
||
| 41 | * Returns the default gateway. |
||
| 42 | * Will throw an exception if the gateway can not be found. |
||
| 43 | * |
||
| 44 | * @return string |
||
| 45 | */ |
||
| 46 | public static function getDefaultGateway() |
||
| 47 | { |
||
| 48 | $gateway = Symphony::Configuration()->get('default_gateway', 'Email'); |
||
| 49 | |||
| 50 | if ($gateway) { |
||
| 51 | return $gateway; |
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 52 | } else { |
||
| 53 | return self::$_default_gateway; |
||
| 54 | } |
||
| 55 | } |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Returns the classname from the gateway name. |
||
| 59 | * Does not check if the gateway exists. |
||
| 60 | * |
||
| 61 | * @param string $name |
||
| 62 | * @return string |
||
| 63 | */ |
||
| 64 | public static function __getClassName($name) |
||
| 65 | { |
||
| 66 | return $name . 'Gateway'; |
||
| 67 | } |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Finds the gateway by name |
||
| 71 | * |
||
| 72 | * @param string $name |
||
| 73 | * The gateway to look for |
||
| 74 | * @return string|boolean |
||
| 75 | * If the gateway is found, the path to the folder containing the |
||
| 76 | * gateway is returned. |
||
| 77 | * If the gateway is not found, false is returned. |
||
| 78 | */ |
||
| 79 | public static function __getClassPath($name) |
||
| 80 | { |
||
| 81 | if (is_file(EMAILGATEWAYS . "/email.$name.php")) { |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $name instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 82 | return EMAILGATEWAYS; |
||
| 83 | } else { |
||
| 84 | $extensions = Symphony::ExtensionManager()->listInstalledHandles(); |
||
| 85 | |||
| 86 | if (is_array($extensions) && !empty($extensions)) { |
||
| 87 | foreach ($extensions as $e) { |
||
| 88 | if (is_file(EXTENSIONS . "/$e/email-gateways/email.$name.php")) { |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $e instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
As per coding-style, please use concatenation or
sprintf for the variable $name instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 89 | return EXTENSIONS . "/$e/email-gateways"; |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $e instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 90 | } |
||
| 91 | } |
||
| 92 | } |
||
| 93 | } |
||
| 94 | |||
| 95 | return false; |
||
| 96 | } |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Returns the path to the gateway file. |
||
| 100 | * |
||
| 101 | * @param string $name |
||
| 102 | * The gateway to look for |
||
| 103 | * @return string|boolean |
||
| 104 | * @todo fix return if gateway does not exist. |
||
| 105 | */ |
||
| 106 | public static function __getDriverPath($name) |
||
| 107 | { |
||
| 108 | return self::__getClassPath($name) . "/email.$name.php"; |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $name instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 109 | } |
||
| 110 | |||
| 111 | /** |
||
| 112 | * Finds the name from the filename. |
||
| 113 | * Does not check if the gateway exists. |
||
| 114 | * |
||
| 115 | * @param string $filename |
||
| 116 | * @return string|boolean |
||
| 117 | */ |
||
| 118 | public static function __getHandleFromFilename($filename) |
||
| 119 | { |
||
| 120 | return preg_replace(array('/^email./i', '/.php$/i'), '', $filename); |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Returns an array of all gateways. |
||
| 125 | * Each item in the array will contain the return value of the about() |
||
| 126 | * function of each gateway. |
||
| 127 | * |
||
| 128 | * @return array |
||
| 129 | */ |
||
| 130 | public static function listAll() |
||
| 131 | { |
||
| 132 | $result = array(); |
||
| 133 | $structure = General::listStructure(EMAILGATEWAYS, '/email.[\\w-]+.php/', false, 'ASC', EMAILGATEWAYS); |
||
|
0 ignored issues
–
show
Are you sure the assignment to
$structure is correct as General::listStructure(E..., 'ASC', EMAILGATEWAYS) targeting General::listStructure() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 134 | |||
| 135 | if (is_array($structure['filelist']) && !empty($structure['filelist'])) { |
||
| 136 | foreach ($structure['filelist'] as $f) { |
||
| 137 | $f = str_replace(array('email.', '.php'), '', $f); |
||
| 138 | $result[$f] = self::about($f); |
||
| 139 | } |
||
| 140 | } |
||
| 141 | |||
| 142 | $extensions = Symphony::ExtensionManager()->listInstalledHandles(); |
||
| 143 | |||
| 144 | if (is_array($extensions) && !empty($extensions)) { |
||
| 145 | foreach ($extensions as $e) { |
||
| 146 | if (!is_dir(EXTENSIONS . "/$e/email-gateways")) { |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $e instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
|
|||
| 147 | continue; |
||
| 148 | } |
||
| 149 | |||
| 150 | $tmp = General::listStructure(EXTENSIONS . "/$e/email-gateways", '/email.[\\w-]+.php/', false, 'ASC', EXTENSIONS . "/$e/email-gateways"); |
||
|
0 ignored issues
–
show
As per coding-style, please use concatenation or
sprintf for the variable $e instead of interpolation.
It is generally a best practice as it is often more readable to use concatenation instead of interpolation for variables inside strings. // Instead of
$x = "foo $bar $baz";
// Better use either
$x = "foo " . $bar . " " . $baz;
$x = sprintf("foo %s %s", $bar, $baz);
Loading history...
Are you sure the assignment to
$tmp is correct as General::listStructure(E...'.$e.'/email-gateways') targeting General::listStructure() seems to always return null.
This check looks for function or method calls that always return null and whose return value is assigned to a variable. class A
{
function getObject()
{
return null;
}
}
$a = new A();
$object = $a->getObject();
The method The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes. Loading history...
|
|||
| 151 | |||
| 152 | if (is_array($tmp['filelist']) && !empty($tmp['filelist'])) { |
||
| 153 | foreach ($tmp['filelist'] as $f) { |
||
| 154 | $f = preg_replace(array('/^email./i', '/.php$/i'), '', $f); |
||
| 155 | $result[$f] = self::about($f); |
||
| 156 | } |
||
| 157 | } |
||
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | ksort($result); |
||
| 162 | return $result; |
||
| 163 | } |
||
| 164 | |||
| 165 | public static function about($name) |
||
| 166 | { |
||
| 167 | $name = strtolower($name); |
||
| 168 | $classname = self::__getClassName($name); |
||
| 169 | $path = self::__getDriverPath($name); |
||
| 170 | |||
| 171 | if (!@file_exists($path)) { |
||
| 172 | return false; |
||
| 173 | } |
||
| 174 | |||
| 175 | require_once $path; |
||
| 176 | |||
| 177 | $handle = self::__getHandleFromFilename(basename($path)); |
||
| 178 | |||
| 179 | if (is_callable(array($classname, 'about'))) { |
||
| 180 | $about = call_user_func(array($classname, 'about')); |
||
| 181 | |||
| 182 | return array_merge($about, array('handle' => $handle)); |
||
| 183 | } |
||
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Creates a new object from a gateway name. |
||
| 188 | * |
||
| 189 | * @param string $name |
||
| 190 | * The gateway to look for |
||
| 191 | * @throws Exception |
||
| 192 | * @return EmailGateway |
||
| 193 | * If the gateway is found, an instantiated object is returned. |
||
| 194 | * If the gateway is not found, an error is triggered. |
||
| 195 | */ |
||
| 196 | public static function create($name) |
||
| 197 | { |
||
| 198 | $name = strtolower($name); |
||
| 199 | $classname = self::__getClassName($name); |
||
| 200 | $path = self::__getDriverPath($name); |
||
| 201 | |||
| 202 | if (!is_file($path)) { |
||
| 203 | throw new Exception( |
||
| 204 | __('Could not find Email Gateway %s.', array('<code>' . $name . '</code>')) |
||
| 205 | . ' ' . __('If it was provided by an Extension, ensure that it is installed, and enabled.') |
||
| 206 | ); |
||
| 207 | } |
||
| 208 | |||
| 209 | if (!class_exists($classname)) { |
||
| 210 | require_once $path; |
||
| 211 | } |
||
| 212 | |||
| 213 | return new $classname; |
||
| 214 | } |
||
| 215 | } |
||
| 216 |