"Fossies" - the Fresh Open Source Software Archive

Member "fogproject-1.5.9/packages/web/lib/router/route.class.php" (13 Sep 2020, 49949 Bytes) of package /linux/misc/fogproject-1.5.9.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the latest Fossies "Diffs" side-by-side code changes report for "route.class.php": 1.5.8_vs_1.5.9.

    1 <?php
    2 /**
    3  * Creates our routes for api configuration.
    4  *
    5  * PHP Version 5
    6  *
    7  * @category Route
    8  * @package  FOGProject
    9  * @author   Tom Elliott <tommygunsster@gmail.com>
   10  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   11  * @link     https://fogproject.org/
   12  */
   13 /**
   14  * Creates our routes for api configuration.
   15  *
   16  * @category Route
   17  * @package  FOGProject
   18  * @author   Tom Elliott <tommygunsster@gmail.com>
   19  * @license  http://opensource.org/licenses/gpl-3.0 GPLv3
   20  * @link     https://fogproject.org/
   21  */
   22 class Route extends FOGBase
   23 {
   24     /**
   25      * The api setup is enabled?
   26      *
   27      * @var bool
   28      */
   29     private static $_enabled = false;
   30     /**
   31      * The currently defined token.
   32      *
   33      * @var string
   34      */
   35     private static $_token = '';
   36     /**
   37      * AltoRouter object container.
   38      *
   39      * @var AltoRouter
   40      */
   41     public static $router = null;
   42     /**
   43      * Matches from AltoRouter.
   44      *
   45      * @var array
   46      */
   47     public static $matches = array();
   48     /**
   49      * Stores the data to print.
   50      *
   51      * @var mixed
   52      */
   53     public static $data;
   54     /**
   55      * Stores the valid classes.
   56      *
   57      * @var array
   58      */
   59     public static $validClasses = array(
   60         'clientupdater',
   61         'dircleaner',
   62         'greenfog',
   63         'group',
   64         'groupassociation',
   65         'history',
   66         'hookevent',
   67         'host',
   68         'hostautologout',
   69         'hostscreensetting',
   70         'image',
   71         'imageassociation',
   72         'imagepartitiontype',
   73         'imagetype',
   74         'imaginglog',
   75         'inventory',
   76         'ipxe',
   77         'keysequence',
   78         'macaddressassociation',
   79         'module',
   80         'moduleassociation',
   81         'multicastsession',
   82         'multicastsessionassociation',
   83         'nodefailure',
   84         'notifyevent',
   85         'os',
   86         'oui',
   87         'plugin',
   88         'powermanagement',
   89         'printer',
   90         'printerassociation',
   91         'pxemenuoptions',
   92         'scheduledtask',
   93         'service',
   94         'snapin',
   95         'snapinassociation',
   96         'snapingroupassociation',
   97         'snapinjob',
   98         'snapintask',
   99         'storagegroup',
  100         'storagenode',
  101         'task',
  102         'tasklog',
  103         'taskstate',
  104         'tasktype',
  105         //'user',
  106         'usercleanup',
  107         'usertracking',
  108         'virus'
  109     );
  110     /**
  111      * Valid Tasking classes.
  112      *
  113      * @var array
  114      */
  115     public static $validTaskingClasses = array(
  116         'group',
  117         'host',
  118         'multicastsession',
  119         'scheduledtask',
  120         'snapinjob',
  121         'snapintask',
  122         'task'
  123     );
  124     /**
  125      * Valid active tasking classes.
  126      *
  127      * @var array
  128      */
  129     public static $validActiveTasks = array(
  130         'multicastsession',
  131         'scheduledtask',
  132         'snapinjob',
  133         'snapintask',
  134         'task'
  135     );
  136     /**
  137      * Initialize element.
  138      *
  139      * @return void
  140      */
  141     public function __construct()
  142     {
  143         list(
  144             self::$_enabled,
  145             self::$_token
  146         ) = self::getSubObjectIDs(
  147             'Service',
  148             array(
  149                 'name' => array(
  150                     'FOG_API_ENABLED',
  151                     'FOG_API_TOKEN'
  152                 )
  153             ),
  154             'value'
  155         );
  156         /**
  157          * If API is not enabled redirect to home page.
  158          */
  159         if (!self::$_enabled) {
  160             header(
  161                 sprintf(
  162                     'Location: %s://%s/fog/management/index.php',
  163                     self::$httpproto,
  164                     self::$httphost
  165                 )
  166             );
  167             exit;
  168         }
  169         if (!self::$FOGUser->isValid()) {
  170             /**
  171              * Test our token.
  172              */
  173             self::_testToken();
  174             /**
  175              * Test our authentication.
  176              */
  177             self::_testAuth();
  178         }
  179         /**
  180          * Ensure api has unlimited time.
  181          */
  182         ignore_user_abort(true);
  183         session_write_close();
  184         set_time_limit(0);
  185         /**
  186          * Define the event so plugins/hooks can modify what/when/where.
  187          */
  188         self::$HookManager
  189             ->processEvent(
  190                 'API_VALID_CLASSES',
  191                 array(
  192                     'validClasses' => &self::$validClasses
  193                 )
  194             );
  195         self::$HookManager
  196             ->processEvent(
  197                 'API_TASKING_CLASSES',
  198                 array(
  199                     'validTaskingClasses' => &self::$validTaskingClasses
  200                 )
  201             );
  202         self::$HookManager
  203             ->processEvent(
  204                 'API_ACTIVE_TASK_CLASSES',
  205                 array(
  206                     'validActiveTasks' => &self::$validActiveTasks
  207                 )
  208             );
  209         /**
  210          * If the router is already defined,
  211          * don't re-instantiate it.
  212          */
  213         if (self::$router) {
  214             return;
  215         }
  216         self::$router = new AltoRouter(
  217             array(),
  218             '/fog'
  219         );
  220         self::defineRoutes();
  221         self::setMatches();
  222         self::runMatches();
  223         self::printer(self::$data);
  224     }
  225     /**
  226      * Defines our standard routes.
  227      *
  228      * @return void
  229      */
  230     protected static function defineRoutes()
  231     {
  232         $expanded = sprintf(
  233             '/[%s:class]',
  234             implode('|', self::$validClasses)
  235         );
  236         $expandedt = sprintf(
  237             '/[%s:class]',
  238             implode('|', self::$validTaskingClasses)
  239         );
  240         $expandeda = sprintf(
  241             '/[%s:class]',
  242             implode('|', self::$validActiveTasks)
  243         );
  244         self::$router
  245             ->map(
  246                 'HEAD|GET',
  247                 '/system/[status|info]',
  248                 array(__CLASS__, 'status'),
  249                 'status'
  250             )
  251             ->get(
  252                 "${expandeda}/[current|active]",
  253                 array(__CLASS__, 'active'),
  254                 'active'
  255             )
  256             ->get(
  257                 "${expanded}/search/[*:item]",
  258                 array(__CLASS__, 'search'),
  259                 'search'
  260             )
  261             ->get(
  262                 "${expanded}/[list|all]?",
  263                 array(__CLASS__, 'listem'),
  264                 'list'
  265             )
  266             ->get(
  267                 "${expanded}/[details]/?[*:item]?",
  268                 array(__CLASS__, 'listdetails'),
  269                 'listdetails'
  270             )
  271             ->get(
  272                 "${expanded}/[i:id]/?[*:item]?",
  273                 array(__CLASS__, 'indiv'),
  274                 'indiv'
  275             )
  276             ->get(
  277                 "${expanded}/names/[*:whereItems]?",
  278                 array(__CLASS__, 'names'),
  279                 'names'
  280             )
  281             ->get(
  282                 "${expanded}/ids/[*:whereItems]?/[*:getField]?",
  283                 array(__CLASS__, 'ids'),
  284                 'ids'
  285             )
  286             ->put(
  287                 "${expanded}/[i:id]/[update|edit]?",
  288                 array(__CLASS__, 'edit'),
  289                 'update'
  290             )
  291             ->post(
  292                 "${expandedt}/[i:id]/[task]",
  293                 array(__CLASS__, 'task'),
  294                 'task'
  295             )
  296             ->post(
  297                 "${expanded}/[create|new]?",
  298                 array(__CLASS__, 'create'),
  299                 'create'
  300             )
  301             ->delete(
  302                 "${expandedt}/[i:id]?/[cancel]",
  303                 array(__CLASS__, 'cancel'),
  304                 'cancel'
  305             )
  306             ->delete(
  307                 "${expanded}/[i:id]/[delete|remove]?",
  308                 array(__CLASS__, 'delete'),
  309                 'delete'
  310             );
  311     }
  312     /**
  313      * Sets the matches variable
  314      *
  315      * @return void
  316      */
  317     public static function setMatches()
  318     {
  319         self::$matches = self::$router->match();
  320     }
  321     /**
  322      * Gets the matches.
  323      *
  324      * @return array
  325      */
  326     public static function getMatches()
  327     {
  328         return self::$matches;
  329     }
  330     /**
  331      * Runs the matches.
  332      *
  333      * @return void
  334      */
  335     public static function runMatches()
  336     {
  337         if (self::$matches
  338             && is_callable(self::$matches['target'])
  339         ) {
  340             call_user_func_array(
  341                 self::$matches['target'],
  342                 self::$matches['params']
  343             );
  344             return;
  345         }
  346         self::sendResponse(
  347             HTTPResponseCodes::HTTP_NOT_IMPLEMENTED
  348         );
  349     }
  350     /**
  351      * Test token information.
  352      *
  353      * @return void
  354      */
  355     private static function _testToken()
  356     {
  357         $passtoken = base64_decode(
  358             filter_input(INPUT_SERVER, 'HTTP_FOG_API_TOKEN')
  359         );
  360         if ($passtoken !== self::$_token) {
  361             self::sendResponse(
  362                 HTTPResponseCodes::HTTP_FORBIDDEN
  363             );
  364         }
  365     }
  366     /**
  367      * Test authentication.
  368      *
  369      * @return void
  370      */
  371     private static function _testAuth()
  372     {
  373         $usertoken = base64_decode(
  374             filter_input(INPUT_SERVER, 'HTTP_FOG_USER_TOKEN')
  375         );
  376         $pwtoken = self::getClass('User')
  377             ->set('token', $usertoken)
  378             ->load('token');
  379         if ($pwtoken->isValid() && $pwtoken->get('api')) {
  380             return;
  381         }
  382         $auth = self::$FOGUser->passwordValidate(
  383             $_SERVER['PHP_AUTH_USER'],
  384             $_SERVER['PHP_AUTH_PW']
  385         );
  386         if (!$auth) {
  387             self::sendResponse(
  388                 HTTPResponseCodes::HTTP_UNAUTHORIZED
  389             );
  390         }
  391     }
  392     /**
  393      * Sends the response code through break head as needed.
  394      *
  395      * @param int $code The code to break head on.
  396      * @param int $msg  The message to send.
  397      *
  398      * @return void
  399      */
  400     public static function sendResponse($code, $msg = false)
  401     {
  402         HTTPResponseCodes::breakHead(
  403             $code,
  404             $msg
  405         );
  406     }
  407     /**
  408      * Presents status to show up or down state.
  409      *
  410      * @return void
  411      */
  412     public static function status()
  413     {
  414         self::sendResponse(
  415             HTTPResponseCodes::HTTP_SUCCESS,
  416             "success\n"
  417         );
  418     }
  419     /**
  420      * Presents the equivalent of a page's list all.
  421      *
  422      * @param string $class  The class to work with.
  423      * @param string $sortby How to sort the data.
  424      * @param bool   $bypass Allow showing hidden data.
  425      * @param array  $find   Additional filter items.
  426      *
  427      * @return void
  428      */
  429     public static function listem(
  430         $class,
  431         $sortby = 'name',
  432         $bypass = false,
  433         $find = array(),
  434         $item = ''
  435     ) {
  436         $classname = strtolower($class);
  437         $classman = self::getClass($class)->getManager();
  438         self::$data = array();
  439         self::$data['count'] = 0;
  440         self::$data[$classname.'s'] = array();
  441         $find = self::fastmerge(
  442             $find,
  443             self::getsearchbody($classname)
  444         );
  445         switch ($classname) {
  446         case 'plugin':
  447             self::$data['count_active'] = 0;
  448             self::$data['count_installed'] = 0;
  449             self::$data['count_not_active'] = 0;
  450             foreach (self::getClass('Plugin')->getPlugins() as $class) {
  451                 self::$data[$classname.'s'][] = self::getter(
  452                     $classname,
  453                     $class,
  454                     $item
  455                 );
  456                 if ($class->isActive() && !$class->isInstalled()) {
  457                     self::$data['count_active']++;
  458                 }
  459                 if ($class->isActive() && $class->isInstalled()) {
  460                     self::$data['count_installed']++;
  461                 }
  462                 if (!$class->isActive() && !$class->isInstalled()) {
  463                     self::$data['count_not_active']++;
  464                 }
  465                 self::$data['count']++;
  466                 unset($class);
  467             }
  468             break;
  469         default:
  470             foreach ((array)$classman->find($find, 'AND', $sortby) as &$class) {
  471                 $test = stripos(
  472                     $class->get('name'),
  473                     '_api_'
  474                 );
  475                 if (!$bypass && false != $test) {
  476                     continue;
  477                 }
  478                 self::$data[$classname.'s'][] = self::getter(
  479                     $classname,
  480                     $class,
  481                     $item
  482                 );
  483                 self::$data['count']++;
  484                 unset($class);
  485             }
  486             break;
  487         }
  488         self::$HookManager
  489             ->processEvent(
  490                 'API_MASSDATA_MAPPING',
  491                 array(
  492                     'data' => &self::$data,
  493                     'classname' => &$classname,
  494                     'classman' => &$classman
  495                 )
  496             );
  497     }
  498     /**
  499      * Presents the equivalent of a detailed page list.
  500      *
  501      * @param string $class  The class to work with.
  502      * @param string $sortby How to sort the data.
  503      * @param bool   $bypass Allow showing hidden data.
  504      * @param array  $find   Additional filter items.
  505      *
  506      * @return void
  507      */
  508     public static function listdetails(
  509         $class,
  510         $item,
  511         $sortby = 'name',
  512         $bypass = false,
  513         $find = array()
  514     ) {
  515         $item = empty($item) ? 'all' : $item;
  516         self::listem($class, $sortby, $bypass, $find, $item);
  517     }
  518     /**
  519      * Presents the equivalent of a page's search.
  520      *
  521      * @param string $class The class to work with.
  522      * @param string $item  The "search".
  523      *
  524      * @return void
  525      */
  526     public static function search($class, $item)
  527     {
  528         $classname = strtolower($class);
  529         $classman = self::getClass($class)->getManager();
  530         self::$data = array();
  531         self::$data['count'] = 0;
  532         self::$data[$classname.'s'] = array();
  533         foreach ($classman->search($item, true) as &$class) {
  534             if (false != stripos($class->get('name'), '_api_')) {
  535                 continue;
  536             }
  537             self::$data[$classname.'s'][] = self::getter(
  538                 $classname,
  539                 $class
  540             );
  541             self::$data['count']++;
  542             unset($class);
  543         }
  544         self::$HookManager
  545             ->processEvent(
  546                 'API_MASSDATA_MAPPING',
  547                 array(
  548                     'data' => &self::$data,
  549                     'classname' => &$classname,
  550                     'classman' => &$classman
  551                 )
  552             );
  553     }
  554     /**
  555      * Displays the individual item.
  556      *
  557      * @param string $class The class to work with.
  558      * @param int    $id    The id of the item.
  559      *
  560      * @return void
  561      */
  562     public static function indiv($class, $id, $item = '')
  563     {
  564         $classname = strtolower($class);
  565         $class = new $class($id);
  566         if (!$class->isValid()) {
  567             self::sendResponse(
  568                 HTTPResponseCodes::HTTP_NOT_FOUND
  569             );
  570         }
  571         self::$data = array();
  572         self::$data = self::getter(
  573             $classname,
  574             $class,
  575             $item
  576         );
  577         self::$HookManager
  578             ->processEvent(
  579                 'API_INDIVDATA_MAPPING',
  580                 array(
  581                     'data' => &self::$data,
  582                     'classname' => &$classname,
  583                     'class' => &$class
  584                 )
  585             );
  586     }
  587     /**
  588      * Enables editing/updating a specified object.
  589      *
  590      * @param string $class The class to work with.
  591      * @param int    $id    The id of the item.
  592      *
  593      * @return void
  594      */
  595     public static function edit($class, $id)
  596     {
  597         $classname = strtolower($class);
  598         $classVars = self::getClass(
  599             $class,
  600             '',
  601             true
  602         );
  603         $class = new $class($id);
  604         if (!$class->isValid()) {
  605             self::sendResponse(
  606                 HTTPResponseCodes::HTTP_NOT_FOUND
  607             );
  608         }
  609         $vars = json_decode(
  610             file_get_contents('php://input')
  611         );
  612         $exists = self::getClass($classname)
  613             ->getManager()
  614             ->exists($vars->name);
  615         if (strtolower($class->get('name')) != $vars->name
  616             && $exists
  617         ) {
  618             self::setErrorMessage(
  619                 _('Already created'),
  620                 HTTPResponseCodes::HTTP_INTERNAL_SERVER_ERROR
  621             );
  622         }
  623         foreach ($classVars['databaseFields'] as &$key) {
  624             $key = $class->key($key);
  625             if (!isset($vars->$key)) {
  626                 $val = $class->get($key);
  627             } else {
  628                 $val = $vars->$key;
  629             }
  630             if ($key == 'id') {
  631                 continue;
  632             }
  633             $class->set($key, $val);
  634             unset($key);
  635         }
  636         switch ($classname) {
  637         case 'host':
  638             if (isset($vars->macs)) {
  639                 $macsToAdd = array_diff(
  640                     (array)$vars->macs,
  641                     $class->getMyMacs()
  642                 );
  643                 $primac = array_shift($macsToAdd);
  644                 $macsToRem = array_diff(
  645                     $class->getMyMacs(),
  646                     (array)$vars->macs
  647                 );
  648                 $class
  649                     ->removeAddMAC($macsToRem)
  650                     ->addPriMAC($primac)
  651                     ->addAddMAC($macsToAdd);
  652             }
  653             if (isset($vars->snapins)) {
  654                 $snapinsToAdd = array_diff(
  655                     (array)$vars->snapins,
  656                     $class->get('snapins')
  657                 );
  658                 $snapinsToRem = array_diff(
  659                     $class->get('snapins'),
  660                     (array)$vars->snapins
  661                 );
  662                 $class
  663                     ->removeSnapin($snapinsToRem)
  664                     ->addSnapin($snapinsToAdd);
  665             }
  666             if (isset($vars->printers)) {
  667                 $printersToAdd = array_diff(
  668                     (array)$vars->printers,
  669                     $class->get('printers')
  670                 );
  671                 $printersToRem = array_diff(
  672                     $class->get('printers'),
  673                     (array)$vars->printers
  674                 );
  675                 $class
  676                     ->removePrinter($printersToRem)
  677                     ->addPrinter($printersToAdd);
  678             }
  679             if (isset($vars->modules)) {
  680                 $modulesToAdd = array_diff(
  681                     (array)$vars->modules,
  682                     $class->get('modules')
  683                 );
  684                 $modulesToRem = array_diff(
  685                     $class->get('modules'),
  686                     (array)$vars->modules
  687                 );
  688                 $class
  689                     ->removeModule($modulesToAdd)
  690                     ->addModule($modulesToRem);
  691             }
  692             if (isset($vars->groups)) {
  693                 $groupsToAdd = array_diff(
  694                     (array)$vars->groups,
  695                     $class->get('groups')
  696                 );
  697                 $groupsToRem = array_diff(
  698                     $class->get('groups'),
  699                     (array)$vars->groups
  700                 );
  701                 $class
  702                     ->removeGroup($groupsToRem)
  703                     ->addGroup($groupsToAdd);
  704             }
  705             break;
  706         case 'group':
  707             if (isset($vars->snapins)) {
  708                 Route::ids('snapin');
  709                 $snapins = json_decode(
  710                     Route::getData(),
  711                     true
  712                 );
  713                 $snapinsToRem = array_diff(
  714                     $snapins,
  715                     (array)$vars->snapins
  716                 );
  717                 $class
  718                     ->removeSnapin($snapinsToRem)
  719                     ->addSnapin($vars->snapins);
  720             }
  721             if (isset($vars->printers)) {
  722                 Route::ids('printer');
  723                 $printers = json_decode(
  724                     Route::getData(),
  725                     true
  726                 );
  727                 $printersToRem = array_diff(
  728                     $printers,
  729                     (array)$vars->printers
  730                 );
  731                 $class
  732                     ->removePrinter($printersToRem)
  733                     ->addPrinter($vars->printers);
  734             }
  735             if (isset($vars->modules)) {
  736                 Route::ids('module');
  737                 $modules = json_decode(
  738                     Route::getData(),
  739                     true
  740                 );
  741                 $modulesToRem = array_diff(
  742                     $modules,
  743                     (array)$vars->modules
  744                 );
  745                 $class
  746                     ->removeModule($modulesToRem)
  747                     ->addPrinter($vars->modules);
  748             }
  749             if (isset($vars->hosts)) {
  750                 $hostsToAdd = array_diff(
  751                     (array)$vars->hosts,
  752                     $class->get('hosts')
  753                 );
  754                 $hostsToRem = array_diff(
  755                     $class->get('hosts'),
  756                     (array)$vars->hosts
  757                 );
  758                 $class
  759                     ->removeHost($hostsToRem)
  760                     ->addHost($hostsToAdd);
  761             }
  762             if ($vars->imageID) {
  763                 $class
  764                     ->addImage($vars->imageID);
  765             }
  766             break;
  767         case 'image':
  768         case 'snapin':
  769             if (isset($vars->hosts)) {
  770                 $hostsToAdd = array_diff(
  771                     (array)$vars->hosts,
  772                     $class->get('hosts')
  773                 );
  774                 $hostsToRem = array_diff(
  775                     $class->get('hosts'),
  776                     (array)$vars->hosts
  777                 );
  778                 $class
  779                     ->removeHost($hostsToRem)
  780                     ->addHost($hostsToAdd);
  781             }
  782             if (isset($vars->storagegroups)) {
  783                 $storageGroupsToAdd = array_diff(
  784                     (array)$vars->storagegroups,
  785                     $class->get('storagegroups')
  786                 );
  787                 $storageGroupsToRem = array_diff(
  788                     $class->get('storagegroups'),
  789                     (array)$vars->storagegroups
  790                 );
  791                 $class
  792                     ->removeGroup($storageGroupsToRem)
  793                     ->addgroup($storageGroupsToAdd);
  794             }
  795             break;
  796         case 'printer':
  797             if (isset($vars->hosts)) {
  798                 $hostsToAdd = array_diff(
  799                     (array)$vars->hosts,
  800                     $class->get('hosts')
  801                 );
  802                 $hostsToRem = array_diff(
  803                     $class->get('hosts'),
  804                     (array)$vars->hosts
  805                 );
  806                 $class
  807                     ->removeHost($hostsToRem)
  808                     ->addHost($hostsToAdd);
  809             }
  810             break;
  811         }
  812         // Store the data and recreate.
  813         // If failed present so.
  814         if ($class->save()) {
  815             $class = new $class($id);
  816         } else {
  817             self::sendResponse(
  818                 HTTPResponseCodes::HTTP_INTERNAL_SERVER_ERROR
  819             );
  820         }
  821         self::indiv($classname, $id);
  822     }
  823     /**
  824      * Generates our task element.
  825      *
  826      * @param string $class The class to work with.
  827      * @param int    $id    The id of the item.
  828      *
  829      * @return void
  830      */
  831     public static function task($class, $id)
  832     {
  833         $classname = strtolower($class);
  834         $class = new $class($id);
  835         if (!$class->isValid()) {
  836             self::sendResponse(
  837                 HTTPResponseCodes::HTTP_NOT_FOUND
  838             );
  839         }
  840         $tids = self::getSubObjectIDs('TaskType');
  841         $task = json_decode(
  842             file_get_contents('php://input')
  843         );
  844         $TaskType = new TaskType($task->taskTypeID);
  845         if (!$TaskType->isValid()) {
  846             $message = _('Invalid tasking type passed');
  847             self::setErrorMessage(
  848                 $message,
  849                 HTTPResponseCodes::HTTP_NOT_IMPLEMENTED
  850             );
  851         }
  852         try {
  853             $class->createImagePackage(
  854                 $task->taskTypeID,
  855                 $task->taskName,
  856                 $task->shutdown,
  857                 $task->debug,
  858                 (
  859                     $task->deploySnapins === true ?
  860                     -1 :
  861                     (
  862                         (is_numeric($task->deploySnapins)
  863                         && $task->deploySnapins > 0)
  864                         || $task->deploySnapins == -1 ?
  865                         $task->deploySnapins :
  866                         false
  867                     )
  868                 ),
  869                 $class instanceof Group,
  870                 $_SERVER['PHP_AUTH_USER'],
  871                 $task->passreset,
  872                 $task->sessionjoin,
  873                 $task->wol
  874             );
  875         } catch (\Exception $e) {
  876             self::setErrorMessage(
  877                 $e->getMessage(),
  878                 HTTPResponseCodes::HTTP_INTERNAL_SERVER_ERROR
  879             );
  880         }
  881     }
  882     /**
  883      * Creates an item.
  884      *
  885      * @param string $class The class to work with.
  886      *
  887      * @return void
  888      */
  889     public static function create($class)
  890     {
  891         $classname = strtolower($class);
  892         $classVars = self::getClass(
  893             $class,
  894             '',
  895             true
  896         );
  897         $class = new $class;
  898         $vars = json_decode(
  899             file_get_contents(
  900                 'php://input'
  901             )
  902         );
  903         $exists = self::getClass($classname)
  904             ->getManager()
  905             ->exists($vars->name);
  906         if ($exists) {
  907             self::setErrorMessage(
  908                 _('Already created'),
  909                 HTTPResponseCodes::HTTP_INTERNAL_SERVER_ERROR
  910             );
  911         }
  912         foreach ($classVars['databaseFields'] as &$key) {
  913             $key = $class->key($key);
  914             $val = $vars->$key;
  915             if ($key == 'id'
  916                 || !$val
  917             ) {
  918                 continue;
  919             }
  920             $class->set($key, $val);
  921             unset($key);
  922         }
  923         switch ($classname) {
  924         case 'host':
  925             if (count($vars->macs)) {
  926                 $class
  927                     ->removeAddMAC($vars->macs)
  928                     ->addPriMAC(array_shift($vars->macs))
  929                     ->addAddMAC($vars->macs);
  930             }
  931             if (isset($vars->snapins)) {
  932                 $class
  933                     ->addSnapin($vars->snapins);
  934             }
  935             if (isset($vars->printers)) {
  936                 $class
  937                     ->addPrinter($vars->printers);
  938             }
  939             if (isset($vars->modules)) {
  940                 $class
  941                     ->addModule($vars->modules);
  942             }
  943             if (isset($vars->groups)) {
  944                 $class
  945                     ->addGroup($vars->groups);
  946             }
  947             break;
  948         case 'group':
  949             if (isset($vars->snapins)) {
  950                 $class
  951                     ->addSnapin($vars->snapins);
  952             }
  953             if (isset($vars->printers)) {
  954                 $class
  955                     ->addPrinter($vars->printers);
  956             }
  957             if (isset($vars->modules)) {
  958                 $class
  959                     ->addModule($vars->modules);
  960             }
  961             if (isset($vars->hosts)) {
  962                 $class
  963                     ->addHost($vars->hosts);
  964                 if (isset($vars->imageID)) {
  965                     $class
  966                         ->addImage($vars->imageID);
  967                 }
  968             }
  969             break;
  970         case 'image':
  971         case 'snapin':
  972             if (isset($vars->hosts)) {
  973                 $class
  974                     ->addHost($vars->hosts);
  975             }
  976             if (isset($vars->storagegroups)) {
  977                 $class
  978                     ->addGroup($vars->storagegroups);
  979             }
  980             break;
  981         case 'printer':
  982             if (isset($vars->hosts)) {
  983                 $class
  984                     ->addHost($vars->hosts);
  985             }
  986             break;
  987         }
  988         foreach ($classVars['databaseFieldsRequired'] as &$key) {
  989             $key = $class->key($key);
  990             $val = $class->get($key);
  991             if (!is_numeric($val) && !$val) {
  992                 self::setErrorMessage(
  993                     self::$foglang['RequiredDB'],
  994                     HTTPResponseCodes::HTTP_EXPECTATION_FAILED
  995                 );
  996             }
  997         }
  998         // Store the data and recreate.
  999         // If failed present so.
 1000         if ($class->save()) {
 1001             $id = $class->get('id');
 1002             $class = new $class($id);
 1003         } else {
 1004             self::sendResponse(
 1005                 HTTPResponseCodes::HTTP_INTERNAL_SERVER_ERROR
 1006             );
 1007         }
 1008         self::indiv($classname, $id);
 1009     }
 1010     /**
 1011      * Cancels a task element.
 1012      *
 1013      * @param string $class The class to work with.
 1014      * @param int    $id    The id of the item.
 1015      *
 1016      * @return void
 1017      */
 1018     public static function cancel($class, $id)
 1019     {
 1020         $classname = strtolower($class);
 1021         $class = new $class($id);
 1022         switch ($classname) {
 1023         case 'group':
 1024             if (!$class->isValid()) {
 1025                 self::sendResponse(
 1026                     HTTPResponseCodes::HTTP_NOT_FOUND
 1027                 );
 1028             }
 1029             foreach (self::getClass('HostManager')
 1030                 ->find(array('id' => $class->get('hosts'))) as &$Host
 1031             ) {
 1032                 if ($Host->get('task') instanceof Task) {
 1033                     $Host->get('task')->cancel();
 1034                 }
 1035                 unset($Host);
 1036             }
 1037             break;
 1038         case 'host':
 1039             if (!$class->isValid()) {
 1040                 self::sendResponse(
 1041                     HTTPResponseCodes::HTTP_NOT_FOUND
 1042                 );
 1043             }
 1044             if ($class->get('task') instanceof Task) {
 1045                 $class->get('task')->cancel();
 1046             }
 1047             break;
 1048         default:
 1049             $states = self::fastmerge(
 1050                 (array)self::getQueuedStates(),
 1051                 (array)self::getProgressState()
 1052             );
 1053             if (!$class->isValid()) {
 1054                 $classman = $class->getManager();
 1055                 $find = self::getsearchbody($classname, $class);
 1056                 $find['stateID'] = $states;
 1057                 $ids = self::getSubObjectIDs(
 1058                     $classname,
 1059                     $find
 1060                 );
 1061                 $classman->cancel($ids);
 1062             } else {
 1063                 if (in_array($class->get('stateID'), $states)) {
 1064                     $class->cancel();
 1065                 }
 1066             }
 1067         }
 1068     }
 1069     /**
 1070      * Get's the json body and sets our vars.
 1071      *
 1072      * @param string $class The class to get vars for/from.
 1073      *
 1074      * @return array
 1075      */
 1076     public static function getsearchbody($class)
 1077     {
 1078         $classVars = self::getClass(
 1079             $class,
 1080             '',
 1081             true
 1082         );
 1083         $vars = json_decode(
 1084             file_get_contents('php://input')
 1085         );
 1086         $find = array();
 1087         $class = new $class;
 1088         foreach ($classVars['databaseFields'] as &$key) {
 1089             $key = $class->key($key);
 1090             if (isset($vars->$key)) {
 1091                 $find[$key] = $vars->$key;
 1092             }
 1093             unset($key);
 1094         }
 1095         return $find;
 1096     }
 1097     /**
 1098      * Get's current/active tasks.
 1099      *
 1100      * @param string $class The class to use.
 1101      *
 1102      * @return void
 1103      */
 1104     public static function active($class)
 1105     {
 1106         $classname = strtolower($class);
 1107         $classman = self::getClass($class)->getManager();
 1108         $find = self::getsearchbody($classname);
 1109         $states = self::fastmerge(
 1110             (array)self::getQueuedStates(),
 1111             (array)self::getProgressState()
 1112         );
 1113         switch ($classname) {
 1114         case 'scheduledtask':
 1115             $find['isActive'] = 1;
 1116             break;
 1117         case 'multicastsession':
 1118         case 'snapinjob':
 1119         case 'snapintask':
 1120         case 'task':
 1121             $find['stateID'] = $states;
 1122         }
 1123         self::$data = array();
 1124         self::$data['count'] = 0;
 1125         self::$data[$classname.'s'] = array();
 1126         foreach ((array)$classman->find($find) as &$class) {
 1127             self::$data[$classname.'s'][] = self::getter(
 1128                 $classname,
 1129                 $class
 1130             );
 1131             self::$data['count']++;
 1132             unset($class);
 1133         }
 1134     }
 1135     /**
 1136      * Deletes an element.
 1137      *
 1138      * @param string $class The class to work with.
 1139      * @param int    $id    The id of class to remove.
 1140      *
 1141      * @return void
 1142      */
 1143     public static function delete($class, $id)
 1144     {
 1145         $classname = strtolower($class);
 1146         $class = new $class($id);
 1147         if (!$class->isValid()) {
 1148             self::sendResponse(
 1149                 HTTPResponseCodes::HTTP_NOT_FOUND
 1150             );
 1151         }
 1152         $class->destroy();
 1153         self::$data = '';
 1154     }
 1155     /**
 1156      * Sets an error message.
 1157      *
 1158      * @param string   $message The error message to pass.
 1159      * @param bool|int $code    Send custom error code.
 1160      *
 1161      * @return void
 1162      */
 1163     public static function setErrorMessage($message, $code = false)
 1164     {
 1165         self::$data['error'] = $message;
 1166         self::printer(self::$data, $code);
 1167     }
 1168     /**
 1169      * Gets json data
 1170      *
 1171      * @return string
 1172      */
 1173     public static function getData()
 1174     {
 1175         $message = json_encode(
 1176             self::$data,
 1177             JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
 1178         );
 1179         self::$data = '';
 1180         return $message;
 1181     }
 1182     /**
 1183      * Generates a default means to print data to screen.
 1184      *
 1185      * @param mixed    $data The data to print.
 1186      * @param bool|int $code Send custom error code.
 1187      *
 1188      * @return void
 1189      */
 1190     public static function printer($data, $code = false)
 1191     {
 1192         $message = json_encode(
 1193             $data,
 1194             JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
 1195         );
 1196         if (false !== $code) {
 1197             self::sendResponse(
 1198                 $code,
 1199                 $message
 1200             );
 1201         }
 1202         self::sendResponse(
 1203             HTTPResponseCodes::HTTP_SUCCESS,
 1204             $message
 1205         );
 1206     }
 1207     /**
 1208      * This is a commonizing element so list/search/getinfo
 1209      * will operate in the same fasion.
 1210      *
 1211      * @param string $classname The name of the class.
 1212      * @param object $class     The class to work with.
 1213      *
 1214      * @return object|array
 1215      */
 1216     public static function getter($classname, $class, $item = '')
 1217     {
 1218         if (!$class instanceof $classname) {
 1219             return;
 1220         }
 1221         switch ($classname) {
 1222         case 'host':
 1223             $pass = $class->get('ADPass');
 1224             $passtest = self::aesdecrypt($pass);
 1225             if ($test_base64 = base64_decode($passtest)) {
 1226                 if (mb_detect_encoding($test_base64, 'utf-8', true)) {
 1227                     $pass = $test_base64;
 1228                 } elseif (mb_detect_encoding($passtest, 'utf-8', true)) {
 1229                     $pass = $passtest;
 1230                 }
 1231             }
 1232             $productKey = $class->get('productKey');
 1233             $productKeytest = self::aesdecrypt($productKey);
 1234             if ($test_base64 = base64_decode($productKeytest)) {
 1235                 if (mb_detect_encoding($test_base64, 'utf-8', true)) {
 1236                     $productKey = $test_base64;
 1237                 } elseif (mb_detect_encoding($productKeytest, 'utf-8', true)) {
 1238                     $productKey = $productKeytest;
 1239                 }
 1240             }
 1241             $data = FOGCore::fastmerge(
 1242                 $class->get(),
 1243                 array(
 1244                     'ADPass' => $pass,
 1245                     'productKey' => $productKey,
 1246                     'hostscreen' => $class->get('hostscreen')->get(),
 1247                     'hostalo' => $class->get('hostalo')->get(),
 1248                     'inventory' => self::getter(
 1249                         'inventory',
 1250                         $class->get('inventory')
 1251                     ),
 1252                     'image' => $class->get('imagename')->get(),
 1253                     'imagename' => $class->getImageName(),
 1254                     'pingstatus' => $class->getPingCodeStr(),
 1255                     'pingstatuscode' => (int)$class->get('pingstatus'),
 1256                     'pingstatustext' => socket_strerror((int)$class->get('pingstatus')),
 1257                     'primac' => $class->get('mac')->__toString(),
 1258                     'macs' => $class->getMyMacs()
 1259                 )
 1260             );
 1261             break;
 1262         case 'inventory':
 1263             $data = FOGCore::fastmerge(
 1264                 $class->get(),
 1265                 array(
 1266                     'memory' => $class->getMem()
 1267                 )
 1268             );
 1269             break;
 1270         case 'group':
 1271             $data = FOGCore::fastmerge(
 1272                 $class->get(),
 1273                 array(
 1274                     'hostcount' => $class->getHostCount()
 1275                 )
 1276             );
 1277             break;
 1278         case 'image':
 1279             $data = FOGCore::fastmerge(
 1280                 $class->get(),
 1281                 array(
 1282                     'os' => $class->get('os')->get(),
 1283                     'imagepartitiontype' => $class->get('imagepartitiontype')->get(),
 1284                     'imagetype' => $class->get('imagetype')->get(),
 1285                     'imagetypename' => $class->getImageType()->get('name'),
 1286                     'imageparttypename' => $class->getImagePartitionType()->get(
 1287                         'name'
 1288                     ),
 1289                     'osname' => $class->getOS()->get('name'),
 1290                     'storagegroupname' => $class->getStorageGroup()->get('name')
 1291                 )
 1292             );
 1293             break;
 1294         case 'snapin':
 1295             $data = FOGCore::fastmerge(
 1296                 $class->get(),
 1297                 array(
 1298                     'storagegroupname' => $class->getStorageGroup()->get('name')
 1299                 )
 1300             );
 1301             break;
 1302         case 'storagenode':
 1303             $extra = array();
 1304             if ($item == 'all') {
 1305                 $extra = array(
 1306                    'logfiles' => (
 1307                        $class->get('online') ?
 1308                         $class->get('logfiles') :
 1309                         []
 1310                    ),
 1311                     'snapinfiles' => (
 1312                         $class->get('online') ?
 1313                         $class->get('snapinfiles') :
 1314                         []
 1315                     ),
 1316                     'images' => (
 1317                         $class->get('online') ?
 1318                         $class->get('images') :
 1319                         []
 1320                     )
 1321                 );
 1322             } elseif (!empty($item)) {
 1323                 $extra = array(
 1324                    "$item" => (
 1325                        $class->get('online') ?
 1326                         $class->get($item) :
 1327                         []
 1328                    )
 1329                 );
 1330             }
 1331             $data = FOGCore::fastmerge(
 1332                 $class->get(),
 1333                 $extra,
 1334                 array(
 1335                     'storagegroup' => self::getter(
 1336                         'storagegroup',
 1337                         $class->get('storagegroup')
 1338                     ),
 1339                     'clientload' => $class->getClientLoad(),
 1340                     'online' => $class->get('online')
 1341                 )
 1342             );
 1343             break;
 1344         case 'storagegroup':
 1345             $data = FOGCore::fastmerge(
 1346                 $class->get(),
 1347                 array(
 1348                     'totalsupportedclients' => $class->getTotalSupportedClients(),
 1349                     'enablednodes' => $class->get('enablednodes'),
 1350                     'allnodes' => $class->get('allnodes')
 1351                 )
 1352             );
 1353             break;
 1354         case 'task':
 1355             $data = FOGCore::fastmerge(
 1356                 $class->get(),
 1357                 array(
 1358                     'image' => $class->get('image')->get(),
 1359                     'host' => self::getter(
 1360                         'host',
 1361                         $class->get('host')
 1362                     ),
 1363                     'type' => $class->get('type')->get(),
 1364                     'state' => $class->get('state')->get(),
 1365                     'storagenode' => $class->get('storagenode')->get(),
 1366                     'storagegroup' => $class->get('storagegroup')->get()
 1367                 )
 1368             );
 1369             break;
 1370         case 'plugin':
 1371             $data = FOGCore::fastmerge(
 1372                 $class->get(),
 1373                 array(
 1374                     'location' => $class->getPath(),
 1375                     'description' => $class->get('description'),
 1376                     'icon' => $class->getIcon(),
 1377                     'runinclude' => $class->getRuninclude(md5($class->get('name'))),
 1378                     'hash' => md5($class->get('name'))
 1379                 )
 1380             );
 1381             break;
 1382         case 'imaginglog':
 1383             $data = FOGCore::fastmerge(
 1384                 $class->get(),
 1385                 array(
 1386                     'host' => self::getter(
 1387                         'host',
 1388                         $class->get('host')
 1389                     ),
 1390                     'image' => (
 1391                         $class->get('image')
 1392                     )
 1393                 )
 1394             );
 1395             unset($data['images']);
 1396             break;
 1397         case 'snapintask':
 1398             $data = FOGCore::fastmerge(
 1399                 $class->get(),
 1400                 array(
 1401                     'snapin' => $class->get('snapin')->get(),
 1402                     'snapinjob' => self::getter(
 1403                         'snapinjob',
 1404                         $class->get('snapinjob')
 1405                     ),
 1406                     'state' => $class->get('state')->get()
 1407                 )
 1408             );
 1409             break;
 1410         case 'snapinjob':
 1411             $data = FOGCore::fastmerge(
 1412                 $class->get(),
 1413                 array(
 1414                     'host' => self::getter(
 1415                         'host',
 1416                         $class->get('host')
 1417                     ),
 1418                     'state' => $class->get('state')->get()
 1419                 )
 1420             );
 1421             break;
 1422         case 'usertracking':
 1423             $data = FOGCore::fastmerge(
 1424                 $class->get(),
 1425                 array(
 1426                     'host' => self::getter(
 1427                         'host',
 1428                         $class->get('host')
 1429                     )
 1430                 )
 1431             );
 1432             break;
 1433         case 'multicastsession':
 1434             $data = FOGCore::fastmerge(
 1435                 $class->get(),
 1436                 array(
 1437                     'imageID' => $class->get('image'),
 1438                     'image' => $class->get('imagename')->get(),
 1439                     'state' => $class->get('state')->get()
 1440                 )
 1441             );
 1442             unset($data['imagename']);
 1443             break;
 1444         case 'scheduledtask':
 1445             $data = FOGCore::fastmerge(
 1446                 $class->get(),
 1447                 array(
 1448                     (
 1449                         $class->isGroupBased() ?
 1450                         'group' :
 1451                         'host'
 1452                     ) => (
 1453                         $class->isGroupBased() ?
 1454                         self::getter(
 1455                             'group',
 1456                             $class->getGroup()
 1457                         ) :
 1458                         self::getter(
 1459                             'host',
 1460                             $class->getHost()
 1461                         )
 1462                     ),
 1463                     'tasktype' => $class->getTaskType()->get(),
 1464                     'runtime' => $class->getTime()
 1465                 )
 1466             );
 1467             break;
 1468         case 'tasktype':
 1469             $data = FOGCore::fastmerge(
 1470                 $class->get(),
 1471                 array(
 1472                     'isSnapinTasking' => $class->isSnapinTasking()
 1473                 )
 1474             );
 1475             break;
 1476         default:
 1477             $data = $class->get();
 1478             break;
 1479         }
 1480         self::$HookManager
 1481             ->processEvent(
 1482                 'API_GETTER',
 1483                 array(
 1484                     'data' => &$data,
 1485                     'classname' => &$classname,
 1486                     'class' => &$class
 1487                 )
 1488             );
 1489         return $data;
 1490     }
 1491     /**
 1492      * Returns only the ids and names of the class passed in.
 1493      *
 1494      * @param string $class      The class to get list of.
 1495      * @param string $whereItems If we want to filter items.
 1496      *
 1497      * @return void
 1498      */
 1499     public function names($class, $whereItems = [])
 1500     {
 1501         $data = [];
 1502         $classname = strtolower($class);
 1503         $classVars = self::getClass(
 1504             $class,
 1505             '',
 1506             true
 1507         );
 1508 
 1509         $sql = 'SELECT `'
 1510             . $classVars['databaseFields']['id']
 1511             . '`,`'
 1512             . $classVars['databaseFields']['name']
 1513             . '` FROM `'
 1514             . $classVars['databaseTable']
 1515             . '`';
 1516 
 1517         if (count($whereItems) > 0) {
 1518             $where = '';
 1519             foreach ($whereItems as $key => &$field) {
 1520                 if (!$where) {
 1521                     $where = ' WHERE `'
 1522                         . $classVars['databaseFields'][$key]
 1523                         . '`';
 1524                 } else {
 1525                     $where .= ' AND `'
 1526                         . $classVars['databaseFields'][$key]
 1527                         . '`';
 1528                 }
 1529                 if (is_array($field)) {
 1530                     $where .= " IN ('"
 1531                         . implode("','", $field)
 1532                         . "')";
 1533                 } else {
 1534                     $where .= " = '"
 1535                         . $field
 1536                         . "'";
 1537                 }
 1538             }
 1539             $sql .= $where;
 1540         }
 1541         $sql .= ' ORDER BY `'
 1542             . (
 1543                 $classVars['databaseFields']['name'] ?:
 1544                 $classVars['databaseFields']['id']
 1545             )
 1546             . '` ASC';
 1547         $vals = self::$DB->query($sql)->fetch('', 'fetch_all')->get();
 1548         foreach ($vals as &$val) {
 1549             $data[] = [
 1550                 'id' => $val[$classVars['databaseFields']['id']],
 1551                 'name' => $val[$classVars['databaseFields']['name']]
 1552             ];
 1553             unset($val);
 1554         }
 1555 
 1556         self::$data = $data;
 1557     }
 1558     /**
 1559      * Returns only the ids of the class.
 1560      *
 1561      * @param string $class      The class to get list of.
 1562      * @param array  $whereItems The items to filter.
 1563      * @param string $getField   The field to get.
 1564      *
 1565      * @return void
 1566      */
 1567     public function ids($class, $whereItems = [], $getField = 'id')
 1568     {
 1569         $data = [];
 1570         $classname = strtolower($class);
 1571         $classVars = self::getClass(
 1572             $class,
 1573             '',
 1574             true
 1575         );
 1576         $vars = json_decode(
 1577             file_get_contents('php://input')
 1578         );
 1579 
 1580         $sql = 'SELECT `'
 1581             . $classVars['databaseFields'][$getField]
 1582             . '` FROM `'
 1583             . $classVars['databaseTable']
 1584             . '`';
 1585 
 1586         if (count($whereItems) > 0) {
 1587             $where = '';
 1588             foreach ($whereItems as $key => &$field) {
 1589                 if (!$where) {
 1590                     $where = ' WHERE `'
 1591                         . $classVars['databaseFields'][$key]
 1592                         . '`';
 1593                 } else {
 1594                     $where .= ' AND `'
 1595                         . $classVars['databaseFields'][$key]
 1596                         . '`';
 1597                 }
 1598                 if (is_array($field)) {
 1599                     $where .= " IN ('"
 1600                         . implode("','", $field)
 1601                         . "')";
 1602                 } else {
 1603                     $where .= " = '"
 1604                         . $field
 1605                         . "'";
 1606                 }
 1607             }
 1608             $sql .= $where;
 1609         }
 1610         $sql .= ' ORDER BY `'
 1611             . (
 1612                 $classVars['databaseFields']['name'] ?:
 1613                 $classVars['databaseFields']['id']
 1614             )
 1615             . '` ASC';
 1616         $vals = self::$DB->query($sql)->fetch('', 'fetch_all')->get();
 1617         foreach ($vals as &$val) {
 1618             $data[] = $val[$classVars['databaseFields'][$getField]];
 1619             unset($val);
 1620         }
 1621         self::$data = $data;
 1622     }
 1623     /**
 1624      * Delete items in mass.
 1625      *
 1626      * @param string $class      The class we're to remove items.
 1627      * @param array  $whereItems The items we're removing.
 1628      *
 1629      * @return void
 1630      */
 1631     public static function deletemass($class, $whereItems = [])
 1632     {
 1633         $data = [];
 1634         $classname = strtolower($class);
 1635         $classVars = self::getClass(
 1636             $class,
 1637             '',
 1638             true
 1639         );
 1640         $vars = json_decode(
 1641             file_get_contents('php://input')
 1642         );
 1643 
 1644         $sql = 'DELETE FROM `'
 1645             . $classVars['databaseTable']
 1646             . '`';
 1647 
 1648         if (count($whereItems) > 0) {
 1649             $where = '';
 1650             foreach ($whereItems as $key => &$field) {
 1651                 if (!$where) {
 1652                     $where = ' WHERE `'
 1653                         . $classVars['databaseFields'][$key]
 1654                         . '`';
 1655                 } else {
 1656                     $where .= ' AND `'
 1657                         . $classVars['databaseFields'][$key]
 1658                         . '`';
 1659                 }
 1660                 if (is_array($field)) {
 1661                     $where .= " IN ('"
 1662                         . implode("','", $field)
 1663                         . "')";
 1664                 } else {
 1665                     $where .= " = '"
 1666                         . $field
 1667                         . "'";
 1668                 }
 1669             }
 1670             $sql .= $where;
 1671         }
 1672 
 1673         return self::$DB->query($sql);
 1674     }
 1675 }