aboutsummaryrefslogtreecommitdiffstats
path: root/admin/include/functions_plugins.inc.php
blob: 3e79988fdf16c6a21f29520db263e14377f4f898 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
// +-----------------------------------------------------------------------+
// | PhpWebGallery - a PHP based picture gallery                           |
// | Copyright (C) 2003-2007 PhpWebGallery Team - http://phpwebgallery.net |
// +-----------------------------------------------------------------------+
// | branch        : BSF (Best So Far)
// | file          : $Id$
// | last update   : $Date$
// | last modifier : $Author$
// | revision      : $Revision$
// +-----------------------------------------------------------------------+
// | This program is free software; you can redistribute it and/or modify  |
// | it under the terms of the GNU General Public License as published by  |
// | the Free Software Foundation                                          |
// |                                                                       |
// | This program is distributed in the hope that it will be useful, but   |
// | WITHOUT ANY WARRANTY; without even the implied warranty of            |
// | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU      |
// | General Public License for more details.                              |
// |                                                                       |
// | You should have received a copy of the GNU General Public License     |
// | along with this program; if not, write to the Free Software           |
// | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
// | USA.                                                                  |
// +-----------------------------------------------------------------------+

/* Returns an array of plugins defined in the plugin directory
*/
function get_fs_plugins()
{
  $plugins = array();

  $dir = opendir(PHPWG_PLUGINS_PATH);
  while ($file = readdir($dir))
  {
    if ($file!='.' and $file!='..')
    {
      $path = PHPWG_PLUGINS_PATH.$file;
      if (is_dir($path) and !is_link($path)
          and preg_match('/^[a-zA-Z0-9-_]+$/', $file )
          and file_exists($path.'/main.inc.php')
          )
      {
        $plugin = array(
            'name'=>$file,
            'version'=>'0',
            'uri'=>'',
            'description'=>'',
            'author'=>'',
          );
        $plg_data = implode( '', file($path.'/main.inc.php') );

        if ( preg_match("|Plugin Name: (.*)|", $plg_data, $val) )
        {
          $plugin['name'] = trim( $val[1] );
        }
        if (preg_match("|Version: (.*)|", $plg_data, $val))
        {
          $plugin['version'] = trim($val[1]);
        }
        if ( preg_match("|Plugin URI: (.*)|", $plg_data, $val) )
        {
          $plugin['uri'] = trim($val[1]);
        }
        if ( preg_match("|Description: (.*)|", $plg_data, $val) )
        {
          $plugin['description'] = trim($val[1]);
        }
        if ( preg_match("|Author: (.*)|", $plg_data, $val) )
        {
          $plugin['author'] = trim($val[1]);
        }
        if ( preg_match("|Author URI: (.*)|", $plg_data, $val) )
        {
          $plugin['author uri'] = trim($val[1]);
        }
        // IMPORTANT SECURITY !
        $plugin = array_map('htmlspecialchars', $plugin);
        $plugins[$file] = $plugin;
      }
    }
  }
  closedir($dir);
  return $plugins;
}

/**
 * Activates a plugin. It will be loaded only on the next page ...
 * @param string $plugin_id the plugin to activate
 * @param array $errors errors to be returned
 */
function activate_plugin($plugin_id, &$errors)
{
  // the plugin_id must exist in the DB as inactive
  $db_plugins = get_db_plugins('', $plugin_id);
  if (!empty($db_plugins))
  {
    $crt_db_plugin = $db_plugins[0];
    if ($crt_db_plugin['state'] != 'inactive')
    {
      array_push($errors, 'CANNOT ACTIVATE - INVALID CURRENT STATE ' . $crt_db_plugin['state']);
      return false;
    }
  }
  else
  {
    array_push($errors, 'CANNOT ACTIVATE - NOT INSTALLED');
    return false;
  }

  // now try to activate it
  $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
  if (file_exists($file_to_include))
  {
    include_once($file_to_include);
    if (function_exists('plugin_activate'))
    {
      plugin_activate($plugin_id, $crt_db_plugin['version'], $errors);
    }
  }
  if (empty($errors))
  {
    $query = '
UPDATE ' . PLUGINS_TABLE . ' SET state="active" WHERE id="' . $plugin_id . '"';
    pwg_query($query);
    return true;
  }
  return false;
}


/**
 * Deactivates a plugin. It will be unloaded only on the next page ...
 * @param string $plugin_id the plugin to activate
 * @param array $errors errors to be returned
 */
function deactivate_plugin($plugin_id, &$errors)
{
  // the plugin_id must exist in the DB as inactive
  $db_plugins = get_db_plugins('', $plugin_id);
  if (!empty($db_plugins))
  {
    $crt_db_plugin = $db_plugins[0];
    if ($crt_db_plugin['state'] != 'active')
    {
      array_push($errors, 'CANNOT DEACTIVATE - INVALID CURRENT STATE ' . $crt_db_plugin['state']);
      return false;
    }
  }
  else
  {
    array_push($errors, 'CANNOT DEACTIVATE - NOT INSTALLED');
    return false;
  }

  // now try to deactivate it
  $query = '
UPDATE ' . PLUGINS_TABLE . ' SET state="inactive" WHERE id="' . $plugin_id . '"';
  pwg_query($query);

  $file_to_include = PHPWG_PLUGINS_PATH . $plugin_id . '/maintain.inc.php';
  if (file_exists($file_to_include))
  {
    include_once($file_to_include);
    if (function_exists('plugin_deactivate'))
    {
      plugin_deactivate($plugin_id);
    }
  }
  return true;
}


/**
 * Retrieves an url for a plugin page.
 * @param string file - php script full name
 */
