Example #1
0
 /**
  * invoke this method when you want to validate json
  */
 public static function checkJSON($json, $rules, $strict = false)
 {
     /**
      * create object of this class
      */
     $checker = new self();
     /**
      * main function
      * invoke it for json checking
      */
     $result_array = $checker->check($json, $rules, array());
     $checker->countJsonKeys($json);
     /**
      * if strict option is true then
      * check on strict matching
      */
     if ($strict) {
         if ($checker->rules_keys_number != $checker->json_keys_number) {
             return false;
         }
     }
     /**
      * if we don't found the false in the result_array
      * then return true
      */
     if (array_search(false, $result_array) === false) {
         return true;
     } else {
         return false;
     }
 }
Example #2
0
 /**
  * Register plugin
  *
  * @param Ant\Ant $ant instance
  *
  * @return void
  */
 public function register(Ant $ant)
 {
     $asset = new self();
     $ant->register('asset', function ($path) use($asset) {
         return $asset->check($path);
     });
 }
Example #3
0
 /**
  * Validate a captcha code input against a captcha ID
  * 
  * @param string $id       The captcha ID to check
  * @param string $value    The captcha value supplied by the user
  * @param array  $options  Array of options to construct Securimage with.
  * Options must include database options if they are not set in securimage.php
  *
  * @see Securimage::$database_driver
  * @return bool true if the code was valid for the given captcha ID, false if not or if database failed to open
  */
 public static function checkByCaptchaId($id, $value, array $options = array())
 {
     $opts = array('captchaId' => $id, 'no_session' => true, 'use_database' => true);
     if (sizeof($options) > 0) {
         $opts = array_merge($options, $opts);
     }
     $si = new self($opts);
     if ($si->openDatabase()) {
         $code = $si->getCodeFromDatabase();
         if (is_array($code)) {
             $si->code = $code['code'];
             $si->code_display = $code['code_disp'];
         }
         if ($si->check($value)) {
             $si->clearCodeFromDatabase();
             return true;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
 /**
  *
  * Extract theme from archive
  * @throws Exception
  * @param string $source_path archive path
  *
  * @return waTheme
  */
 public static function extract($source_path)
 {
     static $white_list = array('js', 'css', 'html', 'txt', 'png', 'jpg', 'jpeg', 'jpe', 'tiff', 'bmp', 'gif', 'svg', 'htc', 'cur', 'ttf', 'eot', 'otf', 'woff', '');
     $autoload = waAutoload::getInstance();
     $autoload->add('Archive_Tar', 'wa-installer/lib/vendors/PEAR/Tar.php');
     $autoload->add('PEAR', 'wa-installer/lib/vendors/PEAR/PEAR.php');
     $instance = null;
     if (class_exists('Archive_Tar')) {
         try {
             $tar_object = new Archive_Tar($source_path, true);
             $files = $tar_object->listContent();
             if (!$files) {
                 self::throwArchiveException('INVALID_OR_EMPTY_ARCHIVE');
             }
             //search theme info
             $info = false;
             $pattern = "@(/|^)" . wa_make_pattern(self::PATH, '@') . "\$@";
             foreach ($files as $file) {
                 if (preg_match($pattern, $file['filename'])) {
                     $info = $tar_object->extractInString($file['filename']);
                     break;
                 }
             }
             if (!$info) {
                 self::throwThemeException('MISSING_THEME_XML');
             }
             $xml = @simplexml_load_string($info);
             $app_id = (string) $xml['app'];
             $id = (string) $xml['id'];
             if (!$app_id) {
                 self::throwThemeException('MISSING_APP_ID');
             } elseif (!$id) {
                 self::throwThemeException('MISSING_THEME_ID');
             } else {
                 if ($app_info = wa()->getAppInfo($app_id)) {
                     //TODO check theme support
                     if ($parent_theme = (string) $xml['parent_theme_id']) {
                         $parent_theme = explode(':', $parent_theme, 2);
                         try {
                             if (count($parent_theme) == 2) {
                                 new waTheme($parent_theme[1], $parent_theme[0]);
                             } else {
                                 new waTheme($parent_theme[0], $app_id);
                             }
                         } catch (Exception $ex) {
                             self::throwThemeException('PARENT_THEME_NOT_FOUND', $ex->getMessage());
                         }
                     }
                 } else {
                     $message = sprintf(_w('Theme “%s” is for app “%s”, which is not installed in your Webasyst. Install the app, and upload theme once again.'), $id, $app_id);
                     throw new waException($message);
                 }
             }
             $wa_path = "wa-apps/{$app_id}/themes/{$id}";
             $wa_pattern = wa_make_pattern($wa_path, '@');
             $file = reset($files);
             if (preg_match("@^{$wa_pattern}(/|\$)@", $file['filename'])) {
                 $extract_path = $wa_path;
                 $extract_pattern = $wa_pattern;
             } else {
                 $extract_path = $id;
                 $extract_pattern = wa_make_pattern($id, '@');
                 if (!preg_match("@^{$extract_pattern}(/|\$)@", $file['filename'])) {
                     $extract_path = '';
                     $extract_pattern = false;
                 }
             }
             if ($extract_path) {
                 $extract_path = trim($extract_path, '/') . '/';
             }
             $missed_files = array();
             foreach ($xml->xpath('/theme/files/file') as $theme_file) {
                 $path = (string) $theme_file['path'];
                 $parent = intval((string) $theme_file['parent']);
                 if (!in_array(pathinfo($theme_file['path'], PATHINFO_EXTENSION), array('html', 'js', 'css'))) {
                     self::throwThemeException('UNEXPECTED_EDITABLE_FILE_TYPE', $theme_file['path']);
                 }
                 if (!$parent) {
                     $missed_files[$path] = $extract_path . $path;
                 }
             }
             #angry check
             foreach ($files as $file) {
                 if ($extract_pattern && !preg_match("@^{$extract_pattern}(/|\$)@", $file['filename'])) {
                     self::throwThemeException('UNEXPECTED_FILE_PATH', "{$file['filename']}. Expect files in [{$extract_path}] directory");
                 } elseif (preg_match('@\\.(php\\d*|pl)@', $file['filename'], $matches)) {
                     if (preg_match('@(^|/)build\\.php$@', $file['filename'])) {
                         $file['content'] = $tar_object->extractInString($file['filename']);
                         if (!preg_match('@^<\\?php[\\s\\n]+return[\\s\\n]+\\d+;[\\s\\n]*$@', $file['content'])) {
                             self::throwThemeException('UNEXPECTED_FILE_CONTENT', $file['filename']);
                         }
                     } else {
                         self::throwThemeException('UNEXPECTED_FILE_TYPE', $file['filename']);
                     }
                 } else {
                     if (preg_match('@(^|/)\\.htaccess$@', $file['filename'])) {
                         $file['content'] = $tar_object->extractInString($file['filename']);
                         if (preg_match('@\\b(add|set)Handler\\b@ui', $file['content'])) {
                             self::throwThemeException('INVALID_HTACCESS', $file['filename']);
                         }
                     } elseif (!in_array(pathinfo($file['filename'], PATHINFO_EXTENSION), $white_list)) {
                         if (!in_array(strtolower(basename($file['filename'])), array('theme.xml', 'build.php', '.htaccess', 'readme'))) {
                             self::throwThemeException('UNEXPECTED_FILE_TYPE', $file['filename']);
                         }
                     }
                     if ($extract_pattern) {
                         $file['filename'] = preg_replace("@^{$extract_pattern}/?@", '', $file['filename']);
                     }
                     if (empty($file['typeflag']) && !empty($file['filename']) && isset($missed_files[$file['filename']])) {
                         unset($missed_files[$file['filename']]);
                     }
                 }
             }
             if (!empty($missed_files)) {
                 self::throwThemeException('MISSING_DESCRIBED_FILES', implode(', ', $missed_files));
             }
             self::verify($id);
             self::protect($app_id);
             $target_path = wa()->getDataPath("themes/{$id}", true, $app_id, false);
             waFiles::delete($target_path);
             if ($extract_path && !$tar_object->extractModify($target_path, $extract_path)) {
                 self::throwArchiveException('INTERNAL_ARCHIVE_ERROR');
             } elseif (!$tar_object->extract($target_path)) {
                 self::throwArchiveException('INTERNAL_ARCHIVE_ERROR');
             }
             $instance = new self($id, $app_id);
             $instance->check();
         } catch (Exception $ex) {
             if (isset($target_path) && $target_path) {
                 waFiles::delete($target_path, true);
             }
             throw $ex;
         }
     } else {
         self::throwArchiveException('UNSUPPORTED_ARCHIVE_TYPE');
     }
     return $instance;
 }
 /**
  * Static validator
  *
  * @param string $code
  * @return boolean
  */
 public static function staticValidate($code)
 {
     $Captcha = new self();
     return $Captcha->check($code);
 }
Example #6
0
 /**
  * Validate a captcha code input against a captcha ID
  * @param string $id The captcha ID to check
  * @param string $value The captcha value supplied by the user
  *
  * @return bool true if the code was valid for the given captcha ID, false if not or if database failed to open
  */
 public static function checkByCaptchaId($id, $value)
 {
     $si = new self(array('captchaId' => $id, 'no_session' => true, 'use_sqlite_db' => true));
     if ($si->openDatabase()) {
         $code = $si->getCodeFromDatabase();
         if (is_array($code)) {
             $si->code = $code['code'];
             $si->code_display = $code['code_disp'];
         }
         if ($si->check($value)) {
             return true;
         } else {
             return false;
         }
     } else {
         return false;
     }
 }
Example #7
0
 /**
  * Set types for a role
  *
  * @param   integer  $role_id  Role ID
  * @param   array    $current  List of types assigned to role
  * @return  boolean  True on success, False on errors
  */
 public function setTypesForRole($role_id = null, $current = null)
 {
     if (!$role_id) {
         $this->setError(\Lang::txt('Missing argument'));
         return false;
     }
     $role_id = intval($role_id);
     // Get an array of all the previous types
     $old = array();
     $types = $this->getTypesForRole($role_id);
     if ($types) {
         foreach ($types as $item) {
             $old[] = $item->id;
         }
     }
     // Run through the $current array and determine if
     // each item is new or not
     $keep = array();
     $add = array();
     if (is_array($current)) {
         foreach ($current as $bit) {
             if (!in_array($bit, $old)) {
                 $add[] = intval($bit);
             } else {
                 $keep[] = intval($bit);
             }
         }
     }
     $remove = array_diff($old, $keep);
     // Remove any types in the remove list
     if (count($remove) > 0) {
         $remove = implode(',', $remove);
         $this->_db->setQuery("DELETE FROM {$this->_tbl} WHERE role_id=" . $this->_db->quote($role_id) . " AND type_id IN ({$remove})");
         if (!$this->_db->query()) {
             $this->setError($this->_db->getErrorMsg());
             return false;
         }
     }
     // Add any types not in the OLD list
     if (count($add) > 0) {
         foreach ($add as $type) {
             $rt = new self($this->_db);
             $rt->role_id = $role_id;
             $rt->type_id = $type;
             if ($rt->check()) {
                 $rt->store();
             }
         }
     }
     return true;
 }
 /**
  *
  * Extract theme from archive
  * @throws Exception
  * @param string $source_path archive path
  *
  * @return waTheme
  */
 public static function extract($source_path)
 {
     $autoload = waAutoload::getInstance();
     $autoload->add('Archive_Tar', 'wa-installer/lib/vendors/PEAR/Tar.php');
     $autoload->add('PEAR', 'wa-installer/lib/vendors/PEAR/PEAR.php');
     if (class_exists('Archive_Tar')) {
         try {
             $tar_object = new Archive_Tar($source_path, true);
             $files = $tar_object->listContent();
             if (!$files) {
                 self::throwArchiveException('INVALID_OR_EMPTY_ARCHIVE');
             }
             //search theme info
             $theme_check_files = array(self::PATH);
             $theme_files_map = array();
             $info = false;
             $pattern = "/(\\/|^)" . wa_make_pattern(self::PATH) . "\$/";
             foreach ($files as $file) {
                 if (preg_match($pattern, $file['filename'])) {
                     $info = $tar_object->extractInString($file['filename']);
                     break;
                 }
             }
             if (!$info) {
                 self::throwThemeException('MISSING_THEME_XML');
             }
             $xml = @simplexml_load_string($info);
             $app_id = (string) $xml['app'];
             $id = (string) $xml['id'];
             if (!$app_id) {
                 self::throwThemeException('MISSING_APP_ID');
             } elseif (!$id) {
                 self::throwThemeException('MISSING_THEME_ID');
             } else {
                 if ($app_info = wa()->getAppInfo($app_id)) {
                     //TODO check theme support
                 } else {
                     $message = sprintf(_w('Theme “%s” is for app “%s”, which is not installed in your Webasyst. Install the app, and upload theme once again.'), $id, $app_id);
                     throw new waException($message);
                 }
             }
             $wa_path = "wa-apps/{$app_id}/themes/{$id}";
             $wa_pattern = wa_make_pattern($wa_path);
             $file = reset($files);
             if (preg_match("@^{$wa_pattern}(/|\$)@", $file['filename'])) {
                 $extract_path = $wa_path;
                 $extract_pattern = $wa_pattern;
             } else {
                 $extract_path = $id;
                 $extract_pattern = wa_make_pattern($id);
                 if (!preg_match("@^{$extract_pattern}(/|\$)@", $file['filename'])) {
                     $extract_path = '';
                     $extract_pattern = false;
                 }
             }
             foreach ($files as $file) {
                 if ($extract_pattern && !preg_match("@^{$extract_pattern}(/|\$)@", $file['filename'])) {
                     self::throwThemeException('UNEXPECTED_FILE_PATH', "{$file['filename']}. Expect files in [{$extract_path}] directory");
                 } elseif (preg_match('@\\.(php\\d*|pl)@', $file['filename'], $matches)) {
                     self::throwThemeException('UNEXPECTED_FILE_TYPE', $file['filename']);
                 }
             }
             self::verify($id);
             self::protect($app_id);
             $target_path = wa()->getDataPath("themes/{$id}", true, $app_id, false);
             waFiles::delete($target_path);
             if ($extract_path && !$tar_object->extractModify($target_path, $extract_path)) {
                 self::throwArchiveException('INTERNAL_ARCHIVE_ERROR');
             } elseif (!$tar_object->extract($target_path)) {
                 self::throwArchiveException('INTERNAL_ARCHIVE_ERROR');
             }
             $instance = new self($id, $app_id);
             $instance->check();
         } catch (Exception $ex) {
             if (isset($target_path) && $target_path) {
                 waFiles::delete($target_path, true);
             }
             throw $ex;
         }
     } else {
         self::throwArchiveException('UNSUPPORTED_ARCHIVE_TYPE');
     }
     return $instance;
 }
Example #9
0
 /**
  * Clone core folders and queries and assign
  * them to a given user ID
  *
  * @param   integer  $user_id  User ID
  * @return  array
  */
 public function cloneCore($user_id = 0)
 {
     // Get all the default folders
     $folders = $this->find('list', array('user_id' => 0, 'sort' => 'ordering', 'sort_Dir' => 'asc', 'iscore' => 1));
     $sq = new Query($this->_db);
     if (count($folders) <= 0) {
         $defaults = array(1 => array('Common', 'Mine', 'Custom'), 2 => array('Common', 'Mine'));
         foreach ($defaults as $iscore => $fldrs) {
             $i = 1;
             foreach ($fldrs as $fldr) {
                 $f = new self($this->_db);
                 $f->iscore = $iscore;
                 $f->title = $fldr;
                 $f->check();
                 $f->ordering = $i;
                 $f->user_id = 0;
                 $f->store();
                 switch ($f->alias) {
                     case 'common':
                         $j = $iscore == 1 ? $sq->populateDefaults('common', $f->id) : $sq->populateDefaults('commonnotacl', $f->id);
                         break;
                     case 'mine':
                         $sq->populateDefaults('mine', $f->id);
                         break;
                     default:
                         // Nothing for custom folder
                         break;
                 }
                 $i++;
                 if ($iscore == 1) {
                     $folders[] = $f;
                 }
             }
         }
     }
     $user_id = $user_id ?: User::get('id');
     $fid = 0;
     // Loop through each folder
     foreach ($folders as $k => $folder) {
         // Copy the folder for the user
         $stqf = new self($this->_db);
         $stqf->bind($folder);
         $stqf->created_by = $user_id;
         $stqf->created = Date::toSql();
         $stqf->id = null;
         $stqf->user_id = $user_id;
         $stqf->iscore = 0;
         $stqf->store();
         $queries = $sq->find('list', array('folder_id' => $folder->id));
         // Copy all the queries from the folder to the user
         foreach ($queries as $query) {
             $stq = new Query($this->_db);
             $stq->bind($query);
             $stq->created_by = $user_id;
             $stq->created = Date::toSql();
             $stq->id = null;
             $stq->user_id = $user_id;
             $stq->folder_id = $stqf->get('id');
             $stq->iscore = 0;
             $stq->store();
         }
         // If the folder is "custom", get its ID
         if ($folder->alias == 'custom') {
             $fid = $stqf->get('id');
         }
         $folders[$k] = $stqf;
     }
     if ($fid) {
         $this->_db->setQuery("UPDATE `#__support_queries` SET `folder_id`=" . $this->_db->quote($fid) . " WHERE `user_id`=" . $this->_db->quote($user_id) . " AND `iscore`=0 AND `folder_id`=0");
         $this->_db->query();
     }
     return $folders;
 }
Example #10
0
 /**
  * Transition old author strings to table
  *
  * @return  boolean  True on success, false on error
  */
 public function transitionAuthors()
 {
     $this->_db->setQuery("SELECT id, authors FROM `#__wiki_page` WHERE authors!='' AND authors IS NOT NULL");
     if ($pages = $this->_db->loadObjectList()) {
         foreach ($pages as $page) {
             $authors = explode(',', $page->authors);
             $authors = array_map('trim', $authors);
             foreach ($authors as $author) {
                 $targetuser = User::getInstance($author);
                 // Ensure we found an account
                 if (is_object($targetuser)) {
                     $wpa = new self($this->_db);
                     $wpa->page_id = $page->id;
                     $wpa->user_id = $targetuser->get('id');
                     if ($wpa->check()) {
                         $wpa->store();
                     } else {
                         $this->setError("Error adding page author: (page_id: {$wpa->page_id}, user_id: {$wpa->user_id}).");
                     }
                 }
             }
         }
     }
     if (!$this->getError()) {
         $this->_db->setQuery("ALTER TABLE {$this->_tbl} DROP COLUMN `authors`");
         if (!$this->_db->query()) {
             $this->setError($this->_db->getErrorMsg());
         }
     }
     if (!$this->getError()) {
         return true;
     }
     return false;
 }
Example #11
0
 /**
  * Override store to add logging
  *
  * @return  boolean
  */
 public function createDefault()
 {
     $tbl = new self($this->_db);
     $tbl->alias = 'default';
     $tbl->jobs = 3;
     if (!$tbl->check()) {
         $this->setError($tbl->getError());
         return false;
     }
     if (!$tbl->store()) {
         $this->setError($tbl->getError());
         return false;
     }
     return true;
 }