Exemplo n.º 1
0
 public static function validateFeed($section, $feedData)
 {
     if (!self::argVal($feedData, 'TITLE')) {
         return new KurogoError(1, $this->getLocalizedString('ERROR_NO_TITLE'), $this->getLocalizedString('ERROR_NO_TITLE_DESCRIPTION'));
     }
     if (!isset($feedData['MODEL_CLASS'])) {
         $feedData['MODEL_CLASS'] = self::$defaultModel;
     }
     try {
         $controller = NewsDataModel::factory($feedData['MODEL_CLASS'], $feedData);
     } catch (KurogoConfigurationException $e) {
         return KurogoError::errorFromException($e);
     }
     return true;
 }
Exemplo n.º 2
0
 private function errorHandler($sql, $errorInfo, $ignoreErrors, $catchErrorCodes)
 {
     $e = new KurogoDataException(sprintf("Error with %s: %s", $sql, $errorInfo['message']), $errorInfo['code']);
     if ($ignoreErrors) {
         $this->lastError = KurogoError::errorFromException($e);
         return;
     }
     // prevent the default error handling mechanism
     // from triggerring in the rare case of expected
     // errors such as unique field violations
     if (in_array($errorInfo[0], $catchErrorCodes)) {
         return $errorInfo;
     }
     Kurogo::log(LOG_WARNING, sprintf("%s error with %s: %s", $this->dbType, $sql, $errorInfo['message']), 'db');
     if (Kurogo::getSiteVar('DB_DEBUG')) {
         throw $e;
     }
 }
Exemplo n.º 3
0
function exceptionHandlerForAPI(Exception $exception)
{
    $bt = $exception->getTrace();
    array_unshift($bt, array('line' => $exception->getLine(), 'file' => $exception->getFile()));
    Kurogo::log(LOG_ALERT, "A " . get_class($exception) . " has occured: " . $exception->getMessage(), "exception", $bt);
    $error = KurogoError::errorFromException($exception);
    $response = new APIResponse();
    $response->setVersion(0);
    $response->setError($error);
    $response->display();
    exit;
}
Exemplo n.º 4
0
 private function uploadFile($type, $section, $subsection, $key, $value)
 {
     $sectionData = $this->getAdminData($type, $section, $subsection);
     if (isset($value['error']) && $value['error'] != UPLOAD_ERR_OK) {
         throw new KurogoDataException(Kurogo::file_upload_error_message($value['error']));
     }
     if (!isset($value['tmp_name']) || !is_uploaded_file($value['tmp_name'])) {
         throw new KurogoDataException("Error locating uploaded file");
     }
     switch ($sectionData['sectiontype']) {
         case 'fields':
             if (!isset($sectionData['fields'][$key])) {
                 throw new KurogoConfigurationException("Invalid key {$key} for {$type} section {$section}");
             }
             $fieldData = $sectionData['fields'][$key];
             break;
         case 'section':
             $fieldData = $sectionData;
             throw new KurogoConfigurationException("Code not written for this type of field");
             break;
         default:
             throw new KurogoConfigurationException("Unable to handle {$type} {$section}. Invalid section type " . $sectionData['sectiontype']);
     }
     if (!isset($fieldData['destinationType'])) {
         throw new KurogoConfigurationException("Unable to determine destination type");
     }
     switch ($fieldData['destinationType']) {
         case 'file':
             if (!isset($fieldData['destinationFile'])) {
                 throw new KurogoConfigurationException("Unable to determine destination location");
             }
             $destination = $fieldData['destinationFile'];
             break;
         case 'folder':
             if (!isset($fieldData['destinationFile'])) {
                 throw new KurogoConfigurationException("Unable to determine destination location");
             }
             if (!isset($fieldData['destinationFolder'])) {
                 throw new KurogoConfigurationException("Unable to determine destination location");
             }
             $destination = rtrim($fieldData['destinationFolder'], '/') . '/' . ltrim($fieldData['destinationFile'], '/');
             break;
     }
     $prefix = isset($fieldData['destinationPrefix']) ? $fieldData['destinationPrefix'] : '';
     if ($prefix && defined($prefix)) {
         $destination = constant($prefix) . '/' . $destination;
     }
     if (isset($fieldData['fileType'])) {
         switch ($fieldData['fileType']) {
             case 'image':
                 $this->setResponseVersion(1);
                 try {
                     $imageData = new ImageProcessor($value['tmp_name']);
                     $transformer = new ImageTransformer($fieldData);
                     $imageType = isset($fieldData['imageType']) ? $fieldData['imageType'] : null;
                     $result = $imageData->transform($transformer, $imageType, $destination);
                     if (KurogoError::isError($result)) {
                         $this->throwError($result);
                     }
                 } catch (KurogoException $e) {
                     throw new KurogoException("Uploaded file must be a valid image (" . $e->getMessage() . ")");
                 }
                 break;
             default:
                 throw new KurogoConfigurationException("Unknown fileType " . $fieldData['fileType']);
         }
     } else {
         if (!move_uploaded_file($value['tmp_name'], $destination)) {
             $this->throwError(new KurogoError(1, "Cannot save file", "Unable to save uploaded file"));
         }
     }
 }