function get_admin_plugin_menu_link($file)
{
  global $page;
  $real_file = realpath($file);
  $url = get_root_url().'admin.php?page=plugin';
  if (false!==$real_file)
  {
    $real_plugin_path = rtrim(realpath(PHPWG_PLUGINS_PATH), '\\/');
    $file = substr($real_file, strlen($real_plugin_path)+1);
    $file = str_replace('\\', '/', $file);//Windows
    $url .= '&amp;section='.urlencode($file);
  }
  else if (isset($page['errors']))
  {
    array_push($page['errors'], 'PLUGIN ERROR: "'.$file.'" is not a valid file');
  }
  return $url;
}

/**
 * Sort plugins by status
 */
function sort_plugins_by_state($plugins, $db_plugins_by_id)
{
  $active_plugins = array();
  $inactive_plugins = array();
  $not_installed = array();

  foreach($plugins as $plugin_id => $plugin)
  {
    if (isset($db_plugins_by_id[$plugin_id]))
    {
      $db_plugins_by_id[$plugin_id]['state'] == 'active' ?
        $active_plugins[$plugin_id] = $plugin : $inactive_plugins[$plugin_id] = $plugin;
    }
    else
    {
      $not_installed[$plugin_id] = $plugin;
    }
  }
  return $active_plugins + $inactive_plugins + $not_installed;
}


/**
 * Retrieve PEM server datas
 * @param bool (true for retrieve new extensions)
 */
function check_server_plugins(& $fs_plugins, $newext=false)
{
  foreach($fs_plugins as $plugin_id => $fs_plugin)
  {
    if (!empty($fs_plugin['uri']) and strpos($fs_plugin['uri'] , 'extension_view.php?eid='))
    {
      list( , $extension) = explode('extension_view.php?eid=', $fs_plugin['uri']);
      if (!is_numeric($extension)) continue;
      $plugins_to_check[] = $extension;
      $fs_plugins[$plugin_id]['extension'] = $extension;
    }
  }

  $url = PEM_URL . '/uptodate.php?version=' . rawurlencode(PHPWG_VERSION) . '&extensions=' . implode(',', $plugins_to_check);
  $url .= $newext ? '&newext=Plugin' : '';

  if (!empty($plugins_to_check) and $source = @file_get_contents($url))
  {
    return @unserialize($source);
  }
  return false;
}


/**
 * Extract plugin files from archive
 * @param string - install or upgrade
 *  @param string - archive URL
  * @param string - destination path
 */
function extract_plugin_files($action, $source, $dest)
{
  if ($archive = tempnam( PHPWG_PLUGINS_PATH, 'zip'))
  {
    if (@copy(PEM_URL . str_replace(' ', '%20', $source), $archive))
    {
      $zip = new PclZip($archive);
      if ($list = $zip->listContent())
      {
        foreach ($list as $file)
        {
          // we search main.inc.php in archive
          if (basename($file['filename']) == 'main.inc.php'
            and (!isset($main_filepath)
            or strlen($file['filename']) < strlen($main_filepath)))
          {
            $main_filepath = $file['filename'];
          }
        }
        if (isset($main_filepath))
        {
          $root = dirname($main_filepath); // main.inc.php path in archive
          if ($action == 'upgrade')
          {
            $extract_path = PHPWG_PLUGINS_PATH.$dest;
          }
          else
          {
            $extract_path = PHPWG_PLUGINS_PATH
                . ($root == '.' ? 'extension_' . $dest : basename($root));
          }
          if($result = $zip->extract(PCLZIP_OPT_PATH, $extract_path,
                                     PCLZIP_OPT_REMOVE_PATH, $root,
                                     PCLZIP_OPT_REPLACE_NEWER))
          {
            foreach ($result as $file)
            {
              if ($file['stored_filename'] == $main_filepath)
              {
                $status = $file['status'];
                break;
              }
            }
          }
          else $status = 'extract_error';
        }
        else $status = 'archive_error';
      }
      else $status = 'archive_error';
    }
    else $status = 'dl_archive_error';
  }
  else $status = 'temp_path_error';

  @unlink($archive);
  return $status;
}


/**
 * delete $path directory
 * @param string - path
 */
function deltree($path)
{
  if (is_dir($path))
  {
    $fh = opendir($path);
    while ($file = readdir($fh))
    {
      if ($file != '.' and $file != '..')
      {
        $pathfile = $path . '/' . $file;
        if (is_dir($pathfile))
        {
          deltree($pathfile);
        }
        else
        {
          @unlink($pathfile);
        }
      }
    }
    closedir($fh);
    return @rmdir($path);
  }
}


/**
 * send $path to trash directory
  * @param string - path
 */
function send_to_trash($path)
{
  $trash_path = PHPWG_PLUGINS_PATH . 'trash';
  if (!is_dir($trash_path))
  {
    @mkdir($trash_path);
    $file = @fopen($trash_path . '/.htaccess', 'w');
    @fwrite($file, 'deny from all');
    @fclose($file);
  }
  while ($r = $trash_path . '/' . md5(uniqid(rand(), true)))
  {
    if (!is_dir($r))
    {
      @rename($path, $r);
      break;
    }
  }
}


/**
 * Sort functions
 */
function extension_name_compare($a, $b)
{
  return strcmp(strtolower($a['ext_name']), strtolower($b['ext_name']));
}

function extension_author_compare($a, $b)
{
  $r = strcmp(strtolower($a['author']), strtolower($b['author']));
  if ($r == 0) return extension_name_compare($a, $b);
  else return $r;
}

function plugin_author_compare($a, $b)
{
  return strcasecmp( $a['author'], $b['author']);
}

function plugin_version_compare($a, $b)
{
  return version_compare( $a['version'], $b['version']);
}

?>