/**
  * Import language from XML form
  * 
  * @param void
  * @return null
  */
 function import()
 {
     if (!extension_loaded('xml') || !function_exists('xml_parser_create')) {
         $this->httpError(HTTP_ERR_NOT_FOUND);
     }
     // if
     $import_url = assemble_url('admin_languages_import');
     $this->wireframe->addBreadCrumb(lang('Import Language'), $import_url);
     $step = $this->request->post('wizard_step') ? $this->request->post('wizard_step') : 'initial';
     if ($step == 'initial') {
         $next_step = 'review';
     } else {
         if ($step == 'review') {
             $next_step = 'finalize';
         }
     }
     // if
     if (!folder_is_writable(LOCALIZATION_PATH)) {
         $this->wireframe->addPageMessage(lang('Localization folder: <strong>:folder</strong> is not writable', array('folder' => LOCALIZATION_PATH)), PAGE_MESSAGE_ERROR);
         $this->smarty->assign('import_enabled', false);
     } else {
         $this->smarty->assign(array('import_url' => $import_url, 'step' => $step, 'next_step' => $next_step, 'import_enabled' => true));
         if ($this->request->isSubmitted()) {
             switch ($step) {
                 case 'initial':
                     break;
                 case 'review':
                     $xml_file = $_FILES['xml']['tmp_name'];
                     if (!is_file($xml_file)) {
                         flash_error('You need to upload XML file first');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     require_once ANGIE_PATH . '/classes/xml/xml2array.php';
                     $language = xml2array(file_get_contents($xml_file));
                     if (!$language) {
                         flash_error('Language XML file is corrupted');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $locale = $language['language']['info']['locale']['value'];
                     $name = $language['language']['info']['name']['value'];
                     $ac_version = $language['language']['info']['ac_version']['value'];
                     $system_version = $this->application->version ? $this->application->version : '1.0';
                     if (!$locale || !$name) {
                         flash_error('Language XML file is corrupted');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     if (Languages::localeExists($locale)) {
                         flash_error('Language with locale :locale is already installed on system', array('locale' => $locale));
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     if (Languages::nameExists($name)) {
                         flash_error('Language with name :name is already installed on system', array('name' => $name));
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $attachment = make_attachment($_FILES['xml']);
                     if (!$attachment || is_error($attachment)) {
                         flash_error($attachment->getMessage(), array('name' => $name));
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     if (version_compare($ac_version, $system_version, '=') != true) {
                         $this->wireframe->addPageMessage(lang('Current activeCollab version is <strong>:system_version</strong> and this translation is made for <strong>:ac_version</strong> version. Importing can continue, but this translation may not work on your system', array('system_version' => $system_version, 'ac_version' => $ac_version)), 'warning');
                     }
                     // if
                     $this->smarty->assign(array('language_ac_version' => $ac_version, 'language_name' => $name, 'language_locale' => $locale, 'system_version' => $system_version, 'attachment_id' => $attachment->getId()));
                     $this->setTemplate('import_review');
                     break;
                 case 'finalize':
                     $attachment_id = $this->request->post('attachment_id');
                     $attachment = Attachments::findById($attachment_id);
                     if (!instance_of($attachment, 'Attachment')) {
                         flash_error('There was some unknown error, please try again');
                         $this->redirectTo($import_url);
                     }
                     // if
                     require_once ANGIE_PATH . '/classes/xml/xml2array.php';
                     $language_array = xml2array(file_get_contents(UPLOAD_PATH . '/' . $attachment->getLocation()));
                     if (!$language_array) {
                         flash_error('Uploaded file is not valid XML');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $language_locale = $language_array['language']['info']['locale']['value'];
                     $language_name = $language_array['language']['info']['name']['value'];
                     $language_version = $language_array['language']['info']['ac_version']['value'];
                     $language = new Language();
                     $language->setLocale($language_locale);
                     $language->setName($language_name);
                     $result = recursive_mkdir($language->getLocalizationPath(), 0777, LOCALIZATION_PATH);
                     if (!$result) {
                         flash_error('Could not create localization folder');
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $save = $language->save();
                     if (!$save || is_error($save)) {
                         flash_error($save->getMessage());
                         $this->redirectToReferer($import_url);
                     }
                     // if
                     $info = array('name' => $language_name, 'code' => $language_locale, 'version' => $language_version);
                     $result = file_put_contents($language->getLocalizationPath() . '/info.php', "<?php return " . var_export($info, true) . " ?>");
                     if (is_foreachable($language_array['language']['translations']['module'])) {
                         foreach ($language_array['language']['translations']['module'] as $module_translation) {
                             if (is_foreachable($module_translation['translation'])) {
                                 $module_name = $module_translation['attr']['name'];
                                 $output = array();
                                 foreach ($module_translation['translation'] as $translation) {
                                     if (is_array($translation)) {
                                         $phrase = $translation['attr']['phrase'];
                                         $translation_value = $translation['value'];
                                         $output[$phrase] = $translation_value;
                                     }
                                     // if
                                 }
                                 // foreach
                                 $filename = Languages::getTranslationPath($language, $module_name);
                                 file_put_contents($filename, "<?php return " . var_export($output, true) . " ?>");
                             }
                             // if
                         }
                         // foreach
                     }
                     // if
                     $attachment->delete();
                     flash_success('Language imported successfully');
                     $this->redirectTo('admin_languages');
                     break;
                 default:
                     break;
             }
         }
         // if
     }
     // if
 }
Esempio n. 2
0
function hj_framework_process_inputs($hook, $type, $return, $params)
{
    $entity = elgg_extract('entity', $params, false);
    $field = elgg_extract('field', $params, false);
    if (!$entity || !$field || !elgg_instanceof($entity)) {
        return true;
    }
    $type = $entity->getType();
    $subtype = $entity->getSubtype();
    switch ($field->input_type) {
        case 'file':
            if (elgg_is_logged_in()) {
                global $_FILES;
                $field_name = $field->name;
                $file = $_FILES[$field_name];
                // Maybe someone doesn't want us to save the file in this particular way
                if (!empty($file['name']) && !elgg_trigger_plugin_hook('hj:framework:form:fileupload', 'all', array('entity' => $entity, 'file' => $file, 'field_name' => $field_name), false)) {
                    hj_framework_process_file_upload($file, $entity, $field_name);
                }
            }
            break;
        case 'entity_icon':
            $field_name = $field->name;
            global $_FILES;
            if (isset($_FILES[$field_name]) && substr_count($_FILES[$field_name]['type'], 'image/')) {
                hj_framework_generate_entity_icons($entity, $field_name);
                $entity->{$field_name} = null;
            }
            break;
        case 'relationship_tags':
            $field_name = $field->name;
            $tags = get_input('relationship_tag_guids');
            $relationship_name = get_input('relationship_tags_name', 'tagged_in');
            $current_tags = elgg_get_entities_from_relationship(array('relationship' => $relationship_name, 'relationship_guid' => $entity->guid, 'inverse_relationship' => true));
            if (is_array($current_tags)) {
                foreach ($current_tags as $current_tag) {
                    if (!in_array($current_tag->guid, $tags)) {
                        remove_entity_relationship($current_tag->guid, $relationship_name, $entity->guid);
                    }
                }
            }
            if (is_array($tags)) {
                foreach ($tags as $tag_guid) {
                    add_entity_relationship($tag_guid, $relationship_name, $entity->guid);
                }
                $tags = implode(',', $tags);
            }
            $entity->{$field_name} = $tags;
            break;
        case 'multifile':
            if (elgg_is_logged_in()) {
                $values = get_input($field->name);
                if (is_array($values)) {
                    foreach ($values as $value) {
                        create_metadata($entity->guid, $field->name, $value, '', $entity->owner_guid, $entity->access_id, true);
                        if (!elgg_trigger_plugin_hook('hj:framework:form:multifile', 'all', array('entity' => $entity, 'file_guid' => $value, 'field_name' => $field_name), false)) {
                            make_attachment($entity->guid, $value);
                        }
                    }
                }
            }
            break;
    }
    return true;
}
Esempio n. 3
0
/**
 * Send a message to specified recipients
 *
 * @param int $sender_guid GUID of the sender entity
 * @param array $recipient_guids An array of recipient GUIDs
 * @param str $subject Subject of the message
 * @param str $message Body of the message
 * @param str $message_type Type of the message
 * @param array $params Additional parameters, e.g. 'message_hash', 'attachments'
 * @return boolean
 */
function hj_inbox_send_message($sender_guid, $recipient_guids, $subject = '', $message = '', $message_type = '', array $params = array())
{
    $ia = elgg_set_ignore_access();
    if (!is_array($recipient_guids)) {
        $recipient_guids = array($recipient_guids);
    }
    if (isset($params['message_hash'])) {
        $message_hash = elgg_extract('message_hash', $params);
    }
    if (isset($params['attachments'])) {
        $attachments = elgg_extract('attachments', $params);
    }
    $user_guids = $recipient_guids;
    $user_guids[] = $sender_guid;
    sort($user_guids);
    if (!$message_hash) {
        $title = strtolower($subject);
        $title = trim(str_replace('re:', '', $title));
        $message_hash = sha1(implode(':', $user_guids) . $title);
    }
    $acl_hash = sha1(implode(':', $user_guids));
    $dbprefix = elgg_get_config('dbprefix');
    $query = "SELECT * FROM {$dbprefix}access_collections WHERE name = '{$acl_hash}'";
    $collection = get_data_row($query);
    //error_log(print_r($collection, true));
    $acl_id = $collection->id;
    if (!$acl_id) {
        $site = elgg_get_site_entity();
        $acl_id = create_access_collection($acl_hash, $site->guid);
        update_access_collection($acl_id, $user_guids);
    }
    //error_log($acl_id);
    $message_sent = new ElggObject();
    $message_sent->subtype = "messages";
    $message_sent->owner_guid = $sender_guid;
    $message_sent->container_guid = $sender_guid;
    $message_sent->access_id = ACCESS_PRIVATE;
    $message_sent->title = $subject;
    $message_sent->description = $message;
    $message_sent->toId = $recipient_guids;
    // the users receiving the message
    $message_sent->fromId = $sender_guid;
    // the user sending the message
    $message_sent->readYet = 1;
    // this is a toggle between 0 / 1 (1 = read)
    $message_sent->hiddenFrom = 0;
    // this is used when a user deletes a message in their sentbox, it is a flag
    $message_sent->hiddenTo = 0;
    // this is used when a user deletes a message in their inbox
    $message_sent->msg = 1;
    $message_sent->msgType = $message_type;
    $message_sent->msgHash = $message_hash;
    $message_sent->save();
    if ($attachments) {
        $count = count($attachments['name']);
        for ($i = 0; $i < $count; $i++) {
            if ($attachments['error'][$i] || !$attachments['name'][$i]) {
                continue;
            }
            $name = $attachments['name'][$i];
            $file = new ElggFile();
            $file->container_guid = $message_sent->guid;
            $file->title = $name;
            $file->access_id = (int) $acl_id;
            $prefix = "file/";
            $filestorename = elgg_strtolower(time() . $name);
            $file->setFilename($prefix . $filestorename);
            $file->open("write");
            $file->close();
            move_uploaded_file($attachments['tmp_name'][$i], $file->getFilenameOnFilestore());
            $saved = $file->save();
            if ($saved) {
                $mime_type = ElggFile::detectMimeType($attachments['tmp_name'][$i], $attachments['type'][$i]);
                $info = pathinfo($name);
                $office_formats = array('docx', 'xlsx', 'pptx');
                if ($mime_type == "application/zip" && in_array($info['extension'], $office_formats)) {
                    switch ($info['extension']) {
                        case 'docx':
                            $mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                            break;
                        case 'xlsx':
                            $mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                            break;
                        case 'pptx':
                            $mime_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                            break;
                    }
                }
                // check for bad ppt detection
                if ($mime_type == "application/vnd.ms-office" && $info['extension'] == "ppt") {
                    $mime_type = "application/vnd.ms-powerpoint";
                }
                $file->msgHash = $message_hash;
                $file->toId = $recipient_guids;
                $file->fromId = $sender_guid;
                $file->setMimeType($mime_type);
                $file->originalfilename = $name;
                if (elgg_is_active_plugin('file')) {
                    $file->simpletype = file_get_simple_type($mime_type);
                }
                $file->save();
                $guid = $file->getGUID();
                $uploaded_attachments[] = $guid;
                $attachment_urls .= '<div class="inbox-attachment">' . elgg_view('output/url', array('href' => "messages/download/{$guid}", 'text' => $file->title, 'is_trusted' => true)) . '</div>';
                if ($file->simpletype == "image") {
                    $file->icontime = time();
                    $thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 60, 60, true);
                    if ($thumbnail) {
                        $thumb = new ElggFile();
                        $thumb->setMimeType($attachments['type'][$i]);
                        $thumb->setFilename($prefix . "thumb" . $filestorename);
                        $thumb->open("write");
                        $thumb->write($thumbnail);
                        $thumb->close();
                        $file->thumbnail = $prefix . "thumb" . $filestorename;
                        unset($thumbnail);
                    }
                    $thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 153, 153, true);
                    if ($thumbsmall) {
                        $thumb->setFilename($prefix . "smallthumb" . $filestorename);
                        $thumb->open("write");
                        $thumb->write($thumbsmall);
                        $thumb->close();
                        $file->smallthumb = $prefix . "smallthumb" . $filestorename;
                        unset($thumbsmall);
                    }
                    $thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(), 600, 600, false);
                    if ($thumblarge) {
                        $thumb->setFilename($prefix . "largethumb" . $filestorename);
                        $thumb->open("write");
                        $thumb->write($thumblarge);
                        $thumb->close();
                        $file->largethumb = $prefix . "largethumb" . $filestorename;
                        unset($thumblarge);
                    }
                }
            }
        }
    }
    $success = $error = 0;
    foreach ($recipient_guids as $recipient_guid) {
        $message_to = new ElggObject();
        $message_to->subtype = "messages";
        $message_to->owner_guid = $recipient_guid;
        $message_to->container_guid = $recipient_guid;
        $message_to->access_id = ACCESS_PRIVATE;
        $message_to->title = $subject;
        $message_to->description = $message;
        $message_to->toId = $recipient_guids;
        // the users receiving the message
        $message_to->fromId = $sender_guid;
        // the user sending the message
        $message_to->readYet = 0;
        // this is a toggle between 0 / 1 (1 = read)
        $message_to->hiddenFrom = 0;
        // this is used when a user deletes a message in their sentbox, it is a flag
        $message_to->hiddenTo = 0;
        // this is used when a user deletes a message in their inbox
        $message_to->msg = 1;
        $message_to->msgType = $message_type;
        $message_to->msgHash = $message_hash;
        if ($message_to->save()) {
            $success++;
            // Make attachments
            if ($uploaded_attachments) {
                foreach ($uploaded_attachments as $attachment_guid) {
                    make_attachment($message_to->guid, $attachment_guid);
                }
            }
            // Send out notifications skipping 'site' notification handler
            if ($recipient_guid != $sender_guid) {
                $methods = (array) get_user_notification_settings($recipient_guid);
                unset($methods['site']);
                if (count($methods)) {
                    $recipient = get_user($recipient_guid);
                    $sender = get_user($sender_guid);
                    $notification_subject = elgg_echo('messages:email:subject');
                    $notification_message = strip_tags($message);
                    if ($uploaded_attachments) {
                        $notification_message .= elgg_view_module('inbox-attachments', elgg_echo('messages:attachments'), $attachment_urls);
                    }
                    $notification_body = elgg_echo('messages:email:body', array($sender->name, $notification_message, elgg_get_site_url() . "messages/inbox/{$recipient->username}?message_type={$message_type}", $sender->name, elgg_get_site_url() . "messages/thread/{$message_hash}"));
                    notify_user($recipient_guid, $sender_guid, $notification_subject, $notification_body, null, $methods);
                }
            }
        } else {
            $error++;
        }
    }
    if ($success > 0) {
        // Make attachments
        if ($uploaded_attachments) {
            foreach ($uploaded_attachments as $attachment_guid) {
                make_attachment($message_sent->guid, $attachment_guid);
            }
        }
        $return = true;
    } else {
        $message_sent->delete();
        $return = false;
    }
    elgg_set_ignore_access($ia);
    return $return;
}
Esempio n. 4
0
/**
 * Process uploaded files
 *
 * @param mixed $files		Uploaded files
 * @param mixed $entity		If an entity is set and it doesn't belong to one of the file subtypes, uploaded files will be converted into hjFile objects and attached to the entity
 * @return void
 */
function hj_framework_process_file_upload($name, $entity = null)
{
    // Normalize the $_FILES array
    if (is_array($_FILES[$name]['name'])) {
        $files = hj_framework_prepare_files_global($_FILES);
        $files = $files[$name];
    } else {
        $files = $_FILES[$name];
        $files = array($files);
    }
    if (elgg_instanceof($entity)) {
        if (!$entity instanceof hjFile) {
            $is_attachment = true;
        }
        $subtype = $entity->getSubtype();
    }
    foreach ($files as $file) {
        if (!is_array($file) || $file['error']) {
            continue;
        }
        if ($is_attachment) {
            $filehandler = new hjFile();
        } else {
            $filehandler = new hjFile($entity->guid);
        }
        $prefix = 'hjfile/';
        if ($entity instanceof hjFile) {
            $filename = $filehandler->getFilenameOnFilestore();
            if (file_exists($filename)) {
                unlink($filename);
            }
            $filestorename = $filehandler->getFilename();
            $filestorename = elgg_substr($filestorename, elgg_strlen($prefix));
        } else {
            $filestorename = elgg_strtolower(time() . $file['name']);
        }
        $filehandler->setFilename($prefix . $filestorename);
        $filehandler->title = $file['name'];
        $mime_type = ElggFile::detectMimeType($file['tmp_name'], $file['type']);
        // hack for Microsoft zipped formats
        $info = pathinfo($file['name']);
        $office_formats = array('docx', 'xlsx', 'pptx');
        if ($mime_type == "application/zip" && in_array($info['extension'], $office_formats)) {
            switch ($info['extension']) {
                case 'docx':
                    $mime_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    break;
                case 'xlsx':
                    $mime_type = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    break;
                case 'pptx':
                    $mime_type = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
                    break;
            }
        }
        // check for bad ppt detection
        if ($mime_type == "application/vnd.ms-office" && $info['extension'] == "ppt") {
            $mime_type = "application/vnd.ms-powerpoint";
        }
        $filehandler->setMimeType($mime_type);
        $filehandler->originalfilename = $file['name'];
        $filehandler->simpletype = hj_framework_get_simple_type($mime_type);
        $filehandler->filesize = $file['size'];
        $filehandler->open("write");
        $filehandler->close();
        move_uploaded_file($file['tmp_name'], $filehandler->getFilenameOnFilestore());
        if ($filehandler->save()) {
            if ($is_attachment && elgg_instanceof($entity)) {
                make_attachment($entity->guid, $filehandler->getGUID());
            }
            // Generate icons for images
            if ($filehandler->simpletype == "image") {
                if (!elgg_instanceof($entity) || $is_attachment) {
                    // no entity provided or this is an attachment generating icons for self
                    hj_framework_generate_entity_icons($filehandler, $filehandler);
                } else {
                    if (elgg_instanceof($entity)) {
                        hj_framework_generate_entity_icons($entity, $filehandler);
                    }
                }
                // the settings tell us not to keep the original image file, so downsizing to master
                if (!HYPEFRAMEWORK_FILES_KEEP_ORIGINALS) {
                    $icon_sizes = hj_framework_get_thumb_sizes($subtype);
                    $values = $icon_sizes['master'];
                    $master = get_resized_image_from_existing_file($filehandler->getFilenameOnFilestore(), $values['w'], $values['h'], $values['square'], 0, 0, 0, 0, $values['upscale']);
                    $filehandler->open('write');
                    $filehandler->write($master);
                    $filehandler->close();
                }
            }
            $return[$file['name']] = $filehandler->getGUID();
        } else {
            $return[$file['name']] = false;
        }
    }
    return $return;
}