Exemplo n.º 5
0
 public function transform(ImageTransformer $transformer, $imageType, $file)
 {
     $boundingBox = $transformer->getBoundingBox($this->width, $this->height);
     if (KurogoError::isError($boundingBox)) {
         return $boundingBox;
     }
     if (is_null($imageType)) {
         $imageType = $this->imagetype;
     } else {
         switch ($imageType) {
             case IMAGETYPE_GIF:
             case IMAGETYPE_JPEG:
             case IMAGETYPE_PNG:
                 break;
             case 'gif':
                 $imageType = IMAGETYPE_GIF;
                 break;
             case 'jpg':
                 $imageType = IMAGETYPE_JPEG;
                 break;
             case 'png':
                 $imageType = IMAGETYPE_PNG;
                 break;
         }
     }
     $width = $boundingBox[0];
     $height = $boundingBox[1];
     if ($this->width == $width && $this->height == $height && $imageType == $this->imagetype) {
         return copy($this->fileName, $file) ? true : new KurogoError(1, "Error copying", "Error saving file");
     }
     if (!function_exists('gd_info')) {
         throw new KurogoDataException("Resizing images requires the GD image library");
     }
     switch ($this->imagetype) {
         case IMAGETYPE_JPEG:
             $src = imagecreatefromjpeg($this->fileName);
             break;
         case IMAGETYPE_PNG:
             $src = imagecreatefrompng($this->fileName);
             break;
         case IMAGETYPE_GIF:
             $src = imagecreatefromgif($this->fileName);
             break;
         default:
             throw new KurogoDataException("Unable to read files of this type ({$this->imagetype})");
     }
     switch ($imageType) {
         case IMAGETYPE_JPEG:
             $dest = imagecreatetruecolor($width, $height);
             $saveFunc = 'ImageJPEG';
             break;
         case IMAGETYPE_PNG:
             $dest = imagecreatetruecolor($width, $height);
             imagealphablending($dest, false);
             imagesavealpha($dest, true);
             $saveFunc = 'ImagePNG';
             break;
         case IMAGETYPE_GIF:
             $dest = imagecreate($width, $height);
             $saveFunc = 'ImageGIF';
             break;
         default:
             throw new KurogoDataException("Unable to save files of this type");
     }
     $crop = false;
     if (isset($boundingBox[2]) && isset($boundingBox[3]) && !isset($boundingBox[4])) {
         //do crop
         $crop = true;
         $srcWidth = $boundingBox[2];
         $srcHeight = $boundingBox[3];
         if ($this->width == $srcWidth) {
             $y = ($this->height - $srcHeight) / 2;
             $x = 0;
         }
         if ($this->height == $srcHeight) {
             $x = ($this->width - $srcWidth) / 2;
             $y = 0;
         }
         imagecopyresampled($dest, $src, 0, 0, $x, $y, $width, $height, $srcWidth, $srcHeight);
     } elseif (isset($boundingBox[2]) && isset($boundingBox[3]) && isset($boundingBox[4])) {
         $crop = true;
         // do fill
         $rgb = $boundingBox[5] ? $boundingBox[5] : "ffffff";
         $bg = imagecolorallocate($dest, hexdec(substr($rgb, 0, 2)), hexdec(substr($rgb, 2, 2)), hexdec(substr($rgb, 4, 2)));
         // fill white color on bg
         imagefill($dest, 0, 0, $bg);
         $y = ($height - $this->height) / 2;
         $x = ($width - $this->width) / 2;
         imagecopy($dest, $src, $x, $y, 0, 0, $this->width, $this->height);
     }
     if (!$crop) {
         if ($this->width != $width || $this->height != $height) {
             imagecopyresampled($dest, $src, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
         } else {
             $dest &= $src;
         }
     }
     return call_user_func($saveFunc, $dest, $file);
 }
Exemplo n.º 6
0
function exceptionHandlerForAPI($exception) {
    $error = KurogoError::errorFromException($exception);
    $response = new APIResponse();
    $response->setVersion(0);
    $response->setError($error);
    $response->display();
}
Exemplo n.º 7
0
  private function errorHandler($sql, $errorInfo, $ignoreErrors, $catchErrorCodes) {

        if (Kurogo::getSiteVar('DB_DEBUG')) {
           $e = new Exception (sprintf("Error with %s: %s", $sql, $errorInfo[2]), $errorInfo[1]);
        }
        
        if ($ignoreErrors) {
            $this->lastError = KurogoError::errorFromException($e);
            return;
        }

        // prevent the default error handling mechanism
        // from triggerring in the rare case of expected
        // errors such as unique field violations
        if(in_array($errorInfo[0], $catchErrorCodes)) {
            return $errorInfo;
        }

        if (Kurogo::getSiteVar('DB_DEBUG')) {
            throw $e;
        } else {
            error_log(sprintf("Error with %s: %s", $sql, $errorInfo[2]));
        }
  }
Exemplo n.º 8
0
 public function transform(ImageTransformer $transformer, $imageType, $file)
 {
     $boundingBox = $transformer->getBoundingBox($this->width, $this->height);
     if (KurogoError::isError($boundingBox)) {
         return $boundingBox;
     }
     if (is_null($imageType)) {
         $imageType = $this->imagetype;
     } else {
         switch ($imageType) {
             case IMAGETYPE_GIF:
             case IMAGETYPE_JPEG:
             case IMAGETYPE_PNG:
                 break;
             case 'gif':
                 $imageType = IMAGETYPE_GIF;
                 break;
             case 'jpg':
                 $imageType = IMAGETYPE_JPEG;
                 break;
             case 'png':
                 $imageType = IMAGETYPE_PNG;
                 break;
         }
     }
     $width = $boundingBox[0];
     $height = $boundingBox[1];
     if ($this->width == $width && $this->height == $height && $imageType == $this->imagetype) {
         return copy($this->fileName, $file) ? true : new KurogoError(1, "Error copying", "Error saving file");
     }
     if (!function_exists('gd_info')) {
         throw new KurogoDataException("Resizing images requires the GD image library");
     }
     switch ($this->imagetype) {
         case IMAGETYPE_JPEG:
             $src = imagecreatefromjpeg($this->fileName);
             break;
         case IMAGETYPE_PNG:
             $src = imagecreatefrompng($this->fileName);
             break;
         case IMAGETYPE_GIF:
             $src = imagecreatefromgif($this->fileName);
             break;
         default:
             throw new KurogoDataException("Unable to read files of this type ({$this->imagetype})");
     }
     switch ($imageType) {
         case IMAGETYPE_JPEG:
             $dest = imagecreatetruecolor($width, $height);
             $saveFunc = 'ImageJPEG';
             break;
         case IMAGETYPE_PNG:
             $dest = imagecreatetruecolor($width, $height);
             imagealphablending($dest, false);
             imagesavealpha($dest, true);
             $saveFunc = 'ImagePNG';
             break;
         case IMAGETYPE_GIF:
             $dest = imagecreate($width, $height);
             $saveFunc = 'ImageGIF';
             break;
         default:
             throw new KurogoDataException("Unable to save files of this type");
     }
     if ($this->width != $width || $this->height != $height) {
         imagecopyresampled($dest, $src, 0, 0, 0, 0, $width, $height, $this->width, $this->height);
     } else {
         $dest &= $src;
     }
     return call_user_func($saveFunc, $dest, $file);
 }
Exemplo n.º 9
0
    private function setConfigVar($type, $section, $subsection, $key, $value) {

        $sectionData = $this->getAdminData($type, $section, $subsection);
        $changed = false;
            
        switch ($sectionData['sectiontype'])
        {
            case 'fields':
                if (!isset($sectionData['fields'][$key])) {
                    throw new Exception("Invalid key $key for $type section $section");
                }
                
                $fieldData = $sectionData['fields'][$key];
                break;
            
            case 'section':
                $fieldData = $sectionData;
                break;
            default:
                throw new Exception("Unable to handle $type $section. Invalid section type " . $sectionData['sectiontype']);
        }
        
        $config = $this->getAdminConfig($type, $fieldData['config'], ConfigFile::OPTION_CREATE_EMPTY);

        //remove blank values before validation
        if (is_array($value)) {
            foreach ($value as $k=>$v) {
                $prefix = isset($value[$k . '_prefix']) ? $value[$k . '_prefix'] : '';
                if ($prefix && defined($prefix)) {
                    $value[$k] = constant($prefix) . '/' . $v;
                }
                if (isset($value[$k . '_prefix'])) {
                    unset($value[$k . '_prefix']);
                }

                if (isset($fieldData['fields'][$k]['omitBlankValue']) && $fieldData['fields'][$k]['omitBlankValue'] && strlen($v)==0) {
                    $changed = $changed || $config->clearVar($key, $k);
                    unset($value[$k]);
                }

                if ($fieldData['fields'][$k]['type']=='paragraph') {
                    $value[$k] = explode("\n\n", str_replace(array("\r\n","\r"), array("\n","\n"), $v));
                }
            }
        }
        
        if (isset($sectionData['sectionvalidatemethod'])) {
            $result = call_user_func($sectionData['sectionvalidatemethod'], $key, $value);
            if (KurogoError::isError($result)) {
                throw new Exception($result->getMessage());
            }
        }
        
        if (is_array($value)) {
            $result = true;
            foreach ($value as $k=>$v) {
                if (preg_match("/^(.*?)_prefix$/", $k,$bits)) {
                    continue;
                } 

                if (!isset($fieldData['fields'][$k])) {
                    throw new Exception("Invalid key $k for $type:" . $fieldData['config'] . " section $key");
                }
                
                $prefix = isset($value[$k . '_prefix']) ? $value[$k . '_prefix'] : '';
                if ($prefix && defined($prefix)) {
                    $v = constant($prefix) . '/' . $v;
                }
                
                if (!$config->setVar($key, $k, $v, $c)) {
                    $result = false;
                }
                $changed = $changed || $c;
            }
        } else {
            if (isset($fieldData['omitBlankValue']) && $fieldData['omitBlankValue'] && strlen($value)==0) {
                $changed = $config->clearVar($fieldData['section'], $key);
            } else {
                if ($fieldData['type']=='paragraph') {
                    $value = explode("\n\n", str_replace(array("\r\n","\r"), array("\n","\n"), $value));
                }
            
                $result = $config->setVar($fieldData['section'], $key, $value, $changed);

                if (!$result) {
                    throw new Exception("Error setting $config $section $key $value");
                }
            }
        }
        
        if ($changed) {    
            if (!in_array($config, $this->changedConfigs)) {
                $this->changedConfigs[] = $config;
            }
        }
    }
Exemplo n.º 10
0
 /**
  * Execute the command. Will call initializeForCommand() which should set the version, error and response
  * values appropriately
  */
 public function executeCommand()
 {
     if (empty($this->command)) {
         throw new KurogoException("Command not specified");
     }
     $this->loadResponseIfNeeded();
     if ($this->clientBrowser == 'native') {
         if ($appData = Kurogo::getAppData($this->clientPlatform)) {
             if ($minversion = Kurogo::arrayVal($appData, 'minversion')) {
                 if (version_compare($this->clientVersion, $minversion) < 0) {
                     $data = array('url' => Kurogo::arrayVal($appData, 'url'), 'version' => Kurogo::arrayVal($appData, 'version'));
                     $error = new KurogoError(7, 'Upgrade Required', 'You must upgrade your application');
                     $error->setData($data);
                     $this->throwError($error);
                 }
             }
         }
     }
     $this->initializeForCommand();
     $json = $this->response->getJSONOutput();
     $size = strlen($json);
     if ($this->logView) {
         $this->logCommand($size);
     }
     header("Content-Length: " . $size);
     header("Content-Type: application/json; charset=utf-8");
     echo $json;
     exit;
 }