1<?php
2/**
3 * Coppermine Photo Gallery
4 *
5 * v1.0 originally written by Gregory Demar
6 *
7 * @copyright  Copyright (c) 2003-2019 Coppermine Dev Team
8 * @license    GNU General Public License version 3 or later; see LICENSE
9 *
10 * include/tool.class.php
11 * @since  1.6.07
12 */
13if (!defined('IN_COPPERMINE')) die('Not in Coppermine...');
14
15abstract class AdminTool extends CPG_Object
16{
17	public $progressive = false;
18
19	protected $scp,			// superCage->post object
20		$albumid,			// albumid # extracted from post
21		$albstr,			// sql album WHERE clause for use in query
22		$procnum = 10,		// number of items to process per pass (progressive)
23		$skpcnt = 0,		// count of skipped items
24		$wrncnt = 0,		// count of warnings
25		$errcnt = 0;		// count of errors
26
27	public function __construct (Inspekt_Cage $scp, $lang)
28	{
29		global $lang_util_php;	// for now we'll merge/use the core language
30
31		parent::__construct();
32		$this->scp = $scp;
33		$this->albumid = $scp->keyExists('albumid') ? $scp->getInt('albumid') : 0;
34		$this->albstr = $this->albumid ? " WHERE aid={$this->albumid}" : '';
35		// attempt to load language files
36		if (!empty($this->toolname)) {
37			$tn = $this->toolname;
38			if (is_dir('tools/'.$tn.'/lang')) {
39				include 'tools/'.$tn.'/lang/english.php';
40				if ($this->config['lang'] != 'english') {
41					include 'tools/'.$tn.'/lang/'.$this->config['lang'].'.php';
42				}
43			}
44			$this->lang = array_merge($this->lang, $lang_util_php, $lang);	// for now we'll merge in the core language
45		}
46	}
47
48	public static function getTool ($tool)
49	{
50		global $superCage;
51		if (!$tool) return null;
52		$tclass = 'CPG_Tool'.ucfirst($tool);
53		$lang = array();
54		include_once 'tools/'.$tool.'/'.$tool.'.php';
55		// Create the tool object with the SC post object and any $lang strings that were in the file
56		if (class_exists($tclass)) {
57			$tobj = new $tclass($superCage->post, $lang);
58			return $tobj;
59		} else return null;
60	}
61
62	// Process the tool
63	// return next index along with warning and error counts
64	public function _process ()
65	{
66		$this->skpcnt = $this->scp->getInt('skps');
67		$this->wrncnt = $this->scp->getInt('wrns');
68		$this->errcnt = $this->scp->getInt('errs');
69		$nxi = $this->process();
70		return array('nxi'=>$nxi, 'skps'=>$this->skpcnt?:0, 'wrns'=>$this->wrncnt?:0, 'errs'=>$this->errcnt?:0);
71	}
72
73	// Method to display tool completion message with status
74	protected function done ($msg, $stat='success')
75	{
76		if ($this->skpcnt) {
77			$stat = 'info';
78			$msg .= '<br>'.$this->skpcnt.' '.$this->_('skipped');
79		}
80		if ($this->wrncnt) {
81			$stat = 'warning';
82			$msg .= '<br>'.$this->wrncnt.' '.$this->_('warnings');
83		}
84		if ($this->errcnt) {
85			$stat = 'error';
86			$msg .= '<br>'.$this->errcnt.' '.$this->_('errors');
87		}
88		msg_box('', $msg, '', '', $stat);
89	}
90
91
92	//==== PLACEHOLDERS FOR OVERRIDDEN METHODS ====
93
94	// Return a description of the tool
95	public function describe ()
96	{
97		return '';
98	}
99
100	// Return the UI elements
101	public function displayUI ()
102	{
103		return '';
104	}
105
106	// Perform the (non-progressive) action
107	public function action ()
108	{
109		return false;
110	}
111
112	// For progressive tools - return the total number of actions to be taken
113	public function total ()
114	{
115		return 0;
116	}
117
118	// For progressive tools - process a number of actions
119	// return the index number of the next item to be processed (-1 if none)
120	public function process ()
121	{
122		return -1;
123	}
124
125}
126
127abstract class CPG_Object
128{
129	protected
130		$config,	// reference to global $CONFIG
131		$lang;		// language strings; merged into by subclasses
132
133	public function __construct ()
134	{
135		global $CONFIG, $lang_common;
136		$this->config =& $CONFIG;
137		$this->lang = $lang_common;
138		register_shutdown_function(array($this, 'check_for_fatal'));
139	}
140
141	public function check_for_fatal ()
142	{
143		if ($error = error_get_last()) {
144			if ($error['type'] == E_ERROR) {
145				cpg_die(CRITICAL_ERROR, $error['message'], $error['file'], $error['line']);
146			} else {
147				cpg_die(ERROR, $error['message'], $error['file'], $error['line']);
148			}
149		}
150	}
151
152	// Method to get language strings
153	protected function _ ($str, $ix=null)
154	{
155		if ($ix && isset($this->lang[$str]) && is_array($this->lang[$str])) {
156			return isset($this->lang[$str][$ix]) ? $this->lang[$str][$ix] : ('UNDEFINED:'.$str.'.'.$ix);
157		} else {
158			return isset($this->lang[$str]) ? $this->lang[$str] : ('UNDEFINED:'.$str);
159		}
160	}
161
162}
163
